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.

815 lines
31KB

  1. /*
  2. * FLI/FLC Animation Video Decoder
  3. * Copyright (C) 2003, 2004 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Autodesk Animator FLI/FLC Video Decoder
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .fli/.flc file format and all of its many
  26. * variations, visit:
  27. * http://www.compuphase.com/flic.htm
  28. *
  29. * This decoder outputs PAL8/RGB555/RGB565 and maybe one day RGB24
  30. * colorspace data, depending on the FLC. To use this decoder, be
  31. * sure that your demuxer sends the FLI file header to the decoder via
  32. * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
  33. * large. The only exception is for FLI files from the game "Magic Carpet",
  34. * in which the header is only 12 bytes.
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "libavutil/intreadwrite.h"
  40. #include "avcodec.h"
  41. #include "bytestream.h"
  42. #include "mathops.h"
  43. #define FLI_256_COLOR 4
  44. #define FLI_DELTA 7
  45. #define FLI_COLOR 11
  46. #define FLI_LC 12
  47. #define FLI_BLACK 13
  48. #define FLI_BRUN 15
  49. #define FLI_COPY 16
  50. #define FLI_MINI 18
  51. #define FLI_DTA_BRUN 25
  52. #define FLI_DTA_COPY 26
  53. #define FLI_DTA_LC 27
  54. #define FLI_TYPE_CODE (0xAF11)
  55. #define FLC_FLX_TYPE_CODE (0xAF12)
  56. #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
  57. #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
  58. #define CHECK_PIXEL_PTR(n) \
  59. if (pixel_ptr + n > pixel_limit) { \
  60. av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
  61. pixel_ptr + n, pixel_limit); \
  62. return AVERROR_INVALIDDATA; \
  63. } \
  64. typedef struct FlicDecodeContext {
  65. AVCodecContext *avctx;
  66. AVFrame frame;
  67. unsigned int palette[256];
  68. int new_palette;
  69. int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
  70. } FlicDecodeContext;
  71. static av_cold int flic_decode_init(AVCodecContext *avctx)
  72. {
  73. FlicDecodeContext *s = avctx->priv_data;
  74. unsigned char *fli_header = (unsigned char *)avctx->extradata;
  75. int depth;
  76. if (avctx->extradata_size != 0 &&
  77. avctx->extradata_size != 12 &&
  78. avctx->extradata_size != 128 &&
  79. avctx->extradata_size != 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 > 0) {
  346. palette_idx1 = bytestream2_get_byte(&g2);
  347. CHECK_PIXEL_PTR(byte_run);
  348. for (j = 0; j < byte_run; j++) {
  349. pixels[pixel_ptr++] = palette_idx1;
  350. pixel_countdown--;
  351. if (pixel_countdown < 0)
  352. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  353. pixel_countdown, lines);
  354. }
  355. } else { /* copy bytes if byte_run < 0 */
  356. byte_run = -byte_run;
  357. CHECK_PIXEL_PTR(byte_run);
  358. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  359. break;
  360. for (j = 0; j < byte_run; j++) {
  361. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  362. pixel_countdown--;
  363. if (pixel_countdown < 0)
  364. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  365. pixel_countdown, lines);
  366. }
  367. }
  368. }
  369. y_ptr += s->frame.linesize[0];
  370. }
  371. break;
  372. case FLI_COPY:
  373. /* copy the chunk (uncompressed frame) */
  374. if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
  375. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  376. "has incorrect size, skipping chunk\n", chunk_size - 6);
  377. bytestream2_skip(&g2, chunk_size - 6);
  378. } else {
  379. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  380. y_ptr += s->frame.linesize[0]) {
  381. bytestream2_get_buffer(&g2, &pixels[y_ptr],
  382. s->avctx->width);
  383. }
  384. }
  385. break;
  386. case FLI_MINI:
  387. /* some sort of a thumbnail? disregard this chunk... */
  388. break;
  389. default:
  390. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  391. break;
  392. }
  393. if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
  394. bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
  395. frame_size -= chunk_size;
  396. num_chunks--;
  397. }
  398. /* by the end of the chunk, the stream ptr should equal the frame
  399. * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
  400. if (bytestream2_get_bytes_left(&g2) > 2)
  401. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  402. "and final chunk ptr = %d\n", buf_size,
  403. buf_size - bytestream2_get_bytes_left(&g2));
  404. /* make the palette available on the way out */
  405. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  406. if (s->new_palette) {
  407. s->frame.palette_has_changed = 1;
  408. s->new_palette = 0;
  409. }
  410. *got_frame = 1;
  411. *(AVFrame*)data = s->frame;
  412. return buf_size;
  413. }
  414. static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
  415. void *data, int *got_frame,
  416. const uint8_t *buf, int buf_size)
  417. {
  418. /* Note, the only difference between the 15Bpp and 16Bpp */
  419. /* Format is the pixel format, the packets are processed the same. */
  420. FlicDecodeContext *s = avctx->priv_data;
  421. GetByteContext g2;
  422. int pixel_ptr;
  423. unsigned char palette_idx1;
  424. unsigned int frame_size;
  425. int num_chunks;
  426. unsigned int chunk_size;
  427. int chunk_type;
  428. int i, j, ret;
  429. int lines;
  430. int compressed_lines;
  431. signed short line_packets;
  432. int y_ptr;
  433. int byte_run;
  434. int pixel_skip;
  435. int pixel_countdown;
  436. unsigned char *pixels;
  437. int pixel;
  438. unsigned int pixel_limit;
  439. bytestream2_init(&g2, buf, buf_size);
  440. s->frame.reference = 3;
  441. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  442. if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  443. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  444. return ret;
  445. }
  446. pixels = s->frame.data[0];
  447. pixel_limit = s->avctx->height * s->frame.linesize[0];
  448. frame_size = bytestream2_get_le32(&g2);
  449. bytestream2_skip(&g2, 2); /* skip the magic number */
  450. num_chunks = bytestream2_get_le16(&g2);
  451. bytestream2_skip(&g2, 8); /* skip padding */
  452. if (frame_size > buf_size)
  453. frame_size = buf_size;
  454. frame_size -= 16;
  455. /* iterate through the chunks */
  456. while ((frame_size > 0) && (num_chunks > 0)) {
  457. int stream_ptr_after_chunk;
  458. chunk_size = bytestream2_get_le32(&g2);
  459. if (chunk_size > frame_size) {
  460. av_log(avctx, AV_LOG_WARNING,
  461. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  462. chunk_size = frame_size;
  463. }
  464. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  465. chunk_type = bytestream2_get_le16(&g2);
  466. switch (chunk_type) {
  467. case FLI_256_COLOR:
  468. case FLI_COLOR:
  469. /* For some reason, it seems that non-palettized flics do
  470. * include one of these chunks in their first frame.
  471. * Why I do not know, it seems rather extraneous. */
  472. av_dlog(avctx,
  473. "Unexpected Palette chunk %d in non-palettized FLC\n",
  474. chunk_type);
  475. bytestream2_skip(&g2, chunk_size - 6);
  476. break;
  477. case FLI_DELTA:
  478. case FLI_DTA_LC:
  479. y_ptr = 0;
  480. compressed_lines = bytestream2_get_le16(&g2);
  481. while (compressed_lines > 0) {
  482. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  483. break;
  484. line_packets = bytestream2_get_le16(&g2);
  485. if (line_packets < 0) {
  486. line_packets = -line_packets;
  487. y_ptr += line_packets * s->frame.linesize[0];
  488. } else {
  489. compressed_lines--;
  490. pixel_ptr = y_ptr;
  491. CHECK_PIXEL_PTR(0);
  492. pixel_countdown = s->avctx->width;
  493. for (i = 0; i < line_packets; i++) {
  494. /* account for the skip bytes */
  495. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  496. break;
  497. pixel_skip = bytestream2_get_byte(&g2);
  498. pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
  499. pixel_countdown -= pixel_skip;
  500. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  501. if (byte_run < 0) {
  502. byte_run = -byte_run;
  503. pixel = bytestream2_get_le16(&g2);
  504. CHECK_PIXEL_PTR(2 * byte_run);
  505. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  506. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  507. pixel_ptr += 2;
  508. }
  509. } else {
  510. if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
  511. break;
  512. CHECK_PIXEL_PTR(2 * byte_run);
  513. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  514. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  515. pixel_ptr += 2;
  516. }
  517. }
  518. }
  519. y_ptr += s->frame.linesize[0];
  520. }
  521. }
  522. break;
  523. case FLI_LC:
  524. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
  525. bytestream2_skip(&g2, chunk_size - 6);
  526. break;
  527. case FLI_BLACK:
  528. /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
  529. memset(pixels, 0x0000,
  530. s->frame.linesize[0] * s->avctx->height);
  531. break;
  532. case FLI_BRUN:
  533. y_ptr = 0;
  534. for (lines = 0; lines < s->avctx->height; lines++) {
  535. pixel_ptr = y_ptr;
  536. /* disregard the line packets; instead, iterate through all
  537. * pixels on a row */
  538. bytestream2_skip(&g2, 1);
  539. pixel_countdown = (s->avctx->width * 2);
  540. while (pixel_countdown > 0) {
  541. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  542. break;
  543. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  544. if (byte_run > 0) {
  545. palette_idx1 = bytestream2_get_byte(&g2);
  546. CHECK_PIXEL_PTR(byte_run);
  547. for (j = 0; j < byte_run; j++) {
  548. pixels[pixel_ptr++] = palette_idx1;
  549. pixel_countdown--;
  550. if (pixel_countdown < 0)
  551. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
  552. pixel_countdown, lines);
  553. }
  554. } else { /* copy bytes if byte_run < 0 */
  555. byte_run = -byte_run;
  556. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  557. break;
  558. CHECK_PIXEL_PTR(byte_run);
  559. for (j = 0; j < byte_run; j++) {
  560. palette_idx1 = bytestream2_get_byte(&g2);
  561. pixels[pixel_ptr++] = palette_idx1;
  562. pixel_countdown--;
  563. if (pixel_countdown < 0)
  564. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  565. pixel_countdown, lines);
  566. }
  567. }
  568. }
  569. /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
  570. * This does not give us any good opportunity to perform word endian conversion
  571. * during decompression. So if it is required (i.e., this is not a LE target, we do
  572. * a second pass over the line here, swapping the bytes.
  573. */
  574. #if HAVE_BIGENDIAN
  575. pixel_ptr = y_ptr;
  576. pixel_countdown = s->avctx->width;
  577. while (pixel_countdown > 0) {
  578. *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
  579. pixel_ptr += 2;
  580. }
  581. #endif
  582. y_ptr += s->frame.linesize[0];
  583. }
  584. break;
  585. case FLI_DTA_BRUN:
  586. y_ptr = 0;
  587. for (lines = 0; lines < s->avctx->height; lines++) {
  588. pixel_ptr = y_ptr;
  589. /* disregard the line packets; instead, iterate through all
  590. * pixels on a row */
  591. bytestream2_skip(&g2, 1);
  592. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  593. while (pixel_countdown > 0) {
  594. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  595. break;
  596. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  597. if (byte_run > 0) {
  598. pixel = bytestream2_get_le16(&g2);
  599. CHECK_PIXEL_PTR(2 * byte_run);
  600. for (j = 0; j < byte_run; j++) {
  601. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  602. pixel_ptr += 2;
  603. pixel_countdown--;
  604. if (pixel_countdown < 0)
  605. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  606. pixel_countdown);
  607. }
  608. } else { /* copy pixels if byte_run < 0 */
  609. byte_run = -byte_run;
  610. if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
  611. break;
  612. CHECK_PIXEL_PTR(2 * byte_run);
  613. for (j = 0; j < byte_run; j++) {
  614. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  615. pixel_ptr += 2;
  616. pixel_countdown--;
  617. if (pixel_countdown < 0)
  618. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  619. pixel_countdown);
  620. }
  621. }
  622. }
  623. y_ptr += s->frame.linesize[0];
  624. }
  625. break;
  626. case FLI_COPY:
  627. case FLI_DTA_COPY:
  628. /* copy the chunk (uncompressed frame) */
  629. if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
  630. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  631. "bigger than image, skipping chunk\n", chunk_size - 6);
  632. bytestream2_skip(&g2, chunk_size - 6);
  633. } else {
  634. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  635. y_ptr += s->frame.linesize[0]) {
  636. pixel_countdown = s->avctx->width;
  637. pixel_ptr = 0;
  638. while (pixel_countdown > 0) {
  639. *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
  640. pixel_ptr += 2;
  641. pixel_countdown--;
  642. }
  643. }
  644. }
  645. break;
  646. case FLI_MINI:
  647. /* some sort of a thumbnail? disregard this chunk... */
  648. bytestream2_skip(&g2, chunk_size - 6);
  649. break;
  650. default:
  651. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  652. break;
  653. }
  654. frame_size -= chunk_size;
  655. num_chunks--;
  656. }
  657. /* by the end of the chunk, the stream ptr should equal the frame
  658. * size (minus 1, possibly); if it doesn't, issue a warning */
  659. if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
  660. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  661. "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
  662. *got_frame = 1;
  663. *(AVFrame*)data = s->frame;
  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. if (s->frame.data[0])
  703. avctx->release_buffer(avctx, &s->frame);
  704. return 0;
  705. }
  706. AVCodec ff_flic_decoder = {
  707. .name = "flic",
  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. .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
  716. };