static int open_input_file_and_get_contexts(const char* input_file_name,
AVFormatContext** input_format_context,
AVCodecContext** input_video_codec_context,
AVCodecContext** input_audio_codec_context,
int* video_index,
int* audio_index)
{
int idx_v = -1, idx_a = -1;
AVCodecContext* codec_ctx_v = NULL;
AVCodecContext* codec_ctx_a = NULL;
AVCodec* codec_v = NULL;
AVCodec* codec_a = NULL;
AVDictionary* options = NULL;
av_dict_set(&options, "rtsp_transport", "tcp", 0);
avformat_open_input(input_format_context, input_file_name, NULL, &options);
avformat_find_stream_info(*input_format_context, NULL);
for (int i = 0; i < (*input_format_context)->nb_streams; ++i)
{
if (AVMEDIA_TYPE_AUDIO == (*input_format_context)->streams[i]->codec->codec_type)
{
idx_a = i;
}
else if (AVMEDIA_TYPE_VIDEO == (*input_format_context)->streams[i]->codec->codec_type)
{
idx_v = i;
}
else
{
;
}
}
if (idx_v >= 0)
{
//codec_v = avcodec_find_decoder((*input_format_context)->streams[idx_v]->codecpar->codec_id);
//codec_ctx_v = avcodec_alloc_context3(codec_v);
//avcodec_parameters_to_context(codec_ctx_v, (*input_format_context)->streams[idx_v]->codecpar);
codec_ctx_v = (*input_format_context)->streams[idx_v]->codec;
codec_v = avcodec_find_decoder((*input_format_context)->streams[idx_v]->codec->codec_id);
avcodec_open2(codec_ctx_v, codec_v, NULL);
*input_video_codec_context = codec_ctx_v;
*video_index = idx_v;
}
if (idx_a >= 0)
{
//codec_a = avcodec_find_decoder((*input_format_context)->streams[idx_a]->codecpar->codec_id);
//codec_ctx_a = avcodec_alloc_context3(codec_a);
//avcodec_parameters_to_context(codec_ctx_a, (*input_format_context)->streams[idx_a]->codecpar);
codec_ctx_a = (*input_format_context)->streams[idx_a]->codec;
codec_a = avcodec_find_decoder((*input_format_context)->streams[idx_a]->codec->codec_id);
avcodec_open2(codec_ctx_a, codec_a, NULL);
*input_audio_codec_context = codec_ctx_a;
*audio_index = idx_a;
}
return 0;
}