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.

513 lines
16KB

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