vmdav.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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_init(AVCodecContext *avctx)
  343. {
  344. VmdVideoContext *s = avctx->priv_data;
  345. int i;
  346. unsigned int *palette32;
  347. int palette_index = 0;
  348. unsigned char r, g, b;
  349. unsigned char *vmd_header;
  350. unsigned char *raw_palette;
  351. s->avctx = avctx;
  352. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  353. /* make sure the VMD header made it */
  354. if (s->avctx->extradata_size != VMD_HEADER_SIZE) {
  355. av_log(s->avctx, AV_LOG_ERROR, "expected extradata size of %d\n",
  356. VMD_HEADER_SIZE);
  357. return AVERROR_INVALIDDATA;
  358. }
  359. vmd_header = (unsigned char *)avctx->extradata;
  360. s->unpack_buffer_size = AV_RL32(&vmd_header[800]);
  361. if (s->unpack_buffer_size) {
  362. s->unpack_buffer = av_malloc(s->unpack_buffer_size);
  363. if (!s->unpack_buffer)
  364. return AVERROR(ENOMEM);
  365. }
  366. /* load up the initial palette */
  367. raw_palette = &vmd_header[28];
  368. palette32 = (unsigned int *)s->palette;
  369. for (i = 0; i < PALETTE_COUNT; i++) {
  370. r = raw_palette[palette_index++] * 4;
  371. g = raw_palette[palette_index++] * 4;
  372. b = raw_palette[palette_index++] * 4;
  373. palette32[i] = 0xFFU << 24 | (r << 16) | (g << 8) | (b);
  374. palette32[i] |= palette32[i] >> 6 & 0x30303;
  375. }
  376. avcodec_get_frame_defaults(&s->prev_frame);
  377. return 0;
  378. }
  379. static int vmdvideo_decode_frame(AVCodecContext *avctx,
  380. void *data, int *got_frame,
  381. AVPacket *avpkt)
  382. {
  383. const uint8_t *buf = avpkt->data;
  384. int buf_size = avpkt->size;
  385. VmdVideoContext *s = avctx->priv_data;
  386. AVFrame *frame = data;
  387. int ret;
  388. s->buf = buf;
  389. s->size = buf_size;
  390. if (buf_size < 16)
  391. return AVERROR_INVALIDDATA;
  392. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  393. return ret;
  394. if ((ret = vmd_decode(s, frame)) < 0)
  395. return ret;
  396. /* make the palette available on the way out */
  397. memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
  398. /* shuffle frames */
  399. av_frame_unref(&s->prev_frame);
  400. if ((ret = av_frame_ref(&s->prev_frame, frame)) < 0)
  401. return ret;
  402. *got_frame = 1;
  403. /* report that the buffer was completely consumed */
  404. return buf_size;
  405. }
  406. static av_cold int vmdvideo_decode_end(AVCodecContext *avctx)
  407. {
  408. VmdVideoContext *s = avctx->priv_data;
  409. av_frame_unref(&s->prev_frame);
  410. av_free(s->unpack_buffer);
  411. return 0;
  412. }
  413. /*
  414. * Audio Decoder
  415. */
  416. #define BLOCK_TYPE_AUDIO 1
  417. #define BLOCK_TYPE_INITIAL 2
  418. #define BLOCK_TYPE_SILENCE 3
  419. typedef struct VmdAudioContext {
  420. int out_bps;
  421. int chunk_size;
  422. } VmdAudioContext;
  423. static const uint16_t vmdaudio_table[128] = {
  424. 0x000, 0x008, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070, 0x080,
  425. 0x090, 0x0A0, 0x0B0, 0x0C0, 0x0D0, 0x0E0, 0x0F0, 0x100, 0x110, 0x120,
  426. 0x130, 0x140, 0x150, 0x160, 0x170, 0x180, 0x190, 0x1A0, 0x1B0, 0x1C0,
  427. 0x1D0, 0x1E0, 0x1F0, 0x200, 0x208, 0x210, 0x218, 0x220, 0x228, 0x230,
  428. 0x238, 0x240, 0x248, 0x250, 0x258, 0x260, 0x268, 0x270, 0x278, 0x280,
  429. 0x288, 0x290, 0x298, 0x2A0, 0x2A8, 0x2B0, 0x2B8, 0x2C0, 0x2C8, 0x2D0,
  430. 0x2D8, 0x2E0, 0x2E8, 0x2F0, 0x2F8, 0x300, 0x308, 0x310, 0x318, 0x320,
  431. 0x328, 0x330, 0x338, 0x340, 0x348, 0x350, 0x358, 0x360, 0x368, 0x370,
  432. 0x378, 0x380, 0x388, 0x390, 0x398, 0x3A0, 0x3A8, 0x3B0, 0x3B8, 0x3C0,
  433. 0x3C8, 0x3D0, 0x3D8, 0x3E0, 0x3E8, 0x3F0, 0x3F8, 0x400, 0x440, 0x480,
  434. 0x4C0, 0x500, 0x540, 0x580, 0x5C0, 0x600, 0x640, 0x680, 0x6C0, 0x700,
  435. 0x740, 0x780, 0x7C0, 0x800, 0x900, 0xA00, 0xB00, 0xC00, 0xD00, 0xE00,
  436. 0xF00, 0x1000, 0x1400, 0x1800, 0x1C00, 0x2000, 0x3000, 0x4000
  437. };
  438. static av_cold int vmdaudio_decode_init(AVCodecContext *avctx)
  439. {
  440. VmdAudioContext *s = avctx->priv_data;
  441. if (avctx->channels < 1 || avctx->channels > 2) {
  442. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  443. return AVERROR(EINVAL);
  444. }
  445. if (avctx->block_align < 1 || avctx->block_align % avctx->channels) {
  446. av_log(avctx, AV_LOG_ERROR, "invalid block align\n");
  447. return AVERROR(EINVAL);
  448. }
  449. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
  450. AV_CH_LAYOUT_STEREO;
  451. if (avctx->bits_per_coded_sample == 16)
  452. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  453. else
  454. avctx->sample_fmt = AV_SAMPLE_FMT_U8;
  455. s->out_bps = av_get_bytes_per_sample(avctx->sample_fmt);
  456. s->chunk_size = avctx->block_align + avctx->channels * (s->out_bps == 2);
  457. av_log(avctx, AV_LOG_DEBUG, "%d channels, %d bits/sample, "
  458. "block align = %d, sample rate = %d\n",
  459. avctx->channels, avctx->bits_per_coded_sample, avctx->block_align,
  460. avctx->sample_rate);
  461. return 0;
  462. }
  463. static void decode_audio_s16(int16_t *out, const uint8_t *buf, int buf_size,
  464. int channels)
  465. {
  466. int ch;
  467. const uint8_t *buf_end = buf + buf_size;
  468. int predictor[2];
  469. int st = channels - 1;
  470. /* decode initial raw sample */
  471. for (ch = 0; ch < channels; ch++) {
  472. predictor[ch] = (int16_t)AV_RL16(buf);
  473. buf += 2;
  474. *out++ = predictor[ch];
  475. }
  476. /* decode DPCM samples */
  477. ch = 0;
  478. while (buf < buf_end) {
  479. uint8_t b = *buf++;
  480. if (b & 0x80)
  481. predictor[ch] -= vmdaudio_table[b & 0x7F];
  482. else
  483. predictor[ch] += vmdaudio_table[b];
  484. predictor[ch] = av_clip_int16(predictor[ch]);
  485. *out++ = predictor[ch];
  486. ch ^= st;
  487. }
  488. }
  489. static int vmdaudio_decode_frame(AVCodecContext *avctx, void *data,
  490. int *got_frame_ptr, AVPacket *avpkt)
  491. {
  492. AVFrame *frame = data;
  493. const uint8_t *buf = avpkt->data;
  494. const uint8_t *buf_end;
  495. int buf_size = avpkt->size;
  496. VmdAudioContext *s = avctx->priv_data;
  497. int block_type, silent_chunks, audio_chunks;
  498. int ret;
  499. uint8_t *output_samples_u8;
  500. int16_t *output_samples_s16;
  501. if (buf_size < 16) {
  502. av_log(avctx, AV_LOG_WARNING, "skipping small junk packet\n");
  503. *got_frame_ptr = 0;
  504. return buf_size;
  505. }
  506. block_type = buf[6];
  507. if (block_type < BLOCK_TYPE_AUDIO || block_type > BLOCK_TYPE_SILENCE) {
  508. av_log(avctx, AV_LOG_ERROR, "unknown block type: %d\n", block_type);
  509. return AVERROR(EINVAL);
  510. }
  511. buf += 16;
  512. buf_size -= 16;
  513. /* get number of silent chunks */
  514. silent_chunks = 0;
  515. if (block_type == BLOCK_TYPE_INITIAL) {
  516. uint32_t flags;
  517. if (buf_size < 4) {
  518. av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
  519. return AVERROR(EINVAL);
  520. }
  521. flags = AV_RB32(buf);
  522. silent_chunks = av_popcount(flags);
  523. buf += 4;
  524. buf_size -= 4;
  525. } else if (block_type == BLOCK_TYPE_SILENCE) {
  526. silent_chunks = 1;
  527. buf_size = 0; // should already be zero but set it just to be sure
  528. }
  529. /* ensure output buffer is large enough */
  530. audio_chunks = buf_size / s->chunk_size;
  531. /* drop incomplete chunks */
  532. buf_size = audio_chunks * s->chunk_size;
  533. /* get output buffer */
  534. frame->nb_samples = ((silent_chunks + audio_chunks) * avctx->block_align) /
  535. avctx->channels;
  536. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  537. return ret;
  538. output_samples_u8 = frame->data[0];
  539. output_samples_s16 = (int16_t *)frame->data[0];
  540. /* decode silent chunks */
  541. if (silent_chunks > 0) {
  542. int silent_size = avctx->block_align * silent_chunks;
  543. av_assert0(avctx->block_align * silent_chunks <= frame->nb_samples * avctx->channels);
  544. if (s->out_bps == 2) {
  545. memset(output_samples_s16, 0x00, silent_size * 2);
  546. output_samples_s16 += silent_size;
  547. } else {
  548. memset(output_samples_u8, 0x80, silent_size);
  549. output_samples_u8 += silent_size;
  550. }
  551. }
  552. /* decode audio chunks */
  553. if (audio_chunks > 0) {
  554. buf_end = buf + buf_size;
  555. av_assert0((buf_size & (avctx->channels > 1)) == 0);
  556. while (buf_end - buf >= s->chunk_size) {
  557. if (s->out_bps == 2) {
  558. decode_audio_s16(output_samples_s16, buf, s->chunk_size,
  559. avctx->channels);
  560. output_samples_s16 += avctx->block_align;
  561. } else {
  562. memcpy(output_samples_u8, buf, s->chunk_size);
  563. output_samples_u8 += avctx->block_align;
  564. }
  565. buf += s->chunk_size;
  566. }
  567. }
  568. *got_frame_ptr = 1;
  569. return avpkt->size;
  570. }
  571. /*
  572. * Public Data Structures
  573. */
  574. AVCodec ff_vmdvideo_decoder = {
  575. .name = "vmdvideo",
  576. .type = AVMEDIA_TYPE_VIDEO,
  577. .id = AV_CODEC_ID_VMDVIDEO,
  578. .priv_data_size = sizeof(VmdVideoContext),
  579. .init = vmdvideo_decode_init,
  580. .close = vmdvideo_decode_end,
  581. .decode = vmdvideo_decode_frame,
  582. .capabilities = CODEC_CAP_DR1,
  583. .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD video"),
  584. };
  585. AVCodec ff_vmdaudio_decoder = {
  586. .name = "vmdaudio",
  587. .type = AVMEDIA_TYPE_AUDIO,
  588. .id = AV_CODEC_ID_VMDAUDIO,
  589. .priv_data_size = sizeof(VmdAudioContext),
  590. .init = vmdaudio_decode_init,
  591. .decode = vmdaudio_decode_frame,
  592. .capabilities = CODEC_CAP_DR1,
  593. .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD audio"),
  594. };