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.

525 lines
17KB

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