⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一个使用 linux编写的mpeg4解码代码,包括mpeg4解码库和示例代码
💻 CPP
字号:
#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <linux/fb.h>#include <sys/ioctl.h>#include <sys/mman.h>#include "sampmp4.h"#include "ippIP.h"#include <qdatetime.h>int framebuffer_fd;struct fb_var_screeninfo vinfo;struct fb_fix_screeninfo finfo;	Ipp16u* framebuffer_ptr;Ipp8u* decoded_image[3];int widthOfScreen = 640;int heightOfScreen = 480;int widthOfImage = 160;int heightOfImage = 120;int screenSize = widthOfScreen * heightOfScreen * 2;
void output_frame_mpeg4_dec(sample_picture *picture){     int     i, step;
    Ipp8u   *dst_y, *dst_u, *dst_v;
	Ipp8u	*decoded_y, *decoded_u, *decoded_v;
    dst_y = (Ipp8u*)picture->pic_plane[0];
    dst_u = (Ipp8u*)picture->pic_plane[1];
    dst_v = (Ipp8u*)picture->pic_plane[2];
        decoded_y = (Ipp8u*)decoded_image[0];    decoded_u = (Ipp8u*)decoded_image[1];    decoded_v = (Ipp8u*)decoded_image[2];   
    /* Y plane */
    step = picture->pic_plane_step[0];        
    for(i = 0; i<picture->pic_height; i++) {
//        fwrite(dst_y, sizeof(char), picture->pic_width, fpout);		memcpy(decoded_y, dst_y, picture->pic_width);		decoded_y += picture->pic_width;
        dst_y += step;          
    }    
    /* U plane */
    step = picture->pic_plane_step[1];
    for(i = 0; i<picture->pic_height/2; i++) {
 //       fwrite(dst_u, sizeof(char), picture->pic_width/2, fpout); 		memcpy(decoded_u, dst_u, picture->pic_width/2); 		decoded_u += picture->pic_width/2; 
        dst_u += step;     
    }
    /* V plane */
    step = picture->pic_plane_step[2];
    for(i = 0; i<picture->pic_height/2; i++) {
//        fwrite(dst_v, sizeof(char), picture->pic_width/2, fpout);		memcpy(decoded_v, dst_v, picture->pic_width/2);		decoded_v += picture->pic_width/2;
        dst_v += step;
    }}
