Revision 7f2fe444

View differences:

libavcodec/mpegvideo.c
109 109
        }
110 110
    }
111 111
}
112
// move into common.c perhaps 
113
#define CHECKED_ALLOCZ(p, size)\
114
{\
115
    p= av_mallocz(size);\
116
    if(p==NULL){\
117
        perror("malloc");\
118
        goto fail;\
119
    }\
120
}
112 121

  
113 122
/* init common structure for both encoder and decoder */
114 123
int MPV_common_init(MpegEncContext *s)
......
143 152
        c_size = (w >> shift) * (h >> shift);
144 153
        pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
145 154

  
146
        pict = av_mallocz(c_size);
147
        if (pict == NULL)
148
            goto fail;
155
        CHECKED_ALLOCZ(pict, c_size)
149 156
        s->last_picture_base[i] = pict;
150 157
        s->last_picture[i] = pict + pict_start;
151 158
    
152
        pict = av_mallocz(c_size);
153
        if (pict == NULL)
154
            goto fail;
159
        CHECKED_ALLOCZ(pict, c_size)
155 160
        s->next_picture_base[i] = pict;
156 161
        s->next_picture[i] = pict + pict_start;
157 162
        
158 163
        if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) {
159 164
        /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but 
160 165
           do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */
161
            pict = av_mallocz(c_size);
162
            if (pict == NULL) 
163
                goto fail;
166
            CHECKED_ALLOCZ(pict, c_size)
164 167
            s->aux_picture_base[i] = pict;
165 168
            s->aux_picture[i] = pict + pict_start;
166 169
        }
......
169 172
    if (s->encoding) {
170 173
        int j;
171 174
        int mv_table_size= (s->mb_width+2)*(s->mb_height+2);
172

  
173
        /* Allocate MB type table */
174
        s->mb_type = av_mallocz(s->mb_num * sizeof(char));
175
        if (s->mb_type == NULL) {
176
            perror("malloc");
177
            goto fail;
178
        }
179 175
        
180
        s->mb_var = av_mallocz(s->mb_num * sizeof(INT16));
181
        if (s->mb_var == NULL) {
182
            perror("malloc");
183
            goto fail;
184
        }
176
        CHECKED_ALLOCZ(s->mb_var   , s->mb_num * sizeof(INT16))
177
        CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16))
185 178

  
186 179
        /* Allocate MV tables */
187
        s->p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
188
        if (s->p_mv_table == NULL) {
189
            perror("malloc");
190
            goto fail;
191
        }
192
        s->last_p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
193
        if (s->last_p_mv_table == NULL) {
194
            perror("malloc");
195
            goto fail;
196
        }
197
        s->b_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
198
        if (s->b_forw_mv_table == NULL) {
199
            perror("malloc");
200
            goto fail;
201
        }
202
        s->b_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
203
        if (s->b_back_mv_table == NULL) {
204
            perror("malloc");
205
            goto fail;
206
        }
207
        s->b_bidir_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
208
        if (s->b_bidir_forw_mv_table == NULL) {
209
            perror("malloc");
210
            goto fail;
211
        }
212
        s->b_bidir_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
213
        if (s->b_bidir_back_mv_table == NULL) {
214
            perror("malloc");
215
            goto fail;
216
        }
217
        s->b_direct_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
218
        if (s->b_direct_forw_mv_table == NULL) {
219
            perror("malloc");
220
            goto fail;
221
        }
222
        s->b_direct_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
223
        if (s->b_direct_back_mv_table == NULL) {
224
            perror("malloc");
225
            goto fail;
226
        }
227
        s->b_direct_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16));
228
        if (s->b_direct_mv_table == NULL) {
229
            perror("malloc");
230
            goto fail;
231
        }
232

  
233
        s->me_scratchpad = av_mallocz( s->linesize*16*3*sizeof(uint8_t));
234
        if (s->me_scratchpad == NULL) {
235
            perror("malloc");
236
            goto fail;
237
        }
180
        CHECKED_ALLOCZ(s->p_mv_table            , mv_table_size * 2 * sizeof(INT16))
181
        CHECKED_ALLOCZ(s->b_forw_mv_table       , mv_table_size * 2 * sizeof(INT16))
182
        CHECKED_ALLOCZ(s->b_back_mv_table       , mv_table_size * 2 * sizeof(INT16))
183
        CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16))
184
        CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16))
185
        CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16))
186
        CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16))
187
        CHECKED_ALLOCZ(s->b_direct_mv_table     , mv_table_size * 2 * sizeof(INT16))
188

  
189
        CHECKED_ALLOCZ(s->me_scratchpad,  s->linesize*16*3*sizeof(uint8_t))
190
        
191
        CHECKED_ALLOCZ(s->me_map      , ME_MAP_SIZE*sizeof(uint32_t))
192
        CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t))
238 193

  
239 194
        if(s->max_b_frames){
240 195
            for(j=0; j<REORDER_BUFFER_SIZE; j++){
......
247 202
                    shift = (i == 0) ? 0 : 1;
248 203
                    c_size = (w >> shift) * (h >> shift);
249 204

  
250
                    pict = av_mallocz(c_size);
251
                    if (pict == NULL)
252
                        goto fail;
205
                    CHECKED_ALLOCZ(pict, c_size);
253 206
                    s->picture_buffer[j][i] = pict;
254 207
                }
255 208
            }
256 209
        }
210

  
211
        if(s->codec_id==CODEC_ID_MPEG4){
212
            CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
213
            CHECKED_ALLOCZ(   s->pb2_buffer, PB_BUFFER_SIZE);
214
        }
257 215
    }
258 216
    
259 217
    if (s->out_format == FMT_H263 || s->encoding) {
260 218
        int size;
219
        /* Allocate MB type table */
220
        CHECKED_ALLOCZ(s->mb_type  , s->mb_num * sizeof(UINT8))
221

  
261 222
        /* MV prediction */
262 223
        size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
263
        s->motion_val = av_malloc(size * 2 * sizeof(INT16));
264
        if (s->motion_val == NULL)
265
            goto fail;
266
        memset(s->motion_val, 0, size * 2 * sizeof(INT16));
224
        CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16));
267 225
    }
268 226

  
269 227
    if (s->h263_pred || s->h263_plus) {
......
274 232
        y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
275 233
        c_size = (s->mb_width + 2) * (s->mb_height + 2);
276 234
        size = y_size + 2 * c_size;
277
        s->dc_val[0] = av_malloc(size * sizeof(INT16));
278
        if (s->dc_val[0] == NULL)
279
            goto fail;
235
        CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16));
280 236
        s->dc_val[1] = s->dc_val[0] + y_size;
281 237
        s->dc_val[2] = s->dc_val[1] + c_size;
282 238
        for(i=0;i<size;i++)
283 239
            s->dc_val[0][i] = 1024;
284 240

  
285 241
        /* ac values */
286
        s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
287
        if (s->ac_val[0] == NULL)
288
            goto fail;
242
        CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16);
289 243
        s->ac_val[1] = s->ac_val[0] + y_size;
