*/
/**
- * @file rv34.c
+ * @file libavcodec/rv34.c
* RV30/40 decoder common data
*/
#include "dsputil.h"
#include "mpegvideo.h"
#include "golomb.h"
+#include "mathops.h"
#include "rectangle.h"
#include "rv34vlc.h"
* Generate VLC from codeword lengths.
* @param bits codeword lengths (zeroes are accepted)
* @param size length of input data
+ * @param vlc output VLC
* @param insyms symbols for input codes (NULL for default ones)
*/
static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms)
/**
* Initialize all tables.
*/
-static av_cold void rv34_init_tables()
+static av_cold void rv34_init_tables(void)
{
int i, j, k;
int ones;
static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
static const int shifts[4] = { 0, 2, 8, 10 };
- int *curshift = shifts;
+ const int *curshift = shifts;
int i, t, mask;
code = get_vlc2(gb, vlc->cbppattern[table].table, 9, 2);
{
int i;
for(i = 0; i < 5; i++)
- if(rv34_mb_max_sizes[i] > mb_size)
+ if(rv34_mb_max_sizes[i] >= mb_size - 1)
break;
return rv34_mb_bits_sizes[i];
}
MpegEncContext *s = &r->s;
int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
int A[2] = {0}, B[2], C[2];
- int i, j;
+ int i, j, k;
int mx, my;
int avail_index = avail_indexes[0];
my += r->dmv[0][1];
for(j = 0; j < 2; j++){
for(i = 0; i < 2; i++){
- s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
- s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
+ for(k = 0; k < 2; k++){
+ s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
+ s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
+ }
}
}
- if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD)
- fill_rectangle(s->current_picture_ptr->motion_val[!dir][mv_pos], 2, 2, s->b8_stride, 0, 4);
}
static const int chroma_coeffs[3] = { 0, 3, 5 };
* @param mv_off offset to the motion vector information
* @param width width of the current partition in 8x8 blocks
* @param height height of the current partition in 8x8 blocks
+ * @param dir motion compensation direction (i.e. from the last or the next reference frame)
+ * @param thirdpel motion vectors are specified in 1/3 of pixel
+ * @param qpel_mc a set of functions used to perform luma motion compensation
+ * @param chroma_mc a set of functions used to perform chroma motion compensation
*/
static inline void rv34_mc(RV34DecContext *r, const int block_type,
const int xoff, const int yoff, int mv_off,
rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30,
r->rv30 ? r->s.dsp.put_rv30_tpel_pixels_tab
: r->s.dsp.put_rv40_qpel_pixels_tab,
- r->s.dsp.put_h264_chroma_pixels_tab);
+ r->rv30 ? r->s.dsp.put_h264_chroma_pixels_tab
+ : r->s.dsp.put_rv40_chroma_pixels_tab);
rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30,
r->rv30 ? r->s.dsp.avg_rv30_tpel_pixels_tab
: r->s.dsp.avg_rv40_qpel_pixels_tab,
}
if(!right && up){
topleft = dst[-stride + 3] * 0x01010101;
- prev = &topleft;
+ prev = (uint8_t*)&topleft;
}
r->h.pred4x4[itype](dst, prev, stride);
}
for(i = 0; i < 2; i++){
if(is_mv_diff_gt_3(motion_val + i, 1))
vmvmask |= 0x11 << (j + i*2);
- if(is_mv_diff_gt_3(motion_val + i, s->b8_stride))
+ if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
hmvmask |= 0x03 << (j + i*2);
}
motion_val += s->b8_stride;
hmvmask &= ~0x000F;
if(!s->mb_x)
vmvmask &= ~0x1111;
- return hmvmask | vmvmask; //XXX: should be stored separately for RV3
+ if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
+ vmvmask |= (vmvmask & 0x4444) >> 1;
+ hmvmask |= (hmvmask & 0x0F00) >> 4;
+ if(s->mb_x)
+ r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
+ if(!s->first_slice_line)
+ r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
+ }
+ return hmvmask | vmvmask;
}
static int rv34_decode_macroblock(RV34DecContext *r, int8_t *intra_types)
s->qscale = r->si.quant;
cbp = cbp2 = rv34_decode_mb_header(r, intra_types);
- r->cbp_luma [s->mb_x + s->mb_y * s->mb_stride] = cbp;
- r->cbp_chroma[s->mb_x + s->mb_y * s->mb_stride] = cbp >> 16;
+ r->cbp_luma [mb_pos] = cbp;
+ r->cbp_chroma[mb_pos] = cbp >> 16;
if(s->pict_type == FF_I_TYPE)
- r->deblock_coefs[mb_pos] = 0;
+ r->deblock_coefs[mb_pos] = 0xFFFF;
else
- r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r);
- s->current_picture_ptr->qscale_table[s->mb_x + s->mb_y * s->mb_stride] = s->qscale;
+ r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
+ s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
if(cbp == -1)
return -1;
rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]],rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]]);
rv34_inv_transform(s->block[blknum] + blkoff);
}
- if(IS_INTRA(s->current_picture_ptr->mb_type[s->mb_x + s->mb_y*s->mb_stride]))
+ if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos]))
rv34_output_macroblock(r, intra_types, cbp2, r->is16);
else
rv34_apply_differences(r, cbp2);
si1->pts != si2->pts;
}
-static int rv34_decode_slice(RV34DecContext *r, int end, uint8_t* buf, int buf_size)
+static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
{
MpegEncContext *s = &r->s;
GetBitContext *gb = &s->gb;
memmove(r->intra_types_hist, r->intra_types, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
memset(r->intra_types, -1, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
+
+ if(r->loop_filter && s->mb_y >= 2)
+ r->loop_filter(r, s->mb_y - 2);
}
if(s->mb_x == s->resync_mb_x)
s->first_slice_line=0;
return 0;
}
-static int get_slice_offset(AVCodecContext *avctx, uint8_t *buf, int n)
+static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
{
if(avctx->slice_count) return avctx->slice_offset[n];
else return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
int ff_rv34_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
- uint8_t *buf, int buf_size)
+ const uint8_t *buf, int buf_size)
{
RV34DecContext *r = avctx->priv_data;
MpegEncContext *s = &r->s;
SliceInfo si;
int i;
int slice_count;
- uint8_t *slices_hdr = NULL;
+ const uint8_t *slices_hdr = NULL;
int last = 0;
/* no supplementary picture */
}else
slice_count = avctx->slice_count;
+ //parse first slice header to check whether this frame can be decoded
+ if(get_slice_offset(avctx, slices_hdr, 0) > buf_size){
+ av_log(avctx, AV_LOG_ERROR, "Slice offset is greater than frame size\n");
+ return -1;
+ }
+ init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), buf_size-get_slice_offset(avctx, slices_hdr, 0));
+ if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
+ av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
+ return -1;
+ }
+ if((!s->last_picture_ptr || !s->last_picture_ptr->data[0]) && si.type == FF_B_TYPE)
+ return -1;
+ /* skip b frames if we are in a hurry */
+ if(avctx->hurry_up && si.type==FF_B_TYPE) return buf_size;
+ if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==FF_B_TYPE)
+ || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=FF_I_TYPE)
+ || avctx->skip_frame >= AVDISCARD_ALL)
+ return buf_size;
+ /* skip everything if we are in a hurry>=5 */
+ if(avctx->hurry_up>=5)
+ return buf_size;
+
for(i=0; i<slice_count; i++){
int offset= get_slice_offset(avctx, slices_hdr, i);
int size;
else
size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
+ if(offset > buf_size){
+ av_log(avctx, AV_LOG_ERROR, "Slice offset is greater than frame size\n");
+ break;
+ }
+
r->si.end = s->mb_width * s->mb_height;
if(i+1 < slice_count){
init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, i+1), (buf_size-get_slice_offset(avctx, slices_hdr, i+1))*8);
if(last){
if(r->loop_filter)
- r->loop_filter(r);
+ r->loop_filter(r, s->mb_height - 1);
ff_er_frame_end(s);
MPV_frame_end(s);
if (s->pict_type == FF_B_TYPE || s->low_delay) {