void *sig_main(void *arg) { int signo; sigset_t waitset; CAP_DBG("sig thread start...\n"); // signals can be handled sigemptyset(&waitset); sigaddset(&waitset, SIGINT); sigaddset(&waitset, SIGTERM); for(; ;) { if(sigwait(&waitset, &signo) != 0) err_sys("sigwait error"); switch(signo) { case SIGINT: case SIGTERM: CAP_DBG("get signal: %d, process exiting...\n", signo); exit(1); default: CAP_DBG("get signal, ignore: %d\n", signo); break; } } }
void encode_mmap() { int fd; int mmaplen; // handle share memory if(shm_unlink(Px_ipc_name(MMAP_SHM_NAME)) == -1) { CAP_DBG("shm_unlink error: %s", strerror(errno)); } mmaplen = sizeof(mmap_ring_t) + MMAP_NODE_NUM * sizeof(mmap_node_t); fd = Shm_open(Px_ipc_name(MMAP_SHM_NAME), O_RDWR | O_CREAT | O_EXCL, FILE_MODE); CAP_DBG("shm_open create successfully\n"); Ftruncate(fd, mmaplen); h264_buf = Mmap(NULL, mmaplen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); // initialize mmap memory after create memset(h264_buf, 0, mmaplen); x264_ring = (mmap_ring_t *)h264_buf; shm_init_mutex(); shm_init_cond(); x264_node = (mmap_node_t *)(h264_buf + sizeof(mmap_ring_t)); Close(fd); }
void encode_spspps() { int i = 0; int i_nal = 0; x264_nal_t* p_nal = NULL; if (x264_encoder_headers(x264_encode.handle, &p_nal, &i_nal) < 0) CAP_DBG_EXIT("x264_encoder_headers error!\n"); for (i = 0; i < i_nal; i++) { if (p_nal[i].i_type == 7) { //sps spslen = p_nal[i].i_payload - 4; memcpy(sps, p_nal[i].p_payload + 4, spslen); } else if (p_nal[i].i_type == 8) { //pps ppslen = p_nal[i].i_payload - 4; memcpy(pps, p_nal[i].p_payload + 4, ppslen); } } // ricann debug CAP_DBG("sps = %s, len = %d\n", sps, spslen); CAP_DBG("pps = %s, len = %d\n", sps, ppslen); // ricann todo //write(fd_write, sps, spslen); //write(fd_write, pps, ppslen); }
void DummyCapturer::CaptureFrame(VideoData & frame) { uint32_t sleepTimeUs; m_TStat.Update(); TIMING_DBG("V4l2Capturer: %s", m_TStat.GetStatStr()); uint8_t *frameptr = (uint8_t *)frame.Data(); curr_Timestamp += 1000 / m_VPar.framerate; frame.m_Timestamp = curr_Timestamp; for (int i = 0; i < m_VPar.width * m_VPar.height; i++) { *frameptr = 0x13 + (int)(5 * ((float)rand()/RAND_MAX)); frameptr++; } for (int i = 0; i < m_VPar.width * m_VPar.height / 2; i++) { *frameptr = 0x80 + (int)(5 * ((float)rand()/RAND_MAX)); frameptr++; } m_pPreviewRenderer->RenderFrame(frame); sleepTimeUs = (uint32_t)(1 / (float)m_VPar.framerate * 1000000); CAP_DBG("usleep %d us", sleepTimeUs); usleep(sleepTimeUs); }
// ricann todo, support other pixel format int encode_frame(uint8_t *in, mmap_node_t *node, int *keyframe) { int i = 0; int n_nal = -1; int result = 0; uint8_t *p_out = node->buf; x264_picture_t pic_out; if(capg.pixelfmt == V4L2_PIX_FMT_YUYV) encode_yuyv(in); else if(capg.pixelfmt == V4L2_PIX_FMT_YUV420) encode_yuv420(in); else CAP_DBG_EXIT("pixelfmt not support\n"); x264_encode.pic->i_pts++; if(x264_encoder_encode(x264_encode.handle, &x264_encode.nal, &n_nal, x264_encode.pic, &pic_out) < 0) CAP_DBG_EXIT("x264_encoder_encode error!\n"); *keyframe = pic_out.i_type==X264_TYPE_IDR; for(i = 0; i < n_nal; i++ ) { memcpy(p_out, x264_encode.nal[i].p_payload, x264_encode.nal[i].i_payload); p_out += x264_encode.nal[i].i_payload; result += x264_encode.nal[i].i_payload; } node->length = result; // ricann debug CAP_DBG("frame len: %d, key frame: %d\n", result, *keyframe); return result; }
// YU YV YU YV void encode_yuyv(uint8_t *in) { int n; int i, j, k; uint8_t *y = x264_encode.pic->img.plane[0]; uint8_t *u = x264_encode.pic->img.plane[1]; uint8_t *v = x264_encode.pic->img.plane[2]; // get Y i = 0; j = 0; k = 0; for(n=0; n<capg.frame_len; n++) { if(n%2 == 0) { y[i++] = in[n]; continue; } if(n%4 == 1) { u[j++] = in[n]; continue; } if(n%4 == 3) { v[k++] = in[n]; continue; } } CAP_DBG("n = %d, i = %d, j = %d, k = %d\n", n, i, j, k); }
void encode_init() { // fill x264_param_t with default values and do CPU detection x264_param_default(x264_encode.para); if(x264_param_default_preset(x264_encode.para, capg.preset, capg.tune) < 0) CAP_DBG_EXIT("x264_param_default_preset error!\n"); // ricann debug CAP_DBG("-------------------after x264_param_default_preset\n"); encode_printpara(); // Configure non-default params // real frame rate is i_fps_num/i_fps_den x264_encode.para->i_fps_num = capg.frame_rate; x264_encode.para->i_width = capg.width; x264_encode.para->i_height = capg.height; x264_encode.para->i_keyint_max = capg.gop_size; x264_encode.para->i_csp = x264_encode.colorspace; x264_encode.para->b_vfr_input = 0; x264_encode.para->b_repeat_headers = 1; // ricann debug CAP_DBG("-------------------after assign value\n"); encode_printpara(); x264_encode.pic->img.i_csp = x264_encode.colorspace; x264_encode.pic->img.i_plane = 3; x264_encode.pic->i_type = X264_TYPE_AUTO; if(x264_param_apply_profile(x264_encode.para, capg.profile) < 0) CAP_DBG_EXIT("x264_param_apply_profile error!\n"); x264_encode.handle = x264_encoder_open(x264_encode.para); if(!x264_encode.handle) CAP_DBG_EXIT("x264_encoder_open error!\n"); // ricann debug CAP_DBG("-------------------after x264_encoder_open\n"); encode_printpara(); }
void encode_printpara() { CAP_DBG("x264_encode.para->i_fps_den = %d\n", x264_encode.para->i_fps_den); CAP_DBG("x264_encode.para->i_fps_num = %d\n", x264_encode.para->i_fps_num); CAP_DBG("x264_encode.para->i_width = %d\n", x264_encode.para->i_width); CAP_DBG("x264_encode.para->i_height = %d\n", x264_encode.para->i_height); CAP_DBG("x264_encode.para->i_threads = %d\n", x264_encode.para->i_threads); CAP_DBG("x264_encode.para->i_keyint_max = %d\n", x264_encode.para->i_keyint_max); CAP_DBG("x264_encode.para->b_intra_refresh = %d\n", x264_encode.para->b_intra_refresh); CAP_DBG("x264_encode.para->b_annexb = %d\n", x264_encode.para->b_annexb); CAP_DBG("x264_encode.para->i_csp = %d\n", x264_encode.para->i_csp); }