Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegvideo_enc.c @ a1a63143

History | View | Annotate | Download (139 KB)

1
/*
2
 * The simplest mpeg encoder (well, it was the simplest!)
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7
 *
8
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24

    
25
/**
26
 * @file libavcodec/mpegvideo_enc.c
27
 * The simplest mpeg encoder (well, it was the simplest!).
28
 */
29

    
30
#include "avcodec.h"
31
#include "dsputil.h"
32
#include "mpegvideo.h"
33
#include "mpegvideo_common.h"
34
#include "mjpegenc.h"
35
#include "msmpeg4.h"
36
#include "h263.h"
37
#include "faandct.h"
38
#include "aandcttab.h"
39
#include <limits.h>
40

    
41
//#undef NDEBUG
42
//#include <assert.h>
43

    
44
static int encode_picture(MpegEncContext *s, int picture_number);
45
static int dct_quantize_refine(MpegEncContext *s, DCTELEM *block, int16_t *weight, DCTELEM *orig, int n, int qscale);
46
static int sse_mb(MpegEncContext *s);
47

    
48
/* enable all paranoid tests for rounding, overflows, etc... */
49
//#define PARANOID
50

    
51
//#define DEBUG
52

    
53
static uint8_t default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
54
static uint8_t default_fcode_tab[MAX_MV*2+1];
55

    
56
void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64], uint16_t (*qmat16)[2][64],
57
                           const uint16_t *quant_matrix, int bias, int qmin, int qmax, int intra)
58
{
59
    int qscale;
60
    int shift=0;
61

    
62
    for(qscale=qmin; qscale<=qmax; qscale++){
63
        int i;
64
        if (dsp->fdct == ff_jpeg_fdct_islow
65
#ifdef FAAN_POSTSCALE
66
            || dsp->fdct == ff_faandct
67
#endif
68
            ) {
69
            for(i=0;i<64;i++) {
70
                const int j= dsp->idct_permutation[i];
71
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
72
                /* 19952             <= ff_aanscales[i] * qscale * quant_matrix[i]               <= 249205026 */
73
                /* (1 << 36) / 19952 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= (1 << 36) / 249205026 */
74
                /* 3444240           >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= 275 */
75

    
76
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
77
                                (qscale * quant_matrix[j]));
78
            }
79
        } else if (dsp->fdct == fdct_ifast
80
#ifndef FAAN_POSTSCALE
81
                   || dsp->fdct == ff_faandct
82
#endif
83
                   ) {
84
            for(i=0;i<64;i++) {
85
                const int j= dsp->idct_permutation[i];
86
                /* 16 <= qscale * quant_matrix[i] <= 7905 */
87
                /* 19952             <= ff_aanscales[i] * qscale * quant_matrix[i]               <= 249205026 */
88
                /* (1 << 36) / 19952 >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
89
                /* 3444240           >= (1 << 36) / (ff_aanscales[i] * qscale * quant_matrix[i]) >= 275 */
90

    
91
                qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) /
92
                                (ff_aanscales[i] * qscale * quant_matrix[j]));
93
            }
94
        } else {
95
            for(i=0;i<64;i++) {
96
                const int j= dsp->idct_permutation[i];
97
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
98
                   So 16           <= qscale * quant_matrix[i]             <= 7905
99
                   so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
100
                   so 32768        >= (1<<19) / (qscale * quant_matrix[i]) >= 67
101
                */
102
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j]));
103
//                qmat  [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
104
                qmat16[qscale][0][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]);
105

    
106
                if(qmat16[qscale][0][i]==0 || qmat16[qscale][0][i]==128*256) qmat16[qscale][0][i]=128*256-1;
107
                qmat16[qscale][1][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][0][i]);
108
            }
109
        }
110

    
111
        for(i=intra; i<64; i++){
112
            int64_t max= 8191;
113
            if (dsp->fdct == fdct_ifast
114
#ifndef FAAN_POSTSCALE
115
                   || dsp->fdct == ff_faandct
116
#endif
117
                   ) {
118
                max = (8191LL*ff_aanscales[i]) >> 14;
119
            }
120
            while(((max * qmat[qscale][i]) >> shift) > INT_MAX){
121
                shift++;
122
            }
123
        }
124
    }
125
    if(shift){
126
        av_log(NULL, AV_LOG_INFO, "Warning, QMAT_SHIFT is larger than %d, overflows possible\n", QMAT_SHIFT - shift);
127
    }
128
}
129

    
130
static inline void update_qscale(MpegEncContext *s){
131
    s->qscale= (s->lambda*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
132
    s->qscale= av_clip(s->qscale, s->avctx->qmin, s->avctx->qmax);
133

    
134
    s->lambda2= (s->lambda*s->lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
135
}
136

    
137
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix){
138
    int i;
139

    
140
    if(matrix){
141
        put_bits(pb, 1, 1);
142
        for(i=0;i<64;i++) {
143
            put_bits(pb, 8, matrix[ ff_zigzag_direct[i] ]);
144
        }
145
    }else
146
        put_bits(pb, 1, 0);
147
}
148

    
149
/**
150
 * init s->current_picture.qscale_table from s->lambda_table
151
 */
152
void ff_init_qscale_tab(MpegEncContext *s){
153
    int8_t * const qscale_table= s->current_picture.qscale_table;
154
    int i;
155

    
156
    for(i=0; i<s->mb_num; i++){
157
        unsigned int lam= s->lambda_table[ s->mb_index2xy[i] ];
158
        int qp= (lam*139 + FF_LAMBDA_SCALE*64) >> (FF_LAMBDA_SHIFT + 7);
159
        qscale_table[ s->mb_index2xy[i] ]= av_clip(qp, s->avctx->qmin, s->avctx->qmax);
160
    }
161
}
162

    
163
static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst, AVFrame *src){
164
    int i;
165

    
166
    dst->pict_type              = src->pict_type;
167
    dst->quality                = src->quality;
168
    dst->coded_picture_number   = src->coded_picture_number;
169
    dst->display_picture_number = src->display_picture_number;
170
//    dst->reference              = src->reference;
171
    dst->pts                    = src->pts;
172
    dst->interlaced_frame       = src->interlaced_frame;
173
    dst->top_field_first        = src->top_field_first;
174

    
175
    if(s->avctx->me_threshold){
176
        if(!src->motion_val[0])
177
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
178
        if(!src->mb_type)
179
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
180
        if(!src->ref_index[0])
181
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
182
        if(src->motion_subsample_log2 != dst->motion_subsample_log2)
183
            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
184
            src->motion_subsample_log2, dst->motion_subsample_log2);
185

    
186
        memcpy(dst->mb_type, src->mb_type, s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
187

    
188
        for(i=0; i<2; i++){
189
            int stride= ((16*s->mb_width )>>src->motion_subsample_log2) + 1;
190
            int height= ((16*s->mb_height)>>src->motion_subsample_log2);
191

    
192
            if(src->motion_val[i] && src->motion_val[i] != dst->motion_val[i]){
193
                memcpy(dst->motion_val[i], src->motion_val[i], 2*stride*height*sizeof(int16_t));
194
            }
195
            if(src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]){
196
                memcpy(dst->ref_index[i], src->ref_index[i], s->b8_stride*2*s->mb_height*sizeof(int8_t));
197
            }
198
        }
199
    }
200
}
201

    
202
static void update_duplicate_context_after_me(MpegEncContext *dst, MpegEncContext *src){
203
#define COPY(a) dst->a= src->a
204
    COPY(pict_type);
205
    COPY(current_picture);
206
    COPY(f_code);
207
    COPY(b_code);
208
    COPY(qscale);
209
    COPY(lambda);
210
    COPY(lambda2);
211
    COPY(picture_in_gop_number);
212
    COPY(gop_picture_number);
213
    COPY(frame_pred_frame_dct); //FIXME don't set in encode_header
214
    COPY(progressive_frame); //FIXME don't set in encode_header
215
    COPY(partitioned_frame); //FIXME don't set in encode_header
216
#undef COPY
217
}
218

    
219
/**
220
 * sets the given MpegEncContext to defaults for encoding.
221
 * the changed fields will not depend upon the prior state of the MpegEncContext.
222
 */
223
static void MPV_encode_defaults(MpegEncContext *s){
224
    int i;
225
    MPV_common_defaults(s);
226

    
227
    for(i=-16; i<16; i++){
228
        default_fcode_tab[i + MAX_MV]= 1;
229
    }
230
    s->me.mv_penalty= default_mv_penalty;
231
    s->fcode_tab= default_fcode_tab;
232
}
233

    
234
/* init video encoder */
235
av_cold int MPV_encode_init(AVCodecContext *avctx)
236
{
237
    MpegEncContext *s = avctx->priv_data;
238
    int i;
239
    int chroma_h_shift, chroma_v_shift;
240

    
241
    MPV_encode_defaults(s);
242

    
243
    switch (avctx->codec_id) {
244
    case CODEC_ID_MPEG2VIDEO:
245
        if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P){
246
            av_log(avctx, AV_LOG_ERROR, "only YUV420 and YUV422 are supported\n");
247
            return -1;
248
        }
249
        break;
250
    case CODEC_ID_LJPEG:
251
    case CODEC_ID_MJPEG:
252
        if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUVJ422P && avctx->pix_fmt != PIX_FMT_RGB32 &&
253
           ((avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P) || avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL)){
254
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
255
            return -1;
256
        }
257
        break;
258
    default:
259
        if(avctx->pix_fmt != PIX_FMT_YUV420P){
260
            av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
261
            return -1;
262
        }
263
    }
264

    
265
    switch (avctx->pix_fmt) {
266
    case PIX_FMT_YUVJ422P:
267
    case PIX_FMT_YUV422P:
268
        s->chroma_format = CHROMA_422;
269
        break;
270
    case PIX_FMT_YUVJ420P:
271
    case PIX_FMT_YUV420P:
272
    default:
273
        s->chroma_format = CHROMA_420;
274
        break;
275
    }
276

    
277
    s->bit_rate = avctx->bit_rate;
278
    s->width = avctx->width;
279
    s->height = avctx->height;
280
    if(avctx->gop_size > 600 && avctx->strict_std_compliance>FF_COMPLIANCE_EXPERIMENTAL){
281
        av_log(avctx, AV_LOG_ERROR, "Warning keyframe interval too large! reducing it ...\n");
282
        avctx->gop_size=600;
283
    }
284
    s->gop_size = avctx->gop_size;
285
    s->avctx = avctx;
286
    s->flags= avctx->flags;
287
    s->flags2= avctx->flags2;
288
    s->max_b_frames= avctx->max_b_frames;
289
    s->codec_id= avctx->codec->id;
290
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
291
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
292
    s->strict_std_compliance= avctx->strict_std_compliance;
293
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
294
    s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0;
295
    s->mpeg_quant= avctx->mpeg_quant;
296
    s->rtp_mode= !!avctx->rtp_payload_size;
297
    s->intra_dc_precision= avctx->intra_dc_precision;
298
    s->user_specified_pts = AV_NOPTS_VALUE;
299

    
300
    if (s->gop_size <= 1) {
301
        s->intra_only = 1;
302
        s->gop_size = 12;
303
    } else {
304
        s->intra_only = 0;
305
    }
306

    
307
    s->me_method = avctx->me_method;
308

    
309
    /* Fixed QSCALE */
310
    s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
311

    
312
    s->adaptive_quant= (   s->avctx->lumi_masking
313
                        || s->avctx->dark_masking
314
                        || s->avctx->temporal_cplx_masking
315
                        || s->avctx->spatial_cplx_masking
316
                        || s->avctx->p_masking
317
                        || s->avctx->border_masking
318
                        || (s->flags&CODEC_FLAG_QP_RD))
319
                       && !s->fixed_qscale;
320

    
321
    s->obmc= !!(s->flags & CODEC_FLAG_OBMC);
322
    s->loop_filter= !!(s->flags & CODEC_FLAG_LOOP_FILTER);
323
    s->alternate_scan= !!(s->flags & CODEC_FLAG_ALT_SCAN);
324
    s->intra_vlc_format= !!(s->flags2 & CODEC_FLAG2_INTRA_VLC);
325
    s->q_scale_type= !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT);
326

    
327
    if(avctx->rc_max_rate && !avctx->rc_buffer_size){
328
        av_log(avctx, AV_LOG_ERROR, "a vbv buffer size is needed, for encoding with a maximum bitrate\n");
329
        return -1;
330
    }
331

    
332
    if(avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate){
333
        av_log(avctx, AV_LOG_INFO, "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
334
    }
335

    
336
    if(avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate){
337
        av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
338
        return -1;
339
    }
340

    
341
    if(avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate){
342
        av_log(avctx, AV_LOG_INFO, "bitrate above max bitrate\n");
343
        return -1;
344
    }
345

    
346
    if(avctx->rc_max_rate && avctx->rc_max_rate == avctx->bit_rate && avctx->rc_max_rate != avctx->rc_min_rate){
347
        av_log(avctx, AV_LOG_INFO, "impossible bitrate constraints, this will fail\n");
348
    }
349

    
350
    if(avctx->rc_buffer_size && avctx->bit_rate*av_q2d(avctx->time_base) > avctx->rc_buffer_size){
351
        av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
352
        return -1;
353
    }
354

    
355
    if(avctx->bit_rate*av_q2d(avctx->time_base) > avctx->bit_rate_tolerance){
356
        av_log(avctx, AV_LOG_ERROR, "bitrate tolerance too small for bitrate\n");
357
        return -1;
358
    }
359

    
360
    if(   s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate
361
       && (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO)
362
       && 90000LL * (avctx->rc_buffer_size-1) > s->avctx->rc_max_rate*0xFFFFLL){
363

    
364
        av_log(avctx, AV_LOG_INFO, "Warning vbv_delay will be set to 0xFFFF (=VBR) as the specified vbv buffer is too large for the given bitrate!\n");
365
    }
366

    
367
    if((s->flags & CODEC_FLAG_4MV) && s->codec_id != CODEC_ID_MPEG4
368
       && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P && s->codec_id != CODEC_ID_FLV1){
369
        av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
370
        return -1;
371
    }
372

    
373
    if(s->obmc && s->avctx->mb_decision != FF_MB_DECISION_SIMPLE){
374
        av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with simple mb decision\n");
375
        return -1;
376
    }
377

    
378
    if(s->obmc && s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P){
379
        av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
380
        return -1;
381
    }
382

    
383
    if(s->quarter_sample && s->codec_id != CODEC_ID_MPEG4){
384
        av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
385
        return -1;
386
    }
387

    
388
    if(s->data_partitioning && s->codec_id != CODEC_ID_MPEG4){
389
        av_log(avctx, AV_LOG_ERROR, "data partitioning not supported by codec\n");
390
        return -1;
391
    }
392

    
393
    if(s->max_b_frames && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO){
394
        av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
395
        return -1;
396
    }
397

    
398
    if((s->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN))
399
       && s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO){
400
        av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
401
        return -1;
402
    }
403

    
404
    if(s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4){ //FIXME mpeg2 uses that too
405
        av_log(avctx, AV_LOG_ERROR, "mpeg2 style quantization not supported by codec\n");
406
        return -1;
407
    }
408

    
409
    if((s->flags & CODEC_FLAG_CBP_RD) && !avctx->trellis){
410
        av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
411
        return -1;
412
    }
413

    
414
    if((s->flags & CODEC_FLAG_QP_RD) && s->avctx->mb_decision != FF_MB_DECISION_RD){
415
        av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
416
        return -1;
417
    }
418

    
419
    if(s->avctx->scenechange_threshold < 1000000000 && (s->flags & CODEC_FLAG_CLOSED_GOP)){
420
        av_log(avctx, AV_LOG_ERROR, "closed gop with scene change detection are not supported yet, set threshold to 1000000000\n");
421
        return -1;
422
    }
423

    
424
    if((s->flags2 & CODEC_FLAG2_INTRA_VLC) && s->codec_id != CODEC_ID_MPEG2VIDEO){
425
        av_log(avctx, AV_LOG_ERROR, "intra vlc table not supported by codec\n");
426
        return -1;
427
    }
428

    
429
    if(s->flags & CODEC_FLAG_LOW_DELAY){
430
        if (s->codec_id != CODEC_ID_MPEG2VIDEO){
431
            av_log(avctx, AV_LOG_ERROR, "low delay forcing is only available for mpeg2\n");
432
            return -1;
433
        }
434
        if (s->max_b_frames != 0){
435
            av_log(avctx, AV_LOG_ERROR, "b frames cannot be used with low delay\n");
436
            return -1;
437
        }
438
    }
439

    
440
    if(s->q_scale_type == 1){
441
        if(s->codec_id != CODEC_ID_MPEG2VIDEO){
442
            av_log(avctx, AV_LOG_ERROR, "non linear quant is only available for mpeg2\n");
443
            return -1;
444
        }
445
        if(avctx->qmax > 12){
446
            av_log(avctx, AV_LOG_ERROR, "non linear quant only supports qmax <= 12 currently\n");
447
            return -1;
448
        }
449
    }
450

    
451
    if(s->avctx->thread_count > 1 && s->codec_id != CODEC_ID_MPEG4
452
       && s->codec_id != CODEC_ID_MPEG1VIDEO && s->codec_id != CODEC_ID_MPEG2VIDEO
453
       && (s->codec_id != CODEC_ID_H263P || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT))){
454
        av_log(avctx, AV_LOG_ERROR, "multi threaded encoding not supported by codec\n");
455
        return -1;
456
    }
457

    
458
    if(s->avctx->thread_count < 1){
459
        av_log(avctx, AV_LOG_ERROR, "automatic thread number detection not supported by codec, patch welcome\n");
460
        return -1;
461
    }
462

    
463
    if(s->avctx->thread_count > 1)
464
        s->rtp_mode= 1;
465

    
466
    if(!avctx->time_base.den || !avctx->time_base.num){
467
        av_log(avctx, AV_LOG_ERROR, "framerate not set\n");
468
        return -1;
469
    }
470

    
471
    i= (INT_MAX/2+128)>>8;
472
    if(avctx->me_threshold >= i){
473
        av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n", i - 1);
474
        return -1;
475
    }
476
    if(avctx->mb_threshold >= i){
477
        av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n", i - 1);
478
        return -1;
479
    }
