h263enc: check w/h for validity
[ffmpeg.git] / libavcodec / mpegvideo_enc.c
1 /*
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>
5  *
6  * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 /**
26  * @file
27  * The simplest mpeg encoder (well, it was the simplest!).
28  */
29
30 #include "libavutil/intmath.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "mpegvideo.h"
36 #include "mpegvideo_common.h"
37 #include "h263.h"
38 #include "mjpegenc.h"
39 #include "msmpeg4.h"
40 #include "faandct.h"
41 #include "thread.h"
42 #include "aandcttab.h"
43 #include "flv.h"
44 #include "mpeg4video.h"
45 #include "internal.h"
46 #include "bytestream.h"
47 #include <limits.h>
48 #include "sp5x.h"
49
50 //#undef NDEBUG
51 //#include <assert.h>
52
53 static int encode_picture(MpegEncContext *s, int picture_number);
54 static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
55 static int sse_mb(MpegEncContext *s);
56 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block);
57 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
58
59 /* enable all paranoid tests for rounding, overflows, etc... */
60 //#define PARANOID
61
62 //#define DEBUG
63
64 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
65 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
66
67 const AVOption ff_mpv_generic_options[] = {
68     FF_MPV_COMMON_OPTS
69     { NULL },
70 };
71
72 void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
73                        uint16_t (*qmat16)[2][64],
74                        const uint16_t *quant_matrix,
75                        int bias, int qmin, int qmax, int intra)
76 {
77     int qscale;
78     int shift = 0;
79
80     for (qscale = qmin; qscale <= qmax; qscale++) {
81         int i;
82         if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
83             dsp->fdct == ff_jpeg_fdct_islow_10 ||
84             dsp->fdct == ff_faandct) {
85             for (i = 0; i < 64; i++) {
86                 const int j = dsp->idct_permutation[i];
87                 /* 16 <= qscale * quant_matrix[i] <= 7905
88                  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
89                  *             19952 <=              x  <= 249205026
90                  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
91                  *           3444240 >= (1 << 36) / (x) >= 275 */
92
93                 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
94                                         (qscale * quant_matrix[j]));
95             }
96         } else if (dsp->fdct == ff_fdct_ifast) {
97             for (i = 0; i < 64; i++) {
98                 const int j = dsp->idct_permutation[i];
99                 /* 16 <= qscale * quant_matrix[i] <= 7905
100                  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
101                  *             19952 <=              x  <= 249205026
102                  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
103                  *           3444240 >= (1 << 36) / (x) >= 275 */
104
105                 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
106                                         (ff_aanscales[i] * qscale * quant_matrix[j]));
107             }
108         } else {
109             for (i = 0; i < 64; i++) {
110                 const int j = dsp->idct_permutation[i];
111                 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
112                  * Assume x = qscale * quant_matrix[i]
113                  * So             16 <=              x  <= 7905
114                  * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
115                  * so          32768 >= (1 << 19) / (x) >= 67 */
116                 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
117                                         (qscale * quant_matrix[j]));
118                 //qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) /
119                 //                    (qscale * quant_matrix[i]);
120                 qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) /
121                                        (qscale * quant_matrix[j]);
122
123                 if (qmat16[qscale][0][i] == 0 ||
124                     qmat16[qscale][0][i] == 128 * 256)
125                     qmat16[qscale][0][i] = 128 * 256 - 1;
126                 qmat16[qscale][1][i] =
127                     ROUNDED_DIV(bias << (16 - QUANT_BIAS_SHIFT),
128                                 qmat16[qscale][0][i]);
129             }
130         }
131
132         for (i = intra; i < 64; i++) {
133             int64_t max = 8191;
134             if (dsp->fdct == ff_fdct_ifast) {
135                 max = (8191LL * ff_aanscales[i]) >> 14;
136             }
137             while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
138                 shift++;
139             }
140         }
141     }
142     if (shift) {
143         av_log(NULL, AV_LOG_INFO,
144                "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
145                QMAT_SHIFT - shift);
146     }
147 }
148
149 static inline void update_qscale(MpegEncContext *s)
150 {
151     s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
152                 (FF_LAMBDA_SHIFT + 7);
153     s->qscale = av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
154
155     s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
156                  FF_LAMBDA_SHIFT;
157 }
158
159 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
160 {
161     int i;
162
163     if (matrix) {
164         put_bits(pb, 1, 1);
165         for (i = 0; i < 64; i++) {
166             put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
167         }
168     } else
169         put_bits(pb, 1, 0);
170 }
171
172 /**
173  * init s->current_picture.qscale_table from s->lambda_table
174  */
175 void ff_init_qscale_tab(MpegEncContext *s)
176 {
177     int8_t * const qscale_table = s->current_picture.f.qscale_table;
178     int i;
179
180     for (i = 0; i < s->mb_num; i++) {
181         unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
182         int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
183         qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
184                                                   s->avctx->qmax);
185     }
186 }
187
188 static void copy_picture_attributes(MpegEncContext *s,
189                                     AVFrame *dst,
190                                     AVFrame *src)
191 {
192     int i;
193
194     dst->pict_type              = src->pict_type;
195     dst->quality                = src->quality;
196     dst->coded_picture_number   = src->coded_picture_number;
197     dst->display_picture_number = src->display_picture_number;
198     //dst->reference              = src->reference;
199     dst->pts                    = src->pts;
200     dst->interlaced_frame       = src->interlaced_frame;
201     dst->top_field_first        = src->top_field_first;
202
203     if (s->avctx->me_threshold) {
204         if (!src->motion_val[0])
205             av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
206         if (!src->mb_type)
207             av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
208         if (!src->ref_index[0])
209             av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
210         if (src->motion_subsample_log2 != dst->motion_subsample_log2)
211             av_log(s->avctx, AV_LOG_ERROR,
212                    "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
213                    src->motion_subsample_log2, dst->motion_subsample_log2);
214
215         memcpy(dst->mb_type, src->mb_type,
216                s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
217
218         for (i = 0; i < 2; i++) {
219             int stride = ((16 * s->mb_width ) >>
220                           src->motion_subsample_log2) + 1;
221             int height = ((16 * s->mb_height) >> src->motion_subsample_log2);
222
223             if (src->motion_val[i] &&
224                 src->motion_val[i] != dst->motion_val[i]) {
225                 memcpy(dst->motion_val[i], src->motion_val[i],
226                        2 * stride * height * sizeof(int16_t));
227             }
228             if (src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]) {
229                 memcpy(dst->ref_index[i], src->ref_index[i],
230                        s->mb_stride * 4 * s->mb_height * sizeof(int8_t));
231             }
232         }
233     }
234 }
235
236 static void update_duplicate_context_after_me(MpegEncContext *dst,
237                                               MpegEncContext *src)
238 {
239 #define COPY(a) dst->a= src->a
240     COPY(pict_type);
241     COPY(current_picture);
242     COPY(f_code);
243     COPY(b_code);
244     COPY(qscale);
245     COPY(lambda);
246     COPY(lambda2);
247     COPY(picture_in_gop_number);
248     COPY(gop_picture_number);
249     COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
250     COPY(progressive_frame);    // FIXME don't set in encode_header
251     COPY(partitioned_frame);    // FIXME don't set in encode_header
252 #undef COPY
253 }
254
255 /**
256  * Set the given MpegEncContext to defaults for encoding.
257  * the changed fields will not depend upon the prior state of the MpegEncContext.
258  */
259 static void MPV_encode_defaults(MpegEncContext *s)
260 {
261     int i;
262     ff_MPV_common_defaults(s);
263
264     for (i = -16; i < 16; i++) {
265         default_fcode_tab[i + MAX_MV] = 1;
266     }
267     s->me.mv_penalty = default_mv_penalty;
268     s->fcode_tab     = default_fcode_tab;
269 }
270
271 /* init video encoder */
272 av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
273 {
274     MpegEncContext *s = avctx->priv_data;
275     int i;
276     int chroma_h_shift, chroma_v_shift;
277
278     MPV_encode_defaults(s);
279
280     switch (avctx->codec_id) {
281     case CODEC_ID_MPEG2VIDEO:
282         if (avctx->pix_fmt != PIX_FMT_YUV420P &&
283             avctx->pix_fmt != PIX_FMT_YUV422P) {
284             av_log(avctx, AV_LOG_ERROR,
285                    "only YUV420 and YUV422 are supported\n");
286             return -1;
287         }
288         break;
289     case CODEC_ID_LJPEG:
290         if (avctx->pix_fmt != PIX_FMT_YUVJ420P &&
291             avctx->pix_fmt != PIX_FMT_YUVJ422P &&
292             avctx->pix_fmt != PIX_FMT_YUVJ444P &&
293             avctx->pix_fmt != PIX_FMT_BGR0     &&
294             avctx->pix_fmt != PIX_FMT_BGRA     &&
295             avctx->pix_fmt != PIX_FMT_BGR24    &&
296             ((avctx->pix_fmt != PIX_FMT_YUV420P &&
297               avctx->pix_fmt != PIX_FMT_YUV422P &&
298               avctx->pix_fmt != PIX_FMT_YUV444P) ||
299              avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
300             av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
301             return -1;
302         }
303         break;
304     case CODEC_ID_MJPEG:
305     case CODEC_ID_AMV:
306         if (avctx->pix_fmt != PIX_FMT_YUVJ420P &&
307             avctx->pix_fmt != PIX_FMT_YUVJ422P &&
308             ((avctx->pix_fmt != PIX_FMT_YUV420P &&
309               avctx->pix_fmt != PIX_FMT_YUV422P) ||
310              avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
311             av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
312             return -1;
313         }
314         break;
315     default:
316         if (avctx->pix_fmt != PIX_FMT_YUV420P) {
317             av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
318             return -1;
319         }
320     }
321
322     switch (avctx->pix_fmt) {
323     case PIX_FMT_YUVJ422P:
324     case PIX_FMT_YUV422P:
325         s->chroma_format = CHROMA_422;
326         break;
327     case PIX_FMT_YUVJ420P:
328     case PIX_FMT_YUV420P:
329     default:
330         s->chroma_format = CHROMA_420;
331         break;
332     }
333
334     s->bit_rate = avctx->bit_rate;
335     s->width    = avctx->width;
336     s->height   = avctx->height;
337     if (avctx->gop_size > 600 &&
338         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
339         av_log(avctx, AV_LOG_WARNING,
340                "keyframe interval too large!, reducing it from %d to %d\n",
341                avctx->gop_size, 600);
342         avctx->gop_size = 600;
343     }
344     s->gop_size     = avctx->gop_size;
345     s->avctx        = avctx;
346     s->flags        = avctx->flags;
347     s->flags2       = avctx->flags2;
348     s->max_b_frames = avctx->max_b_frames;
349     s->codec_id     = avctx->codec->id;
350 #if FF_API_MPV_GLOBAL_OPTS
351     if (avctx->luma_elim_threshold)
352         s->luma_elim_threshold   = avctx->luma_elim_threshold;
353     if (avctx->chroma_elim_threshold)
354         s->chroma_elim_threshold = avctx->chroma_elim_threshold;
355 #endif
356     s->strict_std_compliance = avctx->strict_std_compliance;
357     s->quarter_sample     = (avctx->flags & CODEC_FLAG_QPEL) != 0;
358     s->mpeg_quant         = avctx->mpeg_quant;
359     s->rtp_mode           = !!avctx->rtp_payload_size;
360     s->intra_dc_precision = avctx->intra_dc_precision;
361     s->user_specified_pts = AV_NOPTS_VALUE;
362
363     if (s->gop_size <= 1) {
364         s->intra_only = 1;
365         s->gop_size   = 12;
366     } else {
367         s->intra_only = 0;
368     }
369
370     s->me_method = avctx->me_method;
371
372     /* Fixed QSCALE */
373     s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
374
375 #if FF_API_MPV_GLOBAL_OPTS
376     if (s->flags & CODEC_FLAG_QP_RD)
377         s->mpv_flags |= FF_MPV_FLAG_QP_RD;
378 #endif
379
380     s->adaptive_quant = (s->avctx->lumi_masking ||
381                          s->avctx->dark_masking ||
382                          s->avctx->temporal_cplx_masking ||
383                          s->avctx->spatial_cplx_masking  ||
384                          s->avctx->p_masking      ||
385                          s->avctx->border_masking ||
386                          (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
387                         !s->fixed_qscale;
388
389     s->loop_filter      = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
390
391     if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
392         av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
393         if (avctx->rc_max_rate && !avctx->rc_buffer_size)
394             return -1;
395     }
396
397     if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
398         av_log(avctx, AV_LOG_INFO,
399                "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
400     }
401
402     if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
403         av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
404         return -1;
405     }
406
407     if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
408         av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
409         return -1;
410     }
411
412     if (avctx->rc_max_rate &&
413         avctx->rc_max_rate == avctx->bit_rate &&
414         avctx->rc_max_rate != avctx->rc_min_rate) {
415         av_log(avctx, AV_LOG_INFO,
416                "impossible bitrate constraints, this will fail\n");
417     }
418
419     if (avctx->rc_buffer_size &&
420         avctx->bit_rate * (int64_t)avctx->time_base.num >
421             avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
422         av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
423         return -1;
424     }
425
426     if (!s->fixed_qscale &&
427         avctx->bit_rate * av_q2d(avctx->time_base) >
428             avctx->bit_rate_tolerance) {
429         av_log(avctx, AV_LOG_ERROR,
430                "bitrate tolerance too small for bitrate\n");
431         return -1;
432     }
433
434     if (s->avctx->rc_max_rate &&
435         s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
436         (s->codec_id == CODEC_ID_MPEG1VIDEO ||
437          s->codec_id == CODEC_ID_MPEG2VIDEO) &&
438         90000LL * (avctx->rc_buffer_size - 1) >
439             s->avctx->rc_max_rate * 0xFFFFLL) {
440         av_log(avctx, AV_LOG_INFO,
441                "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
442                "specified vbv buffer is too large for the given bitrate!\n");
443     }
444
445     if ((s->flags & CODEC_FLAG_4MV)  && s->codec_id != CODEC_ID_MPEG4 &&
446         s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P &&
447         s->codec_id != CODEC_ID_FLV1) {
448         av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
449         return -1;
450     }
451
452     if (s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
453         av_log(avctx, AV_LOG_ERROR,
454                "OBMC is only supported with simple mb decision\n");
455         return -1;
456     }
457
458     if (s->quarter_sample && s->codec_id != CODEC_ID_MPEG4) {
459         av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
460         return -1;
461     }
462
463     if (s->max_b_frames                    &&
464         s->codec_id != CODEC_ID_MPEG4      &&
465         s->codec_id != CODEC_ID_MPEG1VIDEO &&
466         s->codec_id != CODEC_ID_MPEG2VIDEO) {
467         av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
468         return -1;
469     }
470
471     if ((s->codec_id == CODEC_ID_MPEG4 ||
472          s->codec_id == CODEC_ID_H263  ||
473          s->codec_id == CODEC_ID_H263P) &&
474         (avctx->sample_aspect_ratio.num > 255 ||
475          avctx->sample_aspect_ratio.den > 255)) {
476         av_log(avctx, AV_LOG_WARNING,
477                "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
478                avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
479         av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
480                    avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den, 255);
481     }
482
483     if ((s->codec_id == CODEC_ID_H263  ||
484          s->codec_id == CODEC_ID_H263P) &&
485         (avctx->width  > 2048 ||
486          avctx->height > 1152 )) {
487         av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
488         return -1;
489     }
490
491     if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
492         s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO) {
493         av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
494         return -1;
495     }
496
497     // FIXME mpeg2 uses that too
498     if (s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4) {
499         av_log(avctx, AV_LOG_ERROR,
500                "mpeg2 style quantization not supported by codec\n");
501         return -1;
502     }
503
504 #if FF_API_MPV_GLOBAL_OPTS
505     if (s->flags & CODEC_FLAG_CBP_RD)
506         s->mpv_flags |= FF_MPV_FLAG_CBP_RD;
507 #endif
508
509     if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
510         av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
511         return -1;
512     }
513
514     if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
515         s->avctx->mb_decision != FF_MB_DECISION_RD) {
516         av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
517         return -1;
518     }
519
520     if (s->avctx->scenechange_threshold < 1000000000 &&
521         (s->flags & CODEC_FLAG_CLOSED_GOP)) {
522         av_log(avctx, AV_LOG_ERROR,
523                "closed gop with scene change detection are not supported yet, "
524                "set threshold to 1000000000\n");
525         return -1;
526     }
527
528     if (s->flags & CODEC_FLAG_LOW_DELAY) {
529         if (s->codec_id != CODEC_ID_MPEG2VIDEO) {
530             av_log(avctx, AV_LOG_ERROR,
531                   "low delay forcing is only available for mpeg2\n");
532             return -1;
533         }
534         if (s->max_b_frames != 0) {
535             av_log(avctx, AV_LOG_ERROR,
536                    "b frames cannot be used with low delay\n");
537             return -1;
538         }
539     }
540
541     if (s->q_scale_type == 1) {
542         if (avctx->qmax > 12) {
543             av_log(avctx, AV_LOG_ERROR,
544                    "non linear quant only supports qmax <= 12 currently\n");
545             return -1;
546         }
547     }
548
549     if (s->avctx->thread_count > 1         &&
550         s->codec_id != CODEC_ID_MPEG4      &&
551         s->codec_id != CODEC_ID_MPEG1VIDEO &&
552         s->codec_id != CODEC_ID_MPEG2VIDEO &&
553         (s->codec_id != CODEC_ID_H263P)) {
554         av_log(avctx, AV_LOG_ERROR,
555                "multi threaded encoding not supported by codec\n");
556         return -1;
557     }
558
559     if (s->avctx->thread_count < 1) {
560         av_log(avctx, AV_LOG_ERROR,
561                "automatic thread number detection not supported by codec, "
562                "patch welcome\n");
563         return -1;
564     }
565
566     if (s->avctx->thread_count > 1)
567         s->rtp_mode = 1;
568
569     if (!avctx->time_base.den || !avctx->time_base.num) {
570         av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
571         return -1;
572     }
573
574     i = (INT_MAX / 2 + 128) >> 8;
575     if (avctx->me_threshold >= i) {
576         av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n",
577                i - 1);
578         return -1;
579     }
580     if (avctx->mb_threshold >= i) {
581         av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
582                i - 1);
583         return -1;
584     }
585
586     if (avctx->b_frame_strategy && (avctx->flags & CODEC_FLAG_PASS2)) {
587         av_log(avctx, AV_LOG_INFO,
588                "notice: b_frame_strategy only affects the first pass\n");
589         avctx->b_frame_strategy = 0;
590     }
591
592     i = av_gcd(avctx->time_base.den, avctx->time_base.num);
593     if (i > 1) {
594         av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
595         avctx->time_base.den /= i;
596         avctx->time_base.num /= i;
597         //return -1;
598     }
599
600     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) {
601         // (a + x * 3 / 8) / x
602         s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
603         s->inter_quant_bias = 0;
604     } else {
605         s->intra_quant_bias = 0;
606         // (a - x / 4) / x
607         s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
608     }
609
610     if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
611         s->intra_quant_bias = avctx->intra_quant_bias;
612     if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
613         s->inter_quant_bias = avctx->inter_quant_bias;
614
615     av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
616
617     avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
618                                   &chroma_v_shift);
619
620     if (avctx->codec_id == CODEC_ID_MPEG4 &&
621         s->avctx->time_base.den > (1 << 16) - 1) {
622         av_log(avctx, AV_LOG_ERROR,
623                "timebase %d/%d not supported by MPEG 4 standard, "
624                "the maximum admitted value for the timebase denominator "
625                "is %d\n", s->avctx->time_base.num, s->avctx->time_base.den,
626                (1 << 16) - 1);
627         return -1;
628     }
629     s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
630
631 #if FF_API_MPV_GLOBAL_OPTS
632     if (avctx->flags2 & CODEC_FLAG2_SKIP_RD)
633         s->mpv_flags |= FF_MPV_FLAG_SKIP_RD;
634     if (avctx->flags2 & CODEC_FLAG2_STRICT_GOP)
635         s->mpv_flags |= FF_MPV_FLAG_STRICT_GOP;
636     if (avctx->quantizer_noise_shaping)
637         s->quantizer_noise_shaping = avctx->quantizer_noise_shaping;
638 #endif
639
640     switch (avctx->codec->id) {
641     case CODEC_ID_MPEG1VIDEO:
642         s->out_format = FMT_MPEG1;
643         s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
644         avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
645         break;
646     case CODEC_ID_MPEG2VIDEO:
647         s->out_format = FMT_MPEG1;
648         s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
649         avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
650         s->rtp_mode   = 1;
651         break;
652     case CODEC_ID_LJPEG:
653     case CODEC_ID_MJPEG:
654     case CODEC_ID_AMV:
655         s->out_format = FMT_MJPEG;
656         s->intra_only = 1; /* force intra only for jpeg */
657         if (avctx->codec->id == CODEC_ID_LJPEG &&
658             (avctx->pix_fmt == PIX_FMT_BGR0
659              || s->avctx->pix_fmt == PIX_FMT_BGRA
660              || s->avctx->pix_fmt == PIX_FMT_BGR24)) {
661             s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
662             s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
663             s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
664         } else {
665             s->mjpeg_vsample[0] = 2;
666             s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
667             s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
668             s->mjpeg_hsample[0] = 2;
669             s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
670             s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
671         }
672         if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
673             ff_mjpeg_encode_init(s) < 0)
674             return -1;
675         avctx->delay = 0;
676         s->low_delay = 1;
677         break;
678     case CODEC_ID_H261:
679         if (!CONFIG_H261_ENCODER)
680             return -1;
681         if (ff_h261_get_picture_format(s->width, s->height) < 0) {
682             av_log(avctx, AV_LOG_ERROR,
683                    "The specified picture size of %dx%d is not valid for the "
684                    "H.261 codec.\nValid sizes are 176x144, 352x288\n",
685                     s->width, s->height);
686             return -1;
687         }
688         s->out_format = FMT_H261;
689         avctx->delay  = 0;
690         s->low_delay  = 1;
691         break;
692     case CODEC_ID_H263:
693         if (!CONFIG_H263_ENCODER)
694             return -1;
695         if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
696                              s->width, s->height) == 8) {
697             av_log(avctx, AV_LOG_ERROR,
698                    "The specified picture size of %dx%d is not valid for "
699                    "the H.263 codec.\nValid sizes are 128x96, 176x144, "
700                    "352x288, 704x576, and 1408x1152. "
701                    "Try H.263+.\n", s->width, s->height);
702             return -1;
703         }
704         s->out_format = FMT_H263;
705         avctx->delay  = 0;
706         s->low_delay  = 1;
707         break;
708     case CODEC_ID_H263P:
709         s->out_format = FMT_H263;
710         s->h263_plus  = 1;
711         /* Fx */
712         s->h263_aic        = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
713         s->modified_quant  = s->h263_aic;
714         s->loop_filter     = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
715         s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
716
717         /* /Fx */
718         /* These are just to be sure */
719         avctx->delay = 0;
720         s->low_delay = 1;
721         break;
722     case CODEC_ID_FLV1:
723         s->out_format      = FMT_H263;
724         s->h263_flv        = 2; /* format = 1; 11-bit codes */
725         s->unrestricted_mv = 1;
726         s->rtp_mode  = 0; /* don't allow GOB */
727         avctx->delay = 0;
728         s->low_delay = 1;
729         break;
730     case CODEC_ID_RV10:
731         s->out_format = FMT_H263;
732         avctx->delay  = 0;
733         s->low_delay  = 1;
734         break;
735     case CODEC_ID_RV20:
736         s->out_format      = FMT_H263;
737         avctx->delay       = 0;
738         s->low_delay       = 1;
739         s->modified_quant  = 1;
740         s->h263_aic        = 1;
741         s->h263_plus       = 1;
742         s->loop_filter     = 1;
743         s->unrestricted_mv = 0;
744         break;
745     case CODEC_ID_MPEG4:
746         s->out_format      = FMT_H263;
747         s->h263_pred       = 1;
748         s->unrestricted_mv = 1;
749         s->low_delay       = s->max_b_frames ? 0 : 1;
750         avctx->delay       = s->low_delay ? 0 : (s->max_b_frames + 1);
751         break;
752     case CODEC_ID_MSMPEG4V2:
753         s->out_format      = FMT_H263;
754         s->h263_pred       = 1;
755         s->unrestricted_mv = 1;
756         s->msmpeg4_version = 2;
757         avctx->delay       = 0;
758         s->low_delay       = 1;
759         break;
760     case CODEC_ID_MSMPEG4V3:
761         s->out_format        = FMT_H263;
762         s->h263_pred         = 1;
763         s->unrestricted_mv   = 1;
764         s->msmpeg4_version   = 3;
765         s->flipflop_rounding = 1;
766         avctx->delay         = 0;
767         s->low_delay         = 1;
768         break;
769     case CODEC_ID_WMV1:
770         s->out_format        = FMT_H263;
771         s->h263_pred         = 1;
772         s->unrestricted_mv   = 1;
773         s->msmpeg4_version   = 4;
774         s->flipflop_rounding = 1;
775         avctx->delay         = 0;
776         s->low_delay         = 1;
777         break;
778     case CODEC_ID_WMV2:
779         s->out_format        = FMT_H263;
780         s->h263_pred         = 1;
781         s->unrestricted_mv   = 1;
782         s->msmpeg4_version   = 5;
783         s->flipflop_rounding = 1;
784         avctx->delay         = 0;
785         s->low_delay         = 1;
786         break;
787     default:
788         return -1;
789     }
790
791     avctx->has_b_frames = !s->low_delay;
792
793     s->encoding = 1;
794
795     s->progressive_frame    =
796     s->progressive_sequence = !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT |
797                                                 CODEC_FLAG_INTERLACED_ME) ||
798                                 s->alternate_scan);
799
800     /* init */
801     if (ff_MPV_common_init(s) < 0)
802         return -1;
803
804     if (!s->dct_quantize)
805         s->dct_quantize = ff_dct_quantize_c;
806     if (!s->denoise_dct)
807         s->denoise_dct  = denoise_dct_c;
808     s->fast_dct_quantize = s->dct_quantize;
809     if (avctx->trellis)
810         s->dct_quantize  = dct_quantize_trellis_c;
811
812     if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
813         s->chroma_qscale_table = ff_h263_chroma_qscale_table;
814
815     s->quant_precision = 5;
816
817     ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
818     ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
819
820     if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
821         ff_h261_encode_init(s);
822     if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
823         ff_h263_encode_init(s);
824     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
825         ff_msmpeg4_encode_init(s);
826     if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
827         && s->out_format == FMT_MPEG1)
828         ff_mpeg1_encode_init(s);
829
830     /* init q matrix */
831     for (i = 0; i < 64; i++) {
832         int j = s->dsp.idct_permutation[i];
833         if (CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4 &&
834             s->mpeg_quant) {
835             s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
836             s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
837         } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
838             s->intra_matrix[j] =
839             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
840         } else {
841             /* mpeg1/2 */
842             s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
843             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
844         }
845         if (s->avctx->intra_matrix)
846             s->intra_matrix[j] = s->avctx->intra_matrix[i];
847         if (s->avctx->inter_matrix)
848             s->inter_matrix[j] = s->avctx->inter_matrix[i];
849     }
850
851     /* precompute matrix */
852     /* for mjpeg, we do include qscale in the matrix */
853     if (s->out_format != FMT_MJPEG) {
854         ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
855                           s->intra_matrix, s->intra_quant_bias, avctx->qmin,
856                           31, 1);
857         ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
858                           s->inter_matrix, s->inter_quant_bias, avctx->qmin,
859                           31, 0);
860     }
861
862     if (ff_rate_control_init(s) < 0)
863         return -1;
864
865     return 0;
866 }
867
868 av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
869 {
870     MpegEncContext *s = avctx->priv_data;
871
872     ff_rate_control_uninit(s);
873
874     ff_MPV_common_end(s);
875     if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
876         s->out_format == FMT_MJPEG)
877         ff_mjpeg_encode_close(s);
878
879     av_freep(&avctx->extradata);
880
881     return 0;
882 }
883
884 static int get_sae(uint8_t *src, int ref, int stride)
885 {
886     int x,y;
887     int acc = 0;
888
889     for (y = 0; y < 16; y++) {
890         for (x = 0; x < 16; x++) {
891             acc += FFABS(src[x + y * stride] - ref);
892         }
893     }
894
895     return acc;
896 }
897
898 static int get_intra_count(MpegEncContext *s, uint8_t *src,
899                            uint8_t *ref, int stride)
900 {
901     int x, y, w, h;
902     int acc = 0;
903
904     w = s->width  & ~15;
905     h = s->height & ~15;
906
907     for (y = 0; y < h; y += 16) {
908         for (x = 0; x < w; x += 16) {
909             int offset = x + y * stride;
910             int sad  = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
911                                      16);
912             int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
913             int sae  = get_sae(src + offset, mean, stride);
914
915             acc += sae + 500 < sad;
916         }
917     }
918     return acc;
919 }
920
921
922 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg)
923 {
924     AVFrame *pic = NULL;
925     int64_t pts;
926     int i;
927     const int encoding_delay = s->max_b_frames ? s->max_b_frames :
928                                                  (s->low_delay ? 0 : 1);
929     int direct = 1;
930
931     if (pic_arg) {
932         pts = pic_arg->pts;
933         pic_arg->display_picture_number = s->input_picture_number++;
934
935         if (pts != AV_NOPTS_VALUE) {
936             if (s->user_specified_pts != AV_NOPTS_VALUE) {
937                 int64_t time = pts;
938                 int64_t last = s->user_specified_pts;
939
940                 if (time <= last) {
941                     av_log(s->avctx, AV_LOG_ERROR,
942                            "Error, Invalid timestamp=%"PRId64", "
943                            "last=%"PRId64"\n", pts, s->user_specified_pts);
944                     return -1;
945                 }
946
947                 if (!s->low_delay && pic_arg->display_picture_number == 1)
948                     s->dts_delta = time - last;
949             }
950             s->user_specified_pts = pts;
951         } else {
952             if (s->user_specified_pts != AV_NOPTS_VALUE) {
953                 s->user_specified_pts =
954                 pts = s->user_specified_pts + 1;
955                 av_log(s->avctx, AV_LOG_INFO,
956                        "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
957                        pts);
958             } else {
959                 pts = pic_arg->display_picture_number;
960             }
961         }
962     }
963
964   if (pic_arg) {
965     if (encoding_delay && !(s->flags & CODEC_FLAG_INPUT_PRESERVED))
966         direct = 0;
967     if (pic_arg->linesize[0] != s->linesize)
968         direct = 0;
969     if (pic_arg->linesize[1] != s->uvlinesize)
970         direct = 0;
971     if (pic_arg->linesize[2] != s->uvlinesize)
972         direct = 0;
973
974     //av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0],
975     //       pic_arg->linesize[1], s->linesize, s->uvlinesize);
976
977     if (direct) {
978         i = ff_find_unused_picture(s, 1);
979         if (i < 0)
980             return i;
981
982         pic = &s->picture[i].f;
983         pic->reference = 3;
984
985         for (i = 0; i < 4; i++) {
986             pic->data[i]     = pic_arg->data[i];
987             pic->linesize[i] = pic_arg->linesize[i];
988         }
989         if (ff_alloc_picture(s, (Picture *) pic, 1) < 0) {
990             return -1;
991         }
992     } else {
993         i = ff_find_unused_picture(s, 0);
994         if (i < 0)
995             return i;
996
997         pic = &s->picture[i].f;
998         pic->reference = 3;
999
1000         if (ff_alloc_picture(s, (Picture *) pic, 0) < 0) {
1001             return -1;
1002         }
1003
1004         if (pic->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1005             pic->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1006             pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1007             // empty
1008         } else {
1009             int h_chroma_shift, v_chroma_shift;
1010             avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift,
1011                                           &v_chroma_shift);
1012
1013             for (i = 0; i < 3; i++) {
1014                 int src_stride = pic_arg->linesize[i];
1015                 int dst_stride = i ? s->uvlinesize : s->linesize;
1016                 int h_shift = i ? h_chroma_shift : 0;
1017                 int v_shift = i ? v_chroma_shift : 0;
1018                 int w = s->width  >> h_shift;
1019                 int h = s->height >> v_shift;
1020                 uint8_t *src = pic_arg->data[i];
1021                 uint8_t *dst = pic->data[i];
1022
1023                 if(s->codec_id == CODEC_ID_AMV && !(s->avctx->flags & CODEC_FLAG_EMU_EDGE)){
1024                     h= ((s->height+15)/16*16)>>v_shift;
1025                 }
1026
1027                 if (!s->avctx->rc_buffer_size)
1028                     dst += INPLACE_OFFSET;
1029
1030                 if (src_stride == dst_stride)
1031                     memcpy(dst, src, src_stride * h);
1032                 else {
1033                     while (h--) {
1034                         memcpy(dst, src, w);
1035                         dst += dst_stride;
1036                         src += src_stride;
1037                     }
1038                 }
1039             }
1040         }
1041     }
1042     copy_picture_attributes(s, pic, pic_arg);
1043     pic->pts = pts; // we set this here to avoid modifiying pic_arg
1044   }
1045
1046     /* shift buffer entries */
1047     for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1048         s->input_picture[i - 1] = s->input_picture[i];
1049
1050     s->input_picture[encoding_delay] = (Picture*) pic;
1051
1052     return 0;
1053 }
1054
1055 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1056 {
1057     int x, y, plane;
1058     int score = 0;
1059     int64_t score64 = 0;
1060
1061     for (plane = 0; plane < 3; plane++) {
1062         const int stride = p->f.linesize[plane];
1063         const int bw = plane ? 1 : 2;
1064         for (y = 0; y < s->mb_height * bw; y++) {
1065             for (x = 0; x < s->mb_width * bw; x++) {
1066                 int off = p->f.type == FF_BUFFER_TYPE_SHARED ? 0 : 16;
1067                 uint8_t *dptr = p->f.data[plane] + 8 * (x + y * stride) + off;
1068                 uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
1069                 int v   = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1070
1071                 switch (s->avctx->frame_skip_exp) {
1072                 case 0: score    =  FFMAX(score, v);          break;
1073                 case 1: score   += FFABS(v);                  break;
1074                 case 2: score   += v * v;                     break;
1075                 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1076                 case 4: score64 += v * v * (int64_t)(v * v);  break;
1077                 }
1078             }
1079         }
1080     }
1081
1082     if (score)
1083         score64 = score;
1084
1085     if (score64 < s->avctx->frame_skip_threshold)
1086         return 1;
1087     if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1088         return 1;
1089     return 0;
1090 }
1091
1092 static int estimate_best_b_count(MpegEncContext *s)
1093 {
1094     AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
1095     AVCodecContext *c = avcodec_alloc_context3(NULL);
1096     AVFrame input[FF_MAX_B_FRAMES + 2];
1097     const int scale = s->avctx->brd_scale;
1098     int i, j, out_size, p_lambda, b_lambda, lambda2;
1099     int outbuf_size  = s->width * s->height; // FIXME
1100     uint8_t *outbuf  = av_malloc(outbuf_size);
1101     int64_t best_rd  = INT64_MAX;
1102     int best_b_count = -1;
1103
1104     assert(scale >= 0 && scale <= 3);
1105
1106     //emms_c();
1107     //s->next_picture_ptr->quality;
1108     p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1109     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1110     b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1111     if (!b_lambda) // FIXME we should do this somewhere else
1112         b_lambda = p_lambda;
1113     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1114                FF_LAMBDA_SHIFT;
1115
1116     c->width        = s->width  >> scale;
1117     c->height       = s->height >> scale;
1118     c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |
1119                       CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
1120     c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;
1121     c->mb_decision  = s->avctx->mb_decision;
1122     c->me_cmp       = s->avctx->me_cmp;
1123     c->mb_cmp       = s->avctx->mb_cmp;
1124     c->me_sub_cmp   = s->avctx->me_sub_cmp;
1125     c->pix_fmt      = PIX_FMT_YUV420P;
1126     c->time_base    = s->avctx->time_base;
1127     c->max_b_frames = s->max_b_frames;
1128
1129     if (avcodec_open2(c, codec, NULL) < 0)
1130         return -1;
1131
1132     for (i = 0; i < s->max_b_frames + 2; i++) {
1133         int ysize = c->width * c->height;
1134         int csize = (c->width / 2) * (c->height / 2);
1135         Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1136                                                 s->next_picture_ptr;
1137
1138         avcodec_get_frame_defaults(&input[i]);
1139         input[i].data[0]     = av_malloc(ysize + 2 * csize);
1140         input[i].data[1]     = input[i].data[0] + ysize;
1141         input[i].data[2]     = input[i].data[1] + csize;
1142         input[i].linesize[0] = c->width;
1143         input[i].linesize[1] =
1144         input[i].linesize[2] = c->width / 2;
1145
1146         if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1147             pre_input = *pre_input_ptr;
1148
1149             if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {
1150                 pre_input.f.data[0] += INPLACE_OFFSET;
1151                 pre_input.f.data[1] += INPLACE_OFFSET;
1152                 pre_input.f.data[2] += INPLACE_OFFSET;
1153             }
1154
1155             s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
1156                                  pre_input.f.data[0], pre_input.f.linesize[0],
1157                                  c->width,      c->height);
1158             s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
1159                                  pre_input.f.data[1], pre_input.f.linesize[1],
1160                                  c->width >> 1, c->height >> 1);
1161             s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
1162                                  pre_input.f.data[2], pre_input.f.linesize[2],
1163                                  c->width >> 1, c->height >> 1);
1164         }
1165     }
1166
1167     for (j = 0; j < s->max_b_frames + 1; j++) {
1168         int64_t rd = 0;
1169
1170         if (!s->input_picture[j])
1171             break;
1172
1173         c->error[0] = c->error[1] = c->error[2] = 0;
1174
1175         input[0].pict_type = AV_PICTURE_TYPE_I;
1176         input[0].quality   = 1 * FF_QP2LAMBDA;
1177         out_size           = avcodec_encode_video(c, outbuf,
1178                                                   outbuf_size, &input[0]);
1179         //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1180
1181         for (i = 0; i < s->max_b_frames + 1; i++) {
1182             int is_p = i % (j + 1) == j || i == s->max_b_frames;
1183
1184             input[i + 1].pict_type = is_p ?
1185                                      AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1186             input[i + 1].quality   = is_p ? p_lambda : b_lambda;
1187             out_size = avcodec_encode_video(c, outbuf, outbuf_size,
1188                                             &input[i + 1]);
1189             rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1190         }
1191
1192         /* get the delayed frames */
1193         while (out_size) {
1194             out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
1195             rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1196         }
1197
1198         rd += c->error[0] + c->error[1] + c->error[2];
1199
1200         if (rd < best_rd) {
1201             best_rd = rd;
1202             best_b_count = j;
1203         }
1204     }
1205
1206     av_freep(&outbuf);
1207     avcodec_close(c);
1208     av_freep(&c);
1209
1210     for (i = 0; i < s->max_b_frames + 2; i++) {
1211         av_freep(&input[i].data[0]);
1212     }
1213
1214     return best_b_count;
1215 }
1216
1217 static int select_input_picture(MpegEncContext *s)
1218 {
1219     int i;
1220
1221     for (i = 1; i < MAX_PICTURE_COUNT; i++)
1222         s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1223     s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1224
1225     /* set next picture type & ordering */
1226     if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1227         if (/*s->picture_in_gop_number >= s->gop_size ||*/
1228             s->next_picture_ptr == NULL || s->intra_only) {
1229             s->reordered_input_picture[0] = s->input_picture[0];
1230             s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_I;
1231             s->reordered_input_picture[0]->f.coded_picture_number =
1232                 s->coded_picture_number++;
1233         } else {
1234             int b_frames;
1235
1236             if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1237                 if (s->picture_in_gop_number < s->gop_size &&
1238                     skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1239                     // FIXME check that te gop check above is +-1 correct
1240                     //av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n",
1241                     //       s->input_picture[0]->f.data[0],
1242                     //       s->input_picture[0]->pts);
1243
1244                     if (s->input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED) {
1245                         for (i = 0; i < 4; i++)
1246                             s->input_picture[0]->f.data[i] = NULL;
1247                         s->input_picture[0]->f.type = 0;
1248                     } else {
1249                         assert(s->input_picture[0]->f.type == FF_BUFFER_TYPE_USER ||
1250                                s->input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL);
1251
1252                         s->avctx->release_buffer(s->avctx,
1253                                                  &s->input_picture[0]->f);
1254                     }
1255
1256                     emms_c();
1257                     ff_vbv_update(s, 0);
1258
1259                     goto no_output_pic;
1260                 }
1261             }
1262
1263             if (s->flags & CODEC_FLAG_PASS2) {
1264                 for (i = 0; i < s->max_b_frames + 1; i++) {
1265                     int pict_num = s->input_picture[0]->f.display_picture_number + i;
1266
1267                     if (pict_num >= s->rc_context.num_entries)
1268                         break;
1269                     if (!s->input_picture[i]) {
1270                         s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1271                         break;
1272                     }
1273
1274                     s->input_picture[i]->f.pict_type =
1275                         s->rc_context.entry[pict_num].new_pict_type;
1276                 }
1277             }
1278
1279             if (s->avctx->b_frame_strategy == 0) {
1280                 b_frames = s->max_b_frames;
1281                 while (b_frames && !s->input_picture[b_frames])
1282                     b_frames--;
1283             } else if (s->avctx->b_frame_strategy == 1) {
1284                 for (i = 1; i < s->max_b_frames + 1; i++) {
1285                     if (s->input_picture[i] &&
1286                         s->input_picture[i]->b_frame_score == 0) {
1287                         s->input_picture[i]->b_frame_score =
1288                             get_intra_count(s,
1289                                             s->input_picture[i    ]->f.data[0],
1290                                             s->input_picture[i - 1]->f.data[0],
1291                                             s->linesize) + 1;
1292                     }
1293                 }
1294                 for (i = 0; i < s->max_b_frames + 1; i++) {
1295                     if (s->input_picture[i] == NULL ||
1296                         s->input_picture[i]->b_frame_score - 1 >
1297                             s->mb_num / s->avctx->b_sensitivity)
1298                         break;
1299                 }
1300
1301                 b_frames = FFMAX(0, i - 1);
1302
1303                 /* reset scores */
1304                 for (i = 0; i < b_frames + 1; i++) {
1305                     s->input_picture[i]->b_frame_score = 0;
1306                 }
1307             } else if (s->avctx->b_frame_strategy == 2) {
1308                 b_frames = estimate_best_b_count(s);
1309             } else {
1310                 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1311                 b_frames = 0;
1312             }
1313
1314             emms_c();
1315             //static int b_count = 0;
1316             //b_count += b_frames;
1317             //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1318
1319             for (i = b_frames - 1; i >= 0; i--) {
1320                 int type = s->input_picture[i]->f.pict_type;
1321                 if (type && type != AV_PICTURE_TYPE_B)
1322                     b_frames = i;
1323             }
1324             if (s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_B &&
1325                 b_frames == s->max_b_frames) {
1326                 av_log(s->avctx, AV_LOG_ERROR,
1327                        "warning, too many b frames in a row\n");
1328             }
1329
1330             if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1331                 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1332                     s->gop_size > s->picture_in_gop_number) {
1333                     b_frames = s->gop_size - s->picture_in_gop_number - 1;
1334                 } else {
1335                     if (s->flags & CODEC_FLAG_CLOSED_GOP)
1336                         b_frames = 0;
1337                     s->input_picture[b_frames]->f.pict_type = AV_PICTURE_TYPE_I;
1338                 }
1339             }
1340
1341             if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1342                 s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_I)
1343                 b_frames--;
1344
1345             s->reordered_input_picture[0] = s->input_picture[b_frames];
1346             if (s->reordered_input_picture[0]->f.pict_type != AV_PICTURE_TYPE_I)
1347                 s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_P;
1348             s->reordered_input_picture[0]->f.coded_picture_number =
1349                 s->coded_picture_number++;
1350             for (i = 0; i < b_frames; i++) {
1351                 s->reordered_input_picture[i + 1] = s->input_picture[i];
1352                 s->reordered_input_picture[i + 1]->f.pict_type =
1353                     AV_PICTURE_TYPE_B;
1354                 s->reordered_input_picture[i + 1]->f.coded_picture_number =
1355                     s->coded_picture_number++;
1356             }
1357         }
1358     }
1359 no_output_pic:
1360     if (s->reordered_input_picture[0]) {
1361         s->reordered_input_picture[0]->f.reference =
1362            s->reordered_input_picture[0]->f.pict_type !=
1363                AV_PICTURE_TYPE_B ? 3 : 0;
1364
1365         ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1366
1367         if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED ||
1368             s->avctx->rc_buffer_size) {
1369             // input is a shared pix, so we can't modifiy it -> alloc a new
1370             // one & ensure that the shared one is reuseable
1371
1372             Picture *pic;
1373             int i = ff_find_unused_picture(s, 0);
1374             if (i < 0)
1375                 return i;
1376             pic = &s->picture[i];
1377
1378             pic->f.reference = s->reordered_input_picture[0]->f.reference;
1379             if (ff_alloc_picture(s, pic, 0) < 0) {
1380                 return -1;
1381             }
1382
1383             /* mark us unused / free shared pic */
1384             if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL)
1385                 s->avctx->release_buffer(s->avctx,
1386                                          &s->reordered_input_picture[0]->f);
1387             for (i = 0; i < 4; i++)
1388                 s->reordered_input_picture[0]->f.data[i] = NULL;
1389             s->reordered_input_picture[0]->f.type = 0;
1390
1391             copy_picture_attributes(s, &pic->f,
1392                                     &s->reordered_input_picture[0]->f);
1393
1394             s->current_picture_ptr = pic;
1395         } else {
1396             // input is not a shared pix -> reuse buffer for current_pix
1397
1398             assert(s->reordered_input_picture[0]->f.type ==
1399                        FF_BUFFER_TYPE_USER ||
1400                    s->reordered_input_picture[0]->f.type ==
1401                        FF_BUFFER_TYPE_INTERNAL);
1402
1403             s->current_picture_ptr = s->reordered_input_picture[0];
1404             for (i = 0; i < 4; i++) {
1405                 s->new_picture.f.data[i] += INPLACE_OFFSET;
1406             }
1407         }
1408         ff_copy_picture(&s->current_picture, s->current_picture_ptr);
1409
1410         s->picture_number = s->new_picture.f.display_picture_number;
1411         //printf("dpn:%d\n", s->picture_number);
1412     } else {
1413         memset(&s->new_picture, 0, sizeof(Picture));
1414     }
1415     return 0;
1416 }
1417
1418 int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1419                           AVFrame *pic_arg, int *got_packet)
1420 {
1421     MpegEncContext *s = avctx->priv_data;
1422     int i, stuffing_count, ret;
1423     int context_count = s->slice_context_count;
1424
1425     s->picture_in_gop_number++;
1426
1427     if (load_input_picture(s, pic_arg) < 0)
1428         return -1;
1429
1430     if (select_input_picture(s) < 0) {
1431         return -1;
1432     }
1433
1434     /* output? */
1435     if (s->new_picture.f.data[0]) {
1436         if ((ret = ff_alloc_packet2(avctx, pkt, s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000)) < 0)
1437             return ret;
1438         if (s->mb_info) {
1439             s->mb_info_ptr = av_packet_new_side_data(pkt,
1440                                  AV_PKT_DATA_H263_MB_INFO,
1441                                  s->mb_width*s->mb_height*12);
1442             s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1443         }
1444
1445         for (i = 0; i < context_count; i++) {
1446             int start_y = s->thread_context[i]->start_mb_y;
1447             int   end_y = s->thread_context[i]->  end_mb_y;
1448             int h       = s->mb_height;
1449             uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1450             uint8_t *end   = pkt->data + (size_t)(((int64_t) pkt->size) *   end_y / h);
1451
1452             init_put_bits(&s->thread_context[i]->pb, start, end - start);
1453         }
1454
1455         s->pict_type = s->new_picture.f.pict_type;
1456         //emms_c();
1457         //printf("qs:%f %f %d\n", s->new_picture.quality,
1458         //       s->current_picture.quality, s->qscale);
1459         ff_MPV_frame_start(s, avctx);
1460 vbv_retry:
1461         if (encode_picture(s, s->picture_number) < 0)
1462             return -1;
1463
1464         avctx->header_bits = s->header_bits;
1465         avctx->mv_bits     = s->mv_bits;
1466         avctx->misc_bits   = s->misc_bits;
1467         avctx->i_tex_bits  = s->i_tex_bits;
1468         avctx->p_tex_bits  = s->p_tex_bits;
1469         avctx->i_count     = s->i_count;
1470         // FIXME f/b_count in avctx
1471         avctx->p_count     = s->mb_num - s->i_count - s->skip_count;
1472         avctx->skip_count  = s->skip_count;
1473
1474         ff_MPV_frame_end(s);
1475
1476         if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1477             ff_mjpeg_encode_picture_trailer(s);
1478
1479         if (avctx->rc_buffer_size) {
1480             RateControlContext *rcc = &s->rc_context;
1481             int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1482
1483             if (put_bits_count(&s->pb) > max_size &&
1484                 s->lambda < s->avctx->lmax) {
1485                 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1486                                        (s->qscale + 1) / s->qscale);
1487                 if (s->adaptive_quant) {
1488                     int i;
1489                     for (i = 0; i < s->mb_height * s->mb_stride; i++)
1490                         s->lambda_table[i] =
1491                             FFMAX(s->lambda_table[i] + 1,
1492                                   s->lambda_table[i] * (s->qscale + 1) /
1493                                   s->qscale);
1494                 }
1495                 s->mb_skipped = 0;        // done in MPV_frame_start()
1496                 // done in encode_picture() so we must undo it
1497                 if (s->pict_type == AV_PICTURE_TYPE_P) {
1498                     if (s->flipflop_rounding          ||
1499                         s->codec_id == CODEC_ID_H263P ||
1500                         s->codec_id == CODEC_ID_MPEG4)
1501                         s->no_rounding ^= 1;
1502                 }
1503                 if (s->pict_type != AV_PICTURE_TYPE_B) {
1504                     s->time_base       = s->last_time_base;
1505                     s->last_non_b_time = s->time - s->pp_time;
1506                 }
1507                 //av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda);
1508                 for (i = 0; i < context_count; i++) {
1509                     PutBitContext *pb = &s->thread_context[i]->pb;
1510                     init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1511                 }
1512                 goto vbv_retry;
1513             }
1514
1515             assert(s->avctx->rc_max_rate);
1516         }
1517
1518         if (s->flags & CODEC_FLAG_PASS1)
1519             ff_write_pass1_stats(s);
1520
1521         for (i = 0; i < 4; i++) {
1522             s->current_picture_ptr->f.error[i] = s->current_picture.f.error[i];
1523             avctx->error[i] += s->current_picture_ptr->f.error[i];
1524         }
1525
1526         if (s->flags & CODEC_FLAG_PASS1)
1527             assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1528                    avctx->i_tex_bits + avctx->p_tex_bits ==
1529                        put_bits_count(&s->pb));
1530         flush_put_bits(&s->pb);
1531         s->frame_bits  = put_bits_count(&s->pb);
1532
1533         stuffing_count = ff_vbv_update(s, s->frame_bits);
1534         if (stuffing_count) {
1535             if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1536                     stuffing_count + 50) {
1537                 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1538                 return -1;
1539             }
1540
1541             switch (s->codec_id) {
1542             case CODEC_ID_MPEG1VIDEO:
1543             case CODEC_ID_MPEG2VIDEO:
1544                 while (stuffing_count--) {
1545                     put_bits(&s->pb, 8, 0);
1546                 }
1547             break;
1548             case CODEC_ID_MPEG4:
1549                 put_bits(&s->pb, 16, 0);
1550                 put_bits(&s->pb, 16, 0x1C3);
1551                 stuffing_count -= 4;
1552                 while (stuffing_count--) {
1553                     put_bits(&s->pb, 8, 0xFF);
1554                 }
1555             break;
1556             default:
1557                 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1558             }
1559             flush_put_bits(&s->pb);
1560             s->frame_bits  = put_bits_count(&s->pb);
1561         }
1562
1563         /* update mpeg1/2 vbv_delay for CBR */
1564         if (s->avctx->rc_max_rate                          &&
1565             s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1566             s->out_format == FMT_MPEG1                     &&
1567             90000LL * (avctx->rc_buffer_size - 1) <=
1568                 s->avctx->rc_max_rate * 0xFFFFLL) {
1569             int vbv_delay, min_delay;
1570             double inbits  = s->avctx->rc_max_rate *
1571                              av_q2d(s->avctx->time_base);
1572             int    minbits = s->frame_bits - 8 *
1573                              (s->vbv_delay_ptr - s->pb.buf - 1);
1574             double bits    = s->rc_context.buffer_index + minbits - inbits;
1575
1576             if (bits < 0)
1577                 av_log(s->avctx, AV_LOG_ERROR,
1578                        "Internal error, negative bits\n");
1579
1580             assert(s->repeat_first_field == 0);
1581
1582             vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1583             min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1584                         s->avctx->rc_max_rate;
1585
1586             vbv_delay = FFMAX(vbv_delay, min_delay);
1587
1588             assert(vbv_delay < 0xFFFF);
1589
1590             s->vbv_delay_ptr[0] &= 0xF8;
1591             s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1592             s->vbv_delay_ptr[1]  = vbv_delay >> 5;
1593             s->vbv_delay_ptr[2] &= 0x07;
1594             s->vbv_delay_ptr[2] |= vbv_delay << 3;
1595             avctx->vbv_delay     = vbv_delay * 300;
1596         }
1597         s->total_bits     += s->frame_bits;
1598         avctx->frame_bits  = s->frame_bits;
1599
1600         pkt->pts = s->current_picture.f.pts;
1601         if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1602             if (!s->current_picture.f.coded_picture_number)
1603                 pkt->dts = pkt->pts - s->dts_delta;
1604             else
1605                 pkt->dts = s->reordered_pts;
1606             s->reordered_pts = pkt->pts;
1607         } else
1608             pkt->dts = pkt->pts;
1609         if (s->current_picture.f.key_frame)
1610             pkt->flags |= AV_PKT_FLAG_KEY;
1611         if (s->mb_info)
1612             av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1613     } else {
1614         assert((put_bits_ptr(&s->pb) == s->pb.buf));
1615         s->frame_bits = 0;
1616     }
1617     assert((s->frame_bits & 7) == 0);
1618
1619     pkt->size = s->frame_bits / 8;
1620     *got_packet = !!pkt->size;
1621     return 0;
1622 }
1623
1624 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1625                                                 int n, int threshold)
1626 {
1627     static const char tab[64] = {
1628         3, 2, 2, 1, 1, 1, 1, 1,
1629         1, 1, 1, 1, 1, 1, 1, 1,
1630         1, 1, 1, 1, 1, 1, 1, 1,
1631         0, 0, 0, 0, 0, 0, 0, 0,
1632         0, 0, 0, 0, 0, 0, 0, 0,
1633         0, 0, 0, 0, 0, 0, 0, 0,
1634         0, 0, 0, 0, 0, 0, 0, 0,
1635         0, 0, 0, 0, 0, 0, 0, 0
1636     };
1637     int score = 0;
1638     int run = 0;
1639     int i;
1640     DCTELEM *block = s->block[n];
1641     const int last_index = s->block_last_index[n];
1642     int skip_dc;
1643
1644     if (threshold < 0) {
1645         skip_dc = 0;
1646         threshold = -threshold;
1647     } else
1648         skip_dc = 1;
1649
1650     /* Are all we could set to zero already zero? */
1651     if (last_index <= skip_dc - 1)
1652         return;
1653
1654     for (i = 0; i <= last_index; i++) {
1655         const int j = s->intra_scantable.permutated[i];
1656         const int level = FFABS(block[j]);
1657         if (level == 1) {
1658             if (skip_dc && i == 0)
1659                 continue;
1660             score += tab[run];
1661             run = 0;
1662         } else if (level > 1) {
1663             return;
1664         } else {
1665             run++;
1666         }
1667     }
1668     if (score >= threshold)
1669         return;
1670     for (i = skip_dc; i <= last_index; i++) {
1671         const int j = s->intra_scantable.permutated[i];
1672         block[j] = 0;
1673     }
1674     if (block[0])
1675         s->block_last_index[n] = 0;
1676     else
1677         s->block_last_index[n] = -1;
1678 }
1679
1680 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block,
1681                                int last_index)
1682 {
1683     int i;
1684     const int maxlevel = s->max_qcoeff;
1685     const int minlevel = s->min_qcoeff;
1686     int overflow = 0;
1687
1688     if (s->mb_intra) {
1689         i = 1; // skip clipping of intra dc
1690     } else
1691         i = 0;
1692
1693     for (; i <= last_index; i++) {
1694         const int j = s->intra_scantable.permutated[i];
1695         int level = block[j];
1696
1697         if (level > maxlevel) {
1698             level = maxlevel;
1699             overflow++;
1700         } else if (level < minlevel) {
1701             level = minlevel;
1702             overflow++;
1703         }
1704
1705         block[j] = level;
1706     }
1707
1708     if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1709         av_log(s->avctx, AV_LOG_INFO,
1710                "warning, clipping %d dct coefficients to %d..%d\n",
1711                overflow, minlevel, maxlevel);
1712 }
1713
1714 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1715 {
1716     int x, y;
1717     // FIXME optimize
1718     for (y = 0; y < 8; y++) {
1719         for (x = 0; x < 8; x++) {
1720             int x2, y2;
1721             int sum = 0;
1722             int sqr = 0;
1723             int count = 0;
1724
1725             for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1726                 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1727                     int v = ptr[x2 + y2 * stride];
1728                     sum += v;
1729                     sqr += v * v;
1730                     count++;
1731                 }
1732             }
1733             weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
1734         }
1735     }
1736 }
1737
1738 static av_always_inline void encode_mb_internal(MpegEncContext *s,
1739                                                 int motion_x, int motion_y,
1740                                                 int mb_block_height,
1741                                                 int mb_block_count)
1742 {
1743     int16_t weight[8][64];
1744     DCTELEM orig[8][64];
1745     const int mb_x = s->mb_x;
1746     const int mb_y = s->mb_y;
1747     int i;
1748     int skip_dct[8];
1749     int dct_offset = s->linesize * 8; // default for progressive frames
1750     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1751     int wrap_y, wrap_c;
1752
1753     for (i = 0; i < mb_block_count; i++)
1754         skip_dct[i] = s->skipdct;
1755
1756     if (s->adaptive_quant) {
1757         const int last_qp = s->qscale;
1758         const int mb_xy = mb_x + mb_y * s->mb_stride;
1759
1760         s->lambda = s->lambda_table[mb_xy];
1761         update_qscale(s);
1762
1763         if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1764             s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];
1765             s->dquant = s->qscale - last_qp;
1766
1767             if (s->out_format == FMT_H263) {
1768                 s->dquant = av_clip(s->dquant, -2, 2);
1769
1770                 if (s->codec_id == CODEC_ID_MPEG4) {
1771                     if (!s->mb_intra) {
1772                         if (s->pict_type == AV_PICTURE_TYPE_B) {
1773                             if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1774                                 s->dquant = 0;
1775                         }
1776                         if (s->mv_type == MV_TYPE_8X8)
1777                             s->dquant = 0;
1778                     }
1779                 }
1780             }
1781         }
1782         ff_set_qscale(s, last_qp + s->dquant);
1783     } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1784         ff_set_qscale(s, s->qscale + s->dquant);
1785
1786     wrap_y = s->linesize;
1787     wrap_c = s->uvlinesize;
1788     ptr_y  = s->new_picture.f.data[0] +
1789              (mb_y * 16 * wrap_y)              + mb_x * 16;
1790     ptr_cb = s->new_picture.f.data[1] +
1791              (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1792     ptr_cr = s->new_picture.f.data[2] +
1793              (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1794
1795     if((mb_x*16+16 > s->width || mb_y*16+16 > s->height) && s->codec_id != CODEC_ID_AMV){
1796         uint8_t *ebuf = s->edge_emu_buffer + 32;
1797         s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,
1798                                 mb_y * 16, s->width, s->height);
1799         ptr_y = ebuf;
1800         s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,
1801                                 mb_block_height, mb_x * 8, mb_y * 8,
1802                                 s->width >> 1, s->height >> 1);
1803         ptr_cb = ebuf + 18 * wrap_y;
1804         s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,
1805                                 mb_block_height, mb_x * 8, mb_y * 8,
1806                                 s->width >> 1, s->height >> 1);
1807         ptr_cr = ebuf + 18 * wrap_y + 8;
1808     }
1809
1810     if (s->mb_intra) {
1811         if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1812             int progressive_score, interlaced_score;
1813
1814             s->interlaced_dct = 0;
1815             progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,
1816                                                     NULL, wrap_y, 8) +
1817                                 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1818                                                     NULL, wrap_y, 8) - 400;
1819
1820             if (progressive_score > 0) {
1821                 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,
1822                                                        NULL, wrap_y * 2, 8) +
1823                                    s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,
1824                                                        NULL, wrap_y * 2, 8);
1825                 if (progressive_score > interlaced_score) {
1826                     s->interlaced_dct = 1;
1827
1828                     dct_offset = wrap_y;
1829                     wrap_y <<= 1;
1830                     if (s->chroma_format == CHROMA_422)
1831                         wrap_c <<= 1;
1832                 }
1833             }
1834         }
1835
1836         s->dsp.get_pixels(s->block[0], ptr_y                  , wrap_y);
1837         s->dsp.get_pixels(s->block[1], ptr_y              + 8 , wrap_y);
1838         s->dsp.get_pixels(s->block[2], ptr_y + dct_offset     , wrap_y);
1839         s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);
1840
1841         if (s->flags & CODEC_FLAG_GRAY) {
1842             skip_dct[4] = 1;
1843             skip_dct[5] = 1;
1844         } else {
1845             s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1846             s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1847             if (!s->chroma_y_shift) { /* 422 */
1848                 s->dsp.get_pixels(s->block[6],
1849                                   ptr_cb + (dct_offset >> 1), wrap_c);
1850                 s->dsp.get_pixels(s->block[7],
1851                                   ptr_cr + (dct_offset >> 1), wrap_c);
1852             }
1853         }
1854     } else {
1855         op_pixels_func (*op_pix)[4];
1856         qpel_mc_func (*op_qpix)[16];
1857         uint8_t *dest_y, *dest_cb, *dest_cr;
1858
1859         dest_y  = s->dest[0];
1860         dest_cb = s->dest[1];
1861         dest_cr = s->dest[2];
1862
1863         if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1864             op_pix  = s->dsp.put_pixels_tab;
1865             op_qpix = s->dsp.put_qpel_pixels_tab;
1866         } else {
1867             op_pix  = s->dsp.put_no_rnd_pixels_tab;
1868             op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;
1869         }
1870
1871         if (s->mv_dir & MV_DIR_FORWARD) {
1872             MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data,
1873                        op_pix, op_qpix);
1874             op_pix  = s->dsp.avg_pixels_tab;
1875             op_qpix = s->dsp.avg_qpel_pixels_tab;
1876         }
1877         if (s->mv_dir & MV_DIR_BACKWARD) {
1878             MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data,
1879                        op_pix, op_qpix);
1880         }
1881
1882         if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1883             int progressive_score, interlaced_score;
1884
1885             s->interlaced_dct = 0;
1886             progressive_score = s->dsp.ildct_cmp[0](s, dest_y,
1887                                                     ptr_y,              wrap_y,
1888                                                     8) +
1889                                 s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,
1890                                                     ptr_y + wrap_y * 8, wrap_y,
1891                                                     8) - 400;
1892
1893             if (s->avctx->ildct_cmp == FF_CMP_VSSE)
1894                 progressive_score -= 400;
1895
1896             if (progressive_score > 0) {
1897                 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,
1898                                                        ptr_y,
1899                                                        wrap_y * 2, 8) +
1900                                    s->dsp.ildct_cmp[0](s, dest_y + wrap_y,
1901                                                        ptr_y + wrap_y,
1902                                                        wrap_y * 2, 8);
1903
1904                 if (progressive_score > interlaced_score) {
1905                     s->interlaced_dct = 1;
1906
1907                     dct_offset = wrap_y;
1908                     wrap_y <<= 1;
1909                     if (s->chroma_format == CHROMA_422)
1910                         wrap_c <<= 1;
1911                 }
1912             }
1913         }
1914
1915         s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
1916         s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
1917         s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,
1918                            dest_y + dct_offset, wrap_y);
1919         s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
1920                            dest_y + dct_offset + 8, wrap_y);
1921
1922         if (s->flags & CODEC_FLAG_GRAY) {
1923             skip_dct[4] = 1;
1924             skip_dct[5] = 1;
1925         } else {
1926             s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1927             s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1928             if (!s->chroma_y_shift) { /* 422 */
1929                 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
1930                                    dest_cb + (dct_offset >> 1), wrap_c);
1931                 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
1932                                    dest_cr + (dct_offset >> 1), wrap_c);
1933             }
1934         }
1935         /* pre quantization */
1936         if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
1937                 2 * s->qscale * s->qscale) {
1938             // FIXME optimize
1939             if (s->dsp.sad[1](NULL, ptr_y , dest_y,
1940                               wrap_y, 8) < 20 * s->qscale)
1941                 skip_dct[0] = 1;
1942             if (s->dsp.sad[1](NULL, ptr_y + 8,
1943                               dest_y + 8, wrap_y, 8) < 20 * s->qscale)
1944                 skip_dct[1] = 1;
1945             if (s->dsp.sad[1](NULL, ptr_y + dct_offset,
1946                               dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)
1947                 skip_dct[2] = 1;
1948             if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,
1949                               dest_y + dct_offset + 8,
1950                               wrap_y, 8) < 20 * s->qscale)
1951                 skip_dct[3] = 1;
1952             if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,
1953                               wrap_c, 8) < 20 * s->qscale)
1954                 skip_dct[4] = 1;
1955             if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,
1956                               wrap_c, 8) < 20 * s->qscale)
1957                 skip_dct[5] = 1;
1958             if (!s->chroma_y_shift) { /* 422 */
1959                 if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),
1960                                   dest_cb + (dct_offset >> 1),
1961                                   wrap_c, 8) < 20 * s->qscale)
1962                     skip_dct[6] = 1;
1963                 if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),
1964                                   dest_cr + (dct_offset >> 1),
1965                                   wrap_c, 8) < 20 * s->qscale)
1966                     skip_dct[7] = 1;
1967             }
1968         }
1969     }
1970
1971     if (s->quantizer_noise_shaping) {
1972         if (!skip_dct[0])
1973             get_visual_weight(weight[0], ptr_y                 , wrap_y);
1974         if (!skip_dct[1])
1975             get_visual_weight(weight[1], ptr_y              + 8, wrap_y);
1976         if (!skip_dct[2])
1977             get_visual_weight(weight[2], ptr_y + dct_offset    , wrap_y);
1978         if (!skip_dct[3])
1979             get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
1980         if (!skip_dct[4])
1981             get_visual_weight(weight[4], ptr_cb                , wrap_c);
1982         if (!skip_dct[5])
1983             get_visual_weight(weight[5], ptr_cr                , wrap_c);
1984         if (!s->chroma_y_shift) { /* 422 */
1985             if (!skip_dct[6])
1986                 get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
1987                                   wrap_c);
1988             if (!skip_dct[7])
1989                 get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
1990                                   wrap_c);
1991         }
1992         memcpy(orig[0], s->block[0], sizeof(DCTELEM) * 64 * mb_block_count);
1993     }
1994
1995     /* DCT & quantize */
1996     assert(s->out_format != FMT_MJPEG || s->qscale == 8);
1997     {
1998         for (i = 0; i < mb_block_count; i++) {
1999             if (!skip_dct[i]) {
2000                 int overflow;
2001                 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2002                 // FIXME we could decide to change to quantizer instead of
2003                 // clipping
2004                 // JS: I don't think that would be a good idea it could lower
2005                 //     quality instead of improve it. Just INTRADC clipping
2006                 //     deserves changes in quantizer
2007                 if (overflow)
2008                     clip_coeffs(s, s->block[i], s->block_last_index[i]);
2009             } else
2010                 s->block_last_index[i] = -1;
2011         }
2012         if (s->quantizer_noise_shaping) {
2013             for (i = 0; i < mb_block_count; i++) {
2014                 if (!skip_dct[i]) {
2015                     s->block_last_index[i] =
2016                         dct_quantize_refine(s, s->block[i], weight[i],
2017                                             orig[i], i, s->qscale);
2018                 }
2019             }
2020         }
2021
2022         if (s->luma_elim_threshold && !s->mb_intra)
2023             for (i = 0; i < 4; i++)
2024                 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2025         if (s->chroma_elim_threshold && !s->mb_intra)
2026             for (i = 4; i < mb_block_count; i++)
2027                 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2028
2029         if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2030             for (i = 0; i < mb_block_count; i++) {
2031                 if (s->block_last_index[i] == -1)
2032                     s->coded_score[i] = INT_MAX / 256;
2033             }
2034         }
2035     }
2036
2037     if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2038         s->block_last_index[4] =
2039         s->block_last_index[5] = 0;
2040         s->block[4][0] =
2041         s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2042     }
2043
2044     // non c quantize code returns incorrect block_last_index FIXME
2045     if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2046         for (i = 0; i < mb_block_count; i++) {
2047             int j;
2048             if (s->block_last_index[i] > 0) {
2049                 for (j = 63; j > 0; j--) {
2050                     if (s->block[i][s->intra_scantable.permutated[j]])
2051                         break;
2052                 }
2053                 s->block_last_index[i] = j;
2054             }
2055         }
2056     }
2057
2058     /* huffman encode */
2059     switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2060     case CODEC_ID_MPEG1VIDEO:
2061     case CODEC_ID_MPEG2VIDEO:
2062         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2063             ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2064         break;
2065     case CODEC_ID_MPEG4:
2066         if (CONFIG_MPEG4_ENCODER)
2067             ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2068         break;
2069     case CODEC_ID_MSMPEG4V2:
2070     case CODEC_ID_MSMPEG4V3:
2071     case CODEC_ID_WMV1:
2072         if (CONFIG_MSMPEG4_ENCODER)
2073             ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2074         break;
2075     case CODEC_ID_WMV2:
2076         if (CONFIG_WMV2_ENCODER)
2077             ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2078         break;
2079     case CODEC_ID_H261:
2080         if (CONFIG_H261_ENCODER)
2081             ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2082         break;
2083     case CODEC_ID_H263:
2084     case CODEC_ID_H263P:
2085     case CODEC_ID_FLV1:
2086     case CODEC_ID_RV10:
2087     case CODEC_ID_RV20:
2088         if (CONFIG_H263_ENCODER)
2089             ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2090         break;
2091     case CODEC_ID_MJPEG:
2092     case CODEC_ID_AMV:
2093         if (CONFIG_MJPEG_ENCODER)
2094             ff_mjpeg_encode_mb(s, s->block);
2095         break;
2096     default:
2097         assert(0);
2098     }
2099 }
2100
2101 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2102 {
2103     if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 6);
2104     else                                encode_mb_internal(s, motion_x, motion_y, 16, 8);
2105 }
2106
2107 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2108     int i;
2109
2110     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2111
2112     /* mpeg1 */
2113     d->mb_skip_run= s->mb_skip_run;
2114     for(i=0; i<3; i++)
2115         d->last_dc[i] = s->last_dc[i];
2116
2117     /* statistics */
2118     d->mv_bits= s->mv_bits;
2119     d->i_tex_bits= s->i_tex_bits;
2120     d->p_tex_bits= s->p_tex_bits;
2121     d->i_count= s->i_count;
2122     d->f_count= s->f_count;
2123     d->b_count= s->b_count;
2124     d->skip_count= s->skip_count;
2125     d->misc_bits= s->misc_bits;
2126     d->last_bits= 0;
2127
2128     d->mb_skipped= 0;
2129     d->qscale= s->qscale;
2130     d->dquant= s->dquant;
2131
2132     d->esc3_level_length= s->esc3_level_length;
2133 }
2134
2135 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2136     int i;
2137
2138     memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2139     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2140
2141     /* mpeg1 */
2142     d->mb_skip_run= s->mb_skip_run;
2143     for(i=0; i<3; i++)
2144         d->last_dc[i] = s->last_dc[i];
2145
2146     /* statistics */
2147     d->mv_bits= s->mv_bits;
2148     d->i_tex_bits= s->i_tex_bits;
2149     d->p_tex_bits= s->p_tex_bits;
2150     d->i_count= s->i_count;
2151     d->f_count= s->f_count;
2152     d->b_count= s->b_count;
2153     d->skip_count= s->skip_count;
2154     d->misc_bits= s->misc_bits;
2155
2156     d->mb_intra= s->mb_intra;
2157     d->mb_skipped= s->mb_skipped;
2158     d->mv_type= s->mv_type;
2159     d->mv_dir= s->mv_dir;
2160     d->pb= s->pb;
2161     if(s->data_partitioning){
2162         d->pb2= s->pb2;
2163         d->tex_pb= s->tex_pb;
2164     }
2165     d->block= s->block;
2166     for(i=0; i<8; i++)
2167         d->block_last_index[i]= s->block_last_index[i];
2168     d->interlaced_dct= s->interlaced_dct;
2169     d->qscale= s->qscale;
2170
2171     d->esc3_level_length= s->esc3_level_length;
2172 }
2173
2174 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2175                            PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2176                            int *dmin, int *next_block, int motion_x, int motion_y)
2177 {
2178     int score;
2179     uint8_t *dest_backup[3];
2180
2181     copy_context_before_encode(s, backup, type);
2182
2183     s->block= s->blocks[*next_block];
2184     s->pb= pb[*next_block];
2185     if(s->data_partitioning){
2186         s->pb2   = pb2   [*next_block];
2187         s->tex_pb= tex_pb[*next_block];
2188     }
2189
2190     if(*next_block){
2191         memcpy(dest_backup, s->dest, sizeof(s->dest));
2192         s->dest[0] = s->rd_scratchpad;
2193         s->dest[1] = s->rd_scratchpad + 16*s->linesize;
2194         s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
2195         assert(s->linesize >= 32); //FIXME
2196     }
2197
2198     encode_mb(s, motion_x, motion_y);
2199
2200     score= put_bits_count(&s->pb);
2201     if(s->data_partitioning){
2202         score+= put_bits_count(&s->pb2);
2203         score+= put_bits_count(&s->tex_pb);
2204     }
2205
2206     if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2207         ff_MPV_decode_mb(s, s->block);
2208
2209         score *= s->lambda2;
2210         score += sse_mb(s) << FF_LAMBDA_SHIFT;
2211     }
2212
2213     if(*next_block){
2214         memcpy(s->dest, dest_backup, sizeof(s->dest));
2215     }
2216
2217     if(score<*dmin){
2218         *dmin= score;
2219         *next_block^=1;
2220
2221         copy_context_after_encode(best, s, type);
2222     }
2223 }
2224
2225 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2226     uint32_t *sq = ff_squareTbl + 256;
2227     int acc=0;
2228     int x,y;
2229
2230     if(w==16 && h==16)
2231         return s->dsp.sse[0](NULL, src1, src2, stride, 16);
2232     else if(w==8 && h==8)
2233         return s->dsp.sse[1](NULL, src1, src2, stride, 8);
2234
2235     for(y=0; y<h; y++){
2236         for(x=0; x<w; x++){
2237             acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2238         }
2239     }
2240
2241     assert(acc>=0);
2242
2243     return acc;
2244 }
2245
2246 static int sse_mb(MpegEncContext *s){
2247     int w= 16;
2248     int h= 16;
2249
2250     if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2251     if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2252
2253     if(w==16 && h==16)
2254       if(s->avctx->mb_cmp == FF_CMP_NSSE){
2255         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)
2256                +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)
2257                +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);
2258       }else{
2259         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)
2260                +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)
2261                +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);
2262       }
2263     else
2264         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)
2265                +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)
2266                +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);
2267 }
2268
2269 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2270     MpegEncContext *s= *(void**)arg;
2271
2272
2273     s->me.pre_pass=1;
2274     s->me.dia_size= s->avctx->pre_dia_size;
2275     s->first_slice_line=1;
2276     for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2277         for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2278             ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2279         }
2280         s->first_slice_line=0;
2281     }
2282
2283     s->me.pre_pass=0;
2284
2285     return 0;
2286 }
2287
2288 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2289     MpegEncContext *s= *(void**)arg;
2290
2291     ff_check_alignment();
2292
2293     s->me.dia_size= s->avctx->dia_size;
2294     s->first_slice_line=1;
2295     for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2296         s->mb_x=0; //for block init below
2297         ff_init_block_index(s);
2298         for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2299             s->block_index[0]+=2;
2300             s->block_index[1]+=2;
2301             s->block_index[2]+=2;
2302             s->block_index[3]+=2;
2303
2304             /* compute motion vector & mb_type and store in context */
2305             if(s->pict_type==AV_PICTURE_TYPE_B)
2306                 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2307             else
2308                 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2309         }
2310         s->first_slice_line=0;
2311     }
2312     return 0;
2313 }
2314
2315 static int mb_var_thread(AVCodecContext *c, void *arg){
2316     MpegEncContext *s= *(void**)arg;
2317     int mb_x, mb_y;
2318
2319     ff_check_alignment();
2320
2321     for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2322         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2323             int xx = mb_x * 16;
2324             int yy = mb_y * 16;
2325             uint8_t *pix = s->new_picture.f.data[0] + (yy * s->linesize) + xx;
2326             int varc;
2327             int sum = s->dsp.pix_sum(pix, s->linesize);
2328
2329             varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
2330
2331             s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2332             s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2333             s->me.mb_var_sum_temp    += varc;
2334         }
2335     }
2336     return 0;
2337 }
2338
2339 static void write_slice_end(MpegEncContext *s){
2340     if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
2341         if(s->partitioned_frame){
2342             ff_mpeg4_merge_partitions(s);
2343         }
2344
2345         ff_mpeg4_stuffing(&s->pb);
2346     }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2347         ff_mjpeg_encode_stuffing(&s->pb);
2348     }
2349
2350     avpriv_align_put_bits(&s->pb);
2351     flush_put_bits(&s->pb);
2352
2353     if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2354         s->misc_bits+= get_bits_diff(s);
2355 }
2356
2357 static void write_mb_info(MpegEncContext *s)
2358 {
2359     uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2360     int offset = put_bits_count(&s->pb);
2361     int mba  = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2362     int gobn = s->mb_y / s->gob_index;
2363     int pred_x, pred_y;
2364     if (CONFIG_H263_ENCODER)
2365         ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2366     bytestream_put_le32(&ptr, offset);
2367     bytestream_put_byte(&ptr, s->qscale);
2368     bytestream_put_byte(&ptr, gobn);
2369     bytestream_put_le16(&ptr, mba);
2370     bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2371     bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2372     /* 4MV not implemented */
2373     bytestream_put_byte(&ptr, 0); /* hmv2 */
2374     bytestream_put_byte(&ptr, 0); /* vmv2 */
2375 }
2376
2377 static void update_mb_info(MpegEncContext *s, int startcode)
2378 {
2379     if (!s->mb_info)
2380         return;
2381     if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2382         s->mb_info_size += 12;
2383         s->prev_mb_info = s->last_mb_info;
2384     }
2385     if (startcode) {
2386         s->prev_mb_info = put_bits_count(&s->pb)/8;
2387         /* This might have incremented mb_info_size above, and we return without
2388          * actually writing any info into that slot yet. But in that case,
2389          * this will be called again at the start of the after writing the
2390          * start code, actually writing the mb info. */
2391         return;
2392     }
2393
2394     s->last_mb_info = put_bits_count(&s->pb)/8;
2395     if (!s->mb_info_size)
2396         s->mb_info_size += 12;
2397     write_mb_info(s);
2398 }
2399
2400 static int encode_thread(AVCodecContext *c, void *arg){
2401     MpegEncContext *s= *(void**)arg;
2402     int mb_x, mb_y, pdif = 0;
2403     int chr_h= 16>>s->chroma_y_shift;
2404     int i, j;
2405     MpegEncContext best_s, backup_s;
2406     uint8_t bit_buf[2][MAX_MB_BYTES];
2407     uint8_t bit_buf2[2][MAX_MB_BYTES];
2408     uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2409     PutBitContext pb[2], pb2[2], tex_pb[2];
2410 //printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
2411
2412     ff_check_alignment();
2413
2414     for(i=0; i<2; i++){
2415         init_put_bits(&pb    [i], bit_buf    [i], MAX_MB_BYTES);
2416         init_put_bits(&pb2   [i], bit_buf2   [i], MAX_MB_BYTES);
2417         init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2418     }
2419
2420     s->last_bits= put_bits_count(&s->pb);
2421     s->mv_bits=0;
2422     s->misc_bits=0;
2423     s->i_tex_bits=0;
2424     s->p_tex_bits=0;
2425     s->i_count=0;
2426     s->f_count=0;
2427     s->b_count=0;
2428     s->skip_count=0;
2429
2430     for(i=0; i<3; i++){
2431         /* init last dc values */
2432         /* note: quant matrix value (8) is implied here */
2433         s->last_dc[i] = 128 << s->intra_dc_precision;
2434
2435         s->current_picture.f.error[i] = 0;
2436     }
2437     if(s->codec_id==CODEC_ID_AMV){
2438         s->last_dc[0] = 128*8/13;
2439         s->last_dc[1] = 128*8/14;
2440         s->last_dc[2] = 128*8/14;
2441     }
2442     s->mb_skip_run = 0;
2443     memset(s->last_mv, 0, sizeof(s->last_mv));
2444
2445     s->last_mv_dir = 0;
2446
2447     switch(s->codec_id){
2448     case CODEC_ID_H263:
2449     case CODEC_ID_H263P:
2450     case CODEC_ID_FLV1:
2451         if (CONFIG_H263_ENCODER)
2452             s->gob_index = ff_h263_get_gob_height(s);
2453         break;
2454     case CODEC_ID_MPEG4:
2455         if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2456             ff_mpeg4_init_partitions(s);
2457         break;
2458     }
2459
2460     s->resync_mb_x=0;
2461     s->resync_mb_y=0;
2462     s->first_slice_line = 1;
2463     s->ptr_lastgob = s->pb.buf;
2464     for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2465 //    printf("row %d at %X\n", s->mb_y, (int)s);
2466         s->mb_x=0;
2467         s->mb_y= mb_y;
2468
2469         ff_set_qscale(s, s->qscale);
2470         ff_init_block_index(s);
2471
2472         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2473             int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2474             int mb_type= s->mb_type[xy];
2475 //            int d;
2476             int dmin= INT_MAX;
2477             int dir;
2478
2479             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2480                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2481                 return -1;
2482             }
2483             if(s->data_partitioning){
2484                 if(   s->pb2   .buf_end - s->pb2   .buf - (put_bits_count(&s->    pb2)>>3) < MAX_MB_BYTES
2485                    || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2486                     av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
2487                     return -1;
2488                 }
2489             }
2490
2491             s->mb_x = mb_x;
2492             s->mb_y = mb_y;  // moved into loop, can get changed by H.261
2493             ff_update_block_index(s);
2494
2495             if(CONFIG_H261_ENCODER && s->codec_id == CODEC_ID_H261){
2496                 ff_h261_reorder_mb_index(s);
2497                 xy= s->mb_y*s->mb_stride + s->mb_x;
2498                 mb_type= s->mb_type[xy];
2499             }
2500
2501             /* write gob / video packet header  */
2502             if(s->rtp_mode){
2503                 int current_packet_size, is_gob_start;
2504
2505                 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2506
2507                 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2508
2509                 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2510
2511                 switch(s->codec_id){
2512                 case CODEC_ID_H263:
2513                 case CODEC_ID_H263P:
2514                     if(!s->h263_slice_structured)
2515                         if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2516                     break;
2517                 case CODEC_ID_MPEG2VIDEO:
2518                     if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2519                 case CODEC_ID_MPEG1VIDEO:
2520                     if(s->mb_skip_run) is_gob_start=0;
2521                     break;
2522                 }
2523
2524                 if(is_gob_start){
2525                     if(s->start_mb_y != mb_y || mb_x!=0){
2526                         write_slice_end(s);
2527
2528                         if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
2529                             ff_mpeg4_init_partitions(s);
2530                         }
2531                     }
2532
2533                     assert((put_bits_count(&s->pb)&7) == 0);
2534                     current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2535
2536                     if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2537                         int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2538                         int d= 100 / s->avctx->error_rate;
2539                         if(r % d == 0){
2540                             current_packet_size=0;
2541                             s->pb.buf_ptr= s->ptr_lastgob;
2542                             assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2543                         }
2544                     }
2545
2546                     if (s->avctx->rtp_callback){
2547                         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2548                         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2549                     }
2550                     update_mb_info(s, 1);
2551
2552                     switch(s->codec_id){
2553                     case CODEC_ID_MPEG4:
2554                         if (CONFIG_MPEG4_ENCODER) {
2555                             ff_mpeg4_encode_video_packet_header(s);
2556                             ff_mpeg4_clean_buffers(s);
2557                         }
2558                     break;
2559                     case CODEC_ID_MPEG1VIDEO:
2560                     case CODEC_ID_MPEG2VIDEO:
2561                         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2562                             ff_mpeg1_encode_slice_header(s);
2563                             ff_mpeg1_clean_buffers(s);
2564                         }
2565                     break;
2566                     case CODEC_ID_H263:
2567                     case CODEC_ID_H263P:
2568                         if (CONFIG_H263_ENCODER)
2569                             ff_h263_encode_gob_header(s, mb_y);
2570                     break;
2571                     }
2572
2573                     if(s->flags&CODEC_FLAG_PASS1){
2574                         int bits= put_bits_count(&s->pb);
2575                         s->misc_bits+= bits - s->last_bits;
2576                         s->last_bits= bits;
2577                     }
2578
2579                     s->ptr_lastgob += current_packet_size;
2580                     s->first_slice_line=1;
2581                     s->resync_mb_x=mb_x;
2582                     s->resync_mb_y=mb_y;
2583                 }
2584             }
2585
2586             if(  (s->resync_mb_x   == s->mb_x)
2587                && s->resync_mb_y+1 == s->mb_y){
2588                 s->first_slice_line=0;
2589             }
2590
2591             s->mb_skipped=0;
2592             s->dquant=0; //only for QP_RD
2593
2594             update_mb_info(s, 0);
2595
2596             if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) { // more than 1 MB type possible or FF_MPV_FLAG_QP_RD
2597                 int next_block=0;
2598                 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2599
2600                 copy_context_before_encode(&backup_s, s, -1);
2601                 backup_s.pb= s->pb;
2602                 best_s.data_partitioning= s->data_partitioning;
2603                 best_s.partitioned_frame= s->partitioned_frame;
2604                 if(s->data_partitioning){
2605                     backup_s.pb2= s->pb2;
2606                     backup_s.tex_pb= s->tex_pb;
2607                 }
2608
2609                 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2610                     s->mv_dir = MV_DIR_FORWARD;
2611                     s->mv_type = MV_TYPE_16X16;
2612                     s->mb_intra= 0;
2613                     s->mv[0][0][0] = s->p_mv_table[xy][0];
2614                     s->mv[0][0][1] = s->p_mv_table[xy][1];
2615                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2616                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2617                 }
2618                 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2619                     s->mv_dir = MV_DIR_FORWARD;
2620                     s->mv_type = MV_TYPE_FIELD;
2621                     s->mb_intra= 0;
2622                     for(i=0; i<2; i++){
2623                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2624                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2625                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2626                     }
2627                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2628                                  &dmin, &next_block, 0, 0);
2629                 }
2630                 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2631                     s->mv_dir = MV_DIR_FORWARD;
2632                     s->mv_type = MV_TYPE_16X16;
2633                     s->mb_intra= 0;
2634                     s->mv[0][0][0] = 0;
2635                     s->mv[0][0][1] = 0;
2636                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2637                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2638                 }
2639                 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2640                     s->mv_dir = MV_DIR_FORWARD;
2641                     s->mv_type = MV_TYPE_8X8;
2642                     s->mb_intra= 0;
2643                     for(i=0; i<4; i++){
2644                         s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
2645                         s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
2646                     }
2647                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2648                                  &dmin, &next_block, 0, 0);
2649                 }
2650                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2651                     s->mv_dir = MV_DIR_FORWARD;
2652                     s->mv_type = MV_TYPE_16X16;
2653                     s->mb_intra= 0;
2654                     s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2655                     s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2656                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2657                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2658                 }
2659                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2660                     s->mv_dir = MV_DIR_BACKWARD;
2661                     s->mv_type = MV_TYPE_16X16;
2662                     s->mb_intra= 0;
2663                     s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2664                     s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2665                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2666                                  &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2667                 }
2668                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2669                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2670                     s->mv_type = MV_TYPE_16X16;
2671                     s->mb_intra= 0;
2672                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2673                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2674                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2675                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2676                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2677                                  &dmin, &next_block, 0, 0);
2678                 }
2679                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2680                     s->mv_dir = MV_DIR_FORWARD;
2681                     s->mv_type = MV_TYPE_FIELD;
2682                     s->mb_intra= 0;
2683                     for(i=0; i<2; i++){
2684                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2685                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2686                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2687                     }
2688                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2689                                  &dmin, &next_block, 0, 0);
2690                 }
2691                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2692                     s->mv_dir = MV_DIR_BACKWARD;
2693                     s->mv_type = MV_TYPE_FIELD;
2694                     s->mb_intra= 0;
2695                     for(i=0; i<2; i++){
2696                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2697                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2698                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2699                     }
2700                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2701                                  &dmin, &next_block, 0, 0);
2702                 }
2703                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2704                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2705                     s->mv_type = MV_TYPE_FIELD;
2706                     s->mb_intra= 0;
2707                     for(dir=0; dir<2; dir++){
2708                         for(i=0; i<2; i++){
2709                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2710                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2711                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2712                         }
2713                     }
2714                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2715                                  &dmin, &next_block, 0, 0);
2716                 }
2717                 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2718                     s->mv_dir = 0;
2719                     s->mv_type = MV_TYPE_16X16;
2720                     s->mb_intra= 1;
2721                     s->mv[0][0][0] = 0;
2722                     s->mv[0][0][1] = 0;
2723                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2724                                  &dmin, &next_block, 0, 0);
2725                     if(s->h263_pred || s->h263_aic){
2726                         if(best_s.mb_intra)
2727                             s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2728                         else
2729                             ff_clean_intra_table_entries(s); //old mode?
2730                     }
2731                 }
2732
2733                 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2734                     if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2735                         const int last_qp= backup_s.qscale;
2736                         int qpi, qp, dc[6];
2737                         DCTELEM ac[6][16];
2738                         const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2739                         static const int dquant_tab[4]={-1,1,-2,2};
2740
2741                         assert(backup_s.dquant == 0);
2742
2743                         //FIXME intra
2744                         s->mv_dir= best_s.mv_dir;
2745                         s->mv_type = MV_TYPE_16X16;
2746                         s->mb_intra= best_s.mb_intra;
2747                         s->mv[0][0][0] = best_s.mv[0][0][0];
2748                         s->mv[0][0][1] = best_s.mv[0][0][1];
2749                         s->mv[1][0][0] = best_s.mv[1][0][0];
2750                         s->mv[1][0][1] = best_s.mv[1][0][1];
2751
2752                         qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2753                         for(; qpi<4; qpi++){
2754                             int dquant= dquant_tab[qpi];
2755                             qp= last_qp + dquant;
2756                             if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2757                                 continue;
2758                             backup_s.dquant= dquant;
2759                             if(s->mb_intra && s->dc_val[0]){
2760                                 for(i=0; i<6; i++){
2761                                     dc[i]= s->dc_val[0][ s->block_index[i] ];
2762                                     memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
2763                                 }
2764                             }
2765
2766                             encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2767                                          &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2768                             if(best_s.qscale != qp){
2769                                 if(s->mb_intra && s->dc_val[0]){
2770                                     for(i=0; i<6; i++){
2771                                         s->dc_val[0][ s->block_index[i] ]= dc[i];
2772                                         memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
2773                                     }
2774                                 }
2775                             }
2776                         }
2777                     }
2778                 }
2779                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2780                     int mx= s->b_direct_mv_table[xy][0];
2781                     int my= s->b_direct_mv_table[xy][1];
2782
2783                     backup_s.dquant = 0;
2784                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2785                     s->mb_intra= 0;
2786                     ff_mpeg4_set_direct_mv(s, mx, my);
2787                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2788                                  &dmin, &next_block, mx, my);
2789                 }
2790                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2791                     backup_s.dquant = 0;
2792                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2793                     s->mb_intra= 0;
2794                     ff_mpeg4_set_direct_mv(s, 0, 0);
2795                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2796                                  &dmin, &next_block, 0, 0);
2797                 }
2798                 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2799                     int coded=0;
2800                     for(i=0; i<6; i++)
2801                         coded |= s->block_last_index[i];
2802                     if(coded){
2803                         int mx,my;
2804                         memcpy(s->mv, best_s.mv, sizeof(s->mv));
2805                         if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2806                             mx=my=0; //FIXME find the one we actually used
2807                             ff_mpeg4_set_direct_mv(s, mx, my);
2808                         }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2809                             mx= s->mv[1][0][0];
2810                             my= s->mv[1][0][1];
2811                         }else{
2812                             mx= s->mv[0][0][0];
2813                             my= s->mv[0][0][1];
2814                         }
2815
2816                         s->mv_dir= best_s.mv_dir;
2817                         s->mv_type = best_s.mv_type;
2818                         s->mb_intra= 0;
2819 /*                        s->mv[0][0][0] = best_s.mv[0][0][0];
2820                         s->mv[0][0][1] = best_s.mv[0][0][1];
2821                         s->mv[1][0][0] = best_s.mv[1][0][0];
2822                         s->mv[1][0][1] = best_s.mv[1][0][1];*/
2823                         backup_s.dquant= 0;
2824                         s->skipdct=1;
2825                         encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2826                                         &dmin, &next_block, mx, my);
2827                         s->skipdct=0;
2828                     }
2829                 }
2830
2831                 s->current_picture.f.qscale_table[xy] = best_s.qscale;
2832
2833                 copy_context_after_encode(s, &best_s, -1);
2834
2835                 pb_bits_count= put_bits_count(&s->pb);
2836                 flush_put_bits(&s->pb);
2837                 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2838                 s->pb= backup_s.pb;
2839
2840                 if(s->data_partitioning){
2841                     pb2_bits_count= put_bits_count(&s->pb2);
2842                     flush_put_bits(&s->pb2);
2843                     avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2844                     s->pb2= backup_s.pb2;
2845
2846                     tex_pb_bits_count= put_bits_count(&s->tex_pb);
2847                     flush_put_bits(&s->tex_pb);
2848                     avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2849                     s->tex_pb= backup_s.tex_pb;
2850                 }
2851                 s->last_bits= put_bits_count(&s->pb);
2852
2853                 if (CONFIG_H263_ENCODER &&
2854                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2855                     ff_h263_update_motion_val(s);
2856
2857                 if(next_block==0){ //FIXME 16 vs linesize16
2858                     s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
2859                     s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
2860                     s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2861                 }
2862
2863                 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2864                     ff_MPV_decode_mb(s, s->block);
2865             } else {
2866                 int motion_x = 0, motion_y = 0;
2867                 s->mv_type=MV_TYPE_16X16;
2868                 // only one MB-Type possible
2869
2870                 switch(mb_type){
2871                 case CANDIDATE_MB_TYPE_INTRA:
2872                     s->mv_dir = 0;
2873                     s->mb_intra= 1;
2874                     motion_x= s->mv[0][0][0] = 0;
2875                     motion_y= s->mv[0][0][1] = 0;
2876                     break;
2877                 case CANDIDATE_MB_TYPE_INTER:
2878                     s->mv_dir = MV_DIR_FORWARD;
2879                     s->mb_intra= 0;
2880                     motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2881                     motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2882                     break;
2883                 case CANDIDATE_MB_TYPE_INTER_I:
2884                     s->mv_dir = MV_DIR_FORWARD;
2885                     s->mv_type = MV_TYPE_FIELD;
2886                     s->mb_intra= 0;
2887                     for(i=0; i<2; i++){
2888                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2889                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2890                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2891                     }
2892                     break;
2893                 case CANDIDATE_MB_TYPE_INTER4V:
2894                     s->mv_dir = MV_DIR_FORWARD;
2895                     s->mv_type = MV_TYPE_8X8;
2896                     s->mb_intra= 0;
2897                     for(i=0; i<4; i++){
2898                         s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
2899                         s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
2900                     }
2901                     break;
2902                 case CANDIDATE_MB_TYPE_DIRECT:
2903                     if (CONFIG_MPEG4_ENCODER) {
2904                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2905                         s->mb_intra= 0;
2906                         motion_x=s->b_direct_mv_table[xy][0];
2907                         motion_y=s->b_direct_mv_table[xy][1];
2908                         ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
2909                     }
2910                     break;
2911                 case CANDIDATE_MB_TYPE_DIRECT0:
2912                     if (CONFIG_MPEG4_ENCODER) {
2913                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2914                         s->mb_intra= 0;
2915                         ff_mpeg4_set_direct_mv(s, 0, 0);
2916                     }
2917                     break;
2918                 case CANDIDATE_MB_TYPE_BIDIR:
2919                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2920                     s->mb_intra= 0;
2921                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2922                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2923                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2924                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2925                     break;
2926                 case CANDIDATE_MB_TYPE_BACKWARD:
2927                     s->mv_dir = MV_DIR_BACKWARD;
2928                     s->mb_intra= 0;
2929                     motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2930                     motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2931                     break;
2932                 case CANDIDATE_MB_TYPE_FORWARD:
2933                     s->mv_dir = MV_DIR_FORWARD;
2934                     s->mb_intra= 0;
2935                     motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2936                     motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2937 //                    printf(" %d %d ", motion_x, motion_y);
2938                     break;
2939                 case CANDIDATE_MB_TYPE_FORWARD_I:
2940                     s->mv_dir = MV_DIR_FORWARD;
2941                     s->mv_type = MV_TYPE_FIELD;
2942                     s->mb_intra= 0;
2943                     for(i=0; i<2; i++){
2944                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2945                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2946                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2947                     }
2948                     break;
2949                 case CANDIDATE_MB_TYPE_BACKWARD_I:
2950                     s->mv_dir = MV_DIR_BACKWARD;
2951                     s->mv_type = MV_TYPE_FIELD;
2952                     s->mb_intra= 0;
2953                     for(i=0; i<2; i++){
2954                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2955                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2956                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2957                     }
2958                     break;
2959                 case CANDIDATE_MB_TYPE_BIDIR_I:
2960                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2961                     s->mv_type = MV_TYPE_FIELD;
2962                     s->mb_intra= 0;
2963                     for(dir=0; dir<2; dir++){
2964                         for(i=0; i<2; i++){
2965                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2966                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2967                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2968                         }
2969                     }
2970                     break;
2971                 default:
2972                     av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
2973                 }
2974
2975                 encode_mb(s, motion_x, motion_y);
2976
2977                 // RAL: Update last macroblock type
2978                 s->last_mv_dir = s->mv_dir;
2979
2980                 if (CONFIG_H263_ENCODER &&
2981                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2982                     ff_h263_update_motion_val(s);
2983
2984                 ff_MPV_decode_mb(s, s->block);
2985             }
2986
2987             /* clean the MV table in IPS frames for direct mode in B frames */
2988             if(s->mb_intra /* && I,P,S_TYPE */){
2989                 s->p_mv_table[xy][0]=0;
2990                 s->p_mv_table[xy][1]=0;
2991             }
2992
2993             if(s->flags&CODEC_FLAG_PSNR){
2994                 int w= 16;
2995                 int h= 16;
2996
2997                 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2998                 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2999
3000                 s->current_picture.f.error[0] += sse(
3001                     s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3002                     s->dest[0], w, h, s->linesize);
3003                 s->current_picture.f.error[1] += sse(
3004                     s, s->new_picture.f.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3005                     s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3006                 s->current_picture.f.error[2] += sse(
3007                     s, s->new_picture.f.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3008                     s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3009             }
3010             if(s->loop_filter){
3011                 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3012                     ff_h263_loop_filter(s);
3013             }
3014 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
3015         }
3016     }
3017
3018     //not beautiful here but we must write it before flushing so it has to be here
3019     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3020         ff_msmpeg4_encode_ext_header(s);
3021
3022     write_slice_end(s);
3023
3024     /* Send the last GOB if RTP */
3025     if (s->avctx->rtp_callback) {
3026         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3027         pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3028         /* Call the RTP callback to send the last GOB */
3029         emms_c();
3030         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3031     }
3032
3033     return 0;
3034 }
3035
3036 #define MERGE(field) dst->field += src->field; src->field=0
3037 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3038     MERGE(me.scene_change_score);
3039     MERGE(me.mc_mb_var_sum_temp);
3040     MERGE(me.mb_var_sum_temp);
3041 }
3042
3043 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3044     int i;
3045
3046     MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3047     MERGE(dct_count[1]);
3048     MERGE(mv_bits);
3049     MERGE(i_tex_bits);
3050     MERGE(p_tex_bits);
3051     MERGE(i_count);
3052     MERGE(f_count);
3053     MERGE(b_count);
3054     MERGE(skip_count);
3055     MERGE(misc_bits);
3056     MERGE(error_count);
3057     MERGE(padding_bug_score);
3058     MERGE(current_picture.f.error[0]);
3059     MERGE(current_picture.f.error[1]);
3060     MERGE(current_picture.f.error[2]);
3061
3062     if(dst->avctx->noise_reduction){
3063         for(i=0; i<64; i++){
3064             MERGE(dct_error_sum[0][i]);
3065             MERGE(dct_error_sum[1][i]);
3066         }
3067     }
3068
3069     assert(put_bits_count(&src->pb) % 8 ==0);
3070     assert(put_bits_count(&dst->pb) % 8 ==0);
3071     avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3072     flush_put_bits(&dst->pb);
3073 }
3074
3075 static int estimate_qp(MpegEncContext *s, int dry_run){
3076     if (s->next_lambda){
3077         s->current_picture_ptr->f.quality =
3078         s->current_picture.f.quality = s->next_lambda;
3079         if(!dry_run) s->next_lambda= 0;
3080     } else if (!s->fixed_qscale) {
3081         s->current_picture_ptr->f.quality =
3082         s->current_picture.f.quality = ff_rate_estimate_qscale(s, dry_run);
3083         if (s->current_picture.f.quality < 0)
3084             return -1;
3085     }
3086
3087     if(s->adaptive_quant){
3088         switch(s->codec_id){
3089         case CODEC_ID_MPEG4:
3090             if (CONFIG_MPEG4_ENCODER)
3091                 ff_clean_mpeg4_qscales(s);
3092             break;
3093         case CODEC_ID_H263:
3094         case CODEC_ID_H263P:
3095         case CODEC_ID_FLV1:
3096             if (CONFIG_H263_ENCODER)
3097                 ff_clean_h263_qscales(s);
3098             break;
3099         default:
3100             ff_init_qscale_tab(s);
3101         }
3102
3103         s->lambda= s->lambda_table[0];
3104         //FIXME broken
3105     }else
3106         s->lambda = s->current_picture.f.quality;
3107 //printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
3108     update_qscale(s);
3109     return 0;
3110 }
3111
3112 /* must be called before writing the header */
3113 static void set_frame_distances(MpegEncContext * s){
3114     assert(s->current_picture_ptr->f.pts != AV_NOPTS_VALUE);
3115     s->time = s->current_picture_ptr->f.pts * s->avctx->time_base.num;
3116
3117     if(s->pict_type==AV_PICTURE_TYPE_B){
3118         s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3119         assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3120     }else{
3121         s->pp_time= s->time - s->last_non_b_time;
3122         s->last_non_b_time= s->time;
3123         assert(s->picture_number==0 || s->pp_time > 0);
3124     }
3125 }
3126
3127 static int encode_picture(MpegEncContext *s, int picture_number)
3128 {
3129     int i;
3130     int bits;
3131     int context_count = s->slice_context_count;
3132
3133     s->picture_number = picture_number;
3134
3135     /* Reset the average MB variance */
3136     s->me.mb_var_sum_temp    =
3137     s->me.mc_mb_var_sum_temp = 0;
3138
3139     /* we need to initialize some time vars before we can encode b-frames */
3140     // RAL: Condition added for MPEG1VIDEO
3141     if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3142         set_frame_distances(s);
3143     if(CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
3144         ff_set_mpeg4_time(s);
3145
3146     s->me.scene_change_score=0;
3147
3148 //    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3149
3150     if(s->pict_type==AV_PICTURE_TYPE_I){
3151         if(s->msmpeg4_version >= 3) s->no_rounding=1;
3152         else                        s->no_rounding=0;
3153     }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3154         if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
3155             s->no_rounding ^= 1;
3156     }
3157
3158     if(s->flags & CODEC_FLAG_PASS2){
3159         if (estimate_qp(s,1) < 0)
3160             return -1;
3161         ff_get_2pass_fcode(s);
3162     }else if(!(s->flags & CODEC_FLAG_QSCALE)){
3163         if(s->pict_type==AV_PICTURE_TYPE_B)
3164             s->lambda= s->last_lambda_for[s->pict_type];
3165         else
3166             s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3167         update_qscale(s);
3168     }
3169
3170     if(s->codec_id != CODEC_ID_AMV){
3171         if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
3172         if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
3173         s->q_chroma_intra_matrix   = s->q_intra_matrix;
3174         s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
3175     }
3176
3177     s->mb_intra=0; //for the rate distortion & bit compare functions
3178     for(i=1; i<context_count; i++){
3179         ff_update_duplicate_context(s->thread_context[i], s);
3180     }
3181
3182     if(ff_init_me(s)<0)
3183         return -1;
3184
3185     /* Estimate motion for every MB */
3186     if(s->pict_type != AV_PICTURE_TYPE_I){
3187         s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3188         s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3189         if(s->pict_type != AV_PICTURE_TYPE_B && s->avctx->me_threshold==0){
3190             if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3191                 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3192             }
3193         }
3194
3195         s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3196     }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3197         /* I-Frame */
3198         for(i=0; i<s->mb_stride*s->mb_height; i++)
3199             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3200
3201         if(!s->fixed_qscale){
3202             /* finding spatial complexity for I-frame rate control */
3203             s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3204         }
3205     }
3206     for(i=1; i<context_count; i++){
3207         merge_context_after_me(s, s->thread_context[i]);
3208     }
3209     s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3210     s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->me.   mb_var_sum_temp;
3211     emms_c();
3212
3213     if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3214         s->pict_type= AV_PICTURE_TYPE_I;
3215         for(i=0; i<s->mb_stride*s->mb_height; i++)
3216             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3217 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3218     }
3219
3220     if(!s->umvplus){
3221         if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3222             s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3223
3224             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3225                 int a,b;
3226                 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3227                 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3228                 s->f_code= FFMAX3(s->f_code, a, b);
3229             }
3230
3231             ff_fix_long_p_mvs(s);
3232             ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3233             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3234                 int j;
3235                 for(i=0; i<2; i++){
3236                     for(j=0; j<2; j++)
3237                         ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3238                                         s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3239                 }
3240             }
3241         }
3242
3243         if(s->pict_type==AV_PICTURE_TYPE_B){
3244             int a, b;
3245
3246             a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3247             b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3248             s->f_code = FFMAX(a, b);
3249
3250             a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3251             b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3252             s->b_code = FFMAX(a, b);
3253
3254             ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3255             ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3256             ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3257             ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3258             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3259                 int dir, j;
3260                 for(dir=0; dir<2; dir++){
3261                     for(i=0; i<2; i++){
3262                         for(j=0; j<2; j++){
3263                             int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3264                                           : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3265                             ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3266                                             s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3267                         }
3268                     }
3269                 }
3270             }
3271         }
3272     }
3273
3274     if (estimate_qp(s, 0) < 0)
3275         return -1;
3276
3277     if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==AV_PICTURE_TYPE_I && !(s->flags & CODEC_FLAG_QSCALE))
3278         s->qscale= 3; //reduce clipping problems
3279
3280     if (s->out_format == FMT_MJPEG) {
3281         /* for mjpeg, we do include qscale in the matrix */
3282         for(i=1;i<64;i++){
3283             int j= s->dsp.idct_permutation[i];
3284
3285             s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3286         }
3287         s->y_dc_scale_table=
3288         s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3289         s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3290         ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
3291                        s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3292         s->qscale= 8;
3293     }
3294     if(s->codec_id == CODEC_ID_AMV){
3295         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};
3296         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};
3297         for(i=1;i<64;i++){
3298             int j= s->dsp.idct_permutation[ff_zigzag_direct[i]];
3299
3300             s->intra_matrix[j] = sp5x_quant_table[5*2+0][i];
3301             s->chroma_intra_matrix[j] = sp5x_quant_table[5*2+1][i];
3302         }
3303         s->y_dc_scale_table= y;
3304         s->c_dc_scale_table= c;
3305         s->intra_matrix[0] = 13;
3306         s->chroma_intra_matrix[0] = 14;
3307         ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
3308                        s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3309         ff_convert_matrix(&s->dsp, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3310                        s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3311         s->qscale= 8;
3312     }
3313
3314     //FIXME var duplication
3315     s->current_picture_ptr->f.key_frame =
3316     s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3317     s->current_picture_ptr->f.pict_type =
3318     s->current_picture.f.pict_type = s->pict_type;
3319
3320     if (s->current_picture.f.key_frame)
3321         s->picture_in_gop_number=0;
3322
3323     s->last_bits= put_bits_count(&s->pb);
3324     switch(s->out_format) {
3325     case FMT_MJPEG:
3326         if (CONFIG_MJPEG_ENCODER)
3327             ff_mjpeg_encode_picture_header(s);
3328         break;
3329     case FMT_H261:
3330         if (CONFIG_H261_ENCODER)
3331             ff_h261_encode_picture_header(s, picture_number);
3332         break;
3333     case FMT_H263:
3334         if (CONFIG_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
3335             ff_wmv2_encode_picture_header(s, picture_number);
3336         else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3337             ff_msmpeg4_encode_picture_header(s, picture_number);
3338         else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3339             ff_mpeg4_encode_picture_header(s, picture_number);
3340         else if (CONFIG_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
3341             ff_rv10_encode_picture_header(s, picture_number);
3342         else if (CONFIG_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
3343             ff_rv20_encode_picture_header(s, picture_number);
3344         else if (CONFIG_FLV_ENCODER && s->codec_id == CODEC_ID_FLV1)
3345             ff_flv_encode_picture_header(s, picture_number);
3346         else if (CONFIG_H263_ENCODER)
3347             ff_h263_encode_picture_header(s, picture_number);
3348         break;
3349     case FMT_MPEG1:
3350         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3351             ff_mpeg1_encode_picture_header(s, picture_number);
3352         break;
3353     case FMT_H264:
3354         break;
3355     default:
3356         assert(0);
3357     }
3358     bits= put_bits_count(&s->pb);
3359     s->header_bits= bits - s->last_bits;
3360
3361     for(i=1; i<context_count; i++){
3362         update_duplicate_context_after_me(s->thread_context[i], s);
3363     }
3364     s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3365     for(i=1; i<context_count; i++){
3366         merge_context_after_encode(s, s->thread_context[i]);
3367     }
3368     emms_c();
3369     return 0;
3370 }
3371
3372 static void denoise_dct_c(MpegEncContext *s, DCTELEM *block){
3373     const int intra= s->mb_intra;
3374     int i;
3375
3376     s->dct_count[intra]++;
3377
3378     for(i=0; i<64; i++){
3379         int level= block[i];
3380
3381         if(level){
3382             if(level>0){
3383                 s->dct_error_sum[intra][i] += level;
3384                 level -= s->dct_offset[intra][i];
3385                 if(level<0) level=0;
3386             }else{
3387                 s->dct_error_sum[intra][i] -= level;
3388                 level += s->dct_offset[intra][i];
3389                 if(level>0) level=0;
3390             }
3391             block[i]= level;
3392         }
3393     }
3394 }
3395
3396 static int dct_quantize_trellis_c(MpegEncContext *s,
3397                                   DCTELEM *block, int n,
3398                                   int qscale, int *overflow){
3399     const int *qmat;
3400     const uint8_t *scantable= s->intra_scantable.scantable;
3401     const uint8_t *perm_scantable= s->intra_scantable.permutated;
3402     int max=0;
3403     unsigned int threshold1, threshold2;
3404     int bias=0;
3405     int run_tab[65];
3406     int level_tab[65];
3407     int score_tab[65];
3408     int survivor[65];
3409     int survivor_count;
3410     int last_run=0;
3411     int last_level=0;
3412     int last_score= 0;
3413     int last_i;
3414     int coeff[2][64];
3415     int coeff_count[64];
3416     int qmul, qadd, start_i, last_non_zero, i, dc;
3417     const int esc_length= s->ac_esc_length;
3418     uint8_t * length;
3419     uint8_t * last_length;
3420     const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3421
3422     s->dsp.fdct (block);
3423
3424     if(s->dct_error_sum)
3425         s->denoise_dct(s, block);
3426     qmul= qscale*16;
3427     qadd= ((qscale-1)|1)*8;
3428
3429     if (s->mb_intra) {
3430         int q;
3431         if (!s->h263_aic) {
3432             if (n < 4)
3433                 q = s->y_dc_scale;
3434             else
3435                 q = s->c_dc_scale;
3436             q = q << 3;
3437         } else{
3438             /* For AIC we skip quant/dequant of INTRADC */
3439             q = 1 << 3;
3440             qadd=0;
3441         }
3442
3443         /* note: block[0] is assumed to be positive */
3444         block[0] = (block[0] + (q >> 1)) / q;
3445         start_i = 1;
3446         last_non_zero = 0;
3447         qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3448         if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3449             bias= 1<<(QMAT_SHIFT-1);
3450         length     = s->intra_ac_vlc_length;
3451         last_length= s->intra_ac_vlc_last_length;
3452     } else {
3453         start_i = 0;
3454         last_non_zero = -1;
3455         qmat = s->q_inter_matrix[qscale];
3456         length     = s->inter_ac_vlc_length;
3457         last_length= s->inter_ac_vlc_last_length;
3458     }
3459     last_i= start_i;
3460
3461     threshold1= (1<<QMAT_SHIFT) - bias - 1;
3462     threshold2= (threshold1<<1);
3463
3464     for(i=63; i>=start_i; i--) {
3465         const int j = scantable[i];
3466         int level = block[j] * qmat[j];
3467
3468         if(((unsigned)(level+threshold1))>threshold2){
3469             last_non_zero = i;
3470             break;
3471         }
3472     }
3473
3474     for(i=start_i; i<=last_non_zero; i++) {
3475         const int j = scantable[i];
3476         int level = block[j] * qmat[j];
3477
3478 //        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3479 //           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3480         if(((unsigned)(level+threshold1))>threshold2){
3481             if(level>0){
3482                 level= (bias + level)>>QMAT_SHIFT;
3483                 coeff[0][i]= level;
3484                 coeff[1][i]= level-1;
3485 //                coeff[2][k]= level-2;
3486             }else{
3487                 level= (bias - level)>>QMAT_SHIFT;
3488                 coeff[0][i]= -level;
3489                 coeff[1][i]= -level+1;
3490 //                coeff[2][k]= -level+2;
3491             }
3492             coeff_count[i]= FFMIN(level, 2);
3493             assert(coeff_count[i]);
3494             max |=level;
3495         }else{
3496             coeff[0][i]= (level>>31)|1;
3497             coeff_count[i]= 1;
3498         }
3499     }
3500
3501     *overflow= s->max_qcoeff < max; //overflow might have happened
3502
3503     if(last_non_zero < start_i){
3504         memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3505         return last_non_zero;
3506     }
3507
3508     score_tab[start_i]= 0;
3509     survivor[0]= start_i;
3510     survivor_count= 1;
3511
3512     for(i=start_i; i<=last_non_zero; i++){
3513         int level_index, j, zero_distortion;
3514         int dct_coeff= FFABS(block[ scantable[i] ]);
3515         int best_score=256*256*256*120;
3516
3517         if (s->dsp.fdct == ff_fdct_ifast)
3518             dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3519         zero_distortion= dct_coeff*dct_coeff;
3520
3521         for(level_index=0; level_index < coeff_count[i]; level_index++){
3522             int distortion;
3523             int level= coeff[level_index][i];
3524             const int alevel= FFABS(level);
3525             int unquant_coeff;
3526
3527             assert(level);
3528
3529             if(s->out_format == FMT_H263){
3530                 unquant_coeff= alevel*qmul + qadd;
3531             }else{ //MPEG1
3532                 j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
3533                 if(s->mb_intra){
3534                         unquant_coeff = (int)(  alevel  * qscale * s->intra_matrix[j]) >> 3;
3535                         unquant_coeff =   (unquant_coeff - 1) | 1;
3536                 }else{
3537                         unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3538                         unquant_coeff =   (unquant_coeff - 1) | 1;
3539                 }
3540                 unquant_coeff<<= 3;
3541             }
3542
3543             distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3544             level+=64;
3545             if((level&(~127)) == 0){
3546                 for(j=survivor_count-1; j>=0; j--){
3547                     int run= i - survivor[j];
3548                     int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3549                     score += score_tab[i-run];
3550
3551                     if(score < best_score){
3552                         best_score= score;
3553                         run_tab[i+1]= run;
3554                         level_tab[i+1]= level-64;
3555                     }
3556                 }
3557
3558                 if(s->out_format == FMT_H263){
3559                     for(j=survivor_count-1; j>=0; j--){
3560                         int run= i - survivor[j];
3561                         int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3562                         score += score_tab[i-run];
3563                         if(score < last_score){
3564                             last_score= score;
3565                             last_run= run;
3566                             last_level= level-64;
3567                             last_i= i+1;
3568                         }
3569                     }
3570                 }
3571             }else{
3572                 distortion += esc_length*lambda;
3573                 for(j=survivor_count-1; j>=0; j--){
3574                     int run= i - survivor[j];
3575                     int score= distortion + score_tab[i-run];
3576
3577                     if(score < best_score){
3578                         best_score= score;
3579                         run_tab[i+1]= run;
3580                         level_tab[i+1]= level-64;
3581                     }
3582                 }
3583
3584                 if(s->out_format == FMT_H263){
3585                   for(j=survivor_count-1; j>=0; j--){
3586                         int run= i - survivor[j];
3587                         int score= distortion + score_tab[i-run];
3588                         if(score < last_score){
3589                             last_score= score;
3590                             last_run= run;
3591                             last_level= level-64;
3592                             last_i= i+1;
3593                         }
3594                     }
3595                 }
3596             }
3597         }
3598
3599         score_tab[i+1]= best_score;
3600
3601         //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3602         if(last_non_zero <= 27){
3603             for(; survivor_count; survivor_count--){
3604                 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3605                     break;
3606             }
3607         }else{
3608             for(; survivor_count; survivor_count--){
3609                 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3610                     break;
3611             }
3612         }
3613
3614         survivor[ survivor_count++ ]= i+1;
3615     }
3616
3617     if(s->out_format != FMT_H263){
3618         last_score= 256*256*256*120;
3619         for(i= survivor[0]; i<=last_non_zero + 1; i++){
3620             int score= score_tab[i];
3621             if(i) score += lambda*2; //FIXME exacter?
3622
3623             if(score < last_score){
3624                 last_score= score;
3625                 last_i= i;
3626                 last_level= level_tab[i];
3627                 last_run= run_tab[i];
3628             }
3629         }
3630     }
3631
3632     s->coded_score[n] = last_score;
3633
3634     dc= FFABS(block[0]);
3635     last_non_zero= last_i - 1;
3636     memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3637
3638     if(last_non_zero < start_i)
3639         return last_non_zero;
3640
3641     if(last_non_zero == 0 && start_i == 0){
3642         int best_level= 0;
3643         int best_score= dc * dc;
3644
3645         for(i=0; i<coeff_count[0]; i++){
3646             int level= coeff[i][0];
3647             int alevel= FFABS(level);
3648             int unquant_coeff, score, distortion;
3649
3650             if(s->out_format == FMT_H263){
3651                     unquant_coeff= (alevel*qmul + qadd)>>3;
3652             }else{ //MPEG1
3653                     unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3654                     unquant_coeff =   (unquant_coeff - 1) | 1;
3655             }
3656             unquant_coeff = (unquant_coeff + 4) >> 3;
3657             unquant_coeff<<= 3 + 3;
3658
3659             distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3660             level+=64;
3661             if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3662             else                    score= distortion + esc_length*lambda;
3663
3664             if(score < best_score){
3665                 best_score= score;
3666                 best_level= level - 64;
3667             }
3668         }
3669         block[0]= best_level;
3670         s->coded_score[n] = best_score - dc*dc;
3671         if(best_level == 0) return -1;
3672         else                return last_non_zero;
3673     }
3674
3675     i= last_i;
3676     assert(last_level);
3677
3678     block[ perm_scantable[last_non_zero] ]= last_level;
3679     i -= last_run + 1;
3680
3681     for(; i>start_i; i -= run_tab[i] + 1){
3682         block[ perm_scantable[i-1] ]= level_tab[i];
3683     }
3684
3685     return last_non_zero;
3686 }
3687
3688 //#define REFINE_STATS 1
3689 static int16_t basis[64][64];
3690
3691 static void build_basis(uint8_t *perm){
3692     int i, j, x, y;
3693     emms_c();
3694     for(i=0; i<8; i++){
3695         for(j=0; j<8; j++){
3696             for(y=0; y<8; y++){
3697                 for(x=0; x<8; x++){
3698                     double s= 0.25*(1<<BASIS_SHIFT);
3699                     int index= 8*i + j;
3700                     int perm_index= perm[index];
3701                     if(i==0) s*= sqrt(0.5);
3702                     if(j==0) s*= sqrt(0.5);
3703                     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)));
3704                 }
3705             }
3706         }
3707     }
3708 }
3709
3710 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3711                         DCTELEM *block, int16_t *weight, DCTELEM *orig,
3712                         int n, int qscale){
3713     int16_t rem[64];
3714     LOCAL_ALIGNED_16(DCTELEM, d1, [64]);
3715     const uint8_t *scantable= s->intra_scantable.scantable;
3716     const uint8_t *perm_scantable= s->intra_scantable.permutated;
3717 //    unsigned int threshold1, threshold2;
3718 //    int bias=0;
3719     int run_tab[65];
3720     int prev_run=0;
3721     int prev_level=0;
3722     int qmul, qadd, start_i, last_non_zero, i, dc;
3723     uint8_t * length;
3724     uint8_t * last_length;
3725     int lambda;
3726     int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3727 #ifdef REFINE_STATS
3728 static int count=0;
3729 static int after_last=0;
3730 static int to_zero=0;
3731 static int from_zero=0;
3732 static int raise=0;
3733 static int lower=0;
3734 static int messed_sign=0;
3735 #endif
3736
3737     if(basis[0][0] == 0)
3738         build_basis(s->dsp.idct_permutation);
3739
3740     qmul= qscale*2;
3741     qadd= (qscale-1)|1;
3742     if (s->mb_intra) {
3743         if (!s->h263_aic) {
3744             if (n < 4)
3745                 q = s->y_dc_scale;
3746             else
3747                 q = s->c_dc_scale;
3748         } else{
3749             /* For AIC we skip quant/dequant of INTRADC */
3750             q = 1;
3751             qadd=0;
3752         }
3753         q <<= RECON_SHIFT-3;
3754         /* note: block[0] is assumed to be positive */
3755         dc= block[0]*q;
3756 //        block[0] = (block[0] + (q >> 1)) / q;
3757         start_i = 1;
3758 //        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3759 //            bias= 1<<(QMAT_SHIFT-1);
3760         length     = s->intra_ac_vlc_length;
3761         last_length= s->intra_ac_vlc_last_length;
3762     } else {
3763         dc= 0;
3764         start_i = 0;
3765         length     = s->inter_ac_vlc_length;
3766         last_length= s->inter_ac_vlc_last_length;
3767     }
3768     last_non_zero = s->block_last_index[n];
3769
3770 #ifdef REFINE_STATS
3771 {START_TIMER
3772 #endif
3773     dc += (1<<(RECON_SHIFT-1));
3774     for(i=0; i<64; i++){
3775         rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME  use orig dirrectly instead of copying to rem[]
3776     }
3777 #ifdef REFINE_STATS
3778 STOP_TIMER("memset rem[]")}
3779 #endif
3780     sum=0;
3781     for(i=0; i<64; i++){
3782         int one= 36;
3783         int qns=4;
3784         int w;
3785
3786         w= FFABS(weight[i]) + qns*one;
3787         w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3788
3789         weight[i] = w;
3790 //        w=weight[i] = (63*qns + (w/2)) / w;
3791
3792         assert(w>0);
3793         assert(w<(1<<6));
3794         sum += w*w;
3795     }
3796     lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3797 #ifdef REFINE_STATS
3798 {START_TIMER
3799 #endif
3800     run=0;
3801     rle_index=0;
3802     for(i=start_i; i<=last_non_zero; i++){
3803         int j= perm_scantable[i];
3804         const int level= block[j];
3805         int coeff;
3806
3807         if(level){
3808             if(level<0) coeff= qmul*level - qadd;
3809             else        coeff= qmul*level + qadd;
3810             run_tab[rle_index++]=run;
3811             run=0;
3812
3813             s->dsp.add_8x8basis(rem, basis[j], coeff);
3814         }else{
3815             run++;
3816         }
3817     }
3818 #ifdef REFINE_STATS
3819 if(last_non_zero>0){
3820 STOP_TIMER("init rem[]")
3821 }
3822 }
3823
3824 {START_TIMER
3825 #endif
3826     for(;;){
3827         int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
3828         int best_coeff=0;
3829         int best_change=0;
3830         int run2, best_unquant_change=0, analyze_gradient;
3831 #ifdef REFINE_STATS
3832 {START_TIMER
3833 #endif
3834         analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
3835
3836         if(analyze_gradient){
3837 #ifdef REFINE_STATS
3838 {START_TIMER
3839 #endif
3840             for(i=0; i<64; i++){
3841                 int w= weight[i];
3842
3843                 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3844             }
3845 #ifdef REFINE_STATS
3846 STOP_TIMER("rem*w*w")}
3847 {START_TIMER
3848 #endif
3849             s->dsp.fdct(d1);
3850 #ifdef REFINE_STATS
3851 STOP_TIMER("dct")}
3852 #endif
3853         }