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.

532 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;
  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. * current row. */
  66. row_ptr -= row_inc;
  67. pixel_ptr = row_ptr;
  68. lines_to_change++;
  69. while (lines_to_change) {
  70. skip = bytestream2_get_byte(&s->g);
  71. rle_code = (signed char)bytestream2_get_byte(&s->g);
  72. if (rle_code == 0)
  73. break;
  74. if(skip & 0x80) {
  75. lines_to_change--;
  76. row_ptr += row_inc;
  77. pixel_ptr = row_ptr + 2 * (skip & 0x7f);
  78. } else
  79. pixel_ptr += 2 * skip;
  80. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  81. if(rle_code == -1)
  82. continue;
  83. if (rle_code < 0) {
  84. /* decode the run length code */
  85. rle_code = -rle_code;
  86. /* get the next 2 bytes from the stream, treat them as groups
  87. * of 8 pixels, and output them rle_code times */
  88. pi0 = bytestream2_get_byte(&s->g);
  89. pi1 = bytestream2_get_byte(&s->g);
  90. CHECK_PIXEL_PTR(rle_code * 2);
  91. while (rle_code--) {
  92. rgb[pixel_ptr++] = pi0;
  93. rgb[pixel_ptr++] = pi1;
  94. }
  95. } else {
  96. /* copy the same pixel directly to output 2 times */
  97. rle_code *= 2;
  98. CHECK_PIXEL_PTR(rle_code);
  99. while (rle_code--)
  100. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  101. }
  102. }
  103. }
  104. static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
  105. int lines_to_change, int bpp)
  106. {
  107. int rle_code, i;
  108. int pixel_ptr;
  109. int row_inc = s->frame.linesize[0];
  110. unsigned char pi[16]; /* 16 palette indices */
  111. unsigned char *rgb = s->frame.data[0];
  112. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  113. int num_pixels = (bpp == 4) ? 8 : 16;
  114. while (lines_to_change--) {
  115. pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  116. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  117. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  118. if (rle_code == 0) {
  119. /* there's another skip code in the stream */
  120. pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  121. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  122. } else if (rle_code < 0) {
  123. /* decode the run length code */
  124. rle_code = -rle_code;
  125. /* get the next 4 bytes from the stream, treat them as palette
  126. * indexes, and output them rle_code times */
  127. for (i = num_pixels-1; i >= 0; i--) {
  128. pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
  129. bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
  130. }
  131. CHECK_PIXEL_PTR(rle_code * num_pixels);
  132. while (rle_code--) {
  133. for (i = 0; i < num_pixels; i++)
  134. rgb[pixel_ptr++] = pi[i];
  135. }
  136. } else {
  137. /* copy the same pixel directly to output 4 times */
  138. rle_code *= 4;
  139. CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
  140. while (rle_code--) {
  141. if(bpp == 4) {
  142. int x = bytestream2_get_byte(&s->g);
  143. rgb[pixel_ptr++] = (x >> 4) & 0x0f;
  144. rgb[pixel_ptr++] = x & 0x0f;
  145. } else {
  146. int x = bytestream2_get_byte(&s->g);
  147. rgb[pixel_ptr++] = (x >> 6) & 0x03;
  148. rgb[pixel_ptr++] = (x >> 4) & 0x03;
  149. rgb[pixel_ptr++] = (x >> 2) & 0x03;
  150. rgb[pixel_ptr++] = x & 0x03;
  151. }
  152. }
  153. }
  154. }
  155. row_ptr += row_inc;
  156. }
  157. }
  158. static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  159. {
  160. int rle_code;
  161. int pixel_ptr;
  162. int row_inc = s->frame.linesize[0];
  163. unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
  164. unsigned char *rgb = s->frame.data[0];
  165. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  166. while (lines_to_change--) {
  167. pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
  168. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  169. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  170. if (rle_code == 0) {
  171. /* there's another skip code in the stream */
  172. pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
  173. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  174. } else if (rle_code < 0) {
  175. /* decode the run length code */
  176. rle_code = -rle_code;
  177. /* get the next 4 bytes from the stream, treat them as palette
  178. * indexes, and output them rle_code times */
  179. pi1 = bytestream2_get_byte(&s->g);
  180. pi2 = bytestream2_get_byte(&s->g);
  181. pi3 = bytestream2_get_byte(&s->g);
  182. pi4 = bytestream2_get_byte(&s->g);
  183. CHECK_PIXEL_PTR(rle_code * 4);
  184. while (rle_code--) {
  185. rgb[pixel_ptr++] = pi1;
  186. rgb[pixel_ptr++] = pi2;
  187. rgb[pixel_ptr++] = pi3;
  188. rgb[pixel_ptr++] = pi4;
  189. }
  190. } else {
  191. /* copy the same pixel directly to output 4 times */
  192. rle_code *= 4;
  193. CHECK_PIXEL_PTR(rle_code);
  194. while (rle_code--) {
  195. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  196. }
  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. unsigned short rgb16;
  208. unsigned char *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); /* make sure pixel_ptr is positive */
  213. while ((rle_code = (signed char)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. *(unsigned short *)(&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. *(unsigned short *)(&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. unsigned char r, g, b;
  246. unsigned char *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); /* make sure pixel_ptr is positive */
  251. while ((rle_code = (signed char)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. unsigned char *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); /* make sure pixel_ptr is positive */
  292. while ((rle_code = (signed char)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. avcodec_get_frame_defaults(&s->frame);
  351. s->frame.data[0] = NULL;
  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. s->frame.reference = 3;
  365. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  366. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  367. if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  368. av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  369. return ret;
  370. }
  371. /* check if this frame is even supposed to change */
  372. if (avpkt->size < 8)
  373. goto done;
  374. /* start after the chunk size */
  375. bytestream2_seek(&s->g, 4, SEEK_SET);
  376. /* fetch the header */
  377. header = bytestream2_get_be16(&s->g);
  378. /* if a header is present, fetch additional decoding parameters */
  379. if (header & 0x0008) {
  380. if (avpkt->size < 14)
  381. goto done;
  382. start_line = bytestream2_get_be16(&s->g);
  383. bytestream2_skip(&s->g, 2);
  384. height = bytestream2_get_be16(&s->g);
  385. bytestream2_skip(&s->g, 2);
  386. if (height > s->avctx->height - start_line)
  387. goto done;
  388. } else {
  389. start_line = 0;
  390. height = s->avctx->height;
  391. }
  392. row_ptr = s->frame.linesize[0] * start_line;
  393. switch (avctx->bits_per_coded_sample) {
  394. case 1:
  395. case 33:
  396. qtrle_decode_1bpp(s, row_ptr, height);
  397. break;
  398. case 2:
  399. case 34:
  400. qtrle_decode_2n4bpp(s, row_ptr, height, 2);
  401. has_palette = 1;
  402. break;
  403. case 4:
  404. case 36:
  405. qtrle_decode_2n4bpp(s, row_ptr, height, 4);
  406. has_palette = 1;
  407. break;
  408. case 8:
  409. case 40:
  410. qtrle_decode_8bpp(s, row_ptr, height);
  411. has_palette = 1;
  412. break;
  413. case 16:
  414. qtrle_decode_16bpp(s, row_ptr, height);
  415. break;
  416. case 24:
  417. qtrle_decode_24bpp(s, row_ptr, height);
  418. break;
  419. case 32:
  420. qtrle_decode_32bpp(s, row_ptr, height);
  421. break;
  422. default:
  423. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  424. avctx->bits_per_coded_sample);
  425. break;
  426. }
  427. if(has_palette) {
  428. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  429. if (pal) {
  430. s->frame.palette_has_changed = 1;
  431. memcpy(s->pal, pal, AVPALETTE_SIZE);
  432. }
  433. /* make the palette available on the way out */
  434. memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
  435. }
  436. done:
  437. *got_frame = 1;
  438. *(AVFrame*)data = s->frame;
  439. /* always report that the buffer was completely consumed */
  440. return avpkt->size;
  441. }
  442. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  443. {
  444. QtrleContext *s = avctx->priv_data;
  445. if (s->frame.data[0])
  446. avctx->release_buffer(avctx, &s->frame);
  447. return 0;
  448. }
  449. AVCodec ff_qtrle_decoder = {
  450. .name = "qtrle",
  451. .type = AVMEDIA_TYPE_VIDEO,
  452. .id = AV_CODEC_ID_QTRLE,
  453. .priv_data_size = sizeof(QtrleContext),
  454. .init = qtrle_decode_init,
  455. .close = qtrle_decode_end,
  456. .decode = qtrle_decode_frame,
  457. .capabilities = CODEC_CAP_DR1,
  458. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  459. };