ffmpeg 打开视频流太慢(下)
前面的博文中已经交代过,ffmpeg打开视频慢主要是因为av_find_stream_info 耗时久。下面给出重写查找音视频stream info的一段代码,用来替代av_find_stream_info 。
static int try_decode_frame(AVFormatContext *s, AVStream *st, AVPacket *avpkt,
AVDictionary **options)
{
const AVCodec *codec;
int got_picture = , ret = ;
AVFrame *frame = av_frame_alloc();
AVSubtitle subtitle;
AVPacket pkt = *avpkt; if (!frame)
return AVERROR(ENOMEM); if (!avcodec_is_open(st->codec) &&
st->info->found_decoder <= &&
(st->codec->codec_id != -st->info->found_decoder || !st->codec->codec_id)) {
AVDictionary *thread_opt = NULL;
codec = avcodec_find_decoder( st->codec->codec_id);
if (!codec) {
st->info->found_decoder = -st->codec->codec_id;
ret = -;
goto fail;
} /* Force thread count to 1 since the H.264 decoder will not extract
* SPS and PPS to extradata during multi-threaded decoding. */
av_dict_set(options ? options : &thread_opt, "threads", "", );
ret = avcodec_open2(st->codec, codec, options ? options : &thread_opt);
if (!options)
av_dict_free(&thread_opt);
if (ret < ) {
st->info->found_decoder = -st->codec->codec_id;
goto fail;
}
st->info->found_decoder = ;
} else if (!st->info->found_decoder)
st->info->found_decoder = ; if (st->info->found_decoder < ) {
ret = -;
goto fail;
} got_picture = ;
switch (st->codec->codec_type) {
case AVMEDIA_TYPE_VIDEO:
if(st->codec->width > && st->codec->height > && st->codec->pix_fmt != AV_PIX_FMT_NONE)
{
ret = ;
break;
}
ret = avcodec_decode_video2(st->codec, frame,
&got_picture, &pkt);
break;
case AVMEDIA_TYPE_AUDIO:
ret = avcodec_decode_audio4(st->codec, frame, &got_picture, &pkt);
break;
case AVMEDIA_TYPE_SUBTITLE:
ret = avcodec_decode_subtitle2(st->codec, &subtitle,
&got_picture, &pkt);
ret = pkt.size;
break;
default:
break;
}
if (ret >= ) {
if (got_picture)
st->nb_decoded_frames++;
pkt.data += ret;
pkt.size -= ret;
ret = got_picture;
} if (!pkt.data && !got_picture)
ret = -; fail:
av_frame_free(&frame);
return ret;
}
static int ff_alloc_extradata(AVCodecContext *avctx, int size)
{
int ret; if (size < || size >= INT32_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
avctx->extradata_size = ;
return AVERROR(EINVAL);
}
avctx->extradata = (uint8_t *)av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
if (avctx->extradata) {
memset(avctx->extradata + size, , FF_INPUT_BUFFER_PADDING_SIZE);
avctx->extradata_size = size;
ret = ;
} else {
avctx->extradata_size = ;
ret = AVERROR(ENOMEM);
}
return ret;
}
static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
AVPacketList **plast_pktl)
{
AVPacketList *pktl = (AVPacketList *)av_mallocz(sizeof(AVPacketList));
if (!pktl)
return NULL; if (*packet_buffer)
(*plast_pktl)->next = pktl;
else
*packet_buffer = pktl; /* Add the packet in the buffered packet list. */
*plast_pktl = pktl;
pktl->pkt = *pkt;
return &pktl->pkt;
}
static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
{
while (*pkt_buf) {
AVPacketList *pktl = *pkt_buf;
*pkt_buf = pktl->next;
av_free_packet(&pktl->pkt);
av_freep(&pktl);
}
*pkt_buf_end = NULL;
}
static int ff_check_interrupt(AVIOInterruptCB *cb)
{
int ret;
if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
return ret;
return ;
} static int has_codec_parameters(AVStream *st, const char **errmsg_ptr,bool keypacket = false)
{
AVCodecContext *avctx = st->codec; #define FAIL(errmsg) do { \
if (errmsg_ptr) \
*errmsg_ptr = errmsg; \
return ; \
} while () /* if(keypacket)
{
static int packetCount = 1;
packetCount++;
if(packetCount > 1)
cout << "packetCount too much" <<endl;
}*/
switch (avctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
if(avctx->codec_id == AV_CODEC_ID_NONE)
break;
if (st->info->found_decoder >= &&
avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
FAIL("unspecified sample format");
if (!avctx->sample_rate)
FAIL("unspecified sample rate");
if (!avctx->channels)
FAIL("unspecified number of channels");
if (st->info->found_decoder >= && !st->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
FAIL("no decodable DTS frames");
break;
case AVMEDIA_TYPE_VIDEO:
if (!avctx->width)
FAIL("unspecified size");
if (st->info->found_decoder >= && avctx->pix_fmt == AV_PIX_FMT_NONE)
FAIL("unspecified pixel format");
break;
case AVMEDIA_TYPE_DATA:
return ;
case AVMEDIA_TYPE_UNKNOWN:
FAIL("unspecified codec type");
break;
default:
return ;
} return ;
}
static const AVCodec *find_decoder(AVFormatContext *s, AVStream *st, enum AVCodecID codec_id)
{
if (st->codec->codec)
return st->codec->codec; switch (st->codec->codec_type)
{
case AVMEDIA_TYPE_VIDEO:
if (s->video_codec) return s->video_codec;
break;
case AVMEDIA_TYPE_AUDIO:
if (s->audio_codec) return s->audio_codec;
break;
case AVMEDIA_TYPE_SUBTITLE:
if (s->subtitle_codec) return s->subtitle_codec;
break;
}
return avcodec_find_decoder(codec_id);
} static int alloc_and_copy_exterdata(AVCodecContext *dest, const AVCodecContext *src,size_t size ,size_t pad)
{
if(src->extradata && size > )
{
dest->extradata = (uint8_t *)av_malloc(size + pad);
if(dest->extradata == nullptr)
{
goto fail;
}
memcpy(dest->extradata, src->extradata, size);
if(pad)
{
memset((uint8_t *)dest->extradata + size, , pad);
}
}
return ;
fail:
av_freep(&dest->extradata);
return AVERROR(ENOMEM);
} static int alloc_and_copy_intra_matrix(AVCodecContext *dest, const AVCodecContext *src,size_t size ,size_t pad)
{
if(src->intra_matrix && size > )
{
dest->intra_matrix = (uint16_t *)av_malloc(size + pad);
if(dest->intra_matrix == nullptr)
{
goto fail;
}
memcpy(dest->intra_matrix, src->intra_matrix, size);
if(pad)
{
memset((uint8_t *)dest->intra_matrix + size, , pad);
}
} fail:
av_freep(&dest->intra_matrix);
return AVERROR(ENOMEM);
} static int alloc_and_copy_inter_matrix(AVCodecContext *dest, const AVCodecContext *src,size_t size ,size_t pad)
{
if(src->inter_matrix && size > )
{
dest->inter_matrix = (uint16_t *)av_malloc(size + pad);
if(dest->inter_matrix == nullptr)
{
goto fail;
}
memcpy(dest->inter_matrix, src->inter_matrix, size);
if(pad)
{
memset((uint8_t *)dest->inter_matrix + size, , pad);
}
} fail:
av_freep(&dest->inter_matrix);
return AVERROR(ENOMEM);
} static int alloc_and_copy_rc_override(AVCodecContext *dest, const AVCodecContext *src,size_t size ,size_t pad)
{
if(src->rc_override && size > )
{
dest->rc_override = (RcOverride *)av_malloc(size + pad);
if(dest->rc_override == nullptr)
{
goto fail;
}
memcpy(dest->rc_override, src->rc_override, size);
if(pad)
{
memset((uint8_t *)dest->rc_override + size, , pad);
}
} fail:
av_freep(&dest->rc_override);
return AVERROR(ENOMEM);
}
static int avcodec_copy_context_private(AVCodecContext *dest, const AVCodecContext *src)
{
if(!src)
{
return -;
}
const AVCodec *orig_codec = dest->codec;
uint8_t *orig_priv_data = (uint8_t *)dest->priv_data; if (avcodec_is_open(dest)) { // check that the dest context is uninitialized
av_log(dest, AV_LOG_ERROR,
"Tried to copy AVCodecContext %p into already-initialized %p\n",
src, dest);
return AVERROR(EINVAL);
} av_opt_free(dest); memcpy(dest, src, sizeof(*dest)); dest->priv_data = orig_priv_data; if (orig_priv_data)
av_opt_copy(orig_priv_data, src->priv_data); dest->codec = orig_codec; /* set values specific to opened codecs back to their default state */
dest->slice_offset = NULL;
dest->hwaccel = NULL;
dest->internal = NULL; /* reallocate values that should be allocated separately */
//dest->rc_eq = NULL;
dest->extradata = NULL;
dest->intra_matrix = NULL;
dest->inter_matrix = NULL;
dest->rc_override = NULL;
dest->subtitle_header = NULL;
/* if (src->rc_eq) {
dest->rc_eq = av_strdup(src->rc_eq);
if (!dest->rc_eq)
return AVERROR(ENOMEM);
}*/
int ret = ;
ret = alloc_and_copy_exterdata(dest, src, src->extradata_size, FF_INPUT_BUFFER_PADDING_SIZE);
if(ret < )
{
return ret ;
}
ret = alloc_and_copy_intra_matrix(dest, src, * sizeof(int16_t), );
if(ret < )
{
return ret ;
}
ret = alloc_and_copy_inter_matrix(dest, src, * sizeof(int16_t), );
if(ret < )
{
return ret ;
}
ret = alloc_and_copy_rc_override(dest, src,src->rc_override_count * sizeof(*src->rc_override), );
return ret ;
} static int avformatFindStreamInfo(AVFormatContext *ic, AVDictionary **options,std::vector<shared_ptr<AVPacket>> &packets,int timeout = )
{
int i, count, ret = , j;
int64_t read_size;
AVStream *st;
// AVPacket *pkt;
AVPacket packet;
int64_t old_offset = ;
int64_t last_offset = ;
bool firstKeyPacket = false;
int tryCount = ;
int orig_nb_streams = ic->nb_streams;
int flush_codecs = ic->probesize > ;
int64_t startTime = av_gettime();
int64_t max_analyze_duration = ic->max_analyze_duration2;
for (i = ; i < ic->nb_streams; i++) {
const AVCodec *codec;
AVDictionary *thread_opt = NULL;
st = ic->streams[i]; if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
if (!st->codec->time_base.num)
st->codec->time_base = st->time_base;
} if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
st->parser = av_parser_init(st->codec->codec_id);
if (st->parser) {
if (st->need_parsing == AVSTREAM_PARSE_HEADERS) {
st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
} else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
}
} else if (st->need_parsing) { }
}
codec = avcodec_find_decoder(st->codec->codec_id); av_dict_set(options ? &options[i] : &thread_opt, "threads", "", );
if (!has_codec_parameters(st, NULL) && st->request_probe <= ) {
if (codec && !st->codec->codec)
if (avcodec_open2(st->codec, codec, options ? &options[i] : &thread_opt) < )
av_log(ic, AV_LOG_WARNING,
"Failed to open codec in av_find_stream_info\n");
}
if (!options)
av_dict_free(&thread_opt);
} for (i = ; i < ic->nb_streams; i++) {
ic->streams[i]->info->fps_first_dts = AV_NOPTS_VALUE;
ic->streams[i]->info->fps_last_dts = AV_NOPTS_VALUE;
} count = ;
read_size = ;
bool saveAudioFlag = false;
shared_ptr<AVPacket> lastAudioPacket[AudioPacketsNum] = {nullptr}; for (;;) {
if (ff_check_interrupt(&ic->interrupt_callback)) {
ret = AVERROR_EXIT;
av_log(ic, AV_LOG_DEBUG, "interrupted\n");
break;
}
if(av_gettime() - startTime > timeout * * )
{
ret = -;
break;
}
/* check if one codec still needs to be handled */
for (i = ; i < ic->nb_streams; i++) {
st = ic->streams[i];
if (!has_codec_parameters(st, NULL,firstKeyPacket))
break; if (st->parser && st->parser->parser->split &&
!st->codec->extradata)
break;
}
if (i == ic->nb_streams && ic->nb_streams > )
{
break;
} shared_ptr<AVPacket> pkt((AVPacket*)av_malloc(sizeof(AVPacket)), [&](AVPacket *p) { av_free_packet(p); av_freep(&p); });
av_init_packet(pkt.get());
old_offset = avio_tell(ic->pb);
ret = av_read_frame(ic, pkt.get());
if (ret == AVERROR(EAGAIN))
{
std::this_thread::sleep_for(std::chrono::milliseconds());
continue;
}
if (ret < ) {
/* EOF or error*/
break;
} if(ic->nb_streams == ) return -;
auto codeType = ic->streams[pkt->stream_index]->codec->codec_type;
if(codeType == AVMEDIA_TYPE_VIDEO && pkt->flags&AV_PKT_FLAG_KEY)
{
firstKeyPacket = true;
}
if(!firstKeyPacket && codeType == AVMEDIA_TYPE_VIDEO)
{
continue;
} shared_ptr<AVPacket> pkt1((AVPacket*)av_malloc(sizeof(AVPacket)), [&](AVPacket *p) { av_free_packet(p); av_freep(&p); });
av_init_packet(pkt1.get());
av_copy_packet(pkt1.get(),pkt.get());
packets.push_back(pkt1); st = ic->streams[pkt->stream_index];
if (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC))
read_size += pkt->size; if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > ) {
/* check for non-increasing dts */
if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
st->info->fps_last_dts >= pkt->dts) { st->info->fps_first_dts =
st->info->fps_last_dts = AV_NOPTS_VALUE;
}
/* Check for a discontinuity in dts. If the difference in dts
* is more than 1000 times the average packet duration in the
* sequence, we treat it as a discontinuity. */
if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
st->info->fps_last_dts_idx > st->info->fps_first_dts_idx &&
(pkt->dts - st->info->fps_last_dts) / >
(st->info->fps_last_dts - st->info->fps_first_dts) /
(st->info->fps_last_dts_idx - st->info->fps_first_dts_idx)) { st->info->fps_first_dts =
st->info->fps_last_dts = AV_NOPTS_VALUE;
} /* update stored dts values */
if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
st->info->fps_first_dts = pkt->dts;
st->info->fps_first_dts_idx = st->codec_info_nb_frames;
}
st->info->fps_last_dts = pkt->dts;
st->info->fps_last_dts_idx = st->codec_info_nb_frames;
} if (st->parser && st->parser->parser->split && !st->codec->extradata) {
int i = st->parser->parser->split(st->codec, pkt->data, pkt->size);
if (i > && i < FF_MAX_EXTRADATA_SIZE) {
if (ff_alloc_extradata(st->codec, i))
return AVERROR(ENOMEM);
memcpy(st->codec->extradata, pkt->data,
st->codec->extradata_size);
}
}
auto decodeRet = try_decode_frame(ic, st, pkt.get(),
(options && i < orig_nb_streams) ? &options[i] : NULL);
st->codec_info_nb_frames++; } for (i = ; i < ic->nb_streams; i++) {
st = ic->streams[i];
avcodec_close(st->codec);
}
for (i = ; i < ic->nb_streams; i++)
{
st = ic->streams[i];
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && !st->codec->codec_tag && !st->codec->bits_per_coded_sample) {
uint32_t tag= avcodec_pix_fmt_to_codec_tag(st->codec->pix_fmt);
/* if (avpriv_find_pix_fmt(ff_raw_pix_fmt_tags, tag) == st->codec->pix_fmt)
st->codec->codec_tag= tag;*/
} if (!st->r_frame_rate.num) {
if ( st->codec->time_base.den * (int64_t) st->time_base.num
<= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t) st->time_base.den) {
st->r_frame_rate.num = st->codec->time_base.den;
st->r_frame_rate.den = st->codec->time_base.num * st->codec->ticks_per_frame;
} else {
st->r_frame_rate.num = st->time_base.den;
st->r_frame_rate.den = st->time_base.num;
}
}
}
else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
if (!st->codec->bits_per_coded_sample)
st->codec->bits_per_coded_sample =
av_get_bits_per_sample(st->codec->codec_id);
// set stream disposition based on audio service type
switch (st->codec->audio_service_type) {
case AV_AUDIO_SERVICE_TYPE_EFFECTS:
st->disposition = AV_DISPOSITION_CLEAN_EFFECTS;
break;
case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED;
break;
case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
st->disposition = AV_DISPOSITION_HEARING_IMPAIRED;
break;
case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
st->disposition = AV_DISPOSITION_COMMENT;
break;
case AV_AUDIO_SERVICE_TYPE_KARAOKE:
st->disposition = AV_DISPOSITION_KARAOKE;
break;
}
}
} find_stream_info_err: for (i = ; i < ic->nb_streams; i++) {
st = ic->streams[i];
if (ic->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
ic->streams[i]->codec->thread_count = ;
if (st->info)
av_freep(&st->info->duration_error);
av_freep(&ic->streams[i]->info);
}
if(ic->nb_streams == )
ret = -;
return ret;
}
这段代码的宗旨是找到音视频必要的信息后,立即返回。方法参数里传入vector> packets 是为了保存在查找的过程中已经从context中读取的视频包。如果不保存,这些视频包将会丢掉,同样会增加视频打开的时间。经现场测试,主流的摄像机例如海康,英飞拓,中威等都可以用它来打开。注意:文件不可以用这种方式打开。如果使用这段代码打不开视频或有疑问,请联系我:350197870。
视频下载地址:http://www.chungen90.com/?news_3/
Demo下载地址: http://www.chungen90.com/?news_2
ffmpeg 打开视频流太慢(下)的更多相关文章
- ffmpeg 打开视频流太慢(上)
新版ffmpeg打开网络视频流需要调用avformat_find_stream_info方法,很多朋友会发现调用改方法耗费很多时间造成打开视频流太慢.有两个参数可以减少avformat_find_st ...
- 基于jsmpeg库下使用ffmpeg创建视频流连接websocket中继器传输视频并播放
这个功能的基本工作是这样的: 1.使用node运行jsmpeg库下的websocket-relay.js文件,这个文件的作用是创建一个websocket视频传输中继器 2.运行ffmpeg,将输出发送 ...
- ffmpeg 从视频流中抓取图片
从视频中不断抓取图片的基本流程:打开视频流地址->获取视频流packt->解码成图片帧->输出图片 一.初始化Ffmpeg void ffmpegInit(){ av_registe ...
- 段错误:使用opencv打开视频流
段错误:使用opencv打开视频流时报这个错误 1 使用命令dmesg 发现是libavutil.so模块发生了错误. 如果是java端报错,可能如下: libavutil.so ... av_di ...
- 【转】NGUI研究院之为什么打开界面太慢(十三)
NGUI打开界面太慢了,起初一直以为是unity的问题,最近经过我的全面测试我发现这和unity没有关系.一般一个比较复杂的界面大概需要150个GameObject 或者 UISprite .我用N ...
- GMF:如何在不打开Editor的情况下生成图片
问题 GMF应用中,有时我们希望在不打开*DiagramEditor的情况下,从文件就能生成它的图片 解决方案 首先,从文件中构造DiagramImpl实例: TransactionalEditi ...
- ffmpeg(2.6) rockplayer android 下编译 小记.
最近因为一些需求,开始学习 ffmgeg 在android 上使用. 使用的环境: 1,VMware V8 虚似机 安装的 FedoraV18 系统.(下载地址,请baidu),虚似机,最好有20-3 ...
- ffmpeg解码视频流
//初始化.注册编解码器 avcodec_init(); av_register_all(); avformat_network_init(); //选取测试文件 char* FileName = & ...
- 使用ffmpeg获取视频流后如何封装存储成mp4文件
int main(int argc,char *argv[]) 02 { 03 AVFormatContext *pFormatCtx; 04 int i,videoStream; 05 AVC ...
随机推荐
- 记录android5.0更新踩过的坑
1. service的注册必须显示注册,不能隐式注册,相关链接http://www.eoeandroid.com/thread-568853-1-1.html 现象:Service Intent mu ...
- hdu 1222 Wolf and Rabbit
Problem Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbi ...
- 入门必须掌握8个DOS命令
一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的:网络上的机器都有唯一确定的IP ...
- jQuery AJAX Call for posting data to ASP.Net page ( not Get but POST)
the following jQuery AJAX call to an ASP.Net page. $.ajax({ async: true, type: "POST", url ...
- Cocos2d-JS事件处理机制
在很多图形用户技术中,事件处理机制一般都有三个重要的角色:事件.事件源和事件处理者.事件源是事件发生的场所,通常就是各个视图或控件,事件处理者是接收事件并对其进行处理的一段程序.事件处理机制中三个角色 ...
- HTML+CSS学习笔记(9)- CSS的继承、层叠和特殊性
标签:HTML+CSS 继承 CSS的某些样式是具有继承性的,那么什么是继承呢?继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码:如某种颜色应用于p标签,这 ...
- 使用VS2015(c#)进行单元测试,显示测试结果与查看代码覆盖率
创建测试的过程可参考如下链接 http://www.cnblogs.com/libaoquan/p/5296384.html (一)如何使用VS2015查看测试结果 问题描述:使用VS2010执行单元 ...
- Android开发之切换activity动画overridePendingTransition
原文地址:http://blog.sina.com.cn/s/blog_706c449f01011s3v.html overridePendingTransition 在startActivity() ...
- Codevs 2837 考前复习
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description Aiden马上要考试了,可他还没怎么复习,于是他 ...
- IOS绘图
#import "ViewController.h" #import "DrawView.h" @interface ViewController () @pr ...