Git init
[framework/multimedia/ffmpeg.git] / libavformat / ffmetadec.c
1 /*
2  * Metadata demuxer
3  * Copyright (c) 2010 Anton Khirnov
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 "avformat.h"
23 #include "ffmeta.h"
24 #include "internal.h"
25 #include "libavutil/dict.h"
26
27 static int probe(AVProbeData *p)
28 {
29     if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
30         return AVPROBE_SCORE_MAX;
31     return 0;
32 }
33
34 static void get_line(AVIOContext *s, uint8_t *buf, int size)
35 {
36     do {
37         uint8_t c;
38         int i = 0;
39
40         while ((c = avio_r8(s))) {
41             if (c == '\\') {
42                 if (i < size - 1)
43                     buf[i++] = c;
44                 c = avio_r8(s);
45             } else if (c == '\n')
46                 break;
47
48             if (i < size - 1)
49                 buf[i++] = c;
50         }
51         buf[i] = 0;
52     } while (!url_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
53 }
54
55 static AVChapter *read_chapter(AVFormatContext *s)
56 {
57     uint8_t line[256];
58     int64_t start, end;
59     AVRational tb = {1, 1e9};
60
61     get_line(s->pb, line, sizeof(line));
62
63     if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
64         get_line(s->pb, line, sizeof(line));
65     if (!sscanf(line, "START=%"SCNd64, &start)) {
66         av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
67         start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
68                  s->chapters[s->nb_chapters - 1]->end : 0;
69     } else
70         get_line(s->pb, line, sizeof(line));
71
72     if (!sscanf(line, "END=%"SCNd64, &end)) {
73         av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
74         end = AV_NOPTS_VALUE;
75     }
76
77     return ff_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
78 }
79
80 static uint8_t *unescape(uint8_t *buf, int size)
81 {
82     uint8_t *ret = av_malloc(size + 1);
83     uint8_t *p1  = ret, *p2 = buf;
84
85     if (!ret)
86         return NULL;
87
88     while (p2 < buf + size) {
89         if (*p2 == '\\')
90             p2++;
91         *p1++ = *p2++;
92     }
93     *p1 = 0;
94     return ret;
95 }
96
97 static int read_tag(uint8_t *line, AVDictionary **m)
98 {
99     uint8_t *key, *value, *p = line;
100
101     /* find first not escaped '=' */
102     while (1) {
103         if (*p == '=')
104             break;
105         else if (*p == '\\')
106             p++;
107
108         if (*p++)
109             continue;
110
111         return 0;
112     }
113
114     if (!(key = unescape(line, p - line)))
115         return AVERROR(ENOMEM);
116     if (!(value = unescape(p + 1, strlen(p + 1)))) {
117         av_free(key);
118         return AVERROR(ENOMEM);
119     }
120
121     av_dict_set(m, key, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
122     return 0;
123 }
124
125 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
126 {
127     AVDictionary **m = &s->metadata;
128     uint8_t line[1024];
129
130     while(!url_feof(s->pb)) {
131         get_line(s->pb, line, sizeof(line));
132
133         if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
134             AVStream *st = av_new_stream(s, 0);
135
136             if (!st)
137                 return -1;
138
139             st->codec->codec_type = AVMEDIA_TYPE_DATA;
140             st->codec->codec_id   = CODEC_ID_FFMETADATA;
141
142             m = &st->metadata;
143         } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
144             AVChapter *ch = read_chapter(s);
145
146             if (!ch)
147                 return -1;
148
149             m = &ch->metadata;
150         } else
151             read_tag(line, m);
152     }
153
154     s->start_time = 0;
155     if (s->nb_chapters)
156         s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
157                                    s->chapters[s->nb_chapters - 1]->time_base,
158                                    AV_TIME_BASE_Q);
159
160     return 0;
161 }
162
163 static int read_packet(AVFormatContext *s, AVPacket *pkt)
164 {
165     return AVERROR_EOF;
166 }
167
168 AVInputFormat ff_ffmetadata_demuxer = {
169     .name        = "ffmetadata",
170     .long_name   = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
171     .read_probe  = probe,
172     .read_header = read_header,
173     .read_packet = read_packet,
174 };