qtrle.c 18 KB

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