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