libavformat/format.c
afc86853
 /*
  * Format register and lookup
  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  *
4a522eff
  * This file is part of FFmpeg.
afc86853
  *
4a522eff
  * FFmpeg is free software; you can redistribute it and/or
afc86853
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
4a522eff
  * FFmpeg is distributed in the hope that it will be useful,
afc86853
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
4a522eff
  * License along with FFmpeg; if not, write to the Free Software
afc86853
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
4182728c
 #include "libavutil/atomic.h"
e253a9e2
 #include "libavutil/avstring.h"
4182728c
 #include "libavutil/bprint.h"
3a19405d
 #include "libavutil/opt.h"
e253a9e2
 
 #include "avio_internal.h"
afc86853
 #include "avformat.h"
e253a9e2
 #include "id3v2.h"
afc86853
 #include "internal.h"
4182728c
 
afc86853
 
 /**
  * @file
  * Format register and lookup
  */
 /** head of registered input format linked list */
 static AVInputFormat *first_iformat = NULL;
 /** head of registered output format linked list */
 static AVOutputFormat *first_oformat = NULL;
 
49f10c9c
 static AVInputFormat **last_iformat = &first_iformat;
 static AVOutputFormat **last_oformat = &first_oformat;
 
ec4f04da
 AVInputFormat *av_iformat_next(const AVInputFormat *f)
afc86853
 {
     if (f)
         return f->next;
     else
         return first_iformat;
 }
 
ec4f04da
 AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
afc86853
 {
     if (f)
         return f->next;
     else
         return first_oformat;
 }
 
 void av_register_input_format(AVInputFormat *format)
 {
49f10c9c
     AVInputFormat **p = last_iformat;
afc86853
 
     format->next = NULL;
133fbfc7
     while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
53fd1ab2
         p = &(*p)->next;
49f10c9c
     last_iformat = &format->next;
afc86853
 }
 
 void av_register_output_format(AVOutputFormat *format)
 {
49f10c9c
     AVOutputFormat **p = last_oformat;
afc86853
 
     format->next = NULL;
133fbfc7
     while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
53fd1ab2
         p = &(*p)->next;
49f10c9c
     last_oformat = &format->next;
afc86853
 }
 
 int av_match_ext(const char *filename, const char *extensions)
 {
0d92b0d5
     const char *ext;
afc86853
 
     if (!filename)
         return 0;
 
     ext = strrchr(filename, '.');
0d92b0d5
     if (ext)
cebe8c80
         return av_match_name(ext + 1, extensions);
afc86853
     return 0;
 }
 
 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
                                 const char *mime_type)
 {
     AVOutputFormat *fmt = NULL, *fmt_found;
     int score_max, score;
 
     /* specific test for image sequences */
 #if CONFIG_IMAGE2_MUXER
     if (!short_name && filename &&
         av_filename_number_test(filename) &&
         ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
         return av_guess_format("image2", NULL, NULL);
     }
 #endif
     /* Find the proper file type. */
     fmt_found = NULL;
     score_max = 0;
     while ((fmt = av_oformat_next(fmt))) {
         score = 0;
31e0b5d3
         if (fmt->name && short_name && av_match_name(short_name, fmt->name))
afc86853
             score += 100;
         if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
             score += 10;
         if (filename && fmt->extensions &&
             av_match_ext(filename, fmt->extensions)) {
             score += 5;
         }
         if (score > score_max) {
             score_max = score;
             fmt_found = fmt;
         }
     }
     return fmt_found;
 }
 
 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
                               const char *filename, const char *mime_type,
                               enum AVMediaType type)
 {
26ffa8ea
     if (av_match_name("segment", fmt->name) || av_match_name("ssegment", fmt->name)) {
98e42a24
         AVOutputFormat *fmt2 = av_guess_format(NULL, filename, NULL);
         if (fmt2)
             fmt = fmt2;
4a522eff
     }
 
afc86853
     if (type == AVMEDIA_TYPE_VIDEO) {
         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
 
 #if CONFIG_IMAGE2_MUXER
         if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
             codec_id = ff_guess_image2_codec(filename);
         }
 #endif
         if (codec_id == AV_CODEC_ID_NONE)
             codec_id = fmt->video_codec;
         return codec_id;
     } else if (type == AVMEDIA_TYPE_AUDIO)
         return fmt->audio_codec;
     else if (type == AVMEDIA_TYPE_SUBTITLE)
         return fmt->subtitle_codec;
