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;
60 int current_sb, current_sb_entry;
64 VIV_SB_entry *sb_entries;
66 int n_audio_subpackets;
67 int current_audio_subpacket;
71 VIV_AudioSubpacket audio_subpackets[MAX_AUDIO_SUBPACKETS];
72 } VividasDemuxContext;
74 static int viv_probe(const AVProbeData *p)
76 if (memcmp(p->buf, "vividas03", 9))
79 return AVPROBE_SCORE_MAX;
82 static const uint8_t keybits[32] = {
83 20, 52, 111, 10, 27, 71, 142, 53,
84 82, 138, 1, 78, 86, 121, 183, 85,
85 105, 152, 39, 140, 172, 11, 64, 144,
86 155, 6, 71, 163, 186, 49, 126, 43,
89 static uint32_t decode_key(uint8_t *buf)
93 for (int i = 0; i < 32; i++) {
94 unsigned p = keybits[i];
95 key |= ((buf[p] >> ((i*5+3)&7)) & 1u) << i;
101 static void put_v(uint8_t *p, unsigned v)
104 *p++ = ((v>>28)&0x7f)|0x80;
106 *p++ = ((v>>21)&0x7f)|0x80;
108 *p++ = ((v>>14)&0x7f)|0x80;
110 *p++ = ((v>>7)&0x7f)|0x80;
113 static unsigned recover_key(unsigned char sample[4], unsigned expected_size)
115 unsigned char plaintext[8] = { 'S', 'B' };
117 put_v(plaintext+2, expected_size);
119 return AV_RL32(sample) ^ AV_RL32(plaintext);
122 static void xor_block(void *p1, void *p2, unsigned size, int key, unsigned *key_ptr)
126 unsigned k = *key_ptr;
131 *d2 = *d1 ^ (HAVE_BIGENDIAN ? av_bswap32(k) : k);
141 static void decode_block(uint8_t *src, uint8_t *dest, unsigned size,
142 uint32_t key, uint32_t *key_ptr,
153 a2 = (4 - align) & 3;
156 uint32_t tmpkey = *key_ptr - key;
159 avpriv_request_sample(NULL, "tiny aligned block\n");
161 memcpy(tmp + align, src, a2);
162 xor_block(tmp, tmp, 4, key, &tmpkey);
163 memcpy(dest, tmp + align, a2);
168 xor_block(src + a2, dest + a2, s & ~3,
175 memcpy(tmp, src + size, s);
176 xor_block(&tmp, &tmp, 4, key, key_ptr);
177 memcpy(dest + size, tmp, s);
181 static uint32_t get_v(uint8_t *p, int len)
184 const uint8_t *end = p + len;
187 if (p >= end || v >= UINT_MAX / 128 - *p)
191 } while (*p++ & 0x80);
196 static uint8_t *read_vblock(AVIOContext *src, uint32_t *size,
197 uint32_t key, uint32_t *k2, int align)
203 if (avio_read(src, tmp, 4) != 4)
206 decode_block(tmp, tmp, 4, key, k2, align);
221 if (avio_read(src, buf + 4, n) == n) {
222 decode_block(buf + 4, buf + 4, n, key, k2, align);
231 static uint8_t *read_sb_block(AVIOContext *src, unsigned *size,
232 uint32_t *key, unsigned expected_size)
235 uint8_t ibuf[8], sbuf[8];
239 if (avio_read(src, ibuf, 8) < 8)
243 decode_block(ibuf, sbuf, 8, *key, &k2, 0);
245 n = get_v(sbuf+2, 6);
247 if (sbuf[0] != 'S' || sbuf[1] != 'B' || (expected_size>0 && n != expected_size)) {
248 uint32_t tmpkey = recover_key(ibuf, expected_size);
250 decode_block(ibuf, sbuf, 8, tmpkey, &k2, 0);
251 n = get_v(sbuf+2, 6);
252 if (sbuf[0] != 'S' || sbuf[1] != 'B' || expected_size != n)
264 memcpy(buf, sbuf, 8);
269 if (avio_read(src, buf+8, n) < n) {
274 decode_block(buf + 8, buf + 8, n, *key, &k2, 0);
279 static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *buf, int size)
285 AVIOContext pb0, *pb = &pb0;
287 ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
289 ffio_read_varlen(pb); // track_header_len
292 val_1 = ffio_read_varlen(pb);
294 for (i=0;i<val_1;i++) {
299 avio_r8(pb); // val_3
300 avio_r8(pb); // val_4
304 avio_r8(pb); // num_streams
307 off += ffio_read_varlen(pb); // val_5
310 num_video = avio_r8(pb);
312 avio_seek(pb, off, SEEK_SET);
313 if (num_video != 1) {
314 av_log(s, AV_LOG_ERROR, "number of video tracks %d is not 1\n", num_video);
315 return AVERROR_PATCHWELCOME;
318 for (i = 0; i < num_video; i++) {
319 AVStream *st = avformat_new_stream(s, NULL);
321 return AVERROR(ENOMEM);
325 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
326 st->codecpar->codec_id = AV_CODEC_ID_VP6;
329 off += ffio_read_varlen(pb);
331 avio_r8(pb); // val_7
332 st->time_base.num = avio_rl32(pb); // frame_time
333 st->time_base.den = avio_rl32(pb); // time_base
334 st->nb_frames = avio_rl32(pb); // n frames
335 st->codecpar->width = avio_rl16(pb); // width
336 st->codecpar->height = avio_rl16(pb); // height
337 avio_r8(pb); // val_8
338 avio_rl32(pb); // val_9
340 avio_seek(pb, off, SEEK_SET);
344 off += ffio_read_varlen(pb); // val_10
346 viv->num_audio = avio_r8(pb);
347 avio_seek(pb, off, SEEK_SET);
349 if (viv->num_audio != 1)
350 av_log(s, AV_LOG_WARNING, "number of audio tracks %d is not 1\n", viv->num_audio);
352 for(i=0;i<viv->num_audio;i++) {
354 AVStream *st = avformat_new_stream(s, NULL);
356 return AVERROR(ENOMEM);
358 st->id = num_video + i;
360 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
361 st->codecpar->codec_id = AV_CODEC_ID_VORBIS;
364 off += ffio_read_varlen(pb); // length
366 avio_r8(pb); //codec_id
367 avio_rl16(pb); //codec_subid
368 st->codecpar->channels = avio_rl16(pb); // channels
369 st->codecpar->sample_rate = avio_rl32(pb); // sample_rate
370 avio_seek(pb, 10, SEEK_CUR); // data_1
372 avio_seek(pb, q, SEEK_CUR); // data_2
373 avio_r8(pb); // zeropad
375 if (avio_tell(pb) < off) {
381 ffio_read_varlen(pb); // val_13
383 ffio_read_varlen(pb); // len_3
384 num_data = avio_r8(pb);
385 for (j = 0; j < num_data; j++) {
386 uint64_t len = ffio_read_varlen(pb);
387 if (len > INT_MAX/2 - xd_size) {
388 return AVERROR_INVALIDDATA;
394 st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
395 if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {
396 return AVERROR(ENOMEM);
399 p = st->codecpar->extradata;
402 for (j = 0; j < num_data - 1; j++) {
403 unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
404 if (delta > data_len[j]) {
405 return AVERROR_INVALIDDATA;
410 for (j = 0; j < num_data; j++) {
411 int ret = avio_read(pb, &p[offset], data_len[j]);
412 if (ret < data_len[j]) {
413 st->codecpar->extradata_size = 0;
414 av_freep(&st->codecpar->extradata);
417 offset += data_len[j];
420 if (offset < st->codecpar->extradata_size)
421 st->codecpar->extradata_size = offset;
428 static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *buf, unsigned size)
433 AVIOContext pb0, *pb = &pb0;
435 int64_t filesize = avio_size(s->pb);
437 ffio_init_context(pb, buf, size, 0, NULL, NULL, NULL, NULL);
439 ffio_read_varlen(pb); // track_index_len
441 viv->n_sb_blocks = ffio_read_varlen(pb);
442 if (viv->n_sb_blocks < 0 || viv->n_sb_blocks > size / 2)
444 viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));
445 if (!viv->sb_blocks) {
446 viv->n_sb_blocks = 0;
447 return AVERROR(ENOMEM);
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);
457 if (size_tmp > INT_MAX || n_packets_tmp > INT_MAX)
460 viv->sb_blocks[i].byte_offset = off;
461 viv->sb_blocks[i].packet_offset = poff;
463 viv->sb_blocks[i].size = size_tmp;
464 viv->sb_blocks[i].n_packets = n_packets_tmp;
466 off += viv->sb_blocks[i].size;
467 poff += viv->sb_blocks[i].n_packets;
469 if (maxnp < viv->sb_blocks[i].n_packets)
470 maxnp = viv->sb_blocks[i].n_packets;
473 if (filesize > 0 && poff > filesize)
476 viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
480 viv->n_sb_blocks = 0;
481 av_freep(&viv->sb_blocks);
482 return AVERROR_INVALIDDATA;
485 static void load_sb_block(AVFormatContext *s, VividasDemuxContext *viv, unsigned expected_size)
497 av_free(viv->sb_buf);
499 viv->sb_buf = read_sb_block(s->pb, &size, &viv->sb_key, expected_size);
504 pb = avio_alloc_context(viv->sb_buf, size, 0, NULL, NULL, NULL, NULL);
512 ffio_read_varlen(pb); // size
514 ffio_read_varlen(pb); // first packet
516 viv->n_sb_entries = viv->sb_blocks[viv->current_sb].n_packets;
518 for (i = 0; i < viv->n_sb_entries; i++) {
519 viv->sb_entries[i].size = ffio_read_varlen(pb);
520 viv->sb_entries[i].flag = avio_r8(pb);
523 ffio_read_varlen(pb);
526 viv->current_sb_entry = 0;
529 static int viv_read_header(AVFormatContext *s)
531 VividasDemuxContext *viv = s->priv_data;
532 AVIOContext *pb = s->pb;
537 uint8_t keybuffer[187];
538 uint32_t b22_size = 0;
539 uint32_t b22_key = 0;
545 header_end = avio_tell(pb);
547 header_end += ffio_read_varlen(pb);
549 num_tracks = avio_r8(pb);
551 if (num_tracks != 1) {
552 av_log(s, AV_LOG_ERROR, "number of tracks %d is not 1\n", num_tracks);
553 return AVERROR(EINVAL);
557 avio_seek(pb, v, SEEK_CUR);
559 avio_read(pb, keybuffer, 187);
560 key = decode_key(keybuffer);
566 int64_t here = avio_tell(pb);
567 int block_len, block_type;
569 if (here >= header_end)
572 block_len = ffio_read_varlen(pb);
573 if (avio_feof(pb) || block_len <= 0)
574 return AVERROR_INVALIDDATA;
576 block_type = avio_r8(pb);
578 if (block_type == 22) {
579 avio_read(pb, keybuffer, 187);
580 b22_key = decode_key(keybuffer);
581 b22_size = avio_rl32(pb);
584 avio_seek(pb, here + block_len, SEEK_SET);
589 buf = read_vblock(pb, &v, b22_key, &k2, 0);
597 buf = read_vblock(pb, &v, key, &k2, 0);
600 ret = track_header(viv, s, buf, v);
605 buf = read_vblock(pb, &v, key, &k2, v);
608 ret = track_index(viv, s, buf, v);
613 viv->sb_offset = avio_tell(pb);
614 if (viv->n_sb_blocks > 0) {
616 load_sb_block(s, viv, viv->sb_blocks[0].size);
618 viv->current_sb = -1;
624 static int viv_read_packet(AVFormatContext *s,
627 VividasDemuxContext *viv = s->priv_data;
634 if (avio_feof(viv->sb_pb))
637 if (viv->current_audio_subpacket < viv->n_audio_subpackets) {
639 int size = viv->audio_subpackets[viv->current_audio_subpacket+1].start - viv->audio_subpackets[viv->current_audio_subpacket].start;
642 ret = av_get_packet(pb, pkt, size);
645 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
647 pkt->stream_index = 1;
648 astream = s->streams[pkt->stream_index];
650 pkt->pts = av_rescale(viv->audio_sample, astream->time_base.den, astream->time_base.num) / astream->codecpar->sample_rate;
651 viv->audio_sample += viv->audio_subpackets[viv->current_audio_subpacket].pcm_bytes / 2 / astream->codecpar->channels;
652 pkt->flags |= AV_PKT_FLAG_KEY;
653 viv->current_audio_subpacket++;
657 if (viv->current_sb_entry >= viv->n_sb_entries) {
658 if (viv->current_sb+1 >= viv->n_sb_blocks)
662 load_sb_block(s, viv, 0);
663 viv->current_sb_entry = 0;
670 off += viv->sb_entries[viv->current_sb_entry].size;
672 if (viv->sb_entries[viv->current_sb_entry].flag == 0) {
673 uint64_t v_size = ffio_read_varlen(pb);
676 return AVERROR_INVALIDDATA;
678 ffio_read_varlen(pb);
679 if (v_size > INT_MAX)
680 return AVERROR_INVALIDDATA;
681 ret = av_get_packet(pb, pkt, v_size);
684 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
686 pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
687 pkt->flags |= (pkt->data[0]&0x80)?0:AV_PKT_FLAG_KEY;
688 pkt->stream_index = 0;
690 for (int i = 0; i < MAX_AUDIO_SUBPACKETS - 1; i++) {
691 int start, pcm_bytes;
692 start = ffio_read_varlen(pb);
693 pcm_bytes = ffio_read_varlen(pb);
695 if (i > 0 && start == 0)
698 viv->n_audio_subpackets = i + 1;
699 viv->audio_subpackets[i].start = start;
700 viv->audio_subpackets[i].pcm_bytes = pcm_bytes;
702 viv->audio_subpackets[viv->n_audio_subpackets].start = (int)(off - avio_tell(pb));
703 viv->current_audio_subpacket = 0;
706 uint64_t v_size = ffio_read_varlen(pb);
708 if (v_size > INT_MAX)
709 return AVERROR_INVALIDDATA;
710 ret = av_get_packet(pb, pkt, v_size);
713 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
714 pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
715 pkt->flags |= (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
716 pkt->stream_index = 0;
719 viv->current_sb_entry++;
724 static int viv_read_close(AVFormatContext *s)
726 VividasDemuxContext *viv = s->priv_data;
728 av_freep(&viv->sb_pb);
729 av_freep(&viv->sb_buf);
730 av_freep(&viv->sb_blocks);
731 av_freep(&viv->sb_entries);
736 static int viv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
738 VividasDemuxContext *viv = s->priv_data;
741 if (stream_index == 0)
744 frame = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[stream_index]->time_base);
746 for (int i = 0; i < viv->n_sb_blocks; i++) {
747 if (frame >= viv->sb_blocks[i].packet_offset && frame < viv->sb_blocks[i].packet_offset + viv->sb_blocks[i].n_packets) {
748 // flush audio packet queue
749 viv->current_audio_subpacket = 0;
750 viv->n_audio_subpackets = 0;
752 // seek to ith sb block
753 avio_seek(s->pb, viv->sb_offset + viv->sb_blocks[i].byte_offset, SEEK_SET);
755 load_sb_block(s, viv, 0);
756 // most problematic part: guess audio offset
757 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));
758 // hand-tuned 1.s a/v offset
759 viv->audio_sample += s->streams[1]->codecpar->sample_rate;
760 viv->current_sb_entry = 0;
767 AVInputFormat ff_vividas_demuxer = {
769 .long_name = NULL_IF_CONFIG_SMALL("Vividas VIV"),
770 .priv_data_size = sizeof(VividasDemuxContext),
771 .read_probe = viv_probe,
772 .read_header = viv_read_header,
773 .read_packet = viv_read_packet,
774 .read_close = viv_read_close,
775 .read_seek = viv_read_seek,