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