📄 pthread_dec.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_DEC.C REVISION HISTORY: Date Ver Name Description ---------- --- --------------------- ----------------------------------------- 06/28/2004 2.0 CheulBeck(whitefe) Created ............................................................................... DESCRIPTION: This Module contains functions for MPEG4 Decoding. ...............................................................................*/ /** ************************************************************************* ** ** 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 "main.h"#include "pthread_dec.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]; /** ************************************************************************* ** ** locals ** ************************************************************************* **/static DEC_OBJECT *sp_dec_object;/** ************************************************************************* ** ** forward declarations ** ************************************************************************* **/void *pthread_dec(void *args){ BOOL exit_dec; RETURN ret; PTHREAD_BUF signal; m_MSG("\t!!!! pthread DECODE !!!!\n"); /* initialize variable value */ exit_dec = FALSE; /* 1st. allocate decoder thread object */ if ( !(sp_dec_object = (DEC_OBJECT *) malloc (sizeof(DEC_OBJECT))) ) { m_ERROR("pthread_dec.c:error In function 'malloc'\n"); return FAILURE; } memset(sp_dec_object, 0x00, sizeof(DEC_OBJECT)); /* 2nd. allocate decoder video buffer */ if ( !(sp_dec_object->p_demux_buf = (UNS16 *) malloc (DEC_MAX_BUF_SIZE + RX_BUF_SIZE * 2)) ) { m_ERROR("pthread_dec.c:error In function 'malloc'\n"); return FAILURE; } memset(sp_dec_object->p_demux_buf, 0x00, DEC_MAX_BUF_SIZE + RX_BUF_SIZE * 2); /* 3rd. open playback file */ if (open_play_file(sp_dec_object) == FAILURE) { m_ERROR("pthread_dec.c:error In function 'open_play_file'\n"); return FAILURE; } /* 4th. set at2041 decoder parameter */ if (dec_set_param(sp_dec_object) == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_set_param'\n"); return FAILURE; } /* 5th. change dec state */ gp_state_thread->state_dec.state = BUSY; sp_dec_object->flag_first = 1; sp_dec_object->global_count = 0; /* 6th. change live audio to decoding state */ set_live_audio_ch(4); /* 7th. start mpeg decoding */ video_decoder_start(); /* 8th. signal send for auto view mode change */ signal.start_id = PTHREAD_DEC; signal.m_value = 1; if(gp_state_thread->state_dec.play_file[24] == 'm') signal.m_channel = 4; else#if 1//big endian signal.m_channel = sp_dec_object->tpp.start_code & 0x0f; #else//little endian signal.m_channel = sp_dec_object->tpp.start_code & 0x0f; #endif if (pthread_send_signal(&signal, PTHREAD_LIVE) == FALSE) { m_ERROR("main.c:error: In function 'pthread_send_signal'\n"); } do { /* * trick mode * * 0: videoPlaying, * 1: videoStop, * 2: videoPaused, * 3: videoSlowMotion, * 4: videoRewind, * 5: videoStepBackward, * 6: videoFastForward, * 7: videoStepForward, * 8: videoStepForward * 9: idle */ switch (gp_state_thread->state_dec.mode_play) { case D_PLAY : ret = dec_play(sp_dec_object); if (ret == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_play'\n"); return FAILURE; } else if (ret == ENDING) { m_MSG("\t!!! END PLAY !!!\n");#ifdef FAST_TEST usleep(500000);#endif #ifdef FOR_DEMO signal.start_id = PTHREAD_INPUT; signal.m_value = SEARCH; if (pthread_send_signal(&signal, PTHREAD_SEARCH) == FALSE) { m_ERROR("pthread_dec.c:error: In function 'pthread_send_signal'\n"); } gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_STOP; break; #else gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_IDLE;#endif } /* for real time play at event playback */ if (sp_dec_object->flag_first) { sp_dec_object->global_count ++; } break; case D_STOP : dec_stop(sp_dec_object); exit_dec = TRUE; break; case D_FF : ret = dec_ff(sp_dec_object); if (ret == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_ff'\n"); return FAILURE; } else if (ret == ENDING) { m_MSG("\t!!! END FAST FORWARD !!!\n");#ifdef FAST_TEST usleep(500000);#endif #ifdef FOR_DEMO signal.start_id = PTHREAD_INPUT; signal.m_value = SEARCH; if (pthread_send_signal(&signal, PTHREAD_SEARCH) == FALSE) { m_ERROR("pthread_dec.c:error: In function 'pthread_send_signal'\n"); } gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_STOP; break; #else gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_IDLE;#endif } break; case D_SLOW : ret = dec_slow(sp_dec_object); if (ret == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_slow'\n"); return FAILURE; } else if (ret == ENDING) { m_MSG("\t!!! END SLOW !!!\n");#ifdef FAST_TEST usleep(500000);#endif #ifdef FOR_DEMO signal.start_id = PTHREAD_INPUT; signal.m_value = SEARCH; if (pthread_send_signal(&signal, PTHREAD_SEARCH) == FALSE) { m_ERROR("pthread_dec.c:error: In function 'pthread_send_signal'\n"); } gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_STOP; break; #else gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_IDLE;#endif } break; case D_REW : ret = dec_rew(sp_dec_object); if (ret == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_rew'\n");#if 1 usleep(500000); signal.start_id = PTHREAD_INPUT; signal.m_value = SEARCH; if (pthread_send_signal(&signal, PTHREAD_SEARCH) == FALSE) { m_ERROR("pthread_dec.c:error: In function 'pthread_send_signal'\n"); } gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_STOP; break; #else return FAILURE;#endif } else if (ret == ENDING) { m_MSG("\t!!! END REWIND !!!\n");#ifdef FAST_TEST usleep(500000);#endif #ifdef FOR_DEMO signal.start_id = PTHREAD_INPUT; signal.m_value = SEARCH; if (pthread_send_signal(&signal, PTHREAD_SEARCH) == FALSE) { m_ERROR("pthread_dec.c:error: In function 'pthread_send_signal'\n"); } gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_STOP; break; #else gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_IDLE;#endif } break; case D_STEP_F : ret = dec_step_forward(sp_dec_object); if (ret == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_step_forward'\n"); return FAILURE; } else if (ret == ENDING) { m_MSG("\t!!! END PLAY !!!\n");#ifdef FAST_TEST usleep(500000);#endif #ifdef FOR_DEMO signal.start_id = PTHREAD_INPUT; signal.m_value = SEARCH; if (pthread_send_signal(&signal, PTHREAD_SEARCH) == FALSE) { m_ERROR("pthread_dec.c:error: In function 'pthread_send_signal'\n"); } gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_STOP; break; #else gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_IDLE;#endif } gp_state_thread->state_dec.mode_play = D_IDLE; break; case D_STEP_B : ret = dec_step_backward(sp_dec_object); if (ret == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_step_backward'\n"); return FAILURE; } else if (ret == ENDING) { m_MSG("\t!!! END REWIND !!!\n");#ifdef FAST_TEST usleep(500000);#endif #ifdef FOR_DEMO signal.start_id = PTHREAD_INPUT; signal.m_value = SEARCH; if (pthread_send_signal(&signal, PTHREAD_SEARCH) == FALSE) { m_ERROR("pthread_dec.c:error: In function 'pthread_send_signal'\n"); } gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_STOP; break; #else gp_state_thread->state_dec.state = ALIVE; gp_state_thread->state_dec.mode_play = D_IDLE;#endif } gp_state_thread->state_dec.mode_play = D_IDLE; break; case D_PAUSE : ret = dec_pause(sp_dec_object); if (ret == FAILURE) { m_ERROR("pthread_dec.c:error In function 'dec_pause'\n"); return FAILURE; } gp_state_thread->state_dec.mode_play = D_IDLE; break; case D_IDLE : usleep(100000); break; default : break; }; } while (!exit_dec); /* free pthread decoder object */ free(sp_dec_object->p_demux_buf); /* free pthread encoder object */ free(sp_dec_object); gp_state_thread->state_dec.state = IDLE;#if 0//org code /* change live audio to previous live state */ set_live_audio_ch(0);#endif#if 1//test code /* bug: The signal message is lost, may be mutex */ usleep(500);#endif /* change mode to Live Monitoring */ signal.start_id = PTHREAD_DEC; signal.m_value = 2; if (pthread_send_signal(&signal, PTHREAD_LIVE) == FALSE) { m_ERROR("main.c:error: In function 'pthread_send_signal'\n"); } m_MSG("\t!!!! pthread DECODE STOP!!!!\n"); return 0;}RETURN open_play_file(DEC_OBJECT *pdo){ S8 str_tmp[40]; FILE *fd; size_t size; STATE_DEC *psd; STATE_DB *psdb; psd = &gp_state_thread->state_dec; psdb = &gp_state_thread->state_db; /* LOCK "MAIN_DB" file */ sem_wait(&psdb->search_sem); /* 1st. open DB file */ if ( !(fd = fopen(MAIN_DB_NAME, "rb")) ) { m_ERROR("pthread_dec.c:error In function 'fopen'\n"); return FAILURE; } /* 2nd. DB file pointer move to present play file */ fseek(fd, psd->signal.m_value, SEEK_SET); fread(&pdo->db_rec, sizeof(UNS8), sizeof(DB_RECORD), fd); /* 3rd. close DB file */ fclose(fd); /* UNLOCK "MAIN_DB" file */ sem_post(&psdb->search_sem); /* 4th. open play file stream */ if ( (pdo->p_fd_ps = fopen(pdo->db_rec.fname, "rb")) < 0 ) { m_ERROR("pthread_dec.c:error In function 'open'\n"); return FAILURE; } m_MSG("\t!!! OPEN: %s !!!\n", pdo->db_rec.fname); /* for display current dec filename */ strcpy(psd->play_file, pdo->db_rec.fname); /* 5th. searching file end pointer */ fseek(pdo->p_fd_ps, 0, SEEK_END); pdo->end_fp = ftell(pdo->p_fd_ps); /* 6th. restore file pointer */ fseek(pdo->p_fd_ps, 0, SEEK_SET); /* 7th. read pack header from file stream */ size = fread(&pdo->tph, sizeof(S8), sizeof(PACK_HEADER), pdo->p_fd_ps); fflush(pdo->p_fd_ps); if (size != sizeof(PACK_HEADER)) { m_ERROR("pthread_dec.c:error In function 'fread'\n"); return FAILURE; } /* 8th. read program stream map from file stream */ size = fread(&pdo->tpsm, sizeof(S8), sizeof(PROGRAM_STREAM_MAP), pdo->p_fd_ps); fflush(pdo->p_fd_ps); if (size != sizeof(PROGRAM_STREAM_MAP)) { m_ERROR("pthread_dec.c:error In function 'fread'\n"); return FAILURE; } /* 9th. read PES packet from file stream */ size = fread(&pdo->tpp, sizeof(S8), sizeof(PES_PACKET), pdo->p_fd_ps); fflush(pdo->p_fd_ps); if (size != sizeof(PES_PACKET)) { m_ERROR("pthread_dec.c:error In function 'fread'\n"); return FAILURE; } /* 10th. search file pointer of start picture */ fseek(pdo->p_fd_ps, ~ sizeof(PES_PACKET) + 1, SEEK_CUR); return SUCCESS;}RETURN dec_set_param(DEC_OBJECT *pdo){ UNS16 ii, ch, type; UNS16 ef_voffset; UNS16 of_voffset; UNS16 hsync_start_position; UNS16 hsync_end_position; UNS16 hsync_to_hvalid_end_cycle; UNS16 start_line_number_ff; UNS16 end_line_number_ff; UNS16 transition_position_ff; UNS16 start_line_number_sf; UNS16 end_line_number_sf; UNS16 transition_position_sf; /* reset buffer for decoding */ video_decoder_buffer_reset(); pdo->pdp = &gp_setup_param->dec; /* format : '0' NTSC, '1' PAL, default is '0' */ output_video_format(gp_setup_param->gp.video_form); if (gp_setup_param->gp.video_form == NTSC) { ef_voffset = 284; of_voffset = 21; hsync_start_position = 28; hsync_end_position = 159; hsync_to_hvalid_end_cycle = 1440; start_line_number_ff = 3; end_line_number_ff = 7; transition_position_ff = 90;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -