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.

541 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
  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 * (skip & 0x7f);
  79. } else
  80. pixel_ptr += 2 * 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);
  92. while (rle_code--) {
  93. rgb[pixel_ptr++] = pi0;
  94. rgb[pixel_ptr++] = pi1;
  95. }
  96. } else {
  97. /* copy the same pixel directly to output 2 times */
  98. rle_code *= 2;
  99. CHECK_PIXEL_PTR(rle_code);
  100. bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
  101. pixel_ptr += rle_code;
  102. }
  103. }
  104. }
  105. static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
  106. int lines_to_change, int bpp)
  107. {
  108. int rle_code, i;
  109. int pixel_ptr;
  110. int row_inc = s->frame->linesize[0];
  111. uint8_t pi[16]; /* 16 palette indices */
  112. uint8_t *rgb = s->frame->data[0];
  113. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  114. int num_pixels = (bpp == 4) ? 8 : 16;
  115. while (lines_to_change--) {
  116. pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  117. CHECK_PIXEL_PTR(0);
  118. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  119. if (bytestream2_get_bytes_left(&s->g) < 1)
  120. return;
  121. if (rle_code == 0) {
  122. /* there's another skip code in the stream */
  123. pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  124. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  125. } else if (rle_code < 0) {
  126. /* decode the run length code */
  127. rle_code = -rle_code;
  128. /* get the next 4 bytes from the stream, treat them as palette
  129. * indexes, and output them rle_code times */
  130. for (i = num_pixels-1; i >= 0; i--) {
  131. pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
  132. bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
  133. }
  134. CHECK_PIXEL_PTR(rle_code * num_pixels);
  135. while (rle_code--) {
  136. memcpy(&rgb[pixel_ptr], &pi, num_pixels);
  137. pixel_ptr += num_pixels;
  138. }
  139. } else {
  140. /* copy the same pixel directly to output 4 times */
  141. rle_code *= 4;
  142. CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
  143. while (rle_code--) {
  144. if(bpp == 4) {
  145. int x = bytestream2_get_byte(&s->g);
  146. rgb[pixel_ptr++] = (x >> 4) & 0x0f;
  147. rgb[pixel_ptr++] = x & 0x0f;
  148. } else {
  149. int x = bytestream2_get_byte(&s->g);
  150. rgb[pixel_ptr++] = (x >> 6) & 0x03;
  151. rgb[pixel_ptr++] = (x >> 4) & 0x03;
  152. rgb[pixel_ptr++] = (x >> 2) & 0x03;
  153. rgb[pixel_ptr++] = x & 0x03;
  154. }
  155. }
  156. }
  157. }
  158. row_ptr += row_inc;
  159. }
  160. }
  161. static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  162. {
  163. int rle_code;
  164. int pixel_ptr;
  165. int row_inc = s->frame->linesize[0];
  166. uint8_t pi1, pi2, pi3, pi4; /* 4 palette indexes */
  167. uint8_t *rgb = s->frame->data[0];
  168. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  169. while (lines_to_change--) {
  170. pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
  171. CHECK_PIXEL_PTR(0);
  172. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  173. if (bytestream2_get_bytes_left(&s->g) < 1)
  174. return;
  175. if (rle_code == 0) {
  176. /* there's another skip code in the stream */
  177. pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
  178. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  179. } else if (rle_code < 0) {
  180. /* decode the run length code */
  181. rle_code = -rle_code;
  182. /* get the next 4 bytes from the stream, treat them as palette
  183. * indexes, and output them rle_code times */
  184. pi1 = bytestream2_get_byte(&s->g);
  185. pi2 = bytestream2_get_byte(&s->g);
  186. pi3 = bytestream2_get_byte(&s->g);
  187. pi4 = bytestream2_get_byte(&s->g);
  188. CHECK_PIXEL_PTR(rle_code * 4);
  189. while (rle_code--) {
  190. rgb[pixel_ptr++] = pi1;
  191. rgb[pixel_ptr++] = pi2;
  192. rgb[pixel_ptr++] = pi3;
  193. rgb[pixel_ptr++] = pi4;
  194. }
  195. } else {
  196. /* copy the same pixel directly to output 4 times */
  197. rle_code *= 4;
  198. CHECK_PIXEL_PTR(rle_code);
  199. bytestream2_get_buffer(&s->g, &rgb[pixel_ptr], rle_code);
  200. pixel_ptr += rle_code;
  201. }
  202. }
  203. row_ptr += row_inc;
  204. }
  205. }
  206. static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  207. {
  208. int rle_code;
  209. int pixel_ptr;
  210. int row_inc = s->frame->linesize[0];
  211. uint16_t rgb16;
  212. uint8_t *rgb = s->frame->data[0];
  213. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  214. while (lines_to_change--) {
  215. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
  216. CHECK_PIXEL_PTR(0);
  217. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  218. if (bytestream2_get_bytes_left(&s->g) < 1)
  219. return;
  220. if (rle_code == 0) {
  221. /* there's another skip code in the stream */
  222. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
  223. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  224. } else if (rle_code < 0) {
  225. /* decode the run length code */
  226. rle_code = -rle_code;
  227. rgb16 = bytestream2_get_be16(&s->g);
  228. CHECK_PIXEL_PTR(rle_code * 2);
  229. while (rle_code--) {
  230. *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
  231. pixel_ptr += 2;
  232. }
  233. } else {
  234. CHECK_PIXEL_PTR(rle_code * 2);
  235. /* copy pixels directly to output */
  236. while (rle_code--) {
  237. rgb16 = bytestream2_get_be16(&s->g);
  238. *(uint16_t *)(&rgb[pixel_ptr]) = rgb16;
  239. pixel_ptr += 2;
  240. }
  241. }
  242. }
  243. row_ptr += row_inc;
  244. }
  245. }
  246. static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  247. {
  248. int rle_code;
  249. int pixel_ptr;
  250. int row_inc = s->frame->linesize[0];
  251. uint8_t r, g, b;
  252. uint8_t *rgb = s->frame->data[0];
  253. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  254. while (lines_to_change--) {
  255. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
  256. CHECK_PIXEL_PTR(0);
  257. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  258. if (bytestream2_get_bytes_left(&s->g) < 1)
  259. return;
  260. if (rle_code == 0) {
  261. /* there's another skip code in the stream */
  262. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
  263. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  264. } else if (rle_code < 0) {
  265. /* decode the run length code */
  266. rle_code = -rle_code;
  267. r = bytestream2_get_byte(&s->g);
  268. g = bytestream2_get_byte(&s->g);
  269. b = bytestream2_get_byte(&s->g);
  270. CHECK_PIXEL_PTR(rle_code * 3);
  271. while (rle_code--) {
  272. rgb[pixel_ptr++] = r;
  273. rgb[pixel_ptr++] = g;
  274. rgb[pixel_ptr++] = b;
  275. }
  276. } else {
  277. CHECK_PIXEL_PTR(rle_code * 3);
  278. /* copy pixels directly to output */
  279. while (rle_code--) {
  280. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  281. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  282. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  283. }
  284. }
  285. }
  286. row_ptr += row_inc;
  287. }
  288. }
  289. static void qtrle_decode_32bpp(QtrleContext *s, 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 int argb;
  295. uint8_t *rgb = s->frame->data[0];
  296. int pixel_limit = s->frame->linesize[0] * s->avctx->height;
  297. while (lines_to_change--) {
  298. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
  299. CHECK_PIXEL_PTR(0);
  300. while ((rle_code = (int8_t)bytestream2_get_byte(&s->g)) != -1) {
  301. if (bytestream2_get_bytes_left(&s->g) < 1)
  302. return;
  303. if (rle_code == 0) {
  304. /* there's another skip code in the stream */
  305. pixel_ptr += (bytestream2_get_byte(&s->g) - 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. argb = bytestream2_get_be32(&s->g);
  311. CHECK_PIXEL_PTR(rle_code * 4);
  312. while (rle_code--) {
  313. AV_WN32A(rgb + pixel_ptr, argb);
  314. pixel_ptr += 4;
  315. }
  316. } else {
  317. CHECK_PIXEL_PTR(rle_code * 4);
  318. /* copy pixels directly to output */
  319. while (rle_code--) {
  320. argb = bytestream2_get_be32(&s->g);
  321. AV_WN32A(rgb + pixel_ptr, argb);
  322. pixel_ptr += 4;
  323. }
  324. }
  325. }
  326. row_ptr += row_inc;
  327. }
  328. }
  329. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  330. {
  331. QtrleContext *s = avctx->priv_data;
  332. s->avctx = avctx;
  333. switch (avctx->bits_per_coded_sample) {
  334. case 1:
  335. case 33:
  336. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  337. break;
  338. case 2:
  339. case 4:
  340. case 8:
  341. case 34:
  342. case 36:
  343. case 40:
  344. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  345. break;
  346. case 16:
  347. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  348. break;
  349. case 24:
  350. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  351. break;
  352. case 32:
  353. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  354. break;
  355. default:
  356. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  357. avctx->bits_per_coded_sample);
  358. return AVERROR_INVALIDDATA;
  359. }
  360. s->frame = av_frame_alloc();
  361. if (!s->frame)
  362. return AVERROR(ENOMEM);
  363. return 0;
  364. }
  365. static int qtrle_decode_frame(AVCodecContext *avctx,
  366. void *data, int *got_frame,
  367. AVPacket *avpkt)
  368. {
  369. QtrleContext *s = avctx->priv_data;
  370. int header, start_line;
  371. int height, row_ptr;
  372. int has_palette = 0;
  373. int ret;
  374. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  375. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  376. return ret;
  377. /* check if this frame is even supposed to change */
  378. if (avpkt->size < 8)
  379. goto done;
  380. /* start after the chunk size */
  381. bytestream2_seek(&s->g, 4, SEEK_SET);
  382. /* fetch the header */
  383. header = bytestream2_get_be16(&s->g);
  384. /* if a header is present, fetch additional decoding parameters */
  385. if (header & 0x0008) {
  386. if (avpkt->size < 14)
  387. goto done;
  388. start_line = bytestream2_get_be16(&s->g);
  389. bytestream2_skip(&s->g, 2);
  390. height = bytestream2_get_be16(&s->g);
  391. bytestream2_skip(&s->g, 2);
  392. if (height > s->avctx->height - start_line)
  393. goto done;
  394. } else {
  395. start_line = 0;
  396. height = s->avctx->height;
  397. }
  398. row_ptr = s->frame->linesize[0] * start_line;
  399. switch (avctx->bits_per_coded_sample) {
  400. case 1:
  401. case 33:
  402. qtrle_decode_1bpp(s, row_ptr, height);
  403. break;
  404. case 2:
  405. case 34:
  406. qtrle_decode_2n4bpp(s, row_ptr, height, 2);
  407. has_palette = 1;
  408. break;
  409. case 4:
  410. case 36:
  411. qtrle_decode_2n4bpp(s, row_ptr, height, 4);
  412. has_palette = 1;
  413. break;
  414. case 8:
  415. case 40:
  416. qtrle_decode_8bpp(s, row_ptr, height);
  417. has_palette = 1;
  418. break;
  419. case 16:
  420. qtrle_decode_16bpp(s, row_ptr, height);
  421. break;
  422. case 24:
  423. qtrle_decode_24bpp(s, row_ptr, height);
  424. break;
  425. case 32:
  426. qtrle_decode_32bpp(s, row_ptr, height);
  427. break;
  428. default:
  429. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  430. avctx->bits_per_coded_sample);
  431. break;
  432. }
  433. if(has_palette) {
  434. int size;
  435. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size);
  436. if (pal && size == AVPALETTE_SIZE) {
  437. s->frame->palette_has_changed = 1;
  438. memcpy(s->pal, pal, AVPALETTE_SIZE);
  439. } else if (pal) {
  440. av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size);
  441. }
  442. /* make the palette available on the way out */
  443. memcpy(s->frame->data[1], s->pal, AVPALETTE_SIZE);
  444. }
  445. done:
  446. if ((ret = av_frame_ref(data, s->frame)) < 0)
  447. return ret;
  448. *got_frame = 1;
  449. /* always report that the buffer was completely consumed */
  450. return avpkt->size;
  451. }
  452. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  453. {
  454. QtrleContext *s = avctx->priv_data;
  455. av_frame_free(&s->frame);
  456. return 0;
  457. }
  458. AVCodec ff_qtrle_decoder = {
  459. .name = "qtrle",
  460. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  461. .type = AVMEDIA_TYPE_VIDEO,
  462. .id = AV_CODEC_ID_QTRLE,
  463. .priv_data_size = sizeof(QtrleContext),
  464. .init = qtrle_decode_init,
  465. .close = qtrle_decode_end,
  466. .decode = qtrle_decode_frame,
  467. .capabilities = AV_CODEC_CAP_DR1,
  468. };