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.

767 lines
29KB

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