void stream_decoder::process(uchar* data, int len)
{
    AVPacket * pkt =build_avpkt(data, len);
    if(pkt->size == 0)
        return;
    decode_frame(pkt);
}
void file_decoder::run()
{
    printf("Starting with file %s",path.c_str());
    for (;;)
    {
        AVPacket * pkt = build_avpkt(&avpkt);
        if(pkt->size ==0)
            continue;
        decode_frame(pkt);
    }
}
void video_decode()
{

	if(-1 == build_avpkt(&avpkt))
	{
		usleep(10000);
		return;  
	}

	if(avpkt.size == 0)  
		return;  

	while(avpkt.size > 0) {  
		len = avcodec_decode_video2(c,picture, &got_picture, &avpkt);//解码每一帧  

		IplImage *showImage = cvCreateImage(cvSize(picture->width, picture->height), 8, 3);  
		avpicture_alloc((AVPicture *)&frameRGB, PIX_FMT_BGR24, picture->width, picture->height);  


		if(len < 0) {  
			printf("Error while decoding frame %d\n",frame);  
			break;  
		}  
		if(got_picture) {  
			/* thepicture is allocated by the decoder. no need to free it */  
			//将YUV420格式的图像转换成RGB格式所需要的转换上下文  
			struct SwsContext * scxt = (struct SwsContext *)sws_getContext(picture->width, picture->height, PIX_FMT_YUV420P,  
					picture->width, picture->height, PIX_FMT_BGR24, 2,NULL,NULL,NULL);  
			if(scxt != NULL)  
			{  
				sws_scale(scxt, picture->data, picture->linesize, 0, c->height, frameRGB.data, frameRGB.linesize);//图像格式转换  
				showImage->imageSize = frameRGB.linesize[0];//指针赋值给要显示的图像  
				showImage->imageData = (char *)frameRGB.data[0];  
				cvShowImage("decode", showImage);//显示 
				cvWaitKey(30);//设置显示一帧,如果不设置由于这是个循环,会导致看不到显示出来的图像  
			}
			avpicture_free((AVPicture *)&frameRGB);
			cvReleaseImage(&showImage);
			sws_freeContext(scxt);
			frame++;  
		}  

		avpkt.size -= len;  
		avpkt.data += len;  
	}  


}
Exemple #4
0
static void video_decode_example(const char *outfilename, const char *filename)   
{   
    AVCodec *codec;  
    AVCodecContext *c= NULL;  
    int frame, got_picture, len;   
    FILE*f, *fout;   
    AVFrame *picture;  
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];  
    char buf[1024];   
    AVPacket avpkt;  
    AVDictionary *opts;  
    
    av_init_packet(&avpkt);  
    
    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */  
    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);  
    
    printf("Video decoding/n");  
    opts = NULL;  
    //av_dict_set(&opts, "b", "2.5M", 0);  
    /* find the mpeg1 video decoder */  
    codec = avcodec_find_decoder(CODEC_ID_H264);  
    if(!codec) {   
        fprintf(stderr,"codec not found/n");  
        exit(1);  
    }  
    
    c = avcodec_alloc_context3(codec);  
    picture= avcodec_alloc_frame();  
    
    if(codec->capabilities&CODEC_CAP_TRUNCATED)  
        c->flags|= CODEC_FLAG_TRUNCATED;/* we do not send complete frames */  
    
    /* For some codecs, such as msmpeg4 and mpeg4, width and height 
       MUST be initialized there because this information is not 
       available in the bitstream. */  
    
    /* open it */  
    if(avcodec_open2(c, codec, NULL) < 0) {   
        fprintf(stderr,"could not open codec/n");  
        exit(1);  
    }  
    
     
    fout=fopen(outfilename,"wb");  
    /* the codec gives us the frame size, in samples */  
    
    f =fopen(filename,"rb");   
    if(!f) {   
        fprintf(stderr,"could not open %s/n", filename);  
        exit(1);  
    }  
    
    frame = 0;  
    for(;;) {  
        //avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);  
        build_avpkt(&avpkt, f);  
        if(avpkt.size == 0)   
            break;  
    
        /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) 
           and this is the only method to use them because you cannot 
           know the compressed data size before analysing it. 
   
           BUT some other codecs (msmpeg4, mpeg4) are inherently frame 
           based, so you must call them with all the data for one 
           frame exactly. You must also initialize 'width' and 
           'height' before initializing them. */  
    
        /* NOTE2: some codecs allow the raw parameters (frame size, 
           sample rate) to be changed at any frame. We handle this, so 
           you should also take care of it */  
    
        /* here, we use a stream based decoder (mpeg1video), so we 
           feed decoder and see if it could decode a frame */  
        //avpkt.data = inbuf;  
        while(avpkt.size > 0) {   
            len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);  
            if(len < 0) {   
                fprintf(stderr,"Error while decoding frame %d/n", frame);  
                break;  
                //   exit(1);  
            }  
            if(got_picture) {   
                printf("saving frame %3d/n", frame);  
                fflush(stdout);  
    
                /* the picture is allocated by the decoder. no need to 
                   free it */  
                sprintf(buf, outfilename, frame);  
                pgm_save(picture->data[0], picture->linesize[0],  
                         c->width, c->height, fout);  
                pgm_save(picture->data[1], picture->linesize[1],  
                         c->width/2, c->height/2, fout);  
                pgm_save(picture->data[2], picture->linesize[2],  
                         c->width/2, c->height/2, fout);  
                frame++;  
            }  
            avpkt.size -= len;  
            avpkt.data += len;  
        }  
    }  
    
    /* some codecs, such as MPEG, transmit the I and P frame with a 
       latency of one frame. You must do the following to have a 
       chance to get the last frame of the video */  
    avpkt.data = NULL;  
    avpkt.size = 0;  
    len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);  
    if(got_picture) {   
        printf("saving last frame %3d/n", frame);  
        fflush(stdout);  
    
        /* the picture is allocated by the decoder. no need to 
           free it */  
        sprintf(buf, outfilename, frame);  
        //pgm_save(picture->data[0], picture->linesize[0],  
		//       c->width, c->height, fout);  
		pgm_save(picture->data[0], picture->linesize[0],c->width, c->height, fout);  
		pgm_save(picture->data[1], picture->linesize[1],c->width/2, c->height/2, fout);  
		pgm_save(picture->data[2], picture->linesize[2],c->width/2, c->height/2, fout);  
    
		frame++;  
    }  
    
    fclose(f);  
	fclose(fout);  
    
    avcodec_close(c);  
    av_free(c);  
    av_free(picture);  
    printf("/n");  
}