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.

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