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.

508 lines
16KB

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