Revision 2d974017

View differences:

Changelog
45 45
- Smacker demuxer and decoder
46 46
- NuppelVideo/MythTV demuxer and RTjpeg decoder
47 47
- KMVC decoder
48
- MPEG-2 intra vlc support
49
- MPEG-2 4:2:2 encoder
48 50

  
49 51
version 0.4.9-pre1:
50 52

  
libavcodec/mpeg12.c
232 232
        }
233 233
    }
234 234

  
235
    if(avctx->profile == FF_PROFILE_UNKNOWN)
236
        avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
237

  
238
    if(avctx->level == FF_LEVEL_UNKNOWN)
239
        avctx->level = s->chroma_format == CHROMA_420 ? 8 : 5;
240

  
235 241
    return 0;
236 242
}
237 243

  
......
319 325
            if(s->codec_id == CODEC_ID_MPEG2VIDEO){
320 326
                put_header(s, EXT_START_CODE);
321 327
                put_bits(&s->pb, 4, 1); //seq ext
322
                put_bits(&s->pb, 1, 0); //esc
323 328

  
324
                if(s->avctx->profile == FF_PROFILE_UNKNOWN){
325
                    put_bits(&s->pb, 3, 4); //profile
326
                }else{
327
                    put_bits(&s->pb, 3, s->avctx->profile); //profile
328
                }
329
                put_bits(&s->pb, 1, s->chroma_format == CHROMA_422); //escx
329 330

  
330
                if(s->avctx->level == FF_LEVEL_UNKNOWN){
331
                    put_bits(&s->pb, 4, 8); //level
332
                }else{
333
                    put_bits(&s->pb, 4, s->avctx->level); //level
334
                }
331
                put_bits(&s->pb, 3, s->avctx->profile); //profile
332
                put_bits(&s->pb, 4, s->avctx->level); //level
335 333

  
336 334
                put_bits(&s->pb, 1, s->progressive_sequence);
337
                put_bits(&s->pb, 2, 1); //chroma format 4:2:0
335
                put_bits(&s->pb, 2, s->chroma_format);
338 336
                put_bits(&s->pb, 2, 0); //horizontal size ext
339 337
                put_bits(&s->pb, 2, 0); //vertical size ext
340 338
                put_bits(&s->pb, 12, v>>18); //bitrate ext
......
468 466
        put_bits(&s->pb, 1, s->alternate_scan);
469 467
        put_bits(&s->pb, 1, s->repeat_first_field);
470 468
        s->progressive_frame = s->progressive_sequence;
471
        put_bits(&s->pb, 1, s->chroma_420_type=s->progressive_frame);
469
        put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
472 470
        put_bits(&s->pb, 1, s->progressive_frame);
473 471
        put_bits(&s->pb, 1, 0); //composite_display_flag
474 472
    }
......
496 494
    }
497 495
}
498 496

  
499
void mpeg1_encode_mb(MpegEncContext *s,
500
                     DCTELEM block[6][64],
501
                     int motion_x, int motion_y)
497
static always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
498
                                                   DCTELEM block[6][64],
499
                                                   int motion_x, int motion_y,
500
                                                   int mb_block_count)
502 501
{
503 502
    int i, cbp;
504 503
    const int mb_x = s->mb_x;
......
507 506

  
508 507
    /* compute cbp */
509 508
    cbp = 0;
510
    for(i=0;i<6;i++) {
509
    for(i=0;i<mb_block_count;i++) {
511 510
        if (s->block_last_index[i] >= 0)
512
            cbp |= 1 << (5 - i);
511
            cbp |= 1 << (mb_block_count - 1 - i);
513 512
    }
514 513

  
515 514
    if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
......
615 614
                }
616 615
                s->mv_bits+= get_bits_diff(s);
617 616
            }
618
            if(cbp)
619
                put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
617
            if(cbp) {
618
                if (s->chroma_y_shift) {
619
                    put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
620
                } else {
621
                    put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
622
                    put_bits(&s->pb, 2, cbp & 3);
623
                }
624
            }
620 625
            s->f_count++;
621 626
        } else{
622 627
            static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
......
694 699
                }
695 700
            }
696 701
            s->mv_bits += get_bits_diff(s);
697
            if(cbp)
698
                put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
702
            if(cbp) {
703
                if (s->chroma_y_shift) {
704
                    put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
705
                } else {
706
                    put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
707
                    put_bits(&s->pb, 2, cbp & 3);
708
                }
709
            }
699 710
        }
