当前位置:网站首页>Pybind pass pointer

Pybind pass pointer

2020-12-08 08:04:05 ShellCollector

code h264 You can refer to :https://blog.csdn.net/jacke121/article/details/87484745

python part :

Receive pointer first vp, Call again , Yes. .

#-*- coding:utf-8 -*-
import binddemo
b= binddemo.add(3, 4)
print(b)


filepath="0217.h264"

vp= binddemo.pre_save(filepath, 1280, 720, 5)
import os
import cv2
import numpy as np
import time

from PIL import Image #PIL pakage name is Pillow




path=r"D:\data\VOC2007\JPEGImages/"

files=os.listdir(path)

for i,file in enumerate(files):
    img=cv2.imread(path+file)
    if img is None:
        continue
    # im = Image.open(path+file)
    #
    # in_data = np.asarray(im, dtype=np.uint8)
    #
    # in_data = np.rollaxis(in_data, 2, 0)
    time1=time.time()
    binddemo.save_frame(img,i,vp)
    # example.GetImage(img)

    print(i,'time',time.time()-time1,flush=True)
    time.sleep(0.02)

c++ part :

pre_save Return pointer

save_frame Receive pointer

void* pre_save(char* filepath, int width, int height, int frame_rate) {
	//void* pre_save(std::string & filepath, int width, int height, int frame_rate) {

	av_log_set_level(AV_LOG_ERROR);
	Rtmp_tool *rtmp_tool;
	rtmp_tool = new Rtmp_tool();
	int nLen;
	int fileI;
	rtmp_tool->nWidth = width;
	rtmp_tool->nHeight = height;

	AVCodecContext *c = NULL;
	AVCodecContext *in_c = NULL;
	AVCodec *pCodecH264; // Encoder     

	pCodecH264 = avcodec_find_encoder(AV_CODEC_ID_H264);// lookup h264 Encoder     
	c = avcodec_alloc_context3(pCodecH264);
	if (!c)
	{
		printf("Could not allocate video codec context\n");
		exit(1);
	}
	//av_opt_set(c->priv_data, "preset", "slow", 0);
	av_opt_set(c->priv_data, "preset", "superfast", 0);
	av_opt_set(c->priv_data, "tune", "zerolatency", 0);
	c->bit_rate = 1024 * 1024;// put sample parameters     
	c->width = width;
	c->height = height;

	c->time_base.den = frame_rate;// Video frames per second 
	c->time_base.num = 1;
	c->framerate.den = 1;// Video frame rate 
	c->framerate.num = frame_rate;
	c->gop_size = 20; // emit one intra frame every ten frames     
	c->max_b_frames = 0;
	c->thread_count = 1;
	c->pix_fmt = AV_PIX_FMT_YUV420P;//PIX_FMT_RGB24; 
									// Turn on the encoder     
	if (avcodec_open2(c, pCodecH264, NULL)<0)
		printf(" Can't open codebase ");


	int size = c->width * c->height;


	rtmp_tool->yuv_buff = (uint8_t *)malloc((size * 3) / 2); // size for YUV 420     


	rtmp_tool->f = fopen(filepath, "wb");
	//rtmp_tool->f = fopen(filepath.data(), "wb");
	if (!rtmp_tool->f)
	{
		printf("could not open %s\n", filepath);
		exit(1);
	}

	printf("context3 w h %d %d\n", c->width, c->height);

	AVFrame *frame = av_frame_alloc();
	if (!frame) {
		fprintf(stderr, "Could not allocate video frame\n");
		exit(1);
	}
	frame->format = c->pix_fmt;
	frame->width = c->width;
	frame->height = c->height;
	int ret = av_frame_get_buffer(frame, 32);
	if (ret < 0)
	{
		printf("Could not allocate the video frame data\n");
		exit(1);
	}

	// Initialize the format converter SwsContext  
	rtmp_tool->scxt = sws_getContext(c->width, c->height, AV_PIX_FMT_BGR24, c->width, c->height, AV_PIX_FMT_YUV420P, SWS_POINT, NULL, NULL, NULL);

	rtmp_tool->m_pYUVFrame = frame;
	rtmp_tool->c = c;

	return rtmp_tool;
}
int save_frame(py::array_t<uint8_t> input1, int len, void* vp) {//, void* service) {
	Rtmp_tool *rtmp_tool = (Rtmp_tool *)vp;

 

版权声明
本文为[ShellCollector]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208065759584r.html