interplayvideo.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * Interplay MVE Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file interplayvideo.c
  24. * Interplay MVE Video Decoder by Mike Melanson (melanson@pcisys.net)
  25. * For more information about the Interplay MVE format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
  27. * This code is written in such a way that the identifiers match up
  28. * with the encoding descriptions in the document.
  29. *
  30. * This decoder presently only supports a PAL8 output colorspace.
  31. *
  32. * An Interplay video frame consists of 2 parts: The decoding map and
  33. * the video data. A demuxer must load these 2 parts together in a single
  34. * buffer before sending it through the stream to this decoder.
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include "common.h"
  41. #include "avcodec.h"
  42. #include "dsputil.h"
  43. #define PALETTE_COUNT 256
  44. /* debugging support */
  45. #define DEBUG_INTERPLAY 0
  46. #if DEBUG_INTERPLAY
  47. #define debug_interplay(x,...) av_log(NULL, AV_LOG_DEBUG, x, __VA_ARGS__)
  48. #else
  49. static inline void debug_interplay(const char *format, ...) { }
  50. #endif
  51. typedef struct IpvideoContext {
  52. AVCodecContext *avctx;
  53. DSPContext dsp;
  54. AVFrame second_last_frame;
  55. AVFrame last_frame;
  56. AVFrame current_frame;
  57. unsigned char *decoding_map;
  58. int decoding_map_size;
  59. unsigned char *buf;
  60. int size;
  61. unsigned char *stream_ptr;
  62. unsigned char *stream_end;
  63. unsigned char *pixel_ptr;
  64. int line_inc;
  65. int stride;
  66. int upper_motion_limit_offset;
  67. } IpvideoContext;
  68. #define CHECK_STREAM_PTR(n) \
  69. if ((s->stream_ptr + n) > s->stream_end) { \
  70. av_log(s->avctx, AV_LOG_ERROR, "Interplay video warning: stream_ptr out of bounds (%p >= %p)\n", \
  71. s->stream_ptr + n, s->stream_end); \
  72. return -1; \
  73. }
  74. #define COPY_FROM_CURRENT() \
  75. motion_offset = current_offset; \
  76. motion_offset += y * s->stride; \
  77. motion_offset += x; \
  78. if (motion_offset < 0) { \
  79. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset); \
  80. return -1; \
  81. } else if (motion_offset > s->upper_motion_limit_offset) { \
  82. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n", \
  83. motion_offset, s->upper_motion_limit_offset); \
  84. return -1; \
  85. } \
  86. s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
  87. s->current_frame.data[0] + motion_offset, s->stride, 8);
  88. #define COPY_FROM_PREVIOUS() \
  89. motion_offset = current_offset; \
  90. motion_offset += y * s->stride; \
  91. motion_offset += x; \
  92. if (motion_offset < 0) { \
  93. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset); \
  94. return -1; \
  95. } else if (motion_offset > s->upper_motion_limit_offset) { \
  96. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n", \
  97. motion_offset, s->upper_motion_limit_offset); \
  98. return -1; \
  99. } \
  100. s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
  101. s->last_frame.data[0] + motion_offset, s->stride, 8);
  102. #define COPY_FROM_SECOND_LAST() \
  103. motion_offset = current_offset; \
  104. motion_offset += y * s->stride; \
  105. motion_offset += x; \
  106. if (motion_offset < 0) { \
  107. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset < 0 (%d)\n", motion_offset); \
  108. return -1; \
  109. } else if (motion_offset > s->upper_motion_limit_offset) { \
  110. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: motion offset above limit (%d >= %d)\n", \
  111. motion_offset, s->upper_motion_limit_offset); \
  112. return -1; \
  113. } \
  114. s->dsp.put_pixels_tab[0][0](s->pixel_ptr, \
  115. s->second_last_frame.data[0] + motion_offset, s->stride, 8);
  116. static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
  117. {
  118. int x, y;
  119. int motion_offset;
  120. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  121. /* copy a block from the previous frame */
  122. x = y = 0;
  123. COPY_FROM_PREVIOUS();
  124. /* report success */
  125. return 0;
  126. }
  127. static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
  128. {
  129. int x, y;
  130. int motion_offset;
  131. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  132. /* copy block from 2 frames ago */
  133. x = y = 0;
  134. COPY_FROM_SECOND_LAST();
  135. /* report success */
  136. return 0;
  137. }
  138. static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
  139. {
  140. unsigned char B;
  141. int x, y;
  142. int motion_offset;
  143. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  144. /* copy block from 2 frames ago using a motion vector; need 1 more byte */
  145. CHECK_STREAM_PTR(1);
  146. B = *s->stream_ptr++;
  147. if (B < 56) {
  148. x = 8 + (B % 7);
  149. y = B / 7;
  150. } else {
  151. x = -14 + ((B - 56) % 29);
  152. y = 8 + ((B - 56) / 29);
  153. }
  154. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  155. COPY_FROM_SECOND_LAST();
  156. /* report success */
  157. return 0;
  158. }
  159. static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
  160. {
  161. unsigned char B;
  162. int x, y;
  163. int motion_offset;
  164. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  165. /* copy 8x8 block from current frame from an up/left block */
  166. /* need 1 more byte for motion */
  167. CHECK_STREAM_PTR(1);
  168. B = *s->stream_ptr++;
  169. if (B < 56) {
  170. x = -(8 + (B % 7));
  171. y = -(B / 7);
  172. } else {
  173. x = -(-14 + ((B - 56) % 29));
  174. y = -( 8 + ((B - 56) / 29));
  175. }
  176. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  177. COPY_FROM_CURRENT();
  178. /* report success */
  179. return 0;
  180. }
  181. static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
  182. {
  183. int x, y;
  184. unsigned char B, BL, BH;
  185. int motion_offset;
  186. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  187. /* copy a block from the previous frame; need 1 more byte */
  188. CHECK_STREAM_PTR(1);
  189. B = *s->stream_ptr++;
  190. BL = B & 0x0F;
  191. BH = (B >> 4) & 0x0F;
  192. x = -8 + BL;
  193. y = -8 + BH;
  194. debug_interplay (" motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
  195. COPY_FROM_PREVIOUS();
  196. /* report success */
  197. return 0;
  198. }
  199. static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
  200. {
  201. signed char x, y;
  202. int motion_offset;
  203. int current_offset = s->pixel_ptr - s->current_frame.data[0];
  204. /* copy a block from the previous frame using an expanded range;
  205. * need 2 more bytes */
  206. CHECK_STREAM_PTR(2);
  207. x = *s->stream_ptr++;
  208. y = *s->stream_ptr++;
  209. debug_interplay (" motion bytes = %d, %d\n", x, y);
  210. COPY_FROM_PREVIOUS();
  211. /* report success */
  212. return 0;
  213. }
  214. static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
  215. {
  216. /* mystery opcode? skip multiple blocks? */
  217. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: Help! Mystery opcode 0x6 seen\n");
  218. /* report success */
  219. return 0;
  220. }
  221. static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
  222. {
  223. int x, y;
  224. unsigned char P0, P1;
  225. unsigned char B[8];
  226. unsigned int flags;
  227. int bitmask;
  228. /* 2-color encoding */
  229. CHECK_STREAM_PTR(2);
  230. P0 = *s->stream_ptr++;
  231. P1 = *s->stream_ptr++;
  232. if (P0 <= P1) {
  233. /* need 8 more bytes from the stream */
  234. CHECK_STREAM_PTR(8);
  235. for (y = 0; y < 8; y++)
  236. B[y] = *s->stream_ptr++;
  237. for (y = 0; y < 8; y++) {
  238. flags = B[y];
  239. for (x = 0x01; x <= 0x80; x <<= 1) {
  240. if (flags & x)
  241. *s->pixel_ptr++ = P1;
  242. else
  243. *s->pixel_ptr++ = P0;
  244. }
  245. s->pixel_ptr += s->line_inc;
  246. }
  247. } else {
  248. /* need 2 more bytes from the stream */
  249. CHECK_STREAM_PTR(2);
  250. B[0] = *s->stream_ptr++;
  251. B[1] = *s->stream_ptr++;
  252. flags = (B[1] << 8) | B[0];
  253. bitmask = 0x0001;
  254. for (y = 0; y < 8; y += 2) {
  255. for (x = 0; x < 8; x += 2, bitmask <<= 1) {
  256. if (flags & bitmask) {
  257. *(s->pixel_ptr + x) = P1;
  258. *(s->pixel_ptr + x + 1) = P1;
  259. *(s->pixel_ptr + s->stride + x) = P1;
  260. *(s->pixel_ptr + s->stride + x + 1) = P1;
  261. } else {
  262. *(s->pixel_ptr + x) = P0;
  263. *(s->pixel_ptr + x + 1) = P0;
  264. *(s->pixel_ptr + s->stride + x) = P0;
  265. *(s->pixel_ptr + s->stride + x + 1) = P0;
  266. }
  267. }
  268. s->pixel_ptr += s->stride * 2;
  269. }
  270. }
  271. /* report success */
  272. return 0;
  273. }
  274. static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
  275. {
  276. int x, y;
  277. unsigned char P[8];
  278. unsigned char B[8];
  279. unsigned int flags = 0;
  280. unsigned int bitmask = 0;
  281. unsigned char P0 = 0, P1 = 0;
  282. int lower_half = 0;
  283. /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
  284. * either top and bottom or left and right halves */
  285. CHECK_STREAM_PTR(2);
  286. P[0] = *s->stream_ptr++;
  287. P[1] = *s->stream_ptr++;
  288. if (P[0] <= P[1]) {
  289. /* need 12 more bytes */
  290. CHECK_STREAM_PTR(12);
  291. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  292. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  293. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  294. P[4] = *s->stream_ptr++; P[5] = *s->stream_ptr++;
  295. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  296. P[6] = *s->stream_ptr++; P[7] = *s->stream_ptr++;
  297. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  298. for (y = 0; y < 8; y++) {
  299. /* time to reload flags? */
  300. if (y == 0) {
  301. flags =
  302. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  303. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  304. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  305. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  306. bitmask = 0x00000001;
  307. lower_half = 0; /* still on top half */
  308. } else if (y == 4) {
  309. flags =
  310. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  311. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  312. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  313. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  314. bitmask = 0x00000001;
  315. lower_half = 2;
  316. }
  317. for (x = 0; x < 8; x++, bitmask <<= 1) {
  318. /* get the pixel values ready for this quadrant */
  319. if (x == 0) {
  320. P0 = P[lower_half + 0];
  321. P1 = P[lower_half + 1];
  322. } else if (x == 4) {
  323. P0 = P[lower_half + 4];
  324. P1 = P[lower_half + 5];
  325. }
  326. if (flags & bitmask)
  327. *s->pixel_ptr++ = P1;
  328. else
  329. *s->pixel_ptr++ = P0;
  330. }
  331. s->pixel_ptr += s->line_inc;
  332. }
  333. } else {
  334. /* need 10 more bytes */
  335. CHECK_STREAM_PTR(10);
  336. B[0] = *s->stream_ptr++; B[1] = *s->stream_ptr++;
  337. B[2] = *s->stream_ptr++; B[3] = *s->stream_ptr++;
  338. P[2] = *s->stream_ptr++; P[3] = *s->stream_ptr++;
  339. B[4] = *s->stream_ptr++; B[5] = *s->stream_ptr++;
  340. B[6] = *s->stream_ptr++; B[7] = *s->stream_ptr++;
  341. if (P[2] <= P[3]) {
  342. /* vertical split; left & right halves are 2-color encoded */
  343. for (y = 0; y < 8; y++) {
  344. /* time to reload flags? */
  345. if (y == 0) {
  346. flags =
  347. ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
  348. ((B[0] & 0x0F) ) | ((B[4] & 0x0F) << 4) |
  349. ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
  350. ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
  351. bitmask = 0x00000001;
  352. } else if (y == 4) {
  353. flags =
  354. ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
  355. ((B[2] & 0x0F) ) | ((B[6] & 0x0F) << 4) |
  356. ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
  357. ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
  358. bitmask = 0x00000001;
  359. }
  360. for (x = 0; x < 8; x++, bitmask <<= 1) {
  361. /* get the pixel values ready for this half */
  362. if (x == 0) {
  363. P0 = P[0];
  364. P1 = P[1];
  365. } else if (x == 4) {
  366. P0 = P[2];
  367. P1 = P[3];
  368. }
  369. if (flags & bitmask)
  370. *s->pixel_ptr++ = P1;
  371. else
  372. *s->pixel_ptr++ = P0;
  373. }
  374. s->pixel_ptr += s->line_inc;
  375. }
  376. } else {
  377. /* horizontal split; top & bottom halves are 2-color encoded */
  378. for (y = 0; y < 8; y++) {
  379. flags = B[y];
  380. if (y == 0) {
  381. P0 = P[0];
  382. P1 = P[1];
  383. } else if (y == 4) {
  384. P0 = P[2];
  385. P1 = P[3];
  386. }
  387. for (bitmask = 0x01; bitmask <= 0x80; bitmask <<= 1) {
  388. if (flags & bitmask)
  389. *s->pixel_ptr++ = P1;
  390. else
  391. *s->pixel_ptr++ = P0;
  392. }
  393. s->pixel_ptr += s->line_inc;
  394. }
  395. }
  396. }
  397. /* report success */
  398. return 0;
  399. }
  400. static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
  401. {
  402. int x, y;
  403. unsigned char P[4];
  404. unsigned char B[4];
  405. unsigned int flags = 0;
  406. int shifter = 0;
  407. unsigned char pix;
  408. /* 4-color encoding */
  409. CHECK_STREAM_PTR(4);
  410. for (y = 0; y < 4; y++)
  411. P[y] = *s->stream_ptr++;
  412. if ((P[0] <= P[1]) && (P[2] <= P[3])) {
  413. /* 1 of 4 colors for each pixel, need 16 more bytes */
  414. CHECK_STREAM_PTR(16);
  415. for (y = 0; y < 8; y++) {
  416. /* get the next set of 8 2-bit flags */
  417. flags = (s->stream_ptr[1] << 8) | s->stream_ptr[0];
  418. s->stream_ptr += 2;
  419. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  420. *s->pixel_ptr++ = P[(flags >> shifter) & 0x03];
  421. }
  422. s->pixel_ptr += s->line_inc;
  423. }
  424. } else if ((P[0] <= P[1]) && (P[2] > P[3])) {
  425. /* 1 of 4 colors for each 2x2 block, need 4 more bytes */
  426. CHECK_STREAM_PTR(4);
  427. B[0] = *s->stream_ptr++;
  428. B[1] = *s->stream_ptr++;
  429. B[2] = *s->stream_ptr++;
  430. B[3] = *s->stream_ptr++;
  431. flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
  432. shifter = 0;
  433. for (y = 0; y < 8; y += 2) {
  434. for (x = 0; x < 8; x += 2, shifter += 2) {
  435. pix = P[(flags >> shifter) & 0x03];
  436. *(s->pixel_ptr + x) = pix;
  437. *(s->pixel_ptr + x + 1) = pix;
  438. *(s->pixel_ptr + s->stride + x) = pix;
  439. *(s->pixel_ptr + s->stride + x + 1) = pix;
  440. }
  441. s->pixel_ptr += s->stride * 2;
  442. }
  443. } else if ((P[0] > P[1]) && (P[2] <= P[3])) {
  444. /* 1 of 4 colors for each 2x1 block, need 8 more bytes */
  445. CHECK_STREAM_PTR(8);
  446. for (y = 0; y < 8; y++) {
  447. /* time to reload flags? */
  448. if ((y == 0) || (y == 4)) {
  449. B[0] = *s->stream_ptr++;
  450. B[1] = *s->stream_ptr++;
  451. B[2] = *s->stream_ptr++;
  452. B[3] = *s->stream_ptr++;
  453. flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
  454. shifter = 0;
  455. }
  456. for (x = 0; x < 8; x += 2, shifter += 2) {
  457. pix = P[(flags >> shifter) & 0x03];
  458. *(s->pixel_ptr + x) = pix;
  459. *(s->pixel_ptr + x + 1) = pix;
  460. }
  461. s->pixel_ptr += s->stride;
  462. }
  463. } else {
  464. /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
  465. CHECK_STREAM_PTR(8);
  466. for (y = 0; y < 8; y += 2) {
  467. /* time to reload flags? */
  468. if ((y == 0) || (y == 4)) {
  469. B[0] = *s->stream_ptr++;
  470. B[1] = *s->stream_ptr++;
  471. B[2] = *s->stream_ptr++;
  472. B[3] = *s->stream_ptr++;
  473. flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
  474. shifter = 0;
  475. }
  476. for (x = 0; x < 8; x++, shifter += 2) {
  477. pix = P[(flags >> shifter) & 0x03];
  478. *(s->pixel_ptr + x) = pix;
  479. *(s->pixel_ptr + s->stride + x) = pix;
  480. }
  481. s->pixel_ptr += s->stride * 2;
  482. }
  483. }
  484. /* report success */
  485. return 0;
  486. }
  487. static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
  488. {
  489. int x, y;
  490. unsigned char P[16];
  491. unsigned char B[16];
  492. int flags = 0;
  493. int shifter = 0;
  494. int index;
  495. int split;
  496. int lower_half;
  497. /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
  498. * either top and bottom or left and right halves */
  499. CHECK_STREAM_PTR(4);
  500. for (y = 0; y < 4; y++)
  501. P[y] = *s->stream_ptr++;
  502. if (P[0] <= P[1]) {
  503. /* 4-color encoding for each quadrant; need 28 more bytes */
  504. CHECK_STREAM_PTR(28);
  505. for (y = 0; y < 4; y++)
  506. B[y] = *s->stream_ptr++;
  507. for (y = 4; y < 16; y += 4) {
  508. for (x = y; x < y + 4; x++)
  509. P[x] = *s->stream_ptr++;
  510. for (x = y; x < y + 4; x++)
  511. B[x] = *s->stream_ptr++;
  512. }
  513. for (y = 0; y < 8; y++) {
  514. lower_half = (y >= 4) ? 4 : 0;
  515. flags = (B[y + 8] << 8) | B[y];
  516. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  517. split = (x >= 4) ? 8 : 0;
  518. index = split + lower_half + ((flags >> shifter) & 0x03);
  519. *s->pixel_ptr++ = P[index];
  520. }
  521. s->pixel_ptr += s->line_inc;
  522. }
  523. } else {
  524. /* 4-color encoding for either left and right or top and bottom
  525. * halves; need 20 more bytes */
  526. CHECK_STREAM_PTR(20);
  527. for (y = 0; y < 8; y++)
  528. B[y] = *s->stream_ptr++;
  529. for (y = 4; y < 8; y++)
  530. P[y] = *s->stream_ptr++;
  531. for (y = 8; y < 16; y++)
  532. B[y] = *s->stream_ptr++;
  533. if (P[4] <= P[5]) {
  534. /* block is divided into left and right halves */
  535. for (y = 0; y < 8; y++) {
  536. flags = (B[y + 8] << 8) | B[y];
  537. split = 0;
  538. for (x = 0, shifter = 0; x < 8; x++, shifter += 2) {
  539. if (x == 4)
  540. split = 4;
  541. *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
  542. }
  543. s->pixel_ptr += s->line_inc;
  544. }
  545. } else {
  546. /* block is divided into top and bottom halves */
  547. split = 0;
  548. for (y = 0; y < 8; y++) {
  549. flags = (B[y * 2 + 1] << 8) | B[y * 2];
  550. if (y == 4)
  551. split = 4;
  552. for (x = 0, shifter = 0; x < 8; x++, shifter += 2)
  553. *s->pixel_ptr++ = P[split + ((flags >> shifter) & 0x03)];
  554. s->pixel_ptr += s->line_inc;
  555. }
  556. }
  557. }
  558. /* report success */
  559. return 0;
  560. }
  561. static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
  562. {
  563. int x, y;
  564. /* 64-color encoding (each pixel in block is a different color) */
  565. CHECK_STREAM_PTR(64);
  566. for (y = 0; y < 8; y++) {
  567. for (x = 0; x < 8; x++) {
  568. *s->pixel_ptr++ = *s->stream_ptr++;
  569. }
  570. s->pixel_ptr += s->line_inc;
  571. }
  572. /* report success */
  573. return 0;
  574. }
  575. static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
  576. {
  577. int x, y;
  578. unsigned char pix;
  579. /* 16-color block encoding: each 2x2 block is a different color */
  580. CHECK_STREAM_PTR(16);
  581. for (y = 0; y < 8; y += 2) {
  582. for (x = 0; x < 8; x += 2) {
  583. pix = *s->stream_ptr++;
  584. *(s->pixel_ptr + x) = pix;
  585. *(s->pixel_ptr + x + 1) = pix;
  586. *(s->pixel_ptr + s->stride + x) = pix;
  587. *(s->pixel_ptr + s->stride + x + 1) = pix;
  588. }
  589. s->pixel_ptr += s->stride * 2;
  590. }
  591. /* report success */
  592. return 0;
  593. }
  594. static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
  595. {
  596. int x, y;
  597. unsigned char P[4];
  598. unsigned char index = 0;
  599. /* 4-color block encoding: each 4x4 block is a different color */
  600. CHECK_STREAM_PTR(4);
  601. for (y = 0; y < 4; y++)
  602. P[y] = *s->stream_ptr++;
  603. for (y = 0; y < 8; y++) {
  604. if (y < 4)
  605. index = 0;
  606. else
  607. index = 2;
  608. for (x = 0; x < 8; x++) {
  609. if (x == 4)
  610. index++;
  611. *s->pixel_ptr++ = P[index];
  612. }
  613. s->pixel_ptr += s->line_inc;
  614. }
  615. /* report success */
  616. return 0;
  617. }
  618. static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
  619. {
  620. int x, y;
  621. unsigned char pix;
  622. /* 1-color encoding: the whole block is 1 solid color */
  623. CHECK_STREAM_PTR(1);
  624. pix = *s->stream_ptr++;
  625. for (y = 0; y < 8; y++) {
  626. for (x = 0; x < 8; x++) {
  627. *s->pixel_ptr++ = pix;
  628. }
  629. s->pixel_ptr += s->line_inc;
  630. }
  631. /* report success */
  632. return 0;
  633. }
  634. static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
  635. {
  636. int x, y;
  637. unsigned char sample0, sample1;
  638. /* dithered encoding */
  639. CHECK_STREAM_PTR(2);
  640. sample0 = *s->stream_ptr++;
  641. sample1 = *s->stream_ptr++;
  642. for (y = 0; y < 8; y++) {
  643. for (x = 0; x < 8; x += 2) {
  644. if (y & 1) {
  645. *s->pixel_ptr++ = sample1;
  646. *s->pixel_ptr++ = sample0;
  647. } else {
  648. *s->pixel_ptr++ = sample0;
  649. *s->pixel_ptr++ = sample1;
  650. }
  651. }
  652. s->pixel_ptr += s->line_inc;
  653. }
  654. /* report success */
  655. return 0;
  656. }
  657. static int (*ipvideo_decode_block[16])(IpvideoContext *s);
  658. static void ipvideo_decode_opcodes(IpvideoContext *s)
  659. {
  660. int x, y;
  661. int index = 0;
  662. unsigned char opcode;
  663. int ret;
  664. int code_counts[16];
  665. static int frame = 0;
  666. debug_interplay("------------------ frame %d\n", frame);
  667. frame++;
  668. for (x = 0; x < 16; x++)
  669. code_counts[x] = 0;
  670. /* this is PAL8, so make the palette available */
  671. memcpy(s->current_frame.data[1], s->avctx->palctrl->palette, PALETTE_COUNT * 4);
  672. s->stride = s->current_frame.linesize[0];
  673. s->stream_ptr = s->buf + 14; /* data starts 14 bytes in */
  674. s->stream_end = s->buf + s->size;
  675. s->line_inc = s->stride - 8;
  676. s->upper_motion_limit_offset = (s->avctx->height - 8) * s->stride
  677. + s->avctx->width - 8;
  678. s->dsp = s->dsp;
  679. for (y = 0; y < (s->stride * s->avctx->height); y += s->stride * 8) {
  680. for (x = y; x < y + s->avctx->width; x += 8) {
  681. /* bottom nibble first, then top nibble (which makes it
  682. * hard to use a GetBitcontext) */
  683. if (index & 1)
  684. opcode = s->decoding_map[index >> 1] >> 4;
  685. else
  686. opcode = s->decoding_map[index >> 1] & 0xF;
  687. index++;
  688. debug_interplay(" block @ (%3d, %3d): encoding 0x%X, data ptr @ %p\n",
  689. x - y, y / s->stride, opcode, s->stream_ptr);
  690. code_counts[opcode]++;
  691. s->pixel_ptr = s->current_frame.data[0] + x;
  692. ret = ipvideo_decode_block[opcode](s);
  693. if (ret != 0) {
  694. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode problem on frame %d, @ block (%d, %d)\n",
  695. frame, x - y, y / s->stride);
  696. return;
  697. }
  698. }
  699. }
  700. if ((s->stream_ptr != s->stream_end) &&
  701. (s->stream_ptr + 1 != s->stream_end)) {
  702. av_log(s->avctx, AV_LOG_ERROR, " Interplay video: decode finished with %td bytes left over\n",
  703. s->stream_end - s->stream_ptr);
  704. }
  705. }
  706. static int ipvideo_decode_init(AVCodecContext *avctx)
  707. {
  708. IpvideoContext *s = avctx->priv_data;
  709. s->avctx = avctx;
  710. if (s->avctx->palctrl == NULL) {
  711. av_log(avctx, AV_LOG_ERROR, " Interplay video: palette expected.\n");
  712. return -1;
  713. }
  714. avctx->pix_fmt = PIX_FMT_PAL8;
  715. dsputil_init(&s->dsp, avctx);
  716. /* decoding map contains 4 bits of information per 8x8 block */
  717. s->decoding_map_size = avctx->width * avctx->height / (8 * 8 * 2);
  718. /* assign block decode functions */
  719. ipvideo_decode_block[0x0] = ipvideo_decode_block_opcode_0x0;
  720. ipvideo_decode_block[0x1] = ipvideo_decode_block_opcode_0x1;
  721. ipvideo_decode_block[0x2] = ipvideo_decode_block_opcode_0x2;
  722. ipvideo_decode_block[0x3] = ipvideo_decode_block_opcode_0x3;
  723. ipvideo_decode_block[0x4] = ipvideo_decode_block_opcode_0x4;
  724. ipvideo_decode_block[0x5] = ipvideo_decode_block_opcode_0x5;
  725. ipvideo_decode_block[0x6] = ipvideo_decode_block_opcode_0x6;
  726. ipvideo_decode_block[0x7] = ipvideo_decode_block_opcode_0x7;
  727. ipvideo_decode_block[0x8] = ipvideo_decode_block_opcode_0x8;
  728. ipvideo_decode_block[0x9] = ipvideo_decode_block_opcode_0x9;
  729. ipvideo_decode_block[0xA] = ipvideo_decode_block_opcode_0xA;
  730. ipvideo_decode_block[0xB] = ipvideo_decode_block_opcode_0xB;
  731. ipvideo_decode_block[0xC] = ipvideo_decode_block_opcode_0xC;
  732. ipvideo_decode_block[0xD] = ipvideo_decode_block_opcode_0xD;
  733. ipvideo_decode_block[0xE] = ipvideo_decode_block_opcode_0xE;
  734. ipvideo_decode_block[0xF] = ipvideo_decode_block_opcode_0xF;
  735. s->current_frame.data[0] = s->last_frame.data[0] =
  736. s->second_last_frame.data[0] = NULL;
  737. return 0;
  738. }
  739. static int ipvideo_decode_frame(AVCodecContext *avctx,
  740. void *data, int *data_size,
  741. uint8_t *buf, int buf_size)
  742. {
  743. IpvideoContext *s = avctx->priv_data;
  744. AVPaletteControl *palette_control = avctx->palctrl;
  745. /* compressed buffer needs to be large enough to at least hold an entire
  746. * decoding map */
  747. if (buf_size < s->decoding_map_size)
  748. return buf_size;
  749. s->decoding_map = buf;
  750. s->buf = buf + s->decoding_map_size;
  751. s->size = buf_size - s->decoding_map_size;
  752. s->current_frame.reference = 3;
  753. if (avctx->get_buffer(avctx, &s->current_frame)) {
  754. av_log(avctx, AV_LOG_ERROR, " Interplay Video: get_buffer() failed\n");
  755. return -1;
  756. }
  757. ipvideo_decode_opcodes(s);
  758. if (palette_control->palette_changed) {
  759. palette_control->palette_changed = 0;
  760. s->current_frame.palette_has_changed = 1;
  761. }
  762. *data_size = sizeof(AVFrame);
  763. *(AVFrame*)data = s->current_frame;
  764. /* shuffle frames */
  765. if (s->second_last_frame.data[0])
  766. avctx->release_buffer(avctx, &s->second_last_frame);
  767. s->second_last_frame = s->last_frame;
  768. s->last_frame = s->current_frame;
  769. s->current_frame.data[0] = NULL; /* catch any access attempts */
  770. /* report that the buffer was completely consumed */
  771. return buf_size;
  772. }
  773. static int ipvideo_decode_end(AVCodecContext *avctx)
  774. {
  775. IpvideoContext *s = avctx->priv_data;
  776. /* release the last frame */
  777. if (s->last_frame.data[0])
  778. avctx->release_buffer(avctx, &s->last_frame);
  779. if (s->second_last_frame.data[0])
  780. avctx->release_buffer(avctx, &s->second_last_frame);
  781. return 0;
  782. }
  783. AVCodec interplay_video_decoder = {
  784. "interplayvideo",
  785. CODEC_TYPE_VIDEO,
  786. CODEC_ID_INTERPLAY_VIDEO,
  787. sizeof(IpvideoContext),
  788. ipvideo_decode_init,
  789. NULL,
  790. ipvideo_decode_end,
  791. ipvideo_decode_frame,
  792. CODEC_CAP_DR1,
  793. };