]> git.sesse.net Git - ffmpeg/blob - libavformat/ffmetadec.c
Merge commit '2eeac79936e83c4495cbe5905064ab797e9b45ff'
[ffmpeg] / 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 "libavutil/bprint.h"
23 #include "libavutil/mathematics.h"
24 #include "avformat.h"
25 #include "ffmeta.h"
26 #include "internal.h"
27 #include "libavutil/dict.h"
28
29 static int probe(AVProbeData *p)
30 {
31     if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
32         return AVPROBE_SCORE_MAX;
33     return 0;
34 }
35
36 static int64_t read_line_to_bprint_escaped(AVIOContext *s, AVBPrint *bp)
37 {
38     int len, end;
39     int64_t read = 0;
40     char tmp[1024];
41     char c;
42     char prev = ' ';
43
44     do {
45         len = 0;
46         do {
47             c = avio_r8(s);
48             end = prev != '\\' && (c == '\r' || c == '\n' || c == '\0');
49             if (!end)
50                 tmp[len++] = c;
51             prev = c;
52         } while (!end && len < sizeof(tmp));
53         av_bprint_append_data(bp, tmp, len);
54         read += len;
55     } while (!end);
56
57     if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
58         avio_skip(s, -1);
59
60     if (!c && s->error)
61         return s->error;
62
63     if (!c && !read && avio_feof(s))
64         return AVERROR_EOF;
65
66     return read;
67 }
68
69 static void get_bprint_line(AVIOContext *s, AVBPrint *bp)
70 {
71
72     do {
73         av_bprint_clear(bp);
74         read_line_to_bprint_escaped(s, bp);
75     } while (!avio_feof(s) && (bp->str[0] == ';' || bp->str[0] == '#' || bp->str[0] == 0));
76 }
77
78 static void get_line(AVIOContext *s, uint8_t *buf, int size)
79 {
80     do {
81         uint8_t c;
82         int i = 0;
83
84         while ((c = avio_r8(s))) {
85             if (c == '\\') {
86                 if (i < size - 1)
87                     buf[i++] = c;
88                 c = avio_r8(s);
89             } else if (c == '\n')
90                 break;
91
92             if (i < size - 1)
93                 buf[i++] = c;
94         }
95         buf[i] = 0;
96     } while (!avio_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
97 }
98
99 static AVChapter *read_chapter(AVFormatContext *s)
100 {
101     uint8_t line[256];
102     int64_t start, end;
103     AVRational tb = {1, 1e9};
104
105     get_line(s->pb, line, sizeof(line));
106
107     if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
108         get_line(s->pb, line, sizeof(line));
109     if (!sscanf(line, "START=%"SCNd64, &start)) {
110         av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
111         start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
112                  s->chapters[s->nb_chapters - 1]->end : 0;
113     } else
114         get_line(s->pb, line, sizeof(line));
115
116     if (!sscanf(line, "END=%"SCNd64, &end)) {
117         av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
118         end = AV_NOPTS_VALUE;
119     }
120
121     return avpriv_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
122 }
123
124 static uint8_t *unescape(const uint8_t *buf, int size)
125 {
126     uint8_t *ret = av_malloc(size + 1);
127     uint8_t *p1  = ret;
128     const uint8_t *p2 = buf;
129
130     if (!ret)
131         return NULL;
132
133     while (p2 < buf + size) {
134         if (*p2 == '\\')
135             p2++;
136         *p1++ = *p2++;
137     }
138     *p1 = 0;
139     return ret;
140 }
141
142 static int read_tag(const uint8_t *line, AVDictionary **m)
143 {
144     uint8_t *key, *value;
145     const uint8_t *p = line;
146
147     /* find first not escaped '=' */
148     while (1) {
149         if (*p == '=')
150             break;
151         else if (*p == '\\')
152             p++;
153
154         if (*p++)
155             continue;
156
157         return 0;
158     }
159
160     if (!(key = unescape(line, p - line)))
161         return AVERROR(ENOMEM);
162     if (!(value = unescape(p + 1, strlen(p + 1)))) {
163         av_free(key);
164         return AVERROR(ENOMEM);
165     }
166
167     av_dict_set(m, key, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
168     return 0;
169 }
170
171 static int read_header(AVFormatContext *s)
172 {
173     AVDictionary **m = &s->metadata;
174     AVBPrint bp;
175
176     av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
177
178     while(!avio_feof(s->pb)) {
179         get_bprint_line(s->pb, &bp);
180
181         if (!memcmp(bp.str, ID_STREAM, strlen(ID_STREAM))) {
182             AVStream *st = avformat_new_stream(s, NULL);
183
184             if (!st)
185                 return AVERROR(ENOMEM);
186
187             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
188             st->codecpar->codec_id   = AV_CODEC_ID_FFMETADATA;
189
190             m = &st->metadata;
191         } else if (!memcmp(bp.str, ID_CHAPTER, strlen(ID_CHAPTER))) {
192             AVChapter *ch = read_chapter(s);
193
194             if (!ch)
195                 return AVERROR(ENOMEM);
196
197             m = &ch->metadata;
198         } else
199             read_tag(bp.str, m);
200     }
201
202     av_bprint_finalize(&bp, NULL);
203
204     s->start_time = 0;
205     if (s->nb_chapters)
206         s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
207                                    s->chapters[s->nb_chapters - 1]->time_base,
208                                    AV_TIME_BASE_Q);
209
210     return 0;
211 }
212
213 static int read_packet(AVFormatContext *s, AVPacket *pkt)
214 {
215     return AVERROR_EOF;
216 }
217
218 AVInputFormat ff_ffmetadata_demuxer = {
219     .name        = "ffmetadata",
220     .long_name   = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text"),
221     .read_probe  = probe,
222     .read_header = read_header,
223     .read_packet = read_packet,
224 };