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.

211 lines
5.7KB

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