AVFormatContext

在此不再詳述,其中AVFormatContext是包含碼流參數較多的結構體。本文將會詳細分析一下該結構體裏每個變量的含義和作用。

  1.  * Format I/O context. 
  2.  * New fields can be added to the end with minor version bumps. 
  3.  * Removal, reordering and changes to existing fields require a major 
  4.  * version bump. 
  5.  * sizeof(AVFormatContext) must not be used outside libav*, use 
  6.  * avformat_alloc_context() to create an AVFormatContext. 
  7.  */  
  8. typedef struct AVFormatContext {  
  9.     /** 
  10.      * A class for logging and AVOptions. Set by avformat_alloc_context(). 
  11.      * Exports (de)muxer private options if they exist. 
  12.      */  
  13.     const AVClass *av_class;  
  14.   
  15.     /** 
  16.      * Can only be iformat or oformat, not both at the same time. 
  17.      * 
  18.      * decoding: set by avformat_open_input(). 
  19.      * encoding: set by the user. 
  20.      */  
  21.     struct AVInputFormat *iformat;  
  22.     struct AVOutputFormat *oformat;  
  23.   
  24.     /** 
  25.      * Format private data. This is an AVOptions-enabled struct 
  26.      * if and only if iformat/oformat.priv_class is not NULL. 
  27.      */  
  28.     void *priv_data;  
  29.   
  30.     /* 
  31.      * I/O context. 
  32.      * 
  33.      * decoding: either set by the user before avformat_open_input() (then 
  34.      * the user must close it manually) or set by avformat_open_input(). 
  35.      * encoding: set by the user. 
  36.      * 
  37.      * Do NOT set this field if AVFMT_NOFILE flag is set in 
  38.      * iformat/oformat.flags. In such a case, the (de)muxer will handle 
  39.      * I/O in some other way and this field will be NULL. 
  40.      */  
  41.     AVIOContext *pb;  
  42.   
  43.     /* stream info */  
  44.     int ctx_flags; /**< Format-specific flags, see AVFMTCTX_xx */  
  45.   
  46.     /** 
  47.      * A list of all streams in the file. New streams are created with 
  48.      * avformat_new_stream(). 
  49.      * 
  50.      * decoding: streams are created by libavformat in avformat_open_input(). 
  51.      * If AVFMTCTX_NOHEADER is set in ctx_flags, then new streams may also 
  52.      * appear in av_read_frame(). 
  53.      * encoding: streams are created by the user before avformat_write_header(). 
  54.      */  
  55.     unsigned int nb_streams;  
  56.     AVStream **streams;  
  57.   
  58.     char filename[1024]; /**< input or output filename */  
  59.   
  60.     /** 
  61.      * Decoding: position of the first frame of the component, in 
  62.      * AV_TIME_BASE fractional seconds. NEVER set this value directly: 
  63.      * It is deduced from the AVStream values. 
  64.      */  
  65.     int64_t start_time;  
  66.   
  67.     /** 
  68.      * Decoding: duration of the stream, in AV_TIME_BASE fractional 
  69.      * seconds. Only set this value if you know none of the individual stream 
  70.      * durations and also do not set any of them. This is deduced from the 
  71.      * AVStream values if not set. 
  72.      */  
  73.     int64_t duration;  
  74.   
  75.     /** 
  76.      * Decoding: total stream bitrate in bit/s, 0 if not 
  77.      * available. Never set it directly if the file_size and the 
  78.      * duration are known as FFmpeg can compute it automatically. 
  79.      */  
  80.     int bit_rate;  
  81.   
  82.     unsigned int packet_size;  
  83.     int max_delay;  
  84.   
  85.     int flags;  
  86. #define AVFMT_FLAG_GENPTS       0x0001 ///< Generate missing pts even if it requires parsing future frames.  
  87. #define AVFMT_FLAG_IGNIDX       0x0002 ///< Ignore index.  
  88. #define AVFMT_FLAG_NONBLOCK     0x0004 ///< Do not block when reading packets from input.  
  89. #define AVFMT_FLAG_IGNDTS       0x0008 ///< Ignore DTS on frames that contain both DTS & PTS  
  90. #define AVFMT_FLAG_NOFILLIN     0x0010 ///< Do not infer any values from other values, just return what is stored in the container  
  91. #define AVFMT_FLAG_NOPARSE      0x0020 ///< Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled  
  92. #define AVFMT_FLAG_CUSTOM_IO    0x0080 ///< The caller has supplied a custom AVIOContext, don't avio_close() it.  
  93. #define AVFMT_FLAG_DISCARD_CORRUPT  0x0100 ///< Discard frames marked corrupted  
  94. #define AVFMT_FLAG_MP4A_LATM    0x8000 ///< Enable RTP MP4A-LATM payload  
  95. #define AVFMT_FLAG_SORT_DTS    0x10000 ///< try to interleave outputted packets by dts (using this flag can slow demuxing down)  
  96. #define AVFMT_FLAG_PRIV_OPT    0x20000 ///< Enable use of private options by delaying codec open (this could be made default once all code is converted)  
  97. #define AVFMT_FLAG_KEEP_SIDE_DATA 0x40000 ///< Dont merge side data but keep it separate.  
  98.   
  99.     /** 
  100.      * decoding: size of data to probe; encoding: unused. 
  101.      */  
  102.     unsigned int probesize;  
  103.   
  104.     /** 
  105.      * decoding: maximum time (in AV_TIME_BASE units) during which the input should 
  106.      * be analyzed in avformat_find_stream_info(). 
  107.      */  
  108.     int max_analyze_duration;  
  109.   
  110.     const uint8_t *key;  
  111.     int keylen;  
  112.   
  113.     unsigned int nb_programs;  
  114.     AVProgram **programs;  
  115.   
  116.     /** 
  117.      * Forced video codec_id. 
  118.      * Demuxing: Set by user. 
  119.      */  
  120.     enum CodecID video_codec_id;  
  121.   
  122.     /** 
  123.      * Forced audio codec_id. 
  124.      * Demuxing: Set by user. 
  125.      */  
  126.     enum CodecID audio_codec_id;  
  127.   
  128.     /** 
  129.      * Forced subtitle codec_id. 
  130.      * Demuxing: Set by user. 
  131.      */  
  132.     enum CodecID subtitle_codec_id;  
  133.   
  134.     /** 
  135.      * Maximum amount of memory in bytes to use for the index of each stream. 
  136.      * If the index exceeds this size, entries will be discarded as 
  137.      * needed to maintain a smaller size. This can lead to slower or less 
  138.      * accurate seeking (depends on demuxer). 
  139.      * Demuxers for which a full in-memory index is mandatory will ignore 
  140.      * this. 
  141.      * muxing  : unused 
  142.      * demuxing: set by user 
  143.      */  
  144.     unsigned int max_index_size;  
  145.   
  146.     /** 
  147.      * Maximum amount of memory in bytes to use for buffering frames 
  148.      * obtained from realtime capture devices. 
  149.      */  
  150.     unsigned int max_picture_buffer;  
  151.   
  152.     unsigned int nb_chapters;  
  153.     AVChapter **chapters;  
  154.   
  155.     AVDictionary *metadata;  
  156.   
  157.     /** 
  158.      * Start time of the stream in real world time, in microseconds 
  159.      * since the unix epoch (00:00 1st January 1970). That is, pts=0 
  160.      * in the stream was captured at this real world time. 
  161.      * - encoding: Set by user. 
  162.      * - decoding: Unused. 
  163.      */  
  164.     int64_t start_time_realtime;  
  165.   
  166.     /** 
  167.      * decoding: number of frames used to probe fps 
  168.      */  
  169.     int fps_probe_size;  
  170.   
  171.     /** 
  172.      * Error recognition; higher values will detect more errors but may 
  173.      * misdetect some more or less valid parts as errors. 
  174.      * - encoding: unused 
  175.      * - decoding: Set by user. 
  176.      */  
  177.     int error_recognition;  
  178.   
  179.     /** 
  180.      * Custom interrupt callbacks for the I/O layer. 
  181.      * 
  182.      * decoding: set by the user before avformat_open_input(). 
  183.      * encoding: set by the user before avformat_write_header() 
  184.      * (mainly useful for AVFMT_NOFILE formats). The callback 
  185.      * should also be passed to avio_open2() if it's used to 
  186.      * open the file. 
  187.      */  
  188.     AVIOInterruptCB interrupt_callback;  
  189.   
  190.     /** 
  191.      * Flags to enable debugging. 
  192.      */  
  193.     int debug;  
  194. #define FF_FDEBUG_TS        0x0001  
  195.   
  196.     /** 
  197.      * Transport stream id. 
  198.      * This will be moved into demuxer private options. Thus no API/ABI compatibility 
  199.      */  
  200.     int ts_id;  
  201.   
  202.     /** 
  203.      * Audio preload in microseconds. 
  204.      * Note, not all formats support this and unpredictable things may happen if it is used when not supported. 
  205.      * - encoding: Set by user via AVOptions (NO direct access) 
  206.      * - decoding: unused 
  207.      */  
  208.     int audio_preload;  
  209.   
  210.     /** 
  211.      * Max chunk time in microseconds. 
  212.      * Note, not all formats support this and unpredictable things may happen if it is used when not supported. 
  213.      * - encoding: Set by user via AVOptions (NO direct access) 
  214.      * - decoding: unused 
  215.      */  
  216.     int max_chunk_duration;  
  217.   
  218.     /** 
  219.      * Max chunk size in bytes 
  220.      * Note, not all formats support this and unpredictable things may happen if it is used when not supported. 
  221.      * - encoding: Set by user via AVOptions (NO direct access) 
  222.      * - decoding: unused 
  223.      */  
  224.     int max_chunk_size;  
  225.   
  226.     /***************************************************************** 
  227.      * All fields below this line are not part of the public API. They 
  228.      * may not be used outside of libavformat and can be changed and 
  229.      * removed at will. 
  230.      * New public fields should be added right above. 
  231.      ***************************************************************** 
  232.      */  
  233.   
  234.     /** 
  235.      * This buffer is only needed when packets were already buffered but 
  236.      * not decoded, for example to get the codec parameters in MPEG 
  237.      * streams. 
  238.      */  
  239.     struct AVPacketList *packet_buffer;  
  240.     struct AVPacketList *packet_buffer_end;  
  241.   
  242.     /* av_seek_frame() support */  
  243.     int64_t data_offset; /**< offset of the first packet */  
  244.   
  245.     /** 
  246.      * Raw packets from the demuxer, prior to parsing and decoding. 
  247.      * This buffer is used for buffering packets until the codec can 
  248.      * be identified, as parsing cannot be done without knowing the 
  249.      * codec. 
  250.      */  
  251.     struct AVPacketList *raw_packet_buffer;  
  252.     struct AVPacketList *raw_packet_buffer_end;  
  253.     /** 
  254.      * Packets split by the parser get queued here. 
  255.      */  
  256.     struct AVPacketList *parse_queue;  
  257.     struct AVPacketList *parse_queue_end;  
  258.     /** 
  259.      * Remaining size available for raw_packet_buffer, in bytes. 
  260.      */  
  261. #define RAW_PACKET_BUFFER_SIZE 2500000  
  262.     int raw_packet_buffer_remaining_size;  
  263. } AVFormatContext;  

