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.

722 lines
21KB

  1. /*
  2. * Interplay MVE Video Decoder
  3. * Copyright (C) 2003 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 libavcodec/interplayvideo.c
  23. * Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
  24. * For more information about the Interplay MVE format, visit:
  25. * http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
  26. * This code is written in such a way that the identifiers match up
  27. * with the encoding descriptions in the document.
  28. *
  29. * This decoder presently only supports a PAL8 output colorspace.
  30. *
  31. * An Interplay video frame consists of 2 parts: The decoding map and
  32. * the video data. A demuxer must load these 2 parts together in a single
  33. * buffer before sending it through the stream to this decoder.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "avcodec.h"
  39. #include "bytestream.h"
  40. #include "dsputil.h"
  41. #define ALT_BITSTREAM_READER_LE
  42. #include "get_bits.h"
  43. #define PALETTE_COUNT 256
  44. /* debugging support */
  45. #define DEBUG_INTERPLAY 0
  46. #if DEBUG_INTERPLAY
  47. #define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
  48. #else
  49. static inline void debug_interplay(const char *format, ...) { }
  50. #endif
  51. typedef struct IpvideoContext {
  52. AVCodecContext *avctx;
  53. DSPContext dsp;
  54. AVFrame second_last_frame;
  55. AVFrame last_frame;
  56. AVFrame current_frame;
  57. const unsigned char *decoding_map;
  58. int decoding_map_size;
  59. const unsigned char *buf;
  60. int size;
  61. int is_16bpp;
  62. const unsigned char *stream_ptr;
  63. const unsigned char *stream_end;
  64. unsigned char *pixel_ptr;
  65. int line_inc;
  66. int stride;
  67. int upper_motion_limit_offset;
  68. } IpvideoContext;
  69. #define CHECK_STREAM_PTR(stream_ptr, stream_end, n) \
  70. if (stream_end - stream_ptr < n) { \
  71. av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
  72. stream_ptr + n, stream_end); \
  73. return -1; \
  74. }
  75. static int copy_from(IpvideoContext *s, AVFrame *src, int delta_x, int delta_y)
  76. {
  77. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  78. int motion_offset = current_offset + delta_y * s->current_frame.linesize[0]
  79. + delta_x * (1 + s->is_16bpp);
  80. if (motion_offset < 0) {
  81. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset);
  82. return -1;
  83. } else if (motion_offset > s->upper_motion_limit_offset) {
  84. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n",
  85. motion_offset, s->upper_motion_limit_offset);
  86. return -1;
  87. }
  88. s->dsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
  89. s->current_frame.linesize[0], 8);
  90. return 0;
  91. }
  92. static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
  93. {
  94. return copy_from(s, &s->last_frame, 0, 0);
  95. }
  96. static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
  97. {
  98. return copy_from(s, &s->second_last_frame, 0, 0);
  99. }
  100. static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
  101. {
  102. unsigned char B;
  103. int x, y;
  104. /* copy block from 2 frames ago using a motion vector; need 1 more byte */
  105. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
  106. B = *s->stream_ptr++;
  107. if (B < 56) {
  108. x = 8 + (B % 7);
  109. y = B / 7;
  110. } else {
  111. x = -14 + ((B - 56) % 29);
  112. y = 8 + ((B - 56) / 29);
  113. }
  114. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  115. return copy_from(s, &s->second_last_frame, x, y);
  116. }
  117. static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
  118. {
  119. unsigned char B;
  120. int x, y;
  121. /* copy 8x8 block from current frame from an up/left block */
  122. /* need 1 more byte for motion */
  123. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
  124. B = *s->stream_ptr++;
  125. if (B < 56) {
  126. x = -(8 + (B % 7));
  127. y = -(B / 7);
  128. } else {
  129. x = -(-14 + ((B - 56) % 29));
  130. y = -( 8 + ((B - 56) / 29));
  131. }
  132. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  133. return copy_from(s, &s->current_frame, x, y);
  134. }
  135. static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
  136. {
  137. int x, y;
  138. unsigned char B, BL, BH;
  139. /* copy a block from the previous frame; need 1 more byte */
  140. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
  141. B = *s->stream_ptr++;
  142. BL = B & 0x0F;
  143. BH = (B >> 4) & 0x0F;
  144. x = -8 + BL;
  145. y = -8 + BH;
  146. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  147. return copy_from(s, &s->last_frame, x, y);
  148. }
  149. static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
  150. {
  151. signed char x, y;
  152. /* copy a block from the previous frame using an expanded range;
  153. * need 2 more bytes */
  154. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
  155. x = *s->stream_ptr++;
  156. y = *s->stream_ptr++;
  157. debug_interplay (" motion bytes = %d, %d\n", x, y);
  158. return copy_from(s, &s->last_frame, x, y);
  159. }
  160. static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
  161. {
  162. /* mystery opcode? skip multiple blocks? */
  163. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: Help! Mystery opcode 0x6 seen\n");
  164. /* report success */
  165. return 0;
  166. }
  167. static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
  168. {
  169. int x, y;
  170. unsigned char P[2];
  171. unsigned int flags;
  172. /* 2-color encoding */
  173. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
  174. P[0] = *s->stream_ptr++;
  175. P[1] = *s->stream_ptr++;
  176. if (P[0] <= P[1]) {
  177. /* need 8 more bytes from the stream */
  178. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
  179. for (y = 0; y < 8; y++) {
  180. flags = *s->stream_ptr++ | 0x100;
  181. for (; flags != 1; flags >>= 1)
  182. *s->pixel_ptr++ = P[flags & 1];
  183. s->pixel_ptr += s->line_inc;
  184. }
  185. } else {
  186. /* need 2 more bytes from the stream */
  187. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
  188. flags = bytestream_get_le16(&s->stream_ptr);
  189. for (y = 0; y < 8; y += 2) {
  190. for (x = 0; x < 8; x += 2, flags >>= 1) {
  191. s->pixel_ptr[x ] =
  192. s->pixel_ptr[x + 1 ] =
  193. s->pixel_ptr[x + s->stride] =
  194. s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
  195. }
  196. s->pixel_ptr += s->stride * 2;
  197. }
  198. }
  199. /* report success */
  200. return 0;
  201. }
  202. static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
  203. {
  204. int x, y;
  205. unsigned char P[2];
  206. unsigned int flags = 0;
  207. /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
  208. * either top and bottom or left and right halves */
  209. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
  210. P[0] = *s->stream_ptr++;
  211. P[1] = *s->stream_ptr++;
  212. if (P[0] <= P[1]) {
  213. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 14);
  214. s->stream_ptr -= 2;
  215. for (y = 0; y < 16; y++) {
  216. // new values for each 4x4 block
  217. if (!(y & 3)) {
  218. P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
  219. flags = bytestream_get_le16(&s->stream_ptr);
  220. }
  221. for (x = 0; x < 4; x++, flags >>= 1)
  222. *s->pixel_ptr++ = P[flags & 1];
  223. s->pixel_ptr += s->stride - 4;
  224. // switch to right half
  225. if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
  226. }
  227. } else {
  228. /* need 10 more bytes */
  229. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 10);
  230. if (s->stream_ptr[4] <= s->stream_ptr[5]) {
  231. flags = bytestream_get_le32(&s->stream_ptr);
  232. /* vertical split; left & right halves are 2-color encoded */
  233. for (y = 0; y < 16; y++) {
  234. for (x = 0; x < 4; x++, flags >>= 1)
  235. *s->pixel_ptr++ = P[flags & 1];
  236. s->pixel_ptr += s->stride - 4;
  237. // switch to right half
  238. if (y == 7) {
  239. s->pixel_ptr -= 8 * s->stride - 4;
  240. P[0] = *s->stream_ptr++; P[1] = *s->stream_ptr++;
  241. flags = bytestream_get_le32(&s->stream_ptr);
  242. }
  243. }
  244. } else {
  245. /* horizontal split; top & bottom halves are 2-color encoded */
  246. for (y = 0; y < 8; y++) {
  247. if (y == 4) {
  248. P[0] = *s->stream_ptr++;
  249. P[1] = *s->stream_ptr++;
  250. }
  251. flags = *s->stream_ptr++ | 0x100;
  252. for (; flags != 1; flags >>= 1)
  253. *s->pixel_ptr++ = P[flags & 1];
  254. s->pixel_ptr += s->line_inc;
  255. }
  256. }
  257. }
  258. /* report success */
  259. return 0;
  260. }
  261. static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
  262. {
  263. int x, y;
  264. unsigned char P[4];
  265. /* 4-color encoding */
  266. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
  267. memcpy(P, s->stream_ptr, 4);
  268. s->stream_ptr += 4;
  269. if (P[0] <= P[1]) {
  270. if (P[2] <= P[3]) {
  271. /* 1 of 4 colors for each pixel, need 16 more bytes */
  272. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
  273. for (y = 0; y < 8; y++) {
  274. /* get the next set of 8 2-bit flags */
  275. int flags = bytestream_get_le16(&s->stream_ptr);
  276. for (x = 0; x < 8; x++, flags >>= 2)
  277. *s->pixel_ptr++ = P[flags & 0x03];
  278. s->pixel_ptr += s->line_inc;
  279. }
  280. } else {
  281. uint32_t flags;
  282. /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
  283. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
  284. flags = bytestream_get_le32(&s->stream_ptr);
  285. for (y = 0; y < 8; y += 2) {
  286. for (x = 0; x < 8; x += 2, flags >>= 2) {
  287. s->pixel_ptr[x ] =
  288. s->pixel_ptr[x + 1 ] =
  289. s->pixel_ptr[x + s->stride] =
  290. s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
  291. }
  292. s->pixel_ptr += s->stride * 2;
  293. }
  294. }
  295. } else {
  296. uint64_t flags;
  297. /* 1 of 4 colors for each 2x1 or 1x2 block, need 8 more bytes */
  298. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
  299. flags = bytestream_get_le64(&s->stream_ptr);
  300. if (P[2] <= P[3]) {
  301. for (y = 0; y < 8; y++) {
  302. for (x = 0; x < 8; x += 2, flags >>= 2) {
  303. s->pixel_ptr[x ] =
  304. s->pixel_ptr[x + 1] = P[flags & 0x03];
  305. }
  306. s->pixel_ptr += s->stride;
  307. }
  308. } else {
  309. for (y = 0; y < 8; y += 2) {
  310. for (x = 0; x < 8; x++, flags >>= 2) {
  311. s->pixel_ptr[x ] =
  312. s->pixel_ptr[x + s->stride] = P[flags & 0x03];
  313. }
  314. s->pixel_ptr += s->stride * 2;
  315. }
  316. }
  317. }
  318. /* report success */
  319. return 0;
  320. }
  321. static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
  322. {
  323. int x, y;
  324. unsigned char P[4];
  325. int flags = 0;
  326. /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
  327. * either top and bottom or left and right halves */
  328. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 24);
  329. if (s->stream_ptr[0] <= s->stream_ptr[1]) {
  330. /* 4-color encoding for each quadrant; need 32 bytes */
  331. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 32);
  332. for (y = 0; y < 16; y++) {
  333. // new values for each 4x4 block
  334. if (!(y & 3)) {
  335. memcpy(P, s->stream_ptr, 4);
  336. s->stream_ptr += 4;
  337. flags = bytestream_get_le32(&s->stream_ptr);
  338. }
  339. for (x = 0; x < 4; x++, flags >>= 2)
  340. *s->pixel_ptr++ = P[flags & 0x03];
  341. s->pixel_ptr += s->stride - 4;
  342. // switch to right half
  343. if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
  344. }
  345. } else {
  346. // vertical split?
  347. int vert = s->stream_ptr[12] <= s->stream_ptr[13];
  348. uint64_t flags = 0;
  349. /* 4-color encoding for either left and right or top and bottom
  350. * halves */
  351. for (y = 0; y < 16; y++) {
  352. // load values for each half
  353. if (!(y & 7)) {
  354. memcpy(P, s->stream_ptr, 4);
  355. s->stream_ptr += 4;
  356. flags = bytestream_get_le64(&s->stream_ptr);
  357. }
  358. for (x = 0; x < 4; x++, flags >>= 2)
  359. *s->pixel_ptr++ = P[flags & 0x03];
  360. if (vert) {
  361. s->pixel_ptr += s->stride - 4;
  362. // switch to right half
  363. if (y == 7) s->pixel_ptr -= 8 * s->stride - 4;
  364. } else if (y & 1) s->pixel_ptr += s->line_inc;
  365. }
  366. }
  367. /* report success */
  368. return 0;
  369. }
  370. static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
  371. {
  372. int y;
  373. /* 64-color encoding (each pixel in block is a different color) */
  374. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 64);
  375. for (y = 0; y < 8; y++) {
  376. memcpy(s->pixel_ptr, s->stream_ptr, 8);
  377. s->stream_ptr += 8;
  378. s->pixel_ptr += s->stride;
  379. }
  380. /* report success */
  381. return 0;
  382. }
  383. static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
  384. {
  385. int x, y;
  386. /* 16-color block encoding: each 2x2 block is a different color */
  387. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 16);
  388. for (y = 0; y < 8; y += 2) {
  389. for (x = 0; x < 8; x += 2) {
  390. s->pixel_ptr[x ] =
  391. s->pixel_ptr[x + 1 ] =
  392. s->pixel_ptr[x + s->stride] =
  393. s->pixel_ptr[x + 1 + s->stride] = *s->stream_ptr++;
  394. }
  395. s->pixel_ptr += s->stride * 2;
  396. }
  397. /* report success */
  398. return 0;
  399. }
  400. static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
  401. {
  402. int y;
  403. unsigned char P[2];
  404. /* 4-color block encoding: each 4x4 block is a different color */
  405. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 4);
  406. for (y = 0; y < 8; y++) {
  407. if (!(y & 3)) {
  408. P[0] = *s->stream_ptr++;
  409. P[1] = *s->stream_ptr++;
  410. }
  411. memset(s->pixel_ptr, P[0], 4);
  412. memset(s->pixel_ptr + 4, P[1], 4);
  413. s->pixel_ptr += s->stride;
  414. }
  415. /* report success */
  416. return 0;
  417. }
  418. static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
  419. {
  420. int y;
  421. unsigned char pix;
  422. /* 1-color encoding: the whole block is 1 solid color */
  423. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 1);
  424. pix = *s->stream_ptr++;
  425. for (y = 0; y < 8; y++) {
  426. memset(s->pixel_ptr, pix, 8);
  427. s->pixel_ptr += s->stride;
  428. }
  429. /* report success */
  430. return 0;
  431. }
  432. static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
  433. {
  434. int x, y;
  435. unsigned char sample[2];
  436. /* dithered encoding */
  437. CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 2);
  438. sample[0] = *s->stream_ptr++;
  439. sample[1] = *s->stream_ptr++;
  440. for (y = 0; y < 8; y++) {
  441. for (x = 0; x < 8; x += 2) {
  442. *s->pixel_ptr++ = sample[ y & 1 ];
  443. *s->pixel_ptr++ = sample[!(y & 1)];
  444. }
  445. s->pixel_ptr += s->line_inc;
  446. }
  447. /* report success */
  448. return 0;
  449. }
  450. static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
  451. ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
  452. ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
  453. ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
  454. ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
  455. ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
  456. ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
  457. ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
  458. ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
  459. };
  460. static void ipvideo_decode_opcodes(IpvideoContext *s)
  461. {
  462. int x, y;
  463. unsigned char opcode;
  464. int ret;
  465. static int frame = 0;
  466. GetBitContext gb;
  467. debug_interplay("------------------ frame %d\n", frame);
  468. frame++;
  469. if (!s->is_16bpp) {
  470. /* this is PAL8, so make the palette available */
  471. memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
  472. s->stride = s->current_frame.linesize[0];
  473. s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
  474. s->stream_end = s->buf + s->size;
  475. } else {
  476. s->stride = s->current_frame.linesize[0] >> 1;
  477. }
  478. s->line_inc = s->stride - 8;
  479. s->upper_motion_limit_offset = (s->avctx->height - 8) * s->current_frame.linesize[0]
  480. + (s->avctx->width - 8) * (1 + s->is_16bpp);
  481. init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
  482. for (y = 0; y < s->avctx->height; y += 8) {
  483. for (x = 0; x < s->avctx->width; x += 8) {
  484. opcode = get_bits(&gb, 4);
  485. debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
  486. x, y, opcode, s->stream_ptr);
  487. if (!s->is_16bpp) {
  488. s->pixel_ptr = s->current_frame.data[0] + x
  489. + y*s->current_frame.linesize[0];
  490. ret = ipvideo_decode_block[opcode](s);
  491. } else {
  492. s->pixel_ptr = s->current_frame.data[0] + x*2
  493. + y*s->current_frame.linesize[0];
  494. }
  495. if (ret != 0) {
  496. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
  497. frame, x, y);
  498. return;
  499. }
  500. }
  501. }
  502. if (s->stream_end - s->stream_ptr > 1) {
  503. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
  504. s->stream_end - s->stream_ptr);
  505. }
  506. }
  507. static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
  508. {
  509. IpvideoContext *s = avctx->priv_data;
  510. s->avctx = avctx;
  511. if (s->avctx->palctrl == NULL) {
  512. av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
  513. return -1;
  514. }
  515. s->is_16bpp = avctx->bits_per_coded_sample == 16;
  516. avctx->pix_fmt = s->is_16bpp ? PIX_FMT_RGB555 : PIX_FMT_PAL8;
  517. if (s->is_16bpp) {
  518. av_log(avctx, AV_LOG_ERROR, "16-bit Interplay video is not supported yet.\n");
  519. return -1;
  520. }
  521. dsputil_init(&s->dsp, avctx);
  522. /* decoding map contains 4 bits of information per 8x8 block */
  523. s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
  524. s->current_frame.data[0] = s->last_frame.data[0] =
  525. s->second_last_frame.data[0] = NULL;
  526. return 0;
  527. }
  528. static int ipvideo_decode_frame(AVCodecContext *avctx,
  529. void *data, int *data_size,
  530. AVPacket *avpkt)
  531. {
  532. const uint8_t *buf = avpkt->data;
  533. int buf_size = avpkt->size;
  534. IpvideoContext *s = avctx->priv_data;
  535. AVPaletteControl *palette_control = avctx->palctrl;
  536. /* compressed buffer needs to be large enough to at least hold an entire
  537. * decoding map */
  538. if (buf_size < s->decoding_map_size)
  539. return buf_size;
  540. s->decoding_map = buf;
  541. s->buf = buf + s->decoding_map_size;
  542. s->size = buf_size - s->decoding_map_size;
  543. s->current_frame.reference = 3;
  544. if (avctx->get_buffer(avctx, &s->current_frame)) {
  545. av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
  546. return -1;
  547. }
  548. ipvideo_decode_opcodes(s);
  549. if (!s->is_16bpp && palette_control->palette_changed) {
  550. palette_control->palette_changed = 0;
  551. s->current_frame.palette_has_changed = 1;
  552. }
  553. *data_size = sizeof(AVFrame);
  554. *(AVFrame*)data = s->current_frame;
  555. /* shuffle frames */
  556. if (s->second_last_frame.data[0])
  557. avctx->release_buffer(avctx, &s->second_last_frame);
  558. s->second_last_frame = s->last_frame;
  559. s->last_frame = s->current_frame;
  560. s->current_frame.data[0] = NULL; /* catch any access attempts */
  561. /* report that the buffer was completely consumed */
  562. return buf_size;
  563. }
  564. static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
  565. {
  566. IpvideoContext *s = avctx->priv_data;
  567. /* release the last frame */
  568. if (s->last_frame.data[0])
  569. avctx->release_buffer(avctx, &s->last_frame);
  570. if (s->second_last_frame.data[0])
  571. avctx->release_buffer(avctx, &s->second_last_frame);
  572. return 0;
  573. }
  574. AVCodec interplay_video_decoder = {
  575. "interplayvideo",
  576. CODEC_TYPE_VIDEO,
  577. CODEC_ID_INTERPLAY_VIDEO,
  578. sizeof(IpvideoContext),
  579. ipvideo_decode_init,
  580. NULL,
  581. ipvideo_decode_end,
  582. ipvideo_decode_frame,
  583. CODEC_CAP_DR1,
  584. .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
  585. };