xref: /third_party/ffmpeg/libavformat/demux.h (revision cabdff1a)
1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * copyright (c) 2001 Fabrice Bellard
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * This file is part of FFmpeg.
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
7cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
8cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
9cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
10cabdff1aSopenharmony_ci *
11cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
12cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
13cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14cabdff1aSopenharmony_ci * Lesser General Public License for more details.
15cabdff1aSopenharmony_ci *
16cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
17cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
18cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19cabdff1aSopenharmony_ci */
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci#ifndef AVFORMAT_DEMUX_H
22cabdff1aSopenharmony_ci#define AVFORMAT_DEMUX_H
23cabdff1aSopenharmony_ci
24cabdff1aSopenharmony_ci#include <stdint.h>
25cabdff1aSopenharmony_ci#include "libavutil/rational.h"
26cabdff1aSopenharmony_ci#include "libavcodec/packet.h"
27cabdff1aSopenharmony_ci#include "avformat.h"
28cabdff1aSopenharmony_ci
29cabdff1aSopenharmony_ci#define MAX_STD_TIMEBASES (30*12+30+3+6)
30cabdff1aSopenharmony_citypedef struct FFStreamInfo {
31cabdff1aSopenharmony_ci    int64_t last_dts;
32cabdff1aSopenharmony_ci    int64_t duration_gcd;
33cabdff1aSopenharmony_ci    int duration_count;
34cabdff1aSopenharmony_ci    int64_t rfps_duration_sum;
35cabdff1aSopenharmony_ci    double (*duration_error)[2][MAX_STD_TIMEBASES];
36cabdff1aSopenharmony_ci    int64_t codec_info_duration;
37cabdff1aSopenharmony_ci    int64_t codec_info_duration_fields;
38cabdff1aSopenharmony_ci    int frame_delay_evidence;
39cabdff1aSopenharmony_ci
40cabdff1aSopenharmony_ci    /**
41cabdff1aSopenharmony_ci     * 0  -> decoder has not been searched for yet.
42cabdff1aSopenharmony_ci     * >0 -> decoder found
43cabdff1aSopenharmony_ci     * <0 -> decoder with codec_id == -found_decoder has not been found
44cabdff1aSopenharmony_ci     */
45cabdff1aSopenharmony_ci    int found_decoder;
46cabdff1aSopenharmony_ci
47cabdff1aSopenharmony_ci    int64_t last_duration;
48cabdff1aSopenharmony_ci
49cabdff1aSopenharmony_ci    /**
50cabdff1aSopenharmony_ci     * Those are used for average framerate estimation.
51cabdff1aSopenharmony_ci     */
52cabdff1aSopenharmony_ci    int64_t fps_first_dts;
53cabdff1aSopenharmony_ci    int     fps_first_dts_idx;
54cabdff1aSopenharmony_ci    int64_t fps_last_dts;
55cabdff1aSopenharmony_ci    int     fps_last_dts_idx;
56cabdff1aSopenharmony_ci} FFStreamInfo;
57cabdff1aSopenharmony_ci
58cabdff1aSopenharmony_ci/**
59cabdff1aSopenharmony_ci * Returned by demuxers to indicate that data was consumed but discarded
60cabdff1aSopenharmony_ci * (ignored streams or junk data). The framework will re-call the demuxer.
61cabdff1aSopenharmony_ci */
62cabdff1aSopenharmony_ci#define FFERROR_REDO FFERRTAG('R','E','D','O')
63cabdff1aSopenharmony_ci
64cabdff1aSopenharmony_ci#define RELATIVE_TS_BASE (INT64_MAX - (1LL << 48))
65cabdff1aSopenharmony_ci
66cabdff1aSopenharmony_cistatic av_always_inline int is_relative(int64_t ts)
67cabdff1aSopenharmony_ci{
68cabdff1aSopenharmony_ci    return ts > (RELATIVE_TS_BASE - (1LL << 48));
69cabdff1aSopenharmony_ci}
70cabdff1aSopenharmony_ci
71cabdff1aSopenharmony_ci/**
72cabdff1aSopenharmony_ci * Wrap a given time stamp, if there is an indication for an overflow
73cabdff1aSopenharmony_ci *
74cabdff1aSopenharmony_ci * @param st stream
75cabdff1aSopenharmony_ci * @param timestamp the time stamp to wrap
76cabdff1aSopenharmony_ci * @return resulting time stamp
77cabdff1aSopenharmony_ci */
78cabdff1aSopenharmony_ciint64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp);
79cabdff1aSopenharmony_ci
80cabdff1aSopenharmony_ci/**
81cabdff1aSopenharmony_ci * Read a transport packet from a media file.
82cabdff1aSopenharmony_ci *
83cabdff1aSopenharmony_ci * @param s media file handle
84cabdff1aSopenharmony_ci * @param pkt is filled
85cabdff1aSopenharmony_ci * @return 0 if OK, AVERROR_xxx on error
86cabdff1aSopenharmony_ci */
87cabdff1aSopenharmony_ciint ff_read_packet(AVFormatContext *s, AVPacket *pkt);
88cabdff1aSopenharmony_ci
89cabdff1aSopenharmony_civoid ff_read_frame_flush(AVFormatContext *s);
90cabdff1aSopenharmony_ci
91cabdff1aSopenharmony_ci/**
92cabdff1aSopenharmony_ci * Perform a binary search using av_index_search_timestamp() and
93cabdff1aSopenharmony_ci * AVInputFormat.read_timestamp().
94cabdff1aSopenharmony_ci *
95cabdff1aSopenharmony_ci * @param target_ts target timestamp in the time base of the given stream
96cabdff1aSopenharmony_ci * @param stream_index stream number
97cabdff1aSopenharmony_ci */
98cabdff1aSopenharmony_ciint ff_seek_frame_binary(AVFormatContext *s, int stream_index,
99cabdff1aSopenharmony_ci                         int64_t target_ts, int flags);
100cabdff1aSopenharmony_ci
101cabdff1aSopenharmony_ci/**
102cabdff1aSopenharmony_ci * Update cur_dts of all streams based on the given timestamp and AVStream.
103cabdff1aSopenharmony_ci *
104cabdff1aSopenharmony_ci * Stream ref_st unchanged, others set cur_dts in their native time base.
105cabdff1aSopenharmony_ci * Only needed for timestamp wrapping or if (dts not set and pts!=dts).
106cabdff1aSopenharmony_ci * @param timestamp new dts expressed in time_base of param ref_st
107cabdff1aSopenharmony_ci * @param ref_st reference stream giving time_base of param timestamp
108cabdff1aSopenharmony_ci */
109cabdff1aSopenharmony_civoid avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp);
110cabdff1aSopenharmony_ci
111cabdff1aSopenharmony_ciint ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
112cabdff1aSopenharmony_ci                    int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
113cabdff1aSopenharmony_ci
114cabdff1aSopenharmony_ci/**
115cabdff1aSopenharmony_ci * Perform a binary search using read_timestamp().
116cabdff1aSopenharmony_ci *
117cabdff1aSopenharmony_ci * @param target_ts target timestamp in the time base of the given stream
118cabdff1aSopenharmony_ci * @param stream_index stream number
119cabdff1aSopenharmony_ci */
120cabdff1aSopenharmony_ciint64_t ff_gen_search(AVFormatContext *s, int stream_index,
121cabdff1aSopenharmony_ci                      int64_t target_ts, int64_t pos_min,
122cabdff1aSopenharmony_ci                      int64_t pos_max, int64_t pos_limit,
123cabdff1aSopenharmony_ci                      int64_t ts_min, int64_t ts_max,
124cabdff1aSopenharmony_ci                      int flags, int64_t *ts_ret,
125cabdff1aSopenharmony_ci                      int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ));
126cabdff1aSopenharmony_ci
127cabdff1aSopenharmony_ci/**
128cabdff1aSopenharmony_ci * Internal version of av_index_search_timestamp
129cabdff1aSopenharmony_ci */
130cabdff1aSopenharmony_ciint ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
131cabdff1aSopenharmony_ci                              int64_t wanted_timestamp, int flags);
132cabdff1aSopenharmony_ci
133cabdff1aSopenharmony_ci/**
134cabdff1aSopenharmony_ci * Internal version of av_add_index_entry
135cabdff1aSopenharmony_ci */
136cabdff1aSopenharmony_ciint ff_add_index_entry(AVIndexEntry **index_entries,
137cabdff1aSopenharmony_ci                       int *nb_index_entries,
138cabdff1aSopenharmony_ci                       unsigned int *index_entries_allocated_size,
139cabdff1aSopenharmony_ci                       int64_t pos, int64_t timestamp, int size, int distance, int flags);
140cabdff1aSopenharmony_ci
141cabdff1aSopenharmony_civoid ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance);
142cabdff1aSopenharmony_ci
143cabdff1aSopenharmony_ci/**
144cabdff1aSopenharmony_ci * Ensure the index uses less memory than the maximum specified in
145cabdff1aSopenharmony_ci * AVFormatContext.max_index_size by discarding entries if it grows
146cabdff1aSopenharmony_ci * too large.
147cabdff1aSopenharmony_ci */
148cabdff1aSopenharmony_civoid ff_reduce_index(AVFormatContext *s, int stream_index);
149cabdff1aSopenharmony_ci
150cabdff1aSopenharmony_ci/**
151cabdff1aSopenharmony_ci * add frame for rfps calculation.
152cabdff1aSopenharmony_ci *
153cabdff1aSopenharmony_ci * @param dts timestamp of the i-th frame
154cabdff1aSopenharmony_ci * @return 0 if OK, AVERROR_xxx on error
155cabdff1aSopenharmony_ci */
156cabdff1aSopenharmony_ciint ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t dts);
157cabdff1aSopenharmony_ci
158cabdff1aSopenharmony_civoid ff_rfps_calculate(AVFormatContext *ic);
159cabdff1aSopenharmony_ci
160cabdff1aSopenharmony_ci/**
161cabdff1aSopenharmony_ci * Rescales a timestamp and the endpoints of an interval to which the temstamp
162cabdff1aSopenharmony_ci * belongs, from a timebase `tb_in` to a timebase `tb_out`.
163cabdff1aSopenharmony_ci *
164cabdff1aSopenharmony_ci * The upper (lower) bound of the output interval is rounded up (down) such that
165cabdff1aSopenharmony_ci * the output interval always falls within the intput interval. The timestamp is
166cabdff1aSopenharmony_ci * rounded to the nearest integer and halfway cases away from zero, and can
167cabdff1aSopenharmony_ci * therefore fall outside of the output interval.
168cabdff1aSopenharmony_ci *
169cabdff1aSopenharmony_ci * Useful to simplify the rescaling of the arguments of AVInputFormat::read_seek2()
170cabdff1aSopenharmony_ci *
171cabdff1aSopenharmony_ci * @param[in] tb_in      Timebase of the input `min_ts`, `ts` and `max_ts`
172cabdff1aSopenharmony_ci * @param[in] tb_out     Timebase of the ouput `min_ts`, `ts` and `max_ts`
173cabdff1aSopenharmony_ci * @param[in,out] min_ts Lower bound of the interval
174cabdff1aSopenharmony_ci * @param[in,out] ts     Timestamp
175cabdff1aSopenharmony_ci * @param[in,out] max_ts Upper bound of the interval
176cabdff1aSopenharmony_ci */
177cabdff1aSopenharmony_civoid ff_rescale_interval(AVRational tb_in, AVRational tb_out,
178cabdff1aSopenharmony_ci                         int64_t *min_ts, int64_t *ts, int64_t *max_ts);
179cabdff1aSopenharmony_ci
180cabdff1aSopenharmony_civoid avpriv_stream_set_need_parsing(AVStream *st, enum AVStreamParseType type);
181cabdff1aSopenharmony_ci
182cabdff1aSopenharmony_ci/**
183cabdff1aSopenharmony_ci * Add a new chapter.
184cabdff1aSopenharmony_ci *
185cabdff1aSopenharmony_ci * @param s media file handle
186cabdff1aSopenharmony_ci * @param id unique ID for this chapter
187cabdff1aSopenharmony_ci * @param start chapter start time in time_base units
188cabdff1aSopenharmony_ci * @param end chapter end time in time_base units
189cabdff1aSopenharmony_ci * @param title chapter title
190cabdff1aSopenharmony_ci *
191cabdff1aSopenharmony_ci * @return AVChapter or NULL on error
192cabdff1aSopenharmony_ci */
193cabdff1aSopenharmony_ciAVChapter *avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base,
194cabdff1aSopenharmony_ci                              int64_t start, int64_t end, const char *title);
195cabdff1aSopenharmony_ci
196cabdff1aSopenharmony_ci/**
197cabdff1aSopenharmony_ci * Add an attached pic to an AVStream.
198cabdff1aSopenharmony_ci *
199cabdff1aSopenharmony_ci * @param st   if set, the stream to add the attached pic to;
200cabdff1aSopenharmony_ci *             if unset, a new stream will be added to s.
201cabdff1aSopenharmony_ci * @param pb   AVIOContext to read data from if buf is unset.
202cabdff1aSopenharmony_ci * @param buf  if set, it contains the data and size information to be used
203cabdff1aSopenharmony_ci *             for the attached pic; if unset, data is read from pb.
204cabdff1aSopenharmony_ci * @param size the size of the data to read if buf is unset.
205cabdff1aSopenharmony_ci *
206cabdff1aSopenharmony_ci * @return 0 on success, < 0 on error. On error, this function removes
207cabdff1aSopenharmony_ci *         the stream it has added (if any).
208cabdff1aSopenharmony_ci */
209cabdff1aSopenharmony_ciint ff_add_attached_pic(AVFormatContext *s, AVStream *st, AVIOContext *pb,
210cabdff1aSopenharmony_ci                        AVBufferRef **buf, int size);
211cabdff1aSopenharmony_ci
212cabdff1aSopenharmony_ci/**
213cabdff1aSopenharmony_ci * Add side data to a packet for changing parameters to the given values.
214cabdff1aSopenharmony_ci * Parameters set to 0 aren't included in the change.
215cabdff1aSopenharmony_ci */
216cabdff1aSopenharmony_ciint ff_add_param_change(AVPacket *pkt, int32_t channels,
217cabdff1aSopenharmony_ci                        uint64_t channel_layout, int32_t sample_rate,
218cabdff1aSopenharmony_ci                        int32_t width, int32_t height);
219cabdff1aSopenharmony_ci
220cabdff1aSopenharmony_ci/**
221cabdff1aSopenharmony_ci * Generate standard extradata for AVC-Intra based on width/height and field
222cabdff1aSopenharmony_ci * order.
223cabdff1aSopenharmony_ci */
224cabdff1aSopenharmony_ciint ff_generate_avci_extradata(AVStream *st);
225cabdff1aSopenharmony_ci
226cabdff1aSopenharmony_ci/**
227cabdff1aSopenharmony_ci * Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end
228cabdff1aSopenharmony_ci * which is always set to 0 and fill it from pb.
229cabdff1aSopenharmony_ci *
230cabdff1aSopenharmony_ci * @param size size of extradata
231cabdff1aSopenharmony_ci * @return >= 0 if OK, AVERROR_xxx on error
232cabdff1aSopenharmony_ci */
233cabdff1aSopenharmony_ciint ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size);
234cabdff1aSopenharmony_ci
235cabdff1aSopenharmony_ci/**
236cabdff1aSopenharmony_ci * Find stream index based on format-specific stream ID
237cabdff1aSopenharmony_ci * @return stream index, or < 0 on error
238cabdff1aSopenharmony_ci */
239cabdff1aSopenharmony_ciint ff_find_stream_index(const AVFormatContext *s, int id);
240cabdff1aSopenharmony_ci
241cabdff1aSopenharmony_ci#endif /* AVFORMAT_DEMUX_H */
242