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)中遍历完整个文件流,最终并不报打开文件错误。
...全文
911 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,最后认为头错误。 但为啥直接打开文件的方式读就可以正常解码呢?

2,543

社区成员

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

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