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.

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