480

    
481
    if(avctx->b_frame_strategy && (avctx->flags&CODEC_FLAG_PASS2)){
482
        av_log(avctx, AV_LOG_INFO, "notice: b_frame_strategy only affects the first pass\n");
483
        avctx->b_frame_strategy = 0;
484
    }
485

    
486
    i= av_gcd(avctx->time_base.den, avctx->time_base.num);
487
    if(i > 1){
488
        av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
489
        avctx->time_base.den /= i;
490
        avctx->time_base.num /= i;
491
//        return -1;
492
    }
493

    
494
    if(s->codec_id==CODEC_ID_MJPEG){
495
        s->intra_quant_bias= 1<<(QUANT_BIAS_SHIFT-1); //(a + x/2)/x
496
        s->inter_quant_bias= 0;
497
    }else if(s->mpeg_quant || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO){
498
        s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
499
        s->inter_quant_bias= 0;
500
    }else{
501
        s->intra_quant_bias=0;
502
        s->inter_quant_bias=-(1<<(QUANT_BIAS_SHIFT-2)); //(a - x/4)/x
503
    }
504

    
505
    if(avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)
506
        s->intra_quant_bias= avctx->intra_quant_bias;
507
    if(avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
508
        s->inter_quant_bias= avctx->inter_quant_bias;
509

    
510
    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
511

    
512
    if(avctx->codec_id == CODEC_ID_MPEG4 && s->avctx->time_base.den > (1<<16)-1){
513
        av_log(avctx, AV_LOG_ERROR, "timebase not supported by mpeg 4 standard\n");
514
        return -1;
515
    }
516
    s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
517

    
518
    switch(avctx->codec->id) {
519
    case CODEC_ID_MPEG1VIDEO:
520
        s->out_format = FMT_MPEG1;
521
        s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
522
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
523
        break;
524
    case CODEC_ID_MPEG2VIDEO:
525
        s->out_format = FMT_MPEG1;
526
        s->low_delay= !!(s->flags & CODEC_FLAG_LOW_DELAY);
527
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
528
        s->rtp_mode= 1;
529
        break;
530
    case CODEC_ID_LJPEG:
531
    case CODEC_ID_MJPEG:
532
        s->out_format = FMT_MJPEG;
533
        s->intra_only = 1; /* force intra only for jpeg */
534
        if(avctx->codec->id == CODEC_ID_LJPEG && avctx->pix_fmt == PIX_FMT_BGRA){
535
            s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
536
            s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
537
            s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
538
        }else{
539
            s->mjpeg_vsample[0] = 2;
540
            s->mjpeg_vsample[1] = 2>>chroma_v_shift;
541
            s->mjpeg_vsample[2] = 2>>chroma_v_shift;
542
            s->mjpeg_hsample[0] = 2;
543
            s->mjpeg_hsample[1] = 2>>chroma_h_shift;
544
            s->mjpeg_hsample[2] = 2>>chroma_h_shift;
545
        }
546
        if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER)
547
            || ff_mjpeg_encode_init(s) < 0)
548
            return -1;
549
        avctx->delay=0;
550
        s->low_delay=1;
551
        break;
552
    case CODEC_ID_H261:
553
        if (!CONFIG_H261_ENCODER)  return -1;
554
        if (ff_h261_get_picture_format(s->width, s->height) < 0) {
555
            av_log(avctx, AV_LOG_ERROR, "The specified picture size of %dx%d is not valid for the H.261 codec.\nValid sizes are 176x144, 352x288\n", s->width, s->height);
556
            return -1;
557
        }
558
        s->out_format = FMT_H261;
559
        avctx->delay=0;
560
        s->low_delay=1;
561
        break;
562
    case CODEC_ID_H263:
563
        if (!CONFIG_H263_ENCODER)  return -1;
564
        if (h263_get_picture_format(s->width, s->height) == 7) {
565
            av_log(avctx, AV_LOG_INFO, "The specified picture size of %dx%d is not valid for the H.263 codec.\nValid sizes are 128x96, 176x144, 352x288, 704x576, and 1408x1152. Try H.263+.\n", s->width, s->height);
566
            return -1;
567
        }
568
        s->out_format = FMT_H263;
569
        s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
570
        avctx->delay=0;
571
        s->low_delay=1;
572
        break;
573
    case CODEC_ID_H263P:
574
        s->out_format = FMT_H263;
575
        s->h263_plus = 1;
576
        /* Fx */
577
        s->umvplus = (avctx->flags & CODEC_FLAG_H263P_UMV) ? 1:0;
578
        s->h263_aic= (avctx->flags & CODEC_FLAG_AC_PRED) ? 1:0;
579
        s->modified_quant= s->h263_aic;
580
        s->alt_inter_vlc= (avctx->flags & CODEC_FLAG_H263P_AIV) ? 1:0;
581
        s->obmc= (avctx->flags & CODEC_FLAG_OBMC) ? 1:0;
582
        s->loop_filter= (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1:0;
583
        s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
584
        s->h263_slice_structured= (s->flags & CODEC_FLAG_H263P_SLICE_STRUCT) ? 1:0;
585

    
586
        /* /Fx */
587
        /* These are just to be sure */
588
        avctx->delay=0;
589
        s->low_delay=1;
590
        break;
591
    case CODEC_ID_FLV1:
592
        s->out_format = FMT_H263;
593
        s->h263_flv = 2; /* format = 1; 11-bit codes */
594
        s->unrestricted_mv = 1;
595
        s->rtp_mode=0; /* don't allow GOB */
596
        avctx->delay=0;
597
        s->low_delay=1;
598
        break;
599
    case CODEC_ID_RV10:
600
        s->out_format = FMT_H263;
601
        avctx->delay=0;
602
        s->low_delay=1;
603
        break;
604
    case CODEC_ID_RV20:
605
        s->out_format = FMT_H263;
606
        avctx->delay=0;
607
        s->low_delay=1;
608
        s->modified_quant=1;
609
        s->h263_aic=1;
610
        s->h263_plus=1;
611
        s->loop_filter=1;
612
        s->unrestricted_mv= s->obmc || s->loop_filter || s->umvplus;
613
        break;
614
    case CODEC_ID_MPEG4:
615
        s->out_format = FMT_H263;
616
        s->h263_pred = 1;
617
        s->unrestricted_mv = 1;
618
        s->low_delay= s->max_b_frames ? 0 : 1;
619
        avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1);
620
        break;
621
    case CODEC_ID_MSMPEG4V1:
622
        s->out_format = FMT_H263;
623
        s->h263_msmpeg4 = 1;
624
        s->h263_pred = 1;
625
        s->unrestricted_mv = 1;
626
        s->msmpeg4_version= 1;
627
        avctx->delay=0;
628
        s->low_delay=1;
629
        break;
630
    case CODEC_ID_MSMPEG4V2:
631
        s->out_format = FMT_H263;
632
        s->h263_msmpeg4 = 1;
633
        s->h263_pred = 1;
634
        s->unrestricted_mv = 1;
635
        s->msmpeg4_version= 2;
636
        avctx->delay=0;
637
        s->low_delay=1;
638
        break;
639
    case CODEC_ID_MSMPEG4V3:
640
        s->out_format = FMT_H263;
641
        s->h263_msmpeg4 = 1;
642
        s->h263_pred = 1;
643
        s->unrestricted_mv = 1;
644
        s->msmpeg4_version= 3;
645
        s->flipflop_rounding=1;
646
        avctx->delay=0;
647
        s->low_delay=1;
648
        break;
649
    case CODEC_ID_WMV1:
650
        s->out_format = FMT_H263;
651
        s->h263_msmpeg4 = 1;
652
        s->h263_pred = 1;
653
        s->unrestricted_mv = 1;
654
        s->msmpeg4_version= 4;
655
        s->flipflop_rounding=1;
656
        avctx->delay=0;
657
        s->low_delay=1;
658
        break;
659
    case CODEC_ID_WMV2:
660
        s->out_format = FMT_H263;
661
        s->h263_msmpeg4 = 1;
662
        s->h263_pred = 1;
663
        s->unrestricted_mv = 1;
664
        s->msmpeg4_version= 5;
665
        s->flipflop_rounding=1;
666
        avctx->delay=0;
667
        s->low_delay=1;
668
        break;
669
    default:
670
        return -1;
671
    }
672

    
673
    avctx->has_b_frames= !s->low_delay;
674

    
675
    s->encoding = 1;
676

    
677
    s->progressive_frame=
678
    s->progressive_sequence= !(avctx->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME|CODEC_FLAG_ALT_SCAN));
679

    
680
    /* init */
681
    if (MPV_common_init(s) < 0)
682
        return -1;
683

    
684
    if(!s->dct_quantize)
685
        s->dct_quantize = dct_quantize_c;
686
    if(!s->denoise_dct)
687
        s->denoise_dct = denoise_dct_c;
688
    s->fast_dct_quantize = s->dct_quantize;
689
    if(avctx->trellis)
690
        s->dct_quantize = dct_quantize_trellis_c;
691

    
692
    if((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
693
        s->chroma_qscale_table= ff_h263_chroma_qscale_table;
694

    
695
    s->quant_precision=5;
696

    
697
    ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
698
    ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
699

    
700
    if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
701
        ff_h261_encode_init(s);
702
    if (CONFIG_ANY_H263_ENCODER && s->out_format == FMT_H263)
703
        h263_encode_init(s);
704
    if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
705
        ff_msmpeg4_encode_init(s);
706
    if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
707
        && s->out_format == FMT_MPEG1)
708
        ff_mpeg1_encode_init(s);
709

    
710
    /* init q matrix */
711
    for(i=0;i<64;i++) {
712
        int j= s->dsp.idct_permutation[i];
713
        if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){
714
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
715
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
716
        }else if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
717
            s->intra_matrix[j] =
718
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
719
        }else
720
        { /* mpeg1/2 */
721
            s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
722
            s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
723
        }
724
        if(s->avctx->intra_matrix)
725
            s->intra_matrix[j] = s->avctx->intra_matrix[i];
726
        if(s->avctx->inter_matrix)
727
            s->inter_matrix[j] = s->avctx->inter_matrix[i];
728
    }
729

    
730
    /* precompute matrix */
731
    /* for mjpeg, we do include qscale in the matrix */
732
    if (s->out_format != FMT_MJPEG) {
733
        ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
734
                       s->intra_matrix, s->intra_quant_bias, avctx->qmin, 31, 1);
735
        ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
736
                       s->inter_matrix, s->inter_quant_bias, avctx->qmin, 31, 0);
737
    }
738

    
739
    if(ff_rate_control_init(s) < 0)
740
        return -1;
741

    
742
    return 0;
743
}
744

    
745
av_cold int MPV_encode_end(AVCodecContext *avctx)
746
{
747
    MpegEncContext *s = avctx->priv_data;
748

    
749
    ff_rate_control_uninit(s);
750

    
751
    MPV_common_end(s);
752
    if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) && s->out_format == FMT_MJPEG)
753
        ff_mjpeg_encode_close(s);
754

    
755
    av_freep(&avctx->extradata);
756

    
757
    return 0;
758
}
759

    
760
static int get_sae(uint8_t *src, int ref, int stride){
761
    int x,y;
762
    int acc=0;
763

    
764
    for(y=0; y<16; y++){
765
        for(x=0; x<16; x++){
766
            acc+= FFABS(src[x+y*stride] - ref);
767
        }
768
    }
769

    
770
    return acc;
771
}
772

    
773
static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){
774
    int x, y, w, h;
775
    int acc=0;
776

    
777
    w= s->width &~15;
778
    h= s->height&~15;
779

    
780
    for(y=0; y<h; y+=16){
781
        for(x=0; x<w; x+=16){
782
            int offset= x + y*stride;
783
            int sad = s->dsp.sad[0](NULL, src + offset, ref + offset, stride, 16);
784
            int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8;
785
            int sae = get_sae(src + offset, mean, stride);
786

    
787
            acc+= sae + 500 < sad;
788
        }
789
    }
790
    return acc;
791
}
792

    
793

    
794
static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){
795
    AVFrame *pic=NULL;
796
    int64_t pts;
797
    int i;
798
    const int encoding_delay= s->max_b_frames;
799
    int direct=1;
800

    
801
    if(pic_arg){
802
        pts= pic_arg->pts;
803
        pic_arg->display_picture_number= s->input_picture_number++;
804

    
805
        if(pts != AV_NOPTS_VALUE){
806
            if(s->user_specified_pts != AV_NOPTS_VALUE){
807
                int64_t time= pts;
808
                int64_t last= s->user_specified_pts;
809

    
810
                if(time <= last){
811
                    av_log(s->avctx, AV_LOG_ERROR, "Error, Invalid timestamp=%"PRId64", last=%"PRId64"\n", pts, s->user_specified_pts);
812
                    return -1;
813
                }
814
            }
815
            s->user_specified_pts= pts;
816
        }else{
817
            if(s->user_specified_pts != AV_NOPTS_VALUE){
818
                s->user_specified_pts=
819
                pts= s->user_specified_pts + 1;
820
                av_log(s->avctx, AV_LOG_INFO, "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n", pts);
821
            }else{
822
                pts= pic_arg->display_picture_number;
823
            }
824
        }
825
    }
826

    
827
  if(pic_arg){
828
    if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0;
829
    if(pic_arg->linesize[0] != s->linesize) direct=0;
830
    if(pic_arg->linesize[1] != s->uvlinesize) direct=0;
831
    if(pic_arg->linesize[2] != s->uvlinesize) direct=0;
832

    
833
//    av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize);
834

    
835
    if(direct){
836
        i= ff_find_unused_picture(s, 1);
837

    
838
        pic= (AVFrame*)&s->picture[i];
839
        pic->reference= 3;
840

    
841
        for(i=0; i<4; i++){
842
            pic->data[i]= pic_arg->data[i];
843
            pic->linesize[i]= pic_arg->linesize[i];
844
        }
845
        ff_alloc_picture(s, (Picture*)pic, 1);
846
    }else{
847
        i= ff_find_unused_picture(s, 0);
848

    
849
        pic= (AVFrame*)&s->picture[i];
850
        pic->reference= 3;
851

    
852
        ff_alloc_picture(s, (Picture*)pic, 0);
853

    
854
        if(   pic->data[0] + INPLACE_OFFSET == pic_arg->data[0]
855
           && pic->data[1] + INPLACE_OFFSET == pic_arg->data[1]
856
           && pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]){
857
       // empty
858
        }else{
859
            int h_chroma_shift, v_chroma_shift;
860
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
861

    
862
            for(i=0; i<3; i++){
863
                int src_stride= pic_arg->linesize[i];
864
                int dst_stride= i ? s->uvlinesize : s->linesize;
865
                int h_shift= i ? h_chroma_shift : 0;
866
                int v_shift= i ? v_chroma_shift : 0;
867
                int w= s->width >>h_shift;
868
                int h= s->height>>v_shift;
869
                uint8_t *src= pic_arg->data[i];
870
                uint8_t *dst= pic->data[i];
871

    
872
                if(!s->avctx->rc_buffer_size)
873
                    dst +=INPLACE_OFFSET;
874

    
875
                if(src_stride==dst_stride)
876
                    memcpy(dst, src, src_stride*h);
877
                else{
878
                    while(h--){
879
                        memcpy(dst, src, w);
880
                        dst += dst_stride;
881
                        src += src_stride;
882
                    }
883
                }
884
            }
885
        }
886
    }
887
    copy_picture_attributes(s, pic, pic_arg);
888
    pic->pts= pts; //we set this here to avoid modifiying pic_arg
889
  }
890

    
891
    /* shift buffer entries */
892
    for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++)
893
        s->input_picture[i-1]= s->input_picture[i];
894

    
895
    s->input_picture[encoding_delay]= (Picture*)pic;
896

    
897
    return 0;
898
}
899

    
900
static int skip_check(MpegEncContext *s, Picture *p, Picture *ref){
901
    int x, y, plane;
902
    int score=0;
903
    int64_t score64=0;
904

    
905
    for(plane=0; plane<3; plane++){
906
        const int stride= p->linesize[plane];
907
        const int bw= plane ? 1 : 2;
908
        for(y=0; y<s->mb_height*bw; y++){
909
            for(x=0; x<s->mb_width*bw; x++){
910
                int off= p->type == FF_BUFFER_TYPE_SHARED ? 0: 16;
911
                int v= s->dsp.frame_skip_cmp[1](s, p->data[plane] + 8*(x + y*stride)+off, ref->data[plane] + 8*(x + y*stride), stride, 8);
912

    
913
                switch(s->avctx->frame_skip_exp){
914
                    case 0: score= FFMAX(score, v); break;
915
                    case 1: score+= FFABS(v);break;
916
                    case 2: score+= v*v;break;
917
                    case 3: score64+= FFABS(v*v*(int64_t)v);break;
918
                    case 4: score64+= v*v*(int64_t)(v*v);break;
919
                }
920
            }
921
        }
922
    }
923

    
924
    if(score) score64= score;
925

    
926
    if(score64 < s->avctx->frame_skip_threshold)
927
        return 1;
928
    if(score64 < ((s->avctx->frame_skip_factor * (int64_t)s->lambda)>>8))
929
        return 1;
930
    return 0;
931
}
932

    
933
static int estimate_best_b_count(MpegEncContext *s){
934
    AVCodec *codec= avcodec_find_encoder(s->avctx->codec_id);
935
    AVCodecContext *c= avcodec_alloc_context();
936
    AVFrame input[FF_MAX_B_FRAMES+2];
937
    const int scale= s->avctx->brd_scale;
938
    int i, j, out_size, p_lambda, b_lambda, lambda2;
939
    int outbuf_size= s->width * s->height; //FIXME
940
    uint8_t *outbuf= av_malloc(outbuf_size);
941
    int64_t best_rd= INT64_MAX;
942
    int best_b_count= -1;
943

    
944
    assert(scale>=0 && scale <=3);
945

    
946
//    emms_c();
947
    p_lambda= s->last_lambda_for[FF_P_TYPE]; //s->next_picture_ptr->quality;
948
    b_lambda= s->last_lambda_for[FF_B_TYPE]; //p_lambda *FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
949
    if(!b_lambda) b_lambda= p_lambda; //FIXME we should do this somewhere else
950
    lambda2= (b_lambda*b_lambda + (1<<FF_LAMBDA_SHIFT)/2 ) >> FF_LAMBDA_SHIFT;
951

    
952
    c->width = s->width >> scale;
953
    c->height= s->height>> scale;
954
    c->flags= CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR | CODEC_FLAG_INPUT_PRESERVED /*| CODEC_FLAG_EMU_EDGE*/;
955
    c->flags|= s->avctx->flags & CODEC_FLAG_QPEL;
956
    c->mb_decision= s->avctx->mb_decision;
957
    c->me_cmp= s->avctx->me_cmp;
958
    c->mb_cmp= s->avctx->mb_cmp;
959
    c->me_sub_cmp= s->avctx->me_sub_cmp;
960
    c->pix_fmt = PIX_FMT_YUV420P;
961
    c->time_base= s->avctx->time_base;
962
    c->max_b_frames= s->max_b_frames;
963

    
964
    if (avcodec_open(c, codec) < 0)
965
        return -1;
966

    
967
    for(i=0; i<s->max_b_frames+2; i++){
968
        int ysize= c->width*c->height;
969
        int csize= (c->width/2)*(c->height/2);
970
        Picture pre_input, *pre_input_ptr= i ? s->input_picture[i-1] : s->next_picture_ptr;
971

    
972
        avcodec_get_frame_defaults(&input[i]);
973
        input[i].data[0]= av_malloc(ysize + 2*csize);
974
        input[i].data[1]= input[i].data[0] + ysize;
975
        input[i].data[2]= input[i].data[1] + csize;
976
        input[i].linesize[0]= c->width;
977
        input[i].linesize[1]=
978
        input[i].linesize[2]= c->width/2;
979

    
980
        if(pre_input_ptr && (!i || s->input_picture[i-1])) {
981
            pre_input= *pre_input_ptr;
982

    
983
            if(pre_input.type != FF_BUFFER_TYPE_SHARED && i) {
984
                pre_input.data[0]+=INPLACE_OFFSET;
985
                pre_input.data[1]+=INPLACE_OFFSET;
986
                pre_input.data[2]+=INPLACE_OFFSET;
987
            }
988

    
989
            s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0], pre_input.data[0], pre_input.linesize[0], c->width, c->height);
990
            s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1], pre_input.data[1], pre_input.linesize[1], c->width>>1, c->height>>1);
