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.

562 lines
19KB

  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 "avcodec.h"
  36. #include "bytestream.h"
  37. #include "internal.h"
  38. typedef struct QtrleContext {
  39. AVCodecContext *avctx;
  40. AVFrame *frame;
  41. GetByteContext g;
  42. uint32_t pal[256];
  43. } QtrleContext;
  44. #define CHECK_PIXEL_PTR(n) \
  45. if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
  46. av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
  47. pixel_ptr + n, pixel_limit); \
  48. return; \
  49. } \
  50. static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  51. {
  52. int rle_code;
  53. int pixel_ptr;
  54. int row_inc = s->frame->linesize[0];
  55. uint8_t pi0, pi1; /* 2 8-pixel values */
  56. uint8_t *rgb = s->frame->data[0];
  57. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  58. int skip;
  59. /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
  60. * as 'go to next line' during the decoding of a frame but is 'go to first
  61. * line' at the beginning. Since we always interpret it as 'go to next line'
  62. * in the decoding loop (which makes code simpler/faster), the first line
  63. * would not be counted, so we count one more.
  64. * See: https://trac.ffmpeg.org/ticket/226
  65. * In the following decoding loop, row_ptr will be the position of the
  66. * current row. */
  67. row_ptr -= row_inc;
  68. pixel_ptr = row_ptr;
  69. lines_to_change++;
  70. while (lines_to_change) {
  71. skip = bytestream2_get_byte(&s->g);
  72. rle_code = (int8_t)bytestream2_get_byte(&s->g);
  73. if (rle_code == 0)
  74. break;
  75. if(skip & 0x80) {
  76. lines_to_change--;
  77. row_ptr += row_inc;
  78. pixel_ptr = row_ptr + 2 * 8 * (skip & 0x7f);
  79. } else
  80. pixel_ptr += 2 * 8 * skip;
  81. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  82. if(rle_code == -1)
  83. continue;
  84. if (rle_code < 0) {
  85. /* decode the run length code */
  86. rle_code = -rle_code;
  87. /* get the next 2 bytes from the stream, treat them as groups
  88. * of 8 pixels, and output them rle_code times */
  89. pi0 = bytestream2_get_byte(&s->g);
  90. pi1 = bytestream2_get_byte(&s->g);
  91. CHECK_PIXEL_PTR(rle_code * 2 * 8);
  92. while (rle_code--) {
  93. rgb[pixel_ptr++] = (pi0 >> 7) & 0x01;
  94. rgb[pixel_ptr++] = (pi0 >> 6) & 0x01;
  95. rgb[pixel_ptr++] = (pi0 >> 5) & 0x01;
  96. rgb[pixel_ptr++] = (pi0 >> 4) & 0x01;
  97. rgb[pixel_ptr++] = (pi0 >> 3) & 0x01;
  98. rgb[pixel_ptr++] = (pi0 >> 2) & 0x01;
  99. rgb[pixel_ptr++] = (pi0 >> 1) & 0x01;
  100. rgb[pixel_ptr++] = pi0 & 0x01;
  101. rgb[pixel_ptr++] = (pi1 >> 7) & 0x01;
  102. rgb[pixel_ptr++] = (pi1 >> 6) & 0x01;
  103. rgb[pixel_ptr++] = (pi1 >> 5) & 0x01;
  104. rgb[pixel_ptr++] = (pi1 >> 4) & 0x01;
  105. rgb[pixel_ptr++] = (pi1 >> 3) & 0x01;
  106. rgb[pixel_ptr++] = (pi1 >> 2) & 0x01;
  107. rgb[pixel_ptr++] = (pi1 >> 1) & 0x01;
  108. rgb[pixel_ptr++] = pi1 & 0x01;
  109. }
  110. } else {
  111. /* copy the same pixel directly to output 2 times */
  112. rle_code *= 2;
  113. CHECK_PIXEL_PTR(rle_code * 8);
  114. while (rle_code--) {
  115. int x = bytestream2_get_byte(&s->g);
  116. rgb[pixel_ptr++] = (x >> 7) & 0x01;
  117. rgb[pixel_ptr++] = (x >> 6) & 0x01;
  118. rgb[pixel_ptr++] = (x >> 5) & 0x01;
  119. rgb[pixel_ptr++] = (x >> 4) & 0x01;
  120. rgb[pixel_ptr++] = (x >> 3) & 0x01;
  121. rgb[pixel_ptr++] = (x >> 2) & 0x01;
  122. rgb[pixel_ptr++] = (x >> 1) & 0x01;
  123. rgb[pixel_ptr++] = x & 0x01;
  124. }
  125. }
  126. }
  127. }
  128. static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
  129. int lines_to_change, int bpp)
  130. {
  131. int rle_code, i;
  132. int pixel_ptr;
  133. int row_inc = s->frame->linesize[0];
  134. uint8_t pi[16]; /* 16 palette indices */
  135. uint8_t *rgb = s->frame->data[0];
  136. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  137. int num_pixels = (bpp == 4) ? 8 : 16;
  138. while (lines_to_change--) {
  139. pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  140. CHECK_PIXEL_PTR(0);
  141. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  142. if (bytestream2_get_bytes_left(&s->g) < 1)
  143. return;
  144. if (rle_code == 0) {
  145. /* there's another skip code in the stream */
  146. pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  147. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  148. } else if (rle_code < 0) {
  149. /* decode the run length code */
  150. rle_code = -rle_code;
  151. /* get the next 4 bytes from the stream, treat them as palette
  152. * indexes, and output them rle_code times */
  153. for (i = num_pixels-1; i >= 0; i--) {
  154. pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
  155. bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
  156. }
  157. CHECK_PIXEL_PTR(rle_code * num_pixels);
  158. while (rle_code--) {
  159. memcpy(&rgb[pixel_ptr], &pi, num_pixels);
  160. pixel_ptr += num_pixels;
  161. }
  162. } else {
  163. /* copy the same pixel directly to output 4 times */
  164. rle_code *= 4;
  165. CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
  166. while (rle_code--) {
  167. if(bpp == 4) {
  168. int x = bytestream2_get_byte(&s->g);
  169. rgb[pixel_ptr++] = (x >> 4) & 0x0f;
  170. rgb[pixel_ptr++] = x & 0x0f;
  171. } else {
  172. int x = bytestream2_get_byte(&s->g);
  173. rgb[pixel_ptr++] = (x >> 6) & 0x03;
  174. rgb[pixel_ptr++] = (x >> 4) & 0x03;
  175. rgb[pixel_ptr++] = (x >> 2) & 0x03;
  176. rgb[pixel_ptr++] = x & 0x03;
  177. }
  178. }
  179. }
  180. }
  181. row_ptr += row_inc;
  182. }
  183. }
  184. static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  185. {
  186. int rle_code;
  187. int pixel_ptr;
  188. int row_inc = s->frame->linesize[0];
  189. uint8_t pi1, pi2, pi3, pi4; /* 4 palette indexes */
  190. uint8_t *rgb = s->frame->data[0];
  191. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  192. while (lines_to_change--) {
  193. pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
  194. CHECK_PIXEL_PTR(0);
  195. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  196. if (bytestream2_get_bytes_left(&s->g) < 1)
  197. return;
  198. if (rle_code == 0) {
  199. /* there's another skip code in the stream */
  200. pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
  201. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  202. } else if (rle_code < 0) {
  203. /* decode the run length code */
  204. rle_code = -rle_code;
  205. /* get the next 4 bytes from the stream, treat them as palette
  206. * indexes, and output them rle_code times */
  207. pi1 = bytestream2_get_byte(&s->g);
  208. pi2 = bytestream2_get_byte(&s->g);
  209. pi3 = bytestream2_get_byte(&s->g);
  210. pi4 = bytestream2_get_byte(&s->g);
  211. CHECK_PIXEL_PTR(rle_code * 4);
  212. while (rle_code--) {
  213. rgb[pixel_ptr++] = pi1;
  214. rgb[pixel_ptr++] = pi2;
  215. rgb[pixel_ptr++] = pi3;
  216. rgb[pixel_ptr++] = pi4;
  217. }
  218. } else {
  219. /* copy the same pixel directly to output 4 times */
  220. rle_code *= 4;
  221. CHECK_PIXEL_PTR(rle_code);
  222. bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
  223. pixel_ptr += rle_code;
  224. }
  225. }
  226. row_ptr += row_inc;
  227. }
  228. }
  229. static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  230. {
  231. int rle_code;
  232. int pixel_ptr;
  233. int row_inc = s->frame->linesize[0];
  234. uint16_t rgb16;
  235. uint8_t *rgb = s->frame->data[0];
  236. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  237. while (lines_to_change--) {
  238. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
  239. CHECK_PIXEL_PTR(0);
  240. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  241. if (bytestream2_get_bytes_left(&s->g) < 1)
  242. return;
  243. if (rle_code == 0) {
  244. /* there's another skip code in the stream */
  245. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
  246. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  247. } else if (rle_code < 0) {
  248. /* decode the run length code */
  249. rle_code = -rle_code;
  250. rgb16 = bytestream2_get_be16(&s->g);
  251. CHECK_PIXEL_PTR(rle_code * 2);
  252. while (rle_code--) {
  253. *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
  254. pixel_ptr += 2;
  255. }
  256. } else {
  257. CHECK_PIXEL_PTR(rle_code * 2);
  258. /* copy pixels directly to output */
  259. while (rle_code--) {
  260. rgb16 = bytestream2_get_be16(&s->g);
  261. *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
  262. pixel_ptr += 2;
  263. }
  264. }
  265. }
  266. row_ptr += row_inc;
  267. }
  268. }
  269. static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  270. {
  271. int rle_code;
  272. int pixel_ptr;
  273. int row_inc = s->frame->linesize[0];
  274. uint8_t r, g, b;
  275. uint8_t *rgb = s->frame->data[0];
  276. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  277. while (lines_to_change--) {
  278. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
  279. CHECK_PIXEL_PTR(0);
  280. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  281. if (bytestream2_get_bytes_left(&s->g) < 1)
  282. return;
  283. if (rle_code == 0) {
  284. /* there's another skip code in the stream */
  285. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
  286. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  287. } else if (rle_code < 0) {
  288. /* decode the run length code */
  289. rle_code = -rle_code;
  290. r = bytestream2_get_byte(&s->g);
  291. g = bytestream2_get_byte(&s->g);
  292. b = bytestream2_get_byte(&s->g);
  293. CHECK_PIXEL_PTR(rle_code * 3);
  294. while (rle_code--) {
  295. rgb[pixel_ptr++] = r;
  296. rgb[pixel_ptr++] = g;
  297. rgb[pixel_ptr++] = b;
  298. }
  299. } else {
  300. CHECK_PIXEL_PTR(rle_code * 3);
  301. /* copy pixels directly to output */
  302. while (rle_code--) {
  303. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  304. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  305. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  306. }
  307. }
  308. }
  309. row_ptr += row_inc;
  310. }
  311. }
  312. static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  313. {
  314. int rle_code;
  315. int pixel_ptr;
  316. int row_inc = s->frame->linesize[0];
  317. unsigned int argb;
  318. uint8_t *rgb = s->frame->data[0];
  319. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  320. while (lines_to_change--) {
  321. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
  322. CHECK_PIXEL_PTR(0);
  323. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  324. if (bytestream2_get_bytes_left(&s->g) < 1)
  325. return;
  326. if (rle_code == 0) {
  327. /* there's another skip code in the stream */
  328. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
  329. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  330. } else if (rle_code < 0) {
  331. /* decode the run length code */
  332. rle_code = -rle_code;
  333. argb = bytestream2_get_be32(&s->g);
  334. CHECK_PIXEL_PTR(rle_code * 4);
  335. while (rle_code--) {
  336. AV_WN32A(rgb + pixel_ptr, argb);
  337. pixel_ptr += 4;
  338. }
  339. } else {
  340. CHECK_PIXEL_PTR(rle_code * 4);
  341. /* copy pixels directly to output */
  342. while (rle_code--) {
  343. argb = bytestream2_get_be32(&s->g);
  344. AV_WN32A(rgb + pixel_ptr, argb);
  345. pixel_ptr += 4;
  346. }
  347. }
  348. }
  349. row_ptr += row_inc;
  350. }
  351. }
  352. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  353. {
  354. QtrleContext *s = avctx->priv_data;
  355. s->avctx = avctx;
  356. switch (avctx->bits_per_coded_sample) {
  357. case 1:
  358. case 2:
  359. case 4:
  360. case 8:
  361. case 33:
  362. case 34:
  363. case 36:
  364. case 40:
  365. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  366. break;
  367. case 16:
  368. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  369. break;
  370. case 24:
  371. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  372. break;
  373. case 32:
  374. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  375. break;
  376. default:
  377. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  378. avctx->bits_per_coded_sample);
  379. return AVERROR_INVALIDDATA;
  380. }
  381. s->frame = av_frame_alloc();
  382. if (!s->frame)
  383. return AVERROR(ENOMEM);
  384. return 0;
  385. }
  386. static int qtrle_decode_frame(AVCodecContext *avctx,
  387. void *data, int *got_frame,
  388. AVPacket *avpkt)
  389. {
  390. QtrleContext *s = avctx->priv_data;
  391. int header, start_line;
  392. int height, row_ptr;
  393. int has_palette = 0;
  394. int ret;
  395. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  396. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  397. return ret;
  398. /* check if this frame is even supposed to change */
  399. if (avpkt->size < 8)
  400. goto done;
  401. /* start after the chunk size */
  402. bytestream2_seek(&s->g, 4, SEEK_SET);
  403. /* fetch the header */
  404. header = bytestream2_get_be16(&s->g);
  405. /* if a header is present, fetch additional decoding parameters */
  406. if (header & 0x0008) {
  407. if (avpkt->size < 14)
  408. goto done;
  409. start_line = bytestream2_get_be16(&s->g);
  410. bytestream2_skip(&s->g, 2);
  411. height = bytestream2_get_be16(&s->g);
  412. bytestream2_skip(&s->g, 2);
  413. if (height > s->avctx->height - start_line)
  414. goto done;
  415. } else {
  416. start_line = 0;
  417. height = s->avctx->height;
  418. }
  419. row_ptr = s->frame->linesize[0] * start_line;
  420. switch (avctx->bits_per_coded_sample) {
  421. case 1:
  422. case 33:
  423. qtrle_decode_1bpp(s, row_ptr, height);
  424. has_palette = 1;
  425. break;
  426. case 2:
  427. case 34:
  428. qtrle_decode_2n4bpp(s, row_ptr, height, 2);
  429. has_palette = 1;
  430. break;
  431. case 4:
  432. case 36:
  433. qtrle_decode_2n4bpp(s, row_ptr, height, 4);
  434. has_palette = 1;
  435. break;
  436. case 8:
  437. case 40:
  438. qtrle_decode_8bpp(s, row_ptr, height);
  439. has_palette = 1;
  440. break;
  441. case 16:
  442. qtrle_decode_16bpp(s, row_ptr, height);
  443. break;
  444. case 24:
  445. qtrle_decode_24bpp(s, row_ptr, height);
  446. break;
  447. case 32:
  448. qtrle_decode_32bpp(s, 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. int size;
  457. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
  458. if (pal && size == AVPALETTE_SIZE) {
  459. s->frame->palette_has_changed = 1;
  460. memcpy(s->pal, pal, AVPALETTE_SIZE);
  461. } else if (pal) {
  462. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
  463. }
  464. /* make the palette available on the way out */
  465. memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
  466. }
  467. done:
  468. if ((ret = av_frame_ref(data, s->frame)) < 0)
  469. return ret;
  470. *got_frame = 1;
  471. /* always report that the buffer was completely consumed */
  472. return avpkt->size;
  473. }
  474. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  475. {
  476. QtrleContext *s = avctx->priv_data;
  477. av_frame_free(&s->frame);
  478. return 0;
  479. }
  480. AVCodec ff_qtrle_decoder = {
  481. .name = "qtrle",
  482. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  483. .type = AVMEDIA_TYPE_VIDEO,
  484. .id = AV_CODEC_ID_QTRLE,
  485. .priv_data_size = sizeof(QtrleContext),
  486. .init = qtrle_decode_init,
  487. .close = qtrle_decode_end,
  488. .decode = qtrle_decode_frame,
  489. .capabilities = AV_CODEC_CAP_DR1,
  490. };