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.

752 lines
28KB

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