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.2KB

  1. /*
  2. * QuickDraw (qdrw) codec
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file qdrw.c
  22. * Apple QuickDraw codec.
  23. */
  24. #include "avcodec.h"
  25. #include "mpegvideo.h"
  26. typedef struct QdrawContext{
  27. AVCodecContext *avctx;
  28. AVFrame pic;
  29. uint8_t palette[256*3];
  30. } QdrawContext;
  31. static int decode_frame(AVCodecContext *avctx,
  32. void *data, int *data_size,
  33. 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. if(p->data[0])
  41. avctx->release_buffer(avctx, p);
  42. p->reference= 0;
  43. if(avctx->get_buffer(avctx, p) < 0){
  44. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  45. return -1;
  46. }
  47. p->pict_type= I_TYPE;
  48. p->key_frame= 1;
  49. outdata = a->pic.data[0];
  50. buf += 0x68; /* jump to palette */
  51. colors = BE_32(buf);
  52. buf += 4;
  53. if(colors < 0 || colors > 256) {
  54. av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
  55. return -1;
  56. }
  57. for (i = 0; i <= colors; i++) {
  58. unsigned int idx;
  59. idx = BE_16(buf); /* color index */
  60. buf += 2;
  61. if (idx > 255) {
  62. av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
  63. buf += 6;
  64. continue;
  65. }
  66. a->palette[idx * 3 + 0] = *buf++;
  67. buf++;
  68. a->palette[idx * 3 + 1] = *buf++;
  69. buf++;
  70. a->palette[idx * 3 + 2] = *buf++;
  71. buf++;
  72. }
  73. buf += 18; /* skip unneeded data */
  74. for (i = 0; i < avctx->height; i++) {
  75. int size, left, code, pix;
  76. uint8_t *next;
  77. uint8_t *out;
  78. int tsize = 0;
  79. /* decode line */
  80. out = outdata;
  81. size = BE_16(buf); /* size of packed line */
  82. buf += 2;
  83. left = size;
  84. next = buf + size;
  85. while (left > 0) {
  86. code = *buf++;
  87. if (code & 0x80 ) { /* run */
  88. int i;
  89. pix = *buf++;
  90. if ((out + (257 - code) * 3) > (outdata + a->pic.linesize[0]))
  91. break;
  92. for (i = 0; i < 257 - code; i++) {
  93. *out++ = a->palette[pix * 3 + 0];
  94. *out++ = a->palette[pix * 3 + 1];
  95. *out++ = a->palette[pix * 3 + 2];
  96. }
  97. tsize += 257 - code;
  98. left -= 2;
  99. } else { /* copy */
  100. int i, pix;
  101. if ((out + code * 3) > (outdata + a->pic.linesize[0]))
  102. break;
  103. for (i = 0; i <= code; i++) {
  104. pix = *buf++;
  105. *out++ = a->palette[pix * 3 + 0];
  106. *out++ = a->palette[pix * 3 + 1];
  107. *out++ = a->palette[pix * 3 + 2];
  108. }
  109. left -= 2 + code;
  110. tsize += code + 1;
  111. }
  112. }
  113. buf = next;
  114. outdata += a->pic.linesize[0];
  115. }
  116. *data_size = sizeof(AVFrame);
  117. *(AVFrame*)data = a->pic;
  118. return buf_size;
  119. }
  120. static int decode_init(AVCodecContext *avctx){
  121. // QdrawContext * const a = avctx->priv_data;
  122. if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
  123. return 1;
  124. }
  125. avctx->pix_fmt= PIX_FMT_RGB24;
  126. return 0;
  127. }
  128. AVCodec qdraw_decoder = {
  129. "qdraw",
  130. CODEC_TYPE_VIDEO,
  131. CODEC_ID_QDRAW,
  132. sizeof(QdrawContext),
  133. decode_init,
  134. NULL,
  135. NULL,
  136. decode_frame,
  137. CODEC_CAP_DR1,
  138. };