~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/mpeg4videoenc.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
21
 */
22
22
 
 
23
#include "libavutil/log.h"
 
24
#include "libavutil/opt.h"
23
25
#include "mpegvideo.h"
24
26
#include "h263.h"
25
27
#include "mpeg4video.h"
124
126
{
125
127
    int score= 0;
126
128
    int i, n;
127
 
    int8_t * const qscale_table= s->current_picture.qscale_table;
 
129
    int8_t * const qscale_table = s->current_picture.f.qscale_table;
128
130
 
129
131
    memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
130
132
 
201
203
 */
202
204
void ff_clean_mpeg4_qscales(MpegEncContext *s){
203
205
    int i;
204
 
    int8_t * const qscale_table= s->current_picture.qscale_table;
 
206
    int8_t * const qscale_table = s->current_picture.f.qscale_table;
205
207
 
206
208
    ff_clean_h263_qscales(s);
207
209
 
236
238
 
237
239
 
238
240
/**
239
 
 * encodes the dc value.
 
241
 * Encode the dc value.
240
242
 * @param n block index (0-3 are luma, 4-5 are chroma)
241
243
 */
242
244
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
289
291
}
290
292
 
291
293
/**
292
 
 * encodes a 8x8 block
 
294
 * Encode an 8x8 block.
293
295
 * @param n block index (0-3 are luma, 4-5 are chroma)
294
296
 */
295
297
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
296
298
                               uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
297
299
{
298
300
    int i, last_non_zero;
299
 
#if 0 //variables for the outcommented version
300
 
    int code, sign, last;
301
 
#endif
302
 
    const RLTable *rl;
303
301
    uint32_t *bits_tab;
304
302
    uint8_t *len_tab;
305
303
    const int last_index = s->block_last_index[n];
309
307
        mpeg4_encode_dc(dc_pb, intra_dc, n);
310
308
        if(last_index<1) return;
311
309
        i = 1;
312
 
        rl = &ff_mpeg4_rl_intra;
313
310
        bits_tab= uni_mpeg4_intra_rl_bits;
314
311
        len_tab = uni_mpeg4_intra_rl_len;
315
312
    } else {
316
313
        if(last_index<0) return;
317
314
        i = 0;
318
 
        rl = &ff_h263_rl_inter;
319
315
        bits_tab= uni_mpeg4_inter_rl_bits;
320
316
        len_tab = uni_mpeg4_inter_rl_len;
321
317
    }
322
318
 
323
319
    /* AC coefs */
324
320
    last_non_zero = i - 1;
325
 
#if 1
326
321
    for (; i < last_index; i++) {
327
322
        int level = block[ scan_table[i] ];
328
323
        if (level) {
348
343
            put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
349
344
        }
350
345
    }
351
 
#else
352
 
    for (; i <= last_index; i++) {
353
 
        const int slevel = block[ scan_table[i] ];
354
 
        if (slevel) {
355
 
            int level;
356
 
            int run = i - last_non_zero - 1;
357
 
            last = (i == last_index);
358
 
            sign = 0;
359
 
            level = slevel;
360
 
            if (level < 0) {
361
 
                sign = 1;
362
 
                level = -level;
363
 
            }
364
 
            code = get_rl_index(rl, last, run, level);
365
 
            put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
366
 
            if (code == rl->n) {
367
 
                int level1, run1;
368
 
                level1 = level - rl->max_level[last][run];
369
 
                if (level1 < 1)
370
 
                    goto esc2;
371
 
                code = get_rl_index(rl, last, run, level1);
372
 
                if (code == rl->n) {
373
 
                esc2:
374
 
                    put_bits(ac_pb, 1, 1);
375
 
                    if (level > MAX_LEVEL)
376
 
                        goto esc3;
377
 
                    run1 = run - rl->max_run[last][level] - 1;
378
 
                    if (run1 < 0)
379
 
                        goto esc3;
380
 
                    code = get_rl_index(rl, last, run1, level);
381
 
                    if (code == rl->n) {
382
 
                    esc3:
383
 
                        /* third escape */
384
 
                        put_bits(ac_pb, 1, 1);
385
 
                        put_bits(ac_pb, 1, last);
386
 
                        put_bits(ac_pb, 6, run);
387
 
                        put_bits(ac_pb, 1, 1);
388
 
                        put_sbits(ac_pb, 12, slevel);
389
 
                        put_bits(ac_pb, 1, 1);
390
 
                    } else {
391
 
                        /* second escape */
392
 
                        put_bits(ac_pb, 1, 0);
393
 
                        put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
394
 
                        put_bits(ac_pb, 1, sign);
395
 
                    }
396
 
                } else {
397
 
                    /* first escape */
398
 
                    put_bits(ac_pb, 1, 0);
399
 
                    put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
400
 
                    put_bits(ac_pb, 1, sign);
401
 
                }
402
 
            } else {
403
 
                put_bits(ac_pb, 1, sign);
404
 
            }
405
 
            last_non_zero = i;
406
 
        }
407
 
    }
408
 
#endif
409
346
}
410
347
 
