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.

167 lines
4.8KB

  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 "dsputil.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. avcodec_get_frame_defaults(&s->frame);
  65. return 0;
  66. }
  67. static int aasc_decode_frame(AVCodecContext *avctx,
  68. void *data, int *got_frame,
  69. AVPacket *avpkt)
  70. {
  71. const uint8_t *buf = avpkt->data;
  72. int buf_size = avpkt->size;
  73. AascContext *s = avctx->priv_data;
  74. int compr, i, stride, psize, ret;
  75. if (buf_size < 4) {
  76. av_log(avctx, AV_LOG_ERROR, "frame too short\n");
  77. return AVERROR_INVALIDDATA;
  78. }
  79. s->frame.reference = 3;
  80. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  81. if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
  82. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  83. return ret;
  84. }
  85. compr = AV_RL32(buf);
  86. buf += 4;
  87. buf_size -= 4;
  88. psize = avctx->bits_per_coded_sample / 8;
  89. switch (avctx->codec_tag) {
  90. case MKTAG('A', 'A', 'S', '4'):
  91. bytestream2_init(&s->gb, buf - 4, buf_size + 4);
  92. ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
  93. break;
  94. case MKTAG('A', 'A', 'S', 'C'):
  95. switch (compr) {
  96. case 0:
  97. stride = (avctx->width * psize + psize) & ~psize;
  98. for (i = avctx->height - 1; i >= 0; i--) {
  99. if (avctx->width * psize > buf_size) {
  100. av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
  101. break;
  102. }
  103. memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width * psize);
  104. buf += stride;
  105. buf_size -= stride;
  106. }
  107. break;
  108. case 1:
  109. bytestream2_init(&s->gb, buf, buf_size);
  110. ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
  111. break;
  112. default:
  113. av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
  114. return AVERROR_INVALIDDATA;
  115. }
  116. break;
  117. default:
  118. av_log(avctx, AV_LOG_ERROR, "Unknown FourCC: %X\n", avctx->codec_tag);
  119. return -1;
  120. }
  121. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  122. memcpy(s->frame.data[1], s->palette, s->palette_size);
  123. *got_frame = 1;
  124. *(AVFrame*)data = s->frame;
  125. /* report that the buffer was completely consumed */
  126. return buf_size;
  127. }
  128. static av_cold int aasc_decode_end(AVCodecContext *avctx)
  129. {
  130. AascContext *s = avctx->priv_data;
  131. /* release the last frame */
  132. if (s->frame.data[0])
  133. avctx->release_buffer(avctx, &s->frame);
  134. return 0;
  135. }
  136. AVCodec ff_aasc_decoder = {
  137. .name = "aasc",
  138. .type = AVMEDIA_TYPE_VIDEO,
  139. .id = AV_CODEC_ID_AASC,
  140. .priv_data_size = sizeof(AascContext),
  141. .init = aasc_decode_init,
  142. .close = aasc_decode_end,
  143. .decode = aasc_decode_frame,
  144. .capabilities = CODEC_CAP_DR1,
  145. .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
  146. };