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.

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