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.

341 lines
11KB

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