FFmpeg
demux.h
Go to the documentation of this file.
1 /*
2  * copyright (c) 2001 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef AVFORMAT_DEMUX_H
22 #define AVFORMAT_DEMUX_H
23 
24 #include <stdint.h>
25 #include "libavutil/rational.h"
26 #include "libavcodec/packet.h"
27 #include "avformat.h"
28 
29 struct AVDeviceInfoList;
30 
31 /**
32  * For an FFInputFormat with this flag set read_close() needs to be called
33  * by the caller upon read_header() failure.
34  */
35 #define FF_INFMT_FLAG_INIT_CLEANUP (1 << 0)
36 
37 typedef struct FFInputFormat {
38  /**
39  * The public AVInputFormat. See avformat.h for it.
40  */
42 
43  /**
44  * Raw demuxers store their codec ID here.
45  */
47 
48  /**
49  * Size of private data so that it can be allocated in the wrapper.
50  */
52 
53  /**
54  * Internal flags. See FF_INFMT_FLAG_* above and FF_FMT_FLAG_* in internal.h.
55  */
57 
58  /**
59  * Tell if a given file has a chance of being parsed as this format.
60  * The buffer provided is guaranteed to be AVPROBE_PADDING_SIZE bytes
61  * big so you do not have to check for that unless you need more.
62  */
63  int (*read_probe)(const AVProbeData *);
64 
65  /**
66  * Read the format header and initialize the AVFormatContext
67  * structure. Return 0 if OK. 'avformat_new_stream' should be
68  * called to create new streams.
69  */
70  int (*read_header)(struct AVFormatContext *);
71 
72  /**
73  * Read one packet and put it in 'pkt'. pts and flags are also
74  * set. 'avformat_new_stream' can be called only if the flag
75  * AVFMTCTX_NOHEADER is used and only in the calling thread (not in a
76  * background thread).
77  * @return 0 on success, < 0 on error.
78  * Upon returning an error, pkt must be unreferenced by the caller.
79  */
81 
82  /**
83  * Close the stream. The AVFormatContext and AVStreams are not
84  * freed by this function
85  */
86  int (*read_close)(struct AVFormatContext *);
87 
88  /**
89  * Seek to a given timestamp relative to the frames in
90  * stream component stream_index.
91  * @param stream_index Must not be -1.
92  * @param flags Selects which direction should be preferred if no exact
93  * match is available.
94  * @return >= 0 on success (but not necessarily the new offset)
95  */
96  int (*read_seek)(struct AVFormatContext *,
97  int stream_index, int64_t timestamp, int flags);
98 
99  /**
100  * Get the next timestamp in stream[stream_index].time_base units.
101  * @return the timestamp or AV_NOPTS_VALUE if an error occurred
102  */
103  int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
104  int64_t *pos, int64_t pos_limit);
105 
106  /**
107  * Start/resume playing - only meaningful if using a network-based format
108  * (RTSP).
109  */
110  int (*read_play)(struct AVFormatContext *);
111 
112  /**
113  * Pause playing - only meaningful if using a network-based format
114  * (RTSP).
115  */
116  int (*read_pause)(struct AVFormatContext *);
117 
118  /**
119  * Seek to timestamp ts.
120  * Seeking will be done so that the point from which all active streams
121  * can be presented successfully will be closest to ts and within min/max_ts.
122  * Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
123  */
124  int (*read_seek2)(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
125 
126  /**
127  * Returns device list with it properties.
128  * @see avdevice_list_devices() for more details.
129  */
130  int (*get_device_list)(struct AVFormatContext *s, struct AVDeviceInfoList *device_list);
131 } FFInputFormat;
132 
133 static inline const FFInputFormat *ffifmt(const AVInputFormat *fmt)
134 {
135  return (const FFInputFormat*)fmt;
136 }
137 
138 #define MAX_STD_TIMEBASES (30*12+30+3+6)
139 typedef struct FFStreamInfo {
148 
149  /**
150  * 0 -> decoder has not been searched for yet.
151  * >0 -> decoder found
152  * <0 -> decoder with codec_id == -found_decoder has not been found
153  */
155 
157 
158  /**
159  * Those are used for average framerate estimation.
160  */
165 } FFStreamInfo;
166 
167 /**
168  * Returned by demuxers to indicate that data was consumed but discarded
169  * (ignored streams or junk data). The framework will re-call the demuxer.
170  */
171 #define FFERROR_REDO FFERRTAG('R','E','D','O')
172 
173 #define RELATIVE_TS_BASE (INT64_MAX - (1LL << 48))
174 
176 {
177  return ts > (RELATIVE_TS_BASE - (1LL << 48));
178 }
179 
180 /**
181  * Wrap a given time stamp, if there is an indication for an overflow
182  *
183  * @param st stream
184  * @param timestamp the time stamp to wrap
185  * @return resulting time stamp
186  */
187 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp);
188 
189 /**
190  * Read a transport packet from a media file.
191  *
192  * @param s media file handle
193  * @param pkt is filled
194  * @return 0 if OK, AVERROR_xxx on error
195  */
197 
199 
200 /**
201  * Perform a binary search using av_index_search_timestamp() and
202  * FFInputFormat.read_timestamp().
203  *
204  * @param target_ts target timestamp in the time base of the given stream
205  * @param stream_index stream number
206  */
207 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
208  int64_t target_ts, int flags);
209 
210 /**
211  * Update cur_dts of all streams based on the given timestamp and AVStream.
212  *
213  * Stream ref_st unchanged, others set cur_dts in their native time base.
214  * Only needed for timestamp wrapping or if (dts not set and pts!=dts).
215  * @param timestamp new dts expressed in time_base of param ref_st
216  * @param ref_st reference stream giving time_base of param timestamp
217  */
218 void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
219 
220 int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
221  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
222 
223 /**
224  * Perform a binary search using read_timestamp().
225  *
226  * @param target_ts target timestamp in the time base of the given stream
227  * @param stream_index stream number
228  */
229 int64_t ff_gen_search(AVFormatContext *s, int stream_index,
230  int64_t target_ts, int64_t pos_min,
231  int64_t pos_max, int64_t pos_limit,
232  int64_t ts_min, int64_t ts_max,
233  int flags, int64_t *ts_ret,
234  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
235 
236 /**
237  * Internal version of av_index_search_timestamp
238  */
239 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
240  int64_t wanted_timestamp, int flags);
241 
242 /**
243  * Internal version of av_add_index_entry
244  */
245 int ff_add_index_entry(AVIndexEntry **index_entries,
246  int *nb_index_entries,
247  unsigned int *index_entries_allocated_size,
248  int64_t pos, int64_t timestamp, int size, int distance, int flags);
249 
251 
252 /**
253  * Ensure the index uses less memory than the maximum specified in
254  * AVFormatContext.max_index_size by discarding entries if it grows
255  * too large.
256  */
257 void ff_reduce_index(AVFormatContext *s, int stream_index);
258 
259 /**
260  * add frame for rfps calculation.
261  *
262  * @param dts timestamp of the i-th frame
263  * @return 0 if OK, AVERROR_xxx on error
264  */
266 
268 
269 /**
270  * Rescales a timestamp and the endpoints of an interval to which the temstamp
271  * belongs, from a timebase `tb_in` to a timebase `tb_out`.
272  *
273  * The upper (lower) bound of the output interval is rounded up (down) such that
274  * the output interval always falls within the intput interval. The timestamp is
275  * rounded to the nearest integer and halfway cases away from zero, and can
276  * therefore fall outside of the output interval.
277  *
278  * Useful to simplify the rescaling of the arguments of FFInputFormat::read_seek2()
279  *
280  * @param[in] tb_in Timebase of the input `min_ts`, `ts` and `max_ts`
281  * @param[in] tb_out Timebase of the output `min_ts`, `ts` and `max_ts`
282  * @param[in,out] min_ts Lower bound of the interval
283  * @param[in,out] ts Timestamp
284  * @param[in,out] max_ts Upper bound of the interval
285  */
286 void ff_rescale_interval(AVRational tb_in, AVRational tb_out,
287  int64_t *min_ts, int64_t *ts, int64_t *max_ts);
288 
290 
291 /**
292  * Add a new chapter.
293  *
294  * @param s media file handle
295  * @param id unique ID for this chapter
296  * @param start chapter start time in time_base units
297  * @param end chapter end time in time_base units
298  * @param title chapter title
299  *
300  * @return AVChapter or NULL on error
301  */
303  int64_t start, int64_t end, const char *title);
304 
305 /**
306  * Add an attached pic to an AVStream.
307  *
308  * @param st if set, the stream to add the attached pic to;
309  * if unset, a new stream will be added to s.
310  * @param pb AVIOContext to read data from if buf is unset.
311  * @param buf if set, it contains the data and size information to be used
312  * for the attached pic; if unset, data is read from pb.
313  * @param size the size of the data to read if buf is unset.
314  *
315  * @return 0 on success, < 0 on error. On error, this function removes
316  * the stream it has added (if any).
317  */
319  AVBufferRef **buf, int size);
320 
321 /**
322  * Add side data to a packet for changing parameters to the given values.
323  * Parameters set to 0 aren't included in the change.
324  */
326  uint64_t channel_layout, int32_t sample_rate,
328 
329 /**
330  * Generate standard extradata for AVC-Intra based on width/height and field
331  * order.
332  */
334 
335 /**
336  * Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end
337  * which is always set to 0 and fill it from pb.
338  *
339  * @param size size of extradata
340  * @return >= 0 if OK, AVERROR_xxx on error
341  */
342 int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size);
343 
344 /**
345  * Find stream index based on format-specific stream ID
346  * @return stream index, or < 0 on error
347  */
348 int ff_find_stream_index(const AVFormatContext *s, int id);
349 
351 
352 #endif /* AVFORMAT_DEMUX_H */
avpriv_new_chapter
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: demux_utils.c:42
ff_gen_search
int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, int64_t pos_min, int64_t pos_max, int64_t pos_limit, int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Perform a binary search using read_timestamp().
Definition: seek.c:397
FFStreamInfo::fps_last_dts
int64_t fps_last_dts
Definition: demux.h:163
ff_seek_frame_binary
int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Perform a binary search using av_index_search_timestamp() and FFInputFormat.read_timestamp().
Definition: seek.c:289
FFInputFormat::read_probe
int(* read_probe)(const AVProbeData *)
Tell if a given file has a chance of being parsed as this format.
Definition: demux.h:63
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
FFInputFormat::read_pause
int(* read_pause)(struct AVFormatContext *)
Pause playing - only meaningful if using a network-based format (RTSP).
Definition: demux.h:116
ff_find_last_ts
int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition: seek.c:359
rational.h
int64_t
long long int64_t
Definition: coverity.c:34
FFInputFormat::read_seek2
int(* read_seek2)(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
Definition: demux.h:124
ff_wrap_timestamp
int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
Wrap a given time stamp, if there is an indication for an overflow.
Definition: demux.c:67
FFInputFormat::priv_data_size
int priv_data_size
Size of private data so that it can be allocated in the wrapper.
Definition: demux.h:51
ff_read_frame_flush
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: seek.c:721
FFInputFormat::read_header
int(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition: demux.h:70
avpriv_update_cur_dts
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition: seek.c:36
AVIndexEntry
Definition: avformat.h:602
ff_generate_avci_extradata
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: demux_utils.c:200
ff_get_extradata
int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: demux_utils.c:335
MAX_STD_TIMEBASES
#define MAX_STD_TIMEBASES
Definition: demux.h:138
FFStreamInfo::fps_first_dts
int64_t fps_first_dts
Those are used for average framerate estimation.
Definition: demux.h:161
AVStreamParseType
AVStreamParseType
Definition: avformat.h:591
AVChapter
Definition: avformat.h:1259
FFInputFormat::read_play
int(* read_play)(struct AVFormatContext *)
Start/resume playing - only meaningful if using a network-based format (RTSP).
Definition: demux.h:110
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
pkt
AVPacket * pkt
Definition: movenc.c:60
AVInputFormat
Definition: avformat.h:548
ff_add_attached_pic
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
Definition: demux_utils.c:116
s
#define s(width, name)
Definition: cbs_vp9.c:198
FFInputFormat::flags_internal
int flags_internal
Internal flags.
Definition: demux.h:56
FFStreamInfo::last_dts
int64_t last_dts
Definition: demux.h:140
RELATIVE_TS_BASE
#define RELATIVE_TS_BASE
Definition: demux.h:173
FFInputFormat::get_device_list
int(* get_device_list)(struct AVFormatContext *s, struct AVDeviceInfoList *device_list)
Returns device list with it properties.
Definition: demux.h:130
channels
channels
Definition: aptx.h:31
FFInputFormat::read_seek
int(* read_seek)(struct AVFormatContext *, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to the frames in stream component stream_index.
Definition: demux.h:96
FFStreamInfo::found_decoder
int found_decoder
0 -> decoder has not been searched for yet.
Definition: demux.h:154
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
ff_index_search_timestamp
int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries, int64_t wanted_timestamp, int flags)
Internal version of av_index_search_timestamp.
Definition: seek.c:131
FFStreamInfo::codec_info_duration
int64_t codec_info_duration
Definition: demux.h:145
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
ff_rescale_interval
void ff_rescale_interval(AVRational tb_in, AVRational tb_out, int64_t *min_ts, int64_t *ts, int64_t *max_ts)
Rescales a timestamp and the endpoints of an interval to which the temstamp belongs,...
Definition: seek.c:764
double
double
Definition: af_crystalizer.c:132
FFStreamInfo::codec_info_duration_fields
int64_t codec_info_duration_fields
Definition: demux.h:146
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
FFInputFormat::read_packet
int(* read_packet)(struct AVFormatContext *, AVPacket *pkt)
Read one packet and put it in 'pkt'.
Definition: demux.h:80
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
FFStreamInfo
Definition: demux.h:139
height
#define height
Definition: dsp.h:85
ff_rfps_add_frame
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t dts)
add frame for rfps calculation.
Definition: demux.c:2286
FFStreamInfo::duration_count
int duration_count
Definition: demux.h:142
size
int size
Definition: twinvq_data.h:10344
ff_add_param_change
int ff_add_param_change(AVPacket *pkt, int32_t channels, uint64_t channel_layout, int32_t sample_rate, int32_t width, int32_t height)
Add side data to a packet for changing parameters to the given values.
Definition: demux_utils.c:151
ff_find_stream_index
int ff_find_stream_index(const AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: demux_utils.c:351
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
FFStreamInfo::last_duration
int64_t last_duration
Definition: demux.h:156
FFStreamInfo::rfps_duration_sum
int64_t rfps_duration_sum
Definition: demux.h:143
avpriv_stream_set_need_parsing
void avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type)
Definition: demux_utils.c:37
FFStreamInfo::duration_error
double(* duration_error)[2][MAX_STD_TIMEBASES]
Definition: demux.h:144
packet.h
ff_rfps_calculate
void ff_rfps_calculate(AVFormatContext *ic)
Definition: demux.c:2347
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFInputFormat::raw_codec_id
enum AVCodecID raw_codec_id
Raw demuxers store their codec ID here.
Definition: demux.h:46
FFStreamInfo::frame_delay_evidence
int frame_delay_evidence
Definition: demux.h:147
ff_add_index_entry
int ff_add_index_entry(AVIndexEntry **index_entries, int *nb_index_entries, unsigned int *index_entries_allocated_size, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Internal version of av_add_index_entry.
Definition: seek.c:63
FFInputFormat::read_timestamp
int64_t(* read_timestamp)(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)
Get the next timestamp in stream[stream_index].time_base units.
Definition: demux.h:103
AVStream
Stream structure.
Definition: avformat.h:748
AVDeviceInfoList
List of devices.
Definition: avdevice.h:343
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
FFInputFormat::read_close
int(* read_close)(struct AVFormatContext *)
Close the stream.
Definition: demux.h:86
ff_configure_buffers_for_index
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
Definition: seek.c:174
FFStreamInfo::fps_first_dts_idx
int fps_first_dts_idx
Definition: demux.h:162
FFStreamInfo::duration_gcd
int64_t duration_gcd
Definition: demux.h:141
ffifmt
static const FFInputFormat * ffifmt(const AVInputFormat *fmt)
Definition: demux.h:133
ff_read_packet
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: demux.c:616
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
ff_buffer_packet
int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
Definition: demux.c:609
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FFInputFormat
Definition: demux.h:37
int32_t
int32_t
Definition: audioconvert.c:56
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:231
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
FFStreamInfo::fps_last_dts_idx
int fps_last_dts_idx
Definition: demux.h:164
read_timestamp
static int64_t read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition: seek.c:280
is_relative
static av_always_inline int is_relative(int64_t ts)
Definition: demux.h:175
width
#define width
Definition: dsp.h:85
ff_reduce_index
void ff_reduce_index(AVFormatContext *s, int stream_index)
Ensure the index uses less memory than the maximum specified in AVFormatContext.max_index_size by dis...
Definition: seek.c:49