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.

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