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.

176 lines
4.7KB

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