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.

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