在使用FFMPEG進行開發的時候,AVFormatContext是一個貫穿始終的數據結構,很多函數都要用到它作爲參數。它是FFMPEG解封裝(flv,mp4,rmvb,avi)功能的結構體。下面看幾個主要變量的作用(在這裏考慮解碼的情況):


struct AVInputFormat *iformat:輸入數據的封裝格式

AVIOContext *pb:輸入數據的緩存

unsigned int nb_streams:視音頻流的個數

AVStream **streams:視音頻流

char filename[1024]:文件名

int64_t duration:時長(單位:微秒us,轉換爲秒需要除以1000000)

int bit_rate:比特率(單位bps,轉換爲kbps需要除以1000)

AVDictionary *metadata:元數據


視頻的時長可以轉換成HH:MM:SS的形式,示例代碼如下:

[cpp] view plain copy
  1. AVFormatContext *pFormatCtx;  
  2. CString timelong;  
  3. ...  
  4. //duration是以微秒爲單位  
  5. //轉換成hh:mm:ss形式  
  6. int tns, thh, tmm, tss;  
  7. tns  = (pFormatCtx->duration)/1000000;  
  8. thh  = tns / 3600;  
  9. tmm  = (tns % 3600) / 60;  
  10. tss  = (tns % 60);  
  11. timelong.Format("%02d:%02d:%02d",thh,tmm,tss);  


