annotate ffmpeg/libavcodec/vble.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * VBLE Decoder
yading@10 3 * Copyright (c) 2011 Derek Buitenhuis
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * VBLE Decoder
yading@10 25 */
yading@10 26
yading@10 27 #define BITSTREAM_READER_LE
yading@10 28
yading@10 29 #include "avcodec.h"
yading@10 30 #include "dsputil.h"
yading@10 31 #include "get_bits.h"
yading@10 32 #include "internal.h"
yading@10 33 #include "mathops.h"
yading@10 34
yading@10 35 typedef struct {
yading@10 36 AVCodecContext *avctx;
yading@10 37 DSPContext dsp;
yading@10 38
yading@10 39 int size;
yading@10 40 uint8_t *val; ///< This array first holds the lengths of vlc symbols and then their value.
yading@10 41 } VBLEContext;
yading@10 42
yading@10 43 static int vble_unpack(VBLEContext *ctx, GetBitContext *gb)
yading@10 44 {
yading@10 45 int i;
yading@10 46 int allbits = 0;
yading@10 47 static const uint8_t LUT[256] = {
yading@10 48 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,
yading@10 49 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,
yading@10 50 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,
yading@10 51 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,
yading@10 52 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,
yading@10 53 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,
yading@10 54 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,
yading@10 55 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,
yading@10 56 };
yading@10 57
yading@10 58 /* Read all the lengths in first */
yading@10 59 for (i = 0; i < ctx->size; i++) {
yading@10 60 /* At most we need to read 9 bits total to get indices up to 8 */
yading@10 61 int val = show_bits(gb, 8);
yading@10 62
yading@10 63 // read reverse unary
yading@10 64 if (val) {
yading@10 65 val = LUT[val];
yading@10 66 skip_bits(gb, val + 1);
yading@10 67 ctx->val[i] = val;
yading@10 68 } else {
yading@10 69 skip_bits(gb, 8);
yading@10 70 if (!get_bits1(gb))
yading@10 71 return -1;
yading@10 72 ctx->val[i] = 8;
yading@10 73 }
yading@10 74 allbits += ctx->val[i];
yading@10 75 }
yading@10 76
yading@10 77 /* Check we have enough bits left */
yading@10 78 if (get_bits_left(gb) < allbits)
yading@10 79 return -1;
yading@10 80 return 0;
yading@10 81 }
yading@10 82
yading@10 83 static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
yading@10 84 GetBitContext *gb, int plane,
yading@10 85 int offset, int width, int height)
yading@10 86 {
yading@10 87 uint8_t *dst = pic->data[plane];
yading@10 88 uint8_t *val = ctx->val + offset;
yading@10 89 int stride = pic->linesize[plane];
yading@10 90 int i, j, left, left_top;
yading@10 91
yading@10 92 for (i = 0; i < height; i++) {
yading@10 93 for (j = 0; j < width; j++) {
yading@10 94 /* get_bits can't take a length of 0 */
yading@10 95 if (val[j]) {
yading@10 96 int v = (1 << val[j]) + get_bits(gb, val[j]) - 1;
yading@10 97 val[j] = (v >> 1) ^ -(v & 1);
yading@10 98 }
yading@10 99 }
yading@10 100 if (i) {
yading@10 101 left = 0;
yading@10 102 left_top = dst[-stride];
yading@10 103 ctx->dsp.add_hfyu_median_prediction(dst, dst-stride, val, width, &left, &left_top);
yading@10 104 } else {
yading@10 105 dst[0] = val[0];
yading@10 106 for (j = 1; j < width; j++)
yading@10 107 dst[j] = val[j] + dst[j - 1];
yading@10 108 }
yading@10 109 dst += stride;
yading@10 110 val += width;
yading@10 111 }
yading@10 112 }
yading@10 113
yading@10 114 static int vble_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
yading@10 115 AVPacket *avpkt)
yading@10 116 {
yading@10 117 VBLEContext *ctx = avctx->priv_data;
yading@10 118 AVFrame *pic = data;
yading@10 119 GetBitContext gb;
yading@10 120 const uint8_t *src = avpkt->data;
yading@10 121 int version;
yading@10 122 int offset = 0;
yading@10 123 int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
yading@10 124 int ret;
yading@10 125
yading@10 126 if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
yading@10 127 av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
yading@10 128 return AVERROR_INVALIDDATA;
yading@10 129 }
yading@10 130
yading@10 131 /* Allocate buffer */
yading@10 132 if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
yading@10 133 return ret;
yading@10 134
yading@10 135 /* Set flags */
yading@10 136 pic->key_frame = 1;
yading@10 137 pic->pict_type = AV_PICTURE_TYPE_I;
yading@10 138
yading@10 139 /* Version should always be 1 */
yading@10 140 version = AV_RL32(src);
yading@10 141
yading@10 142 if (version != 1)
yading@10 143 av_log(avctx, AV_LOG_WARNING, "Unsupported VBLE Version: %d\n", version);
yading@10 144
yading@10 145 init_get_bits(&gb, src + 4, (avpkt->size - 4) * 8);
yading@10 146
yading@10 147 /* Unpack */
yading@10 148 if (vble_unpack(ctx, &gb) < 0) {
yading@10 149 av_log(avctx, AV_LOG_ERROR, "Invalid Code\n");
yading@10 150 return AVERROR_INVALIDDATA;
yading@10 151 }
yading@10 152
yading@10 153 /* Restore planes. Should be almost identical to Huffyuv's. */
yading@10 154 vble_restore_plane(ctx, pic, &gb, 0, offset, avctx->width, avctx->height);
yading@10 155
yading@10 156 /* Chroma */
yading@10 157 if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
yading@10 158 offset += avctx->width * avctx->height;
yading@10 159 vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv);
yading@10 160
yading@10 161 offset += width_uv * height_uv;
yading@10 162 vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv);
yading@10 163 }
yading@10 164
yading@10 165 *got_frame = 1;
yading@10 166
yading@10 167 return avpkt->size;
yading@10 168 }
yading@10 169
yading@10 170 static av_cold int vble_decode_close(AVCodecContext *avctx)
yading@10 171 {
yading@10 172 VBLEContext *ctx = avctx->priv_data;
yading@10 173 av_freep(&ctx->val);
yading@10 174
yading@10 175 return 0;
yading@10 176 }
yading@10 177
yading@10 178 static av_cold int vble_decode_init(AVCodecContext *avctx)
yading@10 179 {
yading@10 180 VBLEContext *ctx = avctx->priv_data;
yading@10 181
yading@10 182 /* Stash for later use */
yading@10 183 ctx->avctx = avctx;
yading@10 184 ff_dsputil_init(&ctx->dsp, avctx);
yading@10 185
yading@10 186 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
yading@10 187 avctx->bits_per_raw_sample = 8;
yading@10 188
yading@10 189 ctx->size = avpicture_get_size(avctx->pix_fmt,
yading@10 190 avctx->width, avctx->height);
yading@10 191
yading@10 192 ctx->val = av_malloc(ctx->size * sizeof(*ctx->val));
yading@10 193
yading@10 194 if (!ctx->val) {
yading@10 195 av_log(avctx, AV_LOG_ERROR, "Could not allocate values buffer.\n");
yading@10 196 vble_decode_close(avctx);
yading@10 197 return AVERROR(ENOMEM);
yading@10 198 }
yading@10 199
yading@10 200 return 0;
yading@10 201 }
yading@10 202
yading@10 203 AVCodec ff_vble_decoder = {
yading@10 204 .name = "vble",
yading@10 205 .type = AVMEDIA_TYPE_VIDEO,
yading@10 206 .id = AV_CODEC_ID_VBLE,
yading@10 207 .priv_data_size = sizeof(VBLEContext),
yading@10 208 .init = vble_decode_init,
yading@10 209 .close = vble_decode_close,
yading@10 210 .decode = vble_decode_frame,
yading@10 211 .capabilities = CODEC_CAP_DR1,
yading@10 212 .long_name = NULL_IF_CONFIG_SMALL("VBLE Lossless Codec"),
yading@10 213 };