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