a47c9331
     else if (type == AVMEDIA_TYPE_DATA)
         return fmt->data_codec;
afc86853
     else
         return AV_CODEC_ID_NONE;
 }
 
 AVInputFormat *av_find_input_format(const char *short_name)
 {
     AVInputFormat *fmt = NULL;
     while ((fmt = av_iformat_next(fmt)))
69e7336b
         if (av_match_name(short_name, fmt->name))
afc86853
             return fmt;
     return NULL;
 }
e253a9e2
 
e066f015
 AVInputFormat *av_probe_input_format3(AVProbeData *pd, int is_opened,
                                       int *score_ret)
e253a9e2
 {
     AVProbeData lpd = *pd;
     AVInputFormat *fmt1 = NULL, *fmt;
8619582b
     int score, score_max = 0;
d36eac69
     const static uint8_t zerobuffer[AVPROBE_PADDING_SIZE];
8619582b
     enum nodat {
         NO_ID3,
77864be4
         ID3_ALMOST_GREATER_PROBE,
8619582b
         ID3_GREATER_PROBE,
         ID3_GREATER_MAX_PROBE,
     } nodat = NO_ID3;
e066f015
 
     if (!lpd.buf)
6ea457f7
         lpd.buf = (unsigned char *) zerobuffer;
e253a9e2
 
     if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
         int id3len = ff_id3v2_tag_len(lpd.buf);
         if (lpd.buf_size > id3len + 16) {
77864be4
             if (lpd.buf_size < 2LL*id3len + 16)
                 nodat = ID3_ALMOST_GREATER_PROBE;
e253a9e2
             lpd.buf      += id3len;
             lpd.buf_size -= id3len;
e066f015
         } else if (id3len >= PROBE_BUF_MAX) {
8619582b
             nodat = ID3_GREATER_MAX_PROBE;
e066f015
         } else
8619582b
             nodat = ID3_GREATER_PROBE;
e253a9e2
     }
 
     fmt = NULL;
     while ((fmt1 = av_iformat_next(fmt1))) {
917d14e6
         if (!is_opened == !(fmt1->flags & AVFMT_NOFILE) && strcmp(fmt1->name, "image2"))
e253a9e2
             continue;
         score = 0;
         if (fmt1->read_probe) {
             score = fmt1->read_probe(&lpd);
75a730a2
             if (score)
                 av_log(NULL, AV_LOG_TRACE, "Probing %s score:%d size:%d\n", fmt1->name, score, lpd.buf_size);
e066f015
             if (fmt1->extensions && av_match_ext(lpd.filename, fmt1->extensions)) {
77864be4
                 switch (nodat) {
                 case NO_ID3:
                     score = FFMAX(score, 1);
                     break;
                 case ID3_GREATER_PROBE:
                 case ID3_ALMOST_GREATER_PROBE:
                     score = FFMAX(score, AVPROBE_SCORE_EXTENSION / 2 - 1);
                     break;
                 case ID3_GREATER_MAX_PROBE:
                     score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
                     break;
                 }
e066f015
             }
e253a9e2
         } else if (fmt1->extensions) {
             if (av_match_ext(lpd.filename, fmt1->extensions))
                 score = AVPROBE_SCORE_EXTENSION;
         }
3a19405d
         if (av_match_name(lpd.mime_type, fmt1->mime_type))
bcac0f40
             score = FFMAX(score, AVPROBE_SCORE_MIME);
e066f015
         if (score > score_max) {
             score_max = score;
             fmt       = fmt1;
         } else if (score == score_max)
e253a9e2
             fmt = NULL;
     }
8619582b
     if (nodat == ID3_GREATER_PROBE)
e066f015
         score_max = FFMIN(AVPROBE_SCORE_EXTENSION / 2 - 1, score_max);
     *score_ret = score_max;
e253a9e2
 
     return fmt;
 }
 
e066f015
 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
 {
     int score_ret;
     AVInputFormat *fmt = av_probe_input_format3(pd, is_opened, &score_ret);
     if (score_ret > *score_max) {
         *score_max = score_ret;
         return fmt;
     } else
         return NULL;
 }
 
e253a9e2
 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
 {
     int score = 0;
     return av_probe_input_format2(pd, is_opened, &score);
 }
 
