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.

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