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.

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