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.

669 lines
21KB

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