qtrle.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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
  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 "avcodec.h"
  36. #include "bytestream.h"
  37. #include "internal.h"
  38. typedef struct QtrleContext {
  39. AVCodecContext *avctx;
  40. AVFrame frame;
  41. GetByteContext g;
  42. uint32_t pal[256];
  43. } QtrleContext;
  44. #define CHECK_PIXEL_PTR(n) \
  45. if ((pixel_ptr + n > pixel_limit) || (pixel_ptr + n < 0)) { \
  46. av_log (s->avctx, AV_LOG_ERROR, "Problem: pixel_ptr = %d, pixel_limit = %d\n",\
  47. pixel_ptr + n, pixel_limit); \
  48. return; \
  49. } \
  50. static void qtrle_decode_1bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  51. {
  52. int rle_code;
  53. int pixel_ptr;
  54. int row_inc = s->frame.linesize[0];
  55. unsigned char pi0, pi1; /* 2 8-pixel values */
  56. unsigned char *rgb = s->frame.data[0];
  57. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  58. int skip;
  59. /* skip & 0x80 appears to mean 'start a new line', which can be interpreted
  60. * as 'go to next line' during the decoding of a frame but is 'go to first
  61. * line' at the beginning. Since we always interpret it as 'go to next line'
  62. * in the decoding loop (which makes code simpler/faster), the first line
  63. * would not be counted, so we count one more.
  64. * See: https://trac.ffmpeg.org/ticket/226
  65. * In the following decoding loop, row_ptr will be the position of the
  66. * current row. */
  67. row_ptr -= row_inc;
  68. pixel_ptr = row_ptr;
  69. lines_to_change++;
  70. while (lines_to_change) {
  71. skip = bytestream2_get_byte(&s->g);
  72. rle_code = (signed char)bytestream2_get_byte(&s->g);
  73. if (rle_code == 0)
  74. break;
  75. if(skip & 0x80) {
  76. lines_to_change--;
  77. row_ptr += row_inc;
  78. pixel_ptr = row_ptr + 2 * (skip & 0x7f);
  79. } else
  80. pixel_ptr += 2 * skip;
  81. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  82. if(rle_code == -1)
  83. continue;
  84. if (rle_code < 0) {
  85. /* decode the run length code */
  86. rle_code = -rle_code;
  87. /* get the next 2 bytes from the stream, treat them as groups
  88. * of 8 pixels, and output them rle_code times */
  89. pi0 = bytestream2_get_byte(&s->g);
  90. pi1 = bytestream2_get_byte(&s->g);
  91. CHECK_PIXEL_PTR(rle_code * 2);
  92. while (rle_code--) {
  93. rgb[pixel_ptr++] = pi0;
  94. rgb[pixel_ptr++] = pi1;
  95. }
  96. } else {
  97. /* copy the same pixel directly to output 2 times */
  98. rle_code *= 2;
  99. CHECK_PIXEL_PTR(rle_code);
  100. while (rle_code--)
  101. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  102. }
  103. }
  104. }
  105. static inline void qtrle_decode_2n4bpp(QtrleContext *s, int row_ptr,
  106. int lines_to_change, int bpp)
  107. {
  108. int rle_code, i;
  109. int pixel_ptr;
  110. int row_inc = s->frame.linesize[0];
  111. unsigned char pi[16]; /* 16 palette indices */
  112. unsigned char *rgb = s->frame.data[0];
  113. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  114. int num_pixels = (bpp == 4) ? 8 : 16;
  115. while (lines_to_change--) {
  116. pixel_ptr = row_ptr + (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  117. CHECK_PIXEL_PTR(0);
  118. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  119. if (rle_code == 0) {
  120. /* there's another skip code in the stream */
  121. pixel_ptr += (num_pixels * (bytestream2_get_byte(&s->g) - 1));
  122. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  123. } else if (rle_code < 0) {
  124. /* decode the run length code */
  125. rle_code = -rle_code;
  126. /* get the next 4 bytes from the stream, treat them as palette
  127. * indexes, and output them rle_code times */
  128. for (i = num_pixels-1; i >= 0; i--) {
  129. pi[num_pixels-1-i] = (bytestream2_peek_byte(&s->g) >> ((i*bpp) & 0x07)) & ((1<<bpp)-1);
  130. bytestream2_skip(&s->g, ((i & ((num_pixels>>2)-1)) == 0));
  131. }
  132. CHECK_PIXEL_PTR(rle_code * num_pixels);
  133. while (rle_code--) {
  134. for (i = 0; i < num_pixels; i++)
  135. rgb[pixel_ptr++] = pi[i];
  136. }
  137. } else {
  138. /* copy the same pixel directly to output 4 times */
  139. rle_code *= 4;
  140. CHECK_PIXEL_PTR(rle_code*(num_pixels>>2));
  141. while (rle_code--) {
  142. if(bpp == 4) {
  143. int x = bytestream2_get_byte(&s->g);
  144. rgb[pixel_ptr++] = (x >> 4) & 0x0f;
  145. rgb[pixel_ptr++] = x & 0x0f;
  146. } else {
  147. int x = bytestream2_get_byte(&s->g);
  148. rgb[pixel_ptr++] = (x >> 6) & 0x03;
  149. rgb[pixel_ptr++] = (x >> 4) & 0x03;
  150. rgb[pixel_ptr++] = (x >> 2) & 0x03;
  151. rgb[pixel_ptr++] = x & 0x03;
  152. }
  153. }
  154. }
  155. }
  156. row_ptr += row_inc;
  157. }
  158. }
  159. static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  160. {
  161. int rle_code;
  162. int pixel_ptr;
  163. int row_inc = s->frame.linesize[0];
  164. unsigned char pi1, pi2, pi3, pi4; /* 4 palette indexes */
  165. unsigned char *rgb = s->frame.data[0];
  166. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  167. while (lines_to_change--) {
  168. pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
  169. CHECK_PIXEL_PTR(0);
  170. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  171. if (rle_code == 0) {
  172. /* there's another skip code in the stream */
  173. pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 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. pi1 = bytestream2_get_byte(&s->g);
  181. pi2 = bytestream2_get_byte(&s->g);
  182. pi3 = bytestream2_get_byte(&s->g);
  183. pi4 = bytestream2_get_byte(&s->g);
  184. CHECK_PIXEL_PTR(rle_code * 4);
  185. while (rle_code--) {
  186. rgb[pixel_ptr++] = pi1;
  187. rgb[pixel_ptr++] = pi2;
  188. rgb[pixel_ptr++] = pi3;
  189. rgb[pixel_ptr++] = pi4;
  190. }
  191. } else {
  192. /* copy the same pixel directly to output 4 times */
  193. rle_code *= 4;
  194. CHECK_PIXEL_PTR(rle_code);
  195. while (rle_code--) {
  196. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  197. }
  198. }
  199. }
  200. row_ptr += row_inc;
  201. }
  202. }
  203. static void qtrle_decode_16bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  204. {
  205. int rle_code;
  206. int pixel_ptr;
  207. int row_inc = s->frame.linesize[0];
  208. unsigned short rgb16;
  209. unsigned char *rgb = s->frame.data[0];
  210. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  211. while (lines_to_change--) {
  212. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 2;
  213. CHECK_PIXEL_PTR(0);
  214. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  215. if (rle_code == 0) {
  216. /* there's another skip code in the stream */
  217. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 2;
  218. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  219. } else if (rle_code < 0) {
  220. /* decode the run length code */
  221. rle_code = -rle_code;
  222. rgb16 = bytestream2_get_be16(&s->g);
  223. CHECK_PIXEL_PTR(rle_code * 2);
  224. while (rle_code--) {
  225. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  226. pixel_ptr += 2;
  227. }
  228. } else {
  229. CHECK_PIXEL_PTR(rle_code * 2);
  230. /* copy pixels directly to output */
  231. while (rle_code--) {
  232. rgb16 = bytestream2_get_be16(&s->g);
  233. *(unsigned short *)(&rgb[pixel_ptr]) = rgb16;
  234. pixel_ptr += 2;
  235. }
  236. }
  237. }
  238. row_ptr += row_inc;
  239. }
  240. }
  241. static void qtrle_decode_24bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  242. {
  243. int rle_code;
  244. int pixel_ptr;
  245. int row_inc = s->frame.linesize[0];
  246. unsigned char r, g, b;
  247. unsigned char *rgb = s->frame.data[0];
  248. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  249. while (lines_to_change--) {
  250. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 3;
  251. CHECK_PIXEL_PTR(0);
  252. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  253. if (rle_code == 0) {
  254. /* there's another skip code in the stream */
  255. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 3;
  256. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  257. } else if (rle_code < 0) {
  258. /* decode the run length code */
  259. rle_code = -rle_code;
  260. r = bytestream2_get_byte(&s->g);
  261. g = bytestream2_get_byte(&s->g);
  262. b = bytestream2_get_byte(&s->g);
  263. CHECK_PIXEL_PTR(rle_code * 3);
  264. while (rle_code--) {
  265. rgb[pixel_ptr++] = r;
  266. rgb[pixel_ptr++] = g;
  267. rgb[pixel_ptr++] = b;
  268. }
  269. } else {
  270. CHECK_PIXEL_PTR(rle_code * 3);
  271. /* copy pixels directly to output */
  272. while (rle_code--) {
  273. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  274. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  275. rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
  276. }
  277. }
  278. }
  279. row_ptr += row_inc;
  280. }
  281. }
  282. static void qtrle_decode_32bpp(QtrleContext *s, int row_ptr, int lines_to_change)
  283. {
  284. int rle_code;
  285. int pixel_ptr;
  286. int row_inc = s->frame.linesize[0];
  287. unsigned int argb;
  288. unsigned char *rgb = s->frame.data[0];
  289. int pixel_limit = s->frame.linesize[0] * s->avctx->height;
  290. while (lines_to_change--) {
  291. pixel_ptr = row_ptr + (bytestream2_get_byte(&s->g) - 1) * 4;
  292. CHECK_PIXEL_PTR(0);
  293. while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
  294. if (rle_code == 0) {
  295. /* there's another skip code in the stream */
  296. pixel_ptr += (bytestream2_get_byte(&s->g) - 1) * 4;
  297. CHECK_PIXEL_PTR(0); /* make sure pixel_ptr is positive */
  298. } else if (rle_code < 0) {
  299. /* decode the run length code */
  300. rle_code = -rle_code;
  301. argb = bytestream2_get_be32(&s->g);
  302. CHECK_PIXEL_PTR(rle_code * 4);
  303. while (rle_code--) {
  304. AV_WN32A(rgb + pixel_ptr, argb);
  305. pixel_ptr += 4;
  306. }
  307. } else {
  308. CHECK_PIXEL_PTR(rle_code * 4);
  309. /* copy pixels directly to output */
  310. while (rle_code--) {
  311. argb = bytestream2_get_be32(&s->g);
  312. AV_WN32A(rgb + pixel_ptr, argb);
  313. pixel_ptr += 4;
  314. }
  315. }
  316. }
  317. row_ptr += row_inc;
  318. }
  319. }
  320. static av_cold int qtrle_decode_init(AVCodecContext *avctx)
  321. {
  322. QtrleContext *s = avctx->priv_data;
  323. s->avctx = avctx;
  324. switch (avctx->bits_per_coded_sample) {
  325. case 1:
  326. case 33:
  327. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  328. break;
  329. case 2:
  330. case 4:
  331. case 8:
  332. case 34:
  333. case 36:
  334. case 40:
  335. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  336. break;
  337. case 16:
  338. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  339. break;
  340. case 24:
  341. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  342. break;
  343. case 32:
  344. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  345. break;
  346. default:
  347. av_log (avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  348. avctx->bits_per_coded_sample);
  349. return AVERROR_INVALIDDATA;
  350. }
  351. avcodec_get_frame_defaults(&s->frame);
  352. return 0;
  353. }
  354. static int qtrle_decode_frame(AVCodecContext *avctx,
  355. void *data, int *got_frame,
  356. AVPacket *avpkt)
  357. {
  358. QtrleContext *s = avctx->priv_data;
  359. int header, start_line;
  360. int height, row_ptr;
  361. int has_palette = 0;
  362. int ret;
  363. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  364. if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
  365. return ret;
  366. /* check if this frame is even supposed to change */
  367. if (avpkt->size < 8)
  368. goto done;
  369. /* start after the chunk size */
  370. bytestream2_seek(&s->g, 4, SEEK_SET);
  371. /* fetch the header */
  372. header = bytestream2_get_be16(&s->g);
  373. /* if a header is present, fetch additional decoding parameters */
  374. if (header & 0x0008) {
  375. if (avpkt->size < 14)
  376. goto done;
  377. start_line = bytestream2_get_be16(&s->g);
  378. bytestream2_skip(&s->g, 2);
  379. height = bytestream2_get_be16(&s->g);
  380. bytestream2_skip(&s->g, 2);
  381. if (height > s->avctx->height - start_line)
  382. goto done;
  383. } else {
  384. start_line = 0;
  385. height = s->avctx->height;
  386. }
  387. row_ptr = s->frame.linesize[0] * start_line;
  388. switch (avctx->bits_per_coded_sample) {
  389. case 1:
  390. case 33:
  391. qtrle_decode_1bpp(s, row_ptr, height);
  392. break;
  393. case 2:
  394. case 34:
  395. qtrle_decode_2n4bpp(s, row_ptr, height, 2);
  396. has_palette = 1;
  397. break;
  398. case 4:
  399. case 36:
  400. qtrle_decode_2n4bpp(s, row_ptr, height, 4);
  401. has_palette = 1;
  402. break;
  403. case 8:
  404. case 40:
  405. qtrle_decode_8bpp(s, row_ptr, height);
  406. has_palette = 1;
  407. break;
  408. case 16:
  409. qtrle_decode_16bpp(s, row_ptr, height);
  410. break;
  411. case 24:
  412. qtrle_decode_24bpp(s, row_ptr, height);
  413. break;
  414. case 32:
  415. qtrle_decode_32bpp(s, row_ptr, height);
  416. break;
  417. default:
  418. av_log (s->avctx, AV_LOG_ERROR, "Unsupported colorspace: %d bits/sample?\n",
  419. avctx->bits_per_coded_sample);
  420. break;
  421. }
  422. if(has_palette) {
  423. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  424. if (pal) {
  425. s->frame.palette_has_changed = 1;
  426. memcpy(s->pal, pal, AVPALETTE_SIZE);
  427. }
  428. /* make the palette available on the way out */
  429. memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
  430. }
  431. done:
  432. if ((ret = av_frame_ref(data, &s->frame)) < 0)
  433. return ret;
  434. *got_frame = 1;
  435. /* always report that the buffer was completely consumed */
  436. return avpkt->size;
  437. }
  438. static av_cold int qtrle_decode_end(AVCodecContext *avctx)
  439. {
  440. QtrleContext *s = avctx->priv_data;
  441. av_frame_unref(&s->frame);
  442. return 0;
  443. }
  444. AVCodec ff_qtrle_decoder = {
  445. .name = "qtrle",
  446. .type = AVMEDIA_TYPE_VIDEO,
  447. .id = AV_CODEC_ID_QTRLE,
  448. .priv_data_size = sizeof(QtrleContext),
  449. .init = qtrle_decode_init,
  450. .close = qtrle_decode_end,
  451. .decode = qtrle_decode_frame,
  452. .capabilities = CODEC_CAP_DR1,
  453. .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
  454. };