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.

748 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 = AV_PIX_FMT_PAL8; break;
  99. case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  100. case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
  101. case 24 : avctx->pix_fmt = AV_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_dlog(avctx,
  422. "Unexpected Palette chunk %d in non-palettized FLC\n",
  423. chunk_type);
  424. bytestream2_skip(&g2, chunk_size - 6);
  425. break;
  426. case FLI_DELTA:
  427. case FLI_DTA_LC:
  428. y_ptr = 0;
  429. compressed_lines = bytestream2_get_le16(&g2);
  430. while (compressed_lines > 0) {
  431. line_packets = bytestream2_get_le16(&g2);
  432. if (line_packets < 0) {
  433. line_packets = -line_packets;
  434. y_ptr += line_packets * s->frame.linesize[0];
  435. } else {
  436. compressed_lines--;
  437. pixel_ptr = y_ptr;
  438. CHECK_PIXEL_PTR(0);
  439. pixel_countdown = s->avctx->width;
  440. for (i = 0; i < line_packets; i++) {
  441. /* account for the skip bytes */
  442. pixel_skip = bytestream2_get_byte(&g2);
  443. pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
  444. pixel_countdown -= pixel_skip;
  445. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  446. if (byte_run < 0) {
  447. byte_run = -byte_run;
  448. pixel = bytestream2_get_le16(&g2);
  449. CHECK_PIXEL_PTR(2 * byte_run);
  450. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  451. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  452. pixel_ptr += 2;
  453. }
  454. } else {
  455. CHECK_PIXEL_PTR(2 * byte_run);
  456. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  457. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  458. pixel_ptr += 2;
  459. }
  460. }
  461. }
  462. y_ptr += s->frame.linesize[0];
  463. }
  464. }
  465. break;
  466. case FLI_LC:
  467. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
  468. bytestream2_skip(&g2, chunk_size - 6);
  469. break;
  470. case FLI_BLACK:
  471. /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
  472. memset(pixels, 0x0000,
  473. s->frame.linesize[0] * s->avctx->height);
  474. break;
  475. case FLI_BRUN:
  476. y_ptr = 0;
  477. for (lines = 0; lines < s->avctx->height; lines++) {
  478. pixel_ptr = y_ptr;
  479. /* disregard the line packets; instead, iterate through all
  480. * pixels on a row */
  481. bytestream2_skip(&g2, 1);
  482. pixel_countdown = (s->avctx->width * 2);
  483. while (pixel_countdown > 0) {
  484. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  485. if (byte_run > 0) {
  486. palette_idx1 = bytestream2_get_byte(&g2);
  487. CHECK_PIXEL_PTR(byte_run);
  488. for (j = 0; j < byte_run; j++) {
  489. pixels[pixel_ptr++] = palette_idx1;
  490. pixel_countdown--;
  491. if (pixel_countdown < 0)
  492. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
  493. pixel_countdown, lines);
  494. }
  495. } else { /* copy bytes if byte_run < 0 */
  496. byte_run = -byte_run;
  497. CHECK_PIXEL_PTR(byte_run);
  498. for (j = 0; j < byte_run; j++) {
  499. palette_idx1 = bytestream2_get_byte(&g2);
  500. pixels[pixel_ptr++] = palette_idx1;
  501. pixel_countdown--;
  502. if (pixel_countdown < 0)
  503. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  504. pixel_countdown, lines);
  505. }
  506. }
  507. }
  508. /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
  509. * This does not give us any good oportunity to perform word endian conversion
  510. * during decompression. So if it is required (i.e., this is not a LE target, we do
  511. * a second pass over the line here, swapping the bytes.
  512. */
  513. #if HAVE_BIGENDIAN
  514. pixel_ptr = y_ptr;
  515. pixel_countdown = s->avctx->width;
  516. while (pixel_countdown > 0) {
  517. *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
  518. pixel_ptr += 2;
  519. }
  520. #endif
  521. y_ptr += s->frame.linesize[0];
  522. }
  523. break;
  524. case FLI_DTA_BRUN:
  525. y_ptr = 0;
  526. for (lines = 0; lines < s->avctx->height; lines++) {
  527. pixel_ptr = y_ptr;
  528. /* disregard the line packets; instead, iterate through all
  529. * pixels on a row */
  530. bytestream2_skip(&g2, 1);
  531. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  532. while (pixel_countdown > 0) {
  533. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  534. if (byte_run > 0) {
  535. pixel = bytestream2_get_le16(&g2);
  536. CHECK_PIXEL_PTR(2 * byte_run);
  537. for (j = 0; j < byte_run; j++) {
  538. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  539. pixel_ptr += 2;
  540. pixel_countdown--;
  541. if (pixel_countdown < 0)
  542. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  543. pixel_countdown);
  544. }
  545. } else { /* copy pixels if byte_run < 0 */
  546. byte_run = -byte_run;
  547. CHECK_PIXEL_PTR(2 * byte_run);
  548. for (j = 0; j < byte_run; j++) {
  549. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  550. pixel_ptr += 2;
  551. pixel_countdown--;
  552. if (pixel_countdown < 0)
  553. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  554. pixel_countdown);
  555. }
  556. }
  557. }
  558. y_ptr += s->frame.linesize[0];
  559. }
  560. break;
  561. case FLI_COPY:
  562. case FLI_DTA_COPY:
  563. /* copy the chunk (uncompressed frame) */
  564. if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
  565. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  566. "bigger than image, skipping chunk\n", chunk_size - 6);
  567. bytestream2_skip(&g2, chunk_size - 6);
  568. } else {
  569. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  570. y_ptr += s->frame.linesize[0]) {
  571. pixel_countdown = s->avctx->width;
  572. pixel_ptr = 0;
  573. while (pixel_countdown > 0) {
  574. *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
  575. pixel_ptr += 2;
  576. pixel_countdown--;
  577. }
  578. }
  579. }
  580. break;
  581. case FLI_MINI:
  582. /* some sort of a thumbnail? disregard this chunk... */
  583. bytestream2_skip(&g2, chunk_size - 6);
  584. break;
  585. default:
  586. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  587. break;
  588. }
  589. frame_size -= chunk_size;
  590. num_chunks--;
  591. }
  592. /* by the end of the chunk, the stream ptr should equal the frame
  593. * size (minus 1, possibly); if it doesn't, issue a warning */
  594. if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
  595. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  596. "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
  597. *data_size=sizeof(AVFrame);
  598. *(AVFrame*)data = s->frame;
  599. return buf_size;
  600. }
  601. static int flic_decode_frame_24BPP(AVCodecContext *avctx,
  602. void *data, int *data_size,
  603. const uint8_t *buf, int buf_size)
  604. {
  605. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
  606. return -1;
  607. }
  608. static int flic_decode_frame(AVCodecContext *avctx,
  609. void *data, int *data_size,
  610. AVPacket *avpkt)
  611. {
  612. const uint8_t *buf = avpkt->data;
  613. int buf_size = avpkt->size;
  614. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  615. return flic_decode_frame_8BPP(avctx, data, data_size,
  616. buf, buf_size);
  617. }
  618. else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
  619. (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
  620. return flic_decode_frame_15_16BPP(avctx, data, data_size,
  621. buf, buf_size);
  622. }
  623. else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  624. return flic_decode_frame_24BPP(avctx, data, data_size,
  625. buf, buf_size);
  626. }
  627. /* Should not get here, ever as the pix_fmt is processed */
  628. /* in flic_decode_init and the above if should deal with */
  629. /* the finite set of possibilites allowable by here. */
  630. /* But in case we do, just error out. */
  631. av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
  632. return -1;
  633. }
  634. static av_cold int flic_decode_end(AVCodecContext *avctx)
  635. {
  636. FlicDecodeContext *s = avctx->priv_data;
  637. if (s->frame.data[0])
  638. avctx->release_buffer(avctx, &s->frame);
  639. return 0;
  640. }
  641. AVCodec ff_flic_decoder = {
  642. .name = "flic",
  643. .type = AVMEDIA_TYPE_VIDEO,
  644. .id = AV_CODEC_ID_FLIC,
  645. .priv_data_size = sizeof(FlicDecodeContext),
  646. .init = flic_decode_init,
  647. .close = flic_decode_end,
  648. .decode = flic_decode_frame,
  649. .capabilities = CODEC_CAP_DR1,
  650. .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
  651. };