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.6KB

  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. static av_cold int sgirle_decode_init(AVCodecContext *avctx)
  31. {
  32. avctx->pix_fmt = AV_PIX_FMT_BGR8;
  33. return 0;
  34. }
  35. /**
  36. * Convert SGI RBG323 pixel into AV_PIX_FMT_BGR8
  37. * SGI RGB data is packed as 8bpp, (msb)3R 2B 3G(lsb)
  38. */
  39. #define RBG323_TO_BGR8(x) (((x << 3) & 0xC0) | \
  40. ((x << 3) & 0x38) | \
  41. ((x >> 5) & 7))
  42. static av_always_inline
  43. void rbg323_to_bgr8(uint8_t *dst, const uint8_t *src, int size)
  44. {
  45. int i;
  46. for (i = 0; i < size; i++)
  47. dst[i] = RBG323_TO_BGR8(src[i]);
  48. }
  49. /**
  50. * @param[out] dst Destination buffer
  51. * @param[in] src Source buffer
  52. * @param src_size Source buffer size (bytes)
  53. * @param width Width of destination buffer (pixels)
  54. * @param height Height of destination buffer (pixels)
  55. * @param linesize Line size of destination buffer (bytes)
  56. *
  57. * @return <0 on error
  58. */
  59. static int decode_sgirle8(AVCodecContext *avctx, uint8_t *dst,
  60. const uint8_t *src, int src_size,
  61. int width, int height, ptrdiff_t linesize)
  62. {
  63. const uint8_t *src_end = src + src_size;
  64. int x = 0, y = 0;
  65. #define INC_XY(n) \
  66. x += n; \
  67. if (x >= width) { \
  68. y++; \
  69. if (y >= height) \
  70. return 0; \
  71. x = 0; \
  72. }
  73. while (src_end - src >= 2) {
  74. uint8_t v = *src++;
  75. if (v > 0 && v < 0xC0) {
  76. do {
  77. int length = FFMIN(v, width - x);
  78. if (length <= 0)
  79. break;
  80. memset(dst + y * linesize + x, RBG323_TO_BGR8(*src), length);
  81. INC_XY(length);
  82. v -= length;
  83. } while (v > 0);
  84. src++;
  85. } else if (v >= 0xC1) {
  86. v -= 0xC0;
  87. do {
  88. int length = FFMIN3(v, width - x, src_end - src);
  89. if (src_end - src < length || length <= 0)
  90. break;
  91. rbg323_to_bgr8(dst + y * linesize + x, src, length);
  92. INC_XY(length);
  93. src += length;
  94. v -= length;
  95. } while (v > 0);
  96. } else {
  97. av_log(avctx, AV_LOG_ERROR, "Invalid opcode %d.\n", v);
  98. return AVERROR_INVALIDDATA;
  99. }
  100. }
  101. return 0;
  102. }
  103. static int sgirle_decode_frame(AVCodecContext *avctx, void *data,
  104. int *got_frame, AVPacket *avpkt)
  105. {
  106. AVFrame *frame = data;
  107. int ret;
  108. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  109. return ret;
  110. ret = decode_sgirle8(avctx, frame->data[0], avpkt->data, avpkt->size,
  111. avctx->width, avctx->height, frame->linesize[0]);
  112. if (ret < 0)
  113. return ret;
  114. frame->pict_type = AV_PICTURE_TYPE_I;
  115. frame->key_frame = 1;
  116. *got_frame = 1;
  117. return avpkt->size;
  118. }
  119. AVCodec ff_sgirle_decoder = {
  120. .name = "sgirle",
  121. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics RLE 8-bit video"),
  122. .type = AVMEDIA_TYPE_VIDEO,
  123. .id = AV_CODEC_ID_SGIRLE,
  124. .init = sgirle_decode_init,
  125. .decode = sgirle_decode_frame,
  126. .capabilities = AV_CODEC_CAP_DR1,
  127. };