411
348
static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
488
425
    }
489
426
}
490
427
 
 
428
static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
 
429
                            int motion_x, int motion_y, int mb_type)
 
430
{
 
431
    int cbp = 0, i;
 
432
 
 
433
    if (s->flags & CODEC_FLAG_CBP_RD) {
 
434
        int score = 0;
 
435
        const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
 
436
 
 
437
        for (i = 0; i < 6; i++)
 
438
            if (s->coded_score[i] < 0) {
 
439
                score += s->coded_score[i];
 
440
                cbp   |= 1 << (5 - i);
 
441
            }
 
442
 
 
443
        if (cbp) {
 
444
            int zero_score = -6;
 
445
            if ((motion_x | motion_y | s->dquant | mb_type) == 0)
 
446
                zero_score -= 4; //2*MV + mb_type + cbp bit
 
447
 
 
448
            zero_score *= lambda;
 
449
            if (zero_score <= score)
 
450
                cbp = 0;
 
451
        }
 
452
 
 
453
        for (i = 0; i < 6; i++) {
 
454
            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
 
455
                s->block_last_index[i] = -1;
 
456
                s->dsp.clear_block(s->block[i]);
 
457
            }
 
458
        }
 
459
    } else {
 
460
        for (i = 0; i < 6; i++) {
 
461
            if (s->block_last_index[i] >= 0)
 
462
                cbp |= 1 << (5 - i);
 
463
        }
 
464
    }
 
465
    return cbp;
 
466
}
 
467
 
491
468
//FIXME this is duplicated to h263.c
492
469
static const int dquant_code[5]= {1,0,9,2,3};
493
470
 
522
499
            assert(mb_type>=0);
523
500
 
524
501
            /* nothing to do if this MB was skipped in the next P Frame */
