ffmpeg 解码内存块 错误(多谢各位支招 在线等)

sinat_34153405 2016-05-15 12:38:20
一段视频使用常规调用可以正常解码,使用自定义avio的方式无法正常解码 各位大神请帮忙
#if 1 的流程是使用自定义avio的解码代码


struct buffer_data {
uint8_t *ptr;
size_t size; ///< size left in the buffer
};

static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
struct buffer_data *bd = (struct buffer_data *)opaque;
buf_size = FFMIN(buf_size, bd->size);

printf("ptr:%p size:%zu\n", bd->ptr, bd->size);

/* copy internal buffer data to buf */
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size;
bd->size -= buf_size;

return buf_size;
}


void * decode_thread(void *para)
{
AVCodecContext* video_dec_ctx = NULL;
AVCodec* video_dec = NULL;
AVPacket *pkt = NULL;
unsigned int framecnt = 0;
unsigned int decodecnt = 0;
struct timeval tv_begin, tv_end;
int index = *(int *)para;
int i,j,a;
int videoindex = -1;
char *src_file=g_src_files[index];
int height = 0;
int width = 0;
unsigned char * ch_pgm = NULL;
unsigned int outputsize = 0;
struct buffer_data bd = { 0 };
unsigned char * buffer = NULL;
size_t buffer_size = 0;

ch_pgm = (unsigned char *)malloc(MAX_PGM_SIZE);

printf("create decode thread. index=%d\n", index);
AVFormatContext* pFormat = NULL;

#if 1
unsigned char *avio_ctx_buffer = NULL;
AVIOContext *avio_ctx = NULL;
AVInputFormat* piFmt = NULL;
if (!(pFormat = avformat_alloc_context()))
{
printf("avformat_alloc_context failed\n");
return (void *)NULL;
}

avio_ctx_buffer = (unsigned char *)malloc(CTX_BUFFER_SIZE);
if(NULL ==avio_ctx_buffer)
{
printf("malloc avio_ctx_buffer failed\n");
return (void *)NULL;
}
memset(avio_ctx_buffer, 0, CTX_BUFFER_SIZE);

/* slurp file content into buffer */
if (av_file_map(src_file, &buffer, &buffer_size, 0, NULL) < 0)
{
printf("av_file_map failed\n");
return (void *)NULL;
}
/* fill opaque structure used by the AVIOContext read callback */
bd.ptr = buffer;
bd.size = buffer_size;

avio_ctx = avio_alloc_context(avio_ctx_buffer, CTX_BUFFER_SIZE,
0, &bd, &read_packet, NULL, NULL);
if (!avio_ctx)
{
printf("avio_alloc_context failed\n");
return (void *)NULL;
}
pFormat->pb = avio_ctx;

//if (av_probe_input_buffer(avio_ctx, &piFmt, "", NULL, 0, 0) < 0)
//{
// printf("probe format failed\n");
// return (void *)NULL;
//}
// else{
// printf("format:%s[%s]\n", piFmt->name, piFmt->long_name);
//}


//pFormat->probesize = 1024;
//pFormat->max_analyze_duration = 1
printf("flsg=0x%x \n"
, pFormat->flags);
//pFormat->flags |= AVFMT_FLAG_NONBLOCK;
if (avformat_open_input(&pFormat, , NULL, NULL) < 0)
{
printf("avformat_open_input failed\n");
return (void *)NULL;

}

printf("max_analyze_duration=%ld probesize=%ld\n"
, pFormat->max_analyze_duration
, pFormat->probesize);
#else
if (avformat_open_input(&pFormat, src_file, NULL, NULL) < 0)
{
printf("%s is not exit\n", src_file);
return (void *)NULL;
}
#endif
if (avformat_find_stream_info(pFormat, NULL) < 0)
{
printf("avformat_find_stream_info failed\n");
return (void *)NULL;
}
av_dump_format(pFormat,0,src_file,0);
gettimeofday(&tv_begin, NULL);
for (i=0; i < pFormat->nb_streams; i++)
{
if (pFormat->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoindex = i;
break;
}
}
if (videoindex == -1) {
printf("Don't find a video stream !\n");
return (void *)NULL;
}

video_dec_ctx = pFormat->streams[videoindex]->codec;
video_dec = avcodec_find_decoder(video_dec_ctx->codec_id);


if (avcodec_open2(video_dec_ctx, video_dec, NULL) < 0)
{
printf("avcodec_open2 failed\n");
return (void *)NULL;
}

height = video_dec_ctx->height;
width = video_dec_ctx->width;

pkt = av_packet_alloc();
av_init_packet(pkt);


outputsize = 0;
while (1)
{
int got_picture = 0,ret = 0;
if (av_read_frame(pFormat, pkt) < 0)
{
//printf("av_read_frame()<0 framecnt=%u decodecnt=%u\n", framecnt, decodecnt);
break;
}
framecnt++;

if (pkt->stream_index == videoindex)
{
//AVFrame *pFrame = avcodec_alloc_frame();// 2.8.6
AVFrame *pFrame = av_frame_alloc();// 3.0.0
decodecnt++;
ret = avcodec_decode_video2(video_dec_ctx, pFrame, &got_picture, pkt);
if (ret < 0)
{
printf("avcodec_decode_video2 failed\n");
//avcodec_free_frame(&pFrame);// 2.8.6
av_frame_free(&pFrame);// 3.0.0
break;
}

if(got_picture)
{
}
}
av_frame_free(&pFrame);// 3.0.0



输出:
flsg=0x200
ptr:0x7fb675889000 size:41381359
ptr:0x7fb675989000 size:40332783
ptr:0x7fb675a89000 size:39284207
ptr:0x7fb675b89000 size:38235631
ptr:0x7fb675c89000 size:37187055
ptr:0x7fb675d89000 size:36138479
ptr:0x7fb675e89000 size:35089903
ptr:0x7fb675f89000 size:34041327
ptr:0x7fb676089000 size:32992751
ptr:0x7fb676189000 size:31944175
ptr:0x7fb676289000 size:30895599
ptr:0x7fb676389000 size:29847023
ptr:0x7fb676489000 size:28798447
ptr:0x7fb676589000 size:27749871
ptr:0x7fb676689000 size:26701295
ptr:0x7fb676789000 size:25652719
ptr:0x7fb676889000 size:24604143
ptr:0x7fb676989000 size:23555567
ptr:0x7fb676a89000 size:22506991
ptr:0x7fb676b89000 size:21458415
ptr:0x7fb676c89000 size:20409839
ptr:0x7fb676d89000 size:19361263
ptr:0x7fb676e89000 size:18312687
ptr:0x7fb676f89000 size:17264111
ptr:0x7fb677089000 size:16215535
ptr:0x7fb677189000 size:15166959
ptr:0x7fb677289000 size:14118383
ptr:0x7fb677389000 size:13069807
ptr:0x7fb677489000 size:12021231
ptr:0x7fb677589000 size:10972655
ptr:0x7fb677689000 size:9924079
ptr:0x7fb677789000 size:8875503
ptr:0x7fb677889000 size:7826927
ptr:0x7fb677989000 size:6778351
ptr:0x7fb677a89000 size:5729775
ptr:0x7fb677b89000 size:4681199
ptr:0x7fb677c89000 size:3632623
ptr:0x7fb677d89000 size:2584047
ptr:0x7fb677e89000 size:1535471
ptr:0x7fb677f89000 size:486895
max_analyze_duration=0 probesize=5000000
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fb678400900] stream 0, offset 0x3e29: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fb678400900] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 1920x1080, 7254 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'hello.mp4':
Metadata:
major_brand : mp42
minor_version : 1
compatible_brands: isommp423gp5
creation_time : 2011-08-04 01:51:12
title : big_buck_bunny_1080p_surround.avi_006.AVI.MP4
artist : created with SUPER(C).v2011.bld.49
encoder : eRightSoft
comment : 09:51:32
Duration: 00:00:45.00, bitrate: N/A
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 1920x1080, 7254 kb/s, 25 fps, 25 tbr, 25 tbn, 50 tbc (default)
Metadata:
creation_time : 2011-08-05 01:50:11
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96 kb/s (default)
Metadata:
creation_time : 2011-08-04 01:51:16
handler_name : GPAC ISO Audio Handler
Stream #0:2(und): Data: none (mp4s / 0x7334706D), 0 kb/s (default)
Metadata:
creation_time : 2011-08-04 01:51:21
handler_name : GPAC MPEG-4 OD Handler
Stream #0:3(und): Data: none (mp4s / 0x7334706D), 0 kb/s (default)
Metadata:
creation_time : 2011-08-04 01:51:21
handler_name : GPAC MPEG-4 BIFS Handler
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fb678400900] stream 0, offset 0x3e29: partial file


