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.

552 lines
17KB

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