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.

807 lines
31KB

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