525
 
            if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){ //FIXME avoid DCT & ...
 
502
            if (s->next_picture.f.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { //FIXME avoid DCT & ...
526
503
                s->skip_count++;
527
504
                s->mv[0][0][0]=
528
505
                s->mv[0][0][1]=
654
631
                    if(y+16 > s->height) y= s->height-16;
655
632
 
656
633
                    offset= x + y*s->linesize;
657
 
                    p_pic= s->new_picture.data[0] + offset;
 
634
                    p_pic = s->new_picture.f.data[0] + offset;
658
635
 
659
636
                    s->mb_skipped=1;
660
637
                    for(i=0; i<s->max_b_frames; i++){
662
639
                        int diff;
663
640
                        Picture *pic= s->reordered_input_picture[i+1];
664
641
 
665
 
                        if(pic==NULL || pic->pict_type!=AV_PICTURE_TYPE_B) break;
 
642
                        if (pic == NULL || pic->f.pict_type != AV_PICTURE_TYPE_B)
 
643
                            break;
666
644
 
667
 
                        b_pic= pic->data[0] + offset;
668
 
                        if(pic->type != FF_BUFFER_TYPE_SHARED)
 
645
                        b_pic = pic->f.data[0] + offset;
 
646
                        if (pic->f.type != FF_BUFFER_TYPE_SHARED)
669
647
                            b_pic+= INPLACE_OFFSET;
670
648
                        diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
671
649
                        if(diff>s->qscale*70){ //FIXME check that 70 is optimal
769
747
                    /* motion vectors: 8x8 mode*/
770
748
                    h263_pred_motion(s, i, 0, &pred_x, &pred_y);
771
749
 
772
 
                    ff_h263_encode_motion_vector(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x,
773
 
                                                    s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
 
750
                    ff_h263_encode_motion_vector(s, s->current_picture.f.motion_val[0][ s->block_index[i] ][0] - pred_x,
 
751
                                                    s->current_picture.f.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
774
752
                }
775
753
            }
776
754
 
879
857
    put_bits(&s->pb, 16, 0);
880
858
    put_bits(&s->pb, 16, GOP_STARTCODE);
881
859
 
882
 
    time= s->current_picture_ptr->pts;
 
860
    time = s->current_picture_ptr->f.pts;
883
861
    if(s->reordered_input_picture[1])
884
 
        time= FFMIN(time, s->reordered_input_picture[1]->pts);
 
862
        time = FFMIN(time, s->reordered_input_picture[1]->f.pts);
885
863
    time= time*s->avctx->time_base.num;
886
864
 
887
865
    seconds= time/s->avctx->time_base.den;
1091
1069
    }
1092
1070
    put_bits(&s->pb, 3, 0);     /* intra dc VLC threshold */
1093
1071
    if(!s->progressive_sequence){
1094
 
         put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
 
1072
         put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
1095
1073
         put_bits(&s->pb, 1, s->alternate_scan);
1096
1074
    }
1097
1075
    //FIXME sprite stuff
1265
1243
    s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
1266
1244
    s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
1267
1245
    s->luma_dc_vlc_length= uni_DCtab_lum_len;
1268
 
    s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
1269
1246
    s->ac_esc_length= 7+2+1+6+1+12+1;
1270
1247
    s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
1271
1248
    s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
1320
1297
    flush_put_bits(&s->tex_pb);
1321
1298
 
1322
1299
    set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1323
 
    ff_copy_bits(&s->pb, s->pb2.buf   , pb2_len);
1324
 
    ff_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
 
1300
    avpriv_copy_bits(&s->pb, s->pb2.buf   , pb2_len);
 
1301
    avpriv_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1325
1302
    s->last_bits= put_bits_count(&s->pb);
1326
1303
}
1327
1304
 
1338
1315
    put_bits(&s->pb, 1, 0); /* no HEC */
1339
1316
}
1340
1317
 
 
1318
#define OFFSET(x) offsetof(MpegEncContext, x)
 
1319
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 
1320
static const AVOption options[] = {
 
1321
    { "data_partitioning",       "Use data partitioning.",      OFFSET(data_partitioning), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
 
1322
    { "alternate_scan",          "Enable alternate scantable.", OFFSET(alternate_scan),    AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
 
1323
    { NULL },
 
1324
};
 
1325
 
 
1326
static const AVClass mpeg4enc_class = {
 
1327
    .class_name = "MPEG4 encoder",
 
1328
    .item_name  = av_default_item_name,
 
1329
    .option     = options,
 
1330
    .version    = LIBAVUTIL_VERSION_INT,
 
1331
};
 
1332
 
1341
1333
AVCodec ff_mpeg4_encoder = {
1342
 
    "mpeg4",
1343
 
    AVMEDIA_TYPE_VIDEO,
1344
 
    CODEC_ID_MPEG4,
1345
 
    sizeof(MpegEncContext),
1346
 
    encode_init,
1347
 
    MPV_encode_picture,
1348
 
    MPV_encode_end,
 
1334
    .name           = "mpeg4",
 
1335
    .type           = AVMEDIA_TYPE_VIDEO,
 
1336
    .id             = CODEC_ID_MPEG4,
 
1337
    .priv_data_size = sizeof(MpegEncContext),
 
1338
    .init           = encode_init,
 
1339
    .encode         = MPV_encode_picture,
 
1340
    .close          = MPV_encode_end,
1349
1341
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
1350
1342
    .capabilities= CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
1351
1343
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
 
1344
    .priv_class     = &mpeg4enc_class,
1352
1345
};