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.

143 lines
4.0KB

  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. } AascContext;
  36. static av_cold int aasc_decode_init(AVCodecContext *avctx)
  37. {
  38. AascContext *s = avctx->priv_data;
  39. s->avctx = avctx;
  40. switch (avctx->bits_per_coded_sample) {
  41. case 16:
  42. avctx->pix_fmt = PIX_FMT_RGB555;
  43. break;
  44. case 24:
  45. avctx->pix_fmt = PIX_FMT_BGR24;
  46. break;
  47. default:
  48. av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_coded_sample);
  49. return -1;
  50. }
  51. avcodec_get_frame_defaults(&s->frame);
  52. return 0;
  53. }
  54. static int aasc_decode_frame(AVCodecContext *avctx,
  55. void *data, int *data_size,
  56. AVPacket *avpkt)
  57. {
  58. const uint8_t *buf = avpkt->data;
  59. int buf_size = avpkt->size;
  60. AascContext *s = avctx->priv_data;
  61. int compr, i, stride;
  62. s->frame.reference = 3;
  63. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  64. if (avctx->reget_buffer(avctx, &s->frame)) {
  65. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  66. return -1;
  67. }
  68. compr = AV_RL32(buf);
  69. buf += 4;
  70. buf_size -= 4;
  71. switch (avctx->codec_tag) {
  72. case MKTAG('A', 'A', 'S', '4'):
  73. bytestream2_init(&s->gb, buf - 4, buf_size + 4);
  74. ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
  75. break;
  76. case MKTAG('A', 'A', 'S', 'C'):
  77. switch(compr){
  78. case 0:
  79. stride = (avctx->width * 3 + 3) & ~3;
  80. for(i = avctx->height - 1; i >= 0; i--){
  81. if(avctx->width*3 > buf_size){
  82. av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
  83. break;
  84. }
  85. memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width*3);
  86. buf += stride;
  87. buf_size -= stride;
  88. }
  89. break;
  90. case 1:
  91. bytestream2_init(&s->gb, buf, buf_size);
  92. ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
  93. break;
  94. default:
  95. av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
  96. return -1;
  97. }
  98. break;
  99. default:
  100. av_log(avctx, AV_LOG_ERROR, "Unknown FourCC: %X\n", avctx->codec_tag);
  101. return -1;
  102. }
  103. *data_size = sizeof(AVFrame);
  104. *(AVFrame*)data = s->frame;
  105. /* report that the buffer was completely consumed */
  106. return buf_size;
  107. }
  108. static av_cold int aasc_decode_end(AVCodecContext *avctx)
  109. {
  110. AascContext *s = avctx->priv_data;
  111. /* release the last frame */
  112. if (s->frame.data[0])
  113. avctx->release_buffer(avctx, &s->frame);
  114. return 0;
  115. }
  116. AVCodec ff_aasc_decoder = {
  117. .name = "aasc",
  118. .type = AVMEDIA_TYPE_VIDEO,
  119. .id = CODEC_ID_AASC,
  120. .priv_data_size = sizeof(AascContext),
  121. .init = aasc_decode_init,
  122. .close = aasc_decode_end,
  123. .decode = aasc_decode_frame,
  124. .capabilities = CODEC_CAP_DR1,
  125. .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
  126. };