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 | 194 | static int ogm_chapter(AVFormatContext *as, const uint8_t *key, const uint8_t *val) | |
44 | { | ||
45 | 194 | int i, cnum, h, m, s, ms, keylen = strlen(key); | |
46 | 194 | AVChapter *chapter = NULL; | |
47 | |||
48 |
5/6✓ Branch 0 taken 78 times.
✓ Branch 1 taken 116 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24 times.
|
194 | if (keylen < 9 || av_strncasecmp(key, "CHAPTER", 7) || sscanf(key+7, "%03d", &cnum) != 1) |
49 | 170 | 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 | 43 | int ff_vorbis_stream_comment(AVFormatContext *as, AVStream *st, | |
75 | const uint8_t *buf, int size) | ||
76 | { | ||
77 | 43 | int updates = ff_vorbis_comment(as, &st->metadata, buf, size, 1); | |
78 | |||
79 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 12 times.
|
43 | if (updates > 0) { |
80 | 31 | st->event_flags |= AVSTREAM_EVENT_FLAG_METADATA_UPDATED; | |
81 | } | ||
82 | |||
83 | 43 | 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 | 201 | 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 | 201 | 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 198 times.
|
201 | if (!v) |
100 | 3 | return 0; | |
101 | |||
102 | 198 | tl = v - t; | |
103 | 198 | vl = size - tl - 1; | |
104 | 198 | v++; | |
105 | |||
106 |
2/4✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 198 times.
|
198 | if (!tl || !vl) |
107 | ✗ | return 0; | |
108 | |||
109 | 198 | t[tl] = 0; | |
110 | |||
111 | 198 | backup = v[vl]; | |
112 | 198 | 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 194 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
202 | 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 170 times.
|
194 | } else if (!ogm_chapter(as, t, v)) { |
137 | 170 | (*updates)++; | |
138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 170 times.
|
170 | if (av_dict_get(*m, t, NULL, 0)) |
139 | ✗ | av_dict_set(m, t, ";", AV_DICT_APPEND); | |
140 | 170 | av_dict_set(m, t, v, AV_DICT_APPEND); | |
141 | } | ||
142 | 24 | end: | |
143 | 198 | t[tl] = '='; | |
144 | 198 | v[vl] = backup; | |
145 | |||
146 | 198 | return 0; | |
147 | } | ||
148 | |||
149 | 82 | int ff_vorbis_comment(AVFormatContext *as, AVDictionary **m, | |
150 | const uint8_t *buf, int size, | ||
151 | int parse_picture) | ||
152 | { | ||
153 | 82 | const uint8_t *p = buf; | |
154 | 82 | const uint8_t *end = buf + size; | |
155 | 82 | 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 82 times.
|
82 | if (size < 8) |
161 | ✗ | return AVERROR_INVALIDDATA; | |
162 | |||
163 | 82 | s = bytestream_get_le32(&p); | |
164 | |||
165 |
2/4✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 82 times.
|
82 | if (end - p - 4 < s || s < 0) |
166 | ✗ | return AVERROR_INVALIDDATA; | |
167 | |||
168 | 82 | p += s; | |
169 | |||
170 | 82 | n = bytestream_get_le32(&p); | |
171 | |||
172 |
4/4✓ Branch 0 taken 205 times.
✓ Branch 1 taken 78 times.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 4 times.
|
283 | while (end - p >= 4 && n > 0) { |
173 | 201 | s = bytestream_get_le32(&p); | |
174 | |||
175 |
2/4✓ Branch 0 taken 201 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 201 times.
✗ Branch 3 not taken.
|
201 | if (end - p < s || s < 0) |
176 | break; | ||
177 | |||
178 | 201 | ret = vorbis_parse_single_comment(as, m, p, s, &updates, parse_picture); | |
179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
|
201 | if (ret < 0) |
180 | ✗ | return ret; | |
181 | 201 | p += s; | |
182 | 201 | n--; | |
183 | } | ||
184 | |||
185 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 78 times.
|
82 | 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 82 times.
|
82 | if (n > 0) |
189 | ✗ | av_log(as, AV_LOG_INFO, | |
190 | "truncated comment header, %i comments not found\n", n); | ||
191 | |||
192 | 82 | ff_metadata_conv(m, NULL, ff_vorbiscomment_metadata_conv); | |
193 | |||
194 | 82 | 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 | 28 | 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 | 28 | len = priv->len[0] + priv->len[1] + priv->len[2]; | |
229 | 28 | buf_len = len + len / 255 + 64; | |
230 | |||
231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (*buf) |
232 | ✗ | return AVERROR_INVALIDDATA; | |
233 | |||
234 | 28 | ptr = *buf = av_realloc(NULL, buf_len); | |
235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!ptr) |
236 | ✗ | return AVERROR(ENOMEM); | |
237 | 28 | memset(*buf, '\0', buf_len); | |
238 | |||
239 | 28 | ptr[0] = 2; | |
240 | 28 | offset = 1; | |
241 | 28 | offset += av_xiphlacing(&ptr[offset], priv->len[0]); | |
242 | 28 | offset += av_xiphlacing(&ptr[offset], priv->len[1]); | |
243 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 28 times.
|
112 | for (i = 0; i < 3; i++) { |
244 | 84 | memcpy(&ptr[offset], priv->packet[i], priv->len[i]); | |
245 | 84 | offset += priv->len[i]; | |
246 | 84 | av_freep(&priv->packet[i]); | |
247 | } | ||
248 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if ((err = av_reallocp(buf, offset + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) |
249 | ✗ | return err; | |
250 | 28 | return offset; | |
251 | } | ||
252 | |||
253 | 28 | static void vorbis_cleanup(AVFormatContext *s, int idx) | |
254 | { | ||
255 | 28 | struct ogg *ogg = s->priv_data; | |
256 | 28 | struct ogg_stream *os = ogg->streams + idx; | |
257 | 28 | struct oggvorbis_private *priv = os->private; | |
258 | int i; | ||
259 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (os->private) { |
260 | 28 | av_vorbis_parse_free(&priv->vp); | |
261 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 28 times.
|
112 | for (i = 0; i < 3; i++) |
262 | 84 | av_freep(&priv->packet[i]); | |
263 | } | ||
264 | 28 | } | |
265 | |||
266 | 30 | static int vorbis_update_metadata(AVFormatContext *s, int idx) | |
267 | { | ||
268 | 30 | struct ogg *ogg = s->priv_data; | |
269 | 30 | struct ogg_stream *os = ogg->streams + idx; | |
270 | 30 | AVStream *st = s->streams[idx]; | |
271 | int ret; | ||
272 | |||
273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (os->psize <= 8) |
274 | ✗ | return 0; | |
275 | |||
276 | /* New metadata packet; release old data. */ | ||
277 | 30 | av_dict_free(&st->metadata); | |
278 | 30 | ret = ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 7, | |
279 | 30 | os->psize - 8); | |
280 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (ret < 0) |
281 | ✗ | return ret; | |
282 | |||
283 | /* Update the metadata if possible. */ | ||
284 | 30 | av_freep(&os->new_metadata); | |
285 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | if (st->metadata) { |
286 | 20 | 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 | 30 | return ret; | |
294 | } | ||
295 | |||
296 | 28 | static int vorbis_parse_header(AVFormatContext *s, AVStream *st, | |
297 | const uint8_t *p, unsigned int psize) | ||
298 | { | ||
299 | unsigned blocksize, bs0, bs1; | ||
300 | int srate; | ||
301 | int channels; | ||
302 | |||
303 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (psize != 30) |
304 | ✗ | return AVERROR_INVALIDDATA; | |
305 | |||
306 | 28 | p += 7; /* skip "\001vorbis" tag */ | |
307 | |||
308 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if (bytestream_get_le32(&p) != 0) /* vorbis_version */ |
309 | ✗ | return AVERROR_INVALIDDATA; | |
310 | |||
311 | 28 | channels = bytestream_get_byte(&p); | |
312 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (st->codecpar->ch_layout.nb_channels && |
313 | ✗ | channels != st->codecpar->ch_layout.nb_channels) { | |
314 | ✗ | av_log(s, AV_LOG_ERROR, "Channel change is not supported\n"); | |
315 | ✗ | return AVERROR_PATCHWELCOME; | |
316 | } | ||
317 | 28 | st->codecpar->ch_layout.nb_channels = channels; | |
318 | 28 | srate = bytestream_get_le32(&p); | |
319 | 28 | p += 4; // skip maximum bitrate | |
320 | 28 | st->codecpar->bit_rate = bytestream_get_le32(&p); // nominal bitrate | |
321 | 28 | p += 4; // skip minimum bitrate | |
322 | |||
323 | 28 | blocksize = bytestream_get_byte(&p); | |
324 | 28 | bs0 = blocksize & 15; | |
325 | 28 | bs1 = blocksize >> 4; | |
326 | |||
327 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (bs0 > bs1) |
328 | ✗ | return AVERROR_INVALIDDATA; | |
329 |
2/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
28 | if (bs0 < 6 || bs1 > 13) |
330 | ✗ | return AVERROR_INVALIDDATA; | |
331 | |||
332 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
|
28 | if (bytestream_get_byte(&p) != 1) /* framing_flag */ |
333 | ✗ | return AVERROR_INVALIDDATA; | |
334 | |||
335 | 28 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
336 | 28 | st->codecpar->codec_id = AV_CODEC_ID_VORBIS; | |
337 | |||
338 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (srate > 0) { |
339 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (st->codecpar->sample_rate && |
340 | ✗ | srate != st->codecpar->sample_rate) { | |
341 | ✗ | av_log(s, AV_LOG_ERROR, "Sample rate change is not supported\n"); | |
342 | ✗ | return AVERROR_PATCHWELCOME; | |
343 | } | ||
344 | |||
345 | 28 | st->codecpar->sample_rate = srate; | |
346 | 28 | avpriv_set_pts_info(st, 64, 1, srate); | |
347 | } | ||
348 | |||
349 | 28 | return 1; | |
350 | } | ||
351 | |||
352 | 113 | static int vorbis_header(AVFormatContext *s, int idx) | |
353 | { | ||
354 | 113 | struct ogg *ogg = s->priv_data; | |
355 | 113 | AVStream *st = s->streams[idx]; | |
356 | 113 | struct ogg_stream *os = ogg->streams + idx; | |
357 | struct oggvorbis_private *priv; | ||
358 | 113 | int pkt_type = os->buf[os->pstart]; | |
359 | |||
360 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 85 times.
|
113 | if (!os->private) { |
361 | 28 | os->private = av_mallocz(sizeof(struct oggvorbis_private)); | |
362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!os->private) |
363 | ✗ | return AVERROR(ENOMEM); | |
364 | } | ||
365 | |||
366 | 113 | priv = os->private; | |
367 | |||
368 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 84 times.
|
113 | if (!(pkt_type & 1)) |
369 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | return priv->vp ? 0 : AVERROR_INVALIDDATA; |
370 | |||
371 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (pkt_type > 5) { |
372 | ✗ | av_log(s, AV_LOG_VERBOSE, "Ignoring packet with unknown type %d\n", pkt_type); | |
373 | ✗ | return 1; | |
374 | } | ||
375 | |||
376 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (os->psize < 1) |
377 | ✗ | return AVERROR_INVALIDDATA; | |
378 | |||
379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (priv->packet[pkt_type >> 1]) |
380 | ✗ | return AVERROR_INVALIDDATA; | |
381 |
6/8✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 56 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
|
84 | if (pkt_type > 1 && !priv->packet[0] || pkt_type > 3 && !priv->packet[1]) |
382 | ✗ | return priv->vp ? 0 : AVERROR_INVALIDDATA; | |
383 | |||
384 | 84 | priv->len[pkt_type >> 1] = os->psize; | |
385 | 84 | priv->packet[pkt_type >> 1] = av_memdup(os->buf + os->pstart, os->psize); | |
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
|
84 | if (!priv->packet[pkt_type >> 1]) |
387 | ✗ | return AVERROR(ENOMEM); | |
388 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 56 times.
|
84 | if (pkt_type == 1) |
389 | 28 | return vorbis_parse_header(s, st, os->buf + os->pstart, os->psize); | |
390 | |||
391 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | if (pkt_type == 3) { |
392 |
2/4✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
28 | if (vorbis_update_metadata(s, idx) >= 0 && priv->len[1] > 10) { |
393 | unsigned new_len; | ||
394 | |||
395 | 28 | int ret = ff_replaygain_export(st, st->metadata); | |
396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (ret < 0) |
397 | ✗ | return ret; | |
398 | |||
399 | // drop all metadata we parsed and which is not required by libvorbis | ||
400 | 28 | new_len = 7 + 4 + AV_RL32(priv->packet[1] + 7) + 4 + 1; | |
401 |
3/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 8 times.
|
28 | if (new_len >= 16 && new_len < os->psize) { |
402 | 20 | AV_WL32(priv->packet[1] + new_len - 5, 0); | |
403 | 20 | priv->packet[1][new_len - 1] = 1; | |
404 | 20 | priv->len[1] = new_len; | |
405 | } | ||
406 | } | ||
407 | } else { | ||
408 | int ret; | ||
409 | |||
410 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (priv->vp) |
411 | ✗ | return AVERROR_INVALIDDATA; | |
412 | |||
413 | 28 | ret = fixup_vorbis_headers(s, priv, &st->codecpar->extradata); | |
414 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (ret < 0) { |
415 | ✗ | st->codecpar->extradata_size = 0; | |
416 | ✗ | return ret; | |
417 | } | ||
418 | 28 | st->codecpar->extradata_size = ret; | |
419 | |||
420 | 28 | priv->vp = av_vorbis_parse_init(st->codecpar->extradata, st->codecpar->extradata_size); | |
421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!priv->vp) { |
422 | ✗ | av_freep(&st->codecpar->extradata); | |
423 | ✗ | st->codecpar->extradata_size = 0; | |
424 | ✗ | return AVERROR_UNKNOWN; | |
425 | } | ||
426 | } | ||
427 | |||
428 | 56 | return 1; | |
429 | } | ||
430 | |||
431 | 11113 | static int vorbis_packet(AVFormatContext *s, int idx) | |
432 | { | ||
433 | 11113 | struct ogg *ogg = s->priv_data; | |
434 | 11113 | struct ogg_stream *os = ogg->streams + idx; | |
435 | 11113 | struct oggvorbis_private *priv = os->private; | |
436 | 11113 | int duration, flags = 0; | |
437 | |||
438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11113 times.
|
11113 | if (!priv->vp) |
439 | ✗ | return AVERROR_INVALIDDATA; | |
440 | |||
441 | /* first packet handling | ||
442 | * here we parse the duration of each packet in the first page and compare | ||
443 | * the total duration to the page granule to find the encoder delay and | ||
444 | * set the first timestamp */ | ||
445 |
8/8✓ Branch 0 taken 11054 times.
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 10576 times.
✓ Branch 3 taken 478 times.
✓ Branch 4 taken 10502 times.
✓ Branch 5 taken 133 times.
✓ Branch 6 taken 10333 times.
✓ Branch 7 taken 169 times.
|
11113 | if ((!os->lastpts || os->lastpts == AV_NOPTS_VALUE) && !(os->flags & OGG_FLAG_EOS) && (int64_t)os->granule>=0) { |
446 | int seg, d; | ||
447 | 10333 | uint8_t *last_pkt = os->buf + os->pstart; | |
448 | 10333 | uint8_t *next_pkt = last_pkt; | |
449 | |||
450 | 10333 | av_vorbis_parse_reset(priv->vp); | |
451 | 10333 | duration = 0; | |
452 | 10333 | seg = os->segp; | |
453 | 10333 | d = av_vorbis_parse_frame_flags(priv->vp, last_pkt, 1, &flags); | |
454 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10333 times.
|
10333 | if (d < 0) { |
455 | ✗ | os->pflags |= AV_PKT_FLAG_CORRUPT; | |
456 | ✗ | return 0; | |
457 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10332 times.
|
10333 | } else if (flags & VORBIS_FLAG_COMMENT) { |
458 | 1 | vorbis_update_metadata(s, idx); | |
459 | 1 | flags = 0; | |
460 | } | ||
461 | 10333 | duration += d; | |
462 | 10333 | last_pkt = next_pkt = next_pkt + os->psize; | |
463 |
2/2✓ Branch 0 taken 192737 times.
✓ Branch 1 taken 10333 times.
|
203070 | for (; seg < os->nsegs; seg++) { |
464 |
2/2✓ Branch 0 taken 143343 times.
✓ Branch 1 taken 49394 times.
|
192737 | if (os->segments[seg] < 255) { |
465 | 143343 | int d = av_vorbis_parse_frame_flags(priv->vp, last_pkt, 1, &flags); | |
466 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143343 times.
|
143343 | if (d < 0) { |
467 | ✗ | duration = os->granule; | |
468 | ✗ | break; | |
469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143343 times.
|
143343 | } else if (flags & VORBIS_FLAG_COMMENT) { |
470 | ✗ | vorbis_update_metadata(s, idx); | |
471 | ✗ | flags = 0; | |
472 | } | ||
473 | 143343 | duration += d; | |
474 | 143343 | last_pkt = next_pkt + os->segments[seg]; | |
475 | } | ||
476 | 192737 | next_pkt += os->segments[seg]; | |
477 | } | ||
478 | 10333 | os->lastpts = | |
479 | 10333 | os->lastdts = os->granule - duration; | |
480 | |||
481 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10330 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
10333 | if (!os->granule && duration) //hack to deal with broken files (Ticket3710) |
482 | ✗ | os->lastpts = os->lastdts = AV_NOPTS_VALUE; | |
483 | |||
484 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 10306 times.
|
10333 | if (s->streams[idx]->start_time == AV_NOPTS_VALUE) { |
485 | 27 | s->streams[idx]->start_time = FFMAX(os->lastpts, 0); | |
486 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if (s->streams[idx]->duration != AV_NOPTS_VALUE) |
487 | 27 | s->streams[idx]->duration -= s->streams[idx]->start_time; | |
488 | } | ||
489 | 10333 | priv->final_pts = AV_NOPTS_VALUE; | |
490 | 10333 | av_vorbis_parse_reset(priv->vp); | |
491 | } | ||
492 | |||
493 | /* parse packet duration */ | ||
494 |
1/2✓ Branch 0 taken 11113 times.
✗ Branch 1 not taken.
|
11113 | if (os->psize > 0) { |
495 | 11113 | duration = av_vorbis_parse_frame_flags(priv->vp, os->buf + os->pstart, 1, &flags); | |
496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11113 times.
|
11113 | if (duration < 0) { |
497 | ✗ | os->pflags |= AV_PKT_FLAG_CORRUPT; | |
498 | ✗ | return 0; | |
499 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11112 times.
|
11113 | } else if (flags & VORBIS_FLAG_COMMENT) { |
500 | 1 | vorbis_update_metadata(s, idx); | |
501 | 1 | flags = 0; | |
502 | } | ||
503 | 11113 | os->pduration = duration; | |
504 | } | ||
505 | |||
506 | /* final packet handling | ||
507 | * here we save the pts of the first packet in the final page, sum up all | ||
508 | * packet durations in the final page except for the last one, and compare | ||
509 | * to the page granule to find the duration of the final packet */ | ||
510 |
2/2✓ Branch 0 taken 138 times.
✓ Branch 1 taken 10975 times.
|
11113 | if (os->flags & OGG_FLAG_EOS) { |
511 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 130 times.
|
138 | if (os->lastpts != AV_NOPTS_VALUE) { |
512 | 8 | priv->final_pts = os->lastpts; | |
513 | 8 | priv->final_duration = 0; | |
514 | } | ||
515 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 131 times.
|
138 | if (os->segp == os->nsegs) { |
516 | 7 | int64_t skip = priv->final_pts + priv->final_duration + os->pduration - os->granule; | |
517 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (skip > 0) |
518 | 7 | os->end_trimming = skip; | |
519 | 7 | os->pduration = os->granule - priv->final_pts - priv->final_duration; | |
520 | } | ||
521 | 138 | priv->final_duration += os->pduration; | |
522 | } | ||
523 | |||
524 | 11113 | return 0; | |
525 | } | ||
526 | |||
527 | const struct ogg_codec ff_vorbis_codec = { | ||
528 | .magic = "\001vorbis", | ||
529 | .magicsize = 7, | ||
530 | .header = vorbis_header, | ||
531 | .packet = vorbis_packet, | ||
532 | .cleanup = vorbis_cleanup, | ||
533 | .nb_header = 3, | ||
534 | }; | ||
535 |