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.

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