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.

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