290 244
        s->ac_val[2] = s->ac_val[1] + c_size;
291 245
        
292 246
        /* cbp values */
293
        s->coded_block = av_mallocz(y_size);
294
        if (!s->coded_block)
295
            goto fail;
247
        CHECKED_ALLOCZ(s->coded_block, y_size);
296 248

  
297 249
        /* which mb is a intra block */
298
        s->mbintra_table = av_mallocz(s->mb_num);
299
        if (!s->mbintra_table)
300
            goto fail;
250
        CHECKED_ALLOCZ(s->mbintra_table, s->mb_num);
301 251
        memset(s->mbintra_table, 1, s->mb_num);
302 252
        
303 253
        /* divx501 bitstream reorder buffer */
304
        s->bitstream_buffer= av_mallocz(BITSTREAM_BUFFER_SIZE);
305
        if (!s->bitstream_buffer)
306
            goto fail;
254
        CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE);
255
        
256
        /* cbp, ac_pred, pred_dir */
257
        CHECKED_ALLOCZ(s->cbp_table  , s->mb_num * sizeof(UINT8))
258
        CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8))
259
        
260
        CHECKED_ALLOCZ(s->qscale_table  , s->mb_num * sizeof(UINT8))
307 261
    }
308 262
    /* default structure is frame */
309 263
    s->picture_structure = PICT_FRAME;
310 264

  
311 265
    /* init macroblock skip table */
312
    s->mbskip_table = av_mallocz(s->mb_num);
313
    if (!s->mbskip_table)
314
        goto fail;
266
    CHECKED_ALLOCZ(s->mbskip_table, s->mb_num);
315 267
    
316 268
    s->block= s->blocks[0];
317 269

  
......
322 274
    return -1;
323 275
}
324 276

  
277

  
278
//extern int sads;
279

  
325 280
/* init common structure for both encoder and decoder */
326 281
void MPV_common_end(MpegEncContext *s)
327 282
{
......
329 284

  
330 285
    av_freep(&s->mb_type);
331 286
    av_freep(&s->mb_var);
287
    av_freep(&s->mc_mb_var);
332 288
    av_freep(&s->p_mv_table);
333
    av_freep(&s->last_p_mv_table);
334 289
    av_freep(&s->b_forw_mv_table);
335 290
    av_freep(&s->b_back_mv_table);
336 291
    av_freep(&s->b_bidir_forw_mv_table);
......
343 298
    av_freep(&s->ac_val[0]);
344 299
    av_freep(&s->coded_block);
345 300
    av_freep(&s->mbintra_table);
301
    av_freep(&s->cbp_table);
302
    av_freep(&s->pred_dir_table);
303
    av_freep(&s->qscale_table);
346 304
    av_freep(&s->me_scratchpad);
347

  
305
    av_freep(&s->me_map);
306
    av_freep(&s->me_score_map);
307
    
348 308
    av_freep(&s->mbskip_table);
349 309
    av_freep(&s->bitstream_buffer);
310
    av_freep(&s->tex_pb_buffer);
311
    av_freep(&s->pb2_buffer);
350 312
    for(i=0;i<3;i++) {
351 313
        int j;
352 314
        av_freep(&s->last_picture_base[i]);
......
372 334
    s->frame_rate = avctx->frame_rate;
373 335
    s->width = avctx->width;
374 336
    s->height = avctx->height;
337
    if(avctx->gop_size > 600){
338
        fprintf(stderr, "Warning keyframe intervall too large! reducing it ...\n");
339
        avctx->gop_size=600;
340
    }
375 341
    s->gop_size = avctx->gop_size;
376 342
    s->rtp_mode = avctx->rtp_mode;
377 343
    s->rtp_payload_size = avctx->rtp_payload_size;
......
383 349
    s->qcompress= avctx->qcompress;
384 350
    s->qblur= avctx->qblur;
385 351
    s->b_quant_factor= avctx->b_quant_factor;
352
    s->b_quant_offset= avctx->b_quant_offset;
386 353
    s->avctx = avctx;
387 354
    s->aspect_ratio_info= avctx->aspect_ratio_info;
388 355
    s->flags= avctx->flags;
......
390 357
    s->rc_strategy= avctx->rc_strategy;
391 358
    s->b_frame_strategy= avctx->b_frame_strategy;
392 359
    s->codec_id= avctx->codec->id;
360
    s->luma_elim_threshold  = avctx->luma_elim_threshold;
361
    s->chroma_elim_threshold= avctx->chroma_elim_threshold;
362
    s->strict_std_compliance= avctx->strict_std_compliance;
363
    s->data_partitioning= avctx->flags & CODEC_FLAG_PART;
393 364

  
394 365
    if (s->gop_size <= 1) {
395 366
        s->intra_only = 1;
......
490 461
        return -1;
491 462
    }
492 463
    
493
    if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){
494
        printf("4MV is currently only supported in HQ mode\n");
495
        return -1;
496
    }
497

  
498 464
    { /* set up some save defaults, some codecs might override them later */
499 465
        static int done=0;
500 466
        if(!done){
......
609 575
    UINT8 *tmp;
610 576

  
611 577
    s->mb_skiped = 0;
578
    s->decoding_error=0;
579

  
612 580
    if (s->pict_type == B_TYPE) {
613 581
        for(i=0;i<3;i++) {
614 582
            s->current_picture[i] = s->aux_picture[i];
......
627 595
/* generic function for encode/decode called after a frame has been coded/decoded */
628 596
void MPV_frame_end(MpegEncContext *s)
629 597
{
598
//    if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads);
599

  
630 600
    /* draw edge for correct motion prediction if outside */
631 601
    if (s->pict_type != B_TYPE && !s->intra_only) {
632 602
      if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){
......
645 615
    if(s->pict_type!=B_TYPE){
646 616
        s->last_non_b_pict_type= s->pict_type;
647 617
        s->last_non_b_qscale= s->qscale;
648
        s->last_non_b_mc_mb_var= s->mc_mb_var;
618
        s->last_non_b_mc_mb_var= s->mc_mb_var_sum;
649 619
        s->num_available_buffers++;
650 620
        if(s->num_available_buffers>2) s->num_available_buffers= 2;
651 621
    }
......
804 774
        
805 775
        if(s->flags&CODEC_FLAG_PASS1)
806 776
            ff_write_pass1_stats(s);
777
    
807 778
    }
808 779

  
809 780
    s->input_picture_number++;
......
897 868
    motion_x>>=1;
898 869
    motion_y>>=1;
899 870
}
900

  
901 871
    dxy = ((motion_y & 1) << 1) | (motion_x & 1);
902 872
    src_x = s->mb_x * 16 + (motion_x >> 1);
903 873
    src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
......
1154 1124
    }
1155 1125
}
1156 1126

  
1127
/**
1128
 * cleans dc, ac, coded_block for the current non intra MB
1129
 */
1130
void ff_clean_intra_table_entries(MpegEncContext *s)
1131
{
1132
    int wrap = s->block_wrap[0];
1133
    int xy = s->block_index[0];
1134
    
1135
    s->dc_val[0][xy           ] = 
1136
    s->dc_val[0][xy + 1       ] = 
1137
    s->dc_val[0][xy     + wrap] =
1138
    s->dc_val[0][xy + 1 + wrap] = 1024;
1139
    /* ac pred */
1140
    memset(s->ac_val[0][xy       ], 0, 32 * sizeof(INT16));
1141
    memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16));
1142
    if (s->msmpeg4_version>=3) {
1143
        s->coded_block[xy           ] =
1144
        s->coded_block[xy + 1       ] =
1145
        s->coded_block[xy     + wrap] =
1146
        s->coded_block[xy + 1 + wrap] = 0;
1147
    }
1148
    /* chroma */
1149
    wrap = s->block_wrap[4];
1150
    xy = s->mb_x + 1 + (s->mb_y + 1) * wrap;
1151
    s->dc_val[1][xy] =
1152
    s->dc_val[2][xy] = 1024;
1153
    /* ac pred */
1154
    memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1155
    memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1156
    
1157
    s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0;
1158
}
1159

  
1157 1160
/* generic function called after a macroblock has been parsed by the
1158 1161
   decoder or after it has been encoded by the encoder.
1159 1162

  
......
1182 1185
    /* update DC predictors for P macroblocks */
1183 1186
    if (!s->mb_intra) {
1184 1187
        if (s->h263_pred || s->h263_aic) {
1185
          if(s->mbintra_table[mb_x + mb_y*s->mb_width])
1186
          {
1187
            int wrap, xy, v;
1188
            s->mbintra_table[mb_x + mb_y*s->mb_width]=0;
1189
            wrap = 2 * s->mb_width + 2;
1190
            xy = 2 * mb_x + 1 +  (2 * mb_y + 1) * wrap;
1191
            v = 1024;
1192
            
1193
            s->dc_val[0][xy] = v;
1194
            s->dc_val[0][xy + 1] = v;
1195
            s->dc_val[0][xy + wrap] = v;
1196
            s->dc_val[0][xy + 1 + wrap] = v;
1197
            /* ac pred */
1198
            memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16));
1199
            memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16));
