2010-12-27 09:46:53 +02:00
|
|
|
/*
|
|
|
|
* Metadata demuxer
|
|
|
|
* Copyright (c) 2010 Anton Khirnov
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2010-12-27 09:46:53 +02:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-12-27 09:46:53 +02: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.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-12-27 09:46:53 +02: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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-12-27 09:46:53 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2011-06-04 14:58:23 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
2010-12-27 09:46:53 +02:00
|
|
|
#include "avformat.h"
|
2011-01-04 14:35:39 +02:00
|
|
|
#include "ffmeta.h"
|
2011-02-06 16:38:55 +02:00
|
|
|
#include "internal.h"
|
2011-05-22 13:46:29 +03:00
|
|
|
#include "libavutil/dict.h"
|
2010-12-27 09:46:53 +02:00
|
|
|
|
|
|
|
static int probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 12:04:12 +02:00
|
|
|
static void get_line(AVIOContext *s, uint8_t *buf, int size)
|
2010-12-27 09:46:53 +02:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
uint8_t c;
|
|
|
|
int i = 0;
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
while ((c = avio_r8(s))) {
|
2010-12-27 09:46:53 +02:00
|
|
|
if (c == '\\') {
|
|
|
|
if (i < size - 1)
|
|
|
|
buf[i++] = c;
|
2011-02-21 17:43:01 +02:00
|
|
|
c = avio_r8(s);
|
2010-12-27 09:46:53 +02:00
|
|
|
} else if (c == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i < size - 1)
|
|
|
|
buf[i++] = c;
|
|
|
|
}
|
|
|
|
buf[i] = 0;
|
2011-03-07 22:50:25 +02:00
|
|
|
} while (!s->eof_reached && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
|
2010-12-27 09:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static AVChapter *read_chapter(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
uint8_t line[256];
|
|
|
|
int64_t start, end;
|
|
|
|
AVRational tb = {1, 1e9};
|
|
|
|
|
|
|
|
get_line(s->pb, line, sizeof(line));
|
|
|
|
|
|
|
|
if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
|
|
|
|
get_line(s->pb, line, sizeof(line));
|
2011-03-24 13:41:03 +02:00
|
|
|
if (!sscanf(line, "START=%"SCNd64, &start)) {
|
2010-12-27 09:46:53 +02: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));
|
|
|
|
|
2011-03-24 13:41:03 +02:00
|
|
|
if (!sscanf(line, "END=%"SCNd64, &end)) {
|
2010-12-27 09:46:53 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
|
|
|
|
end = AV_NOPTS_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ff_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *unescape(uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
uint8_t *ret = av_malloc(size + 1);
|
|
|
|
uint8_t *p1 = ret, *p2 = buf;
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (p2 < buf + size) {
|
|
|
|
if (*p2 == '\\')
|
|
|
|
p2++;
|
|
|
|
*p1++ = *p2++;
|
|
|
|
}
|
|
|
|
*p1 = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-22 13:46:29 +03:00
|
|
|
static int read_tag(uint8_t *line, AVDictionary **m)
|
2010-12-27 09:46:53 +02:00
|
|
|
{
|
|
|
|
uint8_t *key, *value, *p = line;
|
|
|
|
|
|
|
|
/* 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 13:46:29 +03:00
|
|
|
av_dict_set(m, key, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
|
2010-12-27 09:46:53 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_header(AVFormatContext *s, AVFormatParameters *ap)
|
|
|
|
{
|
2011-05-22 13:46:29 +03:00
|
|
|
AVDictionary **m = &s->metadata;
|
2010-12-27 09:46:53 +02:00
|
|
|
uint8_t line[1024];
|
|
|
|
|
2011-03-07 22:50:25 +02:00
|
|
|
while(!s->pb->eof_reached) {
|
2010-12-27 09:46:53 +02:00
|
|
|
get_line(s->pb, line, sizeof(line));
|
|
|
|
|
|
|
|
if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
|
2011-06-18 12:43:24 +03:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2010-12-27 09:46:53 +02:00
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_DATA;
|
|
|
|
st->codec->codec_id = CODEC_ID_FFMETADATA;
|
|
|
|
|
|
|
|
m = &st->metadata;
|
|
|
|
} else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
|
|
|
|
AVChapter *ch = read_chapter(s);
|
|
|
|
|
|
|
|
if (!ch)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
m = &ch->metadata;
|
|
|
|
} else
|
|
|
|
read_tag(line, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_ffmetadata_demuxer = {
|
2010-12-27 09:46:53 +02:00
|
|
|
.name = "ffmetadata",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
|
|
|
|
.read_probe = probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
};
|