首页 技术 正文
技术 2022年11月15日
0 收藏 669 点赞 4,443 浏览 13259 个字

前面的博文中已经交代过,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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850