700
        for(i=0;i<6;i++) {
701
            if (cbp & (1 << (5 - i))) {
711
        for(i=0;i<mb_block_count;i++) {
712
            if (cbp & (1 << (mb_block_count - 1 - i))) {
702 713
                mpeg1_encode_block(s, block[i], i);
703 714
            }
704 715
        }
......
710 721
    }
711 722
}
712 723

  
724
void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
725
{
726
    if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
727
    else                                mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
728
}
729

  
713 730
// RAL: Parameter added: f_or_b_code
714 731
static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
715 732
{
......
905 922

  
906 923
    /* DC coef */
907 924
    if (s->mb_intra) {
908
        component = (n <= 3 ? 0 : n - 4 + 1);
925
        component = (n <= 3 ? 0 : (n&1) + 1);
909 926
        dc = block[0]; /* overflow is impossible */
910 927
        diff = dc - s->last_dc[component];
911 928
        encode_dc(s, diff, component);
......
3249 3266
    MPV_encode_picture,
3250 3267
    MPV_encode_end,
3251 3268
    .supported_framerates= frame_rate_tab+1,
3252
    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
3269
    .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, -1},
3253 3270
    .capabilities= CODEC_CAP_DELAY,
3254 3271
};
3255 3272
#endif
libavcodec/mpegvideo.c
927 927

  
928 928
    MPV_encode_defaults(s);
929 929

  
930
    if(avctx->pix_fmt != PIX_FMT_YUVJ420P && avctx->pix_fmt != PIX_FMT_YUV420P){
931
        av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
932
        return -1;
933
    }
934

  
935
    if(avctx->codec_id == CODEC_ID_MJPEG || avctx->codec_id == CODEC_ID_LJPEG){
936
        if(avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL && avctx->pix_fmt != PIX_FMT_YUVJ420P){
930
    switch (avctx->codec_id) {
931
    case CODEC_ID_MPEG2VIDEO:
932
        if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUV422P){
933
            av_log(avctx, AV_LOG_ERROR, "only YUV420 and YUV422 are supported\n");
934
            return -1;
935
        }
936
        break;
937
    case CODEC_ID_LJPEG:
938
    case CODEC_ID_MJPEG:
939
        if(avctx->pix_fmt != PIX_FMT_YUVJ420P && (avctx->pix_fmt != PIX_FMT_YUV420P || avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL)){
937 940
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
938 941
            return -1;
939 942
        }
940
    }else{
941
        if(avctx->strict_std_compliance>FF_COMPLIANCE_INOFFICIAL && avctx->pix_fmt != PIX_FMT_YUV420P){
942
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported\n");
943
        break;
944
    default:
945
        if(avctx->pix_fmt != PIX_FMT_YUV420P){
946
            av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
943 947
            return -1;
944 948
        }
945 949
    }
946 950

  
951
    switch (avctx->pix_fmt) {
952
    case PIX_FMT_YUVJ422P:
953
    case PIX_FMT_YUV422P:
954
        s->chroma_format = CHROMA_422;
955
        break;
956
    case PIX_FMT_YUVJ420P:
957
    case PIX_FMT_YUV420P:
958
    default:
959
        s->chroma_format = CHROMA_420;
960
        break;
961
    }
962

  
947 963
    s->bit_rate = avctx->bit_rate;
948 964
    s->width = avctx->width;
949 965
    s->height = avctx->height;
......
2466 2482
    AVFrame *pic_arg = data;
2467 2483
    int i, stuffing_count;
2468 2484

  
2469
    if(avctx->pix_fmt != PIX_FMT_YUV420P && avctx->pix_fmt != PIX_FMT_YUVJ420P){
2470
        av_log(avctx, AV_LOG_ERROR, "this codec supports only YUV420P\n");
2471
        return -1;
2472
    }
2473

  
2474 2485
    for(i=0; i<avctx->thread_count; i++){
2475 2486
        int start_y= s->thread_context[i]->start_mb_y;
2476 2487
        int   end_y= s->thread_context[i]->  end_mb_y;
......
3968 3979
                add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
3969 3980

  
3970 3981
                if(!(s->flags&CODEC_FLAG_GRAY)){
3971
                    add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3972
                    add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3982
                    if (s->chroma_y_shift){
3983
                        add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3984
                        add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3985
                    }else{
3986
                        dct_linesize >>= 1;
3987
                        dct_offset >>=1;
3988
                        add_dequant_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);
3989
                        add_dequant_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);
3990
                        add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
3991
                        add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
3992
                    }
3973 3993
                }
3974 3994
            } else if(s->codec_id != CODEC_ID_WMV2){
3975 3995
                add_dct(s, block[0], 0, dest_y                          , dct_linesize);
......
4011 4031
                put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
4012 4032

  
4013 4033
                if(!(s->flags&CODEC_FLAG_GRAY)){
4014
                    put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
4015
                    put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
4034
                    if(s->chroma_y_shift){
4035
                        put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
4036
                        put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
4037
                    }else{
4038
                        dct_offset >>=1;
4039
                        dct_linesize >>=1;
4040
                        put_dct(s, block[4], 4, dest_cb,              dct_linesize, s->chroma_qscale);
4041
                        put_dct(s, block[5], 5, dest_cr,              dct_linesize, s->chroma_qscale);
4042
                        put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
4043
                        put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
4044
                    }
4016 4045
                }
4017 4046
            }else{
4018 4047
                s->dsp.idct_put(dest_y                          , dct_linesize, block[0]);
......
4234 4263
    }
