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.

162 lines
4.6KB

  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 = 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 = PIX_FMT_RGB555;
  56. break;
  57. case 24:
  58. avctx->pix_fmt = 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 *data_size,
  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;
  75. s->frame.reference = 3;
  76. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  77. if (avctx->reget_buffer(avctx, &s->frame)) {
  78. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  79. return -1;
  80. }
  81. compr = AV_RL32(buf);
  82. buf += 4;
  83. buf_size -= 4;
  84. psize = avctx->bits_per_coded_sample / 8;
  85. switch (avctx->codec_tag) {
  86. case MKTAG('A', 'A', 'S', '4'):
  87. bytestream2_init(&s->gb, buf - 4, buf_size + 4);
  88. ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
  89. break;
  90. case MKTAG('A', 'A', 'S', 'C'):
  91. switch(compr){
  92. case 0:
  93. stride = (avctx->width * psize + psize) & ~psize;
  94. for(i = avctx->height - 1; i >= 0; i--){
  95. if(avctx->width * psize > buf_size){
  96. av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
  97. break;
  98. }
  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, (AVPicture*)&s->frame, 8, &s->gb);
  107. break;
  108. default:
  109. av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
  110. return -1;
  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 == PIX_FMT_PAL8)
  118. memcpy(s->frame.data[1], s->palette, s->palette_size);
  119. *data_size = sizeof(AVFrame);
  120. *(AVFrame*)data = s->frame;
  121. /* report that the buffer was completely consumed */
  122. return buf_size;
  123. }
  124. static av_cold int aasc_decode_end(AVCodecContext *avctx)
  125. {
  126. AascContext *s = avctx->priv_data;
  127. /* release the last frame */
  128. if (s->frame.data[0])
  129. avctx->release_buffer(avctx, &s->frame);
  130. return 0;
  131. }
  132. AVCodec ff_aasc_decoder = {
  133. .name = "aasc",
  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 = CODEC_CAP_DR1,
  141. .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
  142. };