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