../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
3 / 457
0.7%
Branches:
1 / 220
0.5%
Functions:
1 / 16
6.2%

libavformat/vividas.c
Line Branch Exec Source
1 /*
2 * Vividas VIV format Demuxer
3 * Copyright (c) 2012 Krzysztof Klinikowski
4 * Copyright (c) 2010 Andrzej Szombierski
5 * based on vivparse Copyright (c) 2007 Måns Rullgård
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * @brief Vividas VIV (.viv) file demuxer
27 * @author Andrzej Szombierski [qq at kuku eu org] (2010-07)
28 * @sa http://wiki.multimedia.cx/index.php?title=Vividas_VIV
29 */
30
31 #include "libavutil/avassert.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem.h"
34 #include "avio_internal.h"
35 #include "avformat.h"
36 #include "demux.h"
37 #include "internal.h"
38
39 #define MAX_AUDIO_SUBPACKETS 100
40
41 typedef struct VIV_SB_block {
42 int size, n_packets;
43 int64_t byte_offset;
44 int64_t packet_offset;
45 } VIV_SB_block;
46
47 typedef struct VIV_SB_entry {
48 int size, flag;
49 } VIV_SB_entry;
50
51 typedef struct VIV_AudioSubpacket {
52 int start, pcm_bytes;
53 } VIV_AudioSubpacket;
54
55 typedef struct VividasDemuxContext {
56 int n_sb_blocks;
57 VIV_SB_block *sb_blocks;
58 int num_audio;
59
60 uint32_t sb_key;
61 int64_t sb_offset;
62
63 int current_sb, current_sb_entry;
64 uint8_t *sb_buf;
65 AVIOContext *sb_pb;
66 int n_sb_entries;
67 VIV_SB_entry *sb_entries;
68
69 int n_audio_subpackets;
70 int current_audio_subpacket;
71
72 int64_t audio_sample;
73
74 VIV_AudioSubpacket audio_subpackets[MAX_AUDIO_SUBPACKETS];
75 } VividasDemuxContext;
76
77 8496 static int viv_probe(const AVProbeData *p)
78 {
79
1/2
✓ Branch 2 → 3 taken 8496 times.
✗ Branch 2 → 4 not taken.
8496 if (memcmp(p->buf, "vividas03", 9))
80 8496 return 0;
81
82 ✗ return AVPROBE_SCORE_MAX;
83 }
84
85 static const uint8_t keybits[32] = {
86 20, 52, 111, 10, 27, 71, 142, 53,
87 82, 138, 1, 78, 86, 121, 183, 85,
88 105, 152, 39, 140, 172, 11, 64, 144,
89 155, 6, 71, 163, 186, 49, 126, 43,
90 };
91
92 ✗ static uint32_t decode_key(uint8_t *buf)
93 {
94 ✗ uint32_t key = 0;
95
96
0/2
✗ Branch 4 → 3 not taken.
✗ Branch 4 → 5 not taken.
✗ for (int i = 0; i < 32; i++) {
97 ✗ unsigned p = keybits[i];
98 ✗ key |= ((buf[p] >> ((i*5+3)&7)) & 1u) << i;
99 }
100
101 ✗ return key;
102 }
103
104 ✗ static void put_v(uint8_t *p, unsigned v)
105 {
106
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (v>>28)
107 ✗ *p++ = ((v>>28)&0x7f)|0x80;
108
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (v>>21)
109 ✗ *p++ = ((v>>21)&0x7f)|0x80;
110
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (v>>14)
111 ✗ *p++ = ((v>>14)&0x7f)|0x80;
112
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (v>>7)
113 ✗ *p++ = ((v>>7)&0x7f)|0x80;
114 ✗ }
115
116 ✗ static unsigned recover_key(unsigned char sample[4], unsigned expected_size)
117 {
118 ✗ unsigned char plaintext[8] = { 'S', 'B' };
119
120 ✗ put_v(plaintext+2, expected_size);
121
122 ✗ return AV_RL32(sample) ^ AV_RL32(plaintext);
123 }
124
125 ✗ static void xor_block(void *p1, void *p2, unsigned size, int key, unsigned *key_ptr)
126 {
127 ✗ unsigned *d1 = p1;
128 ✗ unsigned *d2 = p2;
129 ✗ unsigned k = *key_ptr;
130
131 ✗ size >>= 2;
132
133
0/2
✗ Branch 4 → 3 not taken.
✗ Branch 4 → 5 not taken.
✗ while (size > 0) {
134 ✗ AV_WN32(d2, AV_RN32(d1) ^ (HAVE_BIGENDIAN ? av_bswap32(k) : k));
135 ✗ k += key;
136 ✗ d1++;
137 ✗ d2++;
138 ✗ size--;
139 }
140
141 ✗ *key_ptr = k;
142 ✗ }
143
144 ✗ static void decode_block(uint8_t *src, uint8_t *dest, unsigned size,
145 uint32_t key, uint32_t *key_ptr,
146 int align)
147 {
148 ✗ unsigned s = size;
149 char tmp[4];
150 int a2;
151
152
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (!size)
153 ✗ return;
154
155 ✗ align &= 3;
156 ✗ a2 = (4 - align) & 3;
157
158
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 9 not taken.
✗ if (align) {
159 ✗ uint32_t tmpkey = *key_ptr - key;
160
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (a2 > s) {
161 ✗ a2 = s;
162 ✗ avpriv_request_sample(NULL, "tiny aligned block");
163 }
164 ✗ memcpy(tmp + align, src, a2);
165 ✗ xor_block(tmp, tmp, 4, key, &tmpkey);
166 ✗ memcpy(dest, tmp + align, a2);
167 ✗ s -= a2;
168 }
169
170
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 12 not taken.
✗ if (s >= 4) {
171 ✗ xor_block(src + a2, dest + a2, s & ~3,
172 key, key_ptr);
173 ✗ s &= 3;
174 }
175
176
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 15 not taken.
✗ if (s) {
177 ✗ size -= s;
178 ✗ memcpy(tmp, src + size, s);
179 ✗ xor_block(&tmp, &tmp, 4, key, key_ptr);
180 ✗ memcpy(dest + size, tmp, s);
181 }
182 }
183
184 ✗ static uint32_t get_v(uint8_t *p, int len)
185 {
186 ✗ uint32_t v = 0;
187 ✗ const uint8_t *end = p + len;
188
189 do {
190
0/4
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (p >= end || v >= UINT_MAX / 128 - *p)
191 ✗ return v;
192 ✗ v <<= 7;
193 ✗ v += *p & 0x7f;
194
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ } while (*p++ & 0x80);
195
196 ✗ return v;
197 }
198
199 ✗ static uint8_t *read_vblock(AVIOContext *src, uint32_t *size,
200 uint32_t key, uint32_t *k2, int align)
201 {
202 uint8_t tmp[4];
203 uint8_t *buf;
204 unsigned n;
205
206
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (avio_read(src, tmp, 4) != 4)
207 ✗ return NULL;
208
209 ✗ decode_block(tmp, tmp, 4, key, k2, align);
210
211 ✗ n = get_v(tmp, 4);
212
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if (n < 4)
213 ✗ return NULL;
214
215 ✗ buf = av_malloc(n);
216
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (!buf)
217 ✗ return NULL;
218
219 ✗ *size = n;
220 ✗ n -= 4;
221
222 ✗ memcpy(buf, tmp, 4);
223
224
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (avio_read(src, buf + 4, n) == n) {
225 ✗ decode_block(buf + 4, buf + 4, n, key, k2, align);
226 } else {
227 ✗ av_free(buf);
228 ✗ buf = NULL;
229 }
230
231 ✗ return buf;
232 }
233
234 ✗ static uint8_t *read_sb_block(AVIOContext *src, unsigned *size,
235 uint32_t *key, unsigned expected_size)
236 {
237 uint8_t *buf;
238 uint8_t ibuf[8], sbuf[8];
239 uint32_t k2;
240 unsigned n;
241
242
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (avio_read(src, ibuf, 8) < 8)
243 ✗ return NULL;
244
245 ✗ k2 = *key;
246 ✗ decode_block(ibuf, sbuf, 8, *key, &k2, 0);
247
248 ✗ n = get_v(sbuf+2, 6);
249
250
0/8
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 11 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 11 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 19 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 19 not taken.
✗ if (sbuf[0] != 'S' || sbuf[1] != 'B' || (expected_size>0 && n != expected_size)) {
251 ✗ uint32_t tmpkey = recover_key(ibuf, expected_size);
252 ✗ k2 = tmpkey;
253 ✗ decode_block(ibuf, sbuf, 8, tmpkey, &k2, 0);
254 ✗ n = get_v(sbuf+2, 6);
255
0/6
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 17 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ if (sbuf[0] != 'S' || sbuf[1] != 'B' || expected_size != n)
256 ✗ return NULL;
257 ✗ *key = tmpkey;
258 }
259
260
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ if (n < 8)
261 ✗ return NULL;
262
263 ✗ buf = av_malloc(n);
264
0/2
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if (!buf)
265 ✗ return NULL;
266
267 ✗ memcpy(buf, sbuf, 8);
268
269 ✗ *size = n;
270 ✗ n -= 8;
271
272
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 28 not taken.
✗ if (avio_read(src, buf+8, n) != n) {
273 ✗ av_free(buf);
274 ✗ return NULL;
275 }
276
277 ✗ decode_block(buf + 8, buf + 8, n, *key, &k2, 0);
278
279 ✗ return buf;
280 }
281
282 ✗ static int track_header(VividasDemuxContext *viv, AVFormatContext *s,
283 const uint8_t *buf, int size)
284 {
285 int i, j, ret;
286 int64_t off;
287 int val_1;
288 int num_video;
289 FFIOContext pb0;
290 ✗ AVIOContext *const pb = &pb0.pub;
291
292 ✗ ffio_init_read_context(&pb0, buf, size);
293
294 ✗ ffio_read_varlen(pb); // track_header_len
295 ✗ avio_r8(pb); // '1'
296
297 ✗ val_1 = ffio_read_varlen(pb);
298
299
0/2
✗ Branch 20 → 7 not taken.
✗ Branch 20 → 21 not taken.
✗ for (i=0;i<val_1;i++) {
300 ✗ int c = avio_r8(pb);
301
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ if (avio_feof(pb))
302 ✗ return AVERROR_EOF;
303
0/2
✗ Branch 18 → 12 not taken.
✗ Branch 18 → 19 not taken.
✗ for (j=0;j<c;j++) {
304
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (avio_feof(pb))
305 ✗ return AVERROR_EOF;
306 ✗ avio_r8(pb); // val_3
307 ✗ avio_r8(pb); // val_4
308 }
309 }
310
311 ✗ avio_r8(pb); // num_streams
312
313 ✗ off = avio_tell(pb);
314 ✗ off += ffio_read_varlen(pb); // val_5
315
316 ✗ avio_r8(pb); // '2'
317 ✗ num_video = avio_r8(pb);
318
319 ✗ avio_seek(pb, off, SEEK_SET);
320
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 30 not taken.
✗ if (num_video != 1) {
321 ✗ av_log(s, AV_LOG_ERROR, "number of video tracks %d is not 1\n", num_video);
322 ✗ return AVERROR_PATCHWELCOME;
323 }
324
325
0/2
✗ Branch 48 → 31 not taken.
✗ Branch 48 → 49 not taken.
✗ for (i = 0; i < num_video; i++) {
326 ✗ AVStream *st = avformat_new_stream(s, NULL);
327 int num, den;
328
329
0/2
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 34 not taken.
✗ if (!st)
330 ✗ return AVERROR(ENOMEM);
331
332 ✗ st->id = i;
333
334 ✗ st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
335 ✗ st->codecpar->codec_id = AV_CODEC_ID_VP6;
336
337 ✗ off = avio_tell(pb);
338 ✗ off += ffio_read_varlen(pb);
339 ✗ avio_r8(pb); // '3'
340 ✗ avio_r8(pb); // val_7
341 ✗ num = avio_rl32(pb); // frame_time
342 ✗ den = avio_rl32(pb); // time_base
343 ✗ avpriv_set_pts_info(st, 64, num, den);
344 ✗ st->nb_frames = avio_rl32(pb); // n frames
345 ✗ st->codecpar->width = avio_rl16(pb); // width
346 ✗ st->codecpar->height = avio_rl16(pb); // height
347 ✗ avio_r8(pb); // val_8
348 ✗ avio_rl32(pb); // val_9
349
350 ✗ avio_seek(pb, off, SEEK_SET);
351 }
352
353 ✗ off = avio_tell(pb);
354 ✗ off += ffio_read_varlen(pb); // val_10
355 ✗ avio_r8(pb); // '4'
356 ✗ viv->num_audio = avio_r8(pb);
357 ✗ avio_seek(pb, off, SEEK_SET);
358
359
0/2
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 56 not taken.
✗ if (viv->num_audio != 1)
360 ✗ av_log(s, AV_LOG_WARNING, "number of audio tracks %d is not 1\n", viv->num_audio);
361
362
0/2
✗ Branch 112 → 57 not taken.
✗ Branch 112 → 113 not taken.
✗ for(i=0;i<viv->num_audio;i++) {
363 int q;
364 ✗ AVStream *st = avformat_new_stream(s, NULL);
365
0/2
✗ Branch 58 → 59 not taken.
✗ Branch 58 → 60 not taken.
✗ if (!st)
366 ✗ return AVERROR(ENOMEM);
367
368 ✗ st->id = num_video + i;
369
370 ✗ st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
371 ✗ st->codecpar->codec_id = AV_CODEC_ID_VORBIS;
372
373 ✗ off = avio_tell(pb);
374 ✗ off += ffio_read_varlen(pb); // length
375 ✗ avio_r8(pb); // '5'
376 ✗ avio_r8(pb); //codec_id
377 ✗ avio_rl16(pb); //codec_subid
378 ✗ st->codecpar->ch_layout.nb_channels = avio_rl16(pb); // channels
379 ✗ st->codecpar->sample_rate = avio_rl32(pb); // sample_rate
380
0/4
✗ Branch 67 → 68 not taken.
✗ Branch 67 → 69 not taken.
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 70 not taken.
✗ if (st->codecpar->sample_rate <= 0 || st->codecpar->ch_layout.nb_channels <= 0)
381 ✗ return AVERROR_INVALIDDATA;
382 ✗ avio_seek(pb, 10, SEEK_CUR); // data_1
383 ✗ q = avio_r8(pb);
384 ✗ avio_seek(pb, q, SEEK_CUR); // data_2
385 ✗ avio_r8(pb); // zeropad
386
387
0/2
✗ Branch 75 → 76 not taken.
✗ Branch 75 → 111 not taken.
✗ if (avio_tell(pb) < off) {
388 int num_data;
389 ✗ int xd_size = 1;
390 int data_len[256];
391 ✗ int offset = 1;
392 uint8_t *p;
393 ✗ ffio_read_varlen(pb); // val_13
394 ✗ avio_r8(pb); // '19'
395 ✗ ffio_read_varlen(pb); // len_3
396 ✗ num_data = avio_r8(pb);
397
0/2
✗ Branch 86 → 81 not taken.
✗ Branch 86 → 87 not taken.
✗ for (j = 0; j < num_data; j++) {
398 ✗ int64_t len = ffio_read_varlen(pb);
399
0/4
✗ Branch 82 → 83 not taken.
✗ Branch 82 → 84 not taken.
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 85 not taken.
✗ if (len < 0 || len > INT_MAX/2 - xd_size) {
400 ✗ return AVERROR_INVALIDDATA;
401 }
402 ✗ data_len[j] = len;
403 ✗ xd_size += len + 1 + len/255;
404 }
405
406 ✗ ret = ff_alloc_extradata(st->codecpar, xd_size);
407
0/2
✗ Branch 88 → 89 not taken.
✗ Branch 88 → 90 not taken.
✗ if (ret < 0)
408 ✗ return ret;
409
410 ✗ p = st->codecpar->extradata;
411 ✗ p[0] = 2;
412
413
0/2
✗ Branch 96 → 91 not taken.
✗ Branch 96 → 97 not taken.
✗ for (j = 0; j < num_data - 1; j++) {
414 ✗ unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
415
0/2
✗ Branch 92 → 93 not taken.
✗ Branch 92 → 95 not taken.
✗ av_assert0(delta <= xd_size - offset);
416 ✗ offset += delta;
417 }
418
419
0/2
✗ Branch 106 → 98 not taken.
✗ Branch 106 → 107 not taken.
✗ for (j = 0; j < num_data; j++) {
420 ✗ ret = avio_read(pb, &p[offset], data_len[j]);
421
0/2
✗ Branch 99 → 100 not taken.
✗ Branch 99 → 102 not taken.
✗ if (ret < data_len[j]) {
422 ✗ st->codecpar->extradata_size = 0;
423 ✗ av_freep(&st->codecpar->extradata);
424 ✗ break;
425 }
426
0/2
✗ Branch 102 → 103 not taken.
✗ Branch 102 → 105 not taken.
✗ av_assert0(data_len[j] <= xd_size - offset);
427 ✗ offset += data_len[j];
428 }
429
430
0/2
✗ Branch 107 → 108 not taken.
✗ Branch 107 → 109 not taken.
✗ if (offset < st->codecpar->extradata_size)
431 ✗ st->codecpar->extradata_size = offset;
432 }
433 }
434
435 ✗ return 0;
436 }
437
438 ✗ static int track_index(VividasDemuxContext *viv, AVFormatContext *s,
439 const uint8_t *buf, unsigned size)
440 {
441 int64_t off;
442 int64_t poff;
443 FFIOContext pb0;
444 ✗ AVIOContext *const pb = &pb0.pub;
445 int i;
446 ✗ int64_t filesize = avio_size(s->pb);
447 uint64_t n_sb_blocks_tmp;
448
449 ✗ ffio_init_read_context(&pb0, buf, size);
450
451 ✗ ffio_read_varlen(pb); // track_index_len
452 ✗ avio_r8(pb); // 'c'
453 ✗ n_sb_blocks_tmp = ffio_read_varlen(pb);
454
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if (n_sb_blocks_tmp > size / 2)
455 ✗ return AVERROR_INVALIDDATA;
456 ✗ viv->sb_blocks = av_calloc(n_sb_blocks_tmp, sizeof(*viv->sb_blocks));
457
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (!viv->sb_blocks) {
458 ✗ return AVERROR(ENOMEM);
459 }
460 ✗ viv->n_sb_blocks = n_sb_blocks_tmp;
461
462 ✗ off = 0;
463 ✗ poff = 0;
464
465
0/2
✗ Branch 19 → 13 not taken.
✗ Branch 19 → 20 not taken.
✗ for (i = 0; i < viv->n_sb_blocks; i++) {
466 ✗ uint64_t size_tmp = ffio_read_varlen(pb);
467 ✗ uint64_t n_packets_tmp = ffio_read_varlen(pb);
468
469
0/4
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ if (size_tmp > INT_MAX || n_packets_tmp > INT_MAX)
470 ✗ return AVERROR_INVALIDDATA;
471
472 ✗ viv->sb_blocks[i].byte_offset = off;
473 ✗ viv->sb_blocks[i].packet_offset = poff;
474
475 ✗ viv->sb_blocks[i].size = size_tmp;
476 ✗ viv->sb_blocks[i].n_packets = n_packets_tmp;
477
478 ✗ off += viv->sb_blocks[i].size;
479 ✗ poff += viv->sb_blocks[i].n_packets;
480 }
481
482
0/4
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 23 not taken.
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ if (filesize > 0 && poff > filesize)
483 ✗ return AVERROR_INVALIDDATA;
484
485 ✗ return 0;
486 }
487
488 ✗ static int load_sb_block(AVFormatContext *s, VividasDemuxContext *viv, unsigned expected_size)
489 {
490 ✗ uint32_t size = 0;
491 int i, ret;
492 ✗ AVIOContext *pb = 0;
493
494
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 5 not taken.
✗ if (viv->sb_pb) {
495 ✗ av_free(viv->sb_pb);
496 ✗ viv->sb_pb = NULL;
497 }
498
499
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (viv->sb_buf)
500 ✗ av_free(viv->sb_buf);
501
502 ✗ viv->sb_buf = read_sb_block(s->pb, &size, &viv->sb_key, expected_size);
503
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (!viv->sb_buf) {
504 ✗ return AVERROR_INVALIDDATA;
505 }
506
507 ✗ pb = avio_alloc_context(viv->sb_buf, size, 0, NULL, NULL, NULL, NULL);
508
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ if (!pb)
509 ✗ return AVERROR(ENOMEM);
510
511 ✗ viv->sb_pb = pb;
512
513 ✗ avio_r8(pb); // 'S'
514 ✗ avio_r8(pb); // 'B'
515 ✗ ffio_read_varlen(pb); // size
516 ✗ avio_r8(pb); // junk
517 ✗ ffio_read_varlen(pb); // first packet
518
519 ✗ viv->n_sb_entries = viv->sb_blocks[viv->current_sb].n_packets;
520
521 // Each entry takes at least two bytes of the block.
522
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ if (viv->n_sb_entries > size / 2) {
523 ✗ viv->n_sb_entries = 0;
524 ✗ return 0;
525 }
526 ✗ ret = av_reallocp_array(&viv->sb_entries, viv->n_sb_entries,
527 sizeof(*viv->sb_entries));
528
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ if (ret < 0) {
529 ✗ viv->n_sb_entries = 0;
530 ✗ return ret;
531 }
532
533
0/2
✗ Branch 27 → 24 not taken.
✗ Branch 27 → 28 not taken.
✗ for (i = 0; i < viv->n_sb_entries; i++) {
534 ✗ viv->sb_entries[i].size = ffio_read_varlen(pb);
535 ✗ viv->sb_entries[i].flag = avio_r8(pb);
536 }
537
538 ✗ ffio_read_varlen(pb);
539 ✗ avio_r8(pb);
540
541 ✗ viv->current_sb_entry = 0;
542
543 ✗ return 0;
544 }
545
546 ✗ static int viv_read_header(AVFormatContext *s)
547 {
548 ✗ VividasDemuxContext *viv = s->priv_data;
549 ✗ AVIOContext *pb = s->pb;
550 int64_t header_end;
551 int num_tracks;
552 uint32_t key, k2;
553 uint32_t v;
554 uint8_t keybuffer[187];
555 ✗ uint32_t b22_size = 0;
556 ✗ uint32_t b22_key = 0;
557 ✗ uint8_t *buf = 0;
558 int ret;
559
560 ✗ avio_skip(pb, 9);
561
562 ✗ header_end = avio_tell(pb);
563
564 ✗ header_end += ffio_read_varlen(pb);
565
566 ✗ num_tracks = avio_r8(pb);
567
568
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 9 not taken.
✗ if (num_tracks != 1) {
569 ✗ av_log(s, AV_LOG_ERROR, "number of tracks %d is not 1\n", num_tracks);
570 ✗ return AVERROR(EINVAL);
571 }
572
573 ✗ v = avio_r8(pb);
574 ✗ avio_seek(pb, v, SEEK_CUR);
575
576 ✗ ret = ffio_read_size(pb, keybuffer, 187);
577
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ if (ret < 0)
578 ✗ return ret;
579 ✗ key = decode_key(keybuffer);
580 ✗ viv->sb_key = key;
581
582 ✗ avio_rl32(pb);
583
584 ✗ for (;;) {
585 ✗ int64_t here = avio_tell(pb);
586 int block_len, block_type;
587
588
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ if (here >= header_end)
589 ✗ break;
590
591 ✗ block_len = ffio_read_varlen(pb);
592
0/4
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if (avio_feof(pb) || block_len <= 0)
593 ✗ return AVERROR_INVALIDDATA;
594
595 ✗ block_type = avio_r8(pb);
596
597
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 31 not taken.
✗ if (block_type == 22) {
598 ✗ ret = ffio_read_size(pb, keybuffer, 187);
599
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if (ret < 0)
600 ✗ return ret;
601 ✗ b22_key = decode_key(keybuffer);
602 ✗ b22_size = avio_rl32(pb);
603 }
604
605 ✗ avio_seek(pb, here + block_len, SEEK_SET);
606 }
607
608
0/2
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 38 not taken.
✗ if (b22_size) {
609 ✗ k2 = b22_key;
610 ✗ buf = read_vblock(pb, &v, b22_key, &k2, 0);
611
0/2
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ if (!buf)
612 ✗ return AVERROR_INVALIDDATA;
613
614 ✗ av_free(buf);
615 }
616
617 ✗ k2 = key;
618 ✗ buf = read_vblock(pb, &v, key, &k2, 0);
619
0/2
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 41 not taken.
✗ if (!buf)
620 ✗ return AVERROR_INVALIDDATA;
621 ✗ ret = track_header(viv, s, buf, v);
622 ✗ av_free(buf);
623
0/2
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
✗ if (ret < 0)
624 ✗ return ret;
625
626 ✗ buf = read_vblock(pb, &v, key, &k2, v);
627
0/2
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
✗ if (!buf)
628 ✗ return AVERROR_INVALIDDATA;
629 ✗ ret = track_index(viv, s, buf, v);
630 ✗ av_free(buf);
631
0/2
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 52 not taken.
✗ if (ret < 0)
632 ✗ return ret;
633
634 ✗ viv->sb_offset = avio_tell(pb);
635
0/2
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 57 not taken.
✗ if (viv->n_sb_blocks > 0) {
636 ✗ viv->current_sb = 0;
637 ✗ ret = load_sb_block(s, viv, viv->sb_blocks[0].size);
638
0/2
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 58 not taken.
✗ if (ret < 0)
639 ✗ return ret;
640 } else {
641 ✗ viv->current_sb = -1;
642 }
643
644 ✗ return 0;
645 }
646
647 ✗ static int viv_read_packet(AVFormatContext *s,
648 AVPacket *pkt)
649 {
650 ✗ VividasDemuxContext *viv = s->priv_data;
651 AVIOContext *pb;
652 int64_t off;
653 int ret;
654
655
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (!viv->sb_pb)
656 ✗ return AVERROR_INVALIDDATA;
657
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (avio_feof(viv->sb_pb))
658 ✗ return AVERROR_EOF;
659
660
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 13 not taken.
✗ if (viv->current_audio_subpacket < viv->n_audio_subpackets) {
661 AVStream *astream;
662 ✗ int size = viv->audio_subpackets[viv->current_audio_subpacket+1].start - viv->audio_subpackets[viv->current_audio_subpacket].start;
663
664 ✗ pb = viv->sb_pb;
665 ✗ ret = av_get_packet(pb, pkt, size);
666
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ if (ret < 0)
667 ✗ return ret;
668 ✗ pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
669
670 ✗ pkt->stream_index = 1;
671 ✗ astream = s->streams[pkt->stream_index];
672
673 ✗ pkt->pts = av_rescale_q(viv->audio_sample, av_make_q(1, astream->codecpar->sample_rate), astream->time_base);
674 ✗ viv->audio_sample += viv->audio_subpackets[viv->current_audio_subpacket].pcm_bytes / 2 /
675 ✗ astream->codecpar->ch_layout.nb_channels;
676 ✗ pkt->flags |= AV_PKT_FLAG_KEY;
677 ✗ viv->current_audio_subpacket++;
678 ✗ return 0;
679 }
680
681
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 20 not taken.
✗ if (viv->current_sb_entry >= viv->n_sb_entries) {
682
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ if (viv->current_sb+1 >= viv->n_sb_blocks)
683 ✗ return AVERROR_INVALIDDATA;
684 ✗ viv->current_sb++;
685
686 ✗ ret = load_sb_block(s, viv, 0);
687
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ if (ret < 0)
688 ✗ return ret;
689 ✗ viv->current_sb_entry = 0;
690 }
691
692 ✗ pb = viv->sb_pb;
693
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ if (!pb)
694 ✗ return AVERROR_INVALIDDATA;
695 ✗ off = avio_tell(pb);
696
697
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (viv->current_sb_entry >= viv->n_sb_entries)
698 ✗ return AVERROR_INVALIDDATA;
699
700 ✗ off += viv->sb_entries[viv->current_sb_entry].size;
701
702
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 50 not taken.
✗ if (viv->sb_entries[viv->current_sb_entry].flag == 0) {
703 ✗ uint64_t v_size = ffio_read_varlen(pb);
704 ✗ int last = 0, last_start;
705
706
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if (!viv->num_audio)
707 ✗ return AVERROR_INVALIDDATA;
708
709 ✗ ffio_read_varlen(pb);
710
0/4
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ if (v_size > INT_MAX || !v_size)
711 ✗ return AVERROR_INVALIDDATA;
712 ✗ ret = av_get_packet(pb, pkt, v_size);
713
0/2
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 36 not taken.
✗ if (ret < 0)
714 ✗ return ret;
715 ✗ pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
716
717 ✗ pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
718 ✗ pkt->flags |= (pkt->data[0]&0x80)?0:AV_PKT_FLAG_KEY;
719 ✗ pkt->stream_index = 0;
720
721
0/2
✗ Branch 45 → 37 not taken.
✗ Branch 45 → 46 not taken.
✗ for (int i = 0; i < MAX_AUDIO_SUBPACKETS - 1; i++) {
722 int start, pcm_bytes;
723 ✗ start = ffio_read_varlen(pb);
724 ✗ pcm_bytes = ffio_read_varlen(pb);
725
726
0/4
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 42 not taken.
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 42 not taken.
✗ if (i > 0 && start == 0)
727 ✗ break;
728
0/2
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ if (start < last)
729 ✗ return AVERROR_INVALIDDATA;
730
731 ✗ viv->n_audio_subpackets = i + 1;
732 ✗ last =
733 ✗ viv->audio_subpackets[i].start = start;
734 ✗ viv->audio_subpackets[i].pcm_bytes = pcm_bytes;
735 }
736 ✗ last_start =
737 ✗ viv->audio_subpackets[viv->n_audio_subpackets].start = (int)(off - avio_tell(pb));
738
0/2
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 49 not taken.
✗ if (last_start < last) {
739 ✗ viv->n_audio_subpackets = 0;
740 ✗ return AVERROR_INVALIDDATA;
741 }
742 ✗ viv->current_audio_subpacket = 0;
743
744 } else {
745 ✗ uint64_t v_size = ffio_read_varlen(pb);
746
747
0/4
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 53 not taken.
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 54 not taken.
✗ if (v_size > INT_MAX || !v_size)
748 ✗ return AVERROR_INVALIDDATA;
749 ✗ ret = av_get_packet(pb, pkt, v_size);
750
0/2
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 57 not taken.
✗ if (ret < 0)
751 ✗ return ret;
752 ✗ pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
753 ✗ pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
754 ✗ pkt->flags |= (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
755 ✗ pkt->stream_index = 0;
756 }
757
758 ✗ viv->current_sb_entry++;
759
760 ✗ return 0;
761 }
762
763 ✗ static int viv_read_close(AVFormatContext *s)
764 {
765 ✗ VividasDemuxContext *viv = s->priv_data;
766
767 ✗ av_freep(&viv->sb_pb);
768 ✗ av_freep(&viv->sb_buf);
769 ✗ av_freep(&viv->sb_blocks);
770 ✗ av_freep(&viv->sb_entries);
771
772 ✗ return 0;
773 }
774
775 ✗ static int viv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
776 {
777 ✗ VividasDemuxContext *viv = s->priv_data;
778 int64_t frame;
779 int ret;
780
781
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (stream_index == 0)
782 ✗ frame = timestamp;
783 else
784 ✗ frame = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[stream_index]->time_base);
785
786
0/2
✗ Branch 18 → 6 not taken.
✗ Branch 18 → 19 not taken.
✗ for (int i = 0; i < viv->n_sb_blocks; i++) {
787
0/4
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 17 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 17 not taken.
✗ if (frame >= viv->sb_blocks[i].packet_offset && frame < viv->sb_blocks[i].packet_offset + viv->sb_blocks[i].n_packets) {
788 ✗ viv->current_sb = i;
789 // seek to ith sb block
790 ✗ avio_seek(s->pb, viv->sb_offset + viv->sb_blocks[i].byte_offset, SEEK_SET);
791 // load the block
792 ✗ ret = load_sb_block(s, viv, 0);
793
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (ret < 0)
794 ✗ return ret;
795
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 16 not taken.
✗ if (viv->num_audio) {
796 ✗ const AVCodecParameters *par = s->streams[1]->codecpar;
797 // flush audio packet queue
798 ✗ viv->current_audio_subpacket = 0;
799 ✗ viv->n_audio_subpackets = 0;
800 // most problematic part: guess audio offset
801 ✗ viv->audio_sample = av_rescale_q(viv->sb_blocks[i].packet_offset,
802 ✗ av_make_q(par->sample_rate, 1),
803 ✗ av_inv_q(s->streams[0]->time_base));
804 // hand-tuned 1.s a/v offset
805 ✗ viv->audio_sample += par->sample_rate;
806 }
807 ✗ viv->current_sb_entry = 0;
808 ✗ return 1;
809 }
810 }
811 ✗ return 0;
812 }
813
814 const FFInputFormat ff_vividas_demuxer = {
815 .p.name = "vividas",
816 .p.long_name = NULL_IF_CONFIG_SMALL("Vividas VIV"),
817 .priv_data_size = sizeof(VividasDemuxContext),
818 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
819 .read_probe = viv_probe,
820 .read_header = viv_read_header,
821 .read_packet = viv_read_packet,
822 .read_close = viv_read_close,
823 .read_seek = viv_read_seek,
824 };
825