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.

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