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
7 * This file is part of FFmpeg.
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.
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.
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
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
31 #include "libavutil/intreadwrite.h"
32 #include "avio_internal.h"
36 #define MAX_AUDIO_SUBPACKETS 100
38 typedef struct VIV_SB_block {
41 int64_t packet_offset;
44 typedef struct VIV_SB_entry {
48 typedef struct VIV_AudioSubpacket {
52 typedef struct VividasDemuxContext {
54 VIV_SB_block *sb_blocks;
59 int current_sb, current_sb_entry;
63 VIV_SB_entry *sb_entries;
65 int n_audio_subpackets;
66 int current_audio_subpacket;
70 VIV_AudioSubpacket audio_subpackets[MAX_AUDIO_SUBPACKETS];
71 } VividasDemuxContext;
73 static int viv_probe(const AVProbeData *p)
75 if (memcmp(p->buf, "vividas03", 9))
78 return AVPROBE_SCORE_MAX;
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,
88 static uint32_t decode_key(uint8_t *buf)
92 for (int i = 0; i < 32; i++) {
93 unsigned p = keybits[i];
94 key |= ((buf[p] >> ((i*5+3)&7)) & 1u) << i;
100 static void put_v(uint8_t *p, unsigned v)
103 *p++ = ((v>>28)&0x7f)|0x80;
105 *p++ = ((v>>21)&0x7f)|0x80;
107 *p++ = ((v>>14)&0x7f)|0x80;
109 *p++ = ((v>>7)&0x7f)|0x80;
112 static unsigned recover_key(unsigned char sample[4], unsigned expected_size)
114 unsigned char plaintext[8] = { 'S', 'B' };
116 put_v(plaintext+2, expected_size);
118 return AV_RL32(sample) ^ AV_RL32(plaintext);
121 static void xor_block(void *p1, void *p2, unsigned size, int key, unsigned *key_ptr)
125 unsigned k = *key_ptr;
130 *d2 = *d1 ^ (HAVE_BIGENDIAN ? av_bswap32(k) : k);
140 static void decode_block(uint8_t *src, uint8_t *dest, unsigned size,
141 uint32_t key, uint32_t *key_ptr,
152 a2 = (4 - align) & 3;
155 uint32_t tmpkey = *key_ptr - key;
156 memcpy(tmp + align, src, a2);
157 xor_block(tmp, tmp, 4, key, &tmpkey);
158 memcpy(dest, tmp + align, a2);
165 xor_block(src + a2, dest + a2, s & ~3,
172 memcpy(tmp, src + size, s);
173 xor_block(&tmp, &tmp, 4, key, key_ptr);
174 memcpy(dest + size, tmp, s);
178 static uint32_t get_v(uint8_t *p, int len)
181 const uint8_t *end = p + len;
184 if (p >= end || v >= UINT_MAX / 128 - *p)
188 } while (*p++ & 0x80);
193 static uint8_t *read_vblock(AVIOContext *src, uint32_t *size,
194 uint32_t key, uint32_t *k2, int align)
200 if (avio_read(src, tmp, 4) != 4)
203 decode_block(tmp, tmp, 4, key, k2, align);
218 if (avio_read(src, buf + 4, n) == n) {
219 decode_block(buf + 4, buf + 4, n, key, k2, align + 4);
228 static uint8_t *read_sb_block(AVIOContext *src, unsigned *size,
229 uint32_t *key, unsigned expected_size)
232 uint8_t ibuf[8], sbuf[8];
236 if (avio_read(src, ibuf, 8) < 8)
240 decode_block(ibuf, sbuf, 8, *key, &k2, 0);
242 n = get_v(sbuf+2, 6);
244 if (sbuf[0] != 'S' || sbuf[1] != 'B' || (expected_size>0 && n != expected_size)) {
245 uint32_t tmpkey = recover_key(ibuf, expected_size);
247 decode_block(ibuf, sbuf, 8, tmpkey, &k2, 0);
248 n = get_v(sbuf+2, 6);
249 if (sbuf[0] != 'S' || sbuf[1] != 'B' || expected_size != n)
261 memcpy(buf, sbuf, 8);
266 if (avio_read(src, buf+8, n) < n) {
271 decode_block(buf + 8, buf + 8, n, *key, &k2, 0);
276 static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *buf, int size)
281 int num_video, num_audio;
284 pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
286 return AVERROR(ENOMEM);
288 ffio_read_varlen(pb); // track_header_len
291 val_1 = ffio_read_varlen(pb);
293 for (i=0;i<val_1;i++) {
296 avio_r8(pb); // val_3
297 avio_r8(pb); // val_4
301 avio_r8(pb); // num_streams
304 off += ffio_read_varlen(pb); // val_5
307 num_video = avio_r8(pb);
309 avio_seek(pb, off, SEEK_SET);
311 av_log(s, AV_LOG_WARNING, "number of video tracks %d is not 1\n", num_video);
313 for (i = 0; i < num_video; i++) {
314 AVStream *st = avformat_new_stream(s, NULL);
318 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
319 st->codecpar->codec_id = AV_CODEC_ID_VP6;
322 off += ffio_read_varlen(pb);
324 avio_r8(pb); // val_7
325 st->time_base.num = avio_rl32(pb); // frame_time
326 st->time_base.den = avio_rl32(pb); // time_base
327 st->nb_frames = avio_rl32(pb); // n frames
328 st->codecpar->width = avio_rl16(pb); // width
329 st->codecpar->height = avio_rl16(pb); // height
330 avio_r8(pb); // val_8
331 avio_rl32(pb); // val_9
333 avio_seek(pb, off, SEEK_SET);
337 off += ffio_read_varlen(pb); // val_10
339 num_audio = avio_r8(pb);
340 avio_seek(pb, off, SEEK_SET);
343 av_log(s, AV_LOG_WARNING, "number of audio tracks %d is not 1\n", num_audio);
345 for(i=0;i<num_audio;i++) {
347 AVStream *st = avformat_new_stream(s, NULL);
349 st->id = num_video + i;
351 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
352 st->codecpar->codec_id = AV_CODEC_ID_VORBIS;
355 off += ffio_read_varlen(pb); // length
357 avio_r8(pb); //codec_id
358 avio_rl16(pb); //codec_subid
359 st->codecpar->channels = avio_rl16(pb); // channels
360 st->codecpar->sample_rate = avio_rl32(pb); // sample_rate
361 avio_seek(pb, 10, SEEK_CUR); // data_1
363 avio_seek(pb, q, SEEK_CUR); // data_2
364 avio_r8(pb); // zeropad
366 if (avio_tell(pb) < off) {
372 ffio_read_varlen(pb); // val_13
374 ffio_read_varlen(pb); // len_3
375 num_data = avio_r8(pb);
376 for (j = 0; j < num_data; j++) {
377 uint64_t len = ffio_read_varlen(pb);
378 if (len > INT_MAX/2 - xd_size) {
380 return AVERROR_INVALIDDATA;
386 st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
387 if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {
389 return AVERROR(ENOMEM);
392 p = st->codecpar->extradata;
395 for (j = 0; j < num_data - 1; j++) {
396 unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
397 if (delta > data_len[j]) {
399 return AVERROR_INVALIDDATA;
404 for (j = 0; j < num_data; j++) {
405 int ret = avio_read(pb, &p[offset], data_len[j]);
406 if (ret < data_len[j]) {
407 st->codecpar->extradata_size = 0;
408 av_freep(&st->codecpar->extradata);
411 offset += data_len[j];
414 if (offset < st->codecpar->extradata_size)
415 st->codecpar->extradata_size = offset;
423 static void track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *buf, unsigned size)
431 pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
435 ffio_read_varlen(pb); // track_index_len
437 viv->n_sb_blocks = ffio_read_varlen(pb);
438 viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));
439 if (!viv->sb_blocks) {
440 viv->n_sb_blocks = 0;
448 for (i = 0; i < viv->n_sb_blocks; i++) {
449 viv->sb_blocks[i].byte_offset = off;
450 viv->sb_blocks[i].packet_offset = poff;
452 viv->sb_blocks[i].size = ffio_read_varlen(pb);
453 viv->sb_blocks[i].n_packets = ffio_read_varlen(pb);
455 off += viv->sb_blocks[i].size;
456 poff += viv->sb_blocks[i].n_packets;
459 if (maxnp < viv->sb_blocks[i].n_packets)
460 maxnp = viv->sb_blocks[i].n_packets;
463 viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
467 static void load_sb_block(AVFormatContext *s, VividasDemuxContext *viv, unsigned expected_size)
479 av_free(viv->sb_buf);
481 viv->sb_buf = read_sb_block(s->pb, &size, &viv->sb_key, expected_size);
486 pb = avio_alloc_context(viv->sb_buf, size, 0, NULL, NULL, NULL, NULL);
494 ffio_read_varlen(pb); // size
496 ffio_read_varlen(pb); // first packet
498 viv->n_sb_entries = viv->sb_blocks[viv->current_sb].n_packets;
500 for (i = 0; i < viv->n_sb_entries; i++) {
501 viv->sb_entries[i].size = ffio_read_varlen(pb);
502 viv->sb_entries[i].flag = avio_r8(pb);
505 ffio_read_varlen(pb);
508 viv->current_sb_entry = 0;
511 static int viv_read_header(AVFormatContext *s)
513 VividasDemuxContext *viv = s->priv_data;
514 AVIOContext *pb = s->pb;
519 uint8_t keybuffer[187];
520 uint32_t b22_size = 0;
521 uint32_t b22_key = 0;
527 header_end = avio_tell(pb);
529 header_end += ffio_read_varlen(pb);
531 num_tracks = avio_r8(pb);
533 if (num_tracks != 1) {
534 av_log(s, AV_LOG_ERROR, "number of tracks %d is not 1\n", num_tracks);
535 return AVERROR(EINVAL);
539 avio_seek(pb, v, SEEK_CUR);
541 avio_read(pb, keybuffer, 187);
542 key = decode_key(keybuffer);
548 int64_t here = avio_tell(pb);
549 int block_len, block_type;
551 if (here >= header_end)
554 block_len = ffio_read_varlen(pb);
555 if (avio_feof(pb) || block_len <= 0)
556 return AVERROR_INVALIDDATA;
558 block_type = avio_r8(pb);
560 if (block_type == 22) {
561 avio_read(pb, keybuffer, 187);
562 b22_key = decode_key(keybuffer);
563 b22_size = avio_rl32(pb);
566 avio_seek(pb, here + block_len, SEEK_SET);
571 buf = read_vblock(pb, &v, b22_key, &k2, 0);
579 buf = read_vblock(pb, &v, key, &k2, 0);
582 ret = track_header(viv, s, buf, v);
587 buf = read_vblock(pb, &v, key, &k2, v);
590 track_index(viv, s, buf, v);
593 viv->sb_offset = avio_tell(pb);
594 if (viv->n_sb_blocks > 0) {
596 load_sb_block(s, viv, viv->sb_blocks[0].size);
598 viv->current_sb = -1;
604 static int viv_read_packet(AVFormatContext *s,
607 VividasDemuxContext *viv = s->priv_data;
614 if (avio_feof(viv->sb_pb))
617 if (viv->current_audio_subpacket < viv->n_audio_subpackets) {
619 int size = viv->audio_subpackets[viv->current_audio_subpacket+1].start - viv->audio_subpackets[viv->current_audio_subpacket].start;
622 ret = av_get_packet(pb, pkt, size);
625 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
627 pkt->stream_index = 1;
628 astream = s->streams[pkt->stream_index];
630 pkt->pts = av_rescale(viv->audio_sample, astream->time_base.den, astream->time_base.num) / astream->codecpar->sample_rate;
631 viv->audio_sample += viv->audio_subpackets[viv->current_audio_subpacket].pcm_bytes / 2 / astream->codecpar->channels;
632 pkt->flags |= AV_PKT_FLAG_KEY;
633 viv->current_audio_subpacket++;
637 if (viv->current_sb_entry >= viv->n_sb_entries) {
638 if (viv->current_sb+1 >= viv->n_sb_blocks)
642 load_sb_block(s, viv, 0);
643 viv->current_sb_entry = 0;
650 off += viv->sb_entries[viv->current_sb_entry].size;
652 if (viv->sb_entries[viv->current_sb_entry].flag == 0) {
653 uint64_t v_size = ffio_read_varlen(pb);
655 ffio_read_varlen(pb);
656 if (v_size > INT_MAX)
657 return AVERROR_INVALIDDATA;
658 ret = av_get_packet(pb, pkt, v_size);
661 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
663 pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
664 pkt->flags |= (pkt->data[0]&0x80)?0:AV_PKT_FLAG_KEY;
665 pkt->stream_index = 0;
667 for (int i = 0; i < MAX_AUDIO_SUBPACKETS - 1; i++) {
668 int start, pcm_bytes;
669 start = ffio_read_varlen(pb);
670 pcm_bytes = ffio_read_varlen(pb);
672 if (i > 0 && start == 0)
675 viv->n_audio_subpackets = i + 1;
676 viv->audio_subpackets[i].start = start;
677 viv->audio_subpackets[i].pcm_bytes = pcm_bytes;
679 viv->audio_subpackets[viv->n_audio_subpackets].start = (int)(off - avio_tell(pb));
680 viv->current_audio_subpacket = 0;
683 uint64_t v_size = ffio_read_varlen(pb);
685 if (v_size > INT_MAX)
686 return AVERROR_INVALIDDATA;
687 ret = av_get_packet(pb, pkt, v_size);
690 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
691 pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
692 pkt->flags |= (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
693 pkt->stream_index = 0;
696 viv->current_sb_entry++;
701 static int viv_read_close(AVFormatContext *s)
703 VividasDemuxContext *viv = s->priv_data;
705 av_freep(&viv->sb_pb);
706 av_freep(&viv->sb_buf);
707 av_freep(&viv->sb_blocks);
708 av_freep(&viv->sb_entries);
713 static int viv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
715 VividasDemuxContext *viv = s->priv_data;
718 if (stream_index == 0)
721 frame = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[stream_index]->time_base);
723 for (int i = 0; i < viv->n_sb_blocks; i++) {
724 if (frame >= viv->sb_blocks[i].packet_offset && frame < viv->sb_blocks[i].packet_offset + viv->sb_blocks[i].n_packets) {
725 // flush audio packet queue
726 viv->current_audio_subpacket = 0;
727 viv->n_audio_subpackets = 0;
729 // seek to ith sb block
730 avio_seek(s->pb, viv->sb_offset + viv->sb_blocks[i].byte_offset, SEEK_SET);
732 load_sb_block(s, viv, 0);
733 // most problematic part: guess audio offset
734 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));
735 // hand-tuned 1.s a/v offset
736 viv->audio_sample += s->streams[1]->codecpar->sample_rate;
737 viv->current_sb_entry = 0;
744 AVInputFormat ff_vividas_demuxer = {
746 .long_name = NULL_IF_CONFIG_SMALL("Vividas VIV"),
747 .priv_data_size = sizeof(VividasDemuxContext),
748 .read_probe = viv_probe,
749 .read_header = viv_read_header,
750 .read_packet = viv_read_packet,
751 .read_close = viv_read_close,
752 .read_seek = viv_read_seek,