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