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.

834 lines
32KB

  1. /*
  2. * FLI/FLC Animation Video Decoder
  3. * Copyright (c) 2003, 2004 The FFmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Autodesk Animator FLI/FLC Video Decoder
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the .fli/.flc file format and all of its many
  26. * variations, visit:
  27. * http://www.compuphase.com/flic.htm
  28. *
  29. * This decoder outputs PAL8/RGB555/RGB565 and maybe one day RGB24
  30. * colorspace data, depending on the FLC. To use this decoder, be
  31. * sure that your demuxer sends the FLI file header to the decoder via
  32. * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
  33. * large. The only exception is for FLI files from the game "Magic Carpet",
  34. * in which the header is only 12 bytes.
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include "libavutil/intreadwrite.h"
  40. #include "avcodec.h"
  41. #include "bytestream.h"
  42. #include "internal.h"
  43. #include "mathops.h"
  44. #define FLI_256_COLOR 4
  45. #define FLI_DELTA 7
  46. #define FLI_COLOR 11
  47. #define FLI_LC 12
  48. #define FLI_BLACK 13
  49. #define FLI_BRUN 15
  50. #define FLI_COPY 16
  51. #define FLI_MINI 18
  52. #define FLI_DTA_BRUN 25
  53. #define FLI_DTA_COPY 26
  54. #define FLI_DTA_LC 27
  55. #define FLI_TYPE_CODE (0xAF11)
  56. #define FLC_FLX_TYPE_CODE (0xAF12)
  57. #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
  58. #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
  59. #define CHECK_PIXEL_PTR(n) \
  60. if (pixel_ptr + n > pixel_limit) { \
  61. av_log (s->avctx, AV_LOG_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
  62. pixel_ptr + n, pixel_limit); \
  63. return AVERROR_INVALIDDATA; \
  64. } \
  65. typedef struct FlicDecodeContext {
  66. AVCodecContext *avctx;
  67. AVFrame *frame;
  68. unsigned int palette[256];
  69. int new_palette;
  70. int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
  71. } FlicDecodeContext;
  72. static av_cold int flic_decode_init(AVCodecContext *avctx)
  73. {
  74. FlicDecodeContext *s = avctx->priv_data;
  75. unsigned char *fli_header = (unsigned char *)avctx->extradata;
  76. int depth;
  77. if (avctx->extradata_size != 0 &&
  78. avctx->extradata_size != 12 &&
  79. avctx->extradata_size != 128 &&
  80. avctx->extradata_size != 256 &&
  81. avctx->extradata_size != 904 &&
  82. avctx->extradata_size != 1024) {
  83. av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
  84. return AVERROR_INVALIDDATA;
  85. }
  86. s->avctx = avctx;
  87. if (s->avctx->extradata_size == 12) {
  88. /* special case for magic carpet FLIs */
  89. s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
  90. depth = 8;
  91. } else if (avctx->extradata_size == 1024) {
  92. uint8_t *ptr = avctx->extradata;
  93. int i;
  94. for (i = 0; i < 256; i++) {
  95. s->palette[i] = AV_RL32(ptr);
  96. ptr += 4;
  97. }
  98. depth = 8;
  99. /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
  100. } else if (avctx->extradata_size == 0 ||
  101. avctx->extradata_size == 256 ||
  102. /* see FFmpeg ticket #1234 */
  103. avctx->extradata_size == 904) {
  104. s->fli_type = FLI_TYPE_CODE;
  105. depth = 8;
  106. } else {
  107. s->fli_type = AV_RL16(&fli_header[4]);
  108. depth = AV_RL16(&fli_header[12]);
  109. }
  110. if (depth == 0) {
  111. depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
  112. }
  113. if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
  114. depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
  115. }
  116. switch (depth) {
  117. case 8 : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
  118. case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  119. case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
  120. case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
  121. avpriv_request_sample(avctx, "24Bpp FLC/FLX");
  122. return AVERROR_PATCHWELCOME;
  123. default :
  124. av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
  125. return AVERROR_INVALIDDATA;
  126. }
  127. s->frame = av_frame_alloc();
  128. if (!s->frame)
  129. return AVERROR(ENOMEM);
  130. s->new_palette = 0;
  131. return 0;
  132. }
  133. static int flic_decode_frame_8BPP(AVCodecContext *avctx,
  134. void *data, int *got_frame,
  135. const uint8_t *buf, int buf_size)
  136. {
  137. FlicDecodeContext *s = avctx->priv_data;
  138. GetByteContext g2;
  139. int pixel_ptr;
  140. int palette_ptr;
  141. unsigned char palette_idx1;
  142. unsigned char palette_idx2;
  143. unsigned int frame_size;
  144. int num_chunks;
  145. unsigned int chunk_size;
  146. int chunk_type;
  147. int i, j, ret;
  148. int color_packets;
  149. int color_changes;
  150. int color_shift;
  151. unsigned char r, g, b;
  152. int lines;
  153. int compressed_lines;
  154. int starting_line;
  155. signed short line_packets;
  156. int y_ptr;
  157. int byte_run;
  158. int pixel_skip;
  159. int pixel_countdown;
  160. unsigned char *pixels;
  161. unsigned int pixel_limit;
  162. bytestream2_init(&g2, buf, buf_size);
  163. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  164. return ret;
  165. pixels = s->frame->data[0];
  166. pixel_limit = s->avctx->height * s->frame->linesize[0];
  167. if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + FF_INPUT_BUFFER_PADDING_SIZE))
  168. return AVERROR_INVALIDDATA;
  169. frame_size = bytestream2_get_le32(&g2);
  170. if (frame_size > buf_size)
  171. frame_size = buf_size;
  172. bytestream2_skip(&g2, 2); /* skip the magic number */
  173. num_chunks = bytestream2_get_le16(&g2);
  174. bytestream2_skip(&g2, 8); /* skip padding */
  175. if (frame_size < 16)
  176. return AVERROR_INVALIDDATA;
  177. frame_size -= 16;
  178. /* iterate through the chunks */
  179. while ((frame_size >= 6) && (num_chunks > 0) &&
  180. bytestream2_get_bytes_left(&g2) >= 4) {
  181. int stream_ptr_after_chunk;
  182. chunk_size = bytestream2_get_le32(&g2);
  183. if (chunk_size > frame_size) {
  184. av_log(avctx, AV_LOG_WARNING,
  185. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  186. chunk_size = frame_size;
  187. }
  188. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  189. chunk_type = bytestream2_get_le16(&g2);
  190. switch (chunk_type) {
  191. case FLI_256_COLOR:
  192. case FLI_COLOR:
  193. /* check special case: If this file is from the Magic Carpet
  194. * game and uses 6-bit colors even though it reports 256-color
  195. * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
  196. * initialization) */
  197. if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
  198. color_shift = 0;
  199. else
  200. color_shift = 2;
  201. /* set up the palette */
  202. color_packets = bytestream2_get_le16(&g2);
  203. palette_ptr = 0;
  204. for (i = 0; i < color_packets; i++) {
  205. /* first byte is how many colors to skip */
  206. palette_ptr += bytestream2_get_byte(&g2);
  207. /* next byte indicates how many entries to change */
  208. color_changes = bytestream2_get_byte(&g2);
  209. /* if there are 0 color changes, there are actually 256 */
  210. if (color_changes == 0)
  211. color_changes = 256;
  212. if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
  213. break;
  214. for (j = 0; j < color_changes; j++) {
  215. unsigned int entry;
  216. /* wrap around, for good measure */
  217. if ((unsigned)palette_ptr >= 256)
  218. palette_ptr = 0;
  219. r = bytestream2_get_byte(&g2) << color_shift;
  220. g = bytestream2_get_byte(&g2) << color_shift;
  221. b = bytestream2_get_byte(&g2) << color_shift;
  222. entry = 0xFFU << 24 | r << 16 | g << 8 | b;
  223. if (color_shift == 2)
  224. entry |= entry >> 6 & 0x30303;
  225. if (s->palette[palette_ptr] != entry)
  226. s->new_palette = 1;
  227. s->palette[palette_ptr++] = entry;
  228. }
  229. }
  230. break;
  231. case FLI_DELTA:
  232. y_ptr = 0;
  233. compressed_lines = bytestream2_get_le16(&g2);
  234. while (compressed_lines > 0) {
  235. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  236. break;
  237. if (y_ptr > pixel_limit)
  238. return AVERROR_INVALIDDATA;
  239. line_packets = bytestream2_get_le16(&g2);
  240. if ((line_packets & 0xC000) == 0xC000) {
  241. // line skip opcode
  242. line_packets = -line_packets;
  243. if (line_packets > s->avctx->height)
  244. return AVERROR_INVALIDDATA;
  245. y_ptr += line_packets * s->frame->linesize[0];
  246. } else if ((line_packets & 0xC000) == 0x4000) {
  247. av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
  248. } else if ((line_packets & 0xC000) == 0x8000) {
  249. // "last byte" opcode
  250. pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
  251. CHECK_PIXEL_PTR(0);
  252. pixels[pixel_ptr] = line_packets & 0xff;
  253. } else {
  254. compressed_lines--;
  255. pixel_ptr = y_ptr;
  256. CHECK_PIXEL_PTR(0);
  257. pixel_countdown = s->avctx->width;
  258. for (i = 0; i < line_packets; i++) {
  259. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  260. break;
  261. /* account for the skip bytes */
  262. pixel_skip = bytestream2_get_byte(&g2);
  263. pixel_ptr += pixel_skip;
  264. pixel_countdown -= pixel_skip;
  265. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  266. if (byte_run < 0) {
  267. byte_run = -byte_run;
  268. palette_idx1 = bytestream2_get_byte(&g2);
  269. palette_idx2 = bytestream2_get_byte(&g2);
  270. CHECK_PIXEL_PTR(byte_run * 2);
  271. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  272. pixels[pixel_ptr++] = palette_idx1;
  273. pixels[pixel_ptr++] = palette_idx2;
  274. }
  275. } else {
  276. CHECK_PIXEL_PTR(byte_run * 2);
  277. if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
  278. break;
  279. for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
  280. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  281. }
  282. }
  283. }
  284. y_ptr += s->frame->linesize[0];
  285. }
  286. }
  287. break;
  288. case FLI_LC:
  289. /* line compressed */
  290. starting_line = bytestream2_get_le16(&g2);
  291. if (starting_line >= s->avctx->height)
  292. return AVERROR_INVALIDDATA;
  293. y_ptr = 0;
  294. y_ptr += starting_line * s->frame->linesize[0];
  295. compressed_lines = bytestream2_get_le16(&g2);
  296. while (compressed_lines > 0) {
  297. pixel_ptr = y_ptr;
  298. CHECK_PIXEL_PTR(0);
  299. pixel_countdown = s->avctx->width;
  300. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  301. break;
  302. line_packets = bytestream2_get_byte(&g2);
  303. if (line_packets > 0) {
  304. for (i = 0; i < line_packets; i++) {
  305. /* account for the skip bytes */
  306. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  307. break;
  308. pixel_skip = bytestream2_get_byte(&g2);
  309. pixel_ptr += pixel_skip;
  310. pixel_countdown -= pixel_skip;
  311. byte_run = sign_extend(bytestream2_get_byte(&g2),8);
  312. if (byte_run > 0) {
  313. CHECK_PIXEL_PTR(byte_run);
  314. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  315. break;
  316. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  317. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  318. }
  319. } else if (byte_run < 0) {
  320. byte_run = -byte_run;
  321. palette_idx1 = bytestream2_get_byte(&g2);
  322. CHECK_PIXEL_PTR(byte_run);
  323. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  324. pixels[pixel_ptr++] = palette_idx1;
  325. }
  326. }
  327. }
  328. }
  329. y_ptr += s->frame->linesize[0];
  330. compressed_lines--;
  331. }
  332. break;
  333. case FLI_BLACK:
  334. /* set the whole frame to color 0 (which is usually black) */
  335. memset(pixels, 0,
  336. s->frame->linesize[0] * s->avctx->height);
  337. break;
  338. case FLI_BRUN:
  339. /* Byte run compression: This chunk type only occurs in the first
  340. * FLI frame and it will update the entire frame. */
  341. y_ptr = 0;
  342. for (lines = 0; lines < s->avctx->height; lines++) {
  343. pixel_ptr = y_ptr;
  344. /* disregard the line packets; instead, iterate through all
  345. * pixels on a row */
  346. bytestream2_skip(&g2, 1);
  347. pixel_countdown = s->avctx->width;
  348. while (pixel_countdown > 0) {
  349. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  350. break;
  351. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  352. if (!byte_run) {
  353. av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
  354. return AVERROR_INVALIDDATA;
  355. }
  356. if (byte_run > 0) {
  357. palette_idx1 = bytestream2_get_byte(&g2);
  358. CHECK_PIXEL_PTR(byte_run);
  359. for (j = 0; j < byte_run; j++) {
  360. pixels[pixel_ptr++] = palette_idx1;
  361. pixel_countdown--;
  362. if (pixel_countdown < 0)
  363. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  364. pixel_countdown, lines);
  365. }
  366. } else { /* copy bytes if byte_run < 0 */
  367. byte_run = -byte_run;
  368. CHECK_PIXEL_PTR(byte_run);
  369. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  370. break;
  371. for (j = 0; j < byte_run; j++) {
  372. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  373. pixel_countdown--;
  374. if (pixel_countdown < 0)
  375. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  376. pixel_countdown, lines);
  377. }
  378. }
  379. }
  380. y_ptr += s->frame->linesize[0];
  381. }
  382. break;
  383. case FLI_COPY:
  384. /* copy the chunk (uncompressed frame) */
  385. if (chunk_size - 6 != s->avctx->width * s->avctx->height) {
  386. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  387. "has incorrect size, skipping chunk\n", chunk_size - 6);
  388. bytestream2_skip(&g2, chunk_size - 6);
  389. } else {
  390. for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
  391. y_ptr += s->frame->linesize[0]) {
  392. bytestream2_get_buffer(&g2, &pixels[y_ptr],
  393. s->avctx->width);
  394. }
  395. }
  396. break;
  397. case FLI_MINI:
  398. /* some sort of a thumbnail? disregard this chunk... */
  399. break;
  400. default:
  401. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  402. break;
  403. }
  404. if (stream_ptr_after_chunk - bytestream2_tell(&g2) > 0)
  405. bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
  406. frame_size -= chunk_size;
  407. num_chunks--;
  408. }
  409. /* by the end of the chunk, the stream ptr should equal the frame
  410. * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
  411. if (bytestream2_get_bytes_left(&g2) > 2)
  412. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  413. "and final chunk ptr = %d\n", buf_size,
  414. buf_size - bytestream2_get_bytes_left(&g2));
  415. /* make the palette available on the way out */
  416. memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
  417. if (s->new_palette) {
  418. s->frame->palette_has_changed = 1;
  419. s->new_palette = 0;
  420. }
  421. if ((ret = av_frame_ref(data, s->frame)) < 0)
  422. return ret;
  423. *got_frame = 1;
  424. return buf_size;
  425. }
  426. static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
  427. void *data, int *got_frame,
  428. const uint8_t *buf, int buf_size)
  429. {
  430. /* Note, the only difference between the 15Bpp and 16Bpp */
  431. /* Format is the pixel format, the packets are processed the same. */
  432. FlicDecodeContext *s = avctx->priv_data;
  433. GetByteContext g2;
  434. int pixel_ptr;
  435. unsigned char palette_idx1;
  436. unsigned int frame_size;
  437. int num_chunks;
  438. unsigned int chunk_size;
  439. int chunk_type;
  440. int i, j, ret;
  441. int lines;
  442. int compressed_lines;
  443. signed short line_packets;
  444. int y_ptr;
  445. int byte_run;
  446. int pixel_skip;
  447. int pixel_countdown;
  448. unsigned char *pixels;
  449. int pixel;
  450. unsigned int pixel_limit;
  451. bytestream2_init(&g2, buf, buf_size);
  452. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  453. return ret;
  454. pixels = s->frame->data[0];
  455. pixel_limit = s->avctx->height * s->frame->linesize[0];
  456. frame_size = bytestream2_get_le32(&g2);
  457. bytestream2_skip(&g2, 2); /* skip the magic number */
  458. num_chunks = bytestream2_get_le16(&g2);
  459. bytestream2_skip(&g2, 8); /* skip padding */
  460. if (frame_size > buf_size)
  461. frame_size = buf_size;
  462. if (frame_size < 16)
  463. return AVERROR_INVALIDDATA;
  464. frame_size -= 16;
  465. /* iterate through the chunks */
  466. while ((frame_size > 0) && (num_chunks > 0) &&
  467. bytestream2_get_bytes_left(&g2) >= 4) {
  468. int stream_ptr_after_chunk;
  469. chunk_size = bytestream2_get_le32(&g2);
  470. if (chunk_size > frame_size) {
  471. av_log(avctx, AV_LOG_WARNING,
  472. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  473. chunk_size = frame_size;
  474. }
  475. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  476. chunk_type = bytestream2_get_le16(&g2);
  477. switch (chunk_type) {
  478. case FLI_256_COLOR:
  479. case FLI_COLOR:
  480. /* For some reason, it seems that non-palettized flics do
  481. * include one of these chunks in their first frame.
  482. * Why I do not know, it seems rather extraneous. */
  483. av_dlog(avctx,
  484. "Unexpected Palette chunk %d in non-palettized FLC\n",
  485. chunk_type);
  486. bytestream2_skip(&g2, chunk_size - 6);
  487. break;
  488. case FLI_DELTA:
  489. case FLI_DTA_LC:
  490. y_ptr = 0;
  491. compressed_lines = bytestream2_get_le16(&g2);
  492. while (compressed_lines > 0) {
  493. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  494. break;
  495. if (y_ptr > pixel_limit)
  496. return AVERROR_INVALIDDATA;
  497. line_packets = bytestream2_get_le16(&g2);
  498. if (line_packets < 0) {
  499. line_packets = -line_packets;
  500. if (line_packets > s->avctx->height)
  501. return AVERROR_INVALIDDATA;
  502. y_ptr += line_packets * s->frame->linesize[0];
  503. } else {
  504. compressed_lines--;
  505. pixel_ptr = y_ptr;
  506. CHECK_PIXEL_PTR(0);
  507. pixel_countdown = s->avctx->width;
  508. for (i = 0; i < line_packets; i++) {
  509. /* account for the skip bytes */
  510. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  511. break;
  512. pixel_skip = bytestream2_get_byte(&g2);
  513. pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
  514. pixel_countdown -= pixel_skip;
  515. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  516. if (byte_run < 0) {
  517. byte_run = -byte_run;
  518. pixel = bytestream2_get_le16(&g2);
  519. CHECK_PIXEL_PTR(2 * byte_run);
  520. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  521. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  522. pixel_ptr += 2;
  523. }
  524. } else {
  525. if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
  526. break;
  527. CHECK_PIXEL_PTR(2 * byte_run);
  528. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  529. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  530. pixel_ptr += 2;
  531. }
  532. }
  533. }
  534. y_ptr += s->frame->linesize[0];
  535. }
  536. }
  537. break;
  538. case FLI_LC:
  539. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
  540. bytestream2_skip(&g2, chunk_size - 6);
  541. break;
  542. case FLI_BLACK:
  543. /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
  544. memset(pixels, 0x0000,
  545. s->frame->linesize[0] * s->avctx->height);
  546. break;
  547. case FLI_BRUN:
  548. y_ptr = 0;
  549. for (lines = 0; lines < s->avctx->height; lines++) {
  550. pixel_ptr = y_ptr;
  551. /* disregard the line packets; instead, iterate through all
  552. * pixels on a row */
  553. bytestream2_skip(&g2, 1);
  554. pixel_countdown = (s->avctx->width * 2);
  555. while (pixel_countdown > 0) {
  556. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  557. break;
  558. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  559. if (byte_run > 0) {
  560. palette_idx1 = bytestream2_get_byte(&g2);
  561. CHECK_PIXEL_PTR(byte_run);
  562. for (j = 0; j < byte_run; j++) {
  563. pixels[pixel_ptr++] = palette_idx1;
  564. pixel_countdown--;
  565. if (pixel_countdown < 0)
  566. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
  567. pixel_countdown, lines);
  568. }
  569. } else { /* copy bytes if byte_run < 0 */
  570. byte_run = -byte_run;
  571. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  572. break;
  573. CHECK_PIXEL_PTR(byte_run);
  574. for (j = 0; j < byte_run; j++) {
  575. palette_idx1 = bytestream2_get_byte(&g2);
  576. pixels[pixel_ptr++] = palette_idx1;
  577. pixel_countdown--;
  578. if (pixel_countdown < 0)
  579. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  580. pixel_countdown, lines);
  581. }
  582. }
  583. }
  584. /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
  585. * This does not give us any good opportunity to perform word endian conversion
  586. * during decompression. So if it is required (i.e., this is not a LE target, we do
  587. * a second pass over the line here, swapping the bytes.
  588. */
  589. #if HAVE_BIGENDIAN
  590. pixel_ptr = y_ptr;
  591. pixel_countdown = s->avctx->width;
  592. while (pixel_countdown > 0) {
  593. *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
  594. pixel_ptr += 2;
  595. }
  596. #endif
  597. y_ptr += s->frame->linesize[0];
  598. }
  599. break;
  600. case FLI_DTA_BRUN:
  601. y_ptr = 0;
  602. for (lines = 0; lines < s->avctx->height; lines++) {
  603. pixel_ptr = y_ptr;
  604. /* disregard the line packets; instead, iterate through all
  605. * pixels on a row */
  606. bytestream2_skip(&g2, 1);
  607. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  608. while (pixel_countdown > 0) {
  609. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  610. break;
  611. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  612. if (byte_run > 0) {
  613. pixel = bytestream2_get_le16(&g2);
  614. CHECK_PIXEL_PTR(2 * byte_run);
  615. for (j = 0; j < byte_run; j++) {
  616. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  617. pixel_ptr += 2;
  618. pixel_countdown--;
  619. if (pixel_countdown < 0)
  620. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  621. pixel_countdown);
  622. }
  623. } else { /* copy pixels if byte_run < 0 */
  624. byte_run = -byte_run;
  625. if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
  626. break;
  627. CHECK_PIXEL_PTR(2 * byte_run);
  628. for (j = 0; j < byte_run; j++) {
  629. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  630. pixel_ptr += 2;
  631. pixel_countdown--;
  632. if (pixel_countdown < 0)
  633. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  634. pixel_countdown);
  635. }
  636. }
  637. }
  638. y_ptr += s->frame->linesize[0];
  639. }
  640. break;
  641. case FLI_COPY:
  642. case FLI_DTA_COPY:
  643. /* copy the chunk (uncompressed frame) */
  644. if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
  645. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  646. "bigger than image, skipping chunk\n", chunk_size - 6);
  647. bytestream2_skip(&g2, chunk_size - 6);
  648. } else {
  649. for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
  650. y_ptr += s->frame->linesize[0]) {
  651. pixel_countdown = s->avctx->width;
  652. pixel_ptr = 0;
  653. while (pixel_countdown > 0) {
  654. *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
  655. pixel_ptr += 2;
  656. pixel_countdown--;
  657. }
  658. }
  659. }
  660. break;
  661. case FLI_MINI:
  662. /* some sort of a thumbnail? disregard this chunk... */
  663. bytestream2_skip(&g2, chunk_size - 6);
  664. break;
  665. default:
  666. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  667. break;
  668. }
  669. frame_size -= chunk_size;
  670. num_chunks--;
  671. }
  672. /* by the end of the chunk, the stream ptr should equal the frame
  673. * size (minus 1, possibly); if it doesn't, issue a warning */
  674. if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
  675. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  676. "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
  677. if ((ret = av_frame_ref(data, s->frame)) < 0)
  678. return ret;
  679. *got_frame = 1;
  680. return buf_size;
  681. }
  682. static int flic_decode_frame_24BPP(AVCodecContext *avctx,
  683. void *data, int *got_frame,
  684. const uint8_t *buf, int buf_size)
  685. {
  686. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
  687. return AVERROR_PATCHWELCOME;
  688. }
  689. static int flic_decode_frame(AVCodecContext *avctx,
  690. void *data, int *got_frame,
  691. AVPacket *avpkt)
  692. {
  693. const uint8_t *buf = avpkt->data;
  694. int buf_size = avpkt->size;
  695. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  696. return flic_decode_frame_8BPP(avctx, data, got_frame,
  697. buf, buf_size);
  698. }
  699. else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
  700. (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
  701. return flic_decode_frame_15_16BPP(avctx, data, got_frame,
  702. buf, buf_size);
  703. }
  704. else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  705. return flic_decode_frame_24BPP(avctx, data, got_frame,
  706. buf, buf_size);
  707. }
  708. /* Should not get here, ever as the pix_fmt is processed */
  709. /* in flic_decode_init and the above if should deal with */
  710. /* the finite set of possibilites allowable by here. */
  711. /* But in case we do, just error out. */
  712. av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
  713. return AVERROR_BUG;
  714. }
  715. static av_cold int flic_decode_end(AVCodecContext *avctx)
  716. {
  717. FlicDecodeContext *s = avctx->priv_data;
  718. av_frame_free(&s->frame);
  719. return 0;
  720. }
  721. AVCodec ff_flic_decoder = {
  722. .name = "flic",
  723. .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
  724. .type = AVMEDIA_TYPE_VIDEO,
  725. .id = AV_CODEC_ID_FLIC,
  726. .priv_data_size = sizeof(FlicDecodeContext),
  727. .init = flic_decode_init,
  728. .close = flic_decode_end,
  729. .decode = flic_decode_frame,
  730. .capabilities = CODEC_CAP_DR1,
  731. };