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 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. if (!s->unpack_buffer_size) {
  252. av_log(s->avctx, AV_LOG_ERROR,
  253. "Trying to unpack LZ-compressed frame with no LZ buffer\n");
  254. return AVERROR_INVALIDDATA;
  255. }
  256. lz_unpack(gb.buffer, bytestream2_get_bytes_left(&gb),
  257. s->unpack_buffer, s->unpack_buffer_size);
  258. meth &= 0x7F;
  259. bytestream2_init(&gb, s->unpack_buffer, s->unpack_buffer_size);
  260. }
  261. dp = &frame->data[0][frame_y * frame->linesize[0] + frame_x];
  262. pp = &s->prev_frame->data[0][frame_y * s->prev_frame->linesize[0] + frame_x];
  263. switch (meth) {
  264. case 1:
  265. for (i = 0; i < frame_height; i++) {
  266. ofs = 0;
  267. do {
  268. len = bytestream2_get_byte(&gb);
  269. if (len & 0x80) {
  270. len = (len & 0x7F) + 1;
  271. if (ofs + len > frame_width ||
  272. bytestream2_get_bytes_left(&gb) < len)
  273. return AVERROR_INVALIDDATA;
  274. bytestream2_get_buffer(&gb, &dp[ofs], len);
  275. ofs += len;
  276. } else {
  277. /* interframe pixel copy */
  278. if (ofs + len + 1 > frame_width || !s->prev_frame->data[0])
  279. return AVERROR_INVALIDDATA;
  280. memcpy(&dp[ofs], &pp[ofs], len + 1);
  281. ofs += len + 1;
  282. }
  283. } while (ofs < frame_width);
  284. if (ofs > frame_width) {
  285. av_log(s->avctx, AV_LOG_ERROR,
  286. "VMD video: offset > width (%d > %d)\n",
  287. ofs, frame_width);
  288. return AVERROR_INVALIDDATA;
  289. }
  290. dp += frame->linesize[0];
  291. pp += s->prev_frame->linesize[0];
  292. }
  293. break;
  294. case 2:
  295. for (i = 0; i < frame_height; i++) {
  296. bytestream2_get_buffer(&gb, dp, frame_width);
  297. dp += frame->linesize[0];
  298. pp += s->prev_frame->linesize[0];
  299. }
  300. break;
  301. case 3:
  302. for (i = 0; i < frame_height; i++) {
  303. ofs = 0;
  304. do {
  305. len = bytestream2_get_byte(&gb);
  306. if (len & 0x80) {
  307. len = (len & 0x7F) + 1;
  308. if (bytestream2_peek_byte(&gb) == 0xFF) {
  309. int slen = len;
  310. bytestream2_get_byte(&gb);
  311. len = rle_unpack(gb.buffer, &dp[ofs],
  312. len, bytestream2_get_bytes_left(&gb),
  313. frame_width - ofs);
  314. ofs += slen;
  315. bytestream2_skip(&gb, len);
  316. } else {
  317. bytestream2_get_buffer(&gb, &dp[ofs], len);
  318. ofs += len;
  319. }
  320. } else {
  321. /* interframe pixel copy */
  322. if (ofs + len + 1 > frame_width || !s->prev_frame->data[0])
  323. return AVERROR_INVALIDDATA;
  324. memcpy(&dp[ofs], &pp[ofs], len + 1);
  325. ofs += len + 1;
  326. }
  327. } while (ofs < frame_width);
  328. if (ofs > frame_width) {
  329. av_log(s->avctx, AV_LOG_ERROR,
  330. "VMD video: offset > width (%d > %d)\n",
  331. ofs, frame_width);
  332. return AVERROR_INVALIDDATA;
  333. }
  334. dp += frame->linesize[0];
  335. pp += s->prev_frame->linesize[0];
  336. }
  337. break;
  338. }
  339. return 0;
  340. }
  341. static av_cold int vmdvideo_decode_end(AVCodecContext *avctx)
  342. {
  343. VmdVideoContext *s = avctx->priv_data;
  344. av_frame_free(&s->prev_frame);
  345. av_free(s->unpack_buffer);
  346. return 0;
  347. }
  348. static av_cold int vmdvideo_decode_init(AVCodecContext *avctx)
  349. {
  350. VmdVideoContext *s = avctx->priv_data;
  351. int i;
  352. unsigned int *palette32;
  353. int palette_index = 0;
  354. unsigned char r, g, b;
  355. unsigned char *vmd_header;
  356. unsigned char *raw_palette;
  357. s->avctx = avctx;
  358. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  359. /* make sure the VMD header made it */
  360. if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
  361. av_log(s->avctx, AV_LOG_ERROR, "VMD video: expected extradata size of %d\n",
  362. VMD_HEADER_SIZE);
  363. return -1;
  364. }
  365. vmd_header = (unsigned char *)avctx->extradata;
  366. s->unpack_buffer_size = AV_RL32(&vmd_header[800]);
  367. if (s->unpack_buffer_size) {
  368. s->unpack_buffer = av_malloc(s->unpack_buffer_size);
  369. if (!s->unpack_buffer)
  370. return AVERROR(ENOMEM);
  371. }
  372. /* load up the initial palette */
  373. raw_palette = &vmd_header[28];
  374. palette32 = (unsigned int *)s->palette;
  375. for (i = 0; i < PALETTE_COUNT; i++) {
  376. r = raw_palette[palette_index++] * 4;
  377. g = raw_palette[palette_index++] * 4;
  378. b = raw_palette[palette_index++] * 4;
  379. palette32[i] = (r << 16) | (g << 8) | (b);
  380. }
  381. s->prev_frame = av_frame_alloc();
  382. if (!s->prev_frame) {
  383. vmdvideo_decode_end(avctx);
  384. return AVERROR(ENOMEM);
  385. }
  386. return 0;
  387. }
  388. static int vmdvideo_decode_frame(AVCodecContext *avctx,
  389. void *data, int *got_frame,
  390. AVPacket *avpkt)
  391. {
  392. const uint8_t *buf = avpkt->data;
  393. int buf_size = avpkt->size;
  394. VmdVideoContext *s = avctx->priv_data;
  395. AVFrame *frame = data;
  396. int ret;
  397. s->buf = buf;
  398. s->size = buf_size;
  399. if (buf_size < 16)
  400. return AVERROR_INVALIDDATA;
  401. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
  402. av_log(s->avctx, AV_LOG_ERROR, "VMD Video: get_buffer() failed\n");
  403. return ret;
  404. }
  405. if ((ret = vmd_decode(s, frame)) < 0)
  406. return ret;
  407. /* make the palette available on the way out */
  408. memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
  409. /* shuffle frames */
  410. av_frame_unref(s->prev_frame);
  411. if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
  412. return ret;
  413. *got_frame = 1;
  414. /* report that the buffer was completely consumed */
  415. return buf_size;
  416. }
  417. /*
  418. * Audio Decoder
  419. */
  420. #define BLOCK_TYPE_AUDIO 1
  421. #define BLOCK_TYPE_INITIAL 2
  422. #define BLOCK_TYPE_SILENCE 3
  423. typedef struct VmdAudioContext {
  424. int out_bps;
  425. int chunk_size;
  426. } VmdAudioContext;
  427. static const uint16_t vmdaudio_table[128] = {
  428. 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
  429. 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
  430. 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
  431. 0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230,
  432. 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280,
  433. 0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0,
  434. 0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320,
  435. 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370,
  436. 0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0,
  437. 0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480,
  438. 0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700,
  439. 0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00,
  440. 0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000
  441. };
  442. static av_cold int vmdaudio_decode_init(AVCodecContext *avctx)
  443. {
  444. VmdAudioContext *s = avctx->priv_data;
  445. if (avctx->channels < 1 || avctx->channels > 2) {
  446. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  447. return AVERROR(EINVAL);
  448. }
  449. if (avctx->block_align < 1) {
  450. av_log(avctx, AV_LOG_ERROR, "invalid block align\n");
  451. return AVERROR(EINVAL);
  452. }
  453. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
  454. AV_CH_LAYOUT_STEREO;
  455. if (avctx->bits_per_coded_sample == 16)
  456. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  457. else
  458. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  459. s->out_bps = av_get_bytes_per_sample(avctx->sample_fmt);
  460. s->chunk_size = avctx->block_align + avctx->channels * (s->out_bps == 2);
  461. av_log(avctx, AV_LOG_DEBUG, "%d channels, %d bits/sample, "
  462. "block align = %d, sample rate = %d\n",
  463. avctx->channels, avctx->bits_per_coded_sample, avctx->block_align,
  464. avctx->sample_rate);
  465. return 0;
  466. }
  467. static void decode_audio_s16(int16_t *out, const uint8_t *buf, int buf_size,
  468. int channels)
  469. {
  470. int ch;
  471. const uint8_t *buf_end = buf + buf_size;
  472. int predictor[2];
  473. int st = channels - 1;
  474. /* decode initial raw sample */
  475. for (ch = 0; ch < channels; ch++) {
  476. predictor[ch] = (int16_t)AV_RL16(buf);
  477. buf += 2;
  478. *out++ = predictor[ch];
  479. }
  480. /* decode DPCM samples */
  481. ch = 0;
  482. while (buf < buf_end) {
  483. uint8_t b = *buf++;
  484. if (b & 0x80)
  485. predictor[ch] -= vmdaudio_table[b & 0x7F];
  486. else
  487. predictor[ch] += vmdaudio_table[b];
  488. predictor[ch] = av_clip_int16(predictor[ch]);
  489. *out++ = predictor[ch];
  490. ch ^= st;
  491. }
  492. }
  493. static int vmdaudio_decode_frame(AVCodecContext *avctx, void *data,
  494. int *got_frame_ptr, AVPacket *avpkt)
  495. {
  496. AVFrame *frame = data;
  497. const uint8_t *buf = avpkt->data;
  498. const uint8_t *buf_end;
  499. int buf_size = avpkt->size;
  500. VmdAudioContext *s = avctx->priv_data;
  501. int block_type, silent_chunks, audio_chunks;
  502. int ret;
  503. uint8_t *output_samples_u8;
  504. int16_t *output_samples_s16;
  505. if (buf_size < 16) {
  506. av_log(avctx, AV_LOG_WARNING, "skipping small junk packet\n");
  507. *got_frame_ptr = 0;
  508. return buf_size;
  509. }
  510. block_type = buf[6];
  511. if (block_type < BLOCK_TYPE_AUDIO || block_type > BLOCK_TYPE_SILENCE) {
  512. av_log(avctx, AV_LOG_ERROR, "unknown block type: %d\n", block_type);
  513. return AVERROR(EINVAL);
  514. }
  515. buf += 16;
  516. buf_size -= 16;
  517. /* get number of silent chunks */
  518. silent_chunks = 0;
  519. if (block_type == BLOCK_TYPE_INITIAL) {
  520. uint32_t flags;
  521. if (buf_size < 4) {
  522. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  523. return AVERROR(EINVAL);
  524. }
  525. flags = AV_RB32(buf);
  526. silent_chunks = av_popcount(flags);
  527. buf += 4;
  528. buf_size -= 4;
  529. } else if (block_type == BLOCK_TYPE_SILENCE) {
  530. silent_chunks = 1;
  531. buf_size = 0; // should already be zero but set it just to be sure
  532. }
  533. /* ensure output buffer is large enough */
  534. audio_chunks = buf_size / s->chunk_size;
  535. /* drop incomplete chunks */
  536. buf_size = audio_chunks * s->chunk_size;
  537. /* get output buffer */
  538. frame->nb_samples = ((silent_chunks + audio_chunks) * avctx->block_align) /
  539. avctx->channels;
  540. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  541. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  542. return ret;
  543. }
  544. output_samples_u8 = frame->data[0];
  545. output_samples_s16 = (int16_t *)frame->data[0];
  546. /* decode silent chunks */
  547. if (silent_chunks > 0) {
  548. int silent_size = FFMIN(avctx->block_align * silent_chunks,
  549. frame->nb_samples * avctx->channels);
  550. if (s->out_bps == 2) {
  551. memset(output_samples_s16, 0x00, silent_size * 2);
  552. output_samples_s16 += silent_size;
  553. } else {
  554. memset(output_samples_u8, 0x80, silent_size);
  555. output_samples_u8 += silent_size;
  556. }
  557. }
  558. /* decode audio chunks */
  559. if (audio_chunks > 0) {
  560. buf_end = buf + (buf_size & ~(avctx->channels > 1));
  561. while (buf + s->chunk_size <= buf_end) {
  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. };