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
38
39 #define MAX_AUDIO_SUBPACKETS 100
40
46
50
54
59
62
68
71
73
76
78 {
79 if (memcmp(p->
buf,
"vividas03", 9))
80 return 0;
81
83 }
84
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
93 {
95
96 for (
int i = 0;
i < 32;
i++) {
98 key |= ((buf[p] >> ((
i*5+3)&7)) & 1
u) <<
i;
99 }
100
102 }
103
104 static void put_v(uint8_t *p,
unsigned v)
105 {
106 if (v>>28)
107 *p++ = ((v>>28)&0x7f)|0x80;
108 if (v>>21)
109 *p++ = ((v>>21)&0x7f)|0x80;
110 if (v>>14)
111 *p++ = ((v>>14)&0x7f)|0x80;
112 if (v>>7)
113 *p++ = ((v>>7)&0x7f)|0x80;
114 }
115
117 {
118 unsigned char plaintext[8] = {
'S',
'B' };
119
121
123 }
124
126 {
127 unsigned *d1 = p1;
128 unsigned *d2 = p2;
129 unsigned k = *key_ptr;
130
132
134 *d2 = *d1 ^ (HAVE_BIGENDIAN ?
av_bswap32(k) : k);
136 d1++;
137 d2++;
139 }
140
141 *key_ptr = k;
142 }
143
145 uint32_t
key, uint32_t *key_ptr,
147 {
151
153 return;
154
157
159 uint32_t tmpkey = *key_ptr -
key;
163 }
168 }
169
174 }
175
181 }
182 }
183
185 {
186 uint32_t v = 0;
187 const uint8_t *end = p +
len;
188
189 do {
190 if (p >= end || v >= UINT_MAX / 128 - *p)
191 return v;
192 v <<= 7;
193 v += *p & 0x7f;
194 } while (*p++ & 0x80);
195
196 return v;
197 }
198
200 uint32_t
key, uint32_t *k2,
int align)
201 {
203 uint8_t *buf;
204 unsigned n;
205
208
210
212 if (n < 4)
214
216 if (!buf)
218
220 n -= 4;
221
223
226 } else {
229 }
230
231 return buf;
232 }
233
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
244
247
248 n =
get_v(sbuf+2, 6);
249
250 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;
254 n =
get_v(sbuf+2, 6);
255 if (sbuf[0] != 'S' || sbuf[1] != 'B' || expected_size != n)
258 }
259
260 if (n < 8)
262
264 if (!buf)
266
267 memcpy(buf, sbuf, 8);
268
270 n -= 8;
271
275 }
276
278
279 return buf;
280 }
281
283 const uint8_t *buf,
int size)
284 {
287 int val_1;
288 int num_video;
291
293
296
298
299 for (
i=0;
i<val_1;
i++) {
308 }
309 }
310
312
315
318
320 if (num_video != 1) {
323 }
324
325 for (
i = 0;
i < num_video;
i++) {
327 int num, den;
328
329 if (!st)
331
333
336
349
351 }
352
358
361
363 int q;
365 if (!st)
367
368 st->
id = num_video +
i;
369
372
386
388 int num_data;
389 int xd_size = 1;
390 int data_len[256];
392 uint8_t *p;
397 for (j = 0; j < num_data; j++) {
399 if (len < 0 || len > INT_MAX/2 - xd_size) {
401 }
403 xd_size +=
len + 1 +
len/255;
404 }
405
409
411 p[0] = 2;
412
413 for (j = 0; j < num_data - 1; j++) {
417 }
418
419 for (j = 0; j < num_data; j++) {
421 if (
ret < data_len[j]) {
424 break;
425 }
428 }
429
430 if (offset < st->codecpar->extradata_size)
432 }
433 }
434
435 return 0;
436 }
437
439 const uint8_t *buf,
unsigned size)
440 {
443 int maxnp=0;
448 uint64_t n_sb_blocks_tmp;
449
451
455 if (n_sb_blocks_tmp >
size / 2)
460 }
462
463 off = 0;
464 poff = 0;
465
469
470 if (size_tmp > INT_MAX || n_packets_tmp > INT_MAX)
472
475
478
481
482 if (maxnp < viv->sb_blocks[
i].n_packets)
484 }
485
488
492
493 return 0;
494 }
495
497 {
501
505 }
506
509
512 return;
513 }
514
516 if (!pb)
517 return;
518
520
526
528
532 }
533
536
538 }
539
541 {
545 int num_tracks;
547 uint32_t v;
548 uint8_t keybuffer[187];
549 uint32_t b22_size = 0;
550 uint32_t b22_key = 0;
551 uint8_t *buf = 0;
553
555
557
559
561
562 if (num_tracks != 1) {
565 }
566
569
570 if (
avio_read(pb, keybuffer, 187) != 187)
574
576
577 for (;;) {
579 int block_len, block_type;
580
581 if (here >= header_end)
582 break;
583
587
589
590 if (block_type == 22) {
594 }
595
596 avio_seek(pb, here + block_len, SEEK_SET);
597 }
598
599 if (b22_size) {
600 k2 = b22_key;
602 if (!buf)
604
606 }
607
610 if (!buf)
616
618 if (!buf)
624
629 } else {
631 }
632
633 return 0;
634 }
635
638 {
643
648
652
658
661
667 return 0;
668 }
669
674
677 }
678
680 if (!pb)
683
686
688
691 int last = 0, last_start;
692
695
697 if (v_size > INT_MAX || !v_size)
703
707
709 int start, pcm_bytes;
712
713 if (
i > 0 && start == 0)
714 break;
715 if (start < last)
717
719 last =
722 }
723 last_start =
725 if (last_start < last)
728
729 } else {
731
732 if (v_size > INT_MAX || !v_size)
741 }
742
744
745 return 0;
746 }
747
749 {
751
756
757 return 0;
758 }
759
761 {
764
765 if (stream_index == 0)
767 else
768 frame =
av_rescale_q(timestamp,
s->streams[0]->time_base,
s->streams[stream_index]->time_base);
769
773 // seek to ith sb block
775 // load the block
779 // flush audio packet queue
782 // most problematic part: guess audio offset
786 // hand-tuned 1.s a/v offset
788 }
790 return 1;
791 }
792 }
793 return 0;
794 }
795
806 };