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