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.

561 lines
18KB

  1. /*
  2. * Quicktime Animation (RLE) Video Decoder
  3. * Copyright (C) 2004 the ffmpeg project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. } QtrleContext;
  43. #define CHECK_STREAM_PTR(n) \
  44. if ((stream_ptr + n) > s->size) { \
  45. av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
  46. stream_ptr + n, s->size); \
  47. return; \
  48. }
  49. #define CHECK_PIXEL_PTR(n) \
  50. if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
  51. av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %d\n", \
  52. pixel_ptr + n, pixel_limit); \
  53. return; \
  54. } \
  55. static void qtrle_decode_1bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  56. {
  57. int rle_code;
  58. int pixel_ptr = 0;
  59. int row_inc = s->frame.linesize[0];
  60. unsigned char pi0, pi1; /* 2 8-pixel values */
  61. unsigned char *rgb = s->frame.data[0];
  62. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  63. int skip;
  64. while (lines_to_change) {
  65. CHECK_STREAM_PTR(2);
  66. skip = s->buf[stream_ptr++];
  67. rle_code = (signed char)s->buf[stream_ptr++];
  68. if (rle_code == 0)
  69. break;
  70. if(skip & 0x80) {
  71. lines_to_change--;
  72. row_ptr += row_inc;
  73. pixel_ptr = row_ptr + 2 * (skip & 0x7f);
  74. } else
  75. pixel_ptr += 2 * skip;
  76. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  77. if (rle_code < 0) {
  78. /* decode the run length code */
  79. rle_code = -rle_code;
  80. /* get the next 2 bytes from the stream, treat them as groups
  81. * of 8 pixels, and output them rle_code times */
  82. CHECK_STREAM_PTR(2);
  83. pi0 = s->buf[stream_ptr++];
  84. pi1 = s->buf[stream_ptr++];
  85. CHECK_PIXEL_PTR(rle_code * 2);
  86. while (rle_code--) {
  87. rgb[pixel_ptr++] = pi0;
  88. rgb[pixel_ptr++] = pi1;
  89. }
  90. } else {
  91. /* copy the same pixel directly to output 2 times */
  92. rle_code *= 2;
  93. CHECK_STREAM_PTR(rle_code);
  94. CHECK_PIXEL_PTR(rle_code);
  95. while (rle_code--)
  96. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  97. }
  98. }
  99. }
  100. static inline void qtrle_decode_2n4bpp(QtrleContext *s, int stream_ptr,
  101. int row_ptr, int lines_to_change, int bpp)
  102. {
  103. int rle_code, i;
  104. int pixel_ptr;
  105. int row_inc = s->frame.linesize[0];
  106. unsigned char pi[16]; /* 16 palette indices */
  107. unsigned char *rgb = s->frame.data[0];
  108. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  109. int num_pixels = (bpp == 4) ? 8 : 16;
  110. while (lines_to_change--) {
  111. CHECK_STREAM_PTR(2);
  112. pixel_ptr = row_ptr + (num_pixels * (s->buf[stream_ptr++] - 1));
  113. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  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. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  168. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  169. if (rle_code == 0) {
  170. /* there's another skip code in the stream */
  171. CHECK_STREAM_PTR(1);
  172. pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
  173. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  174. } else if (rle_code < 0) {
  175. /* decode the run length code */
  176. rle_code = -rle_code;
  177. /* get the next 4 bytes from the stream, treat them as palette
  178. * indexes, and output them rle_code times */
  179. CHECK_STREAM_PTR(4);
  180. pi1 = s->buf[stream_ptr++];
  181. pi2 = s->buf[stream_ptr++];
  182. pi3 = s->buf[stream_ptr++];
  183. pi4 = s->buf[stream_ptr++];
  184. CHECK_PIXEL_PTR(rle_code * 4);
  185. while (rle_code--) {
  186. rgb[pixel_ptr++] = pi1;
  187. rgb[pixel_ptr++] = pi2;
  188. rgb[pixel_ptr++] = pi3;
  189. rgb[pixel_ptr++] = pi4;
  190. }
  191. } else {
  192. /* copy the same pixel directly to output 4 times */
  193. rle_code *= 4;
  194. CHECK_STREAM_PTR(rle_code);
  195. CHECK_PIXEL_PTR(rle_code);
  196. while (rle_code--) {
  197. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  198. }
  199. }
  200. }
  201. row_ptr += row_inc;
  202. }
  203. }
  204. static void qtrle_decode_16bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  205. {
  206. int rle_code;
  207. int pixel_ptr;
  208. int row_inc = s->frame.linesize[0];
  209. unsigned short rgb16;
  210. unsigned char *rgb = s->frame.data[0];
  211. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  212. while (lines_to_change--) {
  213. CHECK_STREAM_PTR(2);
  214. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
  215. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  216. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  217. if (rle_code == 0) {
  218. /* there's another skip code in the stream */
  219. CHECK_STREAM_PTR(1);
  220. pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
  221. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  222. } else if (rle_code < 0) {
  223. /* decode the run length code */
  224. rle_code = -rle_code;
  225. CHECK_STREAM_PTR(2);
  226. rgb16 = AV_RB16(&s->buf[stream_ptr]);
  227. stream_ptr += 2;
  228. CHECK_PIXEL_PTR(rle_code * 2);
  229. while (rle_code--) {
  230. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  231. pixel_ptr += 2;
  232. }
  233. } else {
  234. CHECK_STREAM_PTR(rle_code * 2);
  235. CHECK_PIXEL_PTR(rle_code * 2);
  236. /* copy pixels directly to output */
  237. while (rle_code--) {
  238. rgb16 = AV_RB16(&s->buf[stream_ptr]);
  239. stream_ptr += 2;
  240. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  241. pixel_ptr += 2;
  242. }
  243. }
  244. }
  245. row_ptr += row_inc;
  246. }
  247. }
  248. static void qtrle_decode_24bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  249. {
  250. int rle_code;
  251. int pixel_ptr;
  252. int row_inc = s->frame.linesize[0];
  253. unsigned char r, g, b;
  254. unsigned char *rgb = s->frame.data[0];
  255. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  256. while (lines_to_change--) {
  257. CHECK_STREAM_PTR(2);
  258. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
  259. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  260. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  261. if (rle_code == 0) {
  262. /* there's another skip code in the stream */
  263. CHECK_STREAM_PTR(1);
  264. pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
  265. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  266. } else if (rle_code < 0) {
  267. /* decode the run length code */
  268. rle_code = -rle_code;
  269. CHECK_STREAM_PTR(3);
  270. r = s->buf[stream_ptr++];
  271. g = s->buf[stream_ptr++];
  272. b = s->buf[stream_ptr++];
  273. CHECK_PIXEL_PTR(rle_code * 3);
  274. while (rle_code--) {
  275. rgb[pixel_ptr++] = r;
  276. rgb[pixel_ptr++] = g;
  277. rgb[pixel_ptr++] = b;
  278. }
  279. } else {
  280. CHECK_STREAM_PTR(rle_code * 3);
  281. CHECK_PIXEL_PTR(rle_code * 3);
  282. /* copy pixels directly to output */
  283. while (rle_code--) {
  284. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  285. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  286. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  287. }
  288. }
  289. }
  290. row_ptr += row_inc;
  291. }
  292. }
  293. static void qtrle_decode_32bpp(QtrleContext *s, int stream_ptr, int row_ptr, int lines_to_change)
  294. {
  295. int rle_code;
  296. int pixel_ptr;
  297. int row_inc = s->frame.linesize[0];
  298. unsigned char a, r, g, b;
  299. unsigned int argb;
  300. unsigned char *rgb = s->frame.data[0];
  301. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  302. while (lines_to_change--) {
  303. CHECK_STREAM_PTR(2);
  304. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
  305. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  306. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  307. if (rle_code == 0) {
  308. /* there's another skip code in the stream */
  309. CHECK_STREAM_PTR(1);
  310. pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
  311. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  312. } else if (rle_code < 0) {
  313. /* decode the run length code */
  314. rle_code = -rle_code;
  315. CHECK_STREAM_PTR(4);
  316. a = s->buf[stream_ptr++];
  317. r = s->buf[stream_ptr++];
  318. g = s->buf[stream_ptr++];
  319. b = s->buf[stream_ptr++];
  320. argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
  321. CHECK_PIXEL_PTR(rle_code * 4);
  322. while (rle_code--) {
  323. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  324. pixel_ptr += 4;
  325. }
  326. } else {
  327. CHECK_STREAM_PTR(rle_code * 4);
  328. CHECK_PIXEL_PTR(rle_code * 4);
  329. /* copy pixels directly to output */
  330. while (rle_code--) {
  331. a = s->buf[stream_ptr++];
  332. r = s->buf[stream_ptr++];
  333. g = s->buf[stream_ptr++];
  334. b = s->buf[stream_ptr++];
  335. argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
  336. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  337. pixel_ptr += 4;
  338. }
  339. }
  340. }
  341. row_ptr += row_inc;
  342. }
  343. }
  344. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  345. {
  346. QtrleContext *s = avctx->priv_data;
  347. s->avctx = avctx;
  348. switch (avctx->bits_per_coded_sample) {
  349. case 1:
  350. case 33:
  351. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  352. break;
  353. case 2:
  354. case 4:
  355. case 8:
  356. case 34:
  357. case 36:
  358. case 40:
  359. avctx->pix_fmt = PIX_FMT_PAL8;
  360. break;
  361. case 16:
  362. avctx->pix_fmt = PIX_FMT_RGB555;
  363. break;
  364. case 24:
  365. avctx->pix_fmt = PIX_FMT_RGB24;
  366. break;
  367. case 32:
  368. avctx->pix_fmt = PIX_FMT_RGB32;
  369. break;
  370. default:
  371. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  372. avctx->bits_per_coded_sample);
  373. return AVERROR_INVALIDDATA;
  374. }
  375. avcodec_get_frame_defaults(&s->frame);
  376. s->frame.data[0] = NULL;
  377. return 0;
  378. }
  379. static int qtrle_decode_frame(AVCodecContext *avctx,
  380. void *data, int *data_size,
  381. AVPacket *avpkt)
  382. {
  383. const uint8_t *buf = avpkt->data;
  384. int buf_size = avpkt->size;
  385. QtrleContext *s = avctx->priv_data;
  386. int header, start_line;
  387. int stream_ptr, height, row_ptr;
  388. int has_palette = 0;
  389. s->buf = buf;
  390. s->size = buf_size;
  391. s->frame.reference = 1;
  392. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  393. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  394. if (avctx->reget_buffer(avctx, &s->frame)) {
  395. av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  396. return -1;
  397. }
  398. /* check if this frame is even supposed to change */
  399. if (s->size < 8)
  400. goto done;
  401. /* start after the chunk size */
  402. stream_ptr = 4;
  403. /* fetch the header */
  404. header = AV_RB16(&s->buf[stream_ptr]);
  405. stream_ptr += 2;
  406. /* if a header is present, fetch additional decoding parameters */
  407. if (header & 0x0008) {
  408. if(s->size < 14)
  409. goto done;
  410. start_line = AV_RB16(&s->buf[stream_ptr]);
  411. stream_ptr += 4;
  412. height = AV_RB16(&s->buf[stream_ptr]);
  413. stream_ptr += 4;
  414. if (height > s->avctx->height - start_line)
  415. goto done;
  416. } else {
  417. start_line = 0;
  418. height = s->avctx->height;
  419. }
  420. row_ptr = s->frame.linesize[0] * start_line;
  421. switch (avctx->bits_per_coded_sample) {
  422. case 1:
  423. case 33:
  424. qtrle_decode_1bpp(s, stream_ptr, row_ptr, height);
  425. break;
  426. case 2:
  427. case 34:
  428. qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 2);
  429. has_palette = 1;
  430. break;
  431. case 4:
  432. case 36:
  433. qtrle_decode_2n4bpp(s, stream_ptr, row_ptr, height, 4);
  434. has_palette = 1;
  435. break;
  436. case 8:
  437. case 40:
  438. qtrle_decode_8bpp(s, stream_ptr, row_ptr, height);
  439. has_palette = 1;
  440. break;
  441. case 16:
  442. qtrle_decode_16bpp(s, stream_ptr, row_ptr, height);
  443. break;
  444. case 24:
  445. qtrle_decode_24bpp(s, stream_ptr, row_ptr, height);
  446. break;
  447. case 32:
  448. qtrle_decode_32bpp(s, stream_ptr, row_ptr, height);
  449. break;
  450. default:
  451. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  452. avctx->bits_per_coded_sample);
  453. break;
  454. }
  455. if(has_palette) {
  456. /* make the palette available on the way out */
  457. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  458. if (s->avctx->palctrl->palette_changed) {
  459. s->frame.palette_has_changed = 1;
  460. s->avctx->palctrl->palette_changed = 0;
  461. }
  462. }
  463. done:
  464. *data_size = sizeof(AVFrame);
  465. *(AVFrame*)data = s->frame;
  466. /* always report that the buffer was completely consumed */
  467. return buf_size;
  468. }
  469. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  470. {
  471. QtrleContext *s = avctx->priv_data;
  472. if (s->frame.data[0])
  473. avctx->release_buffer(avctx, &s->frame);
  474. return 0;
  475. }
  476. AVCodec ff_qtrle_decoder = {
  477. "qtrle",
  478. AVMEDIA_TYPE_VIDEO,
  479. CODEC_ID_QTRLE,
  480. sizeof(QtrleContext),
  481. qtrle_decode_init,
  482. NULL,
  483. qtrle_decode_end,
  484. qtrle_decode_frame,
  485. CODEC_CAP_DR1,
  486. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  487. };