1200
            memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16));
1201
            memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16));
1202
            if (s->h263_msmpeg4) {
1203
                s->coded_block[xy] = 0;
1204
                s->coded_block[xy + 1] = 0;
1205
                s->coded_block[xy + wrap] = 0;
1206
                s->coded_block[xy + 1 + wrap] = 0;
1207
            }
1208
            /* chroma */
1209
            wrap = s->mb_width + 2;
1210
            xy = mb_x + 1 + (mb_y + 1) * wrap;
1211
            s->dc_val[1][xy] = v;
1212
            s->dc_val[2][xy] = v;
1213
            /* ac pred */
1214
            memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16));
1215
            memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16));
1216
          }
1188
            if(s->mbintra_table[mb_x + mb_y*s->mb_width])
1189
                ff_clean_intra_table_entries(s);
1217 1190
        } else {
1218
            s->last_dc[0] = 128 << s->intra_dc_precision;
1219
            s->last_dc[1] = 128 << s->intra_dc_precision;
1191
            s->last_dc[0] =
1192
            s->last_dc[1] =
1220 1193
            s->last_dc[2] = 128 << s->intra_dc_precision;
1221 1194
        }
1222 1195
    }
......
1284 1257
        }
1285 1258

  
1286 1259
        if (!s->mb_intra) {
1260
            const int xy= s->mb_y * s->mb_width + s->mb_x;
1287 1261
            /* motion handling */
1288
            if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){
1262
            /* decoding or more than one mb_type (MC was allready done otherwise) */
1263
            if((!s->encoding) || (s->mb_type[xy]&(s->mb_type[xy]-1))){
1289 1264
                if ((!s->no_rounding) || s->pict_type==B_TYPE){                
1290 1265
                    op_pix = put_pixels_tab;
1291 1266
                    op_qpix= qpel_mc_rnd_tab;
......
1329 1304
    emms_c(); //FIXME remove
1330 1305
}
1331 1306

  
1307
static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc)
1308
{
1309
    static const char tab[64]=
1310
        {3,2,2,1,1,1,1,1,
1311
         1,1,1,1,1,1,1,1,
1312
         1,1,1,1,1,1,1,1,
1313
         0,0,0,0,0,0,0,0,
1314
         0,0,0,0,0,0,0,0,
1315
         0,0,0,0,0,0,0,0,
1316
         0,0,0,0,0,0,0,0,
1317
         0,0,0,0,0,0,0,0};
1318
    int score=0;
1319
    int run=0;
1320
    int i;
1321
    DCTELEM *block= s->block[n];
1322
    const int last_index= s->block_last_index[n];
1323

  
1324
    if(skip_dc) skip_dc=1;
1325
    
1326
    /* are all which we could set to zero are allready zero? */
1327
    if(last_index<=skip_dc - 1) return;
1328

  
1329
    for(i=0; i<=last_index; i++){
1330
        const int j = zigzag_direct[i];
1331
        const int level = ABS(block[j]);
1332
        if(level==1){
1333
            if(skip_dc && i==0) continue;
1334
            score+= tab[run];
1335
            run=0;
1336
        }else if(level>1){
1337
            return;
1338
        }else{
1339
            run++;
1340
        }
1341
    }
1342
    if(score >= threshold) return;
1343
    for(i=skip_dc; i<=last_index; i++){
1344
        const int j = zigzag_direct[i];
1345
        block[j]=0;
1346
    }
1347
    if(block[0]) s->block_last_index[n]= 0;
1348
    else         s->block_last_index[n]= -1;
1349
}
1350

  
1332 1351
static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1333 1352
{
1334 1353
    int i;
......
1350 1369
    const int mb_x= s->mb_x;
1351 1370
    const int mb_y= s->mb_y;
1352 1371
    int i;
1372
    int skip_dct[6];
1353 1373
#if 0
1354 1374
        if (s->interlaced_dct) {
1355 1375
            dct_linesize = s->linesize * 2;
......
1359 1379
            dct_offset = s->linesize * 8;
1360 1380
        }
1361 1381
#endif
1382
    for(i=0; i<6; i++) skip_dct[i]=0;
1362 1383

  
1363 1384
    if (s->mb_intra) {
1364 1385
        UINT8 *ptr;
......
1381 1402
        op_pixels_func *op_pix;
1382 1403
        qpel_mc_func *op_qpix;
1383 1404
        UINT8 *dest_y, *dest_cb, *dest_cr;
1384
        UINT8 *ptr;
1385
        int wrap;
1405
        UINT8 *ptr_y, *ptr_cb, *ptr_cr;
1406
        int wrap_y, wrap_c;
1386 1407

  
1387 1408
        dest_y  = s->current_picture[0] + (mb_y * 16 * s->linesize       ) + mb_x * 16;
1388 1409
        dest_cb = s->current_picture[1] + (mb_y * 8  * (s->linesize >> 1)) + mb_x * 8;
1389 1410
        dest_cr = s->current_picture[2] + (mb_y * 8  * (s->linesize >> 1)) + mb_x * 8;
1411
        wrap_y = s->linesize;
1412
        wrap_c = wrap_y>>1;
1413
        ptr_y  = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
1414
        ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
1415
        ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
1390 1416

  
1391 1417
        if ((!s->no_rounding) || s->pict_type==B_TYPE){
1392 1418
            op_pix = put_pixels_tab;
......
1406 1432
        if (s->mv_dir & MV_DIR_BACKWARD) {
1407 1433
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix);
1408 1434
        }
1409
        wrap = s->linesize;
1410
        ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
1411
        diff_pixels(s->block[0], ptr               , dest_y               , wrap);
1412
        diff_pixels(s->block[1], ptr            + 8, dest_y            + 8, wrap);
1413
        diff_pixels(s->block[2], ptr + 8 * wrap    , dest_y + 8 * wrap    , wrap);
1414
        diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap);
1415 1435

  
1416
        wrap >>=1;
1417
        ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
1418
        diff_pixels(s->block[4], ptr, dest_cb, wrap);
1436
        diff_pixels(s->block[0], ptr_y                 , dest_y                 , wrap_y);
1437
        diff_pixels(s->block[1], ptr_y              + 8, dest_y              + 8, wrap_y);
1438
        diff_pixels(s->block[2], ptr_y + 8 * wrap_y    , dest_y + 8 * wrap_y    , wrap_y);
1439
        diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y);
1440
        diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1441
        diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1442
    
1443
        /* pre quantization */         
1444
        if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){
1445
            if(pix_abs8x8(ptr_y               , dest_y               , wrap_y) < 20*s->qscale) skip_dct[0]= 1;
1446
            if(pix_abs8x8(ptr_y            + 8, dest_y            + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1;
1447
            if(pix_abs8x8(ptr_y + 8*wrap_y    , dest_y + 8*wrap_y    , wrap_y) < 20*s->qscale) skip_dct[2]= 1;
1448
            if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1;
1449
            if(pix_abs8x8(ptr_cb              , dest_cb              , wrap_y) < 20*s->qscale) skip_dct[4]= 1;
1450
            if(pix_abs8x8(ptr_cr              , dest_cr              , wrap_y) < 20*s->qscale) skip_dct[5]= 1;
1451
#if 0
1452
{
1453
 static int stat[7];
1454
 int num=0;
1455
 for(i=0; i<6; i++)
1456
  if(skip_dct[i]) num++;
1457
 stat[num]++;
1458
 
1459
 if(s->mb_x==0 && s->mb_y==0){
1460
  for(i=0; i<7; i++){
1461
   printf("%6d %1d\n", stat[i], i);
1462
  }
1463
 }
1464
}
1465
#endif
1466
        }
1419 1467

  
1420
        ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
1421
        diff_pixels(s->block[5], ptr, dest_cr, wrap);
1422 1468
    }
1423 1469
            
1424 1470
#if 0
......
1435 1481
            }