现象:
程序会在avformat_open_input(&pFormat, , NULL, NULL)中遍历完整个文件流,最终并不报打开文件错误。
...全文
1118 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
半岛铁盒. 2021-06-17
  • 打赏
  • 举报
回复

楼主问题解决了么,有什么方法

rightorwrong 2016-05-19
  • 打赏
  • 举报
回复
avformat_open_input是封装的函数吧,你怎么知道遍历完了整个流 你可以看看可以正常解码时这个函数的参数值,然后在对照不能解码时的值 通过比较可以知道是哪里问题了
sinat_34153405 2016-05-18
  • 打赏
  • 举报
回复
iformat参数好多,这怎么赋值呢。 ffmpeg判断视频格式可能会参考扩展名,但应该不会就扩展名鉴别这一种方式
引用 2 楼 twtldn 的回复:
直接从文件读的话,ffmpeg是知道视频格式的,比如mp4或者flv,ffmpeg知道如何解析。映射到内存之后没了扩展名,不知道如何解析了吧。猜的,没仔细研究过。估计给formatcontext指定一个iformat就行了吧
twtldn 2016-05-16
  • 打赏
  • 举报
回复
直接从文件读的话,ffmpeg是知道视频格式的,比如mp4或者flv,ffmpeg知道如何解析。映射到内存之后没了扩展名,不知道如何解析了吧。猜的,没仔细研究过。估计给formatcontext指定一个iformat就行了吧
sinat_34153405 2016-05-15
  • 打赏
  • 举报
