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.

154 lines
3.9KB

  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. /* special case for last picture */
  41. if (buf_size == 0) {
  42. return 0;
  43. }
  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= I_TYPE;
  52. p->key_frame= 1;
  53. outdata = a->pic.data[0];
  54. buf += 0x68; /* jump to palette */
  55. colors = BE_32(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. for (i = 0; i <= colors; i++) {
  62. int idx;
  63. idx = BE_16(buf); /* color index */
  64. buf += 2;
  65. a->palette[idx * 3 + 0] = *buf++;
  66. buf++;
  67. a->palette[idx * 3 + 1] = *buf++;
  68. buf++;
  69. a->palette[idx * 3 + 2] = *buf++;
  70. buf++;
  71. }
  72. if (colors)
  73. a->pic.palette_has_changed = 1;
  74. buf += 18; /* skip unneeded data */
  75. for (i = 0; i < avctx->height; i++) {
  76. int size, left, code, pix;
  77. uint8_t *next;
  78. uint8_t *out;
  79. int tsize = 0;
  80. /* decode line */
  81. out = outdata;
  82. size = BE_16(buf); /* size of packed line */
  83. buf += 2;
  84. left = size;
  85. next = buf + size;
  86. while (left > 0) {
  87. code = *buf++;
  88. if (code & 0x80 ) { /* run */
  89. int i;
  90. pix = *buf++;
  91. for (i = 0; i < 257 - code; i++) {
  92. *out++ = a->palette[pix * 3 + 0];
  93. *out++ = a->palette[pix * 3 + 1];
  94. *out++ = a->palette[pix * 3 + 2];
  95. }
  96. tsize += 257 - code;
  97. left -= 2;
  98. } else { /* copy */
  99. int i, pix;
  100. for (i = 0; i <= code; i++) {
  101. pix = *buf++;
  102. *out++ = a->palette[pix * 3 + 0];
  103. *out++ = a->palette[pix * 3 + 1];
  104. *out++ = a->palette[pix * 3 + 2];
  105. }
  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 int decode_init(AVCodecContext *avctx){
  118. // QdrawContext * const a = avctx->priv_data;
  119. avctx->pix_fmt= PIX_FMT_RGB24;
  120. return 0;
  121. }
  122. AVCodec qdraw_decoder = {
  123. "qdraw",
  124. CODEC_TYPE_VIDEO,
  125. CODEC_ID_QDRAW,
  126. sizeof(QdrawContext),
  127. decode_init,
  128. NULL,
  129. NULL,
  130. decode_frame,
  131. CODEC_CAP_DR1,
  132. };