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.

348 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. SHORTCOMMENT = 0x00A0,
  38. LONGCOMMENT,
  39. EOP = 0x00FF,
  40. };
  41. static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
  42. uint32_t *pal, int colors)
  43. {
  44. int i;
  45. for (i = 0; i <= colors; i++) {
  46. uint8_t r, g, b;
  47. unsigned int idx = bytestream2_get_be16(gbc); /* color index */
  48. if (idx > 255) {
  49. av_log(avctx, AV_LOG_WARNING,
  50. "Palette index out of range: %u\n", idx);
  51. bytestream2_skip(gbc, 6);
  52. continue;
  53. }
  54. r = bytestream2_get_byte(gbc);
  55. bytestream2_skip(gbc, 1);
  56. g = bytestream2_get_byte(gbc);
  57. bytestream2_skip(gbc, 1);
  58. b = bytestream2_get_byte(gbc);
  59. bytestream2_skip(gbc, 1);
  60. pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  61. }
  62. return 0;
  63. }
  64. static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
  65. int step)
  66. {
  67. int i, j;
  68. int offset = avctx->width * step;
  69. uint8_t *outdata = p->data[0];
  70. for (i = 0; i < avctx->height; i++) {
  71. int size, left, code, pix;
  72. uint8_t *out = outdata;
  73. int pos = 0;
  74. /* size of packed line */
  75. size = left = bytestream2_get_be16(gbc);
  76. if (bytestream2_get_bytes_left(gbc) < size)
  77. return AVERROR_INVALIDDATA;
  78. /* decode line */
  79. while (left > 0) {
  80. code = bytestream2_get_byte(gbc);
  81. if (code & 0x80 ) { /* run */
  82. pix = bytestream2_get_byte(gbc);
  83. for (j = 0; j < 257 - code; j++) {
  84. out[pos] = pix;
  85. pos += step;
  86. if (pos >= offset) {
  87. pos -= offset;
  88. pos++;
  89. }
  90. if (pos >= offset)
  91. return AVERROR_INVALIDDATA;
  92. }
  93. left -= 2;
  94. } else { /* copy */
  95. for (j = 0; j < code + 1; j++) {
  96. out[pos] = bytestream2_get_byte(gbc);
  97. pos += step;
  98. if (pos >= offset) {
  99. pos -= offset;
  100. pos++;
  101. }
  102. if (pos >= offset)
  103. return AVERROR_INVALIDDATA;
  104. }
  105. left -= 2 + code;
  106. }
  107. }
  108. outdata += p->linesize[0];
  109. }
  110. return 0;
  111. }
  112. static int check_header(const char *buf, int buf_size)
  113. {
  114. unsigned w, h, v0, v1;
  115. if (buf_size < 40)
  116. return 0;
  117. w = AV_RB16(buf+6);
  118. h = AV_RB16(buf+8);
  119. v0 = AV_RB16(buf+10);
  120. v1 = AV_RB16(buf+12);
  121. if (!w || !h)
  122. return 0;
  123. if (v0 == 0x1101)
  124. return 1;
  125. if (v0 == 0x0011 && v1 == 0x02FF)
  126. return 2;
  127. return 0;
  128. }
  129. static int decode_frame(AVCodecContext *avctx,
  130. void *data, int *got_frame,
  131. AVPacket *avpkt)
  132. {
  133. AVFrame * const p = data;
  134. GetByteContext gbc;
  135. int colors;
  136. int w, h, ret;
  137. int ver;
  138. bytestream2_init(&gbc, avpkt->data, avpkt->size);
  139. if ( bytestream2_get_bytes_left(&gbc) >= 552
  140. && check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
  141. )
  142. bytestream2_skip(&gbc, 512);
  143. ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));
  144. /* smallest PICT header */
  145. if (bytestream2_get_bytes_left(&gbc) < 40) {
  146. av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
  147. bytestream2_get_bytes_left(&gbc));
  148. return AVERROR_INVALIDDATA;
  149. }
  150. bytestream2_skip(&gbc, 6);
  151. h = bytestream2_get_be16(&gbc);
  152. w = bytestream2_get_be16(&gbc);
  153. ret = ff_set_dimensions(avctx, w, h);
  154. if (ret < 0)
  155. return ret;
  156. /* version 1 is identified by 0x1101
  157. * it uses byte-aligned opcodes rather than word-aligned */
  158. if (ver == 1) {
  159. avpriv_request_sample(avctx, "QuickDraw version 1");
  160. return AVERROR_PATCHWELCOME;
  161. } else if (ver != 2) {
  162. avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
  163. return AVERROR_PATCHWELCOME;
  164. }
  165. bytestream2_skip(&gbc, 4+26);
  166. while (bytestream2_get_bytes_left(&gbc) >= 4) {
  167. int bppcnt, bpp;
  168. int rowbytes, pack_type;
  169. int opcode = bytestream2_get_be16(&gbc);
  170. switch(opcode) {
  171. case PACKBITSRECT:
  172. case PACKBITSRGN:
  173. av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
  174. bytestream2_skip(&gbc, 30);
  175. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  176. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  177. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  178. if (bppcnt == 1 && bpp == 8) {
  179. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  180. } else {
  181. av_log(avctx, AV_LOG_ERROR,
  182. "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
  183. bppcnt, bpp);
  184. return AVERROR_INVALIDDATA;
  185. }
  186. /* jump to palette */
  187. bytestream2_skip(&gbc, 18);
  188. colors = bytestream2_get_be16(&gbc);
  189. if (colors < 0 || colors > 256) {
  190. av_log(avctx, AV_LOG_ERROR,
  191. "Error color count - %i(0x%X)\n", colors, colors);
  192. return AVERROR_INVALIDDATA;
  193. }
  194. if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
  195. av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
  196. bytestream2_get_bytes_left(&gbc));
  197. return AVERROR_INVALIDDATA;
  198. }
  199. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  200. return ret;
  201. parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
  202. p->palette_has_changed = 1;
  203. /* jump to image data */
  204. bytestream2_skip(&gbc, 18);
  205. if (opcode == PACKBITSRGN) {
  206. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  207. avpriv_report_missing_feature(avctx, "Packbit mask region");
  208. }
  209. ret = decode_rle(avctx, p, &gbc, bppcnt);
  210. if (ret < 0)
  211. return ret;
  212. *got_frame = 1;
  213. break;
  214. case DIRECTBITSRECT:
  215. case DIRECTBITSRGN:
  216. av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
  217. bytestream2_skip(&gbc, 4);
  218. rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
  219. if (rowbytes <= 250) {
  220. avpriv_report_missing_feature(avctx, "Short rowbytes");
  221. return AVERROR_PATCHWELCOME;
  222. }
  223. bytestream2_skip(&gbc, 10);
  224. pack_type = bytestream2_get_be16(&gbc);
  225. bytestream2_skip(&gbc, 16);
  226. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  227. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  228. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  229. if (bppcnt == 3 && bpp == 8) {
  230. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  231. } else if (bppcnt == 4 && bpp == 8) {
  232. avctx->pix_fmt = AV_PIX_FMT_ARGB;
  233. } else {
  234. av_log(avctx, AV_LOG_ERROR,
  235. "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
  236. bppcnt, bpp);
  237. return AVERROR_INVALIDDATA;
  238. }
  239. /* set packing when default is selected */
  240. if (pack_type == 0)
  241. pack_type = bppcnt;
  242. if (pack_type != 3 && pack_type != 4) {
  243. avpriv_request_sample(avctx, "Pack type %d", pack_type);
  244. return AVERROR_PATCHWELCOME;
  245. }
  246. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  247. return ret;
  248. /* jump to data */
  249. bytestream2_skip(&gbc, 30);
  250. if (opcode == DIRECTBITSRGN) {
  251. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  252. avpriv_report_missing_feature(avctx, "DirectBit mask region");
  253. }
  254. ret = decode_rle(avctx, p, &gbc, bppcnt);
  255. if (ret < 0)
  256. return ret;
  257. *got_frame = 1;
  258. break;
  259. case LONGCOMMENT:
  260. bytestream2_get_be16(&gbc);
  261. bytestream2_skip(&gbc, bytestream2_get_be16(&gbc));
  262. break;
  263. default:
  264. av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
  265. break;
  266. }
  267. /* exit the loop when a known pixel block has been found */
  268. if (*got_frame) {
  269. int eop, trail;
  270. /* re-align to a word */
  271. bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
  272. eop = bytestream2_get_be16(&gbc);
  273. trail = bytestream2_get_bytes_left(&gbc);
  274. if (eop != EOP)
  275. av_log(avctx, AV_LOG_WARNING,
  276. "Missing end of picture opcode (found 0x%04X)\n", eop);
  277. if (trail)
  278. av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
  279. break;
  280. }
  281. }
  282. if (*got_frame) {
  283. p->pict_type = AV_PICTURE_TYPE_I;
  284. p->key_frame = 1;
  285. return avpkt->size;
  286. } else {
  287. av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. }
  291. AVCodec ff_qdraw_decoder = {
  292. .name = "qdraw",
  293. .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .id = AV_CODEC_ID_QDRAW,
  296. .decode = decode_frame,
  297. .capabilities = AV_CODEC_CAP_DR1,
  298. };