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.

163 lines
4.5KB

  1. /*
  2. * Autodesk RLE Decoder
  3. * Copyright (C) 2005 The FFmpeg project
  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
  23. * Autodesk RLE Video Decoder by Konstantin Shishkov
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. #include "msrledec.h"
  31. typedef struct AascContext {
  32. AVCodecContext *avctx;
  33. GetByteContext gb;
  34. AVFrame *frame;
  35. uint32_t palette[AVPALETTE_COUNT];
  36. int palette_size;
  37. } AascContext;
  38. static av_cold int aasc_decode_init(AVCodecContext *avctx)
  39. {
  40. AascContext *s = avctx->priv_data;
  41. uint8_t *ptr;
  42. int i;
  43. s->avctx = avctx;
  44. switch (avctx->bits_per_coded_sample) {
  45. case 8:
  46. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  47. ptr = avctx->extradata;
  48. s->palette_size = FFMIN(avctx->extradata_size, AVPALETTE_SIZE);
  49. for (i = 0; i < s->palette_size / 4; i++) {
  50. s->palette[i] = 0xFFU << 24 | AV_RL32(ptr);
  51. ptr += 4;
  52. }
  53. break;
  54. case 16:
  55. avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
  56. break;
  57. case 24:
  58. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  59. break;
  60. default:
  61. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_coded_sample);
  62. return -1;
  63. }
  64. s->frame = av_frame_alloc();
  65. if (!s->frame)
  66. return AVERROR(ENOMEM);
  67. return 0;
  68. }
  69. static int aasc_decode_frame(AVCodecContext *avctx,
  70. void *data, int *got_frame,
  71. AVPacket *avpkt)
  72. {
  73. const uint8_t *buf = avpkt->data;
  74. int buf_size = avpkt->size;
  75. AascContext *s = avctx->priv_data;
  76. int compr, i, stride, psize, ret;
  77. if (buf_size < 4) {
  78. av_log(avctx, AV_LOG_ERROR, "frame too short\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  82. return ret;
  83. compr = AV_RL32(buf);
  84. buf += 4;
  85. buf_size -= 4;
  86. psize = avctx->bits_per_coded_sample / 8;
  87. switch (avctx->codec_tag) {
  88. case MKTAG('A', 'A', 'S', '4'):
  89. bytestream2_init(&s->gb, buf - 4, buf_size + 4);
  90. ff_msrle_decode(avctx, s->frame, 8, &s->gb);
  91. break;
  92. case MKTAG('A', 'A', 'S', 'C'):
  93. switch (compr) {
  94. case 0:
  95. stride = (avctx->width * psize + psize) & ~psize;
  96. if (buf_size < stride * avctx->height)
  97. return AVERROR_INVALIDDATA;
  98. for (i = avctx->height - 1; i >= 0; i--) {
  99. memcpy(s->frame->data[0] + i * s->frame->linesize[0], buf, avctx->width * psize);
  100. buf += stride;
  101. buf_size -= stride;
  102. }
  103. break;
  104. case 1:
  105. bytestream2_init(&s->gb, buf, buf_size);
  106. ff_msrle_decode(avctx, s->frame, 8, &s->gb);
  107. break;
  108. default:
  109. av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
  110. return AVERROR_INVALIDDATA;
  111. }
  112. break;
  113. default:
  114. av_log(avctx, AV_LOG_ERROR, "Unknown FourCC: %X\n", avctx->codec_tag);
  115. return -1;
  116. }
  117. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  118. memcpy(s->frame->data[1], s->palette, s->palette_size);
  119. *got_frame = 1;
  120. if ((ret = av_frame_ref(data, s->frame)) < 0)
  121. return ret;
  122. /* report that the buffer was completely consumed */
  123. return avpkt->size;
  124. }
  125. static av_cold int aasc_decode_end(AVCodecContext *avctx)
  126. {
  127. AascContext *s = avctx->priv_data;
  128. av_frame_free(&s->frame);
  129. return 0;
  130. }
  131. AVCodec ff_aasc_decoder = {
  132. .name = "aasc",
  133. .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
  134. .type = AVMEDIA_TYPE_VIDEO,
  135. .id = AV_CODEC_ID_AASC,
  136. .priv_data_size = sizeof(AascContext),
  137. .init = aasc_decode_init,
  138. .close = aasc_decode_end,
  139. .decode = aasc_decode_frame,
  140. .capabilities = AV_CODEC_CAP_DR1,
  141. };