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.

858 lines
25KB

  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 <unistd.h>
  39. #include "avcodec.h"
  40. #include "bytestream.h"
  41. #include "dsputil.h"
  42. #define PALETTE_COUNT 256
  43. /* debugging support */
  44. #define DEBUG_INTERPLAY 0
  45. #if DEBUG_INTERPLAY
  46. #define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
  47. #else
  48. static inline void debug_interplay(const char *format, ...) { }
  49. #endif
  50. typedef struct IpvideoContext {
  51. AVCodecContext *avctx;
  52. DSPContext dsp;
  53. AVFrame second_last_frame;
  54. AVFrame last_frame;
  55. AVFrame current_frame;
  56. const unsigned char *decoding_map;
  57. int decoding_map_size;
  58. const unsigned char *buf;
  59. int size;
  60. const unsigned char *stream_ptr;
  61. const unsigned char *stream_end;
  62. unsigned char *pixel_ptr;
  63. int line_inc;
  64. int stride;
  65. int upper_motion_limit_offset;
  66. } IpvideoContext;
  67. #define CHECK_STREAM_PTR(n) \
  68. if (s->stream_end - s->stream_ptr < n) { \
  69. av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
  70. s->stream_ptr + n, s->stream_end); \
  71. return -1; \
  72. }
  73. static int copy_from(IpvideoContext *s, AVFrame *src, int delta_x, int delta_y)
  74. {
  75. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  76. int motion_offset = current_offset + delta_y * s->stride + delta_x;
  77. if (motion_offset < 0) {
  78. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset);
  79. return -1;
  80. } else if (motion_offset > s->upper_motion_limit_offset) {
  81. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n",
  82. motion_offset, s->upper_motion_limit_offset);
  83. return -1;
  84. }
  85. s->dsp.put_pixels_tab[1][0](s->pixel_ptr, src->data[0] + motion_offset, s->stride, 8);
  86. return 0;
  87. }
  88. static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
  89. {
  90. return copy_from(s, &s->last_frame, 0, 0);
  91. }
  92. static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
  93. {
  94. return copy_from(s, &s->second_last_frame, 0, 0);
  95. }
  96. static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
  97. {
  98. unsigned char B;
  99. int x, y;
  100. /* copy block from 2 frames ago using a motion vector; need 1 more byte */
  101. CHECK_STREAM_PTR(1);
  102. B = *s->stream_ptr++;
  103. if (B < 56) {
  104. x = 8 + (B % 7);
  105. y = B / 7;
  106. } else {
  107. x = -14 + ((B - 56) % 29);
  108. y = 8 + ((B - 56) / 29);
  109. }
  110. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  111. return copy_from(s, &s->second_last_frame, x, y);
  112. }
  113. static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
  114. {
  115. unsigned char B;
  116. int x, y;
  117. /* copy 8x8 block from current frame from an up/left block */
  118. /* need 1 more byte for motion */
  119. CHECK_STREAM_PTR(1);
  120. B = *s->stream_ptr++;
  121. if (B < 56) {
  122. x = -(8 + (B % 7));
  123. y = -(B / 7);
  124. } else {
  125. x = -(-14 + ((B - 56) % 29));
  126. y = -( 8 + ((B - 56) / 29));
  127. }
  128. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  129. return copy_from(s, &s->current_frame, x, y);
  130. }
  131. static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
  132. {
  133. int x, y;
  134. unsigned char B, BL, BH;
  135. /* copy a block from the previous frame; need 1 more byte */
  136. CHECK_STREAM_PTR(1);
  137. B = *s->stream_ptr++;
  138. BL = B & 0x0F;
  139. BH = (B >> 4) & 0x0F;
  140. x = -8 + BL;
  141. y = -8 + BH;
  142. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  143. return copy_from(s, &s->last_frame, x, y);
  144. }
  145. static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
  146. {
  147. signed char x, y;
  148. /* copy a block from the previous frame using an expanded range;
  149. * need 2 more bytes */
  150. CHECK_STREAM_PTR(2);
  151. x = *s->stream_ptr++;
  152. y = *s->stream_ptr++;
  153. debug_interplay (" motion bytes = %d, %d\n", x, y);
  154. return copy_from(s, &s->last_frame, x, y);
  155. }
  156. static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
  157. {
  158. /* mystery opcode? skip multiple blocks? */
  159. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: Help! Mystery opcode 0x6 seen\n");
  160. /* report success */
  161. return 0;
  162. }
  163. static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
  164. {
  165. int x, y;
  166. unsigned char P0, P1;
  167. unsigned int flags;
  168. int bitmask;
  169. /* 2-color encoding */
  170. CHECK_STREAM_PTR(2);
  171. P0 = *s->stream_ptr++;
  172. P1 = *s->stream_ptr++;
  173. if (P0 <= P1) {
  174. /* need 8 more bytes from the stream */
  175. CHECK_STREAM_PTR(8);
  176. for (y = 0; y < 8; y++) {
  177. flags = *s->stream_ptr++;
  178. for (x = 0x01; x <= 0x80; x <<= 1) {
  179. if (flags & x)
  180. *s->pixel_ptr++ = P1;
  181. else
  182. *s->pixel_ptr++ = P0;
  183. }
  184. s->pixel_ptr += s->line_inc;
  185. }
  186. } else {
  187. /* need 2 more bytes from the stream */
  188. CHECK_STREAM_PTR(2);
  189. flags = bytestream_get_le16(&s->stream_ptr);
  190. bitmask = 0x0001;
  191. for (y = 0; y < 8; y += 2) {
  192. for (x = 0; x < 8; x += 2, bitmask <<= 1) {
  193. if (flags & bitmask) {
  194. *(s->pixel_ptr + x) = P1;
  195. *(s->pixel_ptr + x + 1) = P1;
  196. *(s->pixel_ptr + s->stride + x) = P1;
  197. *(s->pixel_ptr + s->stride + x + 1) = P1;
  198. } else {
  199. *(s->pixel_ptr + x) = P0;
  200. *(s->pixel_ptr + x + 1) = P0;
  201. *(s->pixel_ptr + s->stride + x) = P0;
  202. *(s->pixel_ptr + s->stride + x + 1) = P0;
  203. }
  204. }
  205. s->pixel_ptr += s->stride * 2;
  206. }
  207. }
  208. /* report success */
  209. return 0;
  210. }
  211. static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
  212. {
  213. int x, y;
  214. unsigned char P[8];
  215. unsigned char B[8];
  216. unsigned int flags = 0;
  217. unsigned int bitmask = 0;
  218. unsigned char P0 = 0, P1 = 0;
  219. int lower_half = 0;
  220. /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
  221. * either top and bottom or left and right halves */
  222. CHECK_STREAM_PTR(2);
  223. P[0] = *s->stream_ptr++;
  224. P[1] = *s->stream_ptr++;
  225. if (P[0] <= P[1]) {
  226. /* need 12 more bytes */
  227. CHECK_STREAM_PTR(12);
  228. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  229. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  230. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  231. P[4] = *s->stream_ptr++; P[5] = *s->stream_ptr++;
  232. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  233. P[6] = *s->stream_ptr++; P[7] = *s->stream_ptr++;
  234. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  235. for (y = 0; y < 8; y++) {
  236. /* time to reload flags? */
  237. if (y == 0) {
  238. flags =
  239. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  240. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  241. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  242. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  243. bitmask = 0x00000001;
  244. lower_half = 0; /* still on top half */
  245. } else if (y == 4) {
  246. flags =
  247. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  248. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  249. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  250. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  251. bitmask = 0x00000001;
  252. lower_half = 2;
  253. }
  254. for (x = 0; x < 8; x++, bitmask <<= 1) {
  255. /* get the pixel values ready for this quadrant */
  256. if (x == 0) {
  257. P0 = P[lower_half + 0];
  258. P1 = P[lower_half + 1];
  259. } else if (x == 4) {
  260. P0 = P[lower_half + 4];
  261. P1 = P[lower_half + 5];
  262. }
  263. if (flags & bitmask)
  264. *s->pixel_ptr++ = P1;
  265. else
  266. *s->pixel_ptr++ = P0;
  267. }
  268. s->pixel_ptr += s->line_inc;
  269. }
  270. } else {
  271. /* need 10 more bytes */
  272. CHECK_STREAM_PTR(10);
  273. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  274. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  275. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  276. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  277. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  278. if (P[2] <= P[3]) {
  279. /* vertical split; left & right halves are 2-color encoded */
  280. for (y = 0; y < 8; y++) {
  281. /* time to reload flags? */
  282. if (y == 0) {
  283. flags =
  284. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  285. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  286. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  287. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  288. bitmask = 0x00000001;
  289. } else if (y == 4) {
  290. flags =
  291. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  292. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  293. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  294. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  295. bitmask = 0x00000001;
  296. }
  297. for (x = 0; x < 8; x++, bitmask <<= 1) {
  298. /* get the pixel values ready for this half */
  299. if (x == 0) {
  300. P0 = P[0];
  301. P1 = P[1];
  302. } else if (x == 4) {
  303. P0 = P[2];
  304. P1 = P[3];
  305. }
  306. if (flags & bitmask)
  307. *s->pixel_ptr++ = P1;
  308. else
  309. *s->pixel_ptr++ = P0;
  310. }
  311. s->pixel_ptr += s->line_inc;
  312. }
  313. } else {
  314. /* horizontal split; top & bottom halves are 2-color encoded */
  315. for (y = 0; y < 8; y++) {
  316. flags = B[y];
  317. if (y == 0) {
  318. P0 = P[0];
  319. P1 = P[1];
  320. } else if (y == 4) {
  321. P0 = P[2];
  322. P1 = P[3];
  323. }
  324. for (bitmask = 0x01; bitmask <= 0x80; bitmask <<= 1) {
  325. if (flags & bitmask)
  326. *s->pixel_ptr++ = P1;
  327. else
  328. *s->pixel_ptr++ = P0;
  329. }
  330. s->pixel_ptr += s->line_inc;
  331. }
  332. }
  333. }
  334. /* report success */
  335. return 0;
  336. }
  337. static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
  338. {
  339. int x, y;
  340. unsigned char P[4];
  341. unsigned int flags = 0;
  342. int shifter = 0;
  343. unsigned char pix;
  344. /* 4-color encoding */
  345. CHECK_STREAM_PTR(4);
  346. memcpy(P, s->stream_ptr, 4);
  347. s->stream_ptr += 4;
  348. if ((P[0] <= P[1]) && (P[2] <= P[3])) {
  349. /* 1 of 4 colors for each pixel, need 16 more bytes */
  350. CHECK_STREAM_PTR(16);
  351. for (y = 0; y < 8; y++) {
  352. /* get the next set of 8 2-bit flags */
  353. flags = bytestream_get_le16(&s->stream_ptr);
  354. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  355. *s->pixel_ptr++ = P[(flags >> shifter) & 0x03];
  356. }
  357. s->pixel_ptr += s->line_inc;
  358. }
  359. } else if ((P[0] <= P[1]) && (P[2] > P[3])) {
  360. /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
  361. CHECK_STREAM_PTR(4);
  362. flags = bytestream_get_le32(&s->stream_ptr);
  363. shifter = 0;
  364. for (y = 0; y < 8; y += 2) {
  365. for (x = 0; x < 8; x += 2, shifter += 2) {
  366. pix = P[(flags >> shifter) & 0x03];
  367. *(s->pixel_ptr + x) = pix;
  368. *(s->pixel_ptr + x + 1) = pix;
  369. *(s->pixel_ptr + s->stride + x) = pix;
  370. *(s->pixel_ptr + s->stride + x + 1) = pix;
  371. }
  372. s->pixel_ptr += s->stride * 2;
  373. }
  374. } else if ((P[0] > P[1]) && (P[2] <= P[3])) {
  375. /* 1 of 4 colors for each 2x1 block, need 8 more bytes */
  376. CHECK_STREAM_PTR(8);
  377. for (y = 0; y < 8; y++) {
  378. /* time to reload flags? */
  379. if ((y == 0) || (y == 4)) {
  380. flags = bytestream_get_le32(&s->stream_ptr);
  381. shifter = 0;
  382. }
  383. for (x = 0; x < 8; x += 2, shifter += 2) {
  384. pix = P[(flags >> shifter) & 0x03];
  385. *(s->pixel_ptr + x) = pix;
  386. *(s->pixel_ptr + x + 1) = pix;
  387. }
  388. s->pixel_ptr += s->stride;
  389. }
  390. } else {
  391. /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
  392. CHECK_STREAM_PTR(8);
  393. for (y = 0; y < 8; y += 2) {
  394. /* time to reload flags? */
  395. if ((y == 0) || (y == 4)) {
  396. flags = bytestream_get_le32(&s->stream_ptr);
  397. shifter = 0;
  398. }
  399. for (x = 0; x < 8; x++, shifter += 2) {
  400. pix = P[(flags >> shifter) & 0x03];
  401. *(s->pixel_ptr + x) = pix;
  402. *(s->pixel_ptr + s->stride + x) = pix;
  403. }
  404. s->pixel_ptr += s->stride * 2;
  405. }
  406. }
  407. /* report success */
  408. return 0;
  409. }
  410. static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
  411. {
  412. int x, y;
  413. unsigned char P[16];
  414. unsigned char B[16];
  415. int flags = 0;
  416. int shifter = 0;
  417. int index;
  418. int split;
  419. int lower_half;
  420. /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
  421. * either top and bottom or left and right halves */
  422. CHECK_STREAM_PTR(4);
  423. memcpy(P, s->stream_ptr, 4);
  424. s->stream_ptr += 4;
  425. if (P[0] <= P[1]) {
  426. /* 4-color encoding for each quadrant; need 28 more bytes */
  427. CHECK_STREAM_PTR(28);
  428. memcpy(B, s->stream_ptr, 4);
  429. s->stream_ptr += 4;
  430. for (y = 4; y < 16; y += 4) {
  431. memcpy(P + y, s->stream_ptr, 4);
  432. s->stream_ptr += 4;
  433. memcpy(B + y, s->stream_ptr, 4);
  434. s->stream_ptr += 4;
  435. }
  436. for (y = 0; y < 8; y++) {
  437. lower_half = (y >= 4) ? 4 : 0;
  438. flags = (B[y + 8] << 8) | B[y];
  439. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  440. split = (x >= 4) ? 8 : 0;
  441. index = split + lower_half + ((flags >> shifter) & 0x03);
  442. *s->pixel_ptr++ = P[index];
  443. }
  444. s->pixel_ptr += s->line_inc;
  445. }
  446. } else {
  447. /* 4-color encoding for either left and right or top and bottom
  448. * halves; need 20 more bytes */
  449. CHECK_STREAM_PTR(20);
  450. memcpy(B, s->stream_ptr, 8);
  451. s->stream_ptr += 8;
  452. memcpy(P + 4, s->stream_ptr, 4);
  453. s->stream_ptr += 4;
  454. memcpy(B + 8, s->stream_ptr, 8);
  455. s->stream_ptr += 8;
  456. if (P[4] <= P[5]) {
  457. /* block is divided into left and right halves */
  458. for (y = 0; y < 8; y++) {
  459. flags = (B[y + 8] << 8) | B[y];
  460. split = 0;
  461. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  462. if (x == 4)
  463. split = 4;
  464. *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
  465. }
  466. s->pixel_ptr += s->line_inc;
  467. }
  468. } else {
  469. /* block is divided into top and bottom halves */
  470. split = 0;
  471. for (y = 0; y < 8; y++) {
  472. flags = (B[y * 2 + 1] << 8) | B[y * 2];
  473. if (y == 4)
  474. split = 4;
  475. for (x = 0, shifter = 0; x < 8; x++, shifter += 2)
  476. *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
  477. s->pixel_ptr += s->line_inc;
  478. }
  479. }
  480. }
  481. /* report success */
  482. return 0;
  483. }
  484. static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
  485. {
  486. int y;
  487. /* 64-color encoding (each pixel in block is a different color) */
  488. CHECK_STREAM_PTR(64);
  489. for (y = 0; y < 8; y++) {
  490. memcpy(s->pixel_ptr, s->stream_ptr, 8);
  491. s->stream_ptr += 8;
  492. s->pixel_ptr += s->stride;
  493. }
  494. /* report success */
  495. return 0;
  496. }
  497. static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
  498. {
  499. int x, y;
  500. unsigned char pix;
  501. /* 16-color block encoding: each 2x2 block is a different color */
  502. CHECK_STREAM_PTR(16);
  503. for (y = 0; y < 8; y += 2) {
  504. for (x = 0; x < 8; x += 2) {
  505. pix = *s->stream_ptr++;
  506. *(s->pixel_ptr + x) = pix;
  507. *(s->pixel_ptr + x + 1) = pix;
  508. *(s->pixel_ptr + s->stride + x) = pix;
  509. *(s->pixel_ptr + s->stride + x + 1) = pix;
  510. }
  511. s->pixel_ptr += s->stride * 2;
  512. }
  513. /* report success */
  514. return 0;
  515. }
  516. static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
  517. {
  518. int y;
  519. unsigned char P[4];
  520. unsigned char index = 0;
  521. /* 4-color block encoding: each 4x4 block is a different color */
  522. CHECK_STREAM_PTR(4);
  523. memcpy(P, s->stream_ptr, 4);
  524. s->stream_ptr += 4;
  525. for (y = 0; y < 8; y++) {
  526. if (y < 4)
  527. index = 0;
  528. else
  529. index = 2;
  530. memset(s->pixel_ptr , P[index ], 4);
  531. memset(s->pixel_ptr + 4, P[index + 1], 4);
  532. s->pixel_ptr += s->stride;
  533. }
  534. /* report success */
  535. return 0;
  536. }
  537. static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
  538. {
  539. int y;
  540. unsigned char pix;
  541. /* 1-color encoding: the whole block is 1 solid color */
  542. CHECK_STREAM_PTR(1);
  543. pix = *s->stream_ptr++;
  544. for (y = 0; y < 8; y++) {
  545. memset(s->pixel_ptr, pix, 8);
  546. s->pixel_ptr += s->stride;
  547. }
  548. /* report success */
  549. return 0;
  550. }
  551. static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
  552. {
  553. int x, y;
  554. unsigned char sample0, sample1;
  555. /* dithered encoding */
  556. CHECK_STREAM_PTR(2);
  557. sample0 = *s->stream_ptr++;
  558. sample1 = *s->stream_ptr++;
  559. for (y = 0; y < 8; y++) {
  560. for (x = 0; x < 8; x += 2) {
  561. if (y & 1) {
  562. *s->pixel_ptr++ = sample1;
  563. *s->pixel_ptr++ = sample0;
  564. } else {
  565. *s->pixel_ptr++ = sample0;
  566. *s->pixel_ptr++ = sample1;
  567. }
  568. }
  569. s->pixel_ptr += s->line_inc;
  570. }
  571. /* report success */
  572. return 0;
  573. }
  574. static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
  575. ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
  576. ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
  577. ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
  578. ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
  579. ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
  580. ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
  581. ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
  582. ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
  583. };
  584. static void ipvideo_decode_opcodes(IpvideoContext *s)
  585. {
  586. int x, y;
  587. int index = 0;
  588. unsigned char opcode;
  589. int ret;
  590. int code_counts[16] = {0};
  591. static int frame = 0;
  592. debug_interplay("------------------ frame %d\n", frame);
  593. frame++;
  594. /* this is PAL8, so make the palette available */
  595. memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
  596. s->stride = s->current_frame.linesize[0];
  597. s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
  598. s->stream_end = s->buf + s->size;
  599. s->line_inc = s->stride - 8;
  600. s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
  601. + s->avctx->width - 8;
  602. for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
  603. for (x = y; x < y + s->avctx->width; x += 8) {
  604. /* bottom nibble first, then top nibble (which makes it
  605. * hard to use a GetBitcontext) */
  606. if (index & 1)
  607. opcode = s->decoding_map[index >> 1] >> 4;
  608. else
  609. opcode = s->decoding_map[index >> 1] & 0xF;
  610. index++;
  611. debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
  612. x - y, y / s->stride, opcode, s->stream_ptr);
  613. code_counts[opcode]++;
  614. s->pixel_ptr = s->current_frame.data[0] + x;
  615. ret = ipvideo_decode_block[opcode](s);
  616. if (ret != 0) {
  617. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
  618. frame, x - y, y / s->stride);
  619. return;
  620. }
  621. }
  622. }
  623. if (s->stream_end - s->stream_ptr > 1) {
  624. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
  625. s->stream_end - s->stream_ptr);
  626. }
  627. }
  628. static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
  629. {
  630. IpvideoContext *s = avctx->priv_data;
  631. s->avctx = avctx;
  632. if (s->avctx->palctrl == NULL) {
  633. av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
  634. return -1;
  635. }
  636. avctx->pix_fmt = PIX_FMT_PAL8;
  637. dsputil_init(&s->dsp, avctx);
  638. /* decoding map contains 4 bits of information per 8x8 block */
  639. s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
  640. s->current_frame.data[0] = s->last_frame.data[0] =
  641. s->second_last_frame.data[0] = NULL;
  642. return 0;
  643. }
  644. static int ipvideo_decode_frame(AVCodecContext *avctx,
  645. void *data, int *data_size,
  646. const uint8_t *buf, int buf_size)
  647. {
  648. IpvideoContext *s = avctx->priv_data;
  649. AVPaletteControl *palette_control = avctx->palctrl;
  650. /* compressed buffer needs to be large enough to at least hold an entire
  651. * decoding map */
  652. if (buf_size < s->decoding_map_size)
  653. return buf_size;
  654. s->decoding_map = buf;
  655. s->buf = buf + s->decoding_map_size;
  656. s->size = buf_size - s->decoding_map_size;
  657. s->current_frame.reference = 3;
  658. if (avctx->get_buffer(avctx, &s->current_frame)) {
  659. av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
  660. return -1;
  661. }
  662. ipvideo_decode_opcodes(s);
  663. if (palette_control->palette_changed) {
  664. palette_control->palette_changed = 0;
  665. s->current_frame.palette_has_changed = 1;
  666. }
  667. *data_size = sizeof(AVFrame);
  668. *(AVFrame*)data = s->current_frame;
  669. /* shuffle frames */
  670. if (s->second_last_frame.data[0])
  671. avctx->release_buffer(avctx, &s->second_last_frame);
  672. s->second_last_frame = s->last_frame;
  673. s->last_frame = s->current_frame;
  674. s->current_frame.data[0] = NULL; /* catch any access attempts */
  675. /* report that the buffer was completely consumed */
  676. return buf_size;
  677. }
  678. static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
  679. {
  680. IpvideoContext *s = avctx->priv_data;
  681. /* release the last frame */
  682. if (s->last_frame.data[0])
  683. avctx->release_buffer(avctx, &s->last_frame);
  684. if (s->second_last_frame.data[0])
  685. avctx->release_buffer(avctx, &s->second_last_frame);
  686. return 0;
  687. }
  688. AVCodec interplay_video_decoder = {
  689. "interplayvideo",
  690. CODEC_TYPE_VIDEO,
  691. CODEC_ID_INTERPLAY_VIDEO,
  692. sizeof(IpvideoContext),
  693. ipvideo_decode_init,
  694. NULL,
  695. ipvideo_decode_end,
  696. ipvideo_decode_frame,
  697. CODEC_CAP_DR1,
  698. .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
  699. };