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