1436 1482
#endif
1437 1483
    /* DCT & quantize */
1438
    if (s->h263_pred && s->msmpeg4_version!=2) {
1484
    if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) {
1439 1485
        h263_dc_scale(s);
1440 1486
    } else if (s->h263_aic) {
1441 1487
        s->y_dc_scale = 2*s->qscale;
......
1453 1499
        }
1454 1500
    }else{
1455 1501
        for(i=0;i<6;i++) {
1456
            int overflow;
1457
            s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1502
            if(!skip_dct[i]){
1503
                int overflow;
1504
                s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1458 1505
            // FIXME we could decide to change to quantizer instead of clipping
1459 1506
            // JS: I don't think that would be a good idea it could lower quality instead
1460 1507
            //     of improve it. Just INTRADC clipping deserves changes in quantizer
1461
            if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1508
                if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1509
            }else
1510
                s->block_last_index[i]= -1;
1462 1511
        }
1512
        if(s->luma_elim_threshold && !s->mb_intra)
1513
            for(i=0; i<4; i++)
1514
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0);
1515
        if(s->chroma_elim_threshold && !s->mb_intra)
1516
            for(i=4; i<6; i++)
1517
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1);
1463 1518
    }
1464 1519

  
1465 1520
    /* huffman encode */
......
1481 1536
    }
1482 1537
}
1483 1538

  
1484
static void copy_bits(PutBitContext *pb, UINT8 *src, int length)
1539
void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length)
1485 1540
{
1486 1541
#if 1
1487 1542
    int bytes= length>>4;
1488 1543
    int bits= length&15;
1489 1544
    int i;
1490 1545

  
1546
    if(length==0) return;
1547

  
1491 1548
    for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i]));
1492 1549
    put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits));
1493 1550
#else
......
1500 1557
#endif
1501 1558
}
1502 1559

  
1503
static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1560
static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
1504 1561
    int i;
1505 1562

  
1506 1563
    memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop?
......
1523 1580
    d->mb_skiped= s->mb_skiped;
1524 1581
}
1525 1582

  
1526
static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1583
static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
1527 1584
    int i;
1528 1585

  
1529 1586
    memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); 
......
1548 1605
    d->mv_type= s->mv_type;
1549 1606
    d->mv_dir= s->mv_dir;
1550 1607
    d->pb= s->pb;
1608
    if(s->data_partitioning){
1609
        d->pb2= s->pb2;
1610
        d->tex_pb= s->tex_pb;
1611
    }
1551 1612
    d->block= s->block;
1552 1613
    for(i=0; i<6; i++)
1553 1614
        d->block_last_index[i]= s->block_last_index[i];
1554 1615
}
1555 1616

  
1617
static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, 
1618
                           PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
1619
                           int *dmin, int *next_block, int motion_x, int motion_y)
1620
{
1621
    int bits_count;
1622
    
1623
    copy_context_before_encode(s, backup, type);
1624

  
1625
    s->block= s->blocks[*next_block];
1626
    s->pb= pb[*next_block];
1627
    if(s->data_partitioning){
1628
        s->pb2   = pb2   [*next_block];
1629
        s->tex_pb= tex_pb[*next_block];
1630
    }
1631

  
1632
    encode_mb(s, motion_x, motion_y);
1633

  
1634
    bits_count= get_bit_count(&s->pb);
1635
    if(s->data_partitioning){
1636
        bits_count+= get_bit_count(&s->pb2);
1637
        bits_count+= get_bit_count(&s->tex_pb);
1638
    }
1639

  
1640
    if(bits_count<*dmin){
1641
        *dmin= bits_count;
1642
        *next_block^=1;
1643

  
1644
        copy_context_after_encode(best, s, type);
1645
    }
1646
}
1556 1647

  
1557 1648
static void encode_picture(MpegEncContext *s, int picture_number)
1558 1649
{
......
1560 1651
    int i;
1561 1652
    int bits;
1562 1653
    MpegEncContext best_s, backup_s;
1563
    UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB
1654
    UINT8 bit_buf[2][3000];
1655
    UINT8 bit_buf2[2][3000];
1656
    UINT8 bit_buf_tex[2][3000];
1657
    PutBitContext pb[2], pb2[2], tex_pb[2];
1658

  
1659
    for(i=0; i<2; i++){
1660
        init_put_bits(&pb    [i], bit_buf    [i], 3000, NULL, NULL);
1661
        init_put_bits(&pb2   [i], bit_buf2   [i], 3000, NULL, NULL);
1662
        init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL);
1663
    }
