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.

749 lines
28KB

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