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.

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