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.

629 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. /**
  23. * @file qtrle.c
  24. * QT RLE Video Decoder by Mike Melanson (melanson@pcisys.net)
  25. * For more information about the QT RLE format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. *
  28. * The QT RLE decoder has seven modes of operation:
  29. * 1, 2, 4, 8, 16, 24, and 32 bits per pixel. For modes 1, 2, 4, and 8
  30. * the decoder outputs PAL8 colorspace data. 16-bit data yields RGB555
  31. * data. 24-bit data is RGB24 and 32-bit data is RGB32.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include "avcodec.h"
  38. #include "dsputil.h"
  39. typedef struct QtrleContext {
  40. AVCodecContext *avctx;
  41. DSPContext dsp;
  42. AVFrame frame;
  43. unsigned char *buf;
  44. int size;
  45. } QtrleContext;
  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) || (pixel_ptr + n < 0)) { \
  54. av_log (s->avctx, AV_LOG_INFO, "Problem: pixel_ptr = %d, pixel_limit = %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. int stream_ptr;
  67. int header;
  68. int start_line;
  69. int lines_to_change;
  70. int rle_code;
  71. int row_ptr, pixel_ptr;
  72. int row_inc = s->frame.linesize[0];
  73. unsigned char pi1, pi2, pi3, pi4, pi5, pi6, pi7, pi8; /* 8 palette indices */
  74. unsigned char *rgb = s->frame.data[0];
  75. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  76. /* check if this frame is even supposed to change */
  77. if (s->size < 8)
  78. return;
  79. /* start after the chunk size */
  80. stream_ptr = 4;
  81. /* fetch the header */
  82. CHECK_STREAM_PTR(2);
  83. header = AV_RB16(&s->buf[stream_ptr]);
  84. stream_ptr += 2;
  85. /* if a header is present, fetch additional decoding parameters */
  86. if (header & 0x0008) {
  87. CHECK_STREAM_PTR(8);
  88. start_line = AV_RB16(&s->buf[stream_ptr]);
  89. stream_ptr += 4;
  90. lines_to_change = AV_RB16(&s->buf[stream_ptr]);
  91. stream_ptr += 4;
  92. } else {
  93. start_line = 0;
  94. lines_to_change = s->avctx->height;
  95. }
  96. row_ptr = row_inc * start_line;
  97. while (lines_to_change--) {
  98. CHECK_STREAM_PTR(2);
  99. pixel_ptr = row_ptr + (8 * (s->buf[stream_ptr++] - 1));
  100. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  101. if (rle_code == 0) {
  102. /* there's another skip code in the stream */
  103. CHECK_STREAM_PTR(1);
  104. pixel_ptr += (8 * (s->buf[stream_ptr++] - 1));
  105. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  106. } else if (rle_code < 0) {
  107. /* decode the run length code */
  108. rle_code = -rle_code;
  109. /* get the next 4 bytes from the stream, treat them as palette
  110. * indices, and output them rle_code times */
  111. CHECK_STREAM_PTR(4);
  112. pi1 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
  113. pi2 = (s->buf[stream_ptr++]) & 0x0f;
  114. pi3 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
  115. pi4 = (s->buf[stream_ptr++]) & 0x0f;
  116. pi5 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
  117. pi6 = (s->buf[stream_ptr++]) & 0x0f;
  118. pi7 = ((s->buf[stream_ptr]) >> 4) & 0x0f;
  119. pi8 = (s->buf[stream_ptr++]) & 0x0f;
  120. CHECK_PIXEL_PTR(rle_code * 8);
  121. while (rle_code--) {
  122. rgb[pixel_ptr++] = pi1;
  123. rgb[pixel_ptr++] = pi2;
  124. rgb[pixel_ptr++] = pi3;
  125. rgb[pixel_ptr++] = pi4;
  126. rgb[pixel_ptr++] = pi5;
  127. rgb[pixel_ptr++] = pi6;
  128. rgb[pixel_ptr++] = pi7;
  129. rgb[pixel_ptr++] = pi8;
  130. }
  131. } else {
  132. /* copy the same pixel directly to output 4 times */
  133. rle_code *= 4;
  134. CHECK_STREAM_PTR(rle_code);
  135. CHECK_PIXEL_PTR(rle_code*2);
  136. while (rle_code--) {
  137. rgb[pixel_ptr++] = ((s->buf[stream_ptr]) >> 4) & 0x0f;
  138. rgb[pixel_ptr++] = (s->buf[stream_ptr++]) & 0x0f;
  139. }
  140. }
  141. }
  142. row_ptr += row_inc;
  143. }
  144. }
  145. static void qtrle_decode_8bpp(QtrleContext *s)
  146. {
  147. int stream_ptr;
  148. int header;
  149. int start_line;
  150. int lines_to_change;
  151. int rle_code;
  152. int row_ptr, pixel_ptr;
  153. int row_inc = s->frame.linesize[0];
  154. unsigned char pi1, pi2, pi3, pi4; /* 4 palette indices */
  155. unsigned char *rgb = s->frame.data[0];
  156. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  157. /* check if this frame is even supposed to change */
  158. if (s->size < 8)
  159. return;
  160. /* start after the chunk size */
  161. stream_ptr = 4;
  162. /* fetch the header */
  163. CHECK_STREAM_PTR(2);
  164. header = AV_RB16(&s->buf[stream_ptr]);
  165. stream_ptr += 2;
  166. /* if a header is present, fetch additional decoding parameters */
  167. if (header & 0x0008) {
  168. CHECK_STREAM_PTR(8);
  169. start_line = AV_RB16(&s->buf[stream_ptr]);
  170. stream_ptr += 4;
  171. lines_to_change = AV_RB16(&s->buf[stream_ptr]);
  172. stream_ptr += 4;
  173. } else {
  174. start_line = 0;
  175. lines_to_change = s->avctx->height;
  176. }
  177. row_ptr = row_inc * start_line;
  178. while (lines_to_change--) {
  179. CHECK_STREAM_PTR(2);
  180. pixel_ptr = row_ptr + (4 * (s->buf[stream_ptr++] - 1));
  181. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  182. if (rle_code == 0) {
  183. /* there's another skip code in the stream */
  184. CHECK_STREAM_PTR(1);
  185. pixel_ptr += (4 * (s->buf[stream_ptr++] - 1));
  186. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  187. } else if (rle_code < 0) {
  188. /* decode the run length code */
  189. rle_code = -rle_code;
  190. /* get the next 4 bytes from the stream, treat them as palette
  191. * indices, and output them rle_code times */
  192. CHECK_STREAM_PTR(4);
  193. pi1 = s->buf[stream_ptr++];
  194. pi2 = s->buf[stream_ptr++];
  195. pi3 = s->buf[stream_ptr++];
  196. pi4 = s->buf[stream_ptr++];
  197. CHECK_PIXEL_PTR(rle_code * 4);
  198. while (rle_code--) {
  199. rgb[pixel_ptr++] = pi1;
  200. rgb[pixel_ptr++] = pi2;
  201. rgb[pixel_ptr++] = pi3;
  202. rgb[pixel_ptr++] = pi4;
  203. }
  204. } else {
  205. /* copy the same pixel directly to output 4 times */
  206. rle_code *= 4;
  207. CHECK_STREAM_PTR(rle_code);
  208. CHECK_PIXEL_PTR(rle_code);
  209. while (rle_code--) {
  210. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  211. }
  212. }
  213. }
  214. row_ptr += row_inc;
  215. }
  216. }
  217. static void qtrle_decode_16bpp(QtrleContext *s)
  218. {
  219. int stream_ptr;
  220. int header;
  221. int start_line;
  222. int lines_to_change;
  223. int rle_code;
  224. int row_ptr, pixel_ptr;
  225. int row_inc = s->frame.linesize[0];
  226. unsigned short rgb16;
  227. unsigned char *rgb = s->frame.data[0];
  228. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  229. /* check if this frame is even supposed to change */
  230. if (s->size < 8)
  231. return;
  232. /* start after the chunk size */
  233. stream_ptr = 4;
  234. /* fetch the header */
  235. CHECK_STREAM_PTR(2);
  236. header = AV_RB16(&s->buf[stream_ptr]);
  237. stream_ptr += 2;
  238. /* if a header is present, fetch additional decoding parameters */
  239. if (header & 0x0008) {
  240. CHECK_STREAM_PTR(8);
  241. start_line = AV_RB16(&s->buf[stream_ptr]);
  242. stream_ptr += 4;
  243. lines_to_change = AV_RB16(&s->buf[stream_ptr]);
  244. stream_ptr += 4;
  245. } else {
  246. start_line = 0;
  247. lines_to_change = s->avctx->height;
  248. }
  249. row_ptr = row_inc * start_line;
  250. while (lines_to_change--) {
  251. CHECK_STREAM_PTR(2);
  252. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 2;
  253. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  254. if (rle_code == 0) {
  255. /* there's another skip code in the stream */
  256. CHECK_STREAM_PTR(1);
  257. pixel_ptr += (s->buf[stream_ptr++] - 1) * 2;
  258. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  259. } else if (rle_code < 0) {
  260. /* decode the run length code */
  261. rle_code = -rle_code;
  262. CHECK_STREAM_PTR(2);
  263. rgb16 = AV_RB16(&s->buf[stream_ptr]);
  264. stream_ptr += 2;
  265. CHECK_PIXEL_PTR(rle_code * 2);
  266. while (rle_code--) {
  267. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  268. pixel_ptr += 2;
  269. }
  270. } else {
  271. CHECK_STREAM_PTR(rle_code * 2);
  272. CHECK_PIXEL_PTR(rle_code * 2);
  273. /* copy pixels directly to output */
  274. while (rle_code--) {
  275. rgb16 = AV_RB16(&s->buf[stream_ptr]);
  276. stream_ptr += 2;
  277. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  278. pixel_ptr += 2;
  279. }
  280. }
  281. }
  282. row_ptr += row_inc;
  283. }
  284. }
  285. static void qtrle_decode_24bpp(QtrleContext *s)
  286. {
  287. int stream_ptr;
  288. int header;
  289. int start_line;
  290. int lines_to_change;
  291. int rle_code;
  292. int row_ptr, pixel_ptr;
  293. int row_inc = s->frame.linesize[0];
  294. unsigned char r, g, b;
  295. unsigned char *rgb = s->frame.data[0];
  296. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  297. /* check if this frame is even supposed to change */
  298. if (s->size < 8)
  299. return;
  300. /* start after the chunk size */
  301. stream_ptr = 4;
  302. /* fetch the header */
  303. CHECK_STREAM_PTR(2);
  304. header = AV_RB16(&s->buf[stream_ptr]);
  305. stream_ptr += 2;
  306. /* if a header is present, fetch additional decoding parameters */
  307. if (header & 0x0008) {
  308. CHECK_STREAM_PTR(8);
  309. start_line = AV_RB16(&s->buf[stream_ptr]);
  310. stream_ptr += 4;
  311. lines_to_change = AV_RB16(&s->buf[stream_ptr]);
  312. stream_ptr += 4;
  313. } else {
  314. start_line = 0;
  315. lines_to_change = s->avctx->height;
  316. }
  317. row_ptr = row_inc * start_line;
  318. while (lines_to_change--) {
  319. CHECK_STREAM_PTR(2);
  320. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 3;
  321. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  322. if (rle_code == 0) {
  323. /* there's another skip code in the stream */
  324. CHECK_STREAM_PTR(1);
  325. pixel_ptr += (s->buf[stream_ptr++] - 1) * 3;
  326. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  327. } else if (rle_code < 0) {
  328. /* decode the run length code */
  329. rle_code = -rle_code;
  330. CHECK_STREAM_PTR(3);
  331. r = s->buf[stream_ptr++];
  332. g = s->buf[stream_ptr++];
  333. b = s->buf[stream_ptr++];
  334. CHECK_PIXEL_PTR(rle_code * 3);
  335. while (rle_code--) {
  336. rgb[pixel_ptr++] = r;
  337. rgb[pixel_ptr++] = g;
  338. rgb[pixel_ptr++] = b;
  339. }
  340. } else {
  341. CHECK_STREAM_PTR(rle_code * 3);
  342. CHECK_PIXEL_PTR(rle_code * 3);
  343. /* copy pixels directly to output */
  344. while (rle_code--) {
  345. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  346. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  347. rgb[pixel_ptr++] = s->buf[stream_ptr++];
  348. }
  349. }
  350. }
  351. row_ptr += row_inc;
  352. }
  353. }
  354. static void qtrle_decode_32bpp(QtrleContext *s)
  355. {
  356. int stream_ptr;
  357. int header;
  358. int start_line;
  359. int lines_to_change;
  360. int rle_code;
  361. int row_ptr, pixel_ptr;
  362. int row_inc = s->frame.linesize[0];
  363. unsigned char a, r, g, b;
  364. unsigned int argb;
  365. unsigned char *rgb = s->frame.data[0];
  366. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  367. /* check if this frame is even supposed to change */
  368. if (s->size < 8)
  369. return;
  370. /* start after the chunk size */
  371. stream_ptr = 4;
  372. /* fetch the header */
  373. CHECK_STREAM_PTR(2);
  374. header = AV_RB16(&s->buf[stream_ptr]);
  375. stream_ptr += 2;
  376. /* if a header is present, fetch additional decoding parameters */
  377. if (header & 0x0008) {
  378. CHECK_STREAM_PTR(8);
  379. start_line = AV_RB16(&s->buf[stream_ptr]);
  380. stream_ptr += 4;
  381. lines_to_change = AV_RB16(&s->buf[stream_ptr]);
  382. stream_ptr += 4;
  383. } else {
  384. start_line = 0;
  385. lines_to_change = s->avctx->height;
  386. }
  387. row_ptr = row_inc * start_line;
  388. while (lines_to_change--) {
  389. CHECK_STREAM_PTR(2);
  390. pixel_ptr = row_ptr + (s->buf[stream_ptr++] - 1) * 4;
  391. while ((rle_code = (signed char)s->buf[stream_ptr++]) != -1) {
  392. if (rle_code == 0) {
  393. /* there's another skip code in the stream */
  394. CHECK_STREAM_PTR(1);
  395. pixel_ptr += (s->buf[stream_ptr++] - 1) * 4;
  396. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  397. } else if (rle_code < 0) {
  398. /* decode the run length code */
  399. rle_code = -rle_code;
  400. CHECK_STREAM_PTR(4);
  401. a = s->buf[stream_ptr++];
  402. r = s->buf[stream_ptr++];
  403. g = s->buf[stream_ptr++];
  404. b = s->buf[stream_ptr++];
  405. argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
  406. CHECK_PIXEL_PTR(rle_code * 4);
  407. while (rle_code--) {
  408. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  409. pixel_ptr += 4;
  410. }
  411. } else {
  412. CHECK_STREAM_PTR(rle_code * 4);
  413. CHECK_PIXEL_PTR(rle_code * 4);
  414. /* copy pixels directly to output */
  415. while (rle_code--) {
  416. a = s->buf[stream_ptr++];
  417. r = s->buf[stream_ptr++];
  418. g = s->buf[stream_ptr++];
  419. b = s->buf[stream_ptr++];
  420. argb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
  421. *(unsigned int *)(&rgb[pixel_ptr]) = argb;
  422. pixel_ptr += 4;
  423. }
  424. }
  425. }
  426. row_ptr += row_inc;
  427. }
  428. }
  429. static int qtrle_decode_init(AVCodecContext *avctx)
  430. {
  431. QtrleContext *s = avctx->priv_data;
  432. s->avctx = avctx;
  433. switch (avctx->bits_per_sample) {
  434. case 1:
  435. case 2:
  436. case 4:
  437. case 8:
  438. case 33:
  439. case 34:
  440. case 36:
  441. case 40:
  442. avctx->pix_fmt = PIX_FMT_PAL8;
  443. break;
  444. case 16:
  445. avctx->pix_fmt = PIX_FMT_RGB555;
  446. break;
  447. case 24:
  448. avctx->pix_fmt = PIX_FMT_RGB24;
  449. break;
  450. case 32:
  451. avctx->pix_fmt = PIX_FMT_RGB32;
  452. break;
  453. default:
  454. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  455. avctx->bits_per_sample);
  456. break;
  457. }
  458. dsputil_init(&s->dsp, avctx);
  459. s->frame.data[0] = NULL;
  460. return 0;
  461. }
  462. static int qtrle_decode_frame(AVCodecContext *avctx,
  463. void *data, int *data_size,
  464. uint8_t *buf, int buf_size)
  465. {
  466. QtrleContext *s = avctx->priv_data;
  467. s->buf = buf;
  468. s->size = buf_size;
  469. s->frame.reference = 1;
  470. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  471. FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
  472. if (avctx->reget_buffer(avctx, &s->frame)) {
  473. av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  474. return -1;
  475. }
  476. switch (avctx->bits_per_sample) {
  477. case 1:
  478. case 33:
  479. qtrle_decode_1bpp(s);
  480. break;
  481. case 2:
  482. case 34:
  483. qtrle_decode_2bpp(s);
  484. break;
  485. case 4:
  486. case 36:
  487. qtrle_decode_4bpp(s);
  488. /* make the palette available on the way out */
  489. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  490. if (s->avctx->palctrl->palette_changed) {
  491. s->frame.palette_has_changed = 1;
  492. s->avctx->palctrl->palette_changed = 0;
  493. }
  494. break;
  495. case 8:
  496. case 40:
  497. qtrle_decode_8bpp(s);
  498. /* make the palette available on the way out */
  499. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  500. if (s->avctx->palctrl->palette_changed) {
  501. s->frame.palette_has_changed = 1;
  502. s->avctx->palctrl->palette_changed = 0;
  503. }
  504. break;
  505. case 16:
  506. qtrle_decode_16bpp(s);
  507. break;
  508. case 24:
  509. qtrle_decode_24bpp(s);
  510. break;
  511. case 32:
  512. qtrle_decode_32bpp(s);
  513. break;
  514. default:
  515. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  516. avctx->bits_per_sample);
  517. break;
  518. }
  519. *data_size = sizeof(AVFrame);
  520. *(AVFrame*)data = s->frame;
  521. /* always report that the buffer was completely consumed */
  522. return buf_size;
  523. }
  524. static int qtrle_decode_end(AVCodecContext *avctx)
  525. {
  526. QtrleContext *s = avctx->priv_data;
  527. if (s->frame.data[0])
  528. avctx->release_buffer(avctx, &s->frame);
  529. return 0;
  530. }
  531. AVCodec qtrle_decoder = {
  532. "qtrle",
  533. CODEC_TYPE_VIDEO,
  534. CODEC_ID_QTRLE,
  535. sizeof(QtrleContext),
  536. qtrle_decode_init,
  537. NULL,
  538. qtrle_decode_end,
  539. qtrle_decode_frame,
  540. CODEC_CAP_DR1,
  541. };