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.

230 lines
6.1KB

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