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