ffmpeg 从内存中读取数据(或将数据输出到内存)

原文见雷大神博客:http://blog.csdn.net/leixiaohua1020/article/details/12980423

更新记录(2014.7.24):

1.为了使本文更通俗易懂,更新了部分内容,将例子改为从内存中打开。

2.增加了将数据输出到内存的方法。


从内存中读取数据

ffmpeg一般情况下支持打开一个本地文件,例如“C:\test.avi”

或者是一个流媒体协议的URL,例如“rtmp://222.31.64.208/vod/test.flv”

其打开文件的函数是avformat_open_input(),直接将文件路径或者流媒体URL的字符串传递给该函数就可以了。

但其是否支持从内存中读取数据呢?这个问题困扰了我很长时间。当时在做项目的时候,通过Winpcap抓取网络上的RTP包,打算直接送给ffmpeg进行解码。一直没能找到合适的方法。因为抓取的数据包是存在内存中的,所以无法传递给avformat_open_input()函数其路径(根本没有路径= =)。当然也可以将抓取的数据报存成文件,然后用ffmpeg打开这个文件,但是这样的话,程序的就太难控制了。

后来经过分析ffmpeg的源代码,发现其竟然是可以从内存中读取数据的,代码很简单,如下所示:

[cpp]  view plain  copy
  1. AVFormatContext *ic = NULL;  
  2. ic = avformat_alloc_context();  

[cpp]  view plain  copy
  1. unsigned char * iobuffer=(unsigned char *)av_malloc(32768);  
  2. AVIOContext *avio =avio_alloc_context(iobuffer, 32768,0,NULL,fill_iobuffer,NULL,NULL);  
  3. ic->pb=avio;  
  4. err = avformat_open_input(&ic, "nothing", NULL, NULL);  

关键要在avformat_open_input()之前初始化一个AVIOContext, 而且将原本的AVFormatContext的指针pb(AVIOContext类型)指向这个自行初始化AVIOContext。当自行指定了AVIOContext之后,avformat_open_input()里面的URL参数就不起作用了。示例代码开辟了一块空间iobuffer作为AVIOContext的缓存。

fill_iobuffer则是将数据读取至iobuffer的回调函数。fill_iobuffer()形式(参数,返回值)是固定的,是一个回调函数,如下所示(只是个例子,具体怎么读取数据可以自行设计)。示例中回调函数将文件中的内容通过fread()读入内存。

[cpp]  view plain  copy
  1. //读取数据的回调函数-------------------------  
  2. //AVIOContext使用的回调函数!  
  3. //注意:返回值是读取的字节数  
  4. //手动初始化AVIOContext只需要两个东西:内容来源的buffer,和读取这个Buffer到FFmpeg中的函数  
  5. //回调函数,功能就是:把buf_size字节数据送入buf即可  
  6. //第一个参数(void *opaque)一般情况下可以不用  
  7. int fill_iobuffer(void * opaque,uint8_t *buf, int bufsize){  
  8.     if(!feof(fp_open)){  
  9.         int true_size=fread(buf,1,buf_size,fp_open);  
  10.         return true_size;  
  11.     }else{  
  12.         return -1;  
  13.     }  
  14. }  


整体结构大致如下:

[cpp]  view plain  copy
  1. FILE *fp_open;  
  2.   
  3. int fill_iobuffer(void *opaque, uint8_t *buf, int buf_size){  
  4. ...  
  5. }  
  6.   
  7. int main(){  
  8.     ...  
  9.     fp_open=fopen("test.h264","rb+");  
  10.     AVFormatContext *ic = NULL;  
  11.     ic = avformat_alloc_context();  
  12.     unsigned char * iobuffer=(unsigned char *)av_malloc(32768);  
  13.     AVIOContext *avio =avio_alloc_context(iobuffer, 32768,0,NULL,fill_iobuffer,NULL,NULL);  
  14.     ic->pb=avio;  
  15.     err = avformat_open_input(&ic, "nothing", NULL, NULL);  
  16.     ...//解码  
  17. }  


将数据输出到内存

和从内存中读取数据类似,ffmpeg也可以将处理后的数据输出到内存。

回调函数如下示例,可以将输出到内存的数据写入到文件中。

