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.

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