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.

518 lines
17KB

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