1564 1664

  
1565 1665
    s->picture_number = picture_number;
1566 1666

  
......
1572 1672
    s->block_wrap[5]= s->mb_width + 2;
1573 1673
    
1574 1674
    /* Reset the average MB variance */
1575
    s->avg_mb_var = 0;
1576
    s->mc_mb_var = 0;
1675
    s->mb_var_sum = 0;
1676
    s->mc_mb_var_sum = 0;
1577 1677

  
1578 1678
    /* we need to initialize some time vars before we can encode b-frames */
1579 1679
    if (s->h263_pred && !s->h263_msmpeg4)
......
1581 1681

  
1582 1682
    /* Estimate motion for every MB */
1583 1683
    if(s->pict_type != I_TYPE){
1584
//        int16_t (*tmp)[2]= s->p_mv_table;
1585
//        s->p_mv_table= s->last_mv_table;
1586
//        s->last_mv_table= s->mv_table;
1587
    
1588 1684
        for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1589 1685
            s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1;
1590 1686
            s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1);
......
1607 1703
            }
1608 1704
        }
1609 1705
        emms_c();
1610
    }else if(s->pict_type == I_TYPE){
1706
    }else /* if(s->pict_type == I_TYPE) */{
1611 1707
        /* I-Frame */
1612 1708
        //FIXME do we need to zero them?
1613 1709
        memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
......
1615 1711
        memset(s->mb_type      , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1616 1712
    }
1617 1713

  
1618
    if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits
1714
    if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits
1619 1715
        s->pict_type= I_TYPE;
1620 1716
        memset(s->mb_type   , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
1621 1717
        if(s->max_b_frames==0){
......
1703 1799
            s->gob_index = 2;
1704 1800
        else
1705 1801
            s->gob_index = 4;
1802
    }else if(s->codec_id==CODEC_ID_MPEG4){
1803
        s->gob_index = 1;
1706 1804
    }
1707
        
1708
    s->avg_mb_var = s->avg_mb_var / s->mb_num;        
1709
    
1805

  
1806
    if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
1807
        ff_mpeg4_init_partitions(s);
1808

  
1809
    s->resync_mb_x=0;
1810
    s->resync_mb_y=0;
1710 1811
    for(mb_y=0; mb_y < s->mb_height; mb_y++) {
1711
        /* Put GOB header based on RTP MTU */
1812
        /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/
1712 1813
        /* TODO: Put all this stuff in a separate generic function */
1713 1814
        if (s->rtp_mode) {
1714 1815
            if (!mb_y) {
1715 1816
                s->ptr_lastgob = s->pb.buf;
1716 1817
                s->ptr_last_mb_line = s->pb.buf;
1717 1818
            } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) {
1819
                // MN: we could move the space check from h263 -> here, as its not h263 specific
1718 1820
                last_gob = h263_encode_gob_header(s, mb_y);
1719 1821
                if (last_gob) {
1720
                    s->first_gob_line = 1;
1822
                    s->first_slice_line = 1;
1823
                }else{
1824
                    /*MN: we reset it here instead at the end of each line cuz mpeg4 can have 
1825
                          slice lines starting & ending in the middle*/
1826
                    s->first_slice_line = 0;
1721 1827
                }
1722 1828
            }
1723 1829
        }
......
1731 1837
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1732 1838
            const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x];
1733 1839
            const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1;
1734
            PutBitContext pb;
1735
            int d;
1840
//            int d;
1736 1841
            int dmin=10000000;
1737
            int best=0;
1738 1842

  
1739 1843
            s->mb_x = mb_x;
1740 1844
            s->mb_y = mb_y;
......
1744 1848
            s->block_index[3]+=2;
1745 1849
            s->block_index[4]++;
1746 1850
            s->block_index[5]++;
1851
            
1852
            /* write gob / video packet header for formats which support it at any MB (MPEG4) */
1853
            if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){
1854
                int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob;
1855

  
1856
                //the *2 is there so we stay below the requested size
1857
                if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){ 
1858
                    if(s->codec_id==CODEC_ID_MPEG4){
1859
                        if(s->data_partitioning && s->pict_type!=B_TYPE){
1860
                            ff_mpeg4_merge_partitions(s);
1861
                            ff_mpeg4_init_partitions(s);
1862
                        }
1863
                        ff_mpeg4_encode_video_packet_header(s);
1864

  
1865
                        if(s->flags&CODEC_FLAG_PASS1){
1866
                            int bits= get_bit_count(&s->pb);
1867
                            s->misc_bits+= bits - s->last_bits;
1868
                            s->last_bits= bits;
1869
                        }
1870
                        ff_mpeg4_clean_buffers(s);
1871
                    }
1872
                    s->ptr_lastgob = pbBufPtr(&s->pb);
1873
                    s->first_slice_line=1;
1874
                    s->resync_mb_x=mb_x;
1875
                    s->resync_mb_y=mb_y;
1876
                }
1877

  
1878
                if(  (s->resync_mb_x   == s->mb_x)
1879
                   && s->resync_mb_y+1 == s->mb_y){
1880
                    s->first_slice_line=0; 
1881
                }
1882
            }
1883

  
1747 1884
            if(mb_type & (mb_type-1)){ // more than 1 MB type possible
1748 1885
                int next_block=0;
1749
                pb= s->pb;
1886
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
1750 1887

  
1751 1888
                copy_context_before_encode(&backup_s, s, -1);
1889
                backup_s.pb= s->pb;
1890
                best_s.data_partitioning= s->data_partitioning;
1891
                if(s->data_partitioning){
1892
                    backup_s.pb2= s->pb2;
1893
                    backup_s.tex_pb= s->tex_pb;
1894
                }
1752 1895

  
1753 1896
                if(mb_type&MB_TYPE_INTER){
1754 1897
                    s->mv_dir = MV_DIR_FORWARD;
......
1756 1899
                    s->mb_intra= 0;
1757 1900
                    s->mv[0][0][0] = s->p_mv_table[xy][0];
1758 1901
                    s->mv[0][0][1] = s->p_mv_table[xy][1];
1759
                    init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL);
1760
                    s->block= s->blocks[next_block];
1761
                    s->last_bits= 0; //done in copy_context_before_encode but we skip that here
1762

  
1763
                    encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1764
                    d= get_bit_count(&s->pb);
1765
                    if(d<dmin){
1766
                        flush_put_bits(&s->pb);
1767
                        dmin=d;
1768
                        copy_context_after_encode(&best_s, s, MB_TYPE_INTER);
1769
                        best=1;
1770
                        next_block^=1;
1771
                    }
1902
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, 
1903
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
1772 1904
                }
1773 1905
                if(mb_type&MB_TYPE_INTER4V){                 
1774
                    copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V);