991
            s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2], pre_input.data[2], pre_input.linesize[2], c->width>>1, c->height>>1);
992
        }
993
    }
994

    
995
    for(j=0; j<s->max_b_frames+1; j++){
996
        int64_t rd=0;
997

    
998
        if(!s->input_picture[j])
999
            break;
1000

    
1001
        c->error[0]= c->error[1]= c->error[2]= 0;
1002

    
1003
        input[0].pict_type= FF_I_TYPE;
1004
        input[0].quality= 1 * FF_QP2LAMBDA;
1005
        out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[0]);
1006
//        rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1007

    
1008
        for(i=0; i<s->max_b_frames+1; i++){
1009
            int is_p= i % (j+1) == j || i==s->max_b_frames;
1010

    
1011
            input[i+1].pict_type= is_p ? FF_P_TYPE : FF_B_TYPE;
1012
            input[i+1].quality= is_p ? p_lambda : b_lambda;
1013
            out_size = avcodec_encode_video(c, outbuf, outbuf_size, &input[i+1]);
1014
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1015
        }
1016

    
1017
        /* get the delayed frames */
1018
        while(out_size){
1019
            out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
1020
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1021
        }
1022

    
1023
        rd += c->error[0] + c->error[1] + c->error[2];
1024

    
1025
        if(rd < best_rd){
1026
            best_rd= rd;
1027
            best_b_count= j;
1028
        }
1029
    }
1030

    
1031
    av_freep(&outbuf);
1032
    avcodec_close(c);
1033
    av_freep(&c);
1034

    
1035
    for(i=0; i<s->max_b_frames+2; i++){
1036
        av_freep(&input[i].data[0]);
1037
    }
1038

    
1039
    return best_b_count;
1040
}
1041

    
1042
static void select_input_picture(MpegEncContext *s){
1043
    int i;
1044

    
1045
    for(i=1; i<MAX_PICTURE_COUNT; i++)
1046
        s->reordered_input_picture[i-1]= s->reordered_input_picture[i];
1047
    s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL;
1048

    
1049
    /* set next picture type & ordering */
1050
    if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){
1051
        if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture_ptr==NULL || s->intra_only){
1052
            s->reordered_input_picture[0]= s->input_picture[0];
1053
            s->reordered_input_picture[0]->pict_type= FF_I_TYPE;
1054
            s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1055
        }else{
1056
            int b_frames;
1057

    
1058
            if(s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor){
1059
                if(s->picture_in_gop_number < s->gop_size && skip_check(s, s->input_picture[0], s->next_picture_ptr)){
1060
                //FIXME check that te gop check above is +-1 correct
1061
//av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n", s->input_picture[0]->data[0], s->input_picture[0]->pts);
1062

    
1063
                    if(s->input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1064
                        for(i=0; i<4; i++)
1065
                            s->input_picture[0]->data[i]= NULL;
1066
                        s->input_picture[0]->type= 0;
1067
                    }else{
1068
                        assert(   s->input_picture[0]->type==FF_BUFFER_TYPE_USER
1069
                               || s->input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1070

    
1071
                        s->avctx->release_buffer(s->avctx, (AVFrame*)s->input_picture[0]);
1072
                    }
1073

    
1074
                    emms_c();
1075
                    ff_vbv_update(s, 0);
1076

    
1077
                    goto no_output_pic;
1078
                }
1079
            }
1080

    
1081
            if(s->flags&CODEC_FLAG_PASS2){
1082
                for(i=0; i<s->max_b_frames+1; i++){
1083
                    int pict_num= s->input_picture[0]->display_picture_number + i;
1084

    
1085
                    if(pict_num >= s->rc_context.num_entries)
1086
                        break;
1087
                    if(!s->input_picture[i]){
1088
                        s->rc_context.entry[pict_num-1].new_pict_type = FF_P_TYPE;
1089
                        break;
1090
                    }
1091

    
1092
                    s->input_picture[i]->pict_type=
1093
                        s->rc_context.entry[pict_num].new_pict_type;
1094
                }
1095
            }
1096

    
1097
            if(s->avctx->b_frame_strategy==0){
1098
                b_frames= s->max_b_frames;
1099
                while(b_frames && !s->input_picture[b_frames]) b_frames--;
1100
            }else if(s->avctx->b_frame_strategy==1){
1101
                for(i=1; i<s->max_b_frames+1; i++){
1102
                    if(s->input_picture[i] && s->input_picture[i]->b_frame_score==0){
1103
                        s->input_picture[i]->b_frame_score=
1104
                            get_intra_count(s, s->input_picture[i  ]->data[0],
1105
                                               s->input_picture[i-1]->data[0], s->linesize) + 1;
1106
                    }
1107
                }
1108
                for(i=0; i<s->max_b_frames+1; i++){
1109
                    if(s->input_picture[i]==NULL || s->input_picture[i]->b_frame_score - 1 > s->mb_num/s->avctx->b_sensitivity) break;
1110
                }
1111

    
1112
                b_frames= FFMAX(0, i-1);
1113

    
1114
                /* reset scores */
1115
                for(i=0; i<b_frames+1; i++){
1116
                    s->input_picture[i]->b_frame_score=0;
1117
                }
1118
            }else if(s->avctx->b_frame_strategy==2){
1119
                b_frames= estimate_best_b_count(s);
1120
            }else{
1121
                av_log(s->avctx, AV_LOG_ERROR, "illegal b frame strategy\n");
1122
                b_frames=0;
1123
            }
1124

    
1125
            emms_c();
1126
//static int b_count=0;
1127
//b_count+= b_frames;
1128
//av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1129

    
1130
            for(i= b_frames - 1; i>=0; i--){
1131
                int type= s->input_picture[i]->pict_type;
1132
                if(type && type != FF_B_TYPE)
1133
                    b_frames= i;
1134
            }
1135
            if(s->input_picture[b_frames]->pict_type == FF_B_TYPE && b_frames == s->max_b_frames){
1136
                av_log(s->avctx, AV_LOG_ERROR, "warning, too many b frames in a row\n");
1137
            }
1138

    
1139
            if(s->picture_in_gop_number + b_frames >= s->gop_size){
1140
              if((s->flags2 & CODEC_FLAG2_STRICT_GOP) && s->gop_size > s->picture_in_gop_number){
1141
                    b_frames= s->gop_size - s->picture_in_gop_number - 1;
1142
              }else{
1143
                if(s->flags & CODEC_FLAG_CLOSED_GOP)
1144
                    b_frames=0;
1145
                s->input_picture[b_frames]->pict_type= FF_I_TYPE;
1146
              }
1147
            }
1148

    
1149
            if(   (s->flags & CODEC_FLAG_CLOSED_GOP)
1150
               && b_frames
1151
               && s->input_picture[b_frames]->pict_type== FF_I_TYPE)
1152
                b_frames--;
1153

    
1154
            s->reordered_input_picture[0]= s->input_picture[b_frames];
1155
            if(s->reordered_input_picture[0]->pict_type != FF_I_TYPE)
1156
                s->reordered_input_picture[0]->pict_type= FF_P_TYPE;
1157
            s->reordered_input_picture[0]->coded_picture_number= s->coded_picture_number++;
1158
            for(i=0; i<b_frames; i++){
1159
                s->reordered_input_picture[i+1]= s->input_picture[i];
1160
                s->reordered_input_picture[i+1]->pict_type= FF_B_TYPE;
1161
                s->reordered_input_picture[i+1]->coded_picture_number= s->coded_picture_number++;
1162
            }
1163
        }
1164
    }
1165
no_output_pic:
1166
    if(s->reordered_input_picture[0]){
1167
        s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=FF_B_TYPE ? 3 : 0;
1168

    
1169
        ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1170

    
1171
        if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED || s->avctx->rc_buffer_size){
1172
            // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable
1173

    
1174
            int i= ff_find_unused_picture(s, 0);
1175
            Picture *pic= &s->picture[i];
1176

    
1177
            pic->reference              = s->reordered_input_picture[0]->reference;
1178
            ff_alloc_picture(s, pic, 0);
1179

    
1180
            /* mark us unused / free shared pic */
1181
            if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_INTERNAL)
1182
                s->avctx->release_buffer(s->avctx, (AVFrame*)s->reordered_input_picture[0]);
1183
            for(i=0; i<4; i++)
1184
                s->reordered_input_picture[0]->data[i]= NULL;
1185
            s->reordered_input_picture[0]->type= 0;
1186

    
1187
            copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]);
1188

    
1189
            s->current_picture_ptr= pic;
1190
        }else{
1191
            // input is not a shared pix -> reuse buffer for current_pix
1192

    
1193
            assert(   s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1194
                   || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1195

    
1196
            s->current_picture_ptr= s->reordered_input_picture[0];
1197
            for(i=0; i<4; i++){
1198
                s->new_picture.data[i]+= INPLACE_OFFSET;
1199
            }
1200
        }
1201
        ff_copy_picture(&s->current_picture, s->current_picture_ptr);
1202

    
1203
        s->picture_number= s->new_picture.display_picture_number;
1204
//printf("dpn:%d\n", s->picture_number);
1205
    }else{
1206
       memset(&s->new_picture, 0, sizeof(Picture));
1207
    }
1208
}
1209

    
1210
int MPV_encode_picture(AVCodecContext *avctx,
1211
                       unsigned char *buf, int buf_size, void *data)
