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.

213 lines
5.8KB

  1. /*
  2. * VBLE Decoder
  3. * Copyright (c) 2011 Derek Buitenhuis
  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. * VBLE Decoder
  24. */
  25. #include "libavutil/imgutils.h"
  26. #define BITSTREAM_READER_LE
  27. #include "avcodec.h"
  28. #include "bitstream.h"
  29. #include "huffyuvdsp.h"
  30. #include "internal.h"
  31. #include "mathops.h"
  32. typedef struct VBLEContext {
  33. AVCodecContext *avctx;
  34. HuffYUVDSPContext hdsp;
  35. int size;
  36. uint8_t *val; /* First holds the lengths of vlc symbols and then their values */
  37. } VBLEContext;
  38. static uint8_t vble_read_reverse_unary(BitstreamContext *bc)
  39. {
  40. /* At most we need to read 9 bits total to get indices up to 8 */
  41. uint8_t val = bitstream_peek(bc, 8);
  42. if (val) {
  43. val = 7 - av_log2_16bit(ff_reverse[val]);
  44. bitstream_skip(bc, val + 1);
  45. return val;
  46. } else {
  47. bitstream_skip(bc, 8);
  48. if (bitstream_read_bit(bc))
  49. return 8;
  50. }
  51. /* Return something larger than 8 on error */
  52. return UINT8_MAX;
  53. }
  54. static int vble_unpack(VBLEContext *ctx, BitstreamContext *bc)
  55. {
  56. int i;
  57. /* Read all the lengths in first */
  58. for (i = 0; i < ctx->size; i++) {
  59. ctx->val[i] = vble_read_reverse_unary(bc);
  60. if (ctx->val[i] == UINT8_MAX)
  61. return -1;
  62. }
  63. for (i = 0; i < ctx->size; i++) {
  64. /* Check we have enough bits left */
  65. if (bitstream_bits_left(bc) < ctx->val[i])
  66. return -1;
  67. /* get_bits can't take a length of 0 */
  68. if (ctx->val[i])
  69. ctx->val[i] = (1 << ctx->val[i]) + bitstream_read(bc, ctx->val[i]) - 1;
  70. }
  71. return 0;
  72. }
  73. static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
  74. int plane, int offset,
  75. int width, int height)
  76. {
  77. uint8_t *dst = pic->data[plane];
  78. uint8_t *val = ctx->val + offset;
  79. int stride = pic->linesize[plane];
  80. int i, j, left, left_top;
  81. for (i = 0; i < height; i++) {
  82. for (j = 0; j < width; j++)
  83. val[j] = (val[j] >> 1) ^ -(val[j] & 1);
  84. if (i) {
  85. left = 0;
  86. left_top = dst[-stride];
  87. ctx->hdsp.add_hfyu_median_pred(dst, dst - stride, val,
  88. width, &left, &left_top);
  89. } else {
  90. dst[0] = val[0];
  91. for (j = 1; j < width; j++)
  92. dst[j] = val[j] + dst[j - 1];
  93. }
  94. dst += stride;
  95. val += width;
  96. }
  97. }
  98. static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  99. AVPacket *avpkt)
  100. {
  101. VBLEContext *ctx = avctx->priv_data;
  102. AVFrame *pic = data;
  103. BitstreamContext bc;
  104. const uint8_t *src = avpkt->data;
  105. int version;
  106. int offset = 0;
  107. int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
  108. /* Allocate buffer */
  109. if (ff_get_buffer(avctx, pic, 0) < 0) {
  110. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
  111. return AVERROR(ENOMEM);
  112. }
  113. /* Set flags */
  114. pic->key_frame = 1;
  115. pic->pict_type = AV_PICTURE_TYPE_I;
  116. /* Version should always be 1 */
  117. version = AV_RL32(src);
  118. if (version != 1)
  119. av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
  120. bitstream_init8(&bc, src + 4, avpkt->size - 4);
  121. /* Unpack */
  122. if (vble_unpack(ctx, &bc) < 0) {
  123. av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
  124. return AVERROR_INVALIDDATA;
  125. }
  126. /* Restore planes. Should be almost identical to Huffyuv's. */
  127. vble_restore_plane(ctx, pic, 0, offset, avctx->width, avctx->height);
  128. /* Chroma */
  129. if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  130. offset += avctx->width * avctx->height;
  131. vble_restore_plane(ctx, pic, 1, offset, width_uv, height_uv);
  132. offset += width_uv * height_uv;
  133. vble_restore_plane(ctx, pic, 2, offset, width_uv, height_uv);
  134. }
  135. *got_frame = 1;
  136. return avpkt->size;
  137. }
  138. static av_cold int vble_decode_close(AVCodecContext *avctx)
  139. {
  140. VBLEContext *ctx = avctx->priv_data;
  141. av_freep(&ctx->val);
  142. return 0;
  143. }
  144. static av_cold int vble_decode_init(AVCodecContext *avctx)
  145. {
  146. VBLEContext *ctx = avctx->priv_data;
  147. /* Stash for later use */
  148. ctx->avctx = avctx;
  149. ff_huffyuvdsp_init(&ctx->hdsp);
  150. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  151. avctx->bits_per_raw_sample = 8;
  152. ctx->size = av_image_get_buffer_size(avctx->pix_fmt,
  153. avctx->width, avctx->height, 1);
  154. ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
  155. if (!ctx->val) {
  156. av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
  157. vble_decode_close(avctx);
  158. return AVERROR(ENOMEM);
  159. }
  160. return 0;
  161. }
  162. AVCodec ff_vble_decoder = {
  163. .name = "vble",
  164. .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
  165. .type = AVMEDIA_TYPE_VIDEO,
  166. .id = AV_CODEC_ID_VBLE,
  167. .priv_data_size = sizeof(VBLEContext),
  168. .init = vble_decode_init,
  169. .close = vble_decode_close,
  170. .decode = vble_decode_frame,
  171. .capabilities = AV_CODEC_CAP_DR1,
  172. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  173. };