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. #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. unsigned char pi0, pi1; /* 2 8-pixel values */
  56. unsigned char *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://ffmpeg.org/trac/ffmpeg/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 = (signed char)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. while (rle_code--)
  101. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  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. unsigned char pi[16]; /* 16 palette indices */
  112. unsigned char *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 = (signed char)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. for (i = 0; i < num_pixels; i++)
  135. rgb[pixel_ptr++] = pi[i];
  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. unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
  165. unsigned char *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 = (signed char)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. while (rle_code--) {
  196. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  197. }
  198. }
  199. }
  200. row_ptr += row_inc;
  201. }
  202. }
  203. static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  204. {
  205. int rle_code;
  206. int pixel_ptr;
  207. int row_inc = s->frame.linesize[0];
  208. unsigned short rgb16;
  209. unsigned char *rgb = s->frame.data[0];
  210. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  211. while (lines_to_change--) {
  212. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
  213. CHECK_PIXEL_PTR(0);
  214. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  215. if (rle_code == 0) {
  216. /* there's another skip code in the stream */
  217. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
  218. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  219. } else if (rle_code < 0) {
  220. /* decode the run length code */
  221. rle_code = -rle_code;
  222. rgb16 = bytestream2_get_be16(&s->g);
  223. CHECK_PIXEL_PTR(rle_code * 2);
  224. while (rle_code--) {
  225. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  226. pixel_ptr += 2;
  227. }
  228. } else {
  229. CHECK_PIXEL_PTR(rle_code * 2);
  230. /* copy pixels directly to output */
  231. while (rle_code--) {
  232. rgb16 = bytestream2_get_be16(&s->g);
  233. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  234. pixel_ptr += 2;
  235. }
  236. }
  237. }
  238. row_ptr += row_inc;
  239. }
  240. }
  241. static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  242. {
  243. int rle_code;
  244. int pixel_ptr;
  245. int row_inc = s->frame.linesize[0];
  246. unsigned char r, g, b;
  247. unsigned char *rgb = s->frame.data[0];
  248. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  249. while (lines_to_change--) {
  250. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
  251. CHECK_PIXEL_PTR(0);
  252. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  253. if (rle_code == 0) {
  254. /* there's another skip code in the stream */
  255. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
  256. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  257. } else if (rle_code < 0) {
  258. /* decode the run length code */
  259. rle_code = -rle_code;
  260. r = bytestream2_get_byte(&s->g);
  261. g = bytestream2_get_byte(&s->g);
  262. b = bytestream2_get_byte(&s->g);
  263. CHECK_PIXEL_PTR(rle_code * 3);
  264. while (rle_code--) {
  265. rgb[pixel_ptr++] = r;
  266. rgb[pixel_ptr++] = g;
  267. rgb[pixel_ptr++] = b;
  268. }
  269. } else {
  270. CHECK_PIXEL_PTR(rle_code * 3);
  271. /* copy pixels directly to output */
  272. while (rle_code--) {
  273. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  274. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  275. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  276. }
  277. }
  278. }
  279. row_ptr += row_inc;
  280. }
  281. }
  282. static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  283. {
  284. int rle_code;
  285. int pixel_ptr;
  286. int row_inc = s->frame.linesize[0];
  287. unsigned int argb;
  288. unsigned char *rgb = s->frame.data[0];
  289. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  290. while (lines_to_change--) {
  291. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
  292. CHECK_PIXEL_PTR(0);
  293. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  294. if (rle_code == 0) {
  295. /* there's another skip code in the stream */
  296. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
  297. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  298. } else if (rle_code < 0) {
  299. /* decode the run length code */
  300. rle_code = -rle_code;
  301. argb = bytestream2_get_be32(&s->g);
  302. CHECK_PIXEL_PTR(rle_code * 4);
  303. while (rle_code--) {
  304. AV_WN32A(rgb + pixel_ptr, argb);
  305. pixel_ptr += 4;
  306. }
  307. } else {
  308. CHECK_PIXEL_PTR(rle_code * 4);
  309. /* copy pixels directly to output */
  310. while (rle_code--) {
  311. argb = bytestream2_get_be32(&s->g);
  312. AV_WN32A(rgb + pixel_ptr, argb);
  313. pixel_ptr += 4;
  314. }
  315. }
  316. }
  317. row_ptr += row_inc;
  318. }
  319. }
  320. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  321. {
  322. QtrleContext *s = avctx->priv_data;
  323. s->avctx = avctx;
  324. switch (avctx->bits_per_coded_sample) {
  325. case 1:
  326. case 33:
  327. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  328. break;
  329. case 2:
  330. case 4:
  331. case 8:
  332. case 34:
  333. case 36:
  334. case 40:
  335. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  336. break;
  337. case 16:
  338. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  339. break;
  340. case 24:
  341. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  342. break;
  343. case 32:
  344. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  345. break;
  346. default:
  347. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  348. avctx->bits_per_coded_sample);
  349. return AVERROR_INVALIDDATA;
  350. }
  351. avcodec_get_frame_defaults(&s->frame);
  352. return 0;
  353. }
  354. static int qtrle_decode_frame(AVCodecContext *avctx,
  355. void *data, int *got_frame,
  356. AVPacket *avpkt)
  357. {
  358. QtrleContext *s = avctx->priv_data;
  359. int header, start_line;
  360. int height, row_ptr;
  361. int has_palette = 0;
  362. int ret;
  363. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  364. if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0) {
  365. av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  366. return ret;
  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. if ((ret = av_frame_ref(data, &s->frame)) < 0)
  435. return ret;
  436. *got_frame = 1;
  437. /* always report that the buffer was completely consumed */
  438. return avpkt->size;
  439. }
  440. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  441. {
  442. QtrleContext *s = avctx->priv_data;
  443. av_frame_unref(&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. };