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.

682 lines
22KB

  1. /*
  2. * Sierra VMD Audio & Video Decoders
  3. * Copyright (C) 2004 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Sierra VMD audio & video decoders
  24. * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
  25. * for more information on the Sierra VMD format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. *
  28. * The video decoder outputs PAL8 colorspace data. The decoder expects
  29. * a 0x330-byte VMD file header to be transmitted via extradata during
  30. * codec initialization. Each encoded frame that is sent to this decoder
  31. * is expected to be prepended with the appropriate 16-byte frame
  32. * information record from the VMD file.
  33. *
  34. * The audio decoder, like the video decoder, expects each encoded data
  35. * chunk to be prepended with the appropriate 16-byte frame information
  36. * record from the VMD file. It does not require the 0x330-byte VMD file
  37. * header, but it does need the audio setup parameters passed in through
  38. * normal libavcodec API means.
  39. */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "libavutil/avassert.h"
  44. #include "libavutil/channel_layout.h"
  45. #include "libavutil/common.h"
  46. #include "libavutil/intreadwrite.h"
  47. #include "avcodec.h"
  48. #include "internal.h"
  49. #include "bytestream.h"
  50. #define VMD_HEADER_SIZE 0x330
  51. #define PALETTE_COUNT 256
  52. /*
  53. * Video Decoder
  54. */
  55. typedef struct VmdVideoContext {
  56. AVCodecContext *avctx;
  57. AVFrame *prev_frame;
  58. const unsigned char *buf;
  59. int size;
  60. unsigned char palette[PALETTE_COUNT * 4];
  61. unsigned char *unpack_buffer;
  62. int unpack_buffer_size;
  63. int x_off, y_off;
  64. } VmdVideoContext;
  65. #define QUEUE_SIZE 0x1000
  66. #define QUEUE_MASK 0x0FFF
  67. static void lz_unpack(const unsigned char *src, int src_len,
  68. unsigned char *dest, int dest_len)
  69. {
  70. unsigned char *d;
  71. unsigned char *d_end;
  72. unsigned char queue[QUEUE_SIZE];
  73. unsigned int qpos;
  74. unsigned int dataleft;
  75. unsigned int chainofs;
  76. unsigned int chainlen;
  77. unsigned int speclen;
  78. unsigned char tag;
  79. unsigned int i, j;
  80. GetByteContext gb;
  81. bytestream2_init(&gb, src, src_len);
  82. d = dest;
  83. d_end = d + dest_len;
  84. dataleft = bytestream2_get_le32(&gb);
  85. memset(queue, 0x20, QUEUE_SIZE);
  86. if (bytestream2_get_bytes_left(&gb) < 4)
  87. return;
  88. if (bytestream2_peek_le32(&gb) == 0x56781234) {
  89. bytestream2_skipu(&gb, 4);
  90. qpos = 0x111;
  91. speclen = 0xF + 3;
  92. } else {
  93. qpos = 0xFEE;
  94. speclen = 100; /* no speclen */
  95. }
  96. while (dataleft > 0 && bytestream2_get_bytes_left(&gb) > 0) {
  97. tag = bytestream2_get_byteu(&gb);
  98. if ((tag == 0xFF) && (dataleft > 8)) {
  99. if (d_end - d < 8 || bytestream2_get_bytes_left(&gb) < 8)
  100. return;
  101. for (i = 0; i < 8; i++) {
  102. queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
  103. qpos &= QUEUE_MASK;
  104. }
  105. dataleft -= 8;
  106. } else {
  107. for (i = 0; i < 8; i++) {
  108. if (dataleft == 0)
  109. break;
  110. if (tag & 0x01) {
  111. if (d_end - d < 1 || bytestream2_get_bytes_left(&gb) < 1)
  112. return;
  113. queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
  114. qpos &= QUEUE_MASK;
  115. dataleft--;
  116. } else {
  117. chainofs = bytestream2_get_byte(&gb);
  118. chainofs |= ((bytestream2_peek_byte(&gb) & 0xF0) << 4);
  119. chainlen = (bytestream2_get_byte(&gb) & 0x0F) + 3;
  120. if (chainlen == speclen) {
  121. chainlen = bytestream2_get_byte(&gb) + 0xF + 3;
  122. }
  123. if (d_end - d < chainlen)
  124. return;
  125. for (j = 0; j < chainlen; j++) {
  126. *d = queue[chainofs++ & QUEUE_MASK];
  127. queue[qpos++] = *d++;
  128. qpos &= QUEUE_MASK;
  129. }
  130. dataleft -= chainlen;
  131. }
  132. tag >>= 1;
  133. }
  134. }
  135. }
  136. }
  137. static int rle_unpack(const unsigned char *src, unsigned char *dest,
  138. int src_count, int src_size, int dest_len)
  139. {
  140. unsigned char *pd;
  141. int i, l, used = 0;
  142. unsigned char *dest_end = dest + dest_len;
  143. GetByteContext gb;
  144. uint16_t run_val;
  145. bytestream2_init(&gb, src, src_size);
  146. pd = dest;
  147. if (src_count & 1) {
  148. if (bytestream2_get_bytes_left(&gb) < 1)
  149. return 0;
  150. *pd++ = bytestream2_get_byteu(&gb);
  151. used++;
  152. }
  153. do {
  154. if (bytestream2_get_bytes_left(&gb) < 1)
  155. break;
  156. l = bytestream2_get_byteu(&gb);
  157. if (l & 0x80) {
  158. l = (l & 0x7F) * 2;
  159. if (dest_end - pd < l || bytestream2_get_bytes_left(&gb) < l)
  160. return bytestream2_tell(&gb);
  161. bytestream2_get_bufferu(&gb, pd, l);
  162. pd += l;
  163. } else {
  164. if (dest_end - pd < 2*l || bytestream2_get_bytes_left(&gb) < 2)
  165. return bytestream2_tell(&gb);
  166. run_val = bytestream2_get_ne16(&gb);
  167. for (i = 0; i < l; i++) {
  168. AV_WN16(pd, run_val);
  169. pd += 2;
  170. }
  171. l *= 2;
  172. }
  173. used += l;
  174. } while (used < src_count);
  175. return bytestream2_tell(&gb);
  176. }
  177. static int vmd_decode(VmdVideoContext *s, AVFrame *frame)
  178. {
  179. int i;
  180. unsigned int *palette32;
  181. unsigned char r, g, b;
  182. GetByteContext gb;
  183. unsigned char meth;
  184. unsigned char *dp; /* pointer to current frame */
  185. unsigned char *pp; /* pointer to previous frame */
  186. unsigned char len;
  187. int ofs;
  188. int frame_x, frame_y;
  189. int frame_width, frame_height;
  190. frame_x = AV_RL16(&s->buf[6]);
  191. frame_y = AV_RL16(&s->buf[8]);
  192. frame_width = AV_RL16(&s->buf[10]) - frame_x + 1;
  193. frame_height = AV_RL16(&s->buf[12]) - frame_y + 1;
  194. if ((frame_width == s->avctx->width && frame_height == s->avctx->height) &&
  195. (frame_x || frame_y)) {
  196. s->x_off = frame_x;
  197. s->y_off = frame_y;
  198. }
  199. frame_x -= s->x_off;
  200. frame_y -= s->y_off;
  201. if (frame_x < 0 || frame_width < 0 ||
  202. frame_x >= s->avctx->width ||
  203. frame_width > s->avctx->width ||
  204. frame_x + frame_width > s->avctx->width) {
  205. av_log(s->avctx, AV_LOG_ERROR,
  206. "Invalid horizontal range %d-%d\n",
  207. frame_x, frame_width);
  208. return AVERROR_INVALIDDATA;
  209. }
  210. if (frame_y < 0 || frame_height < 0 ||
  211. frame_y >= s->avctx->height ||
  212. frame_height > s->avctx->height ||
  213. frame_y + frame_height > s->avctx->height) {
  214. av_log(s->avctx, AV_LOG_ERROR,
  215. "Invalid vertical range %d-%d\n",
  216. frame_x, frame_width);
  217. return AVERROR_INVALIDDATA;
  218. }
  219. /* if only a certain region will be updated, copy the entire previous
  220. * frame before the decode */
  221. if (s->prev_frame->data[0] &&
  222. (frame_x || frame_y || (frame_width != s->avctx->width) ||
  223. (frame_height != s->avctx->height))) {
  224. memcpy(frame->data[0], s->prev_frame->data[0],
  225. s->avctx->height * frame->linesize[0]);
  226. }
  227. /* check if there is a new palette */
  228. bytestream2_init(&gb, s->buf + 16, s->size - 16);
  229. if (s->buf[15] & 0x02) {
  230. bytestream2_skip(&gb, 2);
  231. palette32 = (unsigned int *)s->palette;
  232. if (bytestream2_get_bytes_left(&gb) >= PALETTE_COUNT * 3) {
  233. for (i = 0; i < PALETTE_COUNT; i++) {
  234. r = bytestream2_get_byteu(&gb) * 4;
  235. g = bytestream2_get_byteu(&gb) * 4;
  236. b = bytestream2_get_byteu(&gb) * 4;
  237. palette32[i] = 0xFFU << 24 | (r << 16) | (g << 8) | (b);
  238. palette32[i] |= palette32[i] >> 6 & 0x30303;
  239. }
  240. } else {
  241. av_log(s->avctx, AV_LOG_ERROR, "Incomplete palette\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. }
  245. if (!s->size)
  246. return 0;
  247. /* originally UnpackFrame in VAG's code */
  248. if (bytestream2_get_bytes_left(&gb) < 1)
  249. return AVERROR_INVALIDDATA;
  250. meth = bytestream2_get_byteu(&gb);
  251. if (meth & 0x80) {
  252. if (!s->unpack_buffer_size) {
  253. av_log(s->avctx, AV_LOG_ERROR,
  254. "Trying to unpack LZ-compressed frame with no LZ buffer\n");
  255. return AVERROR_INVALIDDATA;
  256. }
  257. lz_unpack(gb.buffer, bytestream2_get_bytes_left(&gb),
  258. s->unpack_buffer, s->unpack_buffer_size);
  259. meth &= 0x7F;
  260. bytestream2_init(&gb, s->unpack_buffer, s->unpack_buffer_size);
  261. }
  262. dp = &frame->data[0][frame_y * frame->linesize[0] + frame_x];
  263. pp = &s->prev_frame->data[0][frame_y * s->prev_frame->linesize[0] + frame_x];
  264. switch (meth) {
  265. case 1:
  266. for (i = 0; i < frame_height; i++) {
  267. ofs = 0;
  268. do {
  269. len = bytestream2_get_byte(&gb);
  270. if (len & 0x80) {
  271. len = (len & 0x7F) + 1;
  272. if (ofs + len > frame_width ||
  273. bytestream2_get_bytes_left(&gb) < len)
  274. return AVERROR_INVALIDDATA;
  275. bytestream2_get_bufferu(&gb, &dp[ofs], len);
  276. ofs += len;
  277. } else {
  278. /* interframe pixel copy */
  279. if (ofs + len + 1 > frame_width || !s->prev_frame->data[0])
  280. return AVERROR_INVALIDDATA;
  281. memcpy(&dp[ofs], &pp[ofs], len + 1);
  282. ofs += len + 1;
  283. }
  284. } while (ofs < frame_width);
  285. if (ofs > frame_width) {
  286. av_log(s->avctx, AV_LOG_ERROR,
  287. "offset > width (%d > %d)\n",
  288. ofs, frame_width);
  289. return AVERROR_INVALIDDATA;
  290. }
  291. dp += frame->linesize[0];
  292. pp += s->prev_frame->linesize[0];
  293. }
  294. break;
  295. case 2:
  296. for (i = 0; i < frame_height; i++) {
  297. bytestream2_get_buffer(&gb, dp, frame_width);
  298. dp += frame->linesize[0];
  299. pp += s->prev_frame->linesize[0];
  300. }
  301. break;
  302. case 3:
  303. for (i = 0; i < frame_height; i++) {
  304. ofs = 0;
  305. do {
  306. len = bytestream2_get_byte(&gb);
  307. if (len & 0x80) {
  308. len = (len & 0x7F) + 1;
  309. if (bytestream2_peek_byte(&gb) == 0xFF) {
  310. int slen = len;
  311. bytestream2_get_byte(&gb);
  312. len = rle_unpack(gb.buffer, &dp[ofs],
  313. len, bytestream2_get_bytes_left(&gb),
  314. frame_width - ofs);
  315. ofs += slen;
  316. bytestream2_skip(&gb, len);
  317. } else {
  318. bytestream2_get_buffer(&gb, &dp[ofs], len);
  319. ofs += len;
  320. }
  321. } else {
  322. /* interframe pixel copy */
  323. if (ofs + len + 1 > frame_width || !s->prev_frame->data[0])
  324. return AVERROR_INVALIDDATA;
  325. memcpy(&dp[ofs], &pp[ofs], len + 1);
  326. ofs += len + 1;
  327. }
  328. } while (ofs < frame_width);
  329. if (ofs > frame_width) {
  330. av_log(s->avctx, AV_LOG_ERROR,
  331. "offset > width (%d > %d)\n",
  332. ofs, frame_width);
  333. return AVERROR_INVALIDDATA;
  334. }
  335. dp += frame->linesize[0];
  336. pp += s->prev_frame->linesize[0];
  337. }
  338. break;
  339. }
  340. return 0;
  341. }
  342. static av_cold int vmdvideo_decode_end(AVCodecContext *avctx)
  343. {
  344. VmdVideoContext *s = avctx->priv_data;
  345. av_frame_free(&s->prev_frame);
  346. av_freep(&s->unpack_buffer);
  347. s->unpack_buffer_size = 0;
  348. return 0;
  349. }
  350. static av_cold int vmdvideo_decode_init(AVCodecContext *avctx)
  351. {
  352. VmdVideoContext *s = avctx->priv_data;
  353. int i;
  354. unsigned int *palette32;
  355. int palette_index = 0;
  356. unsigned char r, g, b;
  357. unsigned char *vmd_header;
  358. unsigned char *raw_palette;
  359. s->avctx = avctx;
  360. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  361. /* make sure the VMD header made it */
  362. if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
  363. av_log(s->avctx, AV_LOG_ERROR, "expected extradata size of %d\n",
  364. VMD_HEADER_SIZE);
  365. return AVERROR_INVALIDDATA;
  366. }
  367. vmd_header = (unsigned char *)avctx->extradata;
  368. s->unpack_buffer_size = AV_RL32(&vmd_header[800]);
  369. if (s->unpack_buffer_size) {
  370. s->unpack_buffer = av_malloc(s->unpack_buffer_size);
  371. if (!s->unpack_buffer)
  372. return AVERROR(ENOMEM);
  373. }
  374. /* load up the initial palette */
  375. raw_palette = &vmd_header[28];
  376. palette32 = (unsigned int *)s->palette;
  377. for (i = 0; i < PALETTE_COUNT; i++) {
  378. r = raw_palette[palette_index++] * 4;
  379. g = raw_palette[palette_index++] * 4;
  380. b = raw_palette[palette_index++] * 4;
  381. palette32[i] = 0xFFU << 24 | (r << 16) | (g << 8) | (b);
  382. palette32[i] |= palette32[i] >> 6 & 0x30303;
  383. }
  384. s->prev_frame = av_frame_alloc();
  385. if (!s->prev_frame) {
  386. vmdvideo_decode_end(avctx);
  387. return AVERROR(ENOMEM);
  388. }
  389. return 0;
  390. }
  391. static int vmdvideo_decode_frame(AVCodecContext *avctx,
  392. void *data, int *got_frame,
  393. AVPacket *avpkt)
  394. {
  395. const uint8_t *buf = avpkt->data;
  396. int buf_size = avpkt->size;
  397. VmdVideoContext *s = avctx->priv_data;
  398. AVFrame *frame = data;
  399. int ret;
  400. s->buf = buf;
  401. s->size = buf_size;
  402. if (buf_size < 16)
  403. return AVERROR_INVALIDDATA;
  404. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  405. return ret;
  406. if ((ret = vmd_decode(s, frame)) < 0)
  407. return ret;
  408. /* make the palette available on the way out */
  409. memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
  410. /* shuffle frames */
  411. av_frame_unref(s->prev_frame);
  412. if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
  413. return ret;
  414. *got_frame = 1;
  415. /* report that the buffer was completely consumed */
  416. return buf_size;
  417. }
  418. /*
  419. * Audio Decoder
  420. */
  421. #define BLOCK_TYPE_AUDIO 1
  422. #define BLOCK_TYPE_INITIAL 2
  423. #define BLOCK_TYPE_SILENCE 3
  424. typedef struct VmdAudioContext {
  425. int out_bps;
  426. int chunk_size;
  427. } VmdAudioContext;
  428. static const uint16_t vmdaudio_table[128] = {
  429. 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
  430. 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
  431. 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
  432. 0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230,
  433. 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280,
  434. 0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0,
  435. 0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320,
  436. 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370,
  437. 0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0,
  438. 0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480,
  439. 0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700,
  440. 0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00,
  441. 0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000
  442. };
  443. static av_cold int vmdaudio_decode_init(AVCodecContext *avctx)
  444. {
  445. VmdAudioContext *s = avctx->priv_data;
  446. if (avctx->channels < 1 || avctx->channels > 2) {
  447. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  448. return AVERROR(EINVAL);
  449. }
  450. if (avctx->block_align < 1 || avctx->block_align % avctx->channels) {
  451. av_log(avctx, AV_LOG_ERROR, "invalid block align\n");
  452. return AVERROR(EINVAL);
  453. }
  454. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
  455. AV_CH_LAYOUT_STEREO;
  456. if (avctx->bits_per_coded_sample == 16)
  457. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  458. else
  459. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  460. s->out_bps = av_get_bytes_per_sample(avctx->sample_fmt);
  461. s->chunk_size = avctx->block_align + avctx->channels * (s->out_bps == 2);
  462. av_log(avctx, AV_LOG_DEBUG, "%d channels, %d bits/sample, "
  463. "block align = %d, sample rate = %d\n",
  464. avctx->channels, avctx->bits_per_coded_sample, avctx->block_align,
  465. avctx->sample_rate);
  466. return 0;
  467. }
  468. static void decode_audio_s16(int16_t *out, const uint8_t *buf, int buf_size,
  469. int channels)
  470. {
  471. int ch;
  472. const uint8_t *buf_end = buf + buf_size;
  473. int predictor[2];
  474. int st = channels - 1;
  475. /* decode initial raw sample */
  476. for (ch = 0; ch < channels; ch++) {
  477. predictor[ch] = (int16_t)AV_RL16(buf);
  478. buf += 2;
  479. *out++ = predictor[ch];
  480. }
  481. /* decode DPCM samples */
  482. ch = 0;
  483. while (buf < buf_end) {
  484. uint8_t b = *buf++;
  485. if (b & 0x80)
  486. predictor[ch] -= vmdaudio_table[b & 0x7F];
  487. else
  488. predictor[ch] += vmdaudio_table[b];
  489. predictor[ch] = av_clip_int16(predictor[ch]);
  490. *out++ = predictor[ch];
  491. ch ^= st;
  492. }
  493. }
  494. static int vmdaudio_decode_frame(AVCodecContext *avctx, void *data,
  495. int *got_frame_ptr, AVPacket *avpkt)
  496. {
  497. AVFrame *frame = data;
  498. const uint8_t *buf = avpkt->data;
  499. const uint8_t *buf_end;
  500. int buf_size = avpkt->size;
  501. VmdAudioContext *s = avctx->priv_data;
  502. int block_type, silent_chunks, audio_chunks;
  503. int ret;
  504. uint8_t *output_samples_u8;
  505. int16_t *output_samples_s16;
  506. if (buf_size < 16) {
  507. av_log(avctx, AV_LOG_WARNING, "skipping small junk packet\n");
  508. *got_frame_ptr = 0;
  509. return buf_size;
  510. }
  511. block_type = buf[6];
  512. if (block_type < BLOCK_TYPE_AUDIO || block_type > BLOCK_TYPE_SILENCE) {
  513. av_log(avctx, AV_LOG_ERROR, "unknown block type: %d\n", block_type);
  514. return AVERROR(EINVAL);
  515. }
  516. buf += 16;
  517. buf_size -= 16;
  518. /* get number of silent chunks */
  519. silent_chunks = 0;
  520. if (block_type == BLOCK_TYPE_INITIAL) {
  521. uint32_t flags;
  522. if (buf_size < 4) {
  523. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  524. return AVERROR(EINVAL);
  525. }
  526. flags = AV_RB32(buf);
  527. silent_chunks = av_popcount(flags);
  528. buf += 4;
  529. buf_size -= 4;
  530. } else if (block_type == BLOCK_TYPE_SILENCE) {
  531. silent_chunks = 1;
  532. buf_size = 0; // should already be zero but set it just to be sure
  533. }
  534. /* ensure output buffer is large enough */
  535. audio_chunks = buf_size / s->chunk_size;
  536. /* drop incomplete chunks */
  537. buf_size = audio_chunks * s->chunk_size;
  538. /* get output buffer */
  539. frame->nb_samples = ((silent_chunks + audio_chunks) * avctx->block_align) /
  540. avctx->channels;
  541. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  542. return ret;
  543. output_samples_u8 = frame->data[0];
  544. output_samples_s16 = (int16_t *)frame->data[0];
  545. /* decode silent chunks */
  546. if (silent_chunks > 0) {
  547. int silent_size = avctx->block_align * silent_chunks;
  548. av_assert0(avctx->block_align * silent_chunks <= frame->nb_samples * avctx->channels);
  549. if (s->out_bps == 2) {
  550. memset(output_samples_s16, 0x00, silent_size * 2);
  551. output_samples_s16 += silent_size;
  552. } else {
  553. memset(output_samples_u8, 0x80, silent_size);
  554. output_samples_u8 += silent_size;
  555. }
  556. }
  557. /* decode audio chunks */
  558. if (audio_chunks > 0) {
  559. buf_end = buf + buf_size;
  560. av_assert0((buf_size & (avctx->channels > 1)) == 0);
  561. while (buf_end - buf >= s->chunk_size) {
  562. if (s->out_bps == 2) {
  563. decode_audio_s16(output_samples_s16, buf, s->chunk_size,
  564. avctx->channels);
  565. output_samples_s16 += avctx->block_align;
  566. } else {
  567. memcpy(output_samples_u8, buf, s->chunk_size);
  568. output_samples_u8 += avctx->block_align;
  569. }
  570. buf += s->chunk_size;
  571. }
  572. }
  573. *got_frame_ptr = 1;
  574. return avpkt->size;
  575. }
  576. /*
  577. * Public Data Structures
  578. */
  579. AVCodec ff_vmdvideo_decoder = {
  580. .name = "vmdvideo",
  581. .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD video"),
  582. .type = AVMEDIA_TYPE_VIDEO,
  583. .id = AV_CODEC_ID_VMDVIDEO,
  584. .priv_data_size = sizeof(VmdVideoContext),
  585. .init = vmdvideo_decode_init,
  586. .close = vmdvideo_decode_end,
  587. .decode = vmdvideo_decode_frame,
  588. .capabilities = CODEC_CAP_DR1,
  589. };
  590. AVCodec ff_vmdaudio_decoder = {
  591. .name = "vmdaudio",
  592. .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD audio"),
  593. .type = AVMEDIA_TYPE_AUDIO,
  594. .id = AV_CODEC_ID_VMDAUDIO,
  595. .priv_data_size = sizeof(VmdAudioContext),
  596. .init = vmdaudio_decode_init,
  597. .decode = vmdaudio_decode_frame,
  598. .capabilities = CODEC_CAP_DR1,
  599. };