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.

154 lines
4.3KB

  1. /*
  2. * QuickDraw (qdrw) codec
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  44. return ret;
  45. }
  46. p->pict_type = AV_PICTURE_TYPE_I;
  47. p->key_frame = 1;
  48. outdata = p->data[0];
  49. if (buf_end - buf < 0x68 + 4)
  50. return AVERROR_INVALIDDATA;
  51. buf += 0x68; /* jump to palette */
  52. colors = AV_RB32(buf);
  53. buf += 4;
  54. if (colors < 0 || colors > 256) {
  55. av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
  56. return AVERROR_INVALIDDATA;
  57. }
  58. if (buf_end - buf < (colors + 1) * 8)
  59. return AVERROR_INVALIDDATA;
  60. pal = (uint32_t*)p->data[1];
  61. for (i = 0; i <= colors; i++) {
  62. unsigned int idx;
  63. idx = AV_RB16(buf); /* color index */
  64. buf += 2;
  65. if (idx > 255) {
  66. av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
  67. buf += 6;
  68. continue;
  69. }
  70. r = *buf++;
  71. buf++;
  72. g = *buf++;
  73. buf++;
  74. b = *buf++;
  75. buf++;
  76. pal[idx] = (r << 16) | (g << 8) | b;
  77. }
  78. p->palette_has_changed = 1;
  79. if (buf_end - buf < 18)
  80. return AVERROR_INVALIDDATA;
  81. buf += 18; /* skip unneeded data */
  82. for (i = 0; i < avctx->height; i++) {
  83. int size, left, code, pix;
  84. const uint8_t *next;
  85. uint8_t *out;
  86. int tsize = 0;
  87. /* decode line */
  88. out = outdata;
  89. size = AV_RB16(buf); /* size of packed line */
  90. buf += 2;
  91. if (buf_end - buf < size)
  92. return AVERROR_INVALIDDATA;
  93. left = size;
  94. next = buf + size;
  95. while (left > 0) {
  96. code = *buf++;
  97. if (code & 0x80 ) { /* run */
  98. pix = *buf++;
  99. if ((out + (257 - code)) > (outdata + p->linesize[0]))
  100. break;
  101. memset(out, pix, 257 - code);
  102. out += 257 - code;
  103. tsize += 257 - code;
  104. left -= 2;
  105. } else { /* copy */
  106. if ((out + code) > (outdata + p->linesize[0]))
  107. break;
  108. if (buf_end - buf < code + 1)
  109. return AVERROR_INVALIDDATA;
  110. memcpy(out, buf, code + 1);
  111. out += code + 1;
  112. buf += code + 1;
  113. left -= 2 + code;
  114. tsize += code + 1;
  115. }
  116. }
  117. buf = next;
  118. outdata += p->linesize[0];
  119. }
  120. *got_frame = 1;
  121. return buf_size;
  122. }
  123. static av_cold int decode_init(AVCodecContext *avctx)
  124. {
  125. avctx->pix_fmt= AV_PIX_FMT_PAL8;
  126. return 0;
  127. }
  128. AVCodec ff_qdraw_decoder = {
  129. .name = "qdraw",
  130. .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
  131. .type = AVMEDIA_TYPE_VIDEO,
  132. .id = AV_CODEC_ID_QDRAW,
  133. .init = decode_init,
  134. .decode = decode_frame,
  135. .capabilities = CODEC_CAP_DR1,
  136. };