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.

820 lines
32KB

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