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.

538 lines
15KB

  1. /*
  2. * Quicktime Animation (RLE) Video Decoder
  3. * Copyright (C) 2004 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file qtrle.c
  22. * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
  23. * For more information about the QT RLE format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. *
  26. * The QT RLE decoder has seven modes of operation:
  27. * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
  28. * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB24
  29. * data. 24-bit data is RGB888 and 32-bit data is RGBA32.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include "common.h"
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. typedef struct QtrleContext {
  39. AVCodecContext *avctx;
  40. DSPContext dsp;
  41. AVFrame frame;
  42. unsigned char *buf;
  43. int size;
  44. } QtrleContext;
  45. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  46. #define CHECK_STREAM_PTR(n) \
  47. if ((stream_ptr + n) > s->size) { \
  48. av_log (s->avctx, AV_LOG_INFO, "Problem: stream_ptr out of bounds (%d >= %d)\n", \
  49. stream_ptr + n, s->size); \
  50. return; \
  51. }
  52. #define CHECK_PIXEL_PTR(n) \
  53. if (pixel_ptr + n > pixel_limit) { \
  54. av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr >= pixel_limit (%d >= %d)\n", \
  55. pixel_ptr + n, pixel_limit); \
  56. return; \
  57. } \
  58. static void qtrle_decode_1bpp(QtrleContext *s)
  59. {
  60. }
  61. static void qtrle_decode_2bpp(QtrleContext *s)
  62. {
  63. }
  64. static void qtrle_decode_4bpp(QtrleContext *s)
  65. {
  66. }
  67. static void qtrle_decode_8bpp(QtrleContext *s)
  68. {
  69. int stream_ptr;
  70. int header;
  71. int start_line;
  72. int lines_to_change;
  73. int rle_code;
  74. int row_ptr, pixel_ptr;
  75. int row_inc = s->frame.linesize[0];
  76. unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
  77. unsigned char *rgb = s->frame.data[0];
  78. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  79. /* check if this frame is even supposed to change */
  80. if (s->size < 8)
  81. return;
  82. /* start after the chunk size */
  83. stream_ptr = 4;
  84. /* fetch the header */
  85. CHECK_STREAM_PTR(2);
  86. header = BE_16(&s->buf[stream_ptr]);
  87. stream_ptr += 2;
  88. /* if a header is present, fetch additional decoding parameters */
  89. if (header & 0x0008) {
  90. CHECK_STREAM_PTR(8);
  91. start_line = BE_16(&s->buf[stream_ptr]);
  92. stream_ptr += 4;
  93. lines_to_change = BE_16(&s->buf[stream_ptr]);
  94. stream_ptr += 4;
  95. } else {
  96. start_line = 0;
  97. lines_to_change = s->avctx->height;
  98. }
  99. row_ptr = row_inc * start_line;
  100. while (lines_to_change--) {
  101. CHECK_STREAM_PTR(2);
  102. pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
  103. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  104. if (rle_code == 0) {
  105. /* there's another skip code in the stream */
  106. CHECK_STREAM_PTR(1);
  107. pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
  108. } else if (rle_code < 0) {
  109. /* decode the run length code */
  110. rle_code = -rle_code;
  111. /* get the next 4 bytes from the stream, treat them as palette
  112. * indices, and output them rle_code times */
  113. CHECK_STREAM_PTR(4);
  114. pi1 = s->buf[stream_ptr++];
  115. pi2 = s->buf[stream_ptr++];
  116. pi3 = s->buf[stream_ptr++];
  117. pi4 = s->buf[stream_ptr++];
  118. CHECK_PIXEL_PTR(rle_code * 4);
  119. while (rle_code--) {
  120. rgb[pixel_ptr++] = pi1;
  121. rgb[pixel_ptr++] = pi2;
  122. rgb[pixel_ptr++] = pi3;
  123. rgb[pixel_ptr++] = pi4;
  124. }
  125. } else {
  126. /* copy the same pixel directly to output 4 times */
  127. rle_code *= 4;
  128. CHECK_STREAM_PTR(rle_code);
  129. CHECK_PIXEL_PTR(rle_code);
  130. while (rle_code--) {
  131. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  132. }
  133. }
  134. }
  135. row_ptr += row_inc;
  136. }
  137. }
  138. static void qtrle_decode_16bpp(QtrleContext *s)
  139. {
  140. int stream_ptr;
  141. int header;
  142. int start_line;
  143. int lines_to_change;
  144. signed char rle_code;
  145. int row_ptr, pixel_ptr;
  146. int row_inc = s->frame.linesize[0];
  147. unsigned short rgb16;
  148. unsigned char *rgb = s->frame.data[0];
  149. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  150. /* check if this frame is even supposed to change */
  151. if (s->size < 8)
  152. return;
  153. /* start after the chunk size */
  154. stream_ptr = 4;
  155. /* fetch the header */
  156. CHECK_STREAM_PTR(2);
  157. header = BE_16(&s->buf[stream_ptr]);
  158. stream_ptr += 2;
  159. /* if a header is present, fetch additional decoding parameters */
  160. if (header & 0x0008) {
  161. CHECK_STREAM_PTR(8);
  162. start_line = BE_16(&s->buf[stream_ptr]);
  163. stream_ptr += 4;
  164. lines_to_change = BE_16(&s->buf[stream_ptr]);
  165. stream_ptr += 4;
  166. } else {
  167. start_line = 0;
  168. lines_to_change = s->avctx->height;
  169. }
  170. row_ptr = row_inc * start_line;
  171. while (lines_to_change--) {
  172. CHECK_STREAM_PTR(2);
  173. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
  174. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  175. if (rle_code == 0) {
  176. /* there's another skip code in the stream */
  177. CHECK_STREAM_PTR(1);
  178. pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
  179. } else if (rle_code < 0) {
  180. /* decode the run length code */
  181. rle_code = -rle_code;
  182. CHECK_STREAM_PTR(2);
  183. rgb16 = BE_16(&s->buf[stream_ptr]);
  184. stream_ptr += 2;
  185. CHECK_PIXEL_PTR(rle_code * 2);
  186. while (rle_code--) {
  187. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  188. pixel_ptr += 2;
  189. }
  190. } else {
  191. CHECK_STREAM_PTR(rle_code * 2);
  192. CHECK_PIXEL_PTR(rle_code * 2);
  193. /* copy pixels directly to output */
  194. while (rle_code--) {
  195. rgb16 = BE_16(&s->buf[stream_ptr]);
  196. stream_ptr += 2;
  197. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  198. pixel_ptr += 2;
  199. }
  200. }
  201. }
  202. row_ptr += row_inc;
  203. }
  204. }
  205. static void qtrle_decode_24bpp(QtrleContext *s)
  206. {
  207. int stream_ptr;
  208. int header;
  209. int start_line;
  210. int lines_to_change;
  211. signed char rle_code;
  212. int row_ptr, pixel_ptr;
  213. int row_inc = s->frame.linesize[0];
  214. unsigned char r, g, b;
  215. unsigned char *rgb = s->frame.data[0];
  216. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  217. /* check if this frame is even supposed to change */
  218. if (s->size < 8)
  219. return;
  220. /* start after the chunk size */
  221. stream_ptr = 4;
  222. /* fetch the header */
  223. CHECK_STREAM_PTR(2);
  224. header = BE_16(&s->buf[stream_ptr]);
  225. stream_ptr += 2;
  226. /* if a header is present, fetch additional decoding parameters */
  227. if (header & 0x0008) {
  228. CHECK_STREAM_PTR(8);
  229. start_line = BE_16(&s->buf[stream_ptr]);
  230. stream_ptr += 4;
  231. lines_to_change = BE_16(&s->buf[stream_ptr]);
  232. stream_ptr += 4;
  233. } else {
  234. start_line = 0;
  235. lines_to_change = s->avctx->height;
  236. }
  237. row_ptr = row_inc * start_line;
  238. while (lines_to_change--) {
  239. CHECK_STREAM_PTR(2);
  240. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
  241. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  242. if (rle_code == 0) {
  243. /* there's another skip code in the stream */
  244. CHECK_STREAM_PTR(1);
  245. pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
  246. } else if (rle_code < 0) {
  247. /* decode the run length code */
  248. rle_code = -rle_code;
  249. CHECK_STREAM_PTR(3);
  250. r = s->buf[stream_ptr++];
  251. g = s->buf[stream_ptr++];
  252. b = s->buf[stream_ptr++];
  253. CHECK_PIXEL_PTR(rle_code * 3);
  254. while (rle_code--) {
  255. rgb[pixel_ptr++] = r;
  256. rgb[pixel_ptr++] = g;
  257. rgb[pixel_ptr++] = b;
  258. }
  259. } else {
  260. CHECK_STREAM_PTR(rle_code * 3);
  261. CHECK_PIXEL_PTR(rle_code * 3);
  262. /* copy pixels directly to output */
  263. while (rle_code--) {
  264. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  265. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  266. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  267. }
  268. }
  269. }
  270. row_ptr += row_inc;
  271. }
  272. }
  273. static void qtrle_decode_32bpp(QtrleContext *s)
  274. {
  275. int stream_ptr;
  276. int header;
  277. int start_line;
  278. int lines_to_change;
  279. signed char rle_code;
  280. int row_ptr, pixel_ptr;
  281. int row_inc = s->frame.linesize[0];
  282. unsigned char r, g, b;
  283. unsigned int argb;
  284. unsigned char *rgb = s->frame.data[0];
  285. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  286. /* check if this frame is even supposed to change */
  287. if (s->size < 8)
  288. return;
  289. /* start after the chunk size */
  290. stream_ptr = 4;
  291. /* fetch the header */
  292. CHECK_STREAM_PTR(2);
  293. header = BE_16(&s->buf[stream_ptr]);
  294. stream_ptr += 2;
  295. /* if a header is present, fetch additional decoding parameters */
  296. if (header & 0x0008) {
  297. CHECK_STREAM_PTR(8);
  298. start_line = BE_16(&s->buf[stream_ptr]);
  299. stream_ptr += 4;
  300. lines_to_change = BE_16(&s->buf[stream_ptr]);
  301. stream_ptr += 4;
  302. } else {
  303. start_line = 0;
  304. lines_to_change = s->avctx->height;
  305. }
  306. row_ptr = row_inc * start_line;
  307. while (lines_to_change--) {
  308. CHECK_STREAM_PTR(2);
  309. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
  310. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  311. if (rle_code == 0) {
  312. /* there's another skip code in the stream */
  313. CHECK_STREAM_PTR(1);
  314. pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
  315. } else if (rle_code < 0) {
  316. /* decode the run length code */
  317. rle_code = -rle_code;
  318. CHECK_STREAM_PTR(4);
  319. stream_ptr++; /* skip the alpha (?) byte */
  320. r = s->buf[stream_ptr++];
  321. g = s->buf[stream_ptr++];
  322. b = s->buf[stream_ptr++];
  323. argb = (r << 16) | (g << 8) | (b << 0);
  324. CHECK_PIXEL_PTR(rle_code * 4);
  325. while (rle_code--) {
  326. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  327. pixel_ptr += 4;
  328. }
  329. } else {
  330. CHECK_STREAM_PTR(rle_code * 4);
  331. CHECK_PIXEL_PTR(rle_code * 4);
  332. /* copy pixels directly to output */
  333. while (rle_code--) {
  334. stream_ptr++; /* skip the alpha (?) byte */
  335. r = s->buf[stream_ptr++];
  336. g = s->buf[stream_ptr++];
  337. b = s->buf[stream_ptr++];
  338. argb = (r << 16) | (g << 8) | (b << 0);
  339. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  340. pixel_ptr += 4;
  341. }
  342. }
  343. }
  344. row_ptr += row_inc;
  345. }
  346. }
  347. static int qtrle_decode_init(AVCodecContext *avctx)
  348. {
  349. QtrleContext *s = (QtrleContext *)avctx->priv_data;
  350. s->avctx = avctx;
  351. switch (avctx->bits_per_sample) {
  352. case 1:
  353. case 2:
  354. case 4:
  355. case 8:
  356. case 33:
  357. case 34:
  358. case 36:
  359. case 40:
  360. avctx->pix_fmt = PIX_FMT_PAL8;
  361. break;
  362. case 16:
  363. avctx->pix_fmt = PIX_FMT_RGB555;
  364. break;
  365. case 24:
  366. avctx->pix_fmt = PIX_FMT_RGB24;
  367. break;
  368. case 32:
  369. avctx->pix_fmt = PIX_FMT_RGBA32;
  370. break;
  371. default:
  372. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  373. avctx->bits_per_sample);
  374. break;
  375. }
  376. avctx->has_b_frames = 0;
  377. dsputil_init(&s->dsp, avctx);
  378. s->frame.data[0] = NULL;
  379. return 0;
  380. }
  381. static int qtrle_decode_frame(AVCodecContext *avctx,
  382. void *data, int *data_size,
  383. uint8_t *buf, int buf_size)
  384. {
  385. QtrleContext *s = (QtrleContext *)avctx->priv_data;
  386. /* no supplementary picture */
  387. if (buf_size == 0)
  388. return 0;
  389. s->buf = buf;
  390. s->size = buf_size;
  391. s->frame.reference = 1;
  392. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  393. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  394. if (avctx->reget_buffer(avctx, &s->frame)) {
  395. av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  396. return -1;
  397. }
  398. switch (avctx->bits_per_sample) {
  399. case 1:
  400. case 33:
  401. qtrle_decode_1bpp(s);
  402. break;
  403. case 2:
  404. case 34:
  405. qtrle_decode_2bpp(s);
  406. break;
  407. case 4:
  408. case 36:
  409. qtrle_decode_4bpp(s);
  410. break;
  411. case 8:
  412. case 40:
  413. qtrle_decode_8bpp(s);
  414. /* make the palette available on the way out */
  415. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  416. if (s->avctx->palctrl->palette_changed) {
  417. s->frame.palette_has_changed = 1;
  418. s->avctx->palctrl->palette_changed = 0;
  419. }
  420. break;
  421. case 16:
  422. qtrle_decode_16bpp(s);
  423. break;
  424. case 24:
  425. qtrle_decode_24bpp(s);
  426. break;
  427. case 32:
  428. qtrle_decode_32bpp(s);
  429. break;
  430. default:
  431. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  432. avctx->bits_per_sample);
  433. break;
  434. }
  435. *data_size = sizeof(AVFrame);
  436. *(AVFrame*)data = s->frame;
  437. /* always report that the buffer was completely consumed */
  438. return buf_size;
  439. }
  440. static int qtrle_decode_end(AVCodecContext *avctx)
  441. {
  442. QtrleContext *s = (QtrleContext *)avctx->priv_data;
  443. if (s->frame.data[0])
  444. avctx->release_buffer(avctx, &s->frame);
  445. return 0;
  446. }
  447. AVCodec qtrle_decoder = {
  448. "qtrle",
  449. CODEC_TYPE_VIDEO,
  450. CODEC_ID_QTRLE,
  451. sizeof(QtrleContext),
  452. qtrle_decode_init,
  453. NULL,
  454. qtrle_decode_end,
  455. qtrle_decode_frame,
  456. CODEC_CAP_DR1,
  457. };