1212
{
1213
    MpegEncContext *s = avctx->priv_data;
1214
    AVFrame *pic_arg = data;
1215
    int i, stuffing_count;
1216

    
1217
    for(i=0; i<avctx->thread_count; i++){
1218
        int start_y= s->thread_context[i]->start_mb_y;
1219
        int   end_y= s->thread_context[i]->  end_mb_y;
1220
        int h= s->mb_height;
1221
        uint8_t *start= buf + (size_t)(((int64_t) buf_size)*start_y/h);
1222
        uint8_t *end  = buf + (size_t)(((int64_t) buf_size)*  end_y/h);
1223

    
1224
        init_put_bits(&s->thread_context[i]->pb, start, end - start);
1225
    }
1226

    
1227
    s->picture_in_gop_number++;
1228

    
1229
    if(load_input_picture(s, pic_arg) < 0)
1230
        return -1;
1231

    
1232
    select_input_picture(s);
1233

    
1234
    /* output? */
1235
    if(s->new_picture.data[0]){
1236
        s->pict_type= s->new_picture.pict_type;
1237
//emms_c();
1238
//printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale);
1239
        MPV_frame_start(s, avctx);
1240
vbv_retry:
1241
        if (encode_picture(s, s->picture_number) < 0)
1242
            return -1;
1243

    
1244
        avctx->header_bits = s->header_bits;
1245
        avctx->mv_bits     = s->mv_bits;
1246
        avctx->misc_bits   = s->misc_bits;
1247
        avctx->i_tex_bits  = s->i_tex_bits;
1248
        avctx->p_tex_bits  = s->p_tex_bits;
1249
        avctx->i_count     = s->i_count;
1250
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx
1251
        avctx->skip_count  = s->skip_count;
1252

    
1253
        MPV_frame_end(s);
1254

    
1255
        if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1256
            ff_mjpeg_encode_picture_trailer(s);
1257

    
1258
        if(avctx->rc_buffer_size){
1259
            RateControlContext *rcc= &s->rc_context;
1260
            int max_size= rcc->buffer_index * avctx->rc_max_available_vbv_use;
1261

    
1262
            if(put_bits_count(&s->pb) > max_size && s->lambda < s->avctx->lmax){
1263
                s->next_lambda= FFMAX(s->lambda+1, s->lambda*(s->qscale+1) / s->qscale);
1264
                if(s->adaptive_quant){
1265
                    int i;
1266
                    for(i=0; i<s->mb_height*s->mb_stride; i++)
1267
                        s->lambda_table[i]= FFMAX(s->lambda_table[i]+1, s->lambda_table[i]*(s->qscale+1) / s->qscale);
1268
                }
1269
                s->mb_skipped = 0;        //done in MPV_frame_start()
1270
                if(s->pict_type==FF_P_TYPE){ //done in encode_picture() so we must undo it
1271
                    if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
1272
                        s->no_rounding ^= 1;
1273
                }
1274
                if(s->pict_type!=FF_B_TYPE){
1275
                    s->time_base= s->last_time_base;
1276
                    s->last_non_b_time= s->time - s->pp_time;
1277
                }
1278
//                av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda);
1279
                for(i=0; i<avctx->thread_count; i++){
1280
                    PutBitContext *pb= &s->thread_context[i]->pb;
1281
                    init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1282
                }
1283
                goto vbv_retry;
1284
            }
1285

    
1286
            assert(s->avctx->rc_max_rate);
1287
        }
1288

    
1289
        if(s->flags&CODEC_FLAG_PASS1)
1290
            ff_write_pass1_stats(s);
1291

    
1292
        for(i=0; i<4; i++){
1293
            s->current_picture_ptr->error[i]= s->current_picture.error[i];
1294
            avctx->error[i] += s->current_picture_ptr->error[i];
1295
        }
1296

    
1297
        if(s->flags&CODEC_FLAG_PASS1)
1298
            assert(avctx->header_bits + avctx->mv_bits + avctx->misc_bits + avctx->i_tex_bits + avctx->p_tex_bits == put_bits_count(&s->pb));
1299
        flush_put_bits(&s->pb);
1300
        s->frame_bits  = put_bits_count(&s->pb);
1301

    
1302
        stuffing_count= ff_vbv_update(s, s->frame_bits);
1303
        if(stuffing_count){
1304
            if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < stuffing_count + 50){
1305
                av_log(s->avctx, AV_LOG_ERROR, "stuffing too large\n");
1306
                return -1;
1307
            }
1308

    
1309
            switch(s->codec_id){
1310
            case CODEC_ID_MPEG1VIDEO:
1311
            case CODEC_ID_MPEG2VIDEO:
1312
                while(stuffing_count--){
1313
                    put_bits(&s->pb, 8, 0);
1314
                }
1315
            break;
1316
            case CODEC_ID_MPEG4:
1317
                put_bits(&s->pb, 16, 0);
1318
                put_bits(&s->pb, 16, 0x1C3);
1319
                stuffing_count -= 4;
1320
                while(stuffing_count--){
1321
                    put_bits(&s->pb, 8, 0xFF);
1322
                }
1323
            break;
1324
            default:
1325
                av_log(s->avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1326
            }
1327
            flush_put_bits(&s->pb);
1328
            s->frame_bits  = put_bits_count(&s->pb);
1329
        }
1330

    
1331
        /* update mpeg1/2 vbv_delay for CBR */
1332
        if(s->avctx->rc_max_rate && s->avctx->rc_min_rate == s->avctx->rc_max_rate && s->out_format == FMT_MPEG1
1333
           && 90000LL * (avctx->rc_buffer_size-1) <= s->avctx->rc_max_rate*0xFFFFLL){
1334
            int vbv_delay, min_delay;
1335
            double inbits = s->avctx->rc_max_rate*av_q2d(s->avctx->time_base);
1336
            int    minbits= s->frame_bits - 8*(s->vbv_delay_ptr - s->pb.buf - 1);
1337
            double bits   = s->rc_context.buffer_index + minbits - inbits;
1338

    
1339
            if(bits<0)
1340
                av_log(s->avctx, AV_LOG_ERROR, "Internal error, negative bits\n");
1341

    
1342
            assert(s->repeat_first_field==0);
1343

    
1344
            vbv_delay=     bits * 90000                               / s->avctx->rc_max_rate;
1345
            min_delay= (minbits * 90000LL + s->avctx->rc_max_rate - 1)/ s->avctx->rc_max_rate;
1346

    
1347
            vbv_delay= FFMAX(vbv_delay, min_delay);
1348

    
1349
            assert(vbv_delay < 0xFFFF);
1350

    
1351
            s->vbv_delay_ptr[0] &= 0xF8;
1352
            s->vbv_delay_ptr[0] |= vbv_delay>>13;
1353
            s->vbv_delay_ptr[1]  = vbv_delay>>5;
1354
            s->vbv_delay_ptr[2] &= 0x07;
1355
            s->vbv_delay_ptr[2] |= vbv_delay<<3;
1356
        }
1357
        s->total_bits += s->frame_bits;
1358
        avctx->frame_bits  = s->frame_bits;
1359
    }else{
1360
        assert((put_bits_ptr(&s->pb) == s->pb.buf));
1361
        s->frame_bits=0;
1362
    }
1363
    assert((s->frame_bits&7)==0);
1364

    
1365
    return s->frame_bits/8;
1366
}
1367

    
1368
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold)
1369
{
1370
    static const char tab[64]=
1371
        {3,2,2,1,1,1,1,1,
1372
         1,1,1,1,1,1,1,1,
1373
         1,1,1,1,1,1,1,1,
1374
         0,0,0,0,0,0,0,0,
1375
         0,0,0,0,0,0,0,0,
1376
         0,0,0,0,0,0,0,0,
1377
         0,0,0,0,0,0,0,0,
1378
         0,0,0,0,0,0,0,0};
1379
    int score=0;
1380
    int run=0;
1381
    int i;
1382
    DCTELEM *block= s->block[n];
1383
    const int last_index= s->block_last_index[n];
1384
    int skip_dc;
1385

    
1386
    if(threshold<0){
1387
        skip_dc=0;
1388
        threshold= -threshold;
1389
    }else
1390
        skip_dc=1;
1391

    
1392
    /* Are all we could set to zero already zero? */
1393
    if(last_index<=skip_dc - 1) return;
1394

    
1395
    for(i=0; i<=last_index; i++){
1396
        const int j = s->intra_scantable.permutated[i];
1397
        const int level = FFABS(block[j]);
1398
        if(level==1){
1399
            if(skip_dc && i==0) continue;
1400
            score+= tab[run];
1401
            run=0;
1402
        }else if(level>1){
1403
            return;
1404
        }else{
1405
            run++;
1406
        }
1407
    }
1408
    if(score >= threshold) return;
1409
    for(i=skip_dc; i<=last_index; i++){
1410
        const int j = s->intra_scantable.permutated[i];
1411
        block[j]=0;
1412
    }
1413
    if(block[0]) s->block_last_index[n]= 0;
1414
    else         s->block_last_index[n]= -1;
1415
}
1416

    
1417
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1418
{
1419
    int i;
1420
    const int maxlevel= s->max_qcoeff;
1421
    const int minlevel= s->min_qcoeff;
1422
    int overflow=0;
1423

    
1424
    if(s->mb_intra){
1425
        i=1; //skip clipping of intra dc
1426
    }else
1427
        i=0;
1428

    
1429
    for(;i<=last_index; i++){
1430
        const int j= s->intra_scantable.permutated[i];
1431
        int level = block[j];
1432

    
1433
        if     (level>maxlevel){
1434
            level=maxlevel;
1435
            overflow++;
1436
        }else if(level<minlevel){
1437
            level=minlevel;
1438
            overflow++;
1439
        }
1440

    
1441
        block[j]= level;
1442
    }
1443

    
1444
    if(overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
1445
        av_log(s->avctx, AV_LOG_INFO, "warning, clipping %d dct coefficients to %d..%d\n", overflow, minlevel, maxlevel);
1446
}
1447

    
1448
static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride){
1449
    int x, y;
1450
//FIXME optimize
1451
    for(y=0; y<8; y++){
1452
        for(x=0; x<8; x++){
1453
            int x2, y2;
1454
            int sum=0;
1455
            int sqr=0;
1456
            int count=0;
1457

    
1458
            for(y2= FFMAX(y-1, 0); y2 < FFMIN(8, y+2); y2++){
1459
                for(x2= FFMAX(x-1, 0); x2 < FFMIN(8, x+2); x2++){
1460
                    int v= ptr[x2 + y2*stride];
1461
                    sum += v;
1462
                    sqr += v*v;
1463
                    count++;
1464
                }
1465
            }
1466
            weight[x + 8*y]= (36*ff_sqrt(count*sqr - sum*sum)) / count;
1467
        }
1468
    }
1469
}
1470

    
1471
static av_always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count)
1472
{
1473
    int16_t weight[8][64];
1474
    DCTELEM orig[8][64];
1475
    const int mb_x= s->mb_x;
1476
    const int mb_y= s->mb_y;
1477
    int i;
1478
    int skip_dct[8];
1479
    int dct_offset   = s->linesize*8; //default for progressive frames
1480
    uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1481
    int wrap_y, wrap_c;
1482

    
1483
    for(i=0; i<mb_block_count; i++) skip_dct[i]=s->skipdct;
1484

    
1485
    if(s->adaptive_quant){
1486
        const int last_qp= s->qscale;
1487
        const int mb_xy= mb_x + mb_y*s->mb_stride;
1488

    
1489
        s->lambda= s->lambda_table[mb_xy];
1490
        update_qscale(s);
1491

    
1492
        if(!(s->flags&CODEC_FLAG_QP_RD)){
1493
            s->qscale= s->current_picture_ptr->qscale_table[mb_xy];
1494
            s->dquant= s->qscale - last_qp;
1495

    
1496
            if(s->out_format==FMT_H263){
1497
                s->dquant= av_clip(s->dquant, -2, 2);
1498

    
1499
                if(s->codec_id==CODEC_ID_MPEG4){
1500
                    if(!s->mb_intra){
1501
                        if(s->pict_type == FF_B_TYPE){
1502
                            if(s->dquant&1 || s->mv_dir&MV_DIRECT)
1503
                                s->dquant= 0;
1504
                        }
1505
                        if(s->mv_type==MV_TYPE_8X8)
1506
                            s->dquant=0;
1507
                    }
1508
                }
1509
            }
1510
        }
1511
        ff_set_qscale(s, last_qp + s->dquant);
1512
    }else if(s->flags&CODEC_FLAG_QP_RD)
1513
        ff_set_qscale(s, s->qscale + s->dquant);
1514

    
1515
    wrap_y = s->linesize;
1516
    wrap_c = s->uvlinesize;
1517
    ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
1518
    ptr_cb = s->new_picture.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1519
    ptr_cr = s->new_picture.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1520

    
1521
    if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
1522
        uint8_t *ebuf= s->edge_emu_buffer + 32;
1523
        ff_emulated_edge_mc(ebuf            , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width   , s->height);
1524
        ptr_y= ebuf;
1525
        ff_emulated_edge_mc(ebuf+18*wrap_y  , ptr_cb, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
1526
        ptr_cb= ebuf+18*wrap_y;
1527
        ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, mb_block_height, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
1528
        ptr_cr= ebuf+18*wrap_y+8;
1529
    }
1530

    
1531
    if (s->mb_intra) {
1532
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
1533
            int progressive_score, interlaced_score;
1534

    
1535
            s->interlaced_dct=0;
1536
            progressive_score= s->dsp.ildct_cmp[4](s, ptr_y           , NULL, wrap_y, 8)
1537
                              +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y*8, NULL, wrap_y, 8) - 400;
1538

    
1539
            if(progressive_score > 0){
1540
                interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y           , NULL, wrap_y*2, 8)
1541
                                  +s->dsp.ildct_cmp[4](s, ptr_y + wrap_y  , NULL, wrap_y*2, 8);
1542
                if(progressive_score > interlaced_score){
1543
                    s->interlaced_dct=1;
1544

    
1545
                    dct_offset= wrap_y;
1546
                    wrap_y<<=1;
1547
                    if (s->chroma_format == CHROMA_422)
1548
                        wrap_c<<=1;
1549
                }
1550
            }
1551
        }
1552

    
1553
        s->dsp.get_pixels(s->block[0], ptr_y                 , wrap_y);
1554
        s->dsp.get_pixels(s->block[1], ptr_y              + 8, wrap_y);
1555
        s->dsp.get_pixels(s->block[2], ptr_y + dct_offset    , wrap_y);
1556
        s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
1557

    
1558
        if(s->flags&CODEC_FLAG_GRAY){
1559
            skip_dct[4]= 1;
1560
            skip_dct[5]= 1;
1561
        }else{
1562
            s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1563
            s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1564
            if(!s->chroma_y_shift){ /* 422 */
1565
                s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset>>1), wrap_c);
1566
                s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset>>1), wrap_c);
1567
            }
1568
        }
1569
    }else{
1570
        op_pixels_func (*op_pix)[4];
1571
        qpel_mc_func (*op_qpix)[16];
1572
        uint8_t *dest_y, *dest_cb, *dest_cr;
1573

    
1574
        dest_y  = s->dest[0];
1575
        dest_cb = s->dest[1];
1576
        dest_cr = s->dest[2];
1577

    
1578
        if ((!s->no_rounding) || s->pict_type==FF_B_TYPE){
1579
            op_pix = s->dsp.put_pixels_tab;
1580
            op_qpix= s->dsp.put_qpel_pixels_tab;
1581
        }else{
1582
            op_pix = s->dsp.put_no_rnd_pixels_tab;
1583
            op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab;
1584
        }
1585

    
1586
        if (s->mv_dir & MV_DIR_FORWARD) {
1587
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix);
1588
            op_pix = s->dsp.avg_pixels_tab;
1589
            op_qpix= s->dsp.avg_qpel_pixels_tab;
1590
        }
1591
        if (s->mv_dir & MV_DIR_BACKWARD) {
1592
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix);
1593
        }
1594

    
1595
        if(s->flags&CODEC_FLAG_INTERLACED_DCT){
1596
            int progressive_score, interlaced_score;
1597

    
1598
            s->interlaced_dct=0;
1599
            progressive_score= s->dsp.ildct_cmp[0](s, dest_y           , ptr_y           , wrap_y, 8)
1600
                              +s->dsp.ildct_cmp[0](s, dest_y + wrap_y*8, ptr_y + wrap_y*8, wrap_y, 8) - 400;
1601

    
1602
            if(s->avctx->ildct_cmp == FF_CMP_VSSE) progressive_score -= 400;
1603

    
1604
            if(progressive_score>0){
1605
                interlaced_score = s->dsp.ildct_cmp[0](s, dest_y           , ptr_y           , wrap_y*2, 8)
1606
                                  +s->dsp.ildct_cmp[0](s, dest_y + wrap_y  , ptr_y + wrap_y  , wrap_y*2, 8);
1607

    
1608
                if(progressive_score > interlaced_score){
1609
                    s->interlaced_dct=1;
1610

    
1611
                    dct_offset= wrap_y;
1612
                    wrap_y<<=1;
1613
                    if (s->chroma_format == CHROMA_422)
1614
                        wrap_c<<=1;
1615
                }
1616
            }
1617
        }
1618

    
1619
        s->dsp.diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
1620
        s->dsp.diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
1621
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset    , dest_y + dct_offset    , wrap_y);
1622
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y);
1623

    
1624
        if(s->flags&CODEC_FLAG_GRAY){
1625
            skip_dct[4]= 1;
1626
            skip_dct[5]= 1;
1627
        }else{
1628
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1629
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1630
            if(!s->chroma_y_shift){ /* 422 */
1631
                s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset>>1), dest_cb + (dct_offset>>1), wrap_c);
1632
                s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset>>1), dest_cr + (dct_offset>>1), wrap_c);
1633
            }
1634
        }
1635
        /* pre quantization */
1636
        if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
1637
            //FIXME optimize
1638
            if(s->dsp.sad[1](NULL, ptr_y               , dest_y               , wrap_y, 8) < 20*s->qscale) skip_dct[0]= 1;
1639
            if(s->dsp.sad[1](NULL, ptr_y            + 8, dest_y            + 8, wrap_y, 8) < 20*s->qscale) skip_dct[1]= 1;
1640
            if(s->dsp.sad[1](NULL, ptr_y +dct_offset   , dest_y +dct_offset   , wrap_y, 8) < 20*s->qscale) skip_dct[2]= 1;
1641
            if(s->dsp.sad[1](NULL, ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y, 8) < 20*s->qscale) skip_dct[3]= 1;
1642
            if(s->dsp.sad[1](NULL, ptr_cb              , dest_cb              , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
1643
            if(s->dsp.sad[1](NULL, ptr_cr              , dest_cr              , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
1644
            if(!s->chroma_y_shift){ /* 422 */
1645
                if(s->dsp.sad[1](NULL, ptr_cb +(dct_offset>>1), dest_cb +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[6]= 1;
1646
                if(s->dsp.sad[1](NULL, ptr_cr +(dct_offset>>1), dest_cr +(dct_offset>>1), wrap_c, 8) < 20*s->qscale) skip_dct[7]= 1;
1647
            }
1648
        }
1649
    }
1650

    
1651
    if(s->avctx->quantizer_noise_shaping){
1652
        if(!skip_dct[0]) get_visual_weight(weight[0], ptr_y                 , wrap_y);
1653
        if(!skip_dct[1]) get_visual_weight(weight[1], ptr_y              + 8, wrap_y);
1654
        if(!skip_dct[2]) get_visual_weight(weight[2], ptr_y + dct_offset    , wrap_y);
1655
        if(!skip_dct[3]) get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
1656
        if(!skip_dct[4]) get_visual_weight(weight[4], ptr_cb                , wrap_c);
1657
        if(!skip_dct[5]) get_visual_weight(weight[5], ptr_cr                , wrap_c);
1658
        if(!s->chroma_y_shift){ /* 422 */
1659
            if(!skip_dct[6]) get_visual_weight(weight[6], ptr_cb + (dct_offset>>1), wrap_c);
1660
            if(!skip_dct[7]) get_visual_weight(weight[7], ptr_cr + (dct_offset>>1), wrap_c);
1661
        }
1662
        memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*mb_block_count);
1663
    }
1664

    
1665
    /* DCT & quantize */
1666
    assert(s->out_format!=FMT_MJPEG || s->qscale==8);
1667
    {
1668
        for(i=0;i<mb_block_count;i++) {
1669
            if(!skip_dct[i]){
1670
                int overflow;
1671
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1672
            // FIXME we could decide to change to quantizer instead of clipping
1673
            // JS: I don't think that would be a good idea it could lower quality instead
1674
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
1675
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1676
            }else
1677
                s->block_last_index[i]= -1;
1678
        }
1679
        if(s->avctx->quantizer_noise_shaping){
1680
            for(i=0;i<mb_block_count;i++) {
1681
                if(!skip_dct[i]){
1682
                    s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
1683
                }
1684
            }
1685
        }
1686

    
1687
        if(s->luma_elim_threshold && !s->mb_intra)
1688
            for(i=0; i<4; i++)
1689
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
1690
        if(s->chroma_elim_threshold && !s->mb_intra)
1691
            for(i=4; i<mb_block_count; i++)
1692
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
1693

    
1694
        if(s->flags & CODEC_FLAG_CBP_RD){
1695
            for(i=0;i<mb_block_count;i++) {
1696
                if(s->block_last_index[i] == -1)
1697
                    s->coded_score[i]= INT_MAX/256;
1698
            }
1699
        }
1700
    }
1701

    
1702
    if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){
1703
        s->block_last_index[4]=
1704
        s->block_last_index[5]= 0;
1705
        s->block[4][0]=
1706
        s->block[5][0]= (1024 + s->c_dc_scale/2)/ s->c_dc_scale;
1707
    }
1708

    
1709
    //non c quantize code returns incorrect block_last_index FIXME
1710
    if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
1711
        for(i=0; i<mb_block_count; i++){
1712
            int j;
1713
            if(s->block_last_index[i]>0){
1714
                for(j=63; j>0; j--){
1715
                    if(s->block[i][ s->intra_scantable.permutated[j] ]) break;
1716
                }
1717
                s->block_last_index[i]= j;
1718
            }
1719
        }
1720
    }
1721

    
1722
    /* huffman encode */
1723
    switch(s->codec_id){ //FIXME funct ptr could be slightly faster
1724
    case CODEC_ID_MPEG1VIDEO:
1725
    case CODEC_ID_MPEG2VIDEO:
1726
        if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
1727
            mpeg1_encode_mb(s, s->block, motion_x, motion_y);
1728
        break;
1729
    case CODEC_ID_MPEG4:
1730
        if (CONFIG_MPEG4_ENCODER)
1731
            mpeg4_encode_mb(s, s->block, motion_x, motion_y);
1732
        break;
1733
    case CODEC_ID_MSMPEG4V2:
1734
    case CODEC_ID_MSMPEG4V3:
1735
    case CODEC_ID_WMV1:
1736
        if (CONFIG_MSMPEG4_ENCODER)
1737
            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
1738
        break;
1739
    case CODEC_ID_WMV2:
1740
        if (CONFIG_WMV2_ENCODER)
1741
            ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
1742
        break;
1743
    case CODEC_ID_H261:
1744
        if (CONFIG_H261_ENCODER)
1745
            ff_h261_encode_mb(s, s->block, motion_x, motion_y);
1746
        break;
1747
    case CODEC_ID_H263:
1748
    case CODEC_ID_H263P:
1749
    case CODEC_ID_FLV1:
1750
    case CODEC_ID_RV10:
1751
    case CODEC_ID_RV20:
1752
        if (CONFIG_H263_ENCODER)
1753
            h263_encode_mb(s, s->block, motion_x, motion_y);
1754
        break;
1755
    case CODEC_ID_MJPEG:
1756
        if (CONFIG_MJPEG_ENCODER)
1757
            ff_mjpeg_encode_mb(s, s->block);
1758
        break;
1759
    default:
1760
        assert(0);
1761
    }
1762
}
1763

    
1764
static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1765
{
1766
    if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 6);
1767
    else                                encode_mb_internal(s, motion_x, motion_y, 16, 8);
1768
}
1769

    
1770
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1771
    int i;
1772

    
1773
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1774

    
1775
    /* mpeg1 */
1776
    d->mb_skip_run= s->mb_skip_run;
1777
    for(i=0; i<3; i++)
1778
        d->last_dc[i]= s->last_dc[i];
1779

    
1780
    /* statistics */
1781
    d->mv_bits= s->mv_bits;
1782
    d->i_tex_bits= s->i_tex_bits;
1783
    d->p_tex_bits= s->p_tex_bits;
1784
    d->i_count= s->i_count;
1785
    d->f_count= s->f_count;
1786
    d->b_count= s->b_count;
1787
    d->skip_count= s->skip_count;
1788
    d->misc_bits= s->misc_bits;
1789
    d->last_bits= 0;
1790

    
1791
    d->mb_skipped= 0;
1792
    d->qscale= s->qscale;
1793
    d->dquant= s->dquant;
1794

    
1795
    d->esc3_level_length= s->esc3_level_length;
1796
}
1797

    
1798
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1799
    int i;
1800

    
1801
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
1802
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
1803

    
1804
    /* mpeg1 */
