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.

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