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