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.

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