FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vble.c
Go to the documentation of this file.
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 /**
23  * @file
24  * VBLE Decoder
25  */
26 
27 #define BITSTREAM_READER_LE
28 
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "get_bits.h"
32 #include "internal.h"
33 #include "mathops.h"
34 
35 typedef struct {
38 
39  int size;
40  uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value.
41 } VBLEContext;
42 
43 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
44 {
45  int i;
46  int allbits = 0;
47  static const uint8_t LUT[256] = {
48  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,
49  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,
50  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,
51  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,
52  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,
53  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,
54  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,
55  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,
56  };
57 
58  /* Read all the lengths in first */
59  for (i = 0; i < ctx->size; i++) {
60  /* At most we need to read 9 bits total to get indices up to 8 */
61  int val = show_bits(gb, 8);
62 
63  // read reverse unary
64  if (val) {
65  val = LUT[val];
66  skip_bits(gb, val + 1);
67  ctx->val[i] = val;
68  } else {
69  skip_bits(gb, 8);
70  if (!get_bits1(gb))
71  return -1;
72  ctx->val[i] = 8;
73  }
74  allbits += ctx->val[i];
75  }
76 
77  /* Check we have enough bits left */
78  if (get_bits_left(gb) < allbits)
79  return -1;
80  return 0;
81 }
82 
83 static void vble_restore_plane(VBLEContext *ctx, GetBitContext *gb, int plane,
84  int offset, int width, int height)
85 {
86  AVFrame *pic = ctx->avctx->coded_frame;
87  uint8_t *dst = pic->data[plane];
88  uint8_t *val = ctx->val + offset;
89  int stride = pic->linesize[plane];
90  int i, j, left, left_top;
91 
92  for (i = 0; i < height; i++) {
93  for (j = 0; j < width; j++) {
94  /* get_bits can't take a length of 0 */
95  if (val[j]) {
96  int v = (1 << val[j]) + get_bits(gb, val[j]) - 1;
97  val[j] = (v >> 1) ^ -(v & 1);
98  }
99  }
100  if (i) {
101  left = 0;
102  left_top = dst[-stride];
103  ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, width, &left, &left_top);
104  } else {
105  dst[0] = val[0];
106  for (j = 1; j < width; j++)
107  dst[j] = val[j] + dst[j - 1];
108  }
109  dst += stride;
110  val += width;
111  }
112 }
113 
114 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
115  AVPacket *avpkt)
116 {
117  VBLEContext *ctx = avctx->priv_data;
118  AVFrame *pic = avctx->coded_frame;
119  GetBitContext gb;
120  const uint8_t *src = avpkt->data;
121  int version;
122  int offset = 0;
123  int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
124 
125  pic->reference = 0;
126 
127  /* Clear buffer if need be */
128  if (pic->data[0])
129  avctx->release_buffer(avctx, pic);
130 
131  if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
132  av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
133  return AVERROR_INVALIDDATA;
134  }
135 
136  /* Allocate buffer */
137  if (ff_get_buffer(avctx, pic) < 0) {
138  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
139  return AVERROR(ENOMEM);
140  }
141 
142  /* Set flags */
143  pic->key_frame = 1;
145 
146  /* Version should always be 1 */
147  version = AV_RL32(src);
148 
149  if (version != 1)
150  av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
151 
152  init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
153 
154  /* Unpack */
155  if (vble_unpack(ctx, &gb) < 0) {
156  av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
157  return AVERROR_INVALIDDATA;
158  }
159 
160  /* Restore planes. Should be almost identical to Huffyuv's. */
161  vble_restore_plane(ctx, &gb, 0, offset, avctx->width, avctx->height);
162 
163  /* Chroma */
164  if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
165  offset += avctx->width * avctx->height;
166  vble_restore_plane(ctx, &gb, 1, offset, width_uv, height_uv);
167 
168  offset += width_uv * height_uv;
169  vble_restore_plane(ctx, &gb, 2, offset, width_uv, height_uv);
170  }
171 
172  *got_frame = 1;
173  *(AVFrame *)data = *pic;
174 
175  return avpkt->size;
176 }
177 
179 {
180  VBLEContext *ctx = avctx->priv_data;
181  AVFrame *pic = avctx->coded_frame;
182 
183  if (pic->data[0])
184  avctx->release_buffer(avctx, pic);
185 
186  av_freep(&avctx->coded_frame);
187  av_freep(&ctx->val);
188 
189  return 0;
190 }
191 
193 {
194  VBLEContext *ctx = avctx->priv_data;
195 
196  /* Stash for later use */
197  ctx->avctx = avctx;
198  ff_dsputil_init(&ctx->dsp, avctx);
199 
200  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
201  avctx->bits_per_raw_sample = 8;
202  avctx->coded_frame = avcodec_alloc_frame();
203 
204  if (!avctx->coded_frame) {
205  av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
206  return AVERROR(ENOMEM);
207  }
208 
209  ctx->size = avpicture_get_size(avctx->pix_fmt,
210  avctx->width, avctx->height);
211 
212  ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
213 
214  if (!ctx->val) {
215  av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
216  vble_decode_close(avctx);
217  return AVERROR(ENOMEM);
218  }
219 
220  return 0;
221 }
222 
224  .name = "vble",
225  .type = AVMEDIA_TYPE_VIDEO,
226  .id = AV_CODEC_ID_VBLE,
227  .priv_data_size = sizeof(VBLEContext),
231  .capabilities = CODEC_CAP_DR1,
232  .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
233 };