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.

152 lines
4.3KB

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