00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #define ALT_BITSTREAM_READER_LE
00028
00029 #include "avcodec.h"
00030 #include "dsputil.h"
00031 #include "get_bits.h"
00032
00033 typedef struct {
00034 AVCodecContext *avctx;
00035 DSPContext dsp;
00036
00037 int size;
00038 uint8_t *val;
00039 } VBLEContext;
00040
00041 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
00042 {
00043 int i;
00044 int allbits = 0;
00045 static const uint8_t LUT[256] = {
00046 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,
00047 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,
00048 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,
00049 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,
00050 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,
00051 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,
00052 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,
00053 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,
00054 };
00055
00056
00057 for (i = 0; i < ctx->size; i++) {
00058
00059 int val = show_bits(gb, 8);
00060
00061
00062 if (val) {
00063 val = LUT[val];
00064 skip_bits(gb, val + 1);
00065 ctx->val[i] = val;
00066 } else {
00067 skip_bits(gb, 8);
00068 if (!get_bits1(gb))
00069 return -1;
00070 ctx->val[i] = 8;
00071 }
00072 allbits += ctx->val[i];
00073 }
00074
00075
00076 if (get_bits_left(gb) < allbits)
00077 return -1;
00078 return 0;
00079 }
00080
00081 static void vble_restore_plane(VBLEContext *ctx, GetBitContext *gb, int plane,
00082 int offset, int width, int height)
00083 {
00084 AVFrame *pic = ctx->avctx->coded_frame;
00085 uint8_t *dst = pic->data[plane];
00086 uint8_t *val = ctx->val + offset;
00087 int stride = pic->linesize[plane];
00088 int i, j, left, left_top;
00089
00090 for (i = 0; i < height; i++) {
00091 for (j = 0; j < width; j++) {
00092
00093 if (val[j]) {
00094 int v = (1 << val[j]) + get_bits(gb, val[j]) - 1;
00095 val[j] = (v >> 1) ^ -(v & 1);
00096 }
00097 }
00098 if (i) {
00099 left = 0;
00100 left_top = dst[-stride];
00101 ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, width, &left, &left_top);
00102 } else {
00103 dst[0] = val[0];
00104 for (j = 1; j < width; j++)
00105 dst[j] = val[j] + dst[j - 1];
00106 }
00107 dst += stride;
00108 val += width;
00109 }
00110 }
00111
00112 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00113 AVPacket *avpkt)
00114 {
00115 VBLEContext *ctx = avctx->priv_data;
00116 AVFrame *pic = avctx->coded_frame;
00117 GetBitContext gb;
00118 const uint8_t *src = avpkt->data;
00119 int version;
00120 int offset = 0;
00121 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
00122
00123 pic->reference = 0;
00124
00125
00126 if (pic->data[0])
00127 avctx->release_buffer(avctx, pic);
00128
00129
00130 if (avctx->get_buffer(avctx, pic) < 0) {
00131 av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
00132 return AVERROR(ENOMEM);
00133 }
00134
00135
00136 pic->key_frame = 1;
00137 pic->pict_type = FF_I_TYPE;
00138
00139
00140 version = AV_RL32(src);
00141
00142 if (version != 1) {
00143 av_log(avctx, AV_LOG_ERROR, "Unsupported VBLE Version: %d\n", version);
00144 return AVERROR_INVALIDDATA;
00145 }
00146
00147 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
00148
00149
00150 if (vble_unpack(ctx, &gb) < 0) {
00151 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
00152 return AVERROR_INVALIDDATA;
00153 }
00154
00155
00156 vble_restore_plane(ctx, &gb, 0, offset, avctx->width, avctx->height);
00157
00158
00159 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
00160 offset += avctx->width * avctx->height;
00161 vble_restore_plane(ctx, &gb, 1, offset, width_uv, height_uv);
00162
00163 offset += width_uv * height_uv;
00164 vble_restore_plane(ctx, &gb, 2, offset, width_uv, height_uv);
00165 }
00166
00167 *data_size = sizeof(AVFrame);
00168 *(AVFrame *)data = *pic;
00169
00170 return avpkt->size;
00171 }
00172
00173 static av_cold int vble_decode_close(AVCodecContext *avctx)
00174 {
00175 VBLEContext *ctx = avctx->priv_data;
00176 AVFrame *pic = avctx->coded_frame;
00177
00178 if (pic->data[0])
00179 avctx->release_buffer(avctx, pic);
00180
00181 av_freep(&avctx->coded_frame);
00182 av_freep(&ctx->val);
00183
00184 return 0;
00185 }
00186
00187 static av_cold int vble_decode_init(AVCodecContext *avctx)
00188 {
00189 VBLEContext *ctx = avctx->priv_data;
00190
00191
00192 ctx->avctx = avctx;
00193 dsputil_init(&ctx->dsp, avctx);
00194
00195 avctx->pix_fmt = PIX_FMT_YUV420P;
00196 avctx->bits_per_raw_sample = 8;
00197 avctx->coded_frame = avcodec_alloc_frame();
00198
00199 if (!avctx->coded_frame) {
00200 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
00201 return AVERROR(ENOMEM);
00202 }
00203
00204 ctx->size = avpicture_get_size(avctx->pix_fmt,
00205 avctx->width, avctx->height);
00206
00207 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
00208
00209 if (!ctx->val) {
00210 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
00211 vble_decode_close(avctx);
00212 return AVERROR(ENOMEM);
00213 }
00214
00215 return 0;
00216 }
00217
00218 AVCodec ff_vble_decoder = {
00219 .name = "vble",
00220 .type = AVMEDIA_TYPE_VIDEO,
00221 .id = CODEC_ID_VBLE,
00222 .priv_data_size = sizeof(VBLEContext),
00223 .init = vble_decode_init,
00224 .close = vble_decode_close,
00225 .decode = vble_decode_frame,
00226 .capabilities = CODEC_CAP_DR1,
00227 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
00228 };