2,543
社区成员
发帖
与我相关
我的任务
分享
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
}
楼主问题解决了么,有什么方法