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.

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