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.

775 lines
29KB

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