前面的博文中已经交代过,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 = 1, ret = 0;
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 <= 0 &&
(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 = -1;
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", "1", 0);
ret = avcodec_open2(st->codec, codec, options ? options : &thread_opt);
if (!options)
av_dict_free(&thread_opt);
if (ret < 0) {
st->info->found_decoder = -st->codec->codec_id;
goto fail;
}
st->info->found_decoder = 1;
} else if (!st->info->found_decoder)
st->info->found_decoder = 1;
if (st->info->found_decoder < 0) {
ret = -1;
goto fail;
}
got_picture = 0;
switch (st->codec->codec_type) {
case AVMEDIA_TYPE_VIDEO:
if(st->codec->width > 0 && st->codec->height > 0 && st->codec->pix_fmt != AV_PIX_FMT_NONE)
{
ret = 0;
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 >= 0) {
if (got_picture)
st->nb_decoded_frames++;
pkt.data += ret;
pkt.size -= ret;
ret = got_picture;
}
if (!pkt.data && !got_picture)
ret = -1;
fail:
av_frame_free(&frame);
return ret;
}
static int ff_alloc_extradata(AVCodecContext *avctx, int size)
{
int ret;
if (size < 0 || size >= INT32_MAX - FF_INPUT_BUFFER_PADDING_SIZE) {
avctx->extradata_size = 0;
return AVERROR(EINVAL);
}
avctx->extradata = (uint8_t *)av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
if (avctx->extradata) {
memset(avctx->extradata + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
avctx->extradata_size = size;
ret = 0;
} else {
avctx->extradata_size = 0;
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 0;
}
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 0; \
} while (0)
/* 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 >= 0 &&
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 >= 0 && !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 >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
FAIL("unspecified pixel format");
break;
case AVMEDIA_TYPE_DATA:
return 1;
case AVMEDIA_TYPE_UNKNOWN:
FAIL("unspecified codec type");
break;
default:
return 0;
}
return 1;
}
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 > 0)
{
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, 0, pad);
}
}
return 0;
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 > 0)
{
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, 0, 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 > 0)
{
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, 0, 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 > 0)
{
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, 0, pad);
}
}
fail:
av_freep(&dest->rc_override);
return AVERROR(ENOMEM);
}
static int avcodec_copy_context_private(AVCodecContext *dest, const AVCodecContext *src)
{
if(!src)
{
return -1;
}
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 = 0;
ret = alloc_and_copy_exterdata(dest, src, src->extradata_size, FF_INPUT_BUFFER_PADDING_SIZE);
if(ret < 0)
{
return ret ;
}
ret = alloc_and_copy_intra_matrix(dest, src, 64 * sizeof(int16_t), 0);
if(ret < 0)
{
return ret ;
}
ret = alloc_and_copy_inter_matrix(dest, src, 64 * sizeof(int16_t), 0);
if(ret < 0)
{
return ret ;
}
ret = alloc_and_copy_rc_override(dest, src,src->rc_override_count * sizeof(*src->rc_override), 0);
return ret ;
}
static int avformatFindStreamInfo(AVFormatContext *ic, AVDictionary **options,std::vector<shared_ptr<AVPacket>> &packets,int timeout = 5)
{
int i, count, ret = 0, j;
int64_t read_size;
AVStream *st;
// AVPacket *pkt;
AVPacket packet;
int64_t old_offset = 0;
int64_t last_offset = 0;
bool firstKeyPacket = false;
int tryCount = 0;
int orig_nb_streams = ic->nb_streams;
int flush_codecs = ic->probesize > 0;
int64_t startTime = av_gettime();
int64_t max_analyze_duration = ic->max_analyze_duration2;
for (i = 0; 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", "1", 0);
if (!has_codec_parameters(st, NULL) && st->request_probe <= 0) {
if (codec && !st->codec->codec)
if (avcodec_open2(st->codec, codec, options ? &options[i] : &thread_opt) < 0)
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 = 0; 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 = 0;
read_size = 0;
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 * 1000 * 1000)
{
ret = -1;
break;
}
/* check if one codec still needs to be handled */
for (i = 0; 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 > 0)
{
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(100));
continue;
}
if (ret < 0) {
/* EOF or error*/
break;
}
if(ic->nb_streams == 0) return -1;
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 > 1) {
/* 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) / 1000 >
(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 > 0 && 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 = 0; i < ic->nb_streams; i++) {
st = ic->streams[i];
avcodec_close(st->codec);
}
for (i = 0; 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 = 0; 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 = 0;
if (st->info)
av_freep(&st->info->duration_error);
av_freep(&ic->streams[i]->info);
}
if(ic->nb_streams == 0)
ret = -1;
return ret;
}
这段代码的宗旨是找到音视频必要的信息后,立即返回。方法参数里传入vector> packets 是为了保存在查找的过程中已经从context中读取的视频包。如果不保存,这些视频包将会丢掉,同样会增加视频打开的时间。经现场测试,主流的摄像机例如海康,英飞拓,中威等都可以用它来打开。注意:文件不可以用这种方式打开。如果使用这段代码打不开视频或有疑问,请联系我:350197870。