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.

816 lines
31KB

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