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.

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