Skip to content

Commit

Permalink
优化AVStream数据解析;
Browse files Browse the repository at this point in the history
优化代码;
  • Loading branch information
RealChuan committed Nov 29, 2023
1 parent 81911b1 commit 2de89a9
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 198 deletions.
2 changes: 1 addition & 1 deletion ffmpeg/audiodecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AudioDecoder::AudioDecoderPrivate

void processEvent()
{
while (q_ptr->m_runing.load() && q_ptr->m_eventQueue.size() > 0) {
while (q_ptr->m_runing.load() && !q_ptr->m_eventQueue.empty()) {
auto eventPtr = q_ptr->m_eventQueue.take();
switch (eventPtr->type()) {
case Event::EventType::Pause: decoderAudioFrame->addEvent(eventPtr); break;
Expand Down
9 changes: 5 additions & 4 deletions ffmpeg/audiodisplay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class AudioDisplay::AudioDisplayPrivate

void processEvent(bool &firstFrame)
{
while (q_ptr->m_runing.load() && q_ptr->m_eventQueue.size() > 0) {
while (q_ptr->m_runing.load() && !q_ptr->m_eventQueue.empty()) {
qDebug() << "AudioFramePrivate::processEvent";
auto eventPtr = q_ptr->m_eventQueue.take();
switch (eventPtr->type()) {
case Event::EventType::Pause: {
auto pauseEvent = static_cast<PauseEvent *>(eventPtr.data());
auto *pauseEvent = static_cast<PauseEvent *>(eventPtr.data());
auto paused = pauseEvent->paused();
clock->setPaused(paused);
} break;
Expand Down Expand Up @@ -68,7 +68,7 @@ AudioDisplay::~AudioDisplay()
void AudioDisplay::setVolume(qreal volume)
{
d_ptr->volume = volume;
if (d_ptr->audioOutputThreadPtr) {
if (d_ptr->audioOutputThreadPtr != nullptr) {
emit d_ptr->audioOutputThreadPtr->volumeChanged(d_ptr->volume);
}
}
Expand All @@ -91,7 +91,8 @@ void AudioDisplay::runDecoder()
auto framePtr(m_queue.take());
if (framePtr.isNull()) {
continue;
} else if (!firstFrame) {
}
if (!firstFrame) {
qDebug() << "Audio firstFrame: "
<< QTime::fromMSecsSinceStartOfDay(framePtr->pts() / 1000)
.toString("hh:mm:ss.zzz");
Expand Down
13 changes: 7 additions & 6 deletions ffmpeg/audioframeconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern "C" {

namespace Ffmpeg {

auto getChannaLayoutFromChannalCount(int nb_channals) -> qint64
static auto getChannaLayoutFromChannalCount(int nb_channals) -> qint64
{
qint64 channalLayout = 0;
switch (nb_channals) {
Expand All @@ -33,7 +33,7 @@ auto getChannaLayoutFromChannalCount(int nb_channals) -> qint64
return channalLayout;
}

auto getChannelLayout(QAudioFormat::ChannelConfig channelConfig) -> qint64
static auto getChannelLayout(QAudioFormat::ChannelConfig channelConfig) -> qint64
{
qint64 channal = 0;
switch (channelConfig) {
Expand All @@ -52,7 +52,7 @@ auto getChannelLayout(QAudioFormat::ChannelConfig channelConfig) -> qint64

return channal;
}
auto getChannelConfig(qint64 channalLayout) -> QAudioFormat::ChannelConfig
static auto getChannelConfig(qint64 channalLayout) -> QAudioFormat::ChannelConfig
{
auto config = QAudioFormat::ChannelConfigUnknown;
switch (channalLayout) {
Expand All @@ -71,7 +71,7 @@ auto getChannelConfig(qint64 channalLayout) -> QAudioFormat::ChannelConfig
return config;
}

auto getAVSampleFormat(QAudioFormat::SampleFormat format) -> AVSampleFormat
static auto getAVSampleFormat(QAudioFormat::SampleFormat format) -> AVSampleFormat
{
auto sampleFormat = AV_SAMPLE_FMT_NONE;
switch (format) {
Expand All @@ -85,7 +85,7 @@ auto getAVSampleFormat(QAudioFormat::SampleFormat format) -> AVSampleFormat
return sampleFormat;
}

auto getSampleFormat(AVSampleFormat format) -> QAudioFormat::SampleFormat
static auto getSampleFormat(AVSampleFormat format) -> QAudioFormat::SampleFormat
{
auto sampleFormat = QAudioFormat::Unknown;
switch (format) {
Expand Down Expand Up @@ -154,7 +154,8 @@ auto AudioFrameConverter::convert(Frame *frame) -> QByteArray
{
auto *avFrame = frame->avFrame();
auto nb_samples = avFrame->nb_samples;
auto out_count = (int64_t) nb_samples * d_ptr->format.sampleRate() / avFrame->sample_rate
auto out_count = static_cast<int64_t>(nb_samples) * d_ptr->format.sampleRate()
/ avFrame->sample_rate
+ 256; // 256 copy from ffplay
auto size = av_samples_get_buffer_size(nullptr,
d_ptr->format.channelCount(),
Expand Down
44 changes: 1 addition & 43 deletions ffmpeg/avcontextinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,6 @@ class AVContextInfo::AVContextInfoPrivate
: q_ptr(q)
{}

void printCodecpar()
{
auto *codecpar = stream->codecpar;
qInfo() << "start_time: " << stream->start_time;
qInfo() << "duration: " << stream->duration;
qInfo() << "nb_frames: " << stream->nb_frames;
qInfo() << "format: " << codecpar->format;
qInfo() << "bit_rate: " << codecpar->bit_rate;
switch (codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO:
qInfo() << "avg_frame_rate: " << av_q2d(stream->avg_frame_rate);
qInfo() << "sample_aspect_ratio: " << av_q2d(stream->sample_aspect_ratio);
qInfo() << "Resolution of resolution: " << codecpar->width << "x" << codecpar->height;
qInfo() << "color_range: " << av_color_range_name(codecpar->color_range);
qInfo() << "color_primaries: " << av_color_primaries_name(codecpar->color_primaries);
qInfo() << "color_trc: " << av_color_transfer_name(codecpar->color_trc);
qInfo() << "color_space: " << av_color_space_name(codecpar->color_space);
qInfo() << "chroma_location: " << av_chroma_location_name(codecpar->chroma_location);
qInfo() << "video_delay: " << codecpar->video_delay;
break;
case AVMEDIA_TYPE_AUDIO:
qInfo() << "channels: " << codecpar->channels;
qInfo() << "channel_layout: " << codecpar->channel_layout;
qInfo() << "sample_rate: " << codecpar->sample_rate;
qInfo() << "frame_size: " << codecpar->frame_size;
break;
default: break;
}
}

void printMetaData()
{
QMap<QString, QString> maps;
AVDictionaryEntry *tag = nullptr;
while (nullptr != (tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
maps.insert(tag->key, QString::fromUtf8(tag->value));
}
qDebug() << maps;
}

AVContextInfo *q_ptr;

QScopedPointer<CodecContext> codecCtx; //解码器上下文
Expand Down Expand Up @@ -110,8 +70,6 @@ auto AVContextInfo::isIndexVaild() -> bool
void AVContextInfo::setStream(AVStream *stream)
{
d_ptr->stream = stream;
// d_ptr->printCodecpar();
// d_ptr->printMetaData();
}

auto AVContextInfo::stream() -> AVStream *
Expand All @@ -122,7 +80,7 @@ auto AVContextInfo::stream() -> AVStream *
auto AVContextInfo::initDecoder(const AVRational &frameRate) -> bool
{
Q_ASSERT(d_ptr->stream != nullptr);
const char *typeStr = av_get_media_type_string(d_ptr->stream->codecpar->codec_type);
const auto *typeStr = av_get_media_type_string(d_ptr->stream->codecpar->codec_type);
const auto *codec = avcodec_find_decoder(d_ptr->stream->codecpar->codec_id);
if (codec == nullptr) {
qWarning() << tr("%1 Codec not found.").arg(typeStr);
Expand Down
95 changes: 44 additions & 51 deletions ffmpeg/formatcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ extern "C" {

namespace Ffmpeg {

static void queryStreamInfo(int bestIndex, StreamInfo &streamInfo, StreamInfos &streamInfos)
{
if (bestIndex == streamInfo.index) {
streamInfo.defaultSelected = true;
streamInfo.selected = true;
}
streamInfos.append(streamInfo);
}

class FormatContext::FormatContextPrivate
{
public:
Expand All @@ -37,38 +46,21 @@ class FormatContext::FormatContextPrivate

//nb_streams视音频流的个数
for (uint i = 0; i < formatCtx->nb_streams; i++) {
StreamInfo streamInfo;
StreamInfo streamInfo(formatCtx->streams[i]);
streamInfo.index = i;
streamInfo.parseStreamData(formatCtx->streams[i]);

switch (formatCtx->streams[i]->codecpar->codec_type) {
case AVMEDIA_TYPE_AUDIO:
if (bestAudioIndex == i) {
streamInfo.defaultSelected = true;
streamInfo.selected = true;
}
audioTracks.append(streamInfo);
queryStreamInfo(bestAudioIndex, streamInfo, audioTracks);
break;
case AVMEDIA_TYPE_VIDEO:
if (bestVideoIndex == i) {
streamInfo.defaultSelected = true;
streamInfo.selected = true;
}
videoTracks.append(streamInfo);
queryStreamInfo(bestVideoIndex, streamInfo, videoTracks);
break;
case AVMEDIA_TYPE_SUBTITLE:
if (bestSubtitleIndex == i) {
streamInfo.defaultSelected = true;
streamInfo.selected = true;
}
subtitleTracks.append(streamInfo);
queryStreamInfo(bestSubtitleIndex, streamInfo, subtitleTracks);
break;
case AVMEDIA_TYPE_ATTACHMENT:
if (bestAttachmentIndex == i) {
streamInfo.defaultSelected = true;
streamInfo.selected = true;
}
attachmentTracks.append(streamInfo);
queryStreamInfo(bestAttachmentIndex, streamInfo, attachmentTracks);
break;
default: break;
}
Expand All @@ -88,10 +80,10 @@ class FormatContext::FormatContextPrivate
FormatContext::OpenMode mode = FormatContext::ReadOnly;
bool isOpen = false;

QVector<StreamInfo> audioTracks;
QVector<StreamInfo> videoTracks;
QVector<StreamInfo> subtitleTracks;
QVector<StreamInfo> attachmentTracks;
StreamInfos audioTracks;
StreamInfos videoTracks;
StreamInfos subtitleTracks;
StreamInfos attachmentTracks;

const qint64 seekOffset = 2 * AV_TIME_BASE;
};
Expand All @@ -108,19 +100,19 @@ FormatContext::~FormatContext()

void FormatContext::copyChapterFrom(FormatContext *src)
{
auto is = src->avFormatContext();
auto os = d_ptr->formatCtx;
AVChapter **tmp = (AVChapter **) av_realloc_f(os->chapters,
is->nb_chapters + os->nb_chapters,
sizeof(*os->chapters));
if (!tmp) {
auto *is = src->avFormatContext();
auto *os = d_ptr->formatCtx;
auto **tmp = static_cast<AVChapter **>(
av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters, sizeof(*os->chapters)));
if (tmp == nullptr) {
qWarning() << tr("Memory request error");
return;
}
os->chapters = tmp;

for (uint i = 0; i < is->nb_chapters; i++) {
AVChapter *in_ch = is->chapters[i], *out_ch;
auto *in_ch = is->chapters[i];
AVChapter *out_ch;
int64_t start_time = 0;
int64_t ts_off = av_rescale_q(start_time, AVRational{1, AV_TIME_BASE}, in_ch->time_base);
int64_t rt = INT64_MAX;
Expand All @@ -131,8 +123,8 @@ void FormatContext::copyChapterFrom(FormatContext *src)
if (rt != INT64_MAX && in_ch->start > rt + ts_off) {
break;
}
out_ch = (AVChapter *) av_mallocz(sizeof(AVChapter));
if (!out_ch) {
out_ch = static_cast<AVChapter *>(av_mallocz(sizeof(AVChapter)));
if (out_ch == nullptr) {
qWarning() << tr("Memory request error");
return;
}
Expand Down Expand Up @@ -199,15 +191,15 @@ void FormatContext::close()
d_ptr->formatCtx = nullptr;
d_ptr->isOpen = false;
break;
case WriteOnly: avio_close(); break;
case WriteOnly: avioClose(); break;
default: break;
}
}

auto FormatContext::avio_open() -> bool
auto FormatContext::avioOpen() -> bool
{
Q_ASSERT(d_ptr->formatCtx != nullptr);
if (d_ptr->formatCtx->oformat->flags & AVFMT_NOFILE) {
if ((d_ptr->formatCtx->oformat->flags & AVFMT_NOFILE) != 0) {
return false;
}
auto ret = ::avio_open(&d_ptr->formatCtx->pb,
Expand All @@ -216,12 +208,12 @@ auto FormatContext::avio_open() -> bool
ERROR_RETURN(ret)
}

void FormatContext::avio_close()
void FormatContext::avioClose()
{
if (!d_ptr->isOpen && d_ptr->mode != WriteOnly) {
return;
}
if (d_ptr->formatCtx && !(d_ptr->formatCtx->oformat->flags & AVFMT_NOFILE)) {
if ((d_ptr->formatCtx != nullptr) && ((d_ptr->formatCtx->oformat->flags & AVFMT_NOFILE) == 0)) {
avio_closep(&d_ptr->formatCtx->pb);
}
avformat_free_context(d_ptr->formatCtx);
Expand Down Expand Up @@ -259,7 +251,7 @@ auto FormatContext::findStream() -> bool
SET_ERROR_CODE(ret);
return false;
}
if (d_ptr->formatCtx->pb) {
if (d_ptr->formatCtx->pb != nullptr) {
// FIXME hack, ffplay maybe should not use avio_feof() to test for the end
d_ptr->formatCtx->pb->eof_reached = 0;
}
Expand All @@ -272,22 +264,22 @@ auto FormatContext::streams() const -> int
return d_ptr->formatCtx->nb_streams;
}

QVector<StreamInfo> FormatContext::audioTracks() const
auto FormatContext::audioTracks() const -> StreamInfos
{
return d_ptr->audioTracks;
}

QVector<StreamInfo> FormatContext::vidioTracks() const
auto FormatContext::vidioTracks() const -> StreamInfos
{
return d_ptr->videoTracks;
}

QVector<StreamInfo> FormatContext::subtitleTracks() const
auto FormatContext::subtitleTracks() const -> StreamInfos
{
return d_ptr->subtitleTracks;
}

QVector<StreamInfo> FormatContext::attachmentTracks() const
auto FormatContext::attachmentTracks() const -> StreamInfos
{
return d_ptr->attachmentTracks;
}
Expand All @@ -297,7 +289,7 @@ auto FormatContext::findBestStreamIndex(AVMediaType type) const -> int
return d_ptr->findBestStreamIndex(type);
}

void FormatContext::discardStreamExcluded(QVector<int> indexs)
void FormatContext::discardStreamExcluded(const QVector<int> &indexs)
{
Q_ASSERT(d_ptr->formatCtx != nullptr);
for (uint i = 0; i < d_ptr->formatCtx->nb_streams; i++) {
Expand All @@ -317,8 +309,8 @@ auto FormatContext::stream(int index) -> AVStream *
auto FormatContext::createStream() -> AVStream *
{
Q_ASSERT(d_ptr->formatCtx != nullptr);
auto stream = avformat_new_stream(d_ptr->formatCtx, nullptr);
if (!stream) {
auto *stream = avformat_new_stream(d_ptr->formatCtx, nullptr);
if (stream == nullptr) {
qWarning() << "Failed allocating output stream\n";
}
return stream;
Expand All @@ -334,7 +326,7 @@ auto FormatContext::readFrame(Packet *packet) -> bool
auto FormatContext::checkPktPlayRange(Packet *packet) -> bool
{
Q_ASSERT(d_ptr->formatCtx != nullptr);
auto avPacket = packet->avPacket();
auto *avPacket = packet->avPacket();
/* check if packet is in play range specified by user, then queue, otherwise discard */
auto start_time = AV_NOPTS_VALUE;
auto duration = AV_NOPTS_VALUE;
Expand All @@ -344,8 +336,9 @@ auto FormatContext::checkPktPlayRange(Packet *packet) -> bool
= duration == AV_NOPTS_VALUE
|| (pkt_ts - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0))
* av_q2d(d_ptr->formatCtx->streams[avPacket->stream_index]->time_base)
- (double) (start_time != AV_NOPTS_VALUE ? start_time : 0) / AV_TIME_BASE
<= ((double) duration / AV_TIME_BASE);
- static_cast<double>(start_time != AV_NOPTS_VALUE ? start_time : 0)
/ AV_TIME_BASE
<= (static_cast<double>(duration) / AV_TIME_BASE);
return pkt_in_play_range;
}

Expand Down
Loading

0 comments on commit 2de89a9

Please sign in to comment.