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.

159 lines
4.1KB

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