1805
    d->mb_skip_run= s->mb_skip_run;
1806
    for(i=0; i<3; i++)
1807
        d->last_dc[i]= s->last_dc[i];
1808

    
1809
    /* statistics */
1810
    d->mv_bits= s->mv_bits;
1811
    d->i_tex_bits= s->i_tex_bits;
1812
    d->p_tex_bits= s->p_tex_bits;
1813
    d->i_count= s->i_count;
1814
    d->f_count= s->f_count;
1815
    d->b_count= s->b_count;
1816
    d->skip_count= s->skip_count;
1817
    d->misc_bits= s->misc_bits;
1818

    
1819
    d->mb_intra= s->mb_intra;
1820
    d->mb_skipped= s->mb_skipped;
1821
    d->mv_type= s->mv_type;
1822
    d->mv_dir= s->mv_dir;
1823
    d->pb= s->pb;
1824
    if(s->data_partitioning){
1825
        d->pb2= s->pb2;
1826
        d->tex_pb= s->tex_pb;
1827
    }
1828
    d->block= s->block;
1829
    for(i=0; i<8; i++)
1830
        d->block_last_index[i]= s->block_last_index[i];
1831
    d->interlaced_dct= s->interlaced_dct;
1832
    d->qscale= s->qscale;
1833

    
1834
    d->esc3_level_length= s->esc3_level_length;
1835
}
1836

    
1837
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
1838
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
1839
                           int *dmin, int *next_block, int motion_x, int motion_y)
1840
{
1841
    int score;
1842
    uint8_t *dest_backup[3];
1843

    
1844
    copy_context_before_encode(s, backup, type);
1845

    
1846
    s->block= s->blocks[*next_block];
1847
    s->pb= pb[*next_block];
1848
    if(s->data_partitioning){
1849
        s->pb2   = pb2   [*next_block];
1850
        s->tex_pb= tex_pb[*next_block];
1851
    }
1852

    
1853
    if(*next_block){
1854
        memcpy(dest_backup, s->dest, sizeof(s->dest));
1855
        s->dest[0] = s->rd_scratchpad;
1856
        s->dest[1] = s->rd_scratchpad + 16*s->linesize;
1857
        s->dest[2] = s->rd_scratchpad + 16*s->linesize + 8;
1858
        assert(s->linesize >= 32); //FIXME
1859
    }
1860

    
1861
    encode_mb(s, motion_x, motion_y);
1862

    
1863
    score= put_bits_count(&s->pb);
1864
    if(s->data_partitioning){
1865
        score+= put_bits_count(&s->pb2);
1866
        score+= put_bits_count(&s->tex_pb);
1867
    }
1868

    
1869
    if(s->avctx->mb_decision == FF_MB_DECISION_RD){
1870
        MPV_decode_mb(s, s->block);
1871

    
1872
        score *= s->lambda2;
1873
        score += sse_mb(s) << FF_LAMBDA_SHIFT;
1874
    }
1875

    
1876
    if(*next_block){
1877
        memcpy(s->dest, dest_backup, sizeof(s->dest));
1878
    }
1879

    
1880
    if(score<*dmin){
1881
        *dmin= score;
1882
        *next_block^=1;
1883

    
1884
        copy_context_after_encode(best, s, type);
1885
    }
1886
}
1887

    
1888
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
1889
    uint32_t *sq = ff_squareTbl + 256;
1890
    int acc=0;
1891
    int x,y;
1892

    
1893
    if(w==16 && h==16)
1894
        return s->dsp.sse[0](NULL, src1, src2, stride, 16);
1895
    else if(w==8 && h==8)
1896
        return s->dsp.sse[1](NULL, src1, src2, stride, 8);
1897

    
1898
    for(y=0; y<h; y++){
1899
        for(x=0; x<w; x++){
1900
            acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
1901
        }
1902
    }
1903

    
1904
    assert(acc>=0);
1905

    
1906
    return acc;
1907
}
1908

    
1909
static int sse_mb(MpegEncContext *s){
1910
    int w= 16;
1911
    int h= 16;
1912

    
1913
    if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
1914
    if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
1915

    
1916
    if(w==16 && h==16)
1917
      if(s->avctx->mb_cmp == FF_CMP_NSSE){
1918
        return  s->dsp.nsse[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
1919
               +s->dsp.nsse[1](s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
1920
               +s->dsp.nsse[1](s, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
1921
      }else{
1922
        return  s->dsp.sse[0](NULL, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], s->linesize, 16)
1923
               +s->dsp.sse[1](NULL, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], s->uvlinesize, 8)
1924
               +s->dsp.sse[1](NULL, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], s->uvlinesize, 8);
1925
      }
1926
    else
1927
        return  sse(s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
1928
               +sse(s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
1929
               +sse(s, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
1930
}
1931

    
1932
static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
1933
    MpegEncContext *s= *(void**)arg;
1934

    
1935

    
1936
    s->me.pre_pass=1;
1937
    s->me.dia_size= s->avctx->pre_dia_size;
1938
    s->first_slice_line=1;
1939
    for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
1940
        for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
1941
            ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
1942
        }
1943
        s->first_slice_line=0;
1944
    }
1945

    
1946
    s->me.pre_pass=0;
1947

    
1948
    return 0;
1949
}
1950

    
1951
static int estimate_motion_thread(AVCodecContext *c, void *arg){
1952
    MpegEncContext *s= *(void**)arg;
1953

    
1954
    ff_check_alignment();
1955

    
1956
    s->me.dia_size= s->avctx->dia_size;
1957
    s->first_slice_line=1;
1958
    for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
1959
        s->mb_x=0; //for block init below
1960
        ff_init_block_index(s);
1961
        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
1962
            s->block_index[0]+=2;
1963
            s->block_index[1]+=2;
1964
            s->block_index[2]+=2;
1965
            s->block_index[3]+=2;
1966

    
1967
            /* compute motion vector & mb_type and store in context */
1968
            if(s->pict_type==FF_B_TYPE)
1969
                ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
1970
            else
1971
                ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
1972
        }
1973
        s->first_slice_line=0;
1974
    }
1975
    return 0;
1976
}
1977

    
1978
static int mb_var_thread(AVCodecContext *c, void *arg){
1979
    MpegEncContext *s= *(void**)arg;
1980
    int mb_x, mb_y;
1981

    
1982
    ff_check_alignment();
1983

    
1984
    for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
1985
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1986
            int xx = mb_x * 16;
1987
            int yy = mb_y * 16;
1988
            uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
1989
            int varc;
1990
            int sum = s->dsp.pix_sum(pix, s->linesize);
1991

    
1992
            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
1993

    
1994
            s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
1995
            s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
1996
            s->me.mb_var_sum_temp    += varc;
1997
        }
1998
    }
1999
    return 0;
2000
}
2001

    
2002
static void write_slice_end(MpegEncContext *s){
2003
    if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
2004
        if(s->partitioned_frame){
2005
            ff_mpeg4_merge_partitions(s);
2006
        }
2007

    
2008
        ff_mpeg4_stuffing(&s->pb);
2009
    }else if(CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG){
2010
        ff_mjpeg_encode_stuffing(&s->pb);
2011
    }
2012

    
2013
    align_put_bits(&s->pb);
2014
    flush_put_bits(&s->pb);
2015

    
2016
    if((s->flags&CODEC_FLAG_PASS1) && !s->partitioned_frame)
2017
        s->misc_bits+= get_bits_diff(s);
2018
}
2019

    
2020
static int encode_thread(AVCodecContext *c, void *arg){
2021
    MpegEncContext *s= *(void**)arg;
2022
    int mb_x, mb_y, pdif = 0;
2023
    int chr_h= 16>>s->chroma_y_shift;
2024
    int i, j;
2025
    MpegEncContext best_s, backup_s;
2026
    uint8_t bit_buf[2][MAX_MB_BYTES];
2027
    uint8_t bit_buf2[2][MAX_MB_BYTES];
2028
    uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2029
    PutBitContext pb[2], pb2[2], tex_pb[2];
2030
//printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
2031

    
2032
    ff_check_alignment();
2033

    
2034
    for(i=0; i<2; i++){
2035
        init_put_bits(&pb    [i], bit_buf    [i], MAX_MB_BYTES);
2036
        init_put_bits(&pb2   [i], bit_buf2   [i], MAX_MB_BYTES);
2037
        init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2038
    }
2039

    
2040
    s->last_bits= put_bits_count(&s->pb);
2041
    s->mv_bits=0;
2042
    s->misc_bits=0;
2043
    s->i_tex_bits=0;
2044
    s->p_tex_bits=0;
2045
    s->i_count=0;
2046
    s->f_count=0;
2047
    s->b_count=0;
2048
    s->skip_count=0;
2049

    
2050
    for(i=0; i<3; i++){
2051
        /* init last dc values */
2052
        /* note: quant matrix value (8) is implied here */
2053
        s->last_dc[i] = 128 << s->intra_dc_precision;
2054

    
2055
        s->current_picture.error[i] = 0;
2056
    }
2057
    s->mb_skip_run = 0;
2058
    memset(s->last_mv, 0, sizeof(s->last_mv));
2059

    
2060
    s->last_mv_dir = 0;
2061

    
2062
    switch(s->codec_id){
2063
    case CODEC_ID_H263:
2064
    case CODEC_ID_H263P:
2065
    case CODEC_ID_FLV1:
2066
        if (CONFIG_H263_ENCODER)
2067
            s->gob_index = ff_h263_get_gob_height(s);
2068
        break;
2069
    case CODEC_ID_MPEG4:
2070
        if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2071
            ff_mpeg4_init_partitions(s);
2072
        break;
2073
    }
2074

    
2075
    s->resync_mb_x=0;
2076
    s->resync_mb_y=0;
2077
    s->first_slice_line = 1;
2078
    s->ptr_lastgob = s->pb.buf;
2079
    for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2080
//    printf("row %d at %X\n", s->mb_y, (int)s);
2081
        s->mb_x=0;
2082
        s->mb_y= mb_y;
2083

    
2084
        ff_set_qscale(s, s->qscale);
2085
        ff_init_block_index(s);
2086

    
2087
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2088
            int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2089
            int mb_type= s->mb_type[xy];
2090
//            int d;
2091
            int dmin= INT_MAX;
2092
            int dir;
2093

    
2094
            if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < MAX_MB_BYTES){
2095
                av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2096
                return -1;
2097
            }
2098
            if(s->data_partitioning){
2099
                if(   s->pb2   .buf_end - s->pb2   .buf - (put_bits_count(&s->    pb2)>>3) < MAX_MB_BYTES
2100
                   || s->tex_pb.buf_end - s->tex_pb.buf - (put_bits_count(&s->tex_pb )>>3) < MAX_MB_BYTES){
2101
                    av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2102
                    return -1;
2103
                }
2104
            }
2105

    
2106
            s->mb_x = mb_x;
2107
            s->mb_y = mb_y;  // moved into loop, can get changed by H.261
2108
            ff_update_block_index(s);
2109

    
2110
            if(CONFIG_H261_ENCODER && s->codec_id == CODEC_ID_H261){
2111
                ff_h261_reorder_mb_index(s);
2112
                xy= s->mb_y*s->mb_stride + s->mb_x;
2113
                mb_type= s->mb_type[xy];
2114
            }
2115

    
2116
            /* write gob / video packet header  */
2117
            if(s->rtp_mode){
2118
                int current_packet_size, is_gob_start;
2119

    
2120
                current_packet_size= ((put_bits_count(&s->pb)+7)>>3) - (s->ptr_lastgob - s->pb.buf);
2121

    
2122
                is_gob_start= s->avctx->rtp_payload_size && current_packet_size >= s->avctx->rtp_payload_size && mb_y + mb_x>0;
2123

    
2124
                if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2125

    
2126
                switch(s->codec_id){
2127
                case CODEC_ID_H263:
2128
                case CODEC_ID_H263P:
2129
                    if(!s->h263_slice_structured)
2130
                        if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2131
                    break;
2132
                case CODEC_ID_MPEG2VIDEO:
2133
                    if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2134
                case CODEC_ID_MPEG1VIDEO:
2135
                    if(s->mb_skip_run) is_gob_start=0;
2136
                    break;
2137
                }
2138

    
2139
                if(is_gob_start){
2140
                    if(s->start_mb_y != mb_y || mb_x!=0){
2141
                        write_slice_end(s);
2142

    
2143
                        if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
2144
                            ff_mpeg4_init_partitions(s);
2145
                        }
2146
                    }
2147

    
2148
                    assert((put_bits_count(&s->pb)&7) == 0);
2149
                    current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2150

    
2151
                    if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2152
                        int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2153
                        int d= 100 / s->avctx->error_rate;
2154
                        if(r % d == 0){
2155
                            current_packet_size=0;
2156
#ifndef ALT_BITSTREAM_WRITER
2157
                            s->pb.buf_ptr= s->ptr_lastgob;
2158
#endif
2159
                            assert(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2160
                        }
2161
                    }
2162

    
2163
                    if (s->avctx->rtp_callback){
2164
                        int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2165
                        s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2166
                    }
2167

    
2168
                    switch(s->codec_id){
2169
                    case CODEC_ID_MPEG4:
2170
                        if (CONFIG_MPEG4_ENCODER) {
2171
                            ff_mpeg4_encode_video_packet_header(s);
2172
                            ff_mpeg4_clean_buffers(s);
2173
                        }
2174
                    break;
2175
                    case CODEC_ID_MPEG1VIDEO:
2176
                    case CODEC_ID_MPEG2VIDEO:
2177
                        if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2178
                            ff_mpeg1_encode_slice_header(s);
2179
                            ff_mpeg1_clean_buffers(s);
2180
                        }
2181
                    break;
2182
                    case CODEC_ID_H263:
2183
                    case CODEC_ID_H263P:
2184
                        if (CONFIG_H263_ENCODER)
2185
                            h263_encode_gob_header(s, mb_y);
2186
                    break;
2187
                    }
2188

    
2189
                    if(s->flags&CODEC_FLAG_PASS1){
2190
                        int bits= put_bits_count(&s->pb);
2191
                        s->misc_bits+= bits - s->last_bits;
2192
                        s->last_bits= bits;
2193
                    }
2194

    
2195
                    s->ptr_lastgob += current_packet_size;
2196
                    s->first_slice_line=1;
2197
                    s->resync_mb_x=mb_x;
2198
                    s->resync_mb_y=mb_y;
2199
                }
2200
            }
2201

    
2202
            if(  (s->resync_mb_x   == s->mb_x)
2203
               && s->resync_mb_y+1 == s->mb_y){
2204
                s->first_slice_line=0;
2205
            }
2206

    
2207
            s->mb_skipped=0;
2208
            s->dquant=0; //only for QP_RD
2209

    
2210
            if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD
2211
                int next_block=0;
2212
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2213

    
2214
                copy_context_before_encode(&backup_s, s, -1);
2215
                backup_s.pb= s->pb;
2216
                best_s.data_partitioning= s->data_partitioning;
2217
                best_s.partitioned_frame= s->partitioned_frame;
2218
                if(s->data_partitioning){
2219
                    backup_s.pb2= s->pb2;
2220
                    backup_s.tex_pb= s->tex_pb;
2221
                }
2222

    
2223
                if(mb_type&CANDIDATE_MB_TYPE_INTER){
2224
                    s->mv_dir = MV_DIR_FORWARD;
2225
                    s->mv_type = MV_TYPE_16X16;
2226
                    s->mb_intra= 0;
2227
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
2228
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
2229
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2230
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2231
                }
2232
                if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2233
                    s->mv_dir = MV_DIR_FORWARD;
2234
                    s->mv_type = MV_TYPE_FIELD;
2235
                    s->mb_intra= 0;
2236
                    for(i=0; i<2; i++){
2237
                        j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2238
                        s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2239
                        s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2240
                    }
2241
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2242
                                 &dmin, &next_block, 0, 0);
2243
                }
2244
                if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
2245
                    s->mv_dir = MV_DIR_FORWARD;
2246
                    s->mv_type = MV_TYPE_16X16;
2247
                    s->mb_intra= 0;
2248
                    s->mv[0][0][0] = 0;
2249
                    s->mv[0][0][1] = 0;
2250
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2251
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2252
                }
2253
                if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
2254
                    s->mv_dir = MV_DIR_FORWARD;
2255
                    s->mv_type = MV_TYPE_8X8;
2256
                    s->mb_intra= 0;
2257
                    for(i=0; i<4; i++){
2258
                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2259
                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2260
                    }
2261
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2262
                                 &dmin, &next_block, 0, 0);
2263
                }
2264
                if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
2265
                    s->mv_dir = MV_DIR_FORWARD;
2266
                    s->mv_type = MV_TYPE_16X16;
2267
                    s->mb_intra= 0;
2268
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2269
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2270
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2271
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2272
                }
2273
                if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
2274
                    s->mv_dir = MV_DIR_BACKWARD;
2275
                    s->mv_type = MV_TYPE_16X16;
2276
                    s->mb_intra= 0;
2277
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2278
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2279
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2280
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
2281
                }
2282
                if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
2283
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2284
                    s->mv_type = MV_TYPE_16X16;
2285
                    s->mb_intra= 0;
2286
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2287
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2288
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2289
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2290
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2291
                                 &dmin, &next_block, 0, 0);
2292
                }
2293
                if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
2294
                    s->mv_dir = MV_DIR_FORWARD;
2295
                    s->mv_type = MV_TYPE_FIELD;
2296
                    s->mb_intra= 0;
2297
                    for(i=0; i<2; i++){
2298
                        j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2299
                        s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2300
                        s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2301
                    }
2302
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2303
                                 &dmin, &next_block, 0, 0);
2304
                }
2305
                if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
2306
                    s->mv_dir = MV_DIR_BACKWARD;
2307
                    s->mv_type = MV_TYPE_FIELD;
2308
                    s->mb_intra= 0;
2309
                    for(i=0; i<2; i++){
2310
                        j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2311
                        s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2312
                        s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2313
                    }
2314
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2315
                                 &dmin, &next_block, 0, 0);
2316
                }
2317
                if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
2318
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2319
                    s->mv_type = MV_TYPE_FIELD;
2320
                    s->mb_intra= 0;
2321
                    for(dir=0; dir<2; dir++){
2322
                        for(i=0; i<2; i++){
2323
                            j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2324
                            s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2325
                            s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2326
                        }
2327
                    }
