00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "avcodec.h"
00029 #include "dsputil.h"
00030 #include "mpegvideo.h"
00031 #include "faandct.h"
00032 #include <limits.h>
00033
00034 #ifdef USE_FASTMEMCPY
00035 #include "fastmemcpy.h"
00036 #endif
00037
00038
00039
00040
00041 #ifdef CONFIG_ENCODERS
00042 static void encode_picture(MpegEncContext *s, int picture_number);
00043 #endif //CONFIG_ENCODERS
00044 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
00045 DCTELEM *block, int n, int qscale);
00046 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
00047 DCTELEM *block, int n, int qscale);
00048 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
00049 DCTELEM *block, int n, int qscale);
00050 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
00051 DCTELEM *block, int n, int qscale);
00052 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
00053 DCTELEM *block, int n, int qscale);
00054 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
00055 DCTELEM *block, int n, int qscale);
00056 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
00057 #ifdef CONFIG_ENCODERS
00058 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
00059 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
00060 static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
00061 static int sse_mb(MpegEncContext *s);
00062 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
00063 #endif //CONFIG_ENCODERS
00064
00065 #ifdef HAVE_XVMC
00066 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
00067 extern void XVMC_field_end(MpegEncContext *s);
00068 extern void XVMC_decode_mb(MpegEncContext *s);
00069 #endif
00070
00071 void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w)= draw_edges_c;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 #define CONST_BITS 14
00082
00083 static const uint16_t aanscales[64] = {
00084
00085 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
00086 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
00087 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
00088 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
00089 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
00090 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
00091 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446,
00092 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247
00093 };
00094
00095 static const uint8_t h263_chroma_roundtab[16] = {
00096
00097 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
00098 };
00099
00100 static const uint8_t ff_default_chroma_qscale_table[32]={
00101
00102 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
00103 };
00104
00105 #ifdef CONFIG_ENCODERS
00106 static uint8_t (*default_mv_penalty)[MAX_MV*2+1]=NULL;
00107 static uint8_t default_fcode_tab[MAX_MV*2+1];
00108
00109 enum PixelFormat ff_yuv420p_list[2]= {PIX_FMT_YUV420P, -1};
00110
00111 static void convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
00112 const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
00113 {
00114 int qscale;
00115 int shift=0;
00116
00117 for(qscale=qmin; qscale<=qmax; qscale++){
00118 int i;
00119 if (dsp->fdct == ff_jpeg_fdct_islow
00120 #ifdef FAAN_POSTSCALE
00121 || dsp->fdct == ff_faandct
00122 #endif
00123 ) {
00124 for(i=0;i<64;i++) {
00125 const int j= dsp->idct_permutation[i];
00126
00127
00128
00129
00130
00131 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) /
00132 (qscale * quant_matrix[j]));
00133 }
00134 } else if (dsp->fdct == fdct_ifast
00135 #ifndef FAAN_POSTSCALE
00136 || dsp->fdct == ff_faandct
00137 #endif
00138 ) {
00139 for(i=0;i<64;i++) {
00140 const int j= dsp->idct_permutation[i];
00141
00142
00143
00144
00145
00146 qmat[qscale][i] = (int)((uint64_t_C(1) << (QMAT_SHIFT + 14)) /
00147 (aanscales[i] * qscale * quant_matrix[j]));
00148 }
00149 } else {
00150 for(i=0;i<64;i++) {
00151 const int j= dsp->idct_permutation[i];
00152
00153
00154
00155
00156
00157 qmat[qscale][i] = (int)((uint64_t_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
00158
00159 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
00160
00161 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
00162 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
00163 }
00164 }
00165
00166 for(i=intra; i<64; i++){
00167 int64_t max= 8191;
00168 if (dsp->fdct == fdct_ifast
00169 #ifndef FAAN_POSTSCALE
00170 || dsp->fdct == ff_faandct
00171 #endif
00172 ) {
00173 max= (8191LL*aanscales[i]) >> 14;
00174 }
00175 while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
00176 shift++;
00177 }
00178 }
00179 }
00180 if(shift){
00181 av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger then %d, overflows possible\n", QMAT_SHIFT - shift);
00182 }
00183 }
00184
00185 static inline void update_qscale(MpegEncContext *s){
00186 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
00187 s->qscale= clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
00188
00189 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
00190 }
00191 #endif //CONFIG_ENCODERS
00192
00193 void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
00194 int i;
00195 int end;
00196
00197 st->scantable= src_scantable;
00198
00199 for(i=0; i<64; i++){
00200 int j;
00201 j = src_scantable[i];
00202 st->permutated[i] = permutation[j];
00203 #ifdef ARCH_POWERPC
00204 st->inverse[j] = i;
00205 #endif
00206 }
00207
00208 end=-1;
00209 for(i=0; i<64; i++){
00210 int j;
00211 j = st->permutated[i];
00212 if(j>end) end=j;
00213 st->raster_end[i]= end;
00214 }
00215 }
00216
00217 #ifdef CONFIG_ENCODERS
00218 void ff_write_quant_matrix(PutBitContext *pb, int16_t *matrix){
00219 int i;
00220
00221 if(matrix){
00222 put_bits(pb, 1, 1);
00223 for(i=0;i<64;i++) {
00224 put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
00225 }
00226 }else
00227 put_bits(pb, 1, 0);
00228 }
00229 #endif //CONFIG_ENCODERS
00230
00231
00232 int DCT_common_init(MpegEncContext *s)
00233 {
00234 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
00235 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
00236 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
00237 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
00238 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
00239 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
00240
00241 #ifdef CONFIG_ENCODERS
00242 s->dct_quantize= dct_quantize_c;
00243 s->denoise_dct= denoise_dct_c;
00244 #endif //CONFIG_ENCODERS
00245
00246 #ifdef HAVE_MMX
00247 MPV_common_init_mmx(s);
00248 #endif
00249 #ifdef ARCH_ALPHA
00250 MPV_common_init_axp(s);
00251 #endif
00252 #ifdef HAVE_MLIB
00253 MPV_common_init_mlib(s);
00254 #endif
00255 #ifdef HAVE_MMI
00256 MPV_common_init_mmi(s);
00257 #endif
00258 #ifdef ARCH_ARMV4L
00259 MPV_common_init_armv4l(s);
00260 #endif
00261 #ifdef ARCH_POWERPC
00262 MPV_common_init_ppc(s);
00263 #endif
00264
00265 #ifdef CONFIG_ENCODERS
00266 s->fast_dct_quantize= s->dct_quantize;
00267
00268 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){
00269 s->dct_quantize= dct_quantize_trellis_c;
00270 }
00271
00272 #endif //CONFIG_ENCODERS
00273
00274
00275
00276
00277 if(s->alternate_scan){
00278 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
00279 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
00280 }else{
00281 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
00282 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
00283 }
00284 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
00285 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
00286
00287 return 0;
00288 }
00289
00290 static void copy_picture(Picture *dst, Picture *src){
00291 *dst = *src;
00292 dst->type= FF_BUFFER_TYPE_COPY;
00293 }
00294
00295 static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
00296 int i;
00297
00298 dst->pict_type = src->pict_type;
00299 dst->quality = src->quality;
00300 dst->coded_picture_number = src->coded_picture_number;
00301 dst->display_picture_number = src->display_picture_number;
00302
00303 dst->pts = src->pts;
00304 dst->interlaced_frame = src->interlaced_frame;
00305 dst->top_field_first = src->top_field_first;
00306
00307 if(s->avctx->me_threshold){
00308 if(!src->motion_val[0])
00309 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
00310 if(!src->mb_type)
00311 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
00312 if(!src->ref_index[0])
00313 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
00314 if(src->motion_subsample_log2 != dst->motion_subsample_log2)
00315 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
00316 src->motion_subsample_log2, dst->motion_subsample_log2);
00317
00318 memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
00319
00320 for(i=0; i<2; i++){
00321 int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
00322 int height= ((16*s->mb_height)>>src->motion_subsample_log2);
00323
00324 if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
00325 memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
00326 }
00327 if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
00328 memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t));
00329 }
00330 }
00331 }
00332 }
00333
00338 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
00339 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1;
00340 const int mb_array_size= s->mb_stride*s->mb_height;
00341 const int b8_array_size= s->b8_stride*s->mb_height*2;
00342 const int b4_array_size= s->b4_stride*s->mb_height*4;
00343 int i;
00344
00345 if(shared){
00346 assert(pic->data[0]);
00347 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
00348 pic->type= FF_BUFFER_TYPE_SHARED;
00349 }else{
00350 int r;
00351
00352 assert(!pic->data[0]);
00353
00354 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
00355
00356 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
00357 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
00358 return -1;
00359 }
00360
00361 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
00362 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
00363 return -1;
00364 }
00365
00366 if(pic->linesize[1] != pic->linesize[2]){
00367 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
00368 return -1;
00369 }
00370
00371 s->linesize = pic->linesize[0];
00372 s->uvlinesize= pic->linesize[1];
00373 }
00374
00375 if(pic->qscale_table==NULL){
00376 if (s->encoding) {
00377 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
00378 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
00379 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
00380 }
00381
00382 CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2)
00383 CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
00384 CHECKED_ALLOCZ(pic->mb_type_base , big_mb_num * sizeof(uint32_t))
00385 pic->mb_type= pic->mb_type_base + s->mb_stride+1;
00386 if(s->out_format == FMT_H264){
00387 for(i=0; i<2; i++){
00388 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t))
00389 pic->motion_val[i]= pic->motion_val_base[i]+4;
00390 CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
00391 }
00392 pic->motion_subsample_log2= 2;
00393 }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
00394 for(i=0; i<2; i++){
00395 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t))
00396 pic->motion_val[i]= pic->motion_val_base[i]+4;
00397 CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
00398 }
00399 pic->motion_subsample_log2= 3;
00400 }
00401 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
00402 CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
00403 }
00404 pic->qstride= s->mb_stride;
00405 CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
00406 }
00407
00408
00409 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
00410 s->prev_pict_types[0]= s->pict_type;
00411 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == B_TYPE)
00412 pic->age= INT_MAX;
00413
00414 return 0;
00415 fail:
00416 return -1;
00417 }
00418
00422 static void free_picture(MpegEncContext *s, Picture *pic){
00423 int i;
00424
00425 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
00426 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00427 }
00428
00429 av_freep(&pic->mb_var);
00430 av_freep(&pic->mc_mb_var);
00431 av_freep(&pic->mb_mean);
00432 av_freep(&pic->mbskip_table);
00433 av_freep(&pic->qscale_table);
00434 av_freep(&pic->mb_type_base);
00435 av_freep(&pic->dct_coeff);
00436 av_freep(&pic->pan_scan);
00437 pic->mb_type= NULL;
00438 for(i=0; i<2; i++){
00439 av_freep(&pic->motion_val_base[i]);
00440 av_freep(&pic->ref_index[i]);
00441 }
00442
00443 if(pic->type == FF_BUFFER_TYPE_SHARED){
00444 for(i=0; i<4; i++){
00445 pic->base[i]=
00446 pic->data[i]= NULL;
00447 }
00448 pic->type= 0;
00449 }
00450 }
00451
00452 static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
00453 int i;
00454
00455
00456 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2);
00457 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17;
00458
00459
00460 CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
00461 s->rd_scratchpad= s->me.scratchpad;
00462 s->b_scratchpad= s->me.scratchpad;
00463 s->obmc_scratchpad= s->me.scratchpad + 16;
00464 if (s->encoding) {
00465 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
00466 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
00467 if(s->avctx->noise_reduction){
00468 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
00469 }
00470 }
00471 CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
00472 s->block= s->blocks[0];
00473
00474 for(i=0;i<12;i++){
00475 s->pblocks[i] = (short *)(&s->block[i]);
00476 }
00477 return 0;
00478 fail:
00479 return -1;
00480 }
00481
00482 static void free_duplicate_context(MpegEncContext *s){
00483 if(s==NULL) return;
00484
00485 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
00486 av_freep(&s->me.scratchpad);
00487 s->rd_scratchpad=
00488 s->b_scratchpad=
00489 s->obmc_scratchpad= NULL;
00490
00491 av_freep(&s->dct_error_sum);
00492 av_freep(&s->me.map);
00493 av_freep(&s->me.score_map);
00494 av_freep(&s->blocks);
00495 s->block= NULL;
00496 }
00497
00498 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
00499 #define COPY(a) bak->a= src->a
00500 COPY(allocated_edge_emu_buffer);
00501 COPY(edge_emu_buffer);
00502 COPY(me.scratchpad);
00503 COPY(rd_scratchpad);
00504 COPY(b_scratchpad);
00505 COPY(obmc_scratchpad);
00506 COPY(me.map);
00507 COPY(me.score_map);
00508 COPY(blocks);
00509 COPY(block);
00510 COPY(start_mb_y);
00511 COPY(end_mb_y);
00512 COPY(me.map_generation);
00513 COPY(pb);
00514 COPY(dct_error_sum);
00515 COPY(dct_count[0]);
00516 COPY(dct_count[1]);
00517 #undef COPY
00518 }
00519
00520 void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
00521 MpegEncContext bak;
00522 int i;
00523
00524
00525 backup_duplicate_context(&bak, dst);
00526 memcpy(dst, src, sizeof(MpegEncContext));
00527 backup_duplicate_context(dst, &bak);
00528 for(i=0;i<12;i++){
00529 dst->pblocks[i] = (short *)(&dst->block[i]);
00530 }
00531
00532 }
00533
00534 static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
00535 #define COPY(a) dst->a= src->a
00536 COPY(pict_type);
00537 COPY(current_picture);
00538 COPY(f_code);
00539 COPY(b_code);
00540 COPY(qscale);
00541 COPY(lambda);
00542 COPY(lambda2);
00543 COPY(picture_in_gop_number);
00544 COPY(gop_picture_number);
00545 COPY(frame_pred_frame_dct);
00546 COPY(progressive_frame);
00547 COPY(partitioned_frame);
00548 #undef COPY
00549 }
00550
00555 static void MPV_common_defaults(MpegEncContext *s){
00556 s->y_dc_scale_table=
00557 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00558 s->chroma_qscale_table= ff_default_chroma_qscale_table;
00559 s->progressive_frame= 1;
00560 s->progressive_sequence= 1;
00561 s->picture_structure= PICT_FRAME;
00562
00563 s->coded_picture_number = 0;
00564 s->picture_number = 0;
00565 s->input_picture_number = 0;
00566
00567 s->picture_in_gop_number = 0;
00568
00569 s->f_code = 1;
00570 s->b_code = 1;
00571 }
00572
00577 void MPV_decode_defaults(MpegEncContext *s){
00578 MPV_common_defaults(s);
00579 }
00580
00586 #ifdef CONFIG_ENCODERS
00587 static void MPV_encode_defaults(MpegEncContext *s){
00588 static int done=0;
00589
00590 MPV_common_defaults(s);
00591
00592 if(!done){
00593 int i;
00594 done=1;
00595
00596 default_mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
00597 memset(default_fcode_tab , 0, sizeof(uint8_t)*(2*MAX_MV+1));
00598
00599 for(i=-16; i<16; i++){
00600 default_fcode_tab[i + MAX_MV]= 1;
00601 }
00602 }
00603 s->me.mv_penalty= default_mv_penalty;
00604 s->fcode_tab= default_fcode_tab;
00605 }
00606 #endif //CONFIG_ENCODERS
00607
00612 int MPV_common_init(MpegEncContext *s)
00613 {
00614 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
00615
00616 if(s->avctx->thread_count > MAX_THREADS || (16*s->avctx->thread_count > s->height && s->height)){
00617 av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
00618 return -1;
00619 }
00620
00621 if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
00622 return -1;
00623
00624 dsputil_init(&s->dsp, s->avctx);
00625 DCT_common_init(s);
00626
00627 s->flags= s->avctx->flags;
00628 s->flags2= s->avctx->flags2;
00629
00630 s->mb_width = (s->width + 15) / 16;
00631 s->mb_height = (s->height + 15) / 16;
00632 s->mb_stride = s->mb_width + 1;
00633 s->b8_stride = s->mb_width*2 + 1;
00634 s->b4_stride = s->mb_width*4 + 1;
00635 mb_array_size= s->mb_height * s->mb_stride;
00636 mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
00637
00638
00639 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
00640 &(s->chroma_y_shift) );
00641
00642
00643 s->h_edge_pos= s->mb_width*16;
00644 s->v_edge_pos= s->mb_height*16;
00645
00646 s->mb_num = s->mb_width * s->mb_height;
00647
00648 s->block_wrap[0]=
00649 s->block_wrap[1]=
00650 s->block_wrap[2]=
00651 s->block_wrap[3]= s->b8_stride;
00652 s->block_wrap[4]=
00653 s->block_wrap[5]= s->mb_stride;
00654
00655 y_size = s->b8_stride * (2 * s->mb_height + 1);
00656 c_size = s->mb_stride * (s->mb_height + 1);
00657 yc_size = y_size + 2 * c_size;
00658
00659
00660 s->avctx->codec_tag= toupper( s->avctx->codec_tag &0xFF)
00661 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
00662 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
00663 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
00664
00665 s->avctx->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
00666 + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
00667 + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
00668 + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
00669
00670 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
00671
00672 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int))
00673 for(y=0; y<s->mb_height; y++){
00674 for(x=0; x<s->mb_width; x++){
00675 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
00676 }
00677 }
00678 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width;
00679
00680 if (s->encoding) {
00681
00682 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00683 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00684 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00685 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00686 CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00687 CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00688 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
00689 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
00690 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
00691 s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
00692 s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
00693 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
00694
00695 if(s->msmpeg4_version){
00696 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
00697 }
00698 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
00699
00700
00701 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t))
00702
00703 CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
00704
00705 CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
00706 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
00707 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
00708 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
00709 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
00710 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
00711
00712 if(s->avctx->noise_reduction){
00713 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
00714 }
00715 }
00716 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
00717
00718 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
00719
00720 if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
00721
00722 for(i=0; i<2; i++){
00723 int j, k;
00724 for(j=0; j<2; j++){
00725 for(k=0; k<2; k++){
00726 CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
00727 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
00728 }
00729 CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
00730 CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
00731 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
00732 }
00733 CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
00734 }
00735 }
00736 if (s->out_format == FMT_H263) {
00737
00738 CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
00739 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
00740 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
00741 s->ac_val[2] = s->ac_val[1] + c_size;
00742
00743
00744 CHECKED_ALLOCZ(s->coded_block_base, y_size);
00745 s->coded_block= s->coded_block_base + s->b8_stride + 1;
00746
00747
00748 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
00749 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
00750 }
00751
00752 if (s->h263_pred || s->h263_plus || !s->encoding) {
00753
00754
00755 CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
00756 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
00757 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
00758 s->dc_val[2] = s->dc_val[1] + c_size;
00759 for(i=0;i<yc_size;i++)
00760 s->dc_val_base[i] = 1024;
00761 }
00762
00763
00764 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
00765 memset(s->mbintra_table, 1, mb_array_size);
00766
00767
00768 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
00769
00770 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
00771
00772 s->parse_context.state= -1;
00773 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
00774 s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
00775 s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
00776 s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
00777 }
00778
00779 s->context_initialized = 1;
00780
00781 s->thread_context[0]= s;
00782 for(i=1; i<s->avctx->thread_count; i++){
00783 s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
00784 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
00785 }
00786
00787 for(i=0; i<s->avctx->thread_count; i++){
00788 if(init_duplicate_context(s->thread_context[i], s) < 0)
00789 goto fail;
00790 s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
00791 s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
00792 }
00793
00794 return 0;
00795 fail:
00796 MPV_common_end(s);
00797 return -1;
00798 }
00799
00800
00801 void MPV_common_end(MpegEncContext *s)
00802 {
00803 int i, j, k;
00804
00805 for(i=0; i<s->avctx->thread_count; i++){
00806 free_duplicate_context(s->thread_context[i]);
00807 }
00808 for(i=1; i<s->avctx->thread_count; i++){
00809 av_freep(&s->thread_context[i]);
00810 }
00811
00812 av_freep(&s->parse_context.buffer);
00813 s->parse_context.buffer_size=0;
00814
00815 av_freep(&s->mb_type);
00816 av_freep(&s->p_mv_table_base);
00817 av_freep(&s->b_forw_mv_table_base);
00818 av_freep(&s->b_back_mv_table_base);
00819 av_freep(&s->b_bidir_forw_mv_table_base);
00820 av_freep(&s->b_bidir_back_mv_table_base);
00821 av_freep(&s->b_direct_mv_table_base);
00822 s->p_mv_table= NULL;
00823 s->b_forw_mv_table= NULL;
00824 s->b_back_mv_table= NULL;
00825 s->b_bidir_forw_mv_table= NULL;
00826 s->b_bidir_back_mv_table= NULL;
00827 s->b_direct_mv_table= NULL;
00828 for(i=0; i<2; i++){
00829 for(j=0; j<2; j++){
00830 for(k=0; k<2; k++){
00831 av_freep(&s->b_field_mv_table_base[i][j][k]);
00832 s->b_field_mv_table[i][j][k]=NULL;
00833 }
00834 av_freep(&s->b_field_select_table[i][j]);
00835 av_freep(&s->p_field_mv_table_base[i][j]);
00836 s->p_field_mv_table[i][j]=NULL;
00837 }
00838 av_freep(&s->p_field_select_table[i]);
00839 }
00840
00841 av_freep(&s->dc_val_base);
00842 av_freep(&s->ac_val_base);
00843 av_freep(&s->coded_block_base);
00844 av_freep(&s->mbintra_table);
00845 av_freep(&s->cbp_table);
00846 av_freep(&s->pred_dir_table);
00847
00848 av_freep(&s->mbskip_table);
00849 av_freep(&s->prev_pict_types);
00850 av_freep(&s->bitstream_buffer);
00851 s->allocated_bitstream_buffer_size=0;
00852
00853 av_freep(&s->avctx->stats_out);
00854 av_freep(&s->ac_stats);
00855 av_freep(&s->error_status_table);
00856 av_freep(&s->mb_index2xy);
00857 av_freep(&s->lambda_table);
00858 av_freep(&s->q_intra_matrix);
00859 av_freep(&s->q_inter_matrix);
00860 av_freep(&s->q_intra_matrix16);
00861 av_freep(&s->q_inter_matrix16);
00862 av_freep(&s->input_picture);
00863 av_freep(&s->reordered_input_picture);
00864 av_freep(&s->dct_offset);
00865
00866 if(s->picture){
00867 for(i=0; i<MAX_PICTURE_COUNT; i++){
00868 free_picture(s, &s->picture[i]);
00869 }
00870 }
00871 av_freep(&s->picture);
00872 s->context_initialized = 0;
00873 s->last_picture_ptr=
00874 s->next_picture_ptr=
00875 s->current_picture_ptr= NULL;
00876 s->linesize= s->uvlinesize= 0;
00877
00878 for(i=0; i<3; i++)
00879 av_freep(&s->visualization_buffer[i]);
00880
00881 avcodec_default_free_buffers(s->avctx);
00882 }
00883
00884 #ifdef CONFIG_ENCODERS
00885
00886
00887 int MPV_encode_init(AVCodecContext *avctx)
00888 {
00889 MpegEncContext *s = avctx->priv_data;
00890 int i, dummy;
00891 int chroma_h_shift, chroma_v_shift;
00892
00893 MPV_encode_defaults(s);
00894
00895 if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUV420P){
00896 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
00897 return -1;
00898 }
00899
00900 if(avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG){
00901 if(avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL && avctx->pix_fmt != PIX_FMT_YUVJ420P){
00902 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
00903 return -1;
00904 }
00905 }else{
00906 if(avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL && avctx->pix_fmt != PIX_FMT_YUV420P){
00907 av_log(avctx, AV_LOG_ERROR, "colorspace not supported\n");
00908 return -1;
00909 }
00910 }
00911
00912 s->bit_rate = avctx->bit_rate;
00913 s->width = avctx->width;
00914 s->height = avctx->height;
00915 if(avctx->gop_size > 600){
00916 av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
00917 avctx->gop_size=600;
00918 }
00919 s->gop_size = avctx->gop_size;
00920 s->avctx = avctx;
00921 s->flags= avctx->flags;
00922 s->flags2= avctx->flags2;
00923 s->max_b_frames= avctx->max_b_frames;
00924 s->codec_id= avctx->codec->id;
00925 s->luma_elim_threshold = avctx->luma_elim_threshold;
00926 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
00927 s->strict_std_compliance= avctx->strict_std_compliance;
00928 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
00929 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
00930 s->mpeg_quant= avctx->mpeg_quant;
00931 s->rtp_mode= !!avctx->rtp_payload_size;
00932 s->intra_dc_precision= avctx->intra_dc_precision;
00933 s->user_specified_pts = AV_NOPTS_VALUE;
00934
00935 if (s->gop_size <= 1) {
00936 s->intra_only = 1;
00937 s->gop_size = 12;
00938 } else {
00939 s->intra_only = 0;
00940 }
00941
00942 s->me_method = avctx->me_method;
00943
00944
00945 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
00946
00947 s->adaptive_quant= ( s->avctx->lumi_masking
00948 || s->avctx->dark_masking
00949 || s->avctx->temporal_cplx_masking
00950 || s->avctx->spatial_cplx_masking
00951 || s->avctx->p_masking
00952 || s->avctx->border_masking
00953 || (s->flags&CODEC_FLAG_QP_RD))
00954 && !s->fixed_qscale;
00955
00956 s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
00957 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
00958 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
00959
00960 if(avctx->rc_max_rate && !avctx->rc_buffer_size){
00961 av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
00962 return -1;
00963 }
00964
00965 if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
00966 av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
00967 }
00968
00969 if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
00970 av_log(avctx, AV_LOG_INFO, "bitrate below min bitrate\n");
00971 return -1;
00972 }
00973
00974 if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
00975 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
00976 return -1;
00977 }
00978
00979 if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
00980 && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
00981 && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
00982
00983 av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n");
00984 }
00985
00986 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
00987 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
00988 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
00989 return -1;
00990 }
00991
00992 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
00993 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n");
00994 return -1;
00995 }
00996
00997 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
00998 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
00999 return -1;
01000 }
01001
01002 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
01003 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
01004 return -1;
01005 }
01006
01007 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
01008 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
01009 return -1;
01010 }
01011
01012 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
01013 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
01014 return -1;
01015 }
01016
01017 if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
01018 && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
01019 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
01020 return -1;
01021 }
01022
01023 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){
01024 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n");
01025 return -1;
01026 }
01027
01028 if((s->flags & CODEC_FLAG_CBP_RD) && !(s->flags & CODEC_FLAG_TRELLIS_QUANT)){
01029 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
01030 return -1;
01031 }
01032
01033 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
01034 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
01035 return -1;
01036 }
01037
01038 if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
01039 av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection arent supported yet\n");
01040 return -1;
01041 }
01042
01043 if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
01044 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
01045 && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
01046 av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
01047 return -1;
01048 }
01049
01050 if(s->avctx->thread_count > 1)
01051 s->rtp_mode= 1;
01052
01053 if(!avctx->time_base.den || !avctx->time_base.num){
01054 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
01055 return -1;
01056 }
01057
01058 i= (INT_MAX/2+128)>>8;
01059 if(avctx->me_threshold >= i){
01060 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1);
01061 return -1;
01062 }
01063 if(avctx->mb_threshold >= i){
01064 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1);
01065 return -1;
01066 }
01067
01068 if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){
01069 av_log(avctx, AV_LOG_ERROR, "b_frame_strategy must be 0 on the second pass");
01070 return -1;
01071 }
01072
01073 i= ff_gcd(avctx->time_base.den, avctx->time_base.num);
01074 if(i > 1){
01075 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
01076 avctx->time_base.den /= i;
01077 avctx->time_base.num /= i;
01078
01079 }
01080
01081 if(s->codec_id==CODEC_ID_MJPEG){
01082 s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1);
01083 s->inter_quant_bias= 0;
01084 }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
01085 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3);
01086 s->inter_quant_bias= 0;
01087 }else{
01088 s->intra_quant_bias=0;
01089 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2));
01090 }
01091
01092 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
01093 s->intra_quant_bias= avctx->intra_quant_bias;
01094 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
01095 s->inter_quant_bias= avctx->inter_quant_bias;
01096
01097 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
01098
01099 if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){
01100 av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n");
01101 return -1;
01102 }
01103 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
01104
01105 switch(avctx->codec->id) {
01106 case CODEC_ID_MPEG1VIDEO:
01107 s->out_format = FMT_MPEG1;
01108 s->low_delay= 0;
01109 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
01110 break;
01111 case CODEC_ID_MPEG2VIDEO:
01112 s->out_format = FMT_MPEG1;
01113 s->low_delay= 0;
01114 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
01115 s->rtp_mode= 1;
01116 break;
01117 case CODEC_ID_LJPEG:
01118 case CODEC_ID_MJPEG:
01119 s->out_format = FMT_MJPEG;
01120 s->intra_only = 1;
01121 s->mjpeg_write_tables = 1;
01122 s->mjpeg_data_only_frames = 0;
01123 s->mjpeg_vsample[0] = 1<<chroma_v_shift;
01124 s->mjpeg_vsample[1] = 1;
01125 s->mjpeg_vsample[2] = 1;
01126 s->mjpeg_hsample[0] = 1<<chroma_h_shift;
01127 s->mjpeg_hsample[1] = 1;
01128 s->mjpeg_hsample[2] = 1;
01129 if (mjpeg_init(s) < 0)
01130 return -1;
01131 avctx->delay=0;
01132 s->low_delay=1;
01133 break;
01134 case CODEC_ID_H261:
01135 s->out_format = FMT_H261;
01136 avctx->delay=0;
01137 s->low_delay=1;
01138 break;
01139 case CODEC_ID_H263:
01140 if (h263_get_picture_format(s->width, s->height) == 7) {
01141 av_log(avctx, AV_LOG_INFO, "Input picture size isn't suitable for h263 codec! try h263+\n");
01142 return -1;
01143 }
01144 s->out_format = FMT_H263;
01145 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
01146 avctx->delay=0;
01147 s->low_delay=1;
01148 break;
01149 case CODEC_ID_H263P:
01150 s->out_format = FMT_H263;
01151 s->h263_plus = 1;
01152
01153 s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
01154 s->h263_aic= (avctx->flags & CODEC_FLAG_H263P_AIC) ? 1:0;
01155 s->modified_quant= s->h263_aic;
01156 s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
01157 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
01158 s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
01159 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
01160 s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
01161
01162
01163
01164 avctx->delay=0;
01165 s->low_delay=1;
01166 break;
01167 case CODEC_ID_FLV1:
01168 s->out_format = FMT_H263;
01169 s->h263_flv = 2;
01170 s->unrestricted_mv = 1;
01171 s->rtp_mode=0;
01172 avctx->delay=0;
01173 s->low_delay=1;
01174 break;
01175 case CODEC_ID_RV10:
01176 s->out_format = FMT_H263;
01177 avctx->delay=0;
01178 s->low_delay=1;
01179 break;
01180 case CODEC_ID_RV20:
01181 s->out_format = FMT_H263;
01182 avctx->delay=0;
01183 s->low_delay=1;
01184 s->modified_quant=1;
01185 s->h263_aic=1;
01186 s->h263_plus=1;
01187 s->loop_filter=1;
01188 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
01189 break;
01190 case CODEC_ID_MPEG4:
01191 s->out_format = FMT_H263;
01192 s->h263_pred = 1;
01193 s->unrestricted_mv = 1;
01194 s->low_delay= s->max_b_frames ? 0 : 1;
01195 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
01196 break;
01197 case CODEC_ID_MSMPEG4V1:
01198 s->out_format = FMT_H263;
01199 s->h263_msmpeg4 = 1;
01200 s->h263_pred = 1;
01201 s->unrestricted_mv = 1;
01202 s->msmpeg4_version= 1;
01203 avctx->delay=0;
01204 s->low_delay=1;
01205 break;
01206 case CODEC_ID_MSMPEG4V2:
01207 s->out_format = FMT_H263;
01208 s->h263_msmpeg4 = 1;
01209 s->h263_pred = 1;
01210 s->unrestricted_mv = 1;
01211 s->msmpeg4_version= 2;
01212 avctx->delay=0;
01213 s->low_delay=1;
01214 break;
01215 case CODEC_ID_MSMPEG4V3:
01216 s->out_format = FMT_H263;
01217 s->h263_msmpeg4 = 1;
01218 s->h263_pred = 1;
01219 s->unrestricted_mv = 1;
01220 s->msmpeg4_version= 3;
01221 s->flipflop_rounding=1;
01222 avctx->delay=0;
01223 s->low_delay=1;
01224 break;
01225 case CODEC_ID_WMV1:
01226 s->out_format = FMT_H263;
01227 s->h263_msmpeg4 = 1;
01228 s->h263_pred = 1;
01229 s->unrestricted_mv = 1;
01230 s->msmpeg4_version= 4;
01231 s->flipflop_rounding=1;
01232 avctx->delay=0;
01233 s->low_delay=1;
01234 break;
01235 case CODEC_ID_WMV2:
01236 s->out_format = FMT_H263;
01237 s->h263_msmpeg4 = 1;
01238 s->h263_pred = 1;
01239 s->unrestricted_mv = 1;
01240 s->msmpeg4_version= 5;
01241 s->flipflop_rounding=1;
01242 avctx->delay=0;
01243 s->low_delay=1;
01244 break;
01245 default:
01246 return -1;
01247 }
01248
01249 avctx->has_b_frames= !s->low_delay;
01250
01251 s->encoding = 1;
01252
01253
01254 if (MPV_common_init(s) < 0)
01255 return -1;
01256
01257 if(s->modified_quant)
01258 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
01259 s->progressive_frame=
01260 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME));
01261 s->quant_precision=5;
01262
01263 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
01264 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
01265
01266 #ifdef CONFIG_H261_ENCODER
01267 if (s->out_format == FMT_H261)
01268 ff_h261_encode_init(s);
01269 #endif
01270 if (s->out_format == FMT_H263)
01271 h263_encode_init(s);
01272 if(s->msmpeg4_version)
01273 ff_msmpeg4_encode_init(s);
01274 if (s->out_format == FMT_MPEG1)
01275 ff_mpeg1_encode_init(s);
01276
01277
01278 for(i=0;i<64;i++) {
01279 int j= s->dsp.idct_permutation[i];
01280 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
01281 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
01282 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
01283 }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
01284 s->intra_matrix[j] =
01285 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
01286 }else
01287 {
01288 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
01289 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
01290 }
01291 if(s->avctx->intra_matrix)
01292 s->intra_matrix[j] = s->avctx->intra_matrix[i];
01293 if(s->avctx->inter_matrix)
01294 s->inter_matrix[j] = s->avctx->inter_matrix[i];
01295 }
01296
01297
01298
01299 if (s->out_format != FMT_MJPEG) {
01300 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
01301 s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
01302 convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
01303 s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
01304 }
01305
01306 if(ff_rate_control_init(s) < 0)
01307 return -1;
01308
01309 return 0;
01310 }
01311
01312 int MPV_encode_end(AVCodecContext *avctx)
01313 {
01314 MpegEncContext *s = avctx->priv_data;
01315
01316 #ifdef STATS
01317 print_stats();
01318 #endif
01319
01320 ff_rate_control_uninit(s);
01321
01322 MPV_common_end(s);
01323 if (s->out_format == FMT_MJPEG)
01324 mjpeg_close(s);
01325
01326 av_freep(&avctx->extradata);
01327
01328 return 0;
01329 }
01330
01331 #endif //CONFIG_ENCODERS
01332
01333 void init_rl(RLTable *rl, int use_static)
01334 {
01335 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
01336 uint8_t index_run[MAX_RUN+1];
01337 int last, run, level, start, end, i;
01338
01339
01340 if(use_static && rl->max_level[0])
01341 return;
01342
01343
01344 for(last=0;last<2;last++) {
01345 if (last == 0) {
01346 start = 0;
01347 end = rl->last;
01348 } else {
01349 start = rl->last;
01350 end = rl->n;
01351 }
01352
01353 memset(max_level, 0, MAX_RUN + 1);
01354 memset(max_run, 0, MAX_LEVEL + 1);
01355 memset(index_run, rl->n, MAX_RUN + 1);
01356 for(i=start;i<end;i++) {
01357 run = rl->table_run[i];
01358 level = rl->table_level[i];
01359 if (index_run[run] == rl->n)
01360 index_run[run] = i;
01361 if (level > max_level[run])
01362 max_level[run] = level;
01363 if (run > max_run[level])
01364 max_run[level] = run;
01365 }
01366 if(use_static)
01367 rl->max_level[last] = av_mallocz_static(MAX_RUN + 1);
01368 else
01369 rl->max_level[last] = av_malloc(MAX_RUN + 1);
01370 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
01371 if(use_static)
01372 rl->max_run[last] = av_mallocz_static(MAX_LEVEL + 1);
01373 else
01374 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
01375 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
01376 if(use_static)
01377 rl->index_run[last] = av_mallocz_static(MAX_RUN + 1);
01378 else
01379 rl->index_run[last] = av_malloc(MAX_RUN + 1);
01380 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
01381 }
01382 }
01383
01384
01385
01386 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
01387 {
01388 uint8_t *ptr, *last_line;
01389 int i;
01390
01391 last_line = buf + (height - 1) * wrap;
01392 for(i=0;i<w;i++) {
01393
01394 memcpy(buf - (i + 1) * wrap, buf, width);
01395 memcpy(last_line + (i + 1) * wrap, last_line, width);
01396 }
01397
01398 ptr = buf;
01399 for(i=0;i<height;i++) {
01400 memset(ptr - w, ptr[0], w);
01401 memset(ptr + width, ptr[width-1], w);
01402 ptr += wrap;
01403 }
01404
01405 for(i=0;i<w;i++) {
01406 memset(buf - (i + 1) * wrap - w, buf[0], w);
01407 memset(buf - (i + 1) * wrap + width, buf[width-1], w);
01408 memset(last_line + (i + 1) * wrap - w, last_line[0], w);
01409 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w);
01410 }
01411 }
01412
01413 int ff_find_unused_picture(MpegEncContext *s, int shared){
01414 int i;
01415
01416 if(shared){
01417 for(i=0; i<MAX_PICTURE_COUNT; i++){
01418 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
01419 }
01420 }else{
01421 for(i=0; i<MAX_PICTURE_COUNT; i++){
01422 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i;
01423 }
01424 for(i=0; i<MAX_PICTURE_COUNT; i++){
01425 if(s->picture[i].data[0]==NULL) return i;
01426 }
01427 }
01428
01429 assert(0);
01430 return -1;
01431 }
01432
01433 static void update_noise_reduction(MpegEncContext *s){
01434 int intra, i;
01435
01436 for(intra=0; intra<2; intra++){
01437 if(s->dct_count[intra] > (1<<16)){
01438 for(i=0; i<64; i++){
01439 s->dct_error_sum[intra][i] >>=1;
01440 }
01441 s->dct_count[intra] >>= 1;
01442 }
01443
01444 for(i=0; i<64; i++){
01445 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
01446 }
01447 }
01448 }
01449
01453 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
01454 {
01455 int i;
01456 AVFrame *pic;
01457 s->mb_skipped = 0;
01458
01459 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
01460
01461
01462 if (s->pict_type != B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
01463 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
01464
01465
01466
01467 if(!s->encoding){
01468 for(i=0; i<MAX_PICTURE_COUNT; i++){
01469 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
01470 av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
01471 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
01472 }
01473 }
01474 }
01475 }
01476 alloc:
01477 if(!s->encoding){
01478
01479 for(i=0; i<MAX_PICTURE_COUNT; i++){
01480 if(s->picture[i].data[0] && !s->picture[i].reference ){
01481 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
01482 }
01483 }
01484
01485 if(s->current_picture_ptr && s->current_picture_ptr->data[0]==NULL)
01486 pic= (AVFrame*)s->current_picture_ptr;
01487 else{
01488 i= ff_find_unused_picture(s, 0);
01489 pic= (AVFrame*)&s->picture[i];
01490 }
01491
01492 pic->reference= (s->pict_type != B_TYPE || s->codec_id == CODEC_ID_H264)
01493 && !s->dropable ? 3 : 0;
01494
01495 pic->coded_picture_number= s->coded_picture_number++;
01496
01497 if( alloc_picture(s, (Picture*)pic, 0) < 0)
01498 return -1;
01499
01500 s->current_picture_ptr= (Picture*)pic;
01501 s->current_picture_ptr->top_field_first= s->top_field_first;
01502 s->current_picture_ptr->interlaced_frame= !s->progressive_frame && !s->progressive_sequence;
01503 }
01504
01505 s->current_picture_ptr->pict_type= s->pict_type;
01506
01507
01508 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
01509
01510 copy_picture(&s->current_picture, s->current_picture_ptr);
01511
01512 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
01513 if (s->pict_type != B_TYPE) {
01514 s->last_picture_ptr= s->next_picture_ptr;
01515 if(!s->dropable)
01516 s->next_picture_ptr= s->current_picture_ptr;
01517 }
01518
01519
01520
01521
01522
01523
01524 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
01525 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
01526
01527 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
01528 av_log(avctx, AV_LOG_ERROR, "warning: first frame is no keyframe\n");
01529 assert(s->pict_type != B_TYPE);
01530 goto alloc;
01531 }
01532
01533 assert(s->pict_type == I_TYPE || (s->last_picture_ptr && s->last_picture_ptr->data[0]));
01534
01535 if(s->picture_structure!=PICT_FRAME){
01536 int i;
01537 for(i=0; i<4; i++){
01538 if(s->picture_structure == PICT_BOTTOM_FIELD){
01539 s->current_picture.data[i] += s->current_picture.linesize[i];
01540 }
01541 s->current_picture.linesize[i] *= 2;
01542 s->last_picture.linesize[i] *=2;
01543 s->next_picture.linesize[i] *=2;
01544 }
01545 }
01546 }
01547
01548 s->hurry_up= s->avctx->hurry_up;
01549 s->error_resilience= avctx->error_resilience;
01550
01551
01552
01553 if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
01554 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
01555 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
01556 }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
01557 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
01558 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
01559 }else{
01560 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
01561 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
01562 }
01563
01564 if(s->dct_error_sum){
01565 assert(s->avctx->noise_reduction && s->encoding);
01566
01567 update_noise_reduction(s);
01568 }
01569
01570 #ifdef HAVE_XVMC
01571 if(s->avctx->xvmc_acceleration)
01572 return XVMC_field_start(s, avctx);
01573 #endif
01574 return 0;
01575 }
01576
01577
01578 void MPV_frame_end(MpegEncContext *s)
01579 {
01580 int i;
01581
01582 #ifdef HAVE_XVMC
01583
01584 if(s->avctx->xvmc_acceleration){
01585 XVMC_field_end(s);
01586 }else
01587 #endif
01588 if(s->unrestricted_mv && s->current_picture.reference && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
01589 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH );
01590 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
01591 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
01592 }
01593 emms_c();
01594
01595 s->last_pict_type = s->pict_type;
01596 if(s->pict_type!=B_TYPE){
01597 s->last_non_b_pict_type= s->pict_type;
01598 }
01599 #if 0
01600
01601 for(i=0; i<MAX_PICTURE_COUNT; i++){
01602 if(s->picture[i].data[0] == s->current_picture.data[0]){
01603 s->picture[i]= s->current_picture;
01604 break;
01605 }
01606 }
01607 assert(i<MAX_PICTURE_COUNT);
01608 #endif
01609
01610 if(s->encoding){
01611
01612 for(i=0; i<MAX_PICTURE_COUNT; i++){
01613 if(s->picture[i].data[0] && !s->picture[i].reference ){
01614 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
01615 }
01616 }
01617 }
01618
01619 #if 0
01620 memset(&s->last_picture, 0, sizeof(Picture));
01621 memset(&s->next_picture, 0, sizeof(Picture));
01622 memset(&s->current_picture, 0, sizeof(Picture));
01623 #endif
01624 s->avctx->coded_frame= (AVFrame*)s->current_picture_ptr;
01625 }
01626
01634 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
01635 int t, x, y, fr, f;
01636
01637 sx= clip(sx, 0, w-1);
01638 sy= clip(sy, 0, h-1);
01639 ex= clip(ex, 0, w-1);
01640 ey= clip(ey, 0, h-1);
01641
01642 buf[sy*stride + sx]+= color;
01643
01644 if(ABS(ex - sx) > ABS(ey - sy)){
01645 if(sx > ex){
01646 t=sx; sx=ex; ex=t;
01647 t=sy; sy=ey; ey=t;
01648 }
01649 buf+= sx + sy*stride;
01650 ex-= sx;
01651 f= ((ey-sy)<<16)/ex;
01652 for(x= 0; x <= ex; x++){
01653 y = (x*f)>>16;
01654 fr= (x*f)&0xFFFF;
01655 buf[ y *stride + x]+= (color*(0x10000-fr))>>16;
01656 buf[(y+1)*stride + x]+= (color* fr )>>16;
01657 }
01658 }else{
01659 if(sy > ey){
01660 t=sx; sx=ex; ex=t;
01661 t=sy; sy=ey; ey=t;
01662 }
01663 buf+= sx + sy*stride;
01664 ey-= sy;
01665 if(ey) f= ((ex-sx)<<16)/ey;
01666 else f= 0;
01667 for(y= 0; y <= ey; y++){
01668 x = (y*f)>>16;
01669 fr= (y*f)&0xFFFF;
01670 buf[y*stride + x ]+= (color*(0x10000-fr))>>16;;
01671 buf[y*stride + x+1]+= (color* fr )>>16;;
01672 }
01673 }
01674 }
01675
01683 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color){
01684 int dx,dy;
01685
01686 sx= clip(sx, -100, w+100);
01687 sy= clip(sy, -100, h+100);
01688 ex= clip(ex, -100, w+100);
01689 ey= clip(ey, -100, h+100);
01690
01691 dx= ex - sx;
01692 dy= ey - sy;
01693
01694 if(dx*dx + dy*dy > 3*3){
01695 int rx= dx + dy;
01696 int ry= -dx + dy;
01697 int length= ff_sqrt((rx*rx + ry*ry)<<8);
01698
01699
01700 rx= ROUNDED_DIV(rx*3<<4, length);
01701 ry= ROUNDED_DIV(ry*3<<4, length);
01702
01703 draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
01704 draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
01705 }
01706 draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
01707 }
01708
01712 void ff_print_debug_info(MpegEncContext *s, AVFrame *pict){
01713
01714 if(!pict || !pict->mb_type) return;
01715
01716 if(s->avctx->debug&(FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)){
01717 int x,y;
01718
01719 av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
01720 switch (pict->pict_type) {
01721 case FF_I_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"I\n"); break;
01722 case FF_P_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"P\n"); break;
01723 case FF_B_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"B\n"); break;
01724 case FF_S_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"S\n"); break;
01725 case FF_SI_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SI\n"); break;
01726 case FF_SP_TYPE: av_log(s->avctx,AV_LOG_DEBUG,"SP\n"); break;
01727 }
01728 for(y=0; y<s->mb_height; y++){
01729 for(x=0; x<s->mb_width; x++){
01730 if(s->avctx->debug&FF_DEBUG_SKIP){
01731 int count= s->mbskip_table[x + y*s->mb_stride];
01732 if(count>9) count=9;
01733 av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
01734 }
01735 if(s->avctx->debug&FF_DEBUG_QP){
01736 av_log(s->avctx, AV_LOG_DEBUG, "%2d", pict->qscale_table[x + y*s->mb_stride]);
01737 }
01738 if(s->avctx->debug&FF_DEBUG_MB_TYPE){
01739 int mb_type= pict->mb_type[x + y*s->mb_stride];
01740
01741 if(IS_PCM(mb_type))
01742 av_log(s->avctx, AV_LOG_DEBUG, "P");
01743 else if(IS_INTRA(mb_type) && IS_ACPRED(mb_type))
01744 av_log(s->avctx, AV_LOG_DEBUG, "A");
01745 else if(IS_INTRA4x4(mb_type))
01746 av_log(s->avctx, AV_LOG_DEBUG, "i");
01747 else if(IS_INTRA16x16(mb_type))
01748 av_log(s->avctx, AV_LOG_DEBUG, "I");
01749 else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type))
01750 av_log(s->avctx, AV_LOG_DEBUG, "d");
01751 else if(IS_DIRECT(mb_type))
01752 av_log(s->avctx, AV_LOG_DEBUG, "D");
01753 else if(IS_GMC(mb_type) && IS_SKIP(mb_type))
01754 av_log(s->avctx, AV_LOG_DEBUG, "g");
01755 else if(IS_GMC(mb_type))
01756 av_log(s->avctx, AV_LOG_DEBUG, "G");
01757 else if(IS_SKIP(mb_type))
01758 av_log(s->avctx, AV_LOG_DEBUG, "S");
01759 else if(!USES_LIST(mb_type, 1))
01760 av_log(s->avctx, AV_LOG_DEBUG, ">");
01761 else if(!USES_LIST(mb_type, 0))
01762 av_log(s->avctx, AV_LOG_DEBUG, "<");
01763 else{
01764 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
01765 av_log(s->avctx, AV_LOG_DEBUG, "X");
01766 }
01767
01768
01769 if(IS_8X8(mb_type))
01770 av_log(s->avctx, AV_LOG_DEBUG, "+");
01771 else if(IS_16X8(mb_type))
01772 av_log(s->avctx, AV_LOG_DEBUG, "-");
01773 else if(IS_8X16(mb_type))
01774 av_log(s->avctx, AV_LOG_DEBUG, "|");
01775 else if(IS_INTRA(mb_type) || IS_16X16(mb_type))
01776 av_log(s->avctx, AV_LOG_DEBUG, " ");
01777 else
01778 av_log(s->avctx, AV_LOG_DEBUG, "?");
01779
01780
01781 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264)
01782 av_log(s->avctx, AV_LOG_DEBUG, "=");
01783 else
01784 av_log(s->avctx, AV_LOG_DEBUG, " ");
01785 }
01786
01787 }
01788 av_log(s->avctx, AV_LOG_DEBUG, "\n");
01789 }
01790 }
01791
01792 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
01793 const int shift= 1 + s->quarter_sample;
01794 int mb_y;
01795 uint8_t *ptr;
01796 int i;
01797 int h_chroma_shift, v_chroma_shift;
01798 const int width = s->avctx->width;
01799 const int height= s->avctx->height;
01800 const int mv_sample_log2= 4 - pict->motion_subsample_log2;
01801 const int mv_stride= (s->mb_width << mv_sample_log2) + 1;
01802 s->low_delay=0;
01803
01804 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
01805 for(i=0; i<3; i++){
01806 memcpy(s->visualization_buffer[i], pict->data[i], (i==0) ? pict->linesize[i]*height:pict->linesize[i]*height >> v_chroma_shift);
01807 pict->data[i]= s->visualization_buffer[i];
01808 }
01809 pict->type= FF_BUFFER_TYPE_COPY;
01810 ptr= pict->data[0];
01811
01812 for(mb_y=0; mb_y<s->mb_height; mb_y++){
01813 int mb_x;
01814 for(mb_x=0; mb_x<s->mb_width; mb_x++){
01815 const int mb_index= mb_x + mb_y*s->mb_stride;
01816 if((s->avctx->debug_mv) && pict->motion_val){
01817 int type;
01818 for(type=0; type<3; type++){
01819 int direction = 0;
01820 switch (type) {
01821 case 0: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_P_FOR)) || (pict->pict_type!=FF_P_TYPE))
01822 continue;
01823 direction = 0;
01824 break;
01825 case 1: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_FOR)) || (pict->pict_type!=FF_B_TYPE))
01826 continue;
01827 direction = 0;
01828 break;
01829 case 2: if ((!(s->avctx->debug_mv&FF_DEBUG_VIS_MV_B_BACK)) || (pict->pict_type!=FF_B_TYPE))
01830 continue;
01831 direction = 1;
01832 break;
01833 }
01834 if(!USES_LIST(pict->mb_type[mb_index], direction))
01835 continue;
01836
01837 if(IS_8X8(pict->mb_type[mb_index])){
01838 int i;
01839 for(i=0; i<4; i++){
01840 int sx= mb_x*16 + 4 + 8*(i&1);
01841 int sy= mb_y*16 + 4 + 8*(i>>1);
01842 int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
01843 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
01844 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
01845 draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
01846 }
01847 }else if(IS_16X8(pict->mb_type[mb_index])){
01848 int i;
01849 for(i=0; i<2; i++){
01850 int sx=mb_x*16 + 8;
01851 int sy=mb_y*16 + 4 + 8*i;
01852 int xy= (mb_x*2 + (mb_y*2 + i)*mv_stride) << (mv_sample_log2-1);
01853 int mx=(pict->motion_val[direction][xy][0]>>shift);
01854 int my=(pict->motion_val[direction][xy][1]>>shift);
01855
01856 if(IS_INTERLACED(pict->mb_type[mb_index]))
01857 my*=2;
01858
01859 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
01860 }
01861 }else if(IS_8X16(pict->mb_type[mb_index])){
01862 int i;
01863 for(i=0; i<2; i++){
01864 int sx=mb_x*16 + 4 + 8*i;
01865 int sy=mb_y*16 + 8;
01866 int xy= (mb_x*2 + i + mb_y*2*mv_stride) << (mv_sample_log2-1);
01867 int mx=(pict->motion_val[direction][xy][0]>>shift);
01868 int my=(pict->motion_val[direction][xy][1]>>shift);
01869
01870 if(IS_INTERLACED(pict->mb_type[mb_index]))
01871 my*=2;
01872
01873 draw_arrow(ptr, sx, sy, mx+sx, my+sy, width, height, s->linesize, 100);
01874 }
01875 }else{
01876 int sx= mb_x*16 + 8;
01877 int sy= mb_y*16 + 8;
01878 int xy= (mb_x + mb_y*mv_stride) << mv_sample_log2;
01879 int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
01880 int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
01881 draw_arrow(ptr, sx, sy, mx, my, width, height, s->linesize, 100);
01882 }
01883 }
01884 }
01885 if((s->avctx->debug&FF_DEBUG_VIS_QP) && pict->motion_val){
01886 uint64_t c= (pict->qscale_table[mb_index]*128/31) * 0x0101010101010101ULL;
01887 int y;
01888 for(y=0; y<8; y++){
01889 *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= c;
01890 *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= c;
01891 }
01892 }
01893 if((s->avctx->debug&FF_DEBUG_VIS_MB_TYPE) && pict->motion_val){
01894 int mb_type= pict->mb_type[mb_index];
01895 uint64_t u,v;
01896 int y;
01897 #define COLOR(theta, r)\
01898 u= (int)(128 + r*cos(theta*3.141592/180));\
01899 v= (int)(128 + r*sin(theta*3.141592/180));
01900
01901
01902 u=v=128;
01903 if(IS_PCM(mb_type)){
01904 COLOR(120,48)
01905 }else if((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) || IS_INTRA16x16(mb_type)){
01906 COLOR(30,48)
01907 }else if(IS_INTRA4x4(mb_type)){
01908 COLOR(90,48)
01909 }else if(IS_DIRECT(mb_type) && IS_SKIP(mb_type)){
01910
01911 }else if(IS_DIRECT(mb_type)){
01912 COLOR(150,48)
01913 }else if(IS_GMC(mb_type) && IS_SKIP(mb_type)){
01914 COLOR(170,48)
01915 }else if(IS_GMC(mb_type)){
01916 COLOR(190,48)
01917 }else if(IS_SKIP(mb_type)){
01918
01919 }else if(!USES_LIST(mb_type, 1)){
01920 COLOR(240,48)
01921 }else if(!USES_LIST(mb_type, 0)){
01922 COLOR(0,48)
01923 }else{
01924 assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
01925 COLOR(300,48)
01926 }
01927
01928 u*= 0x0101010101010101ULL;
01929 v*= 0x0101010101010101ULL;
01930 for(y=0; y<8; y++){
01931 *(uint64_t*)(pict->data[1] + 8*mb_x + (8*mb_y + y)*pict->linesize[1])= u;
01932 *(uint64_t*)(pict->data[2] + 8*mb_x + (8*mb_y + y)*pict->linesize[2])= v;
01933 }
01934
01935
01936 if(IS_8X8(mb_type) || IS_16X8(mb_type)){
01937 *(uint64_t*)(pict->data[0] + 16*mb_x + 0 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
01938 *(uint64_t*)(pict->data[0] + 16*mb_x + 8 + (16*mb_y + 8)*pict->linesize[0])^= 0x8080808080808080ULL;
01939 }
01940 if(IS_8X8(mb_type) || IS_8X16(mb_type)){
01941 for(y=0; y<16; y++)
01942 pict->data[0][16*mb_x + 8 + (16*mb_y + y)*pict->linesize[0]]^= 0x80;
01943 }
01944 if(IS_8X8(mb_type) && mv_sample_log2 >= 2){
01945 int dm= 1 << (mv_sample_log2-2);
01946 for(i=0; i<4; i++){
01947 int sx= mb_x*16 + 8*(i&1);
01948 int sy= mb_y*16 + 8*(i>>1);
01949 int xy= (mb_x*2 + (i&1) + (mb_y*2 + (i>>1))*mv_stride) << (mv_sample_log2-1);
01950
01951 int32_t *mv = (int32_t*)&pict->motion_val[0][xy];
01952 if(mv[0] != mv[dm] || mv[dm*mv_stride] != mv[dm*(mv_stride+1)])
01953 for(y=0; y<8; y++)
01954 pict->data[0][sx + 4 + (sy + y)*pict->linesize[0]]^= 0x80;
01955 if(mv[0] != mv[dm*mv_stride] || mv[dm] != mv[dm*(mv_stride+1)])
01956 *(uint64_t*)(pict->data[0] + sx + (sy + 4)*pict->linesize[0])^= 0x8080808080808080ULL;
01957 }
01958 }
01959
01960 if(IS_INTERLACED(mb_type) && s->codec_id == CODEC_ID_H264){
01961
01962 }
01963 }
01964 s->mbskip_table[mb_index]=0;
01965 }
01966 }
01967 }
01968 }
01969
01970 #ifdef CONFIG_ENCODERS
01971
01972 static int get_sae(uint8_t *src, int ref, int stride){
01973 int x,y;
01974 int acc=0;
01975
01976 for(y=0; y<16; y++){
01977 for(x=0; x<16; x++){
01978 acc+= ABS(src[x+y*stride] - ref);
01979 }
01980 }
01981
01982 return acc;
01983 }
01984
01985 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
01986 int x, y, w, h;
01987 int acc=0;
01988
01989 w= s->width &~15;
01990 h= s->height&~15;
01991
01992 for(y=0; y<h; y+=16){
01993 for(x=0; x<w; x+=16){
01994 int offset= x + y*stride;
01995 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
01996 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
01997 int sae = get_sae(src + offset, mean, stride);
01998
01999 acc+= sae + 500 < sad;
02000 }
02001 }
02002 return acc;
02003 }
02004
02005
02006 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
02007 AVFrame *pic=NULL;
02008 int64_t pts;
02009 int i;
02010 const int encoding_delay= s->max_b_frames;
02011 int direct=1;
02012
02013 if(pic_arg){
02014 pts= pic_arg->pts;
02015 pic_arg->display_picture_number= s->input_picture_number++;
02016
02017 if(pts != AV_NOPTS_VALUE){
02018 if(s->user_specified_pts != AV_NOPTS_VALUE){
02019 int64_t time= pts;
02020 int64_t last= s->user_specified_pts;
02021
02022 if(time <= last){
02023 av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%Ld, last=%Ld\n", pts, s->user_specified_pts);
02024 return -1;
02025 }
02026 }
02027 s->user_specified_pts= pts;
02028 }else{
02029 if(s->user_specified_pts != AV_NOPTS_VALUE){
02030 s->user_specified_pts=
02031 pts= s->user_specified_pts + 1;
02032 av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%Ld)\n", pts);
02033 }else{
02034 pts= pic_arg->display_picture_number;
02035 }
02036 }
02037 }
02038
02039 if(pic_arg){
02040 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
02041 if(pic_arg->linesize[0] != s->linesize) direct=0;
02042 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
02043 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
02044
02045
02046
02047 if(direct){
02048 i= ff_find_unused_picture(s, 1);
02049
02050 pic= (AVFrame*)&s->picture[i];
02051 pic->reference= 3;
02052
02053 for(i=0; i<4; i++){
02054 pic->data[i]= pic_arg->data[i];
02055 pic->linesize[i]= pic_arg->linesize[i];
02056 }
02057 alloc_picture(s, (Picture*)pic, 1);
02058 }else{
02059 int offset= 16;
02060 i= ff_find_unused_picture(s, 0);
02061
02062 pic= (AVFrame*)&s->picture[i];
02063 pic->reference= 3;
02064
02065 alloc_picture(s, (Picture*)pic, 0);
02066
02067 if( pic->data[0] + offset == pic_arg->data[0]
02068 && pic->data[1] + offset == pic_arg->data[1]
02069 && pic->data[2] + offset == pic_arg->data[2]){
02070
02071 }else{
02072 int h_chroma_shift, v_chroma_shift;
02073 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
02074
02075 for(i=0; i<3; i++){
02076 int src_stride= pic_arg->linesize[i];
02077 int dst_stride= i ? s->uvlinesize : s->linesize;
02078 int h_shift= i ? h_chroma_shift : 0;
02079 int v_shift= i ? v_chroma_shift : 0;
02080 int w= s->width >>h_shift;
02081 int h= s->height>>v_shift;
02082 uint8_t *src= pic_arg->data[i];
02083 uint8_t *dst= pic->data[i] + offset;
02084
02085 if(src_stride==dst_stride)
02086 memcpy(dst, src, src_stride*h);
02087 else{
02088 while(h--){
02089 memcpy(dst, src, w);
02090 dst += dst_stride;
02091 src += src_stride;
02092 }
02093 }
02094 }
02095 }
02096 }
02097 copy_picture_attributes(s, pic, pic_arg);
02098 pic->pts= pts;
02099 }
02100
02101
02102 for(i=1; i<MAX_PICTURE_COUNT ; i++)
02103 s->input_picture[i-1]= s->input_picture[i];
02104
02105 s->input_picture[encoding_delay]= (Picture*)pic;
02106
02107 return 0;
02108 }
02109
02110 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){
02111 int x, y, plane;
02112 int score=0;
02113 int64_t score64=0;
02114
02115 for(plane=0; plane<3; plane++){
02116 const int stride= p->linesize[plane];
02117 const int bw= plane ? 1 : 2;
02118 for(y=0; y<s->mb_height*bw; y++){
02119 for(x=0; x<s->mb_width*bw; x++){
02120 int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride), ref->data[plane] + 8*(x + y*stride), stride, 8);
02121
02122 switch(s->avctx->frame_skip_exp){
02123 case 0: score= FFMAX(score, v); break;
02124 case 1: score+= ABS(v);break;
02125 case 2: score+= v*v;break;
02126 case 3: score64+= ABS(v*v*(int64_t)v);break;
02127 case 4: score64+= v*v*(int64_t)(v*v);break;
02128 }
02129 }
02130 }
02131 }
02132
02133 if(score) score64= score;
02134
02135 if(score64 < s->avctx->frame_skip_threshold)
02136 return 1;
02137 if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
02138 return 1;
02139 return 0;
02140 }
02141
02142 static void select_input_picture(MpegEncContext *s){
02143 int i;
02144
02145 for(i=1; i<MAX_PICTURE_COUNT; i++)
02146 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
02147 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
02148
02149
02150 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
02151 if( s->next_picture_ptr==NULL || s->intra_only){
02152 s->reordered_input_picture[0]= s->input_picture[0];
02153 s->reordered_input_picture[0]->pict_type= I_TYPE;
02154 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
02155 }else{
02156 int b_frames;
02157
02158 if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
02159 if(skip_check(s, s->input_picture[0], s->next_picture_ptr)){
02160
02161
02162 if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
02163 for(i=0; i<4; i++)
02164 s->input_picture[0]->data[i]= NULL;
02165 s->input_picture[0]->type= 0;
02166 }else{
02167 assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER
02168 || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
02169
02170 s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
02171 }
02172
02173 goto no_output_pic;
02174 }
02175 }
02176
02177 if(s->flags&CODEC_FLAG_PASS2){
02178 for(i=0; i<s->max_b_frames+1; i++){
02179 int pict_num= s->input_picture[0]->display_picture_number + i;
02180
02181 if(pict_num >= s->rc_context.num_entries)
02182 break;
02183 if(!s->input_picture[i]){
02184 s->rc_context.entry[pict_num-1].new_pict_type = P_TYPE;
02185 break;
02186 }
02187
02188 s->input_picture[i]->pict_type=
02189 s->rc_context.entry[pict_num].new_pict_type;
02190 }
02191 }
02192
02193 if(s->avctx->b_frame_strategy==0){
02194 b_frames= s->max_b_frames;
02195 while(b_frames && !s->input_picture[b_frames]) b_frames--;
02196 }else if(s->avctx->b_frame_strategy==1){
02197 for(i=1; i<s->max_b_frames+1; i++){
02198 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
02199 s->input_picture[i]->b_frame_score=
02200 get_intra_count(s, s->input_picture[i ]->data[0],
02201 s->input_picture[i-1]->data[0], s->linesize) + 1;
02202 }
02203 }
02204 for(i=0; i<s->max_b_frames+1; i++){
02205 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break;
02206 }
02207
02208 b_frames= FFMAX(0, i-1);
02209
02210
02211 for(i=0; i<b_frames+1; i++){
02212 s->input_picture[i]->b_frame_score=0;
02213 }
02214 }else{
02215 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
02216 b_frames=0;
02217 }
02218
02219 emms_c();
02220
02221
02222
02223
02224 for(i= b_frames - 1; i>=0; i--){
02225 int type= s->input_picture[i]->pict_type;
02226 if(type && type != B_TYPE)
02227 b_frames= i;
02228 }
02229 if(s->input_picture[b_frames]->pict_type == B_TYPE && b_frames == s->max_b_frames){
02230 av_log(s->avctx, AV_LOG_ERROR, "warning, too many b frames in a row\n");
02231 }
02232
02233 if(s->picture_in_gop_number + b_frames >= s->gop_size){
02234 if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){
02235 b_frames= s->gop_size - s->picture_in_gop_number - 1;
02236 }else{
02237 if(s->flags & CODEC_FLAG_CLOSED_GOP)
02238 b_frames=0;
02239 s->input_picture[b_frames]->pict_type= I_TYPE;
02240 }
02241 }
02242
02243 if( (s->flags & CODEC_FLAG_CLOSED_GOP)
02244 && b_frames
02245 && s->input_picture[b_frames]->pict_type== I_TYPE)
02246 b_frames--;
02247
02248 s->reordered_input_picture[0]= s->input_picture[b_frames];
02249 if(s->reordered_input_picture[0]->pict_type != I_TYPE)
02250 s->reordered_input_picture[0]->pict_type= P_TYPE;
02251 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
02252 for(i=0; i<b_frames; i++){
02253 s->reordered_input_picture[i+1]= s->input_picture[i];
02254 s->reordered_input_picture[i+1]->pict_type= B_TYPE;
02255 s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
02256 }
02257 }
02258 }
02259 no_output_pic:
02260 if(s->reordered_input_picture[0]){
02261 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
02262
02263 copy_picture(&s->new_picture, s->reordered_input_picture[0]);
02264
02265 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
02266
02267
02268 int i= ff_find_unused_picture(s, 0);
02269 Picture *pic= &s->picture[i];
02270
02271
02272 for(i=0; i<4; i++)
02273 s->reordered_input_picture[0]->data[i]= NULL;
02274 s->reordered_input_picture[0]->type= 0;
02275
02276 pic->reference = s->reordered_input_picture[0]->reference;
02277
02278 alloc_picture(s, pic, 0);
02279
02280 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
02281
02282 s->current_picture_ptr= pic;
02283 }else{
02284
02285
02286 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
02287 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
02288
02289 s->current_picture_ptr= s->reordered_input_picture[0];
02290 for(i=0; i<4; i++){
02291 s->new_picture.data[i]+=16;
02292 }
02293 }
02294 copy_picture(&s->current_picture, s->current_picture_ptr);
02295
02296 s->picture_number= s->new_picture.display_picture_number;
02297
02298 }else{
02299 memset(&s->new_picture, 0, sizeof(Picture));
02300 }
02301 }
02302
02303 int MPV_encode_picture(AVCodecContext *avctx,
02304 unsigned char *buf, int buf_size, void *data)
02305 {
02306 MpegEncContext *s = avctx->priv_data;
02307 AVFrame *pic_arg = data;
02308 int i, stuffing_count;
02309
02310 if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUVJ420P){
02311 av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
02312 return -1;
02313 }
02314
02315 for(i=0; i<avctx->thread_count; i++){
02316 int start_y= s->thread_context[i]->start_mb_y;
02317 int end_y= s->thread_context[i]-> end_mb_y;
02318 int h= s->mb_height;
02319 uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h);
02320 uint8_t *end = buf + (size_t)(((int64_t) buf_size)* end_y/h);
02321
02322 init_put_bits(&s->thread_context[i]->pb, start, end - start);
02323 }
02324
02325 s->picture_in_gop_number++;
02326
02327 if(load_input_picture(s, pic_arg) < 0)
02328 return -1;
02329
02330 select_input_picture(s);
02331
02332
02333 if(s->new_picture.data[0]){
02334 s->pict_type= s->new_picture.pict_type;
02335
02336
02337 MPV_frame_start(s, avctx);
02338
02339 encode_picture(s, s->picture_number);
02340
02341 avctx->real_pict_num = s->picture_number;
02342 avctx->header_bits = s->header_bits;
02343 avctx->mv_bits = s->mv_bits;
02344 avctx->misc_bits = s->misc_bits;
02345 avctx->i_tex_bits = s->i_tex_bits;
02346 avctx->p_tex_bits = s->p_tex_bits;
02347 avctx->i_count = s->i_count;
02348 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
02349 avctx->skip_count = s->skip_count;
02350
02351 MPV_frame_end(s);
02352
02353 if (s->out_format == FMT_MJPEG)
02354 mjpeg_picture_trailer(s);
02355
02356 if(s->flags&CODEC_FLAG_PASS1)
02357 ff_write_pass1_stats(s);
02358
02359 for(i=0; i<4; i++){
02360 avctx->error[i] += s->current_picture_ptr->error[i];
02361 }
02362
02363 if(s->flags&CODEC_FLAG_PASS1)
02364 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb));
02365 flush_put_bits(&s->pb);
02366 s->frame_bits = put_bits_count(&s->pb);
02367
02368 stuffing_count= ff_vbv_update(s, s->frame_bits);
02369 if(stuffing_count){
02370 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){
02371 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
02372 return -1;
02373 }
02374
02375 switch(s->codec_id){
02376 case CODEC_ID_MPEG1VIDEO:
02377 case CODEC_ID_MPEG2VIDEO:
02378 while(stuffing_count--){
02379 put_bits(&s->pb, 8, 0);
02380 }
02381 break;
02382 case CODEC_ID_MPEG4:
02383 put_bits(&s->pb, 16, 0);
02384 put_bits(&s->pb, 16, 0x1C3);
02385 stuffing_count -= 4;
02386 while(stuffing_count--){
02387 put_bits(&s->pb, 8, 0xFF);
02388 }
02389 break;
02390 default:
02391 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
02392 }
02393 flush_put_bits(&s->pb);
02394 s->frame_bits = put_bits_count(&s->pb);
02395 }
02396
02397
02398 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
02399 && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
02400 int vbv_delay;
02401
02402 assert(s->repeat_first_field==0);
02403
02404 vbv_delay= lrintf(90000 * s->rc_context.buffer_index / s->avctx->rc_max_rate);
02405 assert(vbv_delay < 0xFFFF);
02406
02407 s->vbv_delay_ptr[0] &= 0xF8;
02408 s->vbv_delay_ptr[0] |= vbv_delay>>13;
02409 s->vbv_delay_ptr[1] = vbv_delay>>5;
02410 s->vbv_delay_ptr[2] &= 0x07;
02411 s->vbv_delay_ptr[2] |= vbv_delay<<3;
02412 }
02413 s->total_bits += s->frame_bits;
02414 avctx->frame_bits = s->frame_bits;
02415 }else{
02416 assert((pbBufPtr(&s->pb) == s->pb.buf));
02417 s->frame_bits=0;
02418 }
02419 assert((s->frame_bits&7)==0);
02420
02421 return s->frame_bits/8;
02422 }
02423
02424 #endif //CONFIG_ENCODERS
02425
02426 static inline void gmc1_motion(MpegEncContext *s,
02427 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02428 uint8_t **ref_picture)
02429 {
02430 uint8_t *ptr;
02431 int offset, src_x, src_y, linesize, uvlinesize;
02432 int motion_x, motion_y;
02433 int emu=0;
02434
02435 motion_x= s->sprite_offset[0][0];
02436 motion_y= s->sprite_offset[0][1];
02437 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1));
02438 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1));
02439 motion_x<<=(3-s->sprite_warping_accuracy);
02440 motion_y<<=(3-s->sprite_warping_accuracy);
02441 src_x = clip(src_x, -16, s->width);
02442 if (src_x == s->width)
02443 motion_x =0;
02444 src_y = clip(src_y, -16, s->height);
02445 if (src_y == s->height)
02446 motion_y =0;
02447
02448 linesize = s->linesize;
02449 uvlinesize = s->uvlinesize;
02450
02451 ptr = ref_picture[0] + (src_y * linesize) + src_x;
02452
02453 if(s->flags&CODEC_FLAG_EMU_EDGE){
02454 if( (unsigned)src_x >= s->h_edge_pos - 17
02455 || (unsigned)src_y >= s->v_edge_pos - 17){
02456 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
02457 ptr= s->edge_emu_buffer;
02458 }
02459 }
02460
02461 if((motion_x|motion_y)&7){
02462 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
02463 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
02464 }else{
02465 int dxy;
02466
02467 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
02468 if (s->no_rounding){
02469 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
02470 }else{
02471 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16);
02472 }
02473 }
02474
02475 if(s->flags&CODEC_FLAG_GRAY) return;
02476
02477 motion_x= s->sprite_offset[1][0];
02478 motion_y= s->sprite_offset[1][1];
02479 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1));
02480 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1));
02481 motion_x<<=(3-s->sprite_warping_accuracy);
02482 motion_y<<=(3-s->sprite_warping_accuracy);
02483 src_x = clip(src_x, -8, s->width>>1);
02484 if (src_x == s->width>>1)
02485 motion_x =0;
02486 src_y = clip(src_y, -8, s->height>>1);
02487 if (src_y == s->height>>1)
02488 motion_y =0;
02489
02490 offset = (src_y * uvlinesize) + src_x;
02491 ptr = ref_picture[1] + offset;
02492 if(s->flags&CODEC_FLAG_EMU_EDGE){
02493 if( (unsigned)src_x >= (s->h_edge_pos>>1) - 9
02494 || (unsigned)src_y >= (s->v_edge_pos>>1) - 9){
02495 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
02496 ptr= s->edge_emu_buffer;
02497 emu=1;
02498 }
02499 }
02500 s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
02501
02502 ptr = ref_picture[2] + offset;
02503 if(emu){
02504 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
02505 ptr= s->edge_emu_buffer;
02506 }
02507 s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
02508
02509 return;
02510 }
02511
02512 static inline void gmc_motion(MpegEncContext *s,
02513 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02514 uint8_t **ref_picture)
02515 {
02516 uint8_t *ptr;
02517 int linesize, uvlinesize;
02518 const int a= s->sprite_warping_accuracy;
02519 int ox, oy;
02520
02521 linesize = s->linesize;
02522 uvlinesize = s->uvlinesize;
02523
02524 ptr = ref_picture[0];
02525
02526 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16;
02527 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16;
02528
02529 s->dsp.gmc(dest_y, ptr, linesize, 16,
02530 ox,
02531 oy,
02532 s->sprite_delta[0][0], s->sprite_delta[0][1],
02533 s->sprite_delta[1][0], s->sprite_delta[1][1],
02534 a+1, (1<<(2*a+1)) - s->no_rounding,
02535 s->h_edge_pos, s->v_edge_pos);
02536 s->dsp.gmc(dest_y+8, ptr, linesize, 16,
02537 ox + s->sprite_delta[0][0]*8,
02538 oy + s->sprite_delta[1][0]*8,
02539 s->sprite_delta[0][0], s->sprite_delta[0][1],
02540 s->sprite_delta[1][0], s->sprite_delta[1][1],
02541 a+1, (1<<(2*a+1)) - s->no_rounding,
02542 s->h_edge_pos, s->v_edge_pos);
02543
02544 if(s->flags&CODEC_FLAG_GRAY) return;
02545
02546 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8;
02547 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8;
02548
02549 ptr = ref_picture[1];
02550 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
02551 ox,
02552 oy,
02553 s->sprite_delta[0][0], s->sprite_delta[0][1],
02554 s->sprite_delta[1][0], s->sprite_delta[1][1],
02555 a+1, (1<<(2*a+1)) - s->no_rounding,
02556 s->h_edge_pos>>1, s->v_edge_pos>>1);
02557
02558 ptr = ref_picture[2];
02559 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
02560 ox,
02561 oy,
02562 s->sprite_delta[0][0], s->sprite_delta[0][1],
02563 s->sprite_delta[1][0], s->sprite_delta[1][1],
02564 a+1, (1<<(2*a+1)) - s->no_rounding,
02565 s->h_edge_pos>>1, s->v_edge_pos>>1);
02566 }
02567
02580 void ff_emulated_edge_mc(uint8_t *buf, uint8_t *src, int linesize, int block_w, int block_h,
02581 int src_x, int src_y, int w, int h){
02582 int x, y;
02583 int start_y, start_x, end_y, end_x;
02584
02585 if(src_y>= h){
02586 src+= (h-1-src_y)*linesize;
02587 src_y=h-1;
02588 }else if(src_y<=-block_h){
02589 src+= (1-block_h-src_y)*linesize;
02590 src_y=1-block_h;
02591 }
02592 if(src_x>= w){
02593 src+= (w-1-src_x);
02594 src_x=w-1;
02595 }else if(src_x<=-block_w){
02596 src+= (1-block_w-src_x);
02597 src_x=1-block_w;
02598 }
02599
02600 start_y= FFMAX(0, -src_y);
02601 start_x= FFMAX(0, -src_x);
02602 end_y= FFMIN(block_h, h-src_y);
02603 end_x= FFMIN(block_w, w-src_x);
02604
02605
02606 for(y=start_y; y<end_y; y++){
02607 for(x=start_x; x<end_x; x++){
02608 buf[x + y*linesize]= src[x + y*linesize];
02609 }
02610 }
02611
02612
02613 for(y=0; y<start_y; y++){
02614 for(x=start_x; x<end_x; x++){
02615 buf[x + y*linesize]= buf[x + start_y*linesize];
02616 }
02617 }
02618
02619
02620 for(y=end_y; y<block_h; y++){
02621 for(x=start_x; x<end_x; x++){
02622 buf[x + y*linesize]= buf[x + (end_y-1)*linesize];
02623 }
02624 }
02625
02626 for(y=0; y<block_h; y++){
02627
02628 for(x=0; x<start_x; x++){
02629 buf[x + y*linesize]= buf[start_x + y*linesize];
02630 }
02631
02632
02633 for(x=end_x; x<block_w; x++){
02634 buf[x + y*linesize]= buf[end_x - 1 + y*linesize];
02635 }
02636 }
02637 }
02638
02639 static inline int hpel_motion(MpegEncContext *s,
02640 uint8_t *dest, uint8_t *src,
02641 int field_based, int field_select,
02642 int src_x, int src_y,
02643 int width, int height, int stride,
02644 int h_edge_pos, int v_edge_pos,
02645 int w, int h, op_pixels_func *pix_op,
02646 int motion_x, int motion_y)
02647 {
02648 int dxy;
02649 int emu=0;
02650
02651 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
02652 src_x += motion_x >> 1;
02653 src_y += motion_y >> 1;
02654
02655
02656 src_x = clip(src_x, -16, width);
02657 if (src_x == width)
02658 dxy &= ~1;
02659 src_y = clip(src_y, -16, height);
02660 if (src_y == height)
02661 dxy &= ~2;
02662 src += src_y * stride + src_x;
02663
02664 if(s->unrestricted_mv && (s->flags&CODEC_FLAG_EMU_EDGE)){
02665 if( (unsigned)src_x > h_edge_pos - (motion_x&1) - w
02666 || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
02667 ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
02668 src_x, src_y<<field_based, h_edge_pos, s->v_edge_pos);
02669 src= s->edge_emu_buffer;
02670 emu=1;
02671 }
02672 }
02673 if(field_select)
02674 src += s->linesize;
02675 pix_op[dxy](dest, src, stride, h);
02676 return emu;
02677 }
02678
02679 static inline int hpel_motion_lowres(MpegEncContext *s,
02680 uint8_t *dest, uint8_t *src,
02681 int field_based, int field_select,
02682 int src_x, int src_y,
02683 int width, int height, int stride,
02684 int h_edge_pos, int v_edge_pos,
02685 int w, int h, h264_chroma_mc_func *pix_op,
02686 int motion_x, int motion_y)
02687 {
02688 const int lowres= s->avctx->lowres;
02689 const int s_mask= (2<<lowres)-1;
02690 int emu=0;
02691 int sx, sy;
02692
02693 if(s->quarter_sample){
02694 motion_x/=2;
02695 motion_y/=2;
02696 }
02697
02698 sx= motion_x & s_mask;
02699 sy= motion_y & s_mask;
02700 src_x += motion_x >> (lowres+1);
02701 src_y += motion_y >> (lowres+1);
02702
02703 src += src_y * stride + src_x;
02704
02705 if( (unsigned)src_x > h_edge_pos - (!!sx) - w
02706 || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
02707 ff_emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w+1, (h+1)<<field_based,
02708 src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
02709 src= s->edge_emu_buffer;
02710 emu=1;
02711 }
02712
02713 sx <<= 2 - lowres;
02714 sy <<= 2 - lowres;
02715 if(field_select)
02716 src += s->linesize;
02717 pix_op[lowres](dest, src, stride, h, sx, sy);
02718 return emu;
02719 }
02720
02721
02722 static always_inline void mpeg_motion(MpegEncContext *s,
02723 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02724 int field_based, int bottom_field, int field_select,
02725 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
02726 int motion_x, int motion_y, int h)
02727 {
02728 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
02729 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, uvlinesize, linesize;
02730
02731 #if 0
02732 if(s->quarter_sample)
02733 {
02734 motion_x>>=1;
02735 motion_y>>=1;
02736 }
02737 #endif
02738
02739 v_edge_pos = s->v_edge_pos >> field_based;
02740 linesize = s->current_picture.linesize[0] << field_based;
02741 uvlinesize = s->current_picture.linesize[1] << field_based;
02742
02743 dxy = ((motion_y & 1) << 1) | (motion_x & 1);
02744 src_x = s->mb_x* 16 + (motion_x >> 1);
02745 src_y =(s->mb_y<<(4-field_based)) + (motion_y >> 1);
02746
02747 if (s->out_format == FMT_H263) {
02748 if((s->workaround_bugs & FF_BUG_HPEL_CHROMA) && field_based){
02749 mx = (motion_x>>1)|(motion_x&1);
02750 my = motion_y >>1;
02751 uvdxy = ((my & 1) << 1) | (mx & 1);
02752 uvsrc_x = s->mb_x* 8 + (mx >> 1);
02753 uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
02754 }else{
02755 uvdxy = dxy | (motion_y & 2) | ((motion_x & 2) >> 1);
02756 uvsrc_x = src_x>>1;
02757 uvsrc_y = src_y>>1;
02758 }
02759 }else if(s->out_format == FMT_H261){
02760 mx = motion_x / 4;
02761 my = motion_y / 4;
02762 uvdxy = 0;
02763 uvsrc_x = s->mb_x*8 + mx;
02764 uvsrc_y = s->mb_y*8 + my;
02765 } else {
02766 if(s->chroma_y_shift){
02767 mx = motion_x / 2;
02768 my = motion_y / 2;
02769 uvdxy = ((my & 1) << 1) | (mx & 1);
02770 uvsrc_x = s->mb_x* 8 + (mx >> 1);
02771 uvsrc_y = (s->mb_y<<(3-field_based)) + (my >> 1);
02772 } else {
02773 if(s->chroma_x_shift){
02774
02775 mx = motion_x / 2;
02776 uvdxy = ((motion_y & 1) << 1) | (mx & 1);
02777 uvsrc_x = s->mb_x* 8 + (mx >> 1);
02778 uvsrc_y = src_y;
02779 } else {
02780
02781 uvdxy = dxy;
02782 uvsrc_x = src_x;
02783 uvsrc_y = src_y;
02784 }
02785 }
02786 }
02787
02788 ptr_y = ref_picture[0] + src_y * linesize + src_x;
02789 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
02790 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
02791
02792 if( (unsigned)src_x > s->h_edge_pos - (motion_x&1) - 16
02793 || (unsigned)src_y > v_edge_pos - (motion_y&1) - h){
02794 if(s->codec_id == CODEC_ID_MPEG2VIDEO ||
02795 s->codec_id == CODEC_ID_MPEG1VIDEO){
02796 av_log(s->avctx,AV_LOG_DEBUG,"MPEG motion vector out of boundary\n");
02797 return ;
02798 }
02799 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
02800 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
02801 ptr_y = s->edge_emu_buffer;
02802 if(!(s->flags&CODEC_FLAG_GRAY)){
02803 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
02804 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
02805 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
02806 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
02807 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
02808 ptr_cb= uvbuf;
02809 ptr_cr= uvbuf+16;
02810 }
02811 }
02812
02813 if(bottom_field){
02814 dest_y += s->linesize;
02815 dest_cb+= s->uvlinesize;
02816 dest_cr+= s->uvlinesize;
02817 }
02818
02819 if(field_select){
02820 ptr_y += s->linesize;
02821 ptr_cb+= s->uvlinesize;
02822 ptr_cr+= s->uvlinesize;
02823 }
02824
02825 pix_op[0][dxy](dest_y, ptr_y, linesize, h);
02826
02827 if(!(s->flags&CODEC_FLAG_GRAY)){
02828 pix_op[s->chroma_x_shift][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
02829 pix_op[s->chroma_x_shift][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift);
02830 }
02831 #if defined(CONFIG_H261_ENCODER) || defined(CONFIG_H261_DECODER)
02832 if(s->out_format == FMT_H261){
02833 ff_h261_loop_filter(s);
02834 }
02835 #endif
02836 }
02837
02838
02839 static always_inline void mpeg_motion_lowres(MpegEncContext *s,
02840 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
02841 int field_based, int bottom_field, int field_select,
02842 uint8_t **ref_picture, h264_chroma_mc_func *pix_op,
02843 int motion_x, int motion_y, int h)
02844 {
02845 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
02846 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy, uvsx, uvsy;
02847 const int lowres= s->avctx->lowres;
02848 const int block_s= 8>>lowres;
02849 const int s_mask= (2<<lowres)-1;
02850 const int h_edge_pos = s->h_edge_pos >> lowres;
02851 const int v_edge_pos = s->v_edge_pos >> lowres;
02852 linesize = s->current_picture.linesize[0] << field_based;
02853 uvlinesize = s->current_picture.linesize[1] << field_based;
02854
02855 if(s->quarter_sample){
02856 motion_x/=2;
02857 motion_y/=2;
02858 }
02859
02860 if(field_based){
02861 motion_y += (bottom_field - field_select)*((1<<lowres)-1);
02862 }
02863
02864 sx= motion_x & s_mask;
02865 sy= motion_y & s_mask;
02866 src_x = s->mb_x*2*block_s + (motion_x >> (lowres+1));
02867 src_y =(s->mb_y*2*block_s>>field_based) + (motion_y >> (lowres+1));
02868
02869 if (s->out_format == FMT_H263) {
02870 uvsx = ((motion_x>>1) & s_mask) | (sx&1);
02871 uvsy = ((motion_y>>1) & s_mask) | (sy&1);
02872 uvsrc_x = src_x>>1;
02873 uvsrc_y = src_y>>1;
02874 }else if(s->out_format == FMT_H261){
02875 mx = motion_x / 4;
02876 my = motion_y / 4;
02877 uvsx = (2*mx) & s_mask;
02878 uvsy = (2*my) & s_mask;
02879 uvsrc_x = s->mb_x*block_s + (mx >> lowres);
02880 uvsrc_y = s->mb_y*block_s + (my >> lowres);
02881 } else {
02882 mx = motion_x / 2;
02883 my = motion_y / 2;
02884 uvsx = mx & s_mask;
02885 uvsy = my & s_mask;
02886 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
02887 uvsrc_y =(s->mb_y*block_s>>field_based) + (my >> (lowres+1));
02888 }
02889
02890 ptr_y = ref_picture[0] + src_y * linesize + src_x;
02891 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
02892 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
02893
02894 if( (unsigned)src_x > h_edge_pos - (!!sx) - 2*block_s
02895 || (unsigned)src_y >(v_edge_pos >> field_based) - (!!sy) - h){
02896 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
02897 src_x, src_y<<field_based, h_edge_pos, v_edge_pos);
02898 ptr_y = s->edge_emu_buffer;
02899 if(!(s->flags&CODEC_FLAG_GRAY)){
02900 uint8_t *uvbuf= s->edge_emu_buffer+18*s->linesize;
02901 ff_emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9, 9+field_based,
02902 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
02903 ff_emulated_edge_mc(uvbuf+16, ptr_cr, s->uvlinesize, 9, 9+field_based,
02904 uvsrc_x, uvsrc_y<<field_based, h_edge_pos>>1, v_edge_pos>>1);
02905 ptr_cb= uvbuf;
02906 ptr_cr= uvbuf+16;
02907 }
02908 }
02909
02910 if(bottom_field){
02911 dest_y += s->linesize;
02912 dest_cb+= s->uvlinesize;
02913 dest_cr+= s->uvlinesize;
02914 }
02915
02916 if(field_select){
02917 ptr_y += s->linesize;
02918 ptr_cb+= s->uvlinesize;
02919 ptr_cr+= s->uvlinesize;
02920 }
02921
02922 sx <<= 2 - lowres;
02923 sy <<= 2 - lowres;
02924 pix_op[lowres-1](dest_y, ptr_y, linesize, h, sx, sy);
02925
02926 if(!(s->flags&CODEC_FLAG_GRAY)){
02927 uvsx <<= 2 - lowres;
02928 uvsy <<= 2 - lowres;
02929 pix_op[lowres](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
02930 pix_op[lowres](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift, uvsx, uvsy);
02931 }
02932
02933 }
02934
02935
02936 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride){
02937 int x;
02938 uint8_t * const top = src[1];
02939 uint8_t * const left = src[2];
02940 uint8_t * const mid = src[0];
02941 uint8_t * const right = src[3];
02942 uint8_t * const bottom= src[4];
02943 #define OBMC_FILTER(x, t, l, m, r, b)\
02944 dst[x]= (t*top[x] + l*left[x] + m*mid[x] + r*right[x] + b*bottom[x] + 4)>>3
02945 #define OBMC_FILTER4(x, t, l, m, r, b)\
02946 OBMC_FILTER(x , t, l, m, r, b);\
02947 OBMC_FILTER(x+1 , t, l, m, r, b);\
02948 OBMC_FILTER(x +stride, t, l, m, r, b);\
02949 OBMC_FILTER(x+1+stride, t, l, m, r, b);
02950
02951 x=0;
02952 OBMC_FILTER (x , 2, 2, 4, 0, 0);
02953 OBMC_FILTER (x+1, 2, 1, 5, 0, 0);
02954 OBMC_FILTER4(x+2, 2, 1, 5, 0, 0);
02955 OBMC_FILTER4(x+4, 2, 0, 5, 1, 0);
02956 OBMC_FILTER (x+6, 2, 0, 5, 1, 0);
02957 OBMC_FILTER (x+7, 2, 0, 4, 2, 0);
02958 x+= stride;
02959 OBMC_FILTER (x , 1, 2, 5, 0, 0);
02960 OBMC_FILTER (x+1, 1, 2, 5, 0, 0);
02961 OBMC_FILTER (x+6, 1, 0, 5, 2, 0);
02962 OBMC_FILTER (x+7, 1, 0, 5, 2, 0);
02963 x+= stride;
02964 OBMC_FILTER4(x , 1, 2, 5, 0, 0);
02965 OBMC_FILTER4(x+2, 1, 1, 6, 0, 0);
02966 OBMC_FILTER4(x+4, 1, 0, 6, 1, 0);
02967 OBMC_FILTER4(x+6, 1, 0, 5, 2, 0);
02968 x+= 2*stride;
02969 OBMC_FILTER4(x , 0, 2, 5, 0, 1);
02970 OBMC_FILTER4(x+2, 0, 1, 6, 0, 1);
02971 OBMC_FILTER4(x+4, 0, 0, 6, 1, 1);
02972 OBMC_FILTER4(x+6, 0, 0, 5, 2, 1);
02973 x+= 2*stride;
02974 OBMC_FILTER (x , 0, 2, 5, 0, 1);
02975 OBMC_FILTER (x+1, 0, 2, 5, 0, 1);
02976 OBMC_FILTER4(x+2, 0, 1, 5, 0, 2);
02977 OBMC_FILTER4(x+4, 0, 0, 5, 1, 2);
02978 OBMC_FILTER (x+6, 0, 0, 5, 2, 1);
02979 OBMC_FILTER (x+7, 0, 0, 5, 2, 1);
02980 x+= stride;
02981 OBMC_FILTER (x , 0, 2, 4, 0, 2);
02982 OBMC_FILTER (x+1, 0, 1, 5, 0, 2);
02983 OBMC_FILTER (x+6, 0, 0, 5, 1, 2);
02984 OBMC_FILTER (x+7, 0, 0, 4, 2, 2);
02985 }
02986
02987
02988 static inline void obmc_motion(MpegEncContext *s,
02989 uint8_t *dest, uint8_t *src,
02990 int src_x, int src_y,
02991 op_pixels_func *pix_op,
02992 int16_t mv[5][2])
02993 #define MID 0
02994 {
02995 int i;
02996 uint8_t *ptr[5];
02997
02998 assert(s->quarter_sample==0);
02999
03000 for(i=0; i<5; i++){
03001 if(i && mv[i][0]==mv[MID][0] && mv[i][1]==mv[MID][1]){
03002 ptr[i]= ptr[MID];
03003 }else{
03004 ptr[i]= s->obmc_scratchpad + 8*(i&1) + s->linesize*8*(i>>1);
03005 hpel_motion(s, ptr[i], src, 0, 0,
03006 src_x, src_y,
03007 s->width, s->height, s->linesize,
03008 s->h_edge_pos, s->v_edge_pos,
03009 8, 8, pix_op,
03010 mv[i][0], mv[i][1]);
03011 }
03012 }
03013
03014 put_obmc(dest, ptr, s->linesize);
03015 }
03016
03017 static inline void qpel_motion(MpegEncContext *s,
03018 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
03019 int field_based, int bottom_field, int field_select,
03020 uint8_t **ref_picture, op_pixels_func (*pix_op)[4],
03021 qpel_mc_func (*qpix_op)[16],
03022 int motion_x, int motion_y, int h)
03023 {
03024 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
03025 int dxy, uvdxy, mx, my, src_x, src_y, uvsrc_x, uvsrc_y, v_edge_pos, linesize, uvlinesize;
03026
03027 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
03028 src_x = s->mb_x * 16 + (motion_x >> 2);
03029 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2);
03030
03031 v_edge_pos = s->v_edge_pos >> field_based;
03032 linesize = s->linesize << field_based;
03033 uvlinesize = s->uvlinesize << field_based;
03034
03035 if(field_based){
03036 mx= motion_x/2;
03037 my= motion_y>>1;
03038 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA2){
03039 static const int rtab[8]= {0,0,1,1,0,0,0,1};
03040 mx= (motion_x>>1) + rtab[motion_x&7];
03041 my= (motion_y>>1) + rtab[motion_y&7];
03042 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){
03043 mx= (motion_x>>1)|(motion_x&1);
03044 my= (motion_y>>1)|(motion_y&1);
03045 }else{
03046 mx= motion_x/2;
03047 my= motion_y/2;
03048 }
03049 mx= (mx>>1)|(mx&1);
03050 my= (my>>1)|(my&1);
03051
03052 uvdxy= (mx&1) | ((my&1)<<1);
03053 mx>>=1;
03054 my>>=1;
03055
03056 uvsrc_x = s->mb_x * 8 + mx;
03057 uvsrc_y = s->mb_y * (8 >> field_based) + my;
03058
03059 ptr_y = ref_picture[0] + src_y * linesize + src_x;
03060 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
03061 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
03062
03063 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
03064 || (unsigned)src_y > v_edge_pos - (motion_y&3) - h ){
03065 ff_emulated_edge_mc(s->edge_emu_buffer, ptr_y, s->linesize, 17, 17+field_based,
03066 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos);
03067 ptr_y= s->edge_emu_buffer;
03068 if(!(s->flags&CODEC_FLAG_GRAY)){
03069 uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
03070 ff_emulated_edge_mc(uvbuf, ptr_cb, s->uvlinesize, 9, 9 + field_based,
03071 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
03072 ff_emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9, 9 + field_based,
03073 uvsrc_x, uvsrc_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1);
03074 ptr_cb= uvbuf;
03075 ptr_cr= uvbuf + 16;
03076 }
03077 }
03078
03079 if(!field_based)
03080 qpix_op[0][dxy](dest_y, ptr_y, linesize);
03081 else{
03082 if(bottom_field){
03083 dest_y += s->linesize;
03084 dest_cb+= s->uvlinesize;
03085 dest_cr+= s->uvlinesize;
03086 }
03087
03088 if(field_select){
03089 ptr_y += s->linesize;
03090 ptr_cb += s->uvlinesize;
03091 ptr_cr += s->uvlinesize;
03092 }
03093
03094
03095 qpix_op[1][dxy](dest_y , ptr_y , linesize);
03096 qpix_op[1][dxy](dest_y+8, ptr_y+8, linesize);
03097 }
03098 if(!(s->flags&CODEC_FLAG_GRAY)){
03099 pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
03100 pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
03101 }
03102 }
03103
03104 inline int ff_h263_round_chroma(int x){
03105 if (x >= 0)
03106 return (h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
03107 else {
03108 x = -x;
03109 return -(h263_chroma_roundtab[x & 0xf] + ((x >> 3) & ~1));
03110 }
03111 }
03112
03116 static inline void chroma_4mv_motion(MpegEncContext *s,
03117 uint8_t *dest_cb, uint8_t *dest_cr,
03118 uint8_t **ref_picture,
03119 op_pixels_func *pix_op,
03120 int mx, int my){
03121 int dxy, emu=0, src_x, src_y, offset;
03122 uint8_t *ptr;
03123
03124
03125
03126 mx= ff_h263_round_chroma(mx);
03127 my= ff_h263_round_chroma(my);
03128
03129 dxy = ((my & 1) << 1) | (mx & 1);
03130 mx >>= 1;
03131 my >>= 1;
03132
03133 src_x = s->mb_x * 8 + mx;
03134 src_y = s->mb_y * 8 + my;
03135 src_x = clip(src_x, -8, s->width/2);
03136 if (src_x == s->width/2)
03137 dxy &= ~1;
03138 src_y = clip(src_y, -8, s->height/2);
03139 if (src_y == s->height/2)
03140 dxy &= ~2;
03141
03142 offset = (src_y * (s->uvlinesize)) + src_x;
03143 ptr = ref_picture[1] + offset;
03144 if(s->flags&CODEC_FLAG_EMU_EDGE){
03145 if( (unsigned)src_x > (s->h_edge_pos>>1) - (dxy &1) - 8
03146 || (unsigned)src_y > (s->v_edge_pos>>1) - (dxy>>1) - 8){
03147 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
03148 ptr= s->edge_emu_buffer;
03149 emu=1;
03150 }
03151 }
03152 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
03153
03154 ptr = ref_picture[2] + offset;
03155 if(emu){
03156 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
03157 ptr= s->edge_emu_buffer;
03158 }
03159 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
03160 }
03161
03162 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
03163 uint8_t *dest_cb, uint8_t *dest_cr,
03164 uint8_t **ref_picture,
03165 h264_chroma_mc_func *pix_op,
03166 int mx, int my){
03167 const int lowres= s->avctx->lowres;
03168 const int block_s= 8>>lowres;
03169 const int s_mask= (2<<lowres)-1;
03170 const int h_edge_pos = s->h_edge_pos >> (lowres+1);
03171 const int v_edge_pos = s->v_edge_pos >> (lowres+1);
03172 int emu=0, src_x, src_y, offset, sx, sy;
03173 uint8_t *ptr;
03174
03175 if(s->quarter_sample){
03176 mx/=2;
03177 my/=2;
03178 }
03179
03180
03181
03182 mx= ff_h263_round_chroma(mx);
03183 my= ff_h263_round_chroma(my);
03184
03185 sx= mx & s_mask;
03186 sy= my & s_mask;
03187 src_x = s->mb_x*block_s + (mx >> (lowres+1));
03188 src_y = s->mb_y*block_s + (my >> (lowres+1));
03189
03190 offset = src_y * s->uvlinesize + src_x;
03191 ptr = ref_picture[1] + offset;
03192 if(s->flags&CODEC_FLAG_EMU_EDGE){
03193 if( (unsigned)src_x > h_edge_pos - (!!sx) - block_s
03194 || (unsigned)src_y > v_edge_pos - (!!sy) - block_s){
03195 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
03196 ptr= s->edge_emu_buffer;
03197 emu=1;
03198 }
03199 }
03200 sx <<= 2 - lowres;
03201 sy <<= 2 - lowres;
03202 pix_op[lowres](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
03203
03204 ptr = ref_picture[2] + offset;
03205 if(emu){
03206 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
03207 ptr= s->edge_emu_buffer;
03208 }
03209 pix_op[lowres](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
03210 }
03211
03224 static inline void MPV_motion(MpegEncContext *s,
03225 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
03226 int dir, uint8_t **ref_picture,
03227 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16])
03228 {
03229 int dxy, mx, my, src_x, src_y, motion_x, motion_y;
03230 int mb_x, mb_y, i;
03231 uint8_t *ptr, *dest;
03232
03233 mb_x = s->mb_x;
03234 mb_y = s->mb_y;
03235
03236 if(s->obmc && s->pict_type != B_TYPE){
03237 int16_t mv_cache[4][4][2];
03238 const int xy= s->mb_x + s->mb_y*s->mb_stride;
03239 const int mot_stride= s->b8_stride;
03240 const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
03241
03242 assert(!s->mb_skipped);
03243
03244 memcpy(mv_cache[1][1], s->current_picture.motion_val[0][mot_xy ], sizeof(int16_t)*4);
03245 memcpy(mv_cache[2][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
03246 memcpy(mv_cache[3][1], s->current_picture.motion_val[0][mot_xy+mot_stride], sizeof(int16_t)*4);
03247
03248 if(mb_y==0 || IS_INTRA(s->current_picture.mb_type[xy-s->mb_stride])){
03249 memcpy(mv_cache[0][1], mv_cache[1][1], sizeof(int16_t)*4);
03250 }else{
03251 memcpy(mv_cache[0][1], s->current_picture.motion_val[0][mot_xy-mot_stride], sizeof(int16_t)*4);
03252 }
03253
03254 if(mb_x==0 || IS_INTRA(s->current_picture.mb_type[xy-1])){
03255 *(int32_t*)mv_cache[1][0]= *(int32_t*)mv_cache[1][1];
03256 *(int32_t*)mv_cache[2][0]= *(int32_t*)mv_cache[2][1];
03257 }else{
03258 *(int32_t*)mv_cache[1][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1];
03259 *(int32_t*)mv_cache[2][0]= *(int32_t*)s->current_picture.motion_val[0][mot_xy-1+mot_stride];
03260 }
03261
03262 if(mb_x+1>=s->mb_width || IS_INTRA(s->current_picture.mb_type[xy+1])){
03263 *(int32_t*)mv_cache[1][3]= *(int32_t*)mv_cache[1][2];
03264 *(int32_t*)mv_cache[2][3]= *(int32_t*)mv_cache[2][2];
03265 }else{
03266 *(int32_t*)mv_cache[1][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2];
03267 *(int32_t*)mv_cache[2][3]= *(int32_t*)s->current_picture.motion_val[0][mot_xy+2+mot_stride];
03268 }
03269
03270 mx = 0;
03271 my = 0;
03272 for(i=0;i<4;i++) {
03273 const int x= (i&1)+1;
03274 const int y= (i>>1)+1;
03275 int16_t mv[5][2]= {
03276 {mv_cache[y][x ][0], mv_cache[y][x ][1]},
03277 {mv_cache[y-1][x][0], mv_cache[y-1][x][1]},
03278 {mv_cache[y][x-1][0], mv_cache[y][x-1][1]},
03279 {mv_cache[y][x+1][0], mv_cache[y][x+1][1]},
03280 {mv_cache[y+1][x][0], mv_cache[y+1][x][1]}};
03281
03282 obmc_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
03283 ref_picture[0],
03284 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
03285 pix_op[1],
03286 mv);
03287
03288 mx += mv[0][0];
03289 my += mv[0][1];
03290 }
03291 if(!(s->flags&CODEC_FLAG_GRAY))
03292 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
03293
03294 return;
03295 }
03296
03297 switch(s->mv_type) {
03298 case MV_TYPE_16X16:
03299 if(s->mcsel){
03300 if(s->real_sprite_warping_points==1){
03301 gmc1_motion(s, dest_y, dest_cb, dest_cr,
03302 ref_picture);
03303 }else{
03304 gmc_motion(s, dest_y, dest_cb, dest_cr,
03305 ref_picture);
03306 }
03307 }else if(s->quarter_sample){
03308 qpel_motion(s, dest_y, dest_cb, dest_cr,
03309 0, 0, 0,
03310 ref_picture, pix_op, qpix_op,
03311 s->mv[dir][0][0], s->mv[dir][0][1], 16);
03312 }else if(s->mspel){
03313 ff_mspel_motion(s, dest_y, dest_cb, dest_cr,
03314 ref_picture, pix_op,
03315 s->mv[dir][0][0], s->mv[dir][0][1], 16);
03316 }else
03317 {
03318 mpeg_motion(s, dest_y, dest_cb, dest_cr,
03319 0, 0, 0,
03320 ref_picture, pix_op,
03321 s->mv[dir][0][0], s->mv[dir][0][1], 16);
03322 }
03323 break;
03324 case MV_TYPE_8X8:
03325 mx = 0;
03326 my = 0;
03327 if(s->quarter_sample){
03328 for(i=0;i<4;i++) {
03329 motion_x = s->mv[dir][i][0];
03330 motion_y = s->mv[dir][i][1];
03331
03332 dxy = ((motion_y & 3) << 2) | (motion_x & 3);
03333 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
03334 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8;
03335
03336
03337 src_x = clip(src_x, -16, s->width);
03338 if (src_x == s->width)
03339 dxy &= ~3;
03340 src_y = clip(src_y, -16, s->height);
03341 if (src_y == s->height)
03342 dxy &= ~12;
03343
03344 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
03345 if(s->flags&CODEC_FLAG_EMU_EDGE){
03346 if( (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 8
03347 || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 8 ){
03348 ff_emulated_edge_mc(s->edge_emu_buffer, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
03349 ptr= s->edge_emu_buffer;
03350 }
03351 }
03352 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
03353 qpix_op[1][dxy](dest, ptr, s->linesize);
03354
03355 mx += s->mv[dir][i][0]/2;
03356 my += s->mv[dir][i][1]/2;
03357 }
03358 }else{
03359 for(i=0;i<4;i++) {
03360 hpel_motion(s, dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
03361 ref_picture[0], 0, 0,
03362 mb_x * 16 + (i & 1) * 8, mb_y * 16 + (i >>1) * 8,
03363 s->width, s->height, s->linesize,
03364 s->h_edge_pos, s->v_edge_pos,
03365 8, 8, pix_op[1],
03366 s->mv[dir][i][0], s->mv[dir][i][1]);
03367
03368 mx += s->mv[dir][i][0];
03369 my += s->mv[dir][i][1];
03370 }
03371 }
03372
03373 if(!(s->flags&CODEC_FLAG_GRAY))
03374 chroma_4mv_motion(s, dest_cb, dest_cr, ref_picture, pix_op[1], mx, my);
03375 break;
03376 case MV_TYPE_FIELD:
03377 if (s->picture_structure == PICT_FRAME) {
03378 if(s->quarter_sample){
03379 for(i=0; i<2; i++){
03380 qpel_motion(s, dest_y, dest_cb, dest_cr,
03381 1, i, s->field_select[dir][i],
03382 ref_picture, pix_op, qpix_op,
03383 s->mv[dir][i][0], s->mv[dir][i][1], 8);
03384 }
03385 }else{
03386
03387 mpeg_motion(s, dest_y, dest_cb, dest_cr,
03388 1, 0, s->field_select[dir][0],
03389 ref_picture, pix_op,
03390 s->mv[dir][0][0], s->mv[dir][0][1], 8);
03391
03392 mpeg_motion(s, dest_y, dest_cb, dest_cr,
03393 1, 1, s->field_select[dir][1],
03394 ref_picture, pix_op,
03395 s->mv[dir][1][0], s->mv[dir][1][1], 8);
03396 }
03397 } else {
03398 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
03399 ref_picture= s->current_picture_ptr->data;
03400 }
03401
03402 mpeg_motion(s, dest_y, dest_cb, dest_cr,
03403 0, 0, s->field_select[dir][0],
03404 ref_picture, pix_op,
03405 s->mv[dir][0][0], s->mv[dir][0][1], 16);
03406 }
03407 break;
03408 case MV_TYPE_16X8:
03409 for(i=0; i<2; i++){
03410 uint8_t ** ref2picture;
03411
03412 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
03413 ref2picture= ref_picture;
03414 }else{
03415 ref2picture= s->current_picture_ptr->data;
03416 }
03417
03418 mpeg_motion(s, dest_y, dest_cb, dest_cr,
03419 0, 0, s->field_select[dir][i],
03420 ref2picture, pix_op,
03421 s->mv[dir][i][0], s->mv[dir][i][1] + 16*i, 8);
03422
03423 dest_y += 16*s->linesize;
03424 dest_cb+= (16>>s->chroma_y_shift)*s->uvlinesize;
03425 dest_cr+= (16>>s->chroma_y_shift)*s->uvlinesize;
03426 }
03427 break;
03428 case MV_TYPE_DMV:
03429 if(s->picture_structure == PICT_FRAME){
03430 for(i=0; i<2; i++){
03431 int j;
03432 for(j=0; j<2; j++){
03433 mpeg_motion(s, dest_y, dest_cb, dest_cr,
03434 1, j, j^i,
03435 ref_picture, pix_op,
03436 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], 8);
03437 }
03438 pix_op = s->dsp.avg_pixels_tab;
03439 }
03440 }else{
03441 for(i=0; i<2; i++){
03442 mpeg_motion(s, dest_y, dest_cb, dest_cr,
03443 0, 0, s->picture_structure != i+1,
03444 ref_picture, pix_op,
03445 s->mv[dir][2*i][0],s->mv[dir][2*i][1],16);
03446
03447
03448 pix_op=s->dsp.avg_pixels_tab;
03449
03450
03451 if(!s->first_field){
03452 ref_picture = s->current_picture_ptr->data;
03453 }
03454 }
03455 }
03456 break;
03457 default: assert(0);
03458 }
03459 }
03460
03472 static inline void MPV_motion_lowres(MpegEncContext *s,
03473 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
03474 int dir, uint8_t **ref_picture,
03475 h264_chroma_mc_func *pix_op)
03476 {
03477 int mx, my;
03478 int mb_x, mb_y, i;
03479 const int lowres= s->avctx->lowres;
03480 const int block_s= 8>>lowres;
03481
03482 mb_x = s->mb_x;
03483 mb_y = s->mb_y;
03484
03485 switch(s->mv_type) {
03486 case MV_TYPE_16X16:
03487 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
03488 0, 0, 0,
03489 ref_picture, pix_op,
03490 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
03491 break;
03492 case MV_TYPE_8X8:
03493 mx = 0;
03494 my = 0;
03495 for(i=0;i<4;i++) {
03496 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) * s->linesize)*block_s,
03497 ref_picture[0], 0, 0,
03498 (2*mb_x + (i & 1))*block_s, (2*mb_y + (i >>1))*block_s,
03499 s->width, s->height, s->linesize,
03500 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
03501 block_s, block_s, pix_op,
03502 s->mv[dir][i][0], s->mv[dir][i][1]);
03503
03504 mx += s->mv[dir][i][0];
03505 my += s->mv[dir][i][1];
03506 }
03507
03508 if(!(s->flags&CODEC_FLAG_GRAY))
03509 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture, pix_op, mx, my);
03510 break;
03511 case MV_TYPE_FIELD:
03512 if (s->picture_structure == PICT_FRAME) {
03513
03514 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
03515 1, 0, s->field_select[dir][0],
03516 ref_picture, pix_op,
03517 s->mv[dir][0][0], s->mv[dir][0][1], block_s);
03518
03519 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
03520 1, 1, s->field_select[dir][1],
03521 ref_picture, pix_op,
03522 s->mv[dir][1][0], s->mv[dir][1][1], block_s);
03523 } else {
03524 if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != B_TYPE && !s->first_field){
03525 ref_picture= s->current_picture_ptr->data;
03526 }
03527
03528 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
03529 0, 0, s->field_select[dir][0],
03530 ref_picture, pix_op,
03531 s->mv[dir][0][0], s->mv[dir][0][1], 2*block_s);
03532 }
03533 break;
03534 case MV_TYPE_16X8:
03535 for(i=0; i<2; i++){
03536 uint8_t ** ref2picture;
03537
03538 if(s->picture_structure == s->field_select[dir][i] + 1 || s->pict_type == B_TYPE || s->first_field){
03539 ref2picture= ref_picture;
03540 }else{
03541 ref2picture= s->current_picture_ptr->data;
03542 }
03543
03544 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
03545 0, 0, s->field_select[dir][i],
03546 ref2picture, pix_op,
03547 s->mv[dir][i][0], s->mv[dir][i][1] + 2*block_s*i, block_s);
03548
03549 dest_y += 2*block_s*s->linesize;
03550 dest_cb+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
03551 dest_cr+= (2*block_s>>s->chroma_y_shift)*s->uvlinesize;
03552 }
03553 break;
03554 case MV_TYPE_DMV:
03555 if(s->picture_structure == PICT_FRAME){
03556 for(i=0; i<2; i++){
03557 int j;
03558 for(j=0; j<2; j++){
03559 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
03560 1, j, j^i,
03561 ref_picture, pix_op,
03562 s->mv[dir][2*i + j][0], s->mv[dir][2*i + j][1], block_s);
03563 }
03564 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
03565 }
03566 }else{
03567 for(i=0; i<2; i++){
03568 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
03569 0, 0, s->picture_structure != i+1,
03570 ref_picture, pix_op,
03571 s->mv[dir][2*i][0],s->mv[dir][2*i][1],2*block_s);
03572
03573
03574 pix_op = s->dsp.avg_h264_chroma_pixels_tab;
03575
03576
03577 if(!s->first_field){
03578 ref_picture = s->current_picture_ptr->data;
03579 }
03580 }
03581 }
03582 break;
03583 default: assert(0);
03584 }
03585 }
03586
03587
03588 static inline void put_dct(MpegEncContext *s,
03589 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
03590 {
03591 s->dct_unquantize_intra(s, block, i, qscale);
03592 s->dsp.idct_put (dest, line_size, block);
03593 }
03594
03595
03596 static inline void add_dct(MpegEncContext *s,
03597 DCTELEM *block, int i, uint8_t *dest, int line_size)
03598 {
03599 if (s->block_last_index[i] >= 0) {
03600 s->dsp.idct_add (dest, line_size, block);
03601 }
03602 }
03603
03604 static inline void add_dequant_dct(MpegEncContext *s,
03605 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
03606 {
03607 if (s->block_last_index[i] >= 0) {
03608 s->dct_unquantize_inter(s, block, i, qscale);
03609
03610 s->dsp.idct_add (dest, line_size, block);
03611 }
03612 }
03613
03617 void ff_clean_intra_table_entries(MpegEncContext *s)
03618 {
03619 int wrap = s->b8_stride;
03620 int xy = s->block_index[0];
03621
03622 s->dc_val[0][xy ] =
03623 s->dc_val[0][xy + 1 ] =
03624 s->dc_val[0][xy + wrap] =
03625 s->dc_val[0][xy + 1 + wrap] = 1024;
03626
03627 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
03628 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
03629 if (s->msmpeg4_version>=3) {
03630 s->coded_block[xy ] =
03631 s->coded_block[xy + 1 ] =
03632 s->coded_block[xy + wrap] =
03633 s->coded_block[xy + 1 + wrap] = 0;
03634 }
03635
03636 wrap = s->mb_stride;
03637 xy = s->mb_x + s->mb_y * wrap;
03638 s->dc_val[1][xy] =
03639 s->dc_val[2][xy] = 1024;
03640
03641 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
03642 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
03643
03644 s->mbintra_table[xy]= 0;
03645 }
03646
03647
03648
03649
03650
03651
03652
03653
03654
03655
03656
03657 static always_inline void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64], int lowres_flag)
03658 {
03659 int mb_x, mb_y;
03660 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
03661 #ifdef HAVE_XVMC
03662 if(s->avctx->xvmc_acceleration){
03663 XVMC_decode_mb(s);
03664 return;
03665 }
03666 #endif
03667
03668 mb_x = s->mb_x;
03669 mb_y = s->mb_y;
03670
03671 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
03672
03673 int i,j;
03674 DCTELEM *dct = &s->current_picture.dct_coeff[mb_xy*64*6];
03675 for(i=0; i<6; i++)
03676 for(j=0; j<64; j++)
03677 *dct++ = block[i][s->dsp.idct_permutation[j]];
03678 }
03679
03680 s->current_picture.qscale_table[mb_xy]= s->qscale;
03681
03682
03683 if (!s->mb_intra) {
03684 if (s->h263_pred || s->h263_aic) {
03685 if(s->mbintra_table[mb_xy])
03686 ff_clean_intra_table_entries(s);
03687 } else {
03688 s->last_dc[0] =
03689 s->last_dc[1] =
03690 s->last_dc[2] = 128 << s->intra_dc_precision;
03691 }
03692 }
03693 else if (s->h263_pred || s->h263_aic)
03694 s->mbintra_table[mb_xy]=1;
03695
03696 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) {
03697 uint8_t *dest_y, *dest_cb, *dest_cr;
03698 int dct_linesize, dct_offset;
03699 op_pixels_func (*op_pix)[4];
03700 qpel_mc_func (*op_qpix)[16];
03701 const int linesize= s->current_picture.linesize[0];
03702 const int uvlinesize= s->current_picture.linesize[1];
03703 const int readable= s->pict_type != B_TYPE || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
03704 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
03705
03706
03707
03708 if(!s->encoding){
03709 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
03710 const int age= s->current_picture.age;
03711
03712 assert(age);
03713
03714 if (s->mb_skipped) {
03715 s->mb_skipped= 0;
03716 assert(s->pict_type!=I_TYPE);
03717
03718 (*mbskip_ptr) ++;
03719 if(*mbskip_ptr >99) *mbskip_ptr= 99;
03720
03721
03722 if (*mbskip_ptr >= age && s->current_picture.reference){
03723 return;
03724 }
03725 } else if(!s->current_picture.reference){
03726 (*mbskip_ptr) ++;
03727 if(*mbskip_ptr >99) *mbskip_ptr= 99;
03728 } else{
03729 *mbskip_ptr = 0;
03730 }
03731 }
03732
03733 dct_linesize = linesize << s->interlaced_dct;
03734 dct_offset =(s->interlaced_dct)? linesize : linesize*block_size;
03735
03736 if(readable){
03737 dest_y= s->dest[0];
03738 dest_cb= s->dest[1];
03739 dest_cr= s->dest[2];
03740 }else{
03741 dest_y = s->b_scratchpad;
03742 dest_cb= s->b_scratchpad+16*linesize;
03743 dest_cr= s->b_scratchpad+32*linesize;
03744 }
03745
03746 if (!s->mb_intra) {
03747
03748
03749 if(!s->encoding){
03750 if(lowres_flag){
03751 h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
03752
03753 if (s->mv_dir & MV_DIR_FORWARD) {
03754 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix);
03755 op_pix = s->dsp.avg_h264_chroma_pixels_tab;
03756 }
03757 if (s->mv_dir & MV_DIR_BACKWARD) {
03758 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix);
03759 }
03760 }else{
03761 if ((!s->no_rounding) || s->pict_type==B_TYPE){
03762 op_pix = s->dsp.put_pixels_tab;
03763 op_qpix= s->dsp.put_qpel_pixels_tab;
03764 }else{
03765 op_pix = s->dsp.put_no_rnd_pixels_tab;
03766 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
03767 }
03768 if (s->mv_dir & MV_DIR_FORWARD) {
03769 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
03770 op_pix = s->dsp.avg_pixels_tab;
03771 op_qpix= s->dsp.avg_qpel_pixels_tab;
03772 }
03773 if (s->mv_dir & MV_DIR_BACKWARD) {
03774 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
03775 }
03776 }
03777 }
03778
03779
03780 if(s->hurry_up>1) goto skip_idct;
03781 if(s->avctx->skip_idct){
03782 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == B_TYPE)
03783 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != I_TYPE)
03784 || s->avctx->skip_idct >= AVDISCARD_ALL)
03785 goto skip_idct;
03786 }
03787
03788
03789 if(s->encoding || !( s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
03790 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
03791 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
03792 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
03793 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
03794 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
03795
03796 if(!(s->flags&CODEC_FLAG_GRAY)){
03797 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
03798 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
03799 }
03800 } else if(s->codec_id != CODEC_ID_WMV2){
03801 add_dct(s, block[0], 0, dest_y , dct_linesize);
03802 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
03803 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
03804 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
03805
03806 if(!(s->flags&CODEC_FLAG_GRAY)){
03807 if(s->chroma_y_shift){
03808 add_dct(s, block[4], 4, dest_cb, uvlinesize);
03809 add_dct(s, block[5], 5, dest_cr, uvlinesize);
03810 }else{
03811
03812 dct_linesize = uvlinesize << s->interlaced_dct;
03813 dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
03814
03815 add_dct(s, block[4], 4, dest_cb, dct_linesize);
03816 add_dct(s, block[5], 5, dest_cr, dct_linesize);
03817 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
03818 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
03819 if(!s->chroma_x_shift){
03820 add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
03821 add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
03822 add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
03823 add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
03824 }
03825 }
03826 }
03827 }
03828 else{
03829 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
03830 }
03831 } else {
03832
03833 if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
03834 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
03835 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
03836 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
03837 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
03838
03839 if(!(s->flags&CODEC_FLAG_GRAY)){
03840 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
03841 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
03842 }
03843 }else{
03844 s->dsp.idct_put(dest_y , dct_linesize, block[0]);
03845 s->dsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
03846 s->dsp.idct_put(dest_y + dct_offset , dct_linesize, block[2]);
03847 s->dsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
03848
03849 if(!(s->flags&CODEC_FLAG_GRAY)){
03850 if(s->chroma_y_shift){
03851 s->dsp.idct_put(dest_cb, uvlinesize, block[4]);
03852 s->dsp.idct_put(dest_cr, uvlinesize, block[5]);
03853 }else{
03854
03855 dct_linesize = uvlinesize << s->interlaced_dct;
03856 dct_offset =(s->interlaced_dct)? uvlinesize : uvlinesize*8;
03857
03858 s->dsp.idct_put(dest_cb, dct_linesize, block[4]);
03859 s->dsp.idct_put(dest_cr, dct_linesize, block[5]);
03860 s->dsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
03861 s->dsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
03862 if(!s->chroma_x_shift){
03863 s->dsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
03864 s->dsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
03865 s->dsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
03866 s->dsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
03867 }
03868 }
03869 }
03870 }
03871 }
03872 skip_idct:
03873 if(!readable){
03874 s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
03875 s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
03876 s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
03877 }
03878 }
03879 }
03880
03881 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
03882 if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1);
03883 else MPV_decode_mb_internal(s, block, 0);
03884 }
03885
03886 #ifdef CONFIG_ENCODERS
03887
03888 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
03889 {
03890 static const char tab[64]=
03891 {3,2,2,1,1,1,1,1,
03892 1,1,1,1,1,1,1,1,
03893 1,1,1,1,1,1,1,1,
03894 0,0,0,0,0,0,0,0,
03895 0,0,0,0,0,0,0,0,
03896 0,0,0,0,0,0,0,0,
03897 0,0,0,0,0,0,0,0,
03898 0,0,0,0,0,0,0,0};
03899 int score=0;
03900 int run=0;
03901 int i;
03902 DCTELEM *block= s->block[n];
03903 const int last_index= s->block_last_index[n];
03904 int skip_dc;
03905
03906 if(threshold<0){
03907 skip_dc=0;
03908 threshold= -threshold;
03909 }else
03910 skip_dc=1;
03911
03912
03913 if(last_index<=skip_dc - 1) return;
03914
03915 for(i=0; i<=last_index; i++){
03916 const int j = s->intra_scantable.permutated[i];
03917 const int level = ABS(block[j]);
03918 if(level==1){
03919 if(skip_dc && i==0) continue;
03920 score+= tab[run];
03921 run=0;
03922 }else if(level>1){
03923 return;
03924 }else{
03925 run++;
03926 }
03927 }
03928 if(score >= threshold) return;
03929 for(i=skip_dc; i<=last_index; i++){
03930 const int j = s->intra_scantable.permutated[i];
03931 block[j]=0;
03932 }
03933 if(block[0]) s->block_last_index[n]= 0;
03934 else s->block_last_index[n]= -1;
03935 }
03936
03937 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
03938 {
03939 int i;
03940 const int maxlevel= s->max_qcoeff;
03941 const int minlevel= s->min_qcoeff;
03942 int overflow=0;
03943
03944 if(s->mb_intra){
03945 i=1;
03946 }else
03947 i=0;
03948
03949 for(;i<=last_index; i++){
03950 const int j= s->intra_scantable.permutated[i];
03951 int level = block[j];
03952
03953 if (level>maxlevel){
03954 level=maxlevel;
03955 overflow++;
03956 }else if(level<minlevel){
03957 level=minlevel;
03958 overflow++;
03959 }
03960
03961 block[j]= level;
03962 }
03963
03964 if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
03965 av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel);
03966 }
03967
03968 #endif //CONFIG_ENCODERS
03969
03974 void ff_draw_horiz_band(MpegEncContext *s, int y, int h){
03975 if (s->avctx->draw_horiz_band) {
03976 AVFrame *src;
03977 int offset[4];
03978
03979 if(s->picture_structure != PICT_FRAME){
03980 h <<= 1;
03981 y <<= 1;
03982 if(s->first_field && !(s->avctx->slice_flags&SLICE_FLAG_ALLOW_FIELD)) return;
03983 }
03984
03985 h= FFMIN(h, s->avctx->height - y);
03986
03987 if(s->pict_type==B_TYPE || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
03988 src= (AVFrame*)s->current_picture_ptr;
03989 else if(s->last_picture_ptr)
03990 src= (AVFrame*)s->last_picture_ptr;
03991 else
03992 return;
03993
03994 if(s->pict_type==B_TYPE && s->picture_structure == PICT_FRAME && s->out_format != FMT_H264){
03995 offset[0]=
03996 offset[1]=
03997 offset[2]=
03998 offset[3]= 0;
03999 }else{
04000 offset[0]= y * s->linesize;;
04001 offset[1]=
04002 offset[2]= (y >> s->chroma_y_shift) * s->uvlinesize;
04003 offset[3]= 0;
04004 }
04005
04006 emms_c();
04007
04008 s->avctx->draw_horiz_band(s->avctx, src, offset,
04009 y, s->picture_structure, h);
04010 }
04011 }
04012
04013 void ff_init_block_index(MpegEncContext *s){
04014 const int linesize= s->current_picture.linesize[0];
04015 const int uvlinesize= s->current_picture.linesize[1];
04016 const int mb_size= 4 - s->avctx->lowres;
04017
04018 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
04019 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
04020 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
04021 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
04022 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
04023 s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
04024
04025
04026 s->dest[0] = s->current_picture.data[0] + ((s->mb_x - 1) << mb_size);
04027 s->dest[1] = s->current_picture.data[1] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
04028 s->dest[2] = s->current_picture.data[2] + ((s->mb_x - 1) << (mb_size - s->chroma_x_shift));
04029
04030 if(!(s->pict_type==B_TYPE && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
04031 {
04032 s->dest[0] += s->mb_y * linesize << mb_size;
04033 s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
04034 s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
04035 }
04036 }
04037
04038 #ifdef CONFIG_ENCODERS
04039
04040 static void get_vissual_weight(int16_t *weight, uint8_t *ptr, int stride){
04041 int x, y;
04042
04043 for(y=0; y<8; y++){
04044 for(x=0; x<8; x++){
04045 int x2, y2;
04046 int sum=0;
04047 int sqr=0;
04048 int count=0;
04049
04050 for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
04051 for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
04052 int v= ptr[x2 + y2*stride];
04053 sum += v;
04054 sqr += v*v;
04055 count++;
04056 }
04057 }
04058 weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
04059 }
04060 }
04061 }
04062
04063 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
04064 {
04065 int16_t weight[6][64];
04066 DCTELEM orig[6][64];
04067 const int mb_x= s->mb_x;
04068 const int mb_y= s->mb_y;
04069 int i;
04070 int skip_dct[6];
04071 int dct_offset = s->linesize*8;
04072 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
04073 int wrap_y, wrap_c;
04074
04075 for(i=0; i<6; i++) skip_dct[i]=0;
04076
04077 if(s->adaptive_quant){
04078 const int last_qp= s->qscale;
04079 const int mb_xy= mb_x + mb_y*s->mb_stride;
04080
04081 s->lambda= s->lambda_table[mb_xy];
04082 update_qscale(s);
04083
04084 if(!(s->flags&CODEC_FLAG_QP_RD)){
04085 s->dquant= s->qscale - last_qp;
04086
04087 if(s->out_format==FMT_H263){
04088 s->dquant= clip(s->dquant, -2, 2);
04089
04090 if(s->codec_id==CODEC_ID_MPEG4){
04091 if(!s->mb_intra){
04092 if(s->pict_type == B_TYPE){
04093 if(s->dquant&1)
04094 s->dquant= (s->dquant/2)*2;
04095 if(s->mv_dir&MV_DIRECT)
04096 s->dquant= 0;
04097 }
04098 if(s->mv_type==MV_TYPE_8X8)
04099 s->dquant=0;
04100 }
04101 }
04102 }
04103 }
04104 ff_set_qscale(s, last_qp + s->dquant);
04105 }else if(s->flags&CODEC_FLAG_QP_RD)
04106 ff_set_qscale(s, s->qscale + s->dquant);
04107
04108 wrap_y = s->linesize;
04109 wrap_c = s->uvlinesize;
04110 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
04111 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
04112 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
04113
04114 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
04115 uint8_t *ebuf= s->edge_emu_buffer + 32;
04116 ff_emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height);
04117 ptr_y= ebuf;
04118 ff_emulated_edge_mc(ebuf+18*wrap_y , ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
04119 ptr_cb= ebuf+18*wrap_y;
04120 ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
04121 ptr_cr= ebuf+18*wrap_y+8;
04122 }
04123
04124 if (s->mb_intra) {
04125 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
04126 int progressive_score, interlaced_score;
04127
04128 s->interlaced_dct=0;
04129 progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
04130 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
04131
04132 if(progressive_score > 0){
04133 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
04134 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
04135 if(progressive_score > interlaced_score){
04136 s->interlaced_dct=1;
04137
04138 dct_offset= wrap_y;
04139 wrap_y<<=1;
04140 }
04141 }
04142 }
04143
04144 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
04145 s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
04146 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
04147 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
04148
04149 if(s->flags&CODEC_FLAG_GRAY){
04150 skip_dct[4]= 1;
04151 skip_dct[5]= 1;
04152 }else{
04153 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
04154 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
04155 }
04156 }else{
04157 op_pixels_func (*op_pix)[4];
04158 qpel_mc_func (*op_qpix)[16];
04159 uint8_t *dest_y, *dest_cb, *dest_cr;
04160
04161 dest_y = s->dest[0];
04162 dest_cb = s->dest[1];
04163 dest_cr = s->dest[2];
04164
04165 if ((!s->no_rounding) || s->pict_type==B_TYPE){
04166 op_pix = s->dsp.put_pixels_tab;
04167 op_qpix= s->dsp.put_qpel_pixels_tab;
04168 }else{
04169 op_pix = s->dsp.put_no_rnd_pixels_tab;
04170 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
04171 }
04172
04173 if (s->mv_dir & MV_DIR_FORWARD) {
04174 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
04175 op_pix = s->dsp.avg_pixels_tab;
04176 op_qpix= s->dsp.avg_qpel_pixels_tab;
04177 }
04178 if (s->mv_dir & MV_DIR_BACKWARD) {
04179 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
04180 }
04181
04182 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
04183 int progressive_score, interlaced_score;
04184
04185 s->interlaced_dct=0;
04186 progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
04187 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
04188
04189 if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
04190
04191 if(progressive_score>0){
04192 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
04193 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
04194
04195 if(progressive_score > interlaced_score){
04196 s->interlaced_dct=1;
04197
04198 dct_offset= wrap_y;
04199 wrap_y<<=1;
04200 }
04201 }
04202 }
04203
04204 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
04205 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
04206 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
04207 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
04208
04209 if(s->flags&CODEC_FLAG_GRAY){
04210 skip_dct[4]= 1;
04211 skip_dct[5]= 1;
04212 }else{
04213 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
04214 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
04215 }
04216
04217 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
04218
04219 if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
04220 if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
04221 if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
04222 if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
04223 if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
04224 if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
04225 }
04226 }
04227
04228 if(s->avctx->quantizer_noise_shaping){
04229 if(!skip_dct[0]) get_vissual_weight(weight[0], ptr_y , wrap_y);
04230 if(!skip_dct[1]) get_vissual_weight(weight[1], ptr_y + 8, wrap_y);
04231 if(!skip_dct[2]) get_vissual_weight(weight[2], ptr_y + dct_offset , wrap_y);
04232 if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
04233 if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb , wrap_c);
04234 if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr , wrap_c);
04235 memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6);
04236 }
04237
04238
04239 assert(s->out_format!=FMT_MJPEG || s->qscale==8);
04240 {
04241 for(i=0;i<6;i++) {
04242 if(!skip_dct[i]){
04243 int overflow;
04244 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
04245
04246
04247
04248 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
04249 }else
04250 s->block_last_index[i]= -1;
04251 }
04252 if(s->avctx->quantizer_noise_shaping){
04253 for(i=0;i<6;i++) {
04254 if(!skip_dct[i]){
04255 s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
04256 }
04257 }
04258 }
04259
04260 if(s->luma_elim_threshold && !s->mb_intra)
04261 for(i=0; i<4; i++)
04262 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
04263 if(s->chroma_elim_threshold && !s->mb_intra)
04264 for(i=4; i<6; i++)
04265 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
04266
04267 if(s->flags & CODEC_FLAG_CBP_RD){
04268 for(i=0;i<6;i++) {
04269 if(s->block_last_index[i] == -1)
04270 s->coded_score[i]= INT_MAX/256;
04271 }
04272 }
04273 }
04274
04275 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
04276 s->block_last_index[4]=
04277 s->block_last_index[5]= 0;
04278 s->block[4][0]=
04279 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
04280 }
04281
04282
04283 if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
04284 for(i=0; i<6; i++){
04285 int j;
04286 if(s->block_last_index[i]>0){
04287 for(j=63; j>0; j--){
04288 if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
04289 }
04290 s->block_last_index[i]= j;
04291 }
04292 }
04293 }
04294
04295
04296 switch(s->codec_id){
04297 case CODEC_ID_MPEG1VIDEO:
04298 case CODEC_ID_MPEG2VIDEO:
04299 mpeg1_encode_mb(s, s->block, motion_x, motion_y); break;
04300 case CODEC_ID_MPEG4:
04301 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
04302 case CODEC_ID_MSMPEG4V2:
04303 case CODEC_ID_MSMPEG4V3:
04304 case CODEC_ID_WMV1:
04305 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break;
04306 case CODEC_ID_WMV2:
04307 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break;
04308 #ifdef CONFIG_H261_ENCODER
04309 case CODEC_ID_H261:
04310 ff_h261_encode_mb(s, s->block, motion_x, motion_y); break;
04311 #endif
04312 case CODEC_ID_H263:
04313 case CODEC_ID_H263P:
04314 case CODEC_ID_FLV1:
04315 case CODEC_ID_RV10:
04316 case CODEC_ID_RV20:
04317 h263_encode_mb(s, s->block, motion_x, motion_y); break;
04318 case CODEC_ID_MJPEG:
04319 mjpeg_encode_mb(s, s->block); break;
04320 default:
04321 assert(0);
04322 }
04323 }
04324
04325 #endif //CONFIG_ENCODERS
04326
04327 void ff_mpeg_flush(AVCodecContext *avctx){
04328 int i;
04329 MpegEncContext *s = avctx->priv_data;
04330
04331 if(s==NULL || s->picture==NULL)
04332 return;
04333
04334 for(i=0; i<MAX_PICTURE_COUNT; i++){
04335 if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
04336 || s->picture[i].type == FF_BUFFER_TYPE_USER))
04337 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
04338 }
04339 s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
04340
04341 s->mb_x= s->mb_y= 0;
04342
04343 s->parse_context.state= -1;
04344 s->parse_context.frame_start_found= 0;
04345 s->parse_context.overread= 0;
04346 s->parse_context.overread_index= 0;
04347 s->parse_context.index= 0;
04348 s->parse_context.last_index= 0;
04349 s->bitstream_buffer_size=0;
04350 }
04351
04352 #ifdef CONFIG_ENCODERS
04353 void ff_copy_bits(PutBitContext *pb, uint8_t *src, int length)
04354 {
04355 const uint16_t *srcw= (uint16_t*)src;
04356 int words= length>>4;
04357 int bits= length&15;
04358 int i;
04359
04360 if(length==0) return;
04361
04362 if(words < 16){
04363 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
04364 }else if(put_bits_count(pb)&7){
04365 for(i=0; i<words; i++) put_bits(pb, 16, be2me_16(srcw[i]));
04366 }else{
04367 for(i=0; put_bits_count(pb)&31; i++)
04368 put_bits(pb, 8, src[i]);
04369 flush_put_bits(pb);
04370 memcpy(pbBufPtr(pb), src+i, 2*words-i);
04371 skip_put_bytes(pb, 2*words-i);
04372 }
04373
04374 put_bits(pb, bits, be2me_16(srcw[words])>>(16-bits));
04375 }
04376
04377 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
04378 int i;
04379
04380 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int));
04381
04382
04383 d->mb_skip_run= s->mb_skip_run;
04384 for(i=0; i<3; i++)
04385 d->last_dc[i]= s->last_dc[i];
04386
04387
04388 d->mv_bits= s->mv_bits;
04389 d->i_tex_bits= s->i_tex_bits;
04390 d->p_tex_bits= s->p_tex_bits;
04391 d->i_count= s->i_count;
04392 d->f_count= s->f_count;
04393 d->b_count= s->b_count;
04394 d->skip_count= s->skip_count;
04395 d->misc_bits= s->misc_bits;
04396 d->last_bits= 0;
04397
04398 d->mb_skipped= 0;
04399 d->qscale= s->qscale;
04400 d->dquant= s->dquant;
04401 }
04402
04403 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
04404 int i;
04405
04406 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
04407 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int));
04408
04409
04410 d->mb_skip_run= s->mb_skip_run;
04411 for(i=0; i<3; i++)
04412 d->last_dc[i]= s->last_dc[i];
04413
04414
04415 d->mv_bits= s->mv_bits;
04416 d->i_tex_bits= s->i_tex_bits;
04417 d->p_tex_bits= s->p_tex_bits;
04418 d->i_count= s->i_count;
04419 d->f_count= s->f_count;
04420 d->b_count= s->b_count;
04421 d->skip_count= s->skip_count;
04422 d->misc_bits= s->misc_bits;
04423
04424 d->mb_intra= s->mb_intra;
04425 d->mb_skipped= s->mb_skipped;
04426 d->mv_type= s->mv_type;
04427 d->mv_dir= s->mv_dir;
04428 d->pb= s->pb;
04429 if(s->data_partitioning){
04430 d->pb2= s->pb2;
04431 d->tex_pb= s->tex_pb;
04432 }
04433 d->block= s->block;
04434 for(i=0; i<6; i++)
04435 d->block_last_index[i]= s->block_last_index[i];
04436 d->interlaced_dct= s->interlaced_dct;
04437 d->qscale= s->qscale;
04438 }
04439
04440 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
04441 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
04442 int *dmin, int *next_block, int motion_x, int motion_y)
04443 {
04444 int score;
04445 uint8_t *dest_backup[3];
04446
04447 copy_context_before_encode(s, backup, type);
04448
04449 s->block= s->blocks[*next_block];
04450 s->pb= pb[*next_block];
04451 if(s->data_partitioning){
04452 s->pb2 = pb2 [*next_block];
04453 s->tex_pb= tex_pb[*next_block];
04454 }
04455
04456 if(*next_block){
04457 memcpy(dest_backup, s->dest, sizeof(s->dest));
04458 s->dest[0] = s->rd_scratchpad;
04459 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
04460 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
04461 assert(s->linesize >= 32);
04462 }
04463
04464 encode_mb(s, motion_x, motion_y);
04465
04466 score= put_bits_count(&s->pb);
04467 if(s->data_partitioning){
04468 score+= put_bits_count(&s->pb2);
04469 score+= put_bits_count(&s->tex_pb);
04470 }
04471
04472 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
04473 MPV_decode_mb(s, s->block);
04474
04475 score *= s->lambda2;
04476 score += sse_mb(s) << FF_LAMBDA_SHIFT;
04477 }
04478
04479 if(*next_block){
04480 memcpy(s->dest, dest_backup, sizeof(s->dest));
04481 }
04482
04483 if(score<*dmin){
04484 *dmin= score;
04485 *next_block^=1;
04486
04487 copy_context_after_encode(best, s, type);
04488 }
04489 }
04490
04491 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
04492 uint32_t *sq = squareTbl + 256;
04493 int acc=0;
04494 int x,y;
04495
04496 if(w==16 && h==16)
04497 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
04498 else if(w==8 && h==8)
04499 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
04500
04501 for(y=0; y<h; y++){
04502 for(x=0; x<w; x++){
04503 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
04504 }
04505 }
04506
04507 assert(acc>=0);
04508
04509 return acc;
04510 }
04511
04512 static int sse_mb(MpegEncContext *s){
04513 int w= 16;
04514 int h= 16;
04515
04516 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
04517 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
04518
04519 if(w==16 && h==16)
04520 if(s->avctx->mb_cmp == FF_CMP_NSSE){
04521 return s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
04522 +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
04523 +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
04524 }else{
04525 return s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
04526 +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
04527 +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
04528 }
04529 else
04530 return sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
04531 +sse(s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
04532 +sse(s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
04533 }
04534
04535 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
04536 MpegEncContext *s= arg;
04537
04538
04539 s->me.pre_pass=1;
04540 s->me.dia_size= s->avctx->pre_dia_size;
04541 s->first_slice_line=1;
04542 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
04543 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
04544 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
04545 }
04546 s->first_slice_line=0;
04547 }
04548
04549 s->me.pre_pass=0;
04550
04551 return 0;
04552 }
04553
04554 static int estimate_motion_thread(AVCodecContext *c, void *arg){
04555 MpegEncContext *s= arg;
04556
04557 s->me.dia_size= s->avctx->dia_size;
04558 s->first_slice_line=1;
04559 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
04560 s->mb_x=0;
04561 ff_init_block_index(s);
04562 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
04563 s->block_index[0]+=2;
04564 s->block_index[1]+=2;
04565 s->block_index[2]+=2;
04566 s->block_index[3]+=2;
04567
04568
04569 if(s->pict_type==B_TYPE)
04570 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
04571 else
04572 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
04573 }
04574 s->first_slice_line=0;
04575 }
04576 return 0;
04577 }
04578
04579 static int mb_var_thread(AVCodecContext *c, void *arg){
04580 MpegEncContext *s= arg;
04581 int mb_x, mb_y;
04582
04583 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
04584 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
04585 int xx = mb_x * 16;
04586 int yy = mb_y * 16;
04587 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
04588 int varc;
04589 int sum = s->dsp.pix_sum(pix, s->linesize);
04590
04591 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
04592
04593 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
04594 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
04595 s->me.mb_var_sum_temp += varc;
04596 }
04597 }
04598 return 0;
04599 }
04600
04601 static void write_slice_end(MpegEncContext *s){
04602 if(s->codec_id==CODEC_ID_MPEG4){
04603 if(s->partitioned_frame){
04604 ff_mpeg4_merge_partitions(s);
04605 }
04606
04607 ff_mpeg4_stuffing(&s->pb);
04608 }else if(s->out_format == FMT_MJPEG){
04609 ff_mjpeg_stuffing(&s->pb);
04610 }
04611
04612 align_put_bits(&s->pb);
04613 flush_put_bits(&s->pb);
04614
04615 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
04616 s->misc_bits+= get_bits_diff(s);
04617 }
04618
04619 static int encode_thread(AVCodecContext *c, void *arg){
04620 MpegEncContext *s= arg;
04621 int mb_x, mb_y, pdif = 0;
04622 int i, j;
04623 MpegEncContext best_s, backup_s;
04624 uint8_t bit_buf[2][MAX_MB_BYTES];
04625 uint8_t bit_buf2[2][MAX_MB_BYTES];
04626 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
04627 PutBitContext pb[2], pb2[2], tex_pb[2];
04628
04629
04630 for(i=0; i<2; i++){
04631 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
04632 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
04633 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
04634 }
04635
04636 s->last_bits= put_bits_count(&s->pb);
04637 s->mv_bits=0;
04638 s->misc_bits=0;
04639 s->i_tex_bits=0;
04640 s->p_tex_bits=0;
04641 s->i_count=0;
04642 s->f_count=0;
04643 s->b_count=0;
04644 s->skip_count=0;
04645
04646 for(i=0; i<3; i++){
04647
04648
04649 s->last_dc[i] = 128 << s->intra_dc_precision;
04650
04651 s->current_picture_ptr->error[i] = 0;
04652 }
04653 s->mb_skip_run = 0;
04654 memset(s->last_mv, 0, sizeof(s->last_mv));
04655
04656 s->last_mv_dir = 0;
04657
04658 switch(s->codec_id){
04659 case CODEC_ID_H263:
04660 case CODEC_ID_H263P:
04661 case CODEC_ID_FLV1:
04662 s->gob_index = ff_h263_get_gob_height(s);
04663 break;
04664 case CODEC_ID_MPEG4:
04665 if(s->partitioned_frame)
04666 ff_mpeg4_init_partitions(s);
04667 break;
04668 }
04669
04670 s->resync_mb_x=0;
04671 s->resync_mb_y=0;
04672 s->first_slice_line = 1;
04673 s->ptr_lastgob = s->pb.buf;
04674 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
04675
04676 s->mb_x=0;
04677 s->mb_y= mb_y;
04678
04679 ff_set_qscale(s, s->qscale);
04680 ff_init_block_index(s);
04681
04682 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
04683 int xy= mb_y*s->mb_stride + mb_x;
04684 int mb_type= s->mb_type[xy];
04685
04686 int dmin= INT_MAX;
04687 int dir;
04688
04689 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
04690 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
04691 return -1;
04692 }
04693 if(s->data_partitioning){
04694 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
04695 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
04696 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
04697 return -1;
04698 }
04699 }
04700
04701 s->mb_x = mb_x;
04702 s->mb_y = mb_y;
04703 ff_update_block_index(s);
04704
04705 #ifdef CONFIG_H261_ENCODER
04706 if(s->codec_id == CODEC_ID_H261){
04707 ff_h261_reorder_mb_index(s);
04708 xy= s->mb_y*s->mb_stride + s->mb_x;
04709 mb_type= s->mb_type[xy];
04710 }
04711 #endif
04712
04713
04714 if(s->rtp_mode){
04715 int current_packet_size, is_gob_start;
04716
04717 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
04718
04719 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
04720
04721 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
04722
04723 switch(s->codec_id){
04724 case CODEC_ID_H263:
04725 case CODEC_ID_H263P:
04726 if(!s->h263_slice_structured)
04727 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
04728 break;
04729 case CODEC_ID_MPEG2VIDEO:
04730 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
04731 case CODEC_ID_MPEG1VIDEO:
04732 if(s->mb_skip_run) is_gob_start=0;
04733 break;
04734 }
04735
04736 if(is_gob_start){
04737 if(s->start_mb_y != mb_y || mb_x!=0){
04738 write_slice_end(s);
04739
04740 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
04741 ff_mpeg4_init_partitions(s);
04742 }
04743 }
04744
04745 assert((put_bits_count(&s->pb)&7) == 0);
04746 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob;
04747
04748 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
04749 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
04750 int d= 100 / s->avctx->error_rate;
04751 if(r % d == 0){
04752 current_packet_size=0;
04753 #ifndef ALT_BITSTREAM_WRITER
04754 s->pb.buf_ptr= s->ptr_lastgob;
04755 #endif
04756 assert(pbBufPtr(&s->pb) == s->ptr_lastgob);
04757 }
04758 }
04759
04760 if (s->avctx->rtp_callback){
04761 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
04762 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
04763 }
04764
04765 switch(s->codec_id){
04766 case CODEC_ID_MPEG4:
04767 ff_mpeg4_encode_video_packet_header(s);
04768 ff_mpeg4_clean_buffers(s);
04769 break;
04770 case CODEC_ID_MPEG1VIDEO:
04771 case CODEC_ID_MPEG2VIDEO:
04772 ff_mpeg1_encode_slice_header(s);
04773 ff_mpeg1_clean_buffers(s);
04774 break;
04775 case CODEC_ID_H263:
04776 case CODEC_ID_H263P:
04777 h263_encode_gob_header(s, mb_y);
04778 break;
04779 }
04780
04781 if(s->flags&CODEC_FLAG_PASS1){
04782 int bits= put_bits_count(&s->pb);
04783 s->misc_bits+= bits - s->last_bits;
04784 s->last_bits= bits;
04785 }
04786
04787 s->ptr_lastgob += current_packet_size;
04788 s->first_slice_line=1;
04789 s->resync_mb_x=mb_x;
04790 s->resync_mb_y=mb_y;
04791 }
04792 }
04793
04794 if( (s->resync_mb_x == s->mb_x)
04795 && s->resync_mb_y+1 == s->mb_y){
04796 s->first_slice_line=0;
04797 }
04798
04799 s->mb_skipped=0;
04800 s->dquant=0;
04801
04802 if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){
04803 int next_block=0;
04804 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
04805
04806 copy_context_before_encode(&backup_s, s, -1);
04807 backup_s.pb= s->pb;
04808 best_s.data_partitioning= s->data_partitioning;
04809 best_s.partitioned_frame= s->partitioned_frame;
04810 if(s->data_partitioning){
04811 backup_s.pb2= s->pb2;
04812 backup_s.tex_pb= s->tex_pb;
04813 }
04814
04815 if(mb_type&CANDIDATE_MB_TYPE_INTER){
04816 s->mv_dir = MV_DIR_FORWARD;
04817 s->mv_type = MV_TYPE_16X16;
04818 s->mb_intra= 0;
04819 s->mv[0][0][0] = s->p_mv_table[xy][0];
04820 s->mv[0][0][1] = s->p_mv_table[xy][1];
04821 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
04822 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
04823 }
04824 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
04825 s->mv_dir = MV_DIR_FORWARD;
04826 s->mv_type = MV_TYPE_FIELD;
04827 s->mb_intra= 0;
04828 for(i=0; i<2; i++){
04829 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
04830 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
04831 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
04832 }
04833 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
04834 &dmin, &next_block, 0, 0);
04835 }
04836 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
04837 s->mv_dir = MV_DIR_FORWARD;
04838 s->mv_type = MV_TYPE_16X16;
04839 s->mb_intra= 0;
04840 s->mv[0][0][0] = 0;
04841 s->mv[0][0][1] = 0;
04842 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
04843 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
04844 }
04845 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
04846 s->mv_dir = MV_DIR_FORWARD;
04847 s->mv_type = MV_TYPE_8X8;
04848 s->mb_intra= 0;
04849 for(i=0; i<4; i++){
04850 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
04851 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
04852 }
04853 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
04854 &dmin, &next_block, 0, 0);
04855 }
04856 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
04857 s->mv_dir = MV_DIR_FORWARD;
04858 s->mv_type = MV_TYPE_16X16;
04859 s->mb_intra= 0;
04860 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
04861 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
04862 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
04863 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
04864 }
04865 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
04866 s->mv_dir = MV_DIR_BACKWARD;
04867 s->mv_type = MV_TYPE_16X16;
04868 s->mb_intra= 0;
04869 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
04870 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
04871 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
04872 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
04873 }
04874 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
04875 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
04876 s->mv_type = MV_TYPE_16X16;
04877 s->mb_intra= 0;
04878 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
04879 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
04880 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
04881 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
04882 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
04883 &dmin, &next_block, 0, 0);
04884 }
04885 if(mb_type&CANDIDATE_MB_TYPE_DIRECT){
04886 int mx= s->b_direct_mv_table[xy][0];
04887 int my= s->b_direct_mv_table[xy][1];
04888
04889 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
04890 s->mb_intra= 0;
04891 ff_mpeg4_set_direct_mv(s, mx, my);
04892 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
04893 &dmin, &next_block, mx, my);
04894 }
04895 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
04896 s->mv_dir = MV_DIR_FORWARD;
04897 s->mv_type = MV_TYPE_FIELD;
04898 s->mb_intra= 0;
04899 for(i=0; i<2; i++){
04900 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
04901 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
04902 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
04903 }
04904 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
04905 &dmin, &next_block, 0, 0);
04906 }
04907 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
04908 s->mv_dir = MV_DIR_BACKWARD;
04909 s->mv_type = MV_TYPE_FIELD;
04910 s->mb_intra= 0;
04911 for(i=0; i<2; i++){
04912 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
04913 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
04914 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
04915 }
04916 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
04917 &dmin, &next_block, 0, 0);
04918 }
04919 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
04920 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
04921 s->mv_type = MV_TYPE_FIELD;
04922 s->mb_intra= 0;
04923 for(dir=0; dir<2; dir++){
04924 for(i=0; i<2; i++){
04925 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
04926 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
04927 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
04928 }
04929 }
04930 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
04931 &dmin, &next_block, 0, 0);
04932 }
04933 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
04934 s->mv_dir = 0;
04935 s->mv_type = MV_TYPE_16X16;
04936 s->mb_intra= 1;
04937 s->mv[0][0][0] = 0;
04938 s->mv[0][0][1] = 0;
04939 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
04940 &dmin, &next_block, 0, 0);
04941 if(s->h263_pred || s->h263_aic){
04942 if(best_s.mb_intra)
04943 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
04944 else
04945 ff_clean_intra_table_entries(s);
04946 }
04947 }
04948
04949 if(s->flags & CODEC_FLAG_QP_RD){
04950 if(best_s.mv_type==MV_TYPE_16X16 && !(best_s.mv_dir&MV_DIRECT)){
04951 const int last_qp= backup_s.qscale;
04952 int dquant, dir, qp, dc[6];
04953 DCTELEM ac[6][16];
04954 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
04955
04956 assert(backup_s.dquant == 0);
04957
04958
04959 s->mv_dir= best_s.mv_dir;
04960 s->mv_type = MV_TYPE_16X16;
04961 s->mb_intra= best_s.mb_intra;
04962 s->mv[0][0][0] = best_s.mv[0][0][0];
04963 s->mv[0][0][1] = best_s.mv[0][0][1];
04964 s->mv[1][0][0] = best_s.mv[1][0][0];
04965 s->mv[1][0][1] = best_s.mv[1][0][1];
04966
04967 dir= s->pict_type == B_TYPE ? 2 : 1;
04968 if(last_qp + dir > s->avctx->qmax) dir= -dir;
04969 for(dquant= dir; dquant<=2 && dquant>=-2; dquant += dir){
04970 qp= last_qp + dquant;
04971 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
04972 break;
04973 backup_s.dquant= dquant;
04974 if(s->mb_intra && s->dc_val[0]){
04975 for(i=0; i<6; i++){
04976 dc[i]= s->dc_val[0][ s->block_index[i] ];
04977 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
04978 }
04979 }
04980
04981 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
04982 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
04983 if(best_s.qscale != qp){
04984 if(s->mb_intra && s->dc_val[0]){
04985 for(i=0; i<6; i++){
04986 s->dc_val[0][ s->block_index[i] ]= dc[i];
04987 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
04988 }
04989 }
04990 if(dir > 0 && dquant==dir){
04991 dquant= 0;
04992 dir= -dir;
04993 }else
04994 break;
04995 }
04996 }
04997 qp= best_s.qscale;
04998 s->current_picture.qscale_table[xy]= qp;
04999 }
05000 }
05001
05002 copy_context_after_encode(s, &best_s, -1);
05003
05004 pb_bits_count= put_bits_count(&s->pb);
05005 flush_put_bits(&s->pb);
05006 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
05007 s->pb= backup_s.pb;
05008
05009 if(s->data_partitioning){
05010 pb2_bits_count= put_bits_count(&s->pb2);
05011 flush_put_bits(&s->pb2);
05012 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
05013 s->pb2= backup_s.pb2;
05014
05015 tex_pb_bits_count= put_bits_count(&s->tex_pb);
05016 flush_put_bits(&s->tex_pb);
05017 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
05018 s->tex_pb= backup_s.tex_pb;
05019 }
05020 s->last_bits= put_bits_count(&s->pb);
05021
05022 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
05023 ff_h263_update_motion_val(s);
05024
05025 if(next_block==0){
05026 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
05027 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
05028 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
05029 }
05030
05031 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
05032 MPV_decode_mb(s, s->block);
05033 } else {
05034 int motion_x, motion_y;
05035 s->mv_type=MV_TYPE_16X16;
05036
05037
05038 switch(mb_type){
05039 case CANDIDATE_MB_TYPE_INTRA:
05040 s->mv_dir = 0;
05041 s->mb_intra= 1;
05042 motion_x= s->mv[0][0][0] = 0;
05043 motion_y= s->mv[0][0][1] = 0;
05044 break;
05045 case CANDIDATE_MB_TYPE_INTER:
05046 s->mv_dir = MV_DIR_FORWARD;
05047 s->mb_intra= 0;
05048 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
05049 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
05050 break;
05051 case CANDIDATE_MB_TYPE_INTER_I:
05052 s->mv_dir = MV_DIR_FORWARD;
05053 s->mv_type = MV_TYPE_FIELD;
05054 s->mb_intra= 0;
05055 for(i=0; i<2; i++){
05056 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
05057 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
05058 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
05059 }
05060 motion_x = motion_y = 0;
05061 break;
05062 case CANDIDATE_MB_TYPE_INTER4V:
05063 s->mv_dir = MV_DIR_FORWARD;
05064 s->mv_type = MV_TYPE_8X8;
05065 s->mb_intra= 0;
05066 for(i=0; i<4; i++){
05067 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
05068 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
05069 }
05070 motion_x= motion_y= 0;
05071 break;
05072 case CANDIDATE_MB_TYPE_DIRECT:
05073 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
05074 s->mb_intra= 0;
05075 motion_x=s->b_direct_mv_table[xy][0];
05076 motion_y=s->b_direct_mv_table[xy][1];
05077 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
05078 break;
05079 case CANDIDATE_MB_TYPE_BIDIR:
05080 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
05081 s->mb_intra= 0;
05082 motion_x=0;
05083 motion_y=0;
05084 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
05085 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
05086 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
05087 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
05088 break;
05089 case CANDIDATE_MB_TYPE_BACKWARD:
05090 s->mv_dir = MV_DIR_BACKWARD;
05091 s->mb_intra= 0;
05092 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
05093 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
05094 break;
05095 case CANDIDATE_MB_TYPE_FORWARD:
05096 s->mv_dir = MV_DIR_FORWARD;
05097 s->mb_intra= 0;
05098 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
05099 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
05100
05101 break;
05102 case CANDIDATE_MB_TYPE_FORWARD_I:
05103 s->mv_dir = MV_DIR_FORWARD;
05104 s->mv_type = MV_TYPE_FIELD;
05105 s->mb_intra= 0;
05106 for(i=0; i<2; i++){
05107 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
05108 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
05109 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
05110 }
05111 motion_x=motion_y=0;
05112 break;
05113 case CANDIDATE_MB_TYPE_BACKWARD_I:
05114 s->mv_dir = MV_DIR_BACKWARD;
05115 s->mv_type = MV_TYPE_FIELD;
05116 s->mb_intra= 0;
05117 for(i=0; i<2; i++){
05118 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
05119 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
05120 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
05121 }
05122 motion_x=motion_y=0;
05123 break;
05124 case CANDIDATE_MB_TYPE_BIDIR_I:
05125 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
05126 s->mv_type = MV_TYPE_FIELD;
05127 s->mb_intra= 0;
05128 for(dir=0; dir<2; dir++){
05129 for(i=0; i<2; i++){
05130 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
05131 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
05132 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
05133 }
05134 }
05135 motion_x=motion_y=0;
05136 break;
05137 default:
05138 motion_x=motion_y=0;
05139 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
05140 }
05141
05142 encode_mb(s, motion_x, motion_y);
05143
05144
05145 s->last_mv_dir = s->mv_dir;
05146
05147 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE)
05148 ff_h263_update_motion_val(s);
05149
05150 MPV_decode_mb(s, s->block);
05151 }
05152
05153
05154 if(s->mb_intra ){
05155 s->p_mv_table[xy][0]=0;
05156 s->p_mv_table[xy][1]=0;
05157 }
05158
05159 if(s->flags&CODEC_FLAG_PSNR){
05160 int w= 16;
05161 int h= 16;
05162
05163 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
05164 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
05165
05166 s->current_picture_ptr->error[0] += sse(
05167 s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
05168 s->dest[0], w, h, s->linesize);
05169 s->current_picture_ptr->error[1] += sse(
05170 s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
05171 s->dest[1], w>>1, h>>1, s->uvlinesize);
05172 s->current_picture_ptr->error[2] += sse(
05173 s, s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,
05174 s->dest[2], w>>1, h>>1, s->uvlinesize);
05175 }
05176 if(s->loop_filter){
05177 if(s->out_format == FMT_H263)
05178 ff_h263_loop_filter(s);
05179 }
05180
05181 }
05182 }
05183
05184
05185 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
05186 msmpeg4_encode_ext_header(s);
05187
05188 write_slice_end(s);
05189
05190
05191 if (s->avctx->rtp_callback) {
05192 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
05193 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
05194
05195 emms_c();
05196 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
05197 }
05198
05199 return 0;
05200 }
05201
05202 #define MERGE(field) dst->field += src->field; src->field=0
05203 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
05204 MERGE(me.scene_change_score);
05205 MERGE(me.mc_mb_var_sum_temp);
05206 MERGE(me.mb_var_sum_temp);
05207 }
05208
05209 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
05210 int i;
05211
05212 MERGE(dct_count[0]);
05213 MERGE(dct_count[1]);
05214 MERGE(mv_bits);
05215 MERGE(i_tex_bits);
05216 MERGE(p_tex_bits);
05217 MERGE(i_count);
05218 MERGE(f_count);
05219 MERGE(b_count);
05220 MERGE(skip_count);
05221 MERGE(misc_bits);
05222 MERGE(error_count);
05223 MERGE(padding_bug_score);
05224
05225 if(dst->avctx->noise_reduction){
05226 for(i=0; i<64; i++){
05227 MERGE(dct_error_sum[0][i]);
05228 MERGE(dct_error_sum[1][i]);
05229 }
05230 }
05231
05232 assert(put_bits_count(&src->pb) % 8 ==0);
05233 assert(put_bits_count(&dst->pb) % 8 ==0);
05234 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
05235 flush_put_bits(&dst->pb);
05236 }
05237
05238 static void encode_picture(MpegEncContext *s, int picture_number)
05239 {
05240 int i;
05241 int bits;
05242
05243 s->picture_number = picture_number;
05244
05245
05246 s->me.mb_var_sum_temp =
05247 s->me.mc_mb_var_sum_temp = 0;
05248
05249
05250
05251 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
05252 ff_set_mpeg4_time(s, s->picture_number);
05253
05254 s->me.scene_change_score=0;
05255
05256
05257
05258 if(s->pict_type==I_TYPE){
05259 if(s->msmpeg4_version >= 3) s->no_rounding=1;
05260 else s->no_rounding=0;
05261 }else if(s->pict_type!=B_TYPE){
05262 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
05263 s->no_rounding ^= 1;
05264 }
05265
05266 s->mb_intra=0;
05267 for(i=1; i<s->avctx->thread_count; i++){
05268 ff_update_duplicate_context(s->thread_context[i], s);
05269 }
05270
05271 ff_init_me(s);
05272
05273
05274 if(s->pict_type != I_TYPE){
05275 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
05276 s->lambda2= (s->lambda2* s->avctx->me_penalty_compensation + 128)>>8;
05277 if(s->pict_type != B_TYPE && s->avctx->me_threshold==0){
05278 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){
05279 s->avctx->execute(s->avctx, pre_estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
05280 }
05281 }
05282
05283 s->avctx->execute(s->avctx, estimate_motion_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
05284 }else {
05285
05286 for(i=0; i<s->mb_stride*s->mb_height; i++)
05287 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
05288
05289 if(!s->fixed_qscale){
05290
05291 s->avctx->execute(s->avctx, mb_var_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
05292 }
05293 }
05294 for(i=1; i<s->avctx->thread_count; i++){
05295 merge_context_after_me(s, s->thread_context[i]);
05296 }
05297 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
05298 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
05299 emms_c();
05300
05301 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == P_TYPE){
05302 s->pict_type= I_TYPE;
05303 for(i=0; i<s->mb_stride*s->mb_height; i++)
05304 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
05305
05306 }
05307
05308 if(!s->umvplus){
05309 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
05310 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
05311
05312 if(s->flags & CODEC_FLAG_INTERLACED_ME){
05313 int a,b;
05314 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I);
05315 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
05316 s->f_code= FFMAX(s->f_code, FFMAX(a,b));
05317 }
05318
05319 ff_fix_long_p_mvs(s);
05320 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
05321 if(s->flags & CODEC_FLAG_INTERLACED_ME){
05322 int j;
05323 for(i=0; i<2; i++){
05324 for(j=0; j<2; j++)
05325 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
05326 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
05327 }
05328 }
05329 }
05330
05331 if(s->pict_type==B_TYPE){
05332 int a, b;
05333
05334 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
05335 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
05336 s->f_code = FFMAX(a, b);
05337
05338 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
05339 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
05340 s->b_code = FFMAX(a, b);
05341
05342 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
05343 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
05344 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
05345 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
05346 if(s->flags & CODEC_FLAG_INTERLACED_ME){
05347 int dir, j;
05348 for(dir=0; dir<2; dir++){
05349 for(i=0; i<2; i++){
05350 for(j=0; j<2; j++){
05351 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
05352 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
05353 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
05354 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
05355 }
05356 }
05357 }
05358 }
05359 }
05360 }
05361
05362 if (!s->fixed_qscale)
05363 s->current_picture.quality = ff_rate_estimate_qscale(s);
05364
05365 if(s->adaptive_quant){
05366 switch(s->codec_id){
05367 case CODEC_ID_MPEG4:
05368 ff_clean_mpeg4_qscales(s);
05369 break;
05370 case CODEC_ID_H263:
05371 case CODEC_ID_H263P:
05372 case CODEC_ID_FLV1:
05373 ff_clean_h263_qscales(s);
05374 break;
05375 }
05376
05377 s->lambda= s->lambda_table[0];
05378
05379 }else
05380 s->lambda= s->current_picture.quality;
05381
05382 update_qscale(s);
05383
05384 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
05385 s->qscale= 3;
05386
05387 if (s->out_format == FMT_MJPEG) {
05388
05389 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
05390 for(i=1;i<64;i++){
05391 int j= s->dsp.idct_permutation[i];
05392
05393 s->intra_matrix[j] = clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3) & 0xFF;
05394 }
05395 convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
05396 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
05397 s->qscale= 8;
05398 }
05399
05400
05401 s->current_picture_ptr->key_frame=
05402 s->current_picture.key_frame= s->pict_type == I_TYPE;
05403 s->current_picture_ptr->pict_type=
05404 s->current_picture.pict_type= s->pict_type;
05405
05406 if(s->current_picture.key_frame)
05407 s->picture_in_gop_number=0;
05408
05409 s->last_bits= put_bits_count(&s->pb);
05410 switch(s->out_format) {
05411 case FMT_MJPEG:
05412 mjpeg_picture_header(s);
05413 break;
05414 #ifdef CONFIG_H261_ENCODER
05415 case FMT_H261:
05416 ff_h261_encode_picture_header(s, picture_number);
05417 break;
05418 #endif
05419 case FMT_H263:
05420 if (s->codec_id == CODEC_ID_WMV2)
05421 ff_wmv2_encode_picture_header(s, picture_number);
05422 else if (s->h263_msmpeg4)
05423 msmpeg4_encode_picture_header(s, picture_number);
05424 else if (s->h263_pred)
05425 mpeg4_encode_picture_header(s, picture_number);
05426 #ifdef CONFIG_RV10_ENCODER
05427 else if (s->codec_id == CODEC_ID_RV10)
05428 rv10_encode_picture_header(s, picture_number);
05429 #endif
05430 #ifdef CONFIG_RV20_ENCODER
05431 else if (s->codec_id == CODEC_ID_RV20)
05432 rv20_encode_picture_header(s, picture_number);
05433 #endif
05434 else if (s->codec_id == CODEC_ID_FLV1)
05435 ff_flv_encode_picture_header(s, picture_number);
05436 else
05437 h263_encode_picture_header(s, picture_number);
05438 break;
05439 case FMT_MPEG1:
05440 mpeg1_encode_picture_header(s, picture_number);
05441 break;
05442 case FMT_H264:
05443 break;
05444 default:
05445 assert(0);
05446 }
05447 bits= put_bits_count(&s->pb);
05448 s->header_bits= bits - s->last_bits;
05449
05450 for(i=1; i<s->avctx->thread_count; i++){
05451 update_duplicate_context_after_me(s->thread_context[i], s);
05452 }
05453 s->avctx->execute(s->avctx, encode_thread, (void**)&(s->thread_context[0]), NULL, s->avctx->thread_count);
05454 for(i=1; i<s->avctx->thread_count; i++){
05455 merge_context_after_encode(s, s->thread_context[i]);
05456 }
05457 emms_c();
05458 }
05459
05460 #endif //CONFIG_ENCODERS
05461
05462 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
05463 const int intra= s->mb_intra;
05464 int i;
05465
05466 s->dct_count[intra]++;
05467
05468 for(i=0; i<64; i++){
05469 int level= block[i];
05470
05471 if(level){
05472 if(level>0){
05473 s->dct_error_sum[intra][i] += level;
05474 level -= s->dct_offset[intra][i];
05475 if(level<0) level=0;
05476 }else{
05477 s->dct_error_sum[intra][i] -= level;
05478 level += s->dct_offset[intra][i];
05479 if(level>0) level=0;
05480 }
05481 block[i]= level;
05482 }
05483 }
05484 }
05485
05486 #ifdef CONFIG_ENCODERS
05487
05488 static int dct_quantize_trellis_c(MpegEncContext *s,
05489 DCTELEM *block, int n,
05490 int qscale, int *overflow){
05491 const int *qmat;
05492 const uint8_t *scantable= s->intra_scantable.scantable;
05493 const uint8_t *perm_scantable= s->intra_scantable.permutated;
05494 int max=0;
05495 unsigned int threshold1, threshold2;
05496 int bias=0;
05497 int run_tab[65];
05498 int level_tab[65];
05499 int score_tab[65];
05500 int survivor[65];
05501 int survivor_count;
05502 int last_run=0;
05503 int last_level=0;
05504 int last_score= 0;
05505 int last_i;
05506 int coeff[2][64];
05507 int coeff_count[64];
05508 int qmul, qadd, start_i, last_non_zero, i, dc;
05509 const int esc_length= s->ac_esc_length;
05510 uint8_t * length;
05511 uint8_t * last_length;
05512 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
05513
05514 s->dsp.fdct (block);
05515
05516 if(s->dct_error_sum)
05517 s->denoise_dct(s, block);
05518 qmul= qscale*16;
05519 qadd= ((qscale-1)|1)*8;
05520
05521 if (s->mb_intra) {
05522 int q;
05523 if (!s->h263_aic) {
05524 if (n < 4)
05525 q = s->y_dc_scale;
05526 else
05527 q = s->c_dc_scale;
05528 q = q << 3;
05529 } else{
05530
05531 q = 1 << 3;
05532 qadd=0;
05533 }
05534
05535
05536 block[0] = (block[0] + (q >> 1)) / q;
05537 start_i = 1;
05538 last_non_zero = 0;
05539 qmat = s->q_intra_matrix[qscale];
05540 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
05541 bias= 1<<(QMAT_SHIFT-1);
05542 length = s->intra_ac_vlc_length;
05543 last_length= s->intra_ac_vlc_last_length;
05544 } else {
05545 start_i = 0;
05546 last_non_zero = -1;
05547 qmat = s->q_inter_matrix[qscale];
05548 length = s->inter_ac_vlc_length;
05549 last_length= s->inter_ac_vlc_last_length;
05550 }
05551 last_i= start_i;
05552
05553 threshold1= (1<<QMAT_SHIFT) - bias - 1;
05554 threshold2= (threshold1<<1);
05555
05556 for(i=63; i>=start_i; i--) {
05557 const int j = scantable[i];
05558 int level = block[j] * qmat[j];
05559
05560 if(((unsigned)(level+threshold1))>threshold2){
05561 last_non_zero = i;
05562 break;
05563 }
05564 }
05565
05566 for(i=start_i; i<=last_non_zero; i++) {
05567 const int j = scantable[i];
05568 int level = block[j] * qmat[j];
05569
05570
05571
05572 if(((unsigned)(level+threshold1))>threshold2){
05573 if(level>0){
05574 level= (bias + level)>>QMAT_SHIFT;
05575 coeff[0][i]= level;
05576 coeff[1][i]= level-1;
05577
05578 }else{
05579 level= (bias - level)>>QMAT_SHIFT;
05580 coeff[0][i]= -level;
05581 coeff[1][i]= -level+1;
05582
05583 }
05584 coeff_count[i]= FFMIN(level, 2);
05585 assert(coeff_count[i]);
05586 max |=level;
05587 }else{
05588 coeff[0][i]= (level>>31)|1;
05589 coeff_count[i]= 1;
05590 }
05591 }
05592
05593 *overflow= s->max_qcoeff < max;
05594
05595 if(last_non_zero < start_i){
05596 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
05597 return last_non_zero;
05598 }
05599
05600 score_tab[start_i]= 0;
05601 survivor[0]= start_i;
05602 survivor_count= 1;
05603
05604 for(i=start_i; i<=last_non_zero; i++){
05605 int level_index, j;
05606 const int dct_coeff= ABS(block[ scantable[i] ]);
05607 const int zero_distoration= dct_coeff*dct_coeff;
05608 int best_score=256*256*256*120;
05609 for(level_index=0; level_index < coeff_count[i]; level_index++){
05610 int distoration;
05611 int level= coeff[level_index][i];
05612 const int alevel= ABS(level);
05613 int unquant_coeff;
05614
05615 assert(level);
05616
05617 if(s->out_format == FMT_H263){
05618 unquant_coeff= alevel*qmul + qadd;
05619 }else{
05620 j= s->dsp.idct_permutation[ scantable[i] ];
05621 if(s->mb_intra){
05622 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
05623 unquant_coeff = (unquant_coeff - 1) | 1;
05624 }else{
05625 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
05626 unquant_coeff = (unquant_coeff - 1) | 1;
05627 }
05628 unquant_coeff<<= 3;
05629 }
05630
05631 distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distoration;
05632 level+=64;
05633 if((level&(~127)) == 0){
05634 for(j=survivor_count-1; j>=0; j--){
05635 int run= i - survivor[j];
05636 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
05637 score += score_tab[i-run];
05638
05639 if(score < best_score){
05640 best_score= score;
05641 run_tab[i+1]= run;
05642 level_tab[i+1]= level-64;
05643 }
05644 }
05645
05646 if(s->out_format == FMT_H263){
05647 for(j=survivor_count-1; j>=0; j--){
05648 int run= i - survivor[j];
05649 int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
05650 score += score_tab[i-run];
05651 if(score < last_score){
05652 last_score= score;
05653 last_run= run;
05654 last_level= level-64;
05655 last_i= i+1;
05656 }
05657 }
05658 }
05659 }else{
05660 distoration += esc_length*lambda;
05661 for(j=survivor_count-1; j>=0; j--){
05662 int run= i - survivor[j];
05663 int score= distoration + score_tab[i-run];
05664
05665 if(score < best_score){
05666 best_score= score;
05667 run_tab[i+1]= run;
05668 level_tab[i+1]= level-64;
05669 }
05670 }
05671
05672 if(s->out_format == FMT_H263){
05673 for(j=survivor_count-1; j>=0; j--){
05674 int run= i - survivor[j];
05675 int score= distoration + score_tab[i-run];
05676 if(score < last_score){
05677 last_score= score;
05678 last_run= run;
05679 last_level= level-64;
05680 last_i= i+1;
05681 }
05682 }
05683 }
05684 }
05685 }
05686
05687 score_tab[i+1]= best_score;
05688
05689
05690 if(last_non_zero <= 27){
05691 for(; survivor_count; survivor_count--){
05692 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
05693 break;
05694 }
05695 }else{
05696 for(; survivor_count; survivor_count--){
05697 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
05698 break;
05699 }
05700 }
05701
05702 survivor[ survivor_count++ ]= i+1;
05703 }
05704
05705 if(s->out_format != FMT_H263){
05706 last_score= 256*256*256*120;
05707 for(i= survivor[0]; i<=last_non_zero + 1; i++){
05708 int score= score_tab[i];
05709 if(i) score += lambda*2;
05710
05711 if(score < last_score){
05712 last_score= score;
05713 last_i= i;
05714 last_level= level_tab[i];
05715 last_run= run_tab[i];
05716 }
05717 }
05718 }
05719
05720 s->coded_score[n] = last_score;
05721
05722 dc= ABS(block[0]);
05723 last_non_zero= last_i - 1;
05724 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
05725
05726 if(last_non_zero < start_i)
05727 return last_non_zero;
05728
05729 if(last_non_zero == 0 && start_i == 0){
05730 int best_level= 0;
05731 int best_score= dc * dc;
05732
05733 for(i=0; i<coeff_count[0]; i++){
05734 int level= coeff[i][0];
05735 int alevel= ABS(level);
05736 int unquant_coeff, score, distortion;
05737
05738 if(s->out_format == FMT_H263){
05739 unquant_coeff= (alevel*qmul + qadd)>>3;
05740 }else{
05741 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
05742 unquant_coeff = (unquant_coeff - 1) | 1;
05743 }
05744 unquant_coeff = (unquant_coeff + 4) >> 3;
05745 unquant_coeff<<= 3 + 3;
05746
05747 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
05748 level+=64;
05749 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
05750 else score= distortion + esc_length*lambda;
05751
05752 if(score < best_score){
05753 best_score= score;
05754 best_level= level - 64;
05755 }
05756 }
05757 block[0]= best_level;
05758 s->coded_score[n] = best_score - dc*dc;
05759 if(best_level == 0) return -1;
05760 else return last_non_zero;
05761 }
05762
05763 i= last_i;
05764 assert(last_level);
05765
05766 block[ perm_scantable[last_non_zero] ]= last_level;
05767 i -= last_run + 1;
05768
05769 for(; i>start_i; i -= run_tab[i] + 1){
05770 block[ perm_scantable[i-1] ]= level_tab[i];
05771 }
05772
05773 return last_non_zero;
05774 }
05775
05776
05777 static int16_t basis[64][64];
05778
05779 static void build_basis(uint8_t *perm){
05780 int i, j, x, y;
05781 emms_c();
05782 for(i=0; i<8; i++){
05783 for(j=0; j<8; j++){
05784 for(y=0; y<8; y++){
05785 for(x=0; x<8; x++){
05786 double s= 0.25*(1<<BASIS_SHIFT);
05787 int index= 8*i + j;
05788 int perm_index= perm[index];
05789 if(i==0) s*= sqrt(0.5);
05790 if(j==0) s*= sqrt(0.5);
05791 basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
05792 }
05793 }
05794 }
05795 }
05796 }
05797
05798 static int dct_quantize_refine(MpegEncContext *s,
05799 DCTELEM *block, int16_t *weight, DCTELEM *orig,
05800 int n, int qscale){
05801 int16_t rem[64];
05802 DCTELEM d1[64] __align16;
05803 const int *qmat;
05804 const uint8_t *scantable= s->intra_scantable.scantable;
05805 const uint8_t *perm_scantable= s->intra_scantable.permutated;
05806
05807
05808 int run_tab[65];
05809 int prev_run=0;
05810 int prev_level=0;
05811 int qmul, qadd, start_i, last_non_zero, i, dc;
05812 uint8_t * length;
05813 uint8_t * last_length;
05814 int lambda;
05815 int rle_index, run, q, sum;
05816 #ifdef REFINE_STATS
05817 static int count=0;
05818 static int after_last=0;
05819 static int to_zero=0;
05820 static int from_zero=0;
05821 static int raise=0;
05822 static int lower=0;
05823 static int messed_sign=0;
05824 #endif
05825
05826 if(basis[0][0] == 0)
05827 build_basis(s->dsp.idct_permutation);
05828
05829 qmul= qscale*2;
05830 qadd= (qscale-1)|1;
05831 if (s->mb_intra) {
05832 if (!s->h263_aic) {
05833 if (n < 4)
05834 q = s->y_dc_scale;
05835 else
05836 q = s->c_dc_scale;
05837 } else{
05838
05839 q = 1;
05840 qadd=0;
05841 }
05842 q <<= RECON_SHIFT-3;
05843
05844 dc= block[0]*q;
05845
05846 start_i = 1;
05847 qmat = s->q_intra_matrix[qscale];
05848
05849
05850 length = s->intra_ac_vlc_length;
05851 last_length= s->intra_ac_vlc_last_length;
05852 } else {
05853 dc= 0;
05854 start_i = 0;
05855 qmat = s->q_inter_matrix[qscale];
05856 length = s->inter_ac_vlc_length;
05857 last_length= s->inter_ac_vlc_last_length;
05858 }
05859 last_non_zero = s->block_last_index[n];
05860
05861 #ifdef REFINE_STATS
05862 {START_TIMER
05863 #endif
05864 dc += (1<<(RECON_SHIFT-1));
05865 for(i=0; i<64; i++){
05866 rem[i]= dc - (orig[i]<<RECON_SHIFT);
05867 }
05868 #ifdef REFINE_STATS
05869 STOP_TIMER("memset rem[]")}
05870 #endif
05871 sum=0;
05872 for(i=0; i<64; i++){
05873 int one= 36;
05874 int qns=4;
05875 int w;
05876
05877 w= ABS(weight[i]) + qns*one;
05878 w= 15 + (48*qns*one + w/2)/w;
05879
05880 weight[i] = w;
05881
05882
05883 assert(w>0);
05884 assert(w<(1<<6));
05885 sum += w*w;
05886 }
05887 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
05888 #ifdef REFINE_STATS
05889 {START_TIMER
05890 #endif
05891 run=0;
05892 rle_index=0;
05893 for(i=start_i; i<=last_non_zero; i++){
05894 int j= perm_scantable[i];
05895 const int level= block[j];
05896 int coeff;
05897
05898 if(level){
05899 if(level<0) coeff= qmul*level - qadd;
05900 else coeff= qmul*level + qadd;
05901 run_tab[rle_index++]=run;
05902 run=0;
05903
05904 s->dsp.add_8x8basis(rem, basis[j], coeff);
05905 }else{
05906 run++;
05907 }
05908 }
05909 #ifdef REFINE_STATS
05910 if(last_non_zero>0){
05911 STOP_TIMER("init rem[]")
05912 }
05913 }
05914
05915 {START_TIMER
05916 #endif
05917 for(;;){
05918 int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
05919 int best_coeff=0;
05920 int best_change=0;
05921 int run2, best_unquant_change=0, analyze_gradient;
05922 #ifdef REFINE_STATS
05923 {START_TIMER
05924 #endif
05925 analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
05926
05927 if(analyze_gradient){
05928 #ifdef REFINE_STATS
05929 {START_TIMER
05930 #endif
05931 for(i=0; i<64; i++){
05932 int w= weight[i];
05933
05934 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
05935 }
05936 #ifdef REFINE_STATS
05937 STOP_TIMER("rem*w*w")}
05938 {START_TIMER
05939 #endif
05940 s->dsp.fdct(d1);
05941 #ifdef REFINE_STATS
05942 STOP_TIMER("dct")}
05943 #endif
05944 }
05945
05946 if(start_i){
05947 const int level= block[0];
05948 int change, old_coeff;
05949
05950 assert(s->mb_intra);
05951
05952 old_coeff= q*level;
05953
05954 for(change=-1; change<=1; change+=2){
05955 int new_level= level + change;
05956 int score, new_coeff;
05957
05958 new_coeff= q*new_level;
05959 if(new_coeff >= 2048 || new_coeff < 0)
05960 continue;
05961
05962 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
05963 if(score<best_score){
05964 best_score= score;
05965 best_coeff= 0;
05966 best_change= change;
05967 best_unquant_change= new_coeff - old_coeff;
05968 }
05969 }
05970 }
05971
05972 run=0;
05973 rle_index=0;
05974 run2= run_tab[rle_index++];
05975 prev_level=0;
05976 prev_run=0;
05977
05978 for(i=start_i; i<64; i++){
05979 int j= perm_scantable[i];
05980 const int level= block[j];
05981 int change, old_coeff;
05982
05983 if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
05984 break;
05985
05986 if(level){
05987 if(level<0) old_coeff= qmul*level - qadd;
05988 else old_coeff= qmul*level + qadd;
05989 run2= run_tab[rle_index++];
05990 }else{
05991 old_coeff=0;
05992 run2--;
05993 assert(run2>=0 || i >= last_non_zero );
05994 }
05995
05996 for(change=-1; change<=1; change+=2){
05997 int new_level= level + change;
05998 int score, new_coeff, unquant_change;
05999
06000 score=0;
06001 if(s->avctx->quantizer_noise_shaping < 2 && ABS(new_level) > ABS(level))
06002 continue;
06003
06004 if(new_level){
06005 if(new_level<0) new_coeff= qmul*new_level - qadd;
06006 else new_coeff= qmul*new_level + qadd;
06007 if(new_coeff >= 2048 || new_coeff <= -2048)
06008 continue;
06009
06010
06011 if(level){
06012 if(level < 63 && level > -63){
06013 if(i < last_non_zero)
06014 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
06015 - length[UNI_AC_ENC_INDEX(run, level+64)];
06016 else
06017 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
06018 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
06019 }
06020 }else{
06021 assert(ABS(new_level)==1);
06022
06023 if(analyze_gradient){
06024 int g= d1[ scantable[i] ];
06025 if(g && (g^new_level) >= 0)
06026 continue;
06027 }
06028
06029 if(i < last_non_zero){
06030 int next_i= i + run2 + 1;
06031 int next_level= block[ perm_scantable[next_i] ] + 64;
06032
06033 if(next_level&(~127))
06034 next_level= 0;
06035
06036 if(next_i < last_non_zero)
06037 score += length[UNI_AC_ENC_INDEX(run, 65)]
06038 + length[UNI_AC_ENC_INDEX(run2, next_level)]
06039 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
06040 else
06041 score += length[UNI_AC_ENC_INDEX(run, 65)]
06042 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
06043 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
06044 }else{
06045 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
06046 if(prev_level){
06047 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
06048 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
06049 }
06050 }
06051 }
06052 }else{
06053 new_coeff=0;
06054 assert(ABS(level)==1);
06055
06056 if(i < last_non_zero){
06057 int next_i= i + run2 + 1;
06058 int next_level= block[ perm_scantable[next_i] ] + 64;
06059
06060 if(next_level&(~127))
06061 next_level= 0;
06062
06063 if(next_i < last_non_zero)
06064 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
06065 - length[UNI_AC_ENC_INDEX(run2, next_level)]
06066 - length[UNI_AC_ENC_INDEX(run, 65)];
06067 else
06068 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
06069 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
06070 - length[UNI_AC_ENC_INDEX(run, 65)];
06071 }else{
06072 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
06073 if(prev_level){
06074 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
06075 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
06076 }
06077 }
06078 }
06079
06080 score *= lambda;
06081
06082 unquant_change= new_coeff - old_coeff;
06083 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
06084
06085 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
06086 if(score<best_score){
06087 best_score= score;
06088 best_coeff= i;
06089 best_change= change;
06090 best_unquant_change= unquant_change;
06091 }
06092 }
06093 if(level){
06094 prev_level= level + 64;
06095 if(prev_level&(~127))
06096 prev_level= 0;
06097 prev_run= run;
06098 run=0;
06099 }else{
06100 run++;
06101 }
06102 }
06103 #ifdef REFINE_STATS
06104 STOP_TIMER("iterative step")}
06105 #endif
06106
06107 if(best_change){
06108 int j= perm_scantable[ best_coeff ];
06109
06110 block[j] += best_change;
06111
06112 if(best_coeff > last_non_zero){
06113 last_non_zero= best_coeff;
06114 assert(block[j]);
06115 #ifdef REFINE_STATS
06116 after_last++;
06117 #endif
06118 }else{
06119 #ifdef REFINE_STATS
06120 if(block[j]){
06121 if(block[j] - best_change){
06122 if(ABS(block[j]) > ABS(block[j] - best_change)){
06123 raise++;
06124 }else{
06125 lower++;
06126 }
06127 }else{
06128 from_zero++;
06129 }
06130 }else{
06131 to_zero++;
06132 }
06133 #endif
06134 for(; last_non_zero>=start_i; last_non_zero--){
06135 if(block[perm_scantable[last_non_zero]])
06136 break;
06137 }
06138 }
06139 #ifdef REFINE_STATS
06140 count++;
06141 if(256*256*256*64 % count == 0){
06142 printf("after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero, raise, lower, messed_sign, s->mb_x, s->mb_y, s->picture_number);
06143 }
06144 #endif
06145 run=0;
06146 rle_index=0;
06147 for(i=start_i; i<=last_non_zero; i++){
06148 int j= perm_scantable[i];
06149 const int level= block[j];
06150
06151 if(level){
06152 run_tab[rle_index++]=run;
06153 run=0;
06154 }else{
06155 run++;
06156 }
06157 }
06158
06159 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
06160 }else{
06161 break;
06162 }
06163 }
06164 #ifdef REFINE_STATS
06165 if(last_non_zero>0){
06166 STOP_TIMER("iterative search")
06167 }
06168 }
06169 #endif
06170
06171 return last_non_zero;
06172 }
06173
06174 static int dct_quantize_c(MpegEncContext *s,
06175 DCTELEM *block, int n,
06176 int qscale, int *overflow)
06177 {
06178 int i, j, level, last_non_zero, q, start_i;
06179 const int *qmat;
06180 const uint8_t *scantable= s->intra_scantable.scantable;
06181 int bias;
06182 int max=0;
06183 unsigned int threshold1, threshold2;
06184
06185 s->dsp.fdct (block);
06186
06187 if(s->dct_error_sum)
06188 s->denoise_dct(s, block);
06189
06190 if (s->mb_intra) {
06191 if (!s->h263_aic) {
06192 if (n < 4)
06193 q = s->y_dc_scale;
06194 else
06195 q = s->c_dc_scale;
06196 q = q << 3;
06197 } else
06198
06199 q = 1 << 3;
06200
06201
06202 block[0] = (block[0] + (q >> 1)) / q;
06203 start_i = 1;
06204 last_non_zero = 0;
06205 qmat = s->q_intra_matrix[qscale];
06206 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
06207 } else {
06208 start_i = 0;
06209 last_non_zero = -1;
06210 qmat = s->q_inter_matrix[qscale];
06211 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
06212 }
06213 threshold1= (1<<QMAT_SHIFT) - bias - 1;
06214 threshold2= (threshold1<<1);
06215 for(i=63;i>=start_i;i--) {
06216 j = scantable[i];
06217 level = block[j] * qmat[j];
06218
06219 if(((unsigned)(level+threshold1))>threshold2){
06220 last_non_zero = i;
06221 break;
06222 }else{
06223 block[j]=0;
06224 }
06225 }
06226 for(i=start_i; i<=last_non_zero; i++) {
06227 j = scantable[i];
06228 level = block[j] * qmat[j];
06229
06230
06231
06232 if(((unsigned)(level+threshold1))>threshold2){
06233 if(level>0){
06234 level= (bias + level)>>QMAT_SHIFT;
06235 block[j]= level;
06236 }else{
06237 level= (bias - level)>>QMAT_SHIFT;
06238 block[j]= -level;
06239 }
06240 max |=level;
06241 }else{
06242 block[j]=0;
06243 }
06244 }
06245 *overflow= s->max_qcoeff < max;
06246
06247
06248 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
06249 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
06250
06251 return last_non_zero;
06252 }
06253
06254 #endif //CONFIG_ENCODERS
06255
06256 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
06257 DCTELEM *block, int n, int qscale)
06258 {
06259 int i, level, nCoeffs;
06260 const uint16_t *quant_matrix;
06261
06262 nCoeffs= s->block_last_index[n];
06263
06264 if (n < 4)
06265 block[0] = block[0] * s->y_dc_scale;
06266 else
06267 block[0] = block[0] * s->c_dc_scale;
06268
06269 quant_matrix = s->intra_matrix;
06270 for(i=1;i<=nCoeffs;i++) {
06271 int j= s->intra_scantable.permutated[i];
06272 level = block[j];
06273 if (level) {
06274 if (level < 0) {
06275 level = -level;
06276 level = (int)(level * qscale * quant_matrix[j]) >> 3;
06277 level = (level - 1) | 1;
06278 level = -level;
06279 } else {
06280 level = (int)(level * qscale * quant_matrix[j]) >> 3;
06281 level = (level - 1) | 1;
06282 }
06283 block[j] = level;
06284 }
06285 }
06286 }
06287
06288 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
06289 DCTELEM *block, int n, int qscale)
06290 {
06291 int i, level, nCoeffs;
06292 const uint16_t *quant_matrix;
06293
06294 nCoeffs= s->block_last_index[n];
06295
06296 quant_matrix = s->inter_matrix;
06297 for(i=0; i<=nCoeffs; i++) {
06298 int j= s->intra_scantable.permutated[i];
06299 level = block[j];
06300 if (level) {
06301 if (level < 0) {
06302 level = -level;
06303 level = (((level << 1) + 1) * qscale *
06304 ((int) (quant_matrix[j]))) >> 4;
06305 level = (level - 1) | 1;
06306 level = -level;
06307 } else {
06308 level = (((level << 1) + 1) * qscale *
06309 ((int) (quant_matrix[j]))) >> 4;
06310 level = (level - 1) | 1;
06311 }
06312 block[j] = level;
06313 }
06314 }
06315 }
06316
06317 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
06318 DCTELEM *block, int n, int qscale)
06319 {
06320 int i, level, nCoeffs;
06321 const uint16_t *quant_matrix;
06322
06323 if(s->alternate_scan) nCoeffs= 63;
06324 else nCoeffs= s->block_last_index[n];
06325
06326 if (n < 4)
06327 block[0] = block[0] * s->y_dc_scale;
06328 else
06329 block[0] = block[0] * s->c_dc_scale;
06330 quant_matrix = s->intra_matrix;
06331 for(i=1;i<=nCoeffs;i++) {
06332 int j= s->intra_scantable.permutated[i];
06333 level = block[j];
06334 if (level) {
06335 if (level < 0) {
06336 level = -level;
06337 level = (int)(level * qscale * quant_matrix[j]) >> 3;
06338 level = -level;
06339 } else {
06340 level = (int)(level * qscale * quant_matrix[j]) >> 3;
06341 }
06342 block[j] = level;
06343 }
06344 }
06345 }
06346
06347 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
06348 DCTELEM *block, int n, int qscale)
06349 {
06350 int i, level, nCoeffs;
06351 const uint16_t *quant_matrix;
06352 int sum=-1;
06353
06354 if(s->alternate_scan) nCoeffs= 63;
06355 else nCoeffs= s->block_last_index[n];
06356
06357 quant_matrix = s->inter_matrix;
06358 for(i=0; i<=nCoeffs; i++) {
06359 int j= s->intra_scantable.permutated[i];
06360 level = block[j];
06361 if (level) {
06362 if (level < 0) {
06363 level = -level;
06364 level = (((level << 1) + 1) * qscale *
06365 ((int) (quant_matrix[j]))) >> 4;
06366 level = -level;
06367 } else {
06368 level = (((level << 1) + 1) * qscale *
06369 ((int) (quant_matrix[j]))) >> 4;
06370 }
06371 block[j] = level;
06372 sum+=level;
06373 }
06374 }
06375 block[63]^=sum&1;
06376 }
06377
06378 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
06379 DCTELEM *block, int n, int qscale)
06380 {
06381 int i, level, qmul, qadd;
06382 int nCoeffs;
06383
06384 assert(s->block_last_index[n]>=0);
06385
06386 qmul = qscale << 1;
06387
06388 if (!s->h263_aic) {
06389 if (n < 4)
06390 block[0] = block[0] * s->y_dc_scale;
06391 else
06392 block[0] = block[0] * s->c_dc_scale;
06393 qadd = (qscale - 1) | 1;
06394 }else{
06395 qadd = 0;
06396 }
06397 if(s->ac_pred)
06398 nCoeffs=63;
06399 else
06400 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
06401
06402 for(i=1; i<=nCoeffs; i++) {
06403 level = block[i];
06404 if (level) {
06405 if (level < 0) {
06406 level = level * qmul - qadd;
06407 } else {
06408 level = level * qmul + qadd;
06409 }
06410 block[i] = level;
06411 }
06412 }
06413 }
06414
06415 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
06416 DCTELEM *block, int n, int qscale)
06417 {
06418 int i, level, qmul, qadd;
06419 int nCoeffs;
06420
06421 assert(s->block_last_index[n]>=0);
06422
06423 qadd = (qscale - 1) | 1;
06424 qmul = qscale << 1;
06425
06426 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
06427
06428 for(i=0; i<=nCoeffs; i++) {
06429 level = block[i];
06430 if (level) {
06431 if (level < 0) {
06432 level = level * qmul - qadd;
06433 } else {
06434 level = level * qmul + qadd;
06435 }
06436 block[i] = level;
06437 }
06438 }
06439 }
06440
06441 #ifdef CONFIG_ENCODERS
06442 AVCodec h263_encoder = {
06443 "h263",
06444 CODEC_TYPE_VIDEO,
06445 CODEC_ID_H263,
06446 sizeof(MpegEncContext),
06447 MPV_encode_init,
06448 MPV_encode_picture,
06449 MPV_encode_end,
06450 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06451 };
06452
06453 AVCodec h263p_encoder = {
06454 "h263p",
06455 CODEC_TYPE_VIDEO,
06456 CODEC_ID_H263P,
06457 sizeof(MpegEncContext),
06458 MPV_encode_init,
06459 MPV_encode_picture,
06460 MPV_encode_end,
06461 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06462 };
06463
06464 AVCodec flv_encoder = {
06465 "flv",
06466 CODEC_TYPE_VIDEO,
06467 CODEC_ID_FLV1,
06468 sizeof(MpegEncContext),
06469 MPV_encode_init,
06470 MPV_encode_picture,
06471 MPV_encode_end,
06472 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06473 };
06474
06475 AVCodec rv10_encoder = {
06476 "rv10",
06477 CODEC_TYPE_VIDEO,
06478 CODEC_ID_RV10,
06479 sizeof(MpegEncContext),
06480 MPV_encode_init,
06481 MPV_encode_picture,
06482 MPV_encode_end,
06483 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06484 };
06485
06486 AVCodec rv20_encoder = {
06487 "rv20",
06488 CODEC_TYPE_VIDEO,
06489 CODEC_ID_RV20,
06490 sizeof(MpegEncContext),
06491 MPV_encode_init,
06492 MPV_encode_picture,
06493 MPV_encode_end,
06494 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06495 };
06496
06497 AVCodec mpeg4_encoder = {
06498 "mpeg4",
06499 CODEC_TYPE_VIDEO,
06500 CODEC_ID_MPEG4,
06501 sizeof(MpegEncContext),
06502 MPV_encode_init,
06503 MPV_encode_picture,
06504 MPV_encode_end,
06505 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06506 .capabilities= CODEC_CAP_DELAY,
06507 };
06508
06509 AVCodec msmpeg4v1_encoder = {
06510 "msmpeg4v1",
06511 CODEC_TYPE_VIDEO,
06512 CODEC_ID_MSMPEG4V1,
06513 sizeof(MpegEncContext),
06514 MPV_encode_init,
06515 MPV_encode_picture,
06516 MPV_encode_end,
06517 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06518 };
06519
06520 AVCodec msmpeg4v2_encoder = {
06521 "msmpeg4v2",
06522 CODEC_TYPE_VIDEO,
06523 CODEC_ID_MSMPEG4V2,
06524 sizeof(MpegEncContext),
06525 MPV_encode_init,
06526 MPV_encode_picture,
06527 MPV_encode_end,
06528 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06529 };
06530
06531 AVCodec msmpeg4v3_encoder = {
06532 "msmpeg4",
06533 CODEC_TYPE_VIDEO,
06534 CODEC_ID_MSMPEG4V3,
06535 sizeof(MpegEncContext),
06536 MPV_encode_init,
06537 MPV_encode_picture,
06538 MPV_encode_end,
06539 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06540 };
06541
06542 AVCodec wmv1_encoder = {
06543 "wmv1",
06544 CODEC_TYPE_VIDEO,
06545 CODEC_ID_WMV1,
06546 sizeof(MpegEncContext),
06547 MPV_encode_init,
06548 MPV_encode_picture,
06549 MPV_encode_end,
06550 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
06551 };
06552
06553 AVCodec mjpeg_encoder = {
06554 "mjpeg",
06555 CODEC_TYPE_VIDEO,
06556 CODEC_ID_MJPEG,
06557 sizeof(MpegEncContext),
06558 MPV_encode_init,
06559 MPV_encode_picture,
06560 MPV_encode_end,
06561 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUVJ420P, -1},
06562 };
06563
06564 #endif //CONFIG_ENCODERS