format.c 11.8 KB
Newer Older
1 2 3 4
/*
 * Format register and lookup
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 9 10 11
 * 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.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with FFmpeg; if not, write to the Free Software
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/atomic.h"
23
#include "libavutil/avstring.h"
24
#include "libavutil/bprint.h"
25
#include "libavutil/opt.h"
26 27

#include "avio_internal.h"
28
#include "avformat.h"
29
#include "id3v2.h"
30
#include "internal.h"
31

32 33 34 35 36 37 38 39 40 41

/**
 * @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;

42 43 44
static AVInputFormat **last_iformat = &first_iformat;
static AVOutputFormat **last_oformat = &first_oformat;

45
AVInputFormat *av_iformat_next(const AVInputFormat *f)
46 47 48 49 50 51 52
{
    if (f)
        return f->next;
    else
        return first_iformat;
}

53
AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
54 55 56 57 58 59 60 61 62
{
    if (f)
        return f->next;
    else
        return first_oformat;
}

void av_register_input_format(AVInputFormat *format)
{
63
    AVInputFormat **p = last_iformat;
64

65 66
    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
67
        p = &(*p)->next;
68 69 70

    if (!format->next)
        last_iformat = &format->next;
71 72 73 74
}

void av_register_output_format(AVOutputFormat *format)
{
75
    AVOutputFormat **p = last_oformat;
76

77 78
    // Note, format could be added after the first 2 checks but that implies that *p is no longer NULL
    while(p != &format->next && !format->next && avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
79
        p = &(*p)->next;
80 81 82

    if (!format->next)
        last_oformat = &format->next;
83 84 85 86
}

int av_match_ext(const char *filename, const char *extensions)
{
87
    const char *ext;
88 89 90 91 92

    if (!filename)
        return 0;

    ext = strrchr(filename, '.');
93
    if (ext)
94
        return av_match_name(ext + 1, extensions);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    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;
117
        if (fmt->name && short_name && av_match_name(short_name, fmt->name))
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
            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)
{
137
    if (av_match_name("segment", fmt->name) || av_match_name("ssegment", fmt->name)) {
138 139 140
        AVOutputFormat *fmt2 = av_guess_format(NULL, filename, NULL);
        if (fmt2)
            fmt = fmt2;
141 142
    }

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    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;
158 159
    else if (type == AVMEDIA_TYPE_DATA)
        return fmt->data_codec;
160 161 162 163 164 165 166 167
    else
        return AV_CODEC_ID_NONE;
}

AVInputFormat *av_find_input_format(const char *short_name)
{
    AVInputFormat *fmt = NULL;
    while ((fmt = av_iformat_next(fmt)))
168
        if (av_match_name(short_name, fmt->name))
169 170 171
            return fmt;
    return NULL;
}
172

173 174
AVInputFormat *av_probe_input_format3(AVProbeData *pd, int is_opened,
                                      int *score_ret)
175 176 177
{
    AVProbeData lpd = *pd;
    AVInputFormat *fmt1 = NULL, *fmt;
178
    int score, score_max = 0;
179
    const static uint8_t zerobuffer[AVPROBE_PADDING_SIZE];
180 181
    enum nodat {
        NO_ID3,
182
        ID3_ALMOST_GREATER_PROBE,
183 184 185
        ID3_GREATER_PROBE,
        ID3_GREATER_MAX_PROBE,
    } nodat = NO_ID3;
186 187

    if (!lpd.buf)
188
        lpd.buf = (unsigned char *) zerobuffer;
189 190 191 192

    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) {
193 194
            if (lpd.buf_size < 2LL*id3len + 16)
                nodat = ID3_ALMOST_GREATER_PROBE;
195 196
            lpd.buf      += id3len;
            lpd.buf_size -= id3len;
197
        } else if (id3len >= PROBE_BUF_MAX) {
198
            nodat = ID3_GREATER_MAX_PROBE;
199
        } else
200
            nodat = ID3_GREATER_PROBE;
201 202 203 204
    }

    fmt = NULL;
    while ((fmt1 = av_iformat_next(fmt1))) {
205
        if (!is_opened == !(fmt1->flags & AVFMT_NOFILE) && strcmp(fmt1->name, "image2"))
206 207 208 209
            continue;
        score = 0;
        if (fmt1->read_probe) {
            score = fmt1->read_probe(&lpd);
210 211
            if (score)
                av_log(NULL, AV_LOG_TRACE, "Probing %s score:%d size:%d\n", fmt1->name, score, lpd.buf_size);
212
            if (fmt1->extensions && av_match_ext(lpd.filename, fmt1->extensions)) {
213 214 215 216 217 218 219 220 221 222 223 224
                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;
                }
225
            }
226 227 228 229
        } else if (fmt1->extensions) {
            if (av_match_ext(lpd.filename, fmt1->extensions))
                score = AVPROBE_SCORE_EXTENSION;
        }
230 231 232 233 234 235
        if (av_match_name(lpd.mime_type, fmt1->mime_type)) {
            if (AVPROBE_SCORE_MIME > score) {
                av_log(NULL, AV_LOG_DEBUG, "Probing %s score:%d increased to %d due to MIME type\n", fmt1->name, score, AVPROBE_SCORE_MIME);
                score = AVPROBE_SCORE_MIME;
            }
        }
236 237 238 239
        if (score > score_max) {
            score_max = score;
            fmt       = fmt1;
        } else if (score == score_max)
240 241
            fmt = NULL;
    }
242
    if (nodat == ID3_GREATER_PROBE)
243 244
        score_max = FFMIN(AVPROBE_SCORE_EXTENSION / 2 - 1, score_max);
    *score_ret = score_max;
245 246 247 248

    return fmt;
}

249 250 251 252 253 254 255 256 257 258 259
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;
}

260 261 262 263 264 265
AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
{
    int score = 0;
    return av_probe_input_format2(pd, is_opened, &score);
}

266
int av_probe_input_buffer2(AVIOContext *pb, AVInputFormat **fmt,
267 268 269 270 271
                          const char *filename, void *logctx,
                          unsigned int offset, unsigned int max_probe_size)
{
    AVProbeData pd = { filename ? filename : "" };
    uint8_t *buf = NULL;
272 273
    int ret = 0, probe_size, buf_offset = 0;
    int score = 0;
274
    int ret2;
275 276 277

    if (!max_probe_size)
        max_probe_size = PROBE_BUF_MAX;
278 279 280
    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);
281
        return AVERROR(EINVAL);
282
    }
283 284 285

    if (offset >= max_probe_size)
        return AVERROR(EINVAL);
286

287
    if (pb->av_class) {
288
        uint8_t *mime_type_opt = NULL;
289
        char *semi;
290 291
        av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
        pd.mime_type = (const char *)mime_type_opt;
292
        semi = pd.mime_type ? strchr(pd.mime_type, ';') : NULL;
293
        if (semi) {
294
            *semi = '\0';
295
        }
296
    }
297
#if 0
298 299 300 301 302 303
    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);
    }
304
#endif
305 306 307 308

    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))) {
309
        score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0;
310 311 312

        /* Read probe data. */
        if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
