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