]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo_enc.c
avcodec/mpegvideo: Deprecate duplicate matrix opt for unsupported codecs
[ffmpeg] / libavcodec / mpegvideo_enc.c
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 /*
26  * non linear quantizers with large QPs and VBV with restrictive qmin fixes sponsored by NOA GmbH
27  */
28
29 /**
30  * @file
31  * The simplest mpeg encoder (well, it was the simplest!).
32  */
33
34 #include <stdint.h>
35
36 #include "libavutil/internal.h"
37 #include "libavutil/intmath.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/mem_internal.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/thread.h"
43 #include "avcodec.h"
44 #include "dct.h"
45 #include "idctdsp.h"
46 #include "mpeg12.h"
47 #include "mpegvideo.h"
48 #include "mpegvideodata.h"
49 #include "h261.h"
50 #include "h263.h"
51 #include "h263data.h"
52 #include "mjpegenc_common.h"
53 #include "mathops.h"
54 #include "mpegutils.h"
55 #include "mjpegenc.h"
56 #include "speedhqenc.h"
57 #include "msmpeg4.h"
58 #include "pixblockdsp.h"
59 #include "qpeldsp.h"
60 #include "faandct.h"
61 #include "thread.h"
62 #include "aandcttab.h"
63 #include "flv.h"
64 #include "mpeg4video.h"
65 #include "internal.h"
66 #include "bytestream.h"
67 #include "wmv2.h"
68 #include "rv10.h"
69 #include "packet_internal.h"
70 #include <limits.h>
71 #include "sp5x.h"
72
73 #define QUANT_BIAS_SHIFT 8
74
75 #define QMAT_SHIFT_MMX 16
76 #define QMAT_SHIFT 21
77
78 static int encode_picture(MpegEncContext *s, int picture_number);
79 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
80 static int sse_mb(MpegEncContext *s);
81 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
82 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
83
84 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
85 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
86
87 const AVOption ff_mpv_generic_options[] = {
88     FF_MPV_COMMON_OPTS
89 #if FF_API_MPEGVIDEO_OPTS
90     FF_MPV_DEPRECATED_MPEG_QUANT_OPT
91     FF_MPV_DEPRECATED_A53_CC_OPT
92     FF_MPV_DEPRECATED_MATRIX_OPT
93 #endif
94     { NULL },
95 };
96
97 void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
98                        uint16_t (*qmat16)[2][64],
99                        const uint16_t *quant_matrix,
100                        int bias, int qmin, int qmax, int intra)
101 {
102     FDCTDSPContext *fdsp = &s->fdsp;
103     int qscale;
104     int shift = 0;
105
106     for (qscale = qmin; qscale <= qmax; qscale++) {
107         int i;
108         int qscale2;
109
110         if (s->q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
111         else                 qscale2 = qscale << 1;
112
113         if (fdsp->fdct == ff_jpeg_fdct_islow_8  ||
114 #if CONFIG_FAANDCT
115             fdsp->fdct == ff_faandct            ||
116 #endif /* CONFIG_FAANDCT */
117             fdsp->fdct == ff_jpeg_fdct_islow_10) {
118             for (i = 0; i < 64; i++) {
119                 const int j = s->idsp.idct_permutation[i];
120                 int64_t den = (int64_t) qscale2 * quant_matrix[j];
121                 /* 16 <= qscale * quant_matrix[i] <= 7905
122                  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
123                  *             19952 <=              x  <= 249205026
124                  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
125                  *           3444240 >= (1 << 36) / (x) >= 275 */
126
127                 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
128             }
129         } else if (fdsp->fdct == ff_fdct_ifast) {
130             for (i = 0; i < 64; i++) {
131                 const int j = s->idsp.idct_permutation[i];
132                 int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
133                 /* 16 <= qscale * quant_matrix[i] <= 7905
134                  * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
135                  *             19952 <=              x  <= 249205026
136                  * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
137                  *           3444240 >= (1 << 36) / (x) >= 275 */
138
139                 qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
140             }
141         } else {
142             for (i = 0; i < 64; i++) {
143                 const int j = s->idsp.idct_permutation[i];
144                 int64_t den = (int64_t) qscale2 * quant_matrix[j];
145                 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
146                  * Assume x = qscale * quant_matrix[i]
147                  * So             16 <=              x  <= 7905
148                  * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
149                  * so          32768 >= (1 << 19) / (x) >= 67 */
150                 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
151                 //qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) /
152                 //                    (qscale * quant_matrix[i]);
153                 qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
154
155                 if (qmat16[qscale][0][i] == 0 ||
156                     qmat16[qscale][0][i] == 128 * 256)
157                     qmat16[qscale][0][i] = 128 * 256 - 1;
158                 qmat16[qscale][1][i] =
159                     ROUNDED_DIV(bias * (1<<(16 - QUANT_BIAS_SHIFT)),
160                                 qmat16[qscale][0][i]);
161             }
162         }
163
164         for (i = intra; i < 64; i++) {
165             int64_t max = 8191;
166             if (fdsp->fdct == ff_fdct_ifast) {
167                 max = (8191LL * ff_aanscales[i]) >> 14;
168             }
169             while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
170                 shift++;
171             }
172         }
173     }
174     if (shift) {
175         av_log(s->avctx, AV_LOG_INFO,
176                "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
177                QMAT_SHIFT - shift);
178     }
179 }
180
181 static inline void update_qscale(MpegEncContext *s)
182 {
183     if (s->q_scale_type == 1 && 0) {
184         int i;
185         int bestdiff=INT_MAX;
186         int best = 1;
187
188         for (i = 0 ; i<FF_ARRAY_ELEMS(ff_mpeg2_non_linear_qscale); i++) {
189             int diff = FFABS((ff_mpeg2_non_linear_qscale[i]<<(FF_LAMBDA_SHIFT + 6)) - (int)s->lambda * 139);
190             if (ff_mpeg2_non_linear_qscale[i] < s->avctx->qmin ||
191                 (ff_mpeg2_non_linear_qscale[i] > s->avctx->qmax && !s->vbv_ignore_qmax))
192                 continue;
193             if (diff < bestdiff) {
194                 bestdiff = diff;
195                 best = i;
196             }
197         }
198         s->qscale = best;
199     } else {
200         s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
201                     (FF_LAMBDA_SHIFT + 7);
202         s->qscale = av_clip(s->qscale, s->avctx->qmin, s->vbv_ignore_qmax ? 31 : s->avctx->qmax);
203     }
204
205     s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
206                  FF_LAMBDA_SHIFT;
207 }
208
209 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
210 {
211     int i;
212
213     if (matrix) {
214         put_bits(pb, 1, 1);
215         for (i = 0; i < 64; i++) {
216             put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
217         }
218     } else
219         put_bits(pb, 1, 0);
220 }
221
222 /**
223  * init s->current_picture.qscale_table from s->lambda_table
224  */
225 void ff_init_qscale_tab(MpegEncContext *s)
226 {
227     int8_t * const qscale_table = s->current_picture.qscale_table;
228     int i;
229
230     for (i = 0; i < s->mb_num; i++) {
231         unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
232         int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
233         qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
234                                                   s->avctx->qmax);
235     }
236 }
237
238 static void update_duplicate_context_after_me(MpegEncContext *dst,
239                                               MpegEncContext *src)
240 {
241 #define COPY(a) dst->a= src->a
242     COPY(pict_type);
243     COPY(current_picture);
244     COPY(f_code);
245     COPY(b_code);
246     COPY(qscale);
247     COPY(lambda);
248     COPY(lambda2);
249     COPY(picture_in_gop_number);
250     COPY(gop_picture_number);
251     COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
252     COPY(progressive_frame);    // FIXME don't set in encode_header
253     COPY(partitioned_frame);    // FIXME don't set in encode_header
254 #undef COPY
255 }
256
257 static void mpv_encode_init_static(void)
258 {
259    for (int i = -16; i < 16; i++)
260         default_fcode_tab[i + MAX_MV] = 1;
261 }
262
263 /**
264  * Set the given MpegEncContext to defaults for encoding.
265  * the changed fields will not depend upon the prior state of the MpegEncContext.
266  */
267 static void mpv_encode_defaults(MpegEncContext *s)
268 {
269     static AVOnce init_static_once = AV_ONCE_INIT;
270
271     ff_mpv_common_defaults(s);
272
273     ff_thread_once(&init_static_once, mpv_encode_init_static);
274
275     s->me.mv_penalty = default_mv_penalty;
276     s->fcode_tab     = default_fcode_tab;
277
278     s->input_picture_number  = 0;
279     s->picture_in_gop_number = 0;
280 }
281
282 av_cold int ff_dct_encode_init(MpegEncContext *s)
283 {
284     if (ARCH_X86)
285         ff_dct_encode_init_x86(s);
286
287     if (CONFIG_H263_ENCODER)
288         ff_h263dsp_init(&s->h263dsp);
289     if (!s->dct_quantize)
290         s->dct_quantize = ff_dct_quantize_c;
291     if (!s->denoise_dct)
292         s->denoise_dct  = denoise_dct_c;
293     s->fast_dct_quantize = s->dct_quantize;
294     if (s->avctx->trellis)
295         s->dct_quantize  = dct_quantize_trellis_c;
296
297     return 0;
298 }
299
300 /* init video encoder */
301 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
302 {
303     MpegEncContext *s = avctx->priv_data;
304     AVCPBProperties *cpb_props;
305     int i, ret;
306
307     mpv_encode_defaults(s);
308
309     switch (avctx->pix_fmt) {
310     case AV_PIX_FMT_YUVJ444P:
311     case AV_PIX_FMT_YUV444P:
312         s->chroma_format = CHROMA_444;
313         break;
314     case AV_PIX_FMT_YUVJ422P:
315     case AV_PIX_FMT_YUV422P:
316         s->chroma_format = CHROMA_422;
317         break;
318     case AV_PIX_FMT_YUVJ420P:
319     case AV_PIX_FMT_YUV420P:
320     default:
321         s->chroma_format = CHROMA_420;
322         break;
323     }
324
325     avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
326
327 #if FF_API_PRIVATE_OPT
328 FF_DISABLE_DEPRECATION_WARNINGS
329     if (avctx->rtp_payload_size)
330         s->rtp_payload_size = avctx->rtp_payload_size;
331     if (avctx->me_penalty_compensation)
332         s->me_penalty_compensation = avctx->me_penalty_compensation;
333     if (avctx->pre_me)
334         s->me_pre = avctx->pre_me;
335 FF_ENABLE_DEPRECATION_WARNINGS
336 #endif
337
338     s->bit_rate = avctx->bit_rate;
339     s->width    = avctx->width;
340     s->height   = avctx->height;
341     if (avctx->gop_size > 600 &&
342         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
343         av_log(avctx, AV_LOG_WARNING,
344                "keyframe interval too large!, reducing it from %d to %d\n",
345                avctx->gop_size, 600);
346         avctx->gop_size = 600;
347     }
348     s->gop_size     = avctx->gop_size;
349     s->avctx        = avctx;
350     if (avctx->max_b_frames > MAX_B_FRAMES) {
351         av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
352                "is %d.\n", MAX_B_FRAMES);
353         avctx->max_b_frames = MAX_B_FRAMES;
354     }
355     s->max_b_frames = avctx->max_b_frames;
356     s->codec_id     = avctx->codec->id;
357     s->strict_std_compliance = avctx->strict_std_compliance;
358     s->quarter_sample     = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
359     s->rtp_mode           = !!s->rtp_payload_size;
360     s->intra_dc_precision = avctx->intra_dc_precision;
361
362     // workaround some differences between how applications specify dc precision
363     if (s->intra_dc_precision < 0) {
364         s->intra_dc_precision += 8;
365     } else if (s->intra_dc_precision >= 8)
366         s->intra_dc_precision -= 8;
367
368     if (s->intra_dc_precision < 0) {
369         av_log(avctx, AV_LOG_ERROR,
370                 "intra dc precision must be positive, note some applications use"
371                 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
372         return AVERROR(EINVAL);
373     }
374
375     if (avctx->codec_id == AV_CODEC_ID_AMV || (avctx->active_thread_type & FF_THREAD_SLICE))
376         s->huffman = 0;
377
378     if (s->intra_dc_precision > (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 3 : 0)) {
379         av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
380         return AVERROR(EINVAL);
381     }
382     s->user_specified_pts = AV_NOPTS_VALUE;
383
384     if (s->gop_size <= 1) {
385         s->intra_only = 1;
386         s->gop_size   = 12;
387     } else {
388         s->intra_only = 0;
389     }
390
391     /* Fixed QSCALE */
392     s->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
393
394     s->adaptive_quant = (avctx->lumi_masking ||
395                          avctx->dark_masking ||
396                          avctx->temporal_cplx_masking ||
397                          avctx->spatial_cplx_masking  ||
398                          avctx->p_masking      ||
399                          s->border_masking ||
400                          (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
401                         !s->fixed_qscale;
402
403     s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
404
405     if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
406         switch(avctx->codec_id) {
407         case AV_CODEC_ID_MPEG1VIDEO:
408         case AV_CODEC_ID_MPEG2VIDEO:
409             avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
410             break;
411         case AV_CODEC_ID_MPEG4:
412         case AV_CODEC_ID_MSMPEG4V1:
413         case AV_CODEC_ID_MSMPEG4V2:
414         case AV_CODEC_ID_MSMPEG4V3:
415             if       (avctx->rc_max_rate >= 15000000) {
416                 avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
417             } else if(avctx->rc_max_rate >=  2000000) {
418                 avctx->rc_buffer_size =  80 + (avctx->rc_max_rate -  2000000LL) * (320- 80) / (15000000 -  2000000);
419             } else if(avctx->rc_max_rate >=   384000) {
420                 avctx->rc_buffer_size =  40 + (avctx->rc_max_rate -   384000LL) * ( 80- 40) / ( 2000000 -   384000);
421             } else
422                 avctx->rc_buffer_size = 40;
423             avctx->rc_buffer_size *= 16384;
424             break;
425         }
426         if (avctx->rc_buffer_size) {
427             av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
428         }
429     }
430
431     if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
432         av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
433         return AVERROR(EINVAL);
434     }
435
436     if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
437         av_log(avctx, AV_LOG_INFO,
438                "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
439     }
440
441     if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
442         av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
443         return AVERROR(EINVAL);
444     }
445
446     if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
447         av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
448         return AVERROR(EINVAL);
449     }
450
451     if (avctx->rc_max_rate &&
452         avctx->rc_max_rate == avctx->bit_rate &&
453         avctx->rc_max_rate != avctx->rc_min_rate) {
454         av_log(avctx, AV_LOG_INFO,
455                "impossible bitrate constraints, this will fail\n");
456     }
457
458     if (avctx->rc_buffer_size &&
459         avctx->bit_rate * (int64_t)avctx->time_base.num >
460             avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
461         av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
462         return AVERROR(EINVAL);
463     }
464
465     if (!s->fixed_qscale &&
466         avctx->bit_rate * av_q2d(avctx->time_base) >
467             avctx->bit_rate_tolerance) {
468         av_log(avctx, AV_LOG_WARNING,
469                "bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
470         avctx->bit_rate_tolerance = 5 * avctx->bit_rate * av_q2d(avctx->time_base);
471     }
472
473     if (avctx->rc_max_rate &&
474         avctx->rc_min_rate == avctx->rc_max_rate &&
475         (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
476          s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
477         90000LL * (avctx->rc_buffer_size - 1) >
478             avctx->rc_max_rate * 0xFFFFLL) {
479         av_log(avctx, AV_LOG_INFO,
480                "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
481                "specified vbv buffer is too large for the given bitrate!\n");
482     }
483
484     if ((avctx->flags & AV_CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
485         s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
486         s->codec_id != AV_CODEC_ID_FLV1) {
487         av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
488         return AVERROR(EINVAL);
489     }
490
491     if (s->obmc && avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
492         av_log(avctx, AV_LOG_ERROR,
493                "OBMC is only supported with simple mb decision\n");
494         return AVERROR(EINVAL);
495     }
496
497     if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
498         av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
499         return AVERROR(EINVAL);
500     }
501
502     if (s->max_b_frames                    &&
503         s->codec_id != AV_CODEC_ID_MPEG4      &&
504         s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
505         s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
506         av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
507         return AVERROR(EINVAL);
508     }
509     if (s->max_b_frames < 0) {
510         av_log(avctx, AV_LOG_ERROR,
511                "max b frames must be 0 or positive for mpegvideo based encoders\n");
512         return AVERROR(EINVAL);
513     }
514
515     if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
516          s->codec_id == AV_CODEC_ID_H263  ||
517          s->codec_id == AV_CODEC_ID_H263P) &&
518         (avctx->sample_aspect_ratio.num > 255 ||
519          avctx->sample_aspect_ratio.den > 255)) {
520         av_log(avctx, AV_LOG_WARNING,
521                "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
522                avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
523         av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
524                    avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den, 255);
525     }
526
527     if ((s->codec_id == AV_CODEC_ID_H263  ||
528          s->codec_id == AV_CODEC_ID_H263P) &&
529         (avctx->width  > 2048 ||
530          avctx->height > 1152 )) {
531         av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
532         return AVERROR(EINVAL);
533     }
534     if ((s->codec_id == AV_CODEC_ID_H263  ||
535          s->codec_id == AV_CODEC_ID_H263P ||
536          s->codec_id == AV_CODEC_ID_RV20) &&
537         ((avctx->width &3) ||
538          (avctx->height&3) )) {
539         av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
540         return AVERROR(EINVAL);
541     }
542
543     if (s->codec_id == AV_CODEC_ID_RV10 &&
544         (avctx->width &15 ||
545          avctx->height&15 )) {
546         av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
547         return AVERROR(EINVAL);
548     }
549
550     if ((s->codec_id == AV_CODEC_ID_WMV1 ||
551          s->codec_id == AV_CODEC_ID_WMV2) &&
552          avctx->width & 1) {
553         av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
554         return AVERROR(EINVAL);
555     }
556
557     if ((avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
558         s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
559         av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
560         return AVERROR(EINVAL);
561     }
562
563 #if FF_API_PRIVATE_OPT
564     FF_DISABLE_DEPRECATION_WARNINGS
565     if (avctx->mpeg_quant)
566         s->mpeg_quant = 1;
567     FF_ENABLE_DEPRECATION_WARNINGS
568
569     if (s->mpeg_quant && (   s->codec_id != AV_CODEC_ID_MPEG4
570                           && s->codec_id != AV_CODEC_ID_MPEG2VIDEO)) {
571         av_log(avctx, AV_LOG_ERROR,
572                "mpeg2 style quantization not supported by codec\n");
573         return AVERROR(EINVAL);
574     }
575 #endif
576
577     if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
578         av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
579         return AVERROR(EINVAL);
580     }
581
582     if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
583         avctx->mb_decision != FF_MB_DECISION_RD) {
584         av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
585         return AVERROR(EINVAL);
586     }
587
588     if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
589             (s->codec_id == AV_CODEC_ID_AMV ||
590              s->codec_id == AV_CODEC_ID_MJPEG)) {
591         // Used to produce garbage with MJPEG.
592         av_log(avctx, AV_LOG_ERROR,
593                "QP RD is no longer compatible with MJPEG or AMV\n");
594         return AVERROR(EINVAL);
595     }
596
597 #if FF_API_PRIVATE_OPT
598 FF_DISABLE_DEPRECATION_WARNINGS
599     if (avctx->scenechange_threshold)
600         s->scenechange_threshold = avctx->scenechange_threshold;
601 FF_ENABLE_DEPRECATION_WARNINGS
602 #endif
603
604     if (s->scenechange_threshold < 1000000000 &&
605         (avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
606         av_log(avctx, AV_LOG_ERROR,
607                "closed gop with scene change detection are not supported yet, "
608                "set threshold to 1000000000\n");
609         return AVERROR_PATCHWELCOME;
610     }
611
612     if (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
613         if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
614             s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
615             av_log(avctx, AV_LOG_ERROR,
616                    "low delay forcing is only available for mpeg2, "
617                    "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
618             return AVERROR(EINVAL);
619         }
620         if (s->max_b_frames != 0) {
621             av_log(avctx, AV_LOG_ERROR,
622                    "B-frames cannot be used with low delay\n");
623             return AVERROR(EINVAL);
624         }
625     }
626
627     if (s->q_scale_type == 1) {
628         if (avctx->qmax > 28) {
629             av_log(avctx, AV_LOG_ERROR,
630                    "non linear quant only supports qmax <= 28 currently\n");
631             return AVERROR_PATCHWELCOME;
632         }
633     }
634
635     if (avctx->slices > 1 &&
636         (avctx->codec_id == AV_CODEC_ID_FLV1 || avctx->codec_id == AV_CODEC_ID_H261)) {
637         av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
638         return AVERROR(EINVAL);
639     }
640
641     if (avctx->thread_count > 1         &&
642         s->codec_id != AV_CODEC_ID_MPEG4      &&
643         s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
644         s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
645         s->codec_id != AV_CODEC_ID_MJPEG      &&
646         (s->codec_id != AV_CODEC_ID_H263P)) {
647         av_log(avctx, AV_LOG_ERROR,
648                "multi threaded encoding not supported by codec\n");
649         return AVERROR_PATCHWELCOME;
650     }
651
652     if (avctx->thread_count < 1) {
653         av_log(avctx, AV_LOG_ERROR,
654                "automatic thread number detection not supported by codec, "
655                "patch welcome\n");
656         return AVERROR_PATCHWELCOME;
657     }
658
659 #if FF_API_PRIVATE_OPT
660 FF_DISABLE_DEPRECATION_WARNINGS
661     if (avctx->b_frame_strategy)
662         s->b_frame_strategy = avctx->b_frame_strategy;
663     if (avctx->b_sensitivity != 40)
664         s->b_sensitivity = avctx->b_sensitivity;
665 FF_ENABLE_DEPRECATION_WARNINGS
666 #endif
667
668     if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
669         av_log(avctx, AV_LOG_INFO,
670                "notice: b_frame_strategy only affects the first pass\n");
671         s->b_frame_strategy = 0;
672     }
673
674     i = av_gcd(avctx->time_base.den, avctx->time_base.num);
675     if (i > 1) {
676         av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
677         avctx->time_base.den /= i;
678         avctx->time_base.num /= i;
679         //return -1;
680     }
681
682     if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG || s->codec_id == AV_CODEC_ID_AMV || s->codec_id == AV_CODEC_ID_SPEEDHQ) {
683         // (a + x * 3 / 8) / x
684         s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
685         s->inter_quant_bias = 0;
686     } else {
687         s->intra_quant_bias = 0;
688         // (a - x / 4) / x
689         s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
690     }
691
692     if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
693         av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
694         return AVERROR(EINVAL);
695     }
696
697     av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
698
699     if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
700         avctx->time_base.den > (1 << 16) - 1) {
701         av_log(avctx, AV_LOG_ERROR,
702                "timebase %d/%d not supported by MPEG 4 standard, "
703                "the maximum admitted value for the timebase denominator "
704                "is %d\n", avctx->time_base.num, avctx->time_base.den,
705                (1 << 16) - 1);
706         return AVERROR(EINVAL);
707     }
708     s->time_increment_bits = av_log2(avctx->time_base.den - 1) + 1;
709
710     switch (avctx->codec->id) {
711     case AV_CODEC_ID_MPEG1VIDEO:
712         s->out_format = FMT_MPEG1;
713         s->low_delay  = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
714         avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
715         break;
716     case AV_CODEC_ID_MPEG2VIDEO:
717         s->out_format = FMT_MPEG1;
718         s->low_delay  = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
719         avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
720         s->rtp_mode   = 1;
721         break;
722 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
723     case AV_CODEC_ID_MJPEG:
724     case AV_CODEC_ID_AMV:
725         s->out_format = FMT_MJPEG;
726         s->intra_only = 1; /* force intra only for jpeg */
727         if ((ret = ff_mjpeg_encode_init(s)) < 0)
728             return ret;
729         avctx->delay = 0;
730         s->low_delay = 1;
731         break;
732 #endif
733     case AV_CODEC_ID_SPEEDHQ:
734         s->out_format = FMT_SPEEDHQ;
735         s->intra_only = 1; /* force intra only for SHQ */
736         if (!CONFIG_SPEEDHQ_ENCODER)
737             return AVERROR_ENCODER_NOT_FOUND;
738         if ((ret = ff_speedhq_encode_init(s)) < 0)
739             return ret;
740         avctx->delay = 0;
741         s->low_delay = 1;
742         break;
743     case AV_CODEC_ID_H261:
744         if (!CONFIG_H261_ENCODER)
745             return AVERROR_ENCODER_NOT_FOUND;
746         if (ff_h261_get_picture_format(s->width, s->height) < 0) {
747             av_log(avctx, AV_LOG_ERROR,
748                    "The specified picture size of %dx%d is not valid for the "
749                    "H.261 codec.\nValid sizes are 176x144, 352x288\n",
750                     s->width, s->height);
751             return AVERROR(EINVAL);
752         }
753         s->out_format = FMT_H261;
754         avctx->delay  = 0;
755         s->low_delay  = 1;
756         s->rtp_mode   = 0; /* Sliced encoding not supported */
757         break;
758     case AV_CODEC_ID_H263:
759         if (!CONFIG_H263_ENCODER)
760             return AVERROR_ENCODER_NOT_FOUND;
761         if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
762                              s->width, s->height) == 8) {
763             av_log(avctx, AV_LOG_ERROR,
764                    "The specified picture size of %dx%d is not valid for "
765                    "the H.263 codec.\nValid sizes are 128x96, 176x144, "
766                    "352x288, 704x576, and 1408x1152. "
767                    "Try H.263+.\n", s->width, s->height);
768             return AVERROR(EINVAL);
769         }
770         s->out_format = FMT_H263;
771         avctx->delay  = 0;
772         s->low_delay  = 1;
773         break;
774     case AV_CODEC_ID_H263P:
775         s->out_format = FMT_H263;
776         s->h263_plus  = 1;
777         /* Fx */
778         s->h263_aic        = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
779         s->modified_quant  = s->h263_aic;
780         s->loop_filter     = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
781         s->unrestricted_mv = s->obmc || s->loop_filter || s->umvplus;
782
783         /* /Fx */
784         /* These are just to be sure */
785         avctx->delay = 0;
786         s->low_delay = 1;
787         break;
788     case AV_CODEC_ID_FLV1:
789         s->out_format      = FMT_H263;
790         s->h263_flv        = 2; /* format = 1; 11-bit codes */
791         s->unrestricted_mv = 1;
792         s->rtp_mode  = 0; /* don't allow GOB */
793         avctx->delay = 0;
794         s->low_delay = 1;
795         break;
796     case AV_CODEC_ID_RV10:
797         s->out_format = FMT_H263;
798         avctx->delay  = 0;
799         s->low_delay  = 1;
800         break;
801     case AV_CODEC_ID_RV20:
802         s->out_format      = FMT_H263;
803         avctx->delay       = 0;
804         s->low_delay       = 1;
805         s->modified_quant  = 1;
806         s->h263_aic        = 1;
807         s->h263_plus       = 1;
808         s->loop_filter     = 1;
809         s->unrestricted_mv = 0;
810         break;
811     case AV_CODEC_ID_MPEG4:
812         s->out_format      = FMT_H263;
813         s->h263_pred       = 1;
814         s->unrestricted_mv = 1;
815         s->low_delay       = s->max_b_frames ? 0 : 1;
816         avctx->delay       = s->low_delay ? 0 : (s->max_b_frames + 1);
817         break;
818     case AV_CODEC_ID_MSMPEG4V2:
819         s->out_format      = FMT_H263;
820         s->h263_pred       = 1;
821         s->unrestricted_mv = 1;
822         s->msmpeg4_version = 2;
823         avctx->delay       = 0;
824         s->low_delay       = 1;
825         break;
826     case AV_CODEC_ID_MSMPEG4V3:
827         s->out_format        = FMT_H263;
828         s->h263_pred         = 1;
829         s->unrestricted_mv   = 1;
830         s->msmpeg4_version   = 3;
831         s->flipflop_rounding = 1;
832         avctx->delay         = 0;
833         s->low_delay         = 1;
834         break;
835     case AV_CODEC_ID_WMV1:
836         s->out_format        = FMT_H263;
837         s->h263_pred         = 1;
838         s->unrestricted_mv   = 1;
839         s->msmpeg4_version   = 4;
840         s->flipflop_rounding = 1;
841         avctx->delay         = 0;
842         s->low_delay         = 1;
843         break;
844     case AV_CODEC_ID_WMV2:
845         s->out_format        = FMT_H263;
846         s->h263_pred         = 1;
847         s->unrestricted_mv   = 1;
848         s->msmpeg4_version   = 5;
849         s->flipflop_rounding = 1;
850         avctx->delay         = 0;
851         s->low_delay         = 1;
852         break;
853     default:
854         return AVERROR(EINVAL);
855     }
856
857 #if FF_API_PRIVATE_OPT
858     FF_DISABLE_DEPRECATION_WARNINGS
859     if (avctx->noise_reduction)
860         s->noise_reduction = avctx->noise_reduction;
861     FF_ENABLE_DEPRECATION_WARNINGS
862 #endif
863
864     avctx->has_b_frames = !s->low_delay;
865
866     s->encoding = 1;
867
868     s->progressive_frame    =
869     s->progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
870                                                 AV_CODEC_FLAG_INTERLACED_ME) ||
871                                 s->alternate_scan);
872
873     /* init */
874     ff_mpv_idct_init(s);
875     if ((ret = ff_mpv_common_init(s)) < 0)
876         return ret;
877
878     ff_fdctdsp_init(&s->fdsp, avctx);
879     ff_me_cmp_init(&s->mecc, avctx);
880     ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
881     ff_pixblockdsp_init(&s->pdsp, avctx);
882     ff_qpeldsp_init(&s->qdsp);
883
884     if (s->msmpeg4_version) {
885         int ac_stats_size = 2 * 2 * (MAX_LEVEL + 1) *  (MAX_RUN + 1) * 2 * sizeof(int);
886         if (!(s->ac_stats = av_mallocz(ac_stats_size)))
887             return AVERROR(ENOMEM);
888     }
889
890     if (!(avctx->stats_out = av_mallocz(256))               ||
891         !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix,          32) ||
892         !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix,   32) ||
893         !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix,          32) ||
894         !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix16,        32) ||
895         !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32) ||
896         !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix16,        32) ||
897         !FF_ALLOCZ_TYPED_ARRAY(s->input_picture,           MAX_PICTURE_COUNT) ||
898         !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_PICTURE_COUNT))
899         return AVERROR(ENOMEM);
900
901     if (s->noise_reduction) {
902         if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2))
903             return AVERROR(ENOMEM);
904     }
905
906     ff_dct_encode_init(s);
907
908     if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
909         s->chroma_qscale_table = ff_h263_chroma_qscale_table;
910
911     if (s->slice_context_count > 1) {
912         s->rtp_mode = 1;
913
914         if (avctx->codec_id == AV_CODEC_ID_H263P)
915             s->h263_slice_structured = 1;
916     }
917
918     s->quant_precision = 5;
919
920 #if FF_API_PRIVATE_OPT
921 FF_DISABLE_DEPRECATION_WARNINGS
922     if (avctx->frame_skip_threshold)
923         s->frame_skip_threshold = avctx->frame_skip_threshold;
924     if (avctx->frame_skip_factor)
925         s->frame_skip_factor = avctx->frame_skip_factor;
926     if (avctx->frame_skip_exp)
927         s->frame_skip_exp = avctx->frame_skip_exp;
928     if (avctx->frame_skip_cmp != FF_CMP_DCTMAX)
929         s->frame_skip_cmp = avctx->frame_skip_cmp;
930 FF_ENABLE_DEPRECATION_WARNINGS
931 #endif
932
933     ff_set_cmp(&s->mecc, s->mecc.ildct_cmp,      avctx->ildct_cmp);
934     ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->frame_skip_cmp);
935
936     if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
937         ff_h261_encode_init(s);
938     if (CONFIG_H263_ENCODER && s->out_format == FMT_H263)
939         ff_h263_encode_init(s);
940     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
941         ff_msmpeg4_encode_init(s);
942     if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
943         && s->out_format == FMT_MPEG1)
944         ff_mpeg1_encode_init(s);
945
946     /* init q matrix */
947     for (i = 0; i < 64; i++) {
948         int j = s->idsp.idct_permutation[i];
949         if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
950             s->mpeg_quant) {
951             s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
952             s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
953         } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
954             s->intra_matrix[j] =
955             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
956         } else if (CONFIG_SPEEDHQ_ENCODER && s->codec_id == AV_CODEC_ID_SPEEDHQ) {
957             s->intra_matrix[j] =
958             s->inter_matrix[j] = ff_mpeg1_default_intra_matrix[i];
959         } else {
960             /* MPEG-1/2 */
961             s->chroma_intra_matrix[j] =
962             s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
963             s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
964         }
965         if (avctx->intra_matrix)
966             s->intra_matrix[j] = avctx->intra_matrix[i];
967         if (avctx->inter_matrix)
968             s->inter_matrix[j] = avctx->inter_matrix[i];
969     }
970
971     /* precompute matrix */
972     /* for mjpeg, we do include qscale in the matrix */
973     if (s->out_format != FMT_MJPEG) {
974         ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
975                           s->intra_matrix, s->intra_quant_bias, avctx->qmin,
976                           31, 1);
977         ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
978                           s->inter_matrix, s->inter_quant_bias, avctx->qmin,
979                           31, 0);
980     }
981
982     if ((ret = ff_rate_control_init(s)) < 0)
983         return ret;
984
985 #if FF_API_PRIVATE_OPT
986     FF_DISABLE_DEPRECATION_WARNINGS
987     if (avctx->brd_scale)
988         s->brd_scale = avctx->brd_scale;
989
990     if (avctx->prediction_method)
991         s->pred = avctx->prediction_method + 1;
992     FF_ENABLE_DEPRECATION_WARNINGS
993 #endif
994
995     if (s->b_frame_strategy == 2) {
996         for (i = 0; i < s->max_b_frames + 2; i++) {
997             s->tmp_frames[i] = av_frame_alloc();
998             if (!s->tmp_frames[i])
999                 return AVERROR(ENOMEM);
1000
1001             s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
1002             s->tmp_frames[i]->width  = s->width  >> s->brd_scale;
1003             s->tmp_frames[i]->height = s->height >> s->brd_scale;
1004
1005             ret = av_frame_get_buffer(s->tmp_frames[i], 0);
1006             if (ret < 0)
1007                 return ret;
1008         }
1009     }
1010
1011     cpb_props = ff_add_cpb_side_data(avctx);
1012     if (!cpb_props)
1013         return AVERROR(ENOMEM);
1014     cpb_props->max_bitrate = avctx->rc_max_rate;
1015     cpb_props->min_bitrate = avctx->rc_min_rate;
1016     cpb_props->avg_bitrate = avctx->bit_rate;
1017     cpb_props->buffer_size = avctx->rc_buffer_size;
1018
1019     return 0;
1020 }
1021
1022 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
1023 {
1024     MpegEncContext *s = avctx->priv_data;
1025     int i;
1026
1027     ff_rate_control_uninit(s);
1028
1029     ff_mpv_common_end(s);
1030     if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
1031         s->out_format == FMT_MJPEG)
1032         ff_mjpeg_encode_close(s);
1033
1034     av_freep(&avctx->extradata);
1035
1036     for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
1037         av_frame_free(&s->tmp_frames[i]);
1038
1039     ff_free_picture_tables(&s->new_picture);
1040     ff_mpeg_unref_picture(avctx, &s->new_picture);
1041
1042     av_freep(&avctx->stats_out);
1043     av_freep(&s->ac_stats);
1044
1045     if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
1046     if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
1047     s->q_chroma_intra_matrix=   NULL;
1048     s->q_chroma_intra_matrix16= NULL;
1049     av_freep(&s->q_intra_matrix);
1050     av_freep(&s->q_inter_matrix);
1051     av_freep(&s->q_intra_matrix16);
1052     av_freep(&s->q_inter_matrix16);
1053     av_freep(&s->input_picture);
1054     av_freep(&s->reordered_input_picture);
1055     av_freep(&s->dct_offset);
1056
1057     return 0;
1058 }
1059
1060 static int get_sae(uint8_t *src, int ref, int stride)
1061 {
1062     int x,y;
1063     int acc = 0;
1064
1065     for (y = 0; y < 16; y++) {
1066         for (x = 0; x < 16; x++) {
1067             acc += FFABS(src[x + y * stride] - ref);
1068         }
1069     }
1070
1071     return acc;
1072 }
1073
1074 static int get_intra_count(MpegEncContext *s, uint8_t *src,
1075                            uint8_t *ref, int stride)
1076 {
1077     int x, y, w, h;
1078     int acc = 0;
1079
1080     w = s->width  & ~15;
1081     h = s->height & ~15;
1082
1083     for (y = 0; y < h; y += 16) {
1084         for (x = 0; x < w; x += 16) {
1085             int offset = x + y * stride;
1086             int sad  = s->mecc.sad[0](NULL, src + offset, ref + offset,
1087                                       stride, 16);
1088             int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
1089             int sae  = get_sae(src + offset, mean, stride);
1090
1091             acc += sae + 500 < sad;
1092         }
1093     }
1094     return acc;
1095 }
1096
1097 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
1098 {
1099     return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
1100                             s->chroma_x_shift, s->chroma_y_shift, s->out_format,
1101                             s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
1102                             &s->linesize, &s->uvlinesize);
1103 }
1104
1105 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
1106 {
1107     Picture *pic = NULL;
1108     int64_t pts;
1109     int i, display_picture_number = 0, ret;
1110     int encoding_delay = s->max_b_frames ? s->max_b_frames
1111                                          : (s->low_delay ? 0 : 1);
1112     int flush_offset = 1;
1113     int direct = 1;
1114
1115     if (pic_arg) {
1116         pts = pic_arg->pts;
1117         display_picture_number = s->input_picture_number++;
1118
1119         if (pts != AV_NOPTS_VALUE) {
1120             if (s->user_specified_pts != AV_NOPTS_VALUE) {
1121                 int64_t last = s->user_specified_pts;
1122
1123                 if (pts <= last) {
1124                     av_log(s->avctx, AV_LOG_ERROR,
1125                            "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
1126                            pts, last);
1127                     return AVERROR(EINVAL);
1128                 }
1129
1130                 if (!s->low_delay && display_picture_number == 1)
1131                     s->dts_delta = pts - last;
1132             }
1133             s->user_specified_pts = pts;
1134         } else {
1135             if (s->user_specified_pts != AV_NOPTS_VALUE) {
1136                 s->user_specified_pts =
1137                 pts = s->user_specified_pts + 1;
1138                 av_log(s->avctx, AV_LOG_INFO,
1139                        "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1140                        pts);
1141             } else {
1142                 pts = display_picture_number;
1143             }
1144         }
1145
1146         if (!pic_arg->buf[0] ||
1147             pic_arg->linesize[0] != s->linesize ||
1148             pic_arg->linesize[1] != s->uvlinesize ||
1149             pic_arg->linesize[2] != s->uvlinesize)
1150             direct = 0;
1151         if ((s->width & 15) || (s->height & 15))
1152             direct = 0;
1153         if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1154             direct = 0;
1155         if (s->linesize & (STRIDE_ALIGN-1))
1156             direct = 0;
1157
1158         ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
1159                 pic_arg->linesize[1], s->linesize, s->uvlinesize);
1160
1161         i = ff_find_unused_picture(s->avctx, s->picture, direct);
1162         if (i < 0)
1163             return i;
1164
1165         pic = &s->picture[i];
1166         pic->reference = 3;
1167
1168         if (direct) {
1169             if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1170                 return ret;
1171         }
1172         ret = alloc_picture(s, pic, direct);
1173         if (ret < 0)
1174             return ret;
1175
1176         if (!direct) {
1177             if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1178                 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1179                 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1180                 // empty
1181             } else {
1182                 int h_chroma_shift, v_chroma_shift;
1183                 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1184                                                  &h_chroma_shift,
1185                                                  &v_chroma_shift);
1186
1187                 for (i = 0; i < 3; i++) {
1188                     int src_stride = pic_arg->linesize[i];
1189                     int dst_stride = i ? s->uvlinesize : s->linesize;
1190                     int h_shift = i ? h_chroma_shift : 0;
1191                     int v_shift = i ? v_chroma_shift : 0;
1192                     int w = s->width  >> h_shift;
1193                     int h = s->height >> v_shift;
1194                     uint8_t *src = pic_arg->data[i];
1195                     uint8_t *dst = pic->f->data[i];
1196                     int vpad = 16;
1197
1198                     if (   s->codec_id == AV_CODEC_ID_MPEG2VIDEO
1199                         && !s->progressive_sequence
1200                         && FFALIGN(s->height, 32) - s->height > 16)
1201                         vpad = 32;
1202
1203                     if (!s->avctx->rc_buffer_size)
1204                         dst += INPLACE_OFFSET;
1205
1206                     if (src_stride == dst_stride)
1207                         memcpy(dst, src, src_stride * h);
1208                     else {
1209                         int h2 = h;
1210                         uint8_t *dst2 = dst;
1211                         while (h2--) {
1212                             memcpy(dst2, src, w);
1213                             dst2 += dst_stride;
1214                             src += src_stride;
1215                         }
1216                     }
1217                     if ((s->width & 15) || (s->height & (vpad-1))) {
1218                         s->mpvencdsp.draw_edges(dst, dst_stride,
1219                                                 w, h,
1220                                                 16 >> h_shift,
1221                                                 vpad >> v_shift,
1222                                                 EDGE_BOTTOM);
1223                     }
1224                 }
1225                 emms_c();
1226             }
1227         }
1228         ret = av_frame_copy_props(pic->f, pic_arg);
1229         if (ret < 0)
1230             return ret;
1231
1232         pic->f->display_picture_number = display_picture_number;
1233         pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1234     } else {
1235         /* Flushing: When we have not received enough input frames,
1236          * ensure s->input_picture[0] contains the first picture */
1237         for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1238             if (s->input_picture[flush_offset])
1239                 break;
1240
1241         if (flush_offset <= 1)
1242             flush_offset = 1;
1243         else
1244             encoding_delay = encoding_delay - flush_offset + 1;
1245     }
1246
1247     /* shift buffer entries */
1248     for (i = flush_offset; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1249         s->input_picture[i - flush_offset] = s->input_picture[i];
1250
1251     s->input_picture[encoding_delay] = (Picture*) pic;
1252
1253     return 0;
1254 }
1255
1256 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1257 {
1258     int x, y, plane;
1259     int score = 0;
1260     int64_t score64 = 0;
1261
1262     for (plane = 0; plane < 3; plane++) {
1263         const int stride = p->f->linesize[plane];
1264         const int bw = plane ? 1 : 2;
1265         for (y = 0; y < s->mb_height * bw; y++) {
1266             for (x = 0; x < s->mb_width * bw; x++) {
1267                 int off = p->shared ? 0 : 16;
1268                 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1269                 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1270                 int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1271
1272                 switch (FFABS(s->frame_skip_exp)) {
1273                 case 0: score    =  FFMAX(score, v);          break;
1274                 case 1: score   += FFABS(v);                  break;
1275                 case 2: score64 += v * (int64_t)v;                       break;
1276                 case 3: score64 += FFABS(v * (int64_t)v * v);            break;
1277                 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);  break;
1278                 }
1279             }
1280         }
1281     }
1282     emms_c();
1283
1284     if (score)
1285         score64 = score;
1286     if (s->frame_skip_exp < 0)
1287         score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
1288                       -1.0/s->frame_skip_exp);
1289
1290     if (score64 < s->frame_skip_threshold)
1291         return 1;
1292     if (score64 < ((s->frame_skip_factor * (int64_t) s->lambda) >> 8))
1293         return 1;
1294     return 0;
1295 }
1296
1297 static int encode_frame(AVCodecContext *c, AVFrame *frame, AVPacket *pkt)
1298 {
1299     int ret;
1300     int size = 0;
1301
1302     ret = avcodec_send_frame(c, frame);
1303     if (ret < 0)
1304         return ret;
1305
1306     do {
1307         ret = avcodec_receive_packet(c, pkt);
1308         if (ret >= 0) {
1309             size += pkt->size;
1310             av_packet_unref(pkt);
1311         } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1312             return ret;
1313     } while (ret >= 0);
1314
1315     return size;
1316 }
1317
1318 static int estimate_best_b_count(MpegEncContext *s)
1319 {
1320     const AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1321     AVPacket *pkt;
1322     const int scale = s->brd_scale;
1323     int width  = s->width  >> scale;
1324     int height = s->height >> scale;
1325     int i, j, out_size, p_lambda, b_lambda, lambda2;
1326     int64_t best_rd  = INT64_MAX;
1327     int best_b_count = -1;
1328     int ret = 0;
1329
1330     av_assert0(scale >= 0 && scale <= 3);
1331
1332     pkt = av_packet_alloc();
1333     if (!pkt)
1334         return AVERROR(ENOMEM);
1335
1336     //emms_c();
1337     //s->next_picture_ptr->quality;
1338     p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1339     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1340     b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1341     if (!b_lambda) // FIXME we should do this somewhere else
1342         b_lambda = p_lambda;
1343     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1344                FF_LAMBDA_SHIFT;
1345
1346     for (i = 0; i < s->max_b_frames + 2; i++) {
1347         Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1348                                                 s->next_picture_ptr;
1349         uint8_t *data[4];
1350
1351         if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1352             pre_input = *pre_input_ptr;
1353             memcpy(data, pre_input_ptr->f->data, sizeof(data));
1354
1355             if (!pre_input.shared && i) {
1356                 data[0] += INPLACE_OFFSET;
1357                 data[1] += INPLACE_OFFSET;
1358                 data[2] += INPLACE_OFFSET;
1359             }
1360
1361             s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1362                                        s->tmp_frames[i]->linesize[0],
1363                                        data[0],
1364                                        pre_input.f->linesize[0],
1365                                        width, height);
1366             s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1367                                        s->tmp_frames[i]->linesize[1],
1368                                        data[1],
1369                                        pre_input.f->linesize[1],
1370                                        width >> 1, height >> 1);
1371             s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1372                                        s->tmp_frames[i]->linesize[2],
1373                                        data[2],
1374                                        pre_input.f->linesize[2],
1375                                        width >> 1, height >> 1);
1376         }
1377     }
1378
1379     for (j = 0; j < s->max_b_frames + 1; j++) {
1380         AVCodecContext *c;
1381         int64_t rd = 0;
1382
1383         if (!s->input_picture[j])
1384             break;
1385
1386         c = avcodec_alloc_context3(NULL);
1387         if (!c) {
1388             ret = AVERROR(ENOMEM);
1389             goto fail;
1390         }
1391
1392         c->width        = width;
1393         c->height       = height;
1394         c->flags        = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
1395         c->flags       |= s->avctx->flags & AV_CODEC_FLAG_QPEL;
1396         c->mb_decision  = s->avctx->mb_decision;
1397         c->me_cmp       = s->avctx->me_cmp;
1398         c->mb_cmp       = s->avctx->mb_cmp;
1399         c->me_sub_cmp   = s->avctx->me_sub_cmp;
1400         c->pix_fmt      = AV_PIX_FMT_YUV420P;
1401         c->time_base    = s->avctx->time_base;
1402         c->max_b_frames = s->max_b_frames;
1403
1404         ret = avcodec_open2(c, codec, NULL);
1405         if (ret < 0)
1406             goto fail;
1407
1408
1409         s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1410         s->tmp_frames[0]->quality   = 1 * FF_QP2LAMBDA;
1411
1412         out_size = encode_frame(c, s->tmp_frames[0], pkt);
1413         if (out_size < 0) {
1414             ret = out_size;
1415             goto fail;
1416         }
1417
1418         //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1419
1420         for (i = 0; i < s->max_b_frames + 1; i++) {
1421             int is_p = i % (j + 1) == j || i == s->max_b_frames;
1422
1423             s->tmp_frames[i + 1]->pict_type = is_p ?
1424                                      AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1425             s->tmp_frames[i + 1]->quality   = is_p ? p_lambda : b_lambda;
1426
1427             out_size = encode_frame(c, s->tmp_frames[i + 1], pkt);
1428             if (out_size < 0) {
1429                 ret = out_size;
1430                 goto fail;
1431             }
1432
1433             rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1434         }
1435
1436         /* get the delayed frames */
1437         out_size = encode_frame(c, NULL, pkt);
1438         if (out_size < 0) {
1439             ret = out_size;
1440             goto fail;
1441         }
1442         rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1443
1444         rd += c->error[0] + c->error[1] + c->error[2];
1445
1446         if (rd < best_rd) {
1447             best_rd = rd;
1448             best_b_count = j;
1449         }
1450
1451 fail:
1452         avcodec_free_context(&c);
1453         av_packet_unref(pkt);
1454         if (ret < 0) {
1455             best_b_count = ret;
1456             break;
1457         }
1458     }
1459
1460     av_packet_free(&pkt);
1461
1462     return best_b_count;
1463 }
1464
1465 static int select_input_picture(MpegEncContext *s)
1466 {
1467     int i, ret;
1468
1469     for (i = 1; i < MAX_PICTURE_COUNT; i++)
1470         s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1471     s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1472
1473     /* set next picture type & ordering */
1474     if (!s->reordered_input_picture[0] && s->input_picture[0]) {
1475         if (s->frame_skip_threshold || s->frame_skip_factor) {
1476             if (s->picture_in_gop_number < s->gop_size &&
1477                 s->next_picture_ptr &&
1478                 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1479                 // FIXME check that the gop check above is +-1 correct
1480                 av_frame_unref(s->input_picture[0]->f);
1481
1482                 ff_vbv_update(s, 0);
1483
1484                 goto no_output_pic;
1485             }
1486         }
1487
1488         if (/*s->picture_in_gop_number >= s->gop_size ||*/
1489             !s->next_picture_ptr || s->intra_only) {
1490             s->reordered_input_picture[0] = s->input_picture[0];
1491             s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1492             s->reordered_input_picture[0]->f->coded_picture_number =
1493                 s->coded_picture_number++;
1494         } else {
1495             int b_frames = 0;
1496
1497             if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
1498                 for (i = 0; i < s->max_b_frames + 1; i++) {
1499                     int pict_num = s->input_picture[0]->f->display_picture_number + i;
1500
1501                     if (pict_num >= s->rc_context.num_entries)
1502                         break;
1503                     if (!s->input_picture[i]) {
1504                         s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1505                         break;
1506                     }
1507
1508                     s->input_picture[i]->f->pict_type =
1509                         s->rc_context.entry[pict_num].new_pict_type;
1510                 }
1511             }
1512
1513             if (s->b_frame_strategy == 0) {
1514                 b_frames = s->max_b_frames;
1515                 while (b_frames && !s->input_picture[b_frames])
1516                     b_frames--;
1517             } else if (s->b_frame_strategy == 1) {
1518                 for (i = 1; i < s->max_b_frames + 1; i++) {
1519                     if (s->input_picture[i] &&
1520                         s->input_picture[i]->b_frame_score == 0) {
1521                         s->input_picture[i]->b_frame_score =
1522                             get_intra_count(s,
1523                                             s->input_picture[i    ]->f->data[0],
1524                                             s->input_picture[i - 1]->f->data[0],
1525                                             s->linesize) + 1;
1526                     }
1527                 }
1528                 for (i = 0; i < s->max_b_frames + 1; i++) {
1529                     if (!s->input_picture[i] ||
1530                         s->input_picture[i]->b_frame_score - 1 >
1531                             s->mb_num / s->b_sensitivity)
1532                         break;
1533                 }
1534
1535                 b_frames = FFMAX(0, i - 1);
1536
1537                 /* reset scores */
1538                 for (i = 0; i < b_frames + 1; i++) {
1539                     s->input_picture[i]->b_frame_score = 0;
1540                 }
1541             } else if (s->b_frame_strategy == 2) {
1542                 b_frames = estimate_best_b_count(s);
1543                 if (b_frames < 0)
1544                     return b_frames;
1545             }
1546
1547             emms_c();
1548
1549             for (i = b_frames - 1; i >= 0; i--) {
1550                 int type = s->input_picture[i]->f->pict_type;
1551                 if (type && type != AV_PICTURE_TYPE_B)
1552                     b_frames = i;
1553             }
1554             if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1555                 b_frames == s->max_b_frames) {
1556                 av_log(s->avctx, AV_LOG_ERROR,
1557                        "warning, too many B-frames in a row\n");
1558             }
1559
1560             if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1561                 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1562                     s->gop_size > s->picture_in_gop_number) {
1563                     b_frames = s->gop_size - s->picture_in_gop_number - 1;
1564                 } else {
1565                     if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1566                         b_frames = 0;
1567                     s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1568                 }
1569             }
1570
1571             if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1572                 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1573                 b_frames--;
1574
1575             s->reordered_input_picture[0] = s->input_picture[b_frames];
1576             if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1577                 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1578             s->reordered_input_picture[0]->f->coded_picture_number =
1579                 s->coded_picture_number++;
1580             for (i = 0; i < b_frames; i++) {
1581                 s->reordered_input_picture[i + 1] = s->input_picture[i];
1582                 s->reordered_input_picture[i + 1]->f->pict_type =
1583                     AV_PICTURE_TYPE_B;
1584                 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1585                     s->coded_picture_number++;
1586             }
1587         }
1588     }
1589 no_output_pic:
1590     ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1591
1592     if (s->reordered_input_picture[0]) {
1593         s->reordered_input_picture[0]->reference =
1594            s->reordered_input_picture[0]->f->pict_type !=
1595                AV_PICTURE_TYPE_B ? 3 : 0;
1596
1597         if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
1598             return ret;
1599
1600         if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1601             // input is a shared pix, so we can't modify it -> allocate a new
1602             // one & ensure that the shared one is reuseable
1603
1604             Picture *pic;
1605             int i = ff_find_unused_picture(s->avctx, s->picture, 0);
1606             if (i < 0)
1607                 return i;
1608             pic = &s->picture[i];
1609
1610             pic->reference = s->reordered_input_picture[0]->reference;
1611             if (alloc_picture(s, pic, 0) < 0) {
1612                 return -1;
1613             }
1614
1615             ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1616             if (ret < 0)
1617                 return ret;
1618
1619             /* mark us unused / free shared pic */
1620             av_frame_unref(s->reordered_input_picture[0]->f);
1621             s->reordered_input_picture[0]->shared = 0;
1622
1623             s->current_picture_ptr = pic;
1624         } else {
1625             // input is not a shared pix -> reuse buffer for current_pix
1626             s->current_picture_ptr = s->reordered_input_picture[0];
1627             for (i = 0; i < 4; i++) {
1628                 if (s->new_picture.f->data[i])
1629                     s->new_picture.f->data[i] += INPLACE_OFFSET;
1630             }
1631         }
1632         ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1633         if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1634                                        s->current_picture_ptr)) < 0)
1635             return ret;
1636
1637         s->picture_number = s->new_picture.f->display_picture_number;
1638     }
1639     return 0;
1640 }
1641
1642 static void frame_end(MpegEncContext *s)
1643 {
1644     if (s->unrestricted_mv &&
1645         s->current_picture.reference &&
1646         !s->intra_only) {
1647         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1648         int hshift = desc->log2_chroma_w;
1649         int vshift = desc->log2_chroma_h;
1650         s->mpvencdsp.draw_edges(s->current_picture.f->data[0],
1651                                 s->current_picture.f->linesize[0],
1652                                 s->h_edge_pos, s->v_edge_pos,
1653                                 EDGE_WIDTH, EDGE_WIDTH,
1654                                 EDGE_TOP | EDGE_BOTTOM);
1655         s->mpvencdsp.draw_edges(s->current_picture.f->data[1],
1656                                 s->current_picture.f->linesize[1],
1657                                 s->h_edge_pos >> hshift,
1658                                 s->v_edge_pos >> vshift,
1659                                 EDGE_WIDTH >> hshift,
1660                                 EDGE_WIDTH >> vshift,
1661                                 EDGE_TOP | EDGE_BOTTOM);
1662         s->mpvencdsp.draw_edges(s->current_picture.f->data[2],
1663                                 s->current_picture.f->linesize[2],
1664                                 s->h_edge_pos >> hshift,
1665                                 s->v_edge_pos >> vshift,
1666                                 EDGE_WIDTH >> hshift,
1667                                 EDGE_WIDTH >> vshift,
1668                                 EDGE_TOP | EDGE_BOTTOM);
1669     }
1670
1671     emms_c();
1672
1673     s->last_pict_type                 = s->pict_type;
1674     s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1675     if (s->pict_type!= AV_PICTURE_TYPE_B)
1676         s->last_non_b_pict_type = s->pict_type;
1677
1678 #if FF_API_CODED_FRAME
1679 FF_DISABLE_DEPRECATION_WARNINGS
1680     av_frame_unref(s->avctx->coded_frame);
1681     av_frame_copy_props(s->avctx->coded_frame, s->current_picture.f);
1682 FF_ENABLE_DEPRECATION_WARNINGS
1683 #endif
1684 #if FF_API_ERROR_FRAME
1685 FF_DISABLE_DEPRECATION_WARNINGS
1686     memcpy(s->current_picture.f->error, s->current_picture.encoding_error,
1687            sizeof(s->current_picture.encoding_error));
1688 FF_ENABLE_DEPRECATION_WARNINGS
1689 #endif
1690 }
1691
1692 static void update_noise_reduction(MpegEncContext *s)
1693 {
1694     int intra, i;
1695
1696     for (intra = 0; intra < 2; intra++) {
1697         if (s->dct_count[intra] > (1 << 16)) {
1698             for (i = 0; i < 64; i++) {
1699                 s->dct_error_sum[intra][i] >>= 1;
1700             }
1701             s->dct_count[intra] >>= 1;
1702         }
1703
1704         for (i = 0; i < 64; i++) {
1705             s->dct_offset[intra][i] = (s->noise_reduction *
1706                                        s->dct_count[intra] +
1707                                        s->dct_error_sum[intra][i] / 2) /
1708                                       (s->dct_error_sum[intra][i] + 1);
1709         }
1710     }
1711 }
1712
1713 static int frame_start(MpegEncContext *s)
1714 {
1715     int ret;
1716
1717     /* mark & release old frames */
1718     if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1719         s->last_picture_ptr != s->next_picture_ptr &&
1720         s->last_picture_ptr->f->buf[0]) {
1721         ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1722     }
1723
1724     s->current_picture_ptr->f->pict_type = s->pict_type;
1725     s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1726
1727     ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1728     if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1729                                    s->current_picture_ptr)) < 0)
1730         return ret;
1731
1732     if (s->pict_type != AV_PICTURE_TYPE_B) {
1733         s->last_picture_ptr = s->next_picture_ptr;
1734         if (!s->droppable)
1735             s->next_picture_ptr = s->current_picture_ptr;
1736     }
1737
1738     if (s->last_picture_ptr) {
1739         ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1740         if (s->last_picture_ptr->f->buf[0] &&
1741             (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1742                                        s->last_picture_ptr)) < 0)
1743             return ret;
1744     }
1745     if (s->next_picture_ptr) {
1746         ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1747         if (s->next_picture_ptr->f->buf[0] &&
1748             (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1749                                        s->next_picture_ptr)) < 0)
1750             return ret;
1751     }
1752
1753     if (s->picture_structure!= PICT_FRAME) {
1754         int i;
1755         for (i = 0; i < 4; i++) {
1756             if (s->picture_structure == PICT_BOTTOM_FIELD) {
1757                 s->current_picture.f->data[i] +=
1758                     s->current_picture.f->linesize[i];
1759             }
1760             s->current_picture.f->linesize[i] *= 2;
1761             s->last_picture.f->linesize[i]    *= 2;
1762             s->next_picture.f->linesize[i]    *= 2;
1763         }
1764     }
1765
1766     if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1767         s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1768         s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1769     } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1770         s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1771         s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1772     } else {
1773         s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1774         s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1775     }
1776
1777     if (s->dct_error_sum) {
1778         av_assert2(s->noise_reduction && s->encoding);
1779         update_noise_reduction(s);
1780     }
1781
1782     return 0;
1783 }
1784
1785 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1786                           const AVFrame *pic_arg, int *got_packet)
1787 {
1788     MpegEncContext *s = avctx->priv_data;
1789     int i, stuffing_count, ret;
1790     int context_count = s->slice_context_count;
1791
1792     s->vbv_ignore_qmax = 0;
1793
1794     s->picture_in_gop_number++;
1795
1796     if (load_input_picture(s, pic_arg) < 0)
1797         return -1;
1798
1799     if (select_input_picture(s) < 0) {
1800         return -1;
1801     }
1802
1803     /* output? */
1804     if (s->new_picture.f->data[0]) {
1805         int growing_buffer = context_count == 1 && !pkt->data && !s->data_partitioning;
1806         int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - AV_INPUT_BUFFER_PADDING_SIZE
1807                                               :
1808                                               s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
1809         if ((ret = ff_alloc_packet2(avctx, pkt, pkt_size, 0)) < 0)
1810             return ret;
1811         if (s->mb_info) {
1812             s->mb_info_ptr = av_packet_new_side_data(pkt,
1813                                  AV_PKT_DATA_H263_MB_INFO,
1814                                  s->mb_width*s->mb_height*12);
1815             s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1816         }
1817
1818         for (i = 0; i < context_count; i++) {
1819             int start_y = s->thread_context[i]->start_mb_y;
1820             int   end_y = s->thread_context[i]->  end_mb_y;
1821             int h       = s->mb_height;
1822             uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1823             uint8_t *end   = pkt->data + (size_t)(((int64_t) pkt->size) *   end_y / h);
1824
1825             init_put_bits(&s->thread_context[i]->pb, start, end - start);
1826         }
1827
1828         s->pict_type = s->new_picture.f->pict_type;
1829         //emms_c();
1830         ret = frame_start(s);
1831         if (ret < 0)
1832             return ret;
1833 vbv_retry:
1834         ret = encode_picture(s, s->picture_number);
1835         if (growing_buffer) {
1836             av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1837             pkt->data = s->pb.buf;
1838             pkt->size = avctx->internal->byte_buffer_size;
1839         }
1840         if (ret < 0)
1841             return -1;
1842
1843 #if FF_API_STAT_BITS
1844 FF_DISABLE_DEPRECATION_WARNINGS
1845         avctx->header_bits = s->header_bits;
1846         avctx->mv_bits     = s->mv_bits;
1847         avctx->misc_bits   = s->misc_bits;
1848         avctx->i_tex_bits  = s->i_tex_bits;
1849         avctx->p_tex_bits  = s->p_tex_bits;
1850         avctx->i_count     = s->i_count;
1851         // FIXME f/b_count in avctx
1852         avctx->p_count     = s->mb_num - s->i_count - s->skip_count;
1853         avctx->skip_count  = s->skip_count;
1854 FF_ENABLE_DEPRECATION_WARNINGS
1855 #endif
1856
1857         frame_end(s);
1858
1859        if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->out_format == FMT_MJPEG)
1860             ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1861
1862         if (avctx->rc_buffer_size) {
1863             RateControlContext *rcc = &s->rc_context;
1864             int max_size = FFMAX(rcc->buffer_index * avctx->rc_max_available_vbv_use, rcc->buffer_index - 500);
1865             int hq = (avctx->mb_decision == FF_MB_DECISION_RD || avctx->trellis);
1866             int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
1867
1868             if (put_bits_count(&s->pb) > max_size &&
1869                 s->lambda < s->lmax) {
1870                 s->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
1871                                        (s->qscale + 1) / s->qscale);
1872                 if (s->adaptive_quant) {
1873                     int i;
1874                     for (i = 0; i < s->mb_height * s->mb_stride; i++)
1875                         s->lambda_table[i] =
1876                             FFMAX(s->lambda_table[i] + min_step,
1877                                   s->lambda_table[i] * (s->qscale + 1) /
1878                                   s->qscale);
1879                 }
1880                 s->mb_skipped = 0;        // done in frame_start()
1881                 // done in encode_picture() so we must undo it
1882                 if (s->pict_type == AV_PICTURE_TYPE_P) {
1883                     if (s->flipflop_rounding          ||
1884                         s->codec_id == AV_CODEC_ID_H263P ||
1885                         s->codec_id == AV_CODEC_ID_MPEG4)
1886                         s->no_rounding ^= 1;
1887                 }
1888                 if (s->pict_type != AV_PICTURE_TYPE_B) {
1889                     s->time_base       = s->last_time_base;
1890                     s->last_non_b_time = s->time - s->pp_time;
1891                 }
1892                 for (i = 0; i < context_count; i++) {
1893                     PutBitContext *pb = &s->thread_context[i]->pb;
1894                     init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1895                 }
1896                 s->vbv_ignore_qmax = 1;
1897                 av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
1898                 goto vbv_retry;
1899             }
1900
1901             av_assert0(avctx->rc_max_rate);
1902         }
1903
1904         if (avctx->flags & AV_CODEC_FLAG_PASS1)
1905             ff_write_pass1_stats(s);
1906
1907         for (i = 0; i < 4; i++) {
1908             s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
1909             avctx->error[i] += s->current_picture_ptr->encoding_error[i];
1910         }
1911         ff_side_data_set_encoder_stats(pkt, s->current_picture.f->quality,
1912                                        s->current_picture_ptr->encoding_error,
1913                                        (avctx->flags&AV_CODEC_FLAG_PSNR) ? 4 : 0,
1914                                        s->pict_type);
1915
1916         if (avctx->flags & AV_CODEC_FLAG_PASS1)
1917             assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
1918                                              s->misc_bits + s->i_tex_bits +
1919                                              s->p_tex_bits);
1920         flush_put_bits(&s->pb);
1921         s->frame_bits  = put_bits_count(&s->pb);
1922
1923         stuffing_count = ff_vbv_update(s, s->frame_bits);
1924         s->stuffing_bits = 8*stuffing_count;
1925         if (stuffing_count) {
1926             if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
1927                 av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
1928                 return -1;
1929             }
1930
1931             switch (s->codec_id) {
1932             case AV_CODEC_ID_MPEG1VIDEO:
1933             case AV_CODEC_ID_MPEG2VIDEO:
1934                 while (stuffing_count--) {
1935                     put_bits(&s->pb, 8, 0);
1936                 }
1937             break;
1938             case AV_CODEC_ID_MPEG4:
1939                 put_bits(&s->pb, 16, 0);
1940                 put_bits(&s->pb, 16, 0x1C3);
1941                 stuffing_count -= 4;
1942                 while (stuffing_count--) {
1943                     put_bits(&s->pb, 8, 0xFF);
1944                 }
1945             break;
1946             default:
1947                 av_log(avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1948             }
1949             flush_put_bits(&s->pb);
1950             s->frame_bits  = put_bits_count(&s->pb);
1951         }
1952
1953         /* update MPEG-1/2 vbv_delay for CBR */
1954         if (avctx->rc_max_rate                          &&
1955             avctx->rc_min_rate == avctx->rc_max_rate &&
1956             s->out_format == FMT_MPEG1                     &&
1957             90000LL * (avctx->rc_buffer_size - 1) <=
1958                 avctx->rc_max_rate * 0xFFFFLL) {
1959             AVCPBProperties *props;
1960             size_t props_size;
1961
1962             int vbv_delay, min_delay;
1963             double inbits  = avctx->rc_max_rate *
1964                              av_q2d(avctx->time_base);
1965             int    minbits = s->frame_bits - 8 *
1966                              (s->vbv_delay_ptr - s->pb.buf - 1);
1967             double bits    = s->rc_context.buffer_index + minbits - inbits;
1968
1969             if (bits < 0)
1970                 av_log(avctx, AV_LOG_ERROR,
1971                        "Internal error, negative bits\n");
1972
1973             av_assert1(s->repeat_first_field == 0);
1974
1975             vbv_delay = bits * 90000 / avctx->rc_max_rate;
1976             min_delay = (minbits * 90000LL + avctx->rc_max_rate - 1) /
1977                         avctx->rc_max_rate;
1978
1979             vbv_delay = FFMAX(vbv_delay, min_delay);
1980
1981             av_assert0(vbv_delay < 0xFFFF);
1982
1983             s->vbv_delay_ptr[0] &= 0xF8;
1984             s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1985             s->vbv_delay_ptr[1]  = vbv_delay >> 5;
1986             s->vbv_delay_ptr[2] &= 0x07;
1987             s->vbv_delay_ptr[2] |= vbv_delay << 3;
1988
1989             props = av_cpb_properties_alloc(&props_size);
1990             if (!props)
1991                 return AVERROR(ENOMEM);
1992             props->vbv_delay = vbv_delay * 300;
1993
1994             ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
1995                                           (uint8_t*)props, props_size);
1996             if (ret < 0) {
1997                 av_freep(&props);
1998                 return ret;
1999             }
2000
2001 #if FF_API_VBV_DELAY
2002 FF_DISABLE_DEPRECATION_WARNINGS
2003             avctx->vbv_delay     = vbv_delay * 300;
2004 FF_ENABLE_DEPRECATION_WARNINGS
2005 #endif
2006         }
2007         s->total_bits     += s->frame_bits;
2008 #if FF_API_STAT_BITS
2009 FF_DISABLE_DEPRECATION_WARNINGS
2010         avctx->frame_bits  = s->frame_bits;
2011 FF_ENABLE_DEPRECATION_WARNINGS
2012 #endif
2013
2014
2015         pkt->pts = s->current_picture.f->pts;
2016         if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
2017             if (!s->current_picture.f->coded_picture_number)
2018                 pkt->dts = pkt->pts - s->dts_delta;
2019             else
2020                 pkt->dts = s->reordered_pts;
2021             s->reordered_pts = pkt->pts;
2022         } else
2023             pkt->dts = pkt->pts;
2024         if (s->current_picture.f->key_frame)
2025             pkt->flags |= AV_PKT_FLAG_KEY;
2026         if (s->mb_info)
2027             av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
2028     } else {
2029         s->frame_bits = 0;
2030     }
2031
2032     /* release non-reference frames */
2033     for (i = 0; i < MAX_PICTURE_COUNT; i++) {
2034         if (!s->picture[i].reference)
2035             ff_mpeg_unref_picture(avctx, &s->picture[i]);
2036     }
2037
2038     av_assert1((s->frame_bits & 7) == 0);
2039
2040     pkt->size = s->frame_bits / 8;
2041     *got_packet = !!pkt->size;
2042     return 0;
2043 }
2044
2045 static inline void dct_single_coeff_elimination(MpegEncContext *s,
2046                                                 int n, int threshold)
2047 {
2048     static const char tab[64] = {
2049         3, 2, 2, 1, 1, 1, 1, 1,
2050         1, 1, 1, 1, 1, 1, 1, 1,
2051         1, 1, 1, 1, 1, 1, 1, 1,
2052         0, 0, 0, 0, 0, 0, 0, 0,
2053         0, 0, 0, 0, 0, 0, 0, 0,
2054         0, 0, 0, 0, 0, 0, 0, 0,
2055         0, 0, 0, 0, 0, 0, 0, 0,
2056         0, 0, 0, 0, 0, 0, 0, 0
2057     };
2058     int score = 0;
2059     int run = 0;
2060     int i;
2061     int16_t *block = s->block[n];
2062     const int last_index = s->block_last_index[n];
2063     int skip_dc;
2064
2065     if (threshold < 0) {
2066         skip_dc = 0;
2067         threshold = -threshold;
2068     } else
2069         skip_dc = 1;
2070
2071     /* Are all we could set to zero already zero? */
2072     if (last_index <= skip_dc - 1)
2073         return;
2074
2075     for (i = 0; i <= last_index; i++) {
2076         const int j = s->intra_scantable.permutated[i];
2077         const int level = FFABS(block[j]);
2078         if (level == 1) {
2079             if (skip_dc && i == 0)
2080                 continue;
2081             score += tab[run];
2082             run = 0;
2083         } else if (level > 1) {
2084             return;
2085         } else {
2086             run++;
2087         }
2088     }
2089     if (score >= threshold)
2090         return;
2091     for (i = skip_dc; i <= last_index; i++) {
2092         const int j = s->intra_scantable.permutated[i];
2093         block[j] = 0;
2094     }
2095     if (block[0])
2096         s->block_last_index[n] = 0;
2097     else
2098         s->block_last_index[n] = -1;
2099 }
2100
2101 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
2102                                int last_index)
2103 {
2104     int i;
2105     const int maxlevel = s->max_qcoeff;
2106     const int minlevel = s->min_qcoeff;
2107     int overflow = 0;
2108
2109     if (s->mb_intra) {
2110         i = 1; // skip clipping of intra dc
2111     } else
2112         i = 0;
2113
2114     for (; i <= last_index; i++) {
2115         const int j = s->intra_scantable.permutated[i];
2116         int level = block[j];
2117
2118         if (level > maxlevel) {
2119             level = maxlevel;
2120             overflow++;
2121         } else if (level < minlevel) {
2122             level = minlevel;
2123             overflow++;
2124         }
2125
2126         block[j] = level;
2127     }
2128
2129     if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2130         av_log(s->avctx, AV_LOG_INFO,
2131                "warning, clipping %d dct coefficients to %d..%d\n",
2132                overflow, minlevel, maxlevel);
2133 }
2134
2135 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
2136 {
2137     int x, y;
2138     // FIXME optimize
2139     for (y = 0; y < 8; y++) {
2140         for (x = 0; x < 8; x++) {
2141             int x2, y2;
2142             int sum = 0;
2143             int sqr = 0;
2144             int count = 0;
2145
2146             for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2147                 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2148                     int v = ptr[x2 + y2 * stride];
2149                     sum += v;
2150                     sqr += v * v;
2151                     count++;
2152                 }
2153             }
2154             weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2155         }
2156     }
2157 }
2158
2159 static av_always_inline void encode_mb_internal(MpegEncContext *s,
2160                                                 int motion_x, int motion_y,
2161                                                 int mb_block_height,
2162                                                 int mb_block_width,
2163                                                 int mb_block_count)
2164 {
2165     int16_t weight[12][64];
2166     int16_t orig[12][64];
2167     const int mb_x = s->mb_x;
2168     const int mb_y = s->mb_y;
2169     int i;
2170     int skip_dct[12];
2171     int dct_offset = s->linesize * 8; // default for progressive frames
2172     int uv_dct_offset = s->uvlinesize * 8;
2173     uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2174     ptrdiff_t wrap_y, wrap_c;
2175
2176     for (i = 0; i < mb_block_count; i++)
2177         skip_dct[i] = s->skipdct;
2178
2179     if (s->adaptive_quant) {
2180         const int last_qp = s->qscale;
2181         const int mb_xy = mb_x + mb_y * s->mb_stride;
2182
2183         s->lambda = s->lambda_table[mb_xy];
2184         update_qscale(s);
2185
2186         if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2187             s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
2188             s->dquant = s->qscale - last_qp;
2189
2190             if (s->out_format == FMT_H263) {
2191                 s->dquant = av_clip(s->dquant, -2, 2);
2192
2193                 if (s->codec_id == AV_CODEC_ID_MPEG4) {
2194                     if (!s->mb_intra) {
2195                         if (s->pict_type == AV_PICTURE_TYPE_B) {
2196                             if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
2197                                 s->dquant = 0;
2198                         }
2199                         if (s->mv_type == MV_TYPE_8X8)
2200                             s->dquant = 0;
2201                     }
2202                 }
2203             }
2204         }
2205         ff_set_qscale(s, last_qp + s->dquant);
2206     } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2207         ff_set_qscale(s, s->qscale + s->dquant);
2208
2209     wrap_y = s->linesize;
2210     wrap_c = s->uvlinesize;
2211     ptr_y  = s->new_picture.f->data[0] +
2212              (mb_y * 16 * wrap_y)              + mb_x * 16;
2213     ptr_cb = s->new_picture.f->data[1] +
2214              (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2215     ptr_cr = s->new_picture.f->data[2] +
2216              (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2217
2218     if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
2219         uint8_t *ebuf = s->sc.edge_emu_buffer + 38 * wrap_y;
2220         int cw = (s->width  + s->chroma_x_shift) >> s->chroma_x_shift;
2221         int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
2222         s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2223                                  wrap_y, wrap_y,
2224                                  16, 16, mb_x * 16, mb_y * 16,
2225                                  s->width, s->height);
2226         ptr_y = ebuf;
2227         s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2228                                  wrap_c, wrap_c,
2229                                  mb_block_width, mb_block_height,
2230                                  mb_x * mb_block_width, mb_y * mb_block_height,
2231                                  cw, ch);
2232         ptr_cb = ebuf + 16 * wrap_y;
2233         s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2234                                  wrap_c, wrap_c,
2235                                  mb_block_width, mb_block_height,
2236                                  mb_x * mb_block_width, mb_y * mb_block_height,
2237                                  cw, ch);
2238         ptr_cr = ebuf + 16 * wrap_y + 16;
2239     }
2240
2241     if (s->mb_intra) {
2242         if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2243             int progressive_score, interlaced_score;
2244
2245             s->interlaced_dct = 0;
2246             progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
2247                                 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
2248                                                      NULL, wrap_y, 8) - 400;
2249
2250             if (progressive_score > 0) {
2251                 interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
2252                                                         NULL, wrap_y * 2, 8) +
2253                                    s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
2254                                                         NULL, wrap_y * 2, 8);
2255                 if (progressive_score > interlaced_score) {
2256                     s->interlaced_dct = 1;
2257
2258                     dct_offset = wrap_y;
2259                     uv_dct_offset = wrap_c;
2260                     wrap_y <<= 1;
2261                     if (s->chroma_format == CHROMA_422 ||
2262                         s->chroma_format == CHROMA_444)
2263                         wrap_c <<= 1;
2264                 }
2265             }
2266         }
2267
2268         s->pdsp.get_pixels(s->block[0], ptr_y,                  wrap_y);
2269         s->pdsp.get_pixels(s->block[1], ptr_y + 8,              wrap_y);
2270         s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset,     wrap_y);
2271         s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2272
2273         if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2274             skip_dct[4] = 1;
2275             skip_dct[5] = 1;
2276         } else {
2277             s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2278             s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2279             if (!s->chroma_y_shift && s->chroma_x_shift) { /* 422 */
2280                 s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2281                 s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2282             } else if (!s->chroma_y_shift && !s->chroma_x_shift) { /* 444 */
2283                 s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2284                 s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2285                 s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2286                 s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2287                 s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2288                 s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2289             }
2290         }
2291     } else {
2292         op_pixels_func (*op_pix)[4];
2293         qpel_mc_func (*op_qpix)[16];
2294         uint8_t *dest_y, *dest_cb, *dest_cr;
2295
2296         dest_y  = s->dest[0];
2297         dest_cb = s->dest[1];
2298         dest_cr = s->dest[2];
2299
2300         if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
2301             op_pix  = s->hdsp.put_pixels_tab;
2302             op_qpix = s->qdsp.put_qpel_pixels_tab;
2303         } else {
2304             op_pix  = s->hdsp.put_no_rnd_pixels_tab;
2305             op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
2306         }
2307
2308         if (s->mv_dir & MV_DIR_FORWARD) {
2309             ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
2310                           s->last_picture.f->data,
2311                           op_pix, op_qpix);
2312             op_pix  = s->hdsp.avg_pixels_tab;
2313             op_qpix = s->qdsp.avg_qpel_pixels_tab;
2314         }
2315         if (s->mv_dir & MV_DIR_BACKWARD) {
2316             ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
2317                           s->next_picture.f->data,
2318                           op_pix, op_qpix);
2319         }
2320
2321         if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
2322             int progressive_score, interlaced_score;
2323
2324             s->interlaced_dct = 0;
2325             progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2326                                 s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2327                                                      ptr_y + wrap_y * 8,
2328                                                      wrap_y, 8) - 400;
2329
2330             if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2331                 progressive_score -= 400;
2332
2333             if (progressive_score > 0) {
2334                 interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2335                                                         wrap_y * 2, 8) +
2336                                    s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2337                                                         ptr_y + wrap_y,
2338                                                         wrap_y * 2, 8);
2339
2340                 if (progressive_score > interlaced_score) {
2341                     s->interlaced_dct = 1;
2342
2343                     dct_offset = wrap_y;
2344                     uv_dct_offset = wrap_c;
2345                     wrap_y <<= 1;
2346                     if (s->chroma_format == CHROMA_422)
2347                         wrap_c <<= 1;
2348                 }
2349             }
2350         }
2351
2352         s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2353         s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2354         s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2355                             dest_y + dct_offset, wrap_y);
2356         s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2357                             dest_y + dct_offset + 8, wrap_y);
2358
2359         if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2360             skip_dct[4] = 1;
2361             skip_dct[5] = 1;
2362         } else {
2363             s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2364             s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2365             if (!s->chroma_y_shift) { /* 422 */
2366                 s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2367                                     dest_cb + uv_dct_offset, wrap_c);
2368                 s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2369                                     dest_cr + uv_dct_offset, wrap_c);
2370             }
2371         }
2372         /* pre quantization */
2373         if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2374                 2 * s->qscale * s->qscale) {
2375             // FIXME optimize
2376             if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2377                 skip_dct[0] = 1;
2378             if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2379                 skip_dct[1] = 1;
2380             if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2381                                wrap_y, 8) < 20 * s->qscale)
2382                 skip_dct[2] = 1;
2383             if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2384                                wrap_y, 8) < 20 * s->qscale)
2385                 skip_dct[3] = 1;
2386             if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2387                 skip_dct[4] = 1;
2388             if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2389                 skip_dct[5] = 1;
2390             if (!s->chroma_y_shift) { /* 422 */
2391                 if (s->mecc.sad[1](NULL, ptr_cb + uv_dct_offset,
2392                                    dest_cb + uv_dct_offset,
2393                                    wrap_c, 8) < 20 * s->qscale)
2394                     skip_dct[6] = 1;
2395                 if (s->mecc.sad[1](NULL, ptr_cr + uv_dct_offset,
2396                                    dest_cr + uv_dct_offset,
2397                                    wrap_c, 8) < 20 * s->qscale)
2398                     skip_dct[7] = 1;
2399             }
2400         }
2401     }
2402
2403     if (s->quantizer_noise_shaping) {
2404         if (!skip_dct[0])
2405             get_visual_weight(weight[0], ptr_y                 , wrap_y);
2406         if (!skip_dct[1])
2407             get_visual_weight(weight[1], ptr_y              + 8, wrap_y);
2408         if (!skip_dct[2])
2409             get_visual_weight(weight[2], ptr_y + dct_offset    , wrap_y);
2410         if (!skip_dct[3])
2411             get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2412         if (!skip_dct[4])
2413             get_visual_weight(weight[4], ptr_cb                , wrap_c);
2414         if (!skip_dct[5])
2415             get_visual_weight(weight[5], ptr_cr                , wrap_c);
2416         if (!s->chroma_y_shift) { /* 422 */
2417             if (!skip_dct[6])
2418                 get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2419                                   wrap_c);
2420             if (!skip_dct[7])
2421                 get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2422                                   wrap_c);
2423         }
2424         memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2425     }
2426
2427     /* DCT & quantize */
2428     av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
2429     {
2430         for (i = 0; i < mb_block_count; i++) {
2431             if (!skip_dct[i]) {
2432                 int overflow;
2433                 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2434                 // FIXME we could decide to change to quantizer instead of
2435                 // clipping
2436                 // JS: I don't think that would be a good idea it could lower
2437                 //     quality instead of improve it. Just INTRADC clipping
2438                 //     deserves changes in quantizer
2439                 if (overflow)
2440                     clip_coeffs(s, s->block[i], s->block_last_index[i]);
2441             } else
2442                 s->block_last_index[i] = -1;
2443         }
2444         if (s->quantizer_noise_shaping) {
2445             for (i = 0; i < mb_block_count; i++) {
2446                 if (!skip_dct[i]) {
2447                     s->block_last_index[i] =
2448                         dct_quantize_refine(s, s->block[i], weight[i],
2449                                             orig[i], i, s->qscale);
2450                 }
2451             }
2452         }
2453
2454         if (s->luma_elim_threshold && !s->mb_intra)
2455             for (i = 0; i < 4; i++)
2456                 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2457         if (s->chroma_elim_threshold && !s->mb_intra)
2458             for (i = 4; i < mb_block_count; i++)
2459                 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2460
2461         if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2462             for (i = 0; i < mb_block_count; i++) {
2463                 if (s->block_last_index[i] == -1)
2464                     s->coded_score[i] = INT_MAX / 256;
2465             }
2466         }
2467     }
2468
2469     if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
2470         s->block_last_index[4] =
2471         s->block_last_index[5] = 0;
2472         s->block[4][0] =
2473         s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2474         if (!s->chroma_y_shift) { /* 422 / 444 */
2475             for (i=6; i<12; i++) {
2476                 s->block_last_index[i] = 0;
2477                 s->block[i][0] = s->block[4][0];
2478             }
2479         }
2480     }
2481
2482     // non c quantize code returns incorrect block_last_index FIXME
2483     if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2484         for (i = 0; i < mb_block_count; i++) {
2485             int j;
2486             if (s->block_last_index[i] > 0) {
2487                 for (j = 63; j > 0; j--) {
2488                     if (s->block[i][s->intra_scantable.permutated[j]])
2489                         break;
2490                 }
2491                 s->block_last_index[i] = j;
2492             }
2493         }
2494     }
2495
2496     /* huffman encode */
2497     switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2498     case AV_CODEC_ID_MPEG1VIDEO:
2499     case AV_CODEC_ID_MPEG2VIDEO:
2500         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2501             ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2502         break;
2503     case AV_CODEC_ID_MPEG4:
2504         if (CONFIG_MPEG4_ENCODER)
2505             ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2506         break;
2507     case AV_CODEC_ID_MSMPEG4V2:
2508     case AV_CODEC_ID_MSMPEG4V3:
2509     case AV_CODEC_ID_WMV1:
2510         if (CONFIG_MSMPEG4_ENCODER)
2511             ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2512         break;
2513     case AV_CODEC_ID_WMV2:
2514         if (CONFIG_WMV2_ENCODER)
2515             ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2516         break;
2517     case AV_CODEC_ID_H261:
2518         if (CONFIG_H261_ENCODER)
2519             ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2520         break;
2521     case AV_CODEC_ID_H263:
2522     case AV_CODEC_ID_H263P:
2523     case AV_CODEC_ID_FLV1:
2524     case AV_CODEC_ID_RV10:
2525     case AV_CODEC_ID_RV20:
2526         if (CONFIG_H263_ENCODER)
2527             ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2528         break;
2529 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2530     case AV_CODEC_ID_MJPEG:
2531     case AV_CODEC_ID_AMV:
2532         ff_mjpeg_encode_mb(s, s->block);
2533         break;
2534 #endif
2535     case AV_CODEC_ID_SPEEDHQ:
2536         if (CONFIG_SPEEDHQ_ENCODER)
2537             ff_speedhq_encode_mb(s, s->block);
2538         break;
2539     default:
2540         av_assert1(0);
2541     }
2542 }
2543
2544 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2545 {
2546     if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 8, 6);
2547     else if (s->chroma_format == CHROMA_422) encode_mb_internal(s, motion_x, motion_y, 16, 8, 8);
2548     else encode_mb_internal(s, motion_x, motion_y, 16, 16, 12);
2549 }
2550
2551 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2552     int i;
2553
2554     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2555
2556     /* MPEG-1 */
2557     d->mb_skip_run= s->mb_skip_run;
2558     for(i=0; i<3; i++)
2559         d->last_dc[i] = s->last_dc[i];
2560
2561     /* statistics */
2562     d->mv_bits= s->mv_bits;
2563     d->i_tex_bits= s->i_tex_bits;
2564     d->p_tex_bits= s->p_tex_bits;
2565     d->i_count= s->i_count;
2566     d->f_count= s->f_count;
2567     d->b_count= s->b_count;
2568     d->skip_count= s->skip_count;
2569     d->misc_bits= s->misc_bits;
2570     d->last_bits= 0;
2571
2572     d->mb_skipped= 0;
2573     d->qscale= s->qscale;
2574     d->dquant= s->dquant;
2575
2576     d->esc3_level_length= s->esc3_level_length;
2577 }
2578
2579 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2580     int i;
2581
2582     memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2583     memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2584
2585     /* MPEG-1 */
2586     d->mb_skip_run= s->mb_skip_run;
2587     for(i=0; i<3; i++)
2588         d->last_dc[i] = s->last_dc[i];
2589
2590     /* statistics */
2591     d->mv_bits= s->mv_bits;
2592     d->i_tex_bits= s->i_tex_bits;
2593     d->p_tex_bits= s->p_tex_bits;
2594     d->i_count= s->i_count;
2595     d->f_count= s->f_count;
2596     d->b_count= s->b_count;
2597     d->skip_count= s->skip_count;
2598     d->misc_bits= s->misc_bits;
2599
2600     d->mb_intra= s->mb_intra;
2601     d->mb_skipped= s->mb_skipped;
2602     d->mv_type= s->mv_type;
2603     d->mv_dir= s->mv_dir;
2604     d->pb= s->pb;
2605     if(s->data_partitioning){
2606         d->pb2= s->pb2;
2607         d->tex_pb= s->tex_pb;
2608     }
2609     d->block= s->block;
2610     for(i=0; i<8; i++)
2611         d->block_last_index[i]= s->block_last_index[i];
2612     d->interlaced_dct= s->interlaced_dct;
2613     d->qscale= s->qscale;
2614
2615     d->esc3_level_length= s->esc3_level_length;
2616 }
2617
2618 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2619                            PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2620                            int *dmin, int *next_block, int motion_x, int motion_y)
2621 {
2622     int score;
2623     uint8_t *dest_backup[3];
2624
2625     copy_context_before_encode(s, backup, type);
2626
2627     s->block= s->blocks[*next_block];
2628     s->pb= pb[*next_block];
2629     if(s->data_partitioning){
2630         s->pb2   = pb2   [*next_block];
2631         s->tex_pb= tex_pb[*next_block];
2632     }
2633
2634     if(*next_block){
2635         memcpy(dest_backup, s->dest, sizeof(s->dest));
2636         s->dest[0] = s->sc.rd_scratchpad;
2637         s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
2638         s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
2639         av_assert0(s->linesize >= 32); //FIXME
2640     }
2641
2642     encode_mb(s, motion_x, motion_y);
2643
2644     score= put_bits_count(&s->pb);
2645     if(s->data_partitioning){
2646         score+= put_bits_count(&s->pb2);
2647         score+= put_bits_count(&s->tex_pb);
2648     }
2649
2650     if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2651         ff_mpv_reconstruct_mb(s, s->block);
2652
2653         score *= s->lambda2;
2654         score += sse_mb(s) << FF_LAMBDA_SHIFT;
2655     }
2656
2657     if(*next_block){
2658         memcpy(s->dest, dest_backup, sizeof(s->dest));
2659     }
2660
2661     if(score<*dmin){
2662         *dmin= score;
2663         *next_block^=1;
2664
2665         copy_context_after_encode(best, s, type);
2666     }
2667 }
2668
2669 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2670     const uint32_t *sq = ff_square_tab + 256;
2671     int acc=0;
2672     int x,y;
2673
2674     if(w==16 && h==16)
2675         return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2676     else if(w==8 && h==8)
2677         return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2678
2679     for(y=0; y<h; y++){
2680         for(x=0; x<w; x++){
2681             acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2682         }
2683     }
2684
2685     av_assert2(acc>=0);
2686
2687     return acc;
2688 }
2689
2690 static int sse_mb(MpegEncContext *s){
2691     int w= 16;
2692     int h= 16;
2693
2694     if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2695     if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2696
2697     if(w==16 && h==16)
2698       if(s->avctx->mb_cmp == FF_CMP_NSSE){
2699         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) +
2700                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) +
2701                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);
2702       }else{
2703         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) +
2704                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) +
2705                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);
2706       }
2707     else
2708         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)
2709                +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)
2710                +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);
2711 }
2712
2713 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2714     MpegEncContext *s= *(void**)arg;
2715
2716
2717     s->me.pre_pass=1;
2718     s->me.dia_size= s->avctx->pre_dia_size;
2719     s->first_slice_line=1;
2720     for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2721         for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2722             ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2723         }
2724         s->first_slice_line=0;
2725     }
2726
2727     s->me.pre_pass=0;
2728
2729     return 0;
2730 }
2731
2732 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2733     MpegEncContext *s= *(void**)arg;
2734
2735     s->me.dia_size= s->avctx->dia_size;
2736     s->first_slice_line=1;
2737     for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2738         s->mb_x=0; //for block init below
2739         ff_init_block_index(s);
2740         for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2741             s->block_index[0]+=2;
2742             s->block_index[1]+=2;
2743             s->block_index[2]+=2;
2744             s->block_index[3]+=2;
2745
2746             /* compute motion vector & mb_type and store in context */
2747             if(s->pict_type==AV_PICTURE_TYPE_B)
2748                 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2749             else
2750                 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2751         }
2752         s->first_slice_line=0;
2753     }
2754     return 0;
2755 }
2756
2757 static int mb_var_thread(AVCodecContext *c, void *arg){
2758     MpegEncContext *s= *(void**)arg;
2759     int mb_x, mb_y;
2760
2761     for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2762         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2763             int xx = mb_x * 16;
2764             int yy = mb_y * 16;
2765             uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2766             int varc;
2767             int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2768
2769             varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2770                     (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2771
2772             s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2773             s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2774             s->me.mb_var_sum_temp    += varc;
2775         }
2776     }
2777     return 0;
2778 }
2779
2780 static void write_slice_end(MpegEncContext *s){
2781     if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2782         if(s->partitioned_frame){
2783             ff_mpeg4_merge_partitions(s);
2784         }
2785
2786         ff_mpeg4_stuffing(&s->pb);
2787     } else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2788                s->out_format == FMT_MJPEG) {
2789         ff_mjpeg_encode_stuffing(s);
2790     } else if (CONFIG_SPEEDHQ_ENCODER && s->out_format == FMT_SPEEDHQ) {
2791         ff_speedhq_end_slice(s);
2792     }
2793
2794     flush_put_bits(&s->pb);
2795
2796     if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2797         s->misc_bits+= get_bits_diff(s);
2798 }
2799
2800 static void write_mb_info(MpegEncContext *s)
2801 {
2802     uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2803     int offset = put_bits_count(&s->pb);
2804     int mba  = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2805     int gobn = s->mb_y / s->gob_index;
2806     int pred_x, pred_y;
2807     if (CONFIG_H263_ENCODER)
2808         ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2809     bytestream_put_le32(&ptr, offset);
2810     bytestream_put_byte(&ptr, s->qscale);
2811     bytestream_put_byte(&ptr, gobn);
2812     bytestream_put_le16(&ptr, mba);
2813     bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2814     bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2815     /* 4MV not implemented */
2816     bytestream_put_byte(&ptr, 0); /* hmv2 */
2817     bytestream_put_byte(&ptr, 0); /* vmv2 */
2818 }
2819
2820 static void update_mb_info(MpegEncContext *s, int startcode)
2821 {
2822     if (!s->mb_info)
2823         return;
2824     if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
2825         s->mb_info_size += 12;
2826         s->prev_mb_info = s->last_mb_info;
2827     }
2828     if (startcode) {
2829         s->prev_mb_info = put_bytes_count(&s->pb, 0);
2830         /* This might have incremented mb_info_size above, and we return without
2831          * actually writing any info into that slot yet. But in that case,
2832          * this will be called again at the start of the after writing the
2833          * start code, actually writing the mb info. */
2834         return;
2835     }
2836
2837     s->last_mb_info = put_bytes_count(&s->pb, 0);
2838     if (!s->mb_info_size)
2839         s->mb_info_size += 12;
2840     write_mb_info(s);
2841 }
2842
2843 int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
2844 {
2845     if (put_bytes_left(&s->pb, 0) < threshold
2846         && s->slice_context_count == 1
2847         && s->pb.buf == s->avctx->internal->byte_buffer) {
2848         int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2849         int vbv_pos     = s->vbv_delay_ptr - s->pb.buf;
2850
2851         uint8_t *new_buffer = NULL;
2852         int new_buffer_size = 0;
2853
2854         if ((s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2855             av_log(s->avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
2856             return AVERROR(ENOMEM);
2857         }
2858
2859         emms_c();
2860
2861         av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2862                               s->avctx->internal->byte_buffer_size + size_increase);
2863         if (!new_buffer)
2864             return AVERROR(ENOMEM);
2865
2866         memcpy(new_buffer, s->avctx->internal->byte_buffer, s->avctx->internal->byte_buffer_size);
2867         av_free(s->avctx->internal->byte_buffer);
2868         s->avctx->internal->byte_buffer      = new_buffer;
2869         s->avctx->internal->byte_buffer_size = new_buffer_size;
2870         rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2871         s->ptr_lastgob   = s->pb.buf + lastgob_pos;
2872         s->vbv_delay_ptr = s->pb.buf + vbv_pos;
2873     }
2874     if (put_bytes_left(&s->pb, 0) < threshold)
2875         return AVERROR(EINVAL);
2876     return 0;
2877 }
2878
2879 static int encode_thread(AVCodecContext *c, void *arg){
2880     MpegEncContext *s= *(void**)arg;
2881     int mb_x, mb_y, mb_y_order;
2882     int chr_h= 16>>s->chroma_y_shift;
2883     int i, j;
2884     MpegEncContext best_s = { 0 }, backup_s;
2885     uint8_t bit_buf[2][MAX_MB_BYTES];
2886     uint8_t bit_buf2[2][MAX_MB_BYTES];
2887     uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2888     PutBitContext pb[2], pb2[2], tex_pb[2];
2889
2890     for(i=0; i<2; i++){
2891         init_put_bits(&pb    [i], bit_buf    [i], MAX_MB_BYTES);
2892         init_put_bits(&pb2   [i], bit_buf2   [i], MAX_MB_BYTES);
2893         init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2894     }
2895
2896     s->last_bits= put_bits_count(&s->pb);
2897     s->mv_bits=0;
2898     s->misc_bits=0;
2899     s->i_tex_bits=0;
2900     s->p_tex_bits=0;
2901     s->i_count=0;
2902     s->f_count=0;
2903     s->b_count=0;
2904     s->skip_count=0;
2905
2906     for(i=0; i<3; i++){
2907         /* init last dc values */
2908         /* note: quant matrix value (8) is implied here */
2909         s->last_dc[i] = 128 << s->intra_dc_precision;
2910
2911         s->current_picture.encoding_error[i] = 0;
2912     }
2913     if(s->codec_id==AV_CODEC_ID_AMV){
2914         s->last_dc[0] = 128*8/13;
2915         s->last_dc[1] = 128*8/14;
2916         s->last_dc[2] = 128*8/14;
2917     }
2918     s->mb_skip_run = 0;
2919     memset(s->last_mv, 0, sizeof(s->last_mv));
2920
2921     s->last_mv_dir = 0;
2922
2923     switch(s->codec_id){
2924     case AV_CODEC_ID_H263:
2925     case AV_CODEC_ID_H263P:
2926     case AV_CODEC_ID_FLV1:
2927         if (CONFIG_H263_ENCODER)
2928             s->gob_index = H263_GOB_HEIGHT(s->height);
2929         break;
2930     case AV_CODEC_ID_MPEG4:
2931         if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2932             ff_mpeg4_init_partitions(s);
2933         break;
2934     }
2935
2936     s->resync_mb_x=0;
2937     s->resync_mb_y=0;
2938     s->first_slice_line = 1;
2939     s->ptr_lastgob = s->pb.buf;
2940     for (mb_y_order = s->start_mb_y; mb_y_order < s->end_mb_y; mb_y_order++) {
2941         if (CONFIG_SPEEDHQ_ENCODER && s->codec_id == AV_CODEC_ID_SPEEDHQ) {
2942             int first_in_slice;
2943             mb_y = ff_speedhq_mb_y_order_to_mb(mb_y_order, s->mb_height, &first_in_slice);
2944             if (first_in_slice && mb_y_order != s->start_mb_y)
2945                 ff_speedhq_end_slice(s);
2946             s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 1024 << s->intra_dc_precision;
2947         } else {
2948             mb_y = mb_y_order;
2949         }
2950         s->mb_x=0;
2951         s->mb_y= mb_y;
2952
2953         ff_set_qscale(s, s->qscale);
2954         ff_init_block_index(s);
2955
2956         for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2957             int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2958             int mb_type= s->mb_type[xy];
2959 //            int d;
2960             int dmin= INT_MAX;
2961             int dir;
2962             int size_increase =  s->avctx->internal->byte_buffer_size/4
2963                                + s->mb_width*MAX_MB_BYTES;
2964
2965             ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
2966             if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
2967                 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2968                 return -1;
2969             }
2970             if(s->data_partitioning){
2971                 if (put_bytes_left(&s->pb2,    0) < MAX_MB_BYTES ||
2972                     put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
2973                     av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
2974                     return -1;
2975                 }
2976             }
2977
2978             s->mb_x = mb_x;
2979             s->mb_y = mb_y;  // moved into loop, can get changed by H.261
2980             ff_update_block_index(s);
2981
2982             if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2983                 ff_h261_reorder_mb_index(s);
2984                 xy= s->mb_y*s->mb_stride + s->mb_x;
2985                 mb_type= s->mb_type[xy];
2986             }
2987
2988             /* write gob / video packet header  */
2989             if(s->rtp_mode){
2990                 int current_packet_size, is_gob_start;
2991
2992                 current_packet_size = put_bytes_count(&s->pb, 1)
2993                                       - (s->ptr_lastgob - s->pb.buf);
2994
2995                 is_gob_start = s->rtp_payload_size &&
2996                                current_packet_size >= s->rtp_payload_size &&
2997                                mb_y + mb_x > 0;
2998
2999                 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
3000
3001                 switch(s->codec_id){
3002                 case AV_CODEC_ID_H263:
3003                 case AV_CODEC_ID_H263P:
3004                     if(!s->h263_slice_structured)
3005                         if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
3006                     break;
3007                 case AV_CODEC_ID_MPEG2VIDEO:
3008                     if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
3009                 case AV_CODEC_ID_MPEG1VIDEO:
3010                     if(s->mb_skip_run) is_gob_start=0;
3011                     break;
3012                 case AV_CODEC_ID_MJPEG:
3013                     if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
3014                     break;
3015                 }
3016
3017                 if(is_gob_start){
3018                     if(s->start_mb_y != mb_y || mb_x!=0){
3019                         write_slice_end(s);
3020
3021                         if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
3022                             ff_mpeg4_init_partitions(s);
3023                         }
3024                     }
3025
3026                     av_assert2((put_bits_count(&s->pb)&7) == 0);
3027                     current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
3028
3029                     if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
3030                         int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->mb_x + s->mb_y;
3031                         int d = 100 / s->error_rate;
3032                         if(r % d == 0){
3033                             current_packet_size=0;
3034                             s->pb.buf_ptr= s->ptr_lastgob;
3035                             av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
3036                         }
3037                     }
3038
3039 #if FF_API_RTP_CALLBACK
3040 FF_DISABLE_DEPRECATION_WARNINGS
3041                     if (s->avctx->rtp_callback){
3042                         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
3043                         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
3044                     }
3045 FF_ENABLE_DEPRECATION_WARNINGS
3046 #endif
3047                     update_mb_info(s, 1);
3048
3049                     switch(s->codec_id){
3050                     case AV_CODEC_ID_MPEG4:
3051                         if (CONFIG_MPEG4_ENCODER) {
3052                             ff_mpeg4_encode_video_packet_header(s);
3053                             ff_mpeg4_clean_buffers(s);
3054                         }
3055                     break;
3056                     case AV_CODEC_ID_MPEG1VIDEO:
3057                     case AV_CODEC_ID_MPEG2VIDEO:
3058                         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3059                             ff_mpeg1_encode_slice_header(s);
3060                             ff_mpeg1_clean_buffers(s);
3061                         }
3062                     break;
3063                     case AV_CODEC_ID_H263:
3064                     case AV_CODEC_ID_H263P:
3065                         if (CONFIG_H263_ENCODER)
3066                             ff_h263_encode_gob_header(s, mb_y);
3067                     break;
3068                     }
3069
3070                     if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
3071                         int bits= put_bits_count(&s->pb);
3072                         s->misc_bits+= bits - s->last_bits;
3073                         s->last_bits= bits;
3074                     }
3075
3076                     s->ptr_lastgob += current_packet_size;
3077                     s->first_slice_line=1;
3078                     s->resync_mb_x=mb_x;
3079                     s->resync_mb_y=mb_y;
3080                 }
3081             }
3082
3083             if(  (s->resync_mb_x   == s->mb_x)
3084                && s->resync_mb_y+1 == s->mb_y){
3085                 s->first_slice_line=0;
3086             }
3087
3088             s->mb_skipped=0;
3089             s->dquant=0; //only for QP_RD
3090
3091             update_mb_info(s, 0);
3092
3093             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
3094                 int next_block=0;
3095                 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3096
3097                 copy_context_before_encode(&backup_s, s, -1);
3098                 backup_s.pb= s->pb;
3099                 best_s.data_partitioning= s->data_partitioning;
3100                 best_s.partitioned_frame= s->partitioned_frame;
3101                 if(s->data_partitioning){
3102                     backup_s.pb2= s->pb2;
3103                     backup_s.tex_pb= s->tex_pb;
3104                 }
3105
3106                 if(mb_type&CANDIDATE_MB_TYPE_INTER){
3107                     s->mv_dir = MV_DIR_FORWARD;
3108                     s->mv_type = MV_TYPE_16X16;
3109                     s->mb_intra= 0;
3110                     s->mv[0][0][0] = s->p_mv_table[xy][0];
3111                     s->mv[0][0][1] = s->p_mv_table[xy][1];
3112                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3113                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3114                 }
3115                 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
3116                     s->mv_dir = MV_DIR_FORWARD;
3117                     s->mv_type = MV_TYPE_FIELD;
3118                     s->mb_intra= 0;
3119                     for(i=0; i<2; i++){
3120                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3121                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3122                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3123                     }
3124                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3125                                  &dmin, &next_block, 0, 0);
3126                 }
3127                 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
3128                     s->mv_dir = MV_DIR_FORWARD;
3129                     s->mv_type = MV_TYPE_16X16;
3130                     s->mb_intra= 0;
3131                     s->mv[0][0][0] = 0;
3132                     s->mv[0][0][1] = 0;
3133                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3134                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3135                 }
3136                 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
3137                     s->mv_dir = MV_DIR_FORWARD;
3138                     s->mv_type = MV_TYPE_8X8;
3139                     s->mb_intra= 0;
3140                     for(i=0; i<4; i++){
3141                         s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3142                         s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3143                     }
3144                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3145                                  &dmin, &next_block, 0, 0);
3146                 }
3147                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
3148                     s->mv_dir = MV_DIR_FORWARD;
3149                     s->mv_type = MV_TYPE_16X16;
3150                     s->mb_intra= 0;
3151                     s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3152                     s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3153                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3154                                  &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3155                 }
3156                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
3157                     s->mv_dir = MV_DIR_BACKWARD;
3158                     s->mv_type = MV_TYPE_16X16;
3159                     s->mb_intra= 0;
3160                     s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3161                     s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3162                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3163                                  &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3164                 }
3165                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
3166                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3167                     s->mv_type = MV_TYPE_16X16;
3168                     s->mb_intra= 0;
3169                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3170                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3171                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3172                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3173                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3174                                  &dmin, &next_block, 0, 0);
3175                 }
3176                 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
3177                     s->mv_dir = MV_DIR_FORWARD;
3178                     s->mv_type = MV_TYPE_FIELD;
3179                     s->mb_intra= 0;
3180                     for(i=0; i<2; i++){
3181                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3182                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3183                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3184                     }
3185                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3186                                  &dmin, &next_block, 0, 0);
3187                 }
3188                 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
3189                     s->mv_dir = MV_DIR_BACKWARD;
3190                     s->mv_type = MV_TYPE_FIELD;
3191                     s->mb_intra= 0;
3192                     for(i=0; i<2; i++){
3193                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3194                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3195                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3196                     }
3197                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3198                                  &dmin, &next_block, 0, 0);
3199                 }
3200                 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
3201                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3202                     s->mv_type = MV_TYPE_FIELD;
3203                     s->mb_intra= 0;
3204                     for(dir=0; dir<2; dir++){
3205                         for(i=0; i<2; i++){
3206                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3207                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3208                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3209                         }
3210                     }
3211                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3212                                  &dmin, &next_block, 0, 0);
3213                 }
3214                 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
3215                     s->mv_dir = 0;
3216                     s->mv_type = MV_TYPE_16X16;
3217                     s->mb_intra= 1;
3218                     s->mv[0][0][0] = 0;
3219                     s->mv[0][0][1] = 0;
3220                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3221                                  &dmin, &next_block, 0, 0);
3222                     if(s->h263_pred || s->h263_aic){
3223                         if(best_s.mb_intra)
3224                             s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3225                         else
3226                             ff_clean_intra_table_entries(s); //old mode?
3227                     }
3228                 }
3229
3230                 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3231                     if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
3232                         const int last_qp= backup_s.qscale;
3233                         int qpi, qp, dc[6];
3234                         int16_t ac[6][16];
3235                         const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
3236                         static const int dquant_tab[4]={-1,1,-2,2};
3237                         int storecoefs = s->mb_intra && s->dc_val[0];
3238
3239                         av_assert2(backup_s.dquant == 0);
3240
3241                         //FIXME intra
3242                         s->mv_dir= best_s.mv_dir;
3243                         s->mv_type = MV_TYPE_16X16;
3244                         s->mb_intra= best_s.mb_intra;
3245                         s->mv[0][0][0] = best_s.mv[0][0][0];
3246                         s->mv[0][0][1] = best_s.mv[0][0][1];
3247                         s->mv[1][0][0] = best_s.mv[1][0][0];
3248                         s->mv[1][0][1] = best_s.mv[1][0][1];
3249
3250                         qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3251                         for(; qpi<4; qpi++){
3252                             int dquant= dquant_tab[qpi];
3253                             qp= last_qp + dquant;
3254                             if(qp < s->avctx->qmin || qp > s->avctx->qmax)
3255                                 continue;
3256                             backup_s.dquant= dquant;
3257                             if(storecoefs){
3258                                 for(i=0; i<6; i++){
3259                                     dc[i]= s->dc_val[0][ s->block_index[i] ];
3260                                     memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
3261                                 }
3262                             }
3263
3264                             encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3265                                          &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
3266                             if(best_s.qscale != qp){
3267                                 if(storecoefs){
3268                                     for(i=0; i<6; i++){
3269                                         s->dc_val[0][ s->block_index[i] ]= dc[i];
3270                                         memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
3271                                     }
3272                                 }
3273                             }
3274                         }
3275                     }
3276                 }
3277                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3278                     int mx= s->b_direct_mv_table[xy][0];
3279                     int my= s->b_direct_mv_table[xy][1];
3280
3281                     backup_s.dquant = 0;
3282                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3283                     s->mb_intra= 0;
3284                     ff_mpeg4_set_direct_mv(s, mx, my);
3285                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3286                                  &dmin, &next_block, mx, my);
3287                 }
3288                 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3289                     backup_s.dquant = 0;
3290                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3291                     s->mb_intra= 0;
3292                     ff_mpeg4_set_direct_mv(s, 0, 0);
3293                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3294                                  &dmin, &next_block, 0, 0);
3295                 }
3296                 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3297                     int coded=0;
3298                     for(i=0; i<6; i++)
3299                         coded |= s->block_last_index[i];
3300                     if(coded){
3301                         int mx,my;
3302                         memcpy(s->mv, best_s.mv, sizeof(s->mv));
3303                         if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3304                             mx=my=0; //FIXME find the one we actually used
3305                             ff_mpeg4_set_direct_mv(s, mx, my);
3306                         }else if(best_s.mv_dir&MV_DIR_BACKWARD){
3307                             mx= s->mv[1][0][0];
3308                             my= s->mv[1][0][1];
3309                         }else{
3310                             mx= s->mv[0][0][0];
3311                             my= s->mv[0][0][1];
3312                         }
3313
3314                         s->mv_dir= best_s.mv_dir;
3315                         s->mv_type = best_s.mv_type;
3316                         s->mb_intra= 0;
3317 /*                        s->mv[0][0][0] = best_s.mv[0][0][0];
3318                         s->mv[0][0][1] = best_s.mv[0][0][1];
3319                         s->mv[1][0][0] = best_s.mv[1][0][0];
3320                         s->mv[1][0][1] = best_s.mv[1][0][1];*/
3321                         backup_s.dquant= 0;
3322                         s->skipdct=1;
3323                         encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3324                                         &dmin, &next_block, mx, my);
3325                         s->skipdct=0;
3326                     }
3327                 }
3328
3329                 s->current_picture.qscale_table[xy] = best_s.qscale;
3330
3331                 copy_context_after_encode(s, &best_s, -1);
3332
3333                 pb_bits_count= put_bits_count(&s->pb);
3334                 flush_put_bits(&s->pb);
3335                 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3336                 s->pb= backup_s.pb;
3337
3338                 if(s->data_partitioning){
3339                     pb2_bits_count= put_bits_count(&s->pb2);
3340                     flush_put_bits(&s->pb2);
3341                     ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3342                     s->pb2= backup_s.pb2;
3343
3344                     tex_pb_bits_count= put_bits_count(&s->tex_pb);
3345                     flush_put_bits(&s->tex_pb);
3346                     ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3347                     s->tex_pb= backup_s.tex_pb;
3348                 }
3349                 s->last_bits= put_bits_count(&s->pb);
3350
3351                 if (CONFIG_H263_ENCODER &&
3352                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3353                     ff_h263_update_motion_val(s);
3354
3355                 if(next_block==0){ //FIXME 16 vs linesize16
3356                     s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad                     , s->linesize  ,16);
3357                     s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
3358                     s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
3359                 }
3360
3361                 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3362                     ff_mpv_reconstruct_mb(s, s->block);
3363             } else {
3364                 int motion_x = 0, motion_y = 0;
3365                 s->mv_type=MV_TYPE_16X16;
3366                 // only one MB-Type possible
3367
3368                 switch(mb_type){
3369                 case CANDIDATE_MB_TYPE_INTRA:
3370                     s->mv_dir = 0;
3371                     s->mb_intra= 1;
3372                     motion_x= s->mv[0][0][0] = 0;
3373                     motion_y= s->mv[0][0][1] = 0;
3374                     break;
3375                 case CANDIDATE_MB_TYPE_INTER:
3376                     s->mv_dir = MV_DIR_FORWARD;
3377                     s->mb_intra= 0;
3378                     motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3379                     motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3380                     break;
3381                 case CANDIDATE_MB_TYPE_INTER_I:
3382                     s->mv_dir = MV_DIR_FORWARD;
3383                     s->mv_type = MV_TYPE_FIELD;
3384                     s->mb_intra= 0;
3385                     for(i=0; i<2; i++){
3386                         j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3387                         s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3388                         s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3389                     }
3390                     break;
3391                 case CANDIDATE_MB_TYPE_INTER4V:
3392                     s->mv_dir = MV_DIR_FORWARD;
3393                     s->mv_type = MV_TYPE_8X8;
3394                     s->mb_intra= 0;
3395                     for(i=0; i<4; i++){
3396                         s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3397                         s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3398                     }
3399                     break;
3400                 case CANDIDATE_MB_TYPE_DIRECT:
3401                     if (CONFIG_MPEG4_ENCODER) {
3402                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3403                         s->mb_intra= 0;
3404                         motion_x=s->b_direct_mv_table[xy][0];
3405                         motion_y=s->b_direct_mv_table[xy][1];
3406                         ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3407                     }
3408                     break;
3409                 case CANDIDATE_MB_TYPE_DIRECT0:
3410                     if (CONFIG_MPEG4_ENCODER) {
3411                         s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3412                         s->mb_intra= 0;
3413                         ff_mpeg4_set_direct_mv(s, 0, 0);
3414                     }
3415                     break;
3416                 case CANDIDATE_MB_TYPE_BIDIR:
3417                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3418                     s->mb_intra= 0;
3419                     s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3420                     s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3421                     s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3422                     s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3423                     break;
3424                 case CANDIDATE_MB_TYPE_BACKWARD:
3425                     s->mv_dir = MV_DIR_BACKWARD;
3426                     s->mb_intra= 0;
3427                     motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3428                     motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3429                     break;
3430                 case CANDIDATE_MB_TYPE_FORWARD:
3431                     s->mv_dir = MV_DIR_FORWARD;
3432                     s->mb_intra= 0;
3433                     motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3434                     motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3435                     break;
3436                 case CANDIDATE_MB_TYPE_FORWARD_I:
3437                     s->mv_dir = MV_DIR_FORWARD;
3438                     s->mv_type = MV_TYPE_FIELD;
3439                     s->mb_intra= 0;
3440                     for(i=0; i<2; i++){
3441                         j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3442                         s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3443                         s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3444                     }
3445                     break;
3446                 case CANDIDATE_MB_TYPE_BACKWARD_I:
3447                     s->mv_dir = MV_DIR_BACKWARD;
3448                     s->mv_type = MV_TYPE_FIELD;
3449                     s->mb_intra= 0;
3450                     for(i=0; i<2; i++){
3451                         j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3452                         s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3453                         s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3454                     }
3455                     break;
3456                 case CANDIDATE_MB_TYPE_BIDIR_I:
3457                     s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3458                     s->mv_type = MV_TYPE_FIELD;
3459                     s->mb_intra= 0;
3460                     for(dir=0; dir<2; dir++){
3461                         for(i=0; i<2; i++){
3462                             j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3463                             s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3464                             s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3465                         }
3466                     }
3467                     break;
3468                 default:
3469                     av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
3470                 }
3471
3472                 encode_mb(s, motion_x, motion_y);
3473
3474                 // RAL: Update last macroblock type
3475                 s->last_mv_dir = s->mv_dir;
3476
3477                 if (CONFIG_H263_ENCODER &&
3478                     s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3479                     ff_h263_update_motion_val(s);
3480
3481                 ff_mpv_reconstruct_mb(s, s->block);
3482             }
3483
3484             /* clean the MV table in IPS frames for direct mode in B-frames */
3485             if(s->mb_intra /* && I,P,S_TYPE */){
3486                 s->p_mv_table[xy][0]=0;
3487                 s->p_mv_table[xy][1]=0;
3488             }
3489
3490             if (s->avctx->flags & AV_CODEC_FLAG_PSNR) {
3491                 int w= 16;
3492                 int h= 16;
3493
3494                 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3495                 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3496
3497                 s->current_picture.encoding_error[0] += sse(
3498                     s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3499                     s->dest[0], w, h, s->linesize);
3500                 s->current_picture.encoding_error[1] += sse(
3501                     s, s->new_picture.f->data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3502                     s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3503                 s->current_picture.encoding_error[2] += sse(
3504                     s, s->new_picture.f->data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3505                     s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3506             }
3507             if(s->loop_filter){
3508                 if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
3509                     ff_h263_loop_filter(s);
3510             }
3511             ff_dlog(s->avctx, "MB %d %d bits\n",
3512                     s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
3513         }
3514     }
3515
3516     //not beautiful here but we must write it before flushing so it has to be here
3517     if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
3518         ff_msmpeg4_encode_ext_header(s);
3519
3520     write_slice_end(s);
3521
3522 #if FF_API_RTP_CALLBACK
3523 FF_DISABLE_DEPRECATION_WARNINGS
3524     /* Send the last GOB if RTP */
3525     if (s->avctx->rtp_callback) {
3526         int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
3527         int pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
3528         /* Call the RTP callback to send the last GOB */
3529         emms_c();
3530         s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
3531     }
3532 FF_ENABLE_DEPRECATION_WARNINGS
3533 #endif
3534
3535     return 0;
3536 }
3537
3538 #define MERGE(field) dst->field += src->field; src->field=0
3539 static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
3540     MERGE(me.scene_change_score);
3541     MERGE(me.mc_mb_var_sum_temp);
3542     MERGE(me.mb_var_sum_temp);
3543 }
3544
3545 static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
3546     int i;
3547
3548     MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3549     MERGE(dct_count[1]);
3550     MERGE(mv_bits);
3551     MERGE(i_tex_bits);
3552     MERGE(p_tex_bits);
3553     MERGE(i_count);
3554     MERGE(f_count);
3555     MERGE(b_count);
3556     MERGE(skip_count);
3557     MERGE(misc_bits);
3558     MERGE(er.error_count);
3559     MERGE(padding_bug_score);
3560     MERGE(current_picture.encoding_error[0]);
3561     MERGE(current_picture.encoding_error[1]);
3562     MERGE(current_picture.encoding_error[2]);
3563
3564     if (dst->noise_reduction){
3565         for(i=0; i<64; i++){
3566             MERGE(dct_error_sum[0][i]);
3567             MERGE(dct_error_sum[1][i]);
3568         }
3569     }
3570
3571     av_assert1(put_bits_count(&src->pb) % 8 ==0);
3572     av_assert1(put_bits_count(&dst->pb) % 8 ==0);
3573     ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3574     flush_put_bits(&dst->pb);
3575 }
3576
3577 static int estimate_qp(MpegEncContext *s, int dry_run){
3578     if (s->next_lambda){
3579         s->current_picture_ptr->f->quality =
3580         s->current_picture.f->quality = s->next_lambda;
3581         if(!dry_run) s->next_lambda= 0;
3582     } else if (!s->fixed_qscale) {
3583         int quality = ff_rate_estimate_qscale(s, dry_run);
3584         s->current_picture_ptr->f->quality =
3585         s->current_picture.f->quality = quality;
3586         if (s->current_picture.f->quality < 0)
3587             return -1;
3588     }
3589
3590     if(s->adaptive_quant){
3591         switch(s->codec_id){
3592         case AV_CODEC_ID_MPEG4:
3593             if (CONFIG_MPEG4_ENCODER)
3594                 ff_clean_mpeg4_qscales(s);
3595             break;
3596         case AV_CODEC_ID_H263:
3597         case AV_CODEC_ID_H263P:
3598         case AV_CODEC_ID_FLV1:
3599             if (CONFIG_H263_ENCODER)
3600                 ff_clean_h263_qscales(s);
3601             break;
3602         default:
3603             ff_init_qscale_tab(s);
3604         }
3605
3606         s->lambda= s->lambda_table[0];
3607         //FIXME broken
3608     }else
3609         s->lambda = s->current_picture.f->quality;
3610     update_qscale(s);
3611     return 0;
3612 }
3613
3614 /* must be called before writing the header */
3615 static void set_frame_distances(MpegEncContext * s){
3616     av_assert1(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
3617     s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3618
3619     if(s->pict_type==AV_PICTURE_TYPE_B){
3620         s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3621         av_assert1(s->pb_time > 0 && s->pb_time < s->pp_time);
3622     }else{
3623         s->pp_time= s->time - s->last_non_b_time;
3624         s->last_non_b_time= s->time;
3625         av_assert1(s->picture_number==0 || s->pp_time > 0);
3626     }
3627 }
3628
3629 static int encode_picture(MpegEncContext *s, int picture_number)
3630 {
3631     int i, ret;
3632     int bits;
3633     int context_count = s->slice_context_count;
3634
3635     s->picture_number = picture_number;
3636
3637     /* Reset the average MB variance */
3638     s->me.mb_var_sum_temp    =
3639     s->me.mc_mb_var_sum_temp = 0;
3640
3641     /* we need to initialize some time vars before we can encode B-frames */
3642     // RAL: Condition added for MPEG1VIDEO
3643     if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3644         set_frame_distances(s);
3645     if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3646         ff_set_mpeg4_time(s);
3647
3648     s->me.scene_change_score=0;
3649
3650 //    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3651
3652     if(s->pict_type==AV_PICTURE_TYPE_I){
3653         if(s->msmpeg4_version >= 3) s->no_rounding=1;
3654         else                        s->no_rounding=0;
3655     }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3656         if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3657             s->no_rounding ^= 1;
3658     }
3659
3660     if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
3661         if (estimate_qp(s,1) < 0)
3662             return -1;
3663         ff_get_2pass_fcode(s);
3664     } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
3665         if(s->pict_type==AV_PICTURE_TYPE_B)
3666             s->lambda= s->last_lambda_for[s->pict_type];
3667         else
3668             s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3669         update_qscale(s);
3670     }
3671
3672     if(s->codec_id != AV_CODEC_ID_AMV && s->codec_id != AV_CODEC_ID_MJPEG){
3673         if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
3674         if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
3675         s->q_chroma_intra_matrix   = s->q_intra_matrix;
3676         s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
3677     }
3678
3679     s->mb_intra=0; //for the rate distortion & bit compare functions
3680     for(i=1; i<context_count; i++){
3681         ret = ff_update_duplicate_context(s->thread_context[i], s);
3682         if (ret < 0)
3683             return ret;
3684     }
3685
3686     if(ff_init_me(s)<0)
3687         return -1;
3688
3689     /* Estimate motion for every MB */
3690     if(s->pict_type != AV_PICTURE_TYPE_I){
3691         s->lambda  = (s->lambda  * s->me_penalty_compensation + 128) >> 8;
3692         s->lambda2 = (s->lambda2 * (int64_t) s->me_penalty_compensation + 128) >> 8;
3693         if (s->pict_type != AV_PICTURE_TYPE_B) {
3694             if ((s->me_pre && s->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
3695                 s->me_pre == 2) {
3696                 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3697             }
3698         }
3699
3700         s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3701     }else /* if(s->pict_type == AV_PICTURE_TYPE_I) */{
3702         /* I-Frame */
3703         for(i=0; i<s->mb_stride*s->mb_height; i++)
3704             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3705
3706         if(!s->fixed_qscale){
3707             /* finding spatial complexity for I-frame rate control */
3708             s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3709         }
3710     }
3711     for(i=1; i<context_count; i++){
3712         merge_context_after_me(s, s->thread_context[i]);
3713     }
3714     s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
3715     s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->me.   mb_var_sum_temp;
3716     emms_c();
3717
3718     if (s->me.scene_change_score > s->scenechange_threshold &&
3719         s->pict_type == AV_PICTURE_TYPE_P) {
3720         s->pict_type= AV_PICTURE_TYPE_I;
3721         for(i=0; i<s->mb_stride*s->mb_height; i++)
3722             s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3723         if(s->msmpeg4_version >= 3)
3724             s->no_rounding=1;
3725         ff_dlog(s, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
3726                 s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3727     }
3728
3729     if(!s->umvplus){
3730         if(s->pict_type==AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
3731             s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3732
3733             if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3734                 int a,b;
3735                 a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3736                 b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3737                 s->f_code= FFMAX3(s->f_code, a, b);
3738             }
3739
3740             ff_fix_long_p_mvs(s, s->intra_penalty ? CANDIDATE_MB_TYPE_INTER : CANDIDATE_MB_TYPE_INTRA);
3741             ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, !!s->intra_penalty);
3742             if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3743                 int j;
3744                 for(i=0; i<2; i++){
3745                     for(j=0; j<2; j++)
3746                         ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3747                                         s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, !!s->intra_penalty);
3748                 }
3749             }
3750         }
3751
3752         if(s->pict_type==AV_PICTURE_TYPE_B){
3753             int a, b;
3754
3755             a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3756             b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3757             s->f_code = FFMAX(a, b);
3758
3759             a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3760             b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3761             s->b_code = FFMAX(a, b);
3762
3763             ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3764             ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3765             ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3766             ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3767             if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3768                 int dir, j;
3769                 for(dir=0; dir<2; dir++){
3770                     for(i=0; i<2; i++){
3771                         for(j=0; j<2; j++){
3772                             int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3773                                           : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3774                             ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3775                                             s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3776                         }
3777                     }
3778                 }
3779             }
3780         }
3781     }
3782
3783     if (estimate_qp(s, 0) < 0)
3784         return -1;
3785
3786     if (s->qscale < 3 && s->max_qcoeff <= 128 &&
3787         s->pict_type == AV_PICTURE_TYPE_I &&
3788         !(s->avctx->flags & AV_CODEC_FLAG_QSCALE))
3789         s->qscale= 3; //reduce clipping problems
3790
3791     if (s->out_format == FMT_MJPEG) {
3792         const uint16_t *  luma_matrix = ff_mpeg1_default_intra_matrix;
3793         const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
3794
3795         if (s->avctx->intra_matrix) {
3796             chroma_matrix =
3797             luma_matrix = s->avctx->intra_matrix;
3798         }
3799         if (s->avctx->chroma_intra_matrix)
3800             chroma_matrix = s->avctx->chroma_intra_matrix;
3801
3802         /* for mjpeg, we do include qscale in the matrix */
3803         for(i=1;i<64;i++){
3804             int j = s->idsp.idct_permutation[i];
3805
3806             s->chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->qscale) >> 3);
3807             s->       intra_matrix[j] = av_clip_uint8((  luma_matrix[i] * s->qscale) >> 3);
3808         }
3809         s->y_dc_scale_table=
3810         s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3811         s->chroma_intra_matrix[0] =
3812         s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3813         ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3814                        s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3815         ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3816                        s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3817         s->qscale= 8;
3818     }
3819     if(s->codec_id == AV_CODEC_ID_AMV){
3820         static const uint8_t y[32]={13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
3821         static const uint8_t c[32]={14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
3822         for(i=1;i<64;i++){
3823             int j= s->idsp.idct_permutation[ff_zigzag_direct[i]];
3824
3825             s->intra_matrix[j]        = sp5x_qscale_five_quant_table[0][i];
3826             s->chroma_intra_matrix[j] = sp5x_qscale_five_quant_table[1][i];
3827         }
3828         s->y_dc_scale_table= y;
3829         s->c_dc_scale_table= c;
3830         s->intra_matrix[0] = 13;
3831         s->chroma_intra_matrix[0] = 14;
3832         ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3833                        s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3834         ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3835                        s->chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3836         s->qscale= 8;
3837     }
3838
3839     if (s->out_format == FMT_SPEEDHQ) {
3840         s->y_dc_scale_table=
3841         s->c_dc_scale_table= ff_mpeg2_dc_scale_table[3];
3842     }
3843
3844     //FIXME var duplication
3845     s->current_picture_ptr->f->key_frame =
3846     s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3847     s->current_picture_ptr->f->pict_type =
3848     s->current_picture.f->pict_type = s->pict_type;
3849
3850     if (s->current_picture.f->key_frame)
3851         s->picture_in_gop_number=0;
3852
3853     s->mb_x = s->mb_y = 0;
3854     s->last_bits= put_bits_count(&s->pb);
3855     switch(s->out_format) {
3856 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3857     case FMT_MJPEG:
3858         /* s->huffman == HUFFMAN_TABLE_OPTIMAL can only be true for MJPEG. */
3859         if (!CONFIG_MJPEG_ENCODER || s->huffman != HUFFMAN_TABLE_OPTIMAL)
3860             ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
3861                                            s->pred, s->intra_matrix, s->chroma_intra_matrix);
3862         break;
3863 #endif
3864     case FMT_SPEEDHQ:
3865         if (CONFIG_SPEEDHQ_ENCODER)
3866             ff_speedhq_encode_picture_header(s);
3867         break;
3868     case FMT_H261:
3869         if (CONFIG_H261_ENCODER)
3870             ff_h261_encode_picture_header(s, picture_number);
3871         break;
3872     case FMT_H263:
3873         if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3874             ff_wmv2_encode_picture_header(s, picture_number);
3875         else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3876             ff_msmpeg4_encode_picture_header(s, picture_number);
3877         else if (CONFIG_MPEG4_ENCODER && s->h263_pred) {
3878             ret = ff_mpeg4_encode_picture_header(s, picture_number);
3879             if (ret < 0)
3880                 return ret;
3881         } else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10) {
3882             ret = ff_rv10_encode_picture_header(s, picture_number);
3883             if (ret < 0)
3884                 return ret;
3885         }
3886         else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
3887             ff_rv20_encode_picture_header(s, picture_number);
3888         else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3889             ff_flv_encode_picture_header(s, picture_number);
3890         else if (CONFIG_H263_ENCODER)
3891             ff_h263_encode_picture_header(s, picture_number);
3892         break;
3893     case FMT_MPEG1:
3894         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3895             ff_mpeg1_encode_picture_header(s, picture_number);
3896         break;
3897     default:
3898         av_assert0(0);
3899     }
3900     bits= put_bits_count(&s->pb);
3901     s->header_bits= bits - s->last_bits;
3902
3903     for(i=1; i<context_count; i++){
3904         update_duplicate_context_after_me(s->thread_context[i], s);
3905     }
3906     s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
3907     for(i=1; i<context_count; i++){
3908         if (s->pb.buf_end == s->thread_context[i]->pb.buf)
3909             set_put_bits_buffer_size(&s->pb, FFMIN(s->thread_context[i]->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
3910         merge_context_after_encode(s, s->thread_context[i]);
3911     }
3912     emms_c();
3913     return 0;
3914 }
3915
3916 static void denoise_dct_c(MpegEncContext *s, int16_t *block){
3917     const int intra= s->mb_intra;
3918     int i;
3919
3920     s->dct_count[intra]++;
3921
3922     for(i=0; i<64; i++){
3923         int level= block[i];
3924
3925         if(level){
3926             if(level>0){
3927                 s->dct_error_sum[intra][i] += level;
3928                 level -= s->dct_offset[intra][i];
3929                 if(level<0) level=0;
3930             }else{
3931                 s->dct_error_sum[intra][i] -= level;
3932                 level += s->dct_offset[intra][i];
3933                 if(level>0) level=0;
3934             }
3935             block[i]= level;
3936         }
3937     }
3938 }
3939
3940 static int dct_quantize_trellis_c(MpegEncContext *s,
3941                                   int16_t *block, int n,
3942                                   int qscale, int *overflow){
3943     const int *qmat;
3944     const uint16_t *matrix;
3945     const uint8_t *scantable;
3946     const uint8_t *perm_scantable;
3947     int max=0;
3948     unsigned int threshold1, threshold2;
3949     int bias=0;
3950     int run_tab[65];
3951     int level_tab[65];
3952     int score_tab[65];
3953     int survivor[65];
3954     int survivor_count;
3955     int last_run=0;
3956     int last_level=0;
3957     int last_score= 0;
3958     int last_i;
3959     int coeff[2][64];
3960     int coeff_count[64];
3961     int qmul, qadd, start_i, last_non_zero, i, dc;
3962     const int esc_length= s->ac_esc_length;
3963     uint8_t * length;
3964     uint8_t * last_length;
3965     const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3966     int mpeg2_qscale;
3967
3968     s->fdsp.fdct(block);
3969
3970     if(s->dct_error_sum)
3971         s->denoise_dct(s, block);
3972     qmul= qscale*16;
3973     qadd= ((qscale-1)|1)*8;
3974
3975     if (s->q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
3976     else                 mpeg2_qscale = qscale << 1;
3977
3978     if (s->mb_intra) {
3979         int q;
3980         scantable= s->intra_scantable.scantable;
3981         perm_scantable= s->intra_scantable.permutated;
3982         if (!s->h263_aic) {
3983             if (n < 4)
3984                 q = s->y_dc_scale;
3985             else
3986                 q = s->c_dc_scale;
3987             q = q << 3;
3988         } else{
3989             /* For AIC we skip quant/dequant of INTRADC */
3990             q = 1 << 3;
3991             qadd=0;
3992         }
3993
3994         /* note: block[0] is assumed to be positive */
3995         block[0] = (block[0] + (q >> 1)) / q;
3996         start_i = 1;
3997         last_non_zero = 0;
3998         qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3999         matrix = n < 4 ? s->intra_matrix : s->chroma_intra_matrix;
4000         if(s->mpeg_quant || s->out_format == FMT_MPEG1 || s->out_format == FMT_MJPEG)
4001             bias= 1<<(QMAT_SHIFT-1);
4002
4003         if (n > 3 && s->intra_chroma_ac_vlc_length) {
4004             length     = s->intra_chroma_ac_vlc_length;
4005             last_length= s->intra_chroma_ac_vlc_last_length;
4006         } else {
4007             length     = s->intra_ac_vlc_length;
4008             last_length= s->intra_ac_vlc_last_length;
4009         }
4010     } else {
4011         scantable= s->inter_scantable.scantable;
4012         perm_scantable= s->inter_scantable.permutated;
4013         start_i = 0;
4014         last_non_zero = -1;
4015         qmat = s->q_inter_matrix[qscale];
4016         matrix = s->inter_matrix;
4017         length     = s->inter_ac_vlc_length;
4018         last_length= s->inter_ac_vlc_last_length;
4019     }
4020     last_i= start_i;
4021
4022     threshold1= (1<<QMAT_SHIFT) - bias - 1;
4023     threshold2= (threshold1<<1);
4024
4025     for(i=63; i>=start_i; i--) {
4026         const int j = scantable[i];
4027         int level = block[j] * qmat[j];
4028
4029         if(((unsigned)(level+threshold1))>threshold2){
4030             last_non_zero = i;
4031             break;
4032         }
4033     }
4034
4035     for(i=start_i; i<=last_non_zero; i++) {
4036         const int j = scantable[i];
4037         int level = block[j] * qmat[j];
4038
4039 //        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
4040 //           || bias-level >= (1<<(QMAT_SHIFT - 3))){
4041         if(((unsigned)(level+threshold1))>threshold2){
4042             if(level>0){
4043                 level= (bias + level)>>QMAT_SHIFT;
4044                 coeff[0][i]= level;
4045                 coeff[1][i]= level-1;
4046 //                coeff[2][k]= level-2;
4047             }else{
4048                 level= (bias - level)>>QMAT_SHIFT;
4049                 coeff[0][i]= -level;
4050                 coeff[1][i]= -level+1;
4051 //                coeff[2][k]= -level+2;
4052             }
4053             coeff_count[i]= FFMIN(level, 2);
4054             av_assert2(coeff_count[i]);
4055             max |=level;
4056         }else{
4057             coeff[0][i]= (level>>31)|1;
4058             coeff_count[i]= 1;
4059         }
4060     }
4061
4062     *overflow= s->max_qcoeff < max; //overflow might have happened
4063
4064     if(last_non_zero < start_i){
4065         memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4066         return last_non_zero;
4067     }
4068
4069     score_tab[start_i]= 0;
4070     survivor[0]= start_i;
4071     survivor_count= 1;
4072
4073     for(i=start_i; i<=last_non_zero; i++){
4074         int level_index, j, zero_distortion;
4075         int dct_coeff= FFABS(block[ scantable[i] ]);
4076         int best_score=256*256*256*120;
4077
4078         if (s->fdsp.fdct == ff_fdct_ifast)
4079             dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
4080         zero_distortion= dct_coeff*dct_coeff;
4081
4082         for(level_index=0; level_index < coeff_count[i]; level_index++){
4083             int distortion;
4084             int level= coeff[level_index][i];
4085             const int alevel= FFABS(level);
4086             int unquant_coeff;
4087
4088             av_assert2(level);
4089
4090             if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4091                 unquant_coeff= alevel*qmul + qadd;
4092             } else if(s->out_format == FMT_MJPEG) {
4093                 j = s->idsp.idct_permutation[scantable[i]];
4094                 unquant_coeff = alevel * matrix[j] * 8;
4095             }else{ // MPEG-1
4096                 j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
4097                 if(s->mb_intra){
4098                         unquant_coeff = (int)(  alevel  * mpeg2_qscale * matrix[j]) >> 4;
4099                         unquant_coeff =   (unquant_coeff - 1) | 1;
4100                 }else{
4101                         unquant_coeff = (((  alevel  << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4102                         unquant_coeff =   (unquant_coeff - 1) | 1;
4103                 }
4104                 unquant_coeff<<= 3;
4105             }
4106
4107             distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4108             level+=64;
4109             if((level&(~127)) == 0){
4110                 for(j=survivor_count-1; j>=0; j--){
4111                     int run= i - survivor[j];
4112                     int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4113                     score += score_tab[i-run];
4114
4115                     if(score < best_score){
4116                         best_score= score;
4117                         run_tab[i+1]= run;
4118                         level_tab[i+1]= level-64;
4119                     }
4120                 }
4121
4122                 if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4123                     for(j=survivor_count-1; j>=0; j--){
4124                         int run= i - survivor[j];
4125                         int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4126                         score += score_tab[i-run];
4127                         if(score < last_score){
4128                             last_score= score;
4129                             last_run= run;
4130                             last_level= level-64;
4131                             last_i= i+1;
4132                         }
4133                     }
4134                 }
4135             }else{
4136                 distortion += esc_length*lambda;
4137                 for(j=survivor_count-1; j>=0; j--){
4138                     int run= i - survivor[j];
4139                     int score= distortion + score_tab[i-run];
4140
4141                     if(score < best_score){
4142                         best_score= score;
4143                         run_tab[i+1]= run;
4144                         level_tab[i+1]= level-64;
4145                     }
4146                 }
4147
4148                 if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4149                   for(j=survivor_count-1; j>=0; j--){
4150                         int run= i - survivor[j];
4151                         int score= distortion + score_tab[i-run];
4152                         if(score < last_score){
4153                             last_score= score;
4154                             last_run= run;
4155                             last_level= level-64;
4156                             last_i= i+1;
4157                         }
4158                     }
4159                 }
4160             }
4161         }
4162
4163         score_tab[i+1]= best_score;
4164
4165         // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
4166         if(last_non_zero <= 27){
4167             for(; survivor_count; survivor_count--){
4168                 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4169                     break;
4170             }
4171         }else{
4172             for(; survivor_count; survivor_count--){
4173                 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4174                     break;
4175             }
4176         }
4177
4178         survivor[ survivor_count++ ]= i+1;
4179     }
4180
4181     if(s->out_format != FMT_H263 && s->out_format != FMT_H261){
4182         last_score= 256*256*256*120;
4183         for(i= survivor[0]; i<=last_non_zero + 1; i++){
4184             int score= score_tab[i];
4185             if (i)
4186                 score += lambda * 2; // FIXME more exact?
4187
4188             if(score < last_score){
4189                 last_score= score;
4190                 last_i= i;
4191                 last_level= level_tab[i];
4192                 last_run= run_tab[i];
4193             }
4194         }
4195     }
4196
4197     s->coded_score[n] = last_score;
4198
4199     dc= FFABS(block[0]);
4200     last_non_zero= last_i - 1;
4201     memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4202
4203     if(last_non_zero < start_i)
4204         return last_non_zero;
4205
4206     if(last_non_zero == 0 && start_i == 0){
4207         int best_level= 0;
4208         int best_score= dc * dc;
4209
4210         for(i=0; i<coeff_count[0]; i++){
4211             int level= coeff[i][0];
4212             int alevel= FFABS(level);
4213             int unquant_coeff, score, distortion;
4214
4215             if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
4216                     unquant_coeff= (alevel*qmul + qadd)>>3;
4217             } else{ // MPEG-1
4218                     unquant_coeff = (((  alevel  << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4219                     unquant_coeff =   (unquant_coeff - 1) | 1;
4220             }
4221             unquant_coeff = (unquant_coeff + 4) >> 3;
4222             unquant_coeff<<= 3 + 3;
4223
4224             distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4225             level+=64;
4226             if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4227             else                    score= distortion + esc_length*lambda;
4228
4229             if(score < best_score){
4230                 best_score= score;
4231                 best_level= level - 64;
4232             }
4233         }
4234         block[0]= best_level;
4235         s->coded_score[n] = best_score - dc*dc;
4236         if(best_level == 0) return -1;
4237         else                return last_non_zero;
4238     }
4239
4240     i= last_i;
4241     av_assert2(last_level);
4242
4243     block[ perm_scantable[last_non_zero] ]= last_level;
4244     i -= last_run + 1;
4245
4246     for(; i>start_i; i -= run_tab[i] + 1){
4247         block[ perm_scantable[i-1] ]= level_tab[i];
4248     }
4249
4250     return last_non_zero;
4251 }
4252
4253 static int16_t basis[64][64];
4254
4255 static void build_basis(uint8_t *perm){
4256     int i, j, x, y;
4257     emms_c();
4258     for(i=0; i<8; i++){
4259         for(j=0; j<8; j++){
4260             for(y=0; y<8; y++){
4261                 for(x=0; x<8; x++){
4262                     double s= 0.25*(1<<BASIS_SHIFT);
4263                     int index= 8*i + j;
4264                     int perm_index= perm[index];
4265                     if(i==0) s*= sqrt(0.5);
4266                     if(j==0) s*= sqrt(0.5);
4267                     basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
4268                 }
4269             }
4270         }
4271     }
4272 }
4273
4274 static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
4275                         int16_t *block, int16_t *weight, int16_t *orig,
4276                         int n, int qscale){
4277     int16_t rem[64];
4278     LOCAL_ALIGNED_16(int16_t, d1, [64]);
4279     const uint8_t *scantable;
4280     const uint8_t *perm_scantable;
4281 //    unsigned int threshold1, threshold2;
4282 //    int bias=0;
4283     int run_tab[65];
4284     int prev_run=0;
4285     int prev_level=0;
4286     int qmul, qadd, start_i, last_non_zero, i, dc;
4287     uint8_t * length;
4288     uint8_t * last_length;
4289     int lambda;
4290     int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
4291
4292     if(basis[0][0] == 0)
4293         build_basis(s->idsp.idct_permutation);
4294
4295     qmul= qscale*2;
4296     qadd= (qscale-1)|1;
4297     if (s->mb_intra) {
4298         scantable= s->intra_scantable.scantable;
4299         perm_scantable= s->intra_scantable.permutated;
4300         if (!s->h263_aic) {
4301             if (n < 4)
4302                 q = s->y_dc_scale;
4303             else
4304                 q = s->c_dc_scale;
4305         } else{
4306             /* For AIC we skip quant/dequant of INTRADC */
4307             q = 1;
4308             qadd=0;
4309         }
4310         q <<= RECON_SHIFT-3;
4311         /* note: block[0] is assumed to be positive */
4312         dc= block[0]*q;
4313 //        block[0] = (block[0] + (q >> 1)) / q;
4314         start_i = 1;
4315 //        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
4316 //            bias= 1<<(QMAT_SHIFT-1);
4317         if (n > 3 && s->intra_chroma_ac_vlc_length) {
4318             length     = s->intra_chroma_ac_vlc_length;
4319             last_length= s->intra_chroma_ac_vlc_last_length;
4320         } else {
4321             length     = s->intra_ac_vlc_length;
4322             last_length= s->intra_ac_vlc_last_length;
4323         }
4324     } else {
4325         scantable= s->inter_scantable.scantable;
4326         perm_scantable= s->inter_scantable.permutated;
4327         dc= 0;
4328         start_i = 0;
4329         length     = s->inter_ac_vlc_length;
4330         last_length= s->inter_ac_vlc_last_length;
4331     }
4332     last_non_zero = s->block_last_index[n];
4333
4334     dc += (1<<(RECON_SHIFT-1));
4335     for(i=0; i<64; i++){
4336         rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
4337     }
4338
4339     sum=0;
4340     for(i=0; i<64; i++){
4341         int one= 36;
4342         int qns=4;
4343         int w;
4344
4345         w= FFABS(weight[i]) + qns*one;
4346         w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
4347
4348         weight[i] = w;
4349 //        w=weight[i] = (63*qns + (w/2)) / w;
4350
4351         av_assert2(w>0);
4352         av_assert2(w<(1<<6));
4353         sum += w*w;
4354     }
4355     lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
4356
4357     run=0;
4358     rle_index=0;
4359     for(i=start_i; i<=last_non_zero; i++){
4360         int j= perm_scantable[i];
4361         const int level= block[j];
4362         int coeff;
4363
4364         if(level){
4365             if(level<0) coeff= qmul*level - qadd;
4366             else        coeff= qmul*level + qadd;
4367             run_tab[rle_index++]=run;
4368             run=0;
4369
4370             s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
4371         }else{
4372             run++;
4373         }
4374     }
4375
4376     for(;;){
4377         int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
4378         int best_coeff=0;
4379         int best_change=0;
4380         int run2, best_unquant_change=0, analyze_gradient;
4381         analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
4382
4383         if(analyze_gradient){
4384             for(i=0; i<64; i++){
4385                 int w= weight[i];
4386
4387                 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
4388             }
4389             s->fdsp.fdct(d1);
4390         }
4391
4392         if(start_i){
4393             const int level= block[0];
4394             int change, old_coeff;
4395
4396             av_assert2(s->mb_intra);
4397
4398             old_coeff= q*level;
4399
4400             for(change=-1; change<=1; change+=2){
4401                 int new_level= level + change;
4402                 int score, new_coeff;
4403
4404                 new_coeff= q*new_level;
4405                 if(new_coeff >= 2048 || new_coeff < 0)
4406                     continue;
4407
4408                 score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
4409                                                   new_coeff - old_coeff);
4410                 if(score<best_score){
4411                     best_score= score;
4412                     best_coeff= 0;
4413                     best_change= change;
4414                     best_unquant_change= new_coeff - old_coeff;
4415                 }
4416             }
4417         }
4418
4419         run=0;
4420         rle_index=0;
4421         run2= run_tab[rle_index++];
4422         prev_level=0;
4423         prev_run=0;
4424
4425         for(i=start_i; i<64; i++){
4426             int j= perm_scantable[i];
4427             const int level= block[j];
4428             int change, old_coeff;
4429
4430             if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4431                 break;
4432
4433             if(level){
4434                 if(level<0) old_coeff= qmul*level - qadd;
4435                 else        old_coeff= qmul*level + qadd;
4436                 run2= run_tab[rle_index++]; //FIXME ! maybe after last
4437             }else{
4438                 old_coeff=0;
4439                 run2--;
4440                 av_assert2(run2>=0 || i >= last_non_zero );
4441             }
4442
4443             for(change=-1; change<=1; change+=2){
4444                 int new_level= level + change;
4445                 int score, new_coeff, unquant_change;
4446
4447                 score=0;
4448                 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4449                    continue;
4450
4451                 if(new_level){
4452                     if(new_level<0) new_coeff= qmul*new_level - qadd;
4453                     else            new_coeff= qmul*new_level + qadd;
4454                     if(new_coeff >= 2048 || new_coeff <= -2048)
4455                         continue;
4456                     //FIXME check for overflow
4457
4458                     if(level){
4459                         if(level < 63 && level > -63){
4460                             if(i < last_non_zero)
4461                                 score +=   length[UNI_AC_ENC_INDEX(run, new_level+64)]
4462                                          - length[UNI_AC_ENC_INDEX(run, level+64)];
4463                             else
4464                                 score +=   last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4465                                          - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4466                         }
4467                     }else{
4468                         av_assert2(FFABS(new_level)==1);
4469
4470                         if(analyze_gradient){
4471                             int g= d1[ scantable[i] ];
4472                             if(g && (g^new_level) >= 0)
4473                                 continue;
4474                         }
4475
4476                         if(i < last_non_zero){
4477                             int next_i= i + run2 + 1;
4478                             int next_level= block[ perm_scantable[next_i] ] + 64;
4479
4480                             if(next_level&(~127))
4481                                 next_level= 0;
4482
4483                             if(next_i < last_non_zero)
4484                                 score +=   length[UNI_AC_ENC_INDEX(run, 65)]
4485                                          + length[UNI_AC_ENC_INDEX(run2, next_level)]
4486                                          - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4487                             else
4488                                 score +=  length[UNI_AC_ENC_INDEX(run, 65)]
4489                                         + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4490                                         - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4491                         }else{
4492                             score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4493                             if(prev_level){
4494                                 score +=  length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4495                                         - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4496                             }
4497                         }
4498                     }
4499                 }else{
4500                     new_coeff=0;
4501                     av_assert2(FFABS(level)==1);
4502
4503                     if(i < last_non_zero){
4504                         int next_i= i + run2 + 1;
4505                         int next_level= block[ perm_scantable[next_i] ] + 64;
4506
4507                         if(next_level&(~127))
4508                             next_level= 0;
4509
4510                         if(next_i < last_non_zero)
4511                             score +=   length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4512                                      - length[UNI_AC_ENC_INDEX(run2, next_level)]
4513                                      - length[UNI_AC_ENC_INDEX(run, 65)];
4514                         else
4515                             score +=   last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4516                                      - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4517                                      - length[UNI_AC_ENC_INDEX(run, 65)];
4518                     }else{
4519                         score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4520                         if(prev_level){
4521                             score +=  last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4522                                     - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4523                         }
4524                     }
4525                 }
4526
4527                 score *= lambda;
4528
4529                 unquant_change= new_coeff - old_coeff;
4530                 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4531
4532                 score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4533                                                    unquant_change);
4534                 if(score<best_score){
4535                     best_score= score;
4536                     best_coeff= i;
4537                     best_change= change;
4538                     best_unquant_change= unquant_change;
4539                 }
4540             }
4541             if(level){
4542                 prev_level= level + 64;
4543                 if(prev_level&(~127))
4544                     prev_level= 0;
4545                 prev_run= run;
4546                 run=0;
4547             }else{
4548                 run++;
4549             }
4550         }
4551
4552         if(best_change){
4553             int j= perm_scantable[ best_coeff ];
4554
4555             block[j] += best_change;
4556
4557             if(best_coeff > last_non_zero){
4558                 last_non_zero= best_coeff;
4559                 av_assert2(block[j]);
4560             }else{
4561                 for(; last_non_zero>=start_i; last_non_zero--){
4562                     if(block[perm_scantable[last_non_zero]])
4563                         break;
4564                 }
4565             }
4566
4567             run=0;
4568             rle_index=0;
4569             for(i=start_i; i<=last_non_zero; i++){
4570                 int j= perm_scantable[i];
4571                 const int level= block[j];
4572
4573                  if(level){
4574                      run_tab[rle_index++]=run;
4575                      run=0;
4576                  }else{
4577                      run++;
4578                  }
4579             }
4580
4581             s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4582         }else{
4583             break;
4584         }
4585     }
4586
4587     return last_non_zero;
4588 }
4589
4590 /**
4591  * Permute an 8x8 block according to permutation.
4592  * @param block the block which will be permuted according to
4593  *              the given permutation vector
4594  * @param permutation the permutation vector
4595  * @param last the last non zero coefficient in scantable order, used to
4596  *             speed the permutation up
4597  * @param scantable the used scantable, this is only used to speed the
4598  *                  permutation up, the block is not (inverse) permutated
4599  *                  to scantable order!
4600  */
4601 void ff_block_permute(int16_t *block, uint8_t *permutation,
4602                       const uint8_t *scantable, int last)
4603 {
4604     int i;
4605     int16_t temp[64];
4606
4607     if (last <= 0)
4608         return;
4609     //FIXME it is ok but not clean and might fail for some permutations
4610     // if (permutation[1] == 1)
4611     // return;
4612
4613     for (i = 0; i <= last; i++) {
4614         const int j = scantable[i];
4615         temp[j] = block[j];
4616         block[j] = 0;
4617     }
4618
4619     for (i = 0; i <= last; i++) {
4620         const int j = scantable[i];
4621         const int perm_j = permutation[j];
4622         block[perm_j] = temp[j];
4623     }
4624 }
4625
4626 int ff_dct_quantize_c(MpegEncContext *s,
4627                         int16_t *block, int n,
4628                         int qscale, int *overflow)
4629 {
4630     int i, j, level, last_non_zero, q, start_i;
4631     const int *qmat;
4632     const uint8_t *scantable;
4633     int bias;
4634     int max=0;
4635     unsigned int threshold1, threshold2;
4636
4637     s->fdsp.fdct(block);
4638
4639     if(s->dct_error_sum)
4640         s->denoise_dct(s, block);
4641
4642     if (s->mb_intra) {
4643         scantable= s->intra_scantable.scantable;
4644         if (!s->h263_aic) {
4645             if (n < 4)
4646                 q = s->y_dc_scale;
4647             else
4648                 q = s->c_dc_scale;
4649             q = q << 3;
4650         } else
4651             /* For AIC we skip quant/dequant of INTRADC */
4652             q = 1 << 3;
4653
4654         /* note: block[0] is assumed to be positive */
4655         block[0] = (block[0] + (q >> 1)) / q;
4656         start_i = 1;
4657         last_non_zero = 0;
4658         qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
4659         bias= s->intra_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4660     } else {
4661         scantable= s->inter_scantable.scantable;
4662         start_i = 0;
4663         last_non_zero = -1;
4664         qmat = s->q_inter_matrix[qscale];
4665         bias= s->inter_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4666     }
4667     threshold1= (1<<QMAT_SHIFT) - bias - 1;
4668     threshold2= (threshold1<<1);
4669     for(i=63;i>=start_i;i--) {
4670         j = scantable[i];
4671         level = block[j] * qmat[j];
4672
4673         if(((unsigned)(level+threshold1))>threshold2){
4674             last_non_zero = i;
4675             break;
4676         }else{
4677             block[j]=0;
4678         }
4679     }
4680     for(i=start_i; i<=last_non_zero; i++) {
4681         j = scantable[i];
4682         level = block[j] * qmat[j];
4683
4684 //        if(   bias+level >= (1<<QMAT_SHIFT)
4685 //           || bias-level >= (1<<QMAT_SHIFT)){
4686         if(((unsigned)(level+threshold1))>threshold2){
4687             if(level>0){
4688                 level= (bias + level)>>QMAT_SHIFT;
4689                 block[j]= level;
4690             }else{
4691                 level= (bias - level)>>QMAT_SHIFT;
4692                 block[j]= -level;
4693             }
4694             max |=level;
4695         }else{
4696             block[j]=0;
4697         }
4698     }
4699     *overflow= s->max_qcoeff < max; //overflow might have happened
4700
4701     /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4702     if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
4703         ff_block_permute(block, s->idsp.idct_permutation,
4704                       scantable, last_non_zero);
4705
4706     return last_non_zero;
4707 }
4708
4709 #define OFFSET(x) offsetof(MpegEncContext, x)
4710 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4711 static const AVOption h263_options[] = {
4712     { "obmc",         "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4713     { "mb_info",      "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
4714     FF_MPV_COMMON_OPTS
4715 #if FF_API_MPEGVIDEO_OPTS
4716     FF_MPV_DEPRECATED_MPEG_QUANT_OPT
4717     FF_MPV_DEPRECATED_A53_CC_OPT
4718     FF_MPV_DEPRECATED_MATRIX_OPT
4719 #endif
4720     { NULL },
4721 };
4722
4723 static const AVClass h263_class = {
4724     .class_name = "H.263 encoder",
4725     .item_name  = av_default_item_name,
4726     .option     = h263_options,
4727     .version    = LIBAVUTIL_VERSION_INT,
4728 };
4729
4730 AVCodec ff_h263_encoder = {
4731     .name           = "h263",
4732     .long_name      = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4733     .type           = AVMEDIA_TYPE_VIDEO,
4734     .id             = AV_CODEC_ID_H263,
4735     .priv_data_size = sizeof(MpegEncContext),
4736     .init           = ff_mpv_encode_init,
4737     .encode2        = ff_mpv_encode_picture,
4738     .close          = ff_mpv_encode_end,
4739     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
4740     .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4741     .priv_class     = &h263_class,
4742 };
4743
4744 static const AVOption h263p_options[] = {
4745     { "umv",        "Use unlimited motion vectors.",    OFFSET(umvplus),       AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4746     { "aiv",        "Use alternative inter VLC.",       OFFSET(alt_inter_vlc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4747     { "obmc",       "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
4748     { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
4749     FF_MPV_COMMON_OPTS
4750 #if FF_API_MPEGVIDEO_OPTS
4751     FF_MPV_DEPRECATED_MPEG_QUANT_OPT
4752     FF_MPV_DEPRECATED_A53_CC_OPT
4753     FF_MPV_DEPRECATED_MATRIX_OPT
4754 #endif
4755     { NULL },
4756 };
4757 static const AVClass h263p_class = {
4758     .class_name = "H.263p encoder",
4759     .item_name  = av_default_item_name,
4760     .option     = h263p_options,
4761     .version    = LIBAVUTIL_VERSION_INT,
4762 };
4763
4764 AVCodec ff_h263p_encoder = {
4765     .name           = "h263p",
4766     .long_name      = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4767     .type           = AVMEDIA_TYPE_VIDEO,
4768     .id             = AV_CODEC_ID_H263P,
4769     .priv_data_size = sizeof(MpegEncContext),
4770     .init           = ff_mpv_encode_init,
4771     .encode2        = ff_mpv_encode_picture,
4772     .close          = ff_mpv_encode_end,
4773     .capabilities   = AV_CODEC_CAP_SLICE_THREADS,
4774     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
4775     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4776     .priv_class     = &h263p_class,
4777 };
4778
4779 static const AVClass msmpeg4v2_class = {
4780     .class_name = "msmpeg4v2 encoder",
4781     .item_name  = av_default_item_name,
4782     .option     = ff_mpv_generic_options,
4783     .version    = LIBAVUTIL_VERSION_INT,
4784 };
4785
4786 AVCodec ff_msmpeg4v2_encoder = {
4787     .name           = "msmpeg4v2",
4788     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
4789     .type           = AVMEDIA_TYPE_VIDEO,
4790     .id             = AV_CODEC_ID_MSMPEG4V2,
4791     .priv_data_size = sizeof(MpegEncContext),
4792     .init           = ff_mpv_encode_init,
4793     .encode2        = ff_mpv_encode_picture,
4794     .close          = ff_mpv_encode_end,
4795     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
4796     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4797     .priv_class     = &msmpeg4v2_class,
4798 };
4799
4800 static const AVClass msmpeg4v3_class = {
4801     .class_name = "msmpeg4v3 encoder",
4802     .item_name  = av_default_item_name,
4803     .option     = ff_mpv_generic_options,
4804     .version    = LIBAVUTIL_VERSION_INT,
4805 };
4806
4807 AVCodec ff_msmpeg4v3_encoder = {
4808     .name           = "msmpeg4",
4809     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
4810     .type           = AVMEDIA_TYPE_VIDEO,
4811     .id             = AV_CODEC_ID_MSMPEG4V3,
4812     .priv_data_size = sizeof(MpegEncContext),
4813     .init           = ff_mpv_encode_init,
4814     .encode2        = ff_mpv_encode_picture,
4815     .close          = ff_mpv_encode_end,
4816     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
4817     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4818     .priv_class     = &msmpeg4v3_class,
4819 };
4820
4821 static const AVClass wmv1_class = {
4822     .class_name = "wmv1 encoder",
4823     .item_name  = av_default_item_name,
4824     .option     = ff_mpv_generic_options,
4825     .version    = LIBAVUTIL_VERSION_INT,
4826 };
4827
4828 AVCodec ff_wmv1_encoder = {
4829     .name           = "wmv1",
4830     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
4831     .type           = AVMEDIA_TYPE_VIDEO,
4832     .id             = AV_CODEC_ID_WMV1,
4833     .priv_data_size = sizeof(MpegEncContext),
4834     .init           = ff_mpv_encode_init,
4835     .encode2        = ff_mpv_encode_picture,
4836     .close          = ff_mpv_encode_end,
4837     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
4838     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
4839     .priv_class     = &wmv1_class,
4840 };