A packet contains one or more encoded frames which belongs to a single elementary stream. In the lavf API this process is represented by the avformat_open_input() function for opening a file, av_read_frame() for reading a single packet and finally avformat_close_input(), which does the cleanup.
const char *url = "in.mp3"; AVFormatContext *s = NULL; int ret = avformat_open_input(&s, url, NULL, NULL); if (ret < 0) abort();
In some cases you might want to preallocate an AVFormatContext yourself with avformat_alloc_context() and do some tweaking on it before passing it to avformat_open_input(). One such case is when you want to use custom functions for reading input data instead of lavf internal I/O layer. To do that, create your own AVIOContext with avio_alloc_context(), passing your reading callbacks to it. Then set the pb field of your AVFormatContext to newly created AVIOContext.
Since the format of the opened file is in general not known until after avformat_open_input() has returned, it is not possible to set demuxer private options on a preallocated context. Instead, the options should be passed to avformat_open_input() wrapped in an AVDictionary:
AVDictionary *options = NULL; av_dict_set(&options, "video_size", "640x480", 0); av_dict_set(&options, "pixel_format", "rgb24", 0); if (avformat_open_input(&s, url, NULL, &options) < 0) abort(); av_dict_free(&options);
AVDictionaryEntry *e; if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) { fprintf(stderr, "Option %s not recognized by the demuxer.\n", e->key); abort(); }
After you have finished reading the file, you must close it with avformat_close_input(). It will free everything associated with the file.
AVPacket.pts, AVPacket.dts and AVPacket.duration timing information will be set if known. They may also be unset (i.e. AV_NOPTS_VALUE for pts/dts, 0 for duration) if the stream does not provide them. The timing information will be in AVStream.time_base units, i.e. it has to be multiplied by the timebase to convert them to seconds.
The packet data belongs to the demuxer and is invalid after the next call to av_read_frame(). The user must free the packet with av_free_packet() before calling av_read_frame() again or closing the file.
Find the "best" stream in the file.
The best stream is determined according to various heuristics as the most likely to be what the user expects. If the decoder parameter is non-NULL, av_find_best_stream will find the default decoder for the stream's codec; streams for which no decoder can be found are ignored.
Definition at line 2970 of file utils.c.
Referenced by find_stream(), open_codec_context(), open_input_file(), and read_thread().
Find AVInputFormat based on the short name of the input format.
Definition at line 267 of file utils.c.
Referenced by ff_load_image(), http_receive_data(), movie_common_init(), opt_format(), opt_input_file(), parse_ffconfig(), sap_read_header(), and show_help_demuxer().
Find the programs which belong to a given stream.
Definition at line 2953 of file utils.c.
Referenced by av_find_best_stream().
Read packets of a media file to get stream information.
This is useful for file formats with no headers such as MPEG. This function also computes the real framerate in case of MPEG-2 repeat frame mode. The logical file position is not changed by this function; examined packets may be buffered for later processing.
Probe a bytestream to determine the input format.
Each time a probe returns with a score that is too low, the probe buffer size is increased and another attempt is made. When the maximum probe size is reached, the input format with the highest score is returned.
Definition at line 456 of file utils.c.
Referenced by hls_read_header(), and init_input().
Guess the file format.
Definition at line 394 of file utils.c.
Referenced by init_input().
Guess the file format.
Definition at line 383 of file utils.c.
Referenced by av_probe_input_buffer(), av_probe_input_format(), and read_gab2_sub().
Guess the file format.
Definition at line 343 of file utils.c.
Referenced by av_probe_input_format2(), and set_codec_from_probe_data().
Return the next frame of a stream.
This function returns what is stored in the file, and does not validate that what is there are valid frames for the decoder. It will split what is stored in the file into frames and return one for each call. It will not omit invalid data between valid frames so as to give the decoder the maximum information possible for decoding.
The returned packet is valid until the next av_read_frame() or until av_close_input_file() and must be freed with av_free_packet. For video, the packet contains exactly one frame. For audio, it contains an integer number of frames if each frame has a known fixed size (e.g. PCM or ADPCM data). If the audio frames have a variable size (e.g. MPEG audio), then it contains one frame.
pkt->pts, pkt->dts and pkt->duration are always set to correct values in AVStream.time_base units (and guessed if the format cannot provide them). pkt->pts can be AV_NOPTS_VALUE if the video format has B-frames, so it is better to rely on pkt->dts if you do not decompress the payload.
Definition at line 1426 of file utils.c.
Referenced by asf_read_pts(), ff_load_image(), get_input_packet(), hls_read_packet(), http_prepare_data(), main(), movie_push_frame(), mpc_read_seek(), mpegts_get_dts(), read_packets(), read_thread(), sap_fetch_packet(), search_hi_lo_keyframes(), seek_frame_generic(), and wv_read_seek().
This function is obsolete and should never be used. Use av_read_frame() instead.
Definition at line 810 of file utils.c.
Referenced by extract_mpeg4_header().
Pause a network-based stream (e.g.
RTSP stream).
Use av_read_play() to resume it.
Definition at line 3034 of file utils.c.
Referenced by read_thread().
Start playing a network-based stream (e.g.
RTSP stream) at the current position.
Definition at line 3025 of file utils.c.
Referenced by read_thread().
Seek to the keyframe at timestamp.
'timestamp' in 'stream_index'.
Definition at line 2020 of file utils.c.
Referenced by avformat_seek_file(), movie_common_init(), open_input_stream(), opt_input_file(), and rewind_file().
Close an opened input AVFormatContext.
Free it and all its contents and set *s to NULL.
Definition at line 3097 of file utils.c.
Referenced by av_close_input_file(), avi_read_close(), build_feed_streams(), build_file_streams(), close_connection(), close_input_file(), exit_program(), ff_load_image(), ff_rtsp_close_streams(), ff_wms_parse_sdp_a_line(), free_variant_list(), handle_file(), http_prepare_data(), http_receive_data(), main(), movie_uninit(), open_input_stream(), opt_input_file(), rdt_free_context(), read_ffserver_streams(), read_thread(), and sap_read_close().
Read packets of a media file to get stream information.
This is useful for file formats with no headers such as MPEG. This function also computes the real framerate in case of MPEG-2 repeat frame mode. The logical file position is not changed by this function; examined packets may be buffered for later processing.
Definition at line 2542 of file utils.c.
Referenced by av_find_stream_info(), build_file_streams(), handle_file(), hls_read_header(), main(), movie_common_init(), open_input_file(), open_input_stream(), opt_input_file(), and read_thread().
Open an input stream and read the header.
The codecs are not opened. The stream must be closed with av_close_input_file().
Definition at line 586 of file utils.c.
Referenced by build_feed_streams(), build_file_streams(), ff_load_image(), ff_wms_parse_sdp_a_line(), handle_file(), hls_read_header(), http_receive_data(), main(), movie_common_init(), open_input_file(), open_input_stream(), opt_input_file(), rdt_new_context(), read_ffserver_streams(), read_gab2_sub(), read_thread(), and sap_read_header().
Seek to timestamp ts.
Seeking will be done so that the point from which all active streams can be presented successfully will be closest to ts and within min/max_ts. Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
If flags contain AVSEEK_FLAG_BYTE, then all timestamps are in bytes and are the file position (this may not be supported by all demuxers). If flags contain AVSEEK_FLAG_FRAME, then all timestamps are in frames in the stream with stream_index (this may not be supported by all demuxers). Otherwise all timestamps are in units of the stream selected by stream_index or if stream_index is -1, in AV_TIME_BASE units. If flags contain AVSEEK_FLAG_ANY, then non-keyframes are treated as keyframes (this may not be supported by all demuxers).
Definition at line 2030 of file utils.c.
Referenced by main(), read_thread(), and seek_subtitle().