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.

755 lines
28KB

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