pybind 编码264

#include <pybind11/pybind11.h>#include <pybind11/numpy.h>#include <iostream>using namespace std;#include<windows.h>
#include <io.h>#include <fcntl.h>#ifdef __cplusplus
extern "C"
{
#endif
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h> #include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#ifdef __cplusplus
};
#endif
namespace py = pybind11;int add(int i, int j)
{return i + j+2000;
}// display consolevoid OpenConsole(){// create a consoleAllocConsole();// FreeConsole();}void GetImage(py::array_t<char> input1){py::buffer_info buf1 = input1.request();OpenConsole();printf("dim len:%d ", buf1.ndim);for (int cnt = 0; cnt < buf1.ndim; cnt++){printf("dim size %d : %d ", cnt, buf1.shape[cnt]);}unsigned char *ptr1 = (unsigned char *)buf1.ptr;printf("image data: %d %d %d %d %d %d\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3], ptr1[4], ptr1[5]);}
#include <time.h>int gettimeofday(struct timeval *tp, void *tzp)
{time_t clock;struct tm tm;SYSTEMTIME wtm;GetLocalTime(&wtm);tm.tm_year = wtm.wYear - 1900;tm.tm_mon = wtm.wMonth - 1;tm.tm_mday = wtm.wDay;tm.tm_hour = wtm.wHour;tm.tm_min = wtm.wMinute;tm.tm_sec = wtm.wSecond;tm.tm_isdst = -1;clock = mktime(&tm);tp->tv_sec = clock;tp->tv_usec = wtm.wMilliseconds * 1000;return (0);
}
/*
This device demo will connect to url as a channel, the chnum is the channel of this device,
this device will connect chnum times to the url.
*/
class Rtmp_tool {
public:int nWidth = 0;int nHeight = 0;int pts_value = 0;AVCodecContext *c;AVFrame *m_pRGBFrame = new AVFrame[1];  //RGB帧数据      AVFrame *m_pYUVFrame;  //YUV帧数据   uint8_t * yuv_buff;//  SwsContext * scxt;FILE *f = NULL;AVFormatContext  *ofmt_ctx = NULL;};
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; //编码器    pCodecH264 = avcodec_find_encoder(AV_CODEC_ID_H264);//查找h264编码器    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;//视频每秒帧数c->time_base.num = 1;c->framerate.den = 1;//视频帧速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; //打开编码器    if (avcodec_open2(c, pCodecH264, NULL)<0)printf("不能打开编码库");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);}//初始化格式转换器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;//DemoDevice *pDev = (DemoDevice *)service;av_log_set_level(AV_LOG_WARNING);py::buffer_info buf1 = input1.request();/*for (int i = 0; i < len; i++) {plus1[i] = (uint8_t)plus1[i];}*/unsigned char *ptr1 = (unsigned char *)buf1.ptr;AVCodecContext *c = rtmp_tool->c;// (AVCodecContext*)vp;AVPacket *avpkt;AVFrame *m_pRGBFrame = rtmp_tool->m_pRGBFrame;AVFrame *m_pYUVFrame = rtmp_tool->m_pYUVFrame;av_image_fill_arrays(m_pRGBFrame->data, m_pRGBFrame->linesize, (uint8_t*)ptr1, AV_PIX_FMT_RGB24, rtmp_tool->nWidth, rtmp_tool->nHeight, 1);m_pRGBFrame->width = rtmp_tool->nWidth;m_pRGBFrame->height = rtmp_tool->nHeight;av_image_fill_arrays(m_pYUVFrame->data, m_pYUVFrame->linesize, (uint8_t*)rtmp_tool->yuv_buff, AV_PIX_FMT_YUV420P, c->width, c->height, 1);int ret = av_frame_make_writable(m_pYUVFrame);//检测帧对象是否可读if (ret < 0) {printf("av_frame_make_writable %d\n", ret);exit(1);}sws_scale(rtmp_tool->scxt, (uint8_t * const *)m_pRGBFrame->data, m_pRGBFrame->linesize, 0, c->height, m_pYUVFrame->data, m_pYUVFrame->linesize);//m_pYUVFrame->pts++;//将RGB转化为YUV    //sws_scale(rtmp_tool->scxt, m_pRGBFrame->data, m_pRGBFrame->linesize, 0, c->height, m_pYUVFrame->data, m_pYUVFrame->linesize);int got_packet_ptr = 0;avpkt = av_packet_alloc();int send_ret = avcodec_send_frame(c, m_pYUVFrame);m_pYUVFrame->pts++;//printf("avcodec_send_frame %d\n", send_ret);if (ret < 0){printf("Error sending a frame for encoding\n");exit(1);}while (ret >= 0){ret = avcodec_receive_packet(c, avpkt);if (ret != 0) {if (ret == -11 || ret == AVERROR(EAGAIN)) {//-11//printf("avcodec_receive_packet AVERROR(EAGAIN)\n");}else if (ret == AVERROR(EINVAL)) {//-22printf("avcodec_receive_packet AVERROR(EINVAL)\n");}else {printf("avcodec_receive_packet error %d\n", ret);}return ret;}//printf("Write packet %3(size=%5d)\n", avpkt->pts, avpkt->size);fwrite(avpkt->data, 1, avpkt->size, rtmp_tool->f);av_packet_unref(avpkt);}return 0;
}PYBIND11_MODULE(example, m)
{// optional module docstringm.doc() = "pybind11 example plugin";// expose add function, and add keyword arguments and default argumentsm.def("add", &add, "A function which adds two numbers", py::arg("i") = 1, py::arg("j") = 2);// exporting variablesm.attr("the_answer") = 42;py::object world = py::cast("World");m.attr("what") = world;m.def("GetImage", &GetImage, "Get NumPy arrays");m.def("pre_save", &pre_save, "pre_save");m.def("save_frame", &save_frame, "pre_save");}

pybind 编码h264相关推荐

  1. 音视频开发(29)---深入浅出理解视频编码H264结构

    深入浅出理解视频编码H264结构 编码流程: 那么 H.264 其编解码流程是怎么样的呢?其实可以主要分为 5 部分: 帧间和帧内预测(Estimation).变换(Transform)和反变换.量化 ...

  2. YUV通过MediaCodec编码H264

    Camear_MainActivity.java,主界面类 import android.app.Activity; import android.graphics.ImageFormat; impo ...

  3. MediaCodec 编码H264 编码一帧后dequeueOutputBuffer为-1的问题

    在用android MediaCodec编码h264的时候,会遇到,dequeueOutputBuffer在成功获取到config帧(sps pps)及第一个I帧后,dequeueOutputBuff ...

  4. Android视频编码--H264编码

    Android视频编码–H264编码 Android中的H264编码有两种编码方式: 硬编码 软编码 1.硬编码 Android中的H264硬编码主要是通过自身提供的API,调用底层的硬件模块实现编码 ...

  5. EasyPusher安卓Android手机直播推送之MediaCodec 硬编码H264格式

    EasyPusher安卓Android手机直播推送之MediaCodec 硬编码H264格式 最近在研究EasyDarwin的Push库EasyPusher,EasyPusher可以推送H264视频到 ...

  6. 文件编码H264编解码器性能测试

    最近应用开发的过程当中出现了一个小问题,顺便记载一下原因和方法--文件编码 H264编码器性能测试 如今,H264已经成为视频行业的标准规范,企业中应用比较多的当数X264,开源,资源消耗又比较少. ...

  7. ffmpeg + x264 编码H264(x86+arm)

    一.x86-linux 1.  编译x264 我用的是目前最新版:x264-snapshot-20101130-2245.tar.bz2 ./configure --disable-asm mak ...

  8. X264_最简单的视频编码实现(YUV420编码H264)

    下面这个例子是X264项目中的example.c文件实现编码的实现.只是加了一些注释方便以后的理解. #ifdef _WIN32 #include <io.h> /* _setmode() ...

  9. Android MediaCodec硬编码H264文件(四)

    在 Android 4.1 版本提供了 MediaCodec 接口来访问设备的编解码器,不同于 FFmpeg 的软件编解码,它采用的是硬件编解码能力,因此在速度上会比软解更具有优势,但是由于 Andr ...

最新文章

  1. 结构感知图像修复:ICCV2019论文解析
  2. Socket java.net.SocketException: Connection reset的解决方案
  3. wpf mvvm 实例
  4. 项目开发(Require + E.js)
  5. PyG图神经网络框架--构建信息传递网络(MPN)
  6. java新手笔记3 运算符循环
  7. 第十二篇 Android 系统电话管理机制一--工厂模式和代理模式
  8. 实践 | kafka 基本使用
  9. 当MySQL执行XA事务时遭遇崩溃,且看华为云如何保障数据一致性
  10. 多媒体个人计算机软件系统,HP推出新的多媒体个人计算机
  11. excel多列多行堆叠成多列一行_「Excel技巧」如何利用indirect函数快速将一列转为多行多列排版...
  12. 浪漫桃心的Android表白程序
  13. hping3进行SYN Flood攻击
  14. python存钱程序_Python基础(六) 52周存钱demo
  15. RS码(Reed-Solomon码)
  16. 《禅与摩托车维修艺术》(罗伯特·M·波西格)骑行路上的哲学思考 Zen and the Art of Motorcycle Mt...
  17. matlab响应曲面设计,分析响应曲面设计
  18. 开源四轴无人机系统迈入新的阶段!
  19. SnowField目标效果2-6:斜面移动2更真实的斜坡
  20. XBox360自制系统的更新(Update)

热门文章

  1. Android系统的架构
  2. ubuntu as86
  3. linux 5 防火墙,CentOS 5 Linux iptables防火墙的配置
  4. 算法 - 求一个正整数的二进制表示中1的个数(C++)
  5. Linux内核中max()宏的奥妙何在?(二)——大神Linus对这个宏怎么看?
  6. php收集radio表单,jQuery对于单选表单(radio)以及其它表单取值
  7. 运动控制器编程_快速入门 | 篇二十一:运动控制器ZHMI组态编程简介一
  8. java 导出word_Java 生成Word文档
  9. linux string split,linux下awk内置函数的使用(split/substr/length)
  10. oracle 并接去掉字符串,ORACLE删除字符-TRIM字符截取-substr查找字符-instr