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.

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