4235 4264
}
4236 4265

  
4237
static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
4266
static always_inline void encode_mb_internal(MpegEncContext *s, int motion_x, int motion_y, int mb_block_height, int mb_block_count)
4238 4267
{
4239
    int16_t weight[6][64];
4240
    DCTELEM orig[6][64];
4268
    int16_t weight[8][64];
4269
    DCTELEM orig[8][64];
4241 4270
    const int mb_x= s->mb_x;
4242 4271
    const int mb_y= s->mb_y;
4243 4272
    int i;
4244
    int skip_dct[6];
4273
    int skip_dct[8];
4245 4274
    int dct_offset   = s->linesize*8; //default for progressive frames
4246 4275
    uint8_t *ptr_y, *ptr_cb, *ptr_cr;
4247 4276
    int wrap_y, wrap_c;
4248 4277

  
4249
    for(i=0; i<6; i++) skip_dct[i]=0;
4278
    for(i=0; i<mb_block_count; i++) skip_dct[i]=0;
4250 4279

  
4251 4280
    if(s->adaptive_quant){
4252 4281
        const int last_qp= s->qscale;
......
4282 4311
    wrap_y = s->linesize;
4283 4312
    wrap_c = s->uvlinesize;
4284 4313
    ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16;
4285
    ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8;
4286
    ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8;
4314
    ptr_cb = s->new_picture.data[1] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
4315
    ptr_cr = s->new_picture.data[2] + (mb_y * mb_block_height * wrap_c) + mb_x * 8;
4287 4316

  
4288 4317
    if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){
4289 4318
        uint8_t *ebuf= s->edge_emu_buffer + 32;
4290 4319
        ff_emulated_edge_mc(ebuf            , ptr_y , wrap_y,16,16,mb_x*16,mb_y*16, s->width   , s->height);
4291 4320
        ptr_y= ebuf;
4292
        ff_emulated_edge_mc(ebuf+18*wrap_y  , ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
4321
        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);
4293 4322
        ptr_cb= ebuf+18*wrap_y;
4294
        ff_emulated_edge_mc(ebuf+18*wrap_y+8, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1);
4323
        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);
4295 4324
        ptr_cr= ebuf+18*wrap_y+8;
4296 4325
    }
4297 4326

  
......
4311 4340

  
4312 4341
                    dct_offset= wrap_y;
4313 4342
                    wrap_y<<=1;
4343
                    if (s->chroma_format == CHROMA_422)
4344
                        wrap_c<<=1;
4314 4345
                }
4315 4346
            }
4316 4347
        }
......
4326 4357
        }else{
4327 4358
            s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
4328 4359
            s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
4360
            if(!s->chroma_y_shift){ /* 422 */
4361
                s->dsp.get_pixels(s->block[6], ptr_cb + (dct_offset>>1), wrap_c);
4362
                s->dsp.get_pixels(s->block[7], ptr_cr + (dct_offset>>1), wrap_c);
4363
            }
4329 4364
        }
4330 4365
    }else{
4331 4366
        op_pixels_func (*op_pix)[4];
......
4371 4406

  
4372 4407
                    dct_offset= wrap_y;
4373 4408
                    wrap_y<<=1;
4409
                    if (s->chroma_format == CHROMA_422)
4410
                        wrap_c<<=1;
4374 4411
                }
4375 4412
            }
4376 4413
        }
......
4386 4423
        }else{
4387 4424
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
4388 4425
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
4426
            if(!s->chroma_y_shift){ /* 422 */
4427
                s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset>>1), dest_cb + (dct_offset>>1), wrap_c);
4428
                s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset>>1), dest_cr + (dct_offset>>1), wrap_c);
4429
            }
4389 4430
        }
4390 4431
        /* pre quantization */
