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.

124 lines
3.3KB

  1. /*
  2. * Autodesk RLE Decoder
  3. * Copyright (C) 2005 The FFmpeg project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. } 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 = AV_PIX_FMT_BGR24;
  41. s->frame = av_frame_alloc();
  42. if (!s->frame)
  43. return AVERROR(ENOMEM);
  44. return 0;
  45. }
  46. static int aasc_decode_frame(AVCodecContext *avctx,
  47. void *data, int *got_frame,
  48. AVPacket *avpkt)
  49. {
  50. const uint8_t *buf = avpkt->data;
  51. int buf_size = avpkt->size;
  52. AascContext *s = avctx->priv_data;
  53. int compr, i, stride, ret;
  54. if (buf_size < 4)
  55. return AVERROR_INVALIDDATA;
  56. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0) {
  57. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  58. return ret;
  59. }
  60. compr = AV_RL32(buf);
  61. buf += 4;
  62. buf_size -= 4;
  63. switch (compr) {
  64. case 0:
  65. stride = (avctx->width * 3 + 3) & ~3;
  66. if (buf_size < stride * avctx->height)
  67. return AVERROR_INVALIDDATA;
  68. for (i = avctx->height - 1; i >= 0; i--) {
  69. memcpy(s->frame->data[0] + i * s->frame->linesize[0], buf, avctx->width * 3);
  70. buf += stride;
  71. }
  72. break;
  73. case 1:
  74. bytestream2_init(&s->gb, buf, buf_size);
  75. ff_msrle_decode(avctx, s->frame, 8, &s->gb);
  76. break;
  77. default:
  78. av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
  79. return AVERROR_INVALIDDATA;
  80. }
  81. *got_frame = 1;
  82. if ((ret = av_frame_ref(data, s->frame)) < 0)
  83. return ret;
  84. /* report that the buffer was completely consumed */
  85. return avpkt->size;
  86. }
  87. static av_cold int aasc_decode_end(AVCodecContext *avctx)
  88. {
  89. AascContext *s = avctx->priv_data;
  90. av_frame_free(&s->frame);
  91. return 0;
  92. }
  93. AVCodec ff_aasc_decoder = {
  94. .name = "aasc",
  95. .long_name = NULL_IF_CONFIG_SMALL("Autodesk RLE"),
  96. .type = AVMEDIA_TYPE_VIDEO,
  97. .id = AV_CODEC_ID_AASC,
  98. .priv_data_size = sizeof(AascContext),
  99. .init = aasc_decode_init,
  100. .close = aasc_decode_end,
  101. .decode = aasc_decode_frame,
  102. .capabilities = AV_CODEC_CAP_DR1,
  103. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  104. };