|
|
@@ -13,9 +13,9 @@ int64_t start_time = AV_NOPTS_VALUE;
|
|
|
static enum AVPixelFormat hw_pix_fmt;
|
|
|
|
|
|
VideoStateData::VideoStateData(bool use_hardware, bool loop_play)
|
|
|
- : m_bUseHardware(use_hardware), m_bLoopPlay(loop_play)
|
|
|
-{
|
|
|
-}
|
|
|
+ : m_bUseHardware(use_hardware)
|
|
|
+ , m_bLoopPlay(loop_play)
|
|
|
+{}
|
|
|
|
|
|
VideoStateData::~VideoStateData()
|
|
|
{
|
|
|
@@ -25,8 +25,7 @@ VideoStateData::~VideoStateData()
|
|
|
|
|
|
void VideoStateData::delete_video_state()
|
|
|
{
|
|
|
- if (m_pState)
|
|
|
- {
|
|
|
+ if (m_pState) {
|
|
|
stream_close(m_pState);
|
|
|
m_pState = nullptr;
|
|
|
}
|
|
|
@@ -45,15 +44,13 @@ bool VideoStateData::is_hardware_decode() const
|
|
|
int VideoStateData::create_video_state(const char* filename)
|
|
|
{
|
|
|
int ret = -1;
|
|
|
- if (!filename || !filename[0])
|
|
|
- {
|
|
|
+ if (!filename || !filename[0]) {
|
|
|
qDebug("filename is invalid, please select a valid media file.");
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
m_pState = stream_open(filename);
|
|
|
- if (!m_pState)
|
|
|
- {
|
|
|
+ if (!m_pState) {
|
|
|
qDebug("stream_open failed!");
|
|
|
return ret;
|
|
|
}
|
|
|
@@ -63,8 +60,7 @@ int VideoStateData::create_video_state(const char* filename)
|
|
|
|
|
|
void VideoStateData::print_state() const
|
|
|
{
|
|
|
- if (const auto is = m_pState)
|
|
|
- {
|
|
|
+ if (const auto is = m_pState) {
|
|
|
qDebug("[VideoState]PacketQueue(v:%p,a:%p,s:%p)", &is->videoq, &is->audioq, &is->subtitleq);
|
|
|
qDebug("[VideoState]FrameQueue(v:%p,a:%p,s:%p)", &is->pictq, &is->sampq, &is->subpq);
|
|
|
qDebug("[VideoState]Decoder(v:%p,a:%p,s:%p)", &is->viddec, &is->auddec, &is->subdec);
|
|
|
@@ -87,8 +83,7 @@ int VideoStateData::open_media(VideoState* is)
|
|
|
is->eof = 0;
|
|
|
|
|
|
ic = avformat_alloc_context();
|
|
|
- if (!ic)
|
|
|
- {
|
|
|
+ if (!ic) {
|
|
|
av_log(nullptr, AV_LOG_FATAL, "Could not allocate context.\n");
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
goto fail;
|
|
|
@@ -98,8 +93,7 @@ int VideoStateData::open_media(VideoState* is)
|
|
|
ic->interrupt_callback.opaque = is;
|
|
|
|
|
|
err = avformat_open_input(&ic, is->filename, is->iformat, nullptr);
|
|
|
- if (err < 0)
|
|
|
- {
|
|
|
+ if (err < 0) {
|
|
|
av_log(nullptr, AV_LOG_FATAL, "failed to open %s: %d", is->filename, err);
|
|
|
ret = -1;
|
|
|
goto fail;
|
|
|
@@ -115,8 +109,7 @@ int VideoStateData::open_media(VideoState* is)
|
|
|
//int orig_nb_streams = ic->nb_streams;
|
|
|
|
|
|
err = avformat_find_stream_info(ic, nullptr);
|
|
|
- if (err < 0)
|
|
|
- {
|
|
|
+ if (err < 0) {
|
|
|
av_log(nullptr, AV_LOG_WARNING, "%s: could not find codec parameters\n", is->filename);
|
|
|
ret = -1;
|
|
|
goto fail;
|
|
|
@@ -134,8 +127,7 @@ int VideoStateData::open_media(VideoState* is)
|
|
|
is->max_frame_duration = 2.0;
|
|
|
|
|
|
/* if seeking requested, we execute it */
|
|
|
- if (start_time != AV_NOPTS_VALUE)
|
|
|
- {
|
|
|
+ if (start_time != AV_NOPTS_VALUE) {
|
|
|
int64_t timestamp;
|
|
|
|
|
|
timestamp = start_time;
|
|
|
@@ -143,9 +135,12 @@ int VideoStateData::open_media(VideoState* is)
|
|
|
if (ic->start_time != AV_NOPTS_VALUE)
|
|
|
timestamp += ic->start_time;
|
|
|
ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
|
|
|
- if (ret < 0)
|
|
|
- {
|
|
|
- av_log(nullptr, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n", is->filename, (double)timestamp / AV_TIME_BASE);
|
|
|
+ if (ret < 0) {
|
|
|
+ av_log(nullptr,
|
|
|
+ AV_LOG_WARNING,
|
|
|
+ "%s: could not seek to position %0.3f\n",
|
|
|
+ is->filename,
|
|
|
+ (double) timestamp / AV_TIME_BASE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -153,8 +148,7 @@ int VideoStateData::open_media(VideoState* is)
|
|
|
|
|
|
av_dump_format(ic, 0, is->filename, 0);
|
|
|
|
|
|
- for (i = 0; i < ic->nb_streams; i++)
|
|
|
- {
|
|
|
+ for (i = 0; i < ic->nb_streams; i++) {
|
|
|
AVStream* st = ic->streams[i];
|
|
|
enum AVMediaType type = st->codecpar->codec_type;
|
|
|
st->discard = AVDISCARD_ALL;
|
|
|
@@ -162,41 +156,52 @@ int VideoStateData::open_media(VideoState* is)
|
|
|
if (avformat_match_stream_specifier(ic, st, wanted_stream_spec[type]) > 0)
|
|
|
st_index[type] = i;
|
|
|
}
|
|
|
- for (i = 0; i < AVMEDIA_TYPE_NB; i++)
|
|
|
- {
|
|
|
- if (wanted_stream_spec[i] && st_index[i] == -1)
|
|
|
- {
|
|
|
- av_log(nullptr, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n",
|
|
|
- wanted_stream_spec[i], av_get_media_type_string(AVMediaType(i)));
|
|
|
+ for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
|
|
|
+ if (wanted_stream_spec[i] && st_index[i] == -1) {
|
|
|
+ av_log(nullptr,
|
|
|
+ AV_LOG_ERROR,
|
|
|
+ "Stream specifier %s does not match any %s stream\n",
|
|
|
+ wanted_stream_spec[i],
|
|
|
+ av_get_media_type_string(AVMediaType(i)));
|
|
|
st_index[i] = INT_MAX;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- st_index[AVMEDIA_TYPE_VIDEO] = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, st_index[AVMEDIA_TYPE_VIDEO], -1, nullptr, 0);
|
|
|
- st_index[AVMEDIA_TYPE_AUDIO] = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, st_index[AVMEDIA_TYPE_AUDIO],
|
|
|
- st_index[AVMEDIA_TYPE_VIDEO], nullptr, 0);
|
|
|
- st_index[AVMEDIA_TYPE_SUBTITLE] = av_find_best_stream(ic, AVMEDIA_TYPE_SUBTITLE, st_index[AVMEDIA_TYPE_SUBTITLE],
|
|
|
- (st_index[AVMEDIA_TYPE_AUDIO] >= 0 ? st_index[AVMEDIA_TYPE_AUDIO] : st_index[AVMEDIA_TYPE_VIDEO]), nullptr, 0);
|
|
|
+ st_index[AVMEDIA_TYPE_VIDEO]
|
|
|
+ = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, st_index[AVMEDIA_TYPE_VIDEO], -1, nullptr, 0);
|
|
|
+ st_index[AVMEDIA_TYPE_AUDIO] = av_find_best_stream(ic,
|
|
|
+ AVMEDIA_TYPE_AUDIO,
|
|
|
+ st_index[AVMEDIA_TYPE_AUDIO],
|
|
|
+ st_index[AVMEDIA_TYPE_VIDEO],
|
|
|
+ nullptr,
|
|
|
+ 0);
|
|
|
+ st_index[AVMEDIA_TYPE_SUBTITLE] = av_find_best_stream(ic,
|
|
|
+ AVMEDIA_TYPE_SUBTITLE,
|
|
|
+ st_index[AVMEDIA_TYPE_SUBTITLE],
|
|
|
+ (st_index[AVMEDIA_TYPE_AUDIO] >= 0
|
|
|
+ ? st_index[AVMEDIA_TYPE_AUDIO]
|
|
|
+ : st_index[AVMEDIA_TYPE_VIDEO]),
|
|
|
+ nullptr,
|
|
|
+ 0);
|
|
|
|
|
|
/* open the streams */
|
|
|
- if (st_index[AVMEDIA_TYPE_VIDEO] >= 0)
|
|
|
- {
|
|
|
+ if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
|
|
|
stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
|
|
|
}
|
|
|
|
|
|
- if (st_index[AVMEDIA_TYPE_AUDIO] >= 0)
|
|
|
- {
|
|
|
+ if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
|
|
|
stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
|
|
|
}
|
|
|
|
|
|
- if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0)
|
|
|
- {
|
|
|
+ if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
|
|
|
stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
|
|
|
}
|
|
|
|
|
|
- if (is->video_stream < 0 && is->audio_stream < 0)
|
|
|
- {
|
|
|
- av_log(nullptr, AV_LOG_FATAL, "Failed to open file '%s' or configure filtergraph\n", is->filename);
|
|
|
+ if (is->video_stream < 0 && is->audio_stream < 0) {
|
|
|
+ av_log(nullptr,
|
|
|
+ AV_LOG_FATAL,
|
|
|
+ "Failed to open file '%s' or configure filtergraph\n",
|
|
|
+ is->filename);
|
|
|
ret = -1;
|
|
|
goto fail;
|
|
|
}
|
|
|
@@ -219,7 +224,7 @@ VideoState* VideoStateData::stream_open(const char* filename, const AVInputForma
|
|
|
int startup_volume = 100;
|
|
|
int av_sync_type = AV_SYNC_AUDIO_MASTER;
|
|
|
|
|
|
- is = (VideoState*)av_mallocz(sizeof(VideoState));
|
|
|
+ is = (VideoState*) av_mallocz(sizeof(VideoState));
|
|
|
if (!is)
|
|
|
return nullptr;
|
|
|
is->last_video_stream = is->video_stream = -1;
|
|
|
@@ -240,13 +245,11 @@ VideoState* VideoStateData::stream_open(const char* filename, const AVInputForma
|
|
|
if (frame_queue_init(&is->sampq, &is->audioq, SAMPLE_QUEUE_SIZE, 1) < 0)
|
|
|
goto fail;
|
|
|
|
|
|
- if (packet_queue_init(&is->videoq) < 0 ||
|
|
|
- packet_queue_init(&is->audioq) < 0 ||
|
|
|
- packet_queue_init(&is->subtitleq) < 0)
|
|
|
+ if (packet_queue_init(&is->videoq) < 0 || packet_queue_init(&is->audioq) < 0
|
|
|
+ || packet_queue_init(&is->subtitleq) < 0)
|
|
|
goto fail;
|
|
|
|
|
|
- if (!(is->continue_read_thread = new QWaitCondition()))
|
|
|
- {
|
|
|
+ if (!(is->continue_read_thread = new QWaitCondition())) {
|
|
|
av_log(nullptr, AV_LOG_FATAL, "new QWaitCondition() failed!\n");
|
|
|
goto fail;
|
|
|
}
|
|
|
@@ -302,8 +305,7 @@ void VideoStateData::read_thread_exit_wait(VideoState* is)
|
|
|
if (is->read_thread_exit != 0)
|
|
|
return;
|
|
|
|
|
|
- if (is->threads.read_tid)
|
|
|
- {
|
|
|
+ if (is->threads.read_tid) {
|
|
|
av_log(nullptr, AV_LOG_INFO, "read thread wait before!\n");
|
|
|
is->threads.read_tid->wait();
|
|
|
av_log(nullptr, AV_LOG_INFO, "read thread wait after!\n");
|
|
|
@@ -316,32 +318,27 @@ void VideoStateData::threads_exit_wait(VideoState* is)
|
|
|
if (!is)
|
|
|
return;
|
|
|
|
|
|
- if (is->threads.video_play_tid)
|
|
|
- {
|
|
|
+ if (is->threads.video_play_tid) {
|
|
|
is->threads.video_play_tid->wait();
|
|
|
is->threads.video_play_tid = nullptr;
|
|
|
}
|
|
|
|
|
|
- if (is->threads.audio_play_tid)
|
|
|
- {
|
|
|
+ if (is->threads.audio_play_tid) {
|
|
|
is->threads.audio_play_tid->wait();
|
|
|
is->threads.audio_play_tid = nullptr;
|
|
|
}
|
|
|
|
|
|
- if (is->threads.video_decode_tid)
|
|
|
- {
|
|
|
+ if (is->threads.video_decode_tid) {
|
|
|
is->threads.video_decode_tid->wait();
|
|
|
is->threads.video_decode_tid = nullptr;
|
|
|
}
|
|
|
|
|
|
- if (is->threads.audio_decode_tid)
|
|
|
- {
|
|
|
+ if (is->threads.audio_decode_tid) {
|
|
|
is->threads.audio_decode_tid->wait();
|
|
|
is->threads.audio_decode_tid = nullptr;
|
|
|
}
|
|
|
|
|
|
- if (is->threads.subtitle_decode_tid)
|
|
|
- {
|
|
|
+ if (is->threads.subtitle_decode_tid) {
|
|
|
is->threads.subtitle_decode_tid->wait();
|
|
|
is->threads.subtitle_decode_tid = nullptr;
|
|
|
}
|
|
|
@@ -384,8 +381,7 @@ void VideoStateData::stream_close(VideoState* is)
|
|
|
frame_queue_destory(&is->sampq);
|
|
|
frame_queue_destory(&is->subpq);
|
|
|
|
|
|
- if (is->continue_read_thread)
|
|
|
- {
|
|
|
+ if (is->continue_read_thread) {
|
|
|
delete is->continue_read_thread;
|
|
|
is->continue_read_thread = nullptr;
|
|
|
}
|
|
|
@@ -406,13 +402,12 @@ void VideoStateData::stream_close(VideoState* is)
|
|
|
|
|
|
static enum AVPixelFormat get_hw_format(AVCodecContext* ctx, const enum AVPixelFormat* pix_fmts)
|
|
|
{
|
|
|
- for (const enum AVPixelFormat* p = pix_fmts; *p != -1; p++)
|
|
|
- {
|
|
|
+ for (const enum AVPixelFormat* p = pix_fmts; *p != -1; p++) {
|
|
|
if (*p == hw_pix_fmt)
|
|
|
return *p;
|
|
|
}
|
|
|
|
|
|
- fprintf(stderr, "Failed to get HW surface format, codec_id=%d\n", (int)ctx->codec_id);
|
|
|
+ fprintf(stderr, "Failed to get HW surface format, codec_id=%d\n", (int) ctx->codec_id);
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
}
|
|
|
|
|
|
@@ -435,8 +430,7 @@ int VideoStateData::hw_decoder_init(AVCodecContext* ctx, const enum AVHWDeviceTy
|
|
|
{
|
|
|
int err = 0;
|
|
|
|
|
|
- if ((err = av_hwdevice_ctx_create(&m_hw_device_ctx, type, nullptr, nullptr, 0)) < 0)
|
|
|
- {
|
|
|
+ if ((err = av_hwdevice_ctx_create(&m_hw_device_ctx, type, nullptr, nullptr, 0)) < 0) {
|
|
|
fprintf(stderr, "Failed to create specified HW device.\n");
|
|
|
return err;
|
|
|
}
|
|
|
@@ -479,7 +473,7 @@ int VideoStateData::stream_component_open(VideoState* is, int stream_index)
|
|
|
int ret = 0;
|
|
|
int stream_lowres = 0;
|
|
|
|
|
|
- if (stream_index < 0 || ((unsigned int)stream_index) >= ic->nb_streams)
|
|
|
+ if (stream_index < 0 || ((unsigned int) stream_index) >= ic->nb_streams)
|
|
|
return -1;
|
|
|
|
|
|
avctx = avcodec_alloc_context3(nullptr);
|
|
|
@@ -493,45 +487,46 @@ int VideoStateData::stream_component_open(VideoState* is, int stream_index)
|
|
|
|
|
|
codec = avcodec_find_decoder(avctx->codec_id);
|
|
|
|
|
|
- switch (avctx->codec_type)
|
|
|
- {
|
|
|
- case AVMEDIA_TYPE_AUDIO:
|
|
|
- is->last_audio_stream = stream_index;
|
|
|
- break;
|
|
|
- case AVMEDIA_TYPE_SUBTITLE:
|
|
|
- is->last_subtitle_stream = stream_index;
|
|
|
- break;
|
|
|
- case AVMEDIA_TYPE_VIDEO:
|
|
|
- is->last_video_stream = stream_index;
|
|
|
-
|
|
|
- if (m_bUseHardware)
|
|
|
- {
|
|
|
- m_bHardwareSuccess = false;
|
|
|
- const char* hardware_device = "dxva2"; // device = <vaapi|vdpau|dxva2|d3d11va>
|
|
|
- ret = open_hardware(avctx, codec, hardware_device);
|
|
|
- if (!ret)
|
|
|
- {
|
|
|
- qWarning("hardware-accelerated opened failed, device:%s", hardware_device);
|
|
|
- goto fail;
|
|
|
- }
|
|
|
-
|
|
|
- qInfo("hardware-accelerated opened, device:%s", hardware_device);
|
|
|
- m_bHardwareSuccess = true;
|
|
|
+ switch (avctx->codec_type) {
|
|
|
+ case AVMEDIA_TYPE_AUDIO:
|
|
|
+ is->last_audio_stream = stream_index;
|
|
|
+ break;
|
|
|
+ case AVMEDIA_TYPE_SUBTITLE:
|
|
|
+ is->last_subtitle_stream = stream_index;
|
|
|
+ break;
|
|
|
+ case AVMEDIA_TYPE_VIDEO:
|
|
|
+ is->last_video_stream = stream_index;
|
|
|
+
|
|
|
+ if (m_bUseHardware) {
|
|
|
+ m_bHardwareSuccess = false;
|
|
|
+ const char* hardware_device = "dxva2"; // device = <vaapi|vdpau|dxva2|d3d11va>
|
|
|
+ ret = open_hardware(avctx, codec, hardware_device);
|
|
|
+ if (!ret) {
|
|
|
+ qWarning("hardware-accelerated opened failed, device:%s", hardware_device);
|
|
|
+ goto fail;
|
|
|
}
|
|
|
- break;
|
|
|
+
|
|
|
+ qInfo("hardware-accelerated opened, device:%s", hardware_device);
|
|
|
+ m_bHardwareSuccess = true;
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
- if (!codec)
|
|
|
- {
|
|
|
- av_log(nullptr, AV_LOG_WARNING, "No decoder could be found for codec %s\n", avcodec_get_name(avctx->codec_id));
|
|
|
+ if (!codec) {
|
|
|
+ av_log(nullptr,
|
|
|
+ AV_LOG_WARNING,
|
|
|
+ "No decoder could be found for codec %s\n",
|
|
|
+ avcodec_get_name(avctx->codec_id));
|
|
|
ret = AVERROR(EINVAL);
|
|
|
goto fail;
|
|
|
}
|
|
|
|
|
|
avctx->codec_id = codec->id;
|
|
|
- if (stream_lowres > codec->max_lowres)
|
|
|
- {
|
|
|
- av_log(avctx, AV_LOG_WARNING, "The maximum value for lowres supported by the decoder is %d\n", codec->max_lowres);
|
|
|
+ if (stream_lowres > codec->max_lowres) {
|
|
|
+ av_log(avctx,
|
|
|
+ AV_LOG_WARNING,
|
|
|
+ "The maximum value for lowres supported by the decoder is %d\n",
|
|
|
+ codec->max_lowres);
|
|
|
stream_lowres = codec->max_lowres;
|
|
|
}
|
|
|
avctx->lowres = stream_lowres;
|
|
|
@@ -543,83 +538,85 @@ int VideoStateData::stream_component_open(VideoState* is, int stream_index)
|
|
|
if (stream_lowres)
|
|
|
av_dict_set_int(&opts, "lowres", stream_lowres, 0);*/
|
|
|
|
|
|
- if ((ret = avcodec_open2(avctx, codec, &opts)) < 0)
|
|
|
- {
|
|
|
+ if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
|
|
|
goto fail;
|
|
|
}
|
|
|
|
|
|
is->eof = 0;
|
|
|
ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
|
|
|
- switch (avctx->codec_type)
|
|
|
- {
|
|
|
- case AVMEDIA_TYPE_AUDIO:
|
|
|
+ switch (avctx->codec_type) {
|
|
|
+ case AVMEDIA_TYPE_AUDIO:
|
|
|
#if USE_AVFILTER_AUDIO
|
|
|
- {
|
|
|
- AVFilterContext* sink;
|
|
|
- // const char* afilters =
|
|
|
- // "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono"; //
|
|
|
- // "atempo=2"; const char* afilters = nullptr; const char* afilters =
|
|
|
- // "atempo=2.0";
|
|
|
- is->audio_filter_src.freq = avctx->sample_rate;
|
|
|
- is->audio_filter_src.ch_layout.nb_channels = avctx->ch_layout.nb_channels; // avctx->channels;
|
|
|
- is->audio_filter_src.ch_layout = avctx->ch_layout; // avctx->channel_layout
|
|
|
- is->audio_filter_src.fmt = avctx->sample_fmt;
|
|
|
- if ((ret = configure_audio_filters(is, is->afilters, 0)) < 0)
|
|
|
- goto fail;
|
|
|
-
|
|
|
- sink = is->out_audio_filter;
|
|
|
- sample_rate = av_buffersink_get_sample_rate(sink);
|
|
|
- nb_channels = av_buffersink_get_channels(sink);
|
|
|
- // channel_layout = av_buffersink_get_channel_layout(sink);
|
|
|
- format = av_buffersink_get_format(sink);
|
|
|
- AVChannelLayout chn_layout;
|
|
|
- av_buffersink_get_ch_layout(sink, &chn_layout);
|
|
|
- qDebug("afilter sink: sample rate:%d, chn:%d, fmt:%d, chn_layout:%d", sample_rate, nb_channels, format, chn_layout.u);
|
|
|
- }
|
|
|
+ {
|
|
|
+ AVFilterContext* sink;
|
|
|
+ // const char* afilters =
|
|
|
+ // "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono"; //
|
|
|
+ // "atempo=2"; const char* afilters = nullptr; const char* afilters =
|
|
|
+ // "atempo=2.0";
|
|
|
+ is->audio_filter_src.freq = avctx->sample_rate;
|
|
|
+ is->audio_filter_src.ch_layout.nb_channels = avctx->ch_layout.nb_channels; // avctx->channels;
|
|
|
+ is->audio_filter_src.ch_layout = avctx->ch_layout; // avctx->channel_layout
|
|
|
+ is->audio_filter_src.fmt = avctx->sample_fmt;
|
|
|
+ if ((ret = configure_audio_filters(is, is->afilters, 0)) < 0)
|
|
|
+ goto fail;
|
|
|
+
|
|
|
+ sink = is->out_audio_filter;
|
|
|
+ sample_rate = av_buffersink_get_sample_rate(sink);
|
|
|
+ nb_channels = av_buffersink_get_channels(sink);
|
|
|
+ // channel_layout = av_buffersink_get_channel_layout(sink);
|
|
|
+ format = av_buffersink_get_format(sink);
|
|
|
+ AVChannelLayout chn_layout;
|
|
|
+ av_buffersink_get_ch_layout(sink, &chn_layout);
|
|
|
+ qDebug("afilter sink: sample rate:%d, chn:%d, fmt:%d, chn_layout:%d",
|
|
|
+ sample_rate,
|
|
|
+ nb_channels,
|
|
|
+ format,
|
|
|
+ chn_layout.u);
|
|
|
+ }
|
|
|
#else
|
|
|
- sample_rate = avctx->sample_rate;
|
|
|
- ret = av_channel_layout_copy(&ch_layout, &avctx->ch_layout);
|
|
|
- if (ret < 0)
|
|
|
- goto fail;
|
|
|
+ sample_rate = avctx->sample_rate;
|
|
|
+ ret = av_channel_layout_copy(&ch_layout, &avctx->ch_layout);
|
|
|
+ if (ret < 0)
|
|
|
+ goto fail;
|
|
|
|
|
|
#endif
|
|
|
- /* prepare audio output */
|
|
|
- /*if ((ret = audio_open(is, chn_layout, nb_channels, sample_rate,
|
|
|
+ /* prepare audio output */
|
|
|
+ /*if ((ret = audio_open(is, chn_layout, nb_channels, sample_rate,
|
|
|
&is->audio_tgt)) < 0) goto fail;
|
|
|
|
|
|
is->audio_src = is->audio_tgt;*/
|
|
|
|
|
|
- is->audio_stream = stream_index;
|
|
|
- is->audio_st = ic->streams[stream_index];
|
|
|
+ is->audio_stream = stream_index;
|
|
|
+ is->audio_st = ic->streams[stream_index];
|
|
|
|
|
|
- if ((is->ic->iformat->flags & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK)))
|
|
|
- {
|
|
|
- is->auddec.start_pts = is->audio_st->start_time;
|
|
|
- is->auddec.start_pts_tb = is->audio_st->time_base;
|
|
|
- }
|
|
|
+ if ((is->ic->iformat->flags
|
|
|
+ & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK))) {
|
|
|
+ is->auddec.start_pts = is->audio_st->start_time;
|
|
|
+ is->auddec.start_pts_tb = is->audio_st->time_base;
|
|
|
+ }
|
|
|
|
|
|
- m_bHasAudio = true;
|
|
|
- m_avctxAudio = avctx;
|
|
|
- break;
|
|
|
+ m_bHasAudio = true;
|
|
|
+ m_avctxAudio = avctx;
|
|
|
+ break;
|
|
|
|
|
|
- case AVMEDIA_TYPE_VIDEO:
|
|
|
- is->video_stream = stream_index;
|
|
|
- is->video_st = ic->streams[stream_index];
|
|
|
+ case AVMEDIA_TYPE_VIDEO:
|
|
|
+ is->video_stream = stream_index;
|
|
|
+ is->video_st = ic->streams[stream_index];
|
|
|
|
|
|
- m_bHasVideo = true;
|
|
|
- m_avctxVideo = avctx;
|
|
|
- break;
|
|
|
+ m_bHasVideo = true;
|
|
|
+ m_avctxVideo = avctx;
|
|
|
+ break;
|
|
|
|
|
|
- case AVMEDIA_TYPE_SUBTITLE:
|
|
|
- is->subtitle_stream = stream_index;
|
|
|
- is->subtitle_st = ic->streams[stream_index];
|
|
|
+ case AVMEDIA_TYPE_SUBTITLE:
|
|
|
+ is->subtitle_stream = stream_index;
|
|
|
+ is->subtitle_st = ic->streams[stream_index];
|
|
|
|
|
|
- m_bHasSubtitle = true;
|
|
|
- m_avctxSubtitle = avctx;
|
|
|
- break;
|
|
|
+ m_bHasSubtitle = true;
|
|
|
+ m_avctxSubtitle = avctx;
|
|
|
+ break;
|
|
|
|
|
|
- default:
|
|
|
- break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
goto out;
|
|
|
@@ -637,63 +634,61 @@ void VideoStateData::stream_component_close(VideoState* is, int stream_index)
|
|
|
AVFormatContext* ic = is->ic;
|
|
|
AVCodecParameters* codecpar;
|
|
|
|
|
|
- if (stream_index < 0 || ((unsigned int)stream_index) >= ic->nb_streams)
|
|
|
+ if (stream_index < 0 || ((unsigned int) stream_index) >= ic->nb_streams)
|
|
|
return;
|
|
|
|
|
|
codecpar = ic->streams[stream_index]->codecpar;
|
|
|
|
|
|
- switch (codecpar->codec_type)
|
|
|
- {
|
|
|
- case AVMEDIA_TYPE_AUDIO:
|
|
|
- decoder_abort(&is->auddec, &is->sampq);
|
|
|
- // SDL_CloseAudioDevice(audio_dev);
|
|
|
- decoder_destroy(&is->auddec);
|
|
|
+ switch (codecpar->codec_type) {
|
|
|
+ case AVMEDIA_TYPE_AUDIO:
|
|
|
+ decoder_abort(&is->auddec, &is->sampq);
|
|
|
+ // SDL_CloseAudioDevice(audio_dev);
|
|
|
+ decoder_destroy(&is->auddec);
|
|
|
|
|
|
- // swr_free(&is->swr_ctx);
|
|
|
- // av_freep(&is->audio_buf1);
|
|
|
- // is->audio_buf1_size = 0;
|
|
|
- // is->audio_buf = nullptr;
|
|
|
+ // swr_free(&is->swr_ctx);
|
|
|
+ // av_freep(&is->audio_buf1);
|
|
|
+ // is->audio_buf1_size = 0;
|
|
|
+ // is->audio_buf = nullptr;
|
|
|
|
|
|
- /*if (is->rdft) {
|
|
|
+ /*if (is->rdft) {
|
|
|
av_rdft_end(is->rdft);
|
|
|
av_freep(&is->rdft_data);
|
|
|
is->rdft = nullptr;
|
|
|
is->rdft_bits = 0;
|
|
|
}*/
|
|
|
- break;
|
|
|
+ break;
|
|
|
|
|
|
- case AVMEDIA_TYPE_VIDEO:
|
|
|
- decoder_abort(&is->viddec, &is->pictq);
|
|
|
- decoder_destroy(&is->viddec);
|
|
|
- break;
|
|
|
+ case AVMEDIA_TYPE_VIDEO:
|
|
|
+ decoder_abort(&is->viddec, &is->pictq);
|
|
|
+ decoder_destroy(&is->viddec);
|
|
|
+ break;
|
|
|
|
|
|
- case AVMEDIA_TYPE_SUBTITLE:
|
|
|
- decoder_abort(&is->subdec, &is->subpq);
|
|
|
- decoder_destroy(&is->subdec);
|
|
|
- break;
|
|
|
+ case AVMEDIA_TYPE_SUBTITLE:
|
|
|
+ decoder_abort(&is->subdec, &is->subpq);
|
|
|
+ decoder_destroy(&is->subdec);
|
|
|
+ break;
|
|
|
|
|
|
- default:
|
|
|
- qDebug("Not handled yet.......code type:%d", codecpar->codec_type);
|
|
|
- break;
|
|
|
+ default:
|
|
|
+ qDebug("Not handled yet.......code type:%d", codecpar->codec_type);
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
ic->streams[stream_index]->discard = AVDISCARD_ALL;
|
|
|
- switch (codecpar->codec_type)
|
|
|
- {
|
|
|
- case AVMEDIA_TYPE_AUDIO:
|
|
|
- is->audio_st = nullptr;
|
|
|
- is->audio_stream = -1;
|
|
|
- break;
|
|
|
- case AVMEDIA_TYPE_VIDEO:
|
|
|
- is->video_st = nullptr;
|
|
|
- is->video_stream = -1;
|
|
|
- break;
|
|
|
- case AVMEDIA_TYPE_SUBTITLE:
|
|
|
- is->subtitle_st = nullptr;
|
|
|
- is->subtitle_stream = -1;
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
+ switch (codecpar->codec_type) {
|
|
|
+ case AVMEDIA_TYPE_AUDIO:
|
|
|
+ is->audio_st = nullptr;
|
|
|
+ is->audio_stream = -1;
|
|
|
+ break;
|
|
|
+ case AVMEDIA_TYPE_VIDEO:
|
|
|
+ is->video_st = nullptr;
|
|
|
+ is->video_stream = -1;
|
|
|
+ break;
|
|
|
+ case AVMEDIA_TYPE_SUBTITLE:
|
|
|
+ is->subtitle_st = nullptr;
|
|
|
+ is->subtitle_stream = -1;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -715,19 +710,18 @@ bool VideoStateData::has_subtitle() const
|
|
|
AVCodecContext* VideoStateData::get_contex(AVMediaType type) const
|
|
|
{
|
|
|
AVCodecContext* pCtx = nullptr;
|
|
|
- switch (type)
|
|
|
- {
|
|
|
- case AVMEDIA_TYPE_AUDIO:
|
|
|
- pCtx = m_avctxAudio;
|
|
|
- break;
|
|
|
- case AVMEDIA_TYPE_VIDEO:
|
|
|
- pCtx = m_avctxVideo;
|
|
|
- break;
|
|
|
- case AVMEDIA_TYPE_SUBTITLE:
|
|
|
- pCtx = m_avctxSubtitle;
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
+ switch (type) {
|
|
|
+ case AVMEDIA_TYPE_AUDIO:
|
|
|
+ pCtx = m_avctxAudio;
|
|
|
+ break;
|
|
|
+ case AVMEDIA_TYPE_VIDEO:
|
|
|
+ pCtx = m_avctxVideo;
|
|
|
+ break;
|
|
|
+ case AVMEDIA_TYPE_SUBTITLE:
|
|
|
+ pCtx = m_avctxSubtitle;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
return pCtx;
|
|
|
}
|
|
|
@@ -736,8 +730,7 @@ enum AVHWDeviceType VideoStateData::get_hwdevice(const char* device) const
|
|
|
{
|
|
|
// device = <vaapi|vdpau|dxva2|d3d11va>
|
|
|
enum AVHWDeviceType type = av_hwdevice_find_type_by_name(device);
|
|
|
- if (type == AV_HWDEVICE_TYPE_NONE)
|
|
|
- {
|
|
|
+ if (type == AV_HWDEVICE_TYPE_NONE) {
|
|
|
av_log(nullptr, AV_LOG_WARNING, "Device type %s is not supported.\n", device);
|
|
|
|
|
|
av_log(nullptr, AV_LOG_INFO, "Available device types:");
|
|
|
@@ -749,21 +742,24 @@ enum AVHWDeviceType VideoStateData::get_hwdevice(const char* device) const
|
|
|
return type;
|
|
|
}
|
|
|
|
|
|
-enum AVPixelFormat VideoStateData::get_hwdevice_decoder(const AVCodec* decoder, enum AVHWDeviceType type) const
|
|
|
+enum AVPixelFormat VideoStateData::get_hwdevice_decoder(const AVCodec* decoder,
|
|
|
+ enum AVHWDeviceType type) const
|
|
|
{
|
|
|
if (!decoder || AV_HWDEVICE_TYPE_NONE == type)
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
|
|
|
- for (int i = 0;; i++)
|
|
|
- {
|
|
|
+ for (int i = 0;; i++) {
|
|
|
const AVCodecHWConfig* config = avcodec_get_hw_config(decoder, i);
|
|
|
- if (!config)
|
|
|
- {
|
|
|
- av_log(nullptr, AV_LOG_WARNING, "Decoder %s does not support device type %s.\n", decoder->name, av_hwdevice_get_type_name(type));
|
|
|
+ if (!config) {
|
|
|
+ av_log(nullptr,
|
|
|
+ AV_LOG_WARNING,
|
|
|
+ "Decoder %s does not support device type %s.\n",
|
|
|
+ decoder->name,
|
|
|
+ av_hwdevice_get_type_name(type));
|
|
|
return AV_PIX_FMT_NONE;
|
|
|
}
|
|
|
- if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX && config->device_type == type)
|
|
|
- {
|
|
|
+ if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
|
|
|
+ && config->device_type == type) {
|
|
|
return config->pix_fmt;
|
|
|
}
|
|
|
}
|