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