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.

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