ffmpeg从内存中读取数据错误(在做一个RTMP视频服务器,需要将内存中的包直接转码)

sha_dlx 2017-04-25 02:43:55
在av_format_open_input()打开数据时,多次调用回调函数read_buffer()直至内存溢出

#include "Decode.h"     
#include <iostream>
#include <winerror.h>

using namespace std;

namespace Transcode
{
Decode::Decode()
{
avio_in = NULL;
inbuffer = NULL;
packet = NULL;
frame = NULL;
dec_ctx = NULL;
piFmt = NULL;
av_register_all(); //注册所有编解码器,复用器和解复用器
ifmt_ctx = avformat_alloc_context();
}

int read_buffer(void *opaque, uint8_t *buf, int buf_size)
{
//int ret = -1;
if (opaque != NULL)
{
av_log(NULL,AV_LOG_INFO,"buf_size:%i\n",buf_size);
memcpy(buf, opaque, buf_size);
opaque = NULL;
return buf_size;
}
return -1;
}


int Decode::decode(int buf_size, const uint8_t *packet)
{
int ret;
int i = 0;
inbuffer = (unsigned char*)av_malloc(buf_size); //为输入缓冲区间分配内存

uint8_t *decodeBuffer = (uint8_t*)av_malloc(buf_size);
memcpy(decodeBuffer, packet, buf_size);
/*open input file*/
av_log(NULL, AV_LOG_INFO, "buf_size1:%i\n", buf_size);
avio_in = avio_alloc_context(inbuffer, buf_size, 0, decodeBuffer, read_buffer, NULL, NULL);

if (avio_in == NULL)
return 0;
ifmt_ctx->pb = avio_in;

// Determine the input-format:

av_probe_input_buffer(avio_in, &piFmt, "", NULL, 0, 0); //探测流格式
ifmt_ctx->flags = AVFMT_FLAG_CUSTOM_IO; //通过标志位说明采用自定义AVIOContext
//此处报内存错误
if ((ret = avformat_open_input(&ifmt_ctx, "whatever", NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
printf("***nb_stream%d \n", ifmt_ctx->nb_streams);
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
AVStream *stream;
AVCodecContext *codec_ctx;
stream = ifmt_ctx->streams[i];
codec_ctx = stream->codec;
INFO("nb_stream:",i)
/* Reencode video & audio and remux subtitles etc. */
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO){ /
printf("video stream\n");
/* Open decoder */
ret = avcodec_open2(codec_ctx,
avcodec_find_decoder(codec_ctx->codec_id), NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
return ret;
}
}
if (codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO)
INFO("audio stream:")
}
return 1;
}


...全文
503 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,542

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