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.

161 lines
4.2KB

  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. uint8_t palette[256*3];
  32. } QdrawContext;
  33. static int decode_frame(AVCodecContext *avctx,
  34. void *data, int *data_size,
  35. uint8_t *buf, int buf_size)
  36. {
  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. 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= 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. for (i = 0; i <= colors; i++) {
  60. unsigned int idx;
  61. idx = AV_RB16(buf); /* color index */
  62. buf += 2;
  63. if (idx > 255) {
  64. av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
  65. buf += 6;
  66. continue;
  67. }
  68. a->palette[idx * 3 + 0] = *buf++;
  69. buf++;
  70. a->palette[idx * 3 + 1] = *buf++;
  71. buf++;
  72. a->palette[idx * 3 + 2] = *buf++;
  73. buf++;
  74. }
  75. buf += 18; /* skip unneeded data */
  76. for (i = 0; i < avctx->height; i++) {
  77. int size, left, code, pix;
  78. uint8_t *next;
  79. uint8_t *out;
  80. int tsize = 0;
  81. /* decode line */
  82. out = outdata;
  83. size = AV_RB16(buf); /* size of packed line */
  84. buf += 2;
  85. left = size;
  86. next = buf + size;
  87. while (left > 0) {
  88. code = *buf++;
  89. if (code & 0x80 ) { /* run */
  90. int i;
  91. pix = *buf++;
  92. if ((out + (257 - code) * 3) > (outdata + a->pic.linesize[0]))
  93. break;
  94. for (i = 0; i < 257 - code; i++) {
  95. *out++ = a->palette[pix * 3 + 0];
  96. *out++ = a->palette[pix * 3 + 1];
  97. *out++ = a->palette[pix * 3 + 2];
  98. }
  99. tsize += 257 - code;
  100. left -= 2;
  101. } else { /* copy */
  102. int i, pix;
  103. if ((out + code * 3) > (outdata + a->pic.linesize[0]))
  104. break;
  105. for (i = 0; i <= code; i++) {
  106. pix = *buf++;
  107. *out++ = a->palette[pix * 3 + 0];
  108. *out++ = a->palette[pix * 3 + 1];
  109. *out++ = a->palette[pix * 3 + 2];
  110. }
  111. left -= 2 + code;
  112. tsize += code + 1;
  113. }
  114. }
  115. buf = next;
  116. outdata += a->pic.linesize[0];
  117. }
  118. *data_size = sizeof(AVFrame);
  119. *(AVFrame*)data = a->pic;
  120. return buf_size;
  121. }
  122. static int decode_init(AVCodecContext *avctx){
  123. // QdrawContext * const a = avctx->priv_data;
  124. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  125. return 1;
  126. }
  127. avctx->pix_fmt= PIX_FMT_RGB24;
  128. return 0;
  129. }
  130. AVCodec qdraw_decoder = {
  131. "qdraw",
  132. CODEC_TYPE_VIDEO,
  133. CODEC_ID_QDRAW,
  134. sizeof(QdrawContext),
  135. decode_init,
  136. NULL,
  137. NULL,
  138. decode_frame,
  139. CODEC_CAP_DR1,
  140. };