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