2 * Various utilities for ffmpeg system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
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.
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.
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
21 AVInputFormat *first_iformat;
22 AVOutputFormat *first_oformat;
23 AVImageFormat *first_image_format;
25 void av_register_input_format(AVInputFormat *format)
29 while (*p != NULL) p = &(*p)->next;
34 void av_register_output_format(AVOutputFormat *format)
38 while (*p != NULL) p = &(*p)->next;
43 int match_ext(const char *filename, const char *extensions)
48 ext = strrchr(filename, '.');
54 while (*p != '\0' && *p != ',')
57 if (!strcasecmp(ext1, ext))
67 AVOutputFormat *guess_format(const char *short_name, const char *filename,
68 const char *mime_type)
70 AVOutputFormat *fmt, *fmt_found;
73 /* specific test for image sequences */
74 if (!short_name && filename &&
75 filename_number_test(filename) >= 0 &&
76 guess_image_format(filename)) {
77 return guess_format("image", NULL, NULL);
80 /* find the proper file type */
86 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
88 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
90 if (filename && fmt->extensions &&
91 match_ext(filename, fmt->extensions)) {
94 if (score > score_max) {
103 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename,
104 const char *mime_type)
106 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type);
109 AVOutputFormat *stream_fmt;
110 char stream_format_name[64];
112 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
113 stream_fmt = guess_format(stream_format_name, NULL, NULL);
122 AVInputFormat *av_find_input_format(const char *short_name)
125 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) {
126 if (!strcmp(fmt->name, short_name))
132 /* memory handling */
135 * Default packet destructor
137 static void av_destruct_packet(AVPacket *pkt)
140 pkt->data = NULL; pkt->size = 0;
144 * Allocate the payload of a packet and intialized its fields to default values.
147 * @param size wanted payload size
148 * @return 0 if OK. AVERROR_xxx otherwise.
150 int av_new_packet(AVPacket *pkt, int size)
152 void *data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
154 return AVERROR_NOMEM;
155 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
160 pkt->destruct = av_destruct_packet;
166 int fifo_init(FifoBuffer *f, int size)
168 f->buffer = av_malloc(size);
171 f->end = f->buffer + size;
172 f->wptr = f->rptr = f->buffer;
176 void fifo_free(FifoBuffer *f)
181 int fifo_size(FifoBuffer *f, uint8_t *rptr)
185 if (f->wptr >= rptr) {
186 size = f->wptr - rptr;
188 size = (f->end - rptr) + (f->wptr - f->buffer);
193 /* get data from the fifo (return -1 if not enough data) */
194 int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr)
196 uint8_t *rptr = *rptr_ptr;
199 if (f->wptr >= rptr) {
200 size = f->wptr - rptr;
202 size = (f->end - rptr) + (f->wptr - f->buffer);
207 while (buf_size > 0) {
211 memcpy(buf, rptr, len);
222 void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr)
231 memcpy(wptr, buf, len);
241 int filename_number_test(const char *filename)
244 return get_frame_filename(buf, sizeof(buf), filename, 1);
247 /* guess file format */
248 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
250 AVInputFormat *fmt1, *fmt;
251 int score, score_max;
255 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) {
256 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE))
259 if (fmt1->read_probe) {
260 score = fmt1->read_probe(pd);
261 } else if (fmt1->extensions) {
262 if (match_ext(pd->filename, fmt1->extensions)) {
266 if (score > score_max) {
274 /************************************************************/
275 /* input media file */
277 #define PROBE_BUF_SIZE 2048
280 * Open a media file as input. The codec are not opened. Only the file
281 * header (if present) is read.
283 * @param ic_ptr the opened media file handle is put here
284 * @param filename filename to open.
285 * @param fmt if non NULL, force the file format to use
286 * @param buf_size optional buffer size (zero if default is OK)
287 * @param ap additionnal parameters needed when opening the file (NULL if default)
288 * @return 0 if OK. AVERROR_xxx otherwise.
290 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
293 AVFormatParameters *ap)
295 AVFormatContext *ic = NULL;
296 int err, must_open_file;
297 char buf[PROBE_BUF_SIZE];
298 AVProbeData probe_data, *pd = &probe_data;
300 ic = av_mallocz(sizeof(AVFormatContext));
305 ic->duration = AV_NOPTS_VALUE;
306 ic->start_time = AV_NOPTS_VALUE;
307 pstrcpy(ic->filename, sizeof(ic->filename), filename);
308 pd->filename = ic->filename;
313 /* guess format if no file can be opened */
314 fmt = av_probe_input_format(pd, 0);
317 /* do not open file if the format does not need it. XXX: specific
318 hack needed to handle RTSP/TCP */
320 if ((fmt && (fmt->flags & AVFMT_NOFILE))
321 #ifdef CONFIG_NETWORK
322 || (fmt == &rtp_demux && !strcmp(filename, "null"))
328 if (!fmt || must_open_file) {
329 /* if no file needed do not try to open one */
330 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
335 url_setbufsize(&ic->pb, buf_size);
338 /* read probe data */
339 pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE);
340 url_fseek(&ic->pb, 0, SEEK_SET);
344 /* guess file format */
346 fmt = av_probe_input_format(pd, 1);
349 /* if still no format found, error */
355 /* XXX: suppress this hack for redirectors */
356 #ifdef CONFIG_NETWORK
357 if (fmt == &redir_demux) {
358 err = redir_open(ic_ptr, &ic->pb);
367 /* check filename in case of an image number is expected */
368 if (ic->iformat->flags & AVFMT_NEEDNUMBER) {
369 if (filename_number_test(ic->filename) < 0) {
370 err = AVERROR_NUMEXPECTED;
375 /* allocate private data */
376 if (fmt->priv_data_size > 0) {
377 ic->priv_data = av_mallocz(fmt->priv_data_size);
378 if (!ic->priv_data) {
383 ic->priv_data = NULL;
385 /* default pts settings is MPEG like */
386 av_set_pts_info(ic, 33, 1, 90000);
388 err = ic->iformat->read_header(ic, ap);
394 if (!fmt || must_open_file) {
399 av_freep(&ic->priv_data);
407 * Read a packet from a media file
408 * @param s media file handle
409 * @param pkt is filled
410 * @return 0 if OK. AVERROR_xxx if error.
412 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
416 pktl = s->packet_buffer;
418 /* read packet from packet buffer, if there is data */
420 s->packet_buffer = pktl->next;
424 return s->iformat->read_packet(s, pkt);
429 /* return TRUE if the stream has accurate timings for at least one component */
430 static int av_has_timings(AVFormatContext *ic)
435 for(i = 0;i < ic->nb_streams; i++) {
437 if (st->start_time != AV_NOPTS_VALUE &&
438 st->duration != AV_NOPTS_VALUE)
444 /* estimate the stream timings from the one of each components. Also
445 compute the global bitrate if possible */
446 static void av_update_stream_timings(AVFormatContext *ic)
448 int64_t start_time, end_time, end_time1;
452 start_time = MAXINT64;
454 for(i = 0;i < ic->nb_streams; i++) {
456 if (st->start_time != AV_NOPTS_VALUE) {
457 if (st->start_time < start_time)
458 start_time = st->start_time;
459 if (st->duration != AV_NOPTS_VALUE) {
460 end_time1 = st->start_time + st->duration;
461 if (end_time1 > end_time)
462 end_time = end_time1;
466 if (start_time != MAXINT64) {
467 ic->start_time = start_time;
468 if (end_time != MAXINT64) {
469 ic->duration = end_time - start_time;
470 if (ic->file_size > 0) {
471 /* compute the bit rate */
472 ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE /
473 (double)ic->duration;
480 static void fill_all_stream_timings(AVFormatContext *ic)
485 av_update_stream_timings(ic);
486 for(i = 0;i < ic->nb_streams; i++) {
488 if (st->start_time == AV_NOPTS_VALUE) {
489 st->start_time = ic->start_time;
490 st->duration = ic->duration;
495 static void av_estimate_timings_from_bit_rate(AVFormatContext *ic)
497 int64_t filesize, duration;
501 /* if bit_rate is already set, we believe it */
502 if (ic->bit_rate == 0) {
504 for(i=0;i<ic->nb_streams;i++) {
506 bit_rate += st->codec.bit_rate;
508 ic->bit_rate = bit_rate;
511 /* if duration is already set, we believe it */
512 if (ic->duration == AV_NOPTS_VALUE &&
514 ic->file_size != 0) {
515 filesize = ic->file_size;
517 duration = (int64_t)((8 * AV_TIME_BASE * (double)filesize) / (double)ic->bit_rate);
518 for(i = 0; i < ic->nb_streams; i++) {
520 if (st->start_time == AV_NOPTS_VALUE ||
521 st->duration == AV_NOPTS_VALUE) {
523 st->duration = duration;
530 static void flush_packet_queue(AVFormatContext *s)
535 pktl = s->packet_buffer;
538 s->packet_buffer = pktl->next;
539 av_free_packet(&pktl->pkt);
544 #define DURATION_MAX_READ_SIZE 250000
546 /* only usable for MPEG-PS streams */
547 static void av_estimate_timings_from_pts(AVFormatContext *ic)
549 AVPacket pkt1, *pkt = &pkt1;
551 int read_size, i, ret;
552 int64_t start_time, end_time, end_time1;
553 int64_t filesize, offset, duration;
555 /* we read the first packets to get the first PTS (not fully
556 accurate, but it is enough now) */
557 url_fseek(&ic->pb, 0, SEEK_SET);
560 if (read_size >= DURATION_MAX_READ_SIZE)
562 /* if all info is available, we can stop */
563 for(i = 0;i < ic->nb_streams; i++) {
565 if (st->start_time == AV_NOPTS_VALUE)
568 if (i == ic->nb_streams)
571 ret = av_read_packet(ic, pkt);
574 read_size += pkt->size;
575 st = ic->streams[pkt->stream_index];
576 if (pkt->pts != AV_NOPTS_VALUE) {
577 if (st->start_time == AV_NOPTS_VALUE)
578 st->start_time = (int64_t)((double)pkt->pts * ic->pts_num * (double)AV_TIME_BASE / ic->pts_den);
583 /* we compute the minimum start_time and use it as default */
584 start_time = MAXINT64;
585 for(i = 0; i < ic->nb_streams; i++) {
587 if (st->start_time != AV_NOPTS_VALUE &&
588 st->start_time < start_time)
589 start_time = st->start_time;
591 if (start_time != MAXINT64)
592 ic->start_time = start_time;
594 /* estimate the end time (duration) */
595 /* XXX: may need to support wrapping */
596 filesize = ic->file_size;
597 offset = filesize - DURATION_MAX_READ_SIZE;
601 /* flush packet queue */
602 flush_packet_queue(ic);
604 url_fseek(&ic->pb, offset, SEEK_SET);
607 if (read_size >= DURATION_MAX_READ_SIZE)
609 /* if all info is available, we can stop */
610 for(i = 0;i < ic->nb_streams; i++) {
612 if (st->duration == AV_NOPTS_VALUE)
615 if (i == ic->nb_streams)
618 ret = av_read_packet(ic, pkt);
621 read_size += pkt->size;
622 st = ic->streams[pkt->stream_index];
623 if (pkt->pts != AV_NOPTS_VALUE) {
624 end_time = (int64_t)((double)pkt->pts * ic->pts_num * (double)AV_TIME_BASE / ic->pts_den);
625 duration = end_time - st->start_time;
627 if (st->duration == AV_NOPTS_VALUE ||
628 st->duration < duration)
629 st->duration = duration;
635 /* estimate total duration */
637 for(i = 0;i < ic->nb_streams; i++) {
639 if (st->duration != AV_NOPTS_VALUE) {
640 end_time1 = st->start_time + st->duration;
641 if (end_time1 > end_time)
642 end_time = end_time1;
646 /* update start_time (new stream may have been created, so we do
648 if (ic->start_time != AV_NOPTS_VALUE) {
649 for(i = 0; i < ic->nb_streams; i++) {
651 if (st->start_time == AV_NOPTS_VALUE)
652 st->start_time = ic->start_time;
656 if (end_time != MININT64) {
657 /* put dummy values for duration if needed */
658 for(i = 0;i < ic->nb_streams; i++) {
660 if (st->duration == AV_NOPTS_VALUE &&
661 st->start_time != AV_NOPTS_VALUE)
662 st->duration = end_time - st->start_time;
664 ic->duration = end_time - ic->start_time;
667 url_fseek(&ic->pb, 0, SEEK_SET);
670 static void av_estimate_timings(AVFormatContext *ic)
675 /* get the file size, if possible */
676 if (ic->iformat->flags & AVFMT_NOFILE) {
679 h = url_fileno(&ic->pb);
680 file_size = url_filesize(h);
684 ic->file_size = file_size;
686 if (ic->iformat == &mpegps_demux) {
687 /* get accurate estimate from the PTSes */
688 av_estimate_timings_from_pts(ic);
689 } else if (av_has_timings(ic)) {
690 /* at least one components has timings - we use them for all
692 fill_all_stream_timings(ic);
694 /* less precise: use bit rate info */
695 av_estimate_timings_from_bit_rate(ic);
697 av_update_stream_timings(ic);
703 for(i = 0;i < ic->nb_streams; i++) {
705 printf("%d: start_time: %0.3f duration: %0.3f\n",
706 i, (double)st->start_time / AV_TIME_BASE,
707 (double)st->duration / AV_TIME_BASE);
709 printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
710 (double)ic->start_time / AV_TIME_BASE,
711 (double)ic->duration / AV_TIME_BASE,
712 ic->bit_rate / 1000);
717 /* state for codec information */
718 #define CSTATE_NOTFOUND 0
719 #define CSTATE_DECODING 1
720 #define CSTATE_FOUND 2
722 static int has_codec_parameters(AVCodecContext *enc)
725 switch(enc->codec_type) {
726 case CODEC_TYPE_AUDIO:
727 val = enc->sample_rate;
729 case CODEC_TYPE_VIDEO:
740 * Read the beginning of a media file to get stream information. This
741 * is useful for file formats with no headers such as MPEG. This
742 * function also compute the real frame rate in case of mpeg2 repeat
745 * @param ic media file handle
746 * @return >=0 if OK. AVERROR_xxx if error.
748 int av_find_stream_info(AVFormatContext *ic)
750 int i, count, ret, got_picture, size, read_size;
755 AVPacketList *pktl=NULL, **ppktl;
756 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
758 int min_read_size, max_read_size;
760 /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10
761 Mbits. We read at most 0.2 second of file to find all streams */
763 /* XXX: base it on stream bitrate when possible */
764 if (ic->iformat == &mpegts_demux) {
765 /* maximum number of bytes we accept to read to find all the streams
767 min_read_size = 6000000;
769 min_read_size = 250000;
771 /* max read size is 2 seconds of video max */
772 max_read_size = min_read_size * 10;
774 /* set initial codec state */
775 for(i=0;i<ic->nb_streams;i++) {
777 if (has_codec_parameters(&st->codec))
778 st->codec_info_state = CSTATE_FOUND;
780 st->codec_info_state = CSTATE_NOTFOUND;
781 st->codec_info_nb_repeat_frames = 0;
782 st->codec_info_nb_real_frames = 0;
787 ppktl = &ic->packet_buffer;
789 /* check if one codec still needs to be handled */
790 for(i=0;i<ic->nb_streams;i++) {
792 if (st->codec_info_state != CSTATE_FOUND)
795 if (i == ic->nb_streams) {
796 /* NOTE: if the format has no header, then we need to read
797 some packets to get most of the streams, so we cannot
799 if (!(ic->iformat->flags & AVFMT_NOHEADER) ||
800 read_size >= min_read_size) {
801 /* if we found the info for all the codecs, we can stop */
806 /* we did not get all the codec info, but we read too much data */
807 if (read_size >= max_read_size) {
813 pktl = av_mallocz(sizeof(AVPacketList));
819 /* add the packet in the buffered packet list */
823 /* NOTE: a new stream can be added there if no header in file
826 if (ic->iformat->read_packet(ic, pkt) < 0) {
828 ret = -1; /* we could not have all the codec parameters before EOF */
829 if ((ic->iformat->flags & AVFMT_NOHEADER) &&
834 read_size += pkt->size;
836 /* open new codecs */
837 for(i=0;i<ic->nb_streams;i++) {
839 if (st->codec_info_state == CSTATE_NOTFOUND) {
840 /* set to found in case of error */
841 st->codec_info_state = CSTATE_FOUND;
842 codec = avcodec_find_decoder(st->codec.codec_id);
844 if(codec->capabilities & CODEC_CAP_TRUNCATED)
845 st->codec.flags |= CODEC_FLAG_TRUNCATED;
847 ret = avcodec_open(&st->codec, codec);
849 st->codec_info_state = CSTATE_DECODING;
854 st = ic->streams[pkt->stream_index];
855 if (st->codec_info_state == CSTATE_DECODING) {
856 /* decode the data and update codec parameters */
860 switch(st->codec.codec_type) {
861 case CODEC_TYPE_VIDEO:
862 ret = avcodec_decode_video(&st->codec, &picture,
863 &got_picture, ptr, size);
865 case CODEC_TYPE_AUDIO:
866 ret = avcodec_decode_audio(&st->codec, samples,
867 &got_picture, ptr, size);
874 /* if error, simply ignore because another packet
879 /* we got the parameters - now we can stop
880 examining this stream */
881 /* XXX: add a codec info so that we can decide if
882 the codec can repeat frames */
883 if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO &&
884 ic->iformat != &mpegts_demux &&
885 st->codec.sub_id == 2) {
886 /* for mpeg2 video, we want to know the real
887 frame rate, so we decode 40 frames. In mpeg
888 TS case we do not do it because it would be
890 st->codec_info_nb_real_frames++;
891 st->codec_info_nb_repeat_frames += st->codec.coded_frame->repeat_pict;
894 if ((st->codec_info_nb_real_frames % 24) == 23) {
895 st->codec_info_nb_repeat_frames += 2;
898 /* stop after 40 frames */
899 if (st->codec_info_nb_real_frames >= 40) {
902 &st->r_frame_rate_base,
903 (int64_t)st->codec.frame_rate * st->codec_info_nb_real_frames,
904 (st->codec_info_nb_real_frames + (st->codec_info_nb_repeat_frames >> 1)) * st->codec.frame_rate_base,
910 st->codec_info_state = CSTATE_FOUND;
911 avcodec_close(&st->codec);
922 /* close each codec if there are opened */
923 for(i=0;i<ic->nb_streams;i++) {
925 if (st->codec_info_state == CSTATE_DECODING)
926 avcodec_close(&st->codec);
929 /* set real frame rate info */
930 for(i=0;i<ic->nb_streams;i++) {
932 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
933 if (!st->r_frame_rate){
934 st->r_frame_rate = st->codec.frame_rate;
935 st->r_frame_rate_base = st->codec.frame_rate_base;
941 av_estimate_timings(ic);
946 * Close a media file (but not its codecs)
948 * @param s media file handle
950 void av_close_input_file(AVFormatContext *s)
952 int i, must_open_file;
954 if (s->iformat->read_close)
955 s->iformat->read_close(s);
956 for(i=0;i<s->nb_streams;i++) {
957 av_free(s->streams[i]);
959 if (s->packet_buffer) {
960 AVPacketList *p, *p1;
961 p = s->packet_buffer;
964 av_free_packet(&p->pkt);
968 s->packet_buffer = NULL;
971 if ((s->iformat->flags & AVFMT_NOFILE)
972 #ifdef CONFIG_NETWORK
973 || (s->iformat == &rtp_demux && !strcmp(s->filename, "null"))
978 if (must_open_file) {
981 av_freep(&s->priv_data);
986 * Add a new stream to a media file. Can only be called in the
987 * read_header function. If the flag AVFMT_NOHEADER is in the format
988 * description, then new streams can be added in read_packet too.
991 * @param s media file handle
992 * @param id file format dependent stream id
994 AVStream *av_new_stream(AVFormatContext *s, int id)
998 if (s->nb_streams >= MAX_STREAMS)
1001 st = av_mallocz(sizeof(AVStream));
1004 avcodec_get_context_defaults(&st->codec);
1006 /* no default bitrate if decoding */
1007 st->codec.bit_rate = 0;
1009 st->index = s->nb_streams;
1011 st->start_time = AV_NOPTS_VALUE;
1012 st->duration = AV_NOPTS_VALUE;
1013 s->streams[s->nb_streams++] = st;
1017 /************************************************************/
1018 /* output media file */
1020 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
1024 if (s->oformat->priv_data_size > 0) {
1025 s->priv_data = av_mallocz(s->oformat->priv_data_size);
1027 return AVERROR_NOMEM;
1029 s->priv_data = NULL;
1031 if (s->oformat->set_parameters) {
1032 ret = s->oformat->set_parameters(s, ap);
1040 * allocate the stream private data and write the stream header to an
1043 * @param s media file handle
1044 * @return 0 if OK. AVERROR_xxx if error.
1046 int av_write_header(AVFormatContext *s)
1051 /* default pts settings is MPEG like */
1052 av_set_pts_info(s, 33, 1, 90000);
1053 ret = s->oformat->write_header(s);
1057 /* init PTS generation */
1058 for(i=0;i<s->nb_streams;i++) {
1061 switch (st->codec.codec_type) {
1062 case CODEC_TYPE_AUDIO:
1063 av_frac_init(&st->pts, 0, 0,
1064 (int64_t)s->pts_num * st->codec.sample_rate);
1066 case CODEC_TYPE_VIDEO:
1067 av_frac_init(&st->pts, 0, 0,
1068 (int64_t)s->pts_num * st->codec.frame_rate);
1078 * Write a packet to an output media file. The packet shall contain
1079 * one audio or video frame.
1081 * @param s media file handle
1082 * @param stream_index stream index
1083 * @param buf buffer containing the frame data
1084 * @param size size of buffer
1085 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
1087 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
1092 int ret, frame_size;
1094 st = s->streams[stream_index];
1095 pts_mask = (1LL << s->pts_wrap_bits) - 1;
1096 ret = s->oformat->write_packet(s, stream_index, (uint8_t *)buf, size,
1097 st->pts.val & pts_mask);
1102 switch (st->codec.codec_type) {
1103 case CODEC_TYPE_AUDIO:
1104 if (st->codec.frame_size <= 1) {
1105 frame_size = size / st->codec.channels;
1106 /* specific hack for pcm codecs because no frame size is provided */
1107 switch(st->codec.codec_id) {
1108 case CODEC_ID_PCM_S16LE:
1109 case CODEC_ID_PCM_S16BE:
1110 case CODEC_ID_PCM_U16LE:
1111 case CODEC_ID_PCM_U16BE:
1118 frame_size = st->codec.frame_size;
1120 av_frac_add(&st->pts,
1121 (int64_t)s->pts_den * frame_size);
1123 case CODEC_TYPE_VIDEO:
1124 av_frac_add(&st->pts,
1125 (int64_t)s->pts_den * st->codec.frame_rate_base);
1134 * write the stream trailer to an output media file and and free the
1135 * file private data.
1137 * @param s media file handle
1138 * @return 0 if OK. AVERROR_xxx if error. */
1139 int av_write_trailer(AVFormatContext *s)
1142 ret = s->oformat->write_trailer(s);
1143 av_freep(&s->priv_data);
1147 /* "user interface" functions */
1149 void dump_format(AVFormatContext *ic,
1157 fprintf(stderr, "%s #%d, %s, %s '%s':\n",
1158 is_output ? "Output" : "Input",
1160 is_output ? ic->oformat->name : ic->iformat->name,
1161 is_output ? "to" : "from", url);
1163 fprintf(stderr, " Duration: ");
1164 if (ic->duration != AV_NOPTS_VALUE) {
1165 int hours, mins, secs, us;
1166 secs = ic->duration / AV_TIME_BASE;
1167 us = ic->duration % AV_TIME_BASE;
1172 fprintf(stderr, "%02d:%02d:%02d.%01d", hours, mins, secs,
1173 (10 * us) / AV_TIME_BASE);
1175 fprintf(stderr, "N/A");
1177 fprintf(stderr, ", bitrate: ");
1179 fprintf(stderr,"%d kb/s", ic->bit_rate / 1000);
1181 fprintf(stderr, "N/A");
1183 fprintf(stderr, "\n");
1185 for(i=0;i<ic->nb_streams;i++) {
1186 AVStream *st = ic->streams[i];
1187 avcodec_string(buf, sizeof(buf), &st->codec, is_output);
1188 fprintf(stderr, " Stream #%d.%d", index, i);
1189 /* the pid is an important information, so we display it */
1190 /* XXX: add a generic system */
1192 flags = ic->oformat->flags;
1194 flags = ic->iformat->flags;
1195 if (flags & AVFMT_SHOW_IDS) {
1196 fprintf(stderr, "[0x%x]", st->id);
1198 fprintf(stderr, ": %s\n", buf);
1205 int frame_rate, frame_rate_base;
1208 static AbvEntry frame_abvs[] = {
1209 { "ntsc", 720, 480, 30000, 1001 },
1210 { "pal", 720, 576, 25, 1 },
1211 { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */
1212 { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */
1213 { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */
1214 { "spal", 768, 576, 25, 1 }, /* square pixel pal */
1215 { "film", 352, 240, 24, 1 },
1216 { "ntsc-film", 352, 240, 24000, 1001 },
1217 { "sqcif", 128, 96, 0, 0 },
1218 { "qcif", 176, 144, 0, 0 },
1219 { "cif", 352, 288, 0, 0 },
1220 { "4cif", 704, 576, 0, 0 },
1223 int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
1226 int n = sizeof(frame_abvs) / sizeof(AbvEntry);
1228 int frame_width = 0, frame_height = 0;
1231 if (!strcmp(frame_abvs[i].abv, str)) {
1232 frame_width = frame_abvs[i].width;
1233 frame_height = frame_abvs[i].height;
1239 frame_width = strtol(p, (char **)&p, 10);
1242 frame_height = strtol(p, (char **)&p, 10);
1244 if (frame_width <= 0 || frame_height <= 0)
1246 *width_ptr = frame_width;
1247 *height_ptr = frame_height;
1251 int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg)
1256 /* First, we check our abbreviation table */
1257 for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i)
1258 if (!strcmp(frame_abvs[i].abv, arg)) {
1259 *frame_rate = frame_abvs[i].frame_rate;
1260 *frame_rate_base = frame_abvs[i].frame_rate_base;
1264 /* Then, we try to parse it as fraction */
1265 cp = strchr(arg, '/');
1268 *frame_rate = strtol(arg, &cpp, 10);
1269 if (cpp != arg || cpp == cp)
1270 *frame_rate_base = strtol(cp+1, &cpp, 10);
1275 /* Finally we give up and parse it as double */
1276 *frame_rate_base = DEFAULT_FRAME_RATE_BASE;
1277 *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5);
1279 if (!*frame_rate || !*frame_rate_base)
1286 * - If not a duration:
1287 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
1288 * Time is localtime unless Z is suffixed to the end. In this case GMT
1289 * Return the date in micro seconds since 1970
1291 * HH[:MM[:SS[.m...]]]
1294 int64_t parse_date(const char *datestr, int duration)
1300 static const char *date_fmt[] = {
1304 static const char *time_fmt[] = {
1311 time_t now = time(0);
1313 len = strlen(datestr);
1315 lastch = datestr[len - 1];
1318 is_utc = (lastch == 'z' || lastch == 'Z');
1320 memset(&dt, 0, sizeof(dt));
1325 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) {
1326 q = small_strptime(p, date_fmt[i], &dt);
1336 dt = *localtime(&now);
1338 dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
1343 if (*p == 'T' || *p == 't' || *p == ' ')
1346 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) {
1347 q = small_strptime(p, time_fmt[i], &dt);
1353 q = small_strptime(p, time_fmt[0], &dt);
1355 dt.tm_sec = strtol(p, (char **)&q, 10);
1361 /* Now we have all the fields that we can get */
1366 return now * int64_t_C(1000000);
1370 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
1372 dt.tm_isdst = -1; /* unknown */
1385 for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
1388 val += n * (*q - '0');
1395 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return
1397 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
1407 while (*p != '\0' && *p != '=' && *p != '&') {
1408 if ((q - tag) < sizeof(tag) - 1)
1416 while (*p != '&' && *p != '\0') {
1417 if ((q - arg) < arg_size - 1) {
1427 if (!strcmp(tag, tag1))
1436 /* Return in 'buf' the path with '%d' replaced by number. Also handles
1437 the '%0nd' format where 'n' is the total number of digits and
1438 '%%'. Return 0 if OK, and -1 if format error */
1439 int get_frame_filename(char *buf, int buf_size,
1440 const char *path, int number)
1444 int nd, len, c, percentd_found;
1456 while (isdigit(*p)) {
1457 nd = nd * 10 + *p++ - '0';
1460 } while (isdigit(c));
1469 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
1471 if ((q - buf + len) > buf_size - 1)
1473 memcpy(q, buf1, len);
1481 if ((q - buf) < buf_size - 1)
1485 if (!percentd_found)
1496 * Print on stdout a nice hexa dump of a buffer
1498 * @param size buffer size
1500 void av_hex_dump(uint8_t *buf, int size)
1504 for(i=0;i<size;i+=16) {
1511 printf(" %02x", buf[i+j]);
1516 for(j=0;j<len;j++) {
1518 if (c < ' ' || c > '~')
1526 void url_split(char *proto, int proto_size,
1527 char *hostname, int hostname_size,
1529 char *path, int path_size,
1540 while (*p != ':' && *p != '\0') {
1541 if ((q - proto) < proto_size - 1)
1550 if (hostname_size > 0)
1560 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') {
1561 if ((q - hostname) < hostname_size - 1)
1565 if (hostname_size > 0)
1569 port = strtoul(p, (char **)&p, 10);
1574 pstrcpy(path, path_size, p);
1578 * Set the pts for a given stream
1580 * @param pts_wrap_bits number of bits effectively used by the pts
1581 * (used for wrap control, 33 is the value for MPEG)
1582 * @param pts_num numerator to convert to seconds (MPEG: 1)
1583 * @param pts_den denominator to convert to seconds (MPEG: 90000)
1585 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits,
1586 int pts_num, int pts_den)
1588 s->pts_wrap_bits = pts_wrap_bits;
1589 s->pts_num = pts_num;
1590 s->pts_den = pts_den;
1593 /* fraction handling */
1596 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such
1597 * as 0 <= num < den.
1599 * @param f fractional number
1600 * @param val integer value
1601 * @param num must be >= 0
1602 * @param den must be >= 1
1604 void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
1616 /* set f to (val + 0.5) */
1617 void av_frac_set(AVFrac *f, int64_t val)
1620 f->num = f->den >> 1;
1624 * Fractionnal addition to f: f = f + (incr / f->den)
1626 * @param f fractional number
1627 * @param incr increment, can be positive or negative
1629 void av_frac_add(AVFrac *f, int64_t incr)
1633 num = f->num + incr;
1636 f->val += num / den;
1642 } else if (num >= den) {
1643 f->val += num / den;
1650 * register a new image format
1651 * @param img_fmt Image format descriptor
1653 void av_register_image_format(AVImageFormat *img_fmt)
1657 p = &first_image_format;
1658 while (*p != NULL) p = &(*p)->next;
1660 img_fmt->next = NULL;
1663 /* guess image format */
1664 AVImageFormat *av_probe_image_format(AVProbeData *pd)
1666 AVImageFormat *fmt1, *fmt;
1667 int score, score_max;
1671 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
1672 if (fmt1->img_probe) {
1673 score = fmt1->img_probe(pd);
1674 if (score > score_max) {
1683 AVImageFormat *guess_image_format(const char *filename)
1685 AVImageFormat *fmt1;
1687 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
1688 if (fmt1->extensions && match_ext(filename, fmt1->extensions))
1695 * Read an image from a stream.
1696 * @param gb byte stream containing the image
1697 * @param fmt image format, NULL if probing is required
1699 int av_read_image(ByteIOContext *pb, const char *filename,
1701 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque)
1703 char buf[PROBE_BUF_SIZE];
1704 AVProbeData probe_data, *pd = &probe_data;
1709 pd->filename = filename;
1711 pos = url_ftell(pb);
1712 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE);
1713 url_fseek(pb, pos, SEEK_SET);
1714 fmt = av_probe_image_format(pd);
1717 return AVERROR_NOFMT;
1718 ret = fmt->img_read(pb, alloc_cb, opaque);
1723 * Write an image to a stream.
1724 * @param pb byte stream for the image output
1725 * @param fmt image format
1726 * @param img image data and informations
1728 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img)
1730 return fmt->img_write(pb, img);