Mercurial > libavformat.hg
annotate utils.c @ 457:0f5aae2cd43f libavformat
vbr audio fix
author | michael |
---|---|
date | Thu, 06 May 2004 21:52:38 +0000 |
parents | a699cf5c703d |
children | b69898ffc92a |
rev | line source |
---|---|
0 | 1 /* |
2 * Various utilities for ffmpeg system | |
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 #include "avformat.h" | |
20 | |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
21 #undef NDEBUG |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
22 #include <assert.h> |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
23 |
0 | 24 AVInputFormat *first_iformat; |
25 AVOutputFormat *first_oformat; | |
20 | 26 AVImageFormat *first_image_format; |
0 | 27 |
28 void av_register_input_format(AVInputFormat *format) | |
29 { | |
30 AVInputFormat **p; | |
31 p = &first_iformat; | |
32 while (*p != NULL) p = &(*p)->next; | |
33 *p = format; | |
34 format->next = NULL; | |
35 } | |
36 | |
37 void av_register_output_format(AVOutputFormat *format) | |
38 { | |
39 AVOutputFormat **p; | |
40 p = &first_oformat; | |
41 while (*p != NULL) p = &(*p)->next; | |
42 *p = format; | |
43 format->next = NULL; | |
44 } | |
45 | |
46 int match_ext(const char *filename, const char *extensions) | |
47 { | |
48 const char *ext, *p; | |
49 char ext1[32], *q; | |
50 | |
453 | 51 if(!filename) |
52 return 0; | |
53 | |
0 | 54 ext = strrchr(filename, '.'); |
55 if (ext) { | |
56 ext++; | |
57 p = extensions; | |
58 for(;;) { | |
59 q = ext1; | |
60 while (*p != '\0' && *p != ',') | |
61 *q++ = *p++; | |
62 *q = '\0'; | |
63 if (!strcasecmp(ext1, ext)) | |
64 return 1; | |
65 if (*p == '\0') | |
66 break; | |
67 p++; | |
68 } | |
69 } | |
70 return 0; | |
71 } | |
72 | |
73 AVOutputFormat *guess_format(const char *short_name, const char *filename, | |
74 const char *mime_type) | |
75 { | |
76 AVOutputFormat *fmt, *fmt_found; | |
77 int score_max, score; | |
78 | |
20 | 79 /* specific test for image sequences */ |
21 | 80 if (!short_name && filename && |
81 filename_number_test(filename) >= 0 && | |
82 guess_image_format(filename)) { | |
20 | 83 return guess_format("image", NULL, NULL); |
84 } | |
85 | |
0 | 86 /* find the proper file type */ |
87 fmt_found = NULL; | |
88 score_max = 0; | |
89 fmt = first_oformat; | |
90 while (fmt != NULL) { | |
91 score = 0; | |
92 if (fmt->name && short_name && !strcmp(fmt->name, short_name)) | |
93 score += 100; | |
94 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) | |
95 score += 10; | |
96 if (filename && fmt->extensions && | |
97 match_ext(filename, fmt->extensions)) { | |
98 score += 5; | |
99 } | |
100 if (score > score_max) { | |
101 score_max = score; | |
102 fmt_found = fmt; | |
103 } | |
104 fmt = fmt->next; | |
105 } | |
106 return fmt_found; | |
107 } | |
108 | |
109 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename, | |
110 const char *mime_type) | |
111 { | |
112 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type); | |
113 | |
114 if (fmt) { | |
115 AVOutputFormat *stream_fmt; | |
116 char stream_format_name[64]; | |
117 | |
118 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name); | |
119 stream_fmt = guess_format(stream_format_name, NULL, NULL); | |
120 | |
121 if (stream_fmt) | |
122 fmt = stream_fmt; | |
123 } | |
124 | |
125 return fmt; | |
126 } | |
127 | |
128 AVInputFormat *av_find_input_format(const char *short_name) | |
129 { | |
130 AVInputFormat *fmt; | |
131 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) { | |
132 if (!strcmp(fmt->name, short_name)) | |
133 return fmt; | |
134 } | |
135 return NULL; | |
136 } | |
137 | |
138 /* memory handling */ | |
139 | |
140 /** | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
141 * Default packet destructor |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
142 */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
143 static void av_destruct_packet(AVPacket *pkt) |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
144 { |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
145 av_free(pkt->data); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
146 pkt->data = NULL; pkt->size = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
147 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
148 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
149 /** |
0 | 150 * Allocate the payload of a packet and intialized its fields to default values. |
151 * | |
152 * @param pkt packet | |
153 * @param size wanted payload size | |
154 * @return 0 if OK. AVERROR_xxx otherwise. | |
155 */ | |
156 int av_new_packet(AVPacket *pkt, int size) | |
157 { | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
158 void *data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
159 if (!data) |
0 | 160 return AVERROR_NOMEM; |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
161 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
0 | 162 |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
163 av_init_packet(pkt); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
164 pkt->data = data; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
165 pkt->size = size; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
166 pkt->destruct = av_destruct_packet; |
0 | 167 return 0; |
168 } | |
169 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
170 /* This is a hack - the packet memory allocation stuff is broken. The |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
171 packet is allocated if it was not really allocated */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
172 int av_dup_packet(AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
173 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
174 if (pkt->destruct != av_destruct_packet) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
175 uint8_t *data; |
330 | 176 /* we duplicate the packet and don't forget to put the padding |
177 again */ | |
178 data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE); | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
179 if (!data) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
180 return AVERROR_NOMEM; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
181 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
182 memcpy(data, pkt->data, pkt->size); |
330 | 183 memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
184 pkt->data = data; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
185 pkt->destruct = av_destruct_packet; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
186 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
187 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
188 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
189 |
0 | 190 /* fifo handling */ |
191 | |
192 int fifo_init(FifoBuffer *f, int size) | |
193 { | |
194 f->buffer = av_malloc(size); | |
195 if (!f->buffer) | |
196 return -1; | |
197 f->end = f->buffer + size; | |
198 f->wptr = f->rptr = f->buffer; | |
199 return 0; | |
200 } | |
201 | |
202 void fifo_free(FifoBuffer *f) | |
203 { | |
204 av_free(f->buffer); | |
205 } | |
206 | |
65 | 207 int fifo_size(FifoBuffer *f, uint8_t *rptr) |
0 | 208 { |
209 int size; | |
210 | |
211 if (f->wptr >= rptr) { | |
212 size = f->wptr - rptr; | |
213 } else { | |
214 size = (f->end - rptr) + (f->wptr - f->buffer); | |
215 } | |
216 return size; | |
217 } | |
218 | |
219 /* get data from the fifo (return -1 if not enough data) */ | |
65 | 220 int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr) |
0 | 221 { |
65 | 222 uint8_t *rptr = *rptr_ptr; |
0 | 223 int size, len; |
224 | |
225 if (f->wptr >= rptr) { | |
226 size = f->wptr - rptr; | |
227 } else { | |
228 size = (f->end - rptr) + (f->wptr - f->buffer); | |
229 } | |
230 | |
231 if (size < buf_size) | |
232 return -1; | |
233 while (buf_size > 0) { | |
234 len = f->end - rptr; | |
235 if (len > buf_size) | |
236 len = buf_size; | |
237 memcpy(buf, rptr, len); | |
238 buf += len; | |
239 rptr += len; | |
240 if (rptr >= f->end) | |
241 rptr = f->buffer; | |
242 buf_size -= len; | |
243 } | |
244 *rptr_ptr = rptr; | |
245 return 0; | |
246 } | |
247 | |
65 | 248 void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr) |
0 | 249 { |
250 int len; | |
65 | 251 uint8_t *wptr; |
0 | 252 wptr = *wptr_ptr; |
253 while (size > 0) { | |
254 len = f->end - wptr; | |
255 if (len > size) | |
256 len = size; | |
257 memcpy(wptr, buf, len); | |
258 wptr += len; | |
259 if (wptr >= f->end) | |
260 wptr = f->buffer; | |
261 buf += len; | |
262 size -= len; | |
263 } | |
264 *wptr_ptr = wptr; | |
265 } | |
266 | |
267 int filename_number_test(const char *filename) | |
268 { | |
269 char buf[1024]; | |
453 | 270 if(!filename) |
271 return -1; | |
0 | 272 return get_frame_filename(buf, sizeof(buf), filename, 1); |
273 } | |
274 | |
275 /* guess file format */ | |
276 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) | |
277 { | |
278 AVInputFormat *fmt1, *fmt; | |
279 int score, score_max; | |
280 | |
281 fmt = NULL; | |
282 score_max = 0; | |
283 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) { | |
284 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE)) | |
285 continue; | |
286 score = 0; | |
287 if (fmt1->read_probe) { | |
288 score = fmt1->read_probe(pd); | |
289 } else if (fmt1->extensions) { | |
290 if (match_ext(pd->filename, fmt1->extensions)) { | |
291 score = 50; | |
292 } | |
293 } | |
294 if (score > score_max) { | |
295 score_max = score; | |
296 fmt = fmt1; | |
297 } | |
298 } | |
299 return fmt; | |
300 } | |
301 | |
302 /************************************************************/ | |
303 /* input media file */ | |
304 | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
305 /** |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
306 * open a media file from an IO stream. 'fmt' must be specified. |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
307 */ |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
308 |
371 | 309 static const char* format_to_name(void* ptr) |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
310 { |
371 | 311 AVFormatContext* fc = (AVFormatContext*) ptr; |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
312 if(fc->iformat) return fc->iformat->name; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
313 else if(fc->oformat) return fc->oformat->name; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
314 else return "NULL"; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
315 } |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
316 |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
317 static const AVClass av_format_context_class = { "AVFormatContext", format_to_name }; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
318 |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
319 AVFormatContext *av_alloc_format_context(void) |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
320 { |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
321 AVFormatContext *ic; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
322 ic = av_mallocz(sizeof(AVFormatContext)); |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
323 if (!ic) return ic; |
371 | 324 ic->av_class = &av_format_context_class; |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
325 return ic; |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
326 } |
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
327 |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
328 int av_open_input_stream(AVFormatContext **ic_ptr, |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
329 ByteIOContext *pb, const char *filename, |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
330 AVInputFormat *fmt, AVFormatParameters *ap) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
331 { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
332 int err; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
333 AVFormatContext *ic; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
334 |
370
845f9de2c883
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
367
diff
changeset
|
335 ic = av_alloc_format_context(); |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
336 if (!ic) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
337 err = AVERROR_NOMEM; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
338 goto fail; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
339 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
340 ic->iformat = fmt; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
341 if (pb) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
342 ic->pb = *pb; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
343 ic->duration = AV_NOPTS_VALUE; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
344 ic->start_time = AV_NOPTS_VALUE; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
345 pstrcpy(ic->filename, sizeof(ic->filename), filename); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
346 |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
347 /* allocate private data */ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
348 if (fmt->priv_data_size > 0) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
349 ic->priv_data = av_mallocz(fmt->priv_data_size); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
350 if (!ic->priv_data) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
351 err = AVERROR_NOMEM; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
352 goto fail; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
353 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
354 } else { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
355 ic->priv_data = NULL; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
356 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
357 |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
358 /* default pts settings is MPEG like */ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
359 av_set_pts_info(ic, 33, 1, 90000); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
360 ic->last_pkt_pts = AV_NOPTS_VALUE; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
361 ic->last_pkt_dts = AV_NOPTS_VALUE; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
362 ic->last_pkt_stream_pts = AV_NOPTS_VALUE; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
363 ic->last_pkt_stream_dts = AV_NOPTS_VALUE; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
364 |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
365 err = ic->iformat->read_header(ic, ap); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
366 if (err < 0) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
367 goto fail; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
368 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
369 if (pb) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
370 ic->data_offset = url_ftell(&ic->pb); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
371 |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
372 *ic_ptr = ic; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
373 return 0; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
374 fail: |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
375 if (ic) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
376 av_freep(&ic->priv_data); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
377 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
378 av_free(ic); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
379 *ic_ptr = NULL; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
380 return err; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
381 } |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
382 |
0 | 383 #define PROBE_BUF_SIZE 2048 |
384 | |
385 /** | |
386 * Open a media file as input. The codec are not opened. Only the file | |
387 * header (if present) is read. | |
388 * | |
389 * @param ic_ptr the opened media file handle is put here | |
390 * @param filename filename to open. | |
391 * @param fmt if non NULL, force the file format to use | |
392 * @param buf_size optional buffer size (zero if default is OK) | |
393 * @param ap additionnal parameters needed when opening the file (NULL if default) | |
394 * @return 0 if OK. AVERROR_xxx otherwise. | |
395 */ | |
396 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, | |
397 AVInputFormat *fmt, | |
398 int buf_size, | |
399 AVFormatParameters *ap) | |
400 { | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
401 int err, must_open_file, file_opened; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
402 uint8_t buf[PROBE_BUF_SIZE]; |
0 | 403 AVProbeData probe_data, *pd = &probe_data; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
404 ByteIOContext pb1, *pb = &pb1; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
405 |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
406 file_opened = 0; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
407 pd->filename = ""; |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
408 if (filename) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
409 pd->filename = filename; |
0 | 410 pd->buf = buf; |
411 pd->buf_size = 0; | |
412 | |
413 if (!fmt) { | |
414 /* guess format if no file can be opened */ | |
415 fmt = av_probe_input_format(pd, 0); | |
416 } | |
417 | |
172 | 418 /* do not open file if the format does not need it. XXX: specific |
419 hack needed to handle RTSP/TCP */ | |
420 must_open_file = 1; | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
421 if (fmt && (fmt->flags & AVFMT_NOFILE)) { |
172 | 422 must_open_file = 0; |
423 } | |
424 | |
425 if (!fmt || must_open_file) { | |
20 | 426 /* if no file needed do not try to open one */ |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
427 if (url_fopen(pb, filename, URL_RDONLY) < 0) { |
0 | 428 err = AVERROR_IO; |
429 goto fail; | |
430 } | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
431 file_opened = 1; |
0 | 432 if (buf_size > 0) { |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
433 url_setbufsize(pb, buf_size); |
0 | 434 } |
435 if (!fmt) { | |
436 /* read probe data */ | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
437 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
438 url_fseek(pb, 0, SEEK_SET); |
0 | 439 } |
440 } | |
441 | |
442 /* guess file format */ | |
443 if (!fmt) { | |
444 fmt = av_probe_input_format(pd, 1); | |
445 } | |
446 | |
447 /* if still no format found, error */ | |
448 if (!fmt) { | |
449 err = AVERROR_NOFMT; | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
450 goto fail; |
0 | 451 } |
452 | |
453 /* XXX: suppress this hack for redirectors */ | |
22
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
454 #ifdef CONFIG_NETWORK |
0 | 455 if (fmt == &redir_demux) { |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
456 err = redir_open(ic_ptr, pb); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
457 url_fclose(pb); |
0 | 458 return err; |
459 } | |
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
9
diff
changeset
|
460 #endif |
0 | 461 |
20 | 462 /* check filename in case of an image number is expected */ |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
463 if (fmt->flags & AVFMT_NEEDNUMBER) { |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
464 if (filename_number_test(filename) < 0) { |
20 | 465 err = AVERROR_NUMEXPECTED; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
466 goto fail; |
20 | 467 } |
468 } | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
469 err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap); |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
470 if (err) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
471 goto fail; |
0 | 472 return 0; |
473 fail: | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
474 if (file_opened) |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
475 url_fclose(pb); |
0 | 476 *ic_ptr = NULL; |
477 return err; | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
478 |
0 | 479 } |
480 | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
481 /*******************************************************/ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
482 |
0 | 483 /** |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
484 * Read a transport packet from a media file. This function is |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
485 * absolete and should never be used. Use av_read_frame() instead. |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
486 * |
0 | 487 * @param s media file handle |
488 * @param pkt is filled | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
489 * @return 0 if OK. AVERROR_xxx if error. |
0 | 490 */ |
491 int av_read_packet(AVFormatContext *s, AVPacket *pkt) | |
492 { | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
493 return s->iformat->read_packet(s, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
494 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
495 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
496 /**********************************************************/ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
497 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
498 /* convert the packet time stamp units and handle wrapping. The |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
499 wrapping is handled by considering the next PTS/DTS as a delta to |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
500 the previous value. We handle the delta as a fraction to avoid any |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
501 rounding errors. */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
502 static inline int64_t convert_timestamp_units(AVFormatContext *s, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
503 int64_t *plast_pkt_pts, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
504 int *plast_pkt_pts_frac, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
505 int64_t *plast_pkt_stream_pts, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
506 int64_t pts) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
507 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
508 int64_t stream_pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
509 int64_t delta_pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
510 int shift, pts_frac; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
511 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
512 if (pts != AV_NOPTS_VALUE) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
513 stream_pts = pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
514 if (*plast_pkt_stream_pts != AV_NOPTS_VALUE) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
515 shift = 64 - s->pts_wrap_bits; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
516 delta_pts = ((stream_pts - *plast_pkt_stream_pts) << shift) >> shift; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
517 /* XXX: overflow possible but very unlikely as it is a delta */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
518 delta_pts = delta_pts * AV_TIME_BASE * s->pts_num; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
519 pts = *plast_pkt_pts + (delta_pts / s->pts_den); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
520 pts_frac = *plast_pkt_pts_frac + (delta_pts % s->pts_den); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
521 if (pts_frac >= s->pts_den) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
522 pts_frac -= s->pts_den; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
523 pts++; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
524 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
525 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
526 /* no previous pts, so no wrapping possible */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
527 pts = (int64_t)(((double)stream_pts * AV_TIME_BASE * s->pts_num) / |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
528 (double)s->pts_den); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
529 pts_frac = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
530 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
531 *plast_pkt_stream_pts = stream_pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
532 *plast_pkt_pts = pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
533 *plast_pkt_pts_frac = pts_frac; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
534 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
535 return pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
536 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
537 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
538 /* get the number of samples of an audio frame. Return (-1) if error */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
539 static int get_audio_frame_size(AVCodecContext *enc, int size) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
540 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
541 int frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
542 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
543 if (enc->frame_size <= 1) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
544 /* specific hack for pcm codecs because no frame size is |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
545 provided */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
546 switch(enc->codec_id) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
547 case CODEC_ID_PCM_S16LE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
548 case CODEC_ID_PCM_S16BE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
549 case CODEC_ID_PCM_U16LE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
550 case CODEC_ID_PCM_U16BE: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
551 if (enc->channels == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
552 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
553 frame_size = size / (2 * enc->channels); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
554 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
555 case CODEC_ID_PCM_S8: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
556 case CODEC_ID_PCM_U8: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
557 case CODEC_ID_PCM_MULAW: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
558 case CODEC_ID_PCM_ALAW: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
559 if (enc->channels == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
560 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
561 frame_size = size / (enc->channels); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
562 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
563 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
564 /* used for example by ADPCM codecs */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
565 if (enc->bit_rate == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
566 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
567 frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
568 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
569 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
570 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
571 frame_size = enc->frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
572 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
573 return frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
574 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
575 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
576 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
577 /* return the frame duration in seconds, return 0 if not available */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
578 static void compute_frame_duration(int *pnum, int *pden, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
579 AVFormatContext *s, AVStream *st, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
580 AVCodecParserContext *pc, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
581 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
582 int frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
583 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
584 *pnum = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
585 *pden = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
586 switch(st->codec.codec_type) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
587 case CODEC_TYPE_VIDEO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
588 *pnum = st->codec.frame_rate_base; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
589 *pden = st->codec.frame_rate; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
590 if (pc && pc->repeat_pict) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
591 *pden *= 2; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
592 *pnum = (*pnum) * (2 + pc->repeat_pict); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
593 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
594 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
595 case CODEC_TYPE_AUDIO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
596 frame_size = get_audio_frame_size(&st->codec, pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
597 if (frame_size < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
598 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
599 *pnum = frame_size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
600 *pden = st->codec.sample_rate; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
601 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
602 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
603 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
604 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
605 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
606 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
607 static void compute_pkt_fields(AVFormatContext *s, AVStream *st, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
608 AVCodecParserContext *pc, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
609 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
610 int num, den, presentation_delayed; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
611 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
612 if (pkt->duration == 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
613 compute_frame_duration(&num, &den, s, st, pc, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
614 if (den && num) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
615 pkt->duration = (num * (int64_t)AV_TIME_BASE) / den; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
616 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
617 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
618 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
619 /* do we have a video B frame ? */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
620 presentation_delayed = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
621 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
622 /* XXX: need has_b_frame, but cannot get it if the codec is |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
623 not initialized */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
624 if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
625 st->codec.codec_id == CODEC_ID_MPEG2VIDEO || |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
626 st->codec.codec_id == CODEC_ID_MPEG4 || |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
627 st->codec.codec_id == CODEC_ID_H264) && |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
628 pc && pc->pict_type != FF_B_TYPE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
629 presentation_delayed = 1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
630 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
631 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
632 /* interpolate PTS and DTS if they are not present */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
633 if (presentation_delayed) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
634 /* DTS = decompression time stamp */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
635 /* PTS = presentation time stamp */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
636 if (pkt->dts == AV_NOPTS_VALUE) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
637 pkt->dts = st->cur_dts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
638 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
639 st->cur_dts = pkt->dts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
640 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
641 /* this is tricky: the dts must be incremented by the duration |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
642 of the frame we are displaying, i.e. the last I or P frame */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
643 if (st->last_IP_duration == 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
644 st->cur_dts += pkt->duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
645 else |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
646 st->cur_dts += st->last_IP_duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
647 st->last_IP_duration = pkt->duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
648 /* cannot compute PTS if not present (we can compute it only |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
649 by knowing the futur */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
650 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
651 /* presentation is not delayed : PTS and DTS are the same */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
652 if (pkt->pts == AV_NOPTS_VALUE) { |
367
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
653 if (pkt->dts == AV_NOPTS_VALUE) { |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
654 pkt->pts = st->cur_dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
655 pkt->dts = st->cur_dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
656 } |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
657 else { |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
658 st->cur_dts = pkt->dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
659 pkt->pts = pkt->dts; |
3fca8e9142a4
avsync patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
354
diff
changeset
|
660 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
661 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
662 st->cur_dts = pkt->pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
663 pkt->dts = pkt->pts; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
664 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
665 st->cur_dts += pkt->duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
666 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
667 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
668 /* update flags */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
669 if (pc) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
670 pkt->flags = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
671 /* key frame computation */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
672 switch(st->codec.codec_type) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
673 case CODEC_TYPE_VIDEO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
674 if (pc->pict_type == FF_I_TYPE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
675 pkt->flags |= PKT_FLAG_KEY; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
676 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
677 case CODEC_TYPE_AUDIO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
678 pkt->flags |= PKT_FLAG_KEY; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
679 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
680 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
681 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
682 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
683 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
684 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
685 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
686 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
687 static void av_destruct_packet_nofree(AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
688 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
689 pkt->data = NULL; pkt->size = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
690 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
691 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
692 static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
693 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
694 AVStream *st; |
333 | 695 int len, ret, i; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
696 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
697 for(;;) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
698 /* select current input stream component */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
699 st = s->cur_st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
700 if (st) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
701 if (!st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
702 /* no parsing needed: we just output the packet as is */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
703 /* raw data support */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
704 *pkt = s->cur_pkt; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
705 compute_pkt_fields(s, st, NULL, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
706 s->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
707 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
708 } else if (s->cur_len > 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
709 len = av_parser_parse(st->parser, &st->codec, &pkt->data, &pkt->size, |
334
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
710 s->cur_ptr, s->cur_len, |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
711 s->cur_pkt.pts, s->cur_pkt.dts); |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
712 s->cur_pkt.pts = AV_NOPTS_VALUE; |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
713 s->cur_pkt.dts = AV_NOPTS_VALUE; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
714 /* increment read pointer */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
715 s->cur_ptr += len; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
716 s->cur_len -= len; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
717 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
718 /* return packet if any */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
719 if (pkt->size) { |
333 | 720 got_packet: |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
721 pkt->duration = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
722 pkt->stream_index = st->index; |
334
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
723 pkt->pts = st->parser->pts; |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
724 pkt->dts = st->parser->dts; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
725 pkt->destruct = av_destruct_packet_nofree; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
726 compute_pkt_fields(s, st, st->parser, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
727 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
728 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
729 } else { |
319 | 730 /* free packet */ |
731 av_free_packet(&s->cur_pkt); | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
732 s->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
733 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
734 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
735 /* read next packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
736 ret = av_read_packet(s, &s->cur_pkt); |
333 | 737 if (ret < 0) { |
738 if (ret == -EAGAIN) | |
739 return ret; | |
740 /* return the last frames, if any */ | |
741 for(i = 0; i < s->nb_streams; i++) { | |
742 st = s->streams[i]; | |
743 if (st->parser) { | |
744 av_parser_parse(st->parser, &st->codec, | |
745 &pkt->data, &pkt->size, | |
334
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
746 NULL, 0, |
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
747 AV_NOPTS_VALUE, AV_NOPTS_VALUE); |
333 | 748 if (pkt->size) |
749 goto got_packet; | |
750 } | |
751 } | |
752 /* no more packets: really terminates parsing */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
753 return ret; |
333 | 754 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
755 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
756 /* convert the packet time stamp units and handle wrapping */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
757 s->cur_pkt.pts = convert_timestamp_units(s, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
758 &s->last_pkt_pts, &s->last_pkt_pts_frac, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
759 &s->last_pkt_stream_pts, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
760 s->cur_pkt.pts); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
761 s->cur_pkt.dts = convert_timestamp_units(s, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
762 &s->last_pkt_dts, &s->last_pkt_dts_frac, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
763 &s->last_pkt_stream_dts, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
764 s->cur_pkt.dts); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
765 #if 0 |
334
7f089db11f9a
fixed incorrect PTS/DTS logic in MPEG video case (caused rare PTS glitches if start codes were between two PES packets)
bellard
parents:
333
diff
changeset
|
766 if (s->cur_pkt.stream_index == 0) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
767 if (s->cur_pkt.pts != AV_NOPTS_VALUE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
768 printf("PACKET pts=%0.3f\n", |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
769 (double)s->cur_pkt.pts / AV_TIME_BASE); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
770 if (s->cur_pkt.dts != AV_NOPTS_VALUE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
771 printf("PACKET dts=%0.3f\n", |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
772 (double)s->cur_pkt.dts / AV_TIME_BASE); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
773 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
774 #endif |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
775 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
776 /* duration field */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
777 if (s->cur_pkt.duration != 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
778 s->cur_pkt.duration = ((int64_t)s->cur_pkt.duration * AV_TIME_BASE * s->pts_num) / |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
779 s->pts_den; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
780 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
781 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
782 st = s->streams[s->cur_pkt.stream_index]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
783 s->cur_st = st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
784 s->cur_ptr = s->cur_pkt.data; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
785 s->cur_len = s->cur_pkt.size; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
786 if (st->need_parsing && !st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
787 st->parser = av_parser_init(st->codec.codec_id); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
788 if (!st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
789 /* no parser available : just output the raw packets */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
790 st->need_parsing = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
791 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
792 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
793 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
794 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
795 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
796 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
797 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
798 * Return the next frame of a stream. The returned packet is valid |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
799 * until the next av_read_frame() or until av_close_input_file() and |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
800 * must be freed with av_free_packet. For video, the packet contains |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
801 * exactly one frame. For audio, it contains an integer number of |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
802 * frames if each frame has a known fixed size (e.g. PCM or ADPCM |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
803 * data). If the audio frames have a variable size (e.g. MPEG audio), |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
804 * then it contains one frame. |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
805 * |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
806 * pkt->pts, pkt->dts and pkt->duration are always set to correct |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
807 * values in AV_TIME_BASE unit (and guessed if the format cannot |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
808 * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
809 * has B frames, so it is better to rely on pkt->dts if you do not |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
810 * decompress the payload. |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
811 * |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
812 * Return 0 if OK, < 0 if error or end of file. |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
813 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
814 int av_read_frame(AVFormatContext *s, AVPacket *pkt) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
815 { |
0 | 816 AVPacketList *pktl; |
817 | |
818 pktl = s->packet_buffer; | |
819 if (pktl) { | |
820 /* read packet from packet buffer, if there is data */ | |
821 *pkt = pktl->pkt; | |
822 s->packet_buffer = pktl->next; | |
823 av_free(pktl); | |
824 return 0; | |
825 } else { | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
826 return av_read_frame_internal(s, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
827 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
828 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
829 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
830 /* XXX: suppress the packet queue */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
831 static void flush_packet_queue(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
832 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
833 AVPacketList *pktl; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
834 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
835 for(;;) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
836 pktl = s->packet_buffer; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
837 if (!pktl) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
838 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
839 s->packet_buffer = pktl->next; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
840 av_free_packet(&pktl->pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
841 av_free(pktl); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
842 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
843 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
844 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
845 /*******************************************************/ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
846 /* seek support */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
847 |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
848 int av_find_default_stream_index(AVFormatContext *s) |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
849 { |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
850 int i; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
851 AVStream *st; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
852 |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
853 if (s->nb_streams <= 0) |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
854 return -1; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
855 for(i = 0; i < s->nb_streams; i++) { |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
856 st = s->streams[i]; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
857 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
858 return i; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
859 } |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
860 } |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
861 return 0; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
862 } |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
863 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
864 /* flush the frame reader */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
865 static void av_read_frame_flush(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
866 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
867 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
868 int i; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
869 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
870 flush_packet_queue(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
871 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
872 /* free previous packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
873 if (s->cur_st) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
874 if (s->cur_st->parser) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
875 av_free_packet(&s->cur_pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
876 s->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
877 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
878 /* fail safe */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
879 s->cur_ptr = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
880 s->cur_len = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
881 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
882 /* for each stream, reset read state */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
883 for(i = 0; i < s->nb_streams; i++) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
884 st = s->streams[i]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
885 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
886 if (st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
887 av_parser_close(st->parser); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
888 st->parser = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
889 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
890 st->cur_dts = 0; /* we set the current DTS to an unspecified origin */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
891 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
892 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
893 |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
894 /* add a index entry into a sorted list updateing if it is already there */ |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
895 int av_add_index_entry(AVStream *st, |
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
896 int64_t pos, int64_t timestamp, int distance, int flags) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
897 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
898 AVIndexEntry *entries, *ie; |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
899 int index; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
900 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
901 entries = av_fast_realloc(st->index_entries, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
902 &st->index_entries_allocated_size, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
903 (st->nb_index_entries + 1) * |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
904 sizeof(AVIndexEntry)); |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
905 st->index_entries= entries; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
906 |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
907 if(st->nb_index_entries){ |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
908 index= av_index_search_timestamp(st, timestamp); |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
909 ie= &entries[index]; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
910 |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
911 if(ie->timestamp != timestamp){ |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
912 if(ie->timestamp < timestamp){ |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
913 index++; //index points to next instead of previous entry, maybe nonexistant |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
914 ie= &st->index_entries[index]; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
915 }else |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
916 assert(index==0); |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
917 |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
918 if(index != st->nb_index_entries){ |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
919 assert(index < st->nb_index_entries); |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
920 memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(st->nb_index_entries - index)); |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
921 } |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
922 st->nb_index_entries++; |
425 | 923 }else{ |
924 if(ie->pos == pos && distance < ie->min_distance) //dont reduce the distance | |
925 distance= ie->min_distance; | |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
926 } |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
927 }else{ |
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
928 index= st->nb_index_entries++; |
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
929 ie= &entries[index]; |
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
930 } |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
931 |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
932 ie->pos = pos; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
933 ie->timestamp = timestamp; |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
934 ie->min_distance= distance; |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
935 ie->flags = flags; |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
936 |
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
937 return index; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
938 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
939 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
940 /* build an index for raw streams using a parser */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
941 static void av_build_index_raw(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
942 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
943 AVPacket pkt1, *pkt = &pkt1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
944 int ret; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
945 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
946 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
947 st = s->streams[0]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
948 av_read_frame_flush(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
949 url_fseek(&s->pb, s->data_offset, SEEK_SET); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
950 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
951 for(;;) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
952 ret = av_read_frame(s, pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
953 if (ret < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
954 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
955 if (pkt->stream_index == 0 && st->parser && |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
956 (pkt->flags & PKT_FLAG_KEY)) { |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
957 av_add_index_entry(st, st->parser->frame_offset, pkt->dts, |
354
6770ca07abe2
store searched distance in index, so we dont waste time searching for keyframes where we already searched
michael
parents:
346
diff
changeset
|
958 0, AVINDEX_KEYFRAME); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
959 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
960 av_free_packet(pkt); |
0 | 961 } |
962 } | |
963 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
964 /* return TRUE if we deal with a raw stream (raw codec data and |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
965 parsing needed) */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
966 static int is_raw_stream(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
967 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
968 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
969 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
970 if (s->nb_streams != 1) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
971 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
972 st = s->streams[0]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
973 if (!st->need_parsing) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
974 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
975 return 1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
976 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
977 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
978 /* return the largest index entry whose timestamp is <= |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
979 wanted_timestamp */ |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
980 int av_index_search_timestamp(AVStream *st, int wanted_timestamp) |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
981 { |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
982 AVIndexEntry *entries= st->index_entries; |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
983 int nb_entries= st->nb_index_entries; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
984 int a, b, m; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
985 int64_t timestamp; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
986 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
987 if (nb_entries <= 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
988 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
989 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
990 a = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
991 b = nb_entries - 1; |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
992 |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
993 while (a < b) { |
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
994 m = (a + b + 1) >> 1; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
995 timestamp = entries[m].timestamp; |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
996 if (timestamp > wanted_timestamp) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
997 b = m - 1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
998 } else { |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
999 a = m; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1000 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1001 } |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1002 return a; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1003 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1004 |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1005 #define DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1006 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1007 int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1008 AVInputFormat *avif= s->iformat; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1009 int64_t pos_min, pos_max, pos, pos_limit; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1010 int64_t ts_min, ts_max, ts; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1011 int64_t start_pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1012 int index, no_change; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1013 AVStream *st; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1014 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1015 if (stream_index < 0) { |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1016 stream_index = av_find_default_stream_index(s); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1017 if (stream_index < 0) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1018 return -1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1019 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1020 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1021 #ifdef DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1022 av_log(s, AV_LOG_DEBUG, "read_seek: %d %lld\n", stream_index, target_ts); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1023 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1024 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1025 ts_max= |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1026 ts_min= AV_NOPTS_VALUE; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1027 pos_limit= -1; //gcc falsely says it may be uninitalized |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1028 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1029 st= s->streams[stream_index]; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1030 if(st->index_entries){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1031 AVIndexEntry *e; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1032 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1033 index= av_index_search_timestamp(st, target_ts); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1034 e= &st->index_entries[index]; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1035 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1036 if(e->timestamp <= target_ts || e->pos == e->min_distance){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1037 pos_min= e->pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1038 ts_min= e->timestamp; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1039 #ifdef DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1040 av_log(s, AV_LOG_DEBUG, "unsing cached pos_min=0x%llx dts_min=%lld\n", |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1041 pos_min,ts_min); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1042 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1043 }else{ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1044 assert(index==0); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1045 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1046 index++; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1047 if(index < st->nb_index_entries){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1048 e= &st->index_entries[index]; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1049 assert(e->timestamp >= target_ts); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1050 pos_max= e->pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1051 ts_max= e->timestamp; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1052 pos_limit= pos_max - e->min_distance; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1053 #ifdef DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1054 av_log(s, AV_LOG_DEBUG, "unsing cached pos_max=0x%llx pos_limit=0x%llx dts_max=%lld\n", |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1055 pos_max,pos_limit, ts_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1056 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1057 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1058 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1059 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1060 if(ts_min == AV_NOPTS_VALUE){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1061 pos_min = s->data_offset; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1062 ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1063 if (ts_min == AV_NOPTS_VALUE) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1064 return -1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1065 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1066 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1067 if(ts_max == AV_NOPTS_VALUE){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1068 int step= 1024; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1069 pos_max = url_filesize(url_fileno(&s->pb)) - 1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1070 do{ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1071 pos_max -= step; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1072 ts_max = avif->read_timestamp(s, stream_index, &pos_max, pos_max + step); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1073 step += step; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1074 }while(ts_max == AV_NOPTS_VALUE && pos_max >= step); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1075 if (ts_max == AV_NOPTS_VALUE) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1076 return -1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1077 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1078 for(;;){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1079 int64_t tmp_pos= pos_max + 1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1080 int64_t tmp_ts= avif->read_timestamp(s, stream_index, &tmp_pos, INT64_MAX); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1081 if(tmp_ts == AV_NOPTS_VALUE) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1082 break; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1083 ts_max= tmp_ts; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1084 pos_max= tmp_pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1085 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1086 pos_limit= pos_max; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1087 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1088 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1089 no_change=0; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1090 while (pos_min < pos_limit) { |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1091 #ifdef DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1092 av_log(s, AV_LOG_DEBUG, "pos_min=0x%llx pos_max=0x%llx dts_min=%lld dts_max=%lld\n", |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1093 pos_min, pos_max, |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1094 ts_min, ts_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1095 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1096 assert(pos_limit <= pos_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1097 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1098 if(no_change==0){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1099 int64_t approximate_keyframe_distance= pos_max - pos_limit; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1100 // interpolate position (better than dichotomy) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1101 pos = (int64_t)((double)(pos_max - pos_min) * |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1102 (double)(target_ts - ts_min) / |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1103 (double)(ts_max - ts_min)) + pos_min - approximate_keyframe_distance; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1104 }else if(no_change==1){ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1105 // bisection, if interpolation failed to change min or max pos last time |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1106 pos = (pos_min + pos_limit)>>1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1107 }else{ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1108 // linear search if bisection failed, can only happen if there are very few or no keframes between min/max |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1109 pos=pos_min; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1110 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1111 if(pos <= pos_min) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1112 pos= pos_min + 1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1113 else if(pos > pos_limit) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1114 pos= pos_limit; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1115 start_pos= pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1116 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1117 ts = avif->read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1118 if(pos == pos_max) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1119 no_change++; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1120 else |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1121 no_change=0; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1122 #ifdef DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1123 av_log(s, AV_LOG_DEBUG, "%Ld %Ld %Ld / %Ld %Ld %Ld target:%Ld limit:%Ld start:%Ld noc:%d\n", pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts, pos_limit, start_pos, no_change); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1124 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1125 assert(ts != AV_NOPTS_VALUE); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1126 if (target_ts < ts) { |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1127 pos_limit = start_pos - 1; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1128 pos_max = pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1129 ts_max = ts; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1130 } else { |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1131 pos_min = pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1132 ts_min = ts; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1133 /* check if we are lucky */ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1134 if (target_ts == ts) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1135 break; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1136 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1137 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1138 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1139 pos = pos_min; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1140 #ifdef DEBUG_SEEK |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1141 pos_min = pos; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1142 ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1143 pos_min++; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1144 ts_max = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1145 av_log(s, AV_LOG_DEBUG, "pos=0x%llx %lld<=%lld<=%lld\n", |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1146 pos, ts_min, target_ts, ts_max); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1147 #endif |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1148 /* do the seek */ |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1149 url_fseek(&s->pb, pos, SEEK_SET); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1150 st->cur_dts = ts_min; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1151 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1152 return 0; |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1153 } |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1154 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1155 static int av_seek_frame_generic(AVFormatContext *s, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1156 int stream_index, int64_t timestamp) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1157 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1158 int index; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1159 AVStream *st; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1160 AVIndexEntry *ie; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1161 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1162 if (!s->index_built) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1163 if (is_raw_stream(s)) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1164 av_build_index_raw(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1165 } else { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1166 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1167 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1168 s->index_built = 1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1169 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1170 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1171 if (stream_index < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1172 stream_index = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1173 st = s->streams[stream_index]; |
346
e154eb1b7149
caching of timestamps for mpeg-ps so seeking is faster
michael
parents:
334
diff
changeset
|
1174 index = av_index_search_timestamp(st, timestamp); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1175 if (index < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1176 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1177 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1178 /* now we have found the index, we can seek */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1179 ie = &st->index_entries[index]; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1180 av_read_frame_flush(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1181 url_fseek(&s->pb, ie->pos, SEEK_SET); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1182 st->cur_dts = ie->timestamp; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1183 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1184 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1185 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1186 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1187 * Seek to the key frame just before the frame at timestamp |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1188 * 'timestamp' in 'stream_index'. If stream_index is (-1), a default |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1189 * stream is selected |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1190 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1191 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1192 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1193 int ret; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1194 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1195 av_read_frame_flush(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1196 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1197 /* first, we try the format specific seek */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1198 if (s->iformat->read_seek) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1199 ret = s->iformat->read_seek(s, stream_index, timestamp); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1200 else |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1201 ret = -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1202 if (ret >= 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1203 return 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1204 } |
437
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1205 |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1206 if(s->iformat->read_timestamp) |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1207 return av_seek_frame_binary(s, stream_index, timestamp); |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1208 else |
50bae308f71e
moving nearly identical binary search code from nut/mpeg/asf to utils.c
michael
parents:
425
diff
changeset
|
1209 return av_seek_frame_generic(s, stream_index, timestamp); |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1210 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1211 |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1212 /*******************************************************/ |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1213 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1214 /* return TRUE if the stream has accurate timings for at least one component */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1215 static int av_has_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1216 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1217 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1218 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1219 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1220 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1221 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1222 if (st->start_time != AV_NOPTS_VALUE && |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1223 st->duration != AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1224 return 1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1225 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1226 return 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1227 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1228 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1229 /* estimate the stream timings from the one of each components. Also |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1230 compute the global bitrate if possible */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1231 static void av_update_stream_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1232 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1233 int64_t start_time, end_time, end_time1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1234 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1235 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1236 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1237 start_time = MAXINT64; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1238 end_time = MININT64; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1239 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1240 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1241 if (st->start_time != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1242 if (st->start_time < start_time) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1243 start_time = st->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1244 if (st->duration != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1245 end_time1 = st->start_time + st->duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1246 if (end_time1 > end_time) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1247 end_time = end_time1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1248 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1249 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1250 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1251 if (start_time != MAXINT64) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1252 ic->start_time = start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1253 if (end_time != MAXINT64) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1254 ic->duration = end_time - start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1255 if (ic->file_size > 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1256 /* compute the bit rate */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1257 ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE / |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1258 (double)ic->duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1259 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1260 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1261 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1262 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1263 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1264 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1265 static void fill_all_stream_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1266 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1267 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1268 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1269 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1270 av_update_stream_timings(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1271 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1272 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1273 if (st->start_time == AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1274 st->start_time = ic->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1275 st->duration = ic->duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1276 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1277 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1278 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1279 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1280 static void av_estimate_timings_from_bit_rate(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1281 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1282 int64_t filesize, duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1283 int bit_rate, i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1284 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1285 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1286 /* if bit_rate is already set, we believe it */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1287 if (ic->bit_rate == 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1288 bit_rate = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1289 for(i=0;i<ic->nb_streams;i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1290 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1291 bit_rate += st->codec.bit_rate; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1292 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1293 ic->bit_rate = bit_rate; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1294 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1295 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1296 /* if duration is already set, we believe it */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1297 if (ic->duration == AV_NOPTS_VALUE && |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1298 ic->bit_rate != 0 && |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1299 ic->file_size != 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1300 filesize = ic->file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1301 if (filesize > 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1302 duration = (int64_t)((8 * AV_TIME_BASE * (double)filesize) / (double)ic->bit_rate); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1303 for(i = 0; i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1304 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1305 if (st->start_time == AV_NOPTS_VALUE || |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1306 st->duration == AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1307 st->start_time = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1308 st->duration = duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1309 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1310 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1311 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1312 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1313 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1314 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1315 #define DURATION_MAX_READ_SIZE 250000 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1316 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1317 /* only usable for MPEG-PS streams */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1318 static void av_estimate_timings_from_pts(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1319 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1320 AVPacket pkt1, *pkt = &pkt1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1321 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1322 int read_size, i, ret; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1323 int64_t start_time, end_time, end_time1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1324 int64_t filesize, offset, duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1325 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1326 /* free previous packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1327 if (ic->cur_st && ic->cur_st->parser) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1328 av_free_packet(&ic->cur_pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1329 ic->cur_st = NULL; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1330 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1331 /* flush packet queue */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1332 flush_packet_queue(ic); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1333 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1334 |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1335 /* we read the first packets to get the first PTS (not fully |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1336 accurate, but it is enough now) */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1337 url_fseek(&ic->pb, 0, SEEK_SET); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1338 read_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1339 for(;;) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1340 if (read_size >= DURATION_MAX_READ_SIZE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1341 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1342 /* if all info is available, we can stop */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1343 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1344 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1345 if (st->start_time == AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1346 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1347 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1348 if (i == ic->nb_streams) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1349 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1350 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1351 ret = av_read_packet(ic, pkt); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1352 if (ret != 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1353 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1354 read_size += pkt->size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1355 st = ic->streams[pkt->stream_index]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1356 if (pkt->pts != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1357 if (st->start_time == AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1358 st->start_time = (int64_t)((double)pkt->pts * ic->pts_num * (double)AV_TIME_BASE / ic->pts_den); |
224
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1359 } |
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1360 av_free_packet(pkt); |
8d1569be0ee1
memory leak fix by (Tom Dexter <devel at www dot digitalaudiorock dot com>)
michaelni
parents:
205
diff
changeset
|
1361 } |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1362 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1363 /* we compute the minimum start_time and use it as default */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1364 start_time = MAXINT64; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1365 for(i = 0; i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1366 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1367 if (st->start_time != AV_NOPTS_VALUE && |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1368 st->start_time < start_time) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1369 start_time = st->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1370 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1371 if (start_time != MAXINT64) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1372 ic->start_time = start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1373 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1374 /* estimate the end time (duration) */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1375 /* XXX: may need to support wrapping */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1376 filesize = ic->file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1377 offset = filesize - DURATION_MAX_READ_SIZE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1378 if (offset < 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1379 offset = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1380 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1381 url_fseek(&ic->pb, offset, SEEK_SET); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1382 read_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1383 for(;;) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1384 if (read_size >= DURATION_MAX_READ_SIZE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1385 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1386 /* if all info is available, we can stop */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1387 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1388 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1389 if (st->duration == AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1390 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1391 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1392 if (i == ic->nb_streams) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1393 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1394 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1395 ret = av_read_packet(ic, pkt); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1396 if (ret != 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1397 break; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1398 read_size += pkt->size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1399 st = ic->streams[pkt->stream_index]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1400 if (pkt->pts != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1401 end_time = (int64_t)((double)pkt->pts * ic->pts_num * (double)AV_TIME_BASE / ic->pts_den); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1402 duration = end_time - st->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1403 if (duration > 0) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1404 if (st->duration == AV_NOPTS_VALUE || |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1405 st->duration < duration) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1406 st->duration = duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1407 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1408 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1409 av_free_packet(pkt); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1410 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1411 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1412 /* estimate total duration */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1413 end_time = MININT64; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1414 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1415 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1416 if (st->duration != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1417 end_time1 = st->start_time + st->duration; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1418 if (end_time1 > end_time) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1419 end_time = end_time1; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1420 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1421 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1422 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1423 /* update start_time (new stream may have been created, so we do |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1424 it at the end */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1425 if (ic->start_time != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1426 for(i = 0; i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1427 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1428 if (st->start_time == AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1429 st->start_time = ic->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1430 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1431 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1432 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1433 if (end_time != MININT64) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1434 /* put dummy values for duration if needed */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1435 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1436 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1437 if (st->duration == AV_NOPTS_VALUE && |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1438 st->start_time != AV_NOPTS_VALUE) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1439 st->duration = end_time - st->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1440 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1441 ic->duration = end_time - ic->start_time; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1442 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1443 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1444 url_fseek(&ic->pb, 0, SEEK_SET); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1445 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1446 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1447 static void av_estimate_timings(AVFormatContext *ic) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1448 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1449 URLContext *h; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1450 int64_t file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1451 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1452 /* get the file size, if possible */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1453 if (ic->iformat->flags & AVFMT_NOFILE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1454 file_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1455 } else { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1456 h = url_fileno(&ic->pb); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1457 file_size = url_filesize(h); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1458 if (file_size < 0) |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1459 file_size = 0; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1460 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1461 ic->file_size = file_size; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1462 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1463 if (ic->iformat == &mpegps_demux) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1464 /* get accurate estimate from the PTSes */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1465 av_estimate_timings_from_pts(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1466 } else if (av_has_timings(ic)) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1467 /* at least one components has timings - we use them for all |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1468 the components */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1469 fill_all_stream_timings(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1470 } else { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1471 /* less precise: use bit rate info */ |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1472 av_estimate_timings_from_bit_rate(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1473 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1474 av_update_stream_timings(ic); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1475 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1476 #if 0 |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1477 { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1478 int i; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1479 AVStream *st; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1480 for(i = 0;i < ic->nb_streams; i++) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1481 st = ic->streams[i]; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1482 printf("%d: start_time: %0.3f duration: %0.3f\n", |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1483 i, (double)st->start_time / AV_TIME_BASE, |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1484 (double)st->duration / AV_TIME_BASE); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1485 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1486 printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n", |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1487 (double)ic->start_time / AV_TIME_BASE, |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1488 (double)ic->duration / AV_TIME_BASE, |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1489 ic->bit_rate / 1000); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1490 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1491 #endif |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1492 } |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1493 |
0 | 1494 static int has_codec_parameters(AVCodecContext *enc) |
1495 { | |
1496 int val; | |
1497 switch(enc->codec_type) { | |
1498 case CODEC_TYPE_AUDIO: | |
1499 val = enc->sample_rate; | |
1500 break; | |
1501 case CODEC_TYPE_VIDEO: | |
1502 val = enc->width; | |
1503 break; | |
1504 default: | |
1505 val = 1; | |
1506 break; | |
1507 } | |
1508 return (val != 0); | |
1509 } | |
1510 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1511 static int try_decode_frame(AVStream *st, const uint8_t *data, int size) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1512 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1513 int16_t *samples; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1514 AVCodec *codec; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1515 int got_picture, ret; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1516 AVFrame picture; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1517 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1518 codec = avcodec_find_decoder(st->codec.codec_id); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1519 if (!codec) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1520 return -1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1521 ret = avcodec_open(&st->codec, codec); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1522 if (ret < 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1523 return ret; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1524 switch(st->codec.codec_type) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1525 case CODEC_TYPE_VIDEO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1526 ret = avcodec_decode_video(&st->codec, &picture, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1527 &got_picture, (uint8_t *)data, size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1528 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1529 case CODEC_TYPE_AUDIO: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1530 samples = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1531 if (!samples) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1532 goto fail; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1533 ret = avcodec_decode_audio(&st->codec, samples, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1534 &got_picture, (uint8_t *)data, size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1535 av_free(samples); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1536 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1537 default: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1538 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1539 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1540 fail: |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1541 avcodec_close(&st->codec); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1542 return ret; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1543 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1544 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1545 /* absolute maximum size we read until we abort */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1546 #define MAX_READ_SIZE 5000000 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1547 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1548 /* maximum duration until we stop analysing the stream */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1549 #define MAX_STREAM_DURATION ((int)(AV_TIME_BASE * 1.0)) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1550 |
0 | 1551 /** |
1552 * Read the beginning of a media file to get stream information. This | |
1553 * is useful for file formats with no headers such as MPEG. This | |
1554 * function also compute the real frame rate in case of mpeg2 repeat | |
1555 * frame mode. | |
1556 * | |
1557 * @param ic media file handle | |
1558 * @return >=0 if OK. AVERROR_xxx if error. | |
1559 */ | |
1560 int av_find_stream_info(AVFormatContext *ic) | |
1561 { | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1562 int i, count, ret, read_size; |
0 | 1563 AVStream *st; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1564 AVPacket pkt1, *pkt; |
0 | 1565 AVPacketList *pktl=NULL, **ppktl; |
1566 | |
1567 count = 0; | |
1568 read_size = 0; | |
1569 ppktl = &ic->packet_buffer; | |
1570 for(;;) { | |
1571 /* check if one codec still needs to be handled */ | |
1572 for(i=0;i<ic->nb_streams;i++) { | |
1573 st = ic->streams[i]; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1574 if (!has_codec_parameters(&st->codec)) |
0 | 1575 break; |
1576 } | |
1577 if (i == ic->nb_streams) { | |
1578 /* NOTE: if the format has no header, then we need to read | |
1579 some packets to get most of the streams, so we cannot | |
1580 stop here */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1581 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { |
0 | 1582 /* if we found the info for all the codecs, we can stop */ |
1583 ret = count; | |
1584 break; | |
1585 } | |
1586 } else { | |
1587 /* we did not get all the codec info, but we read too much data */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1588 if (read_size >= MAX_READ_SIZE) { |
0 | 1589 ret = count; |
1590 break; | |
1591 } | |
1592 } | |
1593 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1594 /* NOTE: a new stream can be added there if no header in file |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1595 (AVFMTCTX_NOHEADER) */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1596 ret = av_read_frame_internal(ic, &pkt1); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1597 if (ret < 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1598 /* EOF or error */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1599 ret = -1; /* we could not have all the codec parameters before EOF */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1600 if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1601 i == ic->nb_streams) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1602 ret = 0; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1603 break; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1604 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1605 |
0 | 1606 pktl = av_mallocz(sizeof(AVPacketList)); |
1607 if (!pktl) { | |
1608 ret = AVERROR_NOMEM; | |
1609 break; | |
1610 } | |
1611 | |
1612 /* add the packet in the buffered packet list */ | |
1613 *ppktl = pktl; | |
1614 ppktl = &pktl->next; | |
1615 | |
1616 pkt = &pktl->pkt; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1617 *pkt = pkt1; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1618 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1619 /* duplicate the packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1620 if (av_dup_packet(pkt) < 0) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1621 ret = AVERROR_NOMEM; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1622 break; |
0 | 1623 } |
1624 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1625 read_size += pkt->size; |
0 | 1626 |
1627 st = ic->streams[pkt->stream_index]; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1628 st->codec_info_duration += pkt->duration; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1629 if (pkt->duration != 0) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1630 st->codec_info_nb_frames++; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1631 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1632 /* if still no information, we try to open the codec and to |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1633 decompress the frame. We try to avoid that in most cases as |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1634 it takes longer and uses more memory. For MPEG4, we need to |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1635 decompress for Quicktime. */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1636 if (!has_codec_parameters(&st->codec) && |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1637 (st->codec.codec_id == CODEC_ID_FLV1 || |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1638 st->codec.codec_id == CODEC_ID_H264 || |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1639 st->codec.codec_id == CODEC_ID_H263 || |
404 | 1640 st->codec.codec_id == CODEC_ID_VORBIS || |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1641 (st->codec.codec_id == CODEC_ID_MPEG4 && !st->need_parsing))) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1642 try_decode_frame(st, pkt->data, pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1643 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1644 if (st->codec_info_duration >= MAX_STREAM_DURATION) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1645 break; |
0 | 1646 } |
1647 count++; | |
1648 } | |
1649 | |
1650 /* set real frame rate info */ | |
1651 for(i=0;i<ic->nb_streams;i++) { | |
1652 st = ic->streams[i]; | |
1653 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1654 /* compute the real frame rate for telecine */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1655 if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1656 st->codec.codec_id == CODEC_ID_MPEG2VIDEO) && |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1657 st->codec.sub_id == 2) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1658 if (st->codec_info_nb_frames >= 20) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1659 float coded_frame_rate, est_frame_rate; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1660 est_frame_rate = ((double)st->codec_info_nb_frames * AV_TIME_BASE) / |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1661 (double)st->codec_info_duration ; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1662 coded_frame_rate = (double)st->codec.frame_rate / |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1663 (double)st->codec.frame_rate_base; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1664 #if 0 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1665 printf("telecine: coded_frame_rate=%0.3f est_frame_rate=%0.3f\n", |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1666 coded_frame_rate, est_frame_rate); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1667 #endif |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1668 /* if we detect that it could be a telecine, we |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1669 signal it. It would be better to do it at a |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1670 higher level as it can change in a film */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1671 if (coded_frame_rate >= 24.97 && |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1672 (est_frame_rate >= 23.5 && est_frame_rate < 24.5)) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1673 st->r_frame_rate = 24024; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1674 st->r_frame_rate_base = 1001; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1675 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1676 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1677 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1678 /* if no real frame rate, use the codec one */ |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1679 if (!st->r_frame_rate){ |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1680 st->r_frame_rate = st->codec.frame_rate; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1681 st->r_frame_rate_base = st->codec.frame_rate_base; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1682 } |
0 | 1683 } |
1684 } | |
1685 | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1686 av_estimate_timings(ic); |
0 | 1687 return ret; |
1688 } | |
1689 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1690 /*******************************************************/ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1691 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1692 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1693 * start playing a network based stream (e.g. RTSP stream) at the |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1694 * current position |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1695 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1696 int av_read_play(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1697 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1698 if (!s->iformat->read_play) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1699 return AVERROR_NOTSUPP; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1700 return s->iformat->read_play(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1701 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1702 |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1703 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1704 * pause a network based stream (e.g. RTSP stream). Use av_read_play() |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1705 * to resume it. |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1706 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1707 int av_read_pause(AVFormatContext *s) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1708 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1709 if (!s->iformat->read_pause) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1710 return AVERROR_NOTSUPP; |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1711 return s->iformat->read_pause(s); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1712 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1713 |
0 | 1714 /** |
1715 * Close a media file (but not its codecs) | |
1716 * | |
1717 * @param s media file handle | |
1718 */ | |
1719 void av_close_input_file(AVFormatContext *s) | |
1720 { | |
172 | 1721 int i, must_open_file; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1722 AVStream *st; |
0 | 1723 |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1724 /* free previous packet */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1725 if (s->cur_st && s->cur_st->parser) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1726 av_free_packet(&s->cur_pkt); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1727 |
0 | 1728 if (s->iformat->read_close) |
1729 s->iformat->read_close(s); | |
1730 for(i=0;i<s->nb_streams;i++) { | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1731 /* free all data in a stream component */ |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1732 st = s->streams[i]; |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1733 if (st->parser) { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1734 av_parser_close(st->parser); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1735 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1736 av_free(st->index_entries); |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1737 av_free(st); |
0 | 1738 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1739 flush_packet_queue(s); |
172 | 1740 must_open_file = 1; |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1741 if (s->iformat->flags & AVFMT_NOFILE) { |
172 | 1742 must_open_file = 0; |
1743 } | |
1744 if (must_open_file) { | |
0 | 1745 url_fclose(&s->pb); |
1746 } | |
1747 av_freep(&s->priv_data); | |
1748 av_free(s); | |
1749 } | |
1750 | |
1751 /** | |
1752 * Add a new stream to a media file. Can only be called in the | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1753 * read_header function. If the flag AVFMTCTX_NOHEADER is in the |
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1754 * format context, then new streams can be added in read_packet too. |
0 | 1755 * |
1756 * | |
1757 * @param s media file handle | |
293
62cec412a186
make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents:
290
diff
changeset
|
1758 * @param id file format dependent stream id |
0 | 1759 */ |
1760 AVStream *av_new_stream(AVFormatContext *s, int id) | |
1761 { | |
1762 AVStream *st; | |
1763 | |
1764 if (s->nb_streams >= MAX_STREAMS) | |
1765 return NULL; | |
1766 | |
1767 st = av_mallocz(sizeof(AVStream)); | |
1768 if (!st) | |
1769 return NULL; | |
5 | 1770 avcodec_get_context_defaults(&st->codec); |
193 | 1771 if (s->iformat) { |
1772 /* no default bitrate if decoding */ | |
1773 st->codec.bit_rate = 0; | |
1774 } | |
0 | 1775 st->index = s->nb_streams; |
1776 st->id = id; | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1777 st->start_time = AV_NOPTS_VALUE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1778 st->duration = AV_NOPTS_VALUE; |
0 | 1779 s->streams[s->nb_streams++] = st; |
1780 return st; | |
1781 } | |
1782 | |
1783 /************************************************************/ | |
1784 /* output media file */ | |
1785 | |
20 | 1786 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
1787 { | |
1788 int ret; | |
32 | 1789 |
1790 if (s->oformat->priv_data_size > 0) { | |
1791 s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
1792 if (!s->priv_data) | |
1793 return AVERROR_NOMEM; | |
1794 } else | |
1795 s->priv_data = NULL; | |
1796 | |
20 | 1797 if (s->oformat->set_parameters) { |
1798 ret = s->oformat->set_parameters(s, ap); | |
1799 if (ret < 0) | |
1800 return ret; | |
1801 } | |
1802 return 0; | |
1803 } | |
1804 | |
0 | 1805 /** |
1806 * allocate the stream private data and write the stream header to an | |
1807 * output media file | |
1808 * | |
1809 * @param s media file handle | |
1810 * @return 0 if OK. AVERROR_xxx if error. | |
1811 */ | |
1812 int av_write_header(AVFormatContext *s) | |
1813 { | |
1814 int ret, i; | |
1815 AVStream *st; | |
1816 | |
1817 /* default pts settings is MPEG like */ | |
1818 av_set_pts_info(s, 33, 1, 90000); | |
1819 ret = s->oformat->write_header(s); | |
1820 if (ret < 0) | |
1821 return ret; | |
1822 | |
1823 /* init PTS generation */ | |
1824 for(i=0;i<s->nb_streams;i++) { | |
1825 st = s->streams[i]; | |
1826 | |
1827 switch (st->codec.codec_type) { | |
1828 case CODEC_TYPE_AUDIO: | |
1829 av_frac_init(&st->pts, 0, 0, | |
65 | 1830 (int64_t)s->pts_num * st->codec.sample_rate); |
0 | 1831 break; |
1832 case CODEC_TYPE_VIDEO: | |
1833 av_frac_init(&st->pts, 0, 0, | |
65 | 1834 (int64_t)s->pts_num * st->codec.frame_rate); |
0 | 1835 break; |
1836 default: | |
1837 break; | |
1838 } | |
1839 } | |
1840 return 0; | |
1841 } | |
1842 | |
1843 /** | |
1844 * Write a packet to an output media file. The packet shall contain | |
1845 * one audio or video frame. | |
1846 * | |
1847 * @param s media file handle | |
1848 * @param stream_index stream index | |
1849 * @param buf buffer containing the frame data | |
1850 * @param size size of buffer | |
1851 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
1852 */ | |
1853 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, | |
1854 int size) | |
1855 { | |
1856 AVStream *st; | |
65 | 1857 int64_t pts_mask; |
0 | 1858 int ret, frame_size; |
1859 | |
1860 st = s->streams[stream_index]; | |
1861 pts_mask = (1LL << s->pts_wrap_bits) - 1; | |
409 | 1862 |
1863 /* HACK/FIXME we skip all zero size audio packets so a encoder can pass pts by outputing zero size packets */ | |
1864 if(st->codec.codec_type==CODEC_TYPE_AUDIO && size==0) | |
1865 ret = 0; | |
1866 else | |
1867 ret = s->oformat->write_packet(s, stream_index, buf, size, | |
1868 st->pts.val & pts_mask); | |
1869 | |
0 | 1870 if (ret < 0) |
1871 return ret; | |
1872 | |
1873 /* update pts */ | |
1874 switch (st->codec.codec_type) { | |
1875 case CODEC_TYPE_AUDIO: | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1876 frame_size = get_audio_frame_size(&st->codec, size); |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
1877 |
409 | 1878 /* HACK/FIXME, we skip the initial 0-size packets as they are most likely equal to the encoder delay, |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
1879 but it would be better if we had the real timestamps from the encoder */ |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
1880 if (frame_size >= 0 && (size || st->pts.num!=st->pts.den>>1 || st->pts.val)) { |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1881 av_frac_add(&st->pts, |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
1882 (int64_t)s->pts_den * frame_size); |
0 | 1883 } |
1884 break; | |
1885 case CODEC_TYPE_VIDEO: | |
1886 av_frac_add(&st->pts, | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
1887 (int64_t)s->pts_den * st->codec.frame_rate_base); |
0 | 1888 break; |
1889 default: | |
1890 break; | |
1891 } | |
1892 return ret; | |
1893 } | |
1894 | |
1895 /** | |
1896 * write the stream trailer to an output media file and and free the | |
1897 * file private data. | |
1898 * | |
1899 * @param s media file handle | |
1900 * @return 0 if OK. AVERROR_xxx if error. */ | |
1901 int av_write_trailer(AVFormatContext *s) | |
1902 { | |
1903 int ret; | |
1904 ret = s->oformat->write_trailer(s); | |
1905 av_freep(&s->priv_data); | |
1906 return ret; | |
1907 } | |
1908 | |
1909 /* "user interface" functions */ | |
1910 | |
1911 void dump_format(AVFormatContext *ic, | |
1912 int index, | |
1913 const char *url, | |
1914 int is_output) | |
1915 { | |
1916 int i, flags; | |
1917 char buf[256]; | |
1918 | |
371 | 1919 av_log(NULL, AV_LOG_DEBUG, "%s #%d, %s, %s '%s':\n", |
0 | 1920 is_output ? "Output" : "Input", |
1921 index, | |
1922 is_output ? ic->oformat->name : ic->iformat->name, | |
1923 is_output ? "to" : "from", url); | |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1924 if (!is_output) { |
371 | 1925 av_log(NULL, AV_LOG_DEBUG, " Duration: "); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1926 if (ic->duration != AV_NOPTS_VALUE) { |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1927 int hours, mins, secs, us; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1928 secs = ic->duration / AV_TIME_BASE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1929 us = ic->duration % AV_TIME_BASE; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1930 mins = secs / 60; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1931 secs %= 60; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1932 hours = mins / 60; |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1933 mins %= 60; |
371 | 1934 av_log(NULL, AV_LOG_DEBUG, "%02d:%02d:%02d.%01d", hours, mins, secs, |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1935 (10 * us) / AV_TIME_BASE); |
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1936 } else { |
371 | 1937 av_log(NULL, AV_LOG_DEBUG, "N/A"); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1938 } |
371 | 1939 av_log(NULL, AV_LOG_DEBUG, ", bitrate: "); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1940 if (ic->bit_rate) { |
371 | 1941 av_log(NULL, AV_LOG_DEBUG,"%d kb/s", ic->bit_rate / 1000); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1942 } else { |
371 | 1943 av_log(NULL, AV_LOG_DEBUG, "N/A"); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1944 } |
371 | 1945 av_log(NULL, AV_LOG_DEBUG, "\n"); |
192
0316a506aeb0
initial duration/start_time generic support - displays stream duration and average total bitrate when using an input file
bellard
parents:
175
diff
changeset
|
1946 } |
0 | 1947 for(i=0;i<ic->nb_streams;i++) { |
1948 AVStream *st = ic->streams[i]; | |
1949 avcodec_string(buf, sizeof(buf), &st->codec, is_output); | |
371 | 1950 av_log(NULL, AV_LOG_DEBUG, " Stream #%d.%d", index, i); |
0 | 1951 /* the pid is an important information, so we display it */ |
1952 /* XXX: add a generic system */ | |
1953 if (is_output) | |
1954 flags = ic->oformat->flags; | |
1955 else | |
1956 flags = ic->iformat->flags; | |
1957 if (flags & AVFMT_SHOW_IDS) { | |
371 | 1958 av_log(NULL, AV_LOG_DEBUG, "[0x%x]", st->id); |
0 | 1959 } |
371 | 1960 av_log(NULL, AV_LOG_DEBUG, ": %s\n", buf); |
0 | 1961 } |
1962 } | |
1963 | |
1964 typedef struct { | |
168 | 1965 const char *abv; |
0 | 1966 int width, height; |
168 | 1967 int frame_rate, frame_rate_base; |
1968 } AbvEntry; | |
0 | 1969 |
168 | 1970 static AbvEntry frame_abvs[] = { |
204 | 1971 { "ntsc", 720, 480, 30000, 1001 }, |
1972 { "pal", 720, 576, 25, 1 }, | |
1973 { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */ | |
1974 { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */ | |
205 | 1975 { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */ |
1976 { "spal", 768, 576, 25, 1 }, /* square pixel pal */ | |
168 | 1977 { "film", 352, 240, 24, 1 }, |
1978 { "ntsc-film", 352, 240, 24000, 1001 }, | |
1979 { "sqcif", 128, 96, 0, 0 }, | |
1980 { "qcif", 176, 144, 0, 0 }, | |
1981 { "cif", 352, 288, 0, 0 }, | |
1982 { "4cif", 704, 576, 0, 0 }, | |
0 | 1983 }; |
168 | 1984 |
0 | 1985 int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
1986 { | |
1987 int i; | |
168 | 1988 int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
0 | 1989 const char *p; |
1990 int frame_width = 0, frame_height = 0; | |
1991 | |
1992 for(i=0;i<n;i++) { | |
168 | 1993 if (!strcmp(frame_abvs[i].abv, str)) { |
1994 frame_width = frame_abvs[i].width; | |
1995 frame_height = frame_abvs[i].height; | |
0 | 1996 break; |
1997 } | |
1998 } | |
1999 if (i == n) { | |
2000 p = str; | |
2001 frame_width = strtol(p, (char **)&p, 10); | |
2002 if (*p) | |
2003 p++; | |
2004 frame_height = strtol(p, (char **)&p, 10); | |
2005 } | |
2006 if (frame_width <= 0 || frame_height <= 0) | |
2007 return -1; | |
2008 *width_ptr = frame_width; | |
2009 *height_ptr = frame_height; | |
2010 return 0; | |
2011 } | |
2012 | |
168 | 2013 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
2014 { | |
2015 int i; | |
2016 char* cp; | |
2017 | |
2018 /* First, we check our abbreviation table */ | |
2019 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
2020 if (!strcmp(frame_abvs[i].abv, arg)) { | |
2021 *frame_rate = frame_abvs[i].frame_rate; | |
2022 *frame_rate_base = frame_abvs[i].frame_rate_base; | |
2023 return 0; | |
2024 } | |
2025 | |
2026 /* Then, we try to parse it as fraction */ | |
2027 cp = strchr(arg, '/'); | |
2028 if (cp) { | |
2029 char* cpp; | |
2030 *frame_rate = strtol(arg, &cpp, 10); | |
2031 if (cpp != arg || cpp == cp) | |
2032 *frame_rate_base = strtol(cp+1, &cpp, 10); | |
2033 else | |
2034 *frame_rate = 0; | |
2035 } | |
2036 else { | |
2037 /* Finally we give up and parse it as double */ | |
404 | 2038 *frame_rate_base = DEFAULT_FRAME_RATE_BASE; //FIXME use av_d2q() |
168 | 2039 *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5); |
2040 } | |
2041 if (!*frame_rate || !*frame_rate_base) | |
2042 return -1; | |
2043 else | |
2044 return 0; | |
2045 } | |
2046 | |
0 | 2047 /* Syntax: |
2048 * - If not a duration: | |
2049 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
2050 * Time is localtime unless Z is suffixed to the end. In this case GMT | |
2051 * Return the date in micro seconds since 1970 | |
2052 * - If duration: | |
2053 * HH[:MM[:SS[.m...]]] | |
2054 * S+[.m...] | |
2055 */ | |
65 | 2056 int64_t parse_date(const char *datestr, int duration) |
0 | 2057 { |
2058 const char *p; | |
65 | 2059 int64_t t; |
0 | 2060 struct tm dt; |
2061 int i; | |
2062 static const char *date_fmt[] = { | |
2063 "%Y-%m-%d", | |
2064 "%Y%m%d", | |
2065 }; | |
2066 static const char *time_fmt[] = { | |
2067 "%H:%M:%S", | |
2068 "%H%M%S", | |
2069 }; | |
2070 const char *q; | |
2071 int is_utc, len; | |
2072 char lastch; | |
406
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2073 |
ea22a438ca79
fix obnoxious ogg_packet passing from encoder to muxer
michael
parents:
404
diff
changeset
|
2074 #undef time |
0 | 2075 time_t now = time(0); |
2076 | |
2077 len = strlen(datestr); | |
2078 if (len > 0) | |
2079 lastch = datestr[len - 1]; | |
2080 else | |
2081 lastch = '\0'; | |
2082 is_utc = (lastch == 'z' || lastch == 'Z'); | |
2083 | |
2084 memset(&dt, 0, sizeof(dt)); | |
2085 | |
2086 p = datestr; | |
2087 q = NULL; | |
2088 if (!duration) { | |
2089 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { | |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
2090 q = small_strptime(p, date_fmt[i], &dt); |
0 | 2091 if (q) { |
2092 break; | |
2093 } | |
2094 } | |
2095 | |
2096 if (!q) { | |
2097 if (is_utc) { | |
2098 dt = *gmtime(&now); | |
2099 } else { | |
2100 dt = *localtime(&now); | |
2101 } | |
2102 dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
2103 } else { | |
2104 p = q; | |
2105 } | |
2106 | |
2107 if (*p == 'T' || *p == 't' || *p == ' ') | |
2108 p++; | |
2109 | |
2110 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { | |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
2111 q = small_strptime(p, time_fmt[i], &dt); |
0 | 2112 if (q) { |
2113 break; | |
2114 } | |
2115 } | |
2116 } else { | |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
229
diff
changeset
|
2117 q = small_strptime(p, time_fmt[0], &dt); |
0 | 2118 if (!q) { |
2119 dt.tm_sec = strtol(p, (char **)&q, 10); | |
2120 dt.tm_min = 0; | |
2121 dt.tm_hour = 0; | |
2122 } | |
2123 } | |
2124 | |
2125 /* Now we have all the fields that we can get */ | |
2126 if (!q) { | |
2127 if (duration) | |
2128 return 0; | |
2129 else | |
65 | 2130 return now * int64_t_C(1000000); |
0 | 2131 } |
2132 | |
2133 if (duration) { | |
2134 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; | |
2135 } else { | |
2136 dt.tm_isdst = -1; /* unknown */ | |
2137 if (is_utc) { | |
2138 t = mktimegm(&dt); | |
2139 } else { | |
2140 t = mktime(&dt); | |
2141 } | |
2142 } | |
2143 | |
2144 t *= 1000000; | |
2145 | |
2146 if (*q == '.') { | |
2147 int val, n; | |
2148 q++; | |
2149 for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
2150 if (!isdigit(*q)) | |
2151 break; | |
2152 val += n * (*q - '0'); | |
2153 } | |
2154 t += val; | |
2155 } | |
2156 return t; | |
2157 } | |
2158 | |
2159 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return | |
2160 1 if found */ | |
2161 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) | |
2162 { | |
2163 const char *p; | |
2164 char tag[128], *q; | |
2165 | |
2166 p = info; | |
2167 if (*p == '?') | |
2168 p++; | |
2169 for(;;) { | |
2170 q = tag; | |
2171 while (*p != '\0' && *p != '=' && *p != '&') { | |
2172 if ((q - tag) < sizeof(tag) - 1) | |
2173 *q++ = *p; | |
2174 p++; | |
2175 } | |
2176 *q = '\0'; | |
2177 q = arg; | |
2178 if (*p == '=') { | |
2179 p++; | |
2180 while (*p != '&' && *p != '\0') { | |
2181 if ((q - arg) < arg_size - 1) { | |
2182 if (*p == '+') | |
2183 *q++ = ' '; | |
2184 else | |
2185 *q++ = *p; | |
2186 } | |
2187 p++; | |
2188 } | |
2189 *q = '\0'; | |
2190 } | |
2191 if (!strcmp(tag, tag1)) | |
2192 return 1; | |
2193 if (*p != '&') | |
2194 break; | |
2195 p++; | |
2196 } | |
2197 return 0; | |
2198 } | |
2199 | |
2200 /* Return in 'buf' the path with '%d' replaced by number. Also handles | |
2201 the '%0nd' format where 'n' is the total number of digits and | |
2202 '%%'. Return 0 if OK, and -1 if format error */ | |
2203 int get_frame_filename(char *buf, int buf_size, | |
2204 const char *path, int number) | |
2205 { | |
2206 const char *p; | |
290
7a3ed84008ec
GCC 3.3.2 warnings patch by (Panagiotis Issaris <takis at lumumba dot luc dot ac dot be>)
michael
parents:
230
diff
changeset
|
2207 char *q, buf1[20], c; |
7a3ed84008ec
GCC 3.3.2 warnings patch by (Panagiotis Issaris <takis at lumumba dot luc dot ac dot be>)
michael
parents:
230
diff
changeset
|
2208 int nd, len, percentd_found; |
0 | 2209 |
2210 q = buf; | |
2211 p = path; | |
2212 percentd_found = 0; | |
2213 for(;;) { | |
2214 c = *p++; | |
2215 if (c == '\0') | |
2216 break; | |
2217 if (c == '%') { | |
9
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2218 do { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2219 nd = 0; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2220 while (isdigit(*p)) { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2221 nd = nd * 10 + *p++ - '0'; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2222 } |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2223 c = *p++; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2224 } while (isdigit(c)); |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
2225 |
0 | 2226 switch(c) { |
2227 case '%': | |
2228 goto addchar; | |
2229 case 'd': | |
2230 if (percentd_found) | |
2231 goto fail; | |
2232 percentd_found = 1; | |
2233 snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
2234 len = strlen(buf1); | |
2235 if ((q - buf + len) > buf_size - 1) | |
2236 goto fail; | |
2237 memcpy(q, buf1, len); | |
2238 q += len; | |
2239 break; | |
2240 default: | |
2241 goto fail; | |
2242 } | |
2243 } else { | |
2244 addchar: | |
2245 if ((q - buf) < buf_size - 1) | |
2246 *q++ = c; | |
2247 } | |
2248 } | |
2249 if (!percentd_found) | |
2250 goto fail; | |
2251 *q = '\0'; | |
2252 return 0; | |
2253 fail: | |
2254 *q = '\0'; | |
2255 return -1; | |
2256 } | |
2257 | |
2258 /** | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2259 * Print nice hexa dump of a buffer |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2260 * @param f stream for output |
0 | 2261 * @param buf buffer |
2262 * @param size buffer size | |
2263 */ | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2264 void av_hex_dump(FILE *f, uint8_t *buf, int size) |
0 | 2265 { |
2266 int len, i, j, c; | |
2267 | |
2268 for(i=0;i<size;i+=16) { | |
2269 len = size - i; | |
2270 if (len > 16) | |
2271 len = 16; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2272 fprintf(f, "%08x ", i); |
0 | 2273 for(j=0;j<16;j++) { |
2274 if (j < len) | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2275 fprintf(f, " %02x", buf[i+j]); |
0 | 2276 else |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2277 fprintf(f, " "); |
0 | 2278 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2279 fprintf(f, " "); |
0 | 2280 for(j=0;j<len;j++) { |
2281 c = buf[i+j]; | |
2282 if (c < ' ' || c > '~') | |
2283 c = '.'; | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2284 fprintf(f, "%c", c); |
0 | 2285 } |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2286 fprintf(f, "\n"); |
0 | 2287 } |
2288 } | |
2289 | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2290 /** |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2291 * Print on 'f' a nice dump of a packet |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2292 * @param f stream for output |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2293 * @param pkt packet to dump |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2294 * @param dump_payload true if the payload must be displayed too |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2295 */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2296 void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2297 { |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2298 fprintf(f, "stream #%d:\n", pkt->stream_index); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2299 fprintf(f, " keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0)); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2300 fprintf(f, " duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE); |
333 | 2301 /* DTS is _always_ valid after av_read_frame() */ |
2302 fprintf(f, " dts="); | |
2303 if (pkt->dts == AV_NOPTS_VALUE) | |
2304 fprintf(f, "N/A"); | |
2305 else | |
2306 fprintf(f, "%0.3f", (double)pkt->dts / AV_TIME_BASE); | |
303
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2307 /* PTS may be not known if B frames are present */ |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2308 fprintf(f, " pts="); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2309 if (pkt->pts == AV_NOPTS_VALUE) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2310 fprintf(f, "N/A"); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2311 else |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2312 fprintf(f, "%0.3f", (double)pkt->pts / AV_TIME_BASE); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2313 fprintf(f, "\n"); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2314 fprintf(f, " size=%d\n", pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2315 if (dump_payload) |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2316 av_hex_dump(f, pkt->data, pkt->size); |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2317 } |
2833c2311b66
initial av_read_frame() and av_seek_frame() support
bellard
parents:
293
diff
changeset
|
2318 |
0 | 2319 void url_split(char *proto, int proto_size, |
2320 char *hostname, int hostname_size, | |
2321 int *port_ptr, | |
2322 char *path, int path_size, | |
2323 const char *url) | |
2324 { | |
2325 const char *p; | |
2326 char *q; | |
2327 int port; | |
2328 | |
2329 port = -1; | |
2330 | |
2331 p = url; | |
2332 q = proto; | |
2333 while (*p != ':' && *p != '\0') { | |
2334 if ((q - proto) < proto_size - 1) | |
2335 *q++ = *p; | |
2336 p++; | |
2337 } | |
2338 if (proto_size > 0) | |
2339 *q = '\0'; | |
2340 if (*p == '\0') { | |
2341 if (proto_size > 0) | |
2342 proto[0] = '\0'; | |
2343 if (hostname_size > 0) | |
2344 hostname[0] = '\0'; | |
2345 p = url; | |
2346 } else { | |
2347 p++; | |
2348 if (*p == '/') | |
2349 p++; | |
2350 if (*p == '/') | |
2351 p++; | |
2352 q = hostname; | |
2353 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') { | |
2354 if ((q - hostname) < hostname_size - 1) | |
2355 *q++ = *p; | |
2356 p++; | |
2357 } | |
2358 if (hostname_size > 0) | |
2359 *q = '\0'; | |
2360 if (*p == ':') { | |
2361 p++; | |
2362 port = strtoul(p, (char **)&p, 10); | |
2363 } | |
2364 } | |
2365 if (port_ptr) | |
2366 *port_ptr = port; | |
2367 pstrcpy(path, path_size, p); | |
2368 } | |
2369 | |
2370 /** | |
2371 * Set the pts for a given stream | |
2372 * @param s stream | |
2373 * @param pts_wrap_bits number of bits effectively used by the pts | |
2374 * (used for wrap control, 33 is the value for MPEG) | |
2375 * @param pts_num numerator to convert to seconds (MPEG: 1) | |
2376 * @param pts_den denominator to convert to seconds (MPEG: 90000) | |
2377 */ | |
2378 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits, | |
2379 int pts_num, int pts_den) | |
2380 { | |
2381 s->pts_wrap_bits = pts_wrap_bits; | |
2382 s->pts_num = pts_num; | |
2383 s->pts_den = pts_den; | |
2384 } | |
2385 | |
2386 /* fraction handling */ | |
2387 | |
2388 /** | |
2389 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such | |
2390 * as 0 <= num < den. | |
2391 * | |
2392 * @param f fractional number | |
2393 * @param val integer value | |
2394 * @param num must be >= 0 | |
2395 * @param den must be >= 1 | |
2396 */ | |
65 | 2397 void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
0 | 2398 { |
2399 num += (den >> 1); | |
2400 if (num >= den) { | |
2401 val += num / den; | |
2402 num = num % den; | |
2403 } | |
2404 f->val = val; | |
2405 f->num = num; | |
2406 f->den = den; | |
2407 } | |
2408 | |
2409 /* set f to (val + 0.5) */ | |
65 | 2410 void av_frac_set(AVFrac *f, int64_t val) |
0 | 2411 { |
2412 f->val = val; | |
2413 f->num = f->den >> 1; | |
2414 } | |
2415 | |
2416 /** | |
2417 * Fractionnal addition to f: f = f + (incr / f->den) | |
2418 * | |
2419 * @param f fractional number | |
2420 * @param incr increment, can be positive or negative | |
2421 */ | |
65 | 2422 void av_frac_add(AVFrac *f, int64_t incr) |
0 | 2423 { |
65 | 2424 int64_t num, den; |
0 | 2425 |
2426 num = f->num + incr; | |
2427 den = f->den; | |
2428 if (num < 0) { | |
2429 f->val += num / den; | |
2430 num = num % den; | |
2431 if (num < 0) { | |
2432 num += den; | |
2433 f->val--; | |
2434 } | |
2435 } else if (num >= den) { | |
2436 f->val += num / den; | |
2437 num = num % den; | |
2438 } | |
2439 f->num = num; | |
2440 } | |
20 | 2441 |
2442 /** | |
2443 * register a new image format | |
2444 * @param img_fmt Image format descriptor | |
2445 */ | |
2446 void av_register_image_format(AVImageFormat *img_fmt) | |
2447 { | |
2448 AVImageFormat **p; | |
2449 | |
2450 p = &first_image_format; | |
2451 while (*p != NULL) p = &(*p)->next; | |
2452 *p = img_fmt; | |
2453 img_fmt->next = NULL; | |
2454 } | |
2455 | |
2456 /* guess image format */ | |
2457 AVImageFormat *av_probe_image_format(AVProbeData *pd) | |
2458 { | |
2459 AVImageFormat *fmt1, *fmt; | |
2460 int score, score_max; | |
2461 | |
2462 fmt = NULL; | |
2463 score_max = 0; | |
2464 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
2465 if (fmt1->img_probe) { | |
2466 score = fmt1->img_probe(pd); | |
2467 if (score > score_max) { | |
2468 score_max = score; | |
2469 fmt = fmt1; | |
2470 } | |
2471 } | |
2472 } | |
2473 return fmt; | |
2474 } | |
2475 | |
2476 AVImageFormat *guess_image_format(const char *filename) | |
2477 { | |
2478 AVImageFormat *fmt1; | |
2479 | |
2480 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
2481 if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
2482 return fmt1; | |
2483 } | |
2484 return NULL; | |
2485 } | |
2486 | |
2487 /** | |
2488 * Read an image from a stream. | |
2489 * @param gb byte stream containing the image | |
2490 * @param fmt image format, NULL if probing is required | |
2491 */ | |
2492 int av_read_image(ByteIOContext *pb, const char *filename, | |
2493 AVImageFormat *fmt, | |
2494 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
2495 { | |
2496 char buf[PROBE_BUF_SIZE]; | |
2497 AVProbeData probe_data, *pd = &probe_data; | |
2498 offset_t pos; | |
2499 int ret; | |
2500 | |
2501 if (!fmt) { | |
64 | 2502 pd->filename = filename; |
20 | 2503 pd->buf = buf; |
2504 pos = url_ftell(pb); | |
2505 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); | |
2506 url_fseek(pb, pos, SEEK_SET); | |
2507 fmt = av_probe_image_format(pd); | |
2508 } | |
2509 if (!fmt) | |
2510 return AVERROR_NOFMT; | |
2511 ret = fmt->img_read(pb, alloc_cb, opaque); | |
2512 return ret; | |
2513 } | |
2514 | |
2515 /** | |
2516 * Write an image to a stream. | |
2517 * @param pb byte stream for the image output | |
2518 * @param fmt image format | |
2519 * @param img image data and informations | |
2520 */ | |
2521 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
2522 { | |
2523 return fmt->img_write(pb, img); | |
2524 } | |
2525 |