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