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.

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