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