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.

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