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.

152 lines
4.2KB

  1. /*
  2. * QuickDraw (qdrw) codec
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Apple QuickDraw codec.
  24. */
  25. #include "libavutil/common.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. static int decode_frame(AVCodecContext *avctx,
  30. void *data, int *got_frame,
  31. AVPacket *avpkt)
  32. {
  33. const uint8_t *buf = avpkt->data;
  34. const uint8_t *buf_end = avpkt->data + avpkt->size;
  35. int buf_size = avpkt->size;
  36. AVFrame * const p = data;
  37. uint8_t* outdata;
  38. int colors;
  39. int i, ret;
  40. uint32_t *pal;
  41. int r, g, b;
  42. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  43. return ret;
  44. p->pict_type = AV_PICTURE_TYPE_I;
  45. p->key_frame = 1;
  46. outdata = p->data[0];
  47. if (buf_end - buf < 0x68 + 4)
  48. return AVERROR_INVALIDDATA;
  49. buf += 0x68; /* jump to palette */
  50. colors = AV_RB32(buf);
  51. buf += 4;
  52. if (colors < 0 || colors > 256) {
  53. av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
  54. return AVERROR_INVALIDDATA;
  55. }
  56. if (buf_end - buf < (colors + 1) * 8)
  57. return AVERROR_INVALIDDATA;
  58. pal = (uint32_t*)p->data[1];
  59. for (i = 0; i <= colors; i++) {
  60. unsigned int idx;
  61. idx = AV_RB16(buf); /* color index */
  62. buf += 2;
  63. if (idx > 255) {
  64. av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
  65. buf += 6;
  66. continue;
  67. }
  68. r = *buf++;
  69. buf++;
  70. g = *buf++;
  71. buf++;
  72. b = *buf++;
  73. buf++;
  74. pal[idx] = 0xFFU << 24 | r << 16 | g << 8 | b;
  75. }
  76. p->palette_has_changed = 1;
  77. if (buf_end - buf < 18)
  78. return AVERROR_INVALIDDATA;
  79. buf += 18; /* skip unneeded data */
  80. for (i = 0; i < avctx->height; i++) {
  81. int size, left, code, pix;
  82. const uint8_t *next;
  83. uint8_t *out;
  84. int tsize = 0;
  85. /* decode line */
  86. out = outdata;
  87. size = AV_RB16(buf); /* size of packed line */
  88. buf += 2;
  89. if (buf_end - buf < size)
  90. return AVERROR_INVALIDDATA;
  91. left = size;
  92. next = buf + size;
  93. while (left > 0) {
  94. code = *buf++;
  95. if (code & 0x80 ) { /* run */
  96. pix = *buf++;
  97. if ((out + (257 - code)) > (outdata + p->linesize[0]))
  98. break;
  99. memset(out, pix, 257 - code);
  100. out += 257 - code;
  101. tsize += 257 - code;
  102. left -= 2;
  103. } else { /* copy */
  104. if ((out + code) > (outdata + p->linesize[0]))
  105. break;
  106. if (buf_end - buf < code + 1)
  107. return AVERROR_INVALIDDATA;
  108. memcpy(out, buf, code + 1);
  109. out += code + 1;
  110. buf += code + 1;
  111. left -= 2 + code;
  112. tsize += code + 1;
  113. }
  114. }
  115. buf = next;
  116. outdata += p->linesize[0];
  117. }
  118. *got_frame = 1;
  119. return buf_size;
  120. }
  121. static av_cold int decode_init(AVCodecContext *avctx)
  122. {
  123. avctx->pix_fmt= AV_PIX_FMT_PAL8;
  124. return 0;
  125. }
  126. AVCodec ff_qdraw_decoder = {
  127. .name = "qdraw",
  128. .type = AVMEDIA_TYPE_VIDEO,
  129. .id = AV_CODEC_ID_QDRAW,
  130. .init = decode_init,
  131. .decode = decode_frame,
  132. .capabilities = CODEC_CAP_DR1,
  133. .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
  134. };