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