Commit 5ce117c3 authored by Aurelien Jacobs's avatar Aurelien Jacobs
Browse files

VP5 and VP6 video decoder

Originally committed as revision 6213 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 34a8dcd0
......@@ -56,6 +56,8 @@ version <next>
- MacIntel support
- AVISynth support
- VMware video decoder
- VP5 video decoder
- VP6 video decoder
version 0.4.9-pre1:
......
......@@ -164,6 +164,8 @@ Codecs:
vcr1.c Michael Niedermayer
vmnc.c Kostya Shishkov
vp3* Mike Melanson
vp5 Aurelien Jacobs
vp6 Aurelien Jacobs
vqavideo.c Mike Melanson
wmv2.c Michael Niedermayer
wnv1.c Kostya Shishkov
......
......@@ -778,6 +778,8 @@ following image formats are supported:
@item Sorenson Video 1 @tab X @tab X @tab fourcc: SVQ1
@item Sorenson Video 3 @tab @tab X @tab fourcc: SVQ3
@item On2 VP3 @tab @tab X @tab still experimental
@item On2 VP5 @tab @tab X @tab fourcc: VP50
@item On2 VP6 @tab @tab X @tab fourcc: VP62
@item Theora @tab @tab X @tab still experimental
@item Intel Indeo 3 @tab @tab X
@item FLV @tab X @tab X @tab Sorenson H.263 used in Flash
......
......@@ -123,6 +123,8 @@ OBJS-$(CONFIG_VMDVIDEO_DECODER) += vmdav.o
OBJS-$(CONFIG_VMNC_DECODER) += vmnc.o
OBJS-$(CONFIG_VORBIS_DECODER) += vorbis.o
OBJS-$(CONFIG_VP3_DECODER) += vp3.o
OBJS-$(CONFIG_VP5_DECODER) += vp5.o vp56.o vp56data.o
OBJS-$(CONFIG_VP6_DECODER) += vp6.o vp56.o vp56data.o
OBJS-$(CONFIG_VQA_DECODER) += vqavideo.o
OBJS-$(CONFIG_WMAV1_DECODER) += wmadec.o
OBJS-$(CONFIG_WMAV2_DECODER) += wmadec.o
......
......@@ -371,6 +371,15 @@ void avcodec_register_all(void)
#ifdef CONFIG_THEORA_DECODER
register_avcodec(&theora_decoder);
#endif //CONFIG_THEORA_DECODER
#ifdef CONFIG_VP5_DECODER
register_avcodec(&vp5_decoder);
#endif //CONFIG_VP5_DECODER
#ifdef CONFIG_VP6_DECODER
register_avcodec(&vp6_decoder);
#endif //CONFIG_VP6_DECODER
#ifdef CONFIG_VP6F_DECODER
register_avcodec(&vp6f_decoder);
#endif //CONFIG_VP6F_DECODER
#ifdef CONFIG_ASV1_DECODER
register_avcodec(&asv1_decoder);
#endif //CONFIG_ASV1_DECODER
......
......@@ -17,8 +17,8 @@ extern "C" {
#define AV_STRINGIFY(s) AV_TOSTRING(s)
#define AV_TOSTRING(s) #s
#define LIBAVCODEC_VERSION_INT ((51<<16)+(13<<8)+0)
#define LIBAVCODEC_VERSION 51.13.0
#define LIBAVCODEC_VERSION_INT ((51<<16)+(14<<8)+0)
#define LIBAVCODEC_VERSION 51.14.0
#define LIBAVCODEC_BUILD LIBAVCODEC_VERSION_INT
#define LIBAVCODEC_IDENT "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
......@@ -121,6 +121,9 @@ enum CodecID {
CODEC_ID_CAVS,
CODEC_ID_JPEG2000,
CODEC_ID_VMNC,
CODEC_ID_VP5,
CODEC_ID_VP6,
CODEC_ID_VP6F,
/* various pcm "codecs" */
CODEC_ID_PCM_S16LE= 0x10000,
......@@ -2192,6 +2195,9 @@ extern AVCodec h264_decoder;
extern AVCodec indeo3_decoder;
extern AVCodec vp3_decoder;
extern AVCodec theora_decoder;
extern AVCodec vp5_decoder;
extern AVCodec vp6_decoder;
extern AVCodec vp6f_decoder;
extern AVCodec amr_nb_decoder;
extern AVCodec amr_nb_encoder;
extern AVCodec amr_wb_encoder;
......
/**
* @file vp5.c
* VP5 compatible video decoder
*
* Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "avcodec.h"
#include "dsputil.h"
#include "bitstream.h"
#include "mpegvideo.h"
#include "vp56.h"
#include "vp56data.h"
#include "vp5data.h"
static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
int *golden_frame)
{
vp56_range_coder_t *c = &s->c;
int rows, cols;
vp56_init_range_decoder(&s->c, buf, buf_size);
s->frames[VP56_FRAME_CURRENT].key_frame = !vp56_rac_get(c);
vp56_rac_get(c);
vp56_init_dequant(s, vp56_rac_gets(c, 6));
if (s->frames[VP56_FRAME_CURRENT].key_frame)
{
vp56_rac_gets(c, 8);
if(vp56_rac_gets(c, 5) > 5)
return 0;
vp56_rac_gets(c, 2);
if (vp56_rac_get(c)) {
av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
return 0;
}
rows = vp56_rac_gets(c, 8); /* number of stored macroblock rows */
cols = vp56_rac_gets(c, 8); /* number of stored macroblock cols */
vp56_rac_gets(c, 8); /* number of displayed macroblock rows */
vp56_rac_gets(c, 8); /* number of displayed macroblock cols */
vp56_rac_gets(c, 2);
if (16*cols != s->avctx->coded_width ||
16*rows != s->avctx->coded_height) {
avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
return 2;
}
}
return 1;
}
/* Gives very similar result than the vp6 version except in a few cases */
static int vp5_adjust(int v, int t)
{
int s2, s1 = v >> 31;
v ^= s1;
v -= s1;
v *= v < 2*t;
v -= t;
s2 = v >> 31;
v ^= s2;
v -= s2;
v = t - v;
v += s1;
v ^= s1;
return v;
}
static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vector)
{
vp56_range_coder_t *c = &s->c;
int comp, di;
for (comp=0; comp<2; comp++) {
int delta = 0;
if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
int sign = vp56_rac_get_prob(c, s->vector_model_sig[comp]);
di = vp56_rac_get_prob(c, s->vector_model_pdi[comp][0]);
di |= vp56_rac_get_prob(c, s->vector_model_pdi[comp][1]) << 1;
delta = vp56_rac_get_tree(c, vp56_pva_tree,
s->vector_model_pdv[comp]);
delta = di | (delta << 2);
delta = (delta ^ -sign) + sign;
}
if (!comp)
vector->x = delta;
else
vector->y = delta;
}
}
static void vp5_parse_vector_models(vp56_context_t *s)
{
vp56_range_coder_t *c = &s->c;
int comp, node;
for (comp=0; comp<2; comp++) {
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2]))
s->vector_model_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
s->vector_model_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
}
for (comp=0; comp<2; comp++)
for (node=0; node<7; node++)
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node]))
s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
}
static void vp5_parse_coeff_models(vp56_context_t *s)
{
vp56_range_coder_t *c = &s->c;
uint8_t def_prob[11];
int node, cg, ctx;
int ct; /* code type */
int pt; /* plane type (0 for Y, 1 for U or V) */
memset(def_prob, 0x80, sizeof(def_prob));
for (pt=0; pt<2; pt++)
for (node=0; node<11; node++)
if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
def_prob[node] = vp56_rac_gets_nn(c, 7);
s->coeff_model_dccv[pt][node] = def_prob[node];
} else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
s->coeff_model_dccv[pt][node] = def_prob[node];
}
for (ct=0; ct<3; ct++)
for (pt=0; pt<2; pt++)
for (cg=0; cg<6; cg++)
for (node=0; node<11; node++)
if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
def_prob[node] = vp56_rac_gets_nn(c, 7);
s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
} else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
}
/* coeff_model_dcct is a linear combination of coeff_model_dccv */
for (pt=0; pt<2; pt++)
for (ctx=0; ctx<36; ctx++)
for (node=0; node<5; node++)
s->coeff_model_dcct[pt][ctx][node] = clip(((s->coeff_model_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
/* coeff_model_acct is a linear combination of coeff_model_ract */
for (ct=0; ct<3; ct++)
for (pt=0; pt<2; pt++)
for (cg=0; cg<3; cg++)
for (ctx=0; ctx<6; ctx++)
for (node=0; node<5; node++)
s->coeff_model_acct[pt][ct][cg][ctx][node] = clip(((s->coeff_model_ract[pt][ct][cg][node] * vp5_ract_lc[ct][cg][node][ctx][0] + 128) >> 8) + vp5_ract_lc[ct][cg][node][ctx][1], 1, 254);
}
static void vp5_parse_coeff(vp56_context_t *s)
{
vp56_range_coder_t *c = &s->c;
uint8_t *permute = s->scantable.permutated;
uint8_t *model, *model2;
int coeff, sign, coeff_idx;
int b, i, cg, idx, ctx, ctx_last;
int pt = 0; /* plane type (0 for Y, 1 for U or V) */
for (b=0; b<6; b++) {
int ct = 1; /* code type */
if (b > 3) pt = 1;
ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
+ s->above_blocks[s->above_block_idx[b]].not_null_dc;
model = s->coeff_model_dccv[pt];
model2 = s->coeff_model_dcct[pt][ctx];
for (coeff_idx=0; coeff_idx<64; ) {
if (vp56_rac_get_prob(c, model2[0])) {
if (vp56_rac_get_prob(c, model2[2])) {
if (vp56_rac_get_prob(c, model2[3])) {
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
sign = vp56_rac_get(c);
coeff = vp56_coeff_bias[idx];
for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
} else {
if (vp56_rac_get_prob(c, model2[4])) {
coeff = 3 + vp56_rac_get_prob(c, model[5]);
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
} else {
coeff = 2;
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
}
sign = vp56_rac_get(c);
}
ct = 2;
} else {
ct = 1;
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
sign = vp56_rac_get(c);
coeff = 1;
}
coeff = (coeff ^ -sign) + sign;
if (coeff_idx)
coeff *= s->dequant_ac;
s->block_coeff[b][permute[coeff_idx]] = coeff;
} else {
if (ct && !vp56_rac_get_prob(c, model2[1]))
break;
ct = 0;
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
}
cg = vp5_coeff_groups[++coeff_idx];
ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
model = s->coeff_model_ract[pt][ct][cg];
model2 = cg > 2 ? model : s->coeff_model_acct[pt][ct][cg][ctx];
}
ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
if (coeff_idx < ctx_last)
for (i=coeff_idx; i<=ctx_last; i++)
s->coeff_ctx[vp56_b6to4[b]][i] = 5;
s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
}
}
static void vp5_default_models_init(vp56_context_t *s)
{
int i;
for (i=0; i<2; i++) {
s->vector_model_sig[i] = 0x80;
s->vector_model_dct[i] = 0x80;
s->vector_model_pdi[i][0] = 0x55;
s->vector_model_pdi[i][1] = 0x80;
}
memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
memset(s->vector_model_pdv, 0x80, sizeof(s->vector_model_pdv));
}
static int vp5_decode_init(AVCodecContext *avctx)
{
vp56_context_t *s = avctx->priv_data;
vp56_init(s, avctx, 1);
s->vp56_coord_div = vp5_coord_div;
s->parse_vector_adjustment = vp5_parse_vector_adjustment;
s->adjust = vp5_adjust;
s->parse_coeff = vp5_parse_coeff;
s->default_models_init = vp5_default_models_init;
s->parse_vector_models = vp5_parse_vector_models;
s->parse_coeff_models = vp5_parse_coeff_models;
s->parse_header = vp5_parse_header;
return 0;
}
AVCodec vp5_decoder = {
"vp5",
CODEC_TYPE_VIDEO,
CODEC_ID_VP5,
sizeof(vp56_context_t),
vp5_decode_init,
NULL,
vp56_free,
vp56_decode_frame,
};
This diff is collapsed.
/**
* @file vp56.h
* VP5 and VP6 compatible video decoder (common features)
*
* Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef VP56_H
#define VP56_H
#include <stdint.h>
#include "vp56data.h"
#include "dsputil.h"
#include "mpegvideo.h"
typedef struct vp56_context vp56_context_t;
typedef struct vp56_mv vp56_mv_t;
typedef void (*vp56_parse_vector_adjustment_t)(vp56_context_t *s,
vp56_mv_t *vector);
typedef int (*vp56_adjust_t)(int v, int t);
typedef void (*vp56_filter_t)(vp56_context_t *s, uint8_t *dst, uint8_t *src,
int offset1, int offset2, int stride,
vp56_mv_t mv, int mask, int select, int luma);
typedef void (*vp56_parse_coeff_t)(vp56_context_t *s);
typedef void (*vp56_default_models_init_t)(vp56_context_t *s);
typedef void (*vp56_parse_vector_models_t)(vp56_context_t *s);
typedef void (*vp56_parse_coeff_models_t)(vp56_context_t *s);
typedef int (*vp56_parse_header_t)(vp56_context_t *s, uint8_t *buf,
int buf_size, int *golden_frame);
typedef struct {
int high;
int bits;
const uint8_t *buffer;
unsigned long code_word;
} vp56_range_coder_t;
typedef struct {
uint8_t not_null_dc;
vp56_frame_t ref_frame;
DCTELEM dc_coeff;
} vp56_ref_dc_t;
struct vp56_mv {
int x;
int y;
};
typedef struct {
uint8_t type;
vp56_mv_t mv;
} vp56_macroblock_t;
struct vp56_context {
AVCodecContext *avctx;
DSPContext dsp;
ScanTable scantable;
AVFrame frames[3];
uint8_t *edge_emu_buffer_alloc;
uint8_t *edge_emu_buffer;
vp56_range_coder_t c;
/* frame info */
int plane_width[3];
int plane_height[3];
int mb_width; /* number of horizontal MB */
int mb_height; /* number of vertical MB */
int block_offset[6];
int quantizer;
uint16_t dequant_dc;
uint16_t dequant_ac;
/* DC predictors management */
vp56_ref_dc_t *above_blocks;
vp56_ref_dc_t left_block[4];
int above_block_idx[6];
DCTELEM prev_dc[3][3]; /* [plan][ref_frame] */
/* blocks / macroblock */
vp56_mb_t mb_type;
vp56_macroblock_t *macroblocks;
DECLARE_ALIGNED_16(DCTELEM, block_coeff[6][64]);
uint8_t coeff_reorder[64]; /* used in vp6 only */
uint8_t coeff_index_to_pos[64]; /* used in vp6 only */
/* motion vectors */
vp56_mv_t mv[6]; /* vectors for each block in MB */
vp56_mv_t vector_candidate[2];
int vector_candidate_pos;
/* filtering hints */
int deblock_filtering;
int filter_selection;
int filter_mode;
int max_vector_length;
int sample_variance_threshold;
/* AC models */
uint8_t vector_model_sig[2]; /* delta sign */
uint8_t vector_model_dct[2]; /* delta coding types */
uint8_t vector_model_pdi[2][2]; /* predefined delta init */
uint8_t vector_model_pdv[2][7]; /* predefined delta values */
uint8_t vector_model_fdv[2][8]; /* 8 bit delta value definition */
uint8_t mb_type_model[3][10][10]; /* model for decoding MB type */
uint8_t coeff_model_dccv[2][11]; /* DC coeff value */
uint8_t coeff_model_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
uint8_t coeff_model_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
uint8_t coeff_model_dcct[2][36][5]; /* DC coeff coding type */
uint8_t coeff_model_runv[2][14]; /* run value (vp6 only) */
uint8_t mb_types_stats[3][10][2]; /* contextual, next MB type stats */
uint8_t coeff_ctx[4][64]; /* used in vp5 only */
uint8_t coeff_ctx_last[4]; /* used in vp5 only */
/* upside-down flipping hints */
int flip; /* are we flipping ? */
int frbi; /* first row block index in MB */
int srbi; /* second row block index in MB */
int stride[3]; /* stride for each plan */
const uint8_t *vp56_coord_div;
vp56_parse_vector_adjustment_t parse_vector_adjustment;
vp56_adjust_t adjust;
vp56_filter_t filter;
vp56_parse_coeff_t parse_coeff;
vp56_default_models_init_t default_models_init;
vp56_parse_vector_models_t parse_vector_models;
vp56_parse_coeff_models_t parse_coeff_models;
vp56_parse_header_t parse_header;
};
void vp56_init(vp56_context_t *s, AVCodecContext *avctx, int flip);
int vp56_free(AVCodecContext *avctx);
void vp56_init_dequant(vp56_context_t *s, int quantizer);
int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
uint8_t *buf, int buf_size);
/**
* vp56 specific range coder implementation
*/
static inline void vp56_init_range_decoder(vp56_range_coder_t *c,
const uint8_t *buf, int buf_size)
{
c->high = 255;
c->bits = 8;
c->buffer = buf;
c->code_word = *c->buffer++ << 8;
c->code_word |= *c->buffer++;
}
static inline int vp56_rac_get_prob(vp56_range_coder_t *c, uint8_t prob)
{
unsigned int low = 1 + (((c->high - 1) * prob) / 256);
unsigned int low_shift = low << 8;
int bit = c->code_word >= low_shift;
if (bit) {
c->high -= low;
c->code_word -= low_shift;
} else {
c->high = low;
}
/* normalize */
while (c->high < 128) {
c->high <<= 1;
c->code_word <<= 1;
if (--c->bits == 0) {
c->bits = 8;
c->code_word |= *c->buffer++;
}
}
return bit;
}
static inline int vp56_rac_get(vp56_range_coder_t *c)
{
/* equiprobable */
int low = (c->high + 1) >> 1;
unsigned int low_shift = low << 8;
int bit = c->code_word >= low_shift;
if (bit) {
c->high = (c->high - low) << 1;
c->code_word -= low_shift;
} else {
c->high = low << 1;
}
/* normalize */
c->code_word <<= 1;
if (--c->bits == 0) {
c->bits = 8;
c->code_word |= *c->buffer++;
}
return bit;
}
static inline int vp56_rac_gets(vp56_range_coder_t *c, int bits)
{
int value = 0;
while (bits--) {
value = (value << 1) | vp56_rac_get(c);
}
return value;
}
static inline int vp56_rac_gets_nn(vp56_range_coder_t *c, int bits)
{
int v = vp56_rac_gets(c, 7) << 1;