]> git.sesse.net Git - ffmpeg/blob - libavcodec/vble.c
dxva2: Keep code shared between dxva2 and d3d11va under the correct #if
[ffmpeg] / libavcodec / vble.c
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 /**
23  * @file
24  * VBLE Decoder
25  */
26
27 #include "libavutil/imgutils.h"
28
29 #define BITSTREAM_READER_LE
30 #include "avcodec.h"
31 #include "bitstream.h"
32 #include "huffyuvdsp.h"
33 #include "internal.h"
34 #include "mathops.h"
35
36 typedef struct VBLEContext {
37     AVCodecContext *avctx;
38     HuffYUVDSPContext hdsp;
39
40     int            size;
41     uint8_t        *val; /* First holds the lengths of vlc symbols and then their values */
42 } VBLEContext;
43
44 static uint8_t vble_read_reverse_unary(BitstreamContext *bc)
45 {
46     /* At most we need to read 9 bits total to get indices up to 8 */
47     uint8_t val = bitstream_peek(bc, 8);
48
49     if (val) {
50         val = 7 - av_log2_16bit(ff_reverse[val]);
51         bitstream_skip(bc, val + 1);
52         return val;
53     } else {
54         bitstream_skip(bc, 8);
55         if (bitstream_read_bit(bc))
56             return 8;
57     }
58
59     /* Return something larger than 8 on error */
60     return UINT8_MAX;
61 }
62
63 static int vble_unpack(VBLEContext *ctx, BitstreamContext *bc)
64 {
65     int i;
66
67     /* Read all the lengths in first */
68     for (i = 0; i < ctx->size; i++) {
69         ctx->val[i] = vble_read_reverse_unary(bc);
70
71         if (ctx->val[i] == UINT8_MAX)
72             return -1;
73     }
74
75     for (i = 0; i < ctx->size; i++) {
76         /* Check we have enough bits left */
77         if (bitstream_bits_left(bc) < ctx->val[i])
78             return -1;
79
80         /* get_bits can't take a length of 0 */
81         if (ctx->val[i])
82             ctx->val[i] = (1 << ctx->val[i]) + bitstream_read(bc, ctx->val[i]) - 1;
83     }
84
85     return 0;
86 }
87
88 static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
89                                int plane, int offset,
90                                int width, int height)
91 {
92     uint8_t *dst = pic->data[plane];
93     uint8_t *val = ctx->val + offset;
94     int stride = pic->linesize[plane];
95     int i, j, left, left_top;
96
97     for (i = 0; i < height; i++) {
98         for (j = 0; j < width; j++)
99             val[j] = (val[j] >> 1) ^ -(val[j] & 1);
100
101         if (i) {
102             left = 0;
103             left_top = dst[-stride];
104             ctx->hdsp.add_hfyu_median_pred(dst, dst - stride, val,
105                                            width, &left, &left_top);
106         } else {
107             dst[0] = val[0];
108             for (j = 1; j < width; j++)
109                 dst[j] = val[j] + dst[j - 1];
110         }
111         dst += stride;
112         val += width;
113     }
114 }
115
116 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
117                              AVPacket *avpkt)
118 {
119     VBLEContext *ctx = avctx->priv_data;
120     AVFrame *pic     = data;
121     BitstreamContext bc;
122     const uint8_t *src = avpkt->data;
123     int version;
124     int offset = 0;
125     int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
126
127     /* Allocate buffer */
128     if (ff_get_buffer(avctx, pic, 0) < 0) {
129         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
130         return AVERROR(ENOMEM);
131     }
132
133     /* Set flags */
134     pic->key_frame = 1;
135     pic->pict_type = AV_PICTURE_TYPE_I;
136
137     /* Version should always be 1 */
138     version = AV_RL32(src);
139
140     if (version != 1)
141         av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
142
143     bitstream_init(&bc, src + 4, (avpkt->size - 4) * 8);
144
145     /* Unpack */
146     if (vble_unpack(ctx, &bc) < 0) {
147         av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
148         return AVERROR_INVALIDDATA;
149     }
150
151     /* Restore planes. Should be almost identical to Huffyuv's. */
152     vble_restore_plane(ctx, pic, 0, offset, avctx->width, avctx->height);
153
154     /* Chroma */
155     if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
156         offset += avctx->width * avctx->height;
157         vble_restore_plane(ctx, pic, 1, offset, width_uv, height_uv);
158
159         offset += width_uv * height_uv;
160         vble_restore_plane(ctx, pic, 2, offset, width_uv, height_uv);
161     }
162
163     *got_frame       = 1;
164
165     return avpkt->size;
166 }
167
168 static av_cold int vble_decode_close(AVCodecContext *avctx)
169 {
170     VBLEContext *ctx = avctx->priv_data;
171     av_freep(&ctx->val);
172
173     return 0;
174 }
175
176 static av_cold int vble_decode_init(AVCodecContext *avctx)
177 {
178     VBLEContext *ctx = avctx->priv_data;
179
180     /* Stash for later use */
181     ctx->avctx = avctx;
182     ff_huffyuvdsp_init(&ctx->hdsp);
183
184     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
185     avctx->bits_per_raw_sample = 8;
186
187     ctx->size = av_image_get_buffer_size(avctx->pix_fmt,
188                                          avctx->width, avctx->height, 1);
189
190     ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
191
192     if (!ctx->val) {
193         av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
194         vble_decode_close(avctx);
195         return AVERROR(ENOMEM);
196     }
197
198     return 0;
199 }
200
201 AVCodec ff_vble_decoder = {
202     .name           = "vble",
203     .long_name      = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
204     .type           = AVMEDIA_TYPE_VIDEO,
205     .id             = AV_CODEC_ID_VBLE,
206     .priv_data_size = sizeof(VBLEContext),
207     .init           = vble_decode_init,
208     .close          = vble_decode_close,
209     .decode         = vble_decode_frame,
210     .capabilities   = AV_CODEC_CAP_DR1,
211 };