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.

603 lines
20KB

  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, rle_code_half;
  272. int pixel_ptr;
  273. int row_inc = s->frame->linesize[0];
  274. uint8_t b;
  275. uint16_t rg;
  276. uint8_t *rgb = s->frame->data[0];
  277. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  278. while (lines_to_change--) {
  279. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
  280. CHECK_PIXEL_PTR(0);
  281. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  282. if (bytestream2_get_bytes_left(&s->g) < 1)
  283. return;
  284. if (rle_code == 0) {
  285. /* there's another skip code in the stream */
  286. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
  287. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  288. } else if (rle_code < 0) {
  289. /* decode the run length code */
  290. rle_code = -rle_code;
  291. rg = bytestream2_get_ne16(&s->g);
  292. b = bytestream2_get_byte(&s->g);
  293. CHECK_PIXEL_PTR(rle_code * 3);
  294. while (rle_code--) {
  295. AV_WN16(rgb + pixel_ptr, rg);
  296. rgb[pixel_ptr + 2] = b;
  297. pixel_ptr += 3;
  298. }
  299. } else {
  300. CHECK_PIXEL_PTR(rle_code * 3);
  301. rle_code_half = rle_code / 2;
  302. while (rle_code_half--) { /* copy 2 raw rgb value at the same time */
  303. AV_WN32(rgb + pixel_ptr, bytestream2_get_ne32(&s->g)); /* rgbr */
  304. AV_WN16(rgb + pixel_ptr + 4, bytestream2_get_ne16(&s->g)); /* rgbr */
  305. pixel_ptr += 6;
  306. }
  307. if (rle_code % 2 != 0){ /* not even raw value */
  308. AV_WN16(rgb + pixel_ptr, bytestream2_get_ne16(&s->g));
  309. rgb[pixel_ptr + 2] = bytestream2_get_byte(&s->g);
  310. pixel_ptr += 3;
  311. }
  312. }
  313. }
  314. row_ptr += row_inc;
  315. }
  316. }
  317. static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  318. {
  319. int rle_code, rle_code_half;
  320. int pixel_ptr;
  321. int row_inc = s->frame->linesize[0];
  322. unsigned int argb;
  323. uint8_t *rgb = s->frame->data[0];
  324. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  325. while (lines_to_change--) {
  326. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
  327. CHECK_PIXEL_PTR(0);
  328. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  329. if (bytestream2_get_bytes_left(&s->g) < 1)
  330. return;
  331. if (rle_code == 0) {
  332. /* there's another skip code in the stream */
  333. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
  334. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  335. } else if (rle_code < 0) {
  336. /* decode the run length code */
  337. rle_code = -rle_code;
  338. argb = bytestream2_get_ne32(&s->g);
  339. CHECK_PIXEL_PTR(rle_code * 4);
  340. while (rle_code--) {
  341. AV_WN32A(rgb + pixel_ptr, argb);
  342. pixel_ptr += 4;
  343. }
  344. } else {
  345. CHECK_PIXEL_PTR(rle_code * 4);
  346. /* copy pixels directly to output */
  347. rle_code_half = rle_code / 2;
  348. while (rle_code_half--) { /* copy 2 argb raw value at the same time */
  349. AV_WN64(rgb + pixel_ptr, bytestream2_get_ne64(&s->g));
  350. pixel_ptr += 8;
  351. }
  352. if (rle_code % 2 != 0){ /* not even raw value */
  353. AV_WN32A(rgb + pixel_ptr, bytestream2_get_ne32(&s->g));
  354. pixel_ptr += 4;
  355. }
  356. }
  357. }
  358. row_ptr += row_inc;
  359. }
  360. }
  361. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  362. {
  363. QtrleContext *s = avctx->priv_data;
  364. s->avctx = avctx;
  365. switch (avctx->bits_per_coded_sample) {
  366. case 1:
  367. case 2:
  368. case 4:
  369. case 8:
  370. case 33:
  371. case 34:
  372. case 36:
  373. case 40:
  374. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  375. break;
  376. case 16:
  377. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  378. break;
  379. case 24:
  380. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  381. break;
  382. case 32:
  383. avctx->pix_fmt = AV_PIX_FMT_ARGB;
  384. break;
  385. default:
  386. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  387. avctx->bits_per_coded_sample);
  388. return AVERROR_INVALIDDATA;
  389. }
  390. s->frame = av_frame_alloc();
  391. if (!s->frame)
  392. return AVERROR(ENOMEM);
  393. return 0;
  394. }
  395. static int qtrle_decode_frame(AVCodecContext *avctx,
  396. void *data, int *got_frame,
  397. AVPacket *avpkt)
  398. {
  399. QtrleContext *s = avctx->priv_data;
  400. int header, start_line;
  401. int height, row_ptr;
  402. int has_palette = 0;
  403. int duplicate = 0;
  404. int ret, size;
  405. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  406. /* check if this frame is even supposed to change */
  407. if (avpkt->size < 8) {
  408. duplicate = 1;
  409. goto done;
  410. }
  411. /* start after the chunk size */
  412. size = bytestream2_get_be32(&s->g) & 0x3FFFFFFF;
  413. if (size - avpkt->size > size * (int64_t)avctx->discard_damaged_percentage / 100)
  414. return AVERROR_INVALIDDATA;
  415. /* fetch the header */
  416. header = bytestream2_get_be16(&s->g);
  417. /* if a header is present, fetch additional decoding parameters */
  418. if (header & 0x0008) {
  419. if (avpkt->size < 14) {
  420. duplicate = 1;
  421. goto done;
  422. }
  423. start_line = bytestream2_get_be16(&s->g);
  424. bytestream2_skip(&s->g, 2);
  425. height = bytestream2_get_be16(&s->g);
  426. bytestream2_skip(&s->g, 2);
  427. if (height > s->avctx->height - start_line) {
  428. duplicate = 1;
  429. goto done;
  430. }
  431. } else {
  432. start_line = 0;
  433. height = s->avctx->height;
  434. }
  435. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  436. return ret;
  437. row_ptr = s->frame->linesize[0] * start_line;
  438. switch (avctx->bits_per_coded_sample) {
  439. case 1:
  440. case 33:
  441. qtrle_decode_1bpp(s, row_ptr, height);
  442. has_palette = 1;
  443. break;
  444. case 2:
  445. case 34:
  446. qtrle_decode_2n4bpp(s, row_ptr, height, 2);
  447. has_palette = 1;
  448. break;
  449. case 4:
  450. case 36:
  451. qtrle_decode_2n4bpp(s, row_ptr, height, 4);
  452. has_palette = 1;
  453. break;
  454. case 8:
  455. case 40:
  456. qtrle_decode_8bpp(s, row_ptr, height);
  457. has_palette = 1;
  458. break;
  459. case 16:
  460. qtrle_decode_16bpp(s, row_ptr, height);
  461. break;
  462. case 24:
  463. qtrle_decode_24bpp(s, row_ptr, height);
  464. break;
  465. case 32:
  466. qtrle_decode_32bpp(s, row_ptr, height);
  467. break;
  468. default:
  469. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  470. avctx->bits_per_coded_sample);
  471. break;
  472. }
  473. if(has_palette) {
  474. int size;
  475. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
  476. if (pal && size == AVPALETTE_SIZE) {
  477. s->frame->palette_has_changed = 1;
  478. memcpy(s->pal, pal, AVPALETTE_SIZE);
  479. } else if (pal) {
  480. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
  481. }
  482. /* make the palette available on the way out */
  483. memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
  484. }
  485. done:
  486. if (!s->frame->data[0])
  487. return AVERROR_INVALIDDATA;
  488. if (duplicate) {
  489. // ff_reget_buffer() isn't needed when frames don't change, so just update
  490. // frame props.
  491. ret = ff_decode_frame_props(avctx, s->frame);
  492. if (ret < 0)
  493. return ret;
  494. }
  495. if ((ret = av_frame_ref(data, s->frame)) < 0)
  496. return ret;
  497. *got_frame = 1;
  498. /* always report that the buffer was completely consumed */
  499. return avpkt->size;
  500. }
  501. static void qtrle_decode_flush(AVCodecContext *avctx)
  502. {
  503. QtrleContext *s = avctx->priv_data;
  504. av_frame_unref(s->frame);
  505. }
  506. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  507. {
  508. QtrleContext *s = avctx->priv_data;
  509. av_frame_free(&s->frame);
  510. return 0;
  511. }
  512. AVCodec ff_qtrle_decoder = {
  513. .name = "qtrle",
  514. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  515. .type = AVMEDIA_TYPE_VIDEO,
  516. .id = AV_CODEC_ID_QTRLE,
  517. .priv_data_size = sizeof(QtrleContext),
  518. .init = qtrle_decode_init,
  519. .close = qtrle_decode_end,
  520. .decode = qtrle_decode_frame,
  521. .flush = qtrle_decode_flush,
  522. .capabilities = AV_CODEC_CAP_DR1,
  523. };