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.

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