Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (C) 2005 Michael Ahlberg, Måns Rullgård | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person | ||
5 | * obtaining a copy of this software and associated documentation | ||
6 | * files (the "Software"), to deal in the Software without | ||
7 | * restriction, including without limitation the rights to use, copy, | ||
8 | * modify, merge, publish, distribute, sublicense, and/or sell copies | ||
9 | * of the Software, and to permit persons to whom the Software is | ||
10 | * furnished to do so, subject to the following conditions: | ||
11 | * | ||
12 | * The above copyright notice and this permission notice shall be | ||
13 | * included in all copies or substantial portions of the Software. | ||
14 | * | ||
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
19 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
20 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
22 | * DEALINGS IN THE SOFTWARE. | ||
23 | */ | ||
24 | |||
25 | #include <stdlib.h> | ||
26 | |||
27 | #include "libavutil/avstring.h" | ||
28 | #include "libavutil/base64.h" | ||
29 | #include "libavutil/dict.h" | ||
30 | #include "libavutil/mem.h" | ||
31 | |||
32 | #include "libavcodec/bytestream.h" | ||
33 | #include "libavcodec/vorbis_parser.h" | ||
34 | |||
35 | #include "avformat.h" | ||
36 | #include "demux.h" | ||
37 | #include "flac_picture.h" | ||
38 | #include "internal.h" | ||
39 | #include "oggdec.h" | ||
40 | #include "vorbiscomment.h" | ||
41 | #include "replaygain.h" | ||
42 | |||
43 | 183 | static int ogm_chapter(AVFormatContext *as, const uint8_t *key, const uint8_t *val) | |
44 | { | ||
45 | 183 | int i, cnum, h, m, s, ms, keylen = strlen(key); | |
46 | 183 | AVChapter *chapter = NULL; | |
47 | |||
48 |
5/6✓ Branch 0 taken 78 times.
✓ Branch 1 taken 105 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
|
183 | if (keylen < 9 || av_strncasecmp(key, "CHAPTER", 7) || sscanf(key+7, "%03d", &cnum) != 1) |
49 | 159 | return 0; | |
50 | |||
51 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (keylen <= 10) { |
52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (sscanf(val, "%02d:%02d:%02d.%03d", &h, &m, &s, &ms) < 4) |
53 | ✗ | return 0; | |
54 | |||
55 | 12 | avpriv_new_chapter(as, cnum, (AVRational) { 1, 1000 }, | |
56 | 12 | ms + 1000 * (s + 60 * (m + 60 * h)), | |
57 | AV_NOPTS_VALUE, NULL); | ||
58 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | } else if (!av_strcasecmp(key + keylen - 4, "NAME")) { |
59 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | for (i = 0; i < as->nb_chapters; i++) |
60 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 18 times.
|
30 | if (as->chapters[i]->id == cnum) { |
61 | 12 | chapter = as->chapters[i]; | |
62 | 12 | break; | |
63 | } | ||
64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (!chapter) |
65 | ✗ | return 0; | |
66 | |||
67 | 12 | av_dict_set(&chapter->metadata, "title", val, 0); | |
68 | } else | ||
69 | ✗ | return 0; | |
70 | |||
71 | 24 | return 1; | |
72 | } | ||
73 | |||
74 | 37 | int ff_vorbis_stream_comment(AVFormatContext *as, AVStream *st, | |
75 | const uint8_t *buf, int size) | ||
76 | { | ||
77 | 37 | int updates = ff_vorbis_comment(as, &st->metadata, buf, size, 1); | |
78 | |||
79 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 12 times.
|
37 | if (updates > 0) { |
80 | 25 | st->event_flags |= AVSTREAM_EVENT_FLAG_METADATA_UPDATED; | |
81 | } | ||
82 | |||
83 | 37 | return updates; | |
84 | } | ||
85 | |||
86 | /** | ||
87 | * This function temporarily modifies the (const qualified) input buffer | ||
88 | * and reverts its changes before return. The input buffer needs to have | ||
89 | * at least one byte of padding. | ||
90 | */ | ||
91 | 190 | static int vorbis_parse_single_comment(AVFormatContext *as, AVDictionary **m, | |
92 | const uint8_t *buf, uint32_t size, | ||
93 | int *updates, int parse_picture) | ||
94 | { | ||
95 | 190 | char *t = (char*)buf, *v = memchr(t, '=', size); | |
96 | int tl, vl; | ||
97 | char backup; | ||
98 | |||
99 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 187 times.
|
190 | if (!v) |
100 | 3 | return 0; | |
101 | |||
102 | 187 | tl = v - t; | |
103 | 187 | vl = size - tl - 1; | |
104 | 187 | v++; | |
105 | |||
106 |
2/4✓ Branch 0 taken 187 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 187 times.
|
187 | if (!tl || !vl) |
107 | ✗ | return 0; | |
108 | |||
109 | 187 | t[tl] = 0; | |
110 | |||
111 | 187 | backup = v[vl]; | |
112 | 187 | v[vl] = 0; | |
113 | |||
114 | /* The format in which the pictures are stored is the FLAC format. | ||
115 | * Xiph says: "The binary FLAC picture structure is base64 encoded | ||
116 | * and placed within a VorbisComment with the tag name | ||
117 | * 'METADATA_BLOCK_PICTURE'. This is the preferred and | ||
118 | * recommended way of embedding cover art within VorbisComments." | ||
119 | */ | ||
120 |
3/4✓ Branch 1 taken 4 times.
✓ Branch 2 taken 183 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
191 | if (!av_strcasecmp(t, "METADATA_BLOCK_PICTURE") && parse_picture) { |
121 | 4 | int ret, len = AV_BASE64_DECODE_SIZE(vl); | |
122 | 4 | uint8_t *pict = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE); | |
123 | |||
124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!pict) { |
125 | ✗ | av_log(as, AV_LOG_WARNING, "out-of-memory error. Skipping cover art block.\n"); | |
126 | ✗ | goto end; | |
127 | } | ||
128 | 4 | ret = av_base64_decode(pict, v, len); | |
129 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (ret > 0) |
130 | 4 | ret = ff_flac_parse_picture(as, &pict, ret, 0); | |
131 | 4 | av_freep(&pict); | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) { |
133 | ✗ | av_log(as, AV_LOG_WARNING, "Failed to parse cover art block.\n"); | |
134 | ✗ | goto end; | |
135 | } | ||
136 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 2 taken 159 times.
|
183 | } else if (!ogm_chapter(as, t, v)) { |
137 | 159 | (*updates)++; | |
138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 159 times.
|
159 | if (av_dict_get(*m, t, NULL, 0)) |
139 | ✗ | av_dict_set(m, t, ";", AV_DICT_APPEND); | |
140 | 159 | av_dict_set(m, t, v, AV_DICT_APPEND); | |
141 | } | ||
142 | 24 | end: | |
143 | 187 | t[tl] = '='; | |
144 | 187 | v[vl] = backup; | |
145 | |||
146 | 187 | return 0; | |
147 | } | ||
148 | |||
149 | 76 | int ff_vorbis_comment(AVFormatContext *as, AVDictionary **m, | |
150 | const uint8_t *buf, int size, | ||
151 | int parse_picture) | ||
152 | { | ||
153 | 76 | const uint8_t *p = buf; | |
154 | 76 | const uint8_t *end = buf + size; | |
155 | 76 | int updates = 0; | |
156 | unsigned n; | ||
157 | int s, ret; | ||
158 | |||
159 | /* must have vendor_length and user_comment_list_length */ | ||
160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
|
76 | if (size < 8) |
161 | ✗ | return AVERROR_INVALIDDATA; | |
162 | |||
163 | 76 | s = bytestream_get_le32(&p); | |
164 | |||
165 |
2/4✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 76 times.
|
76 | if (end - p - 4 < s || s < 0) |
166 | ✗ | return AVERROR_INVALIDDATA; | |
167 | |||
168 | 76 | p += s; | |
169 | |||
170 | 76 | n = bytestream_get_le32(&p); | |
171 | |||
172 |
4/4✓ Branch 0 taken 194 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 190 times.
✓ Branch 3 taken 4 times.
|
266 | while (end - p >= 4 && n > 0) { |
173 | 190 | s = bytestream_get_le32(&p); | |
174 | |||
175 |
2/4✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 190 times.
✗ Branch 3 not taken.
|
190 | if (end - p < s || s < 0) |
176 | break; | ||
177 | |||
178 | 190 | ret = vorbis_parse_single_comment(as, m, p, s, &updates, parse_picture); | |
179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 190 times.
|
190 | if (ret < 0) |
180 | ✗ | return ret; | |
181 | 190 | p += s; | |
182 | 190 | n--; | |
183 | } | ||
184 | |||
185 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 72 times.
|
76 | if (p != end) |
186 | 4 | av_log(as, AV_LOG_INFO, | |
187 | "%"PTRDIFF_SPECIFIER" bytes of comment header remain\n", end - p); | ||
188 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
|
76 | if (n > 0) |
189 | ✗ | av_log(as, AV_LOG_INFO, | |
190 | "truncated comment header, %i comments not found\n", n); | ||
191 | |||
192 | 76 | ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv); | |
193 | |||
194 | 76 | return updates; | |
195 | } | ||
196 | |||
197 | /* | ||
198 | * Parse the vorbis header | ||
199 | * | ||
200 | * Vorbis Identification header from Vorbis_I_spec.html#vorbis-spec-codec | ||
201 | * [vorbis_version] = read 32 bits as unsigned integer | Not used | ||
202 | * [audio_channels] = read 8 bit integer as unsigned | Used | ||
203 | * [audio_sample_rate] = read 32 bits as unsigned integer | Used | ||
204 | * [bitrate_maximum] = read 32 bits as signed integer | Not used yet | ||
205 | * [bitrate_nominal] = read 32 bits as signed integer | Not used yet | ||
206 | * [bitrate_minimum] = read 32 bits as signed integer | Used as bitrate | ||
207 | * [blocksize_0] = read 4 bits as unsigned integer | Not Used | ||
208 | * [blocksize_1] = read 4 bits as unsigned integer | Not Used | ||
209 | * [framing_flag] = read one bit | Not Used | ||
210 | */ | ||
211 | |||
212 | struct oggvorbis_private { | ||
213 | unsigned int len[3]; | ||
214 | unsigned char *packet[3]; | ||
215 | AVVorbisParseContext *vp; | ||
216 | int64_t final_pts; | ||
217 | int final_duration; | ||
218 | }; | ||
219 | |||
220 | 27 | static int fixup_vorbis_headers(AVFormatContext *as, | |
221 | struct oggvorbis_private *priv, | ||
222 | uint8_t **buf) | ||
223 | { | ||
224 | int i, offset, len, err; | ||
225 | int buf_len; | ||
226 | unsigned char *ptr; | ||
227 | |||
228 | 27 | len = priv->len[0] + priv->len[1] + priv->len[2]; | |
229 | 27 | buf_len = len + len / 255 + 64; | |
230 | |||
231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (*buf) |
232 | ✗ | return AVERROR_INVALIDDATA; | |
233 | |||
234 | 27 | ptr = *buf = av_realloc(NULL, buf_len); | |
235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (!ptr) |
236 | ✗ | return AVERROR(ENOMEM); | |
237 | 27 | memset(*buf, '\0', buf_len); | |
238 | |||
239 | 27 | ptr[0] = 2; | |
240 | 27 | offset = 1; | |
241 | 27 | offset += av_xiphlacing(&ptr[offset], priv->len[0]); | |
242 | 27 | offset += av_xiphlacing(&ptr[offset], priv->len[1]); | |
243 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 27 times.
|
108 | for (i = 0; i < 3; i++) { |
244 | 81 | memcpy(&ptr[offset], priv->packet[i], priv->len[i]); | |
245 | 81 | offset += priv->len[i]; | |
246 | 81 | av_freep(&priv->packet[i]); | |
247 | } | ||
248 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if ((err = av_reallocp(buf, offset + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) |
249 | ✗ | return err; | |
250 | 27 | return offset; | |
251 | } | ||
252 | |||
253 | 27 | static void vorbis_cleanup(AVFormatContext *s, int idx) | |
254 | { | ||
255 | 27 | struct ogg *ogg = s->priv_data; | |
256 | 27 | struct ogg_stream *os = ogg->streams + idx; | |
257 | 27 | struct oggvorbis_private *priv = os->private; | |
258 | int i; | ||
259 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (os->private) { |
260 | 27 | av_vorbis_parse_free(&priv->vp); | |
261 |
2/2✓ Branch 0 taken 81 times.
✓ Branch 1 taken 27 times.
|
108 | for (i = 0; i < 3; i++) |
262 | 81 | av_freep(&priv->packet[i]); | |
263 | } | ||
264 | 27 | } | |
265 | |||
266 | 27 | static int vorbis_update_metadata(AVFormatContext *s, int idx) | |
267 | { | ||
268 | 27 | struct ogg *ogg = s->priv_data; | |
269 | 27 | struct ogg_stream *os = ogg->streams + idx; | |
270 | 27 | AVStream *st = s->streams[idx]; | |
271 | int ret; | ||
272 | |||
273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (os->psize <= 8) |
274 | ✗ | return 0; | |
275 | |||
276 | /* New metadata packet; release old data. */ | ||
277 | 27 | av_dict_free(&st->metadata); | |
278 | 27 | ret = ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 7, | |
279 | 27 | os->psize - 8); | |
280 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (ret < 0) |
281 | ✗ | return ret; | |
282 | |||
283 | /* Update the metadata if possible. */ | ||
284 | 27 | av_freep(&os->new_metadata); | |
285 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 10 times.
|
27 | if (st->metadata) { |
286 | 17 | os->new_metadata = av_packet_pack_dictionary(st->metadata, &os->new_metadata_size); | |
287 | /* Send an empty dictionary to indicate that metadata has been cleared. */ | ||
288 | } else { | ||
289 | 10 | os->new_metadata = av_mallocz(1); | |
290 | 10 | os->new_metadata_size = 0; | |
291 | } | ||
292 | |||
293 | 27 | return ret; | |
294 | } | ||
295 | |||
296 | 109 | static int vorbis_header(AVFormatContext *s, int idx) | |
297 | { | ||
298 | 109 | struct ogg *ogg = s->priv_data; | |
299 | 109 | AVStream *st = s->streams[idx]; | |
300 | 109 | struct ogg_stream *os = ogg->streams + idx; | |
301 | struct oggvorbis_private *priv; | ||
302 | 109 | int pkt_type = os->buf[os->pstart]; | |
303 | |||
304 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 82 times.
|
109 | if (!os->private) { |
305 | 27 | os->private = av_mallocz(sizeof(struct oggvorbis_private)); | |
306 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (!os->private) |
307 | ✗ | return AVERROR(ENOMEM); | |
308 | } | ||
309 | |||
310 | 109 | priv = os->private; | |
311 | |||
312 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 81 times.
|
109 | if (!(pkt_type & 1)) |
313 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | return priv->vp ? 0 : AVERROR_INVALIDDATA; |
314 | |||
315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (pkt_type > 5) { |
316 | ✗ | av_log(s, AV_LOG_VERBOSE, "Ignoring packet with unknown type %d\n", pkt_type); | |
317 | ✗ | return 1; | |
318 | } | ||
319 | |||
320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (os->psize < 1) |
321 | ✗ | return AVERROR_INVALIDDATA; | |
322 | |||
323 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (priv->packet[pkt_type >> 1]) |
324 | ✗ | return AVERROR_INVALIDDATA; | |
325 |
6/8✓ Branch 0 taken 54 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 54 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 27 times.
|
81 | if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1]) |
326 | ✗ | return priv->vp ? 0 : AVERROR_INVALIDDATA; | |
327 | |||
328 | 81 | priv->len[pkt_type >> 1] = os->psize; | |
329 | 81 | priv->packet[pkt_type >> 1] = av_memdup(os->buf + os->pstart, os->psize); | |
330 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (!priv->packet[pkt_type >> 1]) |
331 | ✗ | return AVERROR(ENOMEM); | |
332 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 54 times.
|
81 | if (os->buf[os->pstart] == 1) { |
333 | 27 | const uint8_t *p = os->buf + os->pstart + 7; /* skip "\001vorbis" tag */ | |
334 | unsigned blocksize, bs0, bs1; | ||
335 | int srate; | ||
336 | int channels; | ||
337 | |||
338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (os->psize != 30) |
339 | ✗ | return AVERROR_INVALIDDATA; | |
340 | |||
341 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (bytestream_get_le32(&p) != 0) /* vorbis_version */ |
342 | ✗ | return AVERROR_INVALIDDATA; | |
343 | |||
344 | 27 | channels = bytestream_get_byte(&p); | |
345 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (st->codecpar->ch_layout.nb_channels && |
346 | ✗ | channels != st->codecpar->ch_layout.nb_channels) { | |
347 | ✗ | av_log(s, AV_LOG_ERROR, "Channel change is not supported\n"); | |
348 | ✗ | return AVERROR_PATCHWELCOME; | |
349 | } | ||
350 | 27 | st->codecpar->ch_layout.nb_channels = channels; | |
351 | 27 | srate = bytestream_get_le32(&p); | |
352 | 27 | p += 4; // skip maximum bitrate | |
353 | 27 | st->codecpar->bit_rate = bytestream_get_le32(&p); // nominal bitrate | |
354 | 27 | p += 4; // skip minimum bitrate | |
355 | |||
356 | 27 | blocksize = bytestream_get_byte(&p); | |
357 | 27 | bs0 = blocksize & 15; | |
358 | 27 | bs1 = blocksize >> 4; | |
359 | |||
360 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (bs0 > bs1) |
361 | ✗ | return AVERROR_INVALIDDATA; | |
362 |
2/4✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
|
27 | if (bs0 < 6 || bs1 > 13) |
363 | ✗ | return AVERROR_INVALIDDATA; | |
364 | |||
365 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (bytestream_get_byte(&p) != 1) /* framing_flag */ |
366 | ✗ | return AVERROR_INVALIDDATA; | |
367 | |||
368 | 27 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
369 | 27 | st->codecpar->codec_id = AV_CODEC_ID_VORBIS; | |
370 | |||
371 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (srate > 0) { |
372 | 27 | st->codecpar->sample_rate = srate; | |
373 | 27 | avpriv_set_pts_info(st, 64, 1, srate); | |
374 | } | ||
375 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
|
54 | } else if (os->buf[os->pstart] == 3) { |
376 |
2/4✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
✗ Branch 4 not taken.
|
27 | if (vorbis_update_metadata(s, idx) >= 0 && priv->len[1] > 10) { |
377 | unsigned new_len; | ||
378 | |||
379 | 27 | int ret = ff_replaygain_export(st, st->metadata); | |
380 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (ret < 0) |
381 | ✗ | return ret; | |
382 | |||
383 | // drop all metadata we parsed and which is not required by libvorbis | ||
384 | 27 | new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1; | |
385 |
3/4✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 8 times.
|
27 | if (new_len >= 16 && new_len < os->psize) { |
386 | 19 | AV_WL32(priv->packet[1] + new_len - 5, 0); | |
387 | 19 | priv->packet[1][new_len - 1] = 1; | |
388 | 19 | priv->len[1] = new_len; | |
389 | } | ||
390 | } | ||
391 | } else { | ||
392 | int ret; | ||
393 | |||
394 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (priv->vp) |
395 | ✗ | return AVERROR_INVALIDDATA; | |
396 | |||
397 | 27 | ret = fixup_vorbis_headers(s, priv, &st->codecpar->extradata); | |
398 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (ret < 0) { |
399 | ✗ | st->codecpar->extradata_size = 0; | |
400 | ✗ | return ret; | |
401 | } | ||
402 | 27 | st->codecpar->extradata_size = ret; | |
403 | |||
404 | 27 | priv->vp = av_vorbis_parse_init(st->codecpar->extradata, st->codecpar->extradata_size); | |
405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (!priv->vp) { |
406 | ✗ | av_freep(&st->codecpar->extradata); | |
407 | ✗ | st->codecpar->extradata_size = 0; | |
408 | ✗ | return AVERROR_UNKNOWN; | |
409 | } | ||
410 | } | ||
411 | |||
412 | 81 | return 1; | |
413 | } | ||
414 | |||
415 | 11103 | static int vorbis_packet(AVFormatContext *s, int idx) | |
416 | { | ||
417 | 11103 | struct ogg *ogg = s->priv_data; | |
418 | 11103 | struct ogg_stream *os = ogg->streams + idx; | |
419 | 11103 | struct oggvorbis_private *priv = os->private; | |
420 | 11103 | int duration, flags = 0; | |
421 | |||
422 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11103 times.
|
11103 | if (!priv->vp) |
423 | ✗ | return AVERROR_INVALIDDATA; | |
424 | |||
425 | /* first packet handling | ||
426 | * here we parse the duration of each packet in the first page and compare | ||
427 | * the total duration to the page granule to find the encoder delay and | ||
428 | * set the first timestamp */ | ||
429 |
8/8✓ Branch 0 taken 11049 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 10571 times.
✓ Branch 3 taken 478 times.
✓ Branch 4 taken 10499 times.
✓ Branch 5 taken 126 times.
✓ Branch 6 taken 10330 times.
✓ Branch 7 taken 169 times.
|
11103 | if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule>=0) { |
430 | int seg, d; | ||
431 | 10330 | uint8_t *last_pkt = os->buf + os->pstart; | |
432 | 10330 | uint8_t *next_pkt = last_pkt; | |
433 | |||
434 | 10330 | av_vorbis_parse_reset(priv->vp); | |
435 | 10330 | duration = 0; | |
436 | 10330 | seg = os->segp; | |
437 | 10330 | d = av_vorbis_parse_frame_flags(priv->vp, last_pkt, 1, &flags); | |
438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10330 times.
|
10330 | if (d < 0) { |
439 | ✗ | os->pflags |= AV_PKT_FLAG_CORRUPT; | |
440 | ✗ | return 0; | |
441 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10330 times.
|
10330 | } else if (flags & VORBIS_FLAG_COMMENT) { |
442 | ✗ | vorbis_update_metadata(s, idx); | |
443 | ✗ | flags = 0; | |
444 | } | ||
445 | 10330 | duration += d; | |
446 | 10330 | last_pkt = next_pkt = next_pkt + os->psize; | |
447 |
2/2✓ Branch 0 taken 192724 times.
✓ Branch 1 taken 10330 times.
|
203054 | for (; seg < os->nsegs; seg++) { |
448 |
2/2✓ Branch 0 taken 143342 times.
✓ Branch 1 taken 49382 times.
|
192724 | if (os->segments[seg] < 255) { |
449 | 143342 | int d = av_vorbis_parse_frame_flags(priv->vp, last_pkt, 1, &flags); | |
450 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143342 times.
|
143342 | if (d < 0) { |
451 | ✗ | duration = os->granule; | |
452 | ✗ | break; | |
453 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143342 times.
|
143342 | } else if (flags & VORBIS_FLAG_COMMENT) { |
454 | ✗ | vorbis_update_metadata(s, idx); | |
455 | ✗ | flags = 0; | |
456 | } | ||
457 | 143342 | duration += d; | |
458 | 143342 | last_pkt = next_pkt + os->segments[seg]; | |
459 | } | ||
460 | 192724 | next_pkt += os->segments[seg]; | |
461 | } | ||
462 | 10330 | os->lastpts = | |
463 | 10330 | os->lastdts = os->granule - duration; | |
464 | |||
465 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 10330 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
10330 | if (!os->granule && duration) //hack to deal with broken files (Ticket3710) |
466 | ✗ | os->lastpts = os->lastdts = AV_NOPTS_VALUE; | |
467 | |||
468 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 10303 times.
|
10330 | if (s->streams[idx]->start_time == AV_NOPTS_VALUE) { |
469 | 27 | s->streams[idx]->start_time = FFMAX(os->lastpts, 0); | |
470 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (s->streams[idx]->duration != AV_NOPTS_VALUE) |
471 | 27 | s->streams[idx]->duration -= s->streams[idx]->start_time; | |
472 | } | ||
473 | 10330 | priv->final_pts = AV_NOPTS_VALUE; | |
474 | 10330 | av_vorbis_parse_reset(priv->vp); | |
475 | } | ||
476 | |||
477 | /* parse packet duration */ | ||
478 |
1/2✓ Branch 0 taken 11103 times.
✗ Branch 1 not taken.
|
11103 | if (os->psize > 0) { |
479 | 11103 | duration = av_vorbis_parse_frame_flags(priv->vp, os->buf + os->pstart, 1, &flags); | |
480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11103 times.
|
11103 | if (duration < 0) { |
481 | ✗ | os->pflags |= AV_PKT_FLAG_CORRUPT; | |
482 | ✗ | return 0; | |
483 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11103 times.
|
11103 | } else if (flags & VORBIS_FLAG_COMMENT) { |
484 | ✗ | vorbis_update_metadata(s, idx); | |
485 | ✗ | flags = 0; | |
486 | } | ||
487 | 11103 | os->pduration = duration; | |
488 | } | ||
489 | |||
490 | /* final packet handling | ||
491 | * here we save the pts of the first packet in the final page, sum up all | ||
492 | * packet durations in the final page except for the last one, and compare | ||
493 | * to the page granule to find the duration of the final packet */ | ||
494 |
2/2✓ Branch 0 taken 131 times.
✓ Branch 1 taken 10972 times.
|
11103 | if (os->flags & OGG_FLAG_EOS) { |
495 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 126 times.
|
131 | if (os->lastpts != AV_NOPTS_VALUE) { |
496 | 5 | priv->final_pts = os->lastpts; | |
497 | 5 | priv->final_duration = 0; | |
498 | } | ||
499 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 126 times.
|
131 | if (os->segp == os->nsegs) { |
500 | 5 | int64_t skip = priv->final_pts + priv->final_duration + os->pduration - os->granule; | |
501 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (skip > 0) |
502 | 5 | os->end_trimming = skip; | |
503 | 5 | os->pduration = os->granule - priv->final_pts - priv->final_duration; | |
504 | } | ||
505 | 131 | priv->final_duration += os->pduration; | |
506 | } | ||
507 | |||
508 | 11103 | return 0; | |
509 | } | ||
510 | |||
511 | const struct ogg_codec ff_vorbis_codec = { | ||
512 | .magic = "\001vorbis", | ||
513 | .magicsize = 7, | ||
514 | .header = vorbis_header, | ||
515 | .packet = vorbis_packet, | ||
516 | .cleanup = vorbis_cleanup, | ||
517 | .nb_header = 3, | ||
518 | }; | ||
519 |