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