You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

780 lines
20KB

  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. * @file
  25. * @brief Vividas VIV (.viv) file demuxer
  26. * @author Andrzej Szombierski [qq at kuku eu org] (2010-07)
  27. * @sa http://wiki.multimedia.cx/index.php?title=Vividas_VIV
  28. */
  29. #include "libavutil/intreadwrite.h"
  30. #include "avio_internal.h"
  31. #include "avformat.h"
  32. #include "internal.h"
  33. #define MAX_AUDIO_SUBPACKETS 100
  34. typedef struct VIV_SB_block {
  35. int size, n_packets;
  36. int64_t byte_offset;
  37. int64_t packet_offset;
  38. } VIV_SB_block;
  39. typedef struct VIV_SB_entry {
  40. int size, flag;
  41. } VIV_SB_entry;
  42. typedef struct VIV_AudioSubpacket {
  43. int start, pcm_bytes;
  44. } VIV_AudioSubpacket;
  45. typedef struct VividasDemuxContext {
  46. int n_sb_blocks;
  47. VIV_SB_block *sb_blocks;
  48. uint32_t sb_key;
  49. int64_t sb_offset;
  50. int current_sb, current_sb_entry;
  51. uint8_t *sb_buf;
  52. AVIOContext *sb_pb;
  53. int n_sb_entries;
  54. VIV_SB_entry *sb_entries;
  55. int n_audio_subpackets;
  56. int current_audio_subpacket;
  57. int64_t audio_sample;
  58. VIV_AudioSubpacket audio_subpackets[MAX_AUDIO_SUBPACKETS];
  59. } VividasDemuxContext;
  60. static int viv_probe(const AVProbeData *p)
  61. {
  62. if (memcmp(p->buf, "vividas03", 9))
  63. return 0;
  64. return AVPROBE_SCORE_MAX;
  65. }
  66. static const uint8_t keybits[32] = {
  67. 20, 52, 111, 10, 27, 71, 142, 53,
  68. 82, 138, 1, 78, 86, 121, 183, 85,
  69. 105, 152, 39, 140, 172, 11, 64, 144,
  70. 155, 6, 71, 163, 186, 49, 126, 43,
  71. };
  72. static uint32_t decode_key(uint8_t *buf)
  73. {
  74. uint32_t key = 0;
  75. for (int i = 0; i < 32; i++) {
  76. unsigned p = keybits[i];
  77. key |= ((buf[p] >> ((i*5+3)&7)) & 1u) << i;
  78. }
  79. return key;
  80. }
  81. static void put_v(uint8_t *p, unsigned v)
  82. {
  83. if (v>>28)
  84. *p++ = ((v>>28)&0x7f)|0x80;
  85. if (v>>21)
  86. *p++ = ((v>>21)&0x7f)|0x80;
  87. if (v>>14)
  88. *p++ = ((v>>14)&0x7f)|0x80;
  89. if (v>>7)
  90. *p++ = ((v>>7)&0x7f)|0x80;
  91. }
  92. static unsigned recover_key(unsigned char sample[4], unsigned expected_size)
  93. {
  94. unsigned char plaintext[8] = { 'S', 'B' };
  95. put_v(plaintext+2, expected_size);
  96. return AV_RL32(sample) ^ AV_RL32(plaintext);
  97. }
  98. static void xor_block(void *p1, void *p2, unsigned size, int key, unsigned *key_ptr)
  99. {
  100. unsigned *d1 = p1;
  101. unsigned *d2 = p2;
  102. unsigned k = *key_ptr;
  103. size >>= 2;
  104. while (size > 0) {
  105. *d2 = *d1 ^ (HAVE_BIGENDIAN ? av_bswap32(k) : k);
  106. k += key;
  107. d1++;
  108. d2++;
  109. size--;
  110. }
  111. *key_ptr = k;
  112. }
  113. static void decode_block(uint8_t *src, uint8_t *dest, unsigned size,
  114. uint32_t key, uint32_t *key_ptr,
  115. int align)
  116. {
  117. unsigned s = size;
  118. char tmp[4];
  119. int a2;
  120. if (!size)
  121. return;
  122. align &= 3;
  123. a2 = (4 - align) & 3;
  124. if (align) {
  125. uint32_t tmpkey = *key_ptr - key;
  126. if (a2 > s) {
  127. a2 = s;
  128. avpriv_request_sample(NULL, "tiny aligned block\n");
  129. }
  130. memcpy(tmp + align, src, a2);
  131. xor_block(tmp, tmp, 4, key, &tmpkey);
  132. memcpy(dest, tmp + align, a2);
  133. s -= a2;
  134. }
  135. if (s >= 4) {
  136. xor_block(src + a2, dest + a2, s & ~3,
  137. key, key_ptr);
  138. s &= 3;
  139. }
  140. if (s) {
  141. size -= s;
  142. memcpy(tmp, src + size, s);
  143. xor_block(&tmp, &tmp, 4, key, key_ptr);
  144. memcpy(dest + size, tmp, s);
  145. }
  146. }
  147. static uint32_t get_v(uint8_t *p, int len)
  148. {
  149. uint32_t v = 0;
  150. const uint8_t *end = p + len;
  151. do {
  152. if (p >= end || v >= UINT_MAX / 128 - *p)
  153. return v;
  154. v <<= 7;
  155. v += *p & 0x7f;
  156. } while (*p++ & 0x80);
  157. return v;
  158. }
  159. static uint8_t *read_vblock(AVIOContext *src, uint32_t *size,
  160. uint32_t key, uint32_t *k2, int align)
  161. {
  162. uint8_t tmp[4];
  163. uint8_t *buf;
  164. unsigned n;
  165. if (avio_read(src, tmp, 4) != 4)
  166. return NULL;
  167. decode_block(tmp, tmp, 4, key, k2, align);
  168. n = get_v(tmp, 4);
  169. if (n < 4)
  170. return NULL;
  171. buf = av_malloc(n);
  172. if (!buf)
  173. return NULL;
  174. *size = n;
  175. n -= 4;
  176. memcpy(buf, tmp, 4);
  177. if (avio_read(src, buf + 4, n) == n) {
  178. decode_block(buf + 4, buf + 4, n, key, k2, align);
  179. } else {
  180. av_free(buf);
  181. buf = NULL;
  182. }
  183. return buf;
  184. }
  185. static uint8_t *read_sb_block(AVIOContext *src, unsigned *size,
  186. uint32_t *key, unsigned expected_size)
  187. {
  188. uint8_t *buf;
  189. uint8_t ibuf[8], sbuf[8];
  190. uint32_t k2;
  191. unsigned n;
  192. if (avio_read(src, ibuf, 8) < 8)
  193. return NULL;
  194. k2 = *key;
  195. decode_block(ibuf, sbuf, 8, *key, &k2, 0);
  196. n = get_v(sbuf+2, 6);
  197. if (sbuf[0] != 'S' || sbuf[1] != 'B' || (expected_size>0 && n != expected_size)) {
  198. uint32_t tmpkey = recover_key(ibuf, expected_size);
  199. k2 = tmpkey;
  200. decode_block(ibuf, sbuf, 8, tmpkey, &k2, 0);
  201. n = get_v(sbuf+2, 6);
  202. if (sbuf[0] != 'S' || sbuf[1] != 'B' || expected_size != n)
  203. return NULL;
  204. *key = tmpkey;
  205. }
  206. if (n < 8)
  207. return NULL;
  208. buf = av_malloc(n);
  209. if (!buf)
  210. return NULL;
  211. memcpy(buf, sbuf, 8);
  212. *size = n;
  213. n -= 8;
  214. if (avio_read(src, buf+8, n) < n) {
  215. av_free(buf);
  216. return NULL;
  217. }
  218. decode_block(buf + 8, buf + 8, n, *key, &k2, 0);
  219. return buf;
  220. }
  221. static int track_header(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *buf, int size)
  222. {
  223. int i,j;
  224. int64_t off;
  225. int val_1;
  226. int num_video, num_audio;
  227. AVIOContext *pb;
  228. pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
  229. if (!pb)
  230. return AVERROR(ENOMEM);
  231. ffio_read_varlen(pb); // track_header_len
  232. avio_r8(pb); // '1'
  233. val_1 = ffio_read_varlen(pb);
  234. for (i=0;i<val_1;i++) {
  235. int c = avio_r8(pb);
  236. for (j=0;j<c;j++) {
  237. if (avio_feof(pb))
  238. return AVERROR_EOF;
  239. avio_r8(pb); // val_3
  240. avio_r8(pb); // val_4
  241. }
  242. }
  243. avio_r8(pb); // num_streams
  244. off = avio_tell(pb);
  245. off += ffio_read_varlen(pb); // val_5
  246. avio_r8(pb); // '2'
  247. num_video = avio_r8(pb);
  248. avio_seek(pb, off, SEEK_SET);
  249. if (num_video != 1) {
  250. av_log(s, AV_LOG_ERROR, "number of video tracks %d is not 1\n", num_video);
  251. return AVERROR_PATCHWELCOME;
  252. }
  253. for (i = 0; i < num_video; i++) {
  254. AVStream *st = avformat_new_stream(s, NULL);
  255. st->id = i;
  256. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  257. st->codecpar->codec_id = AV_CODEC_ID_VP6;
  258. off = avio_tell(pb);
  259. off += ffio_read_varlen(pb);
  260. avio_r8(pb); // '3'
  261. avio_r8(pb); // val_7
  262. st->time_base.num = avio_rl32(pb); // frame_time
  263. st->time_base.den = avio_rl32(pb); // time_base
  264. st->nb_frames = avio_rl32(pb); // n frames
  265. st->codecpar->width = avio_rl16(pb); // width
  266. st->codecpar->height = avio_rl16(pb); // height
  267. avio_r8(pb); // val_8
  268. avio_rl32(pb); // val_9
  269. avio_seek(pb, off, SEEK_SET);
  270. }
  271. off = avio_tell(pb);
  272. off += ffio_read_varlen(pb); // val_10
  273. avio_r8(pb); // '4'
  274. num_audio = avio_r8(pb);
  275. avio_seek(pb, off, SEEK_SET);
  276. if (num_audio != 1)
  277. av_log(s, AV_LOG_WARNING, "number of audio tracks %d is not 1\n", num_audio);
  278. for(i=0;i<num_audio;i++) {
  279. int q;
  280. AVStream *st = avformat_new_stream(s, NULL);
  281. st->id = num_video + i;
  282. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  283. st->codecpar->codec_id = AV_CODEC_ID_VORBIS;
  284. off = avio_tell(pb);
  285. off += ffio_read_varlen(pb); // length
  286. avio_r8(pb); // '5'
  287. avio_r8(pb); //codec_id
  288. avio_rl16(pb); //codec_subid
  289. st->codecpar->channels = avio_rl16(pb); // channels
  290. st->codecpar->sample_rate = avio_rl32(pb); // sample_rate
  291. avio_seek(pb, 10, SEEK_CUR); // data_1
  292. q = avio_r8(pb);
  293. avio_seek(pb, q, SEEK_CUR); // data_2
  294. avio_r8(pb); // zeropad
  295. if (avio_tell(pb) < off) {
  296. int num_data;
  297. int xd_size = 0;
  298. int data_len[256];
  299. int offset = 1;
  300. uint8_t *p;
  301. ffio_read_varlen(pb); // val_13
  302. avio_r8(pb); // '19'
  303. ffio_read_varlen(pb); // len_3
  304. num_data = avio_r8(pb);
  305. for (j = 0; j < num_data; j++) {
  306. uint64_t len = ffio_read_varlen(pb);
  307. if (len > INT_MAX/2 - xd_size) {
  308. av_free(pb);
  309. return AVERROR_INVALIDDATA;
  310. }
  311. data_len[j] = len;
  312. xd_size += len;
  313. }
  314. st->codecpar->extradata_size = 64 + xd_size + xd_size / 255;
  315. if (ff_alloc_extradata(st->codecpar, st->codecpar->extradata_size)) {
  316. av_free(pb);
  317. return AVERROR(ENOMEM);
  318. }
  319. p = st->codecpar->extradata;
  320. p[0] = 2;
  321. for (j = 0; j < num_data - 1; j++) {
  322. unsigned delta = av_xiphlacing(&p[offset], data_len[j]);
  323. if (delta > data_len[j]) {
  324. av_free(pb);
  325. return AVERROR_INVALIDDATA;
  326. }
  327. offset += delta;
  328. }
  329. for (j = 0; j < num_data; j++) {
  330. int ret = avio_read(pb, &p[offset], data_len[j]);
  331. if (ret < data_len[j]) {
  332. st->codecpar->extradata_size = 0;
  333. av_freep(&st->codecpar->extradata);
  334. break;
  335. }
  336. offset += data_len[j];
  337. }
  338. if (offset < st->codecpar->extradata_size)
  339. st->codecpar->extradata_size = offset;
  340. }
  341. }
  342. av_free(pb);
  343. return 0;
  344. }
  345. static int track_index(VividasDemuxContext *viv, AVFormatContext *s, uint8_t *buf, unsigned size)
  346. {
  347. int64_t off;
  348. int64_t poff;
  349. int maxnp=0;
  350. AVIOContext *pb;
  351. int i;
  352. int64_t filesize = avio_size(s->pb);
  353. pb = avio_alloc_context(buf, size, 0, NULL, NULL, NULL, NULL);
  354. if (!pb)
  355. return AVERROR(ENOMEM);
  356. ffio_read_varlen(pb); // track_index_len
  357. avio_r8(pb); // 'c'
  358. viv->n_sb_blocks = ffio_read_varlen(pb);
  359. if (viv->n_sb_blocks < 0 || viv->n_sb_blocks > size / 2)
  360. goto error;
  361. viv->sb_blocks = av_calloc(viv->n_sb_blocks, sizeof(VIV_SB_block));
  362. if (!viv->sb_blocks) {
  363. viv->n_sb_blocks = 0;
  364. av_free(pb);
  365. return AVERROR(ENOMEM);
  366. }
  367. off = 0;
  368. poff = 0;
  369. for (i = 0; i < viv->n_sb_blocks; i++) {
  370. uint64_t size_tmp = ffio_read_varlen(pb);
  371. uint64_t n_packets_tmp = ffio_read_varlen(pb);
  372. if (size_tmp > INT_MAX || n_packets_tmp > INT_MAX)
  373. goto error;
  374. viv->sb_blocks[i].byte_offset = off;
  375. viv->sb_blocks[i].packet_offset = poff;
  376. viv->sb_blocks[i].size = size_tmp;
  377. viv->sb_blocks[i].n_packets = n_packets_tmp;
  378. off += viv->sb_blocks[i].size;
  379. poff += viv->sb_blocks[i].n_packets;
  380. if (maxnp < viv->sb_blocks[i].n_packets)
  381. maxnp = viv->sb_blocks[i].n_packets;
  382. }
  383. if (filesize > 0 && poff > filesize)
  384. goto error;
  385. viv->sb_entries = av_calloc(maxnp, sizeof(VIV_SB_entry));
  386. av_free(pb);
  387. return 0;
  388. error:
  389. av_free(pb);
  390. viv->n_sb_blocks = 0;
  391. av_freep(&viv->sb_blocks);
  392. return AVERROR_INVALIDDATA;
  393. }
  394. static void load_sb_block(AVFormatContext *s, VividasDemuxContext *viv, unsigned expected_size)
  395. {
  396. uint32_t size = 0;
  397. int i;
  398. AVIOContext *pb = 0;
  399. if (viv->sb_pb) {
  400. av_free(viv->sb_pb);
  401. viv->sb_pb = NULL;
  402. }
  403. if (viv->sb_buf)
  404. av_free(viv->sb_buf);
  405. viv->sb_buf = read_sb_block(s->pb, &size, &viv->sb_key, expected_size);
  406. if (!viv->sb_buf) {
  407. return;
  408. }
  409. pb = avio_alloc_context(viv->sb_buf, size, 0, NULL, NULL, NULL, NULL);
  410. if (!pb)
  411. return;
  412. viv->sb_pb = pb;
  413. avio_r8(pb); // 'S'
  414. avio_r8(pb); // 'B'
  415. ffio_read_varlen(pb); // size
  416. avio_r8(pb); // junk
  417. ffio_read_varlen(pb); // first packet
  418. viv->n_sb_entries = viv->sb_blocks[viv->current_sb].n_packets;
  419. for (i = 0; i < viv->n_sb_entries; i++) {
  420. viv->sb_entries[i].size = ffio_read_varlen(pb);
  421. viv->sb_entries[i].flag = avio_r8(pb);
  422. }
  423. ffio_read_varlen(pb);
  424. avio_r8(pb);
  425. viv->current_sb_entry = 0;
  426. }
  427. static int viv_read_header(AVFormatContext *s)
  428. {
  429. VividasDemuxContext *viv = s->priv_data;
  430. AVIOContext *pb = s->pb;
  431. int64_t header_end;
  432. int num_tracks;
  433. uint32_t key, k2;
  434. uint32_t v;
  435. uint8_t keybuffer[187];
  436. uint32_t b22_size = 0;
  437. uint32_t b22_key = 0;
  438. uint8_t *buf = 0;
  439. int ret;
  440. avio_skip(pb, 9);
  441. header_end = avio_tell(pb);
  442. header_end += ffio_read_varlen(pb);
  443. num_tracks = avio_r8(pb);
  444. if (num_tracks != 1) {
  445. av_log(s, AV_LOG_ERROR, "number of tracks %d is not 1\n", num_tracks);
  446. return AVERROR(EINVAL);
  447. }
  448. v = avio_r8(pb);
  449. avio_seek(pb, v, SEEK_CUR);
  450. avio_read(pb, keybuffer, 187);
  451. key = decode_key(keybuffer);
  452. viv->sb_key = key;
  453. avio_rl32(pb);
  454. for (;;) {
  455. int64_t here = avio_tell(pb);
  456. int block_len, block_type;
  457. if (here >= header_end)
  458. break;
  459. block_len = ffio_read_varlen(pb);
  460. if (avio_feof(pb) || block_len <= 0)
  461. return AVERROR_INVALIDDATA;
  462. block_type = avio_r8(pb);
  463. if (block_type == 22) {
  464. avio_read(pb, keybuffer, 187);
  465. b22_key = decode_key(keybuffer);
  466. b22_size = avio_rl32(pb);
  467. }
  468. avio_seek(pb, here + block_len, SEEK_SET);
  469. }
  470. if (b22_size) {
  471. k2 = b22_key;
  472. buf = read_vblock(pb, &v, b22_key, &k2, 0);
  473. if (!buf)
  474. return AVERROR(EIO);
  475. av_free(buf);
  476. }
  477. k2 = key;
  478. buf = read_vblock(pb, &v, key, &k2, 0);
  479. if (!buf)
  480. return AVERROR(EIO);
  481. ret = track_header(viv, s, buf, v);
  482. av_free(buf);
  483. if (ret < 0)
  484. return ret;
  485. buf = read_vblock(pb, &v, key, &k2, v);
  486. if (!buf)
  487. return AVERROR(EIO);
  488. ret = track_index(viv, s, buf, v);
  489. av_free(buf);
  490. if (ret < 0)
  491. return ret;
  492. viv->sb_offset = avio_tell(pb);
  493. if (viv->n_sb_blocks > 0) {
  494. viv->current_sb = 0;
  495. load_sb_block(s, viv, viv->sb_blocks[0].size);
  496. } else {
  497. viv->current_sb = -1;
  498. }
  499. return 0;
  500. }
  501. static int viv_read_packet(AVFormatContext *s,
  502. AVPacket *pkt)
  503. {
  504. VividasDemuxContext *viv = s->priv_data;
  505. AVIOContext *pb;
  506. int64_t off;
  507. int ret;
  508. if (!viv->sb_pb)
  509. return AVERROR(EIO);
  510. if (avio_feof(viv->sb_pb))
  511. return AVERROR_EOF;
  512. if (viv->current_audio_subpacket < viv->n_audio_subpackets) {
  513. AVStream *astream;
  514. int size = viv->audio_subpackets[viv->current_audio_subpacket+1].start - viv->audio_subpackets[viv->current_audio_subpacket].start;
  515. pb = viv->sb_pb;
  516. ret = av_get_packet(pb, pkt, size);
  517. if (ret < 0)
  518. return ret;
  519. pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
  520. pkt->stream_index = 1;
  521. astream = s->streams[pkt->stream_index];
  522. pkt->pts = av_rescale(viv->audio_sample, astream->time_base.den, astream->time_base.num) / astream->codecpar->sample_rate;
  523. viv->audio_sample += viv->audio_subpackets[viv->current_audio_subpacket].pcm_bytes / 2 / astream->codecpar->channels;
  524. pkt->flags |= AV_PKT_FLAG_KEY;
  525. viv->current_audio_subpacket++;
  526. return 0;
  527. }
  528. if (viv->current_sb_entry >= viv->n_sb_entries) {
  529. if (viv->current_sb+1 >= viv->n_sb_blocks)
  530. return AVERROR(EIO);
  531. viv->current_sb++;
  532. load_sb_block(s, viv, 0);
  533. viv->current_sb_entry = 0;
  534. }
  535. pb = viv->sb_pb;
  536. if (!pb)
  537. return AVERROR(EIO);
  538. off = avio_tell(pb);
  539. off += viv->sb_entries[viv->current_sb_entry].size;
  540. if (viv->sb_entries[viv->current_sb_entry].flag == 0) {
  541. uint64_t v_size = ffio_read_varlen(pb);
  542. ffio_read_varlen(pb);
  543. if (v_size > INT_MAX)
  544. return AVERROR_INVALIDDATA;
  545. ret = av_get_packet(pb, pkt, v_size);
  546. if (ret < 0)
  547. return ret;
  548. pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
  549. pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
  550. pkt->flags |= (pkt->data[0]&0x80)?0:AV_PKT_FLAG_KEY;
  551. pkt->stream_index = 0;
  552. for (int i = 0; i < MAX_AUDIO_SUBPACKETS - 1; i++) {
  553. int start, pcm_bytes;
  554. start = ffio_read_varlen(pb);
  555. pcm_bytes = ffio_read_varlen(pb);
  556. if (i > 0 && start == 0)
  557. break;
  558. viv->n_audio_subpackets = i + 1;
  559. viv->audio_subpackets[i].start = start;
  560. viv->audio_subpackets[i].pcm_bytes = pcm_bytes;
  561. }
  562. viv->audio_subpackets[viv->n_audio_subpackets].start = (int)(off - avio_tell(pb));
  563. viv->current_audio_subpacket = 0;
  564. } else {
  565. uint64_t v_size = ffio_read_varlen(pb);
  566. if (v_size > INT_MAX)
  567. return AVERROR_INVALIDDATA;
  568. ret = av_get_packet(pb, pkt, v_size);
  569. if (ret < 0)
  570. return ret;
  571. pkt->pos += viv->sb_offset + viv->sb_blocks[viv->current_sb].byte_offset;
  572. pkt->pts = viv->sb_blocks[viv->current_sb].packet_offset + viv->current_sb_entry;
  573. pkt->flags |= (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;
  574. pkt->stream_index = 0;
  575. }
  576. viv->current_sb_entry++;
  577. return 0;
  578. }
  579. static int viv_read_close(AVFormatContext *s)
  580. {
  581. VividasDemuxContext *viv = s->priv_data;
  582. av_freep(&viv->sb_pb);
  583. av_freep(&viv->sb_buf);
  584. av_freep(&viv->sb_blocks);
  585. av_freep(&viv->sb_entries);
  586. return 0;
  587. }
  588. static int viv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  589. {
  590. VividasDemuxContext *viv = s->priv_data;
  591. int64_t frame;
  592. if (stream_index == 0)
  593. frame = timestamp;
  594. else
  595. frame = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[stream_index]->time_base);
  596. for (int i = 0; i < viv->n_sb_blocks; i++) {
  597. if (frame >= viv->sb_blocks[i].packet_offset && frame < viv->sb_blocks[i].packet_offset + viv->sb_blocks[i].n_packets) {
  598. // flush audio packet queue
  599. viv->current_audio_subpacket = 0;
  600. viv->n_audio_subpackets = 0;
  601. viv->current_sb = i;
  602. // seek to ith sb block
  603. avio_seek(s->pb, viv->sb_offset + viv->sb_blocks[i].byte_offset, SEEK_SET);
  604. // load the block
  605. load_sb_block(s, viv, 0);
  606. // most problematic part: guess audio offset
  607. 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));
  608. // hand-tuned 1.s a/v offset
  609. viv->audio_sample += s->streams[1]->codecpar->sample_rate;
  610. viv->current_sb_entry = 0;
  611. return 1;
  612. }
  613. }
  614. return 0;
  615. }
  616. AVInputFormat ff_vividas_demuxer = {
  617. .name = "vividas",
  618. .long_name = NULL_IF_CONFIG_SMALL("Vividas VIV"),
  619. .priv_data_size = sizeof(VividasDemuxContext),
  620. .read_probe = viv_probe,
  621. .read_header = viv_read_header,
  622. .read_packet = viv_read_packet,
  623. .read_close = viv_read_close,
  624. .read_seek = viv_read_seek,
  625. };