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.

309 lines
9.8KB

  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] = (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. /* smallest PICT header */
  116. if (bytestream2_get_bytes_left(&gbc) < 40) {
  117. av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
  118. bytestream2_get_bytes_left(&gbc));
  119. return AVERROR_INVALIDDATA;
  120. }
  121. bytestream2_skip(&gbc, 6);
  122. h = bytestream2_get_be16(&gbc);
  123. w = bytestream2_get_be16(&gbc);
  124. ret = ff_set_dimensions(avctx, w, h);
  125. if (ret < 0)
  126. return ret;
  127. /* version 1 is identified by 0x1101
  128. * it uses byte-aligned opcodes rather than word-aligned */
  129. if (bytestream2_get_be32(&gbc) != 0x001102FF) {
  130. avpriv_request_sample(avctx, "QuickDraw version 1");
  131. return AVERROR_PATCHWELCOME;
  132. }
  133. bytestream2_skip(&gbc, 26);
  134. while (bytestream2_get_bytes_left(&gbc) >= 4) {
  135. int bppcnt, bpp;
  136. int rowbytes, pack_type;
  137. int opcode = bytestream2_get_be16(&gbc);
  138. switch(opcode) {
  139. case PACKBITSRECT:
  140. case PACKBITSRGN:
  141. av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
  142. bytestream2_skip(&gbc, 30);
  143. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  144. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  145. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  146. if (bppcnt == 1 && bpp == 8) {
  147. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  148. } else {
  149. av_log(avctx, AV_LOG_ERROR,
  150. "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
  151. bppcnt, bpp);
  152. return AVERROR_INVALIDDATA;
  153. }
  154. /* jump to palette */
  155. bytestream2_skip(&gbc, 18);
  156. colors = bytestream2_get_be16(&gbc);
  157. if (colors < 0 || colors > 256) {
  158. av_log(avctx, AV_LOG_ERROR,
  159. "Error color count - %i(0x%X)\n", colors, colors);
  160. return AVERROR_INVALIDDATA;
  161. }
  162. if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
  163. av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
  164. bytestream2_get_bytes_left(&gbc));
  165. return AVERROR_INVALIDDATA;
  166. }
  167. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  168. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  169. return ret;
  170. }
  171. parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
  172. p->palette_has_changed = 1;
  173. /* jump to image data */
  174. bytestream2_skip(&gbc, 18);
  175. if (opcode == PACKBITSRGN) {
  176. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  177. avpriv_report_missing_feature(avctx, "Packbit mask region");
  178. }
  179. ret = decode_rle(avctx, p, &gbc, bppcnt);
  180. if (ret < 0)
  181. return ret;
  182. *got_frame = 1;
  183. break;
  184. case DIRECTBITSRECT:
  185. case DIRECTBITSRGN:
  186. av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
  187. bytestream2_skip(&gbc, 4);
  188. rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
  189. if (rowbytes <= 250) {
  190. avpriv_report_missing_feature(avctx, "Short rowbytes");
  191. return AVERROR_PATCHWELCOME;
  192. }
  193. bytestream2_skip(&gbc, 10);
  194. pack_type = bytestream2_get_be16(&gbc);
  195. bytestream2_skip(&gbc, 16);
  196. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  197. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  198. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  199. if (bppcnt == 3 && bpp == 8) {
  200. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  201. } else if (bppcnt == 4 && bpp == 8) {
  202. avctx->pix_fmt = AV_PIX_FMT_ARGB;
  203. } else {
  204. av_log(avctx, AV_LOG_ERROR,
  205. "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
  206. bppcnt, bpp);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. /* set packing when default is selected */
  210. if (pack_type == 0)
  211. pack_type = bppcnt;
  212. if (pack_type != 3 && pack_type != 4) {
  213. avpriv_request_sample(avctx, "Pack type %d", pack_type);
  214. return AVERROR_PATCHWELCOME;
  215. }
  216. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  217. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  218. return ret;
  219. }
  220. /* jump to data */
  221. bytestream2_skip(&gbc, 30);
  222. if (opcode == DIRECTBITSRGN) {
  223. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  224. avpriv_report_missing_feature(avctx, "DirectBit mask region");
  225. }
  226. ret = decode_rle(avctx, p, &gbc, bppcnt);
  227. if (ret < 0)
  228. return ret;
  229. *got_frame = 1;
  230. break;
  231. default:
  232. av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
  233. break;
  234. }
  235. /* exit the loop when a known pixel block has been found */
  236. if (*got_frame) {
  237. int eop, trail;
  238. /* re-align to a word */
  239. bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
  240. eop = bytestream2_get_be16(&gbc);
  241. trail = bytestream2_get_bytes_left(&gbc);
  242. if (eop != EOP)
  243. av_log(avctx, AV_LOG_WARNING,
  244. "Missing end of picture opcode (found 0x%04X)\n", eop);
  245. if (trail)
  246. av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
  247. break;
  248. }
  249. }
  250. if (*got_frame) {
  251. p->pict_type = AV_PICTURE_TYPE_I;
  252. p->key_frame = 1;
  253. return avpkt->size;
  254. } else {
  255. av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
  256. return AVERROR_INVALIDDATA;
  257. }
  258. }
  259. AVCodec ff_qdraw_decoder = {
  260. .name = "qdraw",
  261. .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .id = AV_CODEC_ID_QDRAW,
  264. .decode = decode_frame,
  265. .capabilities = CODEC_CAP_DR1,
  266. };