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.

549 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 "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 int argb;
  296. unsigned char *rgb = s->frame.data[0];
  297. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  298. while (lines_to_change--) {
  299. CHECK_STREAM_PTR(2);
  300. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
  301. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  302. if (rle_code == 0) {
  303. /* there's another skip code in the stream */
  304. CHECK_STREAM_PTR(1);
  305. pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
  306. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  307. } else if (rle_code < 0) {
  308. /* decode the run length code */
  309. rle_code = -rle_code;
  310. CHECK_STREAM_PTR(4);
  311. argb = AV_RB32(s->buf + stream_ptr);
  312. stream_ptr += 4;
  313. CHECK_PIXEL_PTR(rle_code * 4);
  314. while (rle_code--) {
  315. AV_WN32A(rgb + pixel_ptr, argb);
  316. pixel_ptr += 4;
  317. }
  318. } else {
  319. CHECK_STREAM_PTR(rle_code * 4);
  320. CHECK_PIXEL_PTR(rle_code * 4);
  321. /* copy pixels directly to output */
  322. while (rle_code--) {
  323. argb = AV_RB32(s->buf + stream_ptr);
  324. AV_WN32A(rgb + pixel_ptr, argb);
  325. stream_ptr += 4;
  326. pixel_ptr += 4;
  327. }
  328. }
  329. }
  330. row_ptr += row_inc;
  331. }
  332. }
  333. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  334. {
  335. QtrleContext *s = avctx->priv_data;
  336. s->avctx = avctx;
  337. switch (avctx->bits_per_coded_sample) {
  338. case 1:
  339. case 33:
  340. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  341. break;
  342. case 2:
  343. case 4:
  344. case 8:
  345. case 34:
  346. case 36:
  347. case 40:
  348. avctx->pix_fmt = PIX_FMT_PAL8;
  349. break;
  350. case 16:
  351. avctx->pix_fmt = PIX_FMT_RGB555;
  352. break;
  353. case 24:
  354. avctx->pix_fmt = PIX_FMT_RGB24;
  355. break;
  356. case 32:
  357. avctx->pix_fmt = PIX_FMT_RGB32;
  358. break;
  359. default:
  360. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  361. avctx->bits_per_coded_sample);
  362. break;
  363. }
  364. s->frame.data[0] = NULL;
  365. return 0;
  366. }
  367. static int qtrle_decode_frame(AVCodecContext *avctx,
  368. void *data, int *data_size,
  369. AVPacket *avpkt)
  370. {
  371. const uint8_t *buf = avpkt->data;
  372. int buf_size = avpkt->size;
  373. QtrleContext *s = avctx->priv_data;
  374. int header, start_line;
  375. int stream_ptr, height, row_ptr;
  376. int has_palette = 0;
  377. s->buf = buf;
  378. s->size = buf_size;
  379. s->frame.reference = 1;
  380. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  381. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  382. if (avctx->reget_buffer(avctx, &s->frame)) {
  383. av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  384. return -1;
  385. }
  386. /* check if this frame is even supposed to change */
  387. if (s->size < 8)
  388. goto done;
  389. /* start after the chunk size */
  390. stream_ptr = 4;
  391. /* fetch the header */
  392. header = AV_RB16(&s->buf[stream_ptr]);
  393. stream_ptr += 2;
  394. /* if a header is present, fetch additional decoding parameters */
  395. if (header & 0x0008) {
  396. if(s->size < 14)
  397. goto done;
  398. start_line = AV_RB16(&s->buf[stream_ptr]);
  399. stream_ptr += 4;
  400. height = AV_RB16(&s->buf[stream_ptr]);
  401. stream_ptr += 4;
  402. } else {
  403. start_line = 0;
  404. height = s->avctx->height;
  405. }
  406. row_ptr = s->frame.linesize[0] * start_line;
  407. switch (avctx->bits_per_coded_sample) {
  408. case 1:
  409. case 33:
  410. qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
  411. break;
  412. case 2:
  413. case 34:
  414. qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
  415. has_palette = 1;
  416. break;
  417. case 4:
  418. case 36:
  419. qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
  420. has_palette = 1;
  421. break;
  422. case 8:
  423. case 40:
  424. qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
  425. has_palette = 1;
  426. break;
  427. case 16:
  428. qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
  429. break;
  430. case 24:
  431. qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
  432. break;
  433. case 32:
  434. qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
  435. break;
  436. default:
  437. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  438. avctx->bits_per_coded_sample);
  439. break;
  440. }
  441. if(has_palette) {
  442. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  443. if (pal) {
  444. s->frame.palette_has_changed = 1;
  445. memcpy(s->pal, pal, AVPALETTE_SIZE);
  446. }
  447. /* make the palette available on the way out */
  448. memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
  449. }
  450. done:
  451. *data_size = sizeof(AVFrame);
  452. *(AVFrame*)data = s->frame;
  453. /* always report that the buffer was completely consumed */
  454. return buf_size;
  455. }
  456. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  457. {
  458. QtrleContext *s = avctx->priv_data;
  459. if (s->frame.data[0])
  460. avctx->release_buffer(avctx, &s->frame);
  461. return 0;
  462. }
  463. AVCodec ff_qtrle_decoder = {
  464. .name = "qtrle",
  465. .type = AVMEDIA_TYPE_VIDEO,
  466. .id = CODEC_ID_QTRLE,
  467. .priv_data_size = sizeof(QtrleContext),
  468. .init = qtrle_decode_init,
  469. .close = qtrle_decode_end,
  470. .decode = qtrle_decode_frame,
  471. .capabilities = CODEC_CAP_DR1,
  472. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  473. };