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.

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