245680 发表于 2012-10-9 01:53:37

AR.drone2 非sdk读取视频信号(续)

一天改点一天改点,今天有进步,不像之前的大面积错帧,但是还是有问题,pframe(关键帧)是对的,iframe(连续帧)以3秒1次左右的概率出错,把关键代码放上来,欢迎盗版,但是不要忘了顶一下哦,我觉得写h264解码难度蛮大,大家对这个不太感兴趣吗
        CSocket vi;
        vi.Create(5555,SOCK_STREAM,"192.168.1.3");
        vi.Connect("192.168.1.1",5555);

        FILE *outf = fopen("out.yuv", "wb");
        AVCodec *codec;                          // Codec
        AVCodecContext *c;                  // Codec Context
        AVFrame *picture;                  // Frame       
        intgot_picture, consumed_bytes;
        static AVPacket packet;
        avcodec_init();
        avcodec_register_all();
        codec = avcodec_find_decoder(CODEC_ID_H264);
        c = avcodec_alloc_context();
        if(!c){
                return ;
        }
        //open codec

        c->pix_fmt = PIX_FMT_YUV420P;
        c->skip_frame = AVDISCARD_DEFAULT;
        c->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
        c->error_recognition = FF_ER_CAREFUL;
        c->skip_loop_filter = AVDISCARD_DEFAULT;
        //c->workaround_bugs = FF_BUG_AUTODETECT;
        c->codec_type = AVMEDIA_TYPE_VIDEO;
        c->codec_id = CODEC_ID_H264;
        c->skip_idct = AVDISCARD_DEFAULT;

        if (avcodec_open(c, codec) < 0) {
                return ;
        }
        //allocate frame buffer
        picture   = avcodec_alloc_frame();
        if(!picture){
                return ;
        }

        IplImage* dspimg=cvCreateImage(cvSize(640,360),IPL_DEPTH_8U,1);
        int i=1;
        while(i==1)
        {
                char buf;               
                int recv,bufsize=0;
                recv=vi.Receive(buf,123456);
                bufsize+=recv;
                parrot_video_encapsulation_t *PAVE;
                PAVE=(parrot_video_encapsulation_t*)buf;
                char *buff;
                if (PAVE->signature == 'P' &&
                        PAVE->signature == 'a' &&
                        PAVE->signature == 'V' &&
                        PAVE->signature == 'E')
                {
                        buff=buf+PAVE->header_size;//sizeof(parrot_video_encapsulation_t);//why the header is more 4byte than struct?
                        packet.size=PAVE->payload_size;//sizeof(parrot_video_encapsulation_t);
                        while(bufsize<PAVE->payload_size)
                        {
                                recv=vi.Receive(&buf,123456);
                                bufsize+=recv;
                        }
                }
                else
                {
                        continue;

                }
                packet.data=(uint8_t*)buff;
                consumed_bytes= avcodec_decode_video2(c, picture, &got_picture,&packet);//(uint8_t *) buff, recv-sizeof(parrot_video_encapsulation_t));
                if(consumed_bytes>0){
                        for(int j=0; j<c->height; j++){
                                //        fwrite(picture->data + j * picture->linesize, 1, c->width, outf);
                                for(int k=0;k<c->width;k++)
                                        CV_IMAGE_ELEM(dspimg,unsigned char,j,k)=*(picture->data+k+ j * picture->linesize);
                        }
                        //dspimg->imageData=(char*)picture->data;
                        cvShowImage("",dspimg);
                        int k=cvWaitKey(20);
                        if(k==32) i=0;
                }
                else{
                        i--;
                        SetWindowText("no");
                }
                if(got_picture)
                {
                        char buf1;
                        sprintf(buf1,"%d,%d,%d,%d",i,PAVE->frame_type,consumed_bytes,c->width);//360*640
                        SetWindowText(buf1);
                        //break;
                }
        }//鄙人邮箱405181621@qq.com,关于AR.drone2,大家有什么有意思的新成果和我联系哦
        avcodec_close(c);
        av_free(c);
        fclose(outf);
        vi.Close();
页: [1]
查看完整版本: AR.drone2 非sdk读取视频信号(续)