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.

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