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