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.

228 lines
6.6KB

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