4391 4432
        if(s->current_picture.mc_mb_var[s->mb_stride*mb_y+ mb_x]<2*s->qscale*s->qscale){
......
4396 4437
            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;
4397 4438
            if(s->dsp.sad[1](NULL, ptr_cb              , dest_cb              , wrap_c, 8) < 20*s->qscale) skip_dct[4]= 1;
4398 4439
            if(s->dsp.sad[1](NULL, ptr_cr              , dest_cr              , wrap_c, 8) < 20*s->qscale) skip_dct[5]= 1;
4440
            if(!s->chroma_y_shift){ /* 422 */
4441
                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;
4442
                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;
4443
            }
4399 4444
        }
4400 4445
    }
4401 4446

  
......
4406 4451
        if(!skip_dct[3]) get_vissual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
4407 4452
        if(!skip_dct[4]) get_vissual_weight(weight[4], ptr_cb                , wrap_c);
4408 4453
        if(!skip_dct[5]) get_vissual_weight(weight[5], ptr_cr                , wrap_c);
4409
        memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*6);
4454
        if(!s->chroma_y_shift){ /* 422 */
4455
            if(!skip_dct[6]) get_vissual_weight(weight[6], ptr_cb + (dct_offset>>1), wrap_c);
4456
            if(!skip_dct[7]) get_vissual_weight(weight[7], ptr_cr + (dct_offset>>1), wrap_c);
4457
        }
4458
        memcpy(orig[0], s->block[0], sizeof(DCTELEM)*64*mb_block_count);
4410 4459
    }
4411 4460

  
4412 4461
    /* DCT & quantize */
4413 4462
    assert(s->out_format!=FMT_MJPEG || s->qscale==8);
4414 4463
    {
4415
        for(i=0;i<6;i++) {
4464
        for(i=0;i<mb_block_count;i++) {
4416 4465
            if(!skip_dct[i]){
4417 4466
                int overflow;
4418 4467
                s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
......
4424 4473
                s->block_last_index[i]= -1;
4425 4474
        }
4426 4475
        if(s->avctx->quantizer_noise_shaping){
4427
            for(i=0;i<6;i++) {
4476
            for(i=0;i<mb_block_count;i++) {
4428 4477
                if(!skip_dct[i]){
4429 4478
                    s->block_last_index[i] = dct_quantize_refine(s, s->block[i], weight[i], orig[i], i, s->qscale);
4430 4479
                }
......
4435 4484
            for(i=0; i<4; i++)
4436 4485
                dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
4437 4486
        if(s->chroma_elim_threshold && !s->mb_intra)
4438
            for(i=4; i<6; i++)
4487
            for(i=4; i<mb_block_count; i++)
4439 4488
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
4440 4489

  
4441 4490
        if(s->flags & CODEC_FLAG_CBP_RD){
4442
            for(i=0;i<6;i++) {
4491
            for(i=0;i<mb_block_count;i++) {
4443 4492
                if(s->block_last_index[i] == -1)
4444 4493
                    s->coded_score[i]= INT_MAX/256;
4445 4494
            }
......
4455 4504

  
4456 4505
    //non c quantize code returns incorrect block_last_index FIXME
4457 4506
    if(s->alternate_scan && s->dct_quantize != dct_quantize_c){
4458
        for(i=0; i<6; i++){
4507
        for(i=0; i<mb_block_count; i++){
4459 4508
            int j;
4460 4509
            if(s->block_last_index[i]>0){
4461 4510
                for(j=63; j>0; j--){
......
4496 4545
    }
4497 4546
}
4498 4547

  
4548
static always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
4549
{
4550
    if (s->chroma_format == CHROMA_420) encode_mb_internal(s, motion_x, motion_y,  8, 6);
4551
    else                                encode_mb_internal(s, motion_x, motion_y, 16, 8);
4552
}
4553

  
4499 4554
#endif //CONFIG_ENCODERS
4500 4555

  
4501 4556
void ff_mpeg_flush(AVCodecContext *avctx){
......
4605 4660
        d->tex_pb= s->tex_pb;
4606 4661
    }
4607 4662
    d->block= s->block;
4608
    for(i=0; i<6; i++)
4663
    for(i=0; i<8; i++)
4609 4664
        d->block_last_index[i]= s->block_last_index[i];
4610 4665
    d->interlaced_dct= s->interlaced_dct;
4611 4666
    d->qscale= s->qscale;
libavcodec/mpegvideo.h
486 486
    uint8_t *chroma_dc_vlc_length;
487 487
#define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
488 488

  
489
    int coded_score[6];
489
    int coded_score[8];
490 490

  
491 491
    /** precomputed matrix (combine qscale and DCT renorm) */
492 492
    int (*q_intra_matrix)[64];

Also available in: Unified diff