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.

181 lines
4.9KB

  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. typedef struct QdrawContext {
  30. AVCodecContext *avctx;
  31. AVFrame pic;
  32. } QdrawContext;
  33. static int decode_frame(AVCodecContext *avctx,
  34. void *data, int *got_frame,
  35. AVPacket *avpkt)
  36. {
  37. const uint8_t *buf = avpkt->data;
  38. const uint8_t *buf_end = avpkt->data + avpkt->size;
  39. int buf_size = avpkt->size;
  40. QdrawContext * const a = avctx->priv_data;
  41. AVFrame * const p = &a->pic;
  42. uint8_t* outdata;
  43. int colors;
  44. int i, ret;
  45. uint32_t *pal;
  46. int r, g, b;
  47. if (p->data[0])
  48. avctx->release_buffer(avctx, p);
  49. p->reference = 0;
  50. if ((ret = ff_get_buffer(avctx, p)) < 0) {
  51. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  52. return ret;
  53. }
  54. p->pict_type = AV_PICTURE_TYPE_I;
  55. p->key_frame = 1;
  56. outdata = a->pic.data[0];
  57. if (buf_end - buf < 0x68 + 4)
  58. return AVERROR_INVALIDDATA;
  59. buf += 0x68; /* jump to palette */
  60. colors = AV_RB32(buf);
  61. buf += 4;
  62. if (colors < 0 || colors > 256) {
  63. av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
  64. return AVERROR_INVALIDDATA;
  65. }
  66. if (buf_end - buf < (colors + 1) * 8)
  67. return AVERROR_INVALIDDATA;
  68. pal = (uint32_t*)p->data[1];
  69. for (i = 0; i <= colors; i++) {
  70. unsigned int idx;
  71. idx = AV_RB16(buf); /* color index */
  72. buf += 2;
  73. if (idx > 255) {
  74. av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
  75. buf += 6;
  76. continue;
  77. }
  78. r = *buf++;
  79. buf++;
  80. g = *buf++;
  81. buf++;
  82. b = *buf++;
  83. buf++;
  84. pal[idx] = 0xFFU << 24 | r << 16 | g << 8 | b;
  85. }
  86. p->palette_has_changed = 1;
  87. if (buf_end - buf < 18)
  88. return AVERROR_INVALIDDATA;
  89. buf += 18; /* skip unneeded data */
  90. for (i = 0; i < avctx->height; i++) {
  91. int size, left, code, pix;
  92. const uint8_t *next;
  93. uint8_t *out;
  94. int tsize = 0;
  95. /* decode line */
  96. out = outdata;
  97. size = AV_RB16(buf); /* size of packed line */
  98. buf += 2;
  99. if (buf_end - buf < size)
  100. return AVERROR_INVALIDDATA;
  101. left = size;
  102. next = buf + size;
  103. while (left > 0) {
  104. code = *buf++;
  105. if (code & 0x80 ) { /* run */
  106. pix = *buf++;
  107. if ((out + (257 - code)) > (outdata + a->pic.linesize[0]))
  108. break;
  109. memset(out, pix, 257 - code);
  110. out += 257 - code;
  111. tsize += 257 - code;
  112. left -= 2;
  113. } else { /* copy */
  114. if ((out + code) > (outdata + a->pic.linesize[0]))
  115. break;
  116. if (buf_end - buf < code + 1)
  117. return AVERROR_INVALIDDATA;
  118. memcpy(out, buf, code + 1);
  119. out += code + 1;
  120. buf += code + 1;
  121. left -= 2 + code;
  122. tsize += code + 1;
  123. }
  124. }
  125. buf = next;
  126. outdata += a->pic.linesize[0];
  127. }
  128. *got_frame = 1;
  129. *(AVFrame*)data = a->pic;
  130. return buf_size;
  131. }
  132. static av_cold int decode_init(AVCodecContext *avctx)
  133. {
  134. QdrawContext * const a = avctx->priv_data;
  135. avcodec_get_frame_defaults(&a->pic);
  136. avctx->pix_fmt= AV_PIX_FMT_PAL8;
  137. return 0;
  138. }
  139. static av_cold int decode_end(AVCodecContext *avctx)
  140. {
  141. QdrawContext * const a = avctx->priv_data;
  142. AVFrame *pic = &a->pic;
  143. if (pic->data[0])
  144. avctx->release_buffer(avctx, pic);
  145. return 0;
  146. }
  147. AVCodec ff_qdraw_decoder = {
  148. .name = "qdraw",
  149. .type = AVMEDIA_TYPE_VIDEO,
  150. .id = AV_CODEC_ID_QDRAW,
  151. .priv_data_size = sizeof(QdrawContext),
  152. .init = decode_init,
  153. .close = decode_end,
  154. .decode = decode_frame,
  155. .capabilities = CODEC_CAP_DR1,
  156. .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
  157. };