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.

1123 lines
43KB

  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/BGR24. To use this decoder, be
  30. * sure that your demuxer sends the FLI file header to the decoder via
  31. * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
  32. * large. The only exception is for FLI files from the game "Magic Carpet",
  33. * in which the header is only 12 bytes.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "libavutil/intreadwrite.h"
  39. #include "avcodec.h"
  40. #include "bytestream.h"
  41. #include "internal.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_ERROR, "Invalid pixel_ptr = %d > pixel_limit = %d\n", \
  61. pixel_ptr + n, pixel_limit); \
  62. return AVERROR_INVALIDDATA; \
  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 != 0 &&
  77. avctx->extradata_size != 12 &&
  78. avctx->extradata_size != 128 &&
  79. avctx->extradata_size != 256 &&
  80. avctx->extradata_size != 904 &&
  81. avctx->extradata_size != 1024) {
  82. av_log(avctx, AV_LOG_ERROR, "Unexpected extradata size %d\n", avctx->extradata_size);
  83. return AVERROR_INVALIDDATA;
  84. }
  85. s->avctx = avctx;
  86. if (s->avctx->extradata_size == 12) {
  87. /* special case for magic carpet FLIs */
  88. s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
  89. depth = 8;
  90. } else if (avctx->extradata_size == 1024) {
  91. uint8_t *ptr = avctx->extradata;
  92. int i;
  93. for (i = 0; i < 256; i++) {
  94. s->palette[i] = AV_RL32(ptr);
  95. ptr += 4;
  96. }
  97. depth = 8;
  98. /* FLI in MOV, see e.g. FFmpeg trac issue #626 */
  99. } else if (avctx->extradata_size == 0 ||
  100. avctx->extradata_size == 256 ||
  101. /* see FFmpeg ticket #1234 */
  102. avctx->extradata_size == 904) {
  103. s->fli_type = FLI_TYPE_CODE;
  104. depth = 8;
  105. } else {
  106. s->fli_type = AV_RL16(&fli_header[4]);
  107. depth = AV_RL16(&fli_header[12]);
  108. }
  109. if (depth == 0) {
  110. depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
  111. }
  112. if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
  113. depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
  114. }
  115. switch (depth) {
  116. case 8 : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
  117. case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  118. case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
  119. case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
  120. default :
  121. av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
  122. return AVERROR_INVALIDDATA;
  123. }
  124. s->frame = av_frame_alloc();
  125. if (!s->frame)
  126. return AVERROR(ENOMEM);
  127. s->new_palette = 0;
  128. return 0;
  129. }
  130. static int flic_decode_frame_8BPP(AVCodecContext *avctx,
  131. void *data, int *got_frame,
  132. const uint8_t *buf, int buf_size)
  133. {
  134. FlicDecodeContext *s = avctx->priv_data;
  135. GetByteContext g2;
  136. int pixel_ptr;
  137. int palette_ptr;
  138. unsigned char palette_idx1;
  139. unsigned char palette_idx2;
  140. unsigned int frame_size;
  141. int num_chunks;
  142. unsigned int chunk_size;
  143. int chunk_type;
  144. int i, j, ret;
  145. int color_packets;
  146. int color_changes;
  147. int color_shift;
  148. unsigned char r, g, b;
  149. int lines;
  150. int compressed_lines;
  151. int starting_line;
  152. signed short line_packets;
  153. int y_ptr;
  154. int byte_run;
  155. int pixel_skip;
  156. int pixel_countdown;
  157. unsigned char *pixels;
  158. unsigned int pixel_limit;
  159. bytestream2_init(&g2, buf, buf_size);
  160. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  161. return ret;
  162. pixels = s->frame->data[0];
  163. pixel_limit = s->avctx->height * s->frame->linesize[0];
  164. if (buf_size < 16 || buf_size > INT_MAX - (3 * 256 + AV_INPUT_BUFFER_PADDING_SIZE))
  165. return AVERROR_INVALIDDATA;
  166. frame_size = bytestream2_get_le32(&g2);
  167. if (frame_size > buf_size)
  168. frame_size = buf_size;
  169. bytestream2_skip(&g2, 2); /* skip the magic number */
  170. num_chunks = bytestream2_get_le16(&g2);
  171. bytestream2_skip(&g2, 8); /* skip padding */
  172. if (frame_size < 16)
  173. return AVERROR_INVALIDDATA;
  174. frame_size -= 16;
  175. /* iterate through the chunks */
  176. while ((frame_size >= 6) && (num_chunks > 0) &&
  177. bytestream2_get_bytes_left(&g2) >= 4) {
  178. int stream_ptr_after_chunk;
  179. chunk_size = bytestream2_get_le32(&g2);
  180. if (chunk_size > frame_size) {
  181. av_log(avctx, AV_LOG_WARNING,
  182. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  183. chunk_size = frame_size;
  184. }
  185. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  186. chunk_type = bytestream2_get_le16(&g2);
  187. switch (chunk_type) {
  188. case FLI_256_COLOR:
  189. case FLI_COLOR:
  190. /* check special case: If this file is from the Magic Carpet
  191. * game and uses 6-bit colors even though it reports 256-color
  192. * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
  193. * initialization) */
  194. if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
  195. color_shift = 0;
  196. else
  197. color_shift = 2;
  198. /* set up the palette */
  199. color_packets = bytestream2_get_le16(&g2);
  200. palette_ptr = 0;
  201. for (i = 0; i < color_packets; i++) {
  202. /* first byte is how many colors to skip */
  203. palette_ptr += bytestream2_get_byte(&g2);
  204. /* next byte indicates how many entries to change */
  205. color_changes = bytestream2_get_byte(&g2);
  206. /* if there are 0 color changes, there are actually 256 */
  207. if (color_changes == 0)
  208. color_changes = 256;
  209. if (bytestream2_tell(&g2) + color_changes * 3 > stream_ptr_after_chunk)
  210. break;
  211. for (j = 0; j < color_changes; j++) {
  212. unsigned int entry;
  213. /* wrap around, for good measure */
  214. if ((unsigned)palette_ptr >= 256)
  215. palette_ptr = 0;
  216. r = bytestream2_get_byte(&g2) << color_shift;
  217. g = bytestream2_get_byte(&g2) << color_shift;
  218. b = bytestream2_get_byte(&g2) << color_shift;
  219. entry = 0xFFU << 24 | r << 16 | g << 8 | b;
  220. if (color_shift == 2)
  221. entry |= entry >> 6 & 0x30303;
  222. if (s->palette[palette_ptr] != entry)
  223. s->new_palette = 1;
  224. s->palette[palette_ptr++] = entry;
  225. }
  226. }
  227. break;
  228. case FLI_DELTA:
  229. y_ptr = 0;
  230. compressed_lines = bytestream2_get_le16(&g2);
  231. while (compressed_lines > 0) {
  232. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  233. break;
  234. if (y_ptr > pixel_limit)
  235. return AVERROR_INVALIDDATA;
  236. line_packets = bytestream2_get_le16(&g2);
  237. if ((line_packets & 0xC000) == 0xC000) {
  238. // line skip opcode
  239. line_packets = -line_packets;
  240. if (line_packets > s->avctx->height)
  241. return AVERROR_INVALIDDATA;
  242. y_ptr += line_packets * s->frame->linesize[0];
  243. } else if ((line_packets & 0xC000) == 0x4000) {
  244. av_log(avctx, AV_LOG_ERROR, "Undefined opcode (%x) in DELTA_FLI\n", line_packets);
  245. } else if ((line_packets & 0xC000) == 0x8000) {
  246. // "last byte" opcode
  247. pixel_ptr= y_ptr + s->frame->linesize[0] - 1;
  248. CHECK_PIXEL_PTR(0);
  249. pixels[pixel_ptr] = line_packets & 0xff;
  250. } else {
  251. compressed_lines--;
  252. pixel_ptr = y_ptr;
  253. CHECK_PIXEL_PTR(0);
  254. pixel_countdown = s->avctx->width;
  255. for (i = 0; i < line_packets; i++) {
  256. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  257. break;
  258. /* account for the skip bytes */
  259. pixel_skip = bytestream2_get_byte(&g2);
  260. pixel_ptr += pixel_skip;
  261. pixel_countdown -= pixel_skip;
  262. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  263. if (byte_run < 0) {
  264. byte_run = -byte_run;
  265. palette_idx1 = bytestream2_get_byte(&g2);
  266. palette_idx2 = bytestream2_get_byte(&g2);
  267. CHECK_PIXEL_PTR(byte_run * 2);
  268. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  269. pixels[pixel_ptr++] = palette_idx1;
  270. pixels[pixel_ptr++] = palette_idx2;
  271. }
  272. } else {
  273. CHECK_PIXEL_PTR(byte_run * 2);
  274. if (bytestream2_tell(&g2) + byte_run * 2 > stream_ptr_after_chunk)
  275. break;
  276. for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
  277. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  278. }
  279. }
  280. }
  281. y_ptr += s->frame->linesize[0];
  282. }
  283. }
  284. break;
  285. case FLI_LC:
  286. /* line compressed */
  287. starting_line = bytestream2_get_le16(&g2);
  288. if (starting_line >= s->avctx->height)
  289. return AVERROR_INVALIDDATA;
  290. y_ptr = 0;
  291. y_ptr += starting_line * s->frame->linesize[0];
  292. compressed_lines = bytestream2_get_le16(&g2);
  293. while (compressed_lines > 0) {
  294. pixel_ptr = y_ptr;
  295. CHECK_PIXEL_PTR(0);
  296. pixel_countdown = s->avctx->width;
  297. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  298. break;
  299. line_packets = bytestream2_get_byte(&g2);
  300. if (line_packets > 0) {
  301. for (i = 0; i < line_packets; i++) {
  302. /* account for the skip bytes */
  303. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  304. break;
  305. pixel_skip = bytestream2_get_byte(&g2);
  306. pixel_ptr += pixel_skip;
  307. pixel_countdown -= pixel_skip;
  308. byte_run = sign_extend(bytestream2_get_byte(&g2),8);
  309. if (byte_run > 0) {
  310. CHECK_PIXEL_PTR(byte_run);
  311. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  312. break;
  313. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  314. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  315. }
  316. } else if (byte_run < 0) {
  317. byte_run = -byte_run;
  318. palette_idx1 = bytestream2_get_byte(&g2);
  319. CHECK_PIXEL_PTR(byte_run);
  320. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  321. pixels[pixel_ptr++] = palette_idx1;
  322. }
  323. }
  324. }
  325. }
  326. y_ptr += s->frame->linesize[0];
  327. compressed_lines--;
  328. }
  329. break;
  330. case FLI_BLACK:
  331. /* set the whole frame to color 0 (which is usually black) */
  332. memset(pixels, 0,
  333. s->frame->linesize[0] * s->avctx->height);
  334. break;
  335. case FLI_BRUN:
  336. /* Byte run compression: This chunk type only occurs in the first
  337. * FLI frame and it will update the entire frame. */
  338. y_ptr = 0;
  339. for (lines = 0; lines < s->avctx->height; lines++) {
  340. pixel_ptr = y_ptr;
  341. /* disregard the line packets; instead, iterate through all
  342. * pixels on a row */
  343. bytestream2_skip(&g2, 1);
  344. pixel_countdown = s->avctx->width;
  345. while (pixel_countdown > 0) {
  346. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  347. break;
  348. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  349. if (!byte_run) {
  350. av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
  351. return AVERROR_INVALIDDATA;
  352. }
  353. if (byte_run > 0) {
  354. palette_idx1 = bytestream2_get_byte(&g2);
  355. CHECK_PIXEL_PTR(byte_run);
  356. for (j = 0; j < byte_run; j++) {
  357. pixels[pixel_ptr++] = palette_idx1;
  358. pixel_countdown--;
  359. if (pixel_countdown < 0)
  360. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  361. pixel_countdown, lines);
  362. }
  363. } else { /* copy bytes if byte_run < 0 */
  364. byte_run = -byte_run;
  365. CHECK_PIXEL_PTR(byte_run);
  366. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  367. break;
  368. for (j = 0; j < byte_run; j++) {
  369. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  370. pixel_countdown--;
  371. if (pixel_countdown < 0)
  372. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  373. pixel_countdown, lines);
  374. }
  375. }
  376. }
  377. y_ptr += s->frame->linesize[0];
  378. }
  379. break;
  380. case FLI_COPY:
  381. /* copy the chunk (uncompressed frame) */
  382. if (chunk_size - 6 != FFALIGN(s->avctx->width, 4) * s->avctx->height) {
  383. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  384. "has incorrect size, skipping chunk\n", chunk_size - 6);
  385. bytestream2_skip(&g2, chunk_size - 6);
  386. } else {
  387. for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
  388. y_ptr += s->frame->linesize[0]) {
  389. bytestream2_get_buffer(&g2, &pixels[y_ptr],
  390. s->avctx->width);
  391. if (s->avctx->width & 3)
  392. bytestream2_skip(&g2, 4 - (s->avctx->width & 3));
  393. }
  394. }
  395. break;
  396. case FLI_MINI:
  397. /* some sort of a thumbnail? disregard this chunk... */
  398. break;
  399. default:
  400. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  401. break;
  402. }
  403. if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
  404. bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
  405. } else {
  406. av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
  407. break;
  408. }
  409. frame_size -= chunk_size;
  410. num_chunks--;
  411. }
  412. /* by the end of the chunk, the stream ptr should equal the frame
  413. * size (minus 1 or 2, possibly); if it doesn't, issue a warning */
  414. if (bytestream2_get_bytes_left(&g2) > 2)
  415. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  416. "and final chunk ptr = %d\n", buf_size,
  417. buf_size - bytestream2_get_bytes_left(&g2));
  418. /* make the palette available on the way out */
  419. memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
  420. if (s->new_palette) {
  421. s->frame->palette_has_changed = 1;
  422. s->new_palette = 0;
  423. }
  424. if ((ret = av_frame_ref(data, s->frame)) < 0)
  425. return ret;
  426. *got_frame = 1;
  427. return buf_size;
  428. }
  429. static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
  430. void *data, int *got_frame,
  431. const uint8_t *buf, int buf_size)
  432. {
  433. /* Note, the only difference between the 15Bpp and 16Bpp */
  434. /* Format is the pixel format, the packets are processed the same. */
  435. FlicDecodeContext *s = avctx->priv_data;
  436. GetByteContext g2;
  437. int pixel_ptr;
  438. unsigned char palette_idx1;
  439. unsigned int frame_size;
  440. int num_chunks;
  441. unsigned int chunk_size;
  442. int chunk_type;
  443. int i, j, ret;
  444. int lines;
  445. int compressed_lines;
  446. signed short line_packets;
  447. int y_ptr;
  448. int byte_run;
  449. int pixel_skip;
  450. int pixel_countdown;
  451. unsigned char *pixels;
  452. int pixel;
  453. unsigned int pixel_limit;
  454. bytestream2_init(&g2, buf, buf_size);
  455. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  456. return ret;
  457. pixels = s->frame->data[0];
  458. pixel_limit = s->avctx->height * s->frame->linesize[0];
  459. frame_size = bytestream2_get_le32(&g2);
  460. bytestream2_skip(&g2, 2); /* skip the magic number */
  461. num_chunks = bytestream2_get_le16(&g2);
  462. bytestream2_skip(&g2, 8); /* skip padding */
  463. if (frame_size > buf_size)
  464. frame_size = buf_size;
  465. if (frame_size < 16)
  466. return AVERROR_INVALIDDATA;
  467. frame_size -= 16;
  468. /* iterate through the chunks */
  469. while ((frame_size > 0) && (num_chunks > 0) &&
  470. bytestream2_get_bytes_left(&g2) >= 4) {
  471. int stream_ptr_after_chunk;
  472. chunk_size = bytestream2_get_le32(&g2);
  473. if (chunk_size > frame_size) {
  474. av_log(avctx, AV_LOG_WARNING,
  475. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  476. chunk_size = frame_size;
  477. }
  478. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  479. chunk_type = bytestream2_get_le16(&g2);
  480. switch (chunk_type) {
  481. case FLI_256_COLOR:
  482. case FLI_COLOR:
  483. /* For some reason, it seems that non-palettized flics do
  484. * include one of these chunks in their first frame.
  485. * Why I do not know, it seems rather extraneous. */
  486. ff_dlog(avctx,
  487. "Unexpected Palette chunk %d in non-palettized FLC\n",
  488. chunk_type);
  489. bytestream2_skip(&g2, chunk_size - 6);
  490. break;
  491. case FLI_DELTA:
  492. case FLI_DTA_LC:
  493. y_ptr = 0;
  494. compressed_lines = bytestream2_get_le16(&g2);
  495. while (compressed_lines > 0) {
  496. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  497. break;
  498. if (y_ptr > pixel_limit)
  499. return AVERROR_INVALIDDATA;
  500. line_packets = bytestream2_get_le16(&g2);
  501. if (line_packets < 0) {
  502. line_packets = -line_packets;
  503. if (line_packets > s->avctx->height)
  504. return AVERROR_INVALIDDATA;
  505. y_ptr += line_packets * s->frame->linesize[0];
  506. } else {
  507. compressed_lines--;
  508. pixel_ptr = y_ptr;
  509. CHECK_PIXEL_PTR(0);
  510. pixel_countdown = s->avctx->width;
  511. for (i = 0; i < line_packets; i++) {
  512. /* account for the skip bytes */
  513. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  514. break;
  515. pixel_skip = bytestream2_get_byte(&g2);
  516. pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
  517. pixel_countdown -= pixel_skip;
  518. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  519. if (byte_run < 0) {
  520. byte_run = -byte_run;
  521. pixel = bytestream2_get_le16(&g2);
  522. CHECK_PIXEL_PTR(2 * byte_run);
  523. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  524. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  525. pixel_ptr += 2;
  526. }
  527. } else {
  528. if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
  529. break;
  530. CHECK_PIXEL_PTR(2 * byte_run);
  531. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  532. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  533. pixel_ptr += 2;
  534. }
  535. }
  536. }
  537. y_ptr += s->frame->linesize[0];
  538. }
  539. }
  540. break;
  541. case FLI_LC:
  542. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
  543. bytestream2_skip(&g2, chunk_size - 6);
  544. break;
  545. case FLI_BLACK:
  546. /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
  547. memset(pixels, 0x0000,
  548. s->frame->linesize[0] * s->avctx->height);
  549. break;
  550. case FLI_BRUN:
  551. y_ptr = 0;
  552. for (lines = 0; lines < s->avctx->height; lines++) {
  553. pixel_ptr = y_ptr;
  554. /* disregard the line packets; instead, iterate through all
  555. * pixels on a row */
  556. bytestream2_skip(&g2, 1);
  557. pixel_countdown = (s->avctx->width * 2);
  558. while (pixel_countdown > 0) {
  559. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  560. break;
  561. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  562. if (byte_run > 0) {
  563. palette_idx1 = bytestream2_get_byte(&g2);
  564. CHECK_PIXEL_PTR(byte_run);
  565. for (j = 0; j < byte_run; j++) {
  566. pixels[pixel_ptr++] = palette_idx1;
  567. pixel_countdown--;
  568. if (pixel_countdown < 0)
  569. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
  570. pixel_countdown, lines);
  571. }
  572. } else { /* copy bytes if byte_run < 0 */
  573. byte_run = -byte_run;
  574. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  575. break;
  576. CHECK_PIXEL_PTR(byte_run);
  577. for (j = 0; j < byte_run; j++) {
  578. palette_idx1 = bytestream2_get_byte(&g2);
  579. pixels[pixel_ptr++] = palette_idx1;
  580. pixel_countdown--;
  581. if (pixel_countdown < 0)
  582. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  583. pixel_countdown, lines);
  584. }
  585. }
  586. }
  587. /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
  588. * This does not give us any good opportunity to perform word endian conversion
  589. * during decompression. So if it is required (i.e., this is not a LE target, we do
  590. * a second pass over the line here, swapping the bytes.
  591. */
  592. #if HAVE_BIGENDIAN
  593. pixel_ptr = y_ptr;
  594. pixel_countdown = s->avctx->width;
  595. while (pixel_countdown > 0) {
  596. *((signed short*)(&pixels[pixel_ptr])) = AV_RL16(&buf[pixel_ptr]);
  597. pixel_ptr += 2;
  598. }
  599. #endif
  600. y_ptr += s->frame->linesize[0];
  601. }
  602. break;
  603. case FLI_DTA_BRUN:
  604. y_ptr = 0;
  605. for (lines = 0; lines < s->avctx->height; lines++) {
  606. pixel_ptr = y_ptr;
  607. /* disregard the line packets; instead, iterate through all
  608. * pixels on a row */
  609. bytestream2_skip(&g2, 1);
  610. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  611. while (pixel_countdown > 0) {
  612. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  613. break;
  614. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  615. if (byte_run > 0) {
  616. pixel = bytestream2_get_le16(&g2);
  617. CHECK_PIXEL_PTR(2 * byte_run);
  618. for (j = 0; j < byte_run; j++) {
  619. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  620. pixel_ptr += 2;
  621. pixel_countdown--;
  622. if (pixel_countdown < 0)
  623. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  624. pixel_countdown);
  625. }
  626. } else { /* copy pixels if byte_run < 0 */
  627. byte_run = -byte_run;
  628. if (bytestream2_tell(&g2) + 2 * byte_run > stream_ptr_after_chunk)
  629. break;
  630. CHECK_PIXEL_PTR(2 * byte_run);
  631. for (j = 0; j < byte_run; j++) {
  632. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  633. pixel_ptr += 2;
  634. pixel_countdown--;
  635. if (pixel_countdown < 0)
  636. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  637. pixel_countdown);
  638. }
  639. }
  640. }
  641. y_ptr += s->frame->linesize[0];
  642. }
  643. break;
  644. case FLI_COPY:
  645. case FLI_DTA_COPY:
  646. /* copy the chunk (uncompressed frame) */
  647. if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*2) {
  648. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  649. "bigger than image, skipping chunk\n", chunk_size - 6);
  650. bytestream2_skip(&g2, chunk_size - 6);
  651. } else {
  652. for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
  653. y_ptr += s->frame->linesize[0]) {
  654. pixel_countdown = s->avctx->width;
  655. pixel_ptr = 0;
  656. while (pixel_countdown > 0) {
  657. *((signed short*)(&pixels[y_ptr + pixel_ptr])) = bytestream2_get_le16(&g2);
  658. pixel_ptr += 2;
  659. pixel_countdown--;
  660. }
  661. if (s->avctx->width & 1)
  662. bytestream2_skip(&g2, 2);
  663. }
  664. }
  665. break;
  666. case FLI_MINI:
  667. /* some sort of a thumbnail? disregard this chunk... */
  668. bytestream2_skip(&g2, chunk_size - 6);
  669. break;
  670. default:
  671. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  672. break;
  673. }
  674. if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
  675. bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
  676. } else {
  677. av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
  678. break;
  679. }
  680. frame_size -= chunk_size;
  681. num_chunks--;
  682. }
  683. /* by the end of the chunk, the stream ptr should equal the frame
  684. * size (minus 1, possibly); if it doesn't, issue a warning */
  685. if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
  686. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  687. "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
  688. if ((ret = av_frame_ref(data, s->frame)) < 0)
  689. return ret;
  690. *got_frame = 1;
  691. return buf_size;
  692. }
  693. static int flic_decode_frame_24BPP(AVCodecContext *avctx,
  694. void *data, int *got_frame,
  695. const uint8_t *buf, int buf_size)
  696. {
  697. FlicDecodeContext *s = avctx->priv_data;
  698. GetByteContext g2;
  699. int pixel_ptr;
  700. unsigned char palette_idx1;
  701. unsigned int frame_size;
  702. int num_chunks;
  703. unsigned int chunk_size;
  704. int chunk_type;
  705. int i, j, ret;
  706. int lines;
  707. int compressed_lines;
  708. signed short line_packets;
  709. int y_ptr;
  710. int byte_run;
  711. int pixel_skip;
  712. int pixel_countdown;
  713. unsigned char *pixels;
  714. int pixel;
  715. unsigned int pixel_limit;
  716. bytestream2_init(&g2, buf, buf_size);
  717. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  718. return ret;
  719. pixels = s->frame->data[0];
  720. pixel_limit = s->avctx->height * s->frame->linesize[0];
  721. frame_size = bytestream2_get_le32(&g2);
  722. bytestream2_skip(&g2, 2); /* skip the magic number */
  723. num_chunks = bytestream2_get_le16(&g2);
  724. bytestream2_skip(&g2, 8); /* skip padding */
  725. if (frame_size > buf_size)
  726. frame_size = buf_size;
  727. if (frame_size < 16)
  728. return AVERROR_INVALIDDATA;
  729. frame_size -= 16;
  730. /* iterate through the chunks */
  731. while ((frame_size > 0) && (num_chunks > 0) &&
  732. bytestream2_get_bytes_left(&g2) >= 4) {
  733. int stream_ptr_after_chunk;
  734. chunk_size = bytestream2_get_le32(&g2);
  735. if (chunk_size > frame_size) {
  736. av_log(avctx, AV_LOG_WARNING,
  737. "Invalid chunk_size = %u > frame_size = %u\n", chunk_size, frame_size);
  738. chunk_size = frame_size;
  739. }
  740. stream_ptr_after_chunk = bytestream2_tell(&g2) - 4 + chunk_size;
  741. chunk_type = bytestream2_get_le16(&g2);
  742. switch (chunk_type) {
  743. case FLI_256_COLOR:
  744. case FLI_COLOR:
  745. /* For some reason, it seems that non-palettized flics do
  746. * include one of these chunks in their first frame.
  747. * Why I do not know, it seems rather extraneous. */
  748. ff_dlog(avctx,
  749. "Unexpected Palette chunk %d in non-palettized FLC\n",
  750. chunk_type);
  751. bytestream2_skip(&g2, chunk_size - 6);
  752. break;
  753. case FLI_DELTA:
  754. case FLI_DTA_LC:
  755. y_ptr = 0;
  756. compressed_lines = bytestream2_get_le16(&g2);
  757. while (compressed_lines > 0) {
  758. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  759. break;
  760. if (y_ptr > pixel_limit)
  761. return AVERROR_INVALIDDATA;
  762. line_packets = bytestream2_get_le16(&g2);
  763. if (line_packets < 0) {
  764. line_packets = -line_packets;
  765. if (line_packets > s->avctx->height)
  766. return AVERROR_INVALIDDATA;
  767. y_ptr += line_packets * s->frame->linesize[0];
  768. } else {
  769. compressed_lines--;
  770. pixel_ptr = y_ptr;
  771. CHECK_PIXEL_PTR(0);
  772. pixel_countdown = s->avctx->width;
  773. for (i = 0; i < line_packets; i++) {
  774. /* account for the skip bytes */
  775. if (bytestream2_tell(&g2) + 2 > stream_ptr_after_chunk)
  776. break;
  777. pixel_skip = bytestream2_get_byte(&g2);
  778. pixel_ptr += (pixel_skip*3); /* Pixel is 3 bytes wide */
  779. pixel_countdown -= pixel_skip;
  780. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  781. if (byte_run < 0) {
  782. byte_run = -byte_run;
  783. pixel = bytestream2_get_le24(&g2);
  784. CHECK_PIXEL_PTR(3 * byte_run);
  785. for (j = 0; j < byte_run; j++, pixel_countdown -= 1) {
  786. AV_WL24(&pixels[pixel_ptr], pixel);
  787. pixel_ptr += 3;
  788. }
  789. } else {
  790. if (bytestream2_tell(&g2) + 2*byte_run > stream_ptr_after_chunk)
  791. break;
  792. CHECK_PIXEL_PTR(2 * byte_run);
  793. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  794. pixel = bytestream2_get_le24(&g2);
  795. AV_WL24(&pixels[pixel_ptr], pixel);
  796. pixel_ptr += 3;
  797. }
  798. }
  799. }
  800. y_ptr += s->frame->linesize[0];
  801. }
  802. }
  803. break;
  804. case FLI_LC:
  805. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-palettized FLC\n");
  806. bytestream2_skip(&g2, chunk_size - 6);
  807. break;
  808. case FLI_BLACK:
  809. /* set the whole frame to 0x00 which is black for 24 bit mode. */
  810. memset(pixels, 0x00,
  811. s->frame->linesize[0] * s->avctx->height);
  812. break;
  813. case FLI_BRUN:
  814. y_ptr = 0;
  815. for (lines = 0; lines < s->avctx->height; lines++) {
  816. pixel_ptr = y_ptr;
  817. /* disregard the line packets; instead, iterate through all
  818. * pixels on a row */
  819. bytestream2_skip(&g2, 1);
  820. pixel_countdown = (s->avctx->width * 3);
  821. while (pixel_countdown > 0) {
  822. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  823. break;
  824. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  825. if (byte_run > 0) {
  826. palette_idx1 = bytestream2_get_byte(&g2);
  827. CHECK_PIXEL_PTR(byte_run);
  828. for (j = 0; j < byte_run; j++) {
  829. pixels[pixel_ptr++] = palette_idx1;
  830. pixel_countdown--;
  831. if (pixel_countdown < 0)
  832. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
  833. pixel_countdown, lines);
  834. }
  835. } else { /* copy bytes if byte_run < 0 */
  836. byte_run = -byte_run;
  837. if (bytestream2_tell(&g2) + byte_run > stream_ptr_after_chunk)
  838. break;
  839. CHECK_PIXEL_PTR(byte_run);
  840. for (j = 0; j < byte_run; j++) {
  841. palette_idx1 = bytestream2_get_byte(&g2);
  842. pixels[pixel_ptr++] = palette_idx1;
  843. pixel_countdown--;
  844. if (pixel_countdown < 0)
  845. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  846. pixel_countdown, lines);
  847. }
  848. }
  849. }
  850. y_ptr += s->frame->linesize[0];
  851. }
  852. break;
  853. case FLI_DTA_BRUN:
  854. y_ptr = 0;
  855. for (lines = 0; lines < s->avctx->height; lines++) {
  856. pixel_ptr = y_ptr;
  857. /* disregard the line packets; instead, iterate through all
  858. * pixels on a row */
  859. bytestream2_skip(&g2, 1);
  860. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  861. while (pixel_countdown > 0) {
  862. if (bytestream2_tell(&g2) + 1 > stream_ptr_after_chunk)
  863. break;
  864. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  865. if (byte_run > 0) {
  866. pixel = bytestream2_get_le24(&g2);
  867. CHECK_PIXEL_PTR(3 * byte_run);
  868. for (j = 0; j < byte_run; j++) {
  869. AV_WL24(pixels + pixel_ptr, pixel);
  870. pixel_ptr += 3;
  871. pixel_countdown--;
  872. if (pixel_countdown < 0)
  873. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  874. pixel_countdown);
  875. }
  876. } else { /* copy pixels if byte_run < 0 */
  877. byte_run = -byte_run;
  878. if (bytestream2_tell(&g2) + 3 * byte_run > stream_ptr_after_chunk)
  879. break;
  880. CHECK_PIXEL_PTR(3 * byte_run);
  881. for (j = 0; j < byte_run; j++) {
  882. pixel = bytestream2_get_le24(&g2);
  883. AV_WL24(pixels + pixel_ptr, pixel);
  884. pixel_ptr += 3;
  885. pixel_countdown--;
  886. if (pixel_countdown < 0)
  887. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  888. pixel_countdown);
  889. }
  890. }
  891. }
  892. y_ptr += s->frame->linesize[0];
  893. }
  894. break;
  895. case FLI_COPY:
  896. case FLI_DTA_COPY:
  897. /* copy the chunk (uncompressed frame) */
  898. if (chunk_size - 6 > (unsigned int)(FFALIGN(s->avctx->width, 2) * s->avctx->height)*3) {
  899. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  900. "bigger than image, skipping chunk\n", chunk_size - 6);
  901. bytestream2_skip(&g2, chunk_size - 6);
  902. } else {
  903. for (y_ptr = 0; y_ptr < s->frame->linesize[0] * s->avctx->height;
  904. y_ptr += s->frame->linesize[0]) {
  905. pixel_countdown = s->avctx->width;
  906. pixel_ptr = 0;
  907. while (pixel_countdown > 0) {
  908. pixel = bytestream2_get_le24(&g2);
  909. AV_WL24(&pixels[y_ptr + pixel_ptr], pixel);
  910. pixel_ptr += 3;
  911. pixel_countdown--;
  912. }
  913. if (s->avctx->width & 1)
  914. bytestream2_skip(&g2, 3);
  915. }
  916. }
  917. break;
  918. case FLI_MINI:
  919. /* some sort of a thumbnail? disregard this chunk... */
  920. bytestream2_skip(&g2, chunk_size - 6);
  921. break;
  922. default:
  923. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  924. break;
  925. }
  926. if (stream_ptr_after_chunk - bytestream2_tell(&g2) >= 0) {
  927. bytestream2_skip(&g2, stream_ptr_after_chunk - bytestream2_tell(&g2));
  928. } else {
  929. av_log(avctx, AV_LOG_ERROR, "Chunk overread\n");
  930. break;
  931. }
  932. frame_size -= chunk_size;
  933. num_chunks--;
  934. }
  935. /* by the end of the chunk, the stream ptr should equal the frame
  936. * size (minus 1, possibly); if it doesn't, issue a warning */
  937. if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
  938. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  939. "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
  940. if ((ret = av_frame_ref(data, s->frame)) < 0)
  941. return ret;
  942. *got_frame = 1;
  943. return buf_size;
  944. }
  945. static int flic_decode_frame(AVCodecContext *avctx,
  946. void *data, int *got_frame,
  947. AVPacket *avpkt)
  948. {
  949. const uint8_t *buf = avpkt->data;
  950. int buf_size = avpkt->size;
  951. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  952. return flic_decode_frame_8BPP(avctx, data, got_frame,
  953. buf, buf_size);
  954. } else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
  955. (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
  956. return flic_decode_frame_15_16BPP(avctx, data, got_frame,
  957. buf, buf_size);
  958. } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  959. return flic_decode_frame_24BPP(avctx, data, got_frame,
  960. buf, buf_size);
  961. }
  962. /* Should not get here, ever as the pix_fmt is processed */
  963. /* in flic_decode_init and the above if should deal with */
  964. /* the finite set of possibilities allowable by here. */
  965. /* But in case we do, just error out. */
  966. av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
  967. return AVERROR_BUG;
  968. }
  969. static av_cold int flic_decode_end(AVCodecContext *avctx)
  970. {
  971. FlicDecodeContext *s = avctx->priv_data;
  972. av_frame_free(&s->frame);
  973. return 0;
  974. }
  975. AVCodec ff_flic_decoder = {
  976. .name = "flic",
  977. .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
  978. .type = AVMEDIA_TYPE_VIDEO,
  979. .id = AV_CODEC_ID_FLIC,
  980. .priv_data_size = sizeof(FlicDecodeContext),
  981. .init = flic_decode_init,
  982. .close = flic_decode_end,
  983. .decode = flic_decode_frame,
  984. .capabilities = AV_CODEC_CAP_DR1,
  985. };