00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include "libavutil/intmath.h"
00031 #include "avcodec.h"
00032 #include "dsputil.h"
00033 #include "mpegvideo.h"
00034 #include "mpegvideo_common.h"
00035 #include "h263.h"
00036 #include "mjpegenc.h"
00037 #include "msmpeg4.h"
00038 #include "faandct.h"
00039 #include "aandcttab.h"
00040 #include "flv.h"
00041 #include "mpeg4video.h"
00042 #include "internal.h"
00043 #include <limits.h>
00044
00045
00046
00047
00048 static int encode_picture(MpegEncContext *s, int picture_number);
00049 static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
00050 static int sse_mb(MpegEncContext *s);
00051
00052
00053
00054
00055
00056
00057 static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
00058 static uint8_t default_fcode_tab[MAX_MV*2+1];
00059
00060 void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
00061 const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
00062 {
00063 int qscale;
00064 int shift=0;
00065
00066 for(qscale=qmin; qscale<=qmax; qscale++){
00067 int i;
00068 if (dsp->fdct == ff_jpeg_fdct_islow
00069 #ifdef FAAN_POSTSCALE
00070 || dsp->fdct == ff_faandct
00071 #endif
00072 ) {
00073 for(i=0;i<64;i++) {
00074 const int j= dsp->idct_permutation[i];
00075
00076
00077
00078
00079
00080 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
00081 (qscale * quant_matrix[j]));
00082 }
00083 } else if (dsp->fdct == fdct_ifast
00084 #ifndef FAAN_POSTSCALE
00085 || dsp->fdct == ff_faandct
00086 #endif
00087 ) {
00088 for(i=0;i<64;i++) {
00089 const int j= dsp->idct_permutation[i];
00090
00091
00092
00093
00094
00095 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
00096 (ff_aanscales[i] * qscale * quant_matrix[j]));
00097 }
00098 } else {
00099 for(i=0;i<64;i++) {
00100 const int j= dsp->idct_permutation[i];
00101
00102
00103
00104
00105
00106 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
00107
00108 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
00109
00110 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
00111 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
00112 }
00113 }
00114
00115 for(i=intra; i<64; i++){
00116 int64_t max= 8191;
00117 if (dsp->fdct == fdct_ifast
00118 #ifndef FAAN_POSTSCALE
00119 || dsp->fdct == ff_faandct
00120 #endif
00121 ) {
00122 max = (8191LL*ff_aanscales[i]) >> 14;
00123 }
00124 while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
00125 shift++;
00126 }
00127 }
00128 }
00129 if(shift){
00130 av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger than %d, overflows possible\n", QMAT_SHIFT - shift);
00131 }
00132 }
00133
00134 static inline void update_qscale(MpegEncContext *s){
00135 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
00136 s->qscale= av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
00137
00138 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
00139 }
00140
00141 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix){
00142 int i;
00143
00144 if(matrix){
00145 put_bits(pb, 1, 1);
00146 for(i=0;i<64;i++) {
00147 put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
00148 }
00149 }else
00150 put_bits(pb, 1, 0);
00151 }
00152
00156 void ff_init_qscale_tab(MpegEncContext *s){
00157 int8_t * const qscale_table= s->current_picture.qscale_table;
00158 int i;
00159
00160 for(i=0; i<s->mb_num; i++){
00161 unsigned int lam= s->lambda_table[ s->mb_index2xy[i] ];
00162 int qp= (lam*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
00163 qscale_table[ s->mb_index2xy[i] ]= av_clip(qp, s->avctx->qmin, s->avctx->qmax);
00164 }
00165 }
00166
00167 static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
00168 int i;
00169
00170 dst->pict_type = src->pict_type;
00171 dst->quality = src->quality;
00172 dst->coded_picture_number = src->coded_picture_number;
00173 dst->display_picture_number = src->display_picture_number;
00174
00175 dst->pts = src->pts;
00176 dst->interlaced_frame = src->interlaced_frame;
00177 dst->top_field_first = src->top_field_first;
00178
00179 if(s->avctx->me_threshold){
00180 if(!src->motion_val[0])
00181 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
00182 if(!src->mb_type)
00183 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
00184 if(!src->ref_index[0])
00185 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
00186 if(src->motion_subsample_log2 != dst->motion_subsample_log2)
00187 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
00188 src->motion_subsample_log2, dst->motion_subsample_log2);
00189
00190 memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
00191
00192 for(i=0; i<2; i++){
00193 int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
00194 int height= ((16*s->mb_height)>>src->motion_subsample_log2);
00195
00196 if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
00197 memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
00198 }
00199 if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
00200 memcpy(dst->ref_index[i], src->ref_index[i], s->mb_stride*4*s->mb_height*sizeof(int8_t));
00201 }
00202 }
00203 }
00204 }
00205
00206 static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
00207 #define COPY(a) dst->a= src->a
00208 COPY(pict_type);
00209 COPY(current_picture);
00210 COPY(f_code);
00211 COPY(b_code);
00212 COPY(qscale);
00213 COPY(lambda);
00214 COPY(lambda2);
00215 COPY(picture_in_gop_number);
00216 COPY(gop_picture_number);
00217 COPY(frame_pred_frame_dct);
00218 COPY(progressive_frame);
00219 COPY(partitioned_frame);
00220 #undef COPY
00221 }
00222
00227 static void MPV_encode_defaults(MpegEncContext *s){
00228 int i;
00229 MPV_common_defaults(s);
00230
00231 for(i=-16; i<16; i++){
00232 default_fcode_tab[i + MAX_MV]= 1;
00233 }
00234 s->me.mv_penalty= default_mv_penalty;
00235 s->fcode_tab= default_fcode_tab;
00236 }
00237
00238
00239 av_cold int MPV_encode_init(AVCodecContext *avctx)
00240 {
00241 MpegEncContext *s = avctx->priv_data;
00242 int i;
00243 int chroma_h_shift, chroma_v_shift;
00244
00245 MPV_encode_defaults(s);
00246
00247 switch (avctx->codec_id) {
00248 case CODEC_ID_MPEG2VIDEO:
00249 if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P){
00250 av_log(avctx, AV_LOG_ERROR, "only YUV420 and YUV422 are supported\n");
00251 return -1;
00252 }
00253 break;
00254 case CODEC_ID_LJPEG:
00255 case CODEC_ID_MJPEG:
00256 if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P && avctx->pix_fmt != PIX_FMT_RGB32 &&
00257 ((avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P) || avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL)){
00258 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
00259 return -1;
00260 }
00261 break;
00262 default:
00263 if(avctx->pix_fmt != PIX_FMT_YUV420P){
00264 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
00265 return -1;
00266 }
00267 }
00268
00269 switch (avctx->pix_fmt) {
00270 case PIX_FMT_YUVJ422P:
00271 case PIX_FMT_YUV422P:
00272 s->chroma_format = CHROMA_422;
00273 break;
00274 case PIX_FMT_YUVJ420P:
00275 case PIX_FMT_YUV420P:
00276 default:
00277 s->chroma_format = CHROMA_420;
00278 break;
00279 }
00280
00281 s->bit_rate = avctx->bit_rate;
00282 s->width = avctx->width;
00283 s->height = avctx->height;
00284 if(avctx->gop_size > 600 && avctx->strict_std_compliance>FF_COMPLIANCE_EXPERIMENTAL){
00285 av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
00286 avctx->gop_size=600;
00287 }
00288 s->gop_size = avctx->gop_size;
00289 s->avctx = avctx;
00290 s->flags= avctx->flags;
00291 s->flags2= avctx->flags2;
00292 s->max_b_frames= avctx->max_b_frames;
00293 s->codec_id= avctx->codec->id;
00294 s->luma_elim_threshold = avctx->luma_elim_threshold;
00295 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
00296 s->strict_std_compliance= avctx->strict_std_compliance;
00297 s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
00298 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
00299 s->mpeg_quant= avctx->mpeg_quant;
00300 s->rtp_mode= !!avctx->rtp_payload_size;
00301 s->intra_dc_precision= avctx->intra_dc_precision;
00302 s->user_specified_pts = AV_NOPTS_VALUE;
00303
00304 if (s->gop_size <= 1) {
00305 s->intra_only = 1;
00306 s->gop_size = 12;
00307 } else {
00308 s->intra_only = 0;
00309 }
00310
00311 s->me_method = avctx->me_method;
00312
00313
00314 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
00315
00316 s->adaptive_quant= ( s->avctx->lumi_masking
00317 || s->avctx->dark_masking
00318 || s->avctx->temporal_cplx_masking
00319 || s->avctx->spatial_cplx_masking
00320 || s->avctx->p_masking
00321 || s->avctx->border_masking
00322 || (s->flags&CODEC_FLAG_QP_RD))
00323 && !s->fixed_qscale;
00324
00325 s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
00326 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
00327 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
00328 s->intra_vlc_format= !!(s->flags2 & CODEC_FLAG2_INTRA_VLC);
00329 s->q_scale_type= !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT);
00330
00331 if(avctx->rc_max_rate && !avctx->rc_buffer_size){
00332 av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
00333 return -1;
00334 }
00335
00336 if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
00337 av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
00338 }
00339
00340 if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
00341 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
00342 return -1;
00343 }
00344
00345 if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
00346 av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
00347 return -1;
00348 }
00349
00350 if(avctx->rc_max_rate && avctx->rc_max_rate == avctx->bit_rate && avctx->rc_max_rate != avctx->rc_min_rate){
00351 av_log(avctx, AV_LOG_INFO, "impossible bitrate constraints, this will fail\n");
00352 }
00353
00354 if(avctx->rc_buffer_size && avctx->bit_rate*av_q2d(avctx->time_base) > avctx->rc_buffer_size){
00355 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
00356 return -1;
00357 }
00358
00359 if(avctx->bit_rate*av_q2d(avctx->time_base) > avctx->bit_rate_tolerance){
00360 av_log(avctx, AV_LOG_ERROR, "bitrate tolerance too small for bitrate\n");
00361 return -1;
00362 }
00363
00364 if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
00365 && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
00366 && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
00367
00368 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");
00369 }
00370
00371 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
00372 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
00373 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
00374 return -1;
00375 }
00376
00377 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
00378 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n");
00379 return -1;
00380 }
00381
00382 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
00383 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
00384 return -1;
00385 }
00386
00387 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
00388 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
00389 return -1;
00390 }
00391
00392 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
00393 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
00394 return -1;
00395 }
00396
00397 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
00398 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
00399 return -1;
00400 }
00401
00402 if ((s->codec_id == CODEC_ID_MPEG4 || s->codec_id == CODEC_ID_H263 ||
00403 s->codec_id == CODEC_ID_H263P) &&
00404 (avctx->sample_aspect_ratio.num > 255 || avctx->sample_aspect_ratio.den > 255)) {
00405 av_log(avctx, AV_LOG_ERROR, "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
00406 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
00407 return -1;
00408 }
00409
00410 if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
00411 && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
00412 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
00413 return -1;
00414 }
00415
00416 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){
00417 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n");
00418 return -1;
00419 }
00420
00421 if((s->flags & CODEC_FLAG_CBP_RD) && !avctx->trellis){
00422 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
00423 return -1;
00424 }
00425
00426 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
00427 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
00428 return -1;
00429 }
00430
00431 if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
00432 av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection are not supported yet, set threshold to 1000000000\n");
00433 return -1;
00434 }
00435
00436 if((s->flags2 & CODEC_FLAG2_INTRA_VLC) && s->codec_id != CODEC_ID_MPEG2VIDEO){
00437 av_log(avctx, AV_LOG_ERROR, "intra vlc table not supported by codec\n");
00438 return -1;
00439 }
00440
00441 if(s->flags & CODEC_FLAG_LOW_DELAY){
00442 if (s->codec_id != CODEC_ID_MPEG2VIDEO){
00443 av_log(avctx, AV_LOG_ERROR, "low delay forcing is only available for mpeg2\n");
00444 return -1;
00445 }
00446 if (s->max_b_frames != 0){
00447 av_log(avctx, AV_LOG_ERROR, "b frames cannot be used with low delay\n");
00448 return -1;
00449 }
00450 }
00451
00452 if(s->q_scale_type == 1){
00453 if(s->codec_id != CODEC_ID_MPEG2VIDEO){
00454 av_log(avctx, AV_LOG_ERROR, "non linear quant is only available for mpeg2\n");
00455 return -1;
00456 }
00457 if(avctx->qmax > 12){
00458 av_log(avctx, AV_LOG_ERROR, "non linear quant only supports qmax <= 12 currently\n");
00459 return -1;
00460 }
00461 }
00462
00463 if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
00464 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
00465 && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
00466 av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
00467 return -1;
00468 }
00469
00470 if(s->avctx->thread_count < 1){
00471 av_log(avctx, AV_LOG_ERROR, "automatic thread number detection not supported by codec, patch welcome\n");
00472 return -1;
00473 }
00474
00475 if(s->avctx->thread_count > 1)
00476 s->rtp_mode= 1;
00477
00478 if(!avctx->time_base.den || !avctx->time_base.num){
00479 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
00480 return -1;
00481 }
00482
00483 i= (INT_MAX/2+128)>>8;
00484 if(avctx->me_threshold >= i){
00485 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1);
00486 return -1;
00487 }
00488 if(avctx->mb_threshold >= i){
00489 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1);
00490 return -1;
00491 }
00492
00493 if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){
00494 av_log(avctx, AV_LOG_INFO, "notice: b_frame_strategy only affects the first pass\n");
00495 avctx->b_frame_strategy = 0;
00496 }
00497
00498 i= av_gcd(avctx->time_base.den, avctx->time_base.num);
00499 if(i > 1){
00500 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
00501 avctx->time_base.den /= i;
00502 avctx->time_base.num /= i;
00503
00504 }
00505
00506 if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO || s->codec_id==CODEC_ID_MJPEG){
00507 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3);
00508 s->inter_quant_bias= 0;
00509 }else{
00510 s->intra_quant_bias=0;
00511 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2));
00512 }
00513
00514 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
00515 s->intra_quant_bias= avctx->intra_quant_bias;
00516 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
00517 s->inter_quant_bias= avctx->inter_quant_bias;
00518
00519 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
00520
00521 if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){
00522 av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n");
00523 return -1;
00524 }
00525 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
00526
00527 switch(avctx->codec->id) {
00528 case CODEC_ID_MPEG1VIDEO:
00529 s->out_format = FMT_MPEG1;
00530 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
00531 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
00532 break;
00533 case CODEC_ID_MPEG2VIDEO:
00534 s->out_format = FMT_MPEG1;
00535 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
00536 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
00537 s->rtp_mode= 1;
00538 break;
00539 case CODEC_ID_LJPEG:
00540 case CODEC_ID_MJPEG:
00541 s->out_format = FMT_MJPEG;
00542 s->intra_only = 1;
00543 if(avctx->codec->id == CODEC_ID_LJPEG && avctx->pix_fmt == PIX_FMT_BGRA){
00544 s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
00545 s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
00546 s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
00547 }else{
00548 s->mjpeg_vsample[0] = 2;
00549 s->mjpeg_vsample[1] = 2>>chroma_v_shift;
00550 s->mjpeg_vsample[2] = 2>>chroma_v_shift;
00551 s->mjpeg_hsample[0] = 2;
00552 s->mjpeg_hsample[1] = 2>>chroma_h_shift;
00553 s->mjpeg_hsample[2] = 2>>chroma_h_shift;
00554 }
00555 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER)
00556 || ff_mjpeg_encode_init(s) < 0)
00557 return -1;
00558 avctx->delay=0;
00559 s->low_delay=1;
00560 break;
00561 case CODEC_ID_H261:
00562 if (!CONFIG_H261_ENCODER) return -1;
00563 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
00564 av_log(avctx, AV_LOG_ERROR, "The specified picture size of %dx%d is not valid for the H.261 codec.\nValid sizes are 176x144, 352x288\n", s->width, s->height);
00565 return -1;
00566 }
00567 s->out_format = FMT_H261;
00568 avctx->delay=0;
00569 s->low_delay=1;
00570 break;
00571 case CODEC_ID_H263:
00572 if (!CONFIG_H263_ENCODER) return -1;
00573 if (ff_match_2uint16(h263_format, FF_ARRAY_ELEMS(h263_format), s->width, s->height) == 7) {
00574 av_log(avctx, AV_LOG_INFO, "The specified picture size of %dx%d is not valid for the H.263 codec.\nValid sizes are 128x96, 176x144, 352x288, 704x576, and 1408x1152. Try H.263+.\n", s->width, s->height);
00575 return -1;
00576 }
00577 s->out_format = FMT_H263;
00578 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
00579 avctx->delay=0;
00580 s->low_delay=1;
00581 break;
00582 case CODEC_ID_H263P:
00583 s->out_format = FMT_H263;
00584 s->h263_plus = 1;
00585
00586 s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
00587 s->h263_aic= (avctx->flags & CODEC_FLAG_AC_PRED) ? 1:0;
00588 s->modified_quant= s->h263_aic;
00589 s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
00590 s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
00591 s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
00592 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
00593 s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
00594
00595
00596
00597 avctx->delay=0;
00598 s->low_delay=1;
00599 break;
00600 case CODEC_ID_FLV1:
00601 s->out_format = FMT_H263;
00602 s->h263_flv = 2;
00603 s->unrestricted_mv = 1;
00604 s->rtp_mode=0;
00605 avctx->delay=0;
00606 s->low_delay=1;
00607 break;
00608 case CODEC_ID_RV10:
00609 s->out_format = FMT_H263;
00610 avctx->delay=0;
00611 s->low_delay=1;
00612 break;
00613 case CODEC_ID_RV20:
00614 s->out_format = FMT_H263;
00615 avctx->delay=0;
00616 s->low_delay=1;
00617 s->modified_quant=1;
00618 s->h263_aic=1;
00619 s->h263_plus=1;
00620 s->loop_filter=1;
00621 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
00622 break;
00623 case CODEC_ID_MPEG4:
00624 s->out_format = FMT_H263;
00625 s->h263_pred = 1;
00626 s->unrestricted_mv = 1;
00627 s->low_delay= s->max_b_frames ? 0 : 1;
00628 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
00629 break;
00630 case CODEC_ID_MSMPEG4V1:
00631 s->out_format = FMT_H263;
00632 s->h263_msmpeg4 = 1;
00633 s->h263_pred = 1;
00634 s->unrestricted_mv = 1;
00635 s->msmpeg4_version= 1;
00636 avctx->delay=0;
00637 s->low_delay=1;
00638 break;
00639 case CODEC_ID_MSMPEG4V2:
00640 s->out_format = FMT_H263;
00641 s->h263_msmpeg4 = 1;
00642 s->h263_pred = 1;
00643 s->unrestricted_mv = 1;
00644 s->msmpeg4_version= 2;
00645 avctx->delay=0;
00646 s->low_delay=1;
00647 break;
00648 case CODEC_ID_MSMPEG4V3:
00649 s->out_format = FMT_H263;
00650 s->h263_msmpeg4 = 1;
00651 s->h263_pred = 1;
00652 s->unrestricted_mv = 1;
00653 s->msmpeg4_version= 3;
00654 s->flipflop_rounding=1;
00655 avctx->delay=0;
00656 s->low_delay=1;
00657 break;
00658 case CODEC_ID_WMV1:
00659 s->out_format = FMT_H263;
00660 s->h263_msmpeg4 = 1;
00661 s->h263_pred = 1;
00662 s->unrestricted_mv = 1;
00663 s->msmpeg4_version= 4;
00664 s->flipflop_rounding=1;
00665 avctx->delay=0;
00666 s->low_delay=1;
00667 break;
00668 case CODEC_ID_WMV2:
00669 s->out_format = FMT_H263;
00670 s->h263_msmpeg4 = 1;
00671 s->h263_pred = 1;
00672 s->unrestricted_mv = 1;
00673 s->msmpeg4_version= 5;
00674 s->flipflop_rounding=1;
00675 avctx->delay=0;
00676 s->low_delay=1;
00677 break;
00678 default:
00679 return -1;
00680 }
00681
00682 avctx->has_b_frames= !s->low_delay;
00683
00684 s->encoding = 1;
00685
00686 s->progressive_frame=
00687 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN));
00688
00689
00690 if (MPV_common_init(s) < 0)
00691 return -1;
00692
00693 if(!s->dct_quantize)
00694 s->dct_quantize = dct_quantize_c;
00695 if(!s->denoise_dct)
00696 s->denoise_dct = denoise_dct_c;
00697 s->fast_dct_quantize = s->dct_quantize;
00698 if(avctx->trellis)
00699 s->dct_quantize = dct_quantize_trellis_c;
00700
00701 if((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
00702 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
00703
00704 s->quant_precision=5;
00705
00706 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
00707 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
00708
00709 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
00710 ff_h261_encode_init(s);
00711 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
00712 h263_encode_init(s);
00713 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
00714 ff_msmpeg4_encode_init(s);
00715 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
00716 && s->out_format == FMT_MPEG1)
00717 ff_mpeg1_encode_init(s);
00718
00719
00720 for(i=0;i<64;i++) {
00721 int j= s->dsp.idct_permutation[i];
00722 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
00723 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
00724 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
00725 }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
00726 s->intra_matrix[j] =
00727 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
00728 }else
00729 {
00730 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
00731 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
00732 }
00733 if(s->avctx->intra_matrix)
00734 s->intra_matrix[j] = s->avctx->intra_matrix[i];
00735 if(s->avctx->inter_matrix)
00736 s->inter_matrix[j] = s->avctx->inter_matrix[i];
00737 }
00738
00739
00740
00741 if (s->out_format != FMT_MJPEG) {
00742 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
00743 s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
00744 ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
00745 s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
00746 }
00747
00748 if(ff_rate_control_init(s) < 0)
00749 return -1;
00750
00751 return 0;
00752 }
00753
00754 av_cold int MPV_encode_end(AVCodecContext *avctx)
00755 {
00756 MpegEncContext *s = avctx->priv_data;
00757
00758 ff_rate_control_uninit(s);
00759
00760 MPV_common_end(s);
00761 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) && s->out_format == FMT_MJPEG)
00762 ff_mjpeg_encode_close(s);
00763
00764 av_freep(&avctx->extradata);
00765
00766 return 0;
00767 }
00768
00769 static int get_sae(uint8_t *src, int ref, int stride){
00770 int x,y;
00771 int acc=0;
00772
00773 for(y=0; y<16; y++){
00774 for(x=0; x<16; x++){
00775 acc+= FFABS(src[x+y*stride] - ref);
00776 }
00777 }
00778
00779 return acc;
00780 }
00781
00782 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
00783 int x, y, w, h;
00784 int acc=0;
00785
00786 w= s->width &~15;
00787 h= s->height&~15;
00788
00789 for(y=0; y<h; y+=16){
00790 for(x=0; x<w; x+=16){
00791 int offset= x + y*stride;
00792 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
00793 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
00794 int sae = get_sae(src + offset, mean, stride);
00795
00796 acc+= sae + 500 < sad;
00797 }
00798 }
00799 return acc;
00800 }
00801
00802
00803 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
00804 AVFrame *pic=NULL;
00805 int64_t pts;
00806 int i;
00807 const int encoding_delay= s->max_b_frames;
00808 int direct=1;
00809
00810 if(pic_arg){
00811 pts= pic_arg->pts;
00812 pic_arg->display_picture_number= s->input_picture_number++;
00813
00814 if(pts != AV_NOPTS_VALUE){
00815 if(s->user_specified_pts != AV_NOPTS_VALUE){
00816 int64_t time= pts;
00817 int64_t last= s->user_specified_pts;
00818
00819 if(time <= last){
00820 av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts);
00821 return -1;
00822 }
00823 }
00824 s->user_specified_pts= pts;
00825 }else{
00826 if(s->user_specified_pts != AV_NOPTS_VALUE){
00827 s->user_specified_pts=
00828 pts= s->user_specified_pts + 1;
00829 av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts);
00830 }else{
00831 pts= pic_arg->display_picture_number;
00832 }
00833 }
00834 }
00835
00836 if(pic_arg){
00837 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
00838 if(pic_arg->linesize[0] != s->linesize) direct=0;
00839 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
00840 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
00841
00842
00843
00844 if(direct){
00845 i= ff_find_unused_picture(s, 1);
00846
00847 pic= (AVFrame*)&s->picture[i];
00848 pic->reference= 3;
00849
00850 for(i=0; i<4; i++){
00851 pic->data[i]= pic_arg->data[i];
00852 pic->linesize[i]= pic_arg->linesize[i];
00853 }
00854 if(ff_alloc_picture(s, (Picture*)pic, 1) < 0){
00855 return -1;
00856 }
00857 }else{
00858 i= ff_find_unused_picture(s, 0);
00859
00860 pic= (AVFrame*)&s->picture[i];
00861 pic->reference= 3;
00862
00863 if(ff_alloc_picture(s, (Picture*)pic, 0) < 0){
00864 return -1;
00865 }
00866
00867 if( pic->data[0] + INPLACE_OFFSET == pic_arg->data[0]
00868 && pic->data[1] + INPLACE_OFFSET == pic_arg->data[1]
00869 && pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]){
00870
00871 }else{
00872 int h_chroma_shift, v_chroma_shift;
00873 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00874
00875 for(i=0; i<3; i++){
00876 int src_stride= pic_arg->linesize[i];
00877 int dst_stride= i ? s->uvlinesize : s->linesize;
00878 int h_shift= i ? h_chroma_shift : 0;
00879 int v_shift= i ? v_chroma_shift : 0;
00880 int w= s->width >>h_shift;
00881 int h= s->height>>v_shift;
00882 uint8_t *src= pic_arg->data[i];
00883 uint8_t *dst= pic->data[i];
00884
00885 if(!s->avctx->rc_buffer_size)
00886 dst +=INPLACE_OFFSET;
00887
00888 if(src_stride==dst_stride)
00889 memcpy(dst, src, src_stride*h);
00890 else{
00891 while(h--){
00892 memcpy(dst, src, w);
00893 dst += dst_stride;
00894 src += src_stride;
00895 }
00896 }
00897 }
00898 }
00899 }
00900 copy_picture_attributes(s, pic, pic_arg);
00901 pic->pts= pts;
00902 }
00903
00904
00905 for(i=1; i<MAX_PICTURE_COUNT ; i++)
00906 s->input_picture[i-1]= s->input_picture[i];
00907
00908 s->input_picture[encoding_delay]= (Picture*)pic;
00909
00910 return 0;
00911 }
00912
00913 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){
00914 int x, y, plane;
00915 int score=0;
00916 int64_t score64=0;
00917
00918 for(plane=0; plane<3; plane++){
00919 const int stride= p->linesize[plane];
00920 const int bw= plane ? 1 : 2;
00921 for(y=0; y<s->mb_height*bw; y++){
00922 for(x=0; x<s->mb_width*bw; x++){
00923 int off= p->type == FF_BUFFER_TYPE_SHARED ? 0: 16;
00924 int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride)+off, ref->data[plane] + 8*(x + y*stride), stride, 8);
00925
00926 switch(s->avctx->frame_skip_exp){
00927 case 0: score= FFMAX(score, v); break;
00928 case 1: score+= FFABS(v);break;
00929 case 2: score+= v*v;break;
00930 case 3: score64+= FFABS(v*v*(int64_t)v);break;
00931 case 4: score64+= v*v*(int64_t)(v*v);break;
00932 }
00933 }
00934 }
00935 }
00936
00937 if(score) score64= score;
00938
00939 if(score64 < s->avctx->frame_skip_threshold)
00940 return 1;
00941 if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
00942 return 1;
00943 return 0;
00944 }
00945
00946 static int estimate_best_b_count(MpegEncContext *s){
00947 AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id);
00948 AVCodecContext *c= avcodec_alloc_context();
00949 AVFrame input[FF_MAX_B_FRAMES+2];
00950 const int scale= s->avctx->brd_scale;
00951 int i, j, out_size, p_lambda, b_lambda, lambda2;
00952 int outbuf_size= s->width * s->height;
00953 uint8_t *outbuf= av_malloc(outbuf_size);
00954 int64_t best_rd= INT64_MAX;
00955 int best_b_count= -1;
00956
00957 assert(scale>=0 && scale <=3);
00958
00959
00960 p_lambda= s->last_lambda_for[FF_P_TYPE];
00961 b_lambda= s->last_lambda_for[FF_B_TYPE];
00962 if(!b_lambda) b_lambda= p_lambda;
00963 lambda2= (b_lambda*b_lambda + (1<<FF_LAMBDA_SHIFT)/2 ) >> FF_LAMBDA_SHIFT;
00964
00965 c->width = s->width >> scale;
00966 c->height= s->height>> scale;
00967 c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED ;
00968 c->flags|= s->avctx->flags & CODEC_FLAG_QPEL;
00969 c->mb_decision= s->avctx->mb_decision;
00970 c->me_cmp= s->avctx->me_cmp;
00971 c->mb_cmp= s->avctx->mb_cmp;
00972 c->me_sub_cmp= s->avctx->me_sub_cmp;
00973 c->pix_fmt = PIX_FMT_YUV420P;
00974 c->time_base= s->avctx->time_base;
00975 c->max_b_frames= s->max_b_frames;
00976
00977 if (avcodec_open(c, codec) < 0)
00978 return -1;
00979
00980 for(i=0; i<s->max_b_frames+2; i++){
00981 int ysize= c->width*c->height;
00982 int csize= (c->width/2)*(c->height/2);
00983 Picture pre_input, *pre_input_ptr= i ? s->input_picture[i-1] : s->next_picture_ptr;
00984
00985 avcodec_get_frame_defaults(&input[i]);
00986 input[i].data[0]= av_malloc(ysize + 2*csize);
00987 input[i].data[1]= input[i].data[0] + ysize;
00988 input[i].data[2]= input[i].data[1] + csize;
00989 input[i].linesize[0]= c->width;
00990 input[i].linesize[1]=
00991 input[i].linesize[2]= c->width/2;
00992
00993 if(pre_input_ptr && (!i || s->input_picture[i-1])) {
00994 pre_input= *pre_input_ptr;
00995
00996 if(pre_input.type != FF_BUFFER_TYPE_SHARED && i) {
00997 pre_input.data[0]+=INPLACE_OFFSET;
00998 pre_input.data[1]+=INPLACE_OFFSET;
00999 pre_input.data[2]+=INPLACE_OFFSET;
01000 }
01001
01002 s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0], pre_input.data[0], pre_input.linesize[0], c->width, c->height);
01003 s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1], pre_input.data[1], pre_input.linesize[1], c->width>>1, c->height>>1);
01004 s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2], pre_input.data[2], pre_input.linesize[2], c->width>>1, c->height>>1);
01005 }
01006 }
01007
01008 for(j=0; j<s->max_b_frames+1; j++){
01009 int64_t rd=0;
01010
01011 if(!s->input_picture[j])
01012 break;
01013
01014 c->error[0]= c->error[1]= c->error[2]= 0;
01015
01016 input[0].pict_type= FF_I_TYPE;
01017 input[0].quality= 1 * FF_QP2LAMBDA;
01018 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]);
01019
01020
01021 for(i=0; i<s->max_b_frames+1; i++){
01022 int is_p= i % (j+1) == j || i==s->max_b_frames;
01023
01024 input[i+1].pict_type= is_p ? FF_P_TYPE : FF_B_TYPE;
01025 input[i+1].quality= is_p ? p_lambda : b_lambda;
01026 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]);
01027 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
01028 }
01029
01030
01031 while(out_size){
01032 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
01033 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
01034 }
01035
01036 rd += c->error[0] + c->error[1] + c->error[2];
01037
01038 if(rd < best_rd){
01039 best_rd= rd;
01040 best_b_count= j;
01041 }
01042 }
01043
01044 av_freep(&outbuf);
01045 avcodec_close(c);
01046 av_freep(&c);
01047
01048 for(i=0; i<s->max_b_frames+2; i++){
01049 av_freep(&input[i].data[0]);
01050 }
01051
01052 return best_b_count;
01053 }
01054
01055 static int select_input_picture(MpegEncContext *s){
01056 int i;
01057
01058 for(i=1; i<MAX_PICTURE_COUNT; i++)
01059 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
01060 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
01061
01062
01063 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
01064 if( s->next_picture_ptr==NULL || s->intra_only){
01065 s->reordered_input_picture[0]= s->input_picture[0];
01066 s->reordered_input_picture[0]->pict_type= FF_I_TYPE;
01067 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
01068 }else{
01069 int b_frames;
01070
01071 if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
01072 if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){
01073
01074
01075
01076 if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
01077 for(i=0; i<4; i++)
01078 s->input_picture[0]->data[i]= NULL;
01079 s->input_picture[0]->type= 0;
01080 }else{
01081 assert( s->input_picture[0]->type==FF_BUFFER_TYPE_USER
01082 || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
01083
01084 s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
01085 }
01086
01087 emms_c();
01088 ff_vbv_update(s, 0);
01089
01090 goto no_output_pic;
01091 }
01092 }
01093
01094 if(s->flags&CODEC_FLAG_PASS2){
01095 for(i=0; i<s->max_b_frames+1; i++){
01096 int pict_num= s->input_picture[0]->display_picture_number + i;
01097
01098 if(pict_num >= s->rc_context.num_entries)
01099 break;
01100 if(!s->input_picture[i]){
01101 s->rc_context.entry[pict_num-1].new_pict_type = FF_P_TYPE;
01102 break;
01103 }
01104
01105 s->input_picture[i]->pict_type=
01106 s->rc_context.entry[pict_num].new_pict_type;
01107 }
01108 }
01109
01110 if(s->avctx->b_frame_strategy==0){
01111 b_frames= s->max_b_frames;
01112 while(b_frames && !s->input_picture[b_frames]) b_frames--;
01113 }else if(s->avctx->b_frame_strategy==1){
01114 for(i=1; i<s->max_b_frames+1; i++){
01115 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
01116 s->input_picture[i]->b_frame_score=
01117 get_intra_count(s, s->input_picture[i ]->data[0],
01118 s->input_picture[i-1]->data[0], s->linesize) + 1;
01119 }
01120 }
01121 for(i=0; i<s->max_b_frames+1; i++){
01122 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/s->avctx->b_sensitivity) break;
01123 }
01124
01125 b_frames= FFMAX(0, i-1);
01126
01127
01128 for(i=0; i<b_frames+1; i++){
01129 s->input_picture[i]->b_frame_score=0;
01130 }
01131 }else if(s->avctx->b_frame_strategy==2){
01132 b_frames= estimate_best_b_count(s);
01133 }else{
01134 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
01135 b_frames=0;
01136 }
01137
01138 emms_c();
01139
01140
01141
01142
01143 for(i= b_frames - 1; i>=0; i--){
01144 int type= s->input_picture[i]->pict_type;
01145 if(type && type != FF_B_TYPE)
01146 b_frames= i;
01147 }
01148 if(s->input_picture[b_frames]->pict_type == FF_B_TYPE && b_frames == s->max_b_frames){
01149 av_log(s->avctx, AV_LOG_ERROR, "warning, too many b frames in a row\n");
01150 }
01151
01152 if(s->picture_in_gop_number + b_frames >= s->gop_size){
01153 if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){
01154 b_frames= s->gop_size - s->picture_in_gop_number - 1;
01155 }else{
01156 if(s->flags & CODEC_FLAG_CLOSED_GOP)
01157 b_frames=0;
01158 s->input_picture[b_frames]->pict_type= FF_I_TYPE;
01159 }
01160 }
01161
01162 if( (s->flags & CODEC_FLAG_CLOSED_GOP)
01163 && b_frames
01164 && s->input_picture[b_frames]->pict_type== FF_I_TYPE)
01165 b_frames--;
01166
01167 s->reordered_input_picture[0]= s->input_picture[b_frames];
01168 if(s->reordered_input_picture[0]->pict_type != FF_I_TYPE)
01169 s->reordered_input_picture[0]->pict_type= FF_P_TYPE;
01170 s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
01171 for(i=0; i<b_frames; i++){
01172 s->reordered_input_picture[i+1]= s->input_picture[i];
01173 s->reordered_input_picture[i+1]->pict_type= FF_B_TYPE;
01174 s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
01175 }
01176 }
01177 }
01178 no_output_pic:
01179 if(s->reordered_input_picture[0]){
01180 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=FF_B_TYPE ? 3 : 0;
01181
01182 ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
01183
01184 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED || s->avctx->rc_buffer_size){
01185
01186
01187 int i= ff_find_unused_picture(s, 0);
01188 Picture *pic= &s->picture[i];
01189
01190 pic->reference = s->reordered_input_picture[0]->reference;
01191 if(ff_alloc_picture(s, pic, 0) < 0){
01192 return -1;
01193 }
01194
01195
01196 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_INTERNAL)
01197 s->avctx->release_buffer(s->avctx, (AVFrame*)s->reordered_input_picture[0]);
01198 for(i=0; i<4; i++)
01199 s->reordered_input_picture[0]->data[i]= NULL;
01200 s->reordered_input_picture[0]->type= 0;
01201
01202 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
01203
01204 s->current_picture_ptr= pic;
01205 }else{
01206
01207
01208 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
01209 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
01210
01211 s->current_picture_ptr= s->reordered_input_picture[0];
01212 for(i=0; i<4; i++){
01213 s->new_picture.data[i]+= INPLACE_OFFSET;
01214 }
01215 }
01216 ff_copy_picture(&s->current_picture, s->current_picture_ptr);
01217
01218 s->picture_number= s->new_picture.display_picture_number;
01219
01220 }else{
01221 memset(&s->new_picture, 0, sizeof(Picture));
01222 }
01223 return 0;
01224 }
01225
01226 int MPV_encode_picture(AVCodecContext *avctx,
01227 unsigned char *buf, int buf_size, void *data)
01228 {
01229 MpegEncContext *s = avctx->priv_data;
01230 AVFrame *pic_arg = data;
01231 int i, stuffing_count;
01232
01233 for(i=0; i<avctx->thread_count; i++){
01234 int start_y= s->thread_context[i]->start_mb_y;
01235 int end_y= s->thread_context[i]-> end_mb_y;
01236 int h= s->mb_height;
01237 uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h);
01238 uint8_t *end = buf + (size_t)(((int64_t) buf_size)* end_y/h);
01239
01240 init_put_bits(&s->thread_context[i]->pb, start, end - start);
01241 }
01242
01243 s->picture_in_gop_number++;
01244
01245 if(load_input_picture(s, pic_arg) < 0)
01246 return -1;
01247
01248 if(select_input_picture(s) < 0){
01249 return -1;
01250 }
01251
01252
01253 if(s->new_picture.data[0]){
01254 s->pict_type= s->new_picture.pict_type;
01255
01256
01257 MPV_frame_start(s, avctx);
01258 vbv_retry:
01259 if (encode_picture(s, s->picture_number) < 0)
01260 return -1;
01261
01262 avctx->header_bits = s->header_bits;
01263 avctx->mv_bits = s->mv_bits;
01264 avctx->misc_bits = s->misc_bits;
01265 avctx->i_tex_bits = s->i_tex_bits;
01266 avctx->p_tex_bits = s->p_tex_bits;
01267 avctx->i_count = s->i_count;
01268 avctx->p_count = s->mb_num - s->i_count - s->skip_count;
01269 avctx->skip_count = s->skip_count;
01270
01271 MPV_frame_end(s);
01272
01273 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
01274 ff_mjpeg_encode_picture_trailer(s);
01275
01276 if(avctx->rc_buffer_size){
01277 RateControlContext *rcc= &s->rc_context;
01278 int max_size= rcc->buffer_index * avctx->rc_max_available_vbv_use;
01279
01280 if(put_bits_count(&s->pb) > max_size && s->lambda < s->avctx->lmax){
01281 s->next_lambda= FFMAX(s->lambda+1, s->lambda*(s->qscale+1) / s->qscale);
01282 if(s->adaptive_quant){
01283 int i;
01284 for(i=0; i<s->mb_height*s->mb_stride; i++)
01285 s->lambda_table[i]= FFMAX(s->lambda_table[i]+1, s->lambda_table[i]*(s->qscale+1) / s->qscale);
01286 }
01287 s->mb_skipped = 0;
01288 if(s->pict_type==FF_P_TYPE){
01289 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
01290 s->no_rounding ^= 1;
01291 }
01292 if(s->pict_type!=FF_B_TYPE){
01293 s->time_base= s->last_time_base;
01294 s->last_non_b_time= s->time - s->pp_time;
01295 }
01296
01297 for(i=0; i<avctx->thread_count; i++){
01298 PutBitContext *pb= &s->thread_context[i]->pb;
01299 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
01300 }
01301 goto vbv_retry;
01302 }
01303
01304 assert(s->avctx->rc_max_rate);
01305 }
01306
01307 if(s->flags&CODEC_FLAG_PASS1)
01308 ff_write_pass1_stats(s);
01309
01310 for(i=0; i<4; i++){
01311 s->current_picture_ptr->error[i]= s->current_picture.error[i];
01312 avctx->error[i] += s->current_picture_ptr->error[i];
01313 }
01314
01315 if(s->flags&CODEC_FLAG_PASS1)
01316 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb));
01317 flush_put_bits(&s->pb);
01318 s->frame_bits = put_bits_count(&s->pb);
01319
01320 stuffing_count= ff_vbv_update(s, s->frame_bits);
01321 if(stuffing_count){
01322 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){
01323 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
01324 return -1;
01325 }
01326
01327 switch(s->codec_id){
01328 case CODEC_ID_MPEG1VIDEO:
01329 case CODEC_ID_MPEG2VIDEO:
01330 while(stuffing_count--){
01331 put_bits(&s->pb, 8, 0);
01332 }
01333 break;
01334 case CODEC_ID_MPEG4:
01335 put_bits(&s->pb, 16, 0);
01336 put_bits(&s->pb, 16, 0x1C3);
01337 stuffing_count -= 4;
01338 while(stuffing_count--){
01339 put_bits(&s->pb, 8, 0xFF);
01340 }
01341 break;
01342 default:
01343 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
01344 }
01345 flush_put_bits(&s->pb);
01346 s->frame_bits = put_bits_count(&s->pb);
01347 }
01348
01349
01350 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
01351 && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
01352 int vbv_delay, min_delay;
01353 double inbits = s->avctx->rc_max_rate*av_q2d(s->avctx->time_base);
01354 int minbits= s->frame_bits - 8*(s->vbv_delay_ptr - s->pb.buf - 1);
01355 double bits = s->rc_context.buffer_index + minbits - inbits;
01356
01357 if(bits<0)
01358 av_log(s->avctx, AV_LOG_ERROR, "Internal error, negative bits\n");
01359
01360 assert(s->repeat_first_field==0);
01361
01362 vbv_delay= bits * 90000 / s->avctx->rc_max_rate;
01363 min_delay= (minbits * 90000LL + s->avctx->rc_max_rate - 1)/ s->avctx->rc_max_rate;
01364
01365 vbv_delay= FFMAX(vbv_delay, min_delay);
01366
01367 assert(vbv_delay < 0xFFFF);
01368
01369 s->vbv_delay_ptr[0] &= 0xF8;
01370 s->vbv_delay_ptr[0] |= vbv_delay>>13;
01371 s->vbv_delay_ptr[1] = vbv_delay>>5;
01372 s->vbv_delay_ptr[2] &= 0x07;
01373 s->vbv_delay_ptr[2] |= vbv_delay<<3;
01374 }
01375 s->total_bits += s->frame_bits;
01376 avctx->frame_bits = s->frame_bits;
01377 }else{
01378 assert((put_bits_ptr(&s->pb) == s->pb.buf));
01379 s->frame_bits=0;
01380 }
01381 assert((s->frame_bits&7)==0);
01382
01383 return s->frame_bits/8;
01384 }
01385
01386 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
01387 {
01388 static const char tab[64]=
01389 {3,2,2,1,1,1,1,1,
01390 1,1,1,1,1,1,1,1,
01391 1,1,1,1,1,1,1,1,
01392 0,0,0,0,0,0,0,0,
01393 0,0,0,0,0,0,0,0,
01394 0,0,0,0,0,0,0,0,
01395 0,0,0,0,0,0,0,0,
01396 0,0,0,0,0,0,0,0};
01397 int score=0;
01398 int run=0;
01399 int i;
01400 DCTELEM *block= s->block[n];
01401 const int last_index= s->block_last_index[n];
01402 int skip_dc;
01403
01404 if(threshold<0){
01405 skip_dc=0;
01406 threshold= -threshold;
01407 }else
01408 skip_dc=1;
01409
01410
01411 if(last_index<=skip_dc - 1) return;
01412
01413 for(i=0; i<=last_index; i++){
01414 const int j = s->intra_scantable.permutated[i];
01415 const int level = FFABS(block[j]);
01416 if(level==1){
01417 if(skip_dc && i==0) continue;
01418 score+= tab[run];
01419 run=0;
01420 }else if(level>1){
01421 return;
01422 }else{
01423 run++;
01424 }
01425 }
01426 if(score >= threshold) return;
01427 for(i=skip_dc; i<=last_index; i++){
01428 const int j = s->intra_scantable.permutated[i];
01429 block[j]=0;
01430 }
01431 if(block[0]) s->block_last_index[n]= 0;
01432 else s->block_last_index[n]= -1;
01433 }
01434
01435 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
01436 {
01437 int i;
01438 const int maxlevel= s->max_qcoeff;
01439 const int minlevel= s->min_qcoeff;
01440 int overflow=0;
01441
01442 if(s->mb_intra){
01443 i=1;
01444 }else
01445 i=0;
01446
01447 for(;i<=last_index; i++){
01448 const int j= s->intra_scantable.permutated[i];
01449 int level = block[j];
01450
01451 if (level>maxlevel){
01452 level=maxlevel;
01453 overflow++;
01454 }else if(level<minlevel){
01455 level=minlevel;
01456 overflow++;
01457 }
01458
01459 block[j]= level;
01460 }
01461
01462 if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
01463 av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel);
01464 }
01465
01466 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride){
01467 int x, y;
01468
01469 for(y=0; y<8; y++){
01470 for(x=0; x<8; x++){
01471 int x2, y2;
01472 int sum=0;
01473 int sqr=0;
01474 int count=0;
01475
01476 for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
01477 for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
01478 int v= ptr[x2 + y2*stride];
01479 sum += v;
01480 sqr += v*v;
01481 count++;
01482 }
01483 }
01484 weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
01485 }
01486 }
01487 }
01488
01489 static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count)
01490 {
01491 int16_t weight[8][64];
01492 DCTELEM orig[8][64];
01493 const int mb_x= s->mb_x;
01494 const int mb_y= s->mb_y;
01495 int i;
01496 int skip_dct[8];
01497 int dct_offset = s->linesize*8;
01498 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
01499 int wrap_y, wrap_c;
01500
01501 for(i=0; i<mb_block_count; i++) skip_dct[i]=s->skipdct;
01502
01503 if(s->adaptive_quant){
01504 const int last_qp= s->qscale;
01505 const int mb_xy= mb_x + mb_y*s->mb_stride;
01506
01507 s->lambda= s->lambda_table[mb_xy];
01508 update_qscale(s);
01509
01510 if(!(s->flags&CODEC_FLAG_QP_RD)){
01511 s->qscale= s->current_picture_ptr->qscale_table[mb_xy];
01512 s->dquant= s->qscale - last_qp;
01513
01514 if(s->out_format==FMT_H263){
01515 s->dquant= av_clip(s->dquant, -2, 2);
01516
01517 if(s->codec_id==CODEC_ID_MPEG4){
01518 if(!s->mb_intra){
01519 if(s->pict_type == FF_B_TYPE){
01520 if(s->dquant&1 || s->mv_dir&MV_DIRECT)
01521 s->dquant= 0;
01522 }
01523 if(s->mv_type==MV_TYPE_8X8)
01524 s->dquant=0;
01525 }
01526 }
01527 }
01528 }
01529 ff_set_qscale(s, last_qp + s->dquant);
01530 }else if(s->flags&CODEC_FLAG_QP_RD)
01531 ff_set_qscale(s, s->qscale + s->dquant);
01532
01533 wrap_y = s->linesize;
01534 wrap_c = s->uvlinesize;
01535 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
01536 ptr_cb = s->new_picture.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
01537 ptr_cr = s->new_picture.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
01538
01539 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
01540 uint8_t *ebuf= s->edge_emu_buffer + 32;
01541 ff_emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height);
01542 ptr_y= ebuf;
01543 ff_emulated_edge_mc(ebuf+18*wrap_y , ptr_cb, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
01544 ptr_cb= ebuf+18*wrap_y;
01545 ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
01546 ptr_cr= ebuf+18*wrap_y+8;
01547 }
01548
01549 if (s->mb_intra) {
01550 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
01551 int progressive_score, interlaced_score;
01552
01553 s->interlaced_dct=0;
01554 progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
01555 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
01556
01557 if(progressive_score > 0){
01558 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
01559 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
01560 if(progressive_score > interlaced_score){
01561 s->interlaced_dct=1;
01562
01563 dct_offset= wrap_y;
01564 wrap_y<<=1;
01565 if (s->chroma_format == CHROMA_422)
01566 wrap_c<<=1;
01567 }
01568 }
01569 }
01570
01571 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
01572 s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
01573 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
01574 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
01575
01576 if(s->flags&CODEC_FLAG_GRAY){
01577 skip_dct[4]= 1;
01578 skip_dct[5]= 1;
01579 }else{
01580 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
01581 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
01582 if(!s->chroma_y_shift){
01583 s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset>>1), wrap_c);
01584 s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset>>1), wrap_c);
01585 }
01586 }
01587 }else{
01588 op_pixels_func (*op_pix)[4];
01589 qpel_mc_func (*op_qpix)[16];
01590 uint8_t *dest_y, *dest_cb, *dest_cr;
01591
01592 dest_y = s->dest[0];
01593 dest_cb = s->dest[1];
01594 dest_cr = s->dest[2];
01595
01596 if ((!s->no_rounding) || s->pict_type==FF_B_TYPE){
01597 op_pix = s->dsp.put_pixels_tab;
01598 op_qpix= s->dsp.put_qpel_pixels_tab;
01599 }else{
01600 op_pix = s->dsp.put_no_rnd_pixels_tab;
01601 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
01602 }
01603
01604 if (s->mv_dir & MV_DIR_FORWARD) {
01605 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
01606 op_pix = s->dsp.avg_pixels_tab;
01607 op_qpix= s->dsp.avg_qpel_pixels_tab;
01608 }
01609 if (s->mv_dir & MV_DIR_BACKWARD) {
01610 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
01611 }
01612
01613 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
01614 int progressive_score, interlaced_score;
01615
01616 s->interlaced_dct=0;
01617 progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
01618 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
01619
01620 if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
01621
01622 if(progressive_score>0){
01623 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
01624 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
01625
01626 if(progressive_score > interlaced_score){
01627 s->interlaced_dct=1;
01628
01629 dct_offset= wrap_y;
01630 wrap_y<<=1;
01631 if (s->chroma_format == CHROMA_422)
01632 wrap_c<<=1;
01633 }
01634 }
01635 }
01636
01637 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
01638 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
01639 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
01640 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
01641
01642 if(s->flags&CODEC_FLAG_GRAY){
01643 skip_dct[4]= 1;
01644 skip_dct[5]= 1;
01645 }else{
01646 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
01647 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
01648 if(!s->chroma_y_shift){
01649 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset>>1), dest_cb + (dct_offset>>1), wrap_c);
01650 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset>>1), dest_cr + (dct_offset>>1), wrap_c);
01651 }
01652 }
01653
01654 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
01655
01656 if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
01657 if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
01658 if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
01659 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;
01660 if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
01661 if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
01662 if(!s->chroma_y_shift){
01663 if(s->dsp.sad[1](NULL, ptr_cb +(dct_offset>>1), dest_cb +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[6]= 1;
01664 if(s->dsp.sad[1](NULL, ptr_cr +(dct_offset>>1), dest_cr +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[7]= 1;
01665 }
01666 }
01667 }
01668
01669 if(s->avctx->quantizer_noise_shaping){
01670 if(!skip_dct[0]) get_visual_weight(weight[0], ptr_y , wrap_y);
01671 if(!skip_dct[1]) get_visual_weight(weight[1], ptr_y + 8, wrap_y);
01672 if(!skip_dct[2]) get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
01673 if(!skip_dct[3]) get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
01674 if(!skip_dct[4]) get_visual_weight(weight[4], ptr_cb , wrap_c);
01675 if(!skip_dct[5]) get_visual_weight(weight[5], ptr_cr , wrap_c);
01676 if(!s->chroma_y_shift){
01677 if(!skip_dct[6]) get_visual_weight(weight[6], ptr_cb + (dct_offset>>1), wrap_c);
01678 if(!skip_dct[7]) get_visual_weight(weight[7], ptr_cr + (dct_offset>>1), wrap_c);
01679 }
01680 memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*mb_block_count);
01681 }
01682
01683
01684 assert(s->out_format!=FMT_MJPEG || s->qscale==8);
01685 {
01686 for(i=0;i<mb_block_count;i++) {
01687 if(!skip_dct[i]){
01688 int overflow;
01689 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
01690
01691
01692
01693 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
01694 }else
01695 s->block_last_index[i]= -1;
01696 }
01697 if(s->avctx->quantizer_noise_shaping){
01698 for(i=0;i<mb_block_count;i++) {
01699 if(!skip_dct[i]){
01700 s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
01701 }
01702 }
01703 }
01704
01705 if(s->luma_elim_threshold && !s->mb_intra)
01706 for(i=0; i<4; i++)
01707 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
01708 if(s->chroma_elim_threshold && !s->mb_intra)
01709 for(i=4; i<mb_block_count; i++)
01710 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
01711
01712 if(s->flags & CODEC_FLAG_CBP_RD){
01713 for(i=0;i<mb_block_count;i++) {
01714 if(s->block_last_index[i] == -1)
01715 s->coded_score[i]= INT_MAX/256;
01716 }
01717 }
01718 }
01719
01720 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
01721 s->block_last_index[4]=
01722 s->block_last_index[5]= 0;
01723 s->block[4][0]=
01724 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
01725 }
01726
01727
01728 if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
01729 for(i=0; i<mb_block_count; i++){
01730 int j;
01731 if(s->block_last_index[i]>0){
01732 for(j=63; j>0; j--){
01733 if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
01734 }
01735 s->block_last_index[i]= j;
01736 }
01737 }
01738 }
01739
01740
01741 switch(s->codec_id){
01742 case CODEC_ID_MPEG1VIDEO:
01743 case CODEC_ID_MPEG2VIDEO:
01744 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
01745 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
01746 break;
01747 case CODEC_ID_MPEG4:
01748 if (CONFIG_MPEG4_ENCODER)
01749 mpeg4_encode_mb(s, s->block, motion_x, motion_y);
01750 break;
01751 case CODEC_ID_MSMPEG4V2:
01752 case CODEC_ID_MSMPEG4V3:
01753 case CODEC_ID_WMV1:
01754 if (CONFIG_MSMPEG4_ENCODER)
01755 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
01756 break;
01757 case CODEC_ID_WMV2:
01758 if (CONFIG_WMV2_ENCODER)
01759 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
01760 break;
01761 case CODEC_ID_H261:
01762 if (CONFIG_H261_ENCODER)
01763 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
01764 break;
01765 case CODEC_ID_H263:
01766 case CODEC_ID_H263P:
01767 case CODEC_ID_FLV1:
01768 case CODEC_ID_RV10:
01769 case CODEC_ID_RV20:
01770 if (CONFIG_H263_ENCODER)
01771 h263_encode_mb(s, s->block, motion_x, motion_y);
01772 break;
01773 case CODEC_ID_MJPEG:
01774 if (CONFIG_MJPEG_ENCODER)
01775 ff_mjpeg_encode_mb(s, s->block);
01776 break;
01777 default:
01778 assert(0);
01779 }
01780 }
01781
01782 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
01783 {
01784 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
01785 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
01786 }
01787
01788 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
01789 int i;
01790
01791 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int));
01792
01793
01794 d->mb_skip_run= s->mb_skip_run;
01795 for(i=0; i<3; i++)
01796 d->last_dc[i]= s->last_dc[i];
01797
01798
01799 d->mv_bits= s->mv_bits;
01800 d->i_tex_bits= s->i_tex_bits;
01801 d->p_tex_bits= s->p_tex_bits;
01802 d->i_count= s->i_count;
01803 d->f_count= s->f_count;
01804 d->b_count= s->b_count;
01805 d->skip_count= s->skip_count;
01806 d->misc_bits= s->misc_bits;
01807 d->last_bits= 0;
01808
01809 d->mb_skipped= 0;
01810 d->qscale= s->qscale;
01811 d->dquant= s->dquant;
01812
01813 d->esc3_level_length= s->esc3_level_length;
01814 }
01815
01816 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
01817 int i;
01818
01819 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
01820 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int));
01821
01822
01823 d->mb_skip_run= s->mb_skip_run;
01824 for(i=0; i<3; i++)
01825 d->last_dc[i]= s->last_dc[i];
01826
01827
01828 d->mv_bits= s->mv_bits;
01829 d->i_tex_bits= s->i_tex_bits;
01830 d->p_tex_bits= s->p_tex_bits;
01831 d->i_count= s->i_count;
01832 d->f_count= s->f_count;
01833 d->b_count= s->b_count;
01834 d->skip_count= s->skip_count;
01835 d->misc_bits= s->misc_bits;
01836
01837 d->mb_intra= s->mb_intra;
01838 d->mb_skipped= s->mb_skipped;
01839 d->mv_type= s->mv_type;
01840 d->mv_dir= s->mv_dir;
01841 d->pb= s->pb;
01842 if(s->data_partitioning){
01843 d->pb2= s->pb2;
01844 d->tex_pb= s->tex_pb;
01845 }
01846 d->block= s->block;
01847 for(i=0; i<8; i++)
01848 d->block_last_index[i]= s->block_last_index[i];
01849 d->interlaced_dct= s->interlaced_dct;
01850 d->qscale= s->qscale;
01851
01852 d->esc3_level_length= s->esc3_level_length;
01853 }
01854
01855 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
01856 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
01857 int *dmin, int *next_block, int motion_x, int motion_y)
01858 {
01859 int score;
01860 uint8_t *dest_backup[3];
01861
01862 copy_context_before_encode(s, backup, type);
01863
01864 s->block= s->blocks[*next_block];
01865 s->pb= pb[*next_block];
01866 if(s->data_partitioning){
01867 s->pb2 = pb2 [*next_block];
01868 s->tex_pb= tex_pb[*next_block];
01869 }
01870
01871 if(*next_block){
01872 memcpy(dest_backup, s->dest, sizeof(s->dest));
01873 s->dest[0] = s->rd_scratchpad;
01874 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
01875 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
01876 assert(s->linesize >= 32);
01877 }
01878
01879 encode_mb(s, motion_x, motion_y);
01880
01881 score= put_bits_count(&s->pb);
01882 if(s->data_partitioning){
01883 score+= put_bits_count(&s->pb2);
01884 score+= put_bits_count(&s->tex_pb);
01885 }
01886
01887 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
01888 MPV_decode_mb(s, s->block);
01889
01890 score *= s->lambda2;
01891 score += sse_mb(s) << FF_LAMBDA_SHIFT;
01892 }
01893
01894 if(*next_block){
01895 memcpy(s->dest, dest_backup, sizeof(s->dest));
01896 }
01897
01898 if(score<*dmin){
01899 *dmin= score;
01900 *next_block^=1;
01901
01902 copy_context_after_encode(best, s, type);
01903 }
01904 }
01905
01906 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
01907 uint32_t *sq = ff_squareTbl + 256;
01908 int acc=0;
01909 int x,y;
01910
01911 if(w==16 && h==16)
01912 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
01913 else if(w==8 && h==8)
01914 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
01915
01916 for(y=0; y<h; y++){
01917 for(x=0; x<w; x++){
01918 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
01919 }
01920 }
01921
01922 assert(acc>=0);
01923
01924 return acc;
01925 }
01926
01927 static int sse_mb(MpegEncContext *s){
01928 int w= 16;
01929 int h= 16;
01930
01931 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
01932 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
01933
01934 if(w==16 && h==16)
01935 if(s->avctx->mb_cmp == FF_CMP_NSSE){
01936 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)
01937 +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)
01938 +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);
01939 }else{
01940 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)
01941 +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)
01942 +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);
01943 }
01944 else
01945 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)
01946 +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)
01947 +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);
01948 }
01949
01950 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
01951 MpegEncContext *s= *(void**)arg;
01952
01953
01954 s->me.pre_pass=1;
01955 s->me.dia_size= s->avctx->pre_dia_size;
01956 s->first_slice_line=1;
01957 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
01958 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
01959 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
01960 }
01961 s->first_slice_line=0;
01962 }
01963
01964 s->me.pre_pass=0;
01965
01966 return 0;
01967 }
01968
01969 static int estimate_motion_thread(AVCodecContext *c, void *arg){
01970 MpegEncContext *s= *(void**)arg;
01971
01972 ff_check_alignment();
01973
01974 s->me.dia_size= s->avctx->dia_size;
01975 s->first_slice_line=1;
01976 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
01977 s->mb_x=0;
01978 ff_init_block_index(s);
01979 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
01980 s->block_index[0]+=2;
01981 s->block_index[1]+=2;
01982 s->block_index[2]+=2;
01983 s->block_index[3]+=2;
01984
01985
01986 if(s->pict_type==FF_B_TYPE)
01987 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
01988 else
01989 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
01990 }
01991 s->first_slice_line=0;
01992 }
01993 return 0;
01994 }
01995
01996 static int mb_var_thread(AVCodecContext *c, void *arg){
01997 MpegEncContext *s= *(void**)arg;
01998 int mb_x, mb_y;
01999
02000 ff_check_alignment();
02001
02002 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
02003 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
02004 int xx = mb_x * 16;
02005 int yy = mb_y * 16;
02006 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
02007 int varc;
02008 int sum = s->dsp.pix_sum(pix, s->linesize);
02009
02010 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
02011
02012 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
02013 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
02014 s->me.mb_var_sum_temp += varc;
02015 }
02016 }
02017 return 0;
02018 }
02019
02020 static void write_slice_end(MpegEncContext *s){
02021 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
02022 if(s->partitioned_frame){
02023 ff_mpeg4_merge_partitions(s);
02024 }
02025
02026 ff_mpeg4_stuffing(&s->pb);
02027 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
02028 ff_mjpeg_encode_stuffing(&s->pb);
02029 }
02030
02031 align_put_bits(&s->pb);
02032 flush_put_bits(&s->pb);
02033
02034 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
02035 s->misc_bits+= get_bits_diff(s);
02036 }
02037
02038 static int encode_thread(AVCodecContext *c, void *arg){
02039 MpegEncContext *s= *(void**)arg;
02040 int mb_x, mb_y, pdif = 0;
02041 int chr_h= 16>>s->chroma_y_shift;
02042 int i, j;
02043 MpegEncContext best_s, backup_s;
02044 uint8_t bit_buf[2][MAX_MB_BYTES];
02045 uint8_t bit_buf2[2][MAX_MB_BYTES];
02046 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
02047 PutBitContext pb[2], pb2[2], tex_pb[2];
02048
02049
02050 ff_check_alignment();
02051
02052 for(i=0; i<2; i++){
02053 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
02054 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
02055 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
02056 }
02057
02058 s->last_bits= put_bits_count(&s->pb);
02059 s->mv_bits=0;
02060 s->misc_bits=0;
02061 s->i_tex_bits=0;
02062 s->p_tex_bits=0;
02063 s->i_count=0;
02064 s->f_count=0;
02065 s->b_count=0;
02066 s->skip_count=0;
02067
02068 for(i=0; i<3; i++){
02069
02070
02071 s->last_dc[i] = 128 << s->intra_dc_precision;
02072
02073 s->current_picture.error[i] = 0;
02074 }
02075 s->mb_skip_run = 0;
02076 memset(s->last_mv, 0, sizeof(s->last_mv));
02077
02078 s->last_mv_dir = 0;
02079
02080 switch(s->codec_id){
02081 case CODEC_ID_H263:
02082 case CODEC_ID_H263P:
02083 case CODEC_ID_FLV1:
02084 if (CONFIG_H263_ENCODER)
02085 s->gob_index = ff_h263_get_gob_height(s);
02086 break;
02087 case CODEC_ID_MPEG4:
02088 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
02089 ff_mpeg4_init_partitions(s);
02090 break;
02091 }
02092
02093 s->resync_mb_x=0;
02094 s->resync_mb_y=0;
02095 s->first_slice_line = 1;
02096 s->ptr_lastgob = s->pb.buf;
02097 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
02098
02099 s->mb_x=0;
02100 s->mb_y= mb_y;
02101
02102 ff_set_qscale(s, s->qscale);
02103 ff_init_block_index(s);
02104
02105 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
02106 int xy= mb_y*s->mb_stride + mb_x;
02107 int mb_type= s->mb_type[xy];
02108
02109 int dmin= INT_MAX;
02110 int dir;
02111
02112 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
02113 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
02114 return -1;
02115 }
02116 if(s->data_partitioning){
02117 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
02118 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
02119 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
02120 return -1;
02121 }
02122 }
02123
02124 s->mb_x = mb_x;
02125 s->mb_y = mb_y;
02126 ff_update_block_index(s);
02127
02128 if(CONFIG_H261_ENCODER && s->codec_id == CODEC_ID_H261){
02129 ff_h261_reorder_mb_index(s);
02130 xy= s->mb_y*s->mb_stride + s->mb_x;
02131 mb_type= s->mb_type[xy];
02132 }
02133
02134
02135 if(s->rtp_mode){
02136 int current_packet_size, is_gob_start;
02137
02138 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
02139
02140 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
02141
02142 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
02143
02144 switch(s->codec_id){
02145 case CODEC_ID_H263:
02146 case CODEC_ID_H263P:
02147 if(!s->h263_slice_structured)
02148 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
02149 break;
02150 case CODEC_ID_MPEG2VIDEO:
02151 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
02152 case CODEC_ID_MPEG1VIDEO:
02153 if(s->mb_skip_run) is_gob_start=0;
02154 break;
02155 }
02156
02157 if(is_gob_start){
02158 if(s->start_mb_y != mb_y || mb_x!=0){
02159 write_slice_end(s);
02160
02161 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
02162 ff_mpeg4_init_partitions(s);
02163 }
02164 }
02165
02166 assert((put_bits_count(&s->pb)&7) == 0);
02167 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
02168
02169 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
02170 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
02171 int d= 100 / s->avctx->error_rate;
02172 if(r % d == 0){
02173 current_packet_size=0;
02174 #ifndef ALT_BITSTREAM_WRITER
02175 s->pb.buf_ptr= s->ptr_lastgob;
02176 #endif
02177 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
02178 }
02179 }
02180
02181 if (s->avctx->rtp_callback){
02182 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
02183 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
02184 }
02185
02186 switch(s->codec_id){
02187 case CODEC_ID_MPEG4:
02188 if (CONFIG_MPEG4_ENCODER) {
02189 ff_mpeg4_encode_video_packet_header(s);
02190 ff_mpeg4_clean_buffers(s);
02191 }
02192 break;
02193 case CODEC_ID_MPEG1VIDEO:
02194 case CODEC_ID_MPEG2VIDEO:
02195 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
02196 ff_mpeg1_encode_slice_header(s);
02197 ff_mpeg1_clean_buffers(s);
02198 }
02199 break;
02200 case CODEC_ID_H263:
02201 case CODEC_ID_H263P:
02202 if (CONFIG_H263_ENCODER)
02203 h263_encode_gob_header(s, mb_y);
02204 break;
02205 }
02206
02207 if(s->flags&CODEC_FLAG_PASS1){
02208 int bits= put_bits_count(&s->pb);
02209 s->misc_bits+= bits - s->last_bits;
02210 s->last_bits= bits;
02211 }
02212
02213 s->ptr_lastgob += current_packet_size;
02214 s->first_slice_line=1;
02215 s->resync_mb_x=mb_x;
02216 s->resync_mb_y=mb_y;
02217 }
02218 }
02219
02220 if( (s->resync_mb_x == s->mb_x)
02221 && s->resync_mb_y+1 == s->mb_y){
02222 s->first_slice_line=0;
02223 }
02224
02225 s->mb_skipped=0;
02226 s->dquant=0;
02227
02228 if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){
02229 int next_block=0;
02230 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
02231
02232 copy_context_before_encode(&backup_s, s, -1);
02233 backup_s.pb= s->pb;
02234 best_s.data_partitioning= s->data_partitioning;
02235 best_s.partitioned_frame= s->partitioned_frame;
02236 if(s->data_partitioning){
02237 backup_s.pb2= s->pb2;
02238 backup_s.tex_pb= s->tex_pb;
02239 }
02240
02241 if(mb_type&CANDIDATE_MB_TYPE_INTER){
02242 s->mv_dir = MV_DIR_FORWARD;
02243 s->mv_type = MV_TYPE_16X16;
02244 s->mb_intra= 0;
02245 s->mv[0][0][0] = s->p_mv_table[xy][0];
02246 s->mv[0][0][1] = s->p_mv_table[xy][1];
02247 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
02248 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
02249 }
02250 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
02251 s->mv_dir = MV_DIR_FORWARD;
02252 s->mv_type = MV_TYPE_FIELD;
02253 s->mb_intra= 0;
02254 for(i=0; i<2; i++){
02255 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
02256 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
02257 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
02258 }
02259 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
02260 &dmin, &next_block, 0, 0);
02261 }
02262 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
02263 s->mv_dir = MV_DIR_FORWARD;
02264 s->mv_type = MV_TYPE_16X16;
02265 s->mb_intra= 0;
02266 s->mv[0][0][0] = 0;
02267 s->mv[0][0][1] = 0;
02268 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
02269 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
02270 }
02271 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
02272 s->mv_dir = MV_DIR_FORWARD;
02273 s->mv_type = MV_TYPE_8X8;
02274 s->mb_intra= 0;
02275 for(i=0; i<4; i++){
02276 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
02277 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
02278 }
02279 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
02280 &dmin, &next_block, 0, 0);
02281 }
02282 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
02283 s->mv_dir = MV_DIR_FORWARD;
02284 s->mv_type = MV_TYPE_16X16;
02285 s->mb_intra= 0;
02286 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
02287 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
02288 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
02289 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
02290 }
02291 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
02292 s->mv_dir = MV_DIR_BACKWARD;
02293 s->mv_type = MV_TYPE_16X16;
02294 s->mb_intra= 0;
02295 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
02296 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
02297 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
02298 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
02299 }
02300 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
02301 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
02302 s->mv_type = MV_TYPE_16X16;
02303 s->mb_intra= 0;
02304 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
02305 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
02306 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
02307 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
02308 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
02309 &dmin, &next_block, 0, 0);
02310 }
02311 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
02312 s->mv_dir = MV_DIR_FORWARD;
02313 s->mv_type = MV_TYPE_FIELD;
02314 s->mb_intra= 0;
02315 for(i=0; i<2; i++){
02316 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
02317 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
02318 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
02319 }
02320 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
02321 &dmin, &next_block, 0, 0);
02322 }
02323 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
02324 s->mv_dir = MV_DIR_BACKWARD;
02325 s->mv_type = MV_TYPE_FIELD;
02326 s->mb_intra= 0;
02327 for(i=0; i<2; i++){
02328 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
02329 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
02330 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
02331 }
02332 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
02333 &dmin, &next_block, 0, 0);
02334 }
02335 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
02336 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
02337 s->mv_type = MV_TYPE_FIELD;
02338 s->mb_intra= 0;
02339 for(dir=0; dir<2; dir++){
02340 for(i=0; i<2; i++){
02341 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
02342 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
02343 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
02344 }
02345 }
02346 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
02347 &dmin, &next_block, 0, 0);
02348 }
02349 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
02350 s->mv_dir = 0;
02351 s->mv_type = MV_TYPE_16X16;
02352 s->mb_intra= 1;
02353 s->mv[0][0][0] = 0;
02354 s->mv[0][0][1] = 0;
02355 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
02356 &dmin, &next_block, 0, 0);
02357 if(s->h263_pred || s->h263_aic){
02358 if(best_s.mb_intra)
02359 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
02360 else
02361 ff_clean_intra_table_entries(s);
02362 }
02363 }
02364
02365 if((s->flags & CODEC_FLAG_QP_RD) && dmin < INT_MAX){
02366 if(best_s.mv_type==MV_TYPE_16X16){
02367 const int last_qp= backup_s.qscale;
02368 int qpi, qp, dc[6];
02369 DCTELEM ac[6][16];
02370 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
02371 static const int dquant_tab[4]={-1,1,-2,2};
02372
02373 assert(backup_s.dquant == 0);
02374
02375
02376 s->mv_dir= best_s.mv_dir;
02377 s->mv_type = MV_TYPE_16X16;
02378 s->mb_intra= best_s.mb_intra;
02379 s->mv[0][0][0] = best_s.mv[0][0][0];
02380 s->mv[0][0][1] = best_s.mv[0][0][1];
02381 s->mv[1][0][0] = best_s.mv[1][0][0];
02382 s->mv[1][0][1] = best_s.mv[1][0][1];
02383
02384 qpi = s->pict_type == FF_B_TYPE ? 2 : 0;
02385 for(; qpi<4; qpi++){
02386 int dquant= dquant_tab[qpi];
02387 qp= last_qp + dquant;
02388 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
02389 continue;
02390 backup_s.dquant= dquant;
02391 if(s->mb_intra && s->dc_val[0]){
02392 for(i=0; i<6; i++){
02393 dc[i]= s->dc_val[0][ s->block_index[i] ];
02394 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
02395 }
02396 }
02397
02398 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
02399 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
02400 if(best_s.qscale != qp){
02401 if(s->mb_intra && s->dc_val[0]){
02402 for(i=0; i<6; i++){
02403 s->dc_val[0][ s->block_index[i] ]= dc[i];
02404 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
02405 }
02406 }
02407 }
02408 }
02409 }
02410 }
02411 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
02412 int mx= s->b_direct_mv_table[xy][0];
02413 int my= s->b_direct_mv_table[xy][1];
02414
02415 backup_s.dquant = 0;
02416 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
02417 s->mb_intra= 0;
02418 ff_mpeg4_set_direct_mv(s, mx, my);
02419 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
02420 &dmin, &next_block, mx, my);
02421 }
02422 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
02423 backup_s.dquant = 0;
02424 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
02425 s->mb_intra= 0;
02426 ff_mpeg4_set_direct_mv(s, 0, 0);
02427 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
02428 &dmin, &next_block, 0, 0);
02429 }
02430 if(!best_s.mb_intra && s->flags2&CODEC_FLAG2_SKIP_RD){
02431 int coded=0;
02432 for(i=0; i<6; i++)
02433 coded |= s->block_last_index[i];
02434 if(coded){
02435 int mx,my;
02436 memcpy(s->mv, best_s.mv, sizeof(s->mv));
02437 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
02438 mx=my=0;
02439 ff_mpeg4_set_direct_mv(s, mx, my);
02440 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
02441 mx= s->mv[1][0][0];
02442 my= s->mv[1][0][1];
02443 }else{
02444 mx= s->mv[0][0][0];
02445 my= s->mv[0][0][1];
02446 }
02447
02448 s->mv_dir= best_s.mv_dir;
02449 s->mv_type = best_s.mv_type;
02450 s->mb_intra= 0;
02451
02452
02453
02454
02455 backup_s.dquant= 0;
02456 s->skipdct=1;
02457 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
02458 &dmin, &next_block, mx, my);
02459 s->skipdct=0;
02460 }
02461 }
02462
02463 s->current_picture.qscale_table[xy]= best_s.qscale;
02464
02465 copy_context_after_encode(s, &best_s, -1);
02466
02467 pb_bits_count= put_bits_count(&s->pb);
02468 flush_put_bits(&s->pb);
02469 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
02470 s->pb= backup_s.pb;
02471
02472 if(s->data_partitioning){
02473 pb2_bits_count= put_bits_count(&s->pb2);
02474 flush_put_bits(&s->pb2);
02475 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
02476 s->pb2= backup_s.pb2;
02477
02478 tex_pb_bits_count= put_bits_count(&s->tex_pb);
02479 flush_put_bits(&s->tex_pb);
02480 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
02481 s->tex_pb= backup_s.tex_pb;
02482 }
02483 s->last_bits= put_bits_count(&s->pb);
02484
02485 if (CONFIG_H263_ENCODER &&
02486 s->out_format == FMT_H263 && s->pict_type!=FF_B_TYPE)
02487 ff_h263_update_motion_val(s);
02488
02489 if(next_block==0){
02490 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
02491 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
02492 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
02493 }
02494
02495 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
02496 MPV_decode_mb(s, s->block);
02497 } else {
02498 int motion_x = 0, motion_y = 0;
02499 s->mv_type=MV_TYPE_16X16;
02500
02501
02502 switch(mb_type){
02503 case CANDIDATE_MB_TYPE_INTRA:
02504 s->mv_dir = 0;
02505 s->mb_intra= 1;
02506 motion_x= s->mv[0][0][0] = 0;
02507 motion_y= s->mv[0][0][1] = 0;
02508 break;
02509 case CANDIDATE_MB_TYPE_INTER:
02510 s->mv_dir = MV_DIR_FORWARD;
02511 s->mb_intra= 0;
02512 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
02513 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
02514 break;
02515 case CANDIDATE_MB_TYPE_INTER_I:
02516 s->mv_dir = MV_DIR_FORWARD;
02517 s->mv_type = MV_TYPE_FIELD;
02518 s->mb_intra= 0;
02519 for(i=0; i<2; i++){
02520 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
02521 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
02522 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
02523 }
02524 break;
02525 case CANDIDATE_MB_TYPE_INTER4V:
02526 s->mv_dir = MV_DIR_FORWARD;
02527 s->mv_type = MV_TYPE_8X8;
02528 s->mb_intra= 0;
02529 for(i=0; i<4; i++){
02530 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
02531 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
02532 }
02533 break;
02534 case CANDIDATE_MB_TYPE_DIRECT:
02535 if (CONFIG_MPEG4_ENCODER) {
02536 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
02537 s->mb_intra= 0;
02538 motion_x=s->b_direct_mv_table[xy][0];
02539 motion_y=s->b_direct_mv_table[xy][1];
02540 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
02541 }
02542 break;
02543 case CANDIDATE_MB_TYPE_DIRECT0:
02544 if (CONFIG_MPEG4_ENCODER) {
02545 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
02546 s->mb_intra= 0;
02547 ff_mpeg4_set_direct_mv(s, 0, 0);
02548 }
02549 break;
02550 case CANDIDATE_MB_TYPE_BIDIR:
02551 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
02552 s->mb_intra= 0;
02553 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
02554 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
02555 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
02556 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
02557 break;
02558 case CANDIDATE_MB_TYPE_BACKWARD:
02559 s->mv_dir = MV_DIR_BACKWARD;
02560 s->mb_intra= 0;
02561 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
02562 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
02563 break;
02564 case CANDIDATE_MB_TYPE_FORWARD:
02565 s->mv_dir = MV_DIR_FORWARD;
02566 s->mb_intra= 0;
02567 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
02568 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
02569
02570 break;
02571 case CANDIDATE_MB_TYPE_FORWARD_I:
02572 s->mv_dir = MV_DIR_FORWARD;
02573 s->mv_type = MV_TYPE_FIELD;
02574 s->mb_intra= 0;
02575 for(i=0; i<2; i++){
02576 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
02577 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
02578 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
02579 }
02580 break;
02581 case CANDIDATE_MB_TYPE_BACKWARD_I:
02582 s->mv_dir = MV_DIR_BACKWARD;
02583 s->mv_type = MV_TYPE_FIELD;
02584 s->mb_intra= 0;
02585 for(i=0; i<2; i++){
02586 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
02587 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
02588 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
02589 }
02590 break;
02591 case CANDIDATE_MB_TYPE_BIDIR_I:
02592 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
02593 s->mv_type = MV_TYPE_FIELD;
02594 s->mb_intra= 0;
02595 for(dir=0; dir<2; dir++){
02596 for(i=0; i<2; i++){
02597 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
02598 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
02599 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
02600 }
02601 }
02602 break;
02603 default:
02604 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
02605 }
02606
02607 encode_mb(s, motion_x, motion_y);
02608
02609
02610 s->last_mv_dir = s->mv_dir;
02611
02612 if (CONFIG_H263_ENCODER &&
02613 s->out_format == FMT_H263 && s->pict_type!=FF_B_TYPE)
02614 ff_h263_update_motion_val(s);
02615
02616 MPV_decode_mb(s, s->block);
02617 }
02618
02619
02620 if(s->mb_intra ){
02621 s->p_mv_table[xy][0]=0;
02622 s->p_mv_table[xy][1]=0;
02623 }
02624
02625 if(s->flags&CODEC_FLAG_PSNR){
02626 int w= 16;
02627 int h= 16;
02628
02629 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
02630 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
02631
02632 s->current_picture.error[0] += sse(
02633 s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
02634 s->dest[0], w, h, s->linesize);
02635 s->current_picture.error[1] += sse(
02636 s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
02637 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
02638 s->current_picture.error[2] += sse(
02639 s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
02640 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
02641 }
02642 if(s->loop_filter){
02643 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
02644 ff_h263_loop_filter(s);
02645 }
02646
02647 }
02648 }
02649
02650
02651 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == FF_I_TYPE)
02652 msmpeg4_encode_ext_header(s);
02653
02654 write_slice_end(s);
02655
02656
02657 if (s->avctx->rtp_callback) {
02658 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
02659 pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
02660
02661 emms_c();
02662 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
02663 }
02664
02665 return 0;
02666 }
02667
02668 #define MERGE(field) dst->field += src->field; src->field=0
02669 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
02670 MERGE(me.scene_change_score);
02671 MERGE(me.mc_mb_var_sum_temp);
02672 MERGE(me.mb_var_sum_temp);
02673 }
02674
02675 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
02676 int i;
02677
02678 MERGE(dct_count[0]);
02679 MERGE(dct_count[1]);
02680 MERGE(mv_bits);
02681 MERGE(i_tex_bits);
02682 MERGE(p_tex_bits);
02683 MERGE(i_count);
02684 MERGE(f_count);
02685 MERGE(b_count);
02686 MERGE(skip_count);
02687 MERGE(misc_bits);
02688 MERGE(error_count);
02689 MERGE(padding_bug_score);
02690 MERGE(current_picture.error[0]);
02691 MERGE(current_picture.error[1]);
02692 MERGE(current_picture.error[2]);
02693
02694 if(dst->avctx->noise_reduction){
02695 for(i=0; i<64; i++){
02696 MERGE(dct_error_sum[0][i]);
02697 MERGE(dct_error_sum[1][i]);
02698 }
02699 }
02700
02701 assert(put_bits_count(&src->pb) % 8 ==0);
02702 assert(put_bits_count(&dst->pb) % 8 ==0);
02703 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
02704 flush_put_bits(&dst->pb);
02705 }
02706
02707 static int estimate_qp(MpegEncContext *s, int dry_run){
02708 if (s->next_lambda){
02709 s->current_picture_ptr->quality=
02710 s->current_picture.quality = s->next_lambda;
02711 if(!dry_run) s->next_lambda= 0;
02712 } else if (!s->fixed_qscale) {
02713 s->current_picture_ptr->quality=
02714 s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run);
02715 if (s->current_picture.quality < 0)
02716 return -1;
02717 }
02718
02719 if(s->adaptive_quant){
02720 switch(s->codec_id){
02721 case CODEC_ID_MPEG4:
02722 if (CONFIG_MPEG4_ENCODER)
02723 ff_clean_mpeg4_qscales(s);
02724 break;
02725 case CODEC_ID_H263:
02726 case CODEC_ID_H263P:
02727 case CODEC_ID_FLV1:
02728 if (CONFIG_H263_ENCODER)
02729 ff_clean_h263_qscales(s);
02730 break;
02731 default:
02732 ff_init_qscale_tab(s);
02733 }
02734
02735 s->lambda= s->lambda_table[0];
02736
02737 }else
02738 s->lambda= s->current_picture.quality;
02739
02740 update_qscale(s);
02741 return 0;
02742 }
02743
02744
02745 static void set_frame_distances(MpegEncContext * s){
02746 assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE);
02747 s->time= s->current_picture_ptr->pts*s->avctx->time_base.num;
02748
02749 if(s->pict_type==FF_B_TYPE){
02750 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
02751 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
02752 }else{
02753 s->pp_time= s->time - s->last_non_b_time;
02754 s->last_non_b_time= s->time;
02755 assert(s->picture_number==0 || s->pp_time > 0);
02756 }
02757 }
02758
02759 static int encode_picture(MpegEncContext *s, int picture_number)
02760 {
02761 int i;
02762 int bits;
02763
02764 s->picture_number = picture_number;
02765
02766
02767 s->me.mb_var_sum_temp =
02768 s->me.mc_mb_var_sum_temp = 0;
02769
02770
02771
02772 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
02773 set_frame_distances(s);
02774 if(CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
02775 ff_set_mpeg4_time(s);
02776
02777 s->me.scene_change_score=0;
02778
02779
02780
02781 if(s->pict_type==FF_I_TYPE){
02782 if(s->msmpeg4_version >= 3) s->no_rounding=1;
02783 else s->no_rounding=0;
02784 }else if(s->pict_type!=FF_B_TYPE){
02785 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
02786 s->no_rounding ^= 1;
02787 }
02788
02789 if(s->flags & CODEC_FLAG_PASS2){
02790 if (estimate_qp(s,1) < 0)
02791 return -1;
02792 ff_get_2pass_fcode(s);
02793 }else if(!(s->flags & CODEC_FLAG_QSCALE)){
02794 if(s->pict_type==FF_B_TYPE)
02795 s->lambda= s->last_lambda_for[s->pict_type];
02796 else
02797 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
02798 update_qscale(s);
02799 }
02800
02801 s->mb_intra=0;
02802 for(i=1; i<s->avctx->thread_count; i++){
02803 ff_update_duplicate_context(s->thread_context[i], s);
02804 }
02805
02806 if(ff_init_me(s)<0)
02807 return -1;
02808
02809
02810 if(s->pict_type != FF_I_TYPE){
02811 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
02812 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
02813 if(s->pict_type != FF_B_TYPE && s->avctx->me_threshold==0){
02814 if((s->avctx->pre_me && s->last_non_b_pict_type==FF_I_TYPE) || s->avctx->pre_me==2){
02815 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
02816 }
02817 }
02818
02819 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
02820 }else {
02821
02822 for(i=0; i<s->mb_stride*s->mb_height; i++)
02823 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
02824
02825 if(!s->fixed_qscale){
02826
02827 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
02828 }
02829 }
02830 for(i=1; i<s->avctx->thread_count; i++){
02831 merge_context_after_me(s, s->thread_context[i]);
02832 }
02833 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
02834 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
02835 emms_c();
02836
02837 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == FF_P_TYPE){
02838 s->pict_type= FF_I_TYPE;
02839 for(i=0; i<s->mb_stride*s->mb_height; i++)
02840 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
02841
02842 }
02843
02844 if(!s->umvplus){
02845 if(s->pict_type==FF_P_TYPE || s->pict_type==FF_S_TYPE) {
02846 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
02847
02848 if(s->flags & CODEC_FLAG_INTERLACED_ME){
02849 int a,b;
02850 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I);
02851 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
02852 s->f_code= FFMAX3(s->f_code, a, b);
02853 }
02854
02855 ff_fix_long_p_mvs(s);
02856 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
02857 if(s->flags & CODEC_FLAG_INTERLACED_ME){
02858 int j;
02859 for(i=0; i<2; i++){
02860 for(j=0; j<2; j++)
02861 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
02862 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
02863 }
02864 }
02865 }
02866
02867 if(s->pict_type==FF_B_TYPE){
02868 int a, b;
02869
02870 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
02871 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
02872 s->f_code = FFMAX(a, b);
02873
02874 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
02875 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
02876 s->b_code = FFMAX(a, b);
02877
02878 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
02879 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
02880 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
02881 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
02882 if(s->flags & CODEC_FLAG_INTERLACED_ME){
02883 int dir, j;
02884 for(dir=0; dir<2; dir++){
02885 for(i=0; i<2; i++){
02886 for(j=0; j<2; j++){
02887 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
02888 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
02889 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
02890 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
02891 }
02892 }
02893 }
02894 }
02895 }
02896 }
02897
02898 if (estimate_qp(s, 0) < 0)
02899 return -1;
02900
02901 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==FF_I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
02902 s->qscale= 3;
02903
02904 if (s->out_format == FMT_MJPEG) {
02905
02906 for(i=1;i<64;i++){
02907 int j= s->dsp.idct_permutation[i];
02908
02909 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
02910 }
02911 s->y_dc_scale_table=
02912 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
02913 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
02914 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
02915 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
02916 s->qscale= 8;
02917 }
02918
02919
02920 s->current_picture_ptr->key_frame=
02921 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
02922 s->current_picture_ptr->pict_type=
02923 s->current_picture.pict_type= s->pict_type;
02924
02925 if(s->current_picture.key_frame)
02926 s->picture_in_gop_number=0;
02927
02928 s->last_bits= put_bits_count(&s->pb);
02929 switch(s->out_format) {
02930 case FMT_MJPEG:
02931 if (CONFIG_MJPEG_ENCODER)
02932 ff_mjpeg_encode_picture_header(s);
02933 break;
02934 case FMT_H261:
02935 if (CONFIG_H261_ENCODER)
02936 ff_h261_encode_picture_header(s, picture_number);
02937 break;
02938 case FMT_H263:
02939 if (CONFIG_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
02940 ff_wmv2_encode_picture_header(s, picture_number);
02941 else if (CONFIG_MSMPEG4_ENCODER && s->h263_msmpeg4)
02942 msmpeg4_encode_picture_header(s, picture_number);
02943 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
02944 mpeg4_encode_picture_header(s, picture_number);
02945 else if (CONFIG_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
02946 rv10_encode_picture_header(s, picture_number);
02947 else if (CONFIG_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
02948 rv20_encode_picture_header(s, picture_number);
02949 else if (CONFIG_FLV_ENCODER && s->codec_id == CODEC_ID_FLV1)
02950 ff_flv_encode_picture_header(s, picture_number);
02951 else if (CONFIG_H263_ENCODER)
02952 h263_encode_picture_header(s, picture_number);
02953 break;
02954 case FMT_MPEG1:
02955 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
02956 mpeg1_encode_picture_header(s, picture_number);
02957 break;
02958 case FMT_H264:
02959 break;
02960 default:
02961 assert(0);
02962 }
02963 bits= put_bits_count(&s->pb);
02964 s->header_bits= bits - s->last_bits;
02965
02966 for(i=1; i<s->avctx->thread_count; i++){
02967 update_duplicate_context_after_me(s->thread_context[i], s);
02968 }
02969 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
02970 for(i=1; i<s->avctx->thread_count; i++){
02971 merge_context_after_encode(s, s->thread_context[i]);
02972 }
02973 emms_c();
02974 return 0;
02975 }
02976
02977 void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
02978 const int intra= s->mb_intra;
02979 int i;
02980
02981 s->dct_count[intra]++;
02982
02983 for(i=0; i<64; i++){
02984 int level= block[i];
02985
02986 if(level){
02987 if(level>0){
02988 s->dct_error_sum[intra][i] += level;
02989 level -= s->dct_offset[intra][i];
02990 if(level<0) level=0;
02991 }else{
02992 s->dct_error_sum[intra][i] -= level;
02993 level += s->dct_offset[intra][i];
02994 if(level>0) level=0;
02995 }
02996 block[i]= level;
02997 }
02998 }
02999 }
03000
03001 int dct_quantize_trellis_c(MpegEncContext *s,
03002 DCTELEM *block, int n,
03003 int qscale, int *overflow){
03004 const int *qmat;
03005 const uint8_t *scantable= s->intra_scantable.scantable;
03006 const uint8_t *perm_scantable= s->intra_scantable.permutated;
03007 int max=0;
03008 unsigned int threshold1, threshold2;
03009 int bias=0;
03010 int run_tab[65];
03011 int level_tab[65];
03012 int score_tab[65];
03013 int survivor[65];
03014 int survivor_count;
03015 int last_run=0;
03016 int last_level=0;
03017 int last_score= 0;
03018 int last_i;
03019 int coeff[2][64];
03020 int coeff_count[64];
03021 int qmul, qadd, start_i, last_non_zero, i, dc;
03022 const int esc_length= s->ac_esc_length;
03023 uint8_t * length;
03024 uint8_t * last_length;
03025 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
03026
03027 s->dsp.fdct (block);
03028
03029 if(s->dct_error_sum)
03030 s->denoise_dct(s, block);
03031 qmul= qscale*16;
03032 qadd= ((qscale-1)|1)*8;
03033
03034 if (s->mb_intra) {
03035 int q;
03036 if (!s->h263_aic) {
03037 if (n < 4)
03038 q = s->y_dc_scale;
03039 else
03040 q = s->c_dc_scale;
03041 q = q << 3;
03042 } else{
03043
03044 q = 1 << 3;
03045 qadd=0;
03046 }
03047
03048
03049 block[0] = (block[0] + (q >> 1)) / q;
03050 start_i = 1;
03051 last_non_zero = 0;
03052 qmat = s->q_intra_matrix[qscale];
03053 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
03054 bias= 1<<(QMAT_SHIFT-1);
03055 length = s->intra_ac_vlc_length;
03056 last_length= s->intra_ac_vlc_last_length;
03057 } else {
03058 start_i = 0;
03059 last_non_zero = -1;
03060 qmat = s->q_inter_matrix[qscale];
03061 length = s->inter_ac_vlc_length;
03062 last_length= s->inter_ac_vlc_last_length;
03063 }
03064 last_i= start_i;
03065
03066 threshold1= (1<<QMAT_SHIFT) - bias - 1;
03067 threshold2= (threshold1<<1);
03068
03069 for(i=63; i>=start_i; i--) {
03070 const int j = scantable[i];
03071 int level = block[j] * qmat[j];
03072
03073 if(((unsigned)(level+threshold1))>threshold2){
03074 last_non_zero = i;
03075 break;
03076 }
03077 }
03078
03079 for(i=start_i; i<=last_non_zero; i++) {
03080 const int j = scantable[i];
03081 int level = block[j] * qmat[j];
03082
03083
03084
03085 if(((unsigned)(level+threshold1))>threshold2){
03086 if(level>0){
03087 level= (bias + level)>>QMAT_SHIFT;
03088 coeff[0][i]= level;
03089 coeff[1][i]= level-1;
03090
03091 }else{
03092 level= (bias - level)>>QMAT_SHIFT;
03093 coeff[0][i]= -level;
03094 coeff[1][i]= -level+1;
03095
03096 }
03097 coeff_count[i]= FFMIN(level, 2);
03098 assert(coeff_count[i]);
03099 max |=level;
03100 }else{
03101 coeff[0][i]= (level>>31)|1;
03102 coeff_count[i]= 1;
03103 }
03104 }
03105
03106 *overflow= s->max_qcoeff < max;
03107
03108 if(last_non_zero < start_i){
03109 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
03110 return last_non_zero;
03111 }
03112
03113 score_tab[start_i]= 0;
03114 survivor[0]= start_i;
03115 survivor_count= 1;
03116
03117 for(i=start_i; i<=last_non_zero; i++){
03118 int level_index, j, zero_distortion;
03119 int dct_coeff= FFABS(block[ scantable[i] ]);
03120 int best_score=256*256*256*120;
03121
03122 if ( s->dsp.fdct == fdct_ifast
03123 #ifndef FAAN_POSTSCALE
03124 || s->dsp.fdct == ff_faandct
03125 #endif
03126 )
03127 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
03128 zero_distortion= dct_coeff*dct_coeff;
03129
03130 for(level_index=0; level_index < coeff_count[i]; level_index++){
03131 int distortion;
03132 int level= coeff[level_index][i];
03133 const int alevel= FFABS(level);
03134 int unquant_coeff;
03135
03136 assert(level);
03137
03138 if(s->out_format == FMT_H263){
03139 unquant_coeff= alevel*qmul + qadd;
03140 }else{
03141 j= s->dsp.idct_permutation[ scantable[i] ];
03142 if(s->mb_intra){
03143 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
03144 unquant_coeff = (unquant_coeff - 1) | 1;
03145 }else{
03146 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
03147 unquant_coeff = (unquant_coeff - 1) | 1;
03148 }
03149 unquant_coeff<<= 3;
03150 }
03151
03152 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
03153 level+=64;
03154 if((level&(~127)) == 0){
03155 for(j=survivor_count-1; j>=0; j--){
03156 int run= i - survivor[j];
03157 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
03158 score += score_tab[i-run];
03159
03160 if(score < best_score){
03161 best_score= score;
03162 run_tab[i+1]= run;
03163 level_tab[i+1]= level-64;
03164 }
03165 }
03166
03167 if(s->out_format == FMT_H263){
03168 for(j=survivor_count-1; j>=0; j--){
03169 int run= i - survivor[j];
03170 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
03171 score += score_tab[i-run];
03172 if(score < last_score){
03173 last_score= score;
03174 last_run= run;
03175 last_level= level-64;
03176 last_i= i+1;
03177 }
03178 }
03179 }
03180 }else{
03181 distortion += esc_length*lambda;
03182 for(j=survivor_count-1; j>=0; j--){
03183 int run= i - survivor[j];
03184 int score= distortion + score_tab[i-run];
03185
03186 if(score < best_score){
03187 best_score= score;
03188 run_tab[i+1]= run;
03189 level_tab[i+1]= level-64;
03190 }
03191 }
03192
03193 if(s->out_format == FMT_H263){
03194 for(j=survivor_count-1; j>=0; j--){
03195 int run= i - survivor[j];
03196 int score= distortion + score_tab[i-run];
03197 if(score < last_score){
03198 last_score= score;
03199 last_run= run;
03200 last_level= level-64;
03201 last_i= i+1;
03202 }
03203 }
03204 }
03205 }
03206 }
03207
03208 score_tab[i+1]= best_score;
03209
03210
03211 if(last_non_zero <= 27){
03212 for(; survivor_count; survivor_count--){
03213 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
03214 break;
03215 }
03216 }else{
03217 for(; survivor_count; survivor_count--){
03218 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
03219 break;
03220 }
03221 }
03222
03223 survivor[ survivor_count++ ]= i+1;
03224 }
03225
03226 if(s->out_format != FMT_H263){
03227 last_score= 256*256*256*120;
03228 for(i= survivor[0]; i<=last_non_zero + 1; i++){
03229 int score= score_tab[i];
03230 if(i) score += lambda*2;
03231
03232 if(score < last_score){
03233 last_score= score;
03234 last_i= i;
03235 last_level= level_tab[i];
03236 last_run= run_tab[i];
03237 }
03238 }
03239 }
03240
03241 s->coded_score[n] = last_score;
03242
03243 dc= FFABS(block[0]);
03244 last_non_zero= last_i - 1;
03245 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
03246
03247 if(last_non_zero < start_i)
03248 return last_non_zero;
03249
03250 if(last_non_zero == 0 && start_i == 0){
03251 int best_level= 0;
03252 int best_score= dc * dc;
03253
03254 for(i=0; i<coeff_count[0]; i++){
03255 int level= coeff[i][0];
03256 int alevel= FFABS(level);
03257 int unquant_coeff, score, distortion;
03258
03259 if(s->out_format == FMT_H263){
03260 unquant_coeff= (alevel*qmul + qadd)>>3;
03261 }else{
03262 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
03263 unquant_coeff = (unquant_coeff - 1) | 1;
03264 }
03265 unquant_coeff = (unquant_coeff + 4) >> 3;
03266 unquant_coeff<<= 3 + 3;
03267
03268 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
03269 level+=64;
03270 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
03271 else score= distortion + esc_length*lambda;
03272
03273 if(score < best_score){
03274 best_score= score;
03275 best_level= level - 64;
03276 }
03277 }
03278 block[0]= best_level;
03279 s->coded_score[n] = best_score - dc*dc;
03280 if(best_level == 0) return -1;
03281 else return last_non_zero;
03282 }
03283
03284 i= last_i;
03285 assert(last_level);
03286
03287 block[ perm_scantable[last_non_zero] ]= last_level;
03288 i -= last_run + 1;
03289
03290 for(; i>start_i; i -= run_tab[i] + 1){
03291 block[ perm_scantable[i-1] ]= level_tab[i];
03292 }
03293
03294 return last_non_zero;
03295 }
03296
03297
03298 static int16_t basis[64][64];
03299
03300 static void build_basis(uint8_t *perm){
03301 int i, j, x, y;
03302 emms_c();
03303 for(i=0; i<8; i++){
03304 for(j=0; j<8; j++){
03305 for(y=0; y<8; y++){
03306 for(x=0; x<8; x++){
03307 double s= 0.25*(1<<BASIS_SHIFT);
03308 int index= 8*i + j;
03309 int perm_index= perm[index];
03310 if(i==0) s*= sqrt(0.5);
03311 if(j==0) s*= sqrt(0.5);
03312 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)));
03313 }
03314 }
03315 }
03316 }
03317 }
03318
03319 static int dct_quantize_refine(MpegEncContext *s,
03320 DCTELEM *block, int16_t *weight, DCTELEM *orig,
03321 int n, int qscale){
03322 int16_t rem[64];
03323 LOCAL_ALIGNED_16(DCTELEM, d1, [64]);
03324 const uint8_t *scantable= s->intra_scantable.scantable;
03325 const uint8_t *perm_scantable= s->intra_scantable.permutated;
03326
03327
03328 int run_tab[65];
03329 int prev_run=0;
03330 int prev_level=0;
03331 int qmul, qadd, start_i, last_non_zero, i, dc;
03332 uint8_t * length;
03333 uint8_t * last_length;
03334 int lambda;
03335 int rle_index, run, q = 1, sum;
03336 #ifdef REFINE_STATS
03337 static int count=0;
03338 static int after_last=0;
03339 static int to_zero=0;
03340 static int from_zero=0;
03341 static int raise=0;
03342 static int lower=0;
03343 static int messed_sign=0;
03344 #endif
03345
03346 if(basis[0][0] == 0)
03347 build_basis(s->dsp.idct_permutation);
03348
03349 qmul= qscale*2;
03350 qadd= (qscale-1)|1;
03351 if (s->mb_intra) {
03352 if (!s->h263_aic) {
03353 if (n < 4)
03354 q = s->y_dc_scale;
03355 else
03356 q = s->c_dc_scale;
03357 } else{
03358
03359 q = 1;
03360 qadd=0;
03361 }
03362 q <<= RECON_SHIFT-3;
03363
03364 dc= block[0]*q;
03365
03366 start_i = 1;
03367
03368
03369 length = s->intra_ac_vlc_length;
03370 last_length= s->intra_ac_vlc_last_length;
03371 } else {
03372 dc= 0;
03373 start_i = 0;
03374 length = s->inter_ac_vlc_length;
03375 last_length= s->inter_ac_vlc_last_length;
03376 }
03377 last_non_zero = s->block_last_index[n];
03378
03379 #ifdef REFINE_STATS
03380 {START_TIMER
03381 #endif
03382 dc += (1<<(RECON_SHIFT-1));
03383 for(i=0; i<64; i++){
03384 rem[i]= dc - (orig[i]<<RECON_SHIFT);
03385 }
03386 #ifdef REFINE_STATS
03387 STOP_TIMER("memset rem[]")}
03388 #endif
03389 sum=0;
03390 for(i=0; i<64; i++){
03391 int one= 36;
03392 int qns=4;
03393 int w;
03394
03395 w= FFABS(weight[i]) + qns*one;
03396 w= 15 + (48*qns*one + w/2)/w;
03397
03398 weight[i] = w;
03399
03400
03401 assert(w>0);
03402 assert(w<(1<<6));
03403 sum += w*w;
03404 }
03405 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
03406 #ifdef REFINE_STATS
03407 {START_TIMER
03408 #endif
03409 run=0;
03410 rle_index=0;
03411 for(i=start_i; i<=last_non_zero; i++){
03412 int j= perm_scantable[i];
03413 const int level= block[j];
03414 int coeff;
03415
03416 if(level){
03417 if(level<0) coeff= qmul*level - qadd;
03418 else coeff= qmul*level + qadd;
03419 run_tab[rle_index++]=run;
03420 run=0;
03421
03422 s->dsp.add_8x8basis(rem, basis[j], coeff);
03423 }else{
03424 run++;
03425 }
03426 }
03427 #ifdef REFINE_STATS
03428 if(last_non_zero>0){
03429 STOP_TIMER("init rem[]")
03430 }
03431 }
03432
03433 {START_TIMER
03434 #endif
03435 for(;;){
03436 int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
03437 int best_coeff=0;
03438 int best_change=0;
03439 int run2, best_unquant_change=0, analyze_gradient;
03440 #ifdef REFINE_STATS
03441 {START_TIMER
03442 #endif
03443 analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
03444
03445 if(analyze_gradient){
03446 #ifdef REFINE_STATS
03447 {START_TIMER
03448 #endif
03449 for(i=0; i<64; i++){
03450 int w= weight[i];
03451
03452 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
03453 }
03454 #ifdef REFINE_STATS
03455 STOP_TIMER("rem*w*w")}
03456 {START_TIMER
03457 #endif
03458 s->dsp.fdct(d1);
03459 #ifdef REFINE_STATS
03460 STOP_TIMER("dct")}
03461 #endif
03462 }
03463
03464 if(start_i){
03465 const int level= block[0];
03466 int change, old_coeff;
03467
03468 assert(s->mb_intra);
03469
03470 old_coeff= q*level;
03471
03472 for(change=-1; change<=1; change+=2){
03473 int new_level= level + change;
03474 int score, new_coeff;
03475
03476 new_coeff= q*new_level;
03477 if(new_coeff >= 2048 || new_coeff < 0)
03478 continue;
03479
03480 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
03481 if(score<best_score){
03482 best_score= score;
03483 best_coeff= 0;
03484 best_change= change;
03485 best_unquant_change= new_coeff - old_coeff;
03486 }
03487 }
03488 }
03489
03490 run=0;
03491 rle_index=0;
03492 run2= run_tab[rle_index++];
03493 prev_level=0;
03494 prev_run=0;
03495
03496 for(i=start_i; i<64; i++){
03497 int j= perm_scantable[i];
03498 const int level= block[j];
03499 int change, old_coeff;
03500
03501 if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
03502 break;
03503
03504 if(level){
03505 if(level<0) old_coeff= qmul*level - qadd;
03506 else old_coeff= qmul*level + qadd;
03507 run2= run_tab[rle_index++];
03508 }else{
03509 old_coeff=0;
03510 run2--;
03511 assert(run2>=0 || i >= last_non_zero );
03512 }
03513
03514 for(change=-1; change<=1; change+=2){
03515 int new_level= level + change;
03516 int score, new_coeff, unquant_change;
03517
03518 score=0;
03519 if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
03520 continue;
03521
03522 if(new_level){
03523 if(new_level<0) new_coeff= qmul*new_level - qadd;
03524 else new_coeff= qmul*new_level + qadd;
03525 if(new_coeff >= 2048 || new_coeff <= -2048)
03526 continue;
03527
03528
03529 if(level){
03530 if(level < 63 && level > -63){
03531 if(i < last_non_zero)
03532 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
03533 - length[UNI_AC_ENC_INDEX(run, level+64)];
03534 else
03535 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
03536 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
03537 }
03538 }else{
03539 assert(FFABS(new_level)==1);
03540
03541 if(analyze_gradient){
03542 int g= d1[ scantable[i] ];
03543 if(g && (g^new_level) >= 0)
03544 continue;
03545 }
03546
03547 if(i < last_non_zero){
03548 int next_i= i + run2 + 1;
03549 int next_level= block[ perm_scantable[next_i] ] + 64;
03550
03551 if(next_level&(~127))
03552 next_level= 0;
03553
03554 if(next_i < last_non_zero)
03555 score += length[UNI_AC_ENC_INDEX(run, 65)]
03556 + length[UNI_AC_ENC_INDEX(run2, next_level)]
03557 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
03558 else
03559 score += length[UNI_AC_ENC_INDEX(run, 65)]
03560 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
03561 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
03562 }else{
03563 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
03564 if(prev_level){
03565 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
03566 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
03567 }
03568 }
03569 }
03570 }else{
03571 new_coeff=0;
03572 assert(FFABS(level)==1);
03573
03574 if(i < last_non_zero){
03575 int next_i= i + run2 + 1;
03576 int next_level= block[ perm_scantable[next_i] ] + 64;
03577
03578 if(next_level&(~127))
03579 next_level= 0;
03580
03581 if(next_i < last_non_zero)
03582 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
03583 - length[UNI_AC_ENC_INDEX(run2, next_level)]
03584 - length[UNI_AC_ENC_INDEX(run, 65)];
03585 else
03586 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
03587 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
03588 - length[UNI_AC_ENC_INDEX(run, 65)];
03589 }else{
03590 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
03591 if(prev_level){
03592 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
03593 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
03594 }
03595 }
03596 }
03597
03598 score *= lambda;
03599
03600 unquant_change= new_coeff - old_coeff;
03601 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
03602
03603 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
03604 if(score<best_score){
03605 best_score= score;
03606 best_coeff= i;
03607 best_change= change;
03608 best_unquant_change= unquant_change;
03609 }
03610 }
03611 if(level){
03612 prev_level= level + 64;
03613 if(prev_level&(~127))
03614 prev_level= 0;
03615 prev_run= run;
03616 run=0;
03617 }else{
03618 run++;
03619 }
03620 }
03621 #ifdef REFINE_STATS
03622 STOP_TIMER("iterative step")}
03623 #endif
03624
03625 if(best_change){
03626 int j= perm_scantable[ best_coeff ];
03627
03628 block[j] += best_change;
03629
03630 if(best_coeff > last_non_zero){
03631 last_non_zero= best_coeff;
03632 assert(block[j]);
03633 #ifdef REFINE_STATS
03634 after_last++;
03635 #endif
03636 }else{
03637 #ifdef REFINE_STATS
03638 if(block[j]){
03639 if(block[j] - best_change){
03640 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
03641 raise++;
03642 }else{
03643 lower++;
03644 }
03645 }else{
03646 from_zero++;
03647 }
03648 }else{
03649 to_zero++;
03650 }
03651 #endif
03652 for(; last_non_zero>=start_i; last_non_zero--){
03653 if(block[perm_scantable[last_non_zero]])
03654 break;
03655 }
03656 }
03657 #ifdef REFINE_STATS
03658 count++;
03659 if(256*256*256*64 % count == 0){
03660 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);
03661 }
03662 #endif
03663 run=0;
03664 rle_index=0;
03665 for(i=start_i; i<=last_non_zero; i++){
03666 int j= perm_scantable[i];
03667 const int level= block[j];
03668
03669 if(level){
03670 run_tab[rle_index++]=run;
03671 run=0;
03672 }else{
03673 run++;
03674 }
03675 }
03676
03677 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
03678 }else{
03679 break;
03680 }
03681 }
03682 #ifdef REFINE_STATS
03683 if(last_non_zero>0){
03684 STOP_TIMER("iterative search")
03685 }
03686 }
03687 #endif
03688
03689 return last_non_zero;
03690 }
03691
03692 int dct_quantize_c(MpegEncContext *s,
03693 DCTELEM *block, int n,
03694 int qscale, int *overflow)
03695 {
03696 int i, j, level, last_non_zero, q, start_i;
03697 const int *qmat;
03698 const uint8_t *scantable= s->intra_scantable.scantable;
03699 int bias;
03700 int max=0;
03701 unsigned int threshold1, threshold2;
03702
03703 s->dsp.fdct (block);
03704
03705 if(s->dct_error_sum)
03706 s->denoise_dct(s, block);
03707
03708 if (s->mb_intra) {
03709 if (!s->h263_aic) {
03710 if (n < 4)
03711 q = s->y_dc_scale;
03712 else
03713 q = s->c_dc_scale;
03714 q = q << 3;
03715 } else
03716
03717 q = 1 << 3;
03718
03719
03720 block[0] = (block[0] + (q >> 1)) / q;
03721 start_i = 1;
03722 last_non_zero = 0;
03723 qmat = s->q_intra_matrix[qscale];
03724 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
03725 } else {
03726 start_i = 0;
03727 last_non_zero = -1;
03728 qmat = s->q_inter_matrix[qscale];
03729 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
03730 }
03731 threshold1= (1<<QMAT_SHIFT) - bias - 1;
03732 threshold2= (threshold1<<1);
03733 for(i=63;i>=start_i;i--) {
03734 j = scantable[i];
03735 level = block[j] * qmat[j];
03736
03737 if(((unsigned)(level+threshold1))>threshold2){
03738 last_non_zero = i;
03739 break;
03740 }else{
03741 block[j]=0;
03742 }
03743 }
03744 for(i=start_i; i<=last_non_zero; i++) {
03745 j = scantable[i];
03746 level = block[j] * qmat[j];
03747
03748
03749
03750 if(((unsigned)(level+threshold1))>threshold2){
03751 if(level>0){
03752 level= (bias + level)>>QMAT_SHIFT;
03753 block[j]= level;
03754 }else{
03755 level= (bias - level)>>QMAT_SHIFT;
03756 block[j]= -level;
03757 }
03758 max |=level;
03759 }else{
03760 block[j]=0;
03761 }
03762 }
03763 *overflow= s->max_qcoeff < max;
03764
03765
03766 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
03767 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
03768
03769 return last_non_zero;
03770 }
03771
03772 AVCodec h263_encoder = {
03773 "h263",
03774 AVMEDIA_TYPE_VIDEO,
03775 CODEC_ID_H263,
03776 sizeof(MpegEncContext),
03777 MPV_encode_init,
03778 MPV_encode_picture,
03779 MPV_encode_end,
03780 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
03781 .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
03782 };
03783
03784 AVCodec h263p_encoder = {
03785 "h263p",
03786 AVMEDIA_TYPE_VIDEO,
03787 CODEC_ID_H263P,
03788 sizeof(MpegEncContext),
03789 MPV_encode_init,
03790 MPV_encode_picture,
03791 MPV_encode_end,
03792 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
03793 .long_name= NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
03794 };
03795
03796 AVCodec msmpeg4v1_encoder = {
03797 "msmpeg4v1",
03798 AVMEDIA_TYPE_VIDEO,
03799 CODEC_ID_MSMPEG4V1,
03800 sizeof(MpegEncContext),
03801 MPV_encode_init,
03802 MPV_encode_picture,
03803 MPV_encode_end,
03804 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
03805 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
03806 };
03807
03808 AVCodec msmpeg4v2_encoder = {
03809 "msmpeg4v2",
03810 AVMEDIA_TYPE_VIDEO,
03811 CODEC_ID_MSMPEG4V2,
03812 sizeof(MpegEncContext),
03813 MPV_encode_init,
03814 MPV_encode_picture,
03815 MPV_encode_end,
03816 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
03817 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
03818 };
03819
03820 AVCodec msmpeg4v3_encoder = {
03821 "msmpeg4",
03822 AVMEDIA_TYPE_VIDEO,
03823 CODEC_ID_MSMPEG4V3,
03824 sizeof(MpegEncContext),
03825 MPV_encode_init,
03826 MPV_encode_picture,
03827 MPV_encode_end,
03828 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
03829 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
03830 };
03831
03832 AVCodec wmv1_encoder = {
03833 "wmv1",
03834 AVMEDIA_TYPE_VIDEO,
03835 CODEC_ID_WMV1,
03836 sizeof(MpegEncContext),
03837 MPV_encode_init,
03838 MPV_encode_picture,
03839 MPV_encode_end,
03840 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
03841 .long_name= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
03842 };