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