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.

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