Revision cfcf0ffd

View differences:

ffmpeg.c
78 78
static int video_disable = 0;
79 79
static int video_codec_id = CODEC_ID_NONE;
80 80
static int same_quality = 0;
81
static int do_deinterlace = 0;
81 82

  
82 83
static int gop_size = 12;
83 84
static int intra_only = 0;
......
449 450
        }
450 451

  
451 452
        if (use_video) {
452
            AVPicture *picture1;
453
            AVPicture picture;
454
            UINT8 *pict_buffer;
453
            AVPicture *picture1, *picture2, *picture;
454
            AVPicture picture_tmp0, picture_tmp1;
455 455

  
456 456
            ret = url_read(video_handle, picture_in_buf, picture_size);
457 457
            if (ret < 0)
458 458
                break;
459
            
460
            picture2 = &picture_tmp0;
461
            avpicture_fill(picture2, picture_in_buf, pix_fmt, width, height);
462

  
459 463
            if (pix_fmt != PIX_FMT_YUV420P) {
460
                pict_buffer = picture_420p;
461
                img_convert_to_yuv420(pict_buffer, picture_in_buf, pix_fmt, width, height);
464
                picture = &picture_tmp1;
465
                img_convert(picture, PIX_FMT_YUV420P,
466
                            picture2, pix_fmt, 
467
                            width, height);
462 468
            } else {
463
                pict_buffer = picture_in_buf;
469
                picture = picture2;
464 470
            }
465
            /* build a picture storage */
466
            picture.data[0] = pict_buffer;
467
            picture.data[1] = picture.data[0] + width * height;
468
            picture.data[2] = picture.data[1] + (width * height) / 4;
469
            picture.linesize[0] = width;
470
            picture.linesize[1] = width / 2;
471
            picture.linesize[2] = width / 2;
472 471
            
473 472
            for(i=0;i<s->nb_streams;i++) {
474 473
                ost = ost_table[i];
......
485 484
                        if (ost->video_resample) {
486 485
                            picture1 = &ost->pict_tmp;
487 486
                            img_resample(ost->img_resample_ctx, 
488
                                         picture1, &picture);
487
                                         picture1, picture);
489 488
                        } else {
490
                            picture1 = &picture;
489
                            picture1 = picture;
491 490
                        }
492 491
                        ret = avcodec_encode_video(enc, video_buffer, 
493 492
                                                   sizeof(video_buffer), 
......
662 661
}
663 662

  
664 663
/* write a picture to a raw mux */
665
static void write_picture(AVFormatContext *s, int index, AVPicture *picture, int w, int h)
664
static void write_picture(AVFormatContext *s, int index, AVPicture *picture, 
665
                          int pix_fmt, int w, int h)
666 666
{
667 667
    UINT8 *buf, *src, *dest;
668 668
    int size, j, i;
669

  
670
    size = avpicture_get_size(pix_fmt, w, h);
671
    buf = malloc(size);
672
    if (!buf)
673
        return;
674

  
669 675
    /* XXX: not efficient, should add test if we can take
670 676
       directly the AVPicture */
671
    size = (w * h) * 3 / 2; 
672
    buf = malloc(size);
673
    dest = buf;
674
    for(i=0;i<3;i++) {
675
        if (i == 1) {
676
            w >>= 1;
677
            h >>= 1;
677
    switch(pix_fmt) {
678
    case PIX_FMT_YUV420P:
679
        dest = buf;
680
        for(i=0;i<3;i++) {
681
            if (i == 1) {
682
                w >>= 1;
683
                h >>= 1;
684
            }
685
            src = picture->data[i];
686
            for(j=0;j<h;j++) {
687
                memcpy(dest, src, w);
688
                dest += w;
689
                src += picture->linesize[i];
690
            }
691
        }
692
        break;
693
    case PIX_FMT_YUV422P:
694
        size = (w * h) * 2; 
695
        buf = malloc(size);
696
        dest = buf;
697
        for(i=0;i<3;i++) {
698
            if (i == 1) {
699
                w >>= 1;
700
            }
701
            src = picture->data[i];
702
            for(j=0;j<h;j++) {
703
                memcpy(dest, src, w);
704
                dest += w;
705
                src += picture->linesize[i];
706
            }
707
        }
708
        break;
709
    case PIX_FMT_YUV444P:
710
        size = (w * h) * 3; 
711
        buf = malloc(size);
712
        dest = buf;
713
        for(i=0;i<3;i++) {
714
            src = picture->data[i];
715
            for(j=0;j<h;j++) {
716
                memcpy(dest, src, w);
717
                dest += w;
718
                src += picture->linesize[i];
719
            }
720
        }
721
        break;
722
    case PIX_FMT_YUV422:
723
        size = (w * h) * 2; 
724
        buf = malloc(size);
725
        dest = buf;
726
        src = picture->data[0];
727
        for(j=0;j<h;j++) {
728
            memcpy(dest, src, w * 2);
729
            dest += w * 2;
730
            src += picture->linesize[0];
678 731
        }
679
        src = picture->data[i];
732
        break;
733
    case PIX_FMT_RGB24:
734
    case PIX_FMT_BGR24:
735
        size = (w * h) * 3; 
736
        buf = malloc(size);
737
        dest = buf;
738
        src = picture->data[0];
680 739
        for(j=0;j<h;j++) {
681
            memcpy(dest, src, w);
682
            dest += w;
683
            src += picture->linesize[i];
740
            memcpy(dest, src, w * 3);
741
            dest += w * 3;
742
            src += picture->linesize[0];
684 743
        }
744
        break;
745
    default:
746
        return;
685 747
    }
686 748
    s->format->write_packet(s, index, buf, size);
687 749
    free(buf);
......
691 753
static void do_video_out(AVFormatContext *s, 
692 754
                         AVOutputStream *ost, 
693 755
                         AVInputStream *ist,
694
                         AVPicture *pict)
756
                         AVPicture *picture1)
695 757
{
696 758
    int n1, n2, nb, i, ret, frame_number;
697
    AVPicture *picture;
759
    AVPicture *picture, *picture2, *pict;
760
    AVPicture picture_tmp1, picture_tmp2;
698 761
    UINT8 video_buffer[128*1024];
699
    AVCodecContext *enc;
762
    UINT8 *buf = NULL, *buf1 = NULL;
763
    AVCodecContext *enc, *dec;
700 764

  
701 765
    enc = &ost->st->codec;
766
    dec = &ist->st->codec;
702 767

  
703 768
    frame_number = ist->frame_number;
704 769
    /* first drop frame if needed */
705
    n1 = ((INT64)frame_number * enc->frame_rate) / ist->st->codec.frame_rate;
706
    n2 = (((INT64)frame_number + 1) * enc->frame_rate) / ist->st->codec.frame_rate;
770
    n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
771
    n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
707 772
    nb = n2 - n1;
708 773
    if (nb <= 0)
709 774
        return;
710 775
    
776
    /* deinterlace : must be done before any resize */
777
    if (do_deinterlace) {
778
        int size;
779

  
780
        /* create temporary picture */
781
        size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
782
        buf1 = malloc(size);
783
        if (!buf1)
784
            return;
785
        
786
        picture2 = &picture_tmp2;
787
        avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
788

  
789
        if (avpicture_deinterlace(picture2, picture1, 
790
                                  dec->pix_fmt, dec->width, dec->height) < 0) {
791
            /* if error, do not deinterlace */
792
            free(buf1);
793
            buf1 = NULL;
794
            picture2 = picture1;
795
        }
796
    } else {
797
        picture2 = picture1;
798
    }
799

  
800
    /* convert pixel format if needed */
801
    if (enc->pix_fmt != dec->pix_fmt) {
802
        int size;
803

  
804
        /* create temporary picture */
805
        size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
806
        buf = malloc(size);
807
        if (!buf)
808
            return;
809
        pict = &picture_tmp1;
810
        avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
811
        
812
        if (img_convert(pict, enc->pix_fmt, 
813
                        picture2, dec->pix_fmt, 
814
                        dec->width, dec->height) < 0) {
815
            fprintf(stderr, "pixel format conversion not handled\n");
816
            goto the_end;
817
        }
818
    } else {
819
        pict = picture2;
820
    }
821

  
822
    /* XXX: resampling could be done before raw format convertion in
823
       some cases to go faster */
824
    /* XXX: only works for YUV420P */
711 825
    if (ost->video_resample) {
712 826
        picture = &ost->pict_tmp;
713 827
        img_resample(ost->img_resample_ctx, picture, pict);
......
722 836
            /* handles sameq here. This is not correct because it may
723 837
               not be a global option */
724 838
            if (same_quality) {
725
                ost->st->codec.quality = ist->st->codec.quality;
839
                enc->quality = dec->quality;
726 840
            }
727
            ret = avcodec_encode_video(&ost->st->codec, 
841
            ret = avcodec_encode_video(enc, 
728 842
                                       video_buffer, sizeof(video_buffer), 
729 843
                                       picture);
730 844
            s->format->write_packet(s, ost->index, video_buffer, ret);
731 845
        } else {
732
            write_picture(s, ost->index, picture, enc->width, enc->height);
846
            write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
733 847
        }
734 848
    }
849
    the_end:
850
    if (buf)
851
        free(buf);
852
    if (buf1)
853
        free(buf1);
735 854
}
736 855

  
737 856
//#define HEX_DUMP
......
1101 1220
                    if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1102 1221
                        int size;
1103 1222
                        size = (ist->st->codec.width * ist->st->codec.height);
1104
                        
1105
                        picture.data[0] = ptr;
1106
                        picture.data[1] = picture.data[0] + size;
1107
                        picture.data[2] = picture.data[1] + size / 4;
1108
                        picture.linesize[0] = ist->st->codec.width;
1109
                        picture.linesize[1] = ist->st->codec.width / 2;
1110
                        picture.linesize[2] = ist->st->codec.width / 2;
1223
                        avpicture_fill(&picture, ptr, 
1224
                                     ist->st->codec.pix_fmt,
1225
                                     ist->st->codec.width,
1226
                                     ist->st->codec.height);
1111 1227
                        ret = len;
1112 1228
                    } else {
1113 1229
                        data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
......
1800 1916
                video_enc->flags |= CODEC_FLAG_QSCALE;
1801 1917
                video_enc->quality = video_qscale;
1802 1918
            }
1919
            /* XXX: need to find a way to set codec parameters */
1920
            if (oc->format == &ppm_format ||
1921
                oc->format == &ppmpipe_format) {
1922
                video_enc->pix_fmt = PIX_FMT_RGB24;
1923
            }
1803 1924

  
1804 1925
            oc->streams[nb_streams] = st;
1805 1926
            nb_streams++;
......
2013 2134
    { "an", OPT_BOOL, {int_arg: &audio_disable}, "disable audio" },
2014 2135
    { "ad", HAS_ARG | OPT_EXPERT, {opt_audio_device}, "set audio device", "device" },
2015 2136
    { "acodec", HAS_ARG | OPT_EXPERT, {opt_audio_codec}, "force audio codec", "codec" },
2137
    { "deinterlace", OPT_BOOL | OPT_EXPERT, {int_arg: &do_deinterlace}, 
2138
      "deinterlace pictures" },
2016 2139
    { "benchmark", OPT_BOOL | OPT_EXPERT, {int_arg: &do_benchmark}, 
2017 2140
      "add timings for benchmarking" },
2018 2141

  

Also available in: Unified diff