2328
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2329
                                 &dmin, &next_block, 0, 0);
2330
                }
2331
                if(mb_type&CANDIDATE_MB_TYPE_INTRA){
2332
                    s->mv_dir = 0;
2333
                    s->mv_type = MV_TYPE_16X16;
2334
                    s->mb_intra= 1;
2335
                    s->mv[0][0][0] = 0;
2336
                    s->mv[0][0][1] = 0;
2337
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2338
                                 &dmin, &next_block, 0, 0);
2339
                    if(s->h263_pred || s->h263_aic){
2340
                        if(best_s.mb_intra)
2341
                            s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
2342
                        else
2343
                            ff_clean_intra_table_entries(s); //old mode?
2344
                    }
2345
                }
2346

    
2347
                if((s->flags & CODEC_FLAG_QP_RD) && dmin < INT_MAX){
2348
                    if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2349
                        const int last_qp= backup_s.qscale;
2350
                        int qpi, qp, dc[6];
2351
                        DCTELEM ac[6][16];
2352
                        const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
2353
                        static const int dquant_tab[4]={-1,1,-2,2};
2354

    
2355
                        assert(backup_s.dquant == 0);
2356

    
2357
                        //FIXME intra
2358
                        s->mv_dir= best_s.mv_dir;
2359
                        s->mv_type = MV_TYPE_16X16;
2360
                        s->mb_intra= best_s.mb_intra;
2361
                        s->mv[0][0][0] = best_s.mv[0][0][0];
2362
                        s->mv[0][0][1] = best_s.mv[0][0][1];
2363
                        s->mv[1][0][0] = best_s.mv[1][0][0];
2364
                        s->mv[1][0][1] = best_s.mv[1][0][1];
2365

    
2366
                        qpi = s->pict_type == FF_B_TYPE ? 2 : 0;
2367
                        for(; qpi<4; qpi++){
2368
                            int dquant= dquant_tab[qpi];
2369
                            qp= last_qp + dquant;
2370
                            if(qp < s->avctx->qmin || qp > s->avctx->qmax)
2371
                                continue;
2372
                            backup_s.dquant= dquant;
2373
                            if(s->mb_intra && s->dc_val[0]){
2374
                                for(i=0; i<6; i++){
2375
                                    dc[i]= s->dc_val[0][ s->block_index[i] ];
2376
                                    memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(DCTELEM)*16);
2377
                                }
2378
                            }
2379

    
2380
                            encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2381
                                         &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
2382
                            if(best_s.qscale != qp){
2383
                                if(s->mb_intra && s->dc_val[0]){
2384
                                    for(i=0; i<6; i++){
2385
                                        s->dc_val[0][ s->block_index[i] ]= dc[i];
2386
                                        memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(DCTELEM)*16);
2387
                                    }
2388
                                }
2389
                            }
2390
                        }
2391
                    }
2392
                }
2393
                if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
2394
                    int mx= s->b_direct_mv_table[xy][0];
2395
                    int my= s->b_direct_mv_table[xy][1];
2396

    
2397
                    backup_s.dquant = 0;
2398
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2399
                    s->mb_intra= 0;
2400
                    ff_mpeg4_set_direct_mv(s, mx, my);
2401
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2402
                                 &dmin, &next_block, mx, my);
2403
                }
2404
                if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
2405
                    backup_s.dquant = 0;
2406
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2407
                    s->mb_intra= 0;
2408
                    ff_mpeg4_set_direct_mv(s, 0, 0);
2409
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2410
                                 &dmin, &next_block, 0, 0);
2411
                }
2412
                if(!best_s.mb_intra && s->flags2&CODEC_FLAG2_SKIP_RD){
2413
                    int coded=0;
2414
                    for(i=0; i<6; i++)
2415
                        coded |= s->block_last_index[i];
2416
                    if(coded){
2417
                        int mx,my;
2418
                        memcpy(s->mv, best_s.mv, sizeof(s->mv));
2419
                        if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
2420
                            mx=my=0; //FIXME find the one we actually used
2421
                            ff_mpeg4_set_direct_mv(s, mx, my);
2422
                        }else if(best_s.mv_dir&MV_DIR_BACKWARD){
2423
                            mx= s->mv[1][0][0];
2424
                            my= s->mv[1][0][1];
2425
                        }else{
2426
                            mx= s->mv[0][0][0];
2427
                            my= s->mv[0][0][1];
2428
                        }
2429

    
2430
                        s->mv_dir= best_s.mv_dir;
2431
                        s->mv_type = best_s.mv_type;
2432
                        s->mb_intra= 0;
2433
/*                        s->mv[0][0][0] = best_s.mv[0][0][0];
2434
                        s->mv[0][0][1] = best_s.mv[0][0][1];
2435
                        s->mv[1][0][0] = best_s.mv[1][0][0];
2436
                        s->mv[1][0][1] = best_s.mv[1][0][1];*/
2437
                        backup_s.dquant= 0;
2438
                        s->skipdct=1;
2439
                        encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
2440
                                        &dmin, &next_block, mx, my);
2441
                        s->skipdct=0;
2442
                    }
2443
                }
2444

    
2445
                s->current_picture.qscale_table[xy]= best_s.qscale;
2446

    
2447
                copy_context_after_encode(s, &best_s, -1);
2448

    
2449
                pb_bits_count= put_bits_count(&s->pb);
2450
                flush_put_bits(&s->pb);
2451
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
2452
                s->pb= backup_s.pb;
2453

    
2454
                if(s->data_partitioning){
2455
                    pb2_bits_count= put_bits_count(&s->pb2);
2456
                    flush_put_bits(&s->pb2);
2457
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
2458
                    s->pb2= backup_s.pb2;
2459

    
2460
                    tex_pb_bits_count= put_bits_count(&s->tex_pb);
2461
                    flush_put_bits(&s->tex_pb);
2462
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
2463
                    s->tex_pb= backup_s.tex_pb;
2464
                }
2465
                s->last_bits= put_bits_count(&s->pb);
2466

    
2467
                if (CONFIG_ANY_H263_ENCODER &&
2468
                    s->out_format == FMT_H263 && s->pict_type!=FF_B_TYPE)
2469
                    ff_h263_update_motion_val(s);
2470

    
2471
                if(next_block==0){ //FIXME 16 vs linesize16
2472
                    s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
2473
                    s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
2474
                    s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
2475
                }
2476

    
2477
                if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2478
                    MPV_decode_mb(s, s->block);
2479
            } else {
2480
                int motion_x = 0, motion_y = 0;
2481
                s->mv_type=MV_TYPE_16X16;
2482
                // only one MB-Type possible
2483

    
2484
                switch(mb_type){
2485
                case CANDIDATE_MB_TYPE_INTRA:
2486
                    s->mv_dir = 0;
2487
                    s->mb_intra= 1;
2488
                    motion_x= s->mv[0][0][0] = 0;
2489
                    motion_y= s->mv[0][0][1] = 0;
2490
                    break;
2491
                case CANDIDATE_MB_TYPE_INTER:
2492
                    s->mv_dir = MV_DIR_FORWARD;
2493
                    s->mb_intra= 0;
2494
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
2495
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
2496
                    break;
2497
                case CANDIDATE_MB_TYPE_INTER_I:
2498
                    s->mv_dir = MV_DIR_FORWARD;
2499
                    s->mv_type = MV_TYPE_FIELD;
2500
                    s->mb_intra= 0;
2501
                    for(i=0; i<2; i++){
2502
                        j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2503
                        s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2504
                        s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2505
                    }
2506
                    break;
2507
                case CANDIDATE_MB_TYPE_INTER4V:
2508
                    s->mv_dir = MV_DIR_FORWARD;
2509
                    s->mv_type = MV_TYPE_8X8;
2510
                    s->mb_intra= 0;
2511
                    for(i=0; i<4; i++){
2512
                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
2513
                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
2514
                    }
2515
                    break;
2516
                case CANDIDATE_MB_TYPE_DIRECT:
2517
                    if (CONFIG_MPEG4_ENCODER) {
2518
                        s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2519
                        s->mb_intra= 0;
2520
                        motion_x=s->b_direct_mv_table[xy][0];
2521
                        motion_y=s->b_direct_mv_table[xy][1];
2522
                        ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
2523
                    }
2524
                    break;
2525
                case CANDIDATE_MB_TYPE_DIRECT0:
2526
                    if (CONFIG_MPEG4_ENCODER) {
2527
                        s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
2528
                        s->mb_intra= 0;
2529
                        ff_mpeg4_set_direct_mv(s, 0, 0);
2530
                    }
2531
                    break;
2532
                case CANDIDATE_MB_TYPE_BIDIR:
2533
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2534
                    s->mb_intra= 0;
2535
                    s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
2536
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
2537
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
2538
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
2539
                    break;
2540
                case CANDIDATE_MB_TYPE_BACKWARD:
2541
                    s->mv_dir = MV_DIR_BACKWARD;
2542
                    s->mb_intra= 0;
2543
                    motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
2544
                    motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
2545
                    break;
2546
                case CANDIDATE_MB_TYPE_FORWARD:
2547
                    s->mv_dir = MV_DIR_FORWARD;
2548
                    s->mb_intra= 0;
2549
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2550
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2551
//                    printf(" %d %d ", motion_x, motion_y);
2552
                    break;
2553
                case CANDIDATE_MB_TYPE_FORWARD_I:
2554
                    s->mv_dir = MV_DIR_FORWARD;
2555
                    s->mv_type = MV_TYPE_FIELD;
2556
                    s->mb_intra= 0;
2557
                    for(i=0; i<2; i++){
2558
                        j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
2559
                        s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
2560
                        s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
2561
                    }
2562
                    break;
2563
                case CANDIDATE_MB_TYPE_BACKWARD_I:
2564
                    s->mv_dir = MV_DIR_BACKWARD;
2565
                    s->mv_type = MV_TYPE_FIELD;
2566
                    s->mb_intra= 0;
2567
                    for(i=0; i<2; i++){
2568
                        j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
2569
                        s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
2570
                        s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
2571
                    }
2572
                    break;
2573
                case CANDIDATE_MB_TYPE_BIDIR_I:
2574
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2575
                    s->mv_type = MV_TYPE_FIELD;
2576
                    s->mb_intra= 0;
2577
                    for(dir=0; dir<2; dir++){
2578
                        for(i=0; i<2; i++){
2579
                            j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
2580
                            s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
2581
                            s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
2582
                        }
2583
                    }
2584
                    break;
2585
                default:
2586
                    av_log(s->avctx, AV_LOG_ERROR, "illegal MB type\n");
2587
                }
2588

    
2589
                encode_mb(s, motion_x, motion_y);
2590

    
2591
                // RAL: Update last macroblock type
2592
                s->last_mv_dir = s->mv_dir;
2593

    
2594
                if (CONFIG_ANY_H263_ENCODER &&
2595
                    s->out_format == FMT_H263 && s->pict_type!=FF_B_TYPE)
2596
                    ff_h263_update_motion_val(s);
2597

    
2598
                MPV_decode_mb(s, s->block);
2599
            }
2600

    
2601
            /* clean the MV table in IPS frames for direct mode in B frames */
2602
            if(s->mb_intra /* && I,P,S_TYPE */){
2603
                s->p_mv_table[xy][0]=0;
2604
                s->p_mv_table[xy][1]=0;
2605
            }
2606

    
2607
            if(s->flags&CODEC_FLAG_PSNR){
2608
                int w= 16;
2609
                int h= 16;
2610

    
2611
                if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2612
                if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2613

    
2614
                s->current_picture.error[0] += sse(
2615
                    s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
2616
                    s->dest[0], w, h, s->linesize);
2617
                s->current_picture.error[1] += sse(
2618
                    s, s->new_picture.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
2619
                    s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
2620
                s->current_picture.error[2] += sse(
2621
                    s, s->new_picture.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
2622
                    s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
2623
            }
2624
            if(s->loop_filter){
2625
                if(CONFIG_ANY_H263_ENCODER && s->out_format == FMT_H263)
2626
                    ff_h263_loop_filter(s);
2627
            }
2628
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
2629
        }
2630
    }
2631

    
2632
    //not beautiful here but we must write it before flushing so it has to be here
2633
    if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == FF_I_TYPE)
2634
        msmpeg4_encode_ext_header(s);
2635

    
2636
    write_slice_end(s);
2637

    
2638
    /* Send the last GOB if RTP */
2639
    if (s->avctx->rtp_callback) {
2640
        int number_mb = (mb_y - s->resync_mb_y)*s->mb_width - s->resync_mb_x;
2641
        pdif = put_bits_ptr(&s->pb) - s->ptr_lastgob;
2642
        /* Call the RTP callback to send the last GOB */
2643
        emms_c();
2644
        s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, pdif, number_mb);
2645
    }
2646

    
2647
    return 0;
2648
}
2649

    
2650
#define MERGE(field) dst->field += src->field; src->field=0
2651
static void merge_context_after_me(MpegEncContext *dst, MpegEncContext *src){
2652
    MERGE(me.scene_change_score);
2653
    MERGE(me.mc_mb_var_sum_temp);
2654
    MERGE(me.mb_var_sum_temp);
2655
}
2656

    
2657
static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src){
2658
    int i;
2659

    
2660
    MERGE(dct_count[0]); //note, the other dct vars are not part of the context
2661
    MERGE(dct_count[1]);
2662
    MERGE(mv_bits);
2663
    MERGE(i_tex_bits);
2664
    MERGE(p_tex_bits);
2665
    MERGE(i_count);
2666
    MERGE(f_count);
2667
    MERGE(b_count);
2668
    MERGE(skip_count);
2669
    MERGE(misc_bits);
2670
    MERGE(error_count);
2671
    MERGE(padding_bug_score);
2672
    MERGE(current_picture.error[0]);
2673
    MERGE(current_picture.error[1]);
2674
    MERGE(current_picture.error[2]);
2675

    
2676
    if(dst->avctx->noise_reduction){
2677
        for(i=0; i<64; i++){
2678
            MERGE(dct_error_sum[0][i]);
2679
            MERGE(dct_error_sum[1][i]);
2680
        }
2681
    }
2682

    
2683
    assert(put_bits_count(&src->pb) % 8 ==0);
2684
    assert(put_bits_count(&dst->pb) % 8 ==0);
2685
    ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
2686
    flush_put_bits(&dst->pb);
2687
}
2688

    
2689
static int estimate_qp(MpegEncContext *s, int dry_run){
2690
    if (s->next_lambda){
2691
        s->current_picture_ptr->quality=
2692
        s->current_picture.quality = s->next_lambda;
2693
        if(!dry_run) s->next_lambda= 0;
2694
    } else if (!s->fixed_qscale) {
2695
        s->current_picture_ptr->quality=
2696
        s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run);
2697
        if (s->current_picture.quality < 0)
2698
            return -1;
2699
    }
2700

    
2701
    if(s->adaptive_quant){
2702
        switch(s->codec_id){
2703
        case CODEC_ID_MPEG4:
2704
            if (CONFIG_MPEG4_ENCODER)
2705
                ff_clean_mpeg4_qscales(s);
2706
            break;
2707
        case CODEC_ID_H263:
2708
        case CODEC_ID_H263P:
2709
        case CODEC_ID_FLV1:
2710
            if (CONFIG_H263_ENCODER)
2711
                ff_clean_h263_qscales(s);
2712
            break;
2713
        default:
2714
            ff_init_qscale_tab(s);
2715
        }
2716

    
2717
        s->lambda= s->lambda_table[0];
2718
        //FIXME broken
2719
    }else
2720
        s->lambda= s->current_picture.quality;
2721
//printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
2722
    update_qscale(s);
2723
    return 0;
2724
}
2725

    
2726
/* must be called before writing the header */
2727
static void set_frame_distances(MpegEncContext * s){
2728
    assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE);
2729
    s->time= s->current_picture_ptr->pts*s->avctx->time_base.num;
2730

    
2731
    if(s->pict_type==FF_B_TYPE){
2732
        s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
2733
        assert(s->pb_time > 0 && s->pb_time < s->pp_time);
2734
    }else{
2735
        s->pp_time= s->time - s->last_non_b_time;
2736
        s->last_non_b_time= s->time;
2737
        assert(s->picture_number==0 || s->pp_time > 0);
2738
    }
2739
}
2740

    
2741
static int encode_picture(MpegEncContext *s, int picture_number)
2742
{
2743
    int i;
2744
    int bits;
2745

    
2746
    s->picture_number = picture_number;
2747

    
2748
    /* Reset the average MB variance */
2749
    s->me.mb_var_sum_temp    =
2750
    s->me.mc_mb_var_sum_temp = 0;
2751

    
2752
    /* we need to initialize some time vars before we can encode b-frames */
2753
    // RAL: Condition added for MPEG1VIDEO
2754
    if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->h263_msmpeg4))
2755
        set_frame_distances(s);
