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.

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