視頻的原數據(metadata)信息可以通過AVDictionary獲取。元數據存儲在AVDictionaryEntry結構體中,如下所示

[cpp] view plain copy
  1. typedef struct AVDictionaryEntry {  
  2.     char *key;  
  3.     char *value;  
  4. } AVDictionaryEntry;  
每一條元數據分爲key和value兩個屬性。

在ffmpeg中通過av_dict_get()函數獲得視頻的原數據。

下列代碼顯示了獲取元數據並存入meta字符串變量的過程,注意每一條key和value之間有一個"\t:",value之後有一個"\r\n"

[cpp] view plain copy
  1. //MetaData------------------------------------------------------------  
  2. //從AVDictionary獲得  
  3. //需要用到AVDictionaryEntry對象  
  4. //CString author,copyright,description;  
  5. CString meta=NULL,key,value;  
  6. AVDictionaryEntry *m = NULL;  
  7. //不用一個一個找出來  
  8. /*  m=av_dict_get(pFormatCtx->metadata,"author",m,0); 
  9. author.Format("作者:%s",m->value); 
  10. m=av_dict_get(pFormatCtx->metadata,"copyright",m,0); 
  11. copyright.Format("版權:%s",m->value); 
  12. m=av_dict_get(pFormatCtx->metadata,"description",m,0); 
  13. description.Format("描述:%s",m->value); 
  14. */  
  15. //使用循環讀出  
  16. //(需要讀取的數據,字段名稱,前一條字段(循環時使用),參數)  
  17. while(m=av_dict_get(pFormatCtx->metadata,"",m,AV_DICT_IGNORE_SUFFIX)){  
  18.     key.Format(m->key);  
  19.     value.Format(m->value);  
  20.     meta+=key+"\t:"+value+"\r\n" ;  
  21. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章