FFmpeg  2.1.1
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, AVFrame *pic,
84  GetBitContext *gb, int plane,
85  int offset, int width, int height)
86 {
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 = data;
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  int ret;
125 
126  if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
127  av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
128  return AVERROR_INVALIDDATA;
129  }
130 
131  /* Allocate buffer */
132  if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
133  return ret;
134 
135  /* Set flags */
136  pic->key_frame = 1;
138 
139  /* Version should always be 1 */
140  version = AV_RL32(src);
141 
142  if (version != 1)
143  av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
144 
145  init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
146 
147  /* Unpack */
148  if (vble_unpack(ctx, &gb) < 0) {
149  av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
150  return AVERROR_INVALIDDATA;
151  }
152 
153  /* Restore planes. Should be almost identical to Huffyuv's. */
154  vble_restore_plane(ctx, pic, &gb, 0, offset, avctx->width, avctx->height);
155 
156  /* Chroma */
157  if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
158  offset += avctx->width * avctx->height;
159  vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv);
160 
161  offset += width_uv * height_uv;
162  vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv);
163  }
164 
165  *got_frame = 1;
166 
167  return avpkt->size;
168 }
169 
171 {
172  VBLEContext *ctx = avctx->priv_data;
173  av_freep(&ctx->val);
174 
175  return 0;
176 }
177 
179 {
180  VBLEContext *ctx = avctx->priv_data;
181 
182  /* Stash for later use */
183  ctx->avctx = avctx;
184  ff_dsputil_init(&ctx->dsp, avctx);
185 
186  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
187  avctx->bits_per_raw_sample = 8;
188 
189  ctx->size = avpicture_get_size(avctx->pix_fmt,
190  avctx->width, avctx->height);
191 
192  ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
193 
194  if (!ctx->val) {
195  av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
196  vble_decode_close(avctx);
197  return AVERROR(ENOMEM);
198  }
199 
200  return 0;
201 }
202 
204  .name = "vble",
205  .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
206  .type = AVMEDIA_TYPE_VIDEO,
207  .id = AV_CODEC_ID_VBLE,
208  .priv_data_size = sizeof(VBLEContext),
212  .capabilities = CODEC_CAP_DR1,
213 };
const char const char void * val
Definition: avisynth_c.h:671
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2678
float v
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVCodec ff_vble_decoder
Definition: vble.c:203
static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: vble.c:114
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
int version
Definition: avisynth_c.h:666
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2570
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
uint8_t
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
Definition: vble.c:43
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:583
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
Libavcodec external API header.
void(* add_hfyu_median_prediction)(uint8_t *dst, const uint8_t *top, const uint8_t *diff, int w, int *left, int *left_top)
Definition: dsputil.h:201
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:282
int size
Definition: vble.c:39
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
static int width
Definition: utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
static av_cold int vble_decode_close(AVCodecContext *avctx)
Definition: vble.c:170
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
uint8_t * val
This array first holds the lengths of vlc symbols and then their value.
Definition: vble.c:40
uint8_t * data
Definition: avcodec.h:1063
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:405
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
void * priv_data
Definition: avcodec.h:1182
common internal api header.
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:705
AVCodecContext * avctx
Definition: vble.c:36
DSP utils.
static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic, GetBitContext *gb, int plane, int offset, int width, int height)
Definition: vble.c:83
#define AVERROR_INVALIDDATA
#define AV_RL32(x)
Definition: intreadwrite.h:275
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:49
int key_frame
1 -&gt; keyframe, 0-&gt; not
Definition: frame.h:162
#define AVERROR(e)
static av_cold int vble_decode_init(AVCodecContext *avctx)
Definition: vble.c:178
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
DSPContext.
Definition: dsputil.h:124
DSPContext dsp
Definition: vble.c:37