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.

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