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