📄 pthread_enc.c
字号:
/* set tabstop=4 *//******************************************************************************** * * * Copyright(C) 2004 Penta-Micro * * * * ALL RIGHT RESERVED * * * * This software is the property of Penta-Micro and is furnished under * * license by Penta-Micro. This software may be used only in accordance * * with the terms of said license. This copyright notice may not be * * removed, modified or obliterated without the prior written permission * * of Penta-Micro. * * * * This software may not be copyed, transmitted, provided to or otherwise * * made available to any other person, company, corporation or other entity * * except as specified in the terms of said license. * * * * No right, title, ownership or other interest in the software is hereby * * granted or transferred. * * * * The information contained herein is subject to change without notice and * * should not be construed as a commitment by Penta-Micro. * * * ******************************************************************************** MODULE NAME: PTHREAD_ENC.C REVISION HISTORY: Date Ver Name Description ---------- --- --------------------- ----------------------------------------- 06/28/2004 2.0 CheulBeck(whitefe) Created 07/20/2005 2.1 JiGwanKang(xchannel) Modified ............................................................................... DESCRIPTION: This Module contains functions for MPEG4 Encoding. ...............................................................................*/ /** ************************************************************************* ** ** includes ** ************************************************************************* **/#include <fcntl.h> #include <pthread.h> #include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <asm/ioctl.h>#include <asm/errno.h>#include <linux/slab.h>#include <sys/stat.h>#include <errno.h> /* for error num */#include "main.h"#include "pthread_enc.h"/** ************************************************************************* ** ** defines ** ************************************************************************* **///#define m_DEBUG(format, args...) printf(format, ## args);fflush(stdout);#define m_DEBUG(format, args...) #define m_MSG(format, args...) printf(format, ## args);fflush(stdout);#define m_ERROR(format, args...) printf(format, ## args);fflush(stdout);/** ************************************************************************* ** ** typedefs ** ************************************************************************* **/ /** ************************************************************************* ** ** globals ** ************************************************************************* **/extern PTHREAD_STATE *gp_state_thread;extern SETUP_PARAM *gp_setup_param;extern UNS8 MAIN_DB_NAME[32];extern UNS32 gv_cur_disk_id;extern UNS16 g_rec_time_list[];/** ************************************************************************* ** ** locals ** ************************************************************************* **/static ENC_OBJECT *sp_enc_object;/** ************************************************************************* ** ** forward declarations ** ************************************************************************* **/#ifdef __TRANS_THR__ #include "stream_server.h"int trans_send_signal(STRDATA *str_data, unsigned short *buf_data, int channel);unsigned long gop_number[MAX_CH_NUM]={0,0,0,0};unsigned long frame_number[MAX_CH_NUM]={0,0,0,0};unsigned short gop_str_nm[MAX_CH_NUM]={15,15,15,15};#endifvoid *pthread_enc(void *args){ S32 ii; UNS32 size_picture; UNS32 ch_size[MAX_CH_NUM]; UNS32 ch_dis_size[MAX_CH_NUM]; UNS16 ch_count[MAX_CH_NUM]; UNS16 chid; PTHREAD_BUF signal;#ifdef __TRANS_THR__ STRDATA str_data_at2041; unsigned short genbit_at2041, chid_at2041; unsigned short pic_type_at2041; unsigned long word_genbit_at2041; /*GOP Number for each Channel*/ unsigned short buf_count_ipb_at2041[MAX_CH_NUM]={0,0,0,0}; unsigned short *gp_ves_stream; int stream_flag_at2041 = 0; int vid_cnt_at2041=0; int count_at2041= 0; int p_value = -1; (unsigned short *)gp_ves_stream = (unsigned short *)malloc(BUFFER_LEN); memset(gp_ves_stream, '\0', BUFFER_LEN);#endif /*__TRANS_THR__*/ m_DEBUG("\t!!!! pthread ENCODE !!!!\n"); /* initialize variable value */ for (ii = 0; ii < MAX_CH_NUM; ii++) { ch_size[ii] = 0; ch_count[ii] = 0; ch_dis_size[ii] = 0; } /* 1st. allocate encoder thread object */ if ( !(sp_enc_object = (ENC_OBJECT *) malloc (sizeof(ENC_OBJECT))) ) { m_ERROR("pthread_enc.c:error In function 'malloc'\n"); return FAILURE; } memset(sp_enc_object, 0x00, sizeof(ENC_OBJECT)); /* 2nd. allocate video buffer */ if ( !(sp_enc_object->p_mux_buf = (UNS16 *) malloc (ENC_MAX_BUF_SIZE + TX_BUF_SIZE*2)) ) { m_ERROR("pthread_enc.c:error In function 'malloc'\n"); return FAILURE; } memset(sp_enc_object->p_mux_buf, 0x00, ENC_MAX_BUF_SIZE + TX_BUF_SIZE*2); /* 3rd. set at2041 parameter */ if (at2041_set_param(gp_setup_param) == FAILURE) { m_ERROR("pthread_enc.c:error In function 'at2041_set_param'\n"); return FAILURE; } /* 4th. create record file */ create_rec_file(sp_enc_object); /* 5th. insert pack and program header */ insert_program_header(gp_setup_param); /* 6th. start encoding */ video_encoder_start(); m_MSG("\n\n\t!!! MPEG4 Encoding Start !!!\n"); m_MSG("\n\t** average frame rate [byte/frame] **\n"); m_MSG("\t[ CH 1 ] [ CH 2 ] [ CH 3 ] [ CH 4 ]\n"); /* change enc state */ gp_state_thread->state_enc.state = BUSY;#ifdef LOOPTHROUGH_MODE video_decoder_buffer_reset(); output_video_format(0); // NTSC display_offset(0, 0, 0); display_mode(0, 1, 1); display(0, 1);#if 0 display_offset(1, 23, 0); display_mode(1, 3, 3); display(1, 1); display_offset(2, 0, 15); display_mode(2, 3, 3); display(2, 1); display_offset(3, 23, 15); display_mode(3, 3, 3); display(3, 1);#endif decode_mode(0x00); video_decoder_start(); live_set_monitoring(L_4CH_PB);#endif while (1) { /* when the not overwrite and disk fulled */ if (gp_state_thread->state_enc.state == IDLE || gp_state_thread->state_diskm.state == FULL) { if(gp_state_thread->state_diskm.state == FULL) { gp_state_thread->state_enc.manual_rec = 0; gp_state_thread->state_enc.schedule_rec = 0; /* clear REC osd */ signal.start_id = PTHREAD_MANAGER; signal.m_signal = SIGNAL_1; if (pthread_send_signal(&signal, PTHREAD_LIVE) == FALSE) { m_ERROR("main.c:error: In function 'pthread_send_signal'\n"); } /* stop Manual ENC */ gp_state_thread->state_enc.state = IDLE; } /* xchannel-20050510@add-bug fix: when the split frame, processing */ if(sp_enc_object->info_picture.last_flag) { stop_encoding(sp_enc_object);#ifdef LOOPTHROUGH_MODE video_decoder_stop();#endif break; } } /* 6th. get encoding stream */ encoding_stream_read(sp_enc_object->p_mux_buf);#ifdef LOOPTHROUGH_MODE size_picture= sp_enc_object->p_mux_buf[1] & 0x7fff; size_picture <<= 4; // 16 bit unit // test for swpark if ((sp_enc_object->p_mux_buf[0] & 0x0800) == 0x0000) { sp_enc_object->p_mux_buf[size_picture+4+1]= 0x0000; // last picture flag printf("data is spilitted\n"); } else sp_enc_object->p_mux_buf[size_picture+4+1]= 0x0001; // last picture flag decoding_stream_write(sp_enc_object->p_mux_buf+4, (size_picture<<1)+4); // byte length#endif /* 7th. update picture db info */ get_data_inform_from_stream(sp_enc_object); /* 8th. decide in recording permittion about stream */ if ((stream_filter(sp_enc_object) == TRUE) || (gp_state_thread->state_enc.state == IDLE)) { /* 9th. event check module */ check_event_from_picture_info(sp_enc_object); /* 9th. pre-alarm module */ /* 10th. alarm processing module */ insert_picture_info_in_stream(sp_enc_object); /* 11th. write to harddisk */ write_stream_to_harddisk(sp_enc_object); /* 12th. update record file */ if (update_rec_file(sp_enc_object) == FAILURE) { m_ERROR("pthread_enc.c:error In function 'update_rec_file'\n"); break; } }#if 0//org code /* 13th. dispaly message about frame average size */ if (sp_enc_object->info_picture.data_type == DT_VIDEO) { // video stream chid = sp_enc_object->info_picture.ch; ch_count[chid] ++; ch_size[chid] = ch_size[chid] + sp_enc_object->info_picture.size; if (ch_count[chid] == 30) { ch_dis_size[chid] = ch_size[chid]/30; ch_count[chid] = 0; ch_size[chid] = 0; // m_MSG("\t[ %6d ] [ %6d ] [ %6d ] [ %6d ]", // ch_dis_size[0], ch_dis_size[1], ch_dis_size[2], ch_dis_size[3]); // m_MSG("\r"); } }#endif#ifdef __TRANS_THR__ word_genbit_at2041 = sp_enc_object->info_picture.size / 2; pic_type_at2041 = sp_enc_object->info_picture.type - 1; chid_at2041 = sp_enc_object->info_picture.ch;#if 1//xchannel add ( only video data ) if(sp_enc_object->info_picture.data_type == DT_VIDEO) {#endif memcpy(gp_ves_stream, sp_enc_object->p_mux_buf+TX_BUF_SIZE, word_genbit_at2041*2); str_data_at2041.frame_id = (unsigned short)chid_at2041; str_data_at2041.frame_type = (unsigned short)pic_type_at2041; str_data_at2041.frame_size = (unsigned short)(word_genbit_at2041*2); if (pic_type_at2041==0) { /* I-Frame */ str_data_at2041.frame_nm = gop_str_nm[chid_at2041]; if ((gop_number[chid_at2041])>=MAX_STREAM_NUM) { (gop_number[chid_at2041])=INIT_STREAM_NUM; } if ((frame_number[chid_at2041])>=MAX_STREAM_NUM) { (frame_number[chid_at2041])=INIT_STREAM_NUM; } buf_count_ipb_at2041[chid_at2041] = 0; gop_number[chid_at2041]++; str_data_at2041.frame_number = frame_number[chid_at2041]++; str_data_at2041.frame_gop = gop_number[chid_at2041]; str_data_at2041.frame_gop_num = (unsigned short)(buf_count_ipb_at2041[chid_at2041]); trans_send_signal( (STRDATA *)&str_data_at2041, (unsigned short *)gp_ves_stream, chid_at2041); buf_count_ipb_at2041[chid_at2041]++; } else if ( (pic_type_at2041==1) && (buf_count_ipb_at2041[chid_at2041] != 0) ) { /* P-Frame */ str_data_at2041.frame_nm = gop_str_nm[chid_at2041]; str_data_at2041.frame_number = frame_number[chid_at2041]++; str_data_at2041.frame_gop = gop_number[chid_at2041]; str_data_at2041.frame_gop_num = (unsigned short)(buf_count_ipb_at2041[chid_at2041]); trans_send_signal( (STRDATA *)&str_data_at2041, (unsigned short *)gp_ves_stream, chid_at2041); buf_count_ipb_at2041[chid_at2041]++; } }#endif /*__TRANS_THR__*/ } /* free allocate buffer */ free(sp_enc_object->p_mux_buf); /* free pthread encoder object */ free(sp_enc_object);#ifdef __TRANS_THR__ free((unsigned short *)gp_ves_stream);#endif m_MSG("\n\n\t!!! MPEG4 Encoding Stop !!!\n"); return NULL;}RETURN at2041_set_param(SETUP_PARAM *psp){ UNS16 ii; UNS16 hsize; UNS16 vsize; UNS16 rate; UNS16 field; UNS16 h_offset; UNS16 v_offset; UNS16 h_mbsize; UNS16 v_mbsize; UNS32 cbr_q; float fps_val; S32 frame_rate[MAX_CH_NUM]; STATE_MAIN *psm; /* 0, 1, 5, 10, 15, 20, 25 30 */ UNS16 framerate_d0_list[MAX_NTSC_FPS_LIST] = { 0, 30, 6, 3, 2, 3, 4, 1 }; UNS16 framerate_d1_list[MAX_NTSC_FPS_LIST] = { 0, 1, 1, 1, 1, 2, 1, 1 };#if 1//xchannel test UNS16 pal_framerate_d0_list[MAX_PAL_FPS_LIST] = { 0, 25, 5, 5, 5, 5, 1 }; UNS16 pal_framerate_d1_list[MAX_PAL_FPS_LIST] = { 0, 1, 1, 2, 3, 4, 1 };#endif psm = &gp_state_thread->state_main; /* reset buffer for encoding */ video_encoder_buffer_reset(); for (ii = 0; ii < MAX_CH_NUM; ii++) { if(psp->enc_ch[ii].flag_conti_rec || psp->enc_ch[ii].motion.flag || psp->enc_ch[ii].sensor.flag_in || psp->enc_ch[ii].schedule.flag) frame_rate[ii] = fps_list[psp->enc_ch[ii].frame_rate]; else frame_rate[ii] = 0; } /* set each video parameter & tw2834 record part */ if (psp->gp.video_form == NTSC) { fps_val = 30.0; h_offset = 0; v_offset = 0; if (psp->gp.resolution == D1) { hsize = 720; vsize = 480; rate = 4; field = 0; h_mbsize = 45; v_mbsize = 30; // pentamicro 2006.01.11 tw2834_set_mux_rec(D1, NTSC); make_rectable(frame_rate, field, 30); } else if (psp->gp.resolution == CIF) { hsize = 704; vsize = 480; rate = 4; field = 0; h_mbsize = 22; v_mbsize = 15; // pentamicro 2006.01.11 tw2834_set_mux_rec(CIF, NTSC);// make_rectable(frame_rate, field, 30); } }else { fps_val = 25.0; h_offset = 0; v_offset = 0; if (psp->gp.resolution == D1) { hsize = 720; vsize = 576; rate = 3; field = 0; h_mbsize = 45; v_mbsize = 36;// pentamicro 2006.01.11 tw2834_set_mux_rec(D1, PAL); make_rectable(frame_rate, field, 25); } else if (psp->gp.resolution == CIF) { hsize = 704; vsize = 576; rate = 3; field = 0; h_mbsize = 22; v_mbsize = 18; // pentamicro 2006.01.11 tw2834_set_mux_rec(CIF, PAL); // make_rectable(frame_rate, field, 100); } } /* hsize : horizontal size (32 ~ 720) * vsize : vertical size (16 ~ 576) * rate : video input rate code, it's not frame_rate_code of MPEG standard * '0' forbidden, '1' 24000/1001, '2' 24, '3' 25, '4' 30000/1001, * '5' 30, '6' 50, '7' 60000/1001, '8' 60, '9' 100, '10' 120000/1001 * '11' 120 * field : '0' frame mode input (interlaced) * '1' field mode input (progressive) */ set_input_video_parameter(hsize, vsize, rate, field); /* if the output data size is lager than the size, * output data will be splitted. * size : maximum data size (KByte unit) * default is 64KByte */ set_max_output_data_size(ENC_MAX_BUF_SIZE / 1024); for (ii = 0; ii < MAX_CH_NUM; ii++) {#ifdef SIMPLE_PROFILE /* ch_id : channel ID * intra_dc_prec : intra_dc_precision (MPEG2) * '0' 8-bit, '1' 9-bit, '2' 10-bit, '3' 11-bit * default is '0' * qscale_type : q_scale_type (MPEG2) * '0' linear, '1' non-linear, default is '0' * intra_vlc_format : intra_vlc_format (MPEG2, refer to ISO/IEC 13818-2) * alternate_scan : alternate scan (MPEG2, MPEG4) * '0' zigzag scan, '1' alternate scan, default is '0' * vop_round_type : vop_rounding_type (MPEG4, refer to ISO/IEC 14496-2) * intra_dc_vlc_thr : intra_dc_vlc_thr (MPEG4, refer to ISO/IEC 14496-2) * 0 ~ 7, default is 0 * quant_type : quant_type (MPEG4) * '0' off, '1' on, default is '0' * direct_mode : direct mode (MPEG4) * '0' off, '1' on, default is '0' * mode_4mv : 4MV mode (MPEG4) * '0' off, '1' on, default is '0' * simple_profile : simple profile (MPEG4) * '0' off, '1' on, default is '0'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -