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
29KB

  1. /*
  2. * FLI/FLC Animation Video Decoder
  3. * Copyright (C) 2003, 2004 the ffmpeg project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "bytestream.h"
  42. #include "mathops.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 AVERROR_INVALIDDATA; \
  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 av_cold 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. if (avctx->extradata_size != 12 &&
  77. avctx->extradata_size != 128) {
  78. av_log(avctx, AV_LOG_ERROR, "Expected extradata of 12 or 128 bytes\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. s->avctx = avctx;
  82. s->fli_type = AV_RL16(&fli_header[4]); /* Might be overridden if a Magic Carpet FLC */
  83. depth = 0;
  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. depth = 8;
  88. } else {
  89. depth = AV_RL16(&fli_header[12]);
  90. }
  91. if (depth == 0) {
  92. depth = 8; /* Some FLC generators set depth to zero, when they mean 8Bpp. Fix up here */
  93. }
  94. if ((s->fli_type == FLC_FLX_TYPE_CODE) && (depth == 16)) {
  95. depth = 15; /* Original Autodesk FLX's say the depth is 16Bpp when it is really 15Bpp */
  96. }
  97. switch (depth) {
  98. case 8 : avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
  99. case 15 : avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
  100. case 16 : avctx->pix_fmt = AV_PIX_FMT_RGB565; break;
  101. case 24 : avctx->pix_fmt = AV_PIX_FMT_BGR24; /* Supposedly BGR, but havent any files to test with */
  102. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC/FLX is unsupported due to no test files.\n");
  103. return AVERROR_PATCHWELCOME;
  104. default :
  105. av_log(avctx, AV_LOG_ERROR, "Unknown FLC/FLX depth of %d Bpp is unsupported.\n",depth);
  106. return AVERROR_INVALIDDATA;
  107. }
  108. s->frame.data[0] = NULL;
  109. s->new_palette = 0;
  110. return 0;
  111. }
  112. static int flic_decode_frame_8BPP(AVCodecContext *avctx,
  113. void *data, int *got_frame,
  114. const uint8_t *buf, int buf_size)
  115. {
  116. FlicDecodeContext *s = avctx->priv_data;
  117. GetByteContext g2;
  118. int stream_ptr_after_color_chunk;
  119. int pixel_ptr;
  120. int palette_ptr;
  121. unsigned char palette_idx1;
  122. unsigned char palette_idx2;
  123. unsigned int frame_size;
  124. int num_chunks;
  125. unsigned int chunk_size;
  126. int chunk_type;
  127. int i, j, ret;
  128. int color_packets;
  129. int color_changes;
  130. int color_shift;
  131. unsigned char r, g, b;
  132. int lines;
  133. int compressed_lines;
  134. int starting_line;
  135. signed short line_packets;
  136. int y_ptr;
  137. int byte_run;
  138. int pixel_skip;
  139. int pixel_countdown;
  140. unsigned char *pixels;
  141. unsigned int pixel_limit;
  142. bytestream2_init(&g2, buf, buf_size);
  143. s->frame.reference = 1;
  144. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  145. if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  146. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  147. return ret;
  148. }
  149. pixels = s->frame.data[0];
  150. pixel_limit = s->avctx->height * s->frame.linesize[0];
  151. frame_size = bytestream2_get_le32(&g2);
  152. bytestream2_skip(&g2, 2); /* skip the magic number */
  153. num_chunks = bytestream2_get_le16(&g2);
  154. bytestream2_skip(&g2, 8); /* skip padding */
  155. frame_size -= 16;
  156. /* iterate through the chunks */
  157. while ((frame_size > 0) && (num_chunks > 0)) {
  158. chunk_size = bytestream2_get_le32(&g2);
  159. chunk_type = bytestream2_get_le16(&g2);
  160. switch (chunk_type) {
  161. case FLI_256_COLOR:
  162. case FLI_COLOR:
  163. stream_ptr_after_color_chunk = bytestream2_tell(&g2) + chunk_size - 6;
  164. /* check special case: If this file is from the Magic Carpet
  165. * game and uses 6-bit colors even though it reports 256-color
  166. * chunks in a 0xAF12-type file (fli_type is set to 0xAF13 during
  167. * initialization) */
  168. if ((chunk_type == FLI_256_COLOR) && (s->fli_type != FLC_MAGIC_CARPET_SYNTHETIC_TYPE_CODE))
  169. color_shift = 0;
  170. else
  171. color_shift = 2;
  172. /* set up the palette */
  173. color_packets = bytestream2_get_le16(&g2);
  174. palette_ptr = 0;
  175. for (i = 0; i < color_packets; i++) {
  176. /* first byte is how many colors to skip */
  177. palette_ptr += bytestream2_get_byte(&g2);
  178. /* next byte indicates how many entries to change */
  179. color_changes = bytestream2_get_byte(&g2);
  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 = bytestream2_get_byte(&g2) << color_shift;
  189. g = bytestream2_get_byte(&g2) << color_shift;
  190. b = bytestream2_get_byte(&g2) << 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 skip
  199. * to the value calculated w.r.t. the size specified by the color
  200. * chunk header */
  201. if (stream_ptr_after_color_chunk - bytestream2_tell(&g2) > 0)
  202. bytestream2_skip(&g2, stream_ptr_after_color_chunk - bytestream2_tell(&g2));
  203. break;
  204. case FLI_DELTA:
  205. y_ptr = 0;
  206. compressed_lines = bytestream2_get_le16(&g2);
  207. while (compressed_lines > 0) {
  208. line_packets = bytestream2_get_le16(&g2);
  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 = bytestream2_get_byte(&g2);
  228. pixel_ptr += pixel_skip;
  229. pixel_countdown -= pixel_skip;
  230. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  231. if (byte_run < 0) {
  232. byte_run = -byte_run;
  233. palette_idx1 = bytestream2_get_byte(&g2);
  234. palette_idx2 = bytestream2_get_byte(&g2);
  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. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  244. }
  245. }
  246. }
  247. y_ptr += s->frame.linesize[0];
  248. }
  249. }
  250. break;
  251. case FLI_LC:
  252. /* line compressed */
  253. starting_line = bytestream2_get_le16(&g2);
  254. y_ptr = 0;
  255. y_ptr += starting_line * s->frame.linesize[0];
  256. compressed_lines = bytestream2_get_le16(&g2);
  257. while (compressed_lines > 0) {
  258. pixel_ptr = y_ptr;
  259. CHECK_PIXEL_PTR(0);
  260. pixel_countdown = s->avctx->width;
  261. line_packets = bytestream2_get_byte(&g2);
  262. if (line_packets > 0) {
  263. for (i = 0; i < line_packets; i++) {
  264. /* account for the skip bytes */
  265. pixel_skip = bytestream2_get_byte(&g2);
  266. pixel_ptr += pixel_skip;
  267. pixel_countdown -= pixel_skip;
  268. byte_run = sign_extend(bytestream2_get_byte(&g2),8);
  269. if (byte_run > 0) {
  270. CHECK_PIXEL_PTR(byte_run);
  271. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  272. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  273. }
  274. } else if (byte_run < 0) {
  275. byte_run = -byte_run;
  276. palette_idx1 = bytestream2_get_byte(&g2);
  277. CHECK_PIXEL_PTR(byte_run);
  278. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  279. pixels[pixel_ptr++] = palette_idx1;
  280. }
  281. }
  282. }
  283. }
  284. y_ptr += s->frame.linesize[0];
  285. compressed_lines--;
  286. }
  287. break;
  288. case FLI_BLACK:
  289. /* set the whole frame to color 0 (which is usually black) */
  290. memset(pixels, 0,
  291. s->frame.linesize[0] * s->avctx->height);
  292. break;
  293. case FLI_BRUN:
  294. /* Byte run compression: This chunk type only occurs in the first
  295. * FLI frame and it will update the entire frame. */
  296. y_ptr = 0;
  297. for (lines = 0; lines < s->avctx->height; lines++) {
  298. pixel_ptr = y_ptr;
  299. /* disregard the line packets; instead, iterate through all
  300. * pixels on a row */
  301. bytestream2_skip(&g2, 1);
  302. pixel_countdown = s->avctx->width;
  303. while (pixel_countdown > 0) {
  304. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  305. if (!byte_run) {
  306. av_log(avctx, AV_LOG_ERROR, "Invalid byte run value.\n");
  307. return AVERROR_INVALIDDATA;
  308. }
  309. if (byte_run > 0) {
  310. palette_idx1 = bytestream2_get_byte(&g2);
  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. pixels[pixel_ptr++] = bytestream2_get_byte(&g2);
  324. pixel_countdown--;
  325. if (pixel_countdown < 0)
  326. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  327. pixel_countdown, lines);
  328. }
  329. }
  330. }
  331. y_ptr += s->frame.linesize[0];
  332. }
  333. break;
  334. case FLI_COPY:
  335. /* copy the chunk (uncompressed frame) */
  336. if (chunk_size - 6 > s->avctx->width * s->avctx->height) {
  337. av_log(avctx, AV_LOG_ERROR, "In chunk FLI_COPY : source data (%d bytes) " \
  338. "bigger than image, skipping chunk\n", chunk_size - 6);
  339. bytestream2_skip(&g2, chunk_size - 6);
  340. } else {
  341. for (y_ptr = 0; y_ptr < s->frame.linesize[0] * s->avctx->height;
  342. y_ptr += s->frame.linesize[0]) {
  343. bytestream2_get_buffer(&g2, &pixels[y_ptr],
  344. s->avctx->width);
  345. }
  346. }
  347. break;
  348. case FLI_MINI:
  349. /* some sort of a thumbnail? disregard this chunk... */
  350. bytestream2_skip(&g2, chunk_size - 6);
  351. break;
  352. default:
  353. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  354. break;
  355. }
  356. frame_size -= chunk_size;
  357. num_chunks--;
  358. }
  359. /* by the end of the chunk, the stream ptr should equal the frame
  360. * size (minus 1, possibly); if it doesn't, issue a warning */
  361. if ((bytestream2_get_bytes_left(&g2) != 0) &&
  362. (bytestream2_get_bytes_left(&g2) != 1))
  363. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  364. "and final chunk ptr = %d\n", buf_size,
  365. buf_size - bytestream2_get_bytes_left(&g2));
  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. *got_frame = 1;
  373. *(AVFrame*)data = s->frame;
  374. return buf_size;
  375. }
  376. static int flic_decode_frame_15_16BPP(AVCodecContext *avctx,
  377. void *data, int *got_frame,
  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. GetByteContext g2;
  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, ret;
  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. bytestream2_init(&g2, buf, buf_size);
  402. s->frame.reference = 1;
  403. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  404. if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  405. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  406. return ret;
  407. }
  408. pixels = s->frame.data[0];
  409. pixel_limit = s->avctx->height * s->frame.linesize[0];
  410. frame_size = bytestream2_get_le32(&g2);
  411. bytestream2_skip(&g2, 2); /* skip the magic number */
  412. num_chunks = bytestream2_get_le16(&g2);
  413. bytestream2_skip(&g2, 8); /* skip padding */
  414. frame_size -= 16;
  415. /* iterate through the chunks */
  416. while ((frame_size > 0) && (num_chunks > 0)) {
  417. chunk_size = bytestream2_get_le32(&g2);
  418. chunk_type = bytestream2_get_le16(&g2);
  419. switch (chunk_type) {
  420. case FLI_256_COLOR:
  421. case FLI_COLOR:
  422. /* For some reason, it seems that non-palettized flics do
  423. * include one of these chunks in their first frame.
  424. * Why I do not know, it seems rather extraneous. */
  425. av_dlog(avctx,
  426. "Unexpected Palette chunk %d in non-palettized FLC\n",
  427. chunk_type);
  428. bytestream2_skip(&g2, chunk_size - 6);
  429. break;
  430. case FLI_DELTA:
  431. case FLI_DTA_LC:
  432. y_ptr = 0;
  433. compressed_lines = bytestream2_get_le16(&g2);
  434. while (compressed_lines > 0) {
  435. line_packets = bytestream2_get_le16(&g2);
  436. if (line_packets < 0) {
  437. line_packets = -line_packets;
  438. y_ptr += line_packets * s->frame.linesize[0];
  439. } else {
  440. compressed_lines--;
  441. pixel_ptr = y_ptr;
  442. CHECK_PIXEL_PTR(0);
  443. pixel_countdown = s->avctx->width;
  444. for (i = 0; i < line_packets; i++) {
  445. /* account for the skip bytes */
  446. pixel_skip = bytestream2_get_byte(&g2);
  447. pixel_ptr += (pixel_skip*2); /* Pixel is 2 bytes wide */
  448. pixel_countdown -= pixel_skip;
  449. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  450. if (byte_run < 0) {
  451. byte_run = -byte_run;
  452. pixel = bytestream2_get_le16(&g2);
  453. CHECK_PIXEL_PTR(2 * byte_run);
  454. for (j = 0; j < byte_run; j++, pixel_countdown -= 2) {
  455. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  456. pixel_ptr += 2;
  457. }
  458. } else {
  459. CHECK_PIXEL_PTR(2 * byte_run);
  460. for (j = 0; j < byte_run; j++, pixel_countdown--) {
  461. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  462. pixel_ptr += 2;
  463. }
  464. }
  465. }
  466. y_ptr += s->frame.linesize[0];
  467. }
  468. }
  469. break;
  470. case FLI_LC:
  471. av_log(avctx, AV_LOG_ERROR, "Unexpected FLI_LC chunk in non-paletised FLC\n");
  472. bytestream2_skip(&g2, chunk_size - 6);
  473. break;
  474. case FLI_BLACK:
  475. /* set the whole frame to 0x0000 which is black in both 15Bpp and 16Bpp modes. */
  476. memset(pixels, 0x0000,
  477. s->frame.linesize[0] * s->avctx->height);
  478. break;
  479. case FLI_BRUN:
  480. y_ptr = 0;
  481. for (lines = 0; lines < s->avctx->height; lines++) {
  482. pixel_ptr = y_ptr;
  483. /* disregard the line packets; instead, iterate through all
  484. * pixels on a row */
  485. bytestream2_skip(&g2, 1);
  486. pixel_countdown = (s->avctx->width * 2);
  487. while (pixel_countdown > 0) {
  488. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  489. if (byte_run > 0) {
  490. palette_idx1 = bytestream2_get_byte(&g2);
  491. CHECK_PIXEL_PTR(byte_run);
  492. for (j = 0; j < byte_run; j++) {
  493. pixels[pixel_ptr++] = palette_idx1;
  494. pixel_countdown--;
  495. if (pixel_countdown < 0)
  496. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) (linea%d)\n",
  497. pixel_countdown, lines);
  498. }
  499. } else { /* copy bytes if byte_run < 0 */
  500. byte_run = -byte_run;
  501. CHECK_PIXEL_PTR(byte_run);
  502. for (j = 0; j < byte_run; j++) {
  503. palette_idx1 = bytestream2_get_byte(&g2);
  504. pixels[pixel_ptr++] = palette_idx1;
  505. pixel_countdown--;
  506. if (pixel_countdown < 0)
  507. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d) at line %d\n",
  508. pixel_countdown, lines);
  509. }
  510. }
  511. }
  512. /* Now FLX is strange, in that it is "byte" as opposed to "pixel" run length compressed.
  513. * This does not give us any good opportunity to perform word endian conversion
  514. * during decompression. So if it is required (i.e., this is not a LE target, we do
  515. * a second pass over the line here, swapping the bytes.
  516. */
  517. #if HAVE_BIGENDIAN
  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. #endif
  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. bytestream2_skip(&g2, 1);
  535. pixel_countdown = s->avctx->width; /* Width is in pixels, not bytes */
  536. while (pixel_countdown > 0) {
  537. byte_run = sign_extend(bytestream2_get_byte(&g2), 8);
  538. if (byte_run > 0) {
  539. pixel = bytestream2_get_le16(&g2);
  540. CHECK_PIXEL_PTR(2 * byte_run);
  541. for (j = 0; j < byte_run; j++) {
  542. *((signed short*)(&pixels[pixel_ptr])) = pixel;
  543. pixel_ptr += 2;
  544. pixel_countdown--;
  545. if (pixel_countdown < 0)
  546. av_log(avctx, AV_LOG_ERROR, "pixel_countdown < 0 (%d)\n",
  547. pixel_countdown);
  548. }
  549. } else { /* copy pixels if byte_run < 0 */
  550. byte_run = -byte_run;
  551. CHECK_PIXEL_PTR(2 * byte_run);
  552. for (j = 0; j < byte_run; j++) {
  553. *((signed short*)(&pixels[pixel_ptr])) = bytestream2_get_le16(&g2);
  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. bytestream2_skip(&g2, 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])) = bytestream2_get_le16(&g2);
  579. pixel_ptr += 2;
  580. pixel_countdown--;
  581. }
  582. }
  583. }
  584. break;
  585. case FLI_MINI:
  586. /* some sort of a thumbnail? disregard this chunk... */
  587. bytestream2_skip(&g2, chunk_size - 6);
  588. break;
  589. default:
  590. av_log(avctx, AV_LOG_ERROR, "Unrecognized chunk type: %d\n", chunk_type);
  591. break;
  592. }
  593. frame_size -= chunk_size;
  594. num_chunks--;
  595. }
  596. /* by the end of the chunk, the stream ptr should equal the frame
  597. * size (minus 1, possibly); if it doesn't, issue a warning */
  598. if ((bytestream2_get_bytes_left(&g2) != 0) && (bytestream2_get_bytes_left(&g2) != 1))
  599. av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
  600. "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
  601. *got_frame = 1;
  602. *(AVFrame*)data = s->frame;
  603. return buf_size;
  604. }
  605. static int flic_decode_frame_24BPP(AVCodecContext *avctx,
  606. void *data, int *got_frame,
  607. const uint8_t *buf, int buf_size)
  608. {
  609. av_log(avctx, AV_LOG_ERROR, "24Bpp FLC Unsupported due to lack of test files.\n");
  610. return AVERROR_PATCHWELCOME;
  611. }
  612. static int flic_decode_frame(AVCodecContext *avctx,
  613. void *data, int *got_frame,
  614. AVPacket *avpkt)
  615. {
  616. const uint8_t *buf = avpkt->data;
  617. int buf_size = avpkt->size;
  618. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  619. return flic_decode_frame_8BPP(avctx, data, got_frame,
  620. buf, buf_size);
  621. }
  622. else if ((avctx->pix_fmt == AV_PIX_FMT_RGB555) ||
  623. (avctx->pix_fmt == AV_PIX_FMT_RGB565)) {
  624. return flic_decode_frame_15_16BPP(avctx, data, got_frame,
  625. buf, buf_size);
  626. }
  627. else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  628. return flic_decode_frame_24BPP(avctx, data, got_frame,
  629. buf, buf_size);
  630. }
  631. /* Should not get here, ever as the pix_fmt is processed */
  632. /* in flic_decode_init and the above if should deal with */
  633. /* the finite set of possibilites allowable by here. */
  634. /* But in case we do, just error out. */
  635. av_log(avctx, AV_LOG_ERROR, "Unknown FLC format, my science cannot explain how this happened.\n");
  636. return AVERROR_BUG;
  637. }
  638. static av_cold int flic_decode_end(AVCodecContext *avctx)
  639. {
  640. FlicDecodeContext *s = avctx->priv_data;
  641. if (s->frame.data[0])
  642. avctx->release_buffer(avctx, &s->frame);
  643. return 0;
  644. }
  645. AVCodec ff_flic_decoder = {
  646. .name = "flic",
  647. .type = AVMEDIA_TYPE_VIDEO,
  648. .id = AV_CODEC_ID_FLIC,
  649. .priv_data_size = sizeof(FlicDecodeContext),
  650. .init = flic_decode_init,
  651. .close = flic_decode_end,
  652. .decode = flic_decode_frame,
  653. .capabilities = CODEC_CAP_DR1,
  654. .long_name = NULL_IF_CONFIG_SMALL("Autodesk Animator Flic video"),
  655. };