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.

811 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. if (P[2] <= P[3]) {
  256. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  257. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  258. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  259. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  260. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  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. if (y == 4) {
  295. P[0] = *s->stream_ptr++;
  296. P[1] = *s->stream_ptr++;
  297. }
  298. flags = *s->stream_ptr++;
  299. for (bitmask = 0x01; bitmask <= 0x80; bitmask <<= 1) {
  300. *s->pixel_ptr++ = P[!!(flags & bitmask)];
  301. }
  302. s->pixel_ptr += s->line_inc;
  303. }
  304. }
  305. }
  306. /* report success */
  307. return 0;
  308. }
  309. static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
  310. {
  311. int x, y;
  312. unsigned char P[4];
  313. /* 4-color encoding */
  314. CHECK_STREAM_PTR(4);
  315. memcpy(P, s->stream_ptr, 4);
  316. s->stream_ptr += 4;
  317. if ((P[0] <= P[1]) && (P[2] <= P[3])) {
  318. /* 1 of 4 colors for each pixel, need 16 more bytes */
  319. CHECK_STREAM_PTR(16);
  320. for (y = 0; y < 8; y++) {
  321. /* get the next set of 8 2-bit flags */
  322. int flags = bytestream_get_le16(&s->stream_ptr);
  323. for (x = 0; x < 8; x++, flags >>= 2) {
  324. *s->pixel_ptr++ = P[flags & 0x03];
  325. }
  326. s->pixel_ptr += s->line_inc;
  327. }
  328. } else if ((P[0] <= P[1]) && (P[2] > P[3])) {
  329. uint32_t flags;
  330. /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
  331. CHECK_STREAM_PTR(4);
  332. flags = bytestream_get_le32(&s->stream_ptr);
  333. for (y = 0; y < 8; y += 2) {
  334. for (x = 0; x < 8; x += 2, flags >>= 2) {
  335. s->pixel_ptr[x ] =
  336. s->pixel_ptr[x + 1 ] =
  337. s->pixel_ptr[x + s->stride] =
  338. s->pixel_ptr[x + 1 + s->stride] = P[flags & 0x03];
  339. }
  340. s->pixel_ptr += s->stride * 2;
  341. }
  342. } else if ((P[0] > P[1]) && (P[2] <= P[3])) {
  343. uint64_t flags;
  344. /* 1 of 4 colors for each 2x1 block, need 8 more bytes */
  345. CHECK_STREAM_PTR(8);
  346. flags = bytestream_get_le64(&s->stream_ptr);
  347. for (y = 0; y < 8; y++) {
  348. for (x = 0; x < 8; x += 2, flags >>= 2) {
  349. s->pixel_ptr[x ] =
  350. s->pixel_ptr[x + 1] = P[flags & 0x03];
  351. }
  352. s->pixel_ptr += s->stride;
  353. }
  354. } else {
  355. uint64_t flags;
  356. /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
  357. CHECK_STREAM_PTR(8);
  358. flags = bytestream_get_le64(&s->stream_ptr);
  359. for (y = 0; y < 8; y += 2) {
  360. for (x = 0; x < 8; x++, flags >>= 2) {
  361. s->pixel_ptr[x ] =
  362. s->pixel_ptr[x + s->stride] = P[flags & 0x03];
  363. }
  364. s->pixel_ptr += s->stride * 2;
  365. }
  366. }
  367. /* report success */
  368. return 0;
  369. }
  370. static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
  371. {
  372. int x, y;
  373. unsigned char P[16];
  374. unsigned char B[16];
  375. int flags = 0;
  376. int index;
  377. int split;
  378. int lower_half;
  379. /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
  380. * either top and bottom or left and right halves */
  381. CHECK_STREAM_PTR(4);
  382. memcpy(P, s->stream_ptr, 4);
  383. s->stream_ptr += 4;
  384. if (P[0] <= P[1]) {
  385. /* 4-color encoding for each quadrant; need 28 more bytes */
  386. CHECK_STREAM_PTR(28);
  387. memcpy(B, s->stream_ptr, 4);
  388. s->stream_ptr += 4;
  389. for (y = 4; y < 16; y += 4) {
  390. memcpy(P + y, s->stream_ptr, 4);
  391. s->stream_ptr += 4;
  392. memcpy(B + y, s->stream_ptr, 4);
  393. s->stream_ptr += 4;
  394. }
  395. for (y = 0; y < 8; y++) {
  396. lower_half = (y >= 4) ? 4 : 0;
  397. flags = (B[y + 8] << 8) | B[y];
  398. for (x = 0; x < 8; x++, flags >>= 2) {
  399. split = (x >= 4) ? 8 : 0;
  400. index = split + lower_half + (flags & 0x03);
  401. *s->pixel_ptr++ = P[index];
  402. }
  403. s->pixel_ptr += s->line_inc;
  404. }
  405. } else {
  406. /* 4-color encoding for either left and right or top and bottom
  407. * halves; need 20 more bytes */
  408. CHECK_STREAM_PTR(20);
  409. memcpy(B, s->stream_ptr, 8);
  410. s->stream_ptr += 8;
  411. memcpy(P + 4, s->stream_ptr, 4);
  412. s->stream_ptr += 4;
  413. memcpy(B + 8, s->stream_ptr, 8);
  414. s->stream_ptr += 8;
  415. if (P[4] <= P[5]) {
  416. /* block is divided into left and right halves */
  417. for (y = 0; y < 8; y++) {
  418. flags = (B[y + 8] << 8) | B[y];
  419. split = 0;
  420. for (x = 0; x < 8; x++, flags >>= 2) {
  421. if (x == 4)
  422. split = 4;
  423. *s->pixel_ptr++ = P[split + (flags & 0x03)];
  424. }
  425. s->pixel_ptr += s->line_inc;
  426. }
  427. } else {
  428. /* block is divided into top and bottom halves */
  429. split = 0;
  430. for (y = 0; y < 8; y++) {
  431. flags = (B[y * 2 + 1] << 8) | B[y * 2];
  432. if (y == 4)
  433. split = 4;
  434. for (x = 0; x < 8; x++, flags >>= 2)
  435. *s->pixel_ptr++ = P[split + (flags & 0x03)];
  436. s->pixel_ptr += s->line_inc;
  437. }
  438. }
  439. }
  440. /* report success */
  441. return 0;
  442. }
  443. static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
  444. {
  445. int y;
  446. /* 64-color encoding (each pixel in block is a different color) */
  447. CHECK_STREAM_PTR(64);
  448. for (y = 0; y < 8; y++) {
  449. memcpy(s->pixel_ptr, s->stream_ptr, 8);
  450. s->stream_ptr += 8;
  451. s->pixel_ptr += s->stride;
  452. }
  453. /* report success */
  454. return 0;
  455. }
  456. static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
  457. {
  458. int x, y;
  459. /* 16-color block encoding: each 2x2 block is a different color */
  460. CHECK_STREAM_PTR(16);
  461. for (y = 0; y < 8; y += 2) {
  462. for (x = 0; x < 8; x += 2) {
  463. s->pixel_ptr[x ] =
  464. s->pixel_ptr[x + 1 ] =
  465. s->pixel_ptr[x + s->stride] =
  466. s->pixel_ptr[x + 1 + s->stride] = *s->stream_ptr++;
  467. }
  468. s->pixel_ptr += s->stride * 2;
  469. }
  470. /* report success */
  471. return 0;
  472. }
  473. static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
  474. {
  475. int y;
  476. unsigned char P[4];
  477. unsigned char index = 0;
  478. /* 4-color block encoding: each 4x4 block is a different color */
  479. CHECK_STREAM_PTR(4);
  480. memcpy(P, s->stream_ptr, 4);
  481. s->stream_ptr += 4;
  482. for (y = 0; y < 8; y++) {
  483. if (y < 4)
  484. index = 0;
  485. else
  486. index = 2;
  487. memset(s->pixel_ptr , P[index ], 4);
  488. memset(s->pixel_ptr + 4, P[index + 1], 4);
  489. s->pixel_ptr += s->stride;
  490. }
  491. /* report success */
  492. return 0;
  493. }
  494. static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
  495. {
  496. int y;
  497. unsigned char pix;
  498. /* 1-color encoding: the whole block is 1 solid color */
  499. CHECK_STREAM_PTR(1);
  500. pix = *s->stream_ptr++;
  501. for (y = 0; y < 8; y++) {
  502. memset(s->pixel_ptr, pix, 8);
  503. s->pixel_ptr += s->stride;
  504. }
  505. /* report success */
  506. return 0;
  507. }
  508. static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
  509. {
  510. int x, y;
  511. unsigned char sample[2];
  512. /* dithered encoding */
  513. CHECK_STREAM_PTR(2);
  514. sample[0] = *s->stream_ptr++;
  515. sample[1] = *s->stream_ptr++;
  516. for (y = 0; y < 8; y++) {
  517. for (x = 0; x < 8; x += 2) {
  518. *s->pixel_ptr++ = sample[ y & 1 ];
  519. *s->pixel_ptr++ = sample[!(y & 1)];
  520. }
  521. s->pixel_ptr += s->line_inc;
  522. }
  523. /* report success */
  524. return 0;
  525. }
  526. static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
  527. ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
  528. ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
  529. ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
  530. ipvideo_decode_block_opcode_0x6, ipvideo_decode_block_opcode_0x7,
  531. ipvideo_decode_block_opcode_0x8, ipvideo_decode_block_opcode_0x9,
  532. ipvideo_decode_block_opcode_0xA, ipvideo_decode_block_opcode_0xB,
  533. ipvideo_decode_block_opcode_0xC, ipvideo_decode_block_opcode_0xD,
  534. ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
  535. };
  536. static void ipvideo_decode_opcodes(IpvideoContext *s)
  537. {
  538. int x, y;
  539. int index = 0;
  540. unsigned char opcode;
  541. int ret;
  542. int code_counts[16] = {0};
  543. static int frame = 0;
  544. debug_interplay("------------------ frame %d\n", frame);
  545. frame++;
  546. /* this is PAL8, so make the palette available */
  547. memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
  548. s->stride = s->current_frame.linesize[0];
  549. s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
  550. s->stream_end = s->buf + s->size;
  551. s->line_inc = s->stride - 8;
  552. s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
  553. + s->avctx->width - 8;
  554. for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
  555. for (x = y; x < y + s->avctx->width; x += 8) {
  556. /* bottom nibble first, then top nibble (which makes it
  557. * hard to use a GetBitcontext) */
  558. if (index & 1)
  559. opcode = s->decoding_map[index >> 1] >> 4;
  560. else
  561. opcode = s->decoding_map[index >> 1] & 0xF;
  562. index++;
  563. debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
  564. x - y, y / s->stride, opcode, s->stream_ptr);
  565. code_counts[opcode]++;
  566. s->pixel_ptr = s->current_frame.data[0] + x;
  567. ret = ipvideo_decode_block[opcode](s);
  568. if (ret != 0) {
  569. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
  570. frame, x - y, y / s->stride);
  571. return;
  572. }
  573. }
  574. }
  575. if (s->stream_end - s->stream_ptr > 1) {
  576. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
  577. s->stream_end - s->stream_ptr);
  578. }
  579. }
  580. static av_cold int ipvideo_decode_init(AVCodecContext *avctx)
  581. {
  582. IpvideoContext *s = avctx->priv_data;
  583. s->avctx = avctx;
  584. if (s->avctx->palctrl == NULL) {
  585. av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
  586. return -1;
  587. }
  588. avctx->pix_fmt = PIX_FMT_PAL8;
  589. dsputil_init(&s->dsp, avctx);
  590. /* decoding map contains 4 bits of information per 8x8 block */
  591. s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
  592. s->current_frame.data[0] = s->last_frame.data[0] =
  593. s->second_last_frame.data[0] = NULL;
  594. return 0;
  595. }
  596. static int ipvideo_decode_frame(AVCodecContext *avctx,
  597. void *data, int *data_size,
  598. const uint8_t *buf, int buf_size)
  599. {
  600. IpvideoContext *s = avctx->priv_data;
  601. AVPaletteControl *palette_control = avctx->palctrl;
  602. /* compressed buffer needs to be large enough to at least hold an entire
  603. * decoding map */
  604. if (buf_size < s->decoding_map_size)
  605. return buf_size;
  606. s->decoding_map = buf;
  607. s->buf = buf + s->decoding_map_size;
  608. s->size = buf_size - s->decoding_map_size;
  609. s->current_frame.reference = 3;
  610. if (avctx->get_buffer(avctx, &s->current_frame)) {
  611. av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
  612. return -1;
  613. }
  614. ipvideo_decode_opcodes(s);
  615. if (palette_control->palette_changed) {
  616. palette_control->palette_changed = 0;
  617. s->current_frame.palette_has_changed = 1;
  618. }
  619. *data_size = sizeof(AVFrame);
  620. *(AVFrame*)data = s->current_frame;
  621. /* shuffle frames */
  622. if (s->second_last_frame.data[0])
  623. avctx->release_buffer(avctx, &s->second_last_frame);
  624. s->second_last_frame = s->last_frame;
  625. s->last_frame = s->current_frame;
  626. s->current_frame.data[0] = NULL; /* catch any access attempts */
  627. /* report that the buffer was completely consumed */
  628. return buf_size;
  629. }
  630. static av_cold int ipvideo_decode_end(AVCodecContext *avctx)
  631. {
  632. IpvideoContext *s = avctx->priv_data;
  633. /* release the last frame */
  634. if (s->last_frame.data[0])
  635. avctx->release_buffer(avctx, &s->last_frame);
  636. if (s->second_last_frame.data[0])
  637. avctx->release_buffer(avctx, &s->second_last_frame);
  638. return 0;
  639. }
  640. AVCodec interplay_video_decoder = {
  641. "interplayvideo",
  642. CODEC_TYPE_VIDEO,
  643. CODEC_ID_INTERPLAY_VIDEO,
  644. sizeof(IpvideoContext),
  645. ipvideo_decode_init,
  646. NULL,
  647. ipvideo_decode_end,
  648. ipvideo_decode_frame,
  649. CODEC_CAP_DR1,
  650. .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE video"),
  651. };