qtrle.c 18 KB

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