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.

400 lines
14KB

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