DECORE.C
资源名称:VC++视频传输.rar [点击查看]
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:10k
源码类别:
VC书籍
开发平台:
Visual C++
- #include <stdio.h>
- #include <stdlib.h>
- #if ( (defined (WIN32)) && (! defined (_DECORE)) )
- #include <string.h>
- #include <io.h>
- #include <fcntl.h>
- #endif
- #include "gen_usetime.h"
- #include "debug.h"
- #include "mp4_vars.h"
- #include "getbits.h"
- #include "yuv2rgb.h"
- #include "decore.h"
- /**
- *
- **/
- /***/
- static int flag_firstpicture = 1;
- /***/
- //解码器初始化
- int STDCALL decore(unsigned long handle, unsigned long dec_opt,
- void *param1, void *param2)
- {
- if (handle)
- {
- switch (dec_opt)
- {
- case DEC_OPT_MEMORY_REQS:
- {
- DEC_PARAM *dec_param = (DEC_PARAM *)param1;
- DEC_MEM_REQS *dec_mem_reqs = (DEC_MEM_REQS *)param2;
- int coded_y_size = ((dec_param->x_dim + 64) * (dec_param->y_dim + 64));
- int coded_c_size = (((dec_param->x_dim>>1) + 64) * ((dec_param->y_dim>>1) + 64));
- int display_y_size = (dec_param->x_dim * dec_param->y_dim);
- int display_c_size = ((dec_param->x_dim * dec_param->y_dim) >> 2);
- int edged_size = coded_y_size + (2 * coded_c_size);
- int display_size = display_y_size + (2 * display_c_size);
- dec_mem_reqs->mp4_edged_ref_buffers_size = edged_size;
- dec_mem_reqs->mp4_edged_for_buffers_size = edged_size;
- dec_mem_reqs->mp4_display_buffers_size = display_size;
- dec_mem_reqs->mp4_state_size = sizeof(MP4_STATE);
- dec_mem_reqs->mp4_tables_size = sizeof(MP4_TABLES);
- dec_mem_reqs->mp4_stream_size = sizeof(MP4_STREAM);
- return DEC_OK;
- }
- case DEC_OPT_INIT:
- {
- DEC_PARAM *dec_param = (DEC_PARAM *) param1;
- // 初始化解码器的资源
- decore_init(dec_param->x_dim, dec_param->y_dim, dec_param->output_format,
- dec_param->time_incr, dec_param->buffers);
- return DEC_OK;
- }
- break;
- case DEC_OPT_RELEASE:
- {
- decore_release();
- return DEC_OK;
- }
- break;
- case DEC_OPT_SETPP:
- {
- DEC_SET *dec_set = (DEC_SET *) param1;
- int postproc_level = dec_set->postproc_level;
- if ((postproc_level < 0) | (postproc_level > 100))
- return DEC_BAD_FORMAT;
- if (postproc_level < 1) {
- mp4_state->post_flag = 0;
- return DEC_OK;
- }
- else
- {
- mp4_state->post_flag = 1;
- if (postproc_level < 10) {
- mp4_state->pp_options = PP_DEBLOCK_Y_H;
- }
- else if (postproc_level < 20) {
- mp4_state->pp_options = PP_DEBLOCK_Y_H | PP_DEBLOCK_Y_V;
- }
- else if (postproc_level < 30) {
- mp4_state->pp_options = PP_DEBLOCK_Y_H | PP_DEBLOCK_Y_V | PP_DERING_Y;
- }
- else if (postproc_level < 40) {
- mp4_state->pp_options = PP_DEBLOCK_Y_H | PP_DEBLOCK_Y_V | PP_DERING_Y | PP_DEBLOCK_C_H;
- }
- else if (postproc_level < 50) {
- mp4_state->pp_options = PP_DEBLOCK_Y_H | PP_DEBLOCK_Y_V | PP_DERING_Y |
- PP_DEBLOCK_C_H | PP_DEBLOCK_C_V;
- }
- else {
- mp4_state->pp_options = PP_DEBLOCK_Y_H | PP_DEBLOCK_Y_V | PP_DERING_Y |
- PP_DEBLOCK_C_H | PP_DEBLOCK_C_V | PP_DERING_C;
- }
- }
- return DEC_OK;
- }
- break;
- case DEC_OPT_SETOUT:
- {
- DEC_PARAM *dec_param = (DEC_PARAM *) param1;
- decore_setoutput(dec_param->output_format);
- return DEC_OK;
- }
- break;
- default:
- {
- DEC_FRAME *dec_frame = (DEC_FRAME *) param1;
- if (decore_frame(dec_frame->bitstream, dec_frame->length,
- dec_frame->bmp, dec_frame->stride, dec_frame->render_flag))
- return DEC_OK;
- else
- return DEC_EXIT;
- }
- break;
- }
- }
- return DEC_BAD_FORMAT;
- }
- /***/
- int decore_alloc(DEC_BUFFERS buffers);
- // 初始化解码器的资源
- static int decore_init(int hor_size, int ver_size, int output_format,
- int time_inc, DEC_BUFFERS buffers)
- {
- mp4_state = (MP4_STATE *) buffers.mp4_state;
- mp4_tables = (MP4_TABLES *) buffers.mp4_tables;
- ld = (MP4_STREAM *) buffers.mp4_stream;
- #ifndef _DECORE
- // 打开输入文件
- if ((ld->infile = open (mp4_state->infilename, O_RDONLY | O_BINARY)) < 0) {
- _Print ("Input file %s not foundn", mp4_state->infilename);
- exit(91);
- }
- initbits (NULL, 0);
- mp4_state->juice_flag = 0;
- #else
- mp4_state->juice_flag = 1;
- #endif // _DECORE
- mp4_state->post_flag = 0;
- // 读第一次的vol和vop
- mp4_state->hdr.width = hor_size;
- mp4_state->hdr.height = ver_size;
- mp4_state->hdr.quant_precision = 5;
- mp4_state->hdr.bits_per_pixel = 8;
- mp4_state->hdr.quant_type = 0;
- if (flag_firstpicture == 1) {
- mp4_state->hdr.time_increment_resolution = 15;
- flag_firstpicture = 0;
- }
- mp4_state->hdr.complexity_estimation_disable = 1;
- decore_alloc (buffers);
- decore_setoutput (output_format);
- return 1;
- }
- /***/
- //解码器分配存储空间
- int decore_alloc(DEC_BUFFERS buffers)
- {
- mp4_state->hdr.picnum = 0;
- mp4_state->hdr.mb_xsize = mp4_state->hdr.width / 16;
- mp4_state->hdr.mb_ysize = mp4_state->hdr.height / 16;
- mp4_state->hdr.mba_size = mp4_state->hdr.mb_xsize * mp4_state->hdr.mb_ysize;
- // 设置图形维全局参数
- {
- mp4_state->horizontal_size = mp4_state->hdr.width;
- mp4_state->vertical_size = mp4_state->hdr.height;
- mp4_state->mb_width = mp4_state->horizontal_size / 16;
- mp4_state->mb_height = mp4_state->vertical_size / 16;
- mp4_state->coded_picture_width = mp4_state->horizontal_size + 64;
- mp4_state->coded_picture_height = mp4_state->vertical_size + 64;
- mp4_state->chrom_width = mp4_state->coded_picture_width >> 1;
- mp4_state->chrom_height = mp4_state->coded_picture_height >> 1;
- }
- // 初始化解码器
- initdecoder (buffers);
- return 1;
- }
- /***/
- // 解码帧
- int decore_frame(unsigned char *stream, int length, unsigned char *bmp,
- unsigned int stride, int render_flag)
- {
- #ifndef _DECORE
- mp4_state->juice_flag = 0;
- _SetPrintCond(0, 1000, 0, 1000);
- _Print("- Picture %dr", mp4_state->hdr.picnum);
- #else
- initbits (stream, length);
- #endif // _DECORE
- getvolhdr();
- getgophdr();
- if (! getvophdr()) // 读取视频对象平面的头
- return 0;
- get_mp4picture(bmp, stride, render_flag); // 解码vop
- mp4_state->hdr.picnum++;
- return 1;
- }
- /***/
- // 解码器释放
- static int decore_release()
- {
- #ifndef _DECORE
- close (ld->infile);
- #endif // _DECORE
- return 1;
- }
- /***/
- // 解码器输出设置
- int decore_setoutput(int output_format)
- {
- mp4_state->flag_invert = +1;
- switch (output_format)
- {
- case DEC_RGB32:
- mp4_state->convert_yuv = yuv2rgb_32;
- mp4_state->flag_invert = -1;
- break;
- case DEC_RGB32_INV:
- mp4_state->convert_yuv = yuv2rgb_32;
- mp4_state->flag_invert = +1;
- break;
- case DEC_RGB24:
- mp4_state->convert_yuv = yuv2rgb_24;
- mp4_state->flag_invert = -1;
- break;
- case DEC_RGB24_INV:
- mp4_state->convert_yuv = yuv2rgb_24;
- mp4_state->flag_invert = +1;
- break;
- case DEC_RGB555:
- mp4_state->convert_yuv = yuv2rgb_555;
- mp4_state->flag_invert = -1;
- break;
- case DEC_RGB555_INV:
- mp4_state->convert_yuv = yuv2rgb_555;
- mp4_state->flag_invert = +1;
- break;
- case DEC_RGB565:
- mp4_state->convert_yuv = yuv2rgb_565;
- mp4_state->flag_invert = -1;
- break;
- case DEC_RGB565_INV:
- mp4_state->convert_yuv = yuv2rgb_565;
- mp4_state->flag_invert = +1;
- break;
- case DEC_420:
- mp4_state->convert_yuv = yuv12_out;
- break;
- case DEC_YUV2:
- mp4_state->convert_yuv = yuy2_out;
- break;
- case DEC_UYVY:
- mp4_state->convert_yuv = uyvy_out;
- break;
- }
- return 1;
- }
- /**
- * for a console application
- **/
- #ifndef _DECORE
- /***/
- static int dec_reinit();
- static void options (int *argcp, char **argvp[]);
- static void optionhelp (int *argcp);
- int main (int argc, char *argv[])
- {
- char * infilename = argv[1];
- char outputfilename[256] = "Test.yuv";
- DEC_MEM_REQS decMemReqs;
- DEC_PARAM decParam;
- decParam.x_dim = 352;
- decParam.y_dim = 288;
- decParam.output_format = 0;
- decParam.time_incr = 0;
- decore(1, DEC_OPT_MEMORY_REQS, &decParam, &decMemReqs);
- decParam.buffers.mp4_edged_ref_buffers = malloc(decMemReqs.mp4_edged_ref_buffers_size);
- decParam.buffers.mp4_edged_for_buffers = malloc(decMemReqs.mp4_edged_for_buffers_size);
- decParam.buffers.mp4_display_buffers = malloc(decMemReqs.mp4_display_buffers_size);
- decParam.buffers.mp4_state = malloc(decMemReqs.mp4_state_size);
- decParam.buffers.mp4_tables = malloc(decMemReqs.mp4_tables_size);
- decParam.buffers.mp4_stream = malloc(decMemReqs.mp4_stream_size);
- memset(decParam.buffers.mp4_state, 0, decMemReqs.mp4_state_size);
- memset(decParam.buffers.mp4_tables, 0, decMemReqs.mp4_tables_size);
- memset(decParam.buffers.mp4_stream, 0, decMemReqs.mp4_stream_size);
- ((MP4_STATE *) decParam.buffers.mp4_state)->infilename = infilename;
- ((MP4_STATE *) decParam.buffers.mp4_state)->outputname = outputfilename;
- decore(1, DEC_OPT_INIT, &decParam, NULL);
- startTimer();
- // 解码帧
- {
- DEC_FRAME decFrame;
- decFrame.bitstream = NULL;
- decFrame.bmp = NULL;
- decFrame.length = 0;
- decFrame.render_flag = 0;
- while ( decore(1, 0, &decFrame, NULL) == DEC_OK )
- ;
- }
- stopTimer();
- displayTimer(mp4_state->hdr.picnum);
- return 1;
- }
- /***/
- static void options (int *argcp, char **argvp[])
- {
- (*argvp)++;
- (*argcp)--;
- while (*argcp > 1 && (*argvp)[1][0] == '-')
- {
- switch (toupper ((*argvp)[1][1]))
- {
- case 'O':
- mp4_state->output_flag = 1;
- mp4_state->outputname = (*argvp)[2];
- (*argvp) += 2;
- (*argcp) -= 2;
- break;
- case 'J':
- mp4_state->juice_flag = 1;
- mp4_state->juice_hor = atoi ((*argvp)[2]);
- mp4_state->juice_ver = atoi ((*argvp)[3]);
- (*argvp) += 3;
- (*argcp) -= 3;
- break;
- default:
- printf ("Error: Undefined option -%c ignoredn", (*argvp)[1][1]);
- }
- }
- }
- /***/
- static void optionhelp(int *argcp)
- {
- if (*argcp < 2)
- {
- _Print ("Usage: opendivx_dec bitstream {options} n");
- _Print ("Options: -o {outputfilename} YUV concatenated file outputn");
- _Print (" -j {hor_size ver_size} juice stream and its picture formatn");
- exit (0);
- }
- }
- int dec_reinit()
- {
- if (ld->infile != 0)
- lseek (ld->infile, 0l, 0);
- initbits (NULL, 0);
- return 1;
- }
- /***/
- #endif // !_DECORE