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.

513 lines
16KB

  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. CLIP = 0x0001,
  34. PACKBITSRECT = 0x0098,
  35. PACKBITSRGN,
  36. DIRECTBITSRECT,
  37. DIRECTBITSRGN,
  38. SHORTCOMMENT = 0x00A0,
  39. LONGCOMMENT,
  40. EOP = 0x00FF,
  41. };
  42. static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
  43. uint32_t *pal, int colors)
  44. {
  45. int i;
  46. for (i = 0; i <= colors; i++) {
  47. uint8_t r, g, b;
  48. unsigned int idx = bytestream2_get_be16(gbc); /* color index */
  49. if (idx > 255) {
  50. av_log(avctx, AV_LOG_WARNING,
  51. "Palette index out of range: %u\n", idx);
  52. bytestream2_skip(gbc, 6);
  53. continue;
  54. }
  55. r = bytestream2_get_byte(gbc);
  56. bytestream2_skip(gbc, 1);
  57. g = bytestream2_get_byte(gbc);
  58. bytestream2_skip(gbc, 1);
  59. b = bytestream2_get_byte(gbc);
  60. bytestream2_skip(gbc, 1);
  61. pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
  62. }
  63. return 0;
  64. }
  65. static int decode_rle_bpp2(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc)
  66. {
  67. int offset = avctx->width;
  68. uint8_t *outdata = p->data[0];
  69. int i, j;
  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. if (pos < offset)
  85. out[pos++] = (pix & 0xC0) >> 6;
  86. if (pos < offset)
  87. out[pos++] = (pix & 0x30) >> 4;
  88. if (pos < offset)
  89. out[pos++] = (pix & 0x0C) >> 2;
  90. if (pos < offset)
  91. out[pos++] = (pix & 0x03);
  92. }
  93. left -= 2;
  94. } else { /* copy */
  95. for (j = 0; j < code + 1; j++) {
  96. pix = bytestream2_get_byte(gbc);
  97. if (pos < offset)
  98. out[pos++] = (pix & 0xC0) >> 6;
  99. if (pos < offset)
  100. out[pos++] = (pix & 0x30) >> 4;
  101. if (pos < offset)
  102. out[pos++] = (pix & 0x0C) >> 2;
  103. if (pos < offset)
  104. out[pos++] = (pix & 0x03);
  105. }
  106. left -= 1 + (code + 1);
  107. }
  108. }
  109. outdata += p->linesize[0];
  110. }
  111. return 0;
  112. }
  113. static int decode_rle_bpp4(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc)
  114. {
  115. int offset = avctx->width;
  116. uint8_t *outdata = p->data[0];
  117. int i, j;
  118. for (i = 0; i < avctx->height; i++) {
  119. int size, left, code, pix;
  120. uint8_t *out = outdata;
  121. int pos = 0;
  122. /* size of packed line */
  123. size = left = bytestream2_get_be16(gbc);
  124. if (bytestream2_get_bytes_left(gbc) < size)
  125. return AVERROR_INVALIDDATA;
  126. /* decode line */
  127. while (left > 0) {
  128. code = bytestream2_get_byte(gbc);
  129. if (code & 0x80 ) { /* run */
  130. pix = bytestream2_get_byte(gbc);
  131. for (j = 0; j < 257 - code; j++) {
  132. if (pos < offset)
  133. out[pos++] = (pix & 0xF0) >> 4;
  134. if (pos < offset)
  135. out[pos++] = pix & 0xF;
  136. }
  137. left -= 2;
  138. } else { /* copy */
  139. for (j = 0; j < code + 1; j++) {
  140. pix = bytestream2_get_byte(gbc);
  141. if (pos < offset)
  142. out[pos++] = (pix & 0xF0) >> 4;
  143. if (pos < offset)
  144. out[pos++] = pix & 0xF;
  145. }
  146. left -= 1 + (code + 1);
  147. }
  148. }
  149. outdata += p->linesize[0];
  150. }
  151. return 0;
  152. }
  153. static int decode_rle16(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc)
  154. {
  155. int offset = avctx->width;
  156. uint8_t *outdata = p->data[0];
  157. int i, j;
  158. for (i = 0; i < avctx->height; i++) {
  159. int size, left, code, pix;
  160. uint16_t *out = (uint16_t *)outdata;
  161. int pos = 0;
  162. /* size of packed line */
  163. size = left = bytestream2_get_be16(gbc);
  164. if (bytestream2_get_bytes_left(gbc) < size)
  165. return AVERROR_INVALIDDATA;
  166. /* decode line */
  167. while (left > 0) {
  168. code = bytestream2_get_byte(gbc);
  169. if (code & 0x80 ) { /* run */
  170. pix = bytestream2_get_be16(gbc);
  171. for (j = 0; j < 257 - code; j++) {
  172. if (pos < offset) {
  173. out[pos++] = pix;
  174. }
  175. }
  176. left -= 3;
  177. } else { /* copy */
  178. for (j = 0; j < code + 1; j++) {
  179. if (pos < offset) {
  180. out[pos++] = bytestream2_get_be16(gbc);
  181. } else {
  182. bytestream2_skip(gbc, 2);
  183. }
  184. }
  185. left -= 1 + (code + 1) * 2;
  186. }
  187. }
  188. outdata += p->linesize[0];
  189. }
  190. return 0;
  191. }
  192. static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
  193. int step)
  194. {
  195. int i, j;
  196. int offset = avctx->width * step;
  197. uint8_t *outdata = p->data[0];
  198. for (i = 0; i < avctx->height; i++) {
  199. int size, left, code, pix;
  200. uint8_t *out = outdata;
  201. int pos = 0;
  202. /* size of packed line */
  203. size = left = bytestream2_get_be16(gbc);
  204. if (bytestream2_get_bytes_left(gbc) < size)
  205. return AVERROR_INVALIDDATA;
  206. /* decode line */
  207. while (left > 0) {
  208. code = bytestream2_get_byte(gbc);
  209. if (code & 0x80 ) { /* run */
  210. pix = bytestream2_get_byte(gbc);
  211. for (j = 0; j < 257 - code; j++) {
  212. if (pos < offset)
  213. out[pos] = pix;
  214. pos += step;
  215. if (pos >= offset && step > 1) {
  216. pos -= offset;
  217. pos++;
  218. }
  219. }
  220. left -= 2;
  221. } else { /* copy */
  222. for (j = 0; j < code + 1; j++) {
  223. if (pos < offset)
  224. out[pos] = bytestream2_get_byte(gbc);
  225. pos += step;
  226. if (pos >= offset && step > 1) {
  227. pos -= offset;
  228. pos++;
  229. }
  230. }
  231. left -= 2 + code;
  232. }
  233. }
  234. outdata += p->linesize[0];
  235. }
  236. return 0;
  237. }
  238. static int check_header(const char *buf, int buf_size)
  239. {
  240. unsigned w, h, v0, v1;
  241. if (buf_size < 40)
  242. return 0;
  243. w = AV_RB16(buf+6);
  244. h = AV_RB16(buf+8);
  245. v0 = AV_RB16(buf+10);
  246. v1 = AV_RB16(buf+12);
  247. if (!w || !h)
  248. return 0;
  249. if (v0 == 0x1101)
  250. return 1;
  251. if (v0 == 0x0011 && v1 == 0x02FF)
  252. return 2;
  253. return 0;
  254. }
  255. static int decode_frame(AVCodecContext *avctx,
  256. void *data, int *got_frame,
  257. AVPacket *avpkt)
  258. {
  259. AVFrame * const p = data;
  260. GetByteContext gbc;
  261. int colors;
  262. int w, h, ret;
  263. int ver;
  264. bytestream2_init(&gbc, avpkt->data, avpkt->size);
  265. if ( bytestream2_get_bytes_left(&gbc) >= 552
  266. && check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
  267. )
  268. bytestream2_skip(&gbc, 512);
  269. ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));
  270. /* smallest PICT header */
  271. if (bytestream2_get_bytes_left(&gbc) < 40) {
  272. av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
  273. bytestream2_get_bytes_left(&gbc));
  274. return AVERROR_INVALIDDATA;
  275. }
  276. bytestream2_skip(&gbc, 6);
  277. h = bytestream2_get_be16(&gbc);
  278. w = bytestream2_get_be16(&gbc);
  279. ret = ff_set_dimensions(avctx, w, h);
  280. if (ret < 0)
  281. return ret;
  282. /* version 1 is identified by 0x1101
  283. * it uses byte-aligned opcodes rather than word-aligned */
  284. if (ver == 1) {
  285. avpriv_request_sample(avctx, "QuickDraw version 1");
  286. return AVERROR_PATCHWELCOME;
  287. } else if (ver != 2) {
  288. avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
  289. return AVERROR_PATCHWELCOME;
  290. }
  291. bytestream2_skip(&gbc, 4+26);
  292. while (bytestream2_get_bytes_left(&gbc) >= 4) {
  293. int bppcnt, bpp;
  294. int rowbytes, pack_type;
  295. int opcode = bytestream2_get_be16(&gbc);
  296. switch(opcode) {
  297. case CLIP:
  298. bytestream2_skip(&gbc, 10);
  299. break;
  300. case PACKBITSRECT:
  301. case PACKBITSRGN:
  302. av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
  303. bytestream2_skip(&gbc, 30);
  304. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  305. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  306. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  307. if (bppcnt == 1 && bpp == 8) {
  308. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  309. } else if (bppcnt == 1 && (bpp == 4 || bpp == 2)) {
  310. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  311. } else if (bppcnt == 3 && bpp == 5) {
  312. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  313. } else {
  314. av_log(avctx, AV_LOG_ERROR,
  315. "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
  316. bppcnt, bpp);
  317. return AVERROR_INVALIDDATA;
  318. }
  319. /* jump to palette */
  320. bytestream2_skip(&gbc, 18);
  321. colors = bytestream2_get_be16(&gbc);
  322. if (colors < 0 || colors > 256) {
  323. av_log(avctx, AV_LOG_ERROR,
  324. "Error color count - %i(0x%X)\n", colors, colors);
  325. return AVERROR_INVALIDDATA;
  326. }
  327. if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
  328. av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
  329. bytestream2_get_bytes_left(&gbc));
  330. return AVERROR_INVALIDDATA;
  331. }
  332. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  333. return ret;
  334. parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
  335. p->palette_has_changed = 1;
  336. /* jump to image data */
  337. bytestream2_skip(&gbc, 18);
  338. if (opcode == PACKBITSRGN) {
  339. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  340. avpriv_report_missing_feature(avctx, "Packbit mask region");
  341. }
  342. if (avctx->pix_fmt == AV_PIX_FMT_RGB555)
  343. ret = decode_rle16(avctx, p, &gbc);
  344. else if (bpp == 2)
  345. ret = decode_rle_bpp2(avctx, p, &gbc);
  346. else if (bpp == 4)
  347. ret = decode_rle_bpp4(avctx, p, &gbc);
  348. else
  349. ret = decode_rle(avctx, p, &gbc, bppcnt);
  350. if (ret < 0)
  351. return ret;
  352. *got_frame = 1;
  353. break;
  354. case DIRECTBITSRECT:
  355. case DIRECTBITSRGN:
  356. av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
  357. bytestream2_skip(&gbc, 4);
  358. rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
  359. if (rowbytes <= 250) {
  360. avpriv_report_missing_feature(avctx, "Short rowbytes");
  361. return AVERROR_PATCHWELCOME;
  362. }
  363. bytestream2_skip(&gbc, 4);
  364. h = bytestream2_get_be16(&gbc);
  365. w = bytestream2_get_be16(&gbc);
  366. bytestream2_skip(&gbc, 2);
  367. ret = ff_set_dimensions(avctx, w, h);
  368. if (ret < 0)
  369. return ret;
  370. pack_type = bytestream2_get_be16(&gbc);
  371. bytestream2_skip(&gbc, 16);
  372. bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
  373. bpp = bytestream2_get_be16(&gbc); /* cmpSize */
  374. av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
  375. if (bppcnt == 3 && bpp == 8) {
  376. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  377. } else if (bppcnt == 3 && bpp == 5) {
  378. avctx->pix_fmt = AV_PIX_FMT_RGB555;
  379. } else if (bppcnt == 4 && bpp == 8) {
  380. avctx->pix_fmt = AV_PIX_FMT_ARGB;
  381. } else {
  382. av_log(avctx, AV_LOG_ERROR,
  383. "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
  384. bppcnt, bpp);
  385. return AVERROR_INVALIDDATA;
  386. }
  387. /* set packing when default is selected */
  388. if (pack_type == 0)
  389. pack_type = bppcnt;
  390. if (pack_type != 3 && pack_type != 4) {
  391. avpriv_request_sample(avctx, "Pack type %d", pack_type);
  392. return AVERROR_PATCHWELCOME;
  393. }
  394. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  395. return ret;
  396. /* jump to data */
  397. bytestream2_skip(&gbc, 30);
  398. if (opcode == DIRECTBITSRGN) {
  399. bytestream2_skip(&gbc, 2 + 8); /* size + rect */
  400. avpriv_report_missing_feature(avctx, "DirectBit mask region");
  401. }
  402. if (avctx->pix_fmt == AV_PIX_FMT_RGB555)
  403. ret = decode_rle16(avctx, p, &gbc);
  404. else
  405. ret = decode_rle(avctx, p, &gbc, bppcnt);
  406. if (ret < 0)
  407. return ret;
  408. *got_frame = 1;
  409. break;
  410. case LONGCOMMENT:
  411. bytestream2_get_be16(&gbc);
  412. bytestream2_skip(&gbc, bytestream2_get_be16(&gbc));
  413. break;
  414. default:
  415. av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
  416. break;
  417. }
  418. /* exit the loop when a known pixel block has been found */
  419. if (*got_frame) {
  420. int eop, trail;
  421. /* re-align to a word */
  422. bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
  423. eop = bytestream2_get_be16(&gbc);
  424. trail = bytestream2_get_bytes_left(&gbc);
  425. if (eop != EOP)
  426. av_log(avctx, AV_LOG_WARNING,
  427. "Missing end of picture opcode (found 0x%04X)\n", eop);
  428. if (trail)
  429. av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
  430. break;
  431. }
  432. }
  433. if (*got_frame) {
  434. p->pict_type = AV_PICTURE_TYPE_I;
  435. p->key_frame = 1;
  436. return avpkt->size;
  437. } else {
  438. av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
  439. return AVERROR_INVALIDDATA;
  440. }
  441. }
  442. AVCodec ff_qdraw_decoder = {
  443. .name = "qdraw",
  444. .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
  445. .type = AVMEDIA_TYPE_VIDEO,
  446. .id = AV_CODEC_ID_QDRAW,
  447. .decode = decode_frame,
  448. .capabilities = AV_CODEC_CAP_DR1,
  449. };