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.

462 lines
16KB

  1. /*
  2. * FLI/FLC Animation Video Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file flic.c
  22. * Autodesk Animator FLI/FLC Video Decoder
  23. * by Mike Melanson (melanson@pcisys.net)
  24. * for more information on the .fli/.flc file format and all of its many
  25. * variations, visit:
  26. * http://www.compuphase.com/flic.htm
  27. *
  28. * This decoder outputs PAL8 colorspace data. To use this decoder, be
  29. * sure that your demuxer sends the FLI file header to the decoder via
  30. * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
  31. * large. The only exception is for FLI files from the game "Magic Carpet",
  32. * in which the header is only 12 bytes.
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include "common.h"
  39. #include "avcodec.h"
  40. #include "bswap.h"
  41. #define FLI_256_COLOR 4
  42. #define FLI_DELTA 7
  43. #define FLI_COLOR 11
  44. #define FLI_LC 12
  45. #define FLI_BLACK 13
  46. #define FLI_BRUN 15
  47. #define FLI_COPY 16
  48. #define FLI_MINI 18
  49. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  50. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  51. (((uint8_t*)(x))[2] << 16) | \
  52. (((uint8_t*)(x))[1] << 8) | \
  53. ((uint8_t*)(x))[0])
  54. typedef struct FlicDecodeContext {
  55. AVCodecContext *avctx;
  56. AVFrame frame;
  57. AVFrame prev_frame;
  58. unsigned int palette[256];
  59. int new_palette;
  60. int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
  61. } FlicDecodeContext;
  62. static int flic_decode_init(AVCodecContext *avctx)
  63. {
  64. FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
  65. unsigned char *fli_header = (unsigned char *)avctx->extradata;
  66. s->avctx = avctx;
  67. avctx->pix_fmt = PIX_FMT_PAL8;
  68. avctx->has_b_frames = 0;
  69. if (s->avctx->extradata_size == 12) {
  70. /* special case for magic carpet FLIs */
  71. s->fli_type = 0xAF13;
  72. } else if (s->avctx->extradata_size == 128) {
  73. s->fli_type = LE_16(&fli_header[4]);
  74. } else {
  75. printf (" FLI video: expected extradata of 12 or 128 bytes\n");
  76. return -1;
  77. }
  78. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  79. s->new_palette = 0;
  80. return 0;
  81. }
  82. static int flic_decode_frame(AVCodecContext *avctx,
  83. void *data, int *data_size,
  84. uint8_t *buf, int buf_size)
  85. {
  86. FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
  87. int stream_ptr = 0;
  88. int stream_ptr_after_color_chunk;
  89. int pixel_ptr;
  90. int palette_ptr;
  91. unsigned char palette_idx1;
  92. unsigned char palette_idx2;
  93. unsigned int frame_size;
  94. int num_chunks;
  95. unsigned int chunk_size;
  96. int chunk_type;
  97. int i, j;
  98. int color_packets;
  99. int color_changes;
  100. int color_shift;
  101. unsigned char r, g, b;
  102. int lines;
  103. int compressed_lines;
  104. int starting_line;
  105. signed short line_packets;
  106. int y_ptr;
  107. signed char byte_run;
  108. int pixel_skip;
  109. int pixel_countdown;
  110. int height_countdown;
  111. unsigned char *pixels;
  112. unsigned char *prev_pixels;
  113. s->frame.reference = 1;
  114. if (avctx->get_buffer(avctx, &s->frame) < 0) {
  115. fprintf(stderr, " FLI: get_buffer() failed\n");
  116. return -1;
  117. }
  118. pixels = s->frame.data[0];
  119. prev_pixels = s->prev_frame.data[0];
  120. frame_size = LE_32(&buf[stream_ptr]);
  121. stream_ptr += 6; /* skip the magic number */
  122. num_chunks = LE_16(&buf[stream_ptr]);
  123. stream_ptr += 10; /* skip padding */
  124. frame_size -= 16;
  125. /* if there is no data, copy previous frame */
  126. if (frame_size == 0) {
  127. memcpy(pixels, prev_pixels, s->frame.linesize[0] * s->avctx->height);
  128. }
  129. /* iterate through the chunks */
  130. while ((frame_size > 0) && (num_chunks > 0)) {
  131. chunk_size = LE_32(&buf[stream_ptr]);
  132. stream_ptr += 4;
  133. chunk_type = LE_16(&buf[stream_ptr]);
  134. stream_ptr += 2;
  135. switch (chunk_type) {
  136. case FLI_256_COLOR:
  137. case FLI_COLOR:
  138. stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
  139. s->new_palette = 1;
  140. /* check special case: If this file is from the Magic Carpet
  141. * game and uses 6-bit colors even though it reports 256-color
  142. * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
  143. * initialization) */
  144. if ((chunk_type == FLI_256_COLOR) && (s->fli_type != 0xAF13))
  145. color_shift = 0;
  146. else
  147. color_shift = 2;
  148. /* set up the palette */
  149. color_packets = LE_16(&buf[stream_ptr]);
  150. stream_ptr += 2;
  151. palette_ptr = 0;
  152. for (i = 0; i < color_packets; i++) {
  153. /* first byte is how many colors to skip */
  154. palette_ptr += buf[stream_ptr++];
  155. /* next byte indicates how many entries to change */
  156. color_changes = buf[stream_ptr++];
  157. /* if there are 0 color changes, there are actually 256 */
  158. if (color_changes == 0)
  159. color_changes = 256;
  160. for (j = 0; j < color_changes; j++) {
  161. /* wrap around, for good measure */
  162. if (palette_ptr >= 256)
  163. palette_ptr = 0;
  164. r = buf[stream_ptr++] << color_shift;
  165. g = buf[stream_ptr++] << color_shift;
  166. b = buf[stream_ptr++] << color_shift;
  167. s->palette[palette_ptr++] = (r << 16) | (g << 8) | b;
  168. }
  169. }
  170. /* color chunks sometimes have weird 16-bit alignment issues;
  171. * therefore, take the hardline approach and set the stream_ptr
  172. * to the value calculated w.r.t. the size specified by the color
  173. * chunk header */
  174. stream_ptr = stream_ptr_after_color_chunk;
  175. break;
  176. case FLI_DELTA:
  177. y_ptr = 0;
  178. height_countdown = s->avctx->height;
  179. compressed_lines = LE_16(&buf[stream_ptr]);
  180. stream_ptr += 2;
  181. while (compressed_lines > 0) {
  182. line_packets = LE_16(&buf[stream_ptr]);
  183. stream_ptr += 2;
  184. if (line_packets < 0) {
  185. line_packets = -line_packets;
  186. /* height_countdown was already decremented once on
  187. * this iteration */
  188. height_countdown -= line_packets;
  189. /* copy the skipped lines from the previous frame */
  190. while (line_packets--) {
  191. memcpy(&pixels[y_ptr], &prev_pixels[y_ptr],
  192. s->avctx->width);
  193. y_ptr += s->frame.linesize[0];
  194. }
  195. } else {
  196. height_countdown--;
  197. compressed_lines--;
  198. pixel_ptr = y_ptr;
  199. pixel_countdown = s->avctx->width;
  200. for (i = 0; i < line_packets; i++) {
  201. /* account for the skip bytes */
  202. pixel_skip = buf[stream_ptr++];
  203. memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr],
  204. pixel_skip);
  205. pixel_ptr += pixel_skip;
  206. pixel_countdown -= pixel_skip;
  207. byte_run = buf[stream_ptr++];
  208. if (byte_run < 0) {
  209. byte_run = -byte_run;
  210. palette_idx1 = buf[stream_ptr++];
  211. palette_idx2 = buf[stream_ptr++];
  212. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  213. pixels[pixel_ptr++] = palette_idx1;
  214. pixels[pixel_ptr++] = palette_idx2;
  215. }
  216. } else {
  217. for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
  218. palette_idx1 = buf[stream_ptr++];
  219. pixels[pixel_ptr++] = palette_idx1;
  220. }
  221. }
  222. }
  223. /* copy the remaining pixels in the line from the
  224. * previous frame */
  225. memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr],
  226. pixel_countdown);
  227. y_ptr += s->frame.linesize[0];
  228. }
  229. }
  230. /* copy the remainder of the lines from the previous frame */
  231. while (height_countdown--) {
  232. memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width);
  233. y_ptr += s->frame.linesize[0];
  234. }
  235. break;
  236. case FLI_LC:
  237. /* line compressed */
  238. height_countdown = s->avctx->height;
  239. starting_line = LE_16(&buf[stream_ptr]);
  240. stream_ptr += 2;
  241. /* copy from the previous frame all of the lines prior to the
  242. * starting line */
  243. y_ptr = 0;
  244. height_countdown -= starting_line;
  245. while (starting_line--) {
  246. memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width);
  247. y_ptr += s->frame.linesize[0];
  248. }
  249. compressed_lines = LE_16(&buf[stream_ptr]);
  250. stream_ptr += 2;
  251. while (compressed_lines > 0) {
  252. pixel_ptr = y_ptr;
  253. pixel_countdown = s->avctx->width;
  254. line_packets = buf[stream_ptr++];
  255. if (line_packets > 0) {
  256. for (i = 0; i < line_packets; i++) {
  257. /* account for the skip bytes */
  258. pixel_skip = buf[stream_ptr++];
  259. memcpy(&pixels[pixel_ptr],
  260. &prev_pixels[pixel_ptr], pixel_skip);
  261. pixel_ptr += pixel_skip;
  262. pixel_countdown -= pixel_skip;
  263. byte_run = buf[stream_ptr++];
  264. if (byte_run > 0) {
  265. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  266. palette_idx1 = buf[stream_ptr++];
  267. pixels[pixel_ptr++] = palette_idx1;
  268. }
  269. } else {
  270. byte_run = -byte_run;
  271. palette_idx1 = buf[stream_ptr++];
  272. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  273. pixels[pixel_ptr++] = palette_idx1;
  274. }
  275. }
  276. }
  277. }
  278. /* copy the remainder of the line from the previous frame */
  279. memcpy(&pixels[pixel_ptr], &prev_pixels[pixel_ptr],
  280. pixel_countdown);
  281. y_ptr += s->frame.linesize[0];
  282. compressed_lines--;
  283. height_countdown--;
  284. }
  285. /* copy the remainder of the lines from the previous frame */
  286. while (height_countdown--) {
  287. memcpy(&pixels[y_ptr], &prev_pixels[y_ptr], s->avctx->width);
  288. y_ptr += s->frame.linesize[0];
  289. }
  290. break;
  291. case FLI_BLACK:
  292. /* set the whole frame to color 0 (which is usually black) */
  293. memset(pixels, 0,
  294. s->frame.linesize[0] * s->avctx->height);
  295. break;
  296. case FLI_BRUN:
  297. /* Byte run compression: This chunk type only occurs in the first
  298. * FLI frame and it will update the entire frame. */
  299. y_ptr = 0;
  300. for (lines = 0; lines < s->avctx->height; lines++) {
  301. pixel_ptr = y_ptr;
  302. /* disregard the line packets; instead, iterate through all
  303. * pixels on a row */
  304. stream_ptr++;
  305. pixel_countdown = s->avctx->width;
  306. while (pixel_countdown > 0) {
  307. byte_run = buf[stream_ptr++];
  308. if (byte_run > 0) {
  309. palette_idx1 = buf[stream_ptr++];
  310. for (j = 0; j < byte_run; j++) {
  311. pixels[pixel_ptr++] = palette_idx1;
  312. pixel_countdown--;
  313. if (pixel_countdown < 0)
  314. printf ("fli warning: pixel_countdown < 0 (%d)\n",
  315. pixel_countdown);
  316. }
  317. } else { /* copy bytes if byte_run < 0 */
  318. byte_run = -byte_run;
  319. for (j = 0; j < byte_run; j++) {
  320. palette_idx1 = buf[stream_ptr++];
  321. pixels[pixel_ptr++] = palette_idx1;
  322. pixel_countdown--;
  323. if (pixel_countdown < 0)
  324. printf ("fli warning: pixel_countdown < 0 (%d)\n",
  325. pixel_countdown);
  326. }
  327. }
  328. }
  329. y_ptr += s->frame.linesize[0];
  330. }
  331. break;
  332. case FLI_COPY:
  333. /* copy the chunk (uncompressed frame) to the ghost image and
  334. * schedule the whole frame to be updated */
  335. if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
  336. printf(
  337. "FLI: in chunk FLI_COPY : source data (%d bytes) bigger than" \
  338. " image, skipping chunk\n",
  339. chunk_size - 6);
  340. stream_ptr += chunk_size - 6;
  341. } else {
  342. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  343. y_ptr += s->frame.linesize[0]) {
  344. memcpy(&pixels[y_ptr], &buf[stream_ptr],
  345. s->avctx->width);
  346. stream_ptr += s->avctx->width;
  347. }
  348. }
  349. break;
  350. case FLI_MINI:
  351. /* some sort of a thumbnail? disregard this chunk... */
  352. stream_ptr += chunk_size - 6;
  353. break;
  354. default:
  355. printf ("FLI: Unrecognized chunk type: %d\n", chunk_type);
  356. break;
  357. }
  358. frame_size -= chunk_size;
  359. num_chunks--;
  360. }
  361. /* by the end of the chunk, the stream ptr should equal the frame
  362. * size (minus 1, possibly); if it doesn't, issue a warning */
  363. if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
  364. printf (" warning: processed FLI chunk where chunk size = %d\n" \
  365. " and final chunk ptr = %d\n",
  366. buf_size, stream_ptr);
  367. /* make the palette available on the way out */
  368. // if (s->new_palette) {
  369. if (1) {
  370. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  371. s->frame.palette_has_changed = 1;
  372. s->new_palette = 0;
  373. }
  374. if (s->prev_frame.data[0])
  375. avctx->release_buffer(avctx, &s->prev_frame);
  376. /* shuffle frames */
  377. s->prev_frame = s->frame;
  378. *data_size=sizeof(AVFrame);
  379. *(AVFrame*)data = s->frame;
  380. return buf_size;
  381. }
  382. static int flic_decode_end(AVCodecContext *avctx)
  383. {
  384. FlicDecodeContext *s = avctx->priv_data;
  385. if (s->prev_frame.data[0])
  386. avctx->release_buffer(avctx, &s->prev_frame);
  387. return 0;
  388. }
  389. AVCodec flic_decoder = {
  390. "flic",
  391. CODEC_TYPE_VIDEO,
  392. CODEC_ID_FLIC,
  393. sizeof(FlicDecodeContext),
  394. flic_decode_init,
  395. NULL,
  396. flic_decode_end,
  397. flic_decode_frame,
  398. CODEC_CAP_DR1,
  399. NULL
  400. };