1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-02-04 06:08:26 +02:00

Merge commit '62256010e9bc8879e2bf7f3b94af8ff85e239082'

* commit '62256010e9bc8879e2bf7f3b94af8ff85e239082':
  indeo: Refactor ff_ivi_init_tiles and ivi_decode_blocks

Conflicts:
	libavcodec/ivi_common.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
Michael Niedermayer 2013-07-05 12:02:53 +02:00
commit 9b10440dcd

View File

@ -288,39 +288,11 @@ av_cold int ff_ivi_init_planes(IVIPlaneDesc *planes, const IVIPicConfig *cfg)
return 0;
}
av_cold int ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_height)
static int ivi_init_tiles(IVIBandDesc *band, IVITile *ref_tile,
int p, int b, int t_height, int t_width)
{
int p, b, x, y, x_tiles, y_tiles, t_width, t_height;
IVIBandDesc *band;
IVITile *tile, *ref_tile;
for (p = 0; p < 3; p++) {
t_width = !p ? tile_width : (tile_width + 3) >> 2;
t_height = !p ? tile_height : (tile_height + 3) >> 2;
if (!p && planes[0].num_bands == 4) {
t_width >>= 1;
t_height >>= 1;
}
if(t_width<=0 || t_height<=0)
return AVERROR(EINVAL);
for (b = 0; b < planes[p].num_bands; b++) {
band = &planes[p].bands[b];
x_tiles = IVI_NUM_TILES(band->width, t_width);
y_tiles = IVI_NUM_TILES(band->height, t_height);
band->num_tiles = x_tiles * y_tiles;
av_freep(&band->tiles);
band->tiles = av_mallocz(band->num_tiles * sizeof(IVITile));
if (!band->tiles)
return AVERROR(ENOMEM);
tile = band->tiles;
/* use the first luma band as reference for motion vectors
* and quant */
ref_tile = planes[0].bands[0].tiles;
int x, y;
IVITile *tile = band->tiles;
for (y = 0; y < band->height; y += t_height) {
for (x = 0; x < band->width; x += t_width) {
@ -345,15 +317,52 @@ av_cold int ff_ivi_init_tiles(IVIPlaneDesc *planes, int tile_width, int tile_hei
tile->ref_mbs = ref_tile->mbs;
}else
av_log(NULL, AV_LOG_DEBUG, "Cannot use ref_tile, too few mbs\n");
ref_tile++;
}
tile++;
}
}
}// for b
}// for p
return 0;
}
av_cold int ff_ivi_init_tiles(IVIPlaneDesc *planes,
int tile_width, int tile_height)
{
int p, b, x_tiles, y_tiles, t_width, t_height, ret;
IVIBandDesc *band;
for (p = 0; p < 3; p++) {
t_width = !p ? tile_width : (tile_width + 3) >> 2;
t_height = !p ? tile_height : (tile_height + 3) >> 2;
if (!p && planes[0].num_bands == 4) {
t_width >>= 1;
t_height >>= 1;
}
if(t_width<=0 || t_height<=0)
return AVERROR(EINVAL);
for (b = 0; b < planes[p].num_bands; b++) {
band = &planes[p].bands[b];
x_tiles = IVI_NUM_TILES(band->width, t_width);
y_tiles = IVI_NUM_TILES(band->height, t_height);
band->num_tiles = x_tiles * y_tiles;
av_freep(&band->tiles);
band->tiles = av_mallocz(band->num_tiles * sizeof(IVITile));
if (!band->tiles)
return AVERROR(ENOMEM);
/* use the first luma band as reference for motion vectors
* and quant */
ret = ivi_init_tiles(band, planes[0].bands[0].tiles,
p, b, t_height, t_width);
if (ret < 0)
return ret;
}
}
return 0;
}
@ -385,115 +394,46 @@ static int ivi_dec_tile_data_size(GetBitContext *gb)
return len;
}
/*
* Decode block data:
* extract huffman-coded transform coefficients from the bitstream,
* dequantize them, apply inverse transform and motion compensation
* in order to reconstruct the picture.
*
* @param[in,out] gb the GetBit context
* @param[in] band pointer to the band descriptor
* @param[in] tile pointer to the tile descriptor
* @return result code: 0 - OK, -1 = error (corrupted blocks data)
*/
static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile,
static int ivi_decode_coded_blocks(GetBitContext *gb, IVIBandDesc *band,
ivi_mc_func mc, int mv_x, int mv_y,
int *prev_dc, int is_intra, int mc_type,
uint32_t quant, int offs,
AVCodecContext *avctx)
{
int mbn, blk, num_blocks, num_coeffs, blk_size, scan_pos, run, val,
pos, is_intra, mc_type = 0, av_uninit(mv_x), av_uninit(mv_y), col_mask;
uint8_t col_flags[8];
int32_t prev_dc, trvec[64];
uint32_t cbp, av_uninit(sym), lo, hi, quant, buf_offs, q;
IVIMbInfo *mb;
const uint16_t *base_tab = is_intra ? band->intra_base : band->inter_base;
RVMapDesc *rvmap = band->rv_map;
ivi_mc_func mc_with_delta_func, mc_no_delta_func;
const uint16_t *base_tab;
const uint8_t *scale_tab;
uint8_t col_flags[8];
int32_t trvec[64];
uint32_t sym = 0, lo, hi, q;
int pos, run, val;
int blk_size = band->blk_size;
int num_coeffs = blk_size * blk_size;
int col_mask = blk_size - 1;
int scan_pos = -1;
prev_dc = 0; /* init intra prediction for the DC coefficient */
blk_size = band->blk_size;
col_mask = blk_size - 1; /* column mask for tracking non-zero coeffs */
num_blocks = (band->mb_size != blk_size) ? 4 : 1; /* number of blocks per mb */
num_coeffs = blk_size * blk_size;
if (blk_size == 8) {
mc_with_delta_func = ff_ivi_mc_8x8_delta;
mc_no_delta_func = ff_ivi_mc_8x8_no_delta;
} else {
mc_with_delta_func = ff_ivi_mc_4x4_delta;
mc_no_delta_func = ff_ivi_mc_4x4_no_delta;
}
for (mbn = 0, mb = tile->mbs; mbn < tile->num_MBs; mb++, mbn++) {
is_intra = !mb->type;
cbp = mb->cbp;
buf_offs = mb->buf_offs;
quant = band->glob_quant + mb->q_delta;
if (avctx->codec_id == AV_CODEC_ID_INDEO4)
quant = av_clip(quant, 0, 31);
else
quant = av_clip(quant, 0, 23);
base_tab = is_intra ? band->intra_base : band->inter_base;
scale_tab = is_intra ? band->intra_scale : band->inter_scale;
if (scale_tab)
quant = scale_tab[quant];
if (!is_intra) {
mv_x = mb->mv_x;
mv_y = mb->mv_y;
if (band->is_halfpel) {
mc_type = ((mv_y & 1) << 1) | (mv_x & 1);
mv_x >>= 1;
mv_y >>= 1; /* convert halfpel vectors into fullpel ones */
}
if (mb->type) {
int dmv_x, dmv_y, cx, cy;
dmv_x = mb->mv_x >> band->is_halfpel;
dmv_y = mb->mv_y >> band->is_halfpel;
cx = mb->mv_x & band->is_halfpel;
cy = mb->mv_y & band->is_halfpel;
if ( mb->xpos + dmv_x < 0
|| mb->xpos + dmv_x + band->mb_size + cx > band->pitch
|| mb->ypos + dmv_y < 0
|| mb->ypos + dmv_y + band->mb_size + cy > band->aheight) {
return AVERROR_INVALIDDATA;
}
}
}
for (blk = 0; blk < num_blocks; blk++) {
/* adjust block position in the buffer according to its number */
if (blk & 1) {
buf_offs += blk_size;
} else if (blk == 2) {
buf_offs -= blk_size;
buf_offs += blk_size * band->pitch;
}
if (cbp & 1) { /* block coded ? */
if (!band->scan) {
av_log(avctx, AV_LOG_ERROR, "Scan pattern is not set.\n");
return AVERROR_INVALIDDATA;
}
scan_pos = -1;
memset(trvec, 0, num_coeffs*sizeof(trvec[0])); /* zero transform vector */
memset(col_flags, 0, sizeof(col_flags)); /* zero column flags */
/* zero transform vector */
memset(trvec, 0, num_coeffs * sizeof(trvec[0]));
/* zero column flags */
memset(col_flags, 0, sizeof(col_flags));
while (scan_pos <= num_coeffs) {
sym = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
sym = get_vlc2(gb, band->blk_vlc.tab->table,
IVI_VLC_BITS, 1);
if (sym == rvmap->eob_sym)
break; /* End of block */
if (sym == rvmap->esc_sym) { /* Escape - run/val explicitly coded using 3 vlc codes */
/* Escape - run/val explicitly coded using 3 vlc codes */
if (sym == rvmap->esc_sym) {
run = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1) + 1;
lo = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
hi = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
val = IVI_TOSIGNED((hi << 6) | lo); /* merge them and convert into signed val */
/* merge them and convert into signed val */
val = IVI_TOSIGNED((hi << 6) | lo);
} else {
if (sym >= 256U) {
av_log(avctx, AV_LOG_ERROR, "Invalid sym encountered: %d.\n", sym);
@ -516,31 +456,127 @@ static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile
if (q > 1)
val = val * q + FFSIGN(val) * (((q ^ 1) - 1) >> 1);
trvec[pos] = val;
col_flags[pos & col_mask] |= !!val; /* track columns containing non-zero coeffs */
}// while
/* track columns containing non-zero coeffs */
col_flags[pos & col_mask] |= !!val;
}
if (scan_pos >= num_coeffs && sym != rvmap->eob_sym)
return AVERROR_INVALIDDATA; /* corrupt block data */
/* undoing DC coeff prediction for intra-blocks */
if (is_intra && band->is_2d_trans) {
prev_dc += trvec[0];
trvec[0] = prev_dc;
col_flags[0] |= !!prev_dc;
*prev_dc += trvec[0];
trvec[0] = *prev_dc;
col_flags[0] |= !!*prev_dc;
}
if(band->transform_size > band->blk_size){
av_log(NULL, AV_LOG_ERROR, "Too large transform\n");
return AVERROR_INVALIDDATA;
}
/* apply inverse transform */
band->inv_transform(trvec, band->buf + buf_offs,
band->inv_transform(trvec, band->buf + offs,
band->pitch, col_flags);
/* apply motion compensation */
if (!is_intra)
mc_with_delta_func(band->buf + buf_offs,
band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,
mc(band->buf + offs,
band->ref_buf + offs + mv_y * band->pitch + mv_x,
band->pitch, mc_type);
return 0;
}
/*
* Decode block data:
* extract huffman-coded transform coefficients from the bitstream,
* dequantize them, apply inverse transform and motion compensation
* in order to reconstruct the picture.
*
* @param[in,out] gb the GetBit context
* @param[in] band pointer to the band descriptor
* @param[in] tile pointer to the tile descriptor
* @return result code: 0 - OK, -1 = error (corrupted blocks data)
*/
static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band,
IVITile *tile, AVCodecContext *avctx)
{
int mbn, blk, num_blocks, blk_size, ret, is_intra, mc_type = 0;
int mv_x = 0, mv_y = 0;
int32_t prev_dc;
uint32_t cbp, quant, buf_offs;
IVIMbInfo *mb;
ivi_mc_func mc_with_delta_func, mc_no_delta_func;
const uint8_t *scale_tab;
/* init intra prediction for the DC coefficient */
prev_dc = 0;
blk_size = band->blk_size;
/* number of blocks per mb */
num_blocks = (band->mb_size != blk_size) ? 4 : 1;
if (blk_size == 8) {
mc_with_delta_func = ff_ivi_mc_8x8_delta;
mc_no_delta_func = ff_ivi_mc_8x8_no_delta;
} else {
mc_with_delta_func = ff_ivi_mc_4x4_delta;
mc_no_delta_func = ff_ivi_mc_4x4_no_delta;
}
for (mbn = 0, mb = tile->mbs; mbn < tile->num_MBs; mb++, mbn++) {
is_intra = !mb->type;
cbp = mb->cbp;
buf_offs = mb->buf_offs;
quant = band->glob_quant + mb->q_delta;
if (avctx->codec_id == AV_CODEC_ID_INDEO4)
quant = av_clip(quant, 0, 31);
else
quant = av_clip(quant, 0, 23);
scale_tab = is_intra ? band->intra_scale : band->inter_scale;
if (scale_tab)
quant = scale_tab[quant];
if (!is_intra) {
mv_x = mb->mv_x;
mv_y = mb->mv_y;
if (band->is_halfpel) {
mc_type = ((mv_y & 1) << 1) | (mv_x & 1);
mv_x >>= 1;
mv_y >>= 1; /* convert halfpel vectors into fullpel ones */
}
if (mb->type) {
int dmv_x, dmv_y, cx, cy;
dmv_x = mb->mv_x >> band->is_halfpel;
dmv_y = mb->mv_y >> band->is_halfpel;
cx = mb->mv_x & band->is_halfpel;
cy = mb->mv_y & band->is_halfpel;
if (mb->xpos + dmv_x < 0 ||
mb->xpos + dmv_x + band->mb_size + cx > band->pitch ||
mb->ypos + dmv_y < 0 ||
mb->ypos + dmv_y + band->mb_size + cy > band->aheight) {
return AVERROR_INVALIDDATA;
}
}
}
for (blk = 0; blk < num_blocks; blk++) {
/* adjust block position in the buffer according to its number */
if (blk & 1) {
buf_offs += blk_size;
} else if (blk == 2) {
buf_offs -= blk_size;
buf_offs += blk_size * band->pitch;
}
if (cbp & 1) { /* block coded ? */
ret = ivi_decode_coded_blocks(gb, band, mc_with_delta_func,
mv_x, mv_y, &prev_dc, is_intra,
mc_type, quant, buf_offs, avctx);
if (ret < 0)
return ret;
} else {
/* block not coded */
/* for intra blocks apply the dc slant transform */