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.

231 lines
6.2KB

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