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.

823 lines
23KB

  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 P[2];
  167. unsigned int flags;
  168. /* 2-color encoding */
  169. CHECK_STREAM_PTR(2);
  170. P[0] = *s->stream_ptr++;
  171. P[1] = *s->stream_ptr++;
  172. if (P[0] <= P[1]) {
  173. /* need 8 more bytes from the stream */
  174. CHECK_STREAM_PTR(8);
  175. for (y = 0; y < 8; y++) {
  176. flags = *s->stream_ptr++;
  177. for (x = 0x01; x <= 0x80; x <<= 1) {
  178. *s->pixel_ptr++ = P[!!(flags & x)];
  179. }
  180. s->pixel_ptr += s->line_inc;
  181. }
  182. } else {
  183. /* need 2 more bytes from the stream */
  184. CHECK_STREAM_PTR(2);
  185. flags = bytestream_get_le16(&s->stream_ptr);
  186. for (y = 0; y < 8; y += 2) {
  187. for (x = 0; x < 8; x += 2, flags >>= 1) {
  188. s->pixel_ptr[x ] =
  189. s->pixel_ptr[x + 1 ] =
  190. s->pixel_ptr[x + s->stride] =
  191. s->pixel_ptr[x + 1 + s->stride] = P[flags & 1];
  192. }
  193. s->pixel_ptr += s->stride * 2;
  194. }
  195. }
  196. /* report success */
  197. return 0;
  198. }
  199. static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
  200. {
  201. int x, y;
  202. unsigned char P[8];
  203. unsigned char B[8];
  204. unsigned int flags = 0;
  205. unsigned char P0 = 0, P1 = 0;
  206. int lower_half = 0;
  207. /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
  208. * either top and bottom or left and right halves */
  209. CHECK_STREAM_PTR(2);
  210. P[0] = *s->stream_ptr++;
  211. P[1] = *s->stream_ptr++;
  212. if (P[0] <= P[1]) {
  213. /* need 12 more bytes */
  214. CHECK_STREAM_PTR(12);
  215. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  216. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  217. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  218. P[4] = *s->stream_ptr++; P[5] = *s->stream_ptr++;
  219. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  220. P[6] = *s->stream_ptr++; P[7] = *s->stream_ptr++;
  221. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  222. for (y = 0; y < 8; y++) {
  223. /* time to reload flags? */
  224. if (y == 0) {
  225. flags =
  226. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  227. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  228. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  229. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  230. lower_half = 0; /* still on top half */
  231. } else if (y == 4) {
  232. flags =
  233. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  234. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  235. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  236. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  237. lower_half = 2;
  238. }
  239. for (x = 0; x < 8; x++, flags >>= 1) {
  240. /* get the pixel values ready for this quadrant */
  241. if (x == 0) {
  242. P0 = P[lower_half + 0];
  243. P1 = P[lower_half + 1];
  244. } else if (x == 4) {
  245. P0 = P[lower_half + 4];
  246. P1 = P[lower_half + 5];
  247. }
  248. *s->pixel_ptr++ = flags & 1 ? P1 : P0;
  249. }
  250. s->pixel_ptr += s->line_inc;
  251. }
  252. } else {
  253. /* need 10 more bytes */
  254. CHECK_STREAM_PTR(10);
  255. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  256. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  257. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  258. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  259. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  260. if (P[2] <= P[3]) {
  261. /* vertical split; left & right halves are 2-color encoded */
  262. for (y = 0; y < 8; y++) {
  263. /* time to reload flags? */
  264. if (y == 0) {
  265. flags =
  266. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  267. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  268. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  269. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  270. } else if (y == 4) {
  271. flags =
  272. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  273. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  274. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  275. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  276. }
  277. for (x = 0; x < 8; x++, flags >>= 1) {
  278. /* get the pixel values ready for this half */
  279. if (x == 0) {
  280. P0 = P[0];
  281. P1 = P[1];
  282. } else if (x == 4) {
  283. P0 = P[2];
  284. P1 = P[3];
  285. }
  286. *s->pixel_ptr++ = flags & 1 ? P1 : P0;
  287. }
  288. s->pixel_ptr += s->line_inc;
  289. }
  290. } else {
  291. /* horizontal split; top & bottom halves are 2-color encoded */
  292. for (y = 0; y < 8; y++) {
  293. int bitmask;
  294. flags = B[y];
  295. if (y == 0) {
  296. P0 = P[0];
  297. P1 = P[1];
  298. } else if (y == 4) {
  299. P0 = P[2];
  300. P1 = P[3];
  301. }
  302. for (bitmask = 0x01; bitmask <= 0x80; bitmask <<= 1) {
  303. *s->pixel_ptr++ = flags & bitmask ? P1 : P0;
  304. }
  305. s->pixel_ptr += s->line_inc;
  306. }
  307. }
  308. }
  309. /* report success */
  310. return 0;
  311. }
  312. static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
  313. {
  314. int x, y;
  315. unsigned char P[4];
  316. unsigned int flags = 0;
  317. unsigned char pix;
  318. /* 4-color encoding */
  319. CHECK_STREAM_PTR(4);
  320. memcpy(P, s->stream_ptr, 4);
  321. s->stream_ptr += 4;
  322. if ((P[0] <= P[1]) && (P[2] <= P[3])) {
  323. /* 1 of 4 colors for each pixel, need 16 more bytes */
  324. CHECK_STREAM_PTR(16);
  325. for (y = 0; y < 8; y++) {
  326. /* get the next set of 8 2-bit flags */
  327. flags = bytestream_get_le16(&s->stream_ptr);
  328. for (x = 0; x < 8; x++, flags >>= 2) {
  329. *s->pixel_ptr++ = P[flags & 0x03];
  330. }
  331. s->pixel_ptr += s->line_inc;
  332. }
  333. } else if ((P[0] <= P[1]) && (P[2] > P[3])) {
  334. /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
  335. CHECK_STREAM_PTR(4);
  336. flags = bytestream_get_le32(&s->stream_ptr);
  337. for (y = 0; y < 8; y += 2) {
  338. for (x = 0; x < 8; x += 2, flags >>= 2) {
  339. pix = P[flags & 0x03];
  340. s->pixel_ptr[x ] =
  341. s->pixel_ptr[x + 1 ] =
  342. s->pixel_ptr[x + s->stride] =
  343. s->pixel_ptr[x + 1 + s->stride] = pix;
  344. }
  345. s->pixel_ptr += s->stride * 2;
  346. }
  347. } else if ((P[0] > P[1]) && (P[2] <= P[3])) {
  348. /* 1 of 4 colors for each 2x1 block, need 8 more bytes */
  349. CHECK_STREAM_PTR(8);
  350. for (y = 0; y < 8; y++) {
  351. /* time to reload flags? */
  352. if ((y == 0) || (y == 4)) {
  353. flags = bytestream_get_le32(&s->stream_ptr);
  354. }
  355. for (x = 0; x < 8; x += 2, flags >>= 2) {
  356. pix = P[flags & 0x03];
  357. s->pixel_ptr[x ] =
  358. s->pixel_ptr[x + 1] = pix;
  359. }
  360. s->pixel_ptr += s->stride;
  361. }
  362. } else {
  363. /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
  364. CHECK_STREAM_PTR(8);
  365. for (y = 0; y < 8; y += 2) {
  366. /* time to reload flags? */
  367. if ((y == 0) || (y == 4)) {
  368. flags = bytestream_get_le32(&s->stream_ptr);
  369. }
  370. for (x = 0; x < 8; x++, flags >>= 2) {
  371. pix = P[flags & 0x03];
  372. s->pixel_ptr[x ] =
  373. s->pixel_ptr[x + s->stride] = pix;
  374. }
  375. s->pixel_ptr += s->stride * 2;
  376. }
  377. }
  378. /* report success */
  379. return 0;
  380. }
  381. static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
  382. {
  383. int x, y;
  384. unsigned char P[16];
  385. unsigned char B[16];
  386. int flags = 0;
  387. int index;
  388. int split;
  389. int lower_half;
  390. /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
  391. * either top and bottom or left and right halves */
  392. CHECK_STREAM_PTR(4);
  393. memcpy(P, s->stream_ptr, 4);
  394. s->stream_ptr += 4;
  395. if (P[0] <= P[1]) {
  396. /* 4-color encoding for each quadrant; need 28 more bytes */
  397. CHECK_STREAM_PTR(28);
  398. memcpy(B, s->stream_ptr, 4);
  399. s->stream_ptr += 4;
  400. for (y = 4; y < 16; y += 4) {
  401. memcpy(P + y, s->stream_ptr, 4);
  402. s->stream_ptr += 4;
  403. memcpy(B + y, s->stream_ptr, 4);
  404. s->stream_ptr += 4;
  405. }
  406. for (y = 0; y < 8; y++) {
  407. lower_half = (y >= 4) ? 4 : 0;
  408. flags = (B[y + 8] << 8) | B[y];
  409. for (x = 0; x < 8; x++, flags >>= 2) {
  410. split = (x >= 4) ? 8 : 0;
  411. index = split + lower_half + (flags & 0x03);
  412. *s->pixel_ptr++ = P[index];
  413. }
  414. s->pixel_ptr += s->line_inc;
  415. }
  416. } else {
  417. /* 4-color encoding for either left and right or top and bottom
  418. * halves; need 20 more bytes */
  419. CHECK_STREAM_PTR(20);
  420. memcpy(B, s->stream_ptr, 8);
  421. s->stream_ptr += 8;
  422. memcpy(P + 4, s->stream_ptr, 4);
  423. s->stream_ptr += 4;
  424. memcpy(B + 8, s->stream_ptr, 8);
  425. s->stream_ptr += 8;
  426. if (P[4] <= P[5]) {
  427. /* block is divided into left and right halves */
  428. for (y = 0; y < 8; y++) {
  429. flags = (B[y + 8] << 8) | B[y];
  430. split = 0;
  431. for (x = 0; x < 8; x++, flags >>= 2) {
  432. if (x == 4)
  433. split = 4;
  434. *s->pixel_ptr++ = P[split + (flags & 0x03)];
  435. }
  436. s->pixel_ptr += s->line_inc;
  437. }
  438. } else {
  439. /* block is divided into top and bottom halves */
  440. split = 0;
  441. for (y = 0; y < 8; y++) {
  442. flags = (B[y * 2 + 1] << 8) | B[y * 2];
  443. if (y == 4)
  444. split = 4;
  445. for (x = 0; x < 8; x++, flags >>= 2)
  446. *s->pixel_ptr++ = P[split + (flags & 0x03)];
  447. s->pixel_ptr += s->line_inc;
  448. }
  449. }
  450. }
  451. /* report success */
  452. return 0;
  453. }
  454. static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
  455. {
  456. int y;
  457. /* 64-color encoding (each pixel in block is a different color) */
  458. CHECK_STREAM_PTR(64);
  459. for (y = 0; y < 8; y++) {
  460. memcpy(s->pixel_ptr, s->stream_ptr, 8);
  461. s->stream_ptr += 8;
  462. s->pixel_ptr += s->stride;
  463. }
  464. /* report success */
  465. return 0;
  466. }
  467. static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
  468. {
  469. int x, y;
  470. unsigned char pix;
  471. /* 16-color block encoding: each 2x2 block is a different color */
  472. CHECK_STREAM_PTR(16);
  473. for (y = 0; y < 8; y += 2) {
  474. for (x = 0; x < 8; x += 2) {
  475. pix = *s->stream_ptr++;
  476. s->pixel_ptr[x ] =
  477. s->pixel_ptr[x + 1 ] =
  478. s->pixel_ptr[x + s->stride] =
  479. s->pixel_ptr[x + 1 + s->stride] = pix;
  480. }
  481. s->pixel_ptr += s->stride * 2;
  482. }
  483. /* report success */
  484. return 0;
  485. }
  486. static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
  487. {
  488. int y;
  489. unsigned char P[4];
  490. unsigned char index = 0;
  491. /* 4-color block encoding: each 4x4 block is a different color */
  492. CHECK_STREAM_PTR(4);
  493. memcpy(P, s->stream_ptr, 4);
  494. s->stream_ptr += 4;
  495. for (y = 0; y < 8; y++) {
  496. if (y < 4)
  497. index = 0;
  498. else
  499. index = 2;
  500. memset(s->pixel_ptr , P[index ], 4);
  501. memset(s->pixel_ptr + 4, P[index + 1], 4);
  502. s->pixel_ptr += s->stride;
  503. }
  504. /* report success */
  505. return 0;
  506. }
  507. static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
  508. {
  509. int y;
  510. unsigned char pix;
  511. /* 1-color encoding: the whole block is 1 solid color */
  512. CHECK_STREAM_PTR(1);
  513. pix = *s->stream_ptr++;
  514. for (y = 0; y < 8; y++) {
  515. memset(s->pixel_ptr, pix, 8);
  516. s->pixel_ptr += s->stride;
  517. }
  518. /* report success */
  519. return 0;
  520. }
  521. static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
  522. {
  523. int x, y;
  524. unsigned char sample[2];
  525. /* dithered encoding */
  526. CHECK_STREAM_PTR(2);
  527. sample[0] = *s->stream_ptr++;
  528. sample[1] = *s->stream_ptr++;
  529. for (y = 0; y < 8; y++) {
  530. for (x = 0; x < 8; x += 2) {
  531. *s->pixel_ptr++ = sample[ y & 1 ];
  532. *s->pixel_ptr++ = sample[!(y & 1)];
  533. }
  534. s->pixel_ptr += s->line_inc;
  535. }
  536. /* report success */
  537. return 0;
  538. }
  539. static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
  540. ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
  541. ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
  542. ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
  543. ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
  544. ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
  545. ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
  546. ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
  547. ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
  548. };
  549. static void ipvideo_decode_opcodes(IpvideoContext *s)
  550. {
  551. int x, y;
  552. int index = 0;
  553. unsigned char opcode;
  554. int ret;
  555. int code_counts[16] = {0};
  556. static int frame = 0;
  557. debug_interplay("------------------ frame %d\n", frame);
  558. frame++;
  559. /* this is PAL8, so make the palette available */
  560. memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
  561. s->stride = s->current_frame.linesize[0];
  562. s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
  563. s->stream_end = s->buf + s->size;
  564. s->line_inc = s->stride - 8;
  565. s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
  566. + s->avctx->width - 8;
  567. for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
  568. for (x = y; x < y + s->avctx->width; x += 8) {
  569. /* bottom nibble first, then top nibble (which makes it
  570. * hard to use a GetBitcontext) */
  571. if (index & 1)
  572. opcode = s->decoding_map[index >> 1] >> 4;
  573. else
  574. opcode = s->decoding_map[index >> 1] & 0xF;
  575. index++;
  576. debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
  577. x - y, y / s->stride, opcode, s->stream_ptr);
  578. code_counts[opcode]++;
  579. s->pixel_ptr = s->current_frame.data[0] + x;
  580. ret = ipvideo_decode_block[opcode](s);
  581. if (ret != 0) {
  582. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
  583. frame, x - y, y / s->stride);
  584. return;
  585. }
  586. }
  587. }
  588. if (s->stream_end - s->stream_ptr > 1) {
  589. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
  590. s->stream_end - s->stream_ptr);
  591. }
  592. }
  593. static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
  594. {
  595. IpvideoContext *s = avctx->priv_data;
  596. s->avctx = avctx;
  597. if (s->avctx->palctrl == NULL) {
  598. av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
  599. return -1;
  600. }
  601. avctx->pix_fmt = PIX_FMT_PAL8;
  602. dsputil_init(&s->dsp, avctx);
  603. /* decoding map contains 4 bits of information per 8x8 block */
  604. s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
  605. s->current_frame.data[0] = s->last_frame.data[0] =
  606. s->second_last_frame.data[0] = NULL;
  607. return 0;
  608. }
  609. static int ipvideo_decode_frame(AVCodecContext *avctx,
  610. void *data, int *data_size,
  611. const uint8_t *buf, int buf_size)
  612. {
  613. IpvideoContext *s = avctx->priv_data;
  614. AVPaletteControl *palette_control = avctx->palctrl;
  615. /* compressed buffer needs to be large enough to at least hold an entire
  616. * decoding map */
  617. if (buf_size < s->decoding_map_size)
  618. return buf_size;
  619. s->decoding_map = buf;
  620. s->buf = buf + s->decoding_map_size;
  621. s->size = buf_size - s->decoding_map_size;
  622. s->current_frame.reference = 3;
  623. if (avctx->get_buffer(avctx, &s->current_frame)) {
  624. av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
  625. return -1;
  626. }
  627. ipvideo_decode_opcodes(s);
  628. if (palette_control->palette_changed) {
  629. palette_control->palette_changed = 0;
  630. s->current_frame.palette_has_changed = 1;
  631. }
  632. *data_size = sizeof(AVFrame);
  633. *(AVFrame*)data = s->current_frame;
  634. /* shuffle frames */
  635. if (s->second_last_frame.data[0])
  636. avctx->release_buffer(avctx, &s->second_last_frame);
  637. s->second_last_frame = s->last_frame;
  638. s->last_frame = s->current_frame;
  639. s->current_frame.data[0] = NULL; /* catch any access attempts */
  640. /* report that the buffer was completely consumed */
  641. return buf_size;
  642. }
  643. static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
  644. {
  645. IpvideoContext *s = avctx->priv_data;
  646. /* release the last frame */
  647. if (s->last_frame.data[0])
  648. avctx->release_buffer(avctx, &s->last_frame);
  649. if (s->second_last_frame.data[0])
  650. avctx->release_buffer(avctx, &s->second_last_frame);
  651. return 0;
  652. }
  653. AVCodec interplay_video_decoder = {
  654. "interplayvideo",
  655. CODEC_TYPE_VIDEO,
  656. CODEC_ID_INTERPLAY_VIDEO,
  657. sizeof(IpvideoContext),
  658. ipvideo_decode_init,
  659. NULL,
  660. ipvideo_decode_end,
  661. ipvideo_decode_frame,
  662. CODEC_CAP_DR1,
  663. .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
  664. };