# HG changeset patch # User michael # Date 1084117808 0 # Node ID b7c82b9ef098de6ed76150860297637badb02602 # Parent 8c7e7c332b862f4f6079c6f4ebf83c95d0bc9cc6 p frame encoding, only with 0,0 motion vectors yet though diff -r 8c7e7c332b86 -r b7c82b9ef098 svq1.c --- a/svq1.c Sat May 08 14:15:45 2004 +0000 +++ b/svq1.c Sun May 09 15:50:08 2004 +0000 @@ -67,7 +67,8 @@ AVCodecContext *avctx; DSPContext dsp; AVFrame picture; - AVFrame ref_picture[2]; + AVFrame current_picture; + AVFrame last_picture; PutBitContext pb; GetBitContext gb; @@ -834,8 +835,8 @@ &svq1_block_type_vlc[0][0], 2, 1); init_vlc(&svq1_motion_component, 7, 65, - &svq1_motion_component_vlc[0][1], 4, 2, - &svq1_motion_component_vlc[0][0], 4, 2); + &svq1_motion_component_vlc[0][1], 2, 1, + &svq1_motion_component_vlc[0][0], 2, 1); for (i = 0; i < 6; i++) { init_vlc(&svq1_intra_multistage[i], 3, 8, @@ -1311,7 +1312,7 @@ } #endif -static int encode_block(SVQ1Context *s, uint8_t *src, int stride, int level, int threshold, int lambda){ +static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref, uint8_t *decoded, int stride, int level, int threshold, int lambda, int intra){ int count, y, x, i, j, split, best_mean, best_score, best_count; int best_vector[6]; int block_sum[7]= {0, 0, 0, 0, 0, 0}; @@ -1319,30 +1320,37 @@ int h= 2<<((level+1)>>1); int size=w*h; int16_t block[7][256]; - const int intra= 1; const int8_t *codebook_sum, *codebook; const uint16_t (*mean_vlc)[2]; const uint8_t (*multistage_vlc)[2]; + best_score=0; + //FIXME optimize, this doenst need to be done multiple times if(intra){ codebook_sum= svq1_intra_codebook_sum[level]; codebook= svq1_intra_codebooks[level]; mean_vlc= svq1_intra_mean_vlc; multistage_vlc= svq1_intra_multistage_vlc[level]; + for(y=0; y>1)) >> (level+3); - assert(mean >-50 && mean<300); - mean= clip(mean, 0, 255); + assert(mean >-300 && mean<300); + if(intra) mean= clip(mean, 0, 255); + else mean= clip(mean, -256, 255); score= sqr - ((diff*(int64_t)diff)>>(level+3)); //FIXME 64bit slooow if(score < best_vector_score){ best_vector_score= score; @@ -1380,7 +1389,7 @@ best_vector_mean= mean; } } - assert(best_vector_mean != -99); + assert(best_vector_mean != -999); vector= codebook + stage*size*16 + best_vector[stage]*size; for(j=0; j=0; i--){ backup[i]= s->reorder_pb[i]; } - score += encode_block(s, src , stride, level-1, threshold>>1, lambda); - score += encode_block(s, src + offset, stride, level-1, threshold>>1, lambda); + score += encode_block(s, src , ref , decoded , stride, level-1, threshold>>1, lambda, intra); + score += encode_block(s, src + offset, ref + offset, decoded + offset, stride, level-1, threshold>>1, lambda, intra); score += lambda; if(score < best_score){ @@ -1425,7 +1434,8 @@ put_bits(&s->reorder_pb[level], 1, split); if(!split){ - assert(best_mean >= 0 && best_mean<256); + assert((best_mean >= 0 && best_mean<256) || !intra); + assert(best_mean >= -256 && best_mean<256); assert(best_count >=0 && best_count<7); assert(level<4 || best_count==0); @@ -1440,13 +1450,19 @@ assert(best_vector[i]>=0 && best_vector[i]<16); put_bits(&s->reorder_pb[level], 4, best_vector[i]); } + + for(y=0; ypicture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT); static int frame = 0; @@ -1477,32 +1494,71 @@ block_height = (height + 15) / 16; for (y = 0; y < block_height; y++) { + uint8_t src[stride*16]; + + for(i=0; i<16 && i + 16*yavctx, AV_LOG_INFO, "* level 5 vector @ %d, %d:\n", x * 16, y * 16); #endif - - /* copy the block into the current work buffer */ - left_edge = (y * 16 * stride) + (x * 16); for(i=0; i<6; i++){ - init_put_bits(&s->reorder_pb[i], reorder_buffer[i], 7*32); + init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i], 7*32); } - if(x*16 + 16 > width || y*16 + 16 > height){ - ff_emulated_edge_mc(buf, src, stride, 16, 16, 16*x, 16*y, width, height); - src= buf; + if(s->picture.pict_type == P_TYPE){ + const uint8_t *vlc= svq1_block_type_vlc[SVQ1_BLOCK_INTRA]; + put_bits(&s->reorder_pb[5], vlc[1], vlc[0]); + score[0]= vlc[1]*lambda; + } + score[0]+= encode_block(s, src+16*x, ref, temp, stride, 5, 64, lambda, 1); + for(i=0; i<6; i++){ + count[0][i]= put_bits_count(&s->reorder_pb[i]); + flush_put_bits(&s->reorder_pb[i]); + init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i], 7*32); } - s->rd_total += encode_block(s, src, stride, 5, 256, (s->picture.quality*s->picture.quality) >> (2*FF_LAMBDA_SHIFT)); + if(s->picture.pict_type == P_TYPE){ + const uint8_t *vlc= svq1_block_type_vlc[SVQ1_BLOCK_INTER]; + put_bits(&s->reorder_pb[5], vlc[1], vlc[0]); + score[1] = vlc[1]*lambda; + for(i=0; i<2; i++){ + vlc= svq1_motion_component_vlc[32]; + put_bits(&s->reorder_pb[5], vlc[1], vlc[0]); + score[1] += vlc[1]*lambda; + } + + score[1]+= encode_block(s, src+16*x, ref, decoded, stride, 5, 64, lambda, 0); + best= score[1] <= score[0]; + if(best==1){ + for(i=0; i<6; i++){ + count[1][i]= put_bits_count(&s->reorder_pb[i]); + flush_put_bits(&s->reorder_pb[i]); + } + } + }else + best= 0; + + s->rd_total += score[best]; + for(i=5; i>=0; i--){ - int count= put_bits_count(&s->reorder_pb[i]); - - flush_put_bits(&s->reorder_pb[i]); - ff_copy_bits(&s->pb, s->reorder_pb[i].buf, count); + ff_copy_bits(&s->pb, reorder_buffer[best][i], count[best][i]); + } + if(best==0){ + s->dsp.put_pixels_tab[0][0](decoded, temp, stride, 16); } #if 0 @@ -1665,25 +1721,35 @@ SVQ1Context * const s = avctx->priv_data; AVFrame *pict = data; AVFrame * const p= (AVFrame*)&s->picture; + AVFrame temp; + int i; + if(avctx->pix_fmt != PIX_FMT_YUV410P){ + av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); + return -1; + } + + if(!s->current_picture.data[0]){ + avctx->get_buffer(avctx, &s->current_picture); + avctx->get_buffer(avctx, &s->last_picture); + } + + temp= s->current_picture; + s->current_picture= s->last_picture; + s->last_picture= temp; + init_put_bits(&s->pb, buf, buf_size); *p = *pict; - p->pict_type = I_TYPE; + p->pict_type = avctx->frame_number % avctx->gop_size ? P_TYPE : I_TYPE; p->key_frame = p->pict_type == I_TYPE; svq1_write_header(s, p->pict_type); - svq1_encode_plane(s, s->picture.data[0], s->frame_width, s->frame_height, - s->picture.linesize[0]); -// if (avctx->flags & CODEC_FLAG_GRAY) { - if (avctx->pix_fmt != PIX_FMT_YUV410P) { - svq1_output_intra_constant_mean(s, s->c_block_width * 2, - s->c_block_height * 2, 128); - } else { - svq1_encode_plane(s, s->picture.data[1], s->frame_width / 4, - s->frame_height / 4, s->picture.linesize[1]); - svq1_encode_plane(s, s->picture.data[2], s->frame_width / 4, - s->frame_height / 4, s->picture.linesize[2]); + for(i=0; i<3; i++){ + svq1_encode_plane(s, + s->picture.data[i], s->last_picture.data[i], s->current_picture.data[i], + s->frame_width / (i?4:1), s->frame_height / (i?4:1), + s->picture.linesize[i], s->current_picture.linesize[i]); } // align_put_bits(&s->pb); diff -r 8c7e7c332b86 -r b7c82b9ef098 svq1_vlc.h --- a/svq1_vlc.h Sat May 08 14:15:45 2004 +0000 +++ b/svq1_vlc.h Sun May 09 15:50:08 2004 +0000 @@ -9,7 +9,7 @@ }; /* values in this table range from -32..32; adjust retrieved value by -32 */ -static const uint16_t svq1_motion_component_vlc[65][2] = { +static const uint8_t svq1_motion_component_vlc[65][2] = { /* { code, length } */ { 0x5, 13 }, { 0x7, 13 }, { 0x5, 12 }, { 0x7, 12 }, { 0x9, 12 }, { 0xB, 12 }, { 0xD, 12 }, { 0xF, 12 },