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.

161 lines
5.1KB

  1. /*
  2. * Silicon Graphics RLE 8-bit video decoder
  3. * Copyright (c) 2012 Peter Ross
  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. * Silicon Graphics RLE 8-bit video decoder
  24. * @note Data is packed in rbg323 with rle, contained in mv or mov.
  25. * The algorithm and pixfmt are subtly different from SGI images.
  26. */
  27. #include "libavutil/common.h"
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. typedef struct SGIRLEContext {
  31. AVFrame *frame;
  32. } SGIRLEContext;
  33. static av_cold int sgirle_decode_init(AVCodecContext *avctx)
  34. {
  35. SGIRLEContext *s = avctx->priv_data;
  36. avctx->pix_fmt = AV_PIX_FMT_BGR8;
  37. s->frame = av_frame_alloc();
  38. if (!s->frame)
  39. return AVERROR(ENOMEM);
  40. return 0;
  41. }
  42. /**
  43. * Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8
  44. * SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb)
  45. */
  46. #define RBG323_TO_BGR8(x) (((x << 3) & 0xC0) | \
  47. ((x << 3) & 0x38) | \
  48. ((x >> 5) & 7))
  49. static av_always_inline
  50. void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
  51. {
  52. int i;
  53. for (i = 0; i < size; i++)
  54. dst[i] = RBG323_TO_BGR8(src[i]);
  55. }
  56. /**
  57. * @param[out] dst Destination buffer
  58. * @param[in] src Source buffer
  59. * @param src_size Source buffer size (bytes)
  60. * @param width Width of destination buffer (pixels)
  61. * @param height Height of destination buffer (pixels)
  62. * @param linesize Line size of destination buffer (bytes)
  63. *
  64. * @return <0 on error
  65. */
  66. static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst,
  67. const uint8_t *src, int src_size,
  68. int width, int height, ptrdiff_t linesize)
  69. {
  70. const uint8_t *src_end = src + src_size;
  71. int x = 0, y = 0;
  72. #define INC_XY(n) \
  73. x += n; \
  74. if (x >= width) { \
  75. y++; \
  76. if (y >= height) \
  77. return 0; \
  78. x = 0; \
  79. }
  80. while (src_end - src >= 2) {
  81. uint8_t v = *src++;
  82. if (v > 0 && v < 0xC0) {
  83. do {
  84. int length = FFMIN(v, width - x);
  85. if (length <= 0)
  86. break;
  87. memset(dst + y * linesize + x, RBG323_TO_BGR8(*src), length);
  88. INC_XY(length);
  89. v -= length;
  90. } while (v > 0);
  91. src++;
  92. } else if (v >= 0xC1) {
  93. v -= 0xC0;
  94. do {
  95. int length = FFMIN3(v, width - x, src_end - src);
  96. if (src_end - src < length || length <= 0)
  97. break;
  98. rbg323_to_bgr8(dst + y * linesize + x, src, length);
  99. INC_XY(length);
  100. src += length;
  101. v -= length;
  102. } while (v > 0);
  103. } else {
  104. av_log(avctx, AV_LOG_ERROR, "Invalid opcode %d.\n", v);
  105. return AVERROR_INVALIDDATA;
  106. }
  107. }
  108. return 0;
  109. }
  110. static int sgirle_decode_frame(AVCodecContext *avctx, void *data,
  111. int *got_frame, AVPacket *avpkt)
  112. {
  113. SGIRLEContext *s = avctx->priv_data;
  114. int ret;
  115. if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
  116. return ret;
  117. ret = decode_sgirle8(avctx, s->frame->data[0], avpkt->data, avpkt->size,
  118. avctx->width, avctx->height, s->frame->linesize[0]);
  119. if (ret < 0)
  120. return ret;
  121. *got_frame = 1;
  122. if ((ret = av_frame_ref(data, s->frame)) < 0)
  123. return ret;
  124. return avpkt->size;
  125. }
  126. static av_cold int sgirle_decode_end(AVCodecContext *avctx)
  127. {
  128. SGIRLEContext *s = avctx->priv_data;
  129. av_frame_free(&s->frame);
  130. return 0;
  131. }
  132. AVCodec ff_sgirle_decoder = {
  133. .name = "sgirle",
  134. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics RLE 8-bit video"),
  135. .type = AVMEDIA_TYPE_VIDEO,
  136. .id = AV_CODEC_ID_SGIRLE,
  137. .priv_data_size = sizeof(SGIRLEContext),
  138. .init = sgirle_decode_init,
  139. .close = sgirle_decode_end,
  140. .decode = sgirle_decode_frame,
  141. .capabilities = AV_CODEC_CAP_DR1,
  142. };