2756
    if(CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
2757
        ff_set_mpeg4_time(s);
2758

    
2759
    s->me.scene_change_score=0;
2760

    
2761
//    s->lambda= s->current_picture_ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
2762

    
2763
    if(s->pict_type==FF_I_TYPE){
2764
        if(s->msmpeg4_version >= 3) s->no_rounding=1;
2765
        else                        s->no_rounding=0;
2766
    }else if(s->pict_type!=FF_B_TYPE){
2767
        if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
2768
            s->no_rounding ^= 1;
2769
    }
2770

    
2771
    if(s->flags & CODEC_FLAG_PASS2){
2772
        if (estimate_qp(s,1) < 0)
2773
            return -1;
2774
        ff_get_2pass_fcode(s);
2775
    }else if(!(s->flags & CODEC_FLAG_QSCALE)){
2776
        if(s->pict_type==FF_B_TYPE)
2777
            s->lambda= s->last_lambda_for[s->pict_type];
2778
        else
2779
            s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
2780
        update_qscale(s);
2781
    }
2782

    
2783
    s->mb_intra=0; //for the rate distortion & bit compare functions
2784
    for(i=1; i<s->avctx->thread_count; i++){
2785
        ff_update_duplicate_context(s->thread_context[i], s);
2786
    }
2787

    
2788
    if(ff_init_me(s)<0)
2789
        return -1;
2790

    
2791
    /* Estimate motion for every MB */
2792
    if(s->pict_type != FF_I_TYPE){
2793
        s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
2794
        s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
2795
        if(s->pict_type != FF_B_TYPE && s->avctx->me_threshold==0){
2796
            if((s->avctx->pre_me && s->last_non_b_pict_type==FF_I_TYPE) || s->avctx->pre_me==2){
2797
                s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
2798
            }
2799
        }
2800

    
2801
        s->avctx->execute(s->avctx, estimate_motion_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
2802
    }else /* if(s->pict_type == FF_I_TYPE) */{
2803
        /* I-Frame */
2804
        for(i=0; i<s->mb_stride*s->mb_height; i++)
2805
            s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
2806

    
2807
        if(!s->fixed_qscale){
2808
            /* finding spatial complexity for I-frame rate control */
2809
            s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
2810
        }
2811
    }
2812
    for(i=1; i<s->avctx->thread_count; i++){
2813
        merge_context_after_me(s, s->thread_context[i]);
2814
    }
2815
    s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
2816
    s->current_picture.   mb_var_sum= s->current_picture_ptr->   mb_var_sum= s->me.   mb_var_sum_temp;
2817
    emms_c();
2818

    
2819
    if(s->me.scene_change_score > s->avctx->scenechange_threshold && s->pict_type == FF_P_TYPE){
2820
        s->pict_type= FF_I_TYPE;
2821
        for(i=0; i<s->mb_stride*s->mb_height; i++)
2822
            s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
2823
//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
2824
    }
2825

    
2826
    if(!s->umvplus){
2827
        if(s->pict_type==FF_P_TYPE || s->pict_type==FF_S_TYPE) {
2828
            s->f_code= ff_get_best_fcode(s, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
2829

    
2830
            if(s->flags & CODEC_FLAG_INTERLACED_ME){
2831
                int a,b;
2832
                a= ff_get_best_fcode(s, s->p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
2833
                b= ff_get_best_fcode(s, s->p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
2834
                s->f_code= FFMAX3(s->f_code, a, b);
2835
            }
2836

    
2837
            ff_fix_long_p_mvs(s);
2838
            ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, 0);
2839
            if(s->flags & CODEC_FLAG_INTERLACED_ME){
2840
                int j;
2841
                for(i=0; i<2; i++){
2842
                    for(j=0; j<2; j++)
2843
                        ff_fix_long_mvs(s, s->p_field_select_table[i], j,
2844
                                        s->p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, 0);
2845
                }
2846
            }
2847
        }
2848

    
2849
        if(s->pict_type==FF_B_TYPE){
2850
            int a, b;
2851

    
2852
            a = ff_get_best_fcode(s, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
2853
            b = ff_get_best_fcode(s, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
2854
            s->f_code = FFMAX(a, b);
2855

    
2856
            a = ff_get_best_fcode(s, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
2857
            b = ff_get_best_fcode(s, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
2858
            s->b_code = FFMAX(a, b);
2859

    
2860
            ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
2861
            ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
2862
            ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
2863
            ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
2864
            if(s->flags & CODEC_FLAG_INTERLACED_ME){
2865
                int dir, j;
2866
                for(dir=0; dir<2; dir++){
2867
                    for(i=0; i<2; i++){
2868
                        for(j=0; j<2; j++){
2869
                            int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
2870
                                          : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
2871
                            ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
2872
                                            s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
2873
                        }
2874
                    }
2875
                }
2876
            }
2877
        }
2878
    }
2879

    
2880
    if (estimate_qp(s, 0) < 0)
2881
        return -1;
2882

    
2883
    if(s->qscale < 3 && s->max_qcoeff<=128 && s->pict_type==FF_I_TYPE && !(s->flags & CODEC_FLAG_QSCALE))
2884
        s->qscale= 3; //reduce clipping problems
2885

    
2886
    if (s->out_format == FMT_MJPEG) {
2887
        /* for mjpeg, we do include qscale in the matrix */
2888
        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2889
        for(i=1;i<64;i++){
2890
            int j= s->dsp.idct_permutation[i];
2891

    
2892
            s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
2893
        }
2894
        ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
2895
                       s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
2896
        s->qscale= 8;
2897
    }
2898

    
2899
    //FIXME var duplication
2900
    s->current_picture_ptr->key_frame=
2901
    s->current_picture.key_frame= s->pict_type == FF_I_TYPE; //FIXME pic_ptr
2902
    s->current_picture_ptr->pict_type=
2903
    s->current_picture.pict_type= s->pict_type;
2904

    
2905
    if(s->current_picture.key_frame)
2906
        s->picture_in_gop_number=0;
2907

    
2908
    s->last_bits= put_bits_count(&s->pb);
2909
    switch(s->out_format) {
2910
    case FMT_MJPEG:
2911
        if (CONFIG_MJPEG_ENCODER)
2912
            ff_mjpeg_encode_picture_header(s);
2913
        break;
2914
    case FMT_H261:
2915
        if (CONFIG_H261_ENCODER)
2916
            ff_h261_encode_picture_header(s, picture_number);
2917
        break;
2918
    case FMT_H263:
2919
        if (CONFIG_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
2920
            ff_wmv2_encode_picture_header(s, picture_number);
2921
        else if (CONFIG_MSMPEG4_ENCODER && s->h263_msmpeg4)
2922
            msmpeg4_encode_picture_header(s, picture_number);
2923
        else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
2924
            mpeg4_encode_picture_header(s, picture_number);
2925
        else if (CONFIG_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
2926
            rv10_encode_picture_header(s, picture_number);
2927
        else if (CONFIG_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
2928
            rv20_encode_picture_header(s, picture_number);
2929
        else if (CONFIG_FLV_ENCODER && s->codec_id == CODEC_ID_FLV1)
2930
            ff_flv_encode_picture_header(s, picture_number);
2931
        else if (CONFIG_ANY_H263_ENCODER)
2932
            h263_encode_picture_header(s, picture_number);
2933
        break;
2934
    case FMT_MPEG1:
2935
        if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2936
            mpeg1_encode_picture_header(s, picture_number);
2937
        break;
2938
    case FMT_H264:
2939
        break;
2940
    default:
2941
        assert(0);
2942
    }
2943
    bits= put_bits_count(&s->pb);
2944
    s->header_bits= bits - s->last_bits;
2945

    
2946
    for(i=1; i<s->avctx->thread_count; i++){
2947
        update_duplicate_context_after_me(s->thread_context[i], s);
2948
    }
2949
    s->avctx->execute(s->avctx, encode_thread, &s->thread_context[0], NULL, s->avctx->thread_count, sizeof(void*));
2950
    for(i=1; i<s->avctx->thread_count; i++){
2951
        merge_context_after_encode(s, s->thread_context[i]);
2952
    }
2953
    emms_c();
2954
    return 0;
2955
}
2956

    
2957
void  denoise_dct_c(MpegEncContext *s, DCTELEM *block){
2958
    const int intra= s->mb_intra;
2959
    int i;
2960

    
2961
    s->dct_count[intra]++;
2962

    
2963
    for(i=0; i<64; i++){
2964
        int level= block[i];
2965

    
2966
        if(level){
2967
            if(level>0){
2968
                s->dct_error_sum[intra][i] += level;
2969
                level -= s->dct_offset[intra][i];
2970
                if(level<0) level=0;
2971
            }else{
2972
                s->dct_error_sum[intra][i] -= level;
2973
                level += s->dct_offset[intra][i];
2974
                if(level>0) level=0;
2975
            }
2976
            block[i]= level;
2977
        }
2978
    }
2979
}
2980

    
2981
int dct_quantize_trellis_c(MpegEncContext *s,
2982
                        DCTELEM *block, int n,
2983
                        int qscale, int *overflow){
2984
    const int *qmat;
2985
    const uint8_t *scantable= s->intra_scantable.scantable;
2986
    const uint8_t *perm_scantable= s->intra_scantable.permutated;
2987
    int max=0;
2988
    unsigned int threshold1, threshold2;
2989
    int bias=0;
2990
    int run_tab[65];
2991
    int level_tab[65];
2992
    int score_tab[65];
2993
    int survivor[65];
2994
    int survivor_count;
2995
    int last_run=0;
2996
    int last_level=0;
2997
    int last_score= 0;
2998
    int last_i;
2999
    int coeff[2][64];
3000
    int coeff_count[64];
3001
    int qmul, qadd, start_i, last_non_zero, i, dc;
3002
    const int esc_length= s->ac_esc_length;
3003
    uint8_t * length;
3004
    uint8_t * last_length;
3005
    const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3006

    
3007
    s->dsp.fdct (block);
3008

    
3009
    if(s->dct_error_sum)
3010
        s->denoise_dct(s, block);
3011
    qmul= qscale*16;
3012
    qadd= ((qscale-1)|1)*8;
3013

    
3014
    if (s->mb_intra) {
3015
        int q;
3016
        if (!s->h263_aic) {
3017
            if (n < 4)
3018
                q = s->y_dc_scale;
3019
            else
3020
                q = s->c_dc_scale;
3021
            q = q << 3;
3022
        } else{
3023
            /* For AIC we skip quant/dequant of INTRADC */
3024
            q = 1 << 3;
3025
            qadd=0;
3026
        }
3027

    
3028
        /* note: block[0] is assumed to be positive */
3029
        block[0] = (block[0] + (q >> 1)) / q;
3030
        start_i = 1;
3031
        last_non_zero = 0;
3032
        qmat = s->q_intra_matrix[qscale];
3033
        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3034
            bias= 1<<(QMAT_SHIFT-1);
3035
        length     = s->intra_ac_vlc_length;
3036
        last_length= s->intra_ac_vlc_last_length;
3037
    } else {
3038
        start_i = 0;
3039
        last_non_zero = -1;
3040
        qmat = s->q_inter_matrix[qscale];
3041
        length     = s->inter_ac_vlc_length;
3042
        last_length= s->inter_ac_vlc_last_length;
3043
    }
3044
    last_i= start_i;
3045

    
3046
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
3047
    threshold2= (threshold1<<1);
3048

    
3049
    for(i=63; i>=start_i; i--) {
3050
        const int j = scantable[i];
3051
        int level = block[j] * qmat[j];
3052

    
3053
        if(((unsigned)(level+threshold1))>threshold2){
3054
            last_non_zero = i;
3055
            break;
3056
        }
3057
    }
3058

    
3059
    for(i=start_i; i<=last_non_zero; i++) {
3060
        const int j = scantable[i];
3061
        int level = block[j] * qmat[j];
3062

    
3063
//        if(   bias+level >= (1<<(QMAT_SHIFT - 3))
3064
//           || bias-level >= (1<<(QMAT_SHIFT - 3))){
3065
        if(((unsigned)(level+threshold1))>threshold2){
3066
            if(level>0){
3067
                level= (bias + level)>>QMAT_SHIFT;
3068
                coeff[0][i]= level;
3069
                coeff[1][i]= level-1;
3070
//                coeff[2][k]= level-2;
3071
            }else{
3072
                level= (bias - level)>>QMAT_SHIFT;
3073
                coeff[0][i]= -level;
3074
                coeff[1][i]= -level+1;
3075
//                coeff[2][k]= -level+2;
3076
            }
3077
            coeff_count[i]= FFMIN(level, 2);
3078
            assert(coeff_count[i]);
3079
            max |=level;
3080
        }else{
3081
            coeff[0][i]= (level>>31)|1;
3082
            coeff_count[i]= 1;
3083
        }
3084
    }
3085

    
3086
    *overflow= s->max_qcoeff < max; //overflow might have happened
3087

    
3088
    if(last_non_zero < start_i){
3089
        memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3090
        return last_non_zero;
3091
    }
3092

    
3093
    score_tab[start_i]= 0;
3094
    survivor[0]= start_i;
3095
    survivor_count= 1;
3096

    
3097
    for(i=start_i; i<=last_non_zero; i++){
3098
        int level_index, j, zero_distortion;
3099
        int dct_coeff= FFABS(block[ scantable[i] ]);
3100
        int best_score=256*256*256*120;
3101

    
3102
        if (   s->dsp.fdct == fdct_ifast
3103
#ifndef FAAN_POSTSCALE
3104
            || s->dsp.fdct == ff_faandct
3105
#endif
3106
           )
3107
            dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3108
        zero_distortion= dct_coeff*dct_coeff;
3109

    
3110
        for(level_index=0; level_index < coeff_count[i]; level_index++){
3111
            int distortion;
3112
            int level= coeff[level_index][i];
3113
            const int alevel= FFABS(level);
3114
            int unquant_coeff;
3115

    
3116
            assert(level);
3117

    
3118
            if(s->out_format == FMT_H263){
3119
                unquant_coeff= alevel*qmul + qadd;
3120
            }else{ //MPEG1
3121
                j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
3122
                if(s->mb_intra){
3123
                        unquant_coeff = (int)(  alevel  * qscale * s->intra_matrix[j]) >> 3;
3124
                        unquant_coeff =   (unquant_coeff - 1) | 1;
3125
                }else{
3126
                        unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4;
3127
                        unquant_coeff =   (unquant_coeff - 1) | 1;
3128
                }
3129
                unquant_coeff<<= 3;
3130
            }
3131

    
3132
            distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3133
            level+=64;
3134
            if((level&(~127)) == 0){
3135
                for(j=survivor_count-1; j>=0; j--){
3136
                    int run= i - survivor[j];
3137
                    int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3138
                    score += score_tab[i-run];
3139

    
3140
                    if(score < best_score){
3141
                        best_score= score;
3142
                        run_tab[i+1]= run;
3143
                        level_tab[i+1]= level-64;
3144
                    }
3145
                }
3146

    
3147
                if(s->out_format == FMT_H263){
3148
                    for(j=survivor_count-1; j>=0; j--){
3149
                        int run= i - survivor[j];
3150
                        int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3151
                        score += score_tab[i-run];
3152
                        if(score < last_score){
3153
                            last_score= score;
3154
                            last_run= run;
3155
                            last_level= level-64;
3156
                            last_i= i+1;
3157
                        }
3158
                    }
3159
                }
3160
            }else{
3161
                distortion += esc_length*lambda;
3162
                for(j=survivor_count-1; j>=0; j--){
3163
                    int run= i - survivor[j];
3164
                    int score= distortion + score_tab[i-run];
3165

    
3166
                    if(score < best_score){
3167
                        best_score= score;
3168
                        run_tab[i+1]= run;
3169
                        level_tab[i+1]= level-64;
3170
                    }
3171
                }
3172

    
3173
                if(s->out_format == FMT_H263){
3174
                  for(j=survivor_count-1; j>=0; j--){
3175
                        int run= i - survivor[j];
3176
                        int score= distortion + score_tab[i-run];
3177
                        if(score < last_score){
3178
                            last_score= score;
3179
                            last_run= run;
3180
                            last_level= level-64;
3181
                            last_i= i+1;
3182
                        }
3183
                    }
3184
                }
3185
            }
3186
        }
3187

    
3188
        score_tab[i+1]= best_score;
3189

    
3190
        //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level
3191
        if(last_non_zero <= 27){
3192
            for(; survivor_count; survivor_count--){
3193
                if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3194
                    break;
3195
            }
3196
        }else{
3197
            for(; survivor_count; survivor_count--){
3198
                if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3199
                    break;
3200
            }
3201
        }
3202

    
3203
        survivor[ survivor_count++ ]= i+1;
3204
    }
3205

    
3206
    if(s->out_format != FMT_H263){
3207
        last_score= 256*256*256*120;
3208
        for(i= survivor[0]; i<=last_non_zero + 1; i++){
3209
            int score= score_tab[i];
3210
            if(i) score += lambda*2; //FIXME exacter?
3211

    
3212
            if(score < last_score){
3213
                last_score= score;
3214
                last_i= i;
3215
                last_level= level_tab[i];
3216
                last_run= run_tab[i];
3217
            }
3218
        }
3219
    }
3220

    
3221
    s->coded_score[n] = last_score;
3222

    
3223
    dc= FFABS(block[0]);
3224
    last_non_zero= last_i - 1;
3225
    memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM));
3226

    
3227
    if(last_non_zero < start_i)
3228
        return last_non_zero;
3229

    
3230
    if(last_non_zero == 0 && start_i == 0){
3231
        int best_level= 0;
3232
        int best_score= dc * dc;
3233

    
3234
        for(i=0; i<coeff_count[0]; i++){
3235
            int level= coeff[i][0];
3236
            int alevel= FFABS(level);
3237
            int unquant_coeff, score, distortion;
3238

    
3239
            if(s->out_format == FMT_H263){
3240
                    unquant_coeff= (alevel*qmul + qadd)>>3;
3241
            }else{ //MPEG1
3242
                    unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;
3243
                    unquant_coeff =   (unquant_coeff - 1) | 1;
3244
            }
3245
            unquant_coeff = (unquant_coeff + 4) >> 3;
3246
            unquant_coeff<<= 3 + 3;
3247

    
3248
            distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3249
            level+=64;
3250
            if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
3251
            else                    score= distortion + esc_length*lambda;
3252

    
3253
            if(score < best_score){
3254
                best_score= score;
3255
                best_level= level - 64;
3256
            }
3257
        }
3258
        block[0]= best_level;
3259
        s->coded_score[n] = best_score - dc*dc;
3260
        if(best_level == 0) return -1;
3261
        else                return last_non_zero;
3262
    }
3263

    
3264
    i= last_i;
3265
    assert(last_level);
3266

    
3267
    block[ perm_scantable[last_non_zero] ]= last_level;
3268
    i -= last_run + 1;
3269

    
3270
    for(; i>start_i; i -= run_tab[i] + 1){
3271
        block[ perm_scantable[i-1] ]= level_tab[i];
3272
    }
3273

    
3274
    return last_non_zero;
3275
}
3276

    
3277
//#define REFINE_STATS 1
3278
static int16_t basis[64][64];
3279

    
3280
static void build_basis(uint8_t *perm){
3281
    int i, j, x, y;
3282
    emms_c();
3283
    for(i=0; i<8; i++){
3284
        for(j=0; j<8; j++){
3285
            for(y=0; y<8; y++){
3286
                for(x=0; x<8; x++){
3287
                    double s= 0.25*(1<<BASIS_SHIFT);
3288
                    int index= 8*i + j;
3289
                    int perm_index= perm[index];
3290
                    if(i==0) s*= sqrt(0.5);
3291
                    if(j==0) s*= sqrt(0.5);
3292
                    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)));
3293
                }
3294
            }
3295
        }
3296
    }
3297
}
3298

    
3299
static int dct_quantize_refine(MpegEncContext *s, //FIXME breaks denoise?
3300
                        DCTELEM *block, int16_t *weight, DCTELEM *orig,
3301
                        int n, int qscale){
3302
    int16_t rem[64];
3303
    DECLARE_ALIGNED_16(DCTELEM, d1[64]);
3304
    const uint8_t *scantable= s->intra_scantable.scantable;
3305
    const uint8_t *perm_scantable= s->intra_scantable.permutated;
3306
//    unsigned int threshold1, threshold2;
3307
//    int bias=0;
3308
    int run_tab[65];
3309
    int prev_run=0;
3310
    int prev_level=0;
3311
    int qmul, qadd, start_i, last_non_zero, i, dc;
3312
    uint8_t * length;
3313
    uint8_t * last_length;
3314
    int lambda;
3315
    int rle_index, run, q = 1, sum; //q is only used when s->mb_intra is true
3316
#ifdef REFINE_STATS
3317
static int count=0;
3318
static int after_last=0;
3319
static int to_zero=0;
3320
static int from_zero=0;
3321
static int raise=0;
3322
static int lower=0;
3323
static int messed_sign=0;
3324
#endif
3325

    
3326
    if(basis[0][0] == 0)
3327
        build_basis(s->dsp.idct_permutation);
3328

    
3329
    qmul= qscale*2;
3330
    qadd= (qscale-1)|1;
3331
    if (s->mb_intra) {
3332
        if (!s->h263_aic) {
3333
            if (n < 4)
3334
                q = s->y_dc_scale;
3335
            else
3336
                q = s->c_dc_scale;
3337
        } else{
3338
            /* For AIC we skip quant/dequant of INTRADC */
3339
            q = 1;
3340
            qadd=0;
3341
        }
3342
        q <<= RECON_SHIFT-3;
3343
        /* note: block[0] is assumed to be positive */
3344
        dc= block[0]*q;
3345
//        block[0] = (block[0] + (q >> 1)) / q;
3346
        start_i = 1;
3347
//        if(s->mpeg_quant || s->out_format == FMT_MPEG1)
3348
//            bias= 1<<(QMAT_SHIFT-1);
3349
        length     = s->intra_ac_vlc_length;
3350
        last_length= s->intra_ac_vlc_last_length;
3351
    } else {
3352
        dc= 0;
3353
        start_i = 0;
3354
        length     = s->inter_ac_vlc_length;
3355
        last_length= s->inter_ac_vlc_last_length;
3356
    }
3357
    last_non_zero = s->block_last_index[n];
3358

    
3359
#ifdef REFINE_STATS
3360
{START_TIMER
3361
#endif
3362
    dc += (1<<(RECON_SHIFT-1));
3363
    for(i=0; i<64; i++){
3364
        rem[i]= dc - (orig[i]<<RECON_SHIFT); //FIXME  use orig dirrectly instead of copying to rem[]
3365
    }
3366
#ifdef REFINE_STATS
3367
STOP_TIMER("memset rem[]")}
3368
#endif
3369
    sum=0;
3370
    for(i=0; i<64; i++){
3371
        int one= 36;
3372
        int qns=4;
3373
        int w;
3374

    
3375
        w= FFABS(weight[i]) + qns*one;
3376
        w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
3377

    
3378
        weight[i] = w;
3379
//        w=weight[i] = (63*qns + (w/2)) / w;
3380

    
3381
        assert(w>0);
3382
        assert(w<(1<<6));
3383
        sum += w*w;
3384
    }
3385
    lambda= sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
3386
#ifdef REFINE_STATS
3387
{START_TIMER
3388
#endif
3389
    run=0;
3390
    rle_index=0;
3391
    for(i=start_i; i<=last_non_zero; i++){
3392
        int j= perm_scantable[i];
3393
        const int level= block[j];
3394
        int coeff;
3395

    
3396
        if(level){
3397
            if(level<0) coeff= qmul*level - qadd;
3398
            else        coeff= qmul*level + qadd;
3399
            run_tab[rle_index++]=run;
3400
            run=0;
3401

    
3402
            s->dsp.add_8x8basis(rem, basis[j], coeff);
3403
        }else{
3404
            run++;
3405
        }
3406
    }
3407
#ifdef REFINE_STATS
3408
if(last_non_zero>0){
3409
STOP_TIMER("init rem[]")
3410
}
3411
}
3412

    
3413
{START_TIMER
3414
#endif
3415
    for(;;){
3416
        int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
3417
        int best_coeff=0;
3418
        int best_change=0;
3419
        int run2, best_unquant_change=0, analyze_gradient;
3420
#ifdef REFINE_STATS
3421
{START_TIMER
3422
#endif
3423
        analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
3424

    
3425
        if(analyze_gradient){
3426
#ifdef REFINE_STATS
3427
{START_TIMER
3428
#endif
3429
            for(i=0; i<64; i++){
3430
                int w= weight[i];
3431

    
3432
                d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
3433
            }
3434
#ifdef REFINE_STATS
3435
STOP_TIMER("rem*w*w")}
3436
{START_TIMER
3437
#endif
3438
            s->dsp.fdct(d1);
3439
#ifdef REFINE_STATS
3440
STOP_TIMER("dct")}
3441
#endif
3442
        }
3443

    
3444
        if(start_i){
3445
            const int level= block[0];
3446
            int change, old_coeff;
3447

    
3448
            assert(s->mb_intra);
3449

    
3450
            old_coeff= q*level;
3451

    
3452
            for(change=-1; change<=1; change+=2){
3453
                int new_level= level + change;
3454
                int score, new_coeff;
3455

    
3456
                new_coeff= q*new_level;
3457
                if(new_coeff >= 2048 || new_coeff < 0)
3458
                    continue;
3459

    
3460
                score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3461
                if(score<best_score){
3462
                    best_score= score;
3463
                    best_coeff= 0;
3464
                    best_change= change;
3465
                    best_unquant_change= new_coeff - old_coeff;
3466
                }
3467
            }
3468
        }
3469

    
3470
        run=0;
3471
        rle_index=0;
3472
        run2= run_tab[rle_index++];
3473
        prev_level=0;
3474
        prev_run=0;
3475

    
3476
        for(i=start_i; i<64; i++){
3477
            int j= perm_scantable[i];
3478
            const int level= block[j];
3479
            int change, old_coeff;
3480

    
3481
            if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
3482
                break;
3483

    
3484
            if(level){
3485
                if(level<0) old_coeff= qmul*level - qadd;
3486
                else        old_coeff= qmul*level + qadd;
3487
                run2= run_tab[rle_index++]; //FIXME ! maybe after last
3488
            }else{
3489
                old_coeff=0;
3490
                run2--;
3491
                assert(run2>=0 || i >= last_non_zero );
3492
            }
3493

    
3494
            for(change=-1; change<=1; change+=2){
3495
                int new_level= level + change;
3496
                int score, new_coeff, unquant_change;
3497

    
3498
                score=0;
3499
                if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
3500
                   continue;
3501

    
3502
                if(new_level){
3503
                    if(new_level<0) new_coeff= qmul*new_level - qadd;
3504
                    else            new_coeff= qmul*new_level + qadd;
3505
                    if(new_coeff >= 2048 || new_coeff <= -2048)
3506
                        continue;
3507
                    //FIXME check for overflow
3508

    
3509
                    if(level){
3510
                        if(level < 63 && level > -63){
3511
                            if(i < last_non_zero)
3512
                                score +=   length[UNI_AC_ENC_INDEX(run, new_level+64)]
3513
                                         - length[UNI_AC_ENC_INDEX(run, level+64)];
3514
                            else
3515
                                score +=   last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
3516
                                         - last_length[UNI_AC_ENC_INDEX(run, level+64)];
3517
                        }
3518
                    }else{
3519
                        assert(FFABS(new_level)==1);
3520

    
3521
                        if(analyze_gradient){
3522
                            int g= d1[ scantable[i] ];
3523
                            if(g && (g^new_level) >= 0)
3524
                                continue;
3525
                        }
3526

    
3527
                        if(i < last_non_zero){
3528
                            int next_i= i + run2 + 1;
3529
                            int next_level= block[ perm_scantable[next_i] ] + 64;
3530

    
3531
                            if(next_level&(~127))
3532
                                next_level= 0;
3533

    
3534
                            if(next_i < last_non_zero)
3535
                                score +=   length[UNI_AC_ENC_INDEX(run, 65)]
3536
                                         + length[UNI_AC_ENC_INDEX(run2, next_level)]
3537
                                         - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3538
                            else
3539
                                score +=  length[UNI_AC_ENC_INDEX(run, 65)]
3540
                                        + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
3541
                                        - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
3542
                        }else{
3543
                            score += last_length[UNI_AC_ENC_INDEX(run, 65)];
3544
                            if(prev_level){
3545
                                score +=  length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
3546
                                        - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
3547
                            }
3548
                        }
3549
                    }
3550
                }else{
3551
                    new_coeff=0;
3552
                    assert(FFABS(level)==1);
3553

    
3554
                    if(i < last_non_zero){
3555
                        int next_i= i + run2 + 1;
3556
                        int next_level= block[ perm_scantable[next_i] ] + 64;
3557

    
3558
                        if(next_level&(~127))
3559
                            next_level= 0;
3560

    
3561
                        if(next_i < last_non_zero)
3562
                            score +=   length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
3563
                                     - length[UNI_AC_ENC_INDEX(run2, next_level)]
3564
                                     - length[UNI_AC_ENC_INDEX(run, 65)];
3565
                        else
3566
                            score +=   last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
3567
                                     - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
3568
                                     - length[UNI_AC_ENC_INDEX(run, 65)];
3569
                    }else{
3570
                        score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
3571
                        if(prev_level){
3572
                            score +=  last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
3573
                                    - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
3574
                        }
3575
                    }
3576
                }
3577

    
3578
                score *= lambda;
3579

    
3580
                unquant_change= new_coeff - old_coeff;
3581
                assert((score < 100*lambda && score > -100*lambda) || lambda==0);
3582

    
3583
                score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
3584
                if(score<best_score){
3585
                    best_score= score;
3586
                    best_coeff= i;
3587
                    best_change= change;
3588
                    best_unquant_change= unquant_change;
3589
                }
3590
            }
3591
            if(level){
3592
                prev_level= level + 64;
3593
                if(prev_level&(~127))
3594
                    prev_level= 0;
3595
                prev_run= run;
3596
                run=0;
3597
            }else{
3598
                run++;
3599
            }
3600
        }
3601
#ifdef REFINE_STATS
3602
STOP_TIMER("iterative step")}
3603
#endif
3604

    
3605
        if(best_change){
3606
            int j= perm_scantable[ best_coeff ];
3607

    
3608
            block[j] += best_change;
3609

    
3610
            if(best_coeff > last_non_zero){
3611
                last_non_zero= best_coeff;
3612
                assert(block[j]);
3613
#ifdef REFINE_STATS
3614
after_last++;
3615
#endif
3616
            }else{
3617
#ifdef REFINE_STATS
3618
if(block[j]){
3619
    if(block[j] - best_change){
3620
        if(FFABS(block[j]) > FFABS(block[j] - best_change)){
3621
            raise++;
3622
        }else{
3623
            lower++;
3624
        }
3625
    }else{
3626
        from_zero++;
3627
    }
3628
}else{
3629
    to_zero++;
3630
}
3631
#endif
3632
                for(; last_non_zero>=start_i; last_non_zero--){
3633
                    if(block[perm_scantable[last_non_zero]])
3634
                        break;
3635
                }
3636
            }
3637
#ifdef REFINE_STATS
3638
count++;
3639
if(256*256*256*64 % count == 0){
3640
    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);
3641
}
3642
#endif
3643
            run=0;
3644
            rle_index=0;
3645
            for(i=start_i; i<=last_non_zero; i++){
3646
                int j= perm_scantable[i];
3647
                const int level= block[j];
3648

    
3649
                 if(level){
3650
                     run_tab[rle_index++]=run;
3651
                     run=0;
3652
                 }else{
3653
                     run++;
3654
                 }
3655
            }
3656

    
3657
            s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
3658
        }else{
3659
            break;
3660
        }
3661
    }
3662
#ifdef REFINE_STATS
3663
if(last_non_zero>0){
3664
STOP_TIMER("iterative search")
3665
}
3666
}
3667
#endif
3668

    
3669
    return last_non_zero;
3670
}
3671

    
3672
int dct_quantize_c(MpegEncContext *s,
3673
                        DCTELEM *block, int n,
3674
                        int qscale, int *overflow)
3675
{
3676
    int i, j, level, last_non_zero, q, start_i;
3677
    const int *qmat;
3678
    const uint8_t *scantable= s->intra_scantable.scantable;
3679
    int bias;
3680
    int max=0;
3681
    unsigned int threshold1, threshold2;
3682

    
3683
    s->dsp.fdct (block);
3684

    
3685
    if(s->dct_error_sum)
3686
        s->denoise_dct(s, block);
3687

    
3688
    if (s->mb_intra) {
3689
        if (!s->h263_aic) {
3690
            if (n < 4)
3691
                q = s->y_dc_scale;
3692
            else
3693
                q = s->c_dc_scale;
3694
            q = q << 3;
3695
        } else
3696
            /* For AIC we skip quant/dequant of INTRADC */
3697
            q = 1 << 3;
3698

    
3699
        /* note: block[0] is assumed to be positive */
3700
        block[0] = (block[0] + (q >> 1)) / q;
3701
        start_i = 1;
3702
        last_non_zero = 0;
3703
        qmat = s->q_intra_matrix[qscale];
3704
        bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3705
    } else {
3706
        start_i = 0;
3707
        last_non_zero = -1;
3708
        qmat = s->q_inter_matrix[qscale];
3709
        bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT);
3710
    }
3711
    threshold1= (1<<QMAT_SHIFT) - bias - 1;
3712
    threshold2= (threshold1<<1);
3713
    for(i=63;i>=start_i;i--) {
3714
        j = scantable[i];
3715
        level = block[j] * qmat[j];
3716

    
3717
        if(((unsigned)(level+threshold1))>threshold2){
3718
            last_non_zero = i;
3719
            break;
3720
        }else{
3721
            block[j]=0;
3722
        }
3723
    }
3724
    for(i=start_i; i<=last_non_zero; i++) {
3725
        j = scantable[i];
3726
        level = block[j] * qmat[j];
3727

    
3728
//        if(   bias+level >= (1<<QMAT_SHIFT)
3729
//           || bias-level >= (1<<QMAT_SHIFT)){
3730
        if(((unsigned)(level+threshold1))>threshold2){
3731
            if(level>0){
3732
                level= (bias + level)>>QMAT_SHIFT;
3733
                block[j]= level;
3734
            }else{
3735
                level= (bias - level)>>QMAT_SHIFT;
3736
                block[j]= -level;
3737
            }
3738
            max |=level;
3739
        }else{
3740
            block[j]=0;
3741
        }
3742
    }
3743
    *overflow= s->max_qcoeff < max; //overflow might have happened
3744

    
3745
    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
3746
    if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
3747
        ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
3748

    
3749
    return last_non_zero;
3750
}
3751

    
3752
AVCodec h263_encoder = {
3753
    "h263",
3754
    CODEC_TYPE_VIDEO,
3755
    CODEC_ID_H263,
3756
    sizeof(MpegEncContext),
3757
    MPV_encode_init,
3758
    MPV_encode_picture,
3759
    MPV_encode_end,
3760
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3761
    .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
3762
};
3763

    
3764
AVCodec h263p_encoder = {
3765
    "h263p",
3766
    CODEC_TYPE_VIDEO,
3767
    CODEC_ID_H263P,
3768
    sizeof(MpegEncContext),
3769
    MPV_encode_init,
3770
    MPV_encode_picture,
3771
    MPV_encode_end,
3772
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3773
    .long_name= NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
3774
};
3775

    
3776
AVCodec flv_encoder = {
3777
    "flv",
3778
    CODEC_TYPE_VIDEO,
3779
    CODEC_ID_FLV1,
3780
    sizeof(MpegEncContext),
3781
    MPV_encode_init,
3782
    MPV_encode_picture,
3783
    MPV_encode_end,
3784
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3785
    .long_name= NULL_IF_CONFIG_SMALL("Flash Video (FLV) / Sorenson Spark / Sorenson H.263"),
3786
};
3787

    
3788
AVCodec mpeg4_encoder = {
3789
    "mpeg4",
3790
    CODEC_TYPE_VIDEO,
3791
    CODEC_ID_MPEG4,
3792
    sizeof(MpegEncContext),
3793
    MPV_encode_init,
3794
    MPV_encode_picture,
3795
    MPV_encode_end,
3796
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3797
    .capabilities= CODEC_CAP_DELAY,
3798
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
3799
};
3800

    
3801
AVCodec msmpeg4v1_encoder = {
3802
    "msmpeg4v1",
3803
    CODEC_TYPE_VIDEO,
3804
    CODEC_ID_MSMPEG4V1,
3805
    sizeof(MpegEncContext),
3806
    MPV_encode_init,
3807
    MPV_encode_picture,
3808
    MPV_encode_end,
3809
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3810
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
3811
};
3812

    
3813
AVCodec msmpeg4v2_encoder = {
3814
    "msmpeg4v2",
3815
    CODEC_TYPE_VIDEO,
3816
    CODEC_ID_MSMPEG4V2,
3817
    sizeof(MpegEncContext),
3818
    MPV_encode_init,
3819
    MPV_encode_picture,
3820
    MPV_encode_end,
3821
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3822
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
3823
};
3824

    
3825
AVCodec msmpeg4v3_encoder = {
3826
    "msmpeg4",
3827
    CODEC_TYPE_VIDEO,
3828
    CODEC_ID_MSMPEG4V3,
3829
    sizeof(MpegEncContext),
3830
    MPV_encode_init,
3831
    MPV_encode_picture,
3832
    MPV_encode_end,
3833
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3834
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
3835
};
3836

    
3837
AVCodec wmv1_encoder = {
3838
    "wmv1",
3839
    CODEC_TYPE_VIDEO,
3840
    CODEC_ID_WMV1,
3841
    sizeof(MpegEncContext),
3842
    MPV_encode_init,
3843
    MPV_encode_picture,
3844
    MPV_encode_end,
3845
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
3846
    .long_name= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
3847
};