e066f015
 int av_probe_input_buffer2(AVIOContext *pb, AVInputFormat **fmt,
e253a9e2
                           const char *filename, void *logctx,
                           unsigned int offset, unsigned int max_probe_size)
 {
     AVProbeData pd = { filename ? filename : "" };
     uint8_t *buf = NULL;
e066f015
     int ret = 0, probe_size, buf_offset = 0;
     int score = 0;
d38edeee
     int ret2;
e253a9e2
 
     if (!max_probe_size)
         max_probe_size = PROBE_BUF_MAX;
e066f015
     else if (max_probe_size < PROBE_BUF_MIN) {
         av_log(logctx, AV_LOG_ERROR,
                "Specified probe size value %u cannot be < %u\n", max_probe_size, PROBE_BUF_MIN);
e253a9e2
         return AVERROR(EINVAL);
e066f015
     }
e253a9e2
 
     if (offset >= max_probe_size)
         return AVERROR(EINVAL);
e066f015
 
b15b06eb
     if (pb->av_class) {
e96fb980
         uint8_t *mime_type_opt = NULL;
c8370a17
         char *semi;
b15b06eb
         av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
         pd.mime_type = (const char *)mime_type_opt;
c8370a17
         semi = pd.mime_type ? strchr(pd.mime_type, ';') : NULL;
9b10ae57
         if (semi) {
c8370a17
             *semi = '\0';
9b10ae57
         }
b15b06eb
     }
c218d821
 #if 0
e066f015
     if (!*fmt && pb->av_class && av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type) >= 0 && mime_type) {
         if (!av_strcasecmp(mime_type, "audio/aacp")) {
             *fmt = av_find_input_format("aac");
         }
         av_freep(&mime_type);
     }
80a3a661
 #endif
e253a9e2
 
     for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
          probe_size = FFMIN(probe_size << 1,
                             FFMAX(max_probe_size, probe_size + 1))) {
e066f015
         score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0;
e253a9e2
 
         /* Read probe data. */
         if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
3a19405d
             goto fail;
e066f015
         if ((ret = avio_read(pb, buf + buf_offset,
                              probe_size - buf_offset)) < 0) {
e253a9e2
             /* Fail if error was not end of file, otherwise, lower score. */
3a19405d
             if (ret != AVERROR_EOF)
                 goto fail;
 
e253a9e2
             score = 0;
             ret   = 0;          /* error was end of file, nothing read */
         }
e066f015
         buf_offset += ret;
         if (buf_offset < offset)
             continue;
         pd.buf_size = buf_offset - offset;
         pd.buf = &buf[offset];
e253a9e2
 
         memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
 
         /* Guess file format. */
         *fmt = av_probe_input_format2(&pd, 1, &score);
         if (*fmt) {
             /* This can only be true in the last iteration. */
e066f015
             if (score <= AVPROBE_SCORE_RETRY) {
e253a9e2
                 av_log(logctx, AV_LOG_WARNING,
e066f015
                        "Format %s detected only with low score of %d, "
                        "misdetection possible!\n", (*fmt)->name, score);
e253a9e2
             } else
                 av_log(logctx, AV_LOG_DEBUG,
e066f015
                        "Format %s probed with size=%d and score=%d\n",
                        (*fmt)->name, probe_size, score);
 #if 0
             FILE *f = fopen("probestat.tmp", "ab");
             fprintf(f, "probe_size:%d format:%s score:%d filename:%s\n", probe_size, (*fmt)->name, score, filename);
             fclose(f);
 #endif
e253a9e2
         }
     }
 
3a19405d
     if (!*fmt)
         ret = AVERROR_INVALIDDATA;
e253a9e2
 
3a19405d
 fail:
e253a9e2
     /* Rewind. Reuse probe buffer to avoid seeking. */
d38edeee
     ret2 = ffio_rewind_with_probe_data(pb, &buf, buf_offset);
80a3a661
     if (ret >= 0)
d38edeee
         ret = ret2;
e066f015
 
b15b06eb
     av_freep(&pd.mime_type);
e066f015
     return ret < 0 ? ret : score;
 }
e253a9e2
 
e066f015
 int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
                           const char *filename, void *logctx,
                           unsigned int offset, unsigned int max_probe_size)
 {
     int ret = av_probe_input_buffer2(pb, fmt, filename, logctx, offset, max_probe_size);
     return ret < 0 ? ret : 0;
e253a9e2
 }