flicvideo.c 28 KB

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