Mercurial > libavformat.hg
annotate utils.c @ 181:20cd73ef6ead libavformat
ATI VCR1 decoder
author | michaelni |
---|---|
date | Thu, 24 Jul 2003 12:18:46 +0000 |
parents | b6fa8a3b78c6 |
children | 0316a506aeb0 |
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 #include <ctype.h> | |
22
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
21 #ifdef CONFIG_WIN32 |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
22 #define strcasecmp _stricmp |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
23 #include <sys/types.h> |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
24 #include <sys/timeb.h> |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
25 #elif defined(CONFIG_OS2) |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
26 #include <string.h> |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
27 #define strcasecmp stricmp |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
28 #include <sys/time.h> |
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
29 #else |
0 | 30 #include <unistd.h> |
31 #include <fcntl.h> | |
32 #include <sys/time.h> | |
33 #endif | |
34 #include <time.h> | |
35 | |
36 #ifndef HAVE_STRPTIME | |
37 #include "strptime.h" | |
38 #endif | |
39 | |
40 AVInputFormat *first_iformat; | |
41 AVOutputFormat *first_oformat; | |
20 | 42 AVImageFormat *first_image_format; |
0 | 43 |
44 void av_register_input_format(AVInputFormat *format) | |
45 { | |
46 AVInputFormat **p; | |
47 p = &first_iformat; | |
48 while (*p != NULL) p = &(*p)->next; | |
49 *p = format; | |
50 format->next = NULL; | |
51 } | |
52 | |
53 void av_register_output_format(AVOutputFormat *format) | |
54 { | |
55 AVOutputFormat **p; | |
56 p = &first_oformat; | |
57 while (*p != NULL) p = &(*p)->next; | |
58 *p = format; | |
59 format->next = NULL; | |
60 } | |
61 | |
62 int match_ext(const char *filename, const char *extensions) | |
63 { | |
64 const char *ext, *p; | |
65 char ext1[32], *q; | |
66 | |
67 ext = strrchr(filename, '.'); | |
68 if (ext) { | |
69 ext++; | |
70 p = extensions; | |
71 for(;;) { | |
72 q = ext1; | |
73 while (*p != '\0' && *p != ',') | |
74 *q++ = *p++; | |
75 *q = '\0'; | |
76 if (!strcasecmp(ext1, ext)) | |
77 return 1; | |
78 if (*p == '\0') | |
79 break; | |
80 p++; | |
81 } | |
82 } | |
83 return 0; | |
84 } | |
85 | |
86 AVOutputFormat *guess_format(const char *short_name, const char *filename, | |
87 const char *mime_type) | |
88 { | |
89 AVOutputFormat *fmt, *fmt_found; | |
90 int score_max, score; | |
91 | |
20 | 92 /* specific test for image sequences */ |
21 | 93 if (!short_name && filename && |
94 filename_number_test(filename) >= 0 && | |
95 guess_image_format(filename)) { | |
20 | 96 return guess_format("image", NULL, NULL); |
97 } | |
98 | |
0 | 99 /* find the proper file type */ |
100 fmt_found = NULL; | |
101 score_max = 0; | |
102 fmt = first_oformat; | |
103 while (fmt != NULL) { | |
104 score = 0; | |
105 if (fmt->name && short_name && !strcmp(fmt->name, short_name)) | |
106 score += 100; | |
107 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) | |
108 score += 10; | |
109 if (filename && fmt->extensions && | |
110 match_ext(filename, fmt->extensions)) { | |
111 score += 5; | |
112 } | |
113 if (score > score_max) { | |
114 score_max = score; | |
115 fmt_found = fmt; | |
116 } | |
117 fmt = fmt->next; | |
118 } | |
119 return fmt_found; | |
120 } | |
121 | |
122 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename, | |
123 const char *mime_type) | |
124 { | |
125 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type); | |
126 | |
127 if (fmt) { | |
128 AVOutputFormat *stream_fmt; | |
129 char stream_format_name[64]; | |
130 | |
131 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name); | |
132 stream_fmt = guess_format(stream_format_name, NULL, NULL); | |
133 | |
134 if (stream_fmt) | |
135 fmt = stream_fmt; | |
136 } | |
137 | |
138 return fmt; | |
139 } | |
140 | |
141 AVInputFormat *av_find_input_format(const char *short_name) | |
142 { | |
143 AVInputFormat *fmt; | |
144 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) { | |
145 if (!strcmp(fmt->name, short_name)) | |
146 return fmt; | |
147 } | |
148 return NULL; | |
149 } | |
150 | |
151 /* memory handling */ | |
152 | |
153 /** | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
154 * Default packet destructor |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
155 */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
156 static void av_destruct_packet(AVPacket *pkt) |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
157 { |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
158 av_free(pkt->data); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
159 pkt->data = NULL; pkt->size = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
160 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
161 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
162 /** |
0 | 163 * Allocate the payload of a packet and intialized its fields to default values. |
164 * | |
165 * @param pkt packet | |
166 * @param size wanted payload size | |
167 * @return 0 if OK. AVERROR_xxx otherwise. | |
168 */ | |
169 int av_new_packet(AVPacket *pkt, int size) | |
170 { | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
171 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
|
172 if (!data) |
0 | 173 return AVERROR_NOMEM; |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
174 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
0 | 175 |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
176 av_init_packet(pkt); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
177 pkt->data = data; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
178 pkt->size = size; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
32
diff
changeset
|
179 pkt->destruct = av_destruct_packet; |
0 | 180 return 0; |
181 } | |
182 | |
183 /* fifo handling */ | |
184 | |
185 int fifo_init(FifoBuffer *f, int size) | |
186 { | |
187 f->buffer = av_malloc(size); | |
188 if (!f->buffer) | |
189 return -1; | |
190 f->end = f->buffer + size; | |
191 f->wptr = f->rptr = f->buffer; | |
192 return 0; | |
193 } | |
194 | |
195 void fifo_free(FifoBuffer *f) | |
196 { | |
197 av_free(f->buffer); | |
198 } | |
199 | |
65 | 200 int fifo_size(FifoBuffer *f, uint8_t *rptr) |
0 | 201 { |
202 int size; | |
203 | |
204 if (f->wptr >= rptr) { | |
205 size = f->wptr - rptr; | |
206 } else { | |
207 size = (f->end - rptr) + (f->wptr - f->buffer); | |
208 } | |
209 return size; | |
210 } | |
211 | |
212 /* get data from the fifo (return -1 if not enough data) */ | |
65 | 213 int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr) |
0 | 214 { |
65 | 215 uint8_t *rptr = *rptr_ptr; |
0 | 216 int size, len; |
217 | |
218 if (f->wptr >= rptr) { | |
219 size = f->wptr - rptr; | |
220 } else { | |
221 size = (f->end - rptr) + (f->wptr - f->buffer); | |
222 } | |
223 | |
224 if (size < buf_size) | |
225 return -1; | |
226 while (buf_size > 0) { | |
227 len = f->end - rptr; | |
228 if (len > buf_size) | |
229 len = buf_size; | |
230 memcpy(buf, rptr, len); | |
231 buf += len; | |
232 rptr += len; | |
233 if (rptr >= f->end) | |
234 rptr = f->buffer; | |
235 buf_size -= len; | |
236 } | |
237 *rptr_ptr = rptr; | |
238 return 0; | |
239 } | |
240 | |
65 | 241 void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr) |
0 | 242 { |
243 int len; | |
65 | 244 uint8_t *wptr; |
0 | 245 wptr = *wptr_ptr; |
246 while (size > 0) { | |
247 len = f->end - wptr; | |
248 if (len > size) | |
249 len = size; | |
250 memcpy(wptr, buf, len); | |
251 wptr += len; | |
252 if (wptr >= f->end) | |
253 wptr = f->buffer; | |
254 buf += len; | |
255 size -= len; | |
256 } | |
257 *wptr_ptr = wptr; | |
258 } | |
259 | |
260 int filename_number_test(const char *filename) | |
261 { | |
262 char buf[1024]; | |
263 return get_frame_filename(buf, sizeof(buf), filename, 1); | |
264 } | |
265 | |
266 /* guess file format */ | |
267 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) | |
268 { | |
269 AVInputFormat *fmt1, *fmt; | |
270 int score, score_max; | |
271 | |
272 fmt = NULL; | |
273 score_max = 0; | |
274 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) { | |
275 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE)) | |
276 continue; | |
277 score = 0; | |
278 if (fmt1->read_probe) { | |
279 score = fmt1->read_probe(pd); | |
280 } else if (fmt1->extensions) { | |
281 if (match_ext(pd->filename, fmt1->extensions)) { | |
282 score = 50; | |
283 } | |
284 } | |
285 if (score > score_max) { | |
286 score_max = score; | |
287 fmt = fmt1; | |
288 } | |
289 } | |
290 return fmt; | |
291 } | |
292 | |
293 /************************************************************/ | |
294 /* input media file */ | |
295 | |
296 #define PROBE_BUF_SIZE 2048 | |
297 | |
298 /** | |
299 * Open a media file as input. The codec are not opened. Only the file | |
300 * header (if present) is read. | |
301 * | |
302 * @param ic_ptr the opened media file handle is put here | |
303 * @param filename filename to open. | |
304 * @param fmt if non NULL, force the file format to use | |
305 * @param buf_size optional buffer size (zero if default is OK) | |
306 * @param ap additionnal parameters needed when opening the file (NULL if default) | |
307 * @return 0 if OK. AVERROR_xxx otherwise. | |
308 */ | |
309 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, | |
310 AVInputFormat *fmt, | |
311 int buf_size, | |
312 AVFormatParameters *ap) | |
313 { | |
314 AVFormatContext *ic = NULL; | |
172 | 315 int err, must_open_file; |
0 | 316 char buf[PROBE_BUF_SIZE]; |
317 AVProbeData probe_data, *pd = &probe_data; | |
318 | |
319 ic = av_mallocz(sizeof(AVFormatContext)); | |
320 if (!ic) { | |
321 err = AVERROR_NOMEM; | |
322 goto fail; | |
323 } | |
324 pstrcpy(ic->filename, sizeof(ic->filename), filename); | |
325 pd->filename = ic->filename; | |
326 pd->buf = buf; | |
327 pd->buf_size = 0; | |
328 | |
329 if (!fmt) { | |
330 /* guess format if no file can be opened */ | |
331 fmt = av_probe_input_format(pd, 0); | |
332 } | |
333 | |
172 | 334 /* do not open file if the format does not need it. XXX: specific |
335 hack needed to handle RTSP/TCP */ | |
336 must_open_file = 1; | |
175 | 337 if ((fmt && (fmt->flags & AVFMT_NOFILE)) || |
172 | 338 (fmt == &rtp_demux && !strcmp(filename, "null"))) { |
339 must_open_file = 0; | |
340 } | |
341 | |
342 if (!fmt || must_open_file) { | |
20 | 343 /* if no file needed do not try to open one */ |
0 | 344 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) { |
345 err = AVERROR_IO; | |
346 goto fail; | |
347 } | |
348 if (buf_size > 0) { | |
349 url_setbufsize(&ic->pb, buf_size); | |
350 } | |
351 if (!fmt) { | |
352 /* read probe data */ | |
353 pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE); | |
354 url_fseek(&ic->pb, 0, SEEK_SET); | |
355 } | |
356 } | |
357 | |
358 /* guess file format */ | |
359 if (!fmt) { | |
360 fmt = av_probe_input_format(pd, 1); | |
361 } | |
362 | |
363 /* if still no format found, error */ | |
364 if (!fmt) { | |
365 err = AVERROR_NOFMT; | |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
85
diff
changeset
|
366 goto fail1; |
0 | 367 } |
368 | |
369 /* XXX: suppress this hack for redirectors */ | |
22
65433f1b2549
os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents:
21
diff
changeset
|
370 #ifdef CONFIG_NETWORK |
0 | 371 if (fmt == &redir_demux) { |
372 err = redir_open(ic_ptr, &ic->pb); | |
373 url_fclose(&ic->pb); | |
374 av_free(ic); | |
375 return err; | |
376 } | |
11
932b59c66c60
mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents:
9
diff
changeset
|
377 #endif |
0 | 378 |
379 ic->iformat = fmt; | |
380 | |
20 | 381 /* check filename in case of an image number is expected */ |
382 if (ic->iformat->flags & AVFMT_NEEDNUMBER) { | |
383 if (filename_number_test(ic->filename) < 0) { | |
384 err = AVERROR_NUMEXPECTED; | |
385 goto fail1; | |
386 } | |
387 } | |
388 | |
0 | 389 /* allocate private data */ |
32 | 390 if (fmt->priv_data_size > 0) { |
391 ic->priv_data = av_mallocz(fmt->priv_data_size); | |
392 if (!ic->priv_data) { | |
393 err = AVERROR_NOMEM; | |
90
27d6df9208d4
merging a small amount of the changes from BroadQ, the rest is either not clean / doesnt apply / or is PS2 specific (someone with a PS2 should merge/send a patch for the later)
michaelni
parents:
85
diff
changeset
|
394 goto fail1; |
32 | 395 } |
396 } else | |
397 ic->priv_data = NULL; | |
0 | 398 |
399 /* default pts settings is MPEG like */ | |
400 av_set_pts_info(ic, 33, 1, 90000); | |
401 | |
402 err = ic->iformat->read_header(ic, ap); | |
403 if (err < 0) | |
404 goto fail1; | |
405 *ic_ptr = ic; | |
406 return 0; | |
407 fail1: | |
172 | 408 if (!fmt || must_open_file) { |
0 | 409 url_fclose(&ic->pb); |
410 } | |
411 fail: | |
412 if (ic) { | |
413 av_freep(&ic->priv_data); | |
414 } | |
415 av_free(ic); | |
416 *ic_ptr = NULL; | |
417 return err; | |
418 } | |
419 | |
420 /** | |
421 * Read a packet from a media file | |
422 * @param s media file handle | |
423 * @param pkt is filled | |
424 * @return 0 if OK. AVERROR_xxx if error. | |
425 */ | |
426 int av_read_packet(AVFormatContext *s, AVPacket *pkt) | |
427 { | |
428 AVPacketList *pktl; | |
429 | |
430 pktl = s->packet_buffer; | |
431 if (pktl) { | |
432 /* read packet from packet buffer, if there is data */ | |
433 *pkt = pktl->pkt; | |
434 s->packet_buffer = pktl->next; | |
435 av_free(pktl); | |
436 return 0; | |
437 } else { | |
438 return s->iformat->read_packet(s, pkt); | |
439 } | |
440 } | |
441 | |
442 /* state for codec information */ | |
443 #define CSTATE_NOTFOUND 0 | |
444 #define CSTATE_DECODING 1 | |
445 #define CSTATE_FOUND 2 | |
446 | |
447 static int has_codec_parameters(AVCodecContext *enc) | |
448 { | |
449 int val; | |
450 switch(enc->codec_type) { | |
451 case CODEC_TYPE_AUDIO: | |
452 val = enc->sample_rate; | |
453 break; | |
454 case CODEC_TYPE_VIDEO: | |
455 val = enc->width; | |
456 break; | |
457 default: | |
458 val = 1; | |
459 break; | |
460 } | |
461 return (val != 0); | |
462 } | |
463 | |
464 /** | |
465 * Read the beginning of a media file to get stream information. This | |
466 * is useful for file formats with no headers such as MPEG. This | |
467 * function also compute the real frame rate in case of mpeg2 repeat | |
468 * frame mode. | |
469 * | |
470 * @param ic media file handle | |
471 * @return >=0 if OK. AVERROR_xxx if error. | |
472 */ | |
473 int av_find_stream_info(AVFormatContext *ic) | |
474 { | |
475 int i, count, ret, got_picture, size, read_size; | |
476 AVCodec *codec; | |
477 AVStream *st; | |
478 AVPacket *pkt; | |
7 | 479 AVFrame picture; |
0 | 480 AVPacketList *pktl=NULL, **ppktl; |
481 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2]; | |
65 | 482 uint8_t *ptr; |
0 | 483 int min_read_size, max_read_size; |
484 | |
485 /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10 | |
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
486 Mbits. We read at most 0.2 second of file to find all streams */ |
0 | 487 |
488 /* XXX: base it on stream bitrate when possible */ | |
489 if (ic->iformat == &mpegts_demux) { | |
490 /* maximum number of bytes we accept to read to find all the streams | |
491 in a file */ | |
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
492 min_read_size = 6000000; |
0 | 493 } else { |
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
494 min_read_size = 250000; |
0 | 495 } |
496 /* max read size is 2 seconds of video max */ | |
127
b7ce3b3dc171
VOB stream patch ba (Brian Foley <bfoley at compsoc dot nuigalway dot ie>)
michaelni
parents:
90
diff
changeset
|
497 max_read_size = min_read_size * 10; |
0 | 498 |
499 /* set initial codec state */ | |
500 for(i=0;i<ic->nb_streams;i++) { | |
501 st = ic->streams[i]; | |
502 if (has_codec_parameters(&st->codec)) | |
503 st->codec_info_state = CSTATE_FOUND; | |
504 else | |
505 st->codec_info_state = CSTATE_NOTFOUND; | |
506 st->codec_info_nb_repeat_frames = 0; | |
507 st->codec_info_nb_real_frames = 0; | |
508 } | |
509 | |
510 count = 0; | |
511 read_size = 0; | |
512 ppktl = &ic->packet_buffer; | |
513 for(;;) { | |
514 /* check if one codec still needs to be handled */ | |
515 for(i=0;i<ic->nb_streams;i++) { | |
516 st = ic->streams[i]; | |
517 if (st->codec_info_state != CSTATE_FOUND) | |
518 break; | |
519 } | |
520 if (i == ic->nb_streams) { | |
521 /* NOTE: if the format has no header, then we need to read | |
522 some packets to get most of the streams, so we cannot | |
523 stop here */ | |
524 if (!(ic->iformat->flags & AVFMT_NOHEADER) || | |
525 read_size >= min_read_size) { | |
526 /* if we found the info for all the codecs, we can stop */ | |
527 ret = count; | |
528 break; | |
529 } | |
530 } else { | |
531 /* we did not get all the codec info, but we read too much data */ | |
532 if (read_size >= max_read_size) { | |
533 ret = count; | |
534 break; | |
535 } | |
536 } | |
537 | |
538 pktl = av_mallocz(sizeof(AVPacketList)); | |
539 if (!pktl) { | |
540 ret = AVERROR_NOMEM; | |
541 break; | |
542 } | |
543 | |
544 /* add the packet in the buffered packet list */ | |
545 *ppktl = pktl; | |
546 ppktl = &pktl->next; | |
547 | |
548 /* NOTE: a new stream can be added there if no header in file | |
549 (AVFMT_NOHEADER) */ | |
550 pkt = &pktl->pkt; | |
551 if (ic->iformat->read_packet(ic, pkt) < 0) { | |
552 /* EOF or error */ | |
553 ret = -1; /* we could not have all the codec parameters before EOF */ | |
554 if ((ic->iformat->flags & AVFMT_NOHEADER) && | |
555 i == ic->nb_streams) | |
556 ret = 0; | |
557 break; | |
558 } | |
559 read_size += pkt->size; | |
560 | |
561 /* open new codecs */ | |
562 for(i=0;i<ic->nb_streams;i++) { | |
563 st = ic->streams[i]; | |
564 if (st->codec_info_state == CSTATE_NOTFOUND) { | |
565 /* set to found in case of error */ | |
566 st->codec_info_state = CSTATE_FOUND; | |
567 codec = avcodec_find_decoder(st->codec.codec_id); | |
568 if (codec) { | |
569 if(codec->capabilities & CODEC_CAP_TRUNCATED) | |
570 st->codec.flags |= CODEC_FLAG_TRUNCATED; | |
571 | |
572 ret = avcodec_open(&st->codec, codec); | |
573 if (ret >= 0) | |
574 st->codec_info_state = CSTATE_DECODING; | |
575 } | |
576 } | |
577 } | |
578 | |
579 st = ic->streams[pkt->stream_index]; | |
580 if (st->codec_info_state == CSTATE_DECODING) { | |
581 /* decode the data and update codec parameters */ | |
582 ptr = pkt->data; | |
583 size = pkt->size; | |
584 while (size > 0) { | |
585 switch(st->codec.codec_type) { | |
586 case CODEC_TYPE_VIDEO: | |
587 ret = avcodec_decode_video(&st->codec, &picture, | |
588 &got_picture, ptr, size); | |
589 break; | |
590 case CODEC_TYPE_AUDIO: | |
591 ret = avcodec_decode_audio(&st->codec, samples, | |
592 &got_picture, ptr, size); | |
593 break; | |
594 default: | |
595 ret = -1; | |
596 break; | |
597 } | |
598 if (ret < 0) { | |
599 /* if error, simply ignore because another packet | |
600 may be OK */ | |
601 break; | |
602 } | |
603 if (got_picture) { | |
604 /* we got the parameters - now we can stop | |
605 examining this stream */ | |
606 /* XXX: add a codec info so that we can decide if | |
607 the codec can repeat frames */ | |
608 if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO && | |
609 ic->iformat != &mpegts_demux && | |
610 st->codec.sub_id == 2) { | |
611 /* for mpeg2 video, we want to know the real | |
612 frame rate, so we decode 40 frames. In mpeg | |
613 TS case we do not do it because it would be | |
614 too long */ | |
615 st->codec_info_nb_real_frames++; | |
70
e851fa232508
move repeat_pict field from AVCodecContext -> AVFrame (closes bug #683536)
michaelni
parents:
65
diff
changeset
|
616 st->codec_info_nb_repeat_frames += st->codec.coded_frame->repeat_pict; |
0 | 617 #if 0 |
618 /* XXX: testing */ | |
619 if ((st->codec_info_nb_real_frames % 24) == 23) { | |
620 st->codec_info_nb_repeat_frames += 2; | |
621 } | |
622 #endif | |
623 /* stop after 40 frames */ | |
624 if (st->codec_info_nb_real_frames >= 40) { | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
625 av_reduce( |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
626 &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
|
627 &st->r_frame_rate_base, |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
628 (int64_t)st->codec.frame_rate * st->codec_info_nb_real_frames, |
162
35386fc4d47d
mpeg1 bad frame_rate_base fix by (Arthur van Hoff (javanator))
michaelni
parents:
127
diff
changeset
|
629 (st->codec_info_nb_real_frames + (st->codec_info_nb_repeat_frames >> 1)) * st->codec.frame_rate_base, |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
630 1<<30); |
0 | 631 goto close_codec; |
632 } | |
633 } else { | |
634 close_codec: | |
635 st->codec_info_state = CSTATE_FOUND; | |
636 avcodec_close(&st->codec); | |
637 break; | |
638 } | |
639 } | |
640 ptr += ret; | |
641 size -= ret; | |
642 } | |
643 } | |
644 count++; | |
645 } | |
646 | |
647 /* close each codec if there are opened */ | |
648 for(i=0;i<ic->nb_streams;i++) { | |
649 st = ic->streams[i]; | |
650 if (st->codec_info_state == CSTATE_DECODING) | |
651 avcodec_close(&st->codec); | |
652 } | |
653 | |
654 /* set real frame rate info */ | |
655 for(i=0;i<ic->nb_streams;i++) { | |
656 st = ic->streams[i]; | |
657 if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
75
diff
changeset
|
658 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
|
659 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
|
660 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
|
661 } |
0 | 662 } |
663 } | |
664 | |
665 return ret; | |
666 } | |
667 | |
668 /** | |
669 * Close a media file (but not its codecs) | |
670 * | |
671 * @param s media file handle | |
672 */ | |
673 void av_close_input_file(AVFormatContext *s) | |
674 { | |
172 | 675 int i, must_open_file; |
0 | 676 |
677 if (s->iformat->read_close) | |
678 s->iformat->read_close(s); | |
679 for(i=0;i<s->nb_streams;i++) { | |
680 av_free(s->streams[i]); | |
681 } | |
682 if (s->packet_buffer) { | |
683 AVPacketList *p, *p1; | |
684 p = s->packet_buffer; | |
685 while (p != NULL) { | |
686 p1 = p->next; | |
687 av_free_packet(&p->pkt); | |
688 av_free(p); | |
689 p = p1; | |
690 } | |
691 s->packet_buffer = NULL; | |
692 } | |
172 | 693 must_open_file = 1; |
694 if ((s->iformat->flags & AVFMT_NOFILE) || | |
695 (s->iformat == &rtp_demux && !strcmp(s->filename, "null"))) { | |
696 must_open_file = 0; | |
697 } | |
698 if (must_open_file) { | |
0 | 699 url_fclose(&s->pb); |
700 } | |
701 av_freep(&s->priv_data); | |
702 av_free(s); | |
703 } | |
704 | |
705 /** | |
706 * Add a new stream to a media file. Can only be called in the | |
707 * read_header function. If the flag AVFMT_NOHEADER is in the format | |
708 * description, then new streams can be added in read_packet too. | |
709 * | |
710 * | |
711 * @param s media file handle | |
712 * @param id file format dependent stream id | |
713 */ | |
714 AVStream *av_new_stream(AVFormatContext *s, int id) | |
715 { | |
716 AVStream *st; | |
717 | |
718 if (s->nb_streams >= MAX_STREAMS) | |
719 return NULL; | |
720 | |
721 st = av_mallocz(sizeof(AVStream)); | |
722 if (!st) | |
723 return NULL; | |
5 | 724 avcodec_get_context_defaults(&st->codec); |
725 | |
0 | 726 st->index = s->nb_streams; |
727 st->id = id; | |
728 s->streams[s->nb_streams++] = st; | |
729 return st; | |
730 } | |
731 | |
732 /************************************************************/ | |
733 /* output media file */ | |
734 | |
20 | 735 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
736 { | |
737 int ret; | |
32 | 738 |
739 if (s->oformat->priv_data_size > 0) { | |
740 s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
741 if (!s->priv_data) | |
742 return AVERROR_NOMEM; | |
743 } else | |
744 s->priv_data = NULL; | |
745 | |
20 | 746 if (s->oformat->set_parameters) { |
747 ret = s->oformat->set_parameters(s, ap); | |
748 if (ret < 0) | |
749 return ret; | |
750 } | |
751 return 0; | |
752 } | |
753 | |
0 | 754 /** |
755 * allocate the stream private data and write the stream header to an | |
756 * output media file | |
757 * | |
758 * @param s media file handle | |
759 * @return 0 if OK. AVERROR_xxx if error. | |
760 */ | |
761 int av_write_header(AVFormatContext *s) | |
762 { | |
763 int ret, i; | |
764 AVStream *st; | |
765 | |
766 /* default pts settings is MPEG like */ | |
767 av_set_pts_info(s, 33, 1, 90000); | |
768 ret = s->oformat->write_header(s); | |
769 if (ret < 0) | |
770 return ret; | |
771 | |
772 /* init PTS generation */ | |
773 for(i=0;i<s->nb_streams;i++) { | |
774 st = s->streams[i]; | |
775 | |
776 switch (st->codec.codec_type) { | |
777 case CODEC_TYPE_AUDIO: | |
778 av_frac_init(&st->pts, 0, 0, | |
65 | 779 (int64_t)s->pts_num * st->codec.sample_rate); |
0 | 780 break; |
781 case CODEC_TYPE_VIDEO: | |
782 av_frac_init(&st->pts, 0, 0, | |
65 | 783 (int64_t)s->pts_num * st->codec.frame_rate); |
0 | 784 break; |
785 default: | |
786 break; | |
787 } | |
788 } | |
789 return 0; | |
790 } | |
791 | |
792 /** | |
793 * Write a packet to an output media file. The packet shall contain | |
794 * one audio or video frame. | |
795 * | |
796 * @param s media file handle | |
797 * @param stream_index stream index | |
798 * @param buf buffer containing the frame data | |
799 * @param size size of buffer | |
800 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
801 */ | |
802 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, | |
803 int size) | |
804 { | |
805 AVStream *st; | |
65 | 806 int64_t pts_mask; |
0 | 807 int ret, frame_size; |
808 | |
809 st = s->streams[stream_index]; | |
810 pts_mask = (1LL << s->pts_wrap_bits) - 1; | |
811 ret = s->oformat->write_packet(s, stream_index, (uint8_t *)buf, size, | |
812 st->pts.val & pts_mask); | |
813 if (ret < 0) | |
814 return ret; | |
815 | |
816 /* update pts */ | |
817 switch (st->codec.codec_type) { | |
818 case CODEC_TYPE_AUDIO: | |
819 if (st->codec.frame_size <= 1) { | |
820 frame_size = size / st->codec.channels; | |
821 /* specific hack for pcm codecs because no frame size is provided */ | |
822 switch(st->codec.codec_id) { | |
823 case CODEC_ID_PCM_S16LE: | |
824 case CODEC_ID_PCM_S16BE: | |
825 case CODEC_ID_PCM_U16LE: | |
826 case CODEC_ID_PCM_U16BE: | |
827 frame_size >>= 1; | |
828 break; | |
829 default: | |
830 break; | |
831 } | |
832 } else { | |
833 frame_size = st->codec.frame_size; | |
834 } | |
835 av_frac_add(&st->pts, | |
65 | 836 (int64_t)s->pts_den * frame_size); |
0 | 837 break; |
838 case CODEC_TYPE_VIDEO: | |
839 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
|
840 (int64_t)s->pts_den * st->codec.frame_rate_base); |
0 | 841 break; |
842 default: | |
843 break; | |
844 } | |
845 return ret; | |
846 } | |
847 | |
848 /** | |
849 * write the stream trailer to an output media file and and free the | |
850 * file private data. | |
851 * | |
852 * @param s media file handle | |
853 * @return 0 if OK. AVERROR_xxx if error. */ | |
854 int av_write_trailer(AVFormatContext *s) | |
855 { | |
856 int ret; | |
857 ret = s->oformat->write_trailer(s); | |
858 av_freep(&s->priv_data); | |
859 return ret; | |
860 } | |
861 | |
862 /* "user interface" functions */ | |
863 | |
864 void dump_format(AVFormatContext *ic, | |
865 int index, | |
866 const char *url, | |
867 int is_output) | |
868 { | |
869 int i, flags; | |
870 char buf[256]; | |
871 | |
872 fprintf(stderr, "%s #%d, %s, %s '%s':\n", | |
873 is_output ? "Output" : "Input", | |
874 index, | |
875 is_output ? ic->oformat->name : ic->iformat->name, | |
876 is_output ? "to" : "from", url); | |
877 for(i=0;i<ic->nb_streams;i++) { | |
878 AVStream *st = ic->streams[i]; | |
879 avcodec_string(buf, sizeof(buf), &st->codec, is_output); | |
880 fprintf(stderr, " Stream #%d.%d", index, i); | |
881 /* the pid is an important information, so we display it */ | |
882 /* XXX: add a generic system */ | |
883 if (is_output) | |
884 flags = ic->oformat->flags; | |
885 else | |
886 flags = ic->iformat->flags; | |
887 if (flags & AVFMT_SHOW_IDS) { | |
888 fprintf(stderr, "[0x%x]", st->id); | |
889 } | |
890 fprintf(stderr, ": %s\n", buf); | |
891 } | |
892 } | |
893 | |
894 typedef struct { | |
168 | 895 const char *abv; |
0 | 896 int width, height; |
168 | 897 int frame_rate, frame_rate_base; |
898 } AbvEntry; | |
0 | 899 |
168 | 900 static AbvEntry frame_abvs[] = { |
901 { "ntsc", 352, 240, 30000, 1001 }, | |
902 { "pal", 352, 288, 25, 1 }, | |
903 { "film", 352, 240, 24, 1 }, | |
904 { "ntsc-film", 352, 240, 24000, 1001 }, | |
905 { "sqcif", 128, 96, 0, 0 }, | |
906 { "qcif", 176, 144, 0, 0 }, | |
907 { "cif", 352, 288, 0, 0 }, | |
908 { "4cif", 704, 576, 0, 0 }, | |
0 | 909 }; |
168 | 910 |
0 | 911 int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
912 { | |
913 int i; | |
168 | 914 int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
0 | 915 const char *p; |
916 int frame_width = 0, frame_height = 0; | |
917 | |
918 for(i=0;i<n;i++) { | |
168 | 919 if (!strcmp(frame_abvs[i].abv, str)) { |
920 frame_width = frame_abvs[i].width; | |
921 frame_height = frame_abvs[i].height; | |
0 | 922 break; |
923 } | |
924 } | |
925 if (i == n) { | |
926 p = str; | |
927 frame_width = strtol(p, (char **)&p, 10); | |
928 if (*p) | |
929 p++; | |
930 frame_height = strtol(p, (char **)&p, 10); | |
931 } | |
932 if (frame_width <= 0 || frame_height <= 0) | |
933 return -1; | |
934 *width_ptr = frame_width; | |
935 *height_ptr = frame_height; | |
936 return 0; | |
937 } | |
938 | |
168 | 939 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
940 { | |
941 int i; | |
942 char* cp; | |
943 | |
944 /* First, we check our abbreviation table */ | |
945 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
946 if (!strcmp(frame_abvs[i].abv, arg)) { | |
947 *frame_rate = frame_abvs[i].frame_rate; | |
948 *frame_rate_base = frame_abvs[i].frame_rate_base; | |
949 return 0; | |
950 } | |
951 | |
952 /* Then, we try to parse it as fraction */ | |
953 cp = strchr(arg, '/'); | |
954 if (cp) { | |
955 char* cpp; | |
956 *frame_rate = strtol(arg, &cpp, 10); | |
957 if (cpp != arg || cpp == cp) | |
958 *frame_rate_base = strtol(cp+1, &cpp, 10); | |
959 else | |
960 *frame_rate = 0; | |
961 } | |
962 else { | |
963 /* Finally we give up and parse it as double */ | |
964 *frame_rate_base = DEFAULT_FRAME_RATE_BASE; | |
965 *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5); | |
966 } | |
967 if (!*frame_rate || !*frame_rate_base) | |
968 return -1; | |
969 else | |
970 return 0; | |
971 } | |
972 | |
65 | 973 int64_t av_gettime(void) |
0 | 974 { |
975 #ifdef CONFIG_WIN32 | |
976 struct _timeb tb; | |
977 _ftime(&tb); | |
65 | 978 return ((int64_t)tb.time * int64_t_C(1000) + (int64_t)tb.millitm) * int64_t_C(1000); |
0 | 979 #else |
980 struct timeval tv; | |
981 gettimeofday(&tv,NULL); | |
65 | 982 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; |
0 | 983 #endif |
984 } | |
985 | |
986 static time_t mktimegm(struct tm *tm) | |
987 { | |
988 time_t t; | |
989 | |
990 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday; | |
991 | |
992 if (m < 3) { | |
993 m += 12; | |
994 y--; | |
995 } | |
996 | |
997 t = 86400 * | |
998 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469); | |
999 | |
1000 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; | |
1001 | |
1002 return t; | |
1003 } | |
1004 | |
1005 /* Syntax: | |
1006 * - If not a duration: | |
1007 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
1008 * Time is localtime unless Z is suffixed to the end. In this case GMT | |
1009 * Return the date in micro seconds since 1970 | |
1010 * - If duration: | |
1011 * HH[:MM[:SS[.m...]]] | |
1012 * S+[.m...] | |
1013 */ | |
65 | 1014 int64_t parse_date(const char *datestr, int duration) |
0 | 1015 { |
1016 const char *p; | |
65 | 1017 int64_t t; |
0 | 1018 struct tm dt; |
1019 int i; | |
1020 static const char *date_fmt[] = { | |
1021 "%Y-%m-%d", | |
1022 "%Y%m%d", | |
1023 }; | |
1024 static const char *time_fmt[] = { | |
1025 "%H:%M:%S", | |
1026 "%H%M%S", | |
1027 }; | |
1028 const char *q; | |
1029 int is_utc, len; | |
1030 char lastch; | |
1031 time_t now = time(0); | |
1032 | |
1033 len = strlen(datestr); | |
1034 if (len > 0) | |
1035 lastch = datestr[len - 1]; | |
1036 else | |
1037 lastch = '\0'; | |
1038 is_utc = (lastch == 'z' || lastch == 'Z'); | |
1039 | |
1040 memset(&dt, 0, sizeof(dt)); | |
1041 | |
1042 p = datestr; | |
1043 q = NULL; | |
1044 if (!duration) { | |
1045 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { | |
1046 q = strptime(p, date_fmt[i], &dt); | |
1047 if (q) { | |
1048 break; | |
1049 } | |
1050 } | |
1051 | |
1052 if (!q) { | |
1053 if (is_utc) { | |
1054 dt = *gmtime(&now); | |
1055 } else { | |
1056 dt = *localtime(&now); | |
1057 } | |
1058 dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
1059 } else { | |
1060 p = q; | |
1061 } | |
1062 | |
1063 if (*p == 'T' || *p == 't' || *p == ' ') | |
1064 p++; | |
1065 | |
1066 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { | |
1067 q = strptime(p, time_fmt[i], &dt); | |
1068 if (q) { | |
1069 break; | |
1070 } | |
1071 } | |
1072 } else { | |
1073 q = strptime(p, time_fmt[0], &dt); | |
1074 if (!q) { | |
1075 dt.tm_sec = strtol(p, (char **)&q, 10); | |
1076 dt.tm_min = 0; | |
1077 dt.tm_hour = 0; | |
1078 } | |
1079 } | |
1080 | |
1081 /* Now we have all the fields that we can get */ | |
1082 if (!q) { | |
1083 if (duration) | |
1084 return 0; | |
1085 else | |
65 | 1086 return now * int64_t_C(1000000); |
0 | 1087 } |
1088 | |
1089 if (duration) { | |
1090 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; | |
1091 } else { | |
1092 dt.tm_isdst = -1; /* unknown */ | |
1093 if (is_utc) { | |
1094 t = mktimegm(&dt); | |
1095 } else { | |
1096 t = mktime(&dt); | |
1097 } | |
1098 } | |
1099 | |
1100 t *= 1000000; | |
1101 | |
1102 if (*q == '.') { | |
1103 int val, n; | |
1104 q++; | |
1105 for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
1106 if (!isdigit(*q)) | |
1107 break; | |
1108 val += n * (*q - '0'); | |
1109 } | |
1110 t += val; | |
1111 } | |
1112 return t; | |
1113 } | |
1114 | |
1115 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return | |
1116 1 if found */ | |
1117 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) | |
1118 { | |
1119 const char *p; | |
1120 char tag[128], *q; | |
1121 | |
1122 p = info; | |
1123 if (*p == '?') | |
1124 p++; | |
1125 for(;;) { | |
1126 q = tag; | |
1127 while (*p != '\0' && *p != '=' && *p != '&') { | |
1128 if ((q - tag) < sizeof(tag) - 1) | |
1129 *q++ = *p; | |
1130 p++; | |
1131 } | |
1132 *q = '\0'; | |
1133 q = arg; | |
1134 if (*p == '=') { | |
1135 p++; | |
1136 while (*p != '&' && *p != '\0') { | |
1137 if ((q - arg) < arg_size - 1) { | |
1138 if (*p == '+') | |
1139 *q++ = ' '; | |
1140 else | |
1141 *q++ = *p; | |
1142 } | |
1143 p++; | |
1144 } | |
1145 *q = '\0'; | |
1146 } | |
1147 if (!strcmp(tag, tag1)) | |
1148 return 1; | |
1149 if (*p != '&') | |
1150 break; | |
1151 p++; | |
1152 } | |
1153 return 0; | |
1154 } | |
1155 | |
1156 /* Return in 'buf' the path with '%d' replaced by number. Also handles | |
1157 the '%0nd' format where 'n' is the total number of digits and | |
1158 '%%'. Return 0 if OK, and -1 if format error */ | |
1159 int get_frame_filename(char *buf, int buf_size, | |
1160 const char *path, int number) | |
1161 { | |
1162 const char *p; | |
1163 char *q, buf1[20]; | |
1164 int nd, len, c, percentd_found; | |
1165 | |
1166 q = buf; | |
1167 p = path; | |
1168 percentd_found = 0; | |
1169 for(;;) { | |
1170 c = *p++; | |
1171 if (c == '\0') | |
1172 break; | |
1173 if (c == '%') { | |
9
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1174 do { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1175 nd = 0; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1176 while (isdigit(*p)) { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1177 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
|
1178 } |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1179 c = *p++; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1180 if (c == '*' && nd > 0) { |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1181 // The nd field is actually the modulus |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1182 number = number % nd; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1183 c = *p++; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1184 nd = 0; |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1185 } |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1186 } while (isdigit(c)); |
97e61383cb81
* Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents:
7
diff
changeset
|
1187 |
0 | 1188 switch(c) { |
1189 case '%': | |
1190 goto addchar; | |
1191 case 'd': | |
1192 if (percentd_found) | |
1193 goto fail; | |
1194 percentd_found = 1; | |
1195 snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
1196 len = strlen(buf1); | |
1197 if ((q - buf + len) > buf_size - 1) | |
1198 goto fail; | |
1199 memcpy(q, buf1, len); | |
1200 q += len; | |
1201 break; | |
1202 default: | |
1203 goto fail; | |
1204 } | |
1205 } else { | |
1206 addchar: | |
1207 if ((q - buf) < buf_size - 1) | |
1208 *q++ = c; | |
1209 } | |
1210 } | |
1211 if (!percentd_found) | |
1212 goto fail; | |
1213 *q = '\0'; | |
1214 return 0; | |
1215 fail: | |
1216 *q = '\0'; | |
1217 return -1; | |
1218 } | |
1219 | |
1220 /** | |
1221 * | |
1222 * Print on stdout a nice hexa dump of a buffer | |
1223 * @param buf buffer | |
1224 * @param size buffer size | |
1225 */ | |
65 | 1226 void av_hex_dump(uint8_t *buf, int size) |
0 | 1227 { |
1228 int len, i, j, c; | |
1229 | |
1230 for(i=0;i<size;i+=16) { | |
1231 len = size - i; | |
1232 if (len > 16) | |
1233 len = 16; | |
1234 printf("%08x ", i); | |
1235 for(j=0;j<16;j++) { | |
1236 if (j < len) | |
1237 printf(" %02x", buf[i+j]); | |
1238 else | |
1239 printf(" "); | |
1240 } | |
1241 printf(" "); | |
1242 for(j=0;j<len;j++) { | |
1243 c = buf[i+j]; | |
1244 if (c < ' ' || c > '~') | |
1245 c = '.'; | |
1246 printf("%c", c); | |
1247 } | |
1248 printf("\n"); | |
1249 } | |
1250 } | |
1251 | |
1252 void url_split(char *proto, int proto_size, | |
1253 char *hostname, int hostname_size, | |
1254 int *port_ptr, | |
1255 char *path, int path_size, | |
1256 const char *url) | |
1257 { | |
1258 const char *p; | |
1259 char *q; | |
1260 int port; | |
1261 | |
1262 port = -1; | |
1263 | |
1264 p = url; | |
1265 q = proto; | |
1266 while (*p != ':' && *p != '\0') { | |
1267 if ((q - proto) < proto_size - 1) | |
1268 *q++ = *p; | |
1269 p++; | |
1270 } | |
1271 if (proto_size > 0) | |
1272 *q = '\0'; | |
1273 if (*p == '\0') { | |
1274 if (proto_size > 0) | |
1275 proto[0] = '\0'; | |
1276 if (hostname_size > 0) | |
1277 hostname[0] = '\0'; | |
1278 p = url; | |
1279 } else { | |
1280 p++; | |
1281 if (*p == '/') | |
1282 p++; | |
1283 if (*p == '/') | |
1284 p++; | |
1285 q = hostname; | |
1286 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') { | |
1287 if ((q - hostname) < hostname_size - 1) | |
1288 *q++ = *p; | |
1289 p++; | |
1290 } | |
1291 if (hostname_size > 0) | |
1292 *q = '\0'; | |
1293 if (*p == ':') { | |
1294 p++; | |
1295 port = strtoul(p, (char **)&p, 10); | |
1296 } | |
1297 } | |
1298 if (port_ptr) | |
1299 *port_ptr = port; | |
1300 pstrcpy(path, path_size, p); | |
1301 } | |
1302 | |
1303 /** | |
1304 * Set the pts for a given stream | |
1305 * @param s stream | |
1306 * @param pts_wrap_bits number of bits effectively used by the pts | |
1307 * (used for wrap control, 33 is the value for MPEG) | |
1308 * @param pts_num numerator to convert to seconds (MPEG: 1) | |
1309 * @param pts_den denominator to convert to seconds (MPEG: 90000) | |
1310 */ | |
1311 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits, | |
1312 int pts_num, int pts_den) | |
1313 { | |
1314 s->pts_wrap_bits = pts_wrap_bits; | |
1315 s->pts_num = pts_num; | |
1316 s->pts_den = pts_den; | |
1317 } | |
1318 | |
1319 /* fraction handling */ | |
1320 | |
1321 /** | |
1322 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such | |
1323 * as 0 <= num < den. | |
1324 * | |
1325 * @param f fractional number | |
1326 * @param val integer value | |
1327 * @param num must be >= 0 | |
1328 * @param den must be >= 1 | |
1329 */ | |
65 | 1330 void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
0 | 1331 { |
1332 num += (den >> 1); | |
1333 if (num >= den) { | |
1334 val += num / den; | |
1335 num = num % den; | |
1336 } | |
1337 f->val = val; | |
1338 f->num = num; | |
1339 f->den = den; | |
1340 } | |
1341 | |
1342 /* set f to (val + 0.5) */ | |
65 | 1343 void av_frac_set(AVFrac *f, int64_t val) |
0 | 1344 { |
1345 f->val = val; | |
1346 f->num = f->den >> 1; | |
1347 } | |
1348 | |
1349 /** | |
1350 * Fractionnal addition to f: f = f + (incr / f->den) | |
1351 * | |
1352 * @param f fractional number | |
1353 * @param incr increment, can be positive or negative | |
1354 */ | |
65 | 1355 void av_frac_add(AVFrac *f, int64_t incr) |
0 | 1356 { |
65 | 1357 int64_t num, den; |
0 | 1358 |
1359 num = f->num + incr; | |
1360 den = f->den; | |
1361 if (num < 0) { | |
1362 f->val += num / den; | |
1363 num = num % den; | |
1364 if (num < 0) { | |
1365 num += den; | |
1366 f->val--; | |
1367 } | |
1368 } else if (num >= den) { | |
1369 f->val += num / den; | |
1370 num = num % den; | |
1371 } | |
1372 f->num = num; | |
1373 } | |
20 | 1374 |
1375 /** | |
1376 * register a new image format | |
1377 * @param img_fmt Image format descriptor | |
1378 */ | |
1379 void av_register_image_format(AVImageFormat *img_fmt) | |
1380 { | |
1381 AVImageFormat **p; | |
1382 | |
1383 p = &first_image_format; | |
1384 while (*p != NULL) p = &(*p)->next; | |
1385 *p = img_fmt; | |
1386 img_fmt->next = NULL; | |
1387 } | |
1388 | |
1389 /* guess image format */ | |
1390 AVImageFormat *av_probe_image_format(AVProbeData *pd) | |
1391 { | |
1392 AVImageFormat *fmt1, *fmt; | |
1393 int score, score_max; | |
1394 | |
1395 fmt = NULL; | |
1396 score_max = 0; | |
1397 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
1398 if (fmt1->img_probe) { | |
1399 score = fmt1->img_probe(pd); | |
1400 if (score > score_max) { | |
1401 score_max = score; | |
1402 fmt = fmt1; | |
1403 } | |
1404 } | |
1405 } | |
1406 return fmt; | |
1407 } | |
1408 | |
1409 AVImageFormat *guess_image_format(const char *filename) | |
1410 { | |
1411 AVImageFormat *fmt1; | |
1412 | |
1413 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
1414 if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
1415 return fmt1; | |
1416 } | |
1417 return NULL; | |
1418 } | |
1419 | |
1420 /** | |
1421 * Read an image from a stream. | |
1422 * @param gb byte stream containing the image | |
1423 * @param fmt image format, NULL if probing is required | |
1424 */ | |
1425 int av_read_image(ByteIOContext *pb, const char *filename, | |
1426 AVImageFormat *fmt, | |
1427 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
1428 { | |
1429 char buf[PROBE_BUF_SIZE]; | |
1430 AVProbeData probe_data, *pd = &probe_data; | |
1431 offset_t pos; | |
1432 int ret; | |
1433 | |
1434 if (!fmt) { | |
64 | 1435 pd->filename = filename; |
20 | 1436 pd->buf = buf; |
1437 pos = url_ftell(pb); | |
1438 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); | |
1439 url_fseek(pb, pos, SEEK_SET); | |
1440 fmt = av_probe_image_format(pd); | |
1441 } | |
1442 if (!fmt) | |
1443 return AVERROR_NOFMT; | |
1444 ret = fmt->img_read(pb, alloc_cb, opaque); | |
1445 return ret; | |
1446 } | |
1447 | |
1448 /** | |
1449 * Write an image to a stream. | |
1450 * @param pb byte stream for the image output | |
1451 * @param fmt image format | |
1452 * @param img image data and informations | |
1453 */ | |
1454 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
1455 { | |
1456 return fmt->img_write(pb, img); | |
1457 } | |
1458 |