2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 * The simplest mpeg encoder (well, it was the simplest!).
30 #include "libavutil/intmath.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
35 #include "mpegvideo.h"
36 #include "mpegvideo_common.h"
42 #include "aandcttab.h"
44 #include "mpeg4video.h"
52 static int encode_picture(MpegEncContext *s, int picture_number);
53 static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
54 static int sse_mb(MpegEncContext *s);
55 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
56 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
58 /* enable all paranoid tests for rounding, overflows, etc... */
63 static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
64 static uint8_t default_fcode_tab[MAX_MV*2+1];
66 void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
67 const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
72 for(qscale=qmin; qscale<=qmax; qscale++){
74 if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
75 dsp->fdct == ff_jpeg_fdct_islow_10
77 || dsp->fdct == ff_faandct
81 const int j= dsp->idct_permutation[i];
82 /* 16 <= qscale * quant_matrix[i] <= 7905 */
83 /* 19952 <= ff_aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
84 /* (1 << 36) / 19952 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= (1 << 36) / 249205026 */
85 /* 3444240 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= 275 */
87 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
88 (qscale * quant_matrix[j]));
90 } else if (dsp->fdct == fdct_ifast
91 #ifndef FAAN_POSTSCALE
92 || dsp->fdct == ff_faandct
96 const int j= dsp->idct_permutation[i];
97 /* 16 <= qscale * quant_matrix[i] <= 7905 */
98 /* 19952 <= ff_aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
99 /* (1 << 36) / 19952 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
100 /* 3444240 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= 275 */
102 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
103 (ff_aanscales[i] * qscale * quant_matrix[j]));
107 const int j= dsp->idct_permutation[i];
108 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
109 So 16 <= qscale * quant_matrix[i] <= 7905
110 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
111 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
113 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
114 // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
115 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
117 if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
118 qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
122 for(i=intra; i<64; i++){
124 if (dsp->fdct == fdct_ifast
125 #ifndef FAAN_POSTSCALE
126 || dsp->fdct == ff_faandct
129 max = (8191LL*ff_aanscales[i]) >> 14;
131 while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
137 av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger than %d, overflows possible\n", QMAT_SHIFT - shift);
141 static inline void update_qscale(MpegEncContext *s){
142 s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
143 s->qscale= av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
145 s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
148 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix){
154 put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
161 * init s->current_picture.qscale_table from s->lambda_table
163 void ff_init_qscale_tab(MpegEncContext *s){
164 int8_t * const qscale_table = s->current_picture.f.qscale_table;
167 for(i=0; i<s->mb_num; i++){
168 unsigned int lam= s->lambda_table[ s->mb_index2xy[i] ];
169 int qp= (lam*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
170 qscale_table[ s->mb_index2xy[i] ]= av_clip(qp, s->avctx->qmin, s->avctx->qmax);
174 static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
177 dst->pict_type = src->pict_type;
178 dst->quality = src->quality;
179 dst->coded_picture_number = src->coded_picture_number;
180 dst->display_picture_number = src->display_picture_number;
181 // dst->reference = src->reference;
183 dst->interlaced_frame = src->interlaced_frame;
184 dst->top_field_first = src->top_field_first;
186 if(s->avctx->me_threshold){
187 if(!src->motion_val[0])
188 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
190 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
191 if(!src->ref_index[0])
192 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
193 if(src->motion_subsample_log2 != dst->motion_subsample_log2)
194 av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
195 src->motion_subsample_log2, dst->motion_subsample_log2);
197 memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
200 int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
201 int height= ((16*s->mb_height)>>src->motion_subsample_log2);
203 if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
204 memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
206 if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
207 memcpy(dst->ref_index[i], src->ref_index[i], s->mb_stride*4*s->mb_height*sizeof(int8_t));
213 static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
214 #define COPY(a) dst->a= src->a
216 COPY(current_picture);
222 COPY(picture_in_gop_number);
223 COPY(gop_picture_number);
224 COPY(frame_pred_frame_dct); //FIXME don't set in encode_header
225 COPY(progressive_frame); //FIXME don't set in encode_header
226 COPY(partitioned_frame); //FIXME don't set in encode_header
231 * sets the given MpegEncContext to defaults for encoding.
232 * the changed fields will not depend upon the prior state of the MpegEncContext.
234 static void MPV_encode_defaults(MpegEncContext *s){
236 MPV_common_defaults(s);
238 for(i=-16; i<16; i++){
239 default_fcode_tab[i + MAX_MV]= 1;
241 s->me.mv_penalty= default_mv_penalty;
242 s->fcode_tab= default_fcode_tab;
245 /* init video encoder */
246 av_cold int MPV_encode_init(AVCodecContext *avctx)
248 MpegEncContext *s = avctx->priv_data;
250 int chroma_h_shift, chroma_v_shift;
252 MPV_encode_defaults(s);
254 switch (avctx->codec_id) {
255 case CODEC_ID_MPEG2VIDEO:
256 if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P){
257 av_log(avctx, AV_LOG_ERROR, "only YUV420 and YUV422 are supported\n");
262 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 &&
263 ((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)){
264 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
270 if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P &&
271 ((avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P) || avctx->strict_std_compliance>FF_COMPLIANCE_UNOFFICIAL)){
272 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
277 if(avctx->pix_fmt != PIX_FMT_YUV420P){
278 av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
283 switch (avctx->pix_fmt) {
284 case PIX_FMT_YUVJ422P:
285 case PIX_FMT_YUV422P:
286 s->chroma_format = CHROMA_422;
288 case PIX_FMT_YUVJ420P:
289 case PIX_FMT_YUV420P:
291 s->chroma_format = CHROMA_420;
295 s->bit_rate = avctx->bit_rate;
296 s->width = avctx->width;
297 s->height = avctx->height;
298 if(avctx->gop_size > 600 && avctx->strict_std_compliance>FF_COMPLIANCE_EXPERIMENTAL){
299 av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
302 s->gop_size = avctx->gop_size;
304 s->flags= avctx->flags;
305 s->flags2= avctx->flags2;
306 s->max_b_frames= avctx->max_b_frames;
307 s->codec_id= avctx->codec->id;
308 s->luma_elim_threshold = avctx->luma_elim_threshold;
309 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
310 s->strict_std_compliance= avctx->strict_std_compliance;
311 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
312 if (avctx->flags & CODEC_FLAG_PART)
313 s->data_partitioning = 1;
315 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
316 s->mpeg_quant= avctx->mpeg_quant;
317 s->rtp_mode= !!avctx->rtp_payload_size;
318 s->intra_dc_precision= avctx->intra_dc_precision;
319 s->user_specified_pts = AV_NOPTS_VALUE;
321 if (s->gop_size <= 1) {
328 s->me_method = avctx->me_method;
331 s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
333 s->adaptive_quant= ( s->avctx->lumi_masking
334 || s->avctx->dark_masking
335 || s->avctx->temporal_cplx_masking
336 || s->avctx->spatial_cplx_masking
337 || s->avctx->p_masking
338 || s->avctx->border_masking
339 || (s->flags&CODEC_FLAG_QP_RD))
342 s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
343 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
344 s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
345 s->intra_vlc_format= !!(s->flags2 & CODEC_FLAG2_INTRA_VLC);
346 s->q_scale_type= !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT);
347 s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
350 if(avctx->rc_max_rate && !avctx->rc_buffer_size){
351 av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
355 if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
356 av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
359 if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
360 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
364 if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
365 av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
369 if(avctx->rc_max_rate && avctx->rc_max_rate == avctx->bit_rate && avctx->rc_max_rate != avctx->rc_min_rate){
370 av_log(avctx, AV_LOG_INFO, "impossible bitrate constraints, this will fail\n");
373 if(avctx->rc_buffer_size && avctx->bit_rate*(int64_t)avctx->time_base.num > avctx->rc_buffer_size * (int64_t)avctx->time_base.den){
374 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
378 if(!s->fixed_qscale && avctx->bit_rate*av_q2d(avctx->time_base) > avctx->bit_rate_tolerance){
379 av_log(avctx, AV_LOG_ERROR, "bitrate tolerance too small for bitrate\n");
383 if( s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
384 && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
385 && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
387 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");
390 if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
391 && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
392 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
396 if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
397 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n");
401 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
402 if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
403 av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
408 if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
409 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
413 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
414 if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
415 av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
420 if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
421 av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
425 if ((s->codec_id == CODEC_ID_MPEG4 || s->codec_id == CODEC_ID_H263 ||
426 s->codec_id == CODEC_ID_H263P) &&
427 (avctx->sample_aspect_ratio.num > 255 || avctx->sample_aspect_ratio.den > 255)) {
428 av_log(avctx, AV_LOG_WARNING, "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
429 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
430 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
431 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
434 if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
435 && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
436 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
440 if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
441 av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n");
445 if((s->flags & CODEC_FLAG_CBP_RD) && !avctx->trellis){
446 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
450 if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
451 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
455 if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
456 av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection are not supported yet, set threshold to 1000000000\n");
460 if((s->flags2 & CODEC_FLAG2_INTRA_VLC) && s->codec_id != CODEC_ID_MPEG2VIDEO){
461 av_log(avctx, AV_LOG_ERROR, "intra vlc table not supported by codec\n");
465 if(s->flags & CODEC_FLAG_LOW_DELAY){
466 if (s->codec_id != CODEC_ID_MPEG2VIDEO){
467 av_log(avctx, AV_LOG_ERROR, "low delay forcing is only available for mpeg2\n");
470 if (s->max_b_frames != 0){
471 av_log(avctx, AV_LOG_ERROR, "b frames cannot be used with low delay\n");
476 if(s->q_scale_type == 1){
477 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
478 if(s->codec_id != CODEC_ID_MPEG2VIDEO){
479 av_log(avctx, AV_LOG_ERROR, "non linear quant is only available for mpeg2\n");
483 if(avctx->qmax > 12){
484 av_log(avctx, AV_LOG_ERROR, "non linear quant only supports qmax <= 12 currently\n");
489 if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
490 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
491 && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
492 av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
496 if(s->avctx->thread_count < 1){
497 av_log(avctx, AV_LOG_ERROR, "automatic thread number detection not supported by codec, patch welcome\n");
501 if(s->avctx->thread_count > 1)
504 if(!avctx->time_base.den || !avctx->time_base.num){
505 av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
509 i= (INT_MAX/2+128)>>8;
510 if(avctx->me_threshold >= i){
511 av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1);
514 if(avctx->mb_threshold >= i){
515 av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1);
519 if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){
520 av_log(avctx, AV_LOG_INFO, "notice: b_frame_strategy only affects the first pass\n");
521 avctx->b_frame_strategy = 0;
524 i= av_gcd(avctx->time_base.den, avctx->time_base.num);
526 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
527 avctx->time_base.den /= i;
528 avctx->time_base.num /= i;
532 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){
533 s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
534 s->inter_quant_bias= 0;
536 s->intra_quant_bias=0;
537 s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
540 if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
541 s->intra_quant_bias= avctx->intra_quant_bias;
542 if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
543 s->inter_quant_bias= avctx->inter_quant_bias;
545 av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
547 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
549 if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){
550 av_log(avctx, AV_LOG_ERROR, "timebase %d/%d not supported by MPEG 4 standard, "
551 "the maximum admitted value for the timebase denominator is %d\n",
552 s->avctx->time_base.num, s->avctx->time_base.den, (1<<16)-1);
555 s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
557 switch(avctx->codec->id) {
558 case CODEC_ID_MPEG1VIDEO:
559 s->out_format = FMT_MPEG1;
560 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
561 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
563 case CODEC_ID_MPEG2VIDEO:
564 s->out_format = FMT_MPEG1;
565 s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
566 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
572 s->out_format = FMT_MJPEG;
573 s->intra_only = 1; /* force intra only for jpeg */
574 if(avctx->codec->id == CODEC_ID_LJPEG && avctx->pix_fmt == PIX_FMT_BGRA){
575 s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
576 s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
577 s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
579 s->mjpeg_vsample[0] = 2;
580 s->mjpeg_vsample[1] = 2>>chroma_v_shift;
581 s->mjpeg_vsample[2] = 2>>chroma_v_shift;
582 s->mjpeg_hsample[0] = 2;
583 s->mjpeg_hsample[1] = 2>>chroma_h_shift;
584 s->mjpeg_hsample[2] = 2>>chroma_h_shift;
586 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER)
587 || ff_mjpeg_encode_init(s) < 0)
593 if (!CONFIG_H261_ENCODER) return -1;
594 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
595 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);
598 s->out_format = FMT_H261;
603 if (!CONFIG_H263_ENCODER) return -1;
604 if (ff_match_2uint16(h263_format, FF_ARRAY_ELEMS(h263_format), s->width, s->height) == 8) {
605 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);
608 s->out_format = FMT_H263;
613 s->out_format = FMT_H263;
616 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
617 if (avctx->flags & CODEC_FLAG_H263P_UMV)
619 if (avctx->flags & CODEC_FLAG_H263P_AIV)
620 s->alt_inter_vlc = 1;
621 if (avctx->flags & CODEC_FLAG_H263P_SLICE_STRUCT)
622 s->h263_slice_structured = 1;
624 s->h263_aic= (avctx->flags & CODEC_FLAG_AC_PRED) ? 1:0;
625 s->modified_quant= s->h263_aic;
626 s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
627 s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
630 /* These are just to be sure */
635 s->out_format = FMT_H263;
636 s->h263_flv = 2; /* format = 1; 11-bit codes */
637 s->unrestricted_mv = 1;
638 s->rtp_mode=0; /* don't allow GOB */
643 s->out_format = FMT_H263;
648 s->out_format = FMT_H263;
655 s->unrestricted_mv= 0;
658 s->out_format = FMT_H263;
660 s->unrestricted_mv = 1;
661 s->low_delay= s->max_b_frames ? 0 : 1;
662 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
664 case CODEC_ID_MSMPEG4V2:
665 s->out_format = FMT_H263;
667 s->unrestricted_mv = 1;
668 s->msmpeg4_version= 2;
672 case CODEC_ID_MSMPEG4V3:
673 s->out_format = FMT_H263;
675 s->unrestricted_mv = 1;
676 s->msmpeg4_version= 3;
677 s->flipflop_rounding=1;
682 s->out_format = FMT_H263;
684 s->unrestricted_mv = 1;
685 s->msmpeg4_version= 4;
686 s->flipflop_rounding=1;
691 s->out_format = FMT_H263;
693 s->unrestricted_mv = 1;
694 s->msmpeg4_version= 5;
695 s->flipflop_rounding=1;
703 avctx->has_b_frames= !s->low_delay;
707 s->progressive_frame=
708 s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN));
711 if (MPV_common_init(s) < 0)
715 s->dct_quantize = dct_quantize_c;
717 s->denoise_dct = denoise_dct_c;
718 s->fast_dct_quantize = s->dct_quantize;
720 s->dct_quantize = dct_quantize_trellis_c;
722 if((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
723 s->chroma_qscale_table= ff_h263_chroma_qscale_table;
725 s->quant_precision=5;
727 ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
728 ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
730 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
731 ff_h261_encode_init(s);
732 if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
734 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
735 ff_msmpeg4_encode_init(s);
736 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
737 && s->out_format == FMT_MPEG1)
738 ff_mpeg1_encode_init(s);
742 int j= s->dsp.idct_permutation[i];
743 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
744 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
745 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
746 }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
748 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
751 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
752 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
754 if(s->avctx->intra_matrix)
755 s->intra_matrix[j] = s->avctx->intra_matrix[i];
756 if(s->avctx->inter_matrix)
757 s->inter_matrix[j] = s->avctx->inter_matrix[i];
760 /* precompute matrix */
761 /* for mjpeg, we do include qscale in the matrix */
762 if (s->out_format != FMT_MJPEG) {
763 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
764 s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
765 ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
766 s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
769 if(ff_rate_control_init(s) < 0)
775 av_cold int MPV_encode_end(AVCodecContext *avctx)
777 MpegEncContext *s = avctx->priv_data;
779 ff_rate_control_uninit(s);
782 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) && s->out_format == FMT_MJPEG)
783 ff_mjpeg_encode_close(s);
785 av_freep(&avctx->extradata);
790 static int get_sae(uint8_t *src, int ref, int stride){
796 acc+= FFABS(src[x+y*stride] - ref);
803 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
810 for(y=0; y<h; y+=16){
811 for(x=0; x<w; x+=16){
812 int offset= x + y*stride;
813 int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
814 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
815 int sae = get_sae(src + offset, mean, stride);
817 acc+= sae + 500 < sad;
824 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
828 const int encoding_delay= s->max_b_frames;
833 pic_arg->display_picture_number= s->input_picture_number++;
835 if(pts != AV_NOPTS_VALUE){
836 if(s->user_specified_pts != AV_NOPTS_VALUE){
838 int64_t last= s->user_specified_pts;
841 av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts);
845 s->user_specified_pts= pts;
847 if(s->user_specified_pts != AV_NOPTS_VALUE){
848 s->user_specified_pts=
849 pts= s->user_specified_pts + 1;
850 av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts);
852 pts= pic_arg->display_picture_number;
858 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
859 if(pic_arg->linesize[0] != s->linesize) direct=0;
860 if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
861 if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
863 // av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
866 i= ff_find_unused_picture(s, 1);
868 pic= (AVFrame*)&s->picture[i];
872 pic->data[i]= pic_arg->data[i];
873 pic->linesize[i]= pic_arg->linesize[i];
875 if(ff_alloc_picture(s, (Picture*)pic, 1) < 0){
879 i= ff_find_unused_picture(s, 0);
881 pic= (AVFrame*)&s->picture[i];
884 if(ff_alloc_picture(s, (Picture*)pic, 0) < 0){
888 if( pic->data[0] + INPLACE_OFFSET == pic_arg->data[0]
889 && pic->data[1] + INPLACE_OFFSET == pic_arg->data[1]
890 && pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]){
893 int h_chroma_shift, v_chroma_shift;
894 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
897 int src_stride= pic_arg->linesize[i];
898 int dst_stride= i ? s->uvlinesize : s->linesize;
899 int h_shift= i ? h_chroma_shift : 0;
900 int v_shift= i ? v_chroma_shift : 0;
901 int w= s->width >>h_shift;
902 int h= s->height>>v_shift;
903 uint8_t *src= pic_arg->data[i];
904 uint8_t *dst= pic->data[i];
906 if(!s->avctx->rc_buffer_size)
907 dst +=INPLACE_OFFSET;
909 if(src_stride==dst_stride)
910 memcpy(dst, src, src_stride*h);
921 copy_picture_attributes(s, pic, pic_arg);
922 pic->pts= pts; //we set this here to avoid modifiying pic_arg
925 /* shift buffer entries */
926 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
927 s->input_picture[i-1]= s->input_picture[i];
929 s->input_picture[encoding_delay]= (Picture*)pic;
934 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){
939 for(plane=0; plane<3; plane++){
940 const int stride = p->f.linesize[plane];
941 const int bw= plane ? 1 : 2;
942 for(y=0; y<s->mb_height*bw; y++){
943 for(x=0; x<s->mb_width*bw; x++){
944 int off = p->f.type == FF_BUFFER_TYPE_SHARED ? 0: 16;
945 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);
947 switch(s->avctx->frame_skip_exp){
948 case 0: score= FFMAX(score, v); break;
949 case 1: score+= FFABS(v);break;
950 case 2: score+= v*v;break;
951 case 3: score64+= FFABS(v*v*(int64_t)v);break;
952 case 4: score64+= v*v*(int64_t)(v*v);break;
958 if(score) score64= score;
960 if(score64 < s->avctx->frame_skip_threshold)
962 if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
967 static int estimate_best_b_count(MpegEncContext *s){
968 AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id);
969 AVCodecContext *c = avcodec_alloc_context3(NULL);
970 AVFrame input[FF_MAX_B_FRAMES+2];
971 const int scale= s->avctx->brd_scale;
972 int i, j, out_size, p_lambda, b_lambda, lambda2;
973 int outbuf_size= s->width * s->height; //FIXME
974 uint8_t *outbuf= av_malloc(outbuf_size);
975 int64_t best_rd= INT64_MAX;
976 int best_b_count= -1;
978 assert(scale>=0 && scale <=3);
981 p_lambda= s->last_lambda_for[AV_PICTURE_TYPE_P]; //s->next_picture_ptr->quality;
982 b_lambda= s->last_lambda_for[AV_PICTURE_TYPE_B]; //p_lambda *FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
983 if(!b_lambda) b_lambda= p_lambda; //FIXME we should do this somewhere else
984 lambda2= (b_lambda*b_lambda + (1<<FF_LAMBDA_SHIFT)/2 ) >> FF_LAMBDA_SHIFT;
986 c->width = s->width >> scale;
987 c->height= s->height>> scale;
988 c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
989 c->flags|= s->avctx->flags & CODEC_FLAG_QPEL;
990 c->mb_decision= s->avctx->mb_decision;
991 c->me_cmp= s->avctx->me_cmp;
992 c->mb_cmp= s->avctx->mb_cmp;
993 c->me_sub_cmp= s->avctx->me_sub_cmp;
994 c->pix_fmt = PIX_FMT_YUV420P;
995 c->time_base= s->avctx->time_base;
996 c->max_b_frames= s->max_b_frames;
998 if (avcodec_open2(c, codec, NULL) < 0)
1001 for(i=0; i<s->max_b_frames+2; i++){
1002 int ysize= c->width*c->height;
1003 int csize= (c->width/2)*(c->height/2);
1004 Picture pre_input, *pre_input_ptr= i ? s->input_picture[i-1] : s->next_picture_ptr;
1006 avcodec_get_frame_defaults(&input[i]);
1007 input[i].data[0]= av_malloc(ysize + 2*csize);
1008 input[i].data[1]= input[i].data[0] + ysize;
1009 input[i].data[2]= input[i].data[1] + csize;
1010 input[i].linesize[0]= c->width;
1011 input[i].linesize[1]=
1012 input[i].linesize[2]= c->width/2;
1014 if(pre_input_ptr && (!i || s->input_picture[i-1])) {
1015 pre_input= *pre_input_ptr;
1017 if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {
1018 pre_input.f.data[0] += INPLACE_OFFSET;
1019 pre_input.f.data[1] += INPLACE_OFFSET;
1020 pre_input.f.data[2] += INPLACE_OFFSET;
1023 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);
1024 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);
1025 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);
1029 for(j=0; j<s->max_b_frames+1; j++){
1032 if(!s->input_picture[j])
1035 c->error[0]= c->error[1]= c->error[2]= 0;
1037 input[0].pict_type= AV_PICTURE_TYPE_I;
1038 input[0].quality= 1 * FF_QP2LAMBDA;
1039 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]);
1040 // rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1042 for(i=0; i<s->max_b_frames+1; i++){
1043 int is_p= i % (j+1) == j || i==s->max_b_frames;
1045 input[i+1].pict_type= is_p ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1046 input[i+1].quality= is_p ? p_lambda : b_lambda;
1047 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]);
1048 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1051 /* get the delayed frames */
1053 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
1054 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1057 rd += c->error[0] + c->error[1] + c->error[2];
1069 for(i=0; i<s->max_b_frames+2; i++){
1070 av_freep(&input[i].data[0]);
1073 return best_b_count;
1076 static int select_input_picture(MpegEncContext *s){
1079 for(i=1; i<MAX_PICTURE_COUNT; i++)
1080 s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1081 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1083 /* set next picture type & ordering */
1084 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1085 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1086 s->reordered_input_picture[0]= s->input_picture[0];
1087 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_I;
1088 s->reordered_input_picture[0]->f.coded_picture_number = s->coded_picture_number++;
1092 if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
1093 if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){
1094 //FIXME check that te gop check above is +-1 correct
1095 //av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n", s->input_picture[0]->f.data[0], s->input_picture[0]->pts);
1097 if (s->input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED) {
1099 s->input_picture[0]->f.data[i] = NULL;
1100 s->input_picture[0]->f.type = 0;
1102 assert( s->input_picture[0]->f.type == FF_BUFFER_TYPE_USER
1103 || s->input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL);
1105 s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
1109 ff_vbv_update(s, 0);
1115 if(s->flags&CODEC_FLAG_PASS2){
1116 for(i=0; i<s->max_b_frames+1; i++){
1117 int pict_num = s->input_picture[0]->f.display_picture_number + i;
1119 if(pict_num >= s->rc_context.num_entries)
1121 if(!s->input_picture[i]){
1122 s->rc_context.entry[pict_num-1].new_pict_type = AV_PICTURE_TYPE_P;
1126 s->input_picture[i]->f.pict_type =
1127 s->rc_context.entry[pict_num].new_pict_type;
1131 if(s->avctx->b_frame_strategy==0){
1132 b_frames= s->max_b_frames;
1133 while(b_frames && !s->input_picture[b_frames]) b_frames--;
1134 }else if(s->avctx->b_frame_strategy==1){
1135 for(i=1; i<s->max_b_frames+1; i++){
1136 if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1137 s->input_picture[i]->b_frame_score=
1138 get_intra_count(s, s->input_picture[i ]->f.data[0],
1139 s->input_picture[i-1]->f.data[0], s->linesize) + 1;
1142 for(i=0; i<s->max_b_frames+1; i++){
1143 if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/s->avctx->b_sensitivity) break;
1146 b_frames= FFMAX(0, i-1);
1149 for(i=0; i<b_frames+1; i++){
1150 s->input_picture[i]->b_frame_score=0;
1152 }else if(s->avctx->b_frame_strategy==2){
1153 b_frames= estimate_best_b_count(s);
1155 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1160 //static int b_count=0;
1161 //b_count+= b_frames;
1162 //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1164 for(i= b_frames - 1; i>=0; i--){
1165 int type = s->input_picture[i]->f.pict_type;
1166 if(type && type != AV_PICTURE_TYPE_B)
1169 if (s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_B && b_frames == s->max_b_frames){
1170 av_log(s->avctx, AV_LOG_ERROR, "warning, too many b frames in a row\n");
1173 if(s->picture_in_gop_number + b_frames >= s->gop_size){
1174 if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){
1175 b_frames= s->gop_size - s->picture_in_gop_number - 1;
1177 if(s->flags & CODEC_FLAG_CLOSED_GOP)
1179 s->input_picture[b_frames]->f.pict_type = AV_PICTURE_TYPE_I;
1183 if( (s->flags & CODEC_FLAG_CLOSED_GOP)
1185 && s->input_picture[b_frames]->f.pict_type== AV_PICTURE_TYPE_I)
1188 s->reordered_input_picture[0]= s->input_picture[b_frames];
1189 if (s->reordered_input_picture[0]->f.pict_type != AV_PICTURE_TYPE_I)
1190 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_P;
1191 s->reordered_input_picture[0]->f.coded_picture_number = s->coded_picture_number++;
1192 for(i=0; i<b_frames; i++){
1193 s->reordered_input_picture[i + 1] = s->input_picture[i];
1194 s->reordered_input_picture[i + 1]->f.pict_type = AV_PICTURE_TYPE_B;
1195 s->reordered_input_picture[i + 1]->f.coded_picture_number = s->coded_picture_number++;
1200 if(s->reordered_input_picture[0]){
1201 s->reordered_input_picture[0]->f.reference = s->reordered_input_picture[0]->f.pict_type!=AV_PICTURE_TYPE_B ? 3 : 0;
1203 ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1205 if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED || s->avctx->rc_buffer_size) {
1206 // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable
1208 int i= ff_find_unused_picture(s, 0);
1209 Picture *pic= &s->picture[i];
1211 pic->f.reference = s->reordered_input_picture[0]->f.reference;
1212 if(ff_alloc_picture(s, pic, 0) < 0){
1216 /* mark us unused / free shared pic */
1217 if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL)
1218 s->avctx->release_buffer(s->avctx, (AVFrame*)s->reordered_input_picture[0]);
1220 s->reordered_input_picture[0]->f.data[i] = NULL;
1221 s->reordered_input_picture[0]->f.type = 0;
1223 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
1225 s->current_picture_ptr= pic;
1227 // input is not a shared pix -> reuse buffer for current_pix
1229 assert( s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_USER
1230 || s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL);
1232 s->current_picture_ptr= s->reordered_input_picture[0];
1234 s->new_picture.f.data[i] += INPLACE_OFFSET;
1237 ff_copy_picture(&s->current_picture, s->current_picture_ptr);
1239 s->picture_number = s->new_picture.f.display_picture_number;
1240 //printf("dpn:%d\n", s->picture_number);
1242 memset(&s->new_picture, 0, sizeof(Picture));
1247 int MPV_encode_picture(AVCodecContext *avctx,
1248 unsigned char *buf, int buf_size, void *data)
1250 MpegEncContext *s = avctx->priv_data;
1251 AVFrame *pic_arg = data;
1252 int i, stuffing_count, context_count = avctx->thread_count;
1254 for(i=0; i<context_count; i++){
1255 int start_y= s->thread_context[i]->start_mb_y;
1256 int end_y= s->thread_context[i]-> end_mb_y;
1257 int h= s->mb_height;
1258 uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h);
1259 uint8_t *end = buf + (size_t)(((int64_t) buf_size)* end_y/h);
1261 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1264 s->picture_in_gop_number++;
1266 if(load_input_picture(s, pic_arg) < 0)
1269 if(select_input_picture(s) < 0){
1274 if (s->new_picture.f.data[0]) {
1275 s->pict_type = s->new_picture.f.pict_type;
1277 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1278 MPV_frame_start(s, avctx);
1280 if (encode_picture(s, s->picture_number) < 0)
1283 avctx->header_bits = s->header_bits;
1284 avctx->mv_bits = s->mv_bits;
1285 avctx->misc_bits = s->misc_bits;
1286 avctx->i_tex_bits = s->i_tex_bits;
1287 avctx->p_tex_bits = s->p_tex_bits;
1288 avctx->i_count = s->i_count;
1289 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1290 avctx->skip_count = s->skip_count;
1294 if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1295 ff_mjpeg_encode_picture_trailer(s);
1297 if(avctx->rc_buffer_size){
1298 RateControlContext *rcc= &s->rc_context;
1299 int max_size= rcc->buffer_index * avctx->rc_max_available_vbv_use;
1301 if(put_bits_count(&s->pb) > max_size && s->lambda < s->avctx->lmax){
1302 s->next_lambda= FFMAX(s->lambda+1, s->lambda*(s->qscale+1) / s->qscale);
1303 if(s->adaptive_quant){
1305 for(i=0; i<s->mb_height*s->mb_stride; i++)
1306 s->lambda_table[i]= FFMAX(s->lambda_table[i]+1, s->lambda_table[i]*(s->qscale+1) / s->qscale);
1308 s->mb_skipped = 0; //done in MPV_frame_start()
1309 if(s->pict_type==AV_PICTURE_TYPE_P){ //done in encode_picture() so we must undo it
1310 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
1311 s->no_rounding ^= 1;
1313 if(s->pict_type!=AV_PICTURE_TYPE_B){
1314 s->time_base= s->last_time_base;
1315 s->last_non_b_time= s->time - s->pp_time;
1317 // av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda);
1318 for(i=0; i<context_count; i++){
1319 PutBitContext *pb= &s->thread_context[i]->pb;
1320 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1325 assert(s->avctx->rc_max_rate);
1328 if(s->flags&CODEC_FLAG_PASS1)
1329 ff_write_pass1_stats(s);
1332 s->current_picture_ptr->f.error[i] = s->current_picture.f.error[i];
1333 avctx->error[i] += s->current_picture_ptr->f.error[i];
1336 if(s->flags&CODEC_FLAG_PASS1)
1337 assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb));
1338 flush_put_bits(&s->pb);
1339 s->frame_bits = put_bits_count(&s->pb);
1341 stuffing_count= ff_vbv_update(s, s->frame_bits);
1343 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){
1344 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1348 switch(s->codec_id){
1349 case CODEC_ID_MPEG1VIDEO:
1350 case CODEC_ID_MPEG2VIDEO:
1351 while(stuffing_count--){
1352 put_bits(&s->pb, 8, 0);
1355 case CODEC_ID_MPEG4:
1356 put_bits(&s->pb, 16, 0);
1357 put_bits(&s->pb, 16, 0x1C3);
1358 stuffing_count -= 4;
1359 while(stuffing_count--){
1360 put_bits(&s->pb, 8, 0xFF);
1364 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1366 flush_put_bits(&s->pb);
1367 s->frame_bits = put_bits_count(&s->pb);
1370 /* update mpeg1/2 vbv_delay for CBR */
1371 if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
1372 && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
1373 int vbv_delay, min_delay;
1374 double inbits = s->avctx->rc_max_rate*av_q2d(s->avctx->time_base);
1375 int minbits= s->frame_bits - 8*(s->vbv_delay_ptr - s->pb.buf - 1);
1376 double bits = s->rc_context.buffer_index + minbits - inbits;
1379 av_log(s->avctx, AV_LOG_ERROR, "Internal error, negative bits\n");
1381 assert(s->repeat_first_field==0);
1383 vbv_delay= bits * 90000 / s->avctx->rc_max_rate;
1384 min_delay= (minbits * 90000LL + s->avctx->rc_max_rate - 1)/ s->avctx->rc_max_rate;
1386 vbv_delay= FFMAX(vbv_delay, min_delay);
1388 assert(vbv_delay < 0xFFFF);
1390 s->vbv_delay_ptr[0] &= 0xF8;
1391 s->vbv_delay_ptr[0] |= vbv_delay>>13;
1392 s->vbv_delay_ptr[1] = vbv_delay>>5;
1393 s->vbv_delay_ptr[2] &= 0x07;
1394 s->vbv_delay_ptr[2] |= vbv_delay<<3;
1395 avctx->vbv_delay = vbv_delay*300;
1397 s->total_bits += s->frame_bits;
1398 avctx->frame_bits = s->frame_bits;
1400 assert((put_bits_ptr(&s->pb) == s->pb.buf));
1403 assert((s->frame_bits&7)==0);
1405 return s->frame_bits/8;
1408 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
1410 static const char tab[64]=
1422 DCTELEM *block= s->block[n];
1423 const int last_index= s->block_last_index[n];
1428 threshold= -threshold;
1432 /* Are all we could set to zero already zero? */
1433 if(last_index<=skip_dc - 1) return;
1435 for(i=0; i<=last_index; i++){
1436 const int j = s->intra_scantable.permutated[i];
1437 const int level = FFABS(block[j]);
1439 if(skip_dc && i==0) continue;
1448 if(score >= threshold) return;
1449 for(i=skip_dc; i<=last_index; i++){
1450 const int j = s->intra_scantable.permutated[i];
1453 if(block[0]) s->block_last_index[n]= 0;
1454 else s->block_last_index[n]= -1;
1457 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1460 const int maxlevel= s->max_qcoeff;
1461 const int minlevel= s->min_qcoeff;
1465 i=1; //skip clipping of intra dc
1469 for(;i<=last_index; i++){
1470 const int j= s->intra_scantable.permutated[i];
1471 int level = block[j];
1473 if (level>maxlevel){
1476 }else if(level<minlevel){
1484 if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1485 av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel);
1488 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride){
1498 for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
1499 for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
1500 int v= ptr[x2 + y2*stride];
1506 weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
1511 static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count)
1513 int16_t weight[8][64];
1514 DCTELEM orig[8][64];
1515 const int mb_x= s->mb_x;
1516 const int mb_y= s->mb_y;
1519 int dct_offset = s->linesize*8; //default for progressive frames
1520 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1523 for(i=0; i<mb_block_count; i++) skip_dct[i]=s->skipdct;
1525 if(s->adaptive_quant){
1526 const int last_qp= s->qscale;
1527 const int mb_xy= mb_x + mb_y*s->mb_stride;
1529 s->lambda= s->lambda_table[mb_xy];
1532 if(!(s->flags&CODEC_FLAG_QP_RD)){
1533 s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];
1534 s->dquant= s->qscale - last_qp;
1536 if(s->out_format==FMT_H263){
1537 s->dquant= av_clip(s->dquant, -2, 2);
1539 if(s->codec_id==CODEC_ID_MPEG4){
1541 if(s->pict_type == AV_PICTURE_TYPE_B){
1542 if(s->dquant&1 || s->mv_dir&MV_DIRECT)
1545 if(s->mv_type==MV_TYPE_8X8)
1551 ff_set_qscale(s, last_qp + s->dquant);
1552 }else if(s->flags&CODEC_FLAG_QP_RD)
1553 ff_set_qscale(s, s->qscale + s->dquant);
1555 wrap_y = s->linesize;
1556 wrap_c = s->uvlinesize;
1557 ptr_y = s->new_picture.f.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
1558 ptr_cb = s->new_picture.f.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1559 ptr_cr = s->new_picture.f.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1561 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
1562 uint8_t *ebuf= s->edge_emu_buffer + 32;
1563 s->dsp.emulated_edge_mc(ebuf , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width , s->height);
1565 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);
1566 ptr_cb= ebuf+18*wrap_y;
1567 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);
1568 ptr_cr= ebuf+18*wrap_y+8;
1572 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
1573 int progressive_score, interlaced_score;
1575 s->interlaced_dct=0;
1576 progressive_score= s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y, 8)
1577 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
1579 if(progressive_score > 0){
1580 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y , NULL, wrap_y*2, 8)
1581 +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y , NULL, wrap_y*2, 8);
1582 if(progressive_score > interlaced_score){
1583 s->interlaced_dct=1;
1587 if (s->chroma_format == CHROMA_422)
1593 s->dsp.get_pixels(s->block[0], ptr_y , wrap_y);
1594 s->dsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
1595 s->dsp.get_pixels(s->block[2], ptr_y + dct_offset , wrap_y);
1596 s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
1598 if(s->flags&CODEC_FLAG_GRAY){
1602 s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1603 s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1604 if(!s->chroma_y_shift){ /* 422 */
1605 s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset>>1), wrap_c);
1606 s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset>>1), wrap_c);
1610 op_pixels_func (*op_pix)[4];
1611 qpel_mc_func (*op_qpix)[16];
1612 uint8_t *dest_y, *dest_cb, *dest_cr;
1614 dest_y = s->dest[0];
1615 dest_cb = s->dest[1];
1616 dest_cr = s->dest[2];
1618 if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
1619 op_pix = s->dsp.put_pixels_tab;
1620 op_qpix= s->dsp.put_qpel_pixels_tab;
1622 op_pix = s->dsp.put_no_rnd_pixels_tab;
1623 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
1626 if (s->mv_dir & MV_DIR_FORWARD) {
1627 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
1628 op_pix = s->dsp.avg_pixels_tab;
1629 op_qpix= s->dsp.avg_qpel_pixels_tab;
1631 if (s->mv_dir & MV_DIR_BACKWARD) {
1632 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
1635 if(s->flags&CODEC_FLAG_INTERLACED_DCT){
1636 int progressive_score, interlaced_score;
1638 s->interlaced_dct=0;
1639 progressive_score= s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y, 8)
1640 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
1642 if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
1644 if(progressive_score>0){
1645 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y , ptr_y , wrap_y*2, 8)
1646 +s->dsp.ildct_cmp[0](s, dest_y + wrap_y , ptr_y + wrap_y , wrap_y*2, 8);
1648 if(progressive_score > interlaced_score){
1649 s->interlaced_dct=1;
1653 if (s->chroma_format == CHROMA_422)
1659 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y);
1660 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
1661 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y);
1662 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
1664 if(s->flags&CODEC_FLAG_GRAY){
1668 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1669 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1670 if(!s->chroma_y_shift){ /* 422 */
1671 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset>>1), dest_cb + (dct_offset>>1), wrap_c);
1672 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset>>1), dest_cr + (dct_offset>>1), wrap_c);
1675 /* pre quantization */
1676 if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
1678 if(s->dsp.sad[1](NULL, ptr_y , dest_y , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
1679 if(s->dsp.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
1680 if(s->dsp.sad[1](NULL, ptr_y +dct_offset , dest_y +dct_offset , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
1681 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;
1682 if(s->dsp.sad[1](NULL, ptr_cb , dest_cb , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
1683 if(s->dsp.sad[1](NULL, ptr_cr , dest_cr , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
1684 if(!s->chroma_y_shift){ /* 422 */
1685 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;
1686 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;
1691 if(s->avctx->quantizer_noise_shaping){
1692 if(!skip_dct[0]) get_visual_weight(weight[0], ptr_y , wrap_y);
1693 if(!skip_dct[1]) get_visual_weight(weight[1], ptr_y + 8, wrap_y);
1694 if(!skip_dct[2]) get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
1695 if(!skip_dct[3]) get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
1696 if(!skip_dct[4]) get_visual_weight(weight[4], ptr_cb , wrap_c);
1697 if(!skip_dct[5]) get_visual_weight(weight[5], ptr_cr , wrap_c);
1698 if(!s->chroma_y_shift){ /* 422 */
1699 if(!skip_dct[6]) get_visual_weight(weight[6], ptr_cb + (dct_offset>>1), wrap_c);
1700 if(!skip_dct[7]) get_visual_weight(weight[7], ptr_cr + (dct_offset>>1), wrap_c);
1702 memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*mb_block_count);
1705 /* DCT & quantize */
1706 assert(s->out_format!=FMT_MJPEG || s->qscale==8);
1708 for(i=0;i<mb_block_count;i++) {
1711 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1712 // FIXME we could decide to change to quantizer instead of clipping
1713 // JS: I don't think that would be a good idea it could lower quality instead
1714 // of improve it. Just INTRADC clipping deserves changes in quantizer
1715 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1717 s->block_last_index[i]= -1;
1719 if(s->avctx->quantizer_noise_shaping){
1720 for(i=0;i<mb_block_count;i++) {
1722 s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
1727 if(s->luma_elim_threshold && !s->mb_intra)
1729 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
1730 if(s->chroma_elim_threshold && !s->mb_intra)
1731 for(i=4; i<mb_block_count; i++)
1732 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
1734 if(s->flags & CODEC_FLAG_CBP_RD){
1735 for(i=0;i<mb_block_count;i++) {
1736 if(s->block_last_index[i] == -1)
1737 s->coded_score[i]= INT_MAX/256;
1742 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
1743 s->block_last_index[4]=
1744 s->block_last_index[5]= 0;
1746 s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
1749 //non c quantize code returns incorrect block_last_index FIXME
1750 if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
1751 for(i=0; i<mb_block_count; i++){
1753 if(s->block_last_index[i]>0){
1754 for(j=63; j>0; j--){
1755 if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
1757 s->block_last_index[i]= j;
1762 /* huffman encode */
1763 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
1764 case CODEC_ID_MPEG1VIDEO:
1765 case CODEC_ID_MPEG2VIDEO:
1766 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
1767 mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1769 case CODEC_ID_MPEG4:
1770 if (CONFIG_MPEG4_ENCODER)
1771 mpeg4_encode_mb(s, s->block, motion_x, motion_y);
1773 case CODEC_ID_MSMPEG4V2:
1774 case CODEC_ID_MSMPEG4V3:
1776 if (CONFIG_MSMPEG4_ENCODER)
1777 msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1780 if (CONFIG_WMV2_ENCODER)
1781 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
1784 if (CONFIG_H261_ENCODER)
1785 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
1788 case CODEC_ID_H263P:
1792 if (CONFIG_H263_ENCODER)
1793 h263_encode_mb(s, s->block, motion_x, motion_y);
1795 case CODEC_ID_MJPEG:
1797 if (CONFIG_MJPEG_ENCODER)
1798 ff_mjpeg_encode_mb(s, s->block);
1805 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1807 if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y, 8, 6);
1808 else encode_mb_internal(s, motion_x, motion_y, 16, 8);
1811 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1814 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
1817 d->mb_skip_run= s->mb_skip_run;
1819 d->last_dc[i]= s->last_dc[i];
1822 d->mv_bits= s->mv_bits;
1823 d->i_tex_bits= s->i_tex_bits;
1824 d->p_tex_bits= s->p_tex_bits;
1825 d->i_count= s->i_count;
1826 d->f_count= s->f_count;
1827 d->b_count= s->b_count;
1828 d->skip_count= s->skip_count;
1829 d->misc_bits= s->misc_bits;
1833 d->qscale= s->qscale;
1834 d->dquant= s->dquant;
1836 d->esc3_level_length= s->esc3_level_length;
1839 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1842 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
1843 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
1846 d->mb_skip_run= s->mb_skip_run;
1848 d->last_dc[i]= s->last_dc[i];
1851 d->mv_bits= s->mv_bits;
1852 d->i_tex_bits= s->i_tex_bits;
1853 d->p_tex_bits= s->p_tex_bits;
1854 d->i_count= s->i_count;
1855 d->f_count= s->f_count;
1856 d->b_count= s->b_count;
1857 d->skip_count= s->skip_count;
1858 d->misc_bits= s->misc_bits;
1860 d->mb_intra= s->mb_intra;
1861 d->mb_skipped= s->mb_skipped;
1862 d->mv_type= s->mv_type;
1863 d->mv_dir= s->mv_dir;
1865 if(s->data_partitioning){
1867 d->tex_pb= s->tex_pb;
1871 d->block_last_index[i]= s->block_last_index[i];
1872 d->interlaced_dct= s->interlaced_dct;
1873 d->qscale= s->qscale;
1875 d->esc3_level_length= s->esc3_level_length;
1878 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
1879 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
1880 int *dmin, int *next_block, int motion_x, int motion_y)
1883 uint8_t *dest_backup[3];
1885 copy_context_before_encode(s, backup, type);
1887 s->block= s->blocks[*next_block];
1888 s->pb= pb[*next_block];
1889 if(s->data_partitioning){
1890 s->pb2 = pb2 [*next_block];
1891 s->tex_pb= tex_pb[*next_block];
1895 memcpy(dest_backup, s->dest, sizeof(s->dest));
1896 s->dest[0] = s->rd_scratchpad;
1897 s->dest[1] = s->rd_scratchpad + 16*s->linesize;
1898 s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
1899 assert(s->linesize >= 32); //FIXME
1902 encode_mb(s, motion_x, motion_y);
1904 score= put_bits_count(&s->pb);
1905 if(s->data_partitioning){
1906 score+= put_bits_count(&s->pb2);
1907 score+= put_bits_count(&s->tex_pb);
1910 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
1911 MPV_decode_mb(s, s->block);
1913 score *= s->lambda2;
1914 score += sse_mb(s) << FF_LAMBDA_SHIFT;
1918 memcpy(s->dest, dest_backup, sizeof(s->dest));
1925 copy_context_after_encode(best, s, type);
1929 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
1930 uint32_t *sq = ff_squareTbl + 256;
1935 return s->dsp.sse[0](NULL, src1, src2, stride, 16);
1936 else if(w==8 && h==8)
1937 return s->dsp.sse[1](NULL, src1, src2, stride, 8);
1941 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
1950 static int sse_mb(MpegEncContext *s){
1954 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
1955 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
1958 if(s->avctx->mb_cmp == FF_CMP_NSSE){
1959 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)
1960 +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)
1961 +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);
1963 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)
1964 +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)
1965 +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);
1968 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)
1969 +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)
1970 +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);
1973 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
1974 MpegEncContext *s= *(void**)arg;
1978 s->me.dia_size= s->avctx->pre_dia_size;
1979 s->first_slice_line=1;
1980 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
1981 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
1982 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
1984 s->first_slice_line=0;
1992 static int estimate_motion_thread(AVCodecContext *c, void *arg){
1993 MpegEncContext *s= *(void**)arg;
1995 ff_check_alignment();
1997 s->me.dia_size= s->avctx->dia_size;
1998 s->first_slice_line=1;
1999 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2000 s->mb_x=0; //for block init below
2001 ff_init_block_index(s);
2002 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2003 s->block_index[0]+=2;
2004 s->block_index[1]+=2;
2005 s->block_index[2]+=2;
2006 s->block_index[3]+=2;
2008 /* compute motion vector & mb_type and store in context */
2009 if(s->pict_type==AV_PICTURE_TYPE_B)
2010 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2012 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2014 s->first_slice_line=0;
2019 static int mb_var_thread(AVCodecContext *c, void *arg){
2020 MpegEncContext *s= *(void**)arg;
2023 ff_check_alignment();
2025 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2026 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2029 uint8_t *pix = s->new_picture.f.data[0] + (yy * s->linesize) + xx;
2031 int sum = s->dsp.pix_sum(pix, s->linesize);
2033 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
2035 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2036 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2037 s->me.mb_var_sum_temp += varc;
2043 static void write_slice_end(MpegEncContext *s){
2044 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
2045 if(s->partitioned_frame){
2046 ff_mpeg4_merge_partitions(s);
2049 ff_mpeg4_stuffing(&s->pb);
2050 }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2051 ff_mjpeg_encode_stuffing(&s->pb);
2054 align_put_bits(&s->pb);
2055 flush_put_bits(&s->pb);
2057 if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2058 s->misc_bits+= get_bits_diff(s);
2061 static int encode_thread(AVCodecContext *c, void *arg){
2062 MpegEncContext *s= *(void**)arg;
2063 int mb_x, mb_y, pdif = 0;
2064 int chr_h= 16>>s->chroma_y_shift;
2066 MpegEncContext best_s, backup_s;
2067 uint8_t bit_buf[2][MAX_MB_BYTES];
2068 uint8_t bit_buf2[2][MAX_MB_BYTES];
2069 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2070 PutBitContext pb[2], pb2[2], tex_pb[2];
2071 //printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
2073 ff_check_alignment();
2076 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2077 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2078 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2081 s->last_bits= put_bits_count(&s->pb);
2092 /* init last dc values */
2093 /* note: quant matrix value (8) is implied here */
2094 s->last_dc[i] = 128 << s->intra_dc_precision;
2096 s->current_picture.f.error[i] = 0;
2098 if(s->codec_id==CODEC_ID_AMV){
2099 s->last_dc[0] = 128*8/13;
2100 s->last_dc[1] = 128*8/14;
2101 s->last_dc[2] = 128*8/14;
2104 memset(s->last_mv, 0, sizeof(s->last_mv));
2108 switch(s->codec_id){
2110 case CODEC_ID_H263P:
2112 if (CONFIG_H263_ENCODER)
2113 s->gob_index = ff_h263_get_gob_height(s);
2115 case CODEC_ID_MPEG4:
2116 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2117 ff_mpeg4_init_partitions(s);
2123 s->first_slice_line = 1;
2124 s->ptr_lastgob = s->pb.buf;
2125 for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2126 // printf("row %d at %X\n", s->mb_y, (int)s);
2130 ff_set_qscale(s, s->qscale);
2131 ff_init_block_index(s);
2133 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2134 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2135 int mb_type= s->mb_type[xy];
2140 if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2141 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2144 if(s->data_partitioning){
2145 if( s->pb2 .buf_end - s->pb2 .buf - (put_bits_count(&s-> pb2)>>3) < MAX_MB_BYTES
2146 || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2147 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2153 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2154 ff_update_block_index(s);
2156 if(CONFIG_H261_ENCODER && s->codec_id == CODEC_ID_H261){
2157 ff_h261_reorder_mb_index(s);
2158 xy= s->mb_y*s->mb_stride + s->mb_x;
2159 mb_type= s->mb_type[xy];
2162 /* write gob / video packet header */
2164 int current_packet_size, is_gob_start;
2166 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2168 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2170 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2172 switch(s->codec_id){
2174 case CODEC_ID_H263P:
2175 if(!s->h263_slice_structured)
2176 if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2178 case CODEC_ID_MPEG2VIDEO:
2179 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2180 case CODEC_ID_MPEG1VIDEO:
2181 if(s->mb_skip_run) is_gob_start=0;
2186 if(s->start_mb_y != mb_y || mb_x!=0){
2189 if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
2190 ff_mpeg4_init_partitions(s);
2194 assert((put_bits_count(&s->pb)&7) == 0);
2195 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2197 if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2198 int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2199 int d= 100 / s->avctx->error_rate;
2201 current_packet_size=0;
2202 s->pb.buf_ptr= s->ptr_lastgob;
2203 assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2207 if (s->avctx->rtp_callback){
2208 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2209 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2212 switch(s->codec_id){
2213 case CODEC_ID_MPEG4:
2214 if (CONFIG_MPEG4_ENCODER) {
2215 ff_mpeg4_encode_video_packet_header(s);
2216 ff_mpeg4_clean_buffers(s);
2219 case CODEC_ID_MPEG1VIDEO:
2220 case CODEC_ID_MPEG2VIDEO:
2221 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2222 ff_mpeg1_encode_slice_header(s);
2223 ff_mpeg1_clean_buffers(s);
2227 case CODEC_ID_H263P:
2228 if (CONFIG_H263_ENCODER)
2229 h263_encode_gob_header(s, mb_y);
2233 if(s->flags&CODEC_FLAG_PASS1){
2234 int bits= put_bits_count(&s->pb);
2235 s->misc_bits+= bits - s->last_bits;
2239 s->ptr_lastgob += current_packet_size;
2240 s->first_slice_line=1;
2241 s->resync_mb_x=mb_x;
2242 s->resync_mb_y=mb_y;
2246 if( (s->resync_mb_x == s->mb_x)
2247 && s->resync_mb_y+1 == s->mb_y){
2248 s->first_slice_line=0;
2252 s->dquant=0; //only for QP_RD
2254 if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD
2256 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2258 copy_context_before_encode(&backup_s, s, -1);
2260 best_s.data_partitioning= s->data_partitioning;
2261 best_s.partitioned_frame= s->partitioned_frame;
2262 if(s->data_partitioning){
2263 backup_s.pb2= s->pb2;
2264 backup_s.tex_pb= s->tex_pb;
2267 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2268 s->mv_dir = MV_DIR_FORWARD;
2269 s->mv_type = MV_TYPE_16X16;
2271 s->mv[0][0][0] = s->p_mv_table[xy][0];
2272 s->mv[0][0][1] = s->p_mv_table[xy][1];
2273 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2274 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2276 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2277 s->mv_dir = MV_DIR_FORWARD;
2278 s->mv_type = MV_TYPE_FIELD;
2281 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2282 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2283 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2285 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2286 &dmin, &next_block, 0, 0);
2288 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2289 s->mv_dir = MV_DIR_FORWARD;
2290 s->mv_type = MV_TYPE_16X16;
2294 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2295 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2297 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2298 s->mv_dir = MV_DIR_FORWARD;
2299 s->mv_type = MV_TYPE_8X8;
2302 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
2303 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
2305 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2306 &dmin, &next_block, 0, 0);
2308 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2309 s->mv_dir = MV_DIR_FORWARD;
2310 s->mv_type = MV_TYPE_16X16;
2312 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2313 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2314 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2315 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2317 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2318 s->mv_dir = MV_DIR_BACKWARD;
2319 s->mv_type = MV_TYPE_16X16;
2321 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2322 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2323 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2324 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2326 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2327 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2328 s->mv_type = MV_TYPE_16X16;
2330 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2331 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2332 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2333 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2334 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2335 &dmin, &next_block, 0, 0);
2337 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2338 s->mv_dir = MV_DIR_FORWARD;
2339 s->mv_type = MV_TYPE_FIELD;
2342 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2343 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2344 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2346 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2347 &dmin, &next_block, 0, 0);
2349 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2350 s->mv_dir = MV_DIR_BACKWARD;
2351 s->mv_type = MV_TYPE_FIELD;
2354 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2355 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2356 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2358 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2359 &dmin, &next_block, 0, 0);
2361 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2362 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2363 s->mv_type = MV_TYPE_FIELD;
2365 for(dir=0; dir<2; dir++){
2367 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2368 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2369 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2372 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2373 &dmin, &next_block, 0, 0);
2375 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2377 s->mv_type = MV_TYPE_16X16;
2381 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2382 &dmin, &next_block, 0, 0);
2383 if(s->h263_pred || s->h263_aic){
2385 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2387 ff_clean_intra_table_entries(s); //old mode?
2391 if((s->flags & CODEC_FLAG_QP_RD) && dmin < INT_MAX){
2392 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2393 const int last_qp= backup_s.qscale;
2396 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2397 static const int dquant_tab[4]={-1,1,-2,2};
2399 assert(backup_s.dquant == 0);
2402 s->mv_dir= best_s.mv_dir;
2403 s->mv_type = MV_TYPE_16X16;
2404 s->mb_intra= best_s.mb_intra;
2405 s->mv[0][0][0] = best_s.mv[0][0][0];
2406 s->mv[0][0][1] = best_s.mv[0][0][1];
2407 s->mv[1][0][0] = best_s.mv[1][0][0];
2408 s->mv[1][0][1] = best_s.mv[1][0][1];
2410 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2411 for(; qpi<4; qpi++){
2412 int dquant= dquant_tab[qpi];
2413 qp= last_qp + dquant;
2414 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2416 backup_s.dquant= dquant;
2417 if(s->mb_intra && s->dc_val[0]){
2419 dc[i]= s->dc_val[0][ s->block_index[i] ];
2420 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
2424 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2425 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2426 if(best_s.qscale != qp){
2427 if(s->mb_intra && s->dc_val[0]){
2429 s->dc_val[0][ s->block_index[i] ]= dc[i];
2430 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
2437 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2438 int mx= s->b_direct_mv_table[xy][0];
2439 int my= s->b_direct_mv_table[xy][1];
2441 backup_s.dquant = 0;
2442 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2444 ff_mpeg4_set_direct_mv(s, mx, my);
2445 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2446 &dmin, &next_block, mx, my);
2448 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2449 backup_s.dquant = 0;
2450 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2452 ff_mpeg4_set_direct_mv(s, 0, 0);
2453 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2454 &dmin, &next_block, 0, 0);
2456 if(!best_s.mb_intra && s->flags2&CODEC_FLAG2_SKIP_RD){
2459 coded |= s->block_last_index[i];
2462 memcpy(s->mv, best_s.mv, sizeof(s->mv));
2463 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2464 mx=my=0; //FIXME find the one we actually used
2465 ff_mpeg4_set_direct_mv(s, mx, my);
2466 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2474 s->mv_dir= best_s.mv_dir;
2475 s->mv_type = best_s.mv_type;
2477 /* s->mv[0][0][0] = best_s.mv[0][0][0];
2478 s->mv[0][0][1] = best_s.mv[0][0][1];
2479 s->mv[1][0][0] = best_s.mv[1][0][0];
2480 s->mv[1][0][1] = best_s.mv[1][0][1];*/
2483 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2484 &dmin, &next_block, mx, my);
2489 s->current_picture.f.qscale_table[xy] = best_s.qscale;
2491 copy_context_after_encode(s, &best_s, -1);
2493 pb_bits_count= put_bits_count(&s->pb);
2494 flush_put_bits(&s->pb);
2495 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2498 if(s->data_partitioning){
2499 pb2_bits_count= put_bits_count(&s->pb2);
2500 flush_put_bits(&s->pb2);
2501 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2502 s->pb2= backup_s.pb2;
2504 tex_pb_bits_count= put_bits_count(&s->tex_pb);
2505 flush_put_bits(&s->tex_pb);
2506 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2507 s->tex_pb= backup_s.tex_pb;
2509 s->last_bits= put_bits_count(&s->pb);
2511 if (CONFIG_H263_ENCODER &&
2512 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2513 ff_h263_update_motion_val(s);
2515 if(next_block==0){ //FIXME 16 vs linesize16
2516 s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad , s->linesize ,16);
2517 s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
2518 s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2521 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2522 MPV_decode_mb(s, s->block);
2524 int motion_x = 0, motion_y = 0;
2525 s->mv_type=MV_TYPE_16X16;
2526 // only one MB-Type possible
2529 case CANDIDATE_MB_TYPE_INTRA:
2532 motion_x= s->mv[0][0][0] = 0;
2533 motion_y= s->mv[0][0][1] = 0;
2535 case CANDIDATE_MB_TYPE_INTER:
2536 s->mv_dir = MV_DIR_FORWARD;
2538 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2539 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2541 case CANDIDATE_MB_TYPE_INTER_I:
2542 s->mv_dir = MV_DIR_FORWARD;
2543 s->mv_type = MV_TYPE_FIELD;
2546 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2547 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2548 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2551 case CANDIDATE_MB_TYPE_INTER4V:
2552 s->mv_dir = MV_DIR_FORWARD;
2553 s->mv_type = MV_TYPE_8X8;
2556 s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
2557 s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
2560 case CANDIDATE_MB_TYPE_DIRECT:
2561 if (CONFIG_MPEG4_ENCODER) {
2562 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2564 motion_x=s->b_direct_mv_table[xy][0];
2565 motion_y=s->b_direct_mv_table[xy][1];
2566 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
2569 case CANDIDATE_MB_TYPE_DIRECT0:
2570 if (CONFIG_MPEG4_ENCODER) {
2571 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2573 ff_mpeg4_set_direct_mv(s, 0, 0);
2576 case CANDIDATE_MB_TYPE_BIDIR:
2577 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2579 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2580 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2581 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2582 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2584 case CANDIDATE_MB_TYPE_BACKWARD:
2585 s->mv_dir = MV_DIR_BACKWARD;
2587 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2588 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2590 case CANDIDATE_MB_TYPE_FORWARD:
2591 s->mv_dir = MV_DIR_FORWARD;
2593 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2594 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2595 // printf(" %d %d ", motion_x, motion_y);
2597 case CANDIDATE_MB_TYPE_FORWARD_I:
2598 s->mv_dir = MV_DIR_FORWARD;
2599 s->mv_type = MV_TYPE_FIELD;
2602 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2603 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2604 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2607 case CANDIDATE_MB_TYPE_BACKWARD_I:
2608 s->mv_dir = MV_DIR_BACKWARD;
2609 s->mv_type = MV_TYPE_FIELD;
2612 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2613 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2614 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2617 case CANDIDATE_MB_TYPE_BIDIR_I:
2618 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2619 s->mv_type = MV_TYPE_FIELD;
2621 for(dir=0; dir<2; dir++){
2623 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2624 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2625 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2630 av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
2633 encode_mb(s, motion_x, motion_y);
2635 // RAL: Update last macroblock type
2636 s->last_mv_dir = s->mv_dir;
2638 if (CONFIG_H263_ENCODER &&
2639 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2640 ff_h263_update_motion_val(s);
2642 MPV_decode_mb(s, s->block);
2645 /* clean the MV table in IPS frames for direct mode in B frames */
2646 if(s->mb_intra /* && I,P,S_TYPE */){
2647 s->p_mv_table[xy][0]=0;
2648 s->p_mv_table[xy][1]=0;
2651 if(s->flags&CODEC_FLAG_PSNR){
2655 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2656 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2658 s->current_picture.f.error[0] += sse(
2659 s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
2660 s->dest[0], w, h, s->linesize);
2661 s->current_picture.f.error[1] += sse(
2662 s, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
2663 s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
2664 s->current_picture.f.error[2] += sse(
2665 s, s->new_picture.f.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
2666 s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
2669 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
2670 ff_h263_loop_filter(s);
2672 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
2676 //not beautiful here but we must write it before flushing so it has to be here
2677 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
2678 msmpeg4_encode_ext_header(s);
2682 /* Send the last GOB if RTP */
2683 if (s->avctx->rtp_callback) {
2684 int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
2685 pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
2686 /* Call the RTP callback to send the last GOB */
2688 s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
2694 #define MERGE(field) dst->field += src->field; src->field=0
2695 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
2696 MERGE(me.scene_change_score);
2697 MERGE(me.mc_mb_var_sum_temp);
2698 MERGE(me.mb_var_sum_temp);
2701 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
2704 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
2705 MERGE(dct_count[1]);
2715 MERGE(padding_bug_score);
2716 MERGE(current_picture.f.error[0]);
2717 MERGE(current_picture.f.error[1]);
2718 MERGE(current_picture.f.error[2]);
2720 if(dst->avctx->noise_reduction){
2721 for(i=0; i<64; i++){
2722 MERGE(dct_error_sum[0][i]);
2723 MERGE(dct_error_sum[1][i]);
2727 assert(put_bits_count(&src->pb) % 8 ==0);
2728 assert(put_bits_count(&dst->pb) % 8 ==0);
2729 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
2730 flush_put_bits(&dst->pb);
2733 static int estimate_qp(MpegEncContext *s, int dry_run){
2734 if (s->next_lambda){
2735 s->current_picture_ptr->f.quality =
2736 s->current_picture.f.quality = s->next_lambda;
2737 if(!dry_run) s->next_lambda= 0;
2738 } else if (!s->fixed_qscale) {
2739 s->current_picture_ptr->f.quality =
2740 s->current_picture.f.quality = ff_rate_estimate_qscale(s, dry_run);
2741 if (s->current_picture.f.quality < 0)
2745 if(s->adaptive_quant){
2746 switch(s->codec_id){
2747 case CODEC_ID_MPEG4:
2748 if (CONFIG_MPEG4_ENCODER)
2749 ff_clean_mpeg4_qscales(s);
2752 case CODEC_ID_H263P:
2754 if (CONFIG_H263_ENCODER)
2755 ff_clean_h263_qscales(s);
2758 ff_init_qscale_tab(s);
2761 s->lambda= s->lambda_table[0];
2764 s->lambda = s->current_picture.f.quality;
2765 //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
2770 /* must be called before writing the header */
2771 static void set_frame_distances(MpegEncContext * s){
2772 assert(s->current_picture_ptr->f.pts != AV_NOPTS_VALUE);
2773 s->time = s->current_picture_ptr->f.pts * s->avctx->time_base.num;
2775 if(s->pict_type==AV_PICTURE_TYPE_B){
2776 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
2777 assert(s->pb_time > 0 && s->pb_time < s->pp_time);
2779 s->pp_time= s->time - s->last_non_b_time;
2780 s->last_non_b_time= s->time;
2781 assert(s->picture_number==0 || s->pp_time > 0);
2785 static int encode_picture(MpegEncContext *s, int picture_number)
2789 int context_count = s->avctx->thread_count;
2791 s->picture_number = picture_number;
2793 /* Reset the average MB variance */
2794 s->me.mb_var_sum_temp =
2795 s->me.mc_mb_var_sum_temp = 0;
2797 /* we need to initialize some time vars before we can encode b-frames */
2798 // RAL: Condition added for MPEG1VIDEO
2799 if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
2800 set_frame_distances(s);
2801 if(CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
2802 ff_set_mpeg4_time(s);
2804 s->me.scene_change_score=0;
2806 // s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
2808 if(s->pict_type==AV_PICTURE_TYPE_I){
2809 if(s->msmpeg4_version >= 3) s->no_rounding=1;
2810 else s->no_rounding=0;
2811 }else if(s->pict_type!=AV_PICTURE_TYPE_B){
2812 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
2813 s->no_rounding ^= 1;
2816 if(s->flags & CODEC_FLAG_PASS2){
2817 if (estimate_qp(s,1) < 0)
2819 ff_get_2pass_fcode(s);
2820 }else if(!(s->flags & CODEC_FLAG_QSCALE)){
2821 if(s->pict_type==AV_PICTURE_TYPE_B)
2822 s->lambda= s->last_lambda_for[s->pict_type];
2824 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
2828 if(s->codec_id != CODEC_ID_AMV){
2829 if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
2830 if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
2831 s->q_chroma_intra_matrix = s->q_intra_matrix;
2832 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
2835 s->mb_intra=0; //for the rate distortion & bit compare functions
2836 for(i=1; i<context_count; i++){
2837 ff_update_duplicate_context(s->thread_context[i], s);
2843 /* Estimate motion for every MB */
2844 if(s->pict_type != AV_PICTURE_TYPE_I){
2845 s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
2846 s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
2847 if(s->pict_type != AV_PICTURE_TYPE_B && s->avctx->me_threshold==0){
2848 if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
2849 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
2853 s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
2854 }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
2856 for(i=0; i<s->mb_stride*s->mb_height; i++)
2857 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
2859 if(!s->fixed_qscale){
2860 /* finding spatial complexity for I-frame rate control */
2861 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
2864 for(i=1; i<context_count; i++){
2865 merge_context_after_me(s, s->thread_context[i]);
2867 s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
2868 s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
2871 if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
2872 s->pict_type= AV_PICTURE_TYPE_I;
2873 for(i=0; i<s->mb_stride*s->mb_height; i++)
2874 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
2875 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
2879 if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
2880 s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
2882 if(s->flags & CODEC_FLAG_INTERLACED_ME){
2884 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
2885 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
2886 s->f_code= FFMAX3(s->f_code, a, b);
2889 ff_fix_long_p_mvs(s);
2890 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
2891 if(s->flags & CODEC_FLAG_INTERLACED_ME){
2895 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
2896 s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
2901 if(s->pict_type==AV_PICTURE_TYPE_B){
2904 a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
2905 b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
2906 s->f_code = FFMAX(a, b);
2908 a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
2909 b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
2910 s->b_code = FFMAX(a, b);
2912 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
2913 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
2914 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
2915 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
2916 if(s->flags & CODEC_FLAG_INTERLACED_ME){
2918 for(dir=0; dir<2; dir++){
2921 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
2922 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
2923 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
2924 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
2932 if (estimate_qp(s, 0) < 0)
2935 if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==AV_PICTURE_TYPE_I && !(s->flags & CODEC_FLAG_QSCALE))
2936 s->qscale= 3; //reduce clipping problems
2938 if (s->out_format == FMT_MJPEG) {
2939 /* for mjpeg, we do include qscale in the matrix */
2941 int j= s->dsp.idct_permutation[i];
2943 s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2945 s->y_dc_scale_table=
2946 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
2947 s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
2948 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
2949 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
2952 if(s->codec_id == CODEC_ID_AMV){
2953 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};
2954 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};
2956 int j= s->dsp.idct_permutation[ff_zigzag_direct[i]];
2958 s->intra_matrix[j] = sp5x_quant_table[5*2+0][i];
2959 s->chroma_intra_matrix[j] = sp5x_quant_table[5*2+1][i];
2961 s->y_dc_scale_table= y;
2962 s->c_dc_scale_table= c;
2963 s->intra_matrix[0] = 13;
2964 s->chroma_intra_matrix[0] = 14;
2965 ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
2966 s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
2967 ff_convert_matrix(&s->dsp, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
2968 s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
2972 //FIXME var duplication
2973 s->current_picture_ptr->f.key_frame =
2974 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
2975 s->current_picture_ptr->f.pict_type =
2976 s->current_picture.f.pict_type = s->pict_type;
2978 if (s->current_picture.f.key_frame)
2979 s->picture_in_gop_number=0;
2981 s->last_bits= put_bits_count(&s->pb);
2982 switch(s->out_format) {
2984 if (CONFIG_MJPEG_ENCODER)
2985 ff_mjpeg_encode_picture_header(s);
2988 if (CONFIG_H261_ENCODER)
2989 ff_h261_encode_picture_header(s, picture_number);
2992 if (CONFIG_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
2993 ff_wmv2_encode_picture_header(s, picture_number);
2994 else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
2995 msmpeg4_encode_picture_header(s, picture_number);
2996 else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
2997 mpeg4_encode_picture_header(s, picture_number);
2998 else if (CONFIG_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
2999 rv10_encode_picture_header(s, picture_number);
3000 else if (CONFIG_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
3001 rv20_encode_picture_header(s, picture_number);
3002 else if (CONFIG_FLV_ENCODER && s->codec_id == CODEC_ID_FLV1)
3003 ff_flv_encode_picture_header(s, picture_number);
3004 else if (CONFIG_H263_ENCODER)
3005 h263_encode_picture_header(s, picture_number);
3008 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3009 mpeg1_encode_picture_header(s, picture_number);
3016 bits= put_bits_count(&s->pb);
3017 s->header_bits= bits - s->last_bits;
3019 for(i=1; i<context_count; i++){
3020 update_duplicate_context_after_me(s->thread_context[i], s);
3022 s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3023 for(i=1; i<context_count; i++){
3024 merge_context_after_encode(s, s->thread_context[i]);
3030 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
3031 const int intra= s->mb_intra;
3034 s->dct_count[intra]++;
3036 for(i=0; i<64; i++){
3037 int level= block[i];
3041 s->dct_error_sum[intra][i] += level;
3042 level -= s->dct_offset[intra][i];
3043 if(level<0) level=0;
3045 s->dct_error_sum[intra][i] -= level;
3046 level += s->dct_offset[intra][i];
3047 if(level>0) level=0;
3054 static int dct_quantize_trellis_c(MpegEncContext *s,
3055 DCTELEM *block, int n,
3056 int qscale, int *overflow){
3058 const uint8_t *scantable= s->intra_scantable.scantable;
3059 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3061 unsigned int threshold1, threshold2;
3073 int coeff_count[64];
3074 int qmul, qadd, start_i, last_non_zero, i, dc;
3075 const int esc_length= s->ac_esc_length;
3077 uint8_t * last_length;
3078 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3080 s->dsp.fdct (block);
3082 if(s->dct_error_sum)
3083 s->denoise_dct(s, block);
3085 qadd= ((qscale-1)|1)*8;
3096 /* For AIC we skip quant/dequant of INTRADC */
3101 /* note: block[0] is assumed to be positive */
3102 block[0] = (block[0] + (q >> 1)) / q;
3105 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3106 if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3107 bias= 1<<(QMAT_SHIFT-1);
3108 length = s->intra_ac_vlc_length;
3109 last_length= s->intra_ac_vlc_last_length;
3113 qmat = s->q_inter_matrix[qscale];
3114 length = s->inter_ac_vlc_length;
3115 last_length= s->inter_ac_vlc_last_length;
3119 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3120 threshold2= (threshold1<<1);
3122 for(i=63; i>=start_i; i--) {
3123 const int j = scantable[i];
3124 int level = block[j] * qmat[j];
3126 if(((unsigned)(level+threshold1))>threshold2){
3132 for(i=start_i; i<=last_non_zero; i++) {
3133 const int j = scantable[i];
3134 int level = block[j] * qmat[j];
3136 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
3137 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
3138 if(((unsigned)(level+threshold1))>threshold2){
3140 level= (bias + level)>>QMAT_SHIFT;
3142 coeff[1][i]= level-1;
3143 // coeff[2][k]= level-2;
3145 level= (bias - level)>>QMAT_SHIFT;
3146 coeff[0][i]= -level;
3147 coeff[1][i]= -level+1;
3148 // coeff[2][k]= -level+2;
3150 coeff_count[i]= FFMIN(level, 2);
3151 assert(coeff_count[i]);
3154 coeff[0][i]= (level>>31)|1;
3159 *overflow= s->max_qcoeff < max; //overflow might have happened
3161 if(last_non_zero < start_i){
3162 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3163 return last_non_zero;
3166 score_tab[start_i]= 0;
3167 survivor[0]= start_i;
3170 for(i=start_i; i<=last_non_zero; i++){
3171 int level_index, j, zero_distortion;
3172 int dct_coeff= FFABS(block[ scantable[i] ]);
3173 int best_score=256*256*256*120;
3175 if ( s->dsp.fdct == fdct_ifast
3176 #ifndef FAAN_POSTSCALE
3177 || s->dsp.fdct == ff_faandct
3180 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3181 zero_distortion= dct_coeff*dct_coeff;
3183 for(level_index=0; level_index < coeff_count[i]; level_index++){
3185 int level= coeff[level_index][i];
3186 const int alevel= FFABS(level);
3191 if(s->out_format == FMT_H263){
3192 unquant_coeff= alevel*qmul + qadd;
3194 j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
3196 unquant_coeff = (int)( alevel * qscale * s->intra_matrix[j]) >> 3;
3197 unquant_coeff = (unquant_coeff - 1) | 1;
3199 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3200 unquant_coeff = (unquant_coeff - 1) | 1;
3205 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3207 if((level&(~127)) == 0){
3208 for(j=survivor_count-1; j>=0; j--){
3209 int run= i - survivor[j];
3210 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3211 score += score_tab[i-run];
3213 if(score < best_score){
3216 level_tab[i+1]= level-64;
3220 if(s->out_format == FMT_H263){
3221 for(j=survivor_count-1; j>=0; j--){
3222 int run= i - survivor[j];
3223 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3224 score += score_tab[i-run];
3225 if(score < last_score){
3228 last_level= level-64;
3234 distortion += esc_length*lambda;
3235 for(j=survivor_count-1; j>=0; j--){
3236 int run= i - survivor[j];
3237 int score= distortion + score_tab[i-run];
3239 if(score < best_score){
3242 level_tab[i+1]= level-64;
3246 if(s->out_format == FMT_H263){
3247 for(j=survivor_count-1; j>=0; j--){
3248 int run= i - survivor[j];
3249 int score= distortion + score_tab[i-run];
3250 if(score < last_score){
3253 last_level= level-64;
3261 score_tab[i+1]= best_score;
3263 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3264 if(last_non_zero <= 27){
3265 for(; survivor_count; survivor_count--){
3266 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3270 for(; survivor_count; survivor_count--){
3271 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3276 survivor[ survivor_count++ ]= i+1;
3279 if(s->out_format != FMT_H263){
3280 last_score= 256*256*256*120;
3281 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3282 int score= score_tab[i];
3283 if(i) score += lambda*2; //FIXME exacter?
3285 if(score < last_score){
3288 last_level= level_tab[i];
3289 last_run= run_tab[i];
3294 s->coded_score[n] = last_score;
3296 dc= FFABS(block[0]);
3297 last_non_zero= last_i - 1;
3298 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3300 if(last_non_zero < start_i)
3301 return last_non_zero;
3303 if(last_non_zero == 0 && start_i == 0){
3305 int best_score= dc * dc;
3307 for(i=0; i<coeff_count[0]; i++){
3308 int level= coeff[i][0];
3309 int alevel= FFABS(level);
3310 int unquant_coeff, score, distortion;
3312 if(s->out_format == FMT_H263){
3313 unquant_coeff= (alevel*qmul + qadd)>>3;
3315 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3316 unquant_coeff = (unquant_coeff - 1) | 1;
3318 unquant_coeff = (unquant_coeff + 4) >> 3;
3319 unquant_coeff<<= 3 + 3;
3321 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3323 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3324 else score= distortion + esc_length*lambda;
3326 if(score < best_score){
3328 best_level= level - 64;
3331 block[0]= best_level;
3332 s->coded_score[n] = best_score - dc*dc;
3333 if(best_level == 0) return -1;
3334 else return last_non_zero;
3340 block[ perm_scantable[last_non_zero] ]= last_level;
3343 for(; i>start_i; i -= run_tab[i] + 1){
3344 block[ perm_scantable[i-1] ]= level_tab[i];
3347 return last_non_zero;
3350 //#define REFINE_STATS 1
3351 static int16_t basis[64][64];
3353 static void build_basis(uint8_t *perm){
3360 double s= 0.25*(1<<BASIS_SHIFT);
3362 int perm_index= perm[index];
3363 if(i==0) s*= sqrt(0.5);
3364 if(j==0) s*= sqrt(0.5);
3365 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)));
3372 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3373 DCTELEM *block, int16_t *weight, DCTELEM *orig,
3376 LOCAL_ALIGNED_16(DCTELEM, d1, [64]);
3377 const uint8_t *scantable= s->intra_scantable.scantable;
3378 const uint8_t *perm_scantable= s->intra_scantable.permutated;
3379 // unsigned int threshold1, threshold2;
3384 int qmul, qadd, start_i, last_non_zero, i, dc;
3386 uint8_t * last_length;
3388 int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3391 static int after_last=0;
3392 static int to_zero=0;
3393 static int from_zero=0;
3396 static int messed_sign=0;
3399 if(basis[0][0] == 0)
3400 build_basis(s->dsp.idct_permutation);
3411 /* For AIC we skip quant/dequant of INTRADC */
3415 q <<= RECON_SHIFT-3;
3416 /* note: block[0] is assumed to be positive */
3418 // block[0] = (block[0] + (q >> 1)) / q;
3420 // if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3421 // bias= 1<<(QMAT_SHIFT-1);
3422 length = s->intra_ac_vlc_length;
3423 last_length= s->intra_ac_vlc_last_length;
3427 length = s->inter_ac_vlc_length;
3428 last_length= s->inter_ac_vlc_last_length;
3430 last_non_zero = s->block_last_index[n];
3435 dc += (1<<(RECON_SHIFT-1));
3436 for(i=0; i<64; i++){
3437 rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME use orig dirrectly instead of copying to rem[]
3440 STOP_TIMER("memset rem[]")}
3443 for(i=0; i<64; i++){
3448 w= FFABS(weight[i]) + qns*one;
3449 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3452 // w=weight[i] = (63*qns + (w/2)) / w;
3458 lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3464 for(i=start_i; i<=last_non_zero; i++){
3465 int j= perm_scantable[i];
3466 const int level= block[j];
3470 if(level<0) coeff= qmul*level - qadd;
3471 else coeff= qmul*level + qadd;
3472 run_tab[rle_index++]=run;
3475 s->dsp.add_8x8basis(rem, basis[j], coeff);
3481 if(last_non_zero>0){
3482 STOP_TIMER("init rem[]")
3489 int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
3492 int run2, best_unquant_change=0, analyze_gradient;
3496 analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
3498 if(analyze_gradient){
3502 for(i=0; i<64; i++){
3505 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3508 STOP_TIMER("rem*w*w")}
3518 const int level= block[0];
3519 int change, old_coeff;
3521 assert(s->mb_intra);
3525 for(change=-1; change<=1; change+=2){
3526 int new_level= level + change;
3527 int score, new_coeff;
3529 new_coeff= q*new_level;
3530 if(new_coeff >= 2048 || new_coeff < 0)
3533 score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3534 if(score<best_score){
3537 best_change= change;
3538 best_unquant_change= new_coeff - old_coeff;
3545 run2= run_tab[rle_index++];
3549 for(i=start_i; i<64; i++){
3550 int j= perm_scantable[i];
3551 const int level= block[j];
3552 int change, old_coeff;
3554 if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
3558 if(level<0) old_coeff= qmul*level - qadd;
3559 else old_coeff= qmul*level + qadd;
3560 run2= run_tab[rle_index++]; //FIXME ! maybe after last
3564 assert(run2>=0 || i >= last_non_zero );
3567 for(change=-1; change<=1; change+=2){
3568 int new_level= level + change;
3569 int score, new_coeff, unquant_change;
3572 if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
3576 if(new_level<0) new_coeff= qmul*new_level - qadd;
3577 else new_coeff= qmul*new_level + qadd;
3578 if(new_coeff >= 2048 || new_coeff <= -2048)
3580 //FIXME check for overflow
3583 if(level < 63 && level > -63){
3584 if(i < last_non_zero)
3585 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
3586 - length[UNI_AC_ENC_INDEX(run, level+64)];
3588 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
3589 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
3592 assert(FFABS(new_level)==1);
3594 if(analyze_gradient){
3595 int g= d1[ scantable[i] ];
3596 if(g && (g^new_level) >= 0)
3600 if(i < last_non_zero){
3601 int next_i= i + run2 + 1;
3602 int next_level= block[ perm_scantable[next_i] ] + 64;
3604 if(next_level&(~127))
3607 if(next_i < last_non_zero)
3608 score += length[UNI_AC_ENC_INDEX(run, 65)]
3609 + length[UNI_AC_ENC_INDEX(run2, next_level)]
3610 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3612 score += length[UNI_AC_ENC_INDEX(run, 65)]
3613 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
3614 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3616 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
3618 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
3619 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
3625 assert(FFABS(level)==1);
3627 if(i < last_non_zero){
3628 int next_i= i + run2 + 1;
3629 int next_level= block[ perm_scantable[next_i] ] + 64;
3631 if(next_level&(~127))
3634 if(next_i < last_non_zero)
3635 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
3636 - length[UNI_AC_ENC_INDEX(run2, next_level)]
3637 - length[UNI_AC_ENC_INDEX(run, 65)];
3639 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
3640 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
3641 - length[UNI_AC_ENC_INDEX(run, 65)];
3643 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
3645 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
3646 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
3653 unquant_change= new_coeff - old_coeff;
3654 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
3656 score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
3657 if(score<best_score){
3660 best_change= change;
3661 best_unquant_change= unquant_change;
3665 prev_level= level + 64;
3666 if(prev_level&(~127))
3675 STOP_TIMER("iterative step")}
3679 int j= perm_scantable[ best_coeff ];
3681 block[j] += best_change;
3683 if(best_coeff > last_non_zero){
3684 last_non_zero= best_coeff;
3692 if(block[j] - best_change){
3693 if(FFABS(block[j]) > FFABS(block[j] - best_change)){
3705 for(; last_non_zero>=start_i; last_non_zero--){
3706 if(block[perm_scantable[last_non_zero]])
3712 if(256*256*256*64 % count == 0){
3713 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);
3718 for(i=start_i; i<=last_non_zero; i++){
3719 int j= perm_scantable[i];
3720 const int level= block[j];
3723 run_tab[rle_index++]=run;
3730 s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
3736 if(last_non_zero>0){
3737 STOP_TIMER("iterative search")
3742 return last_non_zero;
3745 int dct_quantize_c(MpegEncContext *s,
3746 DCTELEM *block, int n,
3747 int qscale, int *overflow)
3749 int i, j, level, last_non_zero, q, start_i;
3751 const uint8_t *scantable= s->intra_scantable.scantable;
3754 unsigned int threshold1, threshold2;
3756 s->dsp.fdct (block);
3758 if(s->dct_error_sum)
3759 s->denoise_dct(s, block);
3769 /* For AIC we skip quant/dequant of INTRADC */
3772 /* note: block[0] is assumed to be positive */
3773 block[0] = (block[0] + (q >> 1)) / q;
3776 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3777 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3781 qmat = s->q_inter_matrix[qscale];
3782 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3784 threshold1= (1<<QMAT_SHIFT) - bias - 1;
3785 threshold2= (threshold1<<1);
3786 for(i=63;i>=start_i;i--) {
3788 level = block[j] * qmat[j];
3790 if(((unsigned)(level+threshold1))>threshold2){
3797 for(i=start_i; i<=last_non_zero; i++) {
3799 level = block[j] * qmat[j];
3801 // if( bias+level >= (1<<QMAT_SHIFT)
3802 // || bias-level >= (1<<QMAT_SHIFT)){
3803 if(((unsigned)(level+threshold1))>threshold2){
3805 level= (bias + level)>>QMAT_SHIFT;
3808 level= (bias - level)>>QMAT_SHIFT;
3816 *overflow= s->max_qcoeff < max; //overflow might have happened
3818 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3819 if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
3820 ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
3822 return last_non_zero;
3825 #define OFFSET(x) offsetof(MpegEncContext, x)
3826 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
3827 static const AVOption h263_options[] = {
3828 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
3829 { "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},
3833 static const AVClass h263_class = {
3834 .class_name = "H.263 encoder",
3835 .item_name = av_default_item_name,
3836 .option = h263_options,
3837 .version = LIBAVUTIL_VERSION_INT,
3840 AVCodec ff_h263_encoder = {
3842 .type = AVMEDIA_TYPE_VIDEO,
3843 .id = CODEC_ID_H263,
3844 .priv_data_size = sizeof(MpegEncContext),
3845 .init = MPV_encode_init,
3846 .encode = MPV_encode_picture,
3847 .close = MPV_encode_end,
3848 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3849 .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
3850 .priv_class = &h263_class,
3853 static const AVOption h263p_options[] = {
3854 { "umv", "Use unlimited motion vectors.", OFFSET(umvplus), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
3855 { "aiv", "Use alternative inter VLC.", OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
3856 { "obmc", "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
3857 { "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},
3860 static const AVClass h263p_class = {
3861 .class_name = "H.263p encoder",
3862 .item_name = av_default_item_name,
3863 .option = h263p_options,
3864 .version = LIBAVUTIL_VERSION_INT,
3867 AVCodec ff_h263p_encoder = {
3869 .type = AVMEDIA_TYPE_VIDEO,
3870 .id = CODEC_ID_H263P,
3871 .priv_data_size = sizeof(MpegEncContext),
3872 .init = MPV_encode_init,
3873 .encode = MPV_encode_picture,
3874 .close = MPV_encode_end,
3875 .capabilities = CODEC_CAP_SLICE_THREADS,
3876 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3877 .long_name= NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
3878 .priv_class = &h263p_class,
3881 AVCodec ff_msmpeg4v2_encoder = {
3882 .name = "msmpeg4v2",
3883 .type = AVMEDIA_TYPE_VIDEO,
3884 .id = CODEC_ID_MSMPEG4V2,
3885 .priv_data_size = sizeof(MpegEncContext),
3886 .init = MPV_encode_init,
3887 .encode = MPV_encode_picture,
3888 .close = MPV_encode_end,
3889 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3890 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
3893 AVCodec ff_msmpeg4v3_encoder = {
3895 .type = AVMEDIA_TYPE_VIDEO,
3896 .id = CODEC_ID_MSMPEG4V3,
3897 .priv_data_size = sizeof(MpegEncContext),
3898 .init = MPV_encode_init,
3899 .encode = MPV_encode_picture,
3900 .close = MPV_encode_end,
3901 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3902 .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
3905 AVCodec ff_wmv1_encoder = {
3907 .type = AVMEDIA_TYPE_VIDEO,
3908 .id = CODEC_ID_WMV1,
3909 .priv_data_size = sizeof(MpegEncContext),
3910 .init = MPV_encode_init,
3911 .encode = MPV_encode_picture,
3912 .close = MPV_encode_end,
3913 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3914 .long_name= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),