Demuxers read a media file and split it into chunks of data (packets). 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. More...
Demuxers read a media file and split it into chunks of data (packets). 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.
The minimum information required to open a file is its URL, which is passed to avformat_open_input(), as in the following code:
The above code attempts to allocate an AVFormatContext, open the specified file (autodetecting the format) and read the header, exporting the information stored there into s. Some formats do not have a header or do not store enough information there, so it is recommended that you call the avformat_find_stream_info() function which tries to read and decode a few frames to find missing information.
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:
This code passes the private options 'video_size' and 'pixel_format' to the demuxer. They would be necessary for e.g. the rawvideo demuxer, since it cannot know how to interpret raw video data otherwise. If the format turns out to be something different than raw video, those options will not be recognized by the demuxer and therefore will not be applied. Such unrecognized options are then returned in the options dictionary (recognized options are consumed). The calling program can handle such unrecognized options as it wishes, e.g.
After you have finished reading the file, you must close it with avformat_close_input(). It will free everything associated with the file.
Reading data from an opened AVFormatContext is done by repeatedly calling av_read_frame() on it. Each call, if successful, will return an AVPacket containing encoded data for one AVStream, identified by AVPacket.stream_index. This packet may be passed straight into the libavcodec decoding functions avcodec_send_packet() or avcodec_decode_subtitle2() if the caller wishes to decode the data.
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.
A packet returned by av_read_frame() is always reference-counted, i.e. AVPacket.buf is set and the user may keep it indefinitely. The packet must be freed with av_packet_unref() when it is no longer needed.
Find AVInputFormat based on the short name of the input format.
Definition at line 146 of file format.c.
Referenced by ff_alloc_input_device_context(), ff_load_image(), ff_wms_parse_sdp_a_line(), hls_read_header(), ifile_open(), init_subtitle_context(), movie_common_init(), opt_format(), sap_read_header(), and show_help_demuxer().
Guess the file format.
Definition at line 235 of file format.c.
Referenced by av_probe_input_buffer2(), av_probe_input_format(), init_input(), and read_gab2_sub().
Guess the file format.
Definition at line 156 of file format.c.
Referenced by av_probe_input_format2(), ff_img_read_packet(), and set_codec_from_probe_data().
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 253 of file format.c.
Referenced by av_probe_input_buffer(), and init_input().
Like av_probe_input_buffer2() but returns 0 on success.
Definition at line 345 of file format.c.
Referenced by hls_read_header(), and reopen_demux_for_component().
Open an input stream and read the header.
The codecs are not opened. The stream must be closed with avformat_close_input().
Definition at line 221 of file demux.c.
Referenced by ds_open(), dump_stream_meta(), dvdvideo_subdemux_open(), ff_load_image(), ff_wms_parse_sdp_a_line(), handle_file(), hls_read_header(), ifile_open(), init_subtitle_context(), LLVMFuzzerTestOneInput(), main(), movie_common_init(), open_file(), open_input_file(), open_track_resource_context(), rdt_init(), read_gab2_sub(), read_thread(), reopen_demux_for_component(), sap_read_header(), seek_test(), video_decode(), and video_decode_example().
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 2577 of file demux.c.
Referenced by dump_stream_meta(), ff_load_image(), handle_file(), hls_read_header(), ifile_open(), LLVMFuzzerTestOneInput(), main(), movie_common_init(), open_file(), open_input_file(), read_thread(), reopen_demux_for_component(), seek_test(), video_decode(), and video_decode_example().
Find the programs which belong to a given stream.
Definition at line 325 of file avformat.c.
Referenced by av_find_best_stream(), mpegts_push_data(), scte_data_cb(), stream_cycle_channel(), update_stream_timings(), and update_wrap_reference().
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.
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.
On success, the returned packet is reference-counted (pkt->buf is set) and valid indefinitely. The packet must be freed with av_packet_unref() when it is no longer needed. 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 1558 of file demux.c.
Referenced by activate(), asf_read_pts(), compute_crc_of_packets(), concat_read_packet(), dash_read_packet(), decode_audio_frame(), ds_run(), dump_stream_meta(), dvdvideo_read_packet(), ff_load_image(), hls_read_packet(), imf_read_packet(), input_thread(), LLVMFuzzerTestOneInput(), main(), mpc_read_seek(), mpegts_get_dts(), read_interval_packets(), read_subtitle_packet(), read_thread(), sap_fetch_packet(), seek_frame_generic(), video_decode(), and video_decode_example().
Seek to the keyframe at timestamp.
'timestamp' in 'stream_index'.
Definition at line 641 of file seek.c.
Referenced by avformat_seek_file(), compute_crc_of_packets(), dash_seek(), movie_common_init(), process_command(), and rewind_file().
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). If flags contain AVSEEK_FLAG_BACKWARD, it is ignored.
Definition at line 664 of file seek.c.
Referenced by av_seek_frame(), ifile_open(), main(), open_file(), open_track_resource_context(), read_interval_packets(), read_thread(), seek_subtitle(), seek_to_start(), and try_seek().
Discard all internally buffered data.
This can be useful when dealing with discontinuities in the byte stream. Generally works only with formats that can resync. This includes headerless formats like MPEG-TS/TS but should also work with NUT, Ogg and in a limited way AVI for example.
The set of streams, the detected duration, stream parameters and codecs do not change when calling this function. If you want a complete reset, it's better to open a new AVFormatContext.
This does not flush the AVIOContext (s->pb). If necessary, call avio_flush(s->pb) before calling this function.
Start playing a network-based stream (e.g.
RTSP stream) at the current position.
Definition at line 173 of file demux_utils.c.
Referenced by read_thread().
Pause a network-based stream (e.g.
RTSP stream).
Use av_read_play() to resume it.
Definition at line 182 of file demux_utils.c.
Referenced by read_thread().
Close an opened input AVFormatContext.
Free it and all its contents and set *s to NULL.
Definition at line 367 of file demux.c.
Referenced by avi_read_close(), close_demux_for_component(), close_input_file(), concat_read_close(), concat_seek(), ds_free(), dump_stream_meta(), dvdvideo_subdemux_close(), ff_load_image(), ff_rtsp_close_streams(), ff_wms_parse_sdp_a_line(), free_playlist_list(), free_representation(), get_resource_context_for_timestamp(), handle_file(), hls_read_seek(), ifile_close(), imf_seek(), imf_virtual_track_playback_context_deinit(), LLVMFuzzerTestOneInput(), main(), movie_uninit(), open_file(), open_input_file(), open_track_resource_context(), rdt_close_context(), read_subtitle_packet(), read_thread(), recheck_discard_flags(), sap_read_close(), seek_test(), stream_close(), video_decode(), and video_decode_example().