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.

394 lines
13KB

  1. /*
  2. * FLI/FLC Animation Video Decoder
  3. * Copyright (C) 2003, 2004 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. typedef struct FlicDecodeContext {
  50. AVCodecContext *avctx;
  51. AVFrame frame;
  52. unsigned int palette[256];
  53. int new_palette;
  54. int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
  55. } FlicDecodeContext;
  56. static int flic_decode_init(AVCodecContext *avctx)
  57. {
  58. FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
  59. unsigned char *fli_header = (unsigned char *)avctx->extradata;
  60. s->avctx = avctx;
  61. avctx->pix_fmt = PIX_FMT_PAL8;
  62. avctx->has_b_frames = 0;
  63. if (s->avctx->extradata_size == 12) {
  64. /* special case for magic carpet FLIs */
  65. s->fli_type = 0xAF13;
  66. } else if (s->avctx->extradata_size == 128) {
  67. s->fli_type = LE_16(&fli_header[4]);
  68. } else {
  69. av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
  70. return -1;
  71. }
  72. s->frame.data[0] = NULL;
  73. s->new_palette = 0;
  74. return 0;
  75. }
  76. static int flic_decode_frame(AVCodecContext *avctx,
  77. void *data, int *data_size,
  78. uint8_t *buf, int buf_size)
  79. {
  80. FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
  81. int stream_ptr = 0;
  82. int stream_ptr_after_color_chunk;
  83. int pixel_ptr;
  84. int palette_ptr;
  85. unsigned char palette_idx1;
  86. unsigned char palette_idx2;
  87. unsigned int frame_size;
  88. int num_chunks;
  89. unsigned int chunk_size;
  90. int chunk_type;
  91. int i, j;
  92. int color_packets;
  93. int color_changes;
  94. int color_shift;
  95. unsigned char r, g, b;
  96. int lines;
  97. int compressed_lines;
  98. int starting_line;
  99. signed short line_packets;
  100. int y_ptr;
  101. signed char byte_run;
  102. int pixel_skip;
  103. int pixel_countdown;
  104. unsigned char *pixels;
  105. s->frame.reference = 1;
  106. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  107. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  108. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  109. return -1;
  110. }
  111. pixels = s->frame.data[0];
  112. frame_size = LE_32(&buf[stream_ptr]);
  113. stream_ptr += 6; /* skip the magic number */
  114. num_chunks = LE_16(&buf[stream_ptr]);
  115. stream_ptr += 10; /* skip padding */
  116. frame_size -= 16;
  117. /* iterate through the chunks */
  118. while ((frame_size > 0) && (num_chunks > 0)) {
  119. chunk_size = LE_32(&buf[stream_ptr]);
  120. stream_ptr += 4;
  121. chunk_type = LE_16(&buf[stream_ptr]);
  122. stream_ptr += 2;
  123. switch (chunk_type) {
  124. case FLI_256_COLOR:
  125. case FLI_COLOR:
  126. stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
  127. s->new_palette = 1;
  128. /* check special case: If this file is from the Magic Carpet
  129. * game and uses 6-bit colors even though it reports 256-color
  130. * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
  131. * initialization) */
  132. if ((chunk_type == FLI_256_COLOR) && (s->fli_type != 0xAF13))
  133. color_shift = 0;
  134. else
  135. color_shift = 2;
  136. /* set up the palette */
  137. color_packets = LE_16(&buf[stream_ptr]);
  138. stream_ptr += 2;
  139. palette_ptr = 0;
  140. for (i = 0; i < color_packets; i++) {
  141. /* first byte is how many colors to skip */
  142. palette_ptr += buf[stream_ptr++];
  143. /* next byte indicates how many entries to change */
  144. color_changes = buf[stream_ptr++];
  145. /* if there are 0 color changes, there are actually 256 */
  146. if (color_changes == 0)
  147. color_changes = 256;
  148. for (j = 0; j < color_changes; j++) {
  149. /* wrap around, for good measure */
  150. if (palette_ptr >= 256)
  151. palette_ptr = 0;
  152. r = buf[stream_ptr++] << color_shift;
  153. g = buf[stream_ptr++] << color_shift;
  154. b = buf[stream_ptr++] << color_shift;
  155. s->palette[palette_ptr++] = (r << 16) | (g << 8) | b;
  156. }
  157. }
  158. /* color chunks sometimes have weird 16-bit alignment issues;
  159. * therefore, take the hardline approach and set the stream_ptr
  160. * to the value calculated w.r.t. the size specified by the color
  161. * chunk header */
  162. stream_ptr = stream_ptr_after_color_chunk;
  163. break;
  164. case FLI_DELTA:
  165. y_ptr = 0;
  166. compressed_lines = LE_16(&buf[stream_ptr]);
  167. stream_ptr += 2;
  168. while (compressed_lines > 0) {
  169. line_packets = LE_16(&buf[stream_ptr]);
  170. stream_ptr += 2;
  171. if (line_packets < 0) {
  172. line_packets = -line_packets;
  173. y_ptr += line_packets * s->frame.linesize[0];
  174. } else {
  175. compressed_lines--;
  176. pixel_ptr = y_ptr;
  177. pixel_countdown = s->avctx->width;
  178. for (i = 0; i < line_packets; i++) {
  179. /* account for the skip bytes */
  180. pixel_skip = buf[stream_ptr++];
  181. pixel_ptr += pixel_skip;
  182. pixel_countdown -= pixel_skip;
  183. byte_run = buf[stream_ptr++];
  184. if (byte_run < 0) {
  185. byte_run = -byte_run;
  186. palette_idx1 = buf[stream_ptr++];
  187. palette_idx2 = buf[stream_ptr++];
  188. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  189. pixels[pixel_ptr++] = palette_idx1;
  190. pixels[pixel_ptr++] = palette_idx2;
  191. }
  192. } else {
  193. for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
  194. palette_idx1 = buf[stream_ptr++];
  195. pixels[pixel_ptr++] = palette_idx1;
  196. }
  197. }
  198. }
  199. y_ptr += s->frame.linesize[0];
  200. }
  201. }
  202. break;
  203. case FLI_LC:
  204. /* line compressed */
  205. starting_line = LE_16(&buf[stream_ptr]);
  206. stream_ptr += 2;
  207. y_ptr = 0;
  208. y_ptr += starting_line * s->frame.linesize[0];
  209. compressed_lines = LE_16(&buf[stream_ptr]);
  210. stream_ptr += 2;
  211. while (compressed_lines > 0) {
  212. pixel_ptr = y_ptr;
  213. pixel_countdown = s->avctx->width;
  214. line_packets = buf[stream_ptr++];
  215. if (line_packets > 0) {
  216. for (i = 0; i < line_packets; i++) {
  217. /* account for the skip bytes */
  218. pixel_skip = buf[stream_ptr++];
  219. pixel_ptr += pixel_skip;
  220. pixel_countdown -= pixel_skip;
  221. byte_run = buf[stream_ptr++];
  222. if (byte_run > 0) {
  223. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  224. palette_idx1 = buf[stream_ptr++];
  225. pixels[pixel_ptr++] = palette_idx1;
  226. }
  227. } else {
  228. byte_run = -byte_run;
  229. palette_idx1 = buf[stream_ptr++];
  230. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  231. pixels[pixel_ptr++] = palette_idx1;
  232. }
  233. }
  234. }
  235. }
  236. y_ptr += s->frame.linesize[0];
  237. compressed_lines--;
  238. }
  239. break;
  240. case FLI_BLACK:
  241. /* set the whole frame to color 0 (which is usually black) */
  242. memset(pixels, 0,
  243. s->frame.linesize[0] * s->avctx->height);
  244. break;
  245. case FLI_BRUN:
  246. /* Byte run compression: This chunk type only occurs in the first
  247. * FLI frame and it will update the entire frame. */
  248. y_ptr = 0;
  249. for (lines = 0; lines < s->avctx->height; lines++) {
  250. pixel_ptr = y_ptr;
  251. /* disregard the line packets; instead, iterate through all
  252. * pixels on a row */
  253. stream_ptr++;
  254. pixel_countdown = s->avctx->width;
  255. while (pixel_countdown > 0) {
  256. byte_run = buf[stream_ptr++];
  257. if (byte_run > 0) {
  258. palette_idx1 = buf[stream_ptr++];
  259. for (j = 0; j < byte_run; j++) {
  260. pixels[pixel_ptr++] = palette_idx1;
  261. pixel_countdown--;
  262. if (pixel_countdown < 0)
  263. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  264. pixel_countdown);
  265. }
  266. } else { /* copy bytes if byte_run < 0 */
  267. byte_run = -byte_run;
  268. for (j = 0; j < byte_run; j++) {
  269. palette_idx1 = buf[stream_ptr++];
  270. pixels[pixel_ptr++] = palette_idx1;
  271. pixel_countdown--;
  272. if (pixel_countdown < 0)
  273. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  274. pixel_countdown);
  275. }
  276. }
  277. }
  278. y_ptr += s->frame.linesize[0];
  279. }
  280. break;
  281. case FLI_COPY:
  282. /* copy the chunk (uncompressed frame) */
  283. if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
  284. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  285. "bigger than image, skipping chunk\n", chunk_size - 6);
  286. stream_ptr += chunk_size - 6;
  287. } else {
  288. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  289. y_ptr += s->frame.linesize[0]) {
  290. memcpy(&pixels[y_ptr], &buf[stream_ptr],
  291. s->avctx->width);
  292. stream_ptr += s->avctx->width;
  293. }
  294. }
  295. break;
  296. case FLI_MINI:
  297. /* some sort of a thumbnail? disregard this chunk... */
  298. stream_ptr += chunk_size - 6;
  299. break;
  300. default:
  301. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  302. break;
  303. }
  304. frame_size -= chunk_size;
  305. num_chunks--;
  306. }
  307. /* by the end of the chunk, the stream ptr should equal the frame
  308. * size (minus 1, possibly); if it doesn't, issue a warning */
  309. if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
  310. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  311. "and final chunk ptr = %d\n", buf_size, stream_ptr);
  312. /* make the palette available on the way out */
  313. // if (s->new_palette) {
  314. if (1) {
  315. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  316. s->frame.palette_has_changed = 1;
  317. s->new_palette = 0;
  318. }
  319. *data_size=sizeof(AVFrame);
  320. *(AVFrame*)data = s->frame;
  321. return buf_size;
  322. }
  323. static int flic_decode_end(AVCodecContext *avctx)
  324. {
  325. FlicDecodeContext *s = avctx->priv_data;
  326. if (s->frame.data[0])
  327. avctx->release_buffer(avctx, &s->frame);
  328. return 0;
  329. }
  330. AVCodec flic_decoder = {
  331. "flic",
  332. CODEC_TYPE_VIDEO,
  333. CODEC_ID_FLIC,
  334. sizeof(FlicDecodeContext),
  335. flic_decode_init,
  336. NULL,
  337. flic_decode_end,
  338. flic_decode_frame,
  339. CODEC_CAP_DR1,
  340. NULL
  341. };