1775 1906
                    s->mv_dir = MV_DIR_FORWARD;
1776 1907
                    s->mv_type = MV_TYPE_8X8;
1777 1908
                    s->mb_intra= 0;
......
1779 1910
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
1780 1911
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
1781 1912
                    }
1782
                    init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL);
1783
                    s->block= s->blocks[next_block];
1784

  
1785
                    encode_mb(s, 0, 0);
1786
                    d= get_bit_count(&s->pb);
1787
                    if(d<dmin){
1788
                        flush_put_bits(&s->pb);
1789
                        dmin=d;
1790
                        copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V);
1791
                        best=2;
1792
                        next_block^=1;
1793
                    }
1913
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, 
1914
                                 &dmin, &next_block, 0, 0);
1794 1915
                }
1795 1916
                if(mb_type&MB_TYPE_FORWARD){
1796
                    copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD);
1797 1917
                    s->mv_dir = MV_DIR_FORWARD;
1798 1918
                    s->mv_type = MV_TYPE_16X16;
1799 1919
                    s->mb_intra= 0;
1800 1920
                    s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
1801 1921
                    s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
1802
                    init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL);
1803
                    s->block= s->blocks[next_block];
1804

  
1805
                    encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]);
1806
                    d= get_bit_count(&s->pb);
1807
                    if(d<dmin){
1808
                        flush_put_bits(&s->pb);
1809
                        dmin=d;
1810
                        copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD);
1811
                        best=3;
1812
                        next_block^=1;
1813
                    }
1922
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, 
1923
                                 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
1814 1924
                }
1815 1925
                if(mb_type&MB_TYPE_BACKWARD){
1816
                    copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD);
1817 1926
                    s->mv_dir = MV_DIR_BACKWARD;
1818 1927
                    s->mv_type = MV_TYPE_16X16;
1819 1928
                    s->mb_intra= 0;
1820 1929
                    s->mv[1][0][0] = s->b_back_mv_table[xy][0];
1821 1930
                    s->mv[1][0][1] = s->b_back_mv_table[xy][1];
1822
                    init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL);
1823
                    s->block= s->blocks[next_block];
1824

  
1825
                    encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]);
1826
                    d= get_bit_count(&s->pb);
1827
                    if(d<dmin){
1828
                        flush_put_bits(&s->pb);
1829
                        dmin=d;
1830
                        copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD);
1831
                        best=4;
1832
                        next_block^=1;
1833
                    }
1931
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, 
1932
                                 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
1834 1933
                }
1835 1934
                if(mb_type&MB_TYPE_BIDIR){
1836
                    copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR);
1837 1935
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
1838 1936
                    s->mv_type = MV_TYPE_16X16;
1839 1937
                    s->mb_intra= 0;
......
1841 1939
                    s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
1842 1940
                    s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
1843 1941
                    s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
1844
                    init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL);
1845
                    s->block= s->blocks[next_block];
1846

  
1847
                    encode_mb(s, 0, 0);
1848
                    d= get_bit_count(&s->pb);
1849
                    if(d<dmin){
1850
                        flush_put_bits(&s->pb);
1851
                        dmin=d;
1852
                        copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR);
1853
                        best=5;
1854
                        next_block^=1;
1855
                    }
1942
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, 
1943
                                 &dmin, &next_block, 0, 0);
1856 1944
                }
1857 1945
                if(mb_type&MB_TYPE_DIRECT){
1858
                    copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT);
1859 1946
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1860 1947
                    s->mv_type = MV_TYPE_16X16; //FIXME
1861 1948
                    s->mb_intra= 0;
......
1863 1950
                    s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1];
1864 1951
                    s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0];
1865 1952
                    s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1];
1866
                    init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL);
1867
                    s->block= s->blocks[next_block];
1868

  
1869
                    encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
1870
                    d= get_bit_count(&s->pb);
1871
                    if(d<dmin){
1872
                        flush_put_bits(&s->pb);
1873
                        dmin=d;
1874
                        copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT);
1875
                        best=6;
1876
                        next_block^=1;
1877
                    }
1953
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, 
1954
                                 &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]);
1878 1955
                }
1879 1956
                if(mb_type&MB_TYPE_INTRA){
1880
                    copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA);
1881 1957
                    s->mv_dir = MV_DIR_FORWARD;
1882 1958
                    s->mv_type = MV_TYPE_16X16;
1883 1959
                    s->mb_intra= 1;
1884 1960
                    s->mv[0][0][0] = 0;
1885 1961
                    s->mv[0][0][1] = 0;
1886
                    init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL);
1887
                    s->block= s->blocks[next_block];
1888
                   
1889
                    encode_mb(s, 0, 0);
1890
                    d= get_bit_count(&s->pb);
1891
                    if(d<dmin){
1892
                        flush_put_bits(&s->pb);
1893
                        dmin=d;
1894
                        copy_context_after_encode(&best_s, s, MB_TYPE_INTRA);
1895
                        best=0;
1896
                        next_block^=1;
1897
                    }
1962
                    encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, 
1963
                                 &dmin, &next_block, 0, 0);
1898 1964
                    /* force cleaning of ac/dc pred stuff if needed ... */
1899 1965
                    if(s->h263_pred || s->h263_aic)
1900 1966
                        s->mbintra_table[mb_x + mb_y*s->mb_width]=1;
1901 1967
                }
1902 1968
                copy_context_after_encode(s, &best_s, -1);
1903
                copy_bits(&pb, bit_buf[best], dmin);
1904
                s->pb= pb;
1969
                
1970
                pb_bits_count= get_bit_count(&s->pb);
1971
                flush_put_bits(&s->pb);
1972
                ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
1973
                s->pb= backup_s.pb;
1974
                
1975
                if(s->data_partitioning){
1976
                    pb2_bits_count= get_bit_count(&s->pb2);
1977
                    flush_put_bits(&s->pb2);
1978
                    ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
1979
                    s->pb2= backup_s.pb2;
1980
                    
1981
                    tex_pb_bits_count= get_bit_count(&s->tex_pb);
1982
                    flush_put_bits(&s->tex_pb);
1983
                    ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
1984
                    s->tex_pb= backup_s.tex_pb;
1985
                }
1905 1986
                s->last_bits= get_bit_count(&s->pb);
1906 1987
            } else {
1907 1988
                int motion_x, motion_y;
......
1920 2001
                    motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
1921 2002
                    motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
1922 2003
                    break;
2004
                case MB_TYPE_INTER4V:
2005
                    s->mv_dir = MV_DIR_FORWARD;
2006
                    s->mv_type = MV_TYPE_8X8;
2007
                    s->mb_intra= 0;
2008
                    for(i=0; i<4; i++){
2009
                        s->mv[0][i][0] = s->motion_val[s->block_index[i]][0];
2010
                        s->mv[0][i][1] = s->motion_val[s->block_index[i]][1];
2011
                    }
2012
                    motion_x= motion_y= 0;
2013
                    break;
1923 2014
                case MB_TYPE_DIRECT:
1924 2015
                    s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1925 2016
                    s->mb_intra= 0;
......
1966 2057
            }
