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.

313 lines
10.0KB

  1. /*
  2. * QuickDraw (qdrw) codec
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. * Copyright (c) 2015 Vittorio Giovara
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Apple QuickDraw codec.
  25. * https://developer.apple.com/legacy/library/documentation/mac/QuickDraw/QuickDraw-461.html
  26. */
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "bytestream.h"
  31. #include "internal.h"
  32. enum QuickdrawOpcodes {
  33. PACKBITSRECT = 0x0098,
  34. PACKBITSRGN,
  35. DIRECTBITSRECT,
  36. DIRECTBITSRGN,
  37. EOP = 0x00FF,
  38. };
  39. static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
  40. uint32_t *pal, int colors)
  41. {
  42. int i;
  43. for (i = 0; i <= colors; i++) {
  44. uint8_t r, g, b;
  45. unsigned int idx = bytestream2_get_be16(gbc); /* color index */
  46. if (idx > 255) {
  47. av_log(avctx, AV_LOG_WARNING,
  48. "Palette index out of range: %u\n", idx);
  49. bytestream2_skip(gbc, 6);
  50. continue;
  51. }
  52. r = bytestream2_get_byte(gbc);
  53. bytestream2_skip(gbc, 1);
  54. g = bytestream2_get_byte(gbc);
  55. bytestream2_skip(gbc, 1);
  56. b = bytestream2_get_byte(gbc);
  57. bytestream2_skip(gbc, 1);
  58. pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  59. }
  60. return 0;
  61. }
  62. static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
  63. int step)
  64. {
  65. int i, j;
  66. int offset = avctx->width * step;
  67. uint8_t *outdata = p->data[0];
  68. for (i = 0; i < avctx->height; i++) {
  69. int size, left, code, pix;
  70. uint8_t *out = outdata;
  71. int pos = 0;
  72. /* size of packed line */
  73. size = left = bytestream2_get_be16(gbc);
  74. if (bytestream2_get_bytes_left(gbc) < size)
  75. return AVERROR_INVALIDDATA;
  76. /* decode line */
  77. while (left > 0) {
  78. code = bytestream2_get_byte(gbc);
  79. if (code & 0x80 ) { /* run */
  80. pix = bytestream2_get_byte(gbc);
  81. for (j = 0; j < 257 - code; j++) {
  82. out[pos] = pix;
  83. pos += step;
  84. if (pos >= offset) {
  85. pos -= offset;
  86. pos++;
  87. }
  88. }
  89. left -= 2;
  90. } else { /* copy */
  91. for (j = 0; j < code + 1; j++) {
  92. out[pos] = bytestream2_get_byte(gbc);
  93. pos += step;
  94. if (pos >= offset) {
  95. pos -= offset;
  96. pos++;
  97. }
  98. }
  99. left -= 2 + code;
  100. }
  101. }
  102. outdata += p->linesize[0];
  103. }
  104. return 0;
  105. }
  106. static int decode_frame(AVCodecContext *avctx,
  107. void *data, int *got_frame,
  108. AVPacket *avpkt)
  109. {
  110. AVFrame * const p = data;
  111. GetByteContext gbc;
  112. int colors;
  113. int w, h, ret;
  114. bytestream2_init(&gbc, avpkt->data, avpkt->size);
  115. /* PICT images start with a 512 bytes empty header */
  116. if (bytestream2_peek_be32(&gbc) == 0)
  117. bytestream2_skip(&gbc, 512);
  118. /* smallest PICT header */
  119. if (bytestream2_get_bytes_left(&gbc) < 40) {
  120. av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
  121. bytestream2_get_bytes_left(&gbc));
  122. return AVERROR_INVALIDDATA;
  123. }
  124. bytestream2_skip(&gbc, 6);
  125. h = bytestream2_get_be16(&gbc);
  126. w = bytestream2_get_be16(&gbc);
  127. ret = ff_set_dimensions(avctx, w, h);
  128. if (ret < 0)
  129. return ret;
  130. /* version 1 is identified by 0x1101
  131. * it uses byte-aligned opcodes rather than word-aligned */
  132. if (bytestream2_get_be32(&gbc) != 0x001102FF) {
  133. avpriv_request_sample(avctx, "QuickDraw version 1");
  134. return AVERROR_PATCHWELCOME;
  135. }
  136. bytestream2_skip(&gbc, 26);
  137. while (bytestream2_get_bytes_left(&gbc) >= 4) {
  138. int bppcnt, bpp;
  139. int rowbytes, pack_type;
  140. int opcode = bytestream2_get_be16(&gbc);
  141. switch(opcode) {
  142. case PACKBITSRECT:
  143. case PACKBITSRGN:
  144. av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
  145. bytestream2_skip(&gbc, 30);
  146. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  147. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  148. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  149. if (bppcnt == 1 && bpp == 8) {
  150. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  151. } else {
  152. av_log(avctx, AV_LOG_ERROR,
  153. "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
  154. bppcnt, bpp);
  155. return AVERROR_INVALIDDATA;
  156. }
  157. /* jump to palette */
  158. bytestream2_skip(&gbc, 18);
  159. colors = bytestream2_get_be16(&gbc);
  160. if (colors < 0 || colors > 256) {
  161. av_log(avctx, AV_LOG_ERROR,
  162. "Error color count - %i(0x%X)\n", colors, colors);
  163. return AVERROR_INVALIDDATA;
  164. }
  165. if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
  166. av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
  167. bytestream2_get_bytes_left(&gbc));
  168. return AVERROR_INVALIDDATA;
  169. }
  170. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  171. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  172. return ret;
  173. }
  174. parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
  175. p->palette_has_changed = 1;
  176. /* jump to image data */
  177. bytestream2_skip(&gbc, 18);
  178. if (opcode == PACKBITSRGN) {
  179. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  180. avpriv_report_missing_feature(avctx, "Packbit mask region");
  181. }
  182. ret = decode_rle(avctx, p, &gbc, bppcnt);
  183. if (ret < 0)
  184. return ret;
  185. *got_frame = 1;
  186. break;
  187. case DIRECTBITSRECT:
  188. case DIRECTBITSRGN:
  189. av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
  190. bytestream2_skip(&gbc, 4);
  191. rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
  192. if (rowbytes <= 250) {
  193. avpriv_report_missing_feature(avctx, "Short rowbytes");
  194. return AVERROR_PATCHWELCOME;
  195. }
  196. bytestream2_skip(&gbc, 10);
  197. pack_type = bytestream2_get_be16(&gbc);
  198. bytestream2_skip(&gbc, 16);
  199. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  200. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  201. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  202. if (bppcnt == 3 && bpp == 8) {
  203. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  204. } else if (bppcnt == 4 && bpp == 8) {
  205. avctx->pix_fmt = AV_PIX_FMT_ARGB;
  206. } else {
  207. av_log(avctx, AV_LOG_ERROR,
  208. "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
  209. bppcnt, bpp);
  210. return AVERROR_INVALIDDATA;
  211. }
  212. /* set packing when default is selected */
  213. if (pack_type == 0)
  214. pack_type = bppcnt;
  215. if (pack_type != 3 && pack_type != 4) {
  216. avpriv_request_sample(avctx, "Pack type %d", pack_type);
  217. return AVERROR_PATCHWELCOME;
  218. }
  219. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  220. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  221. return ret;
  222. }
  223. /* jump to data */
  224. bytestream2_skip(&gbc, 30);
  225. if (opcode == DIRECTBITSRGN) {
  226. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  227. avpriv_report_missing_feature(avctx, "DirectBit mask region");
  228. }
  229. ret = decode_rle(avctx, p, &gbc, bppcnt);
  230. if (ret < 0)
  231. return ret;
  232. *got_frame = 1;
  233. break;
  234. default:
  235. av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
  236. break;
  237. }
  238. /* exit the loop when a known pixel block has been found */
  239. if (*got_frame) {
  240. int eop, trail;
  241. /* re-align to a word */
  242. bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
  243. eop = bytestream2_get_be16(&gbc);
  244. trail = bytestream2_get_bytes_left(&gbc);
  245. if (eop != EOP)
  246. av_log(avctx, AV_LOG_WARNING,
  247. "Missing end of picture opcode (found 0x%04X)\n", eop);
  248. if (trail)
  249. av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
  250. break;
  251. }
  252. }
  253. if (*got_frame) {
  254. p->pict_type = AV_PICTURE_TYPE_I;
  255. p->key_frame = 1;
  256. return avpkt->size;
  257. } else {
  258. av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
  259. return AVERROR_INVALIDDATA;
  260. }
  261. }
  262. AVCodec ff_qdraw_decoder = {
  263. .name = "qdraw",
  264. .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. .id = AV_CODEC_ID_QDRAW,
  267. .decode = decode_frame,
  268. .capabilities = AV_CODEC_CAP_DR1,
  269. };