]> git.sesse.net Git - ffmpeg/blob - libavformat/vividas.c
ecbe47765c6be72944128464ea72fa45f9bb7eae
[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(AVProbeData *p)
74 {
75     if (memcmp(p->buf, "vividas03", 9))
76         return 0;
77
78     return AVPROBE_SCORE_MAX;
79 }
80
81 static const unsigned short keybits[32] = {
82      163,  416,  893,   82,  223,  572, 1137,  430,
83      659, 1104,   13,  626,  695,  972, 1465,  686,
84      843, 1216,  317, 1122, 1383,   92,  513, 1158,
85     1243,   48,  573, 1306, 1495,  396, 1009,  350,
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>>3] & (1<<(p&7))) << 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 (sample[0]^plaintext[0])|
119         ((sample[1]^plaintext[1])<<8)|
120         ((sample[2]^plaintext[2])<<16)|
121         ((sample[3]^plaintext[3])<<24);
122 }
123
124 static void xor_block(void *p1, void *p2, unsigned size, int key, unsigned *key_ptr)
125 {
126     unsigned *d1 = p1;
127     unsigned *d2 = p2;
128     unsigned k = *key_ptr;
129
130     size >>= 2;
131
132     while (size > 0) {
133         *d2 = *d1 ^ k;
134         k += key;
135         d1++;
136         d2++;
137         size--;
138     }
139
140     *key_ptr = k;
141 }
142
143 static void decode_block(uint8_t *src, uint8_t *dest, unsigned size,
144                          uint32_t key, uint32_t *key_ptr,
145                          int align)
146 {
147     unsigned s = size;
148     char tmp[4];
149     int a2;
150
151     if (!size)
152         return;
153
154     align &= 3;
155     a2 = (4 - align) & 3;
156
157     if (align) {
158         uint32_t tmpkey = *key_ptr - key;
159         memcpy(tmp + align, src, a2);
160         xor_block(tmp, tmp, 4, key, &tmpkey);
161         memcpy(dest, tmp + align, a2);
162         s -= a2;
163     }
164
165     if (s >= 4) {
166         if (!align)
167             align = 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
181 static uint32_t get_v(uint8_t *p)
182 {
183     uint32_t v = 0;
184
185     do {
186         if (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);
208     if (!n)
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 + 4);
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);
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);
251         if (sbuf[0] != 'S' || sbuf[1] != 'B' || expected_size != n)
252             return NULL;
253         *key = tmpkey;
254     }
255
256     buf = av_malloc(n);
257     if (!buf)
258         return NULL;
259
260     memcpy(buf, sbuf, 8);
261
262     *size = n;
263     n -= 8;
264
265     if (avio_read(src, buf+8, n) < n) {
266         av_free(buf);
267         return NULL;
268     }
269
270     decode_block(buf + 8, buf + 8, n, *key, &k2, 0);
271
272     return buf;
273 }
274
275 static void track_header(VividasDemuxContext *viv, AVFormatContext *s,  uint8_t *buf, int size)
276 {
277     int i,j;
278     int64_t off;
279     int val_1;
280     int num_video, num_audio;
281     AVIOContext *pb;
282
283     pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
284     if (!pb)
285         return;
286
287     ffio_read_varlen(pb); // track_header_len
288     avio_r8(pb); // '1'
289
290     val_1 = ffio_read_varlen(pb);
291
292     for (i=0;i<val_1;i++) {
293         int c = avio_r8(pb);
294         for (j=0;j<c;j++) {
295             avio_r8(pb); // val_3
296             avio_r8(pb); // val_4
297         }
298     }
299
300     avio_r8(pb); // num_streams
301
302     off = avio_tell(pb);
303     off += ffio_read_varlen(pb); // val_5
304
305     avio_r8(pb); // '2'
306     num_video = avio_r8(pb);
307
308     avio_seek(pb, off, SEEK_SET);
309     if (num_video != 1)
310         av_log(s, AV_LOG_WARNING, "number of video tracks %d is not 1\n", num_video);
311
312     for (i = 0; i < num_video; i++) {
313         AVStream *st = avformat_new_stream(s, NULL);
314
315         st->id = i;
316
317         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
318         st->codecpar->codec_id = AV_CODEC_ID_VP6;
319
320         off = avio_tell(pb);
321         off += ffio_read_varlen(pb);
322         avio_r8(pb); // '3'
323         avio_r8(pb); // val_7
324         st->time_base.num = avio_rl32(pb); // frame_time
325         st->time_base.den = avio_rl32(pb); // time_base
326         st->nb_frames = avio_rl32(pb); // n frames
327         st->codecpar->width = avio_rl16(pb); // width
328         st->codecpar->height = avio_rl16(pb); // height
329         avio_r8(pb); // val_8
330         avio_rl32(pb); // val_9
331
332         avio_seek(pb, off, SEEK_SET);
333     }
334
335     off = avio_tell(pb);
336     off += ffio_read_varlen(pb); // val_10
337     avio_r8(pb); // '4'
338     num_audio = avio_r8(pb);
339     avio_seek(pb, off, SEEK_SET);
340
341     if (num_audio != 1)
342         av_log(s, AV_LOG_WARNING, "number of audio tracks %d is not 1\n", num_audio);
343
344     for(i=0;i<num_audio;i++) {
345         int q;
346         AVStream *st = avformat_new_stream(s, NULL);
347
348         st->id = num_video + i;
349
350         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
351         st->codecpar->codec_id = AV_CODEC_ID_VORBIS;
352
353         off = avio_tell(pb);
354         off += ffio_read_varlen(pb); // length
355         avio_r8(pb); // '5'
356         avio_r8(pb); //codec_id
357         avio_rl16(pb); //codec_subid
358         st->codecpar->channels = avio_rl16(pb); // channels
359         st->codecpar->sample_rate = avio_rl32(pb); // sample_rate
360         avio_seek(pb, 10, SEEK_CUR); // data_1
361         q = avio_r8(pb);
362         avio_seek(pb, q, SEEK_CUR); // data_2
363         avio_r8(pb); // zeropad
364
365         if (avio_tell(pb) < off) {
366             int num_data;
367             int xd_size = 0;
368             int data_len[256];
369             int offset = 1;
370             uint8_t *p;
371             ffio_read_varlen(pb); // val_13
372             avio_r8(pb); // '19'
373             ffio_read_varlen(pb); // len_3
374             num_data = avio_r8(pb);
375             for (j = 0; j < num_data; j++) {
376                 data_len[j] = ffio_read_varlen(pb);
377                 xd_size += data_len[j];
378             }
379
380             st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
381             if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size))
382                 return;
383
384             p = st->codecpar->extradata;
385             p[0] = 2;
386
387             for (j = 0; j < num_data - 1; j++)
388                 offset += av_xiphlacing(&p[offset], data_len[j]);
389
390             for (j = 0; j < num_data; j++) {
391                 avio_read(pb, &p[offset], data_len[j]);
392                 offset += data_len[j];
393             }
394
395             st->codecpar->extradata_size = offset;
396         }
397     }
398
399     av_free(pb);
400 }
401
402 static void track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *buf, unsigned size)
403 {
404     int64_t off;
405     int64_t poff;
406     int maxnp=0;
407     AVIOContext *pb;
408     int i;
409
410     pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
411     if (!pb)
412         return;
413
414     ffio_read_varlen(pb); // track_index_len
415     avio_r8(pb); // 'c'
416     viv->n_sb_blocks = ffio_read_varlen(pb);
417     viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));
418     if (!viv->sb_blocks) {
419         viv->n_sb_blocks = 0;
420         av_free(pb);
421         return;
422     }
423
424     off = 0;
425     poff = 0;
426
427     for (i = 0; i < viv->n_sb_blocks; i++) {
428         viv->sb_blocks[i].byte_offset = off;
429         viv->sb_blocks[i].packet_offset = poff;
430
431         viv->sb_blocks[i].size = ffio_read_varlen(pb);
432         viv->sb_blocks[i].n_packets = ffio_read_varlen(pb);
433
434         off += viv->sb_blocks[i].size;
435         poff += viv->sb_blocks[i].n_packets;
436
437
438         if (maxnp < viv->sb_blocks[i].n_packets)
439             maxnp = viv->sb_blocks[i].n_packets;
440     }
441
442     viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
443     av_free(pb);
444 }
445
446 static void load_sb_block(AVFormatContext *s, VividasDemuxContext *viv, unsigned expected_size)
447 {
448     uint32_t size = 0;
449     int i;
450     AVIOContext *pb = 0;
451
452     if (viv->sb_pb) {
453         av_free(viv->sb_pb);
454         viv->sb_pb = NULL;
455     }
456
457     if (viv->sb_buf)
458         av_free(viv->sb_buf);
459
460     viv->sb_buf = read_sb_block(s->pb, &size, &viv->sb_key, expected_size);
461     if (!viv->sb_buf) {
462         return;
463     }
464
465     pb = avio_alloc_context(viv->sb_buf, size, 0, NULL, NULL, NULL, NULL);
466     if (!pb)
467         return;
468
469     viv->sb_pb = pb;
470
471     avio_r8(pb); //  'S'
472     avio_r8(pb); //  'B'
473     ffio_read_varlen(pb); //  size
474     avio_r8(pb); //  junk
475     ffio_read_varlen(pb); // first packet
476
477     viv->n_sb_entries = viv->sb_blocks[viv->current_sb].n_packets;
478
479     for (i = 0; i < viv->n_sb_entries; i++) {
480         viv->sb_entries[i].size = ffio_read_varlen(pb);
481         viv->sb_entries[i].flag = avio_r8(pb);
482     }
483
484     ffio_read_varlen(pb);
485     avio_r8(pb);
486
487     viv->current_sb_entry = 0;
488 }
489
490 static int viv_read_header(AVFormatContext *s)
491 {
492     VividasDemuxContext *viv = s->priv_data;
493     AVIOContext *pb = s->pb;
494     int64_t header_end;
495     int num_tracks;
496     uint32_t key, k2;
497     uint32_t v;
498     uint8_t keybuffer[187];
499     uint32_t b22_size = 0;
500     uint32_t b22_key = 0;
501     uint8_t *buf = 0;
502
503     avio_skip(pb, 9);
504
505     header_end = avio_tell(pb);
506
507     header_end += ffio_read_varlen(pb);
508
509     num_tracks = avio_r8(pb);
510
511     if (num_tracks != 1) {
512         av_log(s, AV_LOG_ERROR, "number of tracks %d is not 1\n", num_tracks);
513         return AVERROR(EINVAL);
514     }
515
516     v = avio_r8(pb);
517     avio_seek(pb, v, SEEK_CUR);
518
519     avio_read(pb, keybuffer, 187);
520     key = decode_key(keybuffer);
521     viv->sb_key = key;
522
523     avio_rl32(pb);
524
525     for (;;) {
526         int64_t here = avio_tell(pb);
527         int block_len, block_type;
528
529         if (here >= header_end)
530             break;
531
532         block_len = ffio_read_varlen(pb);
533         block_type = avio_r8(pb);
534
535         if (block_type == 22) {
536             avio_read(pb, keybuffer, 187);
537             b22_key = decode_key(keybuffer);
538             b22_size = avio_rl32(pb);
539         }
540
541         avio_seek(pb, here + block_len, SEEK_SET);
542     }
543
544     if (b22_size) {
545         k2 = b22_key;
546         buf = read_vblock(pb, &v, b22_key, &k2, 0);
547         if (!buf)
548             return AVERROR(EIO);
549
550         av_free(buf);
551     }
552
553     k2 = key;
554     buf = read_vblock(pb, &v, key, &k2, 0);
555     if (!buf)
556         return AVERROR(EIO);
557     track_header(viv, s, buf, v);
558     av_free(buf);
559
560     buf = read_vblock(pb, &v, key, &k2, v);
561     if (!buf)
562         return AVERROR(EIO);
563     track_index(viv, s, buf, v);
564     av_free(buf);
565
566     viv->sb_offset = avio_tell(pb);
567     if (viv->n_sb_blocks > 0) {
568         viv->current_sb = 0;
569         load_sb_block(s, viv, viv->sb_blocks[0].size);
570     } else {
571         viv->current_sb = -1;
572     }
573
574     return 0;
575 }
576
577 static int viv_read_packet(AVFormatContext *s,
578                            AVPacket *pkt)
579 {
580     VividasDemuxContext *viv = s->priv_data;
581     AVIOContext *pb;
582     int64_t off;
583     int ret;
584
585     if (avio_feof(viv->sb_pb))
586         return AVERROR_EOF;
587
588     if (viv->current_audio_subpacket < viv->n_audio_subpackets) {
589         AVStream *astream;
590         int size = viv->audio_subpackets[viv->current_audio_subpacket+1].start - viv->audio_subpackets[viv->current_audio_subpacket].start;
591
592         pb = viv->sb_pb;
593         ret = av_get_packet(pb, pkt, size);
594         if (ret < 0)
595             return ret;
596         pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
597
598         pkt->stream_index = 1;
599         astream = s->streams[pkt->stream_index];
600
601         pkt->pts = av_rescale(viv->audio_sample, astream->time_base.den, astream->time_base.num) / astream->codecpar->sample_rate;
602         viv->audio_sample += viv->audio_subpackets[viv->current_audio_subpacket].pcm_bytes / 2 / astream->codecpar->channels;
603         pkt->flags |= AV_PKT_FLAG_KEY;
604         viv->current_audio_subpacket++;
605         return 0;
606     }
607
608     if (viv->current_sb_entry >= viv->n_sb_entries) {
609         if (viv->current_sb+1 >= viv->n_sb_blocks)
610             return AVERROR(EIO);
611         viv->current_sb++;
612
613         load_sb_block(s, viv, 0);
614         viv->current_sb_entry = 0;
615     }
616
617     pb = viv->sb_pb;
618     off = avio_tell(pb);
619     off += viv->sb_entries[viv->current_sb_entry].size;
620
621     if (viv->sb_entries[viv->current_sb_entry].flag == 0) {
622         uint64_t v_size = ffio_read_varlen(pb);
623
624         ffio_read_varlen(pb);
625         if (v_size > INT_MAX)
626             return AVERROR_INVALIDDATA;
627         ret = av_get_packet(pb, pkt, v_size);
628         if (ret < 0)
629             return ret;
630         pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
631
632         pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
633         pkt->flags |= (pkt->data[0]&0x80)?0:AV_PKT_FLAG_KEY;
634         pkt->stream_index = 0;
635
636         for (int i = 0; i < MAX_AUDIO_SUBPACKETS - 1; i++) {
637             int start, pcm_bytes;
638             start = ffio_read_varlen(pb);
639             pcm_bytes = ffio_read_varlen(pb);
640
641             if (i > 0 && start == 0)
642                 break;
643
644             viv->n_audio_subpackets = i + 1;
645             viv->audio_subpackets[i].start = start;
646             viv->audio_subpackets[i].pcm_bytes = pcm_bytes;
647         }
648         viv->audio_subpackets[viv->n_audio_subpackets].start = (int)(off - avio_tell(pb));
649         viv->current_audio_subpacket = 0;
650
651     } else {
652         uint64_t v_size = ffio_read_varlen(pb);
653
654         if (v_size > INT_MAX)
655             return AVERROR_INVALIDDATA;
656         ret = av_get_packet(pb, pkt, v_size);
657         if (ret < 0)
658             return ret;
659         pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
660         pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
661         pkt->flags |= (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
662         pkt->stream_index = 0;
663     }
664
665     viv->current_sb_entry++;
666
667     return 0;
668 }
669
670 static int viv_read_close(AVFormatContext *s)
671 {
672     VividasDemuxContext *viv = s->priv_data;
673
674     av_freep(&viv->sb_pb);
675     av_freep(&viv->sb_buf);
676     av_freep(&viv->sb_blocks);
677     av_freep(&viv->sb_entries);
678
679     return 0;
680 }
681
682 static int viv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
683 {
684     VividasDemuxContext *viv = s->priv_data;
685     int64_t frame;
686
687     if (stream_index == 0)
688         frame = timestamp;
689     else
690         frame = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[stream_index]->time_base);
691
692     for (int i = 0; i < viv->n_sb_blocks; i++) {
693         if (frame >= viv->sb_blocks[i].packet_offset && frame < viv->sb_blocks[i].packet_offset + viv->sb_blocks[i].n_packets) {
694             // flush audio packet queue
695             viv->current_audio_subpacket = 0;
696             viv->n_audio_subpackets = 0;
697             viv->current_sb = i;
698             // seek to ith sb block
699             avio_seek(s->pb, viv->sb_offset + viv->sb_blocks[i].byte_offset, SEEK_SET);
700             // load the block
701             load_sb_block(s, viv, 0);
702             // most problematic part: guess audio offset
703             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));
704             // hand-tuned 1.s a/v offset
705             viv->audio_sample += s->streams[1]->codecpar->sample_rate;
706             viv->current_sb_entry = 0;
707             return 1;
708         }
709     }
710     return 0;
711 }
712
713 AVInputFormat ff_vividas_demuxer = {
714     .name           = "vividas",
715     .long_name      = NULL_IF_CONFIG_SMALL("Vividas VIV"),
716     .priv_data_size = sizeof(VividasDemuxContext),
717     .read_probe     = viv_probe,
718     .read_header    = viv_read_header,
719     .read_packet    = viv_read_packet,
720     .read_close     = viv_read_close,
721     .read_seek      = viv_read_seek,
722 };