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/avassert.h"
32 #include "libavutil/intreadwrite.h"
33 #include "avio_internal.h"
37 #define MAX_AUDIO_SUBPACKETS 100
39 typedef struct VIV_SB_block {
42 int64_t packet_offset;
45 typedef struct VIV_SB_entry {
49 typedef struct VIV_AudioSubpacket {
53 typedef struct VividasDemuxContext {
55 VIV_SB_block *sb_blocks;
61 int current_sb, current_sb_entry;
65 VIV_SB_entry *sb_entries;
67 int n_audio_subpackets;
68 int current_audio_subpacket;
72 VIV_AudioSubpacket audio_subpackets[MAX_AUDIO_SUBPACKETS];
73 } VividasDemuxContext;
75 static int viv_probe(const AVProbeData *p)
77 if (memcmp(p->buf, "vividas03", 9))
80 return AVPROBE_SCORE_MAX;
83 static const uint8_t keybits[32] = {
84 20, 52, 111, 10, 27, 71, 142, 53,
85 82, 138, 1, 78, 86, 121, 183, 85,
86 105, 152, 39, 140, 172, 11, 64, 144,
87 155, 6, 71, 163, 186, 49, 126, 43,
90 static uint32_t decode_key(uint8_t *buf)
94 for (int i = 0; i < 32; i++) {
95 unsigned p = keybits[i];
96 key |= ((buf[p] >> ((i*5+3)&7)) & 1u) << i;
102 static void put_v(uint8_t *p, unsigned v)
105 *p++ = ((v>>28)&0x7f)|0x80;
107 *p++ = ((v>>21)&0x7f)|0x80;
109 *p++ = ((v>>14)&0x7f)|0x80;
111 *p++ = ((v>>7)&0x7f)|0x80;
114 static unsigned recover_key(unsigned char sample[4], unsigned expected_size)
116 unsigned char plaintext[8] = { 'S', 'B' };
118 put_v(plaintext+2, expected_size);
120 return AV_RL32(sample) ^ AV_RL32(plaintext);
123 static void xor_block(void *p1, void *p2, unsigned size, int key, unsigned *key_ptr)
127 unsigned k = *key_ptr;
132 *d2 = *d1 ^ (HAVE_BIGENDIAN ? av_bswap32(k) : k);
142 static void decode_block(uint8_t *src, uint8_t *dest, unsigned size,
143 uint32_t key, uint32_t *key_ptr,
154 a2 = (4 - align) & 3;
157 uint32_t tmpkey = *key_ptr - key;
160 avpriv_request_sample(NULL, "tiny aligned block");
162 memcpy(tmp + align, src, a2);
163 xor_block(tmp, tmp, 4, key, &tmpkey);
164 memcpy(dest, tmp + align, a2);
169 xor_block(src + a2, dest + a2, s & ~3,
176 memcpy(tmp, src + size, s);
177 xor_block(&tmp, &tmp, 4, key, key_ptr);
178 memcpy(dest + size, tmp, s);
182 static uint32_t get_v(uint8_t *p, int len)
185 const uint8_t *end = p + len;
188 if (p >= end || v >= UINT_MAX / 128 - *p)
192 } while (*p++ & 0x80);
197 static uint8_t *read_vblock(AVIOContext *src, uint32_t *size,
198 uint32_t key, uint32_t *k2, int align)
204 if (avio_read(src, tmp, 4) != 4)
207 decode_block(tmp, tmp, 4, key, k2, align);
222 if (avio_read(src, buf + 4, n) == n) {
223 decode_block(buf + 4, buf + 4, n, key, k2, align);
232 static uint8_t *read_sb_block(AVIOContext *src, unsigned *size,
233 uint32_t *key, unsigned expected_size)
236 uint8_t ibuf[8], sbuf[8];
240 if (avio_read(src, ibuf, 8) < 8)
244 decode_block(ibuf, sbuf, 8, *key, &k2, 0);
246 n = get_v(sbuf+2, 6);
248 if (sbuf[0] != 'S' || sbuf[1] != 'B' || (expected_size>0 && n != expected_size)) {
249 uint32_t tmpkey = recover_key(ibuf, expected_size);
251 decode_block(ibuf, sbuf, 8, tmpkey, &k2, 0);
252 n = get_v(sbuf+2, 6);
253 if (sbuf[0] != 'S' || sbuf[1] != 'B' || expected_size != n)
265 memcpy(buf, sbuf, 8);
270 if (avio_read(src, buf+8, n) != n) {
275 decode_block(buf + 8, buf + 8, n, *key, &k2, 0);
280 static int track_header(VividasDemuxContext *viv, AVFormatContext *s,
281 const uint8_t *buf, int size)
288 AVIOContext *const pb = &pb0.pub;
290 ffio_init_read_context(&pb0, buf, size);
292 ffio_read_varlen(pb); // track_header_len
295 val_1 = ffio_read_varlen(pb);
297 for (i=0;i<val_1;i++) {
304 avio_r8(pb); // val_3
305 avio_r8(pb); // val_4
309 avio_r8(pb); // num_streams
312 off += ffio_read_varlen(pb); // val_5
315 num_video = avio_r8(pb);
317 avio_seek(pb, off, SEEK_SET);
318 if (num_video != 1) {
319 av_log(s, AV_LOG_ERROR, "number of video tracks %d is not 1\n", num_video);
320 return AVERROR_PATCHWELCOME;
323 for (i = 0; i < num_video; i++) {
324 AVStream *st = avformat_new_stream(s, NULL);
328 return AVERROR(ENOMEM);
332 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
333 st->codecpar->codec_id = AV_CODEC_ID_VP6;
336 off += ffio_read_varlen(pb);
338 avio_r8(pb); // val_7
339 num = avio_rl32(pb); // frame_time
340 den = avio_rl32(pb); // time_base
341 avpriv_set_pts_info(st, 64, num, den);
342 st->nb_frames = avio_rl32(pb); // n frames
343 st->codecpar->width = avio_rl16(pb); // width
344 st->codecpar->height = avio_rl16(pb); // height
345 avio_r8(pb); // val_8
346 avio_rl32(pb); // val_9
348 avio_seek(pb, off, SEEK_SET);
352 off += ffio_read_varlen(pb); // val_10
354 viv->num_audio = avio_r8(pb);
355 avio_seek(pb, off, SEEK_SET);
357 if (viv->num_audio != 1)
358 av_log(s, AV_LOG_WARNING, "number of audio tracks %d is not 1\n", viv->num_audio);
360 for(i=0;i<viv->num_audio;i++) {
362 AVStream *st = avformat_new_stream(s, NULL);
364 return AVERROR(ENOMEM);
366 st->id = num_video + i;
368 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
369 st->codecpar->codec_id = AV_CODEC_ID_VORBIS;
372 off += ffio_read_varlen(pb); // length
374 avio_r8(pb); //codec_id
375 avio_rl16(pb); //codec_subid
376 st->codecpar->ch_layout.nb_channels = avio_rl16(pb); // channels
377 st->codecpar->sample_rate = avio_rl32(pb); // sample_rate
378 if (st->codecpar->sample_rate <= 0 || st->codecpar->ch_layout.nb_channels <= 0)
379 return AVERROR_INVALIDDATA;
380 avio_seek(pb, 10, SEEK_CUR); // data_1
382 avio_seek(pb, q, SEEK_CUR); // data_2
383 avio_r8(pb); // zeropad
385 if (avio_tell(pb) < off) {
391 ffio_read_varlen(pb); // val_13
393 ffio_read_varlen(pb); // len_3
394 num_data = avio_r8(pb);
395 for (j = 0; j < num_data; j++) {
396 int64_t len = ffio_read_varlen(pb);
397 if (len < 0 || len > INT_MAX/2 - xd_size) {
398 return AVERROR_INVALIDDATA;
401 xd_size += len + 1 + len/255;
404 ret = ff_alloc_extradata(st->codecpar, xd_size);
408 p = st->codecpar->extradata;
411 for (j = 0; j < num_data - 1; j++) {
412 unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
413 av_assert0(delta <= xd_size - offset);
417 for (j = 0; j < num_data; j++) {
418 int ret = avio_read(pb, &p[offset], data_len[j]);
419 if (ret < data_len[j]) {
420 st->codecpar->extradata_size = 0;
421 av_freep(&st->codecpar->extradata);
424 av_assert0(data_len[j] <= xd_size - offset);
425 offset += data_len[j];
428 if (offset < st->codecpar->extradata_size)
429 st->codecpar->extradata_size = offset;
436 static int track_index(VividasDemuxContext *viv, AVFormatContext *s,
437 const uint8_t *buf, unsigned size)
443 AVIOContext *const pb = &pb0.pub;
445 int64_t filesize = avio_size(s->pb);
446 uint64_t n_sb_blocks_tmp;
448 ffio_init_read_context(&pb0, buf, size);
450 ffio_read_varlen(pb); // track_index_len
452 n_sb_blocks_tmp = ffio_read_varlen(pb);
453 if (n_sb_blocks_tmp > size / 2)
454 return AVERROR_INVALIDDATA;
455 viv->sb_blocks = av_calloc(n_sb_blocks_tmp, sizeof(*viv->sb_blocks));
456 if (!viv->sb_blocks) {
457 return AVERROR(ENOMEM);
459 viv->n_sb_blocks = n_sb_blocks_tmp;
464 for (i = 0; i < viv->n_sb_blocks; i++) {
465 uint64_t size_tmp = ffio_read_varlen(pb);
466 uint64_t n_packets_tmp = ffio_read_varlen(pb);
468 if (size_tmp > INT_MAX || n_packets_tmp > INT_MAX)
469 return AVERROR_INVALIDDATA;
471 viv->sb_blocks[i].byte_offset = off;
472 viv->sb_blocks[i].packet_offset = poff;
474 viv->sb_blocks[i].size = size_tmp;
475 viv->sb_blocks[i].n_packets = n_packets_tmp;
477 off += viv->sb_blocks[i].size;
478 poff += viv->sb_blocks[i].n_packets;
480 if (maxnp < viv->sb_blocks[i].n_packets)
481 maxnp = viv->sb_blocks[i].n_packets;
484 if (filesize > 0 && poff > filesize)
485 return AVERROR_INVALIDDATA;
487 viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
488 if (!viv->sb_entries)
489 return AVERROR(ENOMEM);
494 static void load_sb_block(AVFormatContext *s, VividasDemuxContext *viv, unsigned expected_size)
506 av_free(viv->sb_buf);
508 viv->sb_buf = read_sb_block(s->pb, &size, &viv->sb_key, expected_size);
513 pb = avio_alloc_context(viv->sb_buf, size, 0, NULL, NULL, NULL, NULL);
521 ffio_read_varlen(pb); // size
523 ffio_read_varlen(pb); // first packet
525 viv->n_sb_entries = viv->sb_blocks[viv->current_sb].n_packets;
527 for (i = 0; i < viv->n_sb_entries; i++) {
528 viv->sb_entries[i].size = ffio_read_varlen(pb);
529 viv->sb_entries[i].flag = avio_r8(pb);
532 ffio_read_varlen(pb);
535 viv->current_sb_entry = 0;
538 static int viv_read_header(AVFormatContext *s)
540 VividasDemuxContext *viv = s->priv_data;
541 AVIOContext *pb = s->pb;
546 uint8_t keybuffer[187];
547 uint32_t b22_size = 0;
548 uint32_t b22_key = 0;
554 header_end = avio_tell(pb);
556 header_end += ffio_read_varlen(pb);
558 num_tracks = avio_r8(pb);
560 if (num_tracks != 1) {
561 av_log(s, AV_LOG_ERROR, "number of tracks %d is not 1\n", num_tracks);
562 return AVERROR(EINVAL);
566 avio_seek(pb, v, SEEK_CUR);
568 avio_read(pb, keybuffer, 187);
569 key = decode_key(keybuffer);
575 int64_t here = avio_tell(pb);
576 int block_len, block_type;
578 if (here >= header_end)
581 block_len = ffio_read_varlen(pb);
582 if (avio_feof(pb) || block_len <= 0)
583 return AVERROR_INVALIDDATA;
585 block_type = avio_r8(pb);
587 if (block_type == 22) {
588 avio_read(pb, keybuffer, 187);
589 b22_key = decode_key(keybuffer);
590 b22_size = avio_rl32(pb);
593 avio_seek(pb, here + block_len, SEEK_SET);
598 buf = read_vblock(pb, &v, b22_key, &k2, 0);
606 buf = read_vblock(pb, &v, key, &k2, 0);
609 ret = track_header(viv, s, buf, v);
614 buf = read_vblock(pb, &v, key, &k2, v);
617 ret = track_index(viv, s, buf, v);
622 viv->sb_offset = avio_tell(pb);
623 if (viv->n_sb_blocks > 0) {
625 load_sb_block(s, viv, viv->sb_blocks[0].size);
627 viv->current_sb = -1;
633 static int viv_read_packet(AVFormatContext *s,
636 VividasDemuxContext *viv = s->priv_data;
643 if (avio_feof(viv->sb_pb))
646 if (viv->current_audio_subpacket < viv->n_audio_subpackets) {
648 int size = viv->audio_subpackets[viv->current_audio_subpacket+1].start - viv->audio_subpackets[viv->current_audio_subpacket].start;
651 ret = av_get_packet(pb, pkt, size);
654 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
656 pkt->stream_index = 1;
657 astream = s->streams[pkt->stream_index];
659 pkt->pts = av_rescale_q(viv->audio_sample, av_make_q(1, astream->codecpar->sample_rate), astream->time_base);
660 viv->audio_sample += viv->audio_subpackets[viv->current_audio_subpacket].pcm_bytes / 2 /
661 astream->codecpar->ch_layout.nb_channels;
662 pkt->flags |= AV_PKT_FLAG_KEY;
663 viv->current_audio_subpacket++;
667 if (viv->current_sb_entry >= viv->n_sb_entries) {
668 if (viv->current_sb+1 >= viv->n_sb_blocks)
672 load_sb_block(s, viv, 0);
673 viv->current_sb_entry = 0;
681 if (viv->current_sb_entry >= viv->n_sb_entries)
682 return AVERROR_INVALIDDATA;
684 off += viv->sb_entries[viv->current_sb_entry].size;
686 if (viv->sb_entries[viv->current_sb_entry].flag == 0) {
687 uint64_t v_size = ffio_read_varlen(pb);
688 int last = 0, last_start;
691 return AVERROR_INVALIDDATA;
693 ffio_read_varlen(pb);
694 if (v_size > INT_MAX || !v_size)
695 return AVERROR_INVALIDDATA;
696 ret = av_get_packet(pb, pkt, v_size);
699 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
701 pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
702 pkt->flags |= (pkt->data[0]&0x80)?0:AV_PKT_FLAG_KEY;
703 pkt->stream_index = 0;
705 for (int i = 0; i < MAX_AUDIO_SUBPACKETS - 1; i++) {
706 int start, pcm_bytes;
707 start = ffio_read_varlen(pb);
708 pcm_bytes = ffio_read_varlen(pb);
710 if (i > 0 && start == 0)
713 return AVERROR_INVALIDDATA;
715 viv->n_audio_subpackets = i + 1;
717 viv->audio_subpackets[i].start = start;
718 viv->audio_subpackets[i].pcm_bytes = pcm_bytes;
721 viv->audio_subpackets[viv->n_audio_subpackets].start = (int)(off - avio_tell(pb));
722 if (last_start < last)
723 return AVERROR_INVALIDDATA;
724 viv->current_audio_subpacket = 0;
727 uint64_t v_size = ffio_read_varlen(pb);
729 if (v_size > INT_MAX || !v_size)
730 return AVERROR_INVALIDDATA;
731 ret = av_get_packet(pb, pkt, v_size);
734 pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
735 pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
736 pkt->flags |= (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
737 pkt->stream_index = 0;
740 viv->current_sb_entry++;
745 static int viv_read_close(AVFormatContext *s)
747 VividasDemuxContext *viv = s->priv_data;
749 av_freep(&viv->sb_pb);
750 av_freep(&viv->sb_buf);
751 av_freep(&viv->sb_blocks);
752 av_freep(&viv->sb_entries);
757 static int viv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
759 VividasDemuxContext *viv = s->priv_data;
762 if (stream_index == 0)
765 frame = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[stream_index]->time_base);
767 for (int i = 0; i < viv->n_sb_blocks; i++) {
768 if (frame >= viv->sb_blocks[i].packet_offset && frame < viv->sb_blocks[i].packet_offset + viv->sb_blocks[i].n_packets) {
770 // seek to ith sb block
771 avio_seek(s->pb, viv->sb_offset + viv->sb_blocks[i].byte_offset, SEEK_SET);
773 load_sb_block(s, viv, 0);
774 if (viv->num_audio) {
775 const AVCodecParameters *par = s->streams[1]->codecpar;
776 // flush audio packet queue
777 viv->current_audio_subpacket = 0;
778 viv->n_audio_subpackets = 0;
779 // most problematic part: guess audio offset
780 viv->audio_sample = av_rescale_q(viv->sb_blocks[i].packet_offset,
781 av_make_q(par->sample_rate, 1),
782 av_inv_q(s->streams[0]->time_base));
783 // hand-tuned 1.s a/v offset
784 viv->audio_sample += par->sample_rate;
786 viv->current_sb_entry = 0;
793 const AVInputFormat ff_vividas_demuxer = {
795 .long_name = NULL_IF_CONFIG_SMALL("Vividas VIV"),
796 .priv_data_size = sizeof(VividasDemuxContext),
797 .flags_internal = FF_FMT_INIT_CLEANUP,
798 .read_probe = viv_probe,
799 .read_header = viv_read_header,
800 .read_packet = viv_read_packet,
801 .read_close = viv_read_close,
802 .read_seek = viv_read_seek,