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.

747 lines
27KB

  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. /**
  23. * @file flic.c
  24. * Autodesk Animator FLI/FLC Video Decoder
  25. * by Mike Melanson (melanson@pcisys.net)
  26. * for more information on the .fli/.flc file format and all of its many
  27. * variations, visit:
  28. * http://www.compuphase.com/flic.htm
  29. *
  30. * This decoder outputs PAL8/RGB555/RGB565 and maybe one day RGB24
  31. * colorspace data, depending on the FLC. To use this decoder, be
  32. * sure that your demuxer sends the FLI file header to the decoder via
  33. * the extradata chunk in AVCodecContext. The chunk should be 128 bytes
  34. * large. The only exception is for FLI files from the game "Magic Carpet",
  35. * in which the header is only 12 bytes.
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include "common.h"
  42. #include "avcodec.h"
  43. #include "bswap.h"
  44. #define FLI_256_COLOR 4
  45. #define FLI_DELTA 7
  46. #define FLI_COLOR 11
  47. #define FLI_LC 12
  48. #define FLI_BLACK 13
  49. #define FLI_BRUN 15
  50. #define FLI_COPY 16
  51. #define FLI_MINI 18
  52. #define FLI_DTA_BRUN 25
  53. #define FLI_DTA_COPY 26
  54. #define FLI_DTA_LC 27
  55. #define FLI_TYPE_CODE (0xAF11)
  56. #define FLC_FLX_TYPE_CODE (0xAF12)
  57. #define FLC_DTA_TYPE_CODE (0xAF44) /* Marks an "Extended FLC" comes from Dave's Targa Animator (DTA) */
  58. #define FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE (0xAF13)
  59. #define CHECK_PIXEL_PTR(n) \
  60. if (pixel_ptr + n > pixel_limit) { \
  61. av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
  62. pixel_ptr + n, pixel_limit); \
  63. return -1; \
  64. } \
  65. typedef struct FlicDecodeContext {
  66. AVCodecContext *avctx;
  67. AVFrame frame;
  68. unsigned int palette[256];
  69. int new_palette;
  70. int fli_type; /* either 0xAF11 or 0xAF12, affects palette resolution */
  71. } FlicDecodeContext;
  72. static int flic_decode_init(AVCodecContext *avctx)
  73. {
  74. FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
  75. unsigned char *fli_header = (unsigned char *)avctx->extradata;
  76. int depth;
  77. s->avctx = avctx;
  78. avctx->has_b_frames = 0;
  79. s->fli_type = LE_16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
  80. depth = LE_16(&fli_header[12]);
  81. if (depth == 0) {
  82. depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
  83. }
  84. if (s->avctx->extradata_size == 12) {
  85. /* special case for magic carpet FLIs */
  86. s->fli_type = FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE;
  87. } else if (s->avctx->extradata_size != 128) {
  88. av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
  89. return -1;
  90. }
  91. if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
  92. depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
  93. }
  94. switch (depth) {
  95. case 8 : avctx->pix_fmt = PIX_FMT_PAL8; break;
  96. case 15 : avctx->pix_fmt = PIX_FMT_RGB555; break;
  97. case 16 : avctx->pix_fmt = PIX_FMT_RGB565; break;
  98. case 24 : avctx->pix_fmt = PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
  99. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
  100. return -1;
  101. break;
  102. default :
  103. av_log(avctx, AV_LOG_ERROR, "Unkown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
  104. return -1;
  105. }
  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. uint8_t *buf, int buf_size)
  113. {
  114. FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
  115. int stream_ptr = 0;
  116. int stream_ptr_after_color_chunk;
  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. signed char byte_run;
  136. int pixel_skip;
  137. int pixel_countdown;
  138. unsigned char *pixels;
  139. int pixel_limit;
  140. s->frame.reference = 1;
  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. frame_size = LE_32(&buf[stream_ptr]);
  149. stream_ptr += 6; /* skip the magic number */
  150. num_chunks = LE_16(&buf[stream_ptr]);
  151. stream_ptr += 10; /* skip padding */
  152. frame_size -= 16;
  153. /* iterate through the chunks */
  154. while ((frame_size > 0) && (num_chunks > 0)) {
  155. chunk_size = LE_32(&buf[stream_ptr]);
  156. stream_ptr += 4;
  157. chunk_type = LE_16(&buf[stream_ptr]);
  158. stream_ptr += 2;
  159. switch (chunk_type) {
  160. case FLI_256_COLOR:
  161. case FLI_COLOR:
  162. stream_ptr_after_color_chunk = stream_ptr + chunk_size - 6;
  163. s->new_palette = 1;
  164. /* check special case: If this file is from the Magic Carpet
  165. * game and uses 6-bit colors even though it reports 256-color
  166. * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
  167. * initialization) */
  168. if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
  169. color_shift = 0;
  170. else
  171. color_shift = 2;
  172. /* set up the palette */
  173. color_packets = LE_16(&buf[stream_ptr]);
  174. stream_ptr += 2;
  175. palette_ptr = 0;
  176. for (i = 0; i < color_packets; i++) {
  177. /* first byte is how many colors to skip */
  178. palette_ptr += buf[stream_ptr++];
  179. /* next byte indicates how many entries to change */
  180. color_changes = buf[stream_ptr++];
  181. /* if there are 0 color changes, there are actually 256 */
  182. if (color_changes == 0)
  183. color_changes = 256;
  184. for (j = 0; j < color_changes; j++) {
  185. /* wrap around, for good measure */
  186. if ((unsigned)palette_ptr >= 256)
  187. palette_ptr = 0;
  188. r = buf[stream_ptr++] << color_shift;
  189. g = buf[stream_ptr++] << color_shift;
  190. b = buf[stream_ptr++] << color_shift;
  191. s->palette[palette_ptr++] = (r << 16) | (g << 8) | b;
  192. }
  193. }
  194. /* color chunks sometimes have weird 16-bit alignment issues;
  195. * therefore, take the hardline approach and set the stream_ptr
  196. * to the value calculated w.r.t. the size specified by the color
  197. * chunk header */
  198. stream_ptr = stream_ptr_after_color_chunk;
  199. break;
  200. case FLI_DELTA:
  201. y_ptr = 0;
  202. compressed_lines = LE_16(&buf[stream_ptr]);
  203. stream_ptr += 2;
  204. while (compressed_lines > 0) {
  205. line_packets = LE_16(&buf[stream_ptr]);
  206. stream_ptr += 2;
  207. if (line_packets < 0) {
  208. line_packets = -line_packets;
  209. y_ptr += line_packets * s->frame.linesize[0];
  210. } else {
  211. compressed_lines--;
  212. pixel_ptr = y_ptr;
  213. pixel_countdown = s->avctx->width;
  214. for (i = 0; i < line_packets; i++) {
  215. /* account for the skip bytes */
  216. pixel_skip = buf[stream_ptr++];
  217. pixel_ptr += pixel_skip;
  218. pixel_countdown -= pixel_skip;
  219. byte_run = buf[stream_ptr++];
  220. if (byte_run < 0) {
  221. byte_run = -byte_run;
  222. palette_idx1 = buf[stream_ptr++];
  223. palette_idx2 = buf[stream_ptr++];
  224. CHECK_PIXEL_PTR(byte_run);
  225. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  226. pixels[pixel_ptr++] = palette_idx1;
  227. pixels[pixel_ptr++] = palette_idx2;
  228. }
  229. } else {
  230. CHECK_PIXEL_PTR(byte_run * 2);
  231. for (j = 0; j < byte_run * 2; j++, pixel_countdown--) {
  232. palette_idx1 = buf[stream_ptr++];
  233. pixels[pixel_ptr++] = palette_idx1;
  234. }
  235. }
  236. }
  237. y_ptr += s->frame.linesize[0];
  238. }
  239. }
  240. break;
  241. case FLI_LC:
  242. /* line compressed */
  243. starting_line = LE_16(&buf[stream_ptr]);
  244. stream_ptr += 2;
  245. y_ptr = 0;
  246. y_ptr += starting_line * s->frame.linesize[0];
  247. compressed_lines = LE_16(&buf[stream_ptr]);
  248. stream_ptr += 2;
  249. while (compressed_lines > 0) {
  250. pixel_ptr = y_ptr;
  251. pixel_countdown = s->avctx->width;
  252. line_packets = buf[stream_ptr++];
  253. if (line_packets > 0) {
  254. for (i = 0; i < line_packets; i++) {
  255. /* account for the skip bytes */
  256. pixel_skip = buf[stream_ptr++];
  257. pixel_ptr += pixel_skip;
  258. pixel_countdown -= pixel_skip;
  259. byte_run = buf[stream_ptr++];
  260. if (byte_run > 0) {
  261. CHECK_PIXEL_PTR(byte_run);
  262. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  263. palette_idx1 = buf[stream_ptr++];
  264. pixels[pixel_ptr++] = palette_idx1;
  265. }
  266. } else {
  267. byte_run = -byte_run;
  268. palette_idx1 = buf[stream_ptr++];
  269. CHECK_PIXEL_PTR(byte_run);
  270. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  271. pixels[pixel_ptr++] = palette_idx1;
  272. }
  273. }
  274. }
  275. }
  276. y_ptr += s->frame.linesize[0];
  277. compressed_lines--;
  278. }
  279. break;
  280. case FLI_BLACK:
  281. /* set the whole frame to color 0 (which is usually black) */
  282. memset(pixels, 0,
  283. s->frame.linesize[0] * s->avctx->height);
  284. break;
  285. case FLI_BRUN:
  286. /* Byte run compression: This chunk type only occurs in the first
  287. * FLI frame and it will update the entire frame. */
  288. y_ptr = 0;
  289. for (lines = 0; lines < s->avctx->height; lines++) {
  290. pixel_ptr = y_ptr;
  291. /* disregard the line packets; instead, iterate through all
  292. * pixels on a row */
  293. stream_ptr++;
  294. pixel_countdown = s->avctx->width;
  295. while (pixel_countdown > 0) {
  296. byte_run = buf[stream_ptr++];
  297. if (byte_run > 0) {
  298. palette_idx1 = buf[stream_ptr++];
  299. CHECK_PIXEL_PTR(byte_run);
  300. for (j = 0; j < byte_run; j++) {
  301. pixels[pixel_ptr++] = palette_idx1;
  302. pixel_countdown--;
  303. if (pixel_countdown < 0)
  304. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  305. pixel_countdown);
  306. }
  307. } else { /* copy bytes if byte_run < 0 */
  308. byte_run = -byte_run;
  309. CHECK_PIXEL_PTR(byte_run);
  310. for (j = 0; j < byte_run; j++) {
  311. palette_idx1 = buf[stream_ptr++];
  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)\n",
  316. pixel_countdown);
  317. }
  318. }
  319. }
  320. y_ptr += s->frame.linesize[0];
  321. }
  322. break;
  323. case FLI_COPY:
  324. /* copy the chunk (uncompressed frame) */
  325. if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
  326. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  327. "bigger than image, skipping chunk\n", chunk_size - 6);
  328. stream_ptr += chunk_size - 6;
  329. } else {
  330. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  331. y_ptr += s->frame.linesize[0]) {
  332. memcpy(&pixels[y_ptr], &buf[stream_ptr],
  333. s->avctx->width);
  334. stream_ptr += s->avctx->width;
  335. }
  336. }
  337. break;
  338. case FLI_MINI:
  339. /* some sort of a thumbnail? disregard this chunk... */
  340. stream_ptr += chunk_size - 6;
  341. break;
  342. default:
  343. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  344. break;
  345. }
  346. frame_size -= chunk_size;
  347. num_chunks--;
  348. }
  349. /* by the end of the chunk, the stream ptr should equal the frame
  350. * size (minus 1, possibly); if it doesn't, issue a warning */
  351. if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
  352. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  353. "and final chunk ptr = %d\n", buf_size, stream_ptr);
  354. /* make the palette available on the way out */
  355. // if (s->new_palette) {
  356. if (1) {
  357. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  358. s->frame.palette_has_changed = 1;
  359. s->new_palette = 0;
  360. }
  361. *data_size=sizeof(AVFrame);
  362. *(AVFrame*)data = s->frame;
  363. return buf_size;
  364. }
  365. static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
  366. void *data, int *data_size,
  367. uint8_t *buf, int buf_size)
  368. {
  369. /* Note, the only difference between the 15Bpp and 16Bpp */
  370. /* Format is the pixel format, the packets are processed the same. */
  371. FlicDecodeContext *s = (FlicDecodeContext *)avctx->priv_data;
  372. int stream_ptr = 0;
  373. int pixel_ptr;
  374. unsigned char palette_idx1;
  375. unsigned int frame_size;
  376. int num_chunks;
  377. unsigned int chunk_size;
  378. int chunk_type;
  379. int i, j;
  380. int lines;
  381. int compressed_lines;
  382. signed short line_packets;
  383. int y_ptr;
  384. signed char byte_run;
  385. int pixel_skip;
  386. int pixel_countdown;
  387. unsigned char *pixels;
  388. int pixel;
  389. int pixel_limit;
  390. s->frame.reference = 1;
  391. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  392. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  393. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  394. return -1;
  395. }
  396. pixels = s->frame.data[0];
  397. pixel_limit = s->avctx->height * s->frame.linesize[0];
  398. frame_size = LE_32(&buf[stream_ptr]);
  399. stream_ptr += 6; /* skip the magic number */
  400. num_chunks = LE_16(&buf[stream_ptr]);
  401. stream_ptr += 10; /* skip padding */
  402. frame_size -= 16;
  403. /* iterate through the chunks */
  404. while ((frame_size > 0) && (num_chunks > 0)) {
  405. chunk_size = LE_32(&buf[stream_ptr]);
  406. stream_ptr += 4;
  407. chunk_type = LE_16(&buf[stream_ptr]);
  408. stream_ptr += 2;
  409. switch (chunk_type) {
  410. case FLI_256_COLOR:
  411. case FLI_COLOR:
  412. /* For some reason, it seems that non-paletised flics do include one of these */
  413. /* chunks in their first frame. Why i do not know, it seems rather extraneous */
  414. /* av_log(avctx, AV_LOG_ERROR, "Unexpected Palette chunk %d in non-paletised FLC\n",chunk_type);*/
  415. stream_ptr = stream_ptr + chunk_size - 6;
  416. break;
  417. case FLI_DELTA:
  418. case FLI_DTA_LC:
  419. y_ptr = 0;
  420. compressed_lines = LE_16(&buf[stream_ptr]);
  421. stream_ptr += 2;
  422. while (compressed_lines > 0) {
  423. line_packets = LE_16(&buf[stream_ptr]);
  424. stream_ptr += 2;
  425. if (line_packets < 0) {
  426. line_packets = -line_packets;
  427. y_ptr += line_packets * s->frame.linesize[0];
  428. } else {
  429. compressed_lines--;
  430. pixel_ptr = y_ptr;
  431. pixel_countdown = s->avctx->width;
  432. for (i = 0; i < line_packets; i++) {
  433. /* account for the skip bytes */
  434. pixel_skip = buf[stream_ptr++];
  435. pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
  436. pixel_countdown -= pixel_skip;
  437. byte_run = buf[stream_ptr++];
  438. if (byte_run < 0) {
  439. byte_run = -byte_run;
  440. pixel = LE_16(&buf[stream_ptr]);
  441. stream_ptr += 2;
  442. CHECK_PIXEL_PTR(byte_run);
  443. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  444. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  445. pixel_ptr += 2;
  446. }
  447. } else {
  448. CHECK_PIXEL_PTR(byte_run);
  449. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  450. *((signed short*)(&pixels[pixel_ptr])) = LE_16(&buf[stream_ptr]);
  451. stream_ptr += 2;
  452. pixel_ptr += 2;
  453. }
  454. }
  455. }
  456. y_ptr += s->frame.linesize[0];
  457. }
  458. }
  459. break;
  460. case FLI_LC:
  461. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
  462. stream_ptr = stream_ptr + chunk_size - 6;
  463. break;
  464. case FLI_BLACK:
  465. /* set the whole frame to 0x0000 which is balck in both 15Bpp and 16Bpp modes. */
  466. memset(pixels, 0x0000,
  467. s->frame.linesize[0] * s->avctx->height * 2);
  468. break;
  469. case FLI_BRUN:
  470. y_ptr = 0;
  471. for (lines = 0; lines < s->avctx->height; lines++) {
  472. pixel_ptr = y_ptr;
  473. /* disregard the line packets; instead, iterate through all
  474. * pixels on a row */
  475. stream_ptr++;
  476. pixel_countdown = (s->avctx->width * 2);
  477. while (pixel_countdown > 0) {
  478. byte_run = buf[stream_ptr++];
  479. if (byte_run > 0) {
  480. palette_idx1 = buf[stream_ptr++];
  481. CHECK_PIXEL_PTR(byte_run);
  482. for (j = 0; j < byte_run; j++) {
  483. pixels[pixel_ptr++] = palette_idx1;
  484. pixel_countdown--;
  485. if (pixel_countdown < 0)
  486. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  487. pixel_countdown);
  488. }
  489. } else { /* copy bytes if byte_run < 0 */
  490. byte_run = -byte_run;
  491. CHECK_PIXEL_PTR(byte_run);
  492. for (j = 0; j < byte_run; j++) {
  493. palette_idx1 = buf[stream_ptr++];
  494. pixels[pixel_ptr++] = palette_idx1;
  495. pixel_countdown--;
  496. if (pixel_countdown < 0)
  497. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  498. pixel_countdown);
  499. }
  500. }
  501. }
  502. /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
  503. * This doesnt give us any good oportunity to perform word endian conversion
  504. * during decompression. So if its requried (ie, this isnt a LE target, we do
  505. * a second pass over the line here, swapping the bytes.
  506. */
  507. pixel = 0xFF00;
  508. if (0xFF00 != LE_16(&pixel)) /* Check if its not an LE Target */
  509. {
  510. pixel_ptr = y_ptr;
  511. pixel_countdown = s->avctx->width;
  512. while (pixel_countdown > 0) {
  513. *((signed short*)(&pixels[pixel_ptr])) = LE_16(&buf[pixel_ptr]);
  514. pixel_ptr += 2;
  515. }
  516. }
  517. y_ptr += s->frame.linesize[0];
  518. }
  519. break;
  520. case FLI_DTA_BRUN:
  521. y_ptr = 0;
  522. for (lines = 0; lines < s->avctx->height; lines++) {
  523. pixel_ptr = y_ptr;
  524. /* disregard the line packets; instead, iterate through all
  525. * pixels on a row */
  526. stream_ptr++;
  527. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  528. while (pixel_countdown > 0) {
  529. byte_run = buf[stream_ptr++];
  530. if (byte_run > 0) {
  531. pixel = LE_16(&buf[stream_ptr]);
  532. stream_ptr += 2;
  533. CHECK_PIXEL_PTR(byte_run);
  534. for (j = 0; j < byte_run; j++) {
  535. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  536. pixel_ptr += 2;
  537. pixel_countdown--;
  538. if (pixel_countdown < 0)
  539. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  540. pixel_countdown);
  541. }
  542. } else { /* copy pixels if byte_run < 0 */
  543. byte_run = -byte_run;
  544. CHECK_PIXEL_PTR(byte_run);
  545. for (j = 0; j < byte_run; j++) {
  546. *((signed short*)(&pixels[pixel_ptr])) = LE_16(&buf[stream_ptr]);
  547. stream_ptr += 2;
  548. pixel_ptr += 2;
  549. pixel_countdown--;
  550. if (pixel_countdown < 0)
  551. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  552. pixel_countdown);
  553. }
  554. }
  555. }
  556. y_ptr += s->frame.linesize[0];
  557. }
  558. break;
  559. case FLI_COPY:
  560. case FLI_DTA_COPY:
  561. /* copy the chunk (uncompressed frame) */
  562. if (chunk_size - 6 > (unsigned int)(s->avctx->width * s->avctx->height)*2) {
  563. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  564. "bigger than image, skipping chunk\n", chunk_size - 6);
  565. stream_ptr += chunk_size - 6;
  566. } else {
  567. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  568. y_ptr += s->frame.linesize[0]) {
  569. pixel_countdown = s->avctx->width;
  570. pixel_ptr = 0;
  571. while (pixel_countdown > 0) {
  572. *((signed short*)(&pixels[y_ptr + pixel_ptr])) = LE_16(&buf[stream_ptr+pixel_ptr]);
  573. pixel_ptr += 2;
  574. pixel_countdown--;
  575. }
  576. stream_ptr += s->avctx->width*2;
  577. }
  578. }
  579. break;
  580. case FLI_MINI:
  581. /* some sort of a thumbnail? disregard this chunk... */
  582. stream_ptr += chunk_size - 6;
  583. break;
  584. default:
  585. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  586. break;
  587. }
  588. frame_size -= chunk_size;
  589. num_chunks--;
  590. }
  591. /* by the end of the chunk, the stream ptr should equal the frame
  592. * size (minus 1, possibly); if it doesn't, issue a warning */
  593. if ((stream_ptr != buf_size) && (stream_ptr != buf_size - 1))
  594. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  595. "and final chunk ptr = %d\n", buf_size, stream_ptr);
  596. *data_size=sizeof(AVFrame);
  597. *(AVFrame*)data = s->frame;
  598. return buf_size;
  599. }
  600. static int flic_decode_frame_24BPP(AVCodecContext *avctx,
  601. void *data, int *data_size,
  602. uint8_t *buf, int buf_size)
  603. {
  604. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
  605. return -1;
  606. }
  607. static int flic_decode_frame(AVCodecContext *avctx,
  608. void *data, int *data_size,
  609. uint8_t *buf, int buf_size)
  610. {
  611. if (avctx->pix_fmt == PIX_FMT_PAL8) {
  612. return flic_decode_frame_8BPP(avctx, data, data_size,
  613. buf, buf_size);
  614. }
  615. else if ((avctx->pix_fmt == PIX_FMT_RGB555) ||
  616. (avctx->pix_fmt == PIX_FMT_RGB565)) {
  617. return flic_decode_frame_15_16BPP(avctx, data, data_size,
  618. buf, buf_size);
  619. }
  620. else if (avctx->pix_fmt == PIX_FMT_BGR24) {
  621. return flic_decode_frame_24BPP(avctx, data, data_size,
  622. buf, buf_size);
  623. }
  624. /* Shouldnt get here, ever as the pix_fmt is processed */
  625. /* in flic_decode_init and the above if should deal with */
  626. /* the finite set of possibilites allowable by here. */
  627. /* but in case we do, just error out. */
  628. av_log(avctx, AV_LOG_ERROR, "Unknown Format of FLC. My Science cant explain how this happened\n");
  629. return -1;
  630. }
  631. static int flic_decode_end(AVCodecContext *avctx)
  632. {
  633. FlicDecodeContext *s = avctx->priv_data;
  634. if (s->frame.data[0])
  635. avctx->release_buffer(avctx, &s->frame);
  636. return 0;
  637. }
  638. AVCodec flic_decoder = {
  639. "flic",
  640. CODEC_TYPE_VIDEO,
  641. CODEC_ID_FLIC,
  642. sizeof(FlicDecodeContext),
  643. flic_decode_init,
  644. NULL,
  645. flic_decode_end,
  646. flic_decode_frame,
  647. CODEC_CAP_DR1,
  648. NULL,
  649. NULL,
  650. NULL,
  651. NULL
  652. };