qdrw.c 4.0 KB

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