/*****************************************************************************
// Name:             main
// Description:      Entry function for MPEG4 simple profile decoder
//
// Returns:          0:       decoding okay
//                   others:  decoding fail
*****************************************************************************/
void open_framebuffer(){	//	framebuffer initialize	framebuffer_fd = open("/dev/fb0", O_RDWR);	if (!framebuffer_fd) {		printf("Error: cannot open framebuffer device.\n");	}	printf("The framebuffer device was opened successfully.\n");	/* Get fixed screen information */	if (ioctl(framebuffer_fd, FBIOGET_FSCREENINFO, &finfo)) {		printf("Error reading fixed information.\n");	}	/* Get variable screen information */	if (ioctl(framebuffer_fd, FBIOGET_VSCREENINFO, &vinfo)) {		printf("Error reading variable information.\n");	}	framebuffer_ptr = (Ipp16u *)mmap(0, screenSize, PROT_READ | PROT_WRITE, MAP_SHARED, framebuffer_fd, 0);	if ((int)framebuffer_ptr == -1) 	{ 		printf("Error: failed to mapframebuffer device to memory.\n"); 	}	//	framebuffer initialize end}int main (int argc, char *argv[])
{	QTime time;	decoded_image[0] = (unsigned char*)malloc(widthOfImage*heightOfImage*sizeof(Ipp8u));	decoded_image[1] = (unsigned char*)malloc(widthOfImage*heightOfImage*sizeof(Ipp8u)/4);	decoded_image[2] = (unsigned char*)malloc(widthOfImage*heightOfImage*sizeof(Ipp8u)/4);		int srcStep[3];	srcStep[0] = widthOfImage;	srcStep[1] = widthOfImage/2;	srcStep[2] = widthOfImage/2;		IppiSize roiSize;	roiSize.width = widthOfImage;	roiSize.height = heightOfImage;			int dstStep = 640 * 2;		open_framebuffer();		
    FILE                *fpin = NULL, *fpout = NULL;    
    sample_picture      pic;
    sample_bitstream    stream_buf;
    mp4_dec_state       dec_state;
    sample_status       ret_code;       /* return value */
    int                 end_flag = 0, last_frame_flag = 0;
    
    /* argument check */

    
    /* open input .cmp file handle */
    fpin = fopen(argv[1], "rb");
    if (!fpin)
    {
        printf("Can't open input cmp file!\n");
        return -1;
    }

    
    /* init input video bitstream buffer */
    ret_code = init_input_video_buffer(&stream_buf);
    if (ret_code != SAMPLE_STATUS_NOERR) {
        printf("Init input video bitstream buffer error!\n");
        return -1;
    }
	
    /* load bitstream into buffer for the 1st time */
    ret_code = load_video_buffer(&stream_buf, fpin);
    if (ret_code != SAMPLE_STATUS_NOERR) {
        release_input_video_buffer(&stream_buf);
        printf ("Empty buffer error!\n");
        return -1;
    }
    /* init decoder state struct */
    ret_code = decoder_init_alloc_mpeg4(&stream_buf, &dec_state);    
    if (ret_code != SAMPLE_STATUS_NOERR) {
        release_input_video_buffer(&stream_buf);
        printf ("Init decoder state error!\n");
        return -1;
    }    	int frame = 0;	time.start();
    /* decoding loop */
    while (!end_flag) 
    {
        /* decode video stream frame by frame *///        printf("stream_dec_len: %d\n", stream_buf.bs_cur_byte - stream_buf.bs_buffer);
        ret_code = decode_mpeg4(&stream_buf, &pic, &dec_state);
		printf("decoded frame: %d\n\n", frame++);
        switch (ret_code)
        {
        /* one frame has been decoded successfully */
        case SAMPLE_STATUS_NOERR:            output_frame_mpeg4_dec(&pic);
            /* if it's the last frame, exit the decoding loop */
            if (1 == last_frame_flag) {
                end_flag = 1;
            }
            break;
        
        /* next sync code is not found in the buffer */
        case SAMPLE_STATUS_SYNCNOTFOUND_ERR:
            /* not end of input .cmp file stream */            if (!feof(fpin)) {                /* load bitstream into buffer to fill it up */
                if (SAMPLE_STATUS_NOERR
                    != load_video_buffer(&stream_buf, fpin)) {
                    /* no new bits loaded, this could happen if the buffer
                    // length is less than one frame of raw stream or the 
                    // sync code is missing at all */
                    end_flag = 1;                   
                };
            } else {
                /* to decode the last frame in buffer */
                last_frame_flag = 1;
                /* insert sync code in the end of the stream to provide error
                // protection boundary for the last frame */
                insert_sc_mpeg4 (&stream_buf);
            }
            break;

        default:            /* exceptions */
            end_flag = 1;
        }			Ipp16u *startDisplay = framebuffer_ptr + 200*640 + 50;		ippiYUV420ToBGR565_8u16u_P3C3R(decoded_image, srcStep, startDisplay, dstStep, roiSize);		//		usleep(35000);     
    }

    /* print the final decoding state */
    switch (ret_code)
    {
    case SAMPLE_STATUS_NOERR:
        printf("Everything is okay!\n");
        break;
    case SAMPLE_STATUS_ERR:
        printf("Unknown/Unspecified error!\n");
        break;
    case SAMPLE_STATUS_NOMEM_ERR:
        printf("Memory allocation failed!\n");
        break;
    case SAMPLE_STATUS_BADARG_ERR:
        printf("Bad argument error!\n");
        break;
    case SAMPLE_STATUS_SYNCNOTFOUND_ERR:
        printf("Miss synchronize code!\n");
        break;
    case SAMPLE_STATUS_INPUT_ERR:
        printf("Wrong input parameter!\n");
        break;
    case SAMPLE_STATUS_NOTSUPPORTED_ERR:
        printf("Not support in current version yet!\n");
        break;
    default:
        printf("Out of control!\n");
        break;
    }
	double time_elapsed = (double)time.elapsed() / 1000;	printf("Time elapsed: %f\n", time_elapsed);	printf("Number of Frame: %d\n", frame);		double fps = (double)frame / time_elapsed;		printf("fps!: %f\n", fps);
    /* free decoder state struct working buffer */
    decoder_free_mpeg4(&dec_state);

    /* free input video bitstream buffer */
    release_input_video_buffer(&stream_buf);

    /* close the input and output file handle */
    fclose (fpout); 
    fclose (fpin);

    return (SAMPLE_STATUS_NOERR == ret_code) ? 0 : -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -