FFmpeg
Loading...
Searching...
No Matches
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
29struct 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/*
38 * Prefer the codec framerate for avg_frame_rate computation.
39 */
40#define FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE (1 << 1)
41
42/**
43 * Automatically parse ID3v2 metadata
44 */
45#define FF_INFMT_FLAG_ID3V2_AUTO (1 << 2)
46
47/**
48 * Input format stream state
49 * The stream states to be used for FFInputFormat::read_set_state
50 */
55
56/**
57 * Command handling options
58 * Different options influencing the behaviour of
59 * the FFInputFormat::handle_command callback.
60 */
65
66typedef struct FFInputFormat {
67 /**
68 * The public AVInputFormat. See avformat.h for it.
69 */
71
72 /**
73 * Raw demuxers store their codec ID here.
74 */
76
77 /**
78 * Size of private data so that it can be allocated in the wrapper.
79 */
81
82 /**
83 * Internal flags. See FF_INFMT_FLAG_* above and FF_FMT_FLAG_* in internal.h.
84 */
86
87 /**
88 * Tell if a given file has a chance of being parsed as this format.
89 * The buffer provided is guaranteed to be AVPROBE_PADDING_SIZE bytes
90 * big so you do not have to check for that unless you need more.
91 *
92 * If the final score is not yet resolvable, the special return value
93 * AVERROR(EAGAIN) can be used to signal to the caller that probing must
94 * be tried again with more bytes.
95 *
96 * This is distinct from AVPROBE_SCORE_RETRY, which means "possibly mine,
97 * but if another format is more confident, let it have the file", whereas
98 * AVERROR(EAGAIN) means "possibly mine, don't let another format claim
99 * it until I've seen enough data to make a decision".
100 */
101 int (*read_probe)(const AVProbeData *);
102
103 /**
104 * Read the format header and initialize the AVFormatContext
105 * structure. Return 0 if OK. 'avformat_new_stream' should be
106 * called to create new streams.
107 */
108 int (*read_header)(struct AVFormatContext *);
109
110 /**
111 * Read one packet and put it in 'pkt'. pts and flags are also
112 * set. 'avformat_new_stream' can be called only if the flag
113 * AVFMTCTX_NOHEADER is used and only in the calling thread (not in a
114 * background thread).
115 * @return 0 on success, < 0 on error.
116 * Upon returning an error, pkt must be unreferenced by the caller.
117 */
119
120 /**
121 * Close the stream. The AVFormatContext and AVStreams are not
122 * freed by this function
123 */
124 int (*read_close)(struct AVFormatContext *);
125
126 /**
127 * Seek to a given timestamp relative to the frames in
128 * stream component stream_index.
129 * @param stream_index Must not be -1.
130 * @param flags Selects which direction should be preferred if no exact
131 * match is available.
132 * @return >= 0 on success (but not necessarily the new offset)
133 */
134 int (*read_seek)(struct AVFormatContext *,
135 int stream_index, int64_t timestamp, int flags);
136
137 /**
138 * Get the next timestamp in stream[stream_index].time_base units.
139 * @return the timestamp or AV_NOPTS_VALUE if an error occurred
140 */
141 int64_t (*read_timestamp)(struct AVFormatContext *s, int stream_index,
142 int64_t *pos, int64_t pos_limit);
143
144 /**
145 * Change the stream state - only meaningful if using a network-based format
146 * (RTSP).
147 */
150
151 /**
152 * Handle demuxer commands
153 * This callback is used to either send a command to a demuxer
154 * (FF_INFMT_COMMAND_SUBMIT), or to retrieve the reply for
155 * a previously sent command (FF_INFMT_COMMAND_GET_REPLY).
156 *
157 * Demuxers implementing this must return AVERROR(ENOTSUP)
158 * if the provided \p id is not handled by the demuxer.
159 * Demuxers should return AVERROR(EAGAIN) if they can handle
160 * a specific command but are not able to at the moment.
161 * When a reply is requested that is not available yet, the
162 * demuxer should also return AVERROR(EAGAIN).
163 *
164 * Depending on \p opt, the \p data will be either a pointer
165 * to the command payload structure for the specified ID, or
166 * a pointer to the buffer for the command reply for the
167 * specified ID.
168 *
169 * When a command is submitted, before a previous reply
170 * was requested from the demuxer, the demuxer should discard
171 * the old reply.
172 *
173 * @see avformat_send_command()
174 * @see avformat_receive_command_reply()
175 */
178 enum AVFormatCommandID id, void *data);
179
180 /**
181 * Seek to timestamp ts.
182 * Seeking will be done so that the point from which all active streams
183 * can be presented successfully will be closest to ts and within min/max_ts.
184 * Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
185 */
186 int (*read_seek2)(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
187
188 /**
189 * Returns device list with it properties.
190 * @see avdevice_list_devices() for more details.
191 */
192 int (*get_device_list)(struct AVFormatContext *s, struct AVDeviceInfoList *device_list);
194
195static inline const FFInputFormat *ffifmt(const AVInputFormat *fmt)
196{
197 return (const FFInputFormat*)fmt;
198}
199
200#define MAX_STD_TIMEBASES (30*12+30+3+6)
201typedef struct FFStreamInfo {
207 int64_t codec_info_duration; /**< Duration used for the probing budget. */
208 int64_t fps_duration; /**< Duration of non-corrupt FPS samples. */
211
212 /**
213 * 0 -> decoder has not been searched for yet.
214 * >0 -> decoder found
215 * <0 -> decoder with codec_id == -found_decoder has not been found
216 */
218
220
221 /**
222 * Those are used for average framerate estimation.
223 */
229
230/**
231 * Returned by demuxers to indicate that data was consumed but discarded
232 * (ignored streams or junk data). The framework will re-call the demuxer.
233 */
234#define FFERROR_REDO FFERRTAG('R','E','D','O')
235
236/**
237 * Read a transport packet from a media file.
238 *
239 * @param s media file handle
240 * @param pkt is filled
241 * @return 0 if OK, AVERROR_xxx on error
242 */
244
246
247/**
248 * Perform a binary search using av_index_search_timestamp() and
249 * FFInputFormat.read_timestamp().
250 *
251 * @param target_ts target timestamp in the time base of the given stream
252 * @param stream_index stream number
253 */
254int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
255 int64_t target_ts, int flags);
256
257/**
258 * Update cur_dts of all streams based on the given timestamp and AVStream.
259 *
260 * Stream ref_st unchanged, others set cur_dts in their native time base.
261 * Only needed for timestamp wrapping or if (dts not set and pts!=dts).
262 * @param timestamp new dts expressed in time_base of param ref_st
263 * @param ref_st reference stream giving time_base of param timestamp
264 */
265void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
266
267int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
268 int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
269
270/**
271 * Perform a binary search using read_timestamp().
272 *
273 * @param target_ts target timestamp in the time base of the given stream
274 * @param stream_index stream number
275 */
276int64_t ff_gen_search(AVFormatContext *s, int stream_index,
277 int64_t target_ts, int64_t pos_min,
278 int64_t pos_max, int64_t pos_limit,
279 int64_t ts_min, int64_t ts_max,
280 int flags, int64_t *ts_ret,
281 int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
282
283/**
284 * Internal version of av_index_search_timestamp
285 */
286int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
287 int64_t wanted_timestamp, int flags);
288
289/**
290 * Internal version of av_add_index_entry
291 */
292int ff_add_index_entry(AVIndexEntry **index_entries,
293 int *nb_index_entries,
294 unsigned int *index_entries_allocated_size,
295 int64_t pos, int64_t timestamp, int size, int distance, int flags);
296
298
299/**
300 * Ensure the index uses less memory than the maximum specified in
301 * AVFormatContext.max_index_size by discarding entries if it grows
302 * too large.
303 */
304void ff_reduce_index(AVFormatContext *s, int stream_index);
305
306/**
307 * add frame for rfps calculation.
308 *
309 * @param dts timestamp of the i-th frame
310 * @return 0 if OK, AVERROR_xxx on error
311 */
313
315
316/**
317 * Rescales a timestamp and the endpoints of an interval to which the temstamp
318 * belongs, from a timebase `tb_in` to a timebase `tb_out`.
319 *
320 * The upper (lower) bound of the output interval is rounded up (down) such that
321 * the output interval always falls within the input interval. The timestamp is
322 * rounded to the nearest integer and halfway cases away from zero, then
323 * clamped to the output interval.
324 *
325 * Useful to simplify the rescaling of the arguments of FFInputFormat::read_seek2()
326 *
327 * @param[in] tb_in Timebase of the input `min_ts`, `ts` and `max_ts`
328 * @param[in] tb_out Timebase of the output `min_ts`, `ts` and `max_ts`
329 * @param[in,out] min_ts Lower bound of the interval
330 * @param[in,out] ts Timestamp
331 * @param[in,out] max_ts Upper bound of the interval
332 */
333void ff_rescale_interval(AVRational tb_in, AVRational tb_out,
334 int64_t *min_ts, int64_t *ts, int64_t *max_ts);
335
337
338/**
339 * Add a new chapter.
340 *
341 * @param s media file handle
342 * @param id unique ID for this chapter
343 * @param start chapter start time in time_base units
344 * @param end chapter end time in time_base units
345 * @param title chapter title
346 *
347 * @return AVChapter or NULL on error
348 */
350 int64_t start, int64_t end, const char *title);
351
352/**
353 * Add an attached pic to an AVStream.
354 *
355 * @param st if set, the stream to add the attached pic to;
356 * if unset, a new stream will be added to s.
357 * @param pb AVIOContext to read data from if buf is unset.
358 * @param buf if set, it contains the data and size information to be used
359 * for the attached pic; if unset, data is read from pb.
360 * @param size the size of the data to read if buf is unset.
361 *
362 * @return 0 on success, < 0 on error. On error, this function removes
363 * the stream it has added (if any).
364 */
366 AVBufferRef **buf, int size);
367
368/**
369 * Add side data to a packet for changing parameters to the given values.
370 * Parameters set to 0 aren't included in the change.
371 */
373 uint64_t channel_layout, int32_t sample_rate,
375
376/**
377 * Generate standard extradata for AVC-Intra based on width/height and field
378 * order.
379 */
381
382/**
383 * Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end
384 * which is always set to 0 and fill it from pb.
385 *
386 * @param size size of extradata
387 * @return >= 0 if OK, AVERROR_xxx on error
388 */
389int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size);
390
391/**
392 * Find stream index based on format-specific stream ID
393 * @return stream index, or < 0 on error
394 */
395int ff_find_stream_index(const AVFormatContext *s, int id);
396
397/**
398 * Parse the gapless playback information Apple encoders store in an iTunSMPB
399 * tag. The tag is a list of hexadecimal fields; the ones of interest are
400 *
401 * <reserved> <priming> <remainder> <valid samples>
402 *
403 * The encoded frames hold the three sample counts back to back, the priming
404 * and remainder samples being encoder artifacts. The counts are returned as
405 * the tag stores them: a caller whose decoder adds a delay of its own, as the
406 * MP3 one does, shifts the valid samples later by that delay, adding it to
407 * the priming and subtracting it from the remainder.
408 *
409 * The counts are returned small enough to be added and rescaled without
410 * overflowing.
411 *
412 * @param value tag contents
413 * @param priming samples the encoder added at the start of the stream
414 * @param remainder samples the encoder added at the end of the stream
415 * @param samples number of valid samples between the two
416 * @return >= 0 if OK, AVERROR_INVALIDDATA if the tag could not be parsed or
417 * holds counts too large to be sample counts
418 */
419int ff_itunes_parse_smpb(const char *value, int64_t *priming,
420 int64_t *remainder, int64_t *samples);
421
423
424#endif /* AVFORMAT_DEMUX_H */
channels
Definition aptx.h:31
int32_t
Main libavformat public API header.
AVStreamParseType
Definition avformat.h:611
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
long long int64_t
Definition coverity.c:34
int ff_buffer_packet(AVFormatContext *s, AVPacket *pkt)
Definition demux.c:645
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:46
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:290
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:872
FFInputFormatStreamState
Input format stream state The stream states to be used for FFInputFormat::read_set_state.
Definition demux.h:51
@ FF_INFMT_STATE_PLAY
Definition demux.h:52
@ FF_INFMT_STATE_PAUSE
Definition demux.h:53
void ff_rfps_calculate(AVFormatContext *ic)
Definition demux.c:2471
int ff_itunes_parse_smpb(const char *value, int64_t *priming, int64_t *remainder, int64_t *samples)
Parse the gapless playback information Apple encoders store in an iTunSMPB tag.
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:64
#define MAX_STD_TIMEBASES
Definition demux.h:200
FFInputFormatCommandOption
Command handling options Different options influencing the behaviour of the FFInputFormat::handle_com...
Definition demux.h:61
@ FF_INFMT_COMMAND_GET_REPLY
Definition demux.h:63
@ FF_INFMT_COMMAND_SUBMIT
Definition demux.h:62
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:132
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t dts)
add frame for rfps calculation.
Definition demux.c:2410
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:360
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
Definition seek.c:175
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
static const FFInputFormat * ffifmt(const AVInputFormat *fmt)
Definition demux.h:195
int ff_find_stream_index(const AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition seek.c:835
void avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type)
Definition demux_utils.c:41
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:37
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:398
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.
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition demux.c:652
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...
int ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb, AVBufferRef **buf, int size)
Add an attached pic to an AVStream.
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:50
static AVPacket * pkt
enum AVCodecID id
Definition dts2pts.c:612
double value
Definition eval.c:102
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
AVFormatCommandID
Command IDs that can be sent to the demuxer.
Definition avformat.h:2649
enum MPFType type
Definition jpegmpfenc.c:55
const char data[16]
Definition mxf.c:155
static float distance(float x, float y, int band)
Utilities for rational number calculation.
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:281
unsigned int pos
Definition spdifenc.c:431
A reference to a data buffer.
Definition buffer.h:82
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
List of devices.
Definition avdevice.h:343
Format I/O context.
Definition avformat.h:1401
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:587
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:768
enum AVCodecID raw_codec_id
Raw demuxers store their codec ID here.
Definition demux.h:75
int priv_data_size
Size of private data so that it can be allocated in the wrapper.
Definition demux.h:80
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:134
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:141
int(* get_device_list)(struct AVFormatContext *s, struct AVDeviceInfoList *device_list)
Returns device list with it properties.
Definition demux.h:192
int flags_internal
Internal flags.
Definition demux.h:85
int(* read_set_state)(struct AVFormatContext *, enum FFInputFormatStreamState state)
Change the stream state - only meaningful if using a network-based format (RTSP).
Definition demux.h:148
int(* read_packet)(struct AVFormatContext *, AVPacket *pkt)
Read one packet and put it in 'pkt'.
Definition demux.h:118
int(* read_probe)(const AVProbeData *)
Tell if a given file has a chance of being parsed as this format.
Definition demux.h:101
int(* handle_command)(struct AVFormatContext *, enum FFInputFormatCommandOption opt, enum AVFormatCommandID id, void *data)
Handle demuxer commands This callback is used to either send a command to a demuxer (FF_INFMT_COMMAND...
Definition demux.h:176
AVInputFormat p
The public AVInputFormat.
Definition demux.h:70
int(* read_close)(struct AVFormatContext *)
Close the stream.
Definition demux.h:124
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:186
int(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition demux.h:108
int fps_last_dts_idx
Definition demux.h:227
int64_t codec_info_duration_fields
Definition demux.h:209
int found_decoder
0 -> decoder has not been searched for yet.
Definition demux.h:217
int64_t duration_gcd
Definition demux.h:203
int64_t fps_first_dts
Those are used for average framerate estimation.
Definition demux.h:224
int fps_first_dts_idx
Definition demux.h:225
int64_t fps_duration
Duration of non-corrupt FPS samples.
Definition demux.h:208
int frame_delay_evidence
Definition demux.h:210
double(* duration_error)[2][MAX_STD_TIMEBASES]
Definition demux.h:206
int64_t rfps_duration_sum
Definition demux.h:205
int64_t codec_info_duration
Duration used for the probing budget.
Definition demux.h:207
int64_t last_duration
Definition demux.h:219
int64_t last_dts
Definition demux.h:202
int64_t fps_last_dts
Definition demux.h:226
int duration_count
Definition demux.h:204
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
static struct @232240247175332074307271266321204261331170211232 state
int size