[cpp]  view plain  copy
  1. //写文件的回调函数  
  2. int write_buffer(void *opaque, uint8_t *buf, int buf_size){  
  3.     if(!feof(fp_write)){  
  4.         int true_size=fwrite(buf,1,buf_size,fp_write);  
  5.         return true_size;  
  6.     }else{  
  7.         return -1;  
  8.     }  
  9. }  

主函数如下所示。

[cpp]  view plain  copy
  1. FILE *fp_write;  
  2.   
  3. int write_buffer(void *opaque, uint8_t *buf, int buf_size){  
  4. ...  
  5. }  
  6.   
  7. main(){  
  8.     ...  
  9.     fp_write=fopen("src01.h264","wb+"); //输出文件  
  10.     ...  
  11.     AVFormatContext* ofmt_ctx=NULL;  
  12.     avformat_alloc_output_context2(&ofmt_ctx, NULL, "h264", NULL);  
  13.     unsigned char* outbuffer=(unsigned char*)av_malloc(32768);  
  14.   
  15.     AVIOContext *avio_out =avio_alloc_context(outbuffer, 32768,0,NULL,NULL,write_buffer,NULL);    
  16.   
  17.     ofmt_ctx->pb=avio_out;   
  18.     ofmt_ctx->flags=AVFMT_FLAG_CUSTOM_IO;  
  19.     ...  
  20. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用CUDA将摄像头读取的数据编码成H264流的示例代码,使用了NVIDIA Video Codec SDK和OpenCV库: ```cpp #include <iostream> #include <opencv2/opencv.hpp> #include "NvEncoder/NvEncoderCuda.h" using namespace std; using namespace cv; int main(int argc, char* argv[]) { // 从摄像头读取数据 VideoCapture cap(0); if (!cap.isOpened()) { cout << "Failed to open camera!" << endl; return -1; } // 设置编码器参数 int nWidth = 640, nHeight = 480; int nBitrate = 1000000; int nFps = 30; int nGopSize = 30; int nMaxConcurrentSessions = 1; int nCodec = NV_ENC_H264; std::string sPreset = "hq"; std::string sProfile = "high"; NvEncoderInitParam encodeParams = { 0 }; encodeParams.width = nWidth; encodeParams.height = nHeight; encodeParams.bitrate = nBitrate; encodeParams.fps = nFps; encodeParams.gopSize = nGopSize; encodeParams.codec = nCodec; encodeParams.preset = const_cast<char*>(sPreset.c_str()); encodeParams.profile = const_cast<char*>(sProfile.c_str()); encodeParams.maxConcurrentSessions = nMaxConcurrentSessions; NvEncoderCuda enc(encodeParams); // 分配编码器缓冲区 int nFrameSize = enc.GetFrameSize(); uint8_t* pFrame = new uint8_t[nFrameSize]; uint8_t* pBitstream = new uint8_t[nFrameSize]; // 编码并输出h264流 Mat frame; while (true) { // 读取一帧图像 cap >> frame; if (frame.empty()) { break; } // 将帧数据复制到CUDA缓冲区 uint8_t* dpFrame = NULL; int nPitch = 0; enc.GetDeviceFrameBuffer(&dpFrame, &nPitch); cudaMemcpy2D(dpFrame, nPitch, frame.data, frame.step, nWidth * 3, nHeight, cudaMemcpyHostToDevice); // 编码一帧图像 int nBytes = 0; enc.EncodeFrame(pFrame, &nBytes, dpFrame); // 将编码后的数据复制回主机内存 cudaMemcpy(pBitstream, enc.GetBitstreamBuffer(), nBytes, cudaMemcpyDeviceToHost); // 输出h264流 fwrite(pBitstream, sizeof(uint8_t), nBytes, stdout); } // 释放资源 enc.DestroyEncoder(); delete[] pFrame; delete[] pBitstream; return 0; } ``` 在编译时需要链接NVIDIA Video Codec SDK和OpenCV库。执行程序后,它将从摄像头读取数据并将其编码为H264流输出到标准输出。你可以将输出重定向到文件,例如: ``` ./encode | ffmpeg -i - output.mp4 ``` 这将从标准输入读取H264流,并将其转换为MP4文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值