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