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.

149 lines
3.8KB

  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. int idx;
  59. idx = BE_16(buf); /* color index */
  60. buf += 2;
  61. a->palette[idx * 3 + 0] = *buf++;
  62. buf++;
  63. a->palette[idx * 3 + 1] = *buf++;
  64. buf++;
  65. a->palette[idx * 3 + 2] = *buf++;
  66. buf++;
  67. }
  68. if (colors)
  69. a->pic.palette_has_changed = 1;
  70. buf += 18; /* skip unneeded data */
  71. for (i = 0; i < avctx->height; i++) {
  72. int size, left, code, pix;
  73. uint8_t *next;
  74. uint8_t *out;
  75. int tsize = 0;
  76. /* decode line */
  77. out = outdata;
  78. size = BE_16(buf); /* size of packed line */
  79. buf += 2;
  80. left = size;
  81. next = buf + size;
  82. while (left > 0) {
  83. code = *buf++;
  84. if (code & 0x80 ) { /* run */
  85. int i;
  86. pix = *buf++;
  87. for (i = 0; i < 257 - code; i++) {
  88. *out++ = a->palette[pix * 3 + 0];
  89. *out++ = a->palette[pix * 3 + 1];
  90. *out++ = a->palette[pix * 3 + 2];
  91. }
  92. tsize += 257 - code;
  93. left -= 2;
  94. } else { /* copy */
  95. int i, pix;
  96. for (i = 0; i <= code; i++) {
  97. pix = *buf++;
  98. *out++ = a->palette[pix * 3 + 0];
  99. *out++ = a->palette[pix * 3 + 1];
  100. *out++ = a->palette[pix * 3 + 2];
  101. }
  102. left -= 2 + code;
  103. tsize += code + 1;
  104. }
  105. }
  106. buf = next;
  107. outdata += a->pic.linesize[0];
  108. }
  109. *data_size = sizeof(AVFrame);
  110. *(AVFrame*)data = a->pic;
  111. return buf_size;
  112. }
  113. static int decode_init(AVCodecContext *avctx){
  114. // QdrawContext * const a = avctx->priv_data;
  115. avctx->pix_fmt= PIX_FMT_RGB24;
  116. return 0;
  117. }
  118. AVCodec qdraw_decoder = {
  119. "qdraw",
  120. CODEC_TYPE_VIDEO,
  121. CODEC_ID_QDRAW,
  122. sizeof(QdrawContext),
  123. decode_init,
  124. NULL,
  125. NULL,
  126. decode_frame,
  127. CODEC_CAP_DR1,
  128. };