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
22 #define strcasecmp _stricmp
23 #include <sys/types.h>
24 #include <sys/timeb.h>
25 #elif defined(CONFIG_OS2)
27 #define strcasecmp stricmp
40 AVInputFormat *first_iformat;
41 AVOutputFormat *first_oformat;
42 AVImageFormat *first_image_format;
44 void av_register_input_format(AVInputFormat *format)
48 while (*p != NULL) p = &(*p)->next;
53 void av_register_output_format(AVOutputFormat *format)
57 while (*p != NULL) p = &(*p)->next;
62 int match_ext(const char *filename, const char *extensions)
67 ext = strrchr(filename, '.');
73 while (*p != '\0' && *p != ',')
76 if (!strcasecmp(ext1, ext))
86 AVOutputFormat *guess_format(const char *short_name, const char *filename,
87 const char *mime_type)
89 AVOutputFormat *fmt, *fmt_found;
92 /* specific test for image sequences */
93 if (!short_name && filename &&
94 filename_number_test(filename) >= 0 &&
95 guess_image_format(filename)) {
96 return guess_format("image", NULL, NULL);
99 /* find the proper file type */
103 while (fmt != NULL) {
105 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
107 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
109 if (filename && fmt->extensions &&
110 match_ext(filename, fmt->extensions)) {
113 if (score > score_max) {
122 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename,
123 const char *mime_type)
125 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type);
128 AVOutputFormat *stream_fmt;
129 char stream_format_name[64];
131 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
132 stream_fmt = guess_format(stream_format_name, NULL, NULL);
141 AVInputFormat *av_find_input_format(const char *short_name)
144 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) {
145 if (!strcmp(fmt->name, short_name))
151 /* memory handling */
154 * Default packet destructor
156 static void av_destruct_packet(AVPacket *pkt)
159 pkt->data = NULL; pkt->size = 0;
163 * Allocate the payload of a packet and intialized its fields to default values.
166 * @param size wanted payload size
167 * @return 0 if OK. AVERROR_xxx otherwise.
169 int av_new_packet(AVPacket *pkt, int size)
171 void *data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
173 return AVERROR_NOMEM;
174 memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
179 pkt->destruct = av_destruct_packet;
185 int fifo_init(FifoBuffer *f, int size)
187 f->buffer = av_malloc(size);
190 f->end = f->buffer + size;
191 f->wptr = f->rptr = f->buffer;
195 void fifo_free(FifoBuffer *f)
200 int fifo_size(FifoBuffer *f, uint8_t *rptr)
204 if (f->wptr >= rptr) {
205 size = f->wptr - rptr;
207 size = (f->end - rptr) + (f->wptr - f->buffer);
212 /* get data from the fifo (return -1 if not enough data) */
213 int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr)
215 uint8_t *rptr = *rptr_ptr;
218 if (f->wptr >= rptr) {
219 size = f->wptr - rptr;
221 size = (f->end - rptr) + (f->wptr - f->buffer);
226 while (buf_size > 0) {
230 memcpy(buf, rptr, len);
241 void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr)
250 memcpy(wptr, buf, len);
260 int filename_number_test(const char *filename)
263 return get_frame_filename(buf, sizeof(buf), filename, 1);
266 /* guess file format */
267 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
269 AVInputFormat *fmt1, *fmt;
270 int score, score_max;
274 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) {
275 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE))
278 if (fmt1->read_probe) {
279 score = fmt1->read_probe(pd);
280 } else if (fmt1->extensions) {
281 if (match_ext(pd->filename, fmt1->extensions)) {
285 if (score > score_max) {
293 /************************************************************/
294 /* input media file */
296 #define PROBE_BUF_SIZE 2048
299 * Open a media file as input. The codec are not opened. Only the file
300 * header (if present) is read.
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.
309 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
312 AVFormatParameters *ap)
314 AVFormatContext *ic = NULL;
316 char buf[PROBE_BUF_SIZE];
317 AVProbeData probe_data, *pd = &probe_data;
319 ic = av_mallocz(sizeof(AVFormatContext));
324 pstrcpy(ic->filename, sizeof(ic->filename), filename);
325 pd->filename = ic->filename;
330 /* guess format if no file can be opened */
331 fmt = av_probe_input_format(pd, 0);
334 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
335 /* if no file needed do not try to open one */
336 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
341 url_setbufsize(&ic->pb, buf_size);
344 /* read probe data */
345 pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE);
346 url_fseek(&ic->pb, 0, SEEK_SET);
350 /* guess file format */
352 fmt = av_probe_input_format(pd, 1);
355 /* if still no format found, error */
361 /* XXX: suppress this hack for redirectors */
362 #ifdef CONFIG_NETWORK
363 if (fmt == &redir_demux) {
364 err = redir_open(ic_ptr, &ic->pb);
373 /* check filename in case of an image number is expected */
374 if (ic->iformat->flags & AVFMT_NEEDNUMBER) {
375 if (filename_number_test(ic->filename) < 0) {
376 err = AVERROR_NUMEXPECTED;
381 /* allocate private data */
382 if (fmt->priv_data_size > 0) {
383 ic->priv_data = av_mallocz(fmt->priv_data_size);
384 if (!ic->priv_data) {
389 ic->priv_data = NULL;
391 /* default pts settings is MPEG like */
392 av_set_pts_info(ic, 33, 1, 90000);
394 err = ic->iformat->read_header(ic, ap);
400 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
405 av_freep(&ic->priv_data);
413 * Read a packet from a media file
414 * @param s media file handle
415 * @param pkt is filled
416 * @return 0 if OK. AVERROR_xxx if error.
418 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
422 pktl = s->packet_buffer;
424 /* read packet from packet buffer, if there is data */
426 s->packet_buffer = pktl->next;
430 return s->iformat->read_packet(s, pkt);
434 /* state for codec information */
435 #define CSTATE_NOTFOUND 0
436 #define CSTATE_DECODING 1
437 #define CSTATE_FOUND 2
439 static int has_codec_parameters(AVCodecContext *enc)
442 switch(enc->codec_type) {
443 case CODEC_TYPE_AUDIO:
444 val = enc->sample_rate;
446 case CODEC_TYPE_VIDEO:
457 * Read the beginning of a media file to get stream information. This
458 * is useful for file formats with no headers such as MPEG. This
459 * function also compute the real frame rate in case of mpeg2 repeat
462 * @param ic media file handle
463 * @return >=0 if OK. AVERROR_xxx if error.
465 int av_find_stream_info(AVFormatContext *ic)
467 int i, count, ret, got_picture, size, read_size;
472 AVPacketList *pktl=NULL, **ppktl;
473 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
475 int min_read_size, max_read_size;
477 /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10
478 Mbits. We read at most 0.2 second of file to find all streams */
480 /* XXX: base it on stream bitrate when possible */
481 if (ic->iformat == &mpegts_demux) {
482 /* maximum number of bytes we accept to read to find all the streams
484 min_read_size = 6000000;
486 min_read_size = 250000;
488 /* max read size is 2 seconds of video max */
489 max_read_size = min_read_size * 10;
491 /* set initial codec state */
492 for(i=0;i<ic->nb_streams;i++) {
494 if (has_codec_parameters(&st->codec))
495 st->codec_info_state = CSTATE_FOUND;
497 st->codec_info_state = CSTATE_NOTFOUND;
498 st->codec_info_nb_repeat_frames = 0;
499 st->codec_info_nb_real_frames = 0;
504 ppktl = &ic->packet_buffer;
506 /* check if one codec still needs to be handled */
507 for(i=0;i<ic->nb_streams;i++) {
509 if (st->codec_info_state != CSTATE_FOUND)
512 if (i == ic->nb_streams) {
513 /* NOTE: if the format has no header, then we need to read
514 some packets to get most of the streams, so we cannot
516 if (!(ic->iformat->flags & AVFMT_NOHEADER) ||
517 read_size >= min_read_size) {
518 /* if we found the info for all the codecs, we can stop */
523 /* we did not get all the codec info, but we read too much data */
524 if (read_size >= max_read_size) {
530 pktl = av_mallocz(sizeof(AVPacketList));
536 /* add the packet in the buffered packet list */
540 /* NOTE: a new stream can be added there if no header in file
543 if (ic->iformat->read_packet(ic, pkt) < 0) {
545 ret = -1; /* we could not have all the codec parameters before EOF */
546 if ((ic->iformat->flags & AVFMT_NOHEADER) &&
551 read_size += pkt->size;
553 /* open new codecs */
554 for(i=0;i<ic->nb_streams;i++) {
556 if (st->codec_info_state == CSTATE_NOTFOUND) {
557 /* set to found in case of error */
558 st->codec_info_state = CSTATE_FOUND;
559 codec = avcodec_find_decoder(st->codec.codec_id);
561 if(codec->capabilities & CODEC_CAP_TRUNCATED)
562 st->codec.flags |= CODEC_FLAG_TRUNCATED;
564 ret = avcodec_open(&st->codec, codec);
566 st->codec_info_state = CSTATE_DECODING;
571 st = ic->streams[pkt->stream_index];
572 if (st->codec_info_state == CSTATE_DECODING) {
573 /* decode the data and update codec parameters */
577 switch(st->codec.codec_type) {
578 case CODEC_TYPE_VIDEO:
579 ret = avcodec_decode_video(&st->codec, &picture,
580 &got_picture, ptr, size);
582 case CODEC_TYPE_AUDIO:
583 ret = avcodec_decode_audio(&st->codec, samples,
584 &got_picture, ptr, size);
591 /* if error, simply ignore because another packet
596 /* we got the parameters - now we can stop
597 examining this stream */
598 /* XXX: add a codec info so that we can decide if
599 the codec can repeat frames */
600 if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO &&
601 ic->iformat != &mpegts_demux &&
602 st->codec.sub_id == 2) {
603 /* for mpeg2 video, we want to know the real
604 frame rate, so we decode 40 frames. In mpeg
605 TS case we do not do it because it would be
607 st->codec_info_nb_real_frames++;
608 st->codec_info_nb_repeat_frames += st->codec.coded_frame->repeat_pict;
611 if ((st->codec_info_nb_real_frames % 24) == 23) {
612 st->codec_info_nb_repeat_frames += 2;
615 /* stop after 40 frames */
616 if (st->codec_info_nb_real_frames >= 40) {
619 &st->r_frame_rate_base,
620 (int64_t)st->codec.frame_rate * st->codec_info_nb_real_frames,
621 (st->codec_info_nb_real_frames + (st->codec_info_nb_repeat_frames >> 1)) * st->codec.frame_rate_base,
627 st->codec_info_state = CSTATE_FOUND;
628 avcodec_close(&st->codec);
639 /* close each codec if there are opened */
640 for(i=0;i<ic->nb_streams;i++) {
642 if (st->codec_info_state == CSTATE_DECODING)
643 avcodec_close(&st->codec);
646 /* set real frame rate info */
647 for(i=0;i<ic->nb_streams;i++) {
649 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
650 if (!st->r_frame_rate){
651 st->r_frame_rate = st->codec.frame_rate;
652 st->r_frame_rate_base = st->codec.frame_rate_base;
661 * Close a media file (but not its codecs)
663 * @param s media file handle
665 void av_close_input_file(AVFormatContext *s)
669 if (s->iformat->read_close)
670 s->iformat->read_close(s);
671 for(i=0;i<s->nb_streams;i++) {
672 av_free(s->streams[i]);
674 if (s->packet_buffer) {
675 AVPacketList *p, *p1;
676 p = s->packet_buffer;
679 av_free_packet(&p->pkt);
683 s->packet_buffer = NULL;
685 if (!(s->iformat->flags & AVFMT_NOFILE)) {
688 av_freep(&s->priv_data);
693 * Add a new stream to a media file. Can only be called in the
694 * read_header function. If the flag AVFMT_NOHEADER is in the format
695 * description, then new streams can be added in read_packet too.
698 * @param s media file handle
699 * @param id file format dependent stream id
701 AVStream *av_new_stream(AVFormatContext *s, int id)
705 if (s->nb_streams >= MAX_STREAMS)
708 st = av_mallocz(sizeof(AVStream));
711 avcodec_get_context_defaults(&st->codec);
713 st->index = s->nb_streams;
715 s->streams[s->nb_streams++] = st;
719 /************************************************************/
720 /* output media file */
722 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
726 if (s->oformat->priv_data_size > 0) {
727 s->priv_data = av_mallocz(s->oformat->priv_data_size);
729 return AVERROR_NOMEM;
733 if (s->oformat->set_parameters) {
734 ret = s->oformat->set_parameters(s, ap);
742 * allocate the stream private data and write the stream header to an
745 * @param s media file handle
746 * @return 0 if OK. AVERROR_xxx if error.
748 int av_write_header(AVFormatContext *s)
753 /* default pts settings is MPEG like */
754 av_set_pts_info(s, 33, 1, 90000);
755 ret = s->oformat->write_header(s);
759 /* init PTS generation */
760 for(i=0;i<s->nb_streams;i++) {
763 switch (st->codec.codec_type) {
764 case CODEC_TYPE_AUDIO:
765 av_frac_init(&st->pts, 0, 0,
766 (int64_t)s->pts_num * st->codec.sample_rate);
768 case CODEC_TYPE_VIDEO:
769 av_frac_init(&st->pts, 0, 0,
770 (int64_t)s->pts_num * st->codec.frame_rate);
780 * Write a packet to an output media file. The packet shall contain
781 * one audio or video frame.
783 * @param s media file handle
784 * @param stream_index stream index
785 * @param buf buffer containing the frame data
786 * @param size size of buffer
787 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
789 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
796 st = s->streams[stream_index];
797 pts_mask = (1LL << s->pts_wrap_bits) - 1;
798 ret = s->oformat->write_packet(s, stream_index, (uint8_t *)buf, size,
799 st->pts.val & pts_mask);
804 switch (st->codec.codec_type) {
805 case CODEC_TYPE_AUDIO:
806 if (st->codec.frame_size <= 1) {
807 frame_size = size / st->codec.channels;
808 /* specific hack for pcm codecs because no frame size is provided */
809 switch(st->codec.codec_id) {
810 case CODEC_ID_PCM_S16LE:
811 case CODEC_ID_PCM_S16BE:
812 case CODEC_ID_PCM_U16LE:
813 case CODEC_ID_PCM_U16BE:
820 frame_size = st->codec.frame_size;
822 av_frac_add(&st->pts,
823 (int64_t)s->pts_den * frame_size);
825 case CODEC_TYPE_VIDEO:
826 av_frac_add(&st->pts,
827 (int64_t)s->pts_den * st->codec.frame_rate_base);
836 * write the stream trailer to an output media file and and free the
839 * @param s media file handle
840 * @return 0 if OK. AVERROR_xxx if error. */
841 int av_write_trailer(AVFormatContext *s)
844 ret = s->oformat->write_trailer(s);
845 av_freep(&s->priv_data);
849 /* "user interface" functions */
851 void dump_format(AVFormatContext *ic,
859 fprintf(stderr, "%s #%d, %s, %s '%s':\n",
860 is_output ? "Output" : "Input",
862 is_output ? ic->oformat->name : ic->iformat->name,
863 is_output ? "to" : "from", url);
864 for(i=0;i<ic->nb_streams;i++) {
865 AVStream *st = ic->streams[i];
866 avcodec_string(buf, sizeof(buf), &st->codec, is_output);
867 fprintf(stderr, " Stream #%d.%d", index, i);
868 /* the pid is an important information, so we display it */
869 /* XXX: add a generic system */
871 flags = ic->oformat->flags;
873 flags = ic->iformat->flags;
874 if (flags & AVFMT_SHOW_IDS) {
875 fprintf(stderr, "[0x%x]", st->id);
877 fprintf(stderr, ": %s\n", buf);
886 static SizeEntry sizes[] = {
887 { "sqcif", 128, 96 },
888 { "qcif", 176, 144 },
890 { "4cif", 704, 576 },
893 int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
896 int n = sizeof(sizes) / sizeof(SizeEntry);
898 int frame_width = 0, frame_height = 0;
901 if (!strcmp(sizes[i].str, str)) {
902 frame_width = sizes[i].width;
903 frame_height = sizes[i].height;
909 frame_width = strtol(p, (char **)&p, 10);
912 frame_height = strtol(p, (char **)&p, 10);
914 if (frame_width <= 0 || frame_height <= 0)
916 *width_ptr = frame_width;
917 *height_ptr = frame_height;
921 int64_t av_gettime(void)
926 return ((int64_t)tb.time * int64_t_C(1000) + (int64_t)tb.millitm) * int64_t_C(1000);
929 gettimeofday(&tv,NULL);
930 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
934 static time_t mktimegm(struct tm *tm)
938 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
946 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
948 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
954 * - If not a duration:
955 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
956 * Time is localtime unless Z is suffixed to the end. In this case GMT
957 * Return the date in micro seconds since 1970
959 * HH[:MM[:SS[.m...]]]
962 int64_t parse_date(const char *datestr, int duration)
968 static const char *date_fmt[] = {
972 static const char *time_fmt[] = {
979 time_t now = time(0);
981 len = strlen(datestr);
983 lastch = datestr[len - 1];
986 is_utc = (lastch == 'z' || lastch == 'Z');
988 memset(&dt, 0, sizeof(dt));
993 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) {
994 q = strptime(p, date_fmt[i], &dt);
1004 dt = *localtime(&now);
1006 dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
1011 if (*p == 'T' || *p == 't' || *p == ' ')
1014 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) {
1015 q = strptime(p, time_fmt[i], &dt);
1021 q = strptime(p, time_fmt[0], &dt);
1023 dt.tm_sec = strtol(p, (char **)&q, 10);
1029 /* Now we have all the fields that we can get */
1034 return now * int64_t_C(1000000);
1038 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
1040 dt.tm_isdst = -1; /* unknown */
1053 for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
1056 val += n * (*q - '0');
1063 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return
1065 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
1075 while (*p != '\0' && *p != '=' && *p != '&') {
1076 if ((q - tag) < sizeof(tag) - 1)
1084 while (*p != '&' && *p != '\0') {
1085 if ((q - arg) < arg_size - 1) {
1095 if (!strcmp(tag, tag1))
1104 /* Return in 'buf' the path with '%d' replaced by number. Also handles
1105 the '%0nd' format where 'n' is the total number of digits and
1106 '%%'. Return 0 if OK, and -1 if format error */
1107 int get_frame_filename(char *buf, int buf_size,
1108 const char *path, int number)
1112 int nd, len, c, percentd_found;
1124 while (isdigit(*p)) {
1125 nd = nd * 10 + *p++ - '0';
1128 if (c == '*' && nd > 0) {
1129 // The nd field is actually the modulus
1130 number = number % nd;
1134 } while (isdigit(c));
1143 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
1145 if ((q - buf + len) > buf_size - 1)
1147 memcpy(q, buf1, len);
1155 if ((q - buf) < buf_size - 1)
1159 if (!percentd_found)
1170 * Print on stdout a nice hexa dump of a buffer
1172 * @param size buffer size
1174 void av_hex_dump(uint8_t *buf, int size)
1178 for(i=0;i<size;i+=16) {
1185 printf(" %02x", buf[i+j]);
1190 for(j=0;j<len;j++) {
1192 if (c < ' ' || c > '~')
1200 void url_split(char *proto, int proto_size,
1201 char *hostname, int hostname_size,
1203 char *path, int path_size,
1214 while (*p != ':' && *p != '\0') {
1215 if ((q - proto) < proto_size - 1)
1224 if (hostname_size > 0)
1234 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') {
1235 if ((q - hostname) < hostname_size - 1)
1239 if (hostname_size > 0)
1243 port = strtoul(p, (char **)&p, 10);
1248 pstrcpy(path, path_size, p);
1252 * Set the pts for a given stream
1254 * @param pts_wrap_bits number of bits effectively used by the pts
1255 * (used for wrap control, 33 is the value for MPEG)
1256 * @param pts_num numerator to convert to seconds (MPEG: 1)
1257 * @param pts_den denominator to convert to seconds (MPEG: 90000)
1259 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits,
1260 int pts_num, int pts_den)
1262 s->pts_wrap_bits = pts_wrap_bits;
1263 s->pts_num = pts_num;
1264 s->pts_den = pts_den;
1267 /* fraction handling */
1270 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such
1271 * as 0 <= num < den.
1273 * @param f fractional number
1274 * @param val integer value
1275 * @param num must be >= 0
1276 * @param den must be >= 1
1278 void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
1290 /* set f to (val + 0.5) */
1291 void av_frac_set(AVFrac *f, int64_t val)
1294 f->num = f->den >> 1;
1298 * Fractionnal addition to f: f = f + (incr / f->den)
1300 * @param f fractional number
1301 * @param incr increment, can be positive or negative
1303 void av_frac_add(AVFrac *f, int64_t incr)
1307 num = f->num + incr;
1310 f->val += num / den;
1316 } else if (num >= den) {
1317 f->val += num / den;
1324 * register a new image format
1325 * @param img_fmt Image format descriptor
1327 void av_register_image_format(AVImageFormat *img_fmt)
1331 p = &first_image_format;
1332 while (*p != NULL) p = &(*p)->next;
1334 img_fmt->next = NULL;
1337 /* guess image format */
1338 AVImageFormat *av_probe_image_format(AVProbeData *pd)
1340 AVImageFormat *fmt1, *fmt;
1341 int score, score_max;
1345 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
1346 if (fmt1->img_probe) {
1347 score = fmt1->img_probe(pd);
1348 if (score > score_max) {
1357 AVImageFormat *guess_image_format(const char *filename)
1359 AVImageFormat *fmt1;
1361 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
1362 if (fmt1->extensions && match_ext(filename, fmt1->extensions))
1369 * Read an image from a stream.
1370 * @param gb byte stream containing the image
1371 * @param fmt image format, NULL if probing is required
1373 int av_read_image(ByteIOContext *pb, const char *filename,
1375 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque)
1377 char buf[PROBE_BUF_SIZE];
1378 AVProbeData probe_data, *pd = &probe_data;
1383 pd->filename = filename;
1385 pos = url_ftell(pb);
1386 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE);
1387 url_fseek(pb, pos, SEEK_SET);
1388 fmt = av_probe_image_format(pd);
1391 return AVERROR_NOFMT;
1392 ret = fmt->img_read(pb, alloc_cb, opaque);
1397 * Write an image to a stream.
1398 * @param pb byte stream for the image output
1399 * @param fmt image format
1400 * @param img image data and informations
1402 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img)
1404 return fmt->img_write(pb, img);