1967 2058

  
1968 2059
            MPV_decode_mb(s, s->block);
2060
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb));
1969 2061
        }
1970 2062

  
1971 2063

  
......
1979 2071
            }
1980 2072
            //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, 
1981 2073
            //                    (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize);
1982
            s->first_gob_line = 0;
2074
            if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean
1983 2075
        }
1984 2076
    }
1985 2077
    emms_c();
1986 2078

  
1987
    if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
2079
    if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE)
2080
        ff_mpeg4_merge_partitions(s);
2081

  
2082
    if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE)
1988 2083
        msmpeg4_encode_ext_header(s);
1989 2084

  
2085
    if(s->codec_id==CODEC_ID_MPEG4) 
2086
        ff_mpeg4_stuffing(&s->pb);
2087

  
1990 2088
    //if (s->gob_number)
1991 2089
    //    fprintf(stderr,"\nNumber of GOB: %d", s->gob_number);
1992 2090
    
......
2011 2109
    int bias;
2012 2110
    int max=0;
2013 2111
    unsigned int threshold1, threshold2;
2014

  
2112
    
2015 2113
    av_fdct (block);
2016 2114

  
2017 2115
    /* we need this permutation so that we correct the IDCT
......
2240 2338
    }
2241 2339
}
2242 2340

  
2341
static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y)
2342
{
2343
    int dc, dcb, dcr, y, i;
2344
    for(i=0; i<4; i++){
2345
        dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)];
2346
        for(y=0; y<8; y++){
2347
            int x;
2348
            for(x=0; x<8; x++){
2349
                dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8;
2350
            }
2351
        }
2352
    }
2353
    dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
2354
    dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)];
2355
    for(y=0; y<8; y++){
2356
        int x;
2357
        for(x=0; x<8; x++){
2358
            dest_cb[x + y*(s->linesize>>1)]= dcb/8;
2359
            dest_cr[x + y*(s->linesize>>1)]= dcr/8;
2360
        }
2361
    }
2362
}
2363

  
2364
/**
2365
 * will conceal past errors, and allso drop b frames if needed
2366
 *
2367
 */
2368
void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos)
2369
{
2370
    int mb_x= s->mb_x;
2371
    int mb_y= s->mb_y;
2372
    int mb_dist=0;
2373
    int i, intra_count=0, inter_count=0;
2374
    int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
2375
    int inter_conceal= s->msmpeg4_version ? 50 : 50;
2376
    
2377
    // for last block
2378
    if(mb_x>=s->mb_width)  mb_x= s->mb_width -1;
2379
    if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
2380

  
2381
    if(s->decoding_error==0 && unknown_pos){
2382
        if(s->data_partitioning && s->pict_type!=B_TYPE)
2383
                s->decoding_error= DECODING_AC_LOST;
2384
        else
2385
                s->decoding_error= DECODING_DESYNC;
2386
    }
2387

  
2388
    if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1;
2389

  
2390
    for(i=mb_x + mb_y*s->mb_width; i>=0; i--){
2391
        if(s->mbintra_table[i]) intra_count++;
2392
        else                    inter_count++;
2393
    }
2394
    
2395
    if(s->decoding_error==DECODING_AC_LOST){
2396
        intra_conceal*=2;
2397
        inter_conceal*=2;
2398
    }else if(s->decoding_error==DECODING_ACDC_LOST){
2399
        intra_conceal*=2;
2400
        inter_conceal*=2;
2401
    }
2402

  
2403
    if(unknown_pos && (intra_count<inter_count)){
2404
        intra_conceal= inter_conceal= s->mb_num; 
2405
//        printf("%d %d\n",intra_count, inter_count);
2406
    }
2407

  
2408
    fprintf(stderr, "concealing errors\n");
2409

  
2410
    /* for all MBs from the current one back until the last resync marker */
2411
    for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){
2412
        for(; mb_x>=0; mb_x--){
2413
            uint8_t *dest_y  = s->current_picture[0] + (mb_y * 16*  s->linesize      ) + mb_x * 16;
2414
            uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
2415
            uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
2416
            int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion
2417
            int mb_y_backup= s->mb_y;
2418
            s->mb_x=mb_x;
2419
            s->mb_y=mb_y;
2420
            if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){
2421
                if(s->decoding_error==DECODING_AC_LOST){
2422
                    remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);
2423
//                    printf("remove ac to %d %d\n", mb_x, mb_y);
2424
                }else{
2425
                    mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, 
2426
                                s->last_picture, 0, 0, put_pixels_tab,
2427
                                0/*mx*/, 0/*my*/, 16);
2428
                }
2429
            }
2430
            else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){
2431
                int mx=0;
2432
                int my=0;
2433

  
2434
                if(s->decoding_error!=DECODING_DESYNC){
2435
                    int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2);
2436
                    mx= s->motion_val[ xy ][0];
2437
                    my= s->motion_val[ xy ][1];
2438
                }
2439

  
2440
                mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, 
2441
                            s->last_picture, 0, 0, put_pixels_tab,
2442
                            mx, my, 16);
2443
            }
2444
            s->mb_x= mb_x_backup;
2445
            s->mb_y= mb_y_backup;
2446

  
2447
            if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return;
2448
            if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++;
2449
        }
2450
        mb_x=s->mb_width-1;
2451
    }
