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