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.

156 lines
4.0KB

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