LCOV - code coverage report
Current view: top level - libavformat - format.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 147 171 86.0 %
Date: 2018-05-20 11:54:08 Functions: 8 9 88.9 %

          Line data    Source code
       1             : /*
       2             :  * Format register and lookup
       3             :  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/avstring.h"
      23             : #include "libavutil/bprint.h"
      24             : #include "libavutil/opt.h"
      25             : #include "libavutil/thread.h"
      26             : 
      27             : #include "avio_internal.h"
      28             : #include "avformat.h"
      29             : #include "id3v2.h"
      30             : #include "internal.h"
      31             : 
      32             : 
      33             : /**
      34             :  * @file
      35             :  * Format register and lookup
      36             :  */
      37             : 
      38      799716 : int av_match_ext(const char *filename, const char *extensions)
      39             : {
      40             :     const char *ext;
      41             : 
      42      799716 :     if (!filename)
      43      345870 :         return 0;
      44             : 
      45      453846 :     ext = strrchr(filename, '.');
      46      453846 :     if (ext)
      47      451134 :         return av_match_name(ext + 1, extensions);
      48        2712 :     return 0;
      49             : }
      50             : 
      51        5696 : AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
      52             :                                 const char *mime_type)
      53             : {
      54        5696 :     const AVOutputFormat *fmt = NULL;
      55        5696 :     AVOutputFormat *fmt_found = NULL;
      56        5696 :     void *i = 0;
      57             :     int score_max, score;
      58             : 
      59             :     /* specific test for image sequences */
      60             : #if CONFIG_IMAGE2_MUXER
      61        5860 :     if (!short_name && filename &&
      62         202 :         av_filename_number_test(filename) &&
      63          38 :         ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
      64          33 :         return av_guess_format("image2", NULL, NULL);
      65             :     }
      66             : #endif
      67             :     /* Find the proper file type. */
      68        5663 :     score_max = 0;
      69      939918 :     while ((fmt = av_muxer_iterate(&i))) {
      70      928592 :         score = 0;
      71      928592 :         if (fmt->name && short_name && av_match_name(short_name, fmt->name))
      72        5538 :             score += 100;
      73      928592 :         if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
      74           0 :             score += 10;
      75      944484 :         if (filename && fmt->extensions &&
      76       15892 :             av_match_ext(filename, fmt->extensions)) {
      77         141 :             score += 5;
      78             :         }
      79      928592 :         if (score > score_max) {
      80        5663 :             score_max = score;
      81        5663 :             fmt_found = (AVOutputFormat*)fmt;
      82             :         }
      83             :     }
      84        5663 :     return fmt_found;
      85             : }
      86             : 
      87       18948 : enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
      88             :                               const char *filename, const char *mime_type,
      89             :                               enum AVMediaType type)
      90             : {
      91       18948 :     if (av_match_name("segment", fmt->name) || av_match_name("ssegment", fmt->name)) {
      92           0 :         AVOutputFormat *fmt2 = av_guess_format(NULL, filename, NULL);
      93           0 :         if (fmt2)
      94           0 :             fmt = fmt2;
      95             :     }
      96             : 
      97       18948 :     if (type == AVMEDIA_TYPE_VIDEO) {
      98        7376 :         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
      99             : 
     100             : #if CONFIG_IMAGE2_MUXER
     101        7376 :         if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
     102          72 :             codec_id = ff_guess_image2_codec(filename);
     103             :         }
     104             : #endif
     105        7376 :         if (codec_id == AV_CODEC_ID_NONE)
     106        7304 :             codec_id = fmt->video_codec;
     107        7376 :         return codec_id;
     108       11572 :     } else if (type == AVMEDIA_TYPE_AUDIO)
     109        5961 :         return fmt->audio_codec;
     110        5611 :     else if (type == AVMEDIA_TYPE_SUBTITLE)
     111          26 :         return fmt->subtitle_codec;
     112        5585 :     else if (type == AVMEDIA_TYPE_DATA)
     113        5585 :         return fmt->data_codec;
     114             :     else
     115           0 :         return AV_CODEC_ID_NONE;
     116             : }
     117             : 
     118        2668 : AVInputFormat *av_find_input_format(const char *short_name)
     119             : {
     120        2668 :     const AVInputFormat *fmt = NULL;
     121        2668 :     void *i = 0;
     122      325074 :     while ((fmt = av_demuxer_iterate(&i)))
     123      322406 :         if (av_match_name(short_name, fmt->name))
     124        2668 :             return (AVInputFormat*)fmt;
     125           0 :     return NULL;
     126             : }
     127             : 
     128        9471 : AVInputFormat *av_probe_input_format3(AVProbeData *pd, int is_opened,
     129             :                                       int *score_ret)
     130             : {
     131        9471 :     AVProbeData lpd = *pd;
     132        9471 :     const AVInputFormat *fmt1 = NULL;
     133        9471 :     AVInputFormat *fmt = NULL;
     134        9471 :     int score, score_max = 0;
     135        9471 :     void *i = 0;
     136             :     const static uint8_t zerobuffer[AVPROBE_PADDING_SIZE];
     137             :     enum nodat {
     138             :         NO_ID3,
     139             :         ID3_ALMOST_GREATER_PROBE,
     140             :         ID3_GREATER_PROBE,
     141             :         ID3_GREATER_MAX_PROBE,
     142        9471 :     } nodat = NO_ID3;
     143             : 
     144        9471 :     if (!lpd.buf)
     145        3254 :         lpd.buf = (unsigned char *) zerobuffer;
     146             : 
     147        9471 :     if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
     148          29 :         int id3len = ff_id3v2_tag_len(lpd.buf);
     149          29 :         if (lpd.buf_size > id3len + 16) {
     150          24 :             if (lpd.buf_size < 2LL*id3len + 16)
     151          10 :                 nodat = ID3_ALMOST_GREATER_PROBE;
     152          24 :             lpd.buf      += id3len;
     153          24 :             lpd.buf_size -= id3len;
     154           5 :         } else if (id3len >= PROBE_BUF_MAX) {
     155           0 :             nodat = ID3_GREATER_MAX_PROBE;
     156             :         } else
     157           5 :             nodat = ID3_GREATER_PROBE;
     158             :     }
     159             : 
     160     2773221 :     while ((fmt1 = av_demuxer_iterate(&i))) {
     161     2754279 :         if (!is_opened == !(fmt1->flags & AVFMT_NOFILE) && strcmp(fmt1->name, "image2"))
     162      969325 :             continue;
     163     1784954 :         score = 0;
     164     1784954 :         if (fmt1->read_probe) {
     165     1477135 :             score = fmt1->read_probe(&lpd);
     166     1477135 :             if (score)
     167        5997 :                 av_log(NULL, AV_LOG_TRACE, "Probing %s score:%d size:%d\n", fmt1->name, score, lpd.buf_size);
     168     1477135 :             if (fmt1->extensions && av_match_ext(lpd.filename, fmt1->extensions)) {
     169        1676 :                 switch (nodat) {
     170        1665 :                 case NO_ID3:
     171        1665 :                     score = FFMAX(score, 1);
     172        1665 :                     break;
     173          11 :                 case ID3_GREATER_PROBE:
     174             :                 case ID3_ALMOST_GREATER_PROBE:
     175          11 :                     score = FFMAX(score, AVPROBE_SCORE_EXTENSION / 2 - 1);
     176          11 :                     break;
     177           0 :                 case ID3_GREATER_MAX_PROBE:
     178           0 :                     score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
     179           0 :                     break;
     180             :                 }
     181             :             }
     182      307819 :         } else if (fmt1->extensions) {
     183      161642 :             if (av_match_ext(lpd.filename, fmt1->extensions))
     184          29 :                 score = AVPROBE_SCORE_EXTENSION;
     185             :         }
     186     1784954 :         if (av_match_name(lpd.mime_type, fmt1->mime_type)) {
     187           0 :             if (AVPROBE_SCORE_MIME > score) {
     188           0 :                 av_log(NULL, AV_LOG_DEBUG, "Probing %s score:%d increased to %d due to MIME type\n", fmt1->name, score, AVPROBE_SCORE_MIME);
     189           0 :                 score = AVPROBE_SCORE_MIME;
     190             :             }
     191             :         }
     192     1784954 :         if (score > score_max) {
     193        5429 :             score_max = score;
     194        5429 :             fmt       = (AVInputFormat*)fmt1;
     195     1779525 :         } else if (score == score_max)
     196      958063 :             fmt = NULL;
     197             :     }
     198        9471 :     if (nodat == ID3_GREATER_PROBE)
     199           5 :         score_max = FFMIN(AVPROBE_SCORE_EXTENSION / 2 - 1, score_max);
     200        9471 :     *score_ret = score_max;
     201             : 
     202        9471 :     return fmt;
     203             : }
     204             : 
     205        6726 : AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
     206             : {
     207             :     int score_ret;
     208        6726 :     AVInputFormat *fmt = av_probe_input_format3(pd, is_opened, &score_ret);
     209        6726 :     if (score_ret > *score_max) {
     210        3258 :         *score_max = score_ret;
     211        3258 :         return fmt;
     212             :     } else
     213        3468 :         return NULL;
     214             : }
     215             : 
     216           0 : AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
     217             : {
     218           0 :     int score = 0;
     219           0 :     return av_probe_input_format2(pd, is_opened, &score);
     220             : }
     221             : 
     222        3098 : int av_probe_input_buffer2(AVIOContext *pb, AVInputFormat **fmt,
     223             :                           const char *filename, void *logctx,
     224             :                           unsigned int offset, unsigned int max_probe_size)
     225             : {
     226        3098 :     AVProbeData pd = { filename ? filename : "" };
     227        3098 :     uint8_t *buf = NULL;
     228        3098 :     int ret = 0, probe_size, buf_offset = 0;
     229        3098 :     int score = 0;
     230             :     int ret2;
     231             : 
     232        3098 :     if (!max_probe_size)
     233           4 :         max_probe_size = PROBE_BUF_MAX;
     234        3094 :     else if (max_probe_size < PROBE_BUF_MIN) {
     235           0 :         av_log(logctx, AV_LOG_ERROR,
     236             :                "Specified probe size value %u cannot be < %u\n", max_probe_size, PROBE_BUF_MIN);
     237           0 :         return AVERROR(EINVAL);
     238             :     }
     239             : 
     240        3098 :     if (offset >= max_probe_size)
     241           0 :         return AVERROR(EINVAL);
     242             : 
     243        3098 :     if (pb->av_class) {
     244        3094 :         uint8_t *mime_type_opt = NULL;
     245             :         char *semi;
     246        3094 :         av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
     247        3094 :         pd.mime_type = (const char *)mime_type_opt;
     248        3094 :         semi = pd.mime_type ? strchr(pd.mime_type, ';') : NULL;
     249        3094 :         if (semi) {
     250           0 :             *semi = '\0';
     251             :         }
     252             :     }
     253             : 
     254        9668 :     for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
     255        3472 :          probe_size = FFMIN(probe_size << 1,
     256             :                             FFMAX(max_probe_size, probe_size + 1))) {
     257        3472 :         score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0;
     258             : 
     259             :         /* Read probe data. */
     260        3472 :         if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
     261           0 :             goto fail;
     262        3472 :         if ((ret = avio_read(pb, buf + buf_offset,
     263             :                              probe_size - buf_offset)) < 0) {
     264             :             /* Fail if error was not end of file, otherwise, lower score. */
     265           6 :             if (ret != AVERROR_EOF)
     266           0 :                 goto fail;
     267             : 
     268           6 :             score = 0;
     269           6 :             ret   = 0;          /* error was end of file, nothing read */
     270             :         }
     271        3472 :         buf_offset += ret;
     272        3472 :         if (buf_offset < offset)
     273           0 :             continue;
     274        3472 :         pd.buf_size = buf_offset - offset;
     275        3472 :         pd.buf = &buf[offset];
     276             : 
     277        3472 :         memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
     278             : 
     279             :         /* Guess file format. */
     280        3472 :         *fmt = av_probe_input_format2(&pd, 1, &score);
     281        3472 :         if (*fmt) {
     282             :             /* This can only be true in the last iteration. */
     283        3098 :             if (score <= AVPROBE_SCORE_RETRY) {
     284          14 :                 av_log(logctx, AV_LOG_WARNING,
     285             :                        "Format %s detected only with low score of %d, "
     286           7 :                        "misdetection possible!\n", (*fmt)->name, score);
     287             :             } else
     288        6182 :                 av_log(logctx, AV_LOG_DEBUG,
     289             :                        "Format %s probed with size=%d and score=%d\n",
     290        3091 :                        (*fmt)->name, probe_size, score);
     291             : #if 0
     292             :             FILE *f = fopen("probestat.tmp", "ab");
     293             :             fprintf(f, "probe_size:%d format:%s score:%d filename:%s\n", probe_size, (*fmt)->name, score, filename);
     294             :             fclose(f);
     295             : #endif
     296             :         }
     297             :     }
     298             : 
     299        3098 :     if (!*fmt)
     300           0 :         ret = AVERROR_INVALIDDATA;
     301             : 
     302        6196 : fail:
     303             :     /* Rewind. Reuse probe buffer to avoid seeking. */
     304        3098 :     ret2 = ffio_rewind_with_probe_data(pb, &buf, buf_offset);
     305        3098 :     if (ret >= 0)
     306        3098 :         ret = ret2;
     307             : 
     308        3098 :     av_freep(&pd.mime_type);
     309        3098 :     return ret < 0 ? ret : score;
     310             : }
     311             : 
     312           4 : int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
     313             :                           const char *filename, void *logctx,
     314             :                           unsigned int offset, unsigned int max_probe_size)
     315             : {
     316           4 :     int ret = av_probe_input_buffer2(pb, fmt, filename, logctx, offset, max_probe_size);
     317           4 :     return ret < 0 ? ret : 0;
     318             : }

Generated by: LCOV version 1.13