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.

794 lines
21KB

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