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.

963 lines
28KB

  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 "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. unsigned char *decoding_map;
  57. int decoding_map_size;
  58. unsigned char *buf;
  59. int size;
  60. unsigned char *stream_ptr;
  61. 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[0][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[0][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[0][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 char B[8];
  225. unsigned int flags;
  226. int bitmask;
  227. /* 2-color encoding */
  228. CHECK_STREAM_PTR(2);
  229. P0 = *s->stream_ptr++;
  230. P1 = *s->stream_ptr++;
  231. if (P0 <= P1) {
  232. /* need 8 more bytes from the stream */
  233. CHECK_STREAM_PTR(8);
  234. for (y = 0; y < 8; y++)
  235. B[y] = *s->stream_ptr++;
  236. for (y = 0; y < 8; y++) {
  237. flags = B[y];
  238. for (x = 0x01; x <= 0x80; x <<= 1) {
  239. if (flags & x)
  240. *s->pixel_ptr++ = P1;
  241. else
  242. *s->pixel_ptr++ = P0;
  243. }
  244. s->pixel_ptr += s->line_inc;
  245. }
  246. } else {
  247. /* need 2 more bytes from the stream */
  248. CHECK_STREAM_PTR(2);
  249. B[0] = *s->stream_ptr++;
  250. B[1] = *s->stream_ptr++;
  251. flags = (B[1] << 8) | B[0];
  252. bitmask = 0x0001;
  253. for (y = 0; y < 8; y += 2) {
  254. for (x = 0; x < 8; x += 2, bitmask <<= 1) {
  255. if (flags & bitmask) {
  256. *(s->pixel_ptr + x) = P1;
  257. *(s->pixel_ptr + x + 1) = P1;
  258. *(s->pixel_ptr + s->stride + x) = P1;
  259. *(s->pixel_ptr + s->stride + x + 1) = P1;
  260. } else {
  261. *(s->pixel_ptr + x) = P0;
  262. *(s->pixel_ptr + x + 1) = P0;
  263. *(s->pixel_ptr + s->stride + x) = P0;
  264. *(s->pixel_ptr + s->stride + x + 1) = P0;
  265. }
  266. }
  267. s->pixel_ptr += s->stride * 2;
  268. }
  269. }
  270. /* report success */
  271. return 0;
  272. }
  273. static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
  274. {
  275. int x, y;
  276. unsigned char P[8];
  277. unsigned char B[8];
  278. unsigned int flags = 0;
  279. unsigned int bitmask = 0;
  280. unsigned char P0 = 0, P1 = 0;
  281. int lower_half = 0;
  282. /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
  283. * either top and bottom or left and right halves */
  284. CHECK_STREAM_PTR(2);
  285. P[0] = *s->stream_ptr++;
  286. P[1] = *s->stream_ptr++;
  287. if (P[0] <= P[1]) {
  288. /* need 12 more bytes */
  289. CHECK_STREAM_PTR(12);
  290. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  291. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  292. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  293. P[4] = *s->stream_ptr++; P[5] = *s->stream_ptr++;
  294. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  295. P[6] = *s->stream_ptr++; P[7] = *s->stream_ptr++;
  296. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  297. for (y = 0; y < 8; y++) {
  298. /* time to reload flags? */
  299. if (y == 0) {
  300. flags =
  301. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  302. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  303. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  304. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  305. bitmask = 0x00000001;
  306. lower_half = 0; /* still on top half */
  307. } else if (y == 4) {
  308. flags =
  309. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  310. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  311. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  312. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  313. bitmask = 0x00000001;
  314. lower_half = 2;
  315. }
  316. for (x = 0; x < 8; x++, bitmask <<= 1) {
  317. /* get the pixel values ready for this quadrant */
  318. if (x == 0) {
  319. P0 = P[lower_half + 0];
  320. P1 = P[lower_half + 1];
  321. } else if (x == 4) {
  322. P0 = P[lower_half + 4];
  323. P1 = P[lower_half + 5];
  324. }
  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. } else {
  333. /* need 10 more bytes */
  334. CHECK_STREAM_PTR(10);
  335. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  336. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  337. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  338. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  339. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  340. if (P[2] <= P[3]) {
  341. /* vertical split; left & right halves are 2-color encoded */
  342. for (y = 0; y < 8; y++) {
  343. /* time to reload flags? */
  344. if (y == 0) {
  345. flags =
  346. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  347. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  348. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  349. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  350. bitmask = 0x00000001;
  351. } else if (y == 4) {
  352. flags =
  353. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  354. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  355. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  356. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  357. bitmask = 0x00000001;
  358. }
  359. for (x = 0; x < 8; x++, bitmask <<= 1) {
  360. /* get the pixel values ready for this half */
  361. if (x == 0) {
  362. P0 = P[0];
  363. P1 = P[1];
  364. } else if (x == 4) {
  365. P0 = P[2];
  366. P1 = P[3];
  367. }
  368. if (flags & bitmask)
  369. *s->pixel_ptr++ = P1;
  370. else
  371. *s->pixel_ptr++ = P0;
  372. }
  373. s->pixel_ptr += s->line_inc;
  374. }
  375. } else {
  376. /* horizontal split; top & bottom halves are 2-color encoded */
  377. for (y = 0; y < 8; y++) {
  378. flags = B[y];
  379. if (y == 0) {
  380. P0 = P[0];
  381. P1 = P[1];
  382. } else if (y == 4) {
  383. P0 = P[2];
  384. P1 = P[3];
  385. }
  386. for (bitmask = 0x01; bitmask <= 0x80; bitmask <<= 1) {
  387. if (flags & bitmask)
  388. *s->pixel_ptr++ = P1;
  389. else
  390. *s->pixel_ptr++ = P0;
  391. }
  392. s->pixel_ptr += s->line_inc;
  393. }
  394. }
  395. }
  396. /* report success */
  397. return 0;
  398. }
  399. static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
  400. {
  401. int x, y;
  402. unsigned char P[4];
  403. unsigned char B[4];
  404. unsigned int flags = 0;
  405. int shifter = 0;
  406. unsigned char pix;
  407. /* 4-color encoding */
  408. CHECK_STREAM_PTR(4);
  409. for (y = 0; y < 4; y++)
  410. P[y] = *s->stream_ptr++;
  411. if ((P[0] <= P[1]) && (P[2] <= P[3])) {
  412. /* 1 of 4 colors for each pixel, need 16 more bytes */
  413. CHECK_STREAM_PTR(16);
  414. for (y = 0; y < 8; y++) {
  415. /* get the next set of 8 2-bit flags */
  416. flags = (s->stream_ptr[1] << 8) | s->stream_ptr[0];
  417. s->stream_ptr += 2;
  418. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  419. *s->pixel_ptr++ = P[(flags >> shifter) & 0x03];
  420. }
  421. s->pixel_ptr += s->line_inc;
  422. }
  423. } else if ((P[0] <= P[1]) && (P[2] > P[3])) {
  424. /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
  425. CHECK_STREAM_PTR(4);
  426. B[0] = *s->stream_ptr++;
  427. B[1] = *s->stream_ptr++;
  428. B[2] = *s->stream_ptr++;
  429. B[3] = *s->stream_ptr++;
  430. flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
  431. shifter = 0;
  432. for (y = 0; y < 8; y += 2) {
  433. for (x = 0; x < 8; x += 2, shifter += 2) {
  434. pix = P[(flags >> shifter) & 0x03];
  435. *(s->pixel_ptr + x) = pix;
  436. *(s->pixel_ptr + x + 1) = pix;
  437. *(s->pixel_ptr + s->stride + x) = pix;
  438. *(s->pixel_ptr + s->stride + x + 1) = pix;
  439. }
  440. s->pixel_ptr += s->stride * 2;
  441. }
  442. } else if ((P[0] > P[1]) && (P[2] <= P[3])) {
  443. /* 1 of 4 colors for each 2x1 block, need 8 more bytes */
  444. CHECK_STREAM_PTR(8);
  445. for (y = 0; y < 8; y++) {
  446. /* time to reload flags? */
  447. if ((y == 0) || (y == 4)) {
  448. B[0] = *s->stream_ptr++;
  449. B[1] = *s->stream_ptr++;
  450. B[2] = *s->stream_ptr++;
  451. B[3] = *s->stream_ptr++;
  452. flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
  453. shifter = 0;
  454. }
  455. for (x = 0; x < 8; x += 2, shifter += 2) {
  456. pix = P[(flags >> shifter) & 0x03];
  457. *(s->pixel_ptr + x) = pix;
  458. *(s->pixel_ptr + x + 1) = pix;
  459. }
  460. s->pixel_ptr += s->stride;
  461. }
  462. } else {
  463. /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
  464. CHECK_STREAM_PTR(8);
  465. for (y = 0; y < 8; y += 2) {
  466. /* time to reload flags? */
  467. if ((y == 0) || (y == 4)) {
  468. B[0] = *s->stream_ptr++;
  469. B[1] = *s->stream_ptr++;
  470. B[2] = *s->stream_ptr++;
  471. B[3] = *s->stream_ptr++;
  472. flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
  473. shifter = 0;
  474. }
  475. for (x = 0; x < 8; x++, shifter += 2) {
  476. pix = P[(flags >> shifter) & 0x03];
  477. *(s->pixel_ptr + x) = pix;
  478. *(s->pixel_ptr + s->stride + x) = pix;
  479. }
  480. s->pixel_ptr += s->stride * 2;
  481. }
  482. }
  483. /* report success */
  484. return 0;
  485. }
  486. static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
  487. {
  488. int x, y;
  489. unsigned char P[16];
  490. unsigned char B[16];
  491. int flags = 0;
  492. int shifter = 0;
  493. int index;
  494. int split;
  495. int lower_half;
  496. /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
  497. * either top and bottom or left and right halves */
  498. CHECK_STREAM_PTR(4);
  499. for (y = 0; y < 4; y++)
  500. P[y] = *s->stream_ptr++;
  501. if (P[0] <= P[1]) {
  502. /* 4-color encoding for each quadrant; need 28 more bytes */
  503. CHECK_STREAM_PTR(28);
  504. for (y = 0; y < 4; y++)
  505. B[y] = *s->stream_ptr++;
  506. for (y = 4; y < 16; y += 4) {
  507. for (x = y; x < y + 4; x++)
  508. P[x] = *s->stream_ptr++;
  509. for (x = y; x < y + 4; x++)
  510. B[x] = *s->stream_ptr++;
  511. }
  512. for (y = 0; y < 8; y++) {
  513. lower_half = (y >= 4) ? 4 : 0;
  514. flags = (B[y + 8] << 8) | B[y];
  515. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  516. split = (x >= 4) ? 8 : 0;
  517. index = split + lower_half + ((flags >> shifter) & 0x03);
  518. *s->pixel_ptr++ = P[index];
  519. }
  520. s->pixel_ptr += s->line_inc;
  521. }
  522. } else {
  523. /* 4-color encoding for either left and right or top and bottom
  524. * halves; need 20 more bytes */
  525. CHECK_STREAM_PTR(20);
  526. for (y = 0; y < 8; y++)
  527. B[y] = *s->stream_ptr++;
  528. for (y = 4; y < 8; y++)
  529. P[y] = *s->stream_ptr++;
  530. for (y = 8; y < 16; y++)
  531. B[y] = *s->stream_ptr++;
  532. if (P[4] <= P[5]) {
  533. /* block is divided into left and right halves */
  534. for (y = 0; y < 8; y++) {
  535. flags = (B[y + 8] << 8) | B[y];
  536. split = 0;
  537. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  538. if (x == 4)
  539. split = 4;
  540. *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
  541. }
  542. s->pixel_ptr += s->line_inc;
  543. }
  544. } else {
  545. /* block is divided into top and bottom halves */
  546. split = 0;
  547. for (y = 0; y < 8; y++) {
  548. flags = (B[y * 2 + 1] << 8) | B[y * 2];
  549. if (y == 4)
  550. split = 4;
  551. for (x = 0, shifter = 0; x < 8; x++, shifter += 2)
  552. *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
  553. s->pixel_ptr += s->line_inc;
  554. }
  555. }
  556. }
  557. /* report success */
  558. return 0;
  559. }
  560. static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
  561. {
  562. int x, y;
  563. /* 64-color encoding (each pixel in block is a different color) */
  564. CHECK_STREAM_PTR(64);
  565. for (y = 0; y < 8; y++) {
  566. for (x = 0; x < 8; x++) {
  567. *s->pixel_ptr++ = *s->stream_ptr++;
  568. }
  569. s->pixel_ptr += s->line_inc;
  570. }
  571. /* report success */
  572. return 0;
  573. }
  574. static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
  575. {
  576. int x, y;
  577. unsigned char pix;
  578. /* 16-color block encoding: each 2x2 block is a different color */
  579. CHECK_STREAM_PTR(16);
  580. for (y = 0; y < 8; y += 2) {
  581. for (x = 0; x < 8; x += 2) {
  582. pix = *s->stream_ptr++;
  583. *(s->pixel_ptr + x) = pix;
  584. *(s->pixel_ptr + x + 1) = pix;
  585. *(s->pixel_ptr + s->stride + x) = pix;
  586. *(s->pixel_ptr + s->stride + x + 1) = pix;
  587. }
  588. s->pixel_ptr += s->stride * 2;
  589. }
  590. /* report success */
  591. return 0;
  592. }
  593. static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
  594. {
  595. int x, y;
  596. unsigned char P[4];
  597. unsigned char index = 0;
  598. /* 4-color block encoding: each 4x4 block is a different color */
  599. CHECK_STREAM_PTR(4);
  600. for (y = 0; y < 4; y++)
  601. P[y] = *s->stream_ptr++;
  602. for (y = 0; y < 8; y++) {
  603. if (y < 4)
  604. index = 0;
  605. else
  606. index = 2;
  607. for (x = 0; x < 8; x++) {
  608. if (x == 4)
  609. index++;
  610. *s->pixel_ptr++ = P[index];
  611. }
  612. s->pixel_ptr += s->line_inc;
  613. }
  614. /* report success */
  615. return 0;
  616. }
  617. static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
  618. {
  619. int x, y;
  620. unsigned char pix;
  621. /* 1-color encoding: the whole block is 1 solid color */
  622. CHECK_STREAM_PTR(1);
  623. pix = *s->stream_ptr++;
  624. for (y = 0; y < 8; y++) {
  625. for (x = 0; x < 8; x++) {
  626. *s->pixel_ptr++ = pix;
  627. }
  628. s->pixel_ptr += s->line_inc;
  629. }
  630. /* report success */
  631. return 0;
  632. }
  633. static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
  634. {
  635. int x, y;
  636. unsigned char sample0, sample1;
  637. /* dithered encoding */
  638. CHECK_STREAM_PTR(2);
  639. sample0 = *s->stream_ptr++;
  640. sample1 = *s->stream_ptr++;
  641. for (y = 0; y < 8; y++) {
  642. for (x = 0; x < 8; x += 2) {
  643. if (y & 1) {
  644. *s->pixel_ptr++ = sample1;
  645. *s->pixel_ptr++ = sample0;
  646. } else {
  647. *s->pixel_ptr++ = sample0;
  648. *s->pixel_ptr++ = sample1;
  649. }
  650. }
  651. s->pixel_ptr += s->line_inc;
  652. }
  653. /* report success */
  654. return 0;
  655. }
  656. static int (*ipvideo_decode_block[16])(IpvideoContext *s);
  657. static void ipvideo_decode_opcodes(IpvideoContext *s)
  658. {
  659. int x, y;
  660. int index = 0;
  661. unsigned char opcode;
  662. int ret;
  663. int code_counts[16];
  664. static int frame = 0;
  665. debug_interplay("------------------ frame %d\n", frame);
  666. frame++;
  667. for (x = 0; x < 16; x++)
  668. code_counts[x] = 0;
  669. /* this is PAL8, so make the palette available */
  670. memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
  671. s->stride = s->current_frame.linesize[0];
  672. s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
  673. s->stream_end = s->buf + s->size;
  674. s->line_inc = s->stride - 8;
  675. s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
  676. + s->avctx->width - 8;
  677. s->dsp = s->dsp;
  678. for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
  679. for (x = y; x < y + s->avctx->width; x += 8) {
  680. /* bottom nibble first, then top nibble (which makes it
  681. * hard to use a GetBitcontext) */
  682. if (index & 1)
  683. opcode = s->decoding_map[index >> 1] >> 4;
  684. else
  685. opcode = s->decoding_map[index >> 1] & 0xF;
  686. index++;
  687. debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
  688. x - y, y / s->stride, opcode, s->stream_ptr);
  689. code_counts[opcode]++;
  690. s->pixel_ptr = s->current_frame.data[0] + x;
  691. ret = ipvideo_decode_block[opcode](s);
  692. if (ret != 0) {
  693. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
  694. frame, x - y, y / s->stride);
  695. return;
  696. }
  697. }
  698. }
  699. if ((s->stream_ptr != s->stream_end) &&
  700. (s->stream_ptr + 1 != s->stream_end)) {
  701. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
  702. s->stream_end - s->stream_ptr);
  703. }
  704. }
  705. static int ipvideo_decode_init(AVCodecContext *avctx)
  706. {
  707. IpvideoContext *s = avctx->priv_data;
  708. s->avctx = avctx;
  709. if (s->avctx->palctrl == NULL) {
  710. av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
  711. return -1;
  712. }
  713. avctx->pix_fmt = PIX_FMT_PAL8;
  714. dsputil_init(&s->dsp, avctx);
  715. /* decoding map contains 4 bits of information per 8x8 block */
  716. s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
  717. /* assign block decode functions */
  718. ipvideo_decode_block[0x0] = ipvideo_decode_block_opcode_0x0;
  719. ipvideo_decode_block[0x1] = ipvideo_decode_block_opcode_0x1;
  720. ipvideo_decode_block[0x2] = ipvideo_decode_block_opcode_0x2;
  721. ipvideo_decode_block[0x3] = ipvideo_decode_block_opcode_0x3;
  722. ipvideo_decode_block[0x4] = ipvideo_decode_block_opcode_0x4;
  723. ipvideo_decode_block[0x5] = ipvideo_decode_block_opcode_0x5;
  724. ipvideo_decode_block[0x6] = ipvideo_decode_block_opcode_0x6;
  725. ipvideo_decode_block[0x7] = ipvideo_decode_block_opcode_0x7;
  726. ipvideo_decode_block[0x8] = ipvideo_decode_block_opcode_0x8;
  727. ipvideo_decode_block[0x9] = ipvideo_decode_block_opcode_0x9;
  728. ipvideo_decode_block[0xA] = ipvideo_decode_block_opcode_0xA;
  729. ipvideo_decode_block[0xB] = ipvideo_decode_block_opcode_0xB;
  730. ipvideo_decode_block[0xC] = ipvideo_decode_block_opcode_0xC;
  731. ipvideo_decode_block[0xD] = ipvideo_decode_block_opcode_0xD;
  732. ipvideo_decode_block[0xE] = ipvideo_decode_block_opcode_0xE;
  733. ipvideo_decode_block[0xF] = ipvideo_decode_block_opcode_0xF;
  734. s->current_frame.data[0] = s->last_frame.data[0] =
  735. s->second_last_frame.data[0] = NULL;
  736. return 0;
  737. }
  738. static int ipvideo_decode_frame(AVCodecContext *avctx,
  739. void *data, int *data_size,
  740. uint8_t *buf, int buf_size)
  741. {
  742. IpvideoContext *s = avctx->priv_data;
  743. AVPaletteControl *palette_control = avctx->palctrl;
  744. /* compressed buffer needs to be large enough to at least hold an entire
  745. * decoding map */
  746. if (buf_size < s->decoding_map_size)
  747. return buf_size;
  748. s->decoding_map = buf;
  749. s->buf = buf + s->decoding_map_size;
  750. s->size = buf_size - s->decoding_map_size;
  751. s->current_frame.reference = 3;
  752. if (avctx->get_buffer(avctx, &s->current_frame)) {
  753. av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
  754. return -1;
  755. }
  756. ipvideo_decode_opcodes(s);
  757. if (palette_control->palette_changed) {
  758. palette_control->palette_changed = 0;
  759. s->current_frame.palette_has_changed = 1;
  760. }
  761. *data_size = sizeof(AVFrame);
  762. *(AVFrame*)data = s->current_frame;
  763. /* shuffle frames */
  764. if (s->second_last_frame.data[0])
  765. avctx->release_buffer(avctx, &s->second_last_frame);
  766. s->second_last_frame = s->last_frame;
  767. s->last_frame = s->current_frame;
  768. s->current_frame.data[0] = NULL; /* catch any access attempts */
  769. /* report that the buffer was completely consumed */
  770. return buf_size;
  771. }
  772. static int ipvideo_decode_end(AVCodecContext *avctx)
  773. {
  774. IpvideoContext *s = avctx->priv_data;
  775. /* release the last frame */
  776. if (s->last_frame.data[0])
  777. avctx->release_buffer(avctx, &s->last_frame);
  778. if (s->second_last_frame.data[0])
  779. avctx->release_buffer(avctx, &s->second_last_frame);
  780. return 0;
  781. }
  782. AVCodec interplay_video_decoder = {
  783. "interplayvideo",
  784. CODEC_TYPE_VIDEO,
  785. CODEC_ID_INTERPLAY_VIDEO,
  786. sizeof(IpvideoContext),
  787. ipvideo_decode_init,
  788. NULL,
  789. ipvideo_decode_end,
  790. ipvideo_decode_frame,
  791. CODEC_CAP_DR1,
  792. };