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.

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