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.

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