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.

926 lines
27KB

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