dsputil: Split off *_8x8basis to a separate context
[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_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
705     ff_qpeldsp_init(&s->qdsp);
706
707     s->avctx->coded_frame = s->current_picture.f;
708
709     if (s->msmpeg4_version) {
710         FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
711                           2 * 2 * (MAX_LEVEL + 1) *
712                           (MAX_RUN + 1) * 2 * sizeof(int), fail);
713     }
714     FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
715
716     FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,   64 * 32 * sizeof(int), fail);
717     FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,   64 * 32 * sizeof(int), fail);
718     FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
719     FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
720     FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
721                       MAX_PICTURE_COUNT * sizeof(Picture *), fail);
722     FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
723                       MAX_PICTURE_COUNT * sizeof(Picture *), fail);
724
725     if (s->avctx->noise_reduction) {
726         FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
727                           2 * 64 * sizeof(uint16_t), fail);
728     }
729
730     if (CONFIG_H263_ENCODER)
731         ff_h263dsp_init(&s->h263dsp);
732     if (!s->dct_quantize)
733         s->dct_quantize = ff_dct_quantize_c;
734     if (!s->denoise_dct)
735         s->denoise_dct  = denoise_dct_c;
736     s->fast_dct_quantize = s->dct_quantize;
737     if (avctx->trellis)
738         s->dct_quantize  = dct_quantize_trellis_c;
739
740     if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
741         s->chroma_qscale_table = ff_h263_chroma_qscale_table;
742
743     s->quant_precision = 5;
744
745     ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
746     ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
747
748     if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
749         ff_h261_encode_init(s);
750     if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
751         ff_h263_encode_init(s);
752     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
753         ff_msmpeg4_encode_init(s);
754     if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
755         && s->out_format == FMT_MPEG1)
756         ff_mpeg1_encode_init(s);
757
758     /* init q matrix */
759     for (i = 0; i < 64; i++) {
760         int j = s->idsp.idct_permutation[i];
761         if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
762             s->mpeg_quant) {
763             s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
764             s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
765         } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
766             s->intra_matrix[j] =
767             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
768         } else {
769             /* mpeg1/2 */
770             s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
771             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
772         }
773         if (s->avctx->intra_matrix)
774             s->intra_matrix[j] = s->avctx->intra_matrix[i];
775         if (s->avctx->inter_matrix)
776             s->inter_matrix[j] = s->avctx->inter_matrix[i];
777     }
778
779     /* precompute matrix */
780     /* for mjpeg, we do include qscale in the matrix */
781     if (s->out_format != FMT_MJPEG) {
782         ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
783                           s->intra_matrix, s->intra_quant_bias, avctx->qmin,
784                           31, 1);
785         ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
786                           s->inter_matrix, s->inter_quant_bias, avctx->qmin,
787                           31, 0);
788     }
789
790     if (ff_rate_control_init(s) < 0)
791         return -1;
792
793 #if FF_API_ERROR_RATE
794     FF_DISABLE_DEPRECATION_WARNINGS
795     if (avctx->error_rate)
796         s->error_rate = avctx->error_rate;
797     FF_ENABLE_DEPRECATION_WARNINGS;
798 #endif
799
800 #if FF_API_NORMALIZE_AQP
801     FF_DISABLE_DEPRECATION_WARNINGS
802     if (avctx->flags & CODEC_FLAG_NORMALIZE_AQP)
803         s->mpv_flags |= FF_MPV_FLAG_NAQ;
804     FF_ENABLE_DEPRECATION_WARNINGS;
805 #endif
806
807 #if FF_API_MV0
808     FF_DISABLE_DEPRECATION_WARNINGS
809     if (avctx->flags & CODEC_FLAG_MV0)
810         s->mpv_flags |= FF_MPV_FLAG_MV0;
811     FF_ENABLE_DEPRECATION_WARNINGS
812 #endif
813
814     if (avctx->b_frame_strategy == 2) {
815         for (i = 0; i < s->max_b_frames + 2; i++) {
816             s->tmp_frames[i] = av_frame_alloc();
817             if (!s->tmp_frames[i])
818                 return AVERROR(ENOMEM);
819
820             s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
821             s->tmp_frames[i]->width  = s->width  >> avctx->brd_scale;
822             s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
823
824             ret = av_frame_get_buffer(s->tmp_frames[i], 32);
825             if (ret < 0)
826                 return ret;
827         }
828     }
829
830     return 0;
831 fail:
832     ff_MPV_encode_end(avctx);
833     return AVERROR_UNKNOWN;
834 }
835
836 av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
837 {
838     MpegEncContext *s = avctx->priv_data;
839     int i;
840
841     ff_rate_control_uninit(s);
842
843     ff_MPV_common_end(s);
844     if (CONFIG_MJPEG_ENCODER &&
845         s->out_format == FMT_MJPEG)
846         ff_mjpeg_encode_close(s);
847
848     av_freep(&avctx->extradata);
849
850     for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
851         av_frame_free(&s->tmp_frames[i]);
852
853     ff_free_picture_tables(&s->new_picture);
854     ff_mpeg_unref_picture(s, &s->new_picture);
855
856     av_freep(&s->avctx->stats_out);
857     av_freep(&s->ac_stats);
858
859     av_freep(&s->q_intra_matrix);
860     av_freep(&s->q_inter_matrix);
861     av_freep(&s->q_intra_matrix16);
862     av_freep(&s->q_inter_matrix16);
863     av_freep(&s->input_picture);
864     av_freep(&s->reordered_input_picture);
865     av_freep(&s->dct_offset);
866
867     return 0;
868 }
869
870 static int get_sae(uint8_t *src, int ref, int stride)
871 {
872     int x,y;
873     int acc = 0;
874
875     for (y = 0; y < 16; y++) {
876         for (x = 0; x < 16; x++) {
877             acc += FFABS(src[x + y * stride] - ref);
878         }
879     }
880
881     return acc;
882 }
883
884 static int get_intra_count(MpegEncContext *s, uint8_t *src,
885                            uint8_t *ref, int stride)
886 {
887     int x, y, w, h;
888     int acc = 0;
889
890     w = s->width  & ~15;
891     h = s->height & ~15;
892
893     for (y = 0; y < h; y += 16) {
894         for (x = 0; x < w; x += 16) {
895             int offset = x + y * stride;
896             int sad  = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
897                                      16);
898             int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
899             int sae  = get_sae(src + offset, mean, stride);
900
901             acc += sae + 500 < sad;
902         }
903     }
904     return acc;
905 }
906
907
908 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
909 {
910     Picture *pic = NULL;
911     int64_t pts;
912     int i, display_picture_number = 0, ret;
913     const int encoding_delay = s->max_b_frames ? s->max_b_frames :
914                                                  (s->low_delay ? 0 : 1);
915     int direct = 1;
916
917     if (pic_arg) {
918         pts = pic_arg->pts;
919         display_picture_number = s->input_picture_number++;
920
921         if (pts != AV_NOPTS_VALUE) {
922             if (s->user_specified_pts != AV_NOPTS_VALUE) {
923                 int64_t time = pts;
924                 int64_t last = s->user_specified_pts;
925
926                 if (time <= last) {
927                     av_log(s->avctx, AV_LOG_ERROR,
928                            "Error, Invalid timestamp=%"PRId64", "
929                            "last=%"PRId64"\n", pts, s->user_specified_pts);
930                     return -1;
931                 }
932
933                 if (!s->low_delay && display_picture_number == 1)
934                     s->dts_delta = time - last;
935             }
936             s->user_specified_pts = pts;
937         } else {
938             if (s->user_specified_pts != AV_NOPTS_VALUE) {
939                 s->user_specified_pts =
940                 pts = s->user_specified_pts + 1;
941                 av_log(s->avctx, AV_LOG_INFO,
942                        "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
943                        pts);
944             } else {
945                 pts = display_picture_number;
946             }
947         }
948     }
949
950     if (pic_arg) {
951         if (!pic_arg->buf[0]);
952             direct = 0;
953         if (pic_arg->linesize[0] != s->linesize)
954             direct = 0;
955         if (pic_arg->linesize[1] != s->uvlinesize)
956             direct = 0;
957         if (pic_arg->linesize[2] != s->uvlinesize)
958             direct = 0;
959
960         av_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
961                 pic_arg->linesize[1], s->linesize, s->uvlinesize);
962
963         if (direct) {
964             i = ff_find_unused_picture(s, 1);
965             if (i < 0)
966                 return i;
967
968             pic = &s->picture[i];
969             pic->reference = 3;
970
971             if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
972                 return ret;
973             if (ff_alloc_picture(s, pic, 1) < 0) {
974                 return -1;
975             }
976         } else {
977             i = ff_find_unused_picture(s, 0);
978             if (i < 0)
979                 return i;
980
981             pic = &s->picture[i];
982             pic->reference = 3;
983
984             if (ff_alloc_picture(s, pic, 0) < 0) {
985                 return -1;
986             }
987
988             if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
989                 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
990                 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
991                 // empty
992             } else {
993                 int h_chroma_shift, v_chroma_shift;
994                 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
995                                                  &h_chroma_shift,
996                                                  &v_chroma_shift);
997
998                 for (i = 0; i < 3; i++) {
999                     int src_stride = pic_arg->linesize[i];
1000                     int dst_stride = i ? s->uvlinesize : s->linesize;
1001                     int h_shift = i ? h_chroma_shift : 0;
1002                     int v_shift = i ? v_chroma_shift : 0;
1003                     int w = s->width  >> h_shift;
1004                     int h = s->height >> v_shift;
1005                     uint8_t *src = pic_arg->data[i];
1006                     uint8_t *dst = pic->f->data[i];
1007
1008                     if (!s->avctx->rc_buffer_size)
1009                         dst += INPLACE_OFFSET;
1010
1011                     if (src_stride == dst_stride)
1012                         memcpy(dst, src, src_stride * h);
1013                     else {
1014                         while (h--) {
1015                             memcpy(dst, src, w);
1016                             dst += dst_stride;
1017                             src += src_stride;
1018                         }
1019                     }
1020                 }
1021             }
1022         }
1023         ret = av_frame_copy_props(pic->f, pic_arg);
1024         if (ret < 0)
1025             return ret;
1026
1027         pic->f->display_picture_number = display_picture_number;
1028         pic->f->pts = pts; // we set this here to avoid modifiying pic_arg
1029     }
1030
1031     /* shift buffer entries */
1032     for (i = 1; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1033         s->input_picture[i - 1] = s->input_picture[i];
1034
1035     s->input_picture[encoding_delay] = (Picture*) pic;
1036
1037     return 0;
1038 }
1039
1040 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1041 {
1042     int x, y, plane;
1043     int score = 0;
1044     int64_t score64 = 0;
1045
1046     for (plane = 0; plane < 3; plane++) {
1047         const int stride = p->f->linesize[plane];
1048         const int bw = plane ? 1 : 2;
1049         for (y = 0; y < s->mb_height * bw; y++) {
1050             for (x = 0; x < s->mb_width * bw; x++) {
1051                 int off = p->shared ? 0 : 16;
1052                 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1053                 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1054                 int v   = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1055
1056                 switch (s->avctx->frame_skip_exp) {
1057                 case 0: score    =  FFMAX(score, v);          break;
1058                 case 1: score   += FFABS(v);                  break;
1059                 case 2: score   += v * v;                     break;
1060                 case 3: score64 += FFABS(v * v * (int64_t)v); break;
1061                 case 4: score64 += v * v * (int64_t)(v * v);  break;
1062                 }
1063             }
1064         }
1065     }
1066
1067     if (score)
1068         score64 = score;
1069
1070     if (score64 < s->avctx->frame_skip_threshold)
1071         return 1;
1072     if (score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda) >> 8))
1073         return 1;
1074     return 0;
1075 }
1076
1077 static int encode_frame(AVCodecContext *c, AVFrame *frame)
1078 {
1079     AVPacket pkt = { 0 };
1080     int ret, got_output;
1081
1082     av_init_packet(&pkt);
1083     ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
1084     if (ret < 0)
1085         return ret;
1086
1087     ret = pkt.size;
1088     av_free_packet(&pkt);
1089     return ret;
1090 }
1091
1092 static int estimate_best_b_count(MpegEncContext *s)
1093 {
1094     AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
1095     AVCodecContext *c = avcodec_alloc_context3(NULL);
1096     const int scale = s->avctx->brd_scale;
1097     int i, j, out_size, p_lambda, b_lambda, lambda2;
1098     int64_t best_rd  = INT64_MAX;
1099     int best_b_count = -1;
1100
1101     assert(scale >= 0 && scale <= 3);
1102
1103     //emms_c();
1104     //s->next_picture_ptr->quality;
1105     p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1106     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1107     b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1108     if (!b_lambda) // FIXME we should do this somewhere else
1109         b_lambda = p_lambda;
1110     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1111                FF_LAMBDA_SHIFT;
1112
1113     c->width        = s->width  >> scale;
1114     c->height       = s->height >> scale;
1115     c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;
1116     c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;
1117     c->mb_decision  = s->avctx->mb_decision;
1118     c->me_cmp       = s->avctx->me_cmp;
1119     c->mb_cmp       = s->avctx->mb_cmp;
1120     c->me_sub_cmp   = s->avctx->me_sub_cmp;
1121     c->pix_fmt      = AV_PIX_FMT_YUV420P;
1122     c->time_base    = s->avctx->time_base;
1123     c->max_b_frames = s->max_b_frames;
1124
1125     if (avcodec_open2(c, codec, NULL) < 0)
1126         return -1;
1127
1128     for (i = 0; i < s->max_b_frames + 2; i++) {
1129         Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1130                                                 s->next_picture_ptr;
1131
1132         if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1133             pre_input = *pre_input_ptr;
1134
1135             if (!pre_input.shared && i) {
1136                 pre_input.f->data[0] += INPLACE_OFFSET;
1137                 pre_input.f->data[1] += INPLACE_OFFSET;
1138                 pre_input.f->data[2] += INPLACE_OFFSET;
1139             }
1140
1141             s->dsp.shrink[scale](s->tmp_frames[i]->data[0], s->tmp_frames[i]->linesize[0],
1142                                  pre_input.f->data[0], pre_input.f->linesize[0],
1143                                  c->width,      c->height);
1144             s->dsp.shrink[scale](s->tmp_frames[i]->data[1], s->tmp_frames[i]->linesize[1],
1145                                  pre_input.f->data[1], pre_input.f->linesize[1],
1146                                  c->width >> 1, c->height >> 1);
1147             s->dsp.shrink[scale](s->tmp_frames[i]->data[2], s->tmp_frames[i]->linesize[2],
1148                                  pre_input.f->data[2], pre_input.f->linesize[2],
1149                                  c->width >> 1, c->height >> 1);
1150         }
1151     }
1152
1153     for (j = 0; j < s->max_b_frames + 1; j++) {
1154         int64_t rd = 0;
1155
1156         if (!s->input_picture[j])
1157             break;
1158
1159         c->error[0] = c->error[1] = c->error[2] = 0;
1160
1161         s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1162         s->tmp_frames[0]->quality   = 1 * FF_QP2LAMBDA;
1163
1164         out_size = encode_frame(c, s->tmp_frames[0]);
1165
1166         //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1167
1168         for (i = 0; i < s->max_b_frames + 1; i++) {
1169             int is_p = i % (j + 1) == j || i == s->max_b_frames;
1170
1171             s->tmp_frames[i + 1]->pict_type = is_p ?
1172                                      AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1173             s->tmp_frames[i + 1]->quality   = is_p ? p_lambda : b_lambda;
1174
1175             out_size = encode_frame(c, s->tmp_frames[i + 1]);
1176
1177             rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1178         }
1179
1180         /* get the delayed frames */
1181         while (out_size) {
1182             out_size = encode_frame(c, NULL);
1183             rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1184         }
1185
1186         rd += c->error[0] + c->error[1] + c->error[2];
1187
1188         if (rd < best_rd) {
1189             best_rd = rd;
1190             best_b_count = j;
1191         }
1192     }
1193
1194     avcodec_close(c);
1195     av_freep(&c);
1196
1197     return best_b_count;
1198 }
1199
1200 static int select_input_picture(MpegEncContext *s)
1201 {
1202     int i, ret;
1203
1204     for (i = 1; i < MAX_PICTURE_COUNT; i++)
1205         s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1206     s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1207
1208     /* set next picture type & ordering */
1209     if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1210         if (/*s->picture_in_gop_number >= s->gop_size ||*/
1211             s->next_picture_ptr == NULL || s->intra_only) {
1212             s->reordered_input_picture[0] = s->input_picture[0];
1213             s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1214             s->reordered_input_picture[0]->f->coded_picture_number =
1215                 s->coded_picture_number++;
1216         } else {
1217             int b_frames;
1218
1219             if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1220                 if (s->picture_in_gop_number < s->gop_size &&
1221                     skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1222                     // FIXME check that te gop check above is +-1 correct
1223                     av_frame_unref(s->input_picture[0]->f);
1224
1225                     emms_c();
1226                     ff_vbv_update(s, 0);
1227
1228                     goto no_output_pic;
1229                 }
1230             }
1231
1232             if (s->flags & CODEC_FLAG_PASS2) {
1233                 for (i = 0; i < s->max_b_frames + 1; i++) {
1234                     int pict_num = s->input_picture[0]->f->display_picture_number + i;
1235
1236                     if (pict_num >= s->rc_context.num_entries)
1237                         break;
1238                     if (!s->input_picture[i]) {
1239                         s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1240                         break;
1241                     }
1242
1243                     s->input_picture[i]->f->pict_type =
1244                         s->rc_context.entry[pict_num].new_pict_type;
1245                 }
1246             }
1247
1248             if (s->avctx->b_frame_strategy == 0) {
1249                 b_frames = s->max_b_frames;
1250                 while (b_frames && !s->input_picture[b_frames])
1251                     b_frames--;
1252             } else if (s->avctx->b_frame_strategy == 1) {
1253                 for (i = 1; i < s->max_b_frames + 1; i++) {
1254                     if (s->input_picture[i] &&
1255                         s->input_picture[i]->b_frame_score == 0) {
1256                         s->input_picture[i]->b_frame_score =
1257                             get_intra_count(s,
1258                                             s->input_picture[i    ]->f->data[0],
1259                                             s->input_picture[i - 1]->f->data[0],
1260                                             s->linesize) + 1;
1261                     }
1262                 }
1263                 for (i = 0; i < s->max_b_frames + 1; i++) {
1264                     if (s->input_picture[i] == NULL ||
1265                         s->input_picture[i]->b_frame_score - 1 >
1266                             s->mb_num / s->avctx->b_sensitivity)
1267                         break;
1268                 }
1269
1270                 b_frames = FFMAX(0, i - 1);
1271
1272                 /* reset scores */
1273                 for (i = 0; i < b_frames + 1; i++) {
1274                     s->input_picture[i]->b_frame_score = 0;
1275                 }
1276             } else if (s->avctx->b_frame_strategy == 2) {
1277                 b_frames = estimate_best_b_count(s);
1278             } else {
1279                 av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1280                 b_frames = 0;
1281             }
1282
1283             emms_c();
1284
1285             for (i = b_frames - 1; i >= 0; i--) {
1286                 int type = s->input_picture[i]->f->pict_type;
1287                 if (type && type != AV_PICTURE_TYPE_B)
1288                     b_frames = i;
1289             }
1290             if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1291                 b_frames == s->max_b_frames) {
1292                 av_log(s->avctx, AV_LOG_ERROR,
1293                        "warning, too many b frames in a row\n");
1294             }
1295
1296             if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1297                 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1298                     s->gop_size > s->picture_in_gop_number) {
1299                     b_frames = s->gop_size - s->picture_in_gop_number - 1;
1300                 } else {
1301                     if (s->flags & CODEC_FLAG_CLOSED_GOP)
1302                         b_frames = 0;
1303                     s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1304                 }
1305             }
1306
1307             if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1308                 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1309                 b_frames--;
1310
1311             s->reordered_input_picture[0] = s->input_picture[b_frames];
1312             if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1313                 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1314             s->reordered_input_picture[0]->f->coded_picture_number =
1315                 s->coded_picture_number++;
1316             for (i = 0; i < b_frames; i++) {
1317                 s->reordered_input_picture[i + 1] = s->input_picture[i];
1318                 s->reordered_input_picture[i + 1]->f->pict_type =
1319                     AV_PICTURE_TYPE_B;
1320                 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1321                     s->coded_picture_number++;
1322             }
1323         }
1324     }
1325 no_output_pic:
1326     if (s->reordered_input_picture[0]) {
1327         s->reordered_input_picture[0]->reference =
1328            s->reordered_input_picture[0]->f->pict_type !=
1329                AV_PICTURE_TYPE_B ? 3 : 0;
1330
1331         ff_mpeg_unref_picture(s, &s->new_picture);
1332         if ((ret = ff_mpeg_ref_picture(s, &s->new_picture, s->reordered_input_picture[0])))
1333             return ret;
1334
1335         if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1336             // input is a shared pix, so we can't modifiy it -> alloc a new
1337             // one & ensure that the shared one is reuseable
1338
1339             Picture *pic;
1340             int i = ff_find_unused_picture(s, 0);
1341             if (i < 0)
1342                 return i;
1343             pic = &s->picture[i];
1344
1345             pic->reference = s->reordered_input_picture[0]->reference;
1346             if (ff_alloc_picture(s, pic, 0) < 0) {
1347                 return -1;
1348             }
1349
1350             ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1351             if (ret < 0)
1352                 return ret;
1353
1354             /* mark us unused / free shared pic */
1355             av_frame_unref(s->reordered_input_picture[0]->f);
1356             s->reordered_input_picture[0]->shared = 0;
1357
1358             s->current_picture_ptr = pic;
1359         } else {
1360             // input is not a shared pix -> reuse buffer for current_pix
1361             s->current_picture_ptr = s->reordered_input_picture[0];
1362             for (i = 0; i < 4; i++) {
1363                 s->new_picture.f->data[i] += INPLACE_OFFSET;
1364             }
1365         }
1366         ff_mpeg_unref_picture(s, &s->current_picture);
1367         if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1368                                        s->current_picture_ptr)) < 0)
1369             return ret;
1370
1371         s->picture_number = s->new_picture.f->display_picture_number;
1372     } else {
1373         ff_mpeg_unref_picture(s, &s->new_picture);
1374     }
1375     return 0;
1376 }
1377
1378 static void frame_end(MpegEncContext *s)
1379 {
1380     int i;
1381
1382     if (s->unrestricted_mv &&
1383         s->current_picture.reference &&
1384         !s->intra_only) {
1385         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1386         int hshift = desc->log2_chroma_w;
1387         int vshift = desc->log2_chroma_h;
1388         s->dsp.draw_edges(s->current_picture.f->data[0], s->linesize,
1389                           s->h_edge_pos, s->v_edge_pos,
1390                           EDGE_WIDTH, EDGE_WIDTH,
1391                           EDGE_TOP | EDGE_BOTTOM);
1392         s->dsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
1393                           s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1394                           EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1395                           EDGE_TOP | EDGE_BOTTOM);
1396         s->dsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
1397                           s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1398                           EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1399                           EDGE_TOP | EDGE_BOTTOM);
1400     }
1401
1402     emms_c();
1403
1404     s->last_pict_type                 = s->pict_type;
1405     s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1406     if (s->pict_type!= AV_PICTURE_TYPE_B)
1407         s->last_non_b_pict_type = s->pict_type;
1408
1409     if (s->encoding) {
1410         /* release non-reference frames */
1411         for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1412             if (!s->picture[i].reference)
1413                 ff_mpeg_unref_picture(s, &s->picture[i]);
1414         }
1415     }
1416
1417     s->avctx->coded_frame = s->current_picture_ptr->f;
1418
1419 }
1420
1421 static void update_noise_reduction(MpegEncContext *s)
1422 {
1423     int intra, i;
1424
1425     for (intra = 0; intra < 2; intra++) {
1426         if (s->dct_count[intra] > (1 << 16)) {
1427             for (i = 0; i < 64; i++) {
1428                 s->dct_error_sum[intra][i] >>= 1;
1429             }
1430             s->dct_count[intra] >>= 1;
1431         }
1432
1433         for (i = 0; i < 64; i++) {
1434             s->dct_offset[intra][i] = (s->avctx->noise_reduction *
1435                                        s->dct_count[intra] +
1436                                        s->dct_error_sum[intra][i] / 2) /
1437                                       (s->dct_error_sum[intra][i] + 1);
1438         }
1439     }
1440 }
1441
1442 static int frame_start(MpegEncContext *s)
1443 {
1444     int ret;
1445
1446     /* mark & release old frames */
1447     if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1448         s->last_picture_ptr != s->next_picture_ptr &&
1449         s->last_picture_ptr->f->buf[0]) {
1450         ff_mpeg_unref_picture(s, s->last_picture_ptr);
1451     }
1452
1453     s->current_picture_ptr->f->pict_type = s->pict_type;
1454     s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1455
1456     ff_mpeg_unref_picture(s, &s->current_picture);
1457     if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1458                                    s->current_picture_ptr)) < 0)
1459         return ret;
1460
1461     if (s->pict_type != AV_PICTURE_TYPE_B) {
1462         s->last_picture_ptr = s->next_picture_ptr;
1463         if (!s->droppable)
1464             s->next_picture_ptr = s->current_picture_ptr;
1465     }
1466
1467     if (s->last_picture_ptr) {
1468         ff_mpeg_unref_picture(s, &s->last_picture);
1469         if (s->last_picture_ptr->f->buf[0] &&
1470             (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1471                                        s->last_picture_ptr)) < 0)
1472             return ret;
1473     }
1474     if (s->next_picture_ptr) {
1475         ff_mpeg_unref_picture(s, &s->next_picture);
1476         if (s->next_picture_ptr->f->buf[0] &&
1477             (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1478                                        s->next_picture_ptr)) < 0)
1479             return ret;
1480     }
1481
1482     if (s->picture_structure!= PICT_FRAME) {
1483         int i;
1484         for (i = 0; i < 4; i++) {
1485             if (s->picture_structure == PICT_BOTTOM_FIELD) {
1486                 s->current_picture.f->data[i] +=
1487                     s->current_picture.f->linesize[i];
1488             }
1489             s->current_picture.f->linesize[i] *= 2;
1490             s->last_picture.f->linesize[i]    *= 2;
1491             s->next_picture.f->linesize[i]    *= 2;
1492         }
1493     }
1494
1495     if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1496         s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1497         s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1498     } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1499         s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1500         s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1501     } else {
1502         s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1503         s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1504     }
1505
1506     if (s->dct_error_sum) {
1507         assert(s->avctx->noise_reduction && s->encoding);
1508         update_noise_reduction(s);
1509     }
1510
1511     return 0;
1512 }
1513
1514 int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1515                           const AVFrame *pic_arg, int *got_packet)
1516 {
1517     MpegEncContext *s = avctx->priv_data;
1518     int i, stuffing_count, ret;
1519     int context_count = s->slice_context_count;
1520
1521     s->picture_in_gop_number++;
1522
1523     if (load_input_picture(s, pic_arg) < 0)
1524         return -1;
1525
1526     if (select_input_picture(s) < 0) {
1527         return -1;
1528     }
1529
1530     /* output? */
1531     if (s->new_picture.f->data[0]) {
1532         if (!pkt->data &&
1533             (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
1534             return ret;
1535         if (s->mb_info) {
1536             s->mb_info_ptr = av_packet_new_side_data(pkt,
1537                                  AV_PKT_DATA_H263_MB_INFO,
1538                                  s->mb_width*s->mb_height*12);
1539             s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1540         }
1541
1542         for (i = 0; i < context_count; i++) {
1543             int start_y = s->thread_context[i]->start_mb_y;
1544             int   end_y = s->thread_context[i]->  end_mb_y;
1545             int h       = s->mb_height;
1546             uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1547             uint8_t *end   = pkt->data + (size_t)(((int64_t) pkt->size) *   end_y / h);
1548
1549             init_put_bits(&s->thread_context[i]->pb, start, end - start);
1550         }
1551
1552         s->pict_type = s->new_picture.f->pict_type;
1553         //emms_c();
1554         ret = frame_start(s);
1555         if (ret < 0)
1556             return ret;
1557 vbv_retry:
1558         if (encode_picture(s, s->picture_number) < 0)
1559             return -1;
1560
1561         avctx->header_bits = s->header_bits;
1562         avctx->mv_bits     = s->mv_bits;
1563         avctx->misc_bits   = s->misc_bits;
1564         avctx->i_tex_bits  = s->i_tex_bits;
1565         avctx->p_tex_bits  = s->p_tex_bits;
1566         avctx->i_count     = s->i_count;
1567         // FIXME f/b_count in avctx
1568         avctx->p_count     = s->mb_num - s->i_count - s->skip_count;
1569         avctx->skip_count  = s->skip_count;
1570
1571         frame_end(s);
1572
1573         if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1574             ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1575
1576         if (avctx->rc_buffer_size) {
1577             RateControlContext *rcc = &s->rc_context;
1578             int max_size = rcc->buffer_index * avctx->rc_max_available_vbv_use;
1579
1580             if (put_bits_count(&s->pb) > max_size &&
1581                 s->lambda < s->avctx->lmax) {
1582                 s->next_lambda = FFMAX(s->lambda + 1, s->lambda *
1583                                        (s->qscale + 1) / s->qscale);
1584                 if (s->adaptive_quant) {
1585                     int i;
1586                     for (i = 0; i < s->mb_height * s->mb_stride; i++)
1587                         s->lambda_table[i] =
1588                             FFMAX(s->lambda_table[i] + 1,
1589                                   s->lambda_table[i] * (s->qscale + 1) /
1590                                   s->qscale);
1591                 }
1592                 s->mb_skipped = 0;        // done in frame_start()
1593                 // done in encode_picture() so we must undo it
1594                 if (s->pict_type == AV_PICTURE_TYPE_P) {
1595                     if (s->flipflop_rounding          ||
1596                         s->codec_id == AV_CODEC_ID_H263P ||
1597                         s->codec_id == AV_CODEC_ID_MPEG4)
1598                         s->no_rounding ^= 1;
1599                 }
1600                 if (s->pict_type != AV_PICTURE_TYPE_B) {
1601                     s->time_base       = s->last_time_base;
1602                     s->last_non_b_time = s->time - s->pp_time;
1603                 }
1604                 for (i = 0; i < context_count; i++) {
1605                     PutBitContext *pb = &s->thread_context[i]->pb;
1606                     init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1607                 }
1608                 goto vbv_retry;
1609             }
1610
1611             assert(s->avctx->rc_max_rate);
1612         }
1613
1614         if (s->flags & CODEC_FLAG_PASS1)
1615             ff_write_pass1_stats(s);
1616
1617         for (i = 0; i < 4; i++) {
1618             s->current_picture_ptr->f->error[i] = s->current_picture.f->error[i];
1619             avctx->error[i] += s->current_picture_ptr->f->error[i];
1620         }
1621
1622         if (s->flags & CODEC_FLAG_PASS1)
1623             assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits +
1624                    avctx->i_tex_bits + avctx->p_tex_bits ==
1625                        put_bits_count(&s->pb));
1626         flush_put_bits(&s->pb);
1627         s->frame_bits  = put_bits_count(&s->pb);
1628
1629         stuffing_count = ff_vbv_update(s, s->frame_bits);
1630         if (stuffing_count) {
1631             if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
1632                     stuffing_count + 50) {
1633                 av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1634                 return -1;
1635             }
1636
1637             switch (s->codec_id) {
1638             case AV_CODEC_ID_MPEG1VIDEO:
1639             case AV_CODEC_ID_MPEG2VIDEO:
1640                 while (stuffing_count--) {
1641                     put_bits(&s->pb, 8, 0);
1642                 }
1643             break;
1644             case AV_CODEC_ID_MPEG4:
1645                 put_bits(&s->pb, 16, 0);
1646                 put_bits(&s->pb, 16, 0x1C3);
1647                 stuffing_count -= 4;
1648                 while (stuffing_count--) {
1649                     put_bits(&s->pb, 8, 0xFF);
1650                 }
1651             break;
1652             default:
1653                 av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1654             }
1655             flush_put_bits(&s->pb);
1656             s->frame_bits  = put_bits_count(&s->pb);
1657         }
1658
1659         /* update mpeg1/2 vbv_delay for CBR */
1660         if (s->avctx->rc_max_rate                          &&
1661             s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
1662             s->out_format == FMT_MPEG1                     &&
1663             90000LL * (avctx->rc_buffer_size - 1) <=
1664                 s->avctx->rc_max_rate * 0xFFFFLL) {
1665             int vbv_delay, min_delay;
1666             double inbits  = s->avctx->rc_max_rate *
1667                              av_q2d(s->avctx->time_base);
1668             int    minbits = s->frame_bits - 8 *
1669                              (s->vbv_delay_ptr - s->pb.buf - 1);
1670             double bits    = s->rc_context.buffer_index + minbits - inbits;
1671
1672             if (bits < 0)
1673                 av_log(s->avctx, AV_LOG_ERROR,
1674                        "Internal error, negative bits\n");
1675
1676             assert(s->repeat_first_field == 0);
1677
1678             vbv_delay = bits * 90000 / s->avctx->rc_max_rate;
1679             min_delay = (minbits * 90000LL + s->avctx->rc_max_rate - 1) /
1680                         s->avctx->rc_max_rate;
1681
1682             vbv_delay = FFMAX(vbv_delay, min_delay);
1683
1684             assert(vbv_delay < 0xFFFF);
1685
1686             s->vbv_delay_ptr[0] &= 0xF8;
1687             s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1688             s->vbv_delay_ptr[1]  = vbv_delay >> 5;
1689             s->vbv_delay_ptr[2] &= 0x07;
1690             s->vbv_delay_ptr[2] |= vbv_delay << 3;
1691             avctx->vbv_delay     = vbv_delay * 300;
1692         }
1693         s->total_bits     += s->frame_bits;
1694         avctx->frame_bits  = s->frame_bits;
1695
1696         pkt->pts = s->current_picture.f->pts;
1697         if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1698             if (!s->current_picture.f->coded_picture_number)
1699                 pkt->dts = pkt->pts - s->dts_delta;
1700             else
1701                 pkt->dts = s->reordered_pts;
1702             s->reordered_pts = pkt->pts;
1703         } else
1704             pkt->dts = pkt->pts;
1705         if (s->current_picture.f->key_frame)
1706             pkt->flags |= AV_PKT_FLAG_KEY;
1707         if (s->mb_info)
1708             av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1709     } else {
1710         s->frame_bits = 0;
1711     }
1712     assert((s->frame_bits & 7) == 0);
1713
1714     pkt->size = s->frame_bits / 8;
1715     *got_packet = !!pkt->size;
1716     return 0;
1717 }
1718
1719 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1720                                                 int n, int threshold)
1721 {
1722     static const char tab[64] = {
1723         3, 2, 2, 1, 1, 1, 1, 1,
1724         1, 1, 1, 1, 1, 1, 1, 1,
1725         1, 1, 1, 1, 1, 1, 1, 1,
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         0, 0, 0, 0, 0, 0, 0, 0
1731     };
1732     int score = 0;
1733     int run = 0;
1734     int i;
1735     int16_t *block = s->block[n];
1736     const int last_index = s->block_last_index[n];
1737     int skip_dc;
1738
1739     if (threshold < 0) {
1740         skip_dc = 0;
1741         threshold = -threshold;
1742     } else
1743         skip_dc = 1;
1744
1745     /* Are all we could set to zero already zero? */
1746     if (last_index <= skip_dc - 1)
1747         return;
1748
1749     for (i = 0; i <= last_index; i++) {
1750         const int j = s->intra_scantable.permutated[i];
1751         const int level = FFABS(block[j]);
1752         if (level == 1) {
1753             if (skip_dc && i == 0)
1754                 continue;
1755             score += tab[run];
1756             run = 0;
1757         } else if (level > 1) {
1758             return;
1759         } else {
1760             run++;
1761         }
1762     }
1763     if (score >= threshold)
1764         return;
1765     for (i = skip_dc; i <= last_index; i++) {
1766         const int j = s->intra_scantable.permutated[i];
1767         block[j] = 0;
1768     }
1769     if (block[0])
1770         s->block_last_index[n] = 0;
1771     else
1772         s->block_last_index[n] = -1;
1773 }
1774
1775 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1776                                int last_index)
1777 {
1778     int i;
1779     const int maxlevel = s->max_qcoeff;
1780     const int minlevel = s->min_qcoeff;
1781     int overflow = 0;
1782
1783     if (s->mb_intra) {
1784         i = 1; // skip clipping of intra dc
1785     } else
1786         i = 0;
1787
1788     for (; i <= last_index; i++) {
1789         const int j = s->intra_scantable.permutated[i];
1790         int level = block[j];
1791
1792         if (level > maxlevel) {
1793             level = maxlevel;
1794             overflow++;
1795         } else if (level < minlevel) {
1796             level = minlevel;
1797             overflow++;
1798         }
1799
1800         block[j] = level;
1801     }
1802
1803     if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1804         av_log(s->avctx, AV_LOG_INFO,
1805                "warning, clipping %d dct coefficients to %d..%d\n",
1806                overflow, minlevel, maxlevel);
1807 }
1808
1809 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
1810 {
1811     int x, y;
1812     // FIXME optimize
1813     for (y = 0; y < 8; y++) {
1814         for (x = 0; x < 8; x++) {
1815             int x2, y2;
1816             int sum = 0;
1817             int sqr = 0;
1818             int count = 0;
1819
1820             for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
1821                 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
1822                     int v = ptr[x2 + y2 * stride];
1823                     sum += v;
1824                     sqr += v * v;
1825                     count++;
1826                 }
1827             }
1828             weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
1829         }
1830     }
1831 }
1832
1833 static av_always_inline void encode_mb_internal(MpegEncContext *s,
1834                                                 int motion_x, int motion_y,
1835                                                 int mb_block_height,
1836                                                 int mb_block_count)
1837 {
1838     int16_t weight[8][64];
1839     int16_t orig[8][64];
1840     const int mb_x = s->mb_x;
1841     const int mb_y = s->mb_y;
1842     int i;
1843     int skip_dct[8];
1844     int dct_offset = s->linesize * 8; // default for progressive frames
1845     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1846     ptrdiff_t wrap_y, wrap_c;
1847
1848     for (i = 0; i < mb_block_count; i++)
1849         skip_dct[i] = s->skipdct;
1850
1851     if (s->adaptive_quant) {
1852         const int last_qp = s->qscale;
1853         const int mb_xy = mb_x + mb_y * s->mb_stride;
1854
1855         s->lambda = s->lambda_table[mb_xy];
1856         update_qscale(s);
1857
1858         if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1859             s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
1860             s->dquant = s->qscale - last_qp;
1861
1862             if (s->out_format == FMT_H263) {
1863                 s->dquant = av_clip(s->dquant, -2, 2);
1864
1865                 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1866                     if (!s->mb_intra) {
1867                         if (s->pict_type == AV_PICTURE_TYPE_B) {
1868                             if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1869                                 s->dquant = 0;
1870                         }
1871                         if (s->mv_type == MV_TYPE_8X8)
1872                             s->dquant = 0;
1873                     }
1874                 }
1875             }
1876         }
1877         ff_set_qscale(s, last_qp + s->dquant);
1878     } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1879         ff_set_qscale(s, s->qscale + s->dquant);
1880
1881     wrap_y = s->linesize;
1882     wrap_c = s->uvlinesize;
1883     ptr_y  = s->new_picture.f->data[0] +
1884              (mb_y * 16 * wrap_y)              + mb_x * 16;
1885     ptr_cb = s->new_picture.f->data[1] +
1886              (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1887     ptr_cr = s->new_picture.f->data[2] +
1888              (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1889
1890     if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
1891         uint8_t *ebuf = s->edge_emu_buffer + 32;
1892         s->vdsp.emulated_edge_mc(ebuf, ptr_y,
1893                                  wrap_y, wrap_y,
1894                                  16, 16, mb_x * 16, mb_y * 16,
1895                                  s->width, s->height);
1896         ptr_y = ebuf;
1897         s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
1898                                  wrap_c, wrap_c,
1899                                  8, mb_block_height, mb_x * 8, mb_y * 8,
1900                                  s->width >> 1, s->height >> 1);
1901         ptr_cb = ebuf + 18 * wrap_y;
1902         s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr,
1903                                  wrap_c, wrap_c,
1904                                  8, mb_block_height, mb_x * 8, mb_y * 8,
1905                                  s->width >> 1, s->height >> 1);
1906         ptr_cr = ebuf + 18 * wrap_y + 8;
1907     }
1908
1909     if (s->mb_intra) {
1910         if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1911             int progressive_score, interlaced_score;
1912
1913             s->interlaced_dct = 0;
1914             progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,
1915                                                     NULL, wrap_y, 8) +
1916                                 s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1917                                                     NULL, wrap_y, 8) - 400;
1918
1919             if (progressive_score > 0) {
1920                 interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,
1921                                                        NULL, wrap_y * 2, 8) +
1922                                    s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,
1923                                                        NULL, wrap_y * 2, 8);
1924                 if (progressive_score > interlaced_score) {
1925                     s->interlaced_dct = 1;
1926
1927                     dct_offset = wrap_y;
1928                     wrap_y <<= 1;
1929                     if (s->chroma_format == CHROMA_422)
1930                         wrap_c <<= 1;
1931                 }
1932             }
1933         }
1934
1935         s->dsp.get_pixels(s->block[0], ptr_y                  , wrap_y);
1936         s->dsp.get_pixels(s->block[1], ptr_y              + 8 , wrap_y);
1937         s->dsp.get_pixels(s->block[2], ptr_y + dct_offset     , wrap_y);
1938         s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);
1939
1940         if (s->flags & CODEC_FLAG_GRAY) {
1941             skip_dct[4] = 1;
1942             skip_dct[5] = 1;
1943         } else {
1944             s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1945             s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1946             if (!s->chroma_y_shift) { /* 422 */
1947                 s->dsp.get_pixels(s->block[6],
1948                                   ptr_cb + (dct_offset >> 1), wrap_c);
1949                 s->dsp.get_pixels(s->block[7],
1950                                   ptr_cr + (dct_offset >> 1), wrap_c);
1951             }
1952         }
1953     } else {
1954         op_pixels_func (*op_pix)[4];
1955         qpel_mc_func (*op_qpix)[16];
1956         uint8_t *dest_y, *dest_cb, *dest_cr;
1957
1958         dest_y  = s->dest[0];
1959         dest_cb = s->dest[1];
1960         dest_cr = s->dest[2];
1961
1962         if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1963             op_pix  = s->hdsp.put_pixels_tab;
1964             op_qpix = s->qdsp.put_qpel_pixels_tab;
1965         } else {
1966             op_pix  = s->hdsp.put_no_rnd_pixels_tab;
1967             op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
1968         }
1969
1970         if (s->mv_dir & MV_DIR_FORWARD) {
1971             ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
1972                           s->last_picture.f->data,
1973                           op_pix, op_qpix);
1974             op_pix  = s->hdsp.avg_pixels_tab;
1975             op_qpix = s->qdsp.avg_qpel_pixels_tab;
1976         }
1977         if (s->mv_dir & MV_DIR_BACKWARD) {
1978             ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1,
1979                           s->next_picture.f->data,
1980                           op_pix, op_qpix);
1981         }
1982
1983         if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1984             int progressive_score, interlaced_score;
1985
1986             s->interlaced_dct = 0;
1987             progressive_score = s->dsp.ildct_cmp[0](s, dest_y,
1988                                                     ptr_y,              wrap_y,
1989                                                     8) +
1990                                 s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,
1991                                                     ptr_y + wrap_y * 8, wrap_y,
1992                                                     8) - 400;
1993
1994             if (s->avctx->ildct_cmp == FF_CMP_VSSE)
1995                 progressive_score -= 400;
1996
1997             if (progressive_score > 0) {
1998                 interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,
1999                                                        ptr_y,
2000                                                        wrap_y * 2, 8) +
2001                                    s->dsp.ildct_cmp[0](s, dest_y + wrap_y,
2002                                                        ptr_y + wrap_y,
2003                                                        wrap_y * 2, 8);
2004
2005                 if (progressive_score > interlaced_score) {
2006                     s->interlaced_dct = 1;
2007
2008                     dct_offset = wrap_y;
2009                     wrap_y <<= 1;
2010                     if (s->chroma_format == CHROMA_422)
2011                         wrap_c <<= 1;
2012                 }
2013             }
2014         }
2015
2016         s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2017         s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2018         s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2019                            dest_y + dct_offset, wrap_y);
2020         s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2021                            dest_y + dct_offset + 8, wrap_y);
2022
2023         if (s->flags & CODEC_FLAG_GRAY) {
2024             skip_dct[4] = 1;
2025             skip_dct[5] = 1;
2026         } else {
2027             s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2028             s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2029             if (!s->chroma_y_shift) { /* 422 */
2030                 s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
2031                                    dest_cb + (dct_offset >> 1), wrap_c);
2032                 s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
2033                                    dest_cr + (dct_offset >> 1), wrap_c);
2034             }
2035         }
2036         /* pre quantization */
2037         if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2038                 2 * s->qscale * s->qscale) {
2039             // FIXME optimize
2040             if (s->dsp.sad[1](NULL, ptr_y , dest_y,
2041                               wrap_y, 8) < 20 * s->qscale)
2042                 skip_dct[0] = 1;
2043             if (s->dsp.sad[1](NULL, ptr_y + 8,
2044                               dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2045                 skip_dct[1] = 1;
2046             if (s->dsp.sad[1](NULL, ptr_y + dct_offset,
2047                               dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)
2048                 skip_dct[2] = 1;
2049             if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,
2050                               dest_y + dct_offset + 8,
2051                               wrap_y, 8) < 20 * s->qscale)
2052                 skip_dct[3] = 1;
2053             if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,
2054                               wrap_c, 8) < 20 * s->qscale)
2055                 skip_dct[4] = 1;
2056             if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,
2057                               wrap_c, 8) < 20 * s->qscale)
2058                 skip_dct[5] = 1;
2059             if (!s->chroma_y_shift) { /* 422 */
2060                 if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),
2061                                   dest_cb + (dct_offset >> 1),
2062                                   wrap_c, 8) < 20 * s->qscale)
2063                     skip_dct[6] = 1;
2064                 if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),
2065                                   dest_cr + (dct_offset >> 1),
2066                                   wrap_c, 8) < 20 * s->qscale)
2067                     skip_dct[7] = 1;
2068             }
2069         }
2070     }
2071
2072     if (s->quantizer_noise_shaping) {
2073         if (!skip_dct[0])
2074             get_visual_weight(weight[0], ptr_y                 , wrap_y);
2075         if (!skip_dct[1])
2076             get_visual_weight(weight[1], ptr_y              + 8, wrap_y);
2077         if (!skip_dct[2])
2078             get_visual_weight(weight[2], ptr_y + dct_offset    , wrap_y);
2079         if (!skip_dct[3])
2080             get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2081         if (!skip_dct[4])
2082             get_visual_weight(weight[4], ptr_cb                , wrap_c);
2083         if (!skip_dct[5])
2084             get_visual_weight(weight[5], ptr_cr                , wrap_c);
2085         if (!s->chroma_y_shift) { /* 422 */
2086             if (!skip_dct[6])
2087                 get_visual_weight(weight[6], ptr_cb + (dct_offset >> 1),
2088                                   wrap_c);
2089             if (!skip_dct[7])
2090                 get_visual_weight(weight[7], ptr_cr + (dct_offset >> 1),
2091                                   wrap_c);
2092         }
2093         memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2094     }
2095
2096     /* DCT & quantize */
2097     assert(s->out_format != FMT_MJPEG || s->qscale == 8);
2098     {
2099         for (i = 0; i < mb_block_count; i++) {
2100             if (!skip_dct[i]) {
2101                 int overflow;
2102                 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2103                 // FIXME we could decide to change to quantizer instead of
2104                 // clipping
2105                 // JS: I don't think that would be a good idea it could lower
2106                 //     quality instead of improve it. Just INTRADC clipping
2107                 //     deserves changes in quantizer
2108                 if (overflow)
2109                     clip_coeffs(s, s->block[i], s->block_last_index[i]);
2110             } else
2111                 s->block_last_index[i] = -1;
2112         }
2113         if (s->quantizer_noise_shaping) {
2114             for (i = 0; i < mb_block_count; i++) {
2115                 if (!skip_dct[i]) {
2116                     s->block_last_index[i] =
2117                         dct_quantize_refine(s, s->block[i], weight[i],
2118                                             orig[i], i, s->qscale);
2119                 }
2120             }
2121         }
2122
2123         if (s->luma_elim_threshold && !s->mb_intra)
2124             for (i = 0; i < 4; i++)
2125                 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2126         if (s->chroma_elim_threshold && !s->mb_intra)
2127             for (i = 4; i < mb_block_count; i++)
2128                 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2129
2130         if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2131             for (i = 0; i < mb_block_count; i++) {
2132                 if (s->block_last_index[i] == -1)
2133                     s->coded_score[i] = INT_MAX / 256;
2134             }
2135         }
2136     }
2137
2138     if ((s->flags & CODEC_FLAG_GRAY) && s->mb_intra) {
2139         s->block_last_index[4] =
2140         s->block_last_index[5] = 0;
2141         s->block[4][0] =
2142         s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2143     }
2144
2145     // non c quantize code returns incorrect block_last_index FIXME
2146     if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2147         for (i = 0; i < mb_block_count; i++) {
2148             int j;
2149             if (s->block_last_index[i] > 0) {
2150                 for (j = 63; j > 0; j--) {
2151                     if (s->block[i][s->intra_scantable.permutated[j]])
2152                         break;
2153                 }
2154                 s->block_last_index[i] = j;
2155             }
2156         }
2157     }
2158
2159     /* huffman encode */
2160     switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2161     case AV_CODEC_ID_MPEG1VIDEO:
2162     case AV_CODEC_ID_MPEG2VIDEO:
2163         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2164             ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2165         break;
2166     case AV_CODEC_ID_MPEG4:
2167         if (CONFIG_MPEG4_ENCODER)
2168             ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2169         break;
2170     case AV_CODEC_ID_MSMPEG4V2:
2171     case AV_CODEC_ID_MSMPEG4V3:
2172     case AV_CODEC_ID_WMV1:
2173         if (CONFIG_MSMPEG4_ENCODER)
2174             ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2175         break;
2176     case AV_CODEC_ID_WMV2:
2177         if (CONFIG_WMV2_ENCODER)
2178             ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2179         break;
2180     case AV_CODEC_ID_H261:
2181         if (CONFIG_H261_ENCODER)
2182             ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2183         break;
2184     case AV_CODEC_ID_H263:
2185     case AV_CODEC_ID_H263P:
2186     case AV_CODEC_ID_FLV1:
2187     case AV_CODEC_ID_RV10:
2188     case AV_CODEC_ID_RV20:
2189         if (CONFIG_H263_ENCODER)
2190             ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2191         break;
2192     case AV_CODEC_ID_MJPEG:
2193         if (CONFIG_MJPEG_ENCODER)
2194             ff_mjpeg_encode_mb(s, s->block);
2195         break;
2196     default:
2197         assert(0);
2198     }
2199 }
2200
2201 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2202 {
2203     if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 6);
2204     else                                encode_mb_internal(s, motion_x, motion_y, 16, 8);
2205 }
2206
2207 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2208     int i;
2209
2210     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2211
2212     /* mpeg1 */
2213     d->mb_skip_run= s->mb_skip_run;
2214     for(i=0; i<3; i++)
2215         d->last_dc[i] = s->last_dc[i];
2216
2217     /* statistics */
2218     d->mv_bits= s->mv_bits;
2219     d->i_tex_bits= s->i_tex_bits;
2220     d->p_tex_bits= s->p_tex_bits;
2221     d->i_count= s->i_count;
2222     d->f_count= s->f_count;
2223     d->b_count= s->b_count;
2224     d->skip_count= s->skip_count;
2225     d->misc_bits= s->misc_bits;
2226     d->last_bits= 0;
2227
2228     d->mb_skipped= 0;
2229     d->qscale= s->qscale;
2230     d->dquant= s->dquant;
2231
2232     d->esc3_level_length= s->esc3_level_length;
2233 }
2234
2235 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2236     int i;
2237
2238     memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2239     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2240
2241     /* mpeg1 */
2242     d->mb_skip_run= s->mb_skip_run;
2243     for(i=0; i<3; i++)
2244         d->last_dc[i] = s->last_dc[i];
2245
2246     /* statistics */
2247     d->mv_bits= s->mv_bits;
2248     d->i_tex_bits= s->i_tex_bits;
2249     d->p_tex_bits= s->p_tex_bits;
2250     d->i_count= s->i_count;
2251     d->f_count= s->f_count;
2252     d->b_count= s->b_count;
2253     d->skip_count= s->skip_count;
2254     d->misc_bits= s->misc_bits;
2255
2256     d->mb_intra= s->mb_intra;
2257     d->mb_skipped= s->mb_skipped;
2258     d->mv_type= s->mv_type;
2259     d->mv_dir= s->mv_dir;
2260     d->pb= s->pb;
2261     if(s->data_partitioning){
2262         d->pb2= s->pb2;
2263         d->tex_pb= s->tex_pb;
2264     }
2265     d->block= s->block;
2266     for(i=0; i<8; i++)
2267         d->block_last_index[i]= s->block_last_index[i];
2268     d->interlaced_dct= s->interlaced_dct;
2269     d->qscale= s->qscale;
2270
2271     d->esc3_level_length= s->esc3_level_length;
2272 }
2273
2274 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2275                            PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2276                            int *dmin, int *next_block, int motion_x, int motion_y)
2277 {
2278     int score;
2279     uint8_t *dest_backup[3];
2280
2281     copy_context_before_encode(s, backup, type);
2282
2283     s->block= s->blocks[*next_block];
2284     s->pb= pb[*next_block];
2285     if(s->data_partitioning){
2286         s->pb2   = pb2   [*next_block];
2287         s->tex_pb= tex_pb[*next_block];
2288     }
2289
2290     if(*next_block){
2291         memcpy(dest_backup, s->dest, sizeof(s->dest));
2292         s->dest[0] = s->rd_scratchpad;
2293         s->dest[1] = s->rd_scratchpad + 16*s->linesize;
2294         s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
2295         assert(s->linesize >= 32); //FIXME
2296     }
2297
2298     encode_mb(s, motion_x, motion_y);
2299
2300     score= put_bits_count(&s->pb);
2301     if(s->data_partitioning){
2302         score+= put_bits_count(&s->pb2);
2303         score+= put_bits_count(&s->tex_pb);
2304     }
2305
2306     if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2307         ff_MPV_decode_mb(s, s->block);
2308
2309         score *= s->lambda2;
2310         score += sse_mb(s) << FF_LAMBDA_SHIFT;
2311     }
2312
2313     if(*next_block){
2314         memcpy(s->dest, dest_backup, sizeof(s->dest));
2315     }
2316
2317     if(score<*dmin){
2318         *dmin= score;
2319         *next_block^=1;
2320
2321         copy_context_after_encode(best, s, type);
2322     }
2323 }
2324
2325 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2326     uint32_t *sq = ff_square_tab + 256;
2327     int acc=0;
2328     int x,y;
2329
2330     if(w==16 && h==16)
2331         return s->dsp.sse[0](NULL, src1, src2, stride, 16);
2332     else if(w==8 && h==8)
2333         return s->dsp.sse[1](NULL, src1, src2, stride, 8);
2334
2335     for(y=0; y<h; y++){
2336         for(x=0; x<w; x++){
2337             acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2338         }
2339     }
2340
2341     assert(acc>=0);
2342
2343     return acc;
2344 }
2345
2346 static int sse_mb(MpegEncContext *s){
2347     int w= 16;
2348     int h= 16;
2349
2350     if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2351     if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2352
2353     if(w==16 && h==16)
2354       if(s->avctx->mb_cmp == FF_CMP_NSSE){
2355         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)
2356                +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)
2357                +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);
2358       }else{
2359         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)
2360                +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)
2361                +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);
2362       }
2363     else
2364         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)
2365                +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)
2366                +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);
2367 }
2368
2369 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2370     MpegEncContext *s= *(void**)arg;
2371
2372
2373     s->me.pre_pass=1;
2374     s->me.dia_size= s->avctx->pre_dia_size;
2375     s->first_slice_line=1;
2376     for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2377         for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2378             ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2379         }
2380         s->first_slice_line=0;
2381     }
2382
2383     s->me.pre_pass=0;
2384
2385     return 0;
2386 }
2387
2388 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2389     MpegEncContext *s= *(void**)arg;
2390
2391     s->me.dia_size= s->avctx->dia_size;
2392     s->first_slice_line=1;
2393     for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2394         s->mb_x=0; //for block init below
2395         ff_init_block_index(s);
2396         for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2397             s->block_index[0]+=2;
2398             s->block_index[1]+=2;
2399             s->block_index[2]+=2;
2400             s->block_index[3]+=2;
2401
2402             /* compute motion vector & mb_type and store in context */
2403             if(s->pict_type==AV_PICTURE_TYPE_B)
2404                 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2405             else
2406                 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2407         }
2408         s->first_slice_line=0;
2409     }
2410     return 0;
2411 }
2412
2413 static int mb_var_thread(AVCodecContext *c, void *arg){
2414     MpegEncContext *s= *(void**)arg;
2415     int mb_x, mb_y;
2416
2417     for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2418         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2419             int xx = mb_x * 16;
2420             int yy = mb_y * 16;
2421             uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2422             int varc;
2423             int sum = s->dsp.pix_sum(pix, s->linesize);
2424
2425             varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
2426
2427             s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2428             s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2429             s->me.mb_var_sum_temp    += varc;
2430         }
2431     }
2432     return 0;
2433 }
2434
2435 static void write_slice_end(MpegEncContext *s){
2436     if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2437         if(s->partitioned_frame){
2438             ff_mpeg4_merge_partitions(s);
2439         }
2440
2441         ff_mpeg4_stuffing(&s->pb);
2442     }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2443         ff_mjpeg_encode_stuffing(&s->pb);
2444     }
2445
2446     avpriv_align_put_bits(&s->pb);
2447     flush_put_bits(&s->pb);
2448
2449     if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2450         s->misc_bits+= get_bits_diff(s);
2451 }
2452
2453 static void write_mb_info(MpegEncContext *s)
2454 {
2455     uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2456     int offset = put_bits_count(&s->pb);
2457     int mba  = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2458     int gobn = s->mb_y / s->gob_index;
2459     int pred_x, pred_y;
2460     if (CONFIG_H263_ENCODER)
2461         ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2462     bytestream_put_le32(&ptr, offset);
2463     bytestream_put_byte(&ptr, s->qscale);
2464     bytestream_put_byte(&ptr, gobn);
2465     bytestream_put_le16(&ptr, mba);
2466     bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2467     bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2468     /* 4MV not implemented */
2469     bytestream_put_byte(&ptr, 0); /* hmv2 */
2470     bytestream_put_byte(&ptr, 0); /* vmv2 */
2471 }
2472
2473 static void update_mb_info(MpegEncContext *s, int startcode)
2474 {
2475     if (!s->mb_info)
2476         return;
2477     if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
2478         s->mb_info_size += 12;
2479         s->prev_mb_info = s->last_mb_info;
2480     }
2481     if (startcode) {
2482         s->prev_mb_info = put_bits_count(&s->pb)/8;
2483         /* This might have incremented mb_info_size above, and we return without
2484          * actually writing any info into that slot yet. But in that case,
2485          * this will be called again at the start of the after writing the
2486          * start code, actually writing the mb info. */
2487         return;
2488     }
2489
2490     s->last_mb_info = put_bits_count(&s->pb)/8;
2491     if (!s->mb_info_size)
2492         s->mb_info_size += 12;
2493     write_mb_info(s);
2494 }
2495
2496 static int encode_thread(AVCodecContext *c, void *arg){
2497     MpegEncContext *s= *(void**)arg;
2498     int mb_x, mb_y, pdif = 0;
2499     int chr_h= 16>>s->chroma_y_shift;
2500     int i, j;
2501     MpegEncContext best_s, backup_s;
2502     uint8_t bit_buf[2][MAX_MB_BYTES];
2503     uint8_t bit_buf2[2][MAX_MB_BYTES];
2504     uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2505     PutBitContext pb[2], pb2[2], tex_pb[2];
2506
2507     for(i=0; i<2; i++){
2508         init_put_bits(&pb    [i], bit_buf    [i], MAX_MB_BYTES);
2509         init_put_bits(&pb2   [i], bit_buf2   [i], MAX_MB_BYTES);
2510         init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2511     }
2512
2513     s->last_bits= put_bits_count(&s->pb);
2514     s->mv_bits=0;
2515     s->misc_bits=0;
2516     s->i_tex_bits=0;
2517     s->p_tex_bits=0;
2518     s->i_count=0;
2519     s->f_count=0;
2520     s->b_count=0;
2521     s->skip_count=0;
2522
2523     for(i=0; i<3; i++){
2524         /* init last dc values */
2525         /* note: quant matrix value (8) is implied here */
2526         s->last_dc[i] = 128 << s->intra_dc_precision;
2527
2528         s->current_picture.f->error[i] = 0;
2529     }
2530     s->mb_skip_run = 0;
2531     memset(s->last_mv, 0, sizeof(s->last_mv));
2532
2533     s->last_mv_dir = 0;
2534
2535     switch(s->codec_id){
2536     case AV_CODEC_ID_H263:
2537     case AV_CODEC_ID_H263P:
2538     case AV_CODEC_ID_FLV1:
2539         if (CONFIG_H263_ENCODER)
2540             s->gob_index = ff_h263_get_gob_height(s);
2541         break;
2542     case AV_CODEC_ID_MPEG4:
2543         if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2544             ff_mpeg4_init_partitions(s);
2545         break;
2546     }
2547
2548     s->resync_mb_x=0;
2549     s->resync_mb_y=0;
2550     s->first_slice_line = 1;
2551     s->ptr_lastgob = s->pb.buf;
2552     for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2553         s->mb_x=0;
2554         s->mb_y= mb_y;
2555
2556         ff_set_qscale(s, s->qscale);
2557         ff_init_block_index(s);
2558
2559         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2560             int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2561             int mb_type= s->mb_type[xy];
2562 //            int d;
2563             int dmin= INT_MAX;
2564             int dir;
2565
2566             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2567                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2568                 return -1;
2569             }
2570             if(s->data_partitioning){
2571                 if(   s->pb2   .buf_end - s->pb2   .buf - (put_bits_count(&s->    pb2)>>3) < MAX_MB_BYTES
2572                    || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2573                     av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2574                     return -1;
2575                 }
2576             }
2577
2578             s->mb_x = mb_x;
2579             s->mb_y = mb_y;  // moved into loop, can get changed by H.261
2580             ff_update_block_index(s);
2581
2582             if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2583                 ff_h261_reorder_mb_index(s);
2584                 xy= s->mb_y*s->mb_stride + s->mb_x;
2585                 mb_type= s->mb_type[xy];
2586             }
2587
2588             /* write gob / video packet header  */
2589             if(s->rtp_mode){
2590                 int current_packet_size, is_gob_start;
2591
2592                 current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2593
2594                 is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2595
2596                 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2597
2598                 switch(s->codec_id){
2599                 case AV_CODEC_ID_H263:
2600                 case AV_CODEC_ID_H263P:
2601                     if(!s->h263_slice_structured)
2602                         if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2603                     break;
2604                 case AV_CODEC_ID_MPEG2VIDEO:
2605                     if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2606                 case AV_CODEC_ID_MPEG1VIDEO:
2607                     if(s->mb_skip_run) is_gob_start=0;
2608                     break;
2609                 }
2610
2611                 if(is_gob_start){
2612                     if(s->start_mb_y != mb_y || mb_x!=0){
2613                         write_slice_end(s);
2614
2615                         if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2616                             ff_mpeg4_init_partitions(s);
2617                         }
2618                     }
2619
2620                     assert((put_bits_count(&s->pb)&7) == 0);
2621                     current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2622
2623                     if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2624                         int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2625                         int d = 100 / s->error_rate;
2626                         if(r % d == 0){
2627                             current_packet_size=0;
2628                             s->pb.buf_ptr= s->ptr_lastgob;
2629                             assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2630                         }
2631                     }
2632
2633                     if (s->avctx->rtp_callback){
2634                         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2635                         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2636                     }
2637                     update_mb_info(s, 1);
2638
2639                     switch(s->codec_id){
2640                     case AV_CODEC_ID_MPEG4:
2641                         if (CONFIG_MPEG4_ENCODER) {
2642                             ff_mpeg4_encode_video_packet_header(s);
2643                             ff_mpeg4_clean_buffers(s);
2644                         }
2645                     break;
2646                     case AV_CODEC_ID_MPEG1VIDEO:
2647                     case AV_CODEC_ID_MPEG2VIDEO:
2648                         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2649                             ff_mpeg1_encode_slice_header(s);
2650                             ff_mpeg1_clean_buffers(s);
2651                         }
2652                     break;
2653                     case AV_CODEC_ID_H263:
2654                     case AV_CODEC_ID_H263P:
2655                         if (CONFIG_H263_ENCODER)
2656                             ff_h263_encode_gob_header(s, mb_y);
2657                     break;
2658                     }
2659
2660                     if(s->flags&CODEC_FLAG_PASS1){
2661                         int bits= put_bits_count(&s->pb);
2662                         s->misc_bits+= bits - s->last_bits;
2663                         s->last_bits= bits;
2664                     }
2665
2666                     s->ptr_lastgob += current_packet_size;
2667                     s->first_slice_line=1;
2668                     s->resync_mb_x=mb_x;
2669                     s->resync_mb_y=mb_y;
2670                 }
2671             }
2672
2673             if(  (s->resync_mb_x   == s->mb_x)
2674                && s->resync_mb_y+1 == s->mb_y){
2675                 s->first_slice_line=0;
2676             }
2677
2678             s->mb_skipped=0;
2679             s->dquant=0; //only for QP_RD
2680
2681             update_mb_info(s, 0);
2682
2683             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
2684                 int next_block=0;
2685                 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2686
2687                 copy_context_before_encode(&backup_s, s, -1);
2688                 backup_s.pb= s->pb;
2689                 best_s.data_partitioning= s->data_partitioning;
2690                 best_s.partitioned_frame= s->partitioned_frame;
2691                 if(s->data_partitioning){
2692                     backup_s.pb2= s->pb2;
2693                     backup_s.tex_pb= s->tex_pb;
2694                 }
2695
2696                 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2697                     s->mv_dir = MV_DIR_FORWARD;
2698                     s->mv_type = MV_TYPE_16X16;
2699                     s->mb_intra= 0;
2700                     s->mv[0][0][0] = s->p_mv_table[xy][0];
2701                     s->mv[0][0][1] = s->p_mv_table[xy][1];
2702                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2703                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2704                 }
2705                 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2706                     s->mv_dir = MV_DIR_FORWARD;
2707                     s->mv_type = MV_TYPE_FIELD;
2708                     s->mb_intra= 0;
2709                     for(i=0; i<2; i++){
2710                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2711                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2712                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2713                     }
2714                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2715                                  &dmin, &next_block, 0, 0);
2716                 }
2717                 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2718                     s->mv_dir = MV_DIR_FORWARD;
2719                     s->mv_type = MV_TYPE_16X16;
2720                     s->mb_intra= 0;
2721                     s->mv[0][0][0] = 0;
2722                     s->mv[0][0][1] = 0;
2723                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2724                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2725                 }
2726                 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2727                     s->mv_dir = MV_DIR_FORWARD;
2728                     s->mv_type = MV_TYPE_8X8;
2729                     s->mb_intra= 0;
2730                     for(i=0; i<4; i++){
2731                         s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2732                         s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2733                     }
2734                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2735                                  &dmin, &next_block, 0, 0);
2736                 }
2737                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2738                     s->mv_dir = MV_DIR_FORWARD;
2739                     s->mv_type = MV_TYPE_16X16;
2740                     s->mb_intra= 0;
2741                     s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2742                     s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2743                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2744                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2745                 }
2746                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2747                     s->mv_dir = MV_DIR_BACKWARD;
2748                     s->mv_type = MV_TYPE_16X16;
2749                     s->mb_intra= 0;
2750                     s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2751                     s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2752                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2753                                  &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2754                 }
2755                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2756                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2757                     s->mv_type = MV_TYPE_16X16;
2758                     s->mb_intra= 0;
2759                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2760                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2761                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2762                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2763                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2764                                  &dmin, &next_block, 0, 0);
2765                 }
2766                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2767                     s->mv_dir = MV_DIR_FORWARD;
2768                     s->mv_type = MV_TYPE_FIELD;
2769                     s->mb_intra= 0;
2770                     for(i=0; i<2; i++){
2771                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2772                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2773                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2774                     }
2775                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2776                                  &dmin, &next_block, 0, 0);
2777                 }
2778                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2779                     s->mv_dir = MV_DIR_BACKWARD;
2780                     s->mv_type = MV_TYPE_FIELD;
2781                     s->mb_intra= 0;
2782                     for(i=0; i<2; i++){
2783                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2784                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2785                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2786                     }
2787                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2788                                  &dmin, &next_block, 0, 0);
2789                 }
2790                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2791                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2792                     s->mv_type = MV_TYPE_FIELD;
2793                     s->mb_intra= 0;
2794                     for(dir=0; dir<2; dir++){
2795                         for(i=0; i<2; i++){
2796                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2797                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2798                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2799                         }
2800                     }
2801                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2802                                  &dmin, &next_block, 0, 0);
2803                 }
2804                 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2805                     s->mv_dir = 0;
2806                     s->mv_type = MV_TYPE_16X16;
2807                     s->mb_intra= 1;
2808                     s->mv[0][0][0] = 0;
2809                     s->mv[0][0][1] = 0;
2810                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2811                                  &dmin, &next_block, 0, 0);
2812                     if(s->h263_pred || s->h263_aic){
2813                         if(best_s.mb_intra)
2814                             s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2815                         else
2816                             ff_clean_intra_table_entries(s); //old mode?
2817                     }
2818                 }
2819
2820                 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2821                     if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2822                         const int last_qp= backup_s.qscale;
2823                         int qpi, qp, dc[6];
2824                         int16_t ac[6][16];
2825                         const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2826                         static const int dquant_tab[4]={-1,1,-2,2};
2827
2828                         assert(backup_s.dquant == 0);
2829
2830                         //FIXME intra
2831                         s->mv_dir= best_s.mv_dir;
2832                         s->mv_type = MV_TYPE_16X16;
2833                         s->mb_intra= best_s.mb_intra;
2834                         s->mv[0][0][0] = best_s.mv[0][0][0];
2835                         s->mv[0][0][1] = best_s.mv[0][0][1];
2836                         s->mv[1][0][0] = best_s.mv[1][0][0];
2837                         s->mv[1][0][1] = best_s.mv[1][0][1];
2838
2839                         qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
2840                         for(; qpi<4; qpi++){
2841                             int dquant= dquant_tab[qpi];
2842                             qp= last_qp + dquant;
2843                             if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2844                                 continue;
2845                             backup_s.dquant= dquant;
2846                             if(s->mb_intra && s->dc_val[0]){
2847                                 for(i=0; i<6; i++){
2848                                     dc[i]= s->dc_val[0][ s->block_index[i] ];
2849                                     memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
2850                                 }
2851                             }
2852
2853                             encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2854                                          &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2855                             if(best_s.qscale != qp){
2856                                 if(s->mb_intra && s->dc_val[0]){
2857                                     for(i=0; i<6; i++){
2858                                         s->dc_val[0][ s->block_index[i] ]= dc[i];
2859                                         memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
2860                                     }
2861                                 }
2862                             }
2863                         }
2864                     }
2865                 }
2866                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2867                     int mx= s->b_direct_mv_table[xy][0];
2868                     int my= s->b_direct_mv_table[xy][1];
2869
2870                     backup_s.dquant = 0;
2871                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2872                     s->mb_intra= 0;
2873                     ff_mpeg4_set_direct_mv(s, mx, my);
2874                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2875                                  &dmin, &next_block, mx, my);
2876                 }
2877                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2878                     backup_s.dquant = 0;
2879                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2880                     s->mb_intra= 0;
2881                     ff_mpeg4_set_direct_mv(s, 0, 0);
2882                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2883                                  &dmin, &next_block, 0, 0);
2884                 }
2885                 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2886                     int coded=0;
2887                     for(i=0; i<6; i++)
2888                         coded |= s->block_last_index[i];
2889                     if(coded){
2890                         int mx,my;
2891                         memcpy(s->mv, best_s.mv, sizeof(s->mv));
2892                         if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2893                             mx=my=0; //FIXME find the one we actually used
2894                             ff_mpeg4_set_direct_mv(s, mx, my);
2895                         }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2896                             mx= s->mv[1][0][0];
2897                             my= s->mv[1][0][1];
2898                         }else{
2899                             mx= s->mv[0][0][0];
2900                             my= s->mv[0][0][1];
2901                         }
2902
2903                         s->mv_dir= best_s.mv_dir;
2904                         s->mv_type = best_s.mv_type;
2905                         s->mb_intra= 0;
2906 /*                        s->mv[0][0][0] = best_s.mv[0][0][0];
2907                         s->mv[0][0][1] = best_s.mv[0][0][1];
2908                         s->mv[1][0][0] = best_s.mv[1][0][0];
2909                         s->mv[1][0][1] = best_s.mv[1][0][1];*/
2910                         backup_s.dquant= 0;
2911                         s->skipdct=1;
2912                         encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2913                                         &dmin, &next_block, mx, my);
2914                         s->skipdct=0;
2915                     }
2916                 }
2917
2918                 s->current_picture.qscale_table[xy] = best_s.qscale;
2919
2920                 copy_context_after_encode(s, &best_s, -1);
2921
2922                 pb_bits_count= put_bits_count(&s->pb);
2923                 flush_put_bits(&s->pb);
2924                 avpriv_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2925                 s->pb= backup_s.pb;
2926
2927                 if(s->data_partitioning){
2928                     pb2_bits_count= put_bits_count(&s->pb2);
2929                     flush_put_bits(&s->pb2);
2930                     avpriv_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2931                     s->pb2= backup_s.pb2;
2932
2933                     tex_pb_bits_count= put_bits_count(&s->tex_pb);
2934                     flush_put_bits(&s->tex_pb);
2935                     avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2936                     s->tex_pb= backup_s.tex_pb;
2937                 }
2938                 s->last_bits= put_bits_count(&s->pb);
2939
2940                 if (CONFIG_H263_ENCODER &&
2941                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2942                     ff_h263_update_motion_val(s);
2943
2944                 if(next_block==0){ //FIXME 16 vs linesize16
2945                     s->hdsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
2946                     s->hdsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
2947                     s->hdsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2948                 }
2949
2950                 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2951                     ff_MPV_decode_mb(s, s->block);
2952             } else {
2953                 int motion_x = 0, motion_y = 0;
2954                 s->mv_type=MV_TYPE_16X16;
2955                 // only one MB-Type possible
2956
2957                 switch(mb_type){
2958                 case CANDIDATE_MB_TYPE_INTRA:
2959                     s->mv_dir = 0;
2960                     s->mb_intra= 1;
2961                     motion_x= s->mv[0][0][0] = 0;
2962                     motion_y= s->mv[0][0][1] = 0;
2963                     break;
2964                 case CANDIDATE_MB_TYPE_INTER:
2965                     s->mv_dir = MV_DIR_FORWARD;
2966                     s->mb_intra= 0;
2967                     motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2968                     motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2969                     break;
2970                 case CANDIDATE_MB_TYPE_INTER_I:
2971                     s->mv_dir = MV_DIR_FORWARD;
2972                     s->mv_type = MV_TYPE_FIELD;
2973                     s->mb_intra= 0;
2974                     for(i=0; i<2; i++){
2975                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2976                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2977                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2978                     }
2979                     break;
2980                 case CANDIDATE_MB_TYPE_INTER4V:
2981                     s->mv_dir = MV_DIR_FORWARD;
2982                     s->mv_type = MV_TYPE_8X8;
2983                     s->mb_intra= 0;
2984                     for(i=0; i<4; i++){
2985                         s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2986                         s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2987                     }
2988                     break;
2989                 case CANDIDATE_MB_TYPE_DIRECT:
2990                     if (CONFIG_MPEG4_ENCODER) {
2991                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2992                         s->mb_intra= 0;
2993                         motion_x=s->b_direct_mv_table[xy][0];
2994                         motion_y=s->b_direct_mv_table[xy][1];
2995                         ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
2996                     }
2997                     break;
2998                 case CANDIDATE_MB_TYPE_DIRECT0:
2999                     if (CONFIG_MPEG4_ENCODER) {
3000                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3001                         s->mb_intra= 0;
3002                         ff_mpeg4_set_direct_mv(s, 0, 0);
3003                     }
3004                     break;
3005                 case CANDIDATE_MB_TYPE_BIDIR:
3006                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3007                     s->mb_intra= 0;
3008                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3009                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3010                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3011                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3012                     break;
3013                 case CANDIDATE_MB_TYPE_BACKWARD:
3014                     s->mv_dir = MV_DIR_BACKWARD;
3015                     s->mb_intra= 0;
3016                     motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3017                     motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3018                     break;
3019                 case CANDIDATE_MB_TYPE_FORWARD:
3020                     s->mv_dir = MV_DIR_FORWARD;
3021                     s->mb_intra= 0;
3022                     motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3023                     motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3024                     break;
3025                 case CANDIDATE_MB_TYPE_FORWARD_I:
3026                     s->mv_dir = MV_DIR_FORWARD;
3027                     s->mv_type = MV_TYPE_FIELD;
3028                     s->mb_intra= 0;
3029                     for(i=0; i<2; i++){
3030                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3031                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3032                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3033                     }
3034                     break;
3035                 case CANDIDATE_MB_TYPE_BACKWARD_I:
3036                     s->mv_dir = MV_DIR_BACKWARD;
3037                     s->mv_type = MV_TYPE_FIELD;
3038                     s->mb_intra= 0;
3039                     for(i=0; i<2; i++){
3040                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3041                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3042                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3043                     }
3044                     break;
3045                 case CANDIDATE_MB_TYPE_BIDIR_I:
3046                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3047                     s->mv_type = MV_TYPE_FIELD;
3048                     s->mb_intra= 0;
3049                     for(dir=0; dir<2; dir++){
3050                         for(i=0; i<2; i++){
3051                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3052                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3053                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3054                         }
3055                     }
3056                     break;
3057                 default:
3058                     av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3059                 }
3060
3061                 encode_mb(s, motion_x, motion_y);
3062
3063                 // RAL: Update last macroblock type
3064                 s->last_mv_dir = s->mv_dir;
3065
3066                 if (CONFIG_H263_ENCODER &&
3067                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3068                     ff_h263_update_motion_val(s);
3069
3070                 ff_MPV_decode_mb(s, s->block);
3071             }
3072
3073             /* clean the MV table in IPS frames for direct mode in B frames */
3074             if(s->mb_intra /* && I,P,S_TYPE */){
3075                 s->p_mv_table[xy][0]=0;
3076                 s->p_mv_table[xy][1]=0;
3077             }
3078
3079             if(s->flags&CODEC_FLAG_PSNR){
3080                 int w= 16;
3081                 int h= 16;
3082
3083                 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3084                 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3085
3086                 s->current_picture.f->error[0] += sse(
3087                     s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3088                     s->dest[0], w, h, s->linesize);
3089                 s->current_picture.f->error[1] += sse(
3090                     s, s->new_picture.f->data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3091                     s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3092                 s->current_picture.f->error[2] += sse(
3093                     s, s->new_picture.f->data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3094                     s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3095             }
3096             if(s->loop_filter){
3097                 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3098                     ff_h263_loop_filter(s);
3099             }
3100             av_dlog(s->avctx, "MB %d %d bits\n",
3101                     s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3102         }
3103     }
3104
3105     //not beautiful here but we must write it before flushing so it has to be here
3106     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3107         ff_msmpeg4_encode_ext_header(s);
3108
3109     write_slice_end(s);
3110
3111     /* Send the last GOB if RTP */
3112     if (s->avctx->rtp_callback) {
3113         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3114         pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3115         /* Call the RTP callback to send the last GOB */
3116         emms_c();
3117         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3118     }
3119
3120     return 0;
3121 }
3122
3123 #define MERGE(field) dst->field += src->field; src->field=0
3124 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3125     MERGE(me.scene_change_score);
3126     MERGE(me.mc_mb_var_sum_temp);
3127     MERGE(me.mb_var_sum_temp);
3128 }
3129
3130 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3131     int i;
3132
3133     MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3134     MERGE(dct_count[1]);
3135     MERGE(mv_bits);
3136     MERGE(i_tex_bits);
3137     MERGE(p_tex_bits);
3138     MERGE(i_count);
3139     MERGE(f_count);
3140     MERGE(b_count);
3141     MERGE(skip_count);
3142     MERGE(misc_bits);
3143     MERGE(er.error_count);
3144     MERGE(padding_bug_score);
3145     MERGE(current_picture.f->error[0]);
3146     MERGE(current_picture.f->error[1]);
3147     MERGE(current_picture.f->error[2]);
3148
3149     if(dst->avctx->noise_reduction){
3150         for(i=0; i<64; i++){
3151             MERGE(dct_error_sum[0][i]);
3152             MERGE(dct_error_sum[1][i]);
3153         }
3154     }
3155
3156     assert(put_bits_count(&src->pb) % 8 ==0);
3157     assert(put_bits_count(&dst->pb) % 8 ==0);
3158     avpriv_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3159     flush_put_bits(&dst->pb);
3160 }
3161
3162 static int estimate_qp(MpegEncContext *s, int dry_run){
3163     if (s->next_lambda){
3164         s->current_picture_ptr->f->quality =
3165         s->current_picture.f->quality = s->next_lambda;
3166         if(!dry_run) s->next_lambda= 0;
3167     } else if (!s->fixed_qscale) {
3168         s->current_picture_ptr->f->quality =
3169         s->current_picture.f->quality = ff_rate_estimate_qscale(s, dry_run);
3170         if (s->current_picture.f->quality < 0)
3171             return -1;
3172     }
3173
3174     if(s->adaptive_quant){
3175         switch(s->codec_id){
3176         case AV_CODEC_ID_MPEG4:
3177             if (CONFIG_MPEG4_ENCODER)
3178                 ff_clean_mpeg4_qscales(s);
3179             break;
3180         case AV_CODEC_ID_H263:
3181         case AV_CODEC_ID_H263P:
3182         case AV_CODEC_ID_FLV1:
3183             if (CONFIG_H263_ENCODER)
3184                 ff_clean_h263_qscales(s);
3185             break;
3186         default:
3187             ff_init_qscale_tab(s);
3188         }
3189
3190         s->lambda= s->lambda_table[0];
3191         //FIXME broken
3192     }else
3193         s->lambda = s->current_picture.f->quality;
3194     update_qscale(s);
3195     return 0;
3196 }
3197
3198 /* must be called before writing the header */
3199 static void set_frame_distances(MpegEncContext * s){
3200     assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3201     s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3202
3203     if(s->pict_type==AV_PICTURE_TYPE_B){
3204         s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3205         assert(s->pb_time > 0 && s->pb_time < s->pp_time);
3206     }else{
3207         s->pp_time= s->time - s->last_non_b_time;
3208         s->last_non_b_time= s->time;
3209         assert(s->picture_number==0 || s->pp_time > 0);
3210     }
3211 }
3212
3213 static int encode_picture(MpegEncContext *s, int picture_number)
3214 {
3215     int i, ret;
3216     int bits;
3217     int context_count = s->slice_context_count;
3218
3219     s->picture_number = picture_number;
3220
3221     /* Reset the average MB variance */
3222     s->me.mb_var_sum_temp    =
3223     s->me.mc_mb_var_sum_temp = 0;
3224
3225     /* we need to initialize some time vars before we can encode b-frames */
3226     // RAL: Condition added for MPEG1VIDEO
3227     if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3228         set_frame_distances(s);
3229     if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3230         ff_set_mpeg4_time(s);
3231
3232     s->me.scene_change_score=0;
3233
3234 //    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3235
3236     if(s->pict_type==AV_PICTURE_TYPE_I){
3237         if(s->msmpeg4_version >= 3) s->no_rounding=1;
3238         else                        s->no_rounding=0;
3239     }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3240         if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3241             s->no_rounding ^= 1;
3242     }
3243
3244     if(s->flags & CODEC_FLAG_PASS2){
3245         if (estimate_qp(s,1) < 0)
3246             return -1;
3247         ff_get_2pass_fcode(s);
3248     }else if(!(s->flags & CODEC_FLAG_QSCALE)){
3249         if(s->pict_type==AV_PICTURE_TYPE_B)
3250             s->lambda= s->last_lambda_for[s->pict_type];
3251         else
3252             s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3253         update_qscale(s);
3254     }
3255
3256     s->mb_intra=0; //for the rate distortion & bit compare functions
3257     for(i=1; i<context_count; i++){
3258         ret = ff_update_duplicate_context(s->thread_context[i], s);
3259         if (ret < 0)
3260             return ret;
3261     }
3262
3263     if(ff_init_me(s)<0)
3264         return -1;
3265
3266     /* Estimate motion for every MB */
3267     if(s->pict_type != AV_PICTURE_TYPE_I){
3268         s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
3269         s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
3270         if (s->pict_type != AV_PICTURE_TYPE_B) {
3271             if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
3272                 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3273             }
3274         }
3275
3276         s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3277     }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3278         /* I-Frame */
3279         for(i=0; i<s->mb_stride*s->mb_height; i++)
3280             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3281
3282         if(!s->fixed_qscale){
3283             /* finding spatial complexity for I-frame rate control */
3284             s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3285         }
3286     }
3287     for(i=1; i<context_count; i++){
3288         merge_context_after_me(s, s->thread_context[i]);
3289     }
3290     s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3291     s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->me.   mb_var_sum_temp;
3292     emms_c();
3293
3294     if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == AV_PICTURE_TYPE_P){
3295         s->pict_type= AV_PICTURE_TYPE_I;
3296         for(i=0; i<s->mb_stride*s->mb_height; i++)
3297             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3298         av_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
3299                 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3300     }
3301
3302     if(!s->umvplus){
3303         if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3304             s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3305
3306             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3307                 int a,b;
3308                 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3309                 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3310                 s->f_code= FFMAX3(s->f_code, a, b);
3311             }
3312
3313             ff_fix_long_p_mvs(s);
3314             ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
3315             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3316                 int j;
3317                 for(i=0; i<2; i++){
3318                     for(j=0; j<2; j++)
3319                         ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3320                                         s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
3321                 }
3322             }
3323         }
3324
3325         if(s->pict_type==AV_PICTURE_TYPE_B){
3326             int a, b;
3327
3328             a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3329             b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3330             s->f_code = FFMAX(a, b);
3331
3332             a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3333             b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3334             s->b_code = FFMAX(a, b);
3335
3336             ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3337             ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3338             ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3339             ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3340             if(s->flags & CODEC_FLAG_INTERLACED_ME){
3341                 int dir, j;
3342                 for(dir=0; dir<2; dir++){
3343                     for(i=0; i<2; i++){
3344                         for(j=0; j<2; j++){
3345                             int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3346                                           : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3347                             ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3348                                             s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3349                         }
3350                     }
3351                 }
3352             }
3353         }
3354     }
3355
3356     if (estimate_qp(s, 0) < 0)
3357         return -1;
3358
3359     if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==AV_PICTURE_TYPE_I && !(s->flags & CODEC_FLAG_QSCALE))
3360         s->qscale= 3; //reduce clipping problems
3361
3362     if (s->out_format == FMT_MJPEG) {
3363         /* for mjpeg, we do include qscale in the matrix */
3364         for(i=1;i<64;i++){
3365             int j = s->idsp.idct_permutation[i];
3366
3367             s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3368         }
3369         s->y_dc_scale_table=
3370         s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3371         s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3372         ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3373                        s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3374         s->qscale= 8;
3375     }
3376
3377     //FIXME var duplication
3378     s->current_picture_ptr->f->key_frame =
3379     s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3380     s->current_picture_ptr->f->pict_type =
3381     s->current_picture.f->pict_type = s->pict_type;
3382
3383     if (s->current_picture.f->key_frame)
3384         s->picture_in_gop_number=0;
3385
3386     s->last_bits= put_bits_count(&s->pb);
3387     switch(s->out_format) {
3388     case FMT_MJPEG:
3389         if (CONFIG_MJPEG_ENCODER)
3390             ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3391                                            s->intra_matrix);
3392         break;
3393     case FMT_H261:
3394         if (CONFIG_H261_ENCODER)
3395             ff_h261_encode_picture_header(s, picture_number);
3396         break;
3397     case FMT_H263:
3398         if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3399             ff_wmv2_encode_picture_header(s, picture_number);
3400         else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3401             ff_msmpeg4_encode_picture_header(s, picture_number);
3402         else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3403             ff_mpeg4_encode_picture_header(s, picture_number);
3404         else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10)
3405             ff_rv10_encode_picture_header(s, picture_number);
3406         else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3407             ff_rv20_encode_picture_header(s, picture_number);
3408         else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3409             ff_flv_encode_picture_header(s, picture_number);
3410         else if (CONFIG_H263_ENCODER)
3411             ff_h263_encode_picture_header(s, picture_number);
3412         break;
3413     case FMT_MPEG1:
3414         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3415             ff_mpeg1_encode_picture_header(s, picture_number);
3416         break;
3417     default:
3418         assert(0);
3419     }
3420     bits= put_bits_count(&s->pb);
3421     s->header_bits= bits - s->last_bits;
3422
3423     for(i=1; i<context_count; i++){
3424         update_duplicate_context_after_me(s->thread_context[i], s);
3425     }
3426     s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3427     for(i=1; i<context_count; i++){
3428         merge_context_after_encode(s, s->thread_context[i]);
3429     }
3430     emms_c();
3431     return 0;
3432 }
3433
3434 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3435     const int intra= s->mb_intra;
3436     int i;
3437
3438     s->dct_count[intra]++;
3439
3440     for(i=0; i<64; i++){
3441         int level= block[i];
3442
3443         if(level){
3444             if(level>0){
3445                 s->dct_error_sum[intra][i] += level;
3446                 level -= s->dct_offset[intra][i];
3447                 if(level<0) level=0;
3448             }else{
3449                 s->dct_error_sum[intra][i] -= level;
3450                 level += s->dct_offset[intra][i];
3451                 if(level>0) level=0;
3452             }
3453             block[i]= level;
3454         }
3455     }
3456 }
3457
3458 static int dct_quantize_trellis_c(MpegEncContext *s,
3459                                   int16_t *block, int n,
3460                                   int qscale, int *overflow){
3461     const int *qmat;
3462     const uint8_t *scantable= s->intra_scantable.scantable;
3463     const uint8_t *perm_scantable= s->intra_scantable.permutated;
3464     int max=0;
3465     unsigned int threshold1, threshold2;
3466     int bias=0;
3467     int run_tab[65];
3468     int level_tab[65];
3469     int score_tab[65];
3470     int survivor[65];
3471     int survivor_count;
3472     int last_run=0;
3473     int last_level=0;
3474     int last_score= 0;
3475     int last_i;
3476     int coeff[2][64];
3477     int coeff_count[64];
3478     int qmul, qadd, start_i, last_non_zero, i, dc;
3479     const int esc_length= s->ac_esc_length;
3480     uint8_t * length;
3481     uint8_t * last_length;
3482     const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3483
3484     s->dsp.fdct (block);
3485
3486     if(s->dct_error_sum)
3487         s->denoise_dct(s, block);
3488     qmul= qscale*16;
3489     qadd= ((qscale-1)|1)*8;
3490
3491     if (s->mb_intra) {
3492         int q;
3493         if (!s->h263_aic) {
3494             if (n < 4)
3495                 q = s->y_dc_scale;
3496             else
3497                 q = s->c_dc_scale;
3498             q = q << 3;
3499         } else{
3500             /* For AIC we skip quant/dequant of INTRADC */
3501             q = 1 << 3;
3502             qadd=0;
3503         }
3504
3505         /* note: block[0] is assumed to be positive */
3506         block[0] = (block[0] + (q >> 1)) / q;
3507         start_i = 1;
3508         last_non_zero = 0;
3509         qmat = s->q_intra_matrix[qscale];
3510         if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3511             bias= 1<<(QMAT_SHIFT-1);
3512         length     = s->intra_ac_vlc_length;
3513         last_length= s->intra_ac_vlc_last_length;
3514     } else {
3515         start_i = 0;
3516         last_non_zero = -1;
3517         qmat = s->q_inter_matrix[qscale];
3518         length     = s->inter_ac_vlc_length;
3519         last_length= s->inter_ac_vlc_last_length;
3520     }
3521     last_i= start_i;
3522
3523     threshold1= (1<<QMAT_SHIFT) - bias - 1;
3524     threshold2= (threshold1<<1);
3525
3526     for(i=63; i>=start_i; i--) {
3527         const int j = scantable[i];
3528         int level = block[j] * qmat[j];
3529
3530         if(((unsigned)(level+threshold1))>threshold2){
3531             last_non_zero = i;
3532             break;
3533         }
3534     }
3535
3536     for(i=start_i; i<=last_non_zero; i++) {
3537         const int j = scantable[i];
3538         int level = block[j] * qmat[j];
3539
3540 //        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3541 //           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3542         if(((unsigned)(level+threshold1))>threshold2){
3543             if(level>0){
3544                 level= (bias + level)>>QMAT_SHIFT;
3545                 coeff[0][i]= level;
3546                 coeff[1][i]= level-1;
3547 //                coeff[2][k]= level-2;
3548             }else{
3549                 level= (bias - level)>>QMAT_SHIFT;
3550                 coeff[0][i]= -level;
3551                 coeff[1][i]= -level+1;
3552 //                coeff[2][k]= -level+2;
3553             }
3554             coeff_count[i]= FFMIN(level, 2);
3555             assert(coeff_count[i]);
3556             max |=level;
3557         }else{
3558             coeff[0][i]= (level>>31)|1;
3559             coeff_count[i]= 1;
3560         }
3561     }
3562
3563     *overflow= s->max_qcoeff < max; //overflow might have happened
3564
3565     if(last_non_zero < start_i){
3566         memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3567         return last_non_zero;
3568     }
3569
3570     score_tab[start_i]= 0;
3571     survivor[0]= start_i;
3572     survivor_count= 1;
3573
3574     for(i=start_i; i<=last_non_zero; i++){
3575         int level_index, j, zero_distortion;
3576         int dct_coeff= FFABS(block[ scantable[i] ]);
3577         int best_score=256*256*256*120;
3578
3579         if (s->dsp.fdct == ff_fdct_ifast)
3580             dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3581         zero_distortion= dct_coeff*dct_coeff;
3582
3583         for(level_index=0; level_index < coeff_count[i]; level_index++){
3584             int distortion;
3585             int level= coeff[level_index][i];
3586             const int alevel= FFABS(level);
3587             int unquant_coeff;
3588
3589             assert(level);
3590
3591             if(s->out_format == FMT_H263){
3592                 unquant_coeff= alevel*qmul + qadd;
3593             }else{ //MPEG1
3594                 j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
3595                 if(s->mb_intra){
3596                         unquant_coeff = (int)(  alevel  * qscale * s->intra_matrix[j]) >> 3;
3597                         unquant_coeff =   (unquant_coeff - 1) | 1;
3598                 }else{
3599                         unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3600                         unquant_coeff =   (unquant_coeff - 1) | 1;
3601                 }
3602                 unquant_coeff<<= 3;
3603             }
3604
3605             distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3606             level+=64;
3607             if((level&(~127)) == 0){
3608                 for(j=survivor_count-1; j>=0; j--){
3609                     int run= i - survivor[j];
3610                     int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3611                     score += score_tab[i-run];
3612
3613                     if(score < best_score){
3614                         best_score= score;
3615                         run_tab[i+1]= run;
3616                         level_tab[i+1]= level-64;
3617                     }
3618                 }
3619
3620                 if(s->out_format == FMT_H263){
3621                     for(j=survivor_count-1; j>=0; j--){
3622                         int run= i - survivor[j];
3623                         int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3624                         score += score_tab[i-run];
3625                         if(score < last_score){
3626                             last_score= score;
3627                             last_run= run;
3628                             last_level= level-64;
3629                             last_i= i+1;
3630                         }
3631                     }
3632                 }
3633             }else{
3634                 distortion += esc_length*lambda;
3635                 for(j=survivor_count-1; j>=0; j--){
3636                     int run= i - survivor[j];
3637                     int score= distortion + score_tab[i-run];
3638
3639                     if(score < best_score){
3640                         best_score= score;
3641                         run_tab[i+1]= run;
3642                         level_tab[i+1]= level-64;
3643                     }
3644                 }
3645
3646                 if(s->out_format == FMT_H263){
3647                   for(j=survivor_count-1; j>=0; j--){
3648                         int run= i - survivor[j];
3649                         int score= distortion + score_tab[i-run];
3650                         if(score < last_score){
3651                             last_score= score;
3652                             last_run= run;
3653                             last_level= level-64;
3654                             last_i= i+1;
3655                         }
3656                     }
3657                 }
3658             }
3659         }
3660
3661         score_tab[i+1]= best_score;
3662
3663         //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3664         if(last_non_zero <= 27){
3665             for(; survivor_count; survivor_count--){
3666                 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3667                     break;
3668             }
3669         }else{
3670             for(; survivor_count; survivor_count--){
3671                 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3672                     break;
3673             }
3674         }
3675
3676         survivor[ survivor_count++ ]= i+1;
3677     }
3678
3679     if(s->out_format != FMT_H263){
3680         last_score= 256*256*256*120;
3681         for(i= survivor[0]; i<=last_non_zero + 1; i++){
3682             int score= score_tab[i];
3683             if(i) score += lambda*2; //FIXME exacter?
3684
3685             if(score < last_score){
3686                 last_score= score;
3687                 last_i= i;
3688                 last_level= level_tab[i];
3689                 last_run= run_tab[i];
3690             }
3691         }
3692     }
3693
3694     s->coded_score[n] = last_score;
3695
3696     dc= FFABS(block[0]);
3697     last_non_zero= last_i - 1;
3698     memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
3699
3700     if(last_non_zero < start_i)
3701         return last_non_zero;
3702
3703     if(last_non_zero == 0 && start_i == 0){
3704         int best_level= 0;
3705         int best_score= dc * dc;
3706
3707         for(i=0; i<coeff_count[0]; i++){
3708             int level= coeff[i][0];
3709             int alevel= FFABS(level);
3710             int unquant_coeff, score, distortion;
3711
3712             if(s->out_format == FMT_H263){
3713                     unquant_coeff= (alevel*qmul + qadd)>>3;
3714             }else{ //MPEG1
3715                     unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3716                     unquant_coeff =   (unquant_coeff - 1) | 1;
3717             }
3718             unquant_coeff = (unquant_coeff + 4) >> 3;
3719             unquant_coeff<<= 3 + 3;
3720
3721             distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3722             level+=64;
3723             if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3724             else                    score= distortion + esc_length*lambda;
3725
3726             if(score < best_score){
3727                 best_score= score;
3728                 best_level= level - 64;
3729             }
3730         }
3731         block[0]= best_level;
3732         s->coded_score[n] = best_score - dc*dc;
3733         if(best_level == 0) return -1;
3734         else                return last_non_zero;
3735     }
3736
3737     i= last_i;
3738     assert(last_level);
3739
3740     block[ perm_scantable[last_non_zero] ]= last_level;
3741     i -= last_run + 1;
3742
3743     for(; i>start_i; i -= run_tab[i] + 1){
3744         block[ perm_scantable[i-1] ]= level_tab[i];
3745     }
3746
3747     return last_non_zero;
3748 }
3749
3750 //#define REFINE_STATS 1
3751 static int16_t basis[64][64];
3752
3753 static void build_basis(uint8_t *perm){
3754     int i, j, x, y;
3755     emms_c();
3756     for(i=0; i<8; i++){
3757         for(j=0; j<8; j++){
3758             for(y=0; y<8; y++){
3759                 for(x=0; x<8; x++){
3760                     double s= 0.25*(1<<BASIS_SHIFT);
3761                     int index= 8*i + j;
3762                     int perm_index= perm[index];
3763                     if(i==0) s*= sqrt(0.5);
3764                     if(j==0) s*= sqrt(0.5);
3765                     basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
3766                 }
3767             }
3768         }
3769     }
3770 }
3771
3772 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3773                         int16_t *block, int16_t *weight, int16_t *orig,
3774                         int n, int qscale){
3775     int16_t rem[64];
3776     LOCAL_ALIGNED_16(int16_t, d1, [64]);
3777     const uint8_t *scantable= s->intra_scantable.scantable;
3778     const uint8_t *perm_scantable= s->intra_scantable.permutated;
3779 //    unsigned int threshold1, threshold2;
3780 //    int bias=0;
3781     int run_tab[65];
3782     int prev_run=0;
3783     int prev_level=0;
3784     int qmul, qadd, start_i, last_non_zero, i, dc;
3785     uint8_t * length;
3786     uint8_t * last_length;
3787     int lambda;
3788     int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3789 #ifdef REFINE_STATS
3790 static int count=0;
3791 static int after_last=0;
3792 static int to_zero=0;
3793 static int from_zero=0;
3794 static int raise=0;
3795 static int lower=0;
3796 static int messed_sign=0;
3797 #endif
3798
3799     if(basis[0][0] == 0)
3800         build_basis(s->idsp.idct_permutation);
3801
3802     qmul= qscale*2;
3803     qadd= (qscale-1)|1;
3804     if (s->mb_intra) {
3805         if (!s->h263_aic) {
3806             if (n < 4)
3807                 q = s->y_dc_scale;
3808             else
3809                 q = s->c_dc_scale;
3810         } else{
3811             /* For AIC we skip quant/dequant of INTRADC */
3812             q = 1;
3813             qadd=0;
3814         }
3815         q <<= RECON_SHIFT-3;
3816         /* note: block[0] is assumed to be positive */
3817         dc= block[0]*q;
3818 //        block[0] = (block[0] + (q >> 1)) / q;
3819         start_i = 1;
3820 //        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3821 //            bias= 1<<(QMAT_SHIFT-1);
3822         length     = s->intra_ac_vlc_length;
3823         last_length= s->intra_ac_vlc_last_length;
3824     } else {
3825         dc= 0;
3826         start_i = 0;
3827         length     = s->inter_ac_vlc_length;
3828         last_length= s->inter_ac_vlc_last_length;
3829     }
3830     last_non_zero = s->block_last_index[n];
3831
3832 #ifdef REFINE_STATS
3833 {START_TIMER
3834 #endif
3835     dc += (1<<(RECON_SHIFT-1));
3836     for(i=0; i<64; i++){
3837         rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME  use orig dirrectly instead of copying to rem[]
3838     }
3839 #ifdef REFINE_STATS
3840 STOP_TIMER("memset rem[]")}
3841 #endif
3842     sum=0;
3843     for(i=0; i<64; i++){
3844         int one= 36;
3845         int qns=4;
3846         int w;
3847
3848         w= FFABS(weight[i]) + qns*one;
3849         w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3850
3851         weight[i] = w;
3852 //        w=weight[i] = (63*qns + (w/2)) / w;
3853
3854         assert(w>0);
3855         assert(w<(1<<6));
3856         sum += w*w;
3857     }
3858     lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3859 #ifdef REFINE_STATS