2013-06-15 09:26:09 +00:00
|
|
|
/*
|
|
|
|
* Format register and lookup
|
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
|
|
|
*
|
2013-06-16 21:38:27 +00:00
|
|
|
* This file is part of FFmpeg.
|
2013-06-15 09:26:09 +00:00
|
|
|
*
|
2013-06-16 21:38:27 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2013-06-15 09:26:09 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2013-06-16 21:38:27 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2013-06-15 09:26:09 +00:00
|
|
|
* 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
|
2013-06-16 21:38:27 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2013-06-15 09:26:09 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2014-03-13 18:54:00 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2014-07-29 19:35:07 +00:00
|
|
|
#include "libavutil/bprint.h"
|
2014-03-13 21:11:12 +00:00
|
|
|
#include "libavutil/opt.h"
|
2018-01-04 18:12:53 +00:00
|
|
|
#include "libavutil/thread.h"
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
#include "avio_internal.h"
|
2013-06-15 09:26:09 +00:00
|
|
|
#include "avformat.h"
|
2014-03-13 18:54:00 +00:00
|
|
|
#include "id3v2.h"
|
2013-06-15 09:26:09 +00:00
|
|
|
#include "internal.h"
|
2014-07-29 19:35:07 +00:00
|
|
|
|
2013-06-15 09:26:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Format register and lookup
|
|
|
|
*/
|
|
|
|
|
|
|
|
int av_match_ext(const char *filename, const char *extensions)
|
|
|
|
{
|
2014-09-30 17:00:52 +00:00
|
|
|
const char *ext;
|
2013-06-15 09:26:09 +00:00
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ext = strrchr(filename, '.');
|
2014-09-30 17:00:52 +00:00
|
|
|
if (ext)
|
2014-10-23 02:33:09 +00:00
|
|
|
return av_match_name(ext + 1, extensions);
|
2013-06-15 09:26:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
|
|
|
|
const char *mime_type)
|
2013-06-15 09:26:09 +00:00
|
|
|
{
|
2018-03-31 23:19:49 +00:00
|
|
|
const AVOutputFormat *fmt = NULL;
|
2021-10-07 09:36:28 +00:00
|
|
|
const AVOutputFormat *fmt_found = NULL;
|
2018-01-21 16:06:54 +00:00
|
|
|
void *i = 0;
|
2013-06-15 09:26:09 +00:00
|
|
|
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. */
|
|
|
|
score_max = 0;
|
2018-03-31 23:19:49 +00:00
|
|
|
while ((fmt = av_muxer_iterate(&i))) {
|
2013-06-15 09:26:09 +00:00
|
|
|
score = 0;
|
2014-07-29 18:09:15 +00:00
|
|
|
if (fmt->name && short_name && av_match_name(short_name, fmt->name))
|
2013-06-15 09:26:09 +00:00
|
|
|
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;
|
2021-10-07 09:36:28 +00:00
|
|
|
fmt_found = fmt;
|
2013-06-15 09:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt_found;
|
|
|
|
}
|
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
enum AVCodecID av_guess_codec(const AVOutputFormat *fmt, const char *short_name,
|
2013-06-15 09:26:09 +00:00
|
|
|
const char *filename, const char *mime_type,
|
|
|
|
enum AVMediaType type)
|
|
|
|
{
|
2014-08-02 01:31:22 +00:00
|
|
|
if (av_match_name("segment", fmt->name) || av_match_name("ssegment", fmt->name)) {
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVOutputFormat *fmt2 = av_guess_format(NULL, filename, NULL);
|
2014-08-02 02:16:02 +00:00
|
|
|
if (fmt2)
|
|
|
|
fmt = fmt2;
|
2013-06-16 21:38:27 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 09:26:09 +00:00
|
|
|
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;
|
2015-01-16 01:00:23 +00:00
|
|
|
else if (type == AVMEDIA_TYPE_DATA)
|
|
|
|
return fmt->data_codec;
|
2013-06-15 09:26:09 +00:00
|
|
|
else
|
|
|
|
return AV_CODEC_ID_NONE;
|
|
|
|
}
|
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVInputFormat *av_find_input_format(const char *short_name)
|
2013-06-15 09:26:09 +00:00
|
|
|
{
|
2018-04-01 02:17:07 +00:00
|
|
|
const AVInputFormat *fmt = NULL;
|
2018-01-21 16:06:54 +00:00
|
|
|
void *i = 0;
|
|
|
|
while ((fmt = av_demuxer_iterate(&i)))
|
2014-07-29 13:29:13 +00:00
|
|
|
if (av_match_name(short_name, fmt->name))
|
2021-10-07 09:36:28 +00:00
|
|
|
return fmt;
|
2013-06-15 09:26:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-13 18:54:00 +00:00
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVInputFormat *av_probe_input_format3(const AVProbeData *pd,
|
|
|
|
int is_opened, int *score_ret)
|
2014-03-13 18:54:00 +00:00
|
|
|
{
|
|
|
|
AVProbeData lpd = *pd;
|
2018-04-01 02:17:07 +00:00
|
|
|
const AVInputFormat *fmt1 = NULL;
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVInputFormat *fmt = NULL;
|
2016-01-30 20:48:16 +00:00
|
|
|
int score, score_max = 0;
|
2018-01-21 16:06:54 +00:00
|
|
|
void *i = 0;
|
2015-09-17 02:53:13 +00:00
|
|
|
const static uint8_t zerobuffer[AVPROBE_PADDING_SIZE];
|
2016-01-30 20:48:16 +00:00
|
|
|
enum nodat {
|
|
|
|
NO_ID3,
|
2016-01-30 21:01:16 +00:00
|
|
|
ID3_ALMOST_GREATER_PROBE,
|
2016-01-30 20:48:16 +00:00
|
|
|
ID3_GREATER_PROBE,
|
|
|
|
ID3_GREATER_MAX_PROBE,
|
|
|
|
} nodat = NO_ID3;
|
2014-07-29 17:55:15 +00:00
|
|
|
|
|
|
|
if (!lpd.buf)
|
2015-09-19 21:37:14 +00:00
|
|
|
lpd.buf = (unsigned char *) zerobuffer;
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
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) {
|
2016-01-30 21:01:16 +00:00
|
|
|
if (lpd.buf_size < 2LL*id3len + 16)
|
|
|
|
nodat = ID3_ALMOST_GREATER_PROBE;
|
2014-03-13 18:54:00 +00:00
|
|
|
lpd.buf += id3len;
|
|
|
|
lpd.buf_size -= id3len;
|
2014-07-29 17:55:15 +00:00
|
|
|
} else if (id3len >= PROBE_BUF_MAX) {
|
2016-01-30 20:48:16 +00:00
|
|
|
nodat = ID3_GREATER_MAX_PROBE;
|
2014-07-29 17:55:15 +00:00
|
|
|
} else
|
2016-01-30 20:48:16 +00:00
|
|
|
nodat = ID3_GREATER_PROBE;
|
2014-03-13 18:54:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 16:06:54 +00:00
|
|
|
while ((fmt1 = av_demuxer_iterate(&i))) {
|
2022-01-02 17:38:02 +00:00
|
|
|
if (fmt1->flags & AVFMT_EXPERIMENTAL)
|
|
|
|
continue;
|
2014-09-11 14:48:02 +00:00
|
|
|
if (!is_opened == !(fmt1->flags & AVFMT_NOFILE) && strcmp(fmt1->name, "image2"))
|
2014-03-13 18:54:00 +00:00
|
|
|
continue;
|
|
|
|
score = 0;
|
|
|
|
if (fmt1->read_probe) {
|
|
|
|
score = fmt1->read_probe(&lpd);
|
2015-04-20 15:38:56 +00:00
|
|
|
if (score)
|
|
|
|
av_log(NULL, AV_LOG_TRACE, "Probing %s score:%d size:%d\n", fmt1->name, score, lpd.buf_size);
|
2014-07-29 17:55:15 +00:00
|
|
|
if (fmt1->extensions && av_match_ext(lpd.filename, fmt1->extensions)) {
|
2016-01-30 21:01:16 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-07-29 17:55:15 +00:00
|
|
|
}
|
2014-03-13 18:54:00 +00:00
|
|
|
} else if (fmt1->extensions) {
|
|
|
|
if (av_match_ext(lpd.filename, fmt1->extensions))
|
|
|
|
score = AVPROBE_SCORE_EXTENSION;
|
|
|
|
}
|
2016-02-25 18:36:01 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 17:55:15 +00:00
|
|
|
if (score > score_max) {
|
|
|
|
score_max = score;
|
2021-02-25 02:11:32 +00:00
|
|
|
fmt = fmt1;
|
2014-07-29 17:55:15 +00:00
|
|
|
} else if (score == score_max)
|
2014-03-13 18:54:00 +00:00
|
|
|
fmt = NULL;
|
|
|
|
}
|
2016-01-30 20:48:16 +00:00
|
|
|
if (nodat == ID3_GREATER_PROBE)
|
2014-07-29 17:55:15 +00:00
|
|
|
score_max = FFMIN(AVPROBE_SCORE_EXTENSION / 2 - 1, score_max);
|
|
|
|
*score_ret = score_max;
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVInputFormat *av_probe_input_format2(const AVProbeData *pd,
|
|
|
|
int is_opened, int *score_max)
|
2014-07-29 17:55:15 +00:00
|
|
|
{
|
|
|
|
int score_ret;
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVInputFormat *fmt = av_probe_input_format3(pd, is_opened, &score_ret);
|
2014-07-29 17:55:15 +00:00
|
|
|
if (score_ret > *score_max) {
|
|
|
|
*score_max = score_ret;
|
|
|
|
return fmt;
|
|
|
|
} else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
const AVInputFormat *av_probe_input_format(const AVProbeData *pd, int is_opened)
|
2014-03-13 18:54:00 +00:00
|
|
|
{
|
|
|
|
int score = 0;
|
|
|
|
return av_probe_input_format2(pd, is_opened, &score);
|
|
|
|
}
|
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
int av_probe_input_buffer2(AVIOContext *pb, const AVInputFormat **fmt,
|
|
|
|
const char *filename, void *logctx,
|
|
|
|
unsigned int offset, unsigned int max_probe_size)
|
2014-03-13 18:54:00 +00:00
|
|
|
{
|
|
|
|
AVProbeData pd = { filename ? filename : "" };
|
|
|
|
uint8_t *buf = NULL;
|
2014-07-29 17:55:15 +00:00
|
|
|
int ret = 0, probe_size, buf_offset = 0;
|
|
|
|
int score = 0;
|
2014-07-29 19:38:15 +00:00
|
|
|
int ret2;
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
if (!max_probe_size)
|
|
|
|
max_probe_size = PROBE_BUF_MAX;
|
2014-07-29 17:55:15 +00:00
|
|
|
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);
|
2014-03-13 18:54:00 +00:00
|
|
|
return AVERROR(EINVAL);
|
2014-07-29 17:55:15 +00:00
|
|
|
}
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
if (offset >= max_probe_size)
|
|
|
|
return AVERROR(EINVAL);
|
2014-07-29 17:55:15 +00:00
|
|
|
|
2014-09-30 10:40:36 +00:00
|
|
|
if (pb->av_class) {
|
2014-10-10 01:08:11 +00:00
|
|
|
uint8_t *mime_type_opt = NULL;
|
2015-09-14 15:42:12 +00:00
|
|
|
char *semi;
|
2014-09-30 10:40:36 +00:00
|
|
|
av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
|
|
|
|
pd.mime_type = (const char *)mime_type_opt;
|
2015-09-14 15:42:12 +00:00
|
|
|
semi = pd.mime_type ? strchr(pd.mime_type, ';') : NULL;
|
2015-09-11 22:08:19 +00:00
|
|
|
if (semi) {
|
2015-09-14 15:42:12 +00:00
|
|
|
*semi = '\0';
|
2015-09-11 22:08:19 +00:00
|
|
|
}
|
2014-09-30 10:40:36 +00:00
|
|
|
}
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
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))) {
|
2014-07-29 17:55:15 +00:00
|
|
|
score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0;
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
/* Read probe data. */
|
|
|
|
if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
|
2014-03-13 21:11:12 +00:00
|
|
|
goto fail;
|
2014-07-29 17:55:15 +00:00
|
|
|
if ((ret = avio_read(pb, buf + buf_offset,
|
|
|
|
probe_size - buf_offset)) < 0) {
|
2014-03-13 18:54:00 +00:00
|
|
|
/* Fail if error was not end of file, otherwise, lower score. */
|
2014-03-13 21:11:12 +00:00
|
|
|
if (ret != AVERROR_EOF)
|
|
|
|
goto fail;
|
|
|
|
|
2014-03-13 18:54:00 +00:00
|
|
|
score = 0;
|
|
|
|
ret = 0; /* error was end of file, nothing read */
|
|
|
|
}
|
2014-07-29 17:55:15 +00:00
|
|
|
buf_offset += ret;
|
|
|
|
if (buf_offset < offset)
|
|
|
|
continue;
|
|
|
|
pd.buf_size = buf_offset - offset;
|
|
|
|
pd.buf = &buf[offset];
|
2014-03-13 18:54:00 +00:00
|
|
|
|
|
|
|
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. */
|
2014-07-29 17:55:15 +00:00
|
|
|
if (score <= AVPROBE_SCORE_RETRY) {
|
2014-03-13 18:54:00 +00:00
|
|
|
av_log(logctx, AV_LOG_WARNING,
|
2014-07-29 17:55:15 +00:00
|
|
|
"Format %s detected only with low score of %d, "
|
|
|
|
"misdetection possible!\n", (*fmt)->name, score);
|
2014-03-13 18:54:00 +00:00
|
|
|
} else
|
|
|
|
av_log(logctx, AV_LOG_DEBUG,
|
2014-07-29 17:55:15 +00:00
|
|
|
"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
|
2014-03-13 18:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 21:11:12 +00:00
|
|
|
if (!*fmt)
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
2014-03-13 18:54:00 +00:00
|
|
|
|
2014-03-13 21:11:12 +00:00
|
|
|
fail:
|
2014-03-13 18:54:00 +00:00
|
|
|
/* Rewind. Reuse probe buffer to avoid seeking. */
|
2014-07-29 19:38:15 +00:00
|
|
|
ret2 = ffio_rewind_with_probe_data(pb, &buf, buf_offset);
|
2014-07-29 18:57:42 +00:00
|
|
|
if (ret >= 0)
|
2014-07-29 19:38:15 +00:00
|
|
|
ret = ret2;
|
2014-07-29 17:55:15 +00:00
|
|
|
|
2014-09-30 10:40:36 +00:00
|
|
|
av_freep(&pd.mime_type);
|
2014-07-29 17:55:15 +00:00
|
|
|
return ret < 0 ? ret : score;
|
|
|
|
}
|
2014-03-13 18:54:00 +00:00
|
|
|
|
2021-02-25 02:11:32 +00:00
|
|
|
int av_probe_input_buffer(AVIOContext *pb, const AVInputFormat **fmt,
|
2014-07-29 17:55:15 +00:00
|
|
|
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;
|
2014-03-13 18:54:00 +00:00
|
|
|
}
|