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.

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