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