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.

177 lines
4.8KB

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