回复
libavformat/mov.c 中mov_read_header() 一直在读header,最后认为头错误。 但为啥直接打开文件的方式读就可以正常解码呢?
内容概要:本文研究了基于角蜥蜴优化算法(HLOA)优化BP神经网络的风电功率预测模型,并提供了完整的Matlab代码实现。该方法通过引入HLOA算法对BP神经网络的初始权值和阈值进行全局寻优,有效解决了传统BP神经网络收敛速度慢、易陷入局部最优的问题,显著提升了风电功率预测的精度与鲁棒性。文中系统阐述了HLOA算法的生物启发机制、数学模型及其寻优过程,详细构建了HLOA-BP预测模型的技术框架,并结合实际风电场历史数据开展仿真实验。结果表明,该模型在均方根误差(RMSE)、平均绝对误差(MAE)和决定系数(R²)等关键指标上均优于标准BP神经网络及其他主流智能优化算法(如PSO、GWO)优化的对比模型,验证了其在短期风电功率预测中的优越性能。; 适合人群:具备一定机器学习与智能优化算法基础,从事新能源发电预测、电力系统调度、智能算法应用或相关领域研究的科研人员及工程技术人员,尤其适合电气工程、自动化、计算机等相关专业的研究生及以上学历层次的学习者与开发者。; 使用场景及目标:①应用于风电场短期功率预测,提升电网调度的稳定性与经济性;②为智能优化算法与神经网络融合的技术方案提供实践参考;③作为科研论文复现、算法改进或毕业课题设计的高质量模板。; 阅读建议:建议读者结合所提供的Matlab代码,按照文中模型构建流程逐步调试与运行,重点关注HLOA算法的参数敏感性分析及其对网络训练过程的影响,同时可尝试将该方法迁移至光伏发电预测、负荷预测或其他时间序列预测场景中进行拓展验证与性能对比。
内容概要:本文围绕“参与调峰的储能系统配置方案及经济性分析”展开研究,基于Matlab代码实现,重点探讨了储能系统在电力系统调峰中的优化配置方法与经济效益评估。研究内容涵盖储能系统应对负荷峰谷差、提升电网稳定性的技术路径,构建了综合考虑投资成本、运行维护费用、峰谷电价套利及电网服务收益的经济性模型,并通过仿真验证不同配置方案的技术可行性与经济优势。文中结合实际电力市场环境,系统分析了储能容量、功率配置、充放电策略等关键因素对经济性的影响,深入探讨了多场景下的敏感性变化规律,为储能项目的科学规划与投资决策提供了量化依据与理论支撑; 适合人群:具备电力系统基础知识和Matlab编程能力,从事新能源、储能技术、电力市场或相关领域研究的研发人员、工程技术人员及高校研究生。; 使用场景及目标:①用于科研复现EI期刊论文中的储能配置与经济性分析模型;②指导实际储能项目在调峰场景下的容量规划、运行策略制定与经济评估;③辅助高校教学与课题研究,深入理解储能系统在电力系统中的多重价值实现机制与优化逻辑。; 阅读建议:建议结合文中的Matlab代码与理论模型同步阅读,重点关注目标函数构建、约束条件设定及参数敏感性分析部分,通过调整输入数据和关键参数进行仿真验证,以深化对储能系统经济性影响因素及其优化路径的理解。

2,554

社区成员

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

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