qtrle.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Quicktime Animation (RLE) Video Decoder
  3. * Copyright (C) 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. * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
  24. * For more information about the QT RLE format, visit:
  25. * http://www.pcisys.net/~melanson/codecs/
  26. *
  27. * The QT RLE decoder has seven modes of operation:
  28. * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
  29. * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
  30. * data. 24-bit data is RGB24 and 32-bit data is RGB32.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "libavutil/intreadwrite.h"
  36. #include "avcodec.h"
  37. typedef struct QtrleContext {
  38. AVCodecContext *avctx;
  39. AVFrame frame;
  40. const unsigned char *buf;
  41. int size;
  42. uint32_t pal[256];
  43. } QtrleContext;
  44. #define CHECK_STREAM_PTR(n) \
  45. if ((stream_ptr + n) > s->size) { \
  46. av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
  47. stream_ptr + n, s->size); \
  48. return; \
  49. }
  50. #define CHECK_PIXEL_PTR(n) \
  51. if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
  52. av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
  53. pixel_ptr + n, pixel_limit); \
  54. return; \
  55. } \
  56. static void qtrle_decode_1bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  57. {
  58. int rle_code;
  59. int pixel_ptr = 0;
  60. int row_inc = s->frame.linesize[0];
  61. unsigned char pi0, pi1; /* 2 8-pixel values */
  62. unsigned char *rgb = s->frame.data[0];
  63. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  64. int skip;
  65. while (lines_to_change) {
  66. CHECK_STREAM_PTR(2);
  67. skip = s->buf[stream_ptr++];
  68. rle_code = (signed char)s->buf[stream_ptr++];
  69. if (rle_code == 0)
  70. break;
  71. if(skip & 0x80) {
  72. lines_to_change--;
  73. row_ptr += row_inc;
  74. pixel_ptr = row_ptr + 2 * (skip & 0x7f);
  75. } else
  76. pixel_ptr += 2 * skip;
  77. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  78. if (rle_code < 0) {
  79. /* decode the run length code */
  80. rle_code = -rle_code;
  81. /* get the next 2 bytes from the stream, treat them as groups
  82. * of 8 pixels, and output them rle_code times */
  83. CHECK_STREAM_PTR(2);
  84. pi0 = s->buf[stream_ptr++];
  85. pi1 = s->buf[stream_ptr++];
  86. CHECK_PIXEL_PTR(rle_code * 2);
  87. while (rle_code--) {
  88. rgb[pixel_ptr++] = pi0;
  89. rgb[pixel_ptr++] = pi1;
  90. }
  91. } else {
  92. /* copy the same pixel directly to output 2 times */
  93. rle_code *= 2;
  94. CHECK_STREAM_PTR(rle_code);
  95. CHECK_PIXEL_PTR(rle_code);
  96. while (rle_code--)
  97. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  98. }
  99. }
  100. }
  101. static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
  102. int row_ptr, int lines_to_change, int bpp)
  103. {
  104. int rle_code, i;
  105. int pixel_ptr;
  106. int row_inc = s->frame.linesize[0];
  107. unsigned char pi[16]; /* 16 palette indices */
  108. unsigned char *rgb = s->frame.data[0];
  109. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  110. int num_pixels = (bpp == 4) ? 8 : 16;
  111. while (lines_to_change--) {
  112. CHECK_STREAM_PTR(2);
  113. pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
  114. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  115. if (rle_code == 0) {
  116. /* there's another skip code in the stream */
  117. CHECK_STREAM_PTR(1);
  118. pixel_ptr += (num_pixels * (s->buf[stream_ptr++] - 1));
  119. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  120. } else if (rle_code < 0) {
  121. /* decode the run length code */
  122. rle_code = -rle_code;
  123. /* get the next 4 bytes from the stream, treat them as palette
  124. * indexes, and output them rle_code times */
  125. CHECK_STREAM_PTR(4);
  126. for (i = num_pixels-1; i >= 0; i--) {
  127. pi[num_pixels-1-i] = (s->buf[stream_ptr] >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
  128. stream_ptr+= ((i & ((num_pixels>>2)-1)) == 0);
  129. }
  130. CHECK_PIXEL_PTR(rle_code * num_pixels);
  131. while (rle_code--) {
  132. for (i = 0; i < num_pixels; i++)
  133. rgb[pixel_ptr++] = pi[i];
  134. }
  135. } else {
  136. /* copy the same pixel directly to output 4 times */
  137. rle_code *= 4;
  138. CHECK_STREAM_PTR(rle_code);
  139. CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
  140. while (rle_code--) {
  141. if(bpp == 4) {
  142. rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
  143. rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
  144. } else {
  145. rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 6) & 0x03;
  146. rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x03;
  147. rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 2) & 0x03;
  148. rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x03;
  149. }
  150. }
  151. }
  152. }
  153. row_ptr += row_inc;
  154. }
  155. }
  156. static void qtrle_decode_8bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  157. {
  158. int rle_code;
  159. int pixel_ptr;
  160. int row_inc = s->frame.linesize[0];
  161. unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
  162. unsigned char *rgb = s->frame.data[0];
  163. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  164. while (lines_to_change--) {
  165. CHECK_STREAM_PTR(2);
  166. pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
  167. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  168. if (rle_code == 0) {
  169. /* there's another skip code in the stream */
  170. CHECK_STREAM_PTR(1);
  171. pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
  172. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  173. } else if (rle_code < 0) {
  174. /* decode the run length code */
  175. rle_code = -rle_code;
  176. /* get the next 4 bytes from the stream, treat them as palette
  177. * indexes, and output them rle_code times */
  178. CHECK_STREAM_PTR(4);
  179. pi1 = s->buf[stream_ptr++];
  180. pi2 = s->buf[stream_ptr++];
  181. pi3 = s->buf[stream_ptr++];
  182. pi4 = s->buf[stream_ptr++];
  183. CHECK_PIXEL_PTR(rle_code * 4);
  184. while (rle_code--) {
  185. rgb[pixel_ptr++] = pi1;
  186. rgb[pixel_ptr++] = pi2;
  187. rgb[pixel_ptr++] = pi3;
  188. rgb[pixel_ptr++] = pi4;
  189. }
  190. } else {
  191. /* copy the same pixel directly to output 4 times */
  192. rle_code *= 4;
  193. CHECK_STREAM_PTR(rle_code);
  194. CHECK_PIXEL_PTR(rle_code);
  195. while (rle_code--) {
  196. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  197. }
  198. }
  199. }
  200. row_ptr += row_inc;
  201. }
  202. }
  203. static void qtrle_decode_16bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  204. {
  205. int rle_code;
  206. int pixel_ptr;
  207. int row_inc = s->frame.linesize[0];
  208. unsigned short rgb16;
  209. unsigned char *rgb = s->frame.data[0];
  210. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  211. while (lines_to_change--) {
  212. CHECK_STREAM_PTR(2);
  213. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
  214. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  215. if (rle_code == 0) {
  216. /* there's another skip code in the stream */
  217. CHECK_STREAM_PTR(1);
  218. pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
  219. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  220. } else if (rle_code < 0) {
  221. /* decode the run length code */
  222. rle_code = -rle_code;
  223. CHECK_STREAM_PTR(2);
  224. rgb16 = AV_RB16(&s->buf[stream_ptr]);
  225. stream_ptr += 2;
  226. CHECK_PIXEL_PTR(rle_code * 2);
  227. while (rle_code--) {
  228. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  229. pixel_ptr += 2;
  230. }
  231. } else {
  232. CHECK_STREAM_PTR(rle_code * 2);
  233. CHECK_PIXEL_PTR(rle_code * 2);
  234. /* copy pixels directly to output */
  235. while (rle_code--) {
  236. rgb16 = AV_RB16(&s->buf[stream_ptr]);
  237. stream_ptr += 2;
  238. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  239. pixel_ptr += 2;
  240. }
  241. }
  242. }
  243. row_ptr += row_inc;
  244. }
  245. }
  246. static void qtrle_decode_24bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  247. {
  248. int rle_code;
  249. int pixel_ptr;
  250. int row_inc = s->frame.linesize[0];
  251. unsigned char r, g, b;
  252. unsigned char *rgb = s->frame.data[0];
  253. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  254. while (lines_to_change--) {
  255. CHECK_STREAM_PTR(2);
  256. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
  257. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  258. if (rle_code == 0) {
  259. /* there's another skip code in the stream */
  260. CHECK_STREAM_PTR(1);
  261. pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
  262. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  263. } else if (rle_code < 0) {
  264. /* decode the run length code */
  265. rle_code = -rle_code;
  266. CHECK_STREAM_PTR(3);
  267. r = s->buf[stream_ptr++];
  268. g = s->buf[stream_ptr++];
  269. b = s->buf[stream_ptr++];
  270. CHECK_PIXEL_PTR(rle_code * 3);
  271. while (rle_code--) {
  272. rgb[pixel_ptr++] = r;
  273. rgb[pixel_ptr++] = g;
  274. rgb[pixel_ptr++] = b;
  275. }
  276. } else {
  277. CHECK_STREAM_PTR(rle_code * 3);
  278. CHECK_PIXEL_PTR(rle_code * 3);
  279. /* copy pixels directly to output */
  280. while (rle_code--) {
  281. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  282. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  283. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  284. }
  285. }
  286. }
  287. row_ptr += row_inc;
  288. }
  289. }
  290. static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  291. {
  292. int rle_code;
  293. int pixel_ptr;
  294. int row_inc = s->frame.linesize[0];
  295. unsigned char a, r, g, b;
  296. unsigned int argb;
  297. unsigned char *rgb = s->frame.data[0];
  298. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  299. while (lines_to_change--) {
  300. CHECK_STREAM_PTR(2);
  301. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
  302. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  303. if (rle_code == 0) {
  304. /* there's another skip code in the stream */
  305. CHECK_STREAM_PTR(1);
  306. pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
  307. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  308. } else if (rle_code < 0) {
  309. /* decode the run length code */
  310. rle_code = -rle_code;
  311. CHECK_STREAM_PTR(4);
  312. a = s->buf[stream_ptr++];
  313. r = s->buf[stream_ptr++];
  314. g = s->buf[stream_ptr++];
  315. b = s->buf[stream_ptr++];
  316. argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
  317. CHECK_PIXEL_PTR(rle_code * 4);
  318. while (rle_code--) {
  319. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  320. pixel_ptr += 4;
  321. }
  322. } else {
  323. CHECK_STREAM_PTR(rle_code * 4);
  324. CHECK_PIXEL_PTR(rle_code * 4);
  325. /* copy pixels directly to output */
  326. while (rle_code--) {
  327. a = s->buf[stream_ptr++];
  328. r = s->buf[stream_ptr++];
  329. g = s->buf[stream_ptr++];
  330. b = s->buf[stream_ptr++];
  331. argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
  332. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  333. pixel_ptr += 4;
  334. }
  335. }
  336. }
  337. row_ptr += row_inc;
  338. }
  339. }
  340. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  341. {
  342. QtrleContext *s = avctx->priv_data;
  343. s->avctx = avctx;
  344. switch (avctx->bits_per_coded_sample) {
  345. case 1:
  346. case 33:
  347. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  348. break;
  349. case 2:
  350. case 4:
  351. case 8:
  352. case 34:
  353. case 36:
  354. case 40:
  355. avctx->pix_fmt = PIX_FMT_PAL8;
  356. break;
  357. case 16:
  358. avctx->pix_fmt = PIX_FMT_RGB555;
  359. break;
  360. case 24:
  361. avctx->pix_fmt = PIX_FMT_RGB24;
  362. break;
  363. case 32:
  364. avctx->pix_fmt = PIX_FMT_RGB32;
  365. break;
  366. default:
  367. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  368. avctx->bits_per_coded_sample);
  369. break;
  370. }
  371. s->frame.data[0] = NULL;
  372. return 0;
  373. }
  374. static int qtrle_decode_frame(AVCodecContext *avctx,
  375. void *data, int *data_size,
  376. AVPacket *avpkt)
  377. {
  378. const uint8_t *buf = avpkt->data;
  379. int buf_size = avpkt->size;
  380. QtrleContext *s = avctx->priv_data;
  381. int header, start_line;
  382. int stream_ptr, height, row_ptr;
  383. int has_palette = 0;
  384. s->buf = buf;
  385. s->size = buf_size;
  386. s->frame.reference = 1;
  387. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  388. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  389. if (avctx->reget_buffer(avctx, &s->frame)) {
  390. av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  391. return -1;
  392. }
  393. /* check if this frame is even supposed to change */
  394. if (s->size < 8)
  395. goto done;
  396. /* start after the chunk size */
  397. stream_ptr = 4;
  398. /* fetch the header */
  399. header = AV_RB16(&s->buf[stream_ptr]);
  400. stream_ptr += 2;
  401. /* if a header is present, fetch additional decoding parameters */
  402. if (header & 0x0008) {
  403. if(s->size < 14)
  404. goto done;
  405. start_line = AV_RB16(&s->buf[stream_ptr]);
  406. stream_ptr += 4;
  407. height = AV_RB16(&s->buf[stream_ptr]);
  408. stream_ptr += 4;
  409. } else {
  410. start_line = 0;
  411. height = s->avctx->height;
  412. }
  413. row_ptr = s->frame.linesize[0] * start_line;
  414. switch (avctx->bits_per_coded_sample) {
  415. case 1:
  416. case 33:
  417. qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
  418. break;
  419. case 2:
  420. case 34:
  421. qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
  422. has_palette = 1;
  423. break;
  424. case 4:
  425. case 36:
  426. qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
  427. has_palette = 1;
  428. break;
  429. case 8:
  430. case 40:
  431. qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
  432. has_palette = 1;
  433. break;
  434. case 16:
  435. qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
  436. break;
  437. case 24:
  438. qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
  439. break;
  440. case 32:
  441. qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
  442. break;
  443. default:
  444. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  445. avctx->bits_per_coded_sample);
  446. break;
  447. }
  448. if(has_palette) {
  449. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  450. if (pal) {
  451. s->frame.palette_has_changed = 1;
  452. memcpy(s->pal, pal, AVPALETTE_SIZE);
  453. }
  454. /* make the palette available on the way out */
  455. memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
  456. }
  457. done:
  458. *data_size = sizeof(AVFrame);
  459. *(AVFrame*)data = s->frame;
  460. /* always report that the buffer was completely consumed */
  461. return buf_size;
  462. }
  463. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  464. {
  465. QtrleContext *s = avctx->priv_data;
  466. if (s->frame.data[0])
  467. avctx->release_buffer(avctx, &s->frame);
  468. return 0;
  469. }
  470. AVCodec ff_qtrle_decoder = {
  471. "qtrle",
  472. AVMEDIA_TYPE_VIDEO,
  473. CODEC_ID_QTRLE,
  474. sizeof(QtrleContext),
  475. qtrle_decode_init,
  476. NULL,
  477. qtrle_decode_end,
  478. qtrle_decode_frame,
  479. CODEC_CAP_DR1,
  480. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  481. };