2010-12-27 07:46:53 +00:00
|
|
|
/*
|
|
|
|
* Metadata demuxer
|
|
|
|
* Copyright (c) 2010 Anton Khirnov
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2018-12-13 12:06:30 +00:00
|
|
|
#include "libavutil/bprint.h"
|
2011-06-04 11:58:23 +00:00
|
|
|
#include "libavutil/mathematics.h"
|
2010-12-27 07:46:53 +00:00
|
|
|
#include "avformat.h"
|
2011-01-04 12:35:39 +00:00
|
|
|
#include "ffmeta.h"
|
2011-02-06 14:38:55 +00:00
|
|
|
#include "internal.h"
|
2011-05-22 10:46:29 +00:00
|
|
|
#include "libavutil/dict.h"
|
2010-12-27 07:46:53 +00:00
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int probe(const AVProbeData *p)
|
2010-12-27 07:46:53 +00:00
|
|
|
{
|
|
|
|
if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-13 12:06:30 +00:00
|
|
|
static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
|
|
|
|
{
|
|
|
|
int len, end;
|
|
|
|
int64_t read = 0;
|
|
|
|
char tmp[1024];
|
|
|
|
char c;
|
|
|
|
char prev = ' ';
|
|
|
|
|
|
|
|
do {
|
|
|
|
len = 0;
|
|
|
|
do {
|
|
|
|
c = avio_r8(s);
|
|
|
|
end = prev != '\\' && (c == '\r' || c == '\n' || c == '\0');
|
|
|
|
if (!end)
|
|
|
|
tmp[len++] = c;
|
|
|
|
prev = c;
|
|
|
|
} while (!end && len < sizeof(tmp));
|
|
|
|
av_bprint_append_data(bp, tmp, len);
|
|
|
|
read += len;
|
|
|
|
} while (!end);
|
|
|
|
|
|
|
|
if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
|
|
|
|
avio_skip(s, -1);
|
|
|
|
|
|
|
|
if (!c && s->error)
|
|
|
|
return s->error;
|
|
|
|
|
|
|
|
if (!c && !read && avio_feof(s))
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
|
|
|
|
{
|
|
|
|
|
|
|
|
do {
|
|
|
|
av_bprint_clear(bp);
|
|
|
|
read_line_to_bprint_escaped(s, bp);
|
|
|
|
} while (!avio_feof(s) && (bp->str[0] == ';' || bp->str[0] == '#' || bp->str[0] == 0));
|
|
|
|
}
|
|
|
|
|
2011-02-20 10:04:12 +00:00
|
|
|
static void get_line(AVIOContext *s, uint8_t *buf, int size)
|
2010-12-27 07:46:53 +00:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
uint8_t c;
|
|
|
|
int i = 0;
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
while ((c = avio_r8(s))) {
|
2010-12-27 07:46:53 +00:00
|
|
|
if (c == '\\') {
|
|
|
|
if (i < size - 1)
|
|
|
|
buf[i++] = c;
|
2011-02-21 15:43:01 +00:00
|
|
|
c = avio_r8(s);
|
2010-12-27 07:46:53 +00:00
|
|
|
} else if (c == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i < size - 1)
|
|
|
|
buf[i++] = c;
|
|
|
|
}
|
|
|
|
buf[i] = 0;
|
2014-08-07 20:12:41 +00:00
|
|
|
} while (!avio_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
|
2010-12-27 07:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static AVChapter *read_chapter(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
uint8_t line[256];
|
|
|
|
int64_t start, end;
|
|
|
|
AVRational tb = {1, 1e9};
|
2020-08-20 19:14:52 +00:00
|
|
|
int ret;
|
2010-12-27 07:46:53 +00:00
|
|
|
|
|
|
|
get_line(s->pb, line, sizeof(line));
|
|
|
|
|
|
|
|
if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
|
|
|
|
get_line(s->pb, line, sizeof(line));
|
2020-08-20 19:14:52 +00:00
|
|
|
ret = sscanf(line, "START=%"SCNd64, &start);
|
|
|
|
if (ret <= 0) {
|
2010-12-27 07:46:53 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
|
|
|
|
start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
|
|
|
|
s->chapters[s->nb_chapters - 1]->end : 0;
|
|
|
|
} else
|
|
|
|
get_line(s->pb, line, sizeof(line));
|
|
|
|
|
2020-08-20 19:14:52 +00:00
|
|
|
ret = sscanf(line, "END=%"SCNd64, &end);
|
|
|
|
if (ret <= 0) {
|
2010-12-27 07:46:53 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
|
|
|
|
end = AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
|
2011-10-17 06:58:50 +00:00
|
|
|
return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
|
2010-12-27 07:46:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 16:33:55 +00:00
|
|
|
static uint8_t *unescape(const uint8_t *buf, int size)
|
2010-12-27 07:46:53 +00:00
|
|
|
{
|
|
|
|
uint8_t *ret = av_malloc(size + 1);
|
2014-09-24 16:33:55 +00:00
|
|
|
uint8_t *p1 = ret;
|
|
|
|
const uint8_t *p2 = buf;
|
2010-12-27 07:46:53 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (p2 < buf + size) {
|
|
|
|
if (*p2 == '\\')
|
|
|
|
p2++;
|
|
|
|
*p1++ = *p2++;
|
|
|
|
}
|
|
|
|
*p1 = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-24 16:33:55 +00:00
|
|
|
static int read_tag(const uint8_t *line, AVDictionary **m)
|
2010-12-27 07:46:53 +00:00
|
|
|
{
|
2014-09-24 16:33:55 +00:00
|
|
|
uint8_t *key, *value;
|
|
|
|
const uint8_t *p = line;
|
2010-12-27 07:46:53 +00:00
|
|
|
|
|
|
|
/* find first not escaped '=' */
|
|
|
|
while (1) {
|
|
|
|
if (*p == '=')
|
|
|
|
break;
|
|
|
|
else if (*p == '\\')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(key = unescape(line, p - line)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
if (!(value = unescape(p + 1, strlen(p + 1)))) {
|
|
|
|
av_free(key);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2011-05-22 10:46:29 +00:00
|
|
|
av_dict_set(m, key, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
|
2010-12-27 07:46:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int read_header(AVFormatContext *s)
|
2010-12-27 07:46:53 +00:00
|
|
|
{
|
2011-05-22 10:46:29 +00:00
|
|
|
AVDictionary **m = &s->metadata;
|
2018-12-13 12:06:30 +00:00
|
|
|
AVBPrint bp;
|
|
|
|
|
|
|
|
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
|
2010-12-27 07:46:53 +00:00
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
while(!avio_feof(s->pb)) {
|
2018-12-13 12:06:30 +00:00
|
|
|
get_bprint_line(s->pb, &bp);
|
2010-12-27 07:46:53 +00:00
|
|
|
|
2018-12-13 12:06:30 +00:00
|
|
|
if (!memcmp(bp.str, ID_STREAM, strlen(ID_STREAM))) {
|
2011-06-18 09:43:24 +00:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2010-12-27 07:46:53 +00:00
|
|
|
|
|
|
|
if (!st)
|
2020-10-20 20:56:13 +00:00
|
|
|
goto nomem;
|
2010-12-27 07:46:53 +00:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_FFMETADATA;
|
2010-12-27 07:46:53 +00:00
|
|
|
|
|
|
|
m = &st->metadata;
|
2018-12-13 12:06:30 +00:00
|
|
|
} else if (!memcmp(bp.str, ID_CHAPTER, strlen(ID_CHAPTER))) {
|
2010-12-27 07:46:53 +00:00
|
|
|
AVChapter *ch = read_chapter(s);
|
|
|
|
|
|
|
|
if (!ch)
|
2020-10-20 20:56:13 +00:00
|
|
|
goto nomem;
|
2010-12-27 07:46:53 +00:00
|
|
|
|
|
|
|
m = &ch->metadata;
|
|
|
|
} else
|
2018-12-13 12:06:30 +00:00
|
|
|
read_tag(bp.str, m);
|
2010-12-27 07:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 12:06:30 +00:00
|
|
|
av_bprint_finalize(&bp, NULL);
|
|
|
|
|
2010-12-27 07:46:53 +00:00
|
|
|
s->start_time = 0;
|
|
|
|
if (s->nb_chapters)
|
|
|
|
s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
|
|
|
|
s->chapters[s->nb_chapters - 1]->time_base,
|
|
|
|
AV_TIME_BASE_Q);
|
|
|
|
|
|
|
|
return 0;
|
2020-10-20 20:56:13 +00:00
|
|
|
nomem:
|
|
|
|
av_bprint_finalize(&bp, NULL);
|
|
|
|
|
|
|
|
return AVERROR(ENOMEM);
|
2010-12-27 07:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_ffmetadata_demuxer = {
|
2010-12-27 07:46:53 +00:00
|
|
|
.name = "ffmetadata",
|
2012-07-24 21:51:41 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
|
2010-12-27 07:46:53 +00:00
|
|
|
.read_probe = probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
};
|