313
            goto fail;
314 315
        if ((ret = avio_read(pb, buf + buf_offset,
                             probe_size - buf_offset)) < 0) {
316
            /* Fail if error was not end of file, otherwise, lower score. */
317 318 319
            if (ret != AVERROR_EOF)
                goto fail;

320 321 322
            score = 0;
            ret   = 0;          /* error was end of file, nothing read */
        }
323 324 325 326 327
        buf_offset += ret;
        if (buf_offset < offset)
            continue;
        pd.buf_size = buf_offset - offset;
        pd.buf = &buf[offset];
328 329 330 331 332 333 334

        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. */
335
            if (score <= AVPROBE_SCORE_RETRY) {
336
                av_log(logctx, AV_LOG_WARNING,
337 338
                       "Format %s detected only with low score of %d, "
                       "misdetection possible!\n", (*fmt)->name, score);
339 340
            } else
                av_log(logctx, AV_LOG_DEBUG,
341 342 343 344 345 346 347
                       "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
348 349 350
        }
    }

351 352
    if (!*fmt)
        ret = AVERROR_INVALIDDATA;
353

354
fail:
355
    /* Rewind. Reuse probe buffer to avoid seeking. */
356
    ret2 = ffio_rewind_with_probe_data(pb, &buf, buf_offset);
357
    if (ret >= 0)
358
        ret = ret2;
359

360
    av_freep(&pd.mime_type);
361 362
    return ret < 0 ? ret : score;
}
363

364 365 366 367 368 369
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;
370
}