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.

796 lines
31KB

  1. /*
  2. * FLI/FLC Animation Video Decoder
  3. * Copyright (C) 2003, 2004 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
  23. * Autodesk Animator FLI/FLC Video Decoder
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .fli/.flc file format and all of its many
  26. * variations, visit:
  27. * http://www.compuphase.com/flic.htm
  28. *
  29. * This decoder outputs PAL8/RGB555/RGB565 and maybe one day RGB24
  30. * colorspace data, depending on the FLC. To use this decoder, be
  31. * sure that your demuxer sends the FLI file header to the decoder via
  32. * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
  33. * large. The only exception is for FLI files from the game "Magic Carpet",
  34. * in which the header is only 12 bytes.
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "libavutil/intreadwrite.h"
  40. #include "avcodec.h"
  41. #include "bytestream.h"
  42. #include "mathops.h"
  43. #define FLI_256_COLOR 4
  44. #define FLI_DELTA 7
  45. #define FLI_COLOR 11
  46. #define FLI_LC 12
  47. #define FLI_BLACK 13
  48. #define FLI_BRUN 15
  49. #define FLI_COPY 16
  50. #define FLI_MINI 18
  51. #define FLI_DTA_BRUN 25
  52. #define FLI_DTA_COPY 26
  53. #define FLI_DTA_LC 27
  54. #define FLI_TYPE_CODE (0xAF11)
  55. #define FLC_FLX_TYPE_CODE (0xAF12)
  56. #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
  57. #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
  58. #define CHECK_PIXEL_PTR(n) \
  59. if (pixel_ptr + n > pixel_limit) { \
  60. av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
  61. pixel_ptr + n, pixel_limit); \
  62. return AVERROR_INVALIDDATA; \
  63. } \
  64. typedef struct FlicDecodeContext {
  65. AVCodecContext *avctx;
  66. AVFrame frame;
  67. unsigned int palette[256];
  68. int new_palette;
  69. int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
  70. } FlicDecodeContext;
  71. static av_cold int flic_decode_init(AVCodecContext *avctx)
  72. {
  73. FlicDecodeContext *s = avctx->priv_data;
  74. unsigned char *fli_header = (unsigned char *)avctx->extradata;
  75. int depth;
  76. if (avctx->extradata_size != 12 &&
  77. avctx->extradata_size != 128) {
  78. av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. s->avctx = avctx;
  82. s->fli_type = AV_RL16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
  83. depth = 0;
  84. if (s->avctx->extradata_size == 12) {
  85. /* special case for magic carpet FLIs */
  86. s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
  87. depth = 8;
  88. } else {
  89. depth = AV_RL16(&fli_header[12]);
  90. }
  91. if (depth == 0) {
  92. depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
  93. }
  94. if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
  95. depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
  96. }
  97. switch (depth) {
  98. case 8 : avctx->pix_fmt = PIX_FMT_PAL8; break;
  99. case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
  100. case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
  101. case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
  102. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
  103. return -1;
  104. default :
  105. av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
  106. return -1;
  107. }
  108. avcodec_get_frame_defaults(&s->frame);
  109. s->frame.data[0] = NULL;
  110. s->new_palette = 0;
  111. return 0;
  112. }
  113. static int flic_decode_frame_8BPP(AVCodecContext *avctx,
  114. void *data, int *data_size,
  115. const uint8_t *buf, int buf_size)
  116. {
  117. FlicDecodeContext *s = avctx->priv_data;
  118. GetByteContext g2;
  119. int pixel_ptr;
  120. int palette_ptr;
  121. unsigned char palette_idx1;
  122. unsigned char palette_idx2;
  123. unsigned int frame_size;
  124. int num_chunks;
  125. unsigned int chunk_size;
  126. int chunk_type;
  127. int i, j;
  128. int color_packets;
  129. int color_changes;
  130. int color_shift;
  131. unsigned char r, g, b;
  132. int lines;
  133. int compressed_lines;
  134. int starting_line;
  135. signed short line_packets;
  136. int y_ptr;
  137. int byte_run;
  138. int pixel_skip;
  139. int pixel_countdown;
  140. unsigned char *pixels;
  141. unsigned int pixel_limit;
  142. bytestream2_init(&g2, buf, buf_size);
  143. s->frame.reference = 3;
  144. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  145. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  146. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  147. return -1;
  148. }
  149. pixels = s->frame.data[0];
  150. pixel_limit = s->avctx->height * s->frame.linesize[0];
  151. if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + FF_INPUT_BUFFER_PADDING_SIZE))
  152. return AVERROR_INVALIDDATA;
  153. frame_size = bytestream2_get_le32(&g2);
  154. if (frame_size > buf_size)
  155. frame_size = buf_size;
  156. bytestream2_skip(&g2, 2); /* skip the magic number */
  157. num_chunks = bytestream2_get_le16(&g2);
  158. bytestream2_skip(&g2, 8); /* skip padding */
  159. frame_size -= 16;
  160. /* iterate through the chunks */
  161. while ((frame_size >= 6) && (num_chunks > 0)) {
  162. int stream_ptr_after_chunk;
  163. chunk_size = bytestream2_get_le32(&g2);
  164. if (chunk_size > frame_size) {
  165. av_log(avctx, AV_LOG_WARNING,
  166. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  167. chunk_size = frame_size;
  168. }
  169. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  170. chunk_type = bytestream2_get_le16(&g2);
  171. switch (chunk_type) {
  172. case FLI_256_COLOR:
  173. case FLI_COLOR:
  174. /* check special case: If this file is from the Magic Carpet
  175. * game and uses 6-bit colors even though it reports 256-color
  176. * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
  177. * initialization) */
  178. if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
  179. color_shift = 0;
  180. else
  181. color_shift = 2;
  182. /* set up the palette */
  183. color_packets = bytestream2_get_le16(&g2);
  184. palette_ptr = 0;
  185. for (i = 0; i < color_packets; i++) {
  186. /* first byte is how many colors to skip */
  187. palette_ptr += bytestream2_get_byte(&g2);
  188. /* next byte indicates how many entries to change */
  189. color_changes = bytestream2_get_byte(&g2);
  190. /* if there are 0 color changes, there are actually 256 */
  191. if (color_changes == 0)
  192. color_changes = 256;
  193. if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
  194. break;
  195. for (j = 0; j < color_changes; j++) {
  196. unsigned int entry;
  197. /* wrap around, for good measure */
  198. if ((unsigned)palette_ptr >= 256)
  199. palette_ptr = 0;
  200. r = bytestream2_get_byte(&g2) << color_shift;
  201. g = bytestream2_get_byte(&g2) << color_shift;
  202. b = bytestream2_get_byte(&g2) << color_shift;
  203. entry = 0xFF << 24 | r << 16 | g << 8 | b;
  204. if (color_shift == 2)
  205. entry |= entry >> 6 & 0x30303;
  206. if (s->palette[palette_ptr] != entry)
  207. s->new_palette = 1;
  208. s->palette[palette_ptr++] = entry;
  209. }
  210. }
  211. break;
  212. case FLI_DELTA:
  213. y_ptr = 0;
  214. compressed_lines = bytestream2_get_le16(&g2);
  215. while (compressed_lines > 0) {
  216. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  217. break;
  218. line_packets = bytestream2_get_le16(&g2);
  219. if ((line_packets & 0xC000) == 0xC000) {
  220. // line skip opcode
  221. line_packets = -line_packets;
  222. y_ptr += line_packets * s->frame.linesize[0];
  223. } else if ((line_packets & 0xC000) == 0x4000) {
  224. av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
  225. } else if ((line_packets & 0xC000) == 0x8000) {
  226. // "last byte" opcode
  227. pixel_ptr= y_ptr + s->frame.linesize[0] - 1;
  228. CHECK_PIXEL_PTR(0);
  229. pixels[pixel_ptr] = line_packets & 0xff;
  230. } else {
  231. compressed_lines--;
  232. pixel_ptr = y_ptr;
  233. CHECK_PIXEL_PTR(0);
  234. pixel_countdown = s->avctx->width;
  235. for (i = 0; i < line_packets; i++) {
  236. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  237. break;
  238. /* account for the skip bytes */
  239. pixel_skip = bytestream2_get_byte(&g2);
  240. pixel_ptr += pixel_skip;
  241. pixel_countdown -= pixel_skip;
  242. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  243. if (byte_run < 0) {
  244. byte_run = -byte_run;
  245. palette_idx1 = bytestream2_get_byte(&g2);
  246. palette_idx2 = bytestream2_get_byte(&g2);
  247. CHECK_PIXEL_PTR(byte_run * 2);
  248. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  249. pixels[pixel_ptr++] = palette_idx1;
  250. pixels[pixel_ptr++] = palette_idx2;
  251. }
  252. } else {
  253. CHECK_PIXEL_PTR(byte_run * 2);
  254. if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
  255. break;
  256. for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
  257. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  258. }
  259. }
  260. }
  261. y_ptr += s->frame.linesize[0];
  262. }
  263. }
  264. break;
  265. case FLI_LC:
  266. /* line compressed */
  267. starting_line = bytestream2_get_le16(&g2);
  268. y_ptr = 0;
  269. y_ptr += starting_line * s->frame.linesize[0];
  270. compressed_lines = bytestream2_get_le16(&g2);
  271. while (compressed_lines > 0) {
  272. pixel_ptr = y_ptr;
  273. CHECK_PIXEL_PTR(0);
  274. pixel_countdown = s->avctx->width;
  275. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  276. break;
  277. line_packets = bytestream2_get_byte(&g2);
  278. if (line_packets > 0) {
  279. for (i = 0; i < line_packets; i++) {
  280. /* account for the skip bytes */
  281. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  282. break;
  283. pixel_skip = bytestream2_get_byte(&g2);
  284. pixel_ptr += pixel_skip;
  285. pixel_countdown -= pixel_skip;
  286. byte_run = sign_extend(bytestream2_get_byte(&g2),8);
  287. if (byte_run > 0) {
  288. CHECK_PIXEL_PTR(byte_run);
  289. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  290. break;
  291. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  292. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  293. }
  294. } else if (byte_run < 0) {
  295. byte_run = -byte_run;
  296. palette_idx1 = bytestream2_get_byte(&g2);
  297. CHECK_PIXEL_PTR(byte_run);
  298. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  299. pixels[pixel_ptr++] = palette_idx1;
  300. }
  301. }
  302. }
  303. }
  304. y_ptr += s->frame.linesize[0];
  305. compressed_lines--;
  306. }
  307. break;
  308. case FLI_BLACK:
  309. /* set the whole frame to color 0 (which is usually black) */
  310. memset(pixels, 0,
  311. s->frame.linesize[0] * s->avctx->height);
  312. break;
  313. case FLI_BRUN:
  314. /* Byte run compression: This chunk type only occurs in the first
  315. * FLI frame and it will update the entire frame. */
  316. y_ptr = 0;
  317. for (lines = 0; lines < s->avctx->height; lines++) {
  318. pixel_ptr = y_ptr;
  319. /* disregard the line packets; instead, iterate through all
  320. * pixels on a row */
  321. bytestream2_skip(&g2, 1);
  322. pixel_countdown = s->avctx->width;
  323. while (pixel_countdown > 0) {
  324. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  325. break;
  326. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  327. if (byte_run > 0) {
  328. palette_idx1 = bytestream2_get_byte(&g2);
  329. CHECK_PIXEL_PTR(byte_run);
  330. for (j = 0; j < byte_run; j++) {
  331. pixels[pixel_ptr++] = palette_idx1;
  332. pixel_countdown--;
  333. if (pixel_countdown < 0)
  334. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  335. pixel_countdown, lines);
  336. }
  337. } else { /* copy bytes if byte_run < 0 */
  338. byte_run = -byte_run;
  339. CHECK_PIXEL_PTR(byte_run);
  340. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  341. break;
  342. for (j = 0; j < byte_run; j++) {
  343. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  344. pixel_countdown--;
  345. if (pixel_countdown < 0)
  346. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  347. pixel_countdown, lines);
  348. }
  349. }
  350. }
  351. y_ptr += s->frame.linesize[0];
  352. }
  353. break;
  354. case FLI_COPY:
  355. /* copy the chunk (uncompressed frame) */
  356. if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
  357. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  358. "has incorrect size, skipping chunk\n", chunk_size - 6);
  359. bytestream2_skip(&g2, chunk_size - 6);
  360. } else {
  361. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  362. y_ptr += s->frame.linesize[0]) {
  363. bytestream2_get_buffer(&g2, &pixels[y_ptr],
  364. s->avctx->width);
  365. }
  366. }
  367. break;
  368. case FLI_MINI:
  369. /* some sort of a thumbnail? disregard this chunk... */
  370. break;
  371. default:
  372. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  373. break;
  374. }
  375. if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
  376. bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
  377. frame_size -= chunk_size;
  378. num_chunks--;
  379. }
  380. /* by the end of the chunk, the stream ptr should equal the frame
  381. * size (minus 1, possibly); if it doesn't, issue a warning */
  382. if ((bytestream2_get_bytes_left(&g2) != 0) &&
  383. (bytestream2_get_bytes_left(&g2) != 1))
  384. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  385. "and final chunk ptr = %d\n", buf_size,
  386. buf_size - bytestream2_get_bytes_left(&g2));
  387. /* make the palette available on the way out */
  388. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  389. if (s->new_palette) {
  390. s->frame.palette_has_changed = 1;
  391. s->new_palette = 0;
  392. }
  393. *data_size=sizeof(AVFrame);
  394. *(AVFrame*)data = s->frame;
  395. return buf_size;
  396. }
  397. static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
  398. void *data, int *data_size,
  399. const uint8_t *buf, int buf_size)
  400. {
  401. /* Note, the only difference between the 15Bpp and 16Bpp */
  402. /* Format is the pixel format, the packets are processed the same. */
  403. FlicDecodeContext *s = avctx->priv_data;
  404. GetByteContext g2;
  405. int pixel_ptr;
  406. unsigned char palette_idx1;
  407. unsigned int frame_size;
  408. int num_chunks;
  409. unsigned int chunk_size;
  410. int chunk_type;
  411. int i, j;
  412. int lines;
  413. int compressed_lines;
  414. signed short line_packets;
  415. int y_ptr;
  416. int byte_run;
  417. int pixel_skip;
  418. int pixel_countdown;
  419. unsigned char *pixels;
  420. int pixel;
  421. unsigned int pixel_limit;
  422. bytestream2_init(&g2, buf, buf_size);
  423. s->frame.reference = 3;
  424. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  425. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  426. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  427. return -1;
  428. }
  429. pixels = s->frame.data[0];
  430. pixel_limit = s->avctx->height * s->frame.linesize[0];
  431. frame_size = bytestream2_get_le32(&g2);
  432. bytestream2_skip(&g2, 2); /* skip the magic number */
  433. num_chunks = bytestream2_get_le16(&g2);
  434. bytestream2_skip(&g2, 8); /* skip padding */
  435. if (frame_size > buf_size)
  436. frame_size = buf_size;
  437. frame_size -= 16;
  438. /* iterate through the chunks */
  439. while ((frame_size > 0) && (num_chunks > 0)) {
  440. int stream_ptr_after_chunk;
  441. chunk_size = bytestream2_get_le32(&g2);
  442. if (chunk_size > frame_size) {
  443. av_log(avctx, AV_LOG_WARNING,
  444. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  445. chunk_size = frame_size;
  446. }
  447. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  448. chunk_type = bytestream2_get_le16(&g2);
  449. switch (chunk_type) {
  450. case FLI_256_COLOR:
  451. case FLI_COLOR:
  452. /* For some reason, it seems that non-palettized flics do
  453. * include one of these chunks in their first frame.
  454. * Why I do not know, it seems rather extraneous. */
  455. /* av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
  456. bytestream2_skip(&g2, chunk_size - 6);
  457. break;
  458. case FLI_DELTA:
  459. case FLI_DTA_LC:
  460. y_ptr = 0;
  461. compressed_lines = bytestream2_get_le16(&g2);
  462. while (compressed_lines > 0) {
  463. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  464. break;
  465. line_packets = bytestream2_get_le16(&g2);
  466. if (line_packets < 0) {
  467. line_packets = -line_packets;
  468. y_ptr += line_packets * s->frame.linesize[0];
  469. } else {
  470. compressed_lines--;
  471. pixel_ptr = y_ptr;
  472. CHECK_PIXEL_PTR(0);
  473. pixel_countdown = s->avctx->width;
  474. for (i = 0; i < line_packets; i++) {
  475. /* account for the skip bytes */
  476. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  477. break;
  478. pixel_skip = bytestream2_get_byte(&g2);
  479. pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
  480. pixel_countdown -= pixel_skip;
  481. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  482. if (byte_run < 0) {
  483. byte_run = -byte_run;
  484. pixel = bytestream2_get_le16(&g2);
  485. CHECK_PIXEL_PTR(2 * byte_run);
  486. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  487. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  488. pixel_ptr += 2;
  489. }
  490. } else {
  491. if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
  492. break;
  493. CHECK_PIXEL_PTR(2 * byte_run);
  494. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  495. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  496. pixel_ptr += 2;
  497. }
  498. }
  499. }
  500. y_ptr += s->frame.linesize[0];
  501. }
  502. }
  503. break;
  504. case FLI_LC:
  505. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
  506. bytestream2_skip(&g2, chunk_size - 6);
  507. break;
  508. case FLI_BLACK:
  509. /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
  510. memset(pixels, 0x0000,
  511. s->frame.linesize[0] * s->avctx->height);
  512. break;
  513. case FLI_BRUN:
  514. y_ptr = 0;
  515. for (lines = 0; lines < s->avctx->height; lines++) {
  516. pixel_ptr = y_ptr;
  517. /* disregard the line packets; instead, iterate through all
  518. * pixels on a row */
  519. bytestream2_skip(&g2, 1);
  520. pixel_countdown = (s->avctx->width * 2);
  521. while (pixel_countdown > 0) {
  522. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  523. break;
  524. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  525. if (byte_run > 0) {
  526. palette_idx1 = bytestream2_get_byte(&g2);
  527. CHECK_PIXEL_PTR(byte_run);
  528. for (j = 0; j < byte_run; j++) {
  529. pixels[pixel_ptr++] = palette_idx1;
  530. pixel_countdown--;
  531. if (pixel_countdown < 0)
  532. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
  533. pixel_countdown, lines);
  534. }
  535. } else { /* copy bytes if byte_run < 0 */
  536. byte_run = -byte_run;
  537. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  538. break;
  539. CHECK_PIXEL_PTR(byte_run);
  540. for (j = 0; j < byte_run; j++) {
  541. palette_idx1 = bytestream2_get_byte(&g2);
  542. pixels[pixel_ptr++] = palette_idx1;
  543. pixel_countdown--;
  544. if (pixel_countdown < 0)
  545. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  546. pixel_countdown, lines);
  547. }
  548. }
  549. }
  550. /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
  551. * This does not give us any good oportunity to perform word endian conversion
  552. * during decompression. So if it is required (i.e., this is not a LE target, we do
  553. * a second pass over the line here, swapping the bytes.
  554. */
  555. #if HAVE_BIGENDIAN
  556. pixel_ptr = y_ptr;
  557. pixel_countdown = s->avctx->width;
  558. while (pixel_countdown > 0) {
  559. *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
  560. pixel_ptr += 2;
  561. }
  562. #endif
  563. y_ptr += s->frame.linesize[0];
  564. }
  565. break;
  566. case FLI_DTA_BRUN:
  567. y_ptr = 0;
  568. for (lines = 0; lines < s->avctx->height; lines++) {
  569. pixel_ptr = y_ptr;
  570. /* disregard the line packets; instead, iterate through all
  571. * pixels on a row */
  572. bytestream2_skip(&g2, 1);
  573. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  574. while (pixel_countdown > 0) {
  575. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  576. break;
  577. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  578. if (byte_run > 0) {
  579. pixel = bytestream2_get_le16(&g2);
  580. CHECK_PIXEL_PTR(2 * byte_run);
  581. for (j = 0; j < byte_run; j++) {
  582. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  583. pixel_ptr += 2;
  584. pixel_countdown--;
  585. if (pixel_countdown < 0)
  586. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  587. pixel_countdown);
  588. }
  589. } else { /* copy pixels if byte_run < 0 */
  590. byte_run = -byte_run;
  591. if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
  592. break;
  593. CHECK_PIXEL_PTR(2 * byte_run);
  594. for (j = 0; j < byte_run; j++) {
  595. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  596. pixel_ptr += 2;
  597. pixel_countdown--;
  598. if (pixel_countdown < 0)
  599. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  600. pixel_countdown);
  601. }
  602. }
  603. }
  604. y_ptr += s->frame.linesize[0];
  605. }
  606. break;
  607. case FLI_COPY:
  608. case FLI_DTA_COPY:
  609. /* copy the chunk (uncompressed frame) */
  610. if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
  611. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  612. "bigger than image, skipping chunk\n", chunk_size - 6);
  613. bytestream2_skip(&g2, chunk_size - 6);
  614. } else {
  615. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  616. y_ptr += s->frame.linesize[0]) {
  617. pixel_countdown = s->avctx->width;
  618. pixel_ptr = 0;
  619. while (pixel_countdown > 0) {
  620. *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
  621. pixel_ptr += 2;
  622. pixel_countdown--;
  623. }
  624. }
  625. }
  626. break;
  627. case FLI_MINI:
  628. /* some sort of a thumbnail? disregard this chunk... */
  629. bytestream2_skip(&g2, chunk_size - 6);
  630. break;
  631. default:
  632. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  633. break;
  634. }
  635. frame_size -= chunk_size;
  636. num_chunks--;
  637. }
  638. /* by the end of the chunk, the stream ptr should equal the frame
  639. * size (minus 1, possibly); if it doesn't, issue a warning */
  640. if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
  641. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  642. "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
  643. *data_size=sizeof(AVFrame);
  644. *(AVFrame*)data = s->frame;
  645. return buf_size;
  646. }
  647. static int flic_decode_frame_24BPP(AVCodecContext *avctx,
  648. void *data, int *data_size,
  649. const uint8_t *buf, int buf_size)
  650. {
  651. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
  652. return -1;
  653. }
  654. static int flic_decode_frame(AVCodecContext *avctx,
  655. void *data, int *data_size,
  656. AVPacket *avpkt)
  657. {
  658. const uint8_t *buf = avpkt->data;
  659. int buf_size = avpkt->size;
  660. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  661. return flic_decode_frame_8BPP(avctx, data, data_size,
  662. buf, buf_size);
  663. }
  664. else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
  665. (avctx->pix_fmt == PIX_FMT_RGB565)) {
  666. return flic_decode_frame_15_16BPP(avctx, data, data_size,
  667. buf, buf_size);
  668. }
  669. else if (avctx->pix_fmt == PIX_FMT_BGR24) {
  670. return flic_decode_frame_24BPP(avctx, data, data_size,
  671. buf, buf_size);
  672. }
  673. /* Should not get here, ever as the pix_fmt is processed */
  674. /* in flic_decode_init and the above if should deal with */
  675. /* the finite set of possibilites allowable by here. */
  676. /* But in case we do, just error out. */
  677. av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
  678. return -1;
  679. }
  680. static av_cold int flic_decode_end(AVCodecContext *avctx)
  681. {
  682. FlicDecodeContext *s = avctx->priv_data;
  683. if (s->frame.data[0])
  684. avctx->release_buffer(avctx, &s->frame);
  685. return 0;
  686. }
  687. AVCodec ff_flic_decoder = {
  688. .name = "flic",
  689. .type = AVMEDIA_TYPE_VIDEO,
  690. .id = CODEC_ID_FLIC,
  691. .priv_data_size = sizeof(FlicDecodeContext),
  692. .init = flic_decode_init,
  693. .close = flic_decode_end,
  694. .decode = flic_decode_frame,
  695. .capabilities = CODEC_CAP_DR1,
  696. .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
  697. };