2452
}
2453

  
2243 2454
AVCodec mpeg1video_encoder = {
2244 2455
    "mpeg1video",
2245 2456
    CODEC_TYPE_VIDEO,
libavcodec/mpegvideo.h
37 37
#define MAX_MV 2048
38 38
#define REORDER_BUFFER_SIZE (FF_MAX_B_FRAMES+2)
39 39

  
40
#define ME_MAP_SIZE 64
41
#define ME_MAP_SHIFT 3
42
#define ME_MAP_MV_BITS 11
43

  
40 44
typedef struct Predictor{
41 45
    double coeff;
42 46
    double count;
......
100 104
    int force_input_type;/* 0= no force, otherwise I_TYPE, P_TYPE, ... */
101 105
    int max_b_frames; /* max number of b-frames for encoding */
102 106
    float b_quant_factor;/* qscale factor between ips and b frames */
107
    float b_quant_offset;/* qscale offset between ips and b frames */
103 108
    int rc_strategy;
104 109
    int b_frame_strategy;
110
    int luma_elim_threshold;
111
    int chroma_elim_threshold;
112
    int strict_std_compliance; /* strictly follow the std (MPEG4, ...) */
105 113
    int workaround_bugs;       /* workaround bugs in encoders which cannot be detected automatically */
106 114
    /* the following fields are managed internally by the encoder */
107 115

  
......
142 150
    UINT8 *mbskip_table;        /* used to avoid copy if macroblock skipped (for black regions for example) 
143 151
                                   and used for b-frame encoding & decoding (contains skip table of next P Frame) */
144 152
    UINT8 *mbintra_table;       /* used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding */
153
    UINT8 *cbp_table;           /* used to store cbp, ac_pred for partitioned decoding */
154
    UINT8 *pred_dir_table;      /* used to store pred_dir for partitioned decoding */
155
    INT8 *qscale_table;         /* used to store qscale for partitioned decoding (& postprocessing FIXME export) */
145 156

  
146 157
    int input_qscale;           /* qscale prior to reordering of frames */
147 158
    int input_pict_type;        /* pict_type prior to reordering of frames */
......
159 170
    int b_code; /* backward MV resolution for B Frames (mpeg4) */
160 171
    INT16 (*motion_val)[2];            /* used for MV prediction (4MV per MB) */
161 172
    INT16 (*p_mv_table)[2];            /* MV table (1MV per MB) p-frame encoding */
162
    INT16 (*last_p_mv_table)[2];       /* MV table (1MV per MB) p-frame encoding */
163 173
    INT16 (*b_forw_mv_table)[2];       /* MV table (1MV per MB) forward mode b-frame encoding */
164 174
    INT16 (*b_back_mv_table)[2];       /* MV table (1MV per MB) backward mode b-frame encoding */
165 175
    INT16 (*b_bidir_forw_mv_table)[2]; /* MV table (1MV per MB) bidir mode b-frame encoding */
......
169 179
    INT16 (*b_direct_mv_table)[2];     /* MV table (1MV per MB) direct mode b-frame encoding */
170 180
    int me_method;                     /* ME algorithm */
171 181
    uint8_t *me_scratchpad;            /* data area for the me algo, so that the ME doesnt need to malloc/free */
182
    uint32_t *me_map;                  /* map to avoid duplicate evaluations */
183
    uint16_t *me_score_map;            /* map to store the SADs */
184
    int me_map_generation;
185
    int skip_me;                       /* set if ME is skiped for the current MB */
172 186
    int mv_dir;
173 187
#define MV_DIR_BACKWARD  1
174 188
#define MV_DIR_FORWARD   2
......
201 215
    int mb_x, mb_y;
202 216
    int mb_incr;
203 217
    int mb_intra;
204
    UINT16 *mb_var;    /* Table for MB variances */
205
    UINT8 *mb_type;    /* Table for MB type */
218
    UINT16 *mb_var;       /* Table for MB variances */
219
    UINT16 *mc_mb_var;    /* Table for motion compensated MB variances */
220
    UINT8 *mb_type;       /* Table for MB type */
206 221
#define MB_TYPE_INTRA    0x01
207 222
#define MB_TYPE_INTER    0x02
208 223
#define MB_TYPE_INTER4V  0x04
209 224
#define MB_TYPE_SKIPED   0x08
225
#define MB_TYPE_GMC      0x10
226

  
210 227
#define MB_TYPE_DIRECT   0x10
211 228
#define MB_TYPE_FORWARD  0x20
212 229
#define MB_TYPE_BACKWARD 0x40
......
239 256

  
240 257
    /* bit rate control */
241 258
    int I_frame_bits; //FIXME used in mpeg12 ...
242
    int avg_mb_var;        /* average MB variance for current frame */
243
    int mc_mb_var;         /* motion compensated MB variance for current frame */
259
    int mb_var_sum;          /* sum of MB variance for current frame */
260
    int mc_mb_var_sum;       /* motion compensated MB variance for current frame */
244 261
    int last_non_b_mc_mb_var;/* motion compensated MB variance for last non b frame */
245 262
    INT64 wanted_bits;
246 263
    INT64 total_bits;
......
264 281
    int skip_count;
265 282
    int misc_bits; // cbp, mb_type
266 283
    int last_bits; //temp var used for calculating the above vars
284
    
285
    /* error concealment / resync */
286
    int resync_mb_x;                 /* x position of last resync marker */
287
    int resync_mb_y;                 /* y position of last resync marker */
288
    int mb_num_left;                 /* number of MBs left in this video packet */
289
    GetBitContext next_resync_gb;    /* starts at the next resync marker */
290
    int next_resync_qscale;          /* qscale of next resync marker */
291
    int next_resync_pos;             /* bitstream position of next resync marker */
292
#define DECODING_AC_LOST -1
293
#define DECODING_ACDC_LOST -2
294
#define DECODING_DESYNC -3
295
    int decoding_error;
296
    int next_p_frame_damaged;        /* set if the next p frame is damaged, to avoid showing trashed b frames */
297
    int error_resilience;
267 298

  
268 299
    /* H.263 specific */
269 300
    int gob_number;
270 301
    int gob_index;
271
    int first_gob_line;
272 302
        
273 303
    /* H.263+ specific */
274 304
    int umvplus;
......
306 336
    int aspect_ratio_info;
307 337
    int sprite_warping_accuracy;
308 338
    int low_latency_sprite;
309
    int data_partioning;
310
    int resync_marker;
311
    int resync_x_pos;
339
    int data_partitioning;
340
    int rvlc;                        /* reversible vlc */
341
    int resync_marker;               /* could this stream contain resync markers*/
312 342
    int low_delay;                   /* no reordering needed / has no b-frames */
313 343
    int vo_type;
314 344
    int vol_control_parameters;      /* does the stream contain the low_delay flag, used to workaround buggy encoders */
345
    PutBitContext tex_pb;            /* used for data partitioned VOPs */
346
    PutBitContext pb2;               /* used for data partitioned VOPs */
347
#define PB_BUFFER_SIZE 1024*256
348
    uint8_t *tex_pb_buffer;          
349
    uint8_t *pb2_buffer;
315 350

  
316 351
    /* divx specific, used to workaround (many) bugs in divx5 */
317 352
    int divx_version;
......
341 376
    int slice_height;      /* in macroblocks */
342 377
    int first_slice_line;  /* used in mpeg4 too to handle resync markers */
343 378
    int flipflop_rounding;
344
    int bitrate;
345 379
    int msmpeg4_version;   /* 0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 */
346 380
    /* decompression specific */
347 381
    GetBitContext gb;
......
402 436
#endif
403 437
int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
404 438
void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w);
439
void ff_conceal_past_errors(MpegEncContext *s, int conceal_all);
440
void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length);
441
void ff_clean_intra_table_entries(MpegEncContext *s);
405 442

  
406 443
/* motion_est.c */
407 444
void ff_estimate_p_frame_motion(MpegEncContext * s,
......
479 516
int h263_decode_mb(MpegEncContext *s,
480 517
                   DCTELEM block[6][64]);
481 518
int h263_get_picture_format(int width, int height);
519
int ff_mpeg4_decode_video_packet_header(MpegEncContext *s);
520
int ff_mpeg4_resync(MpegEncContext *s);
521
void ff_mpeg4_encode_video_packet_header(MpegEncContext *s);
522
void ff_mpeg4_clean_buffers(MpegEncContext *s);
523
void ff_mpeg4_stuffing(PutBitContext * pbc);
524
void ff_mpeg4_init_partitions(MpegEncContext *s);
525
void ff_mpeg4_merge_partitions(MpegEncContext *s);
482 526

  
483 527
/* rv10.c */
484 528
void rv10_encode_picture_header(MpegEncContext *s, int picture_number);

Also available in: Unified diff