interplayvideo.c 27 KB

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