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.

569 lines
18KB

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