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.

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