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.

755 lines
29KB

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