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