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.

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