Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

av: fixup codec on mtk devices #4

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion include/media/MediaExtractorPluginHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class MediaTrackHelper {
virtual media_status_t read(
MediaBufferHelper **buffer, const ReadOptions *options = NULL) = 0;
virtual bool supportsNonBlockingRead() { return false; }
virtual const char * extractorName() { return ""; }
protected:
friend CMediaTrack *wrap(MediaTrackHelper *track);
MediaBufferGroupHelper *mBufferGroup;
Expand All @@ -177,7 +178,13 @@ inline CMediaTrack *wrap(MediaTrackHelper *track) {
CMediaTrack *wrapper = (CMediaTrack*) malloc(sizeof(CMediaTrack));
wrapper->data = track;
wrapper->free = [](void *data) -> void {
delete (MediaTrackHelper*)(data);
// delete MtkAVISource in ~MtkAVIExtractor
if (strcmp(((MediaTrackHelper*)data)->extractorName(), "MtkAVIExtractor")) {
delete (MediaTrackHelper*)(data);
} else {
// stop MTKAVISource to release buffer (for mp3 clone buffer)
((MediaTrackHelper*)data)->stop();
}
};
wrapper->start = [](void *data, CMediaBufferGroup *bufferGroup) -> media_status_t {
if (((MediaTrackHelper*)data)->mBufferGroup) {
Expand Down
3 changes: 2 additions & 1 deletion media/extractors/ogg/OggExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,14 +968,15 @@ void MyOggExtractor::buildTableOfContents() {
Page page;
ssize_t pageSize;
while ((pageSize = readPage(offset, &page)) > 0) {
if ((page.mFlags & 1) == 0) {
mTableOfContents.push();

TOCEntry &entry =
mTableOfContents.editItemAt(mTableOfContents.size() - 1);

entry.mPageOffset = offset;
entry.mTimeUs = getTimeUsOfGranule(page.mGranulePosition);

}
offset += (size_t)pageSize;
}

Expand Down
65 changes: 65 additions & 0 deletions media/libmediaplayerservice/nuplayer/GenericSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "GenericSource.h"
#include "NuPlayerDrm.h"

#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <cutils/properties.h>
#include <datasource/PlayerServiceDataSourceFactory.h>
Expand Down Expand Up @@ -84,6 +85,9 @@ NuPlayer::GenericSource::GenericSource(
mBufferingSettings.mInitialMarkMs = kInitialMarkMs;
mBufferingSettings.mResumePlaybackMarkMs = kResumePlaybackMarkMs;
resetDataSource();
//mtkadd+
init();
//mtkadd-
}

void NuPlayer::GenericSource::resetDataSource() {
Expand Down Expand Up @@ -201,6 +205,23 @@ status_t NuPlayer::GenericSource::initFromDataSource() {
}
}

//mtkadd+
//for mp3 low power
mExtractor = extractor;
const char *extractorName = mExtractor->name();
if (extractorName != NULL
&& !strcasecmp(extractorName, "MtkMP3Extractor")) {
mIsMtkMp3 = true;
} else if (extractorName != NULL
&& !strcasecmp(extractorName, "CAFExtractor")) {
mIsMtkAlac = true;
}
if (mIsMtkMp3 && mPID < 0xffffffff) {
ALOGV("initFromDataSource, set mPID:%d to MtkMP3Extractor!",mPID);
sp<MetaData> mp3Meta = extractor->getTrackMetaData(0, mPID);
}
//mtkadd-

int32_t totalBitrate = 0;

mMimes.clear();
Expand Down Expand Up @@ -342,6 +363,10 @@ bool NuPlayer::GenericSource::isStreaming() const {

NuPlayer::GenericSource::~GenericSource() {
ALOGV("~GenericSource");
//mtkadd for stop toc thread before source release.
if ((mIsMtkMp3 || mIsMtkAlac) && mAudioTrack.mSource != NULL) {
mAudioTrack.mSource->stop();
}
if (mLooper != NULL) {
mLooper->unregisterHandler(id());
mLooper->stop();
Expand Down Expand Up @@ -1334,6 +1359,13 @@ void NuPlayer::GenericSource::readBuffer(
case MEDIA_TRACK_TYPE_AUDIO:
track = &mAudioTrack;
maxBuffers = 64;
// mtkadd+
// add for TS, resolve 4k video play not smooth, too many buffers will cause parse audio too long time.
// it will block video parse, as TS video and audio is interleave.
if (isTS()) {
maxBuffers = 8;
}
// mtkadd-
break;
case MEDIA_TRACK_TYPE_SUBTITLE:
track = &mSubtitleTrack;
Expand Down Expand Up @@ -1715,4 +1747,37 @@ void NuPlayer::GenericSource::signalBufferReturned(MediaBufferBase *buffer)
buffer->release(); // this leads to delete since that there is no observor
}

//mtkadd+

void NuPlayer::GenericSource::init() {
mExtractor = NULL;
mIsMtkMp3 = false;
mIsMtkAlac = false;
mPID = IPCThreadState::self()->getCallingPid();
}

void NuPlayer::GenericSource::setGetMp3Param(int32_t *flag, bool set) {
if (!set) { //getflag
*flag = mIsMtkMp3 ? 1 : 0;
return;
}
//set flag
if (mIsMtkMp3 && *flag == 1) {
ALOGV("set mp3 codec sucessfully");
mExtractor->getTrackMetaData(0/*index*/, 0xffffffff/*flag*/);
}
}

// for TS
bool NuPlayer::GenericSource::isTS() {
const char *mime = NULL;
if (mFileMeta != NULL && mFileMeta->findCString(kKeyMIMEType, &mime)
&& !strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
return true;
}
return false;
}
//mtkadd-


} // namespace android
22 changes: 22 additions & 0 deletions media/libmediaplayerservice/nuplayer/NuPlayerDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {
ALOGV("onConfigure mCrypto: %p (%d) mIsSecure: %d",
crypto.get(), (crypto != NULL ? crypto->getStrongCount() : 0), mIsSecure);

//mtkadd+
if (!strcasecmp(mComponentName.c_str(), "OMX.MTK.AUDIO.DECODER.MP3")) {
int32_t mtkmp3extractorFlag = 0;
//get mtkmp3extractor flag
mSource->setGetMp3Param(&mtkmp3extractorFlag, false /*get*/);
if (mtkmp3extractorFlag == 1) {
format->setInt32("mtk-mp3extractor", 1);
format->setInt32("app-pid", (int32_t)mPid);
ALOGV("set mp3 lowpwer flag to decoder app-pid:%ld", (long)mPid);
}
}
//mtkadd-

err = mCodec->configure(
format, mSurface, crypto, 0 /* flags */);

Expand Down Expand Up @@ -394,6 +407,15 @@ void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) {

mPaused = false;
mResumePending = false;

//mtkadd+ for mp3 lowpower
int32_t mtkMp3Codec = 0;
if (!strcasecmp(mComponentName.c_str(), "OMX.MTK.AUDIO.DECODER.MP3")
&& format->findInt32("mtkMp3Codec", &mtkMp3Codec)) {
ALOGV("turn on mp3 codec lowpower mode,mtkMp3Codec:%d.",mtkMp3Codec);
mSource->setGetMp3Param(&mtkMp3Codec, true /*set*/); //set mp3 codec sucessfully
}
//mtkadd-
}

void NuPlayer::Decoder::onSetParameters(const sp<AMessage> &params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class IMediaSource;
class MediaBuffer;
struct MediaClock;
struct NuCachedSource2;
//mtkadd+
class IMediaExtractor;
//mtkadd-

struct NuPlayer::GenericSource : public NuPlayer::Source,
public MediaBufferObserver // Modular DRM
Expand Down Expand Up @@ -239,6 +242,20 @@ struct NuPlayer::GenericSource : public NuPlayer::Source,

status_t checkDrmInfo();

//mtkadd+
pid_t mPID;
sp<IMediaExtractor> mExtractor;
bool mIsMtkMp3;
bool mIsMtkAlac;
void init();
public:
virtual void setGetMp3Param(int32_t *flag, bool set);

private:
// add for TS
bool isTS();
//mtkadd-

DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ struct NuPlayer::Source : public AHandler {
// Modular DRM
void notifyDrmInfo(const sp<ABuffer> &buffer);

public:
//mtkadd+
virtual void setGetMp3Param(int32_t * /*flag*/, bool /*set*/){};
//mtkadd-

private:
sp<AMessage> mNotify;

Expand Down
62 changes: 60 additions & 2 deletions media/libstagefright/ACodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource;
using hardware::media::omx::V1_0::Status;

enum {
kMaxIndicesToCheck = 32, // used when enumerating supported formats and profiles
kMaxIndicesToCheck = 128, // used when enumerating supported formats and profiles
};

namespace {
Expand Down Expand Up @@ -622,6 +622,10 @@ ACodec::ACodec()
ACodec::~ACodec() {
}

status_t ACodec::setupAudioCodec(status_t err, const char *, bool, const sp<AMessage> &) {
return err;
}

void ACodec::initiateSetup(const sp<AMessage> &msg) {
msg->setWhat(kWhatSetup);
msg->setTarget(this);
Expand Down Expand Up @@ -1070,6 +1074,26 @@ status_t ACodec::setupNativeWindowSizeFormatAndUsage(
InitOMXParams(&def);
def.nPortIndex = kPortIndexOutput;

//mtk add query HWC usage for decoder
OMX_U32 ANW_HWComposer = 0;
OMX_PARAM_U32TYPE param;
OMX_INDEXTYPE index2;
status_t err2 = nativeWindow->query(nativeWindow, NATIVE_WINDOW_QUEUES_TO_WINDOW_COMPOSER , (int *)&ANW_HWComposer);
if (OK == err2) {
err2 = mOMXNode->getExtensionIndex("OMX.MTK.index.param.video.ANW_HWComposer", &index2);
if (OK == err2) {
InitOMXParams(&param);
param.nU32 = ANW_HWComposer;
err2 = mOMXNode->setParameter(index2, &param, sizeof(param));
if (OK != err2) {
ALOGW("Failed to set ANW_HWComposer to OMX, ignoring (%d)", err2);
}
}
}
else {
ALOGW("Failed to query NW for HWC usage, ignoring: %s (%d)", strerror(-err2), -err2);
}

status_t err = mOMXNode->getParameter(
OMX_IndexParamPortDefinition, &def, sizeof(def));

Expand Down Expand Up @@ -2174,6 +2198,14 @@ status_t ACodec::configureCodec(
sampleRate,
numChannels);
}
//mtkadd+
if (!strcmp(mComponentName.c_str(), "OMX.MTK.AUDIO.DECODER.MP3")
&& err == OK && setOmxReadMultiFrame(mOMXNode, msg) == OK) {
msg->setInt32("mtkMp3Codec", 1);
} else {
ALOGW("setOmxReadMultiFrame fail!");
}
//mtkadd-
} else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
int32_t numChannels, sampleRate;
if (!msg->findInt32("channel-count", &numChannels)
Expand Down Expand Up @@ -2334,6 +2366,9 @@ status_t ACodec::configureCodec(
} else {
err = setupAC4Codec(encoder, numChannels, sampleRate);
}
} else {
// user can choose if support mtk audio codec
err = setupAudioCodec(err, mime, encoder, msg);
}

if (err != OK) {
Expand Down Expand Up @@ -2386,6 +2421,12 @@ status_t ACodec::configureCodec(
err = OK; // ignore errors
}

//set mtk parameters
status_t err_mtkparam = setMtkParameters(mOMXNode, msg, mIsEncoder);
if (err_mtkparam != OK) {
return err_mtkparam;
}

if (err == OK) {
err = setVendorParameters(msg);
if (err != OK) {
Expand Down Expand Up @@ -4138,6 +4179,12 @@ status_t ACodec::setupVideoEncoder(
video_def->eCompressionFormat = compressionFormat;
video_def->eColorFormat = OMX_COLOR_FormatUnused;

//set mtk parameters
status_t err_mtkparam = setMtkParameters(mOMXNode, msg, mIsEncoder);
if (err_mtkparam != OK) {
return err_mtkparam;
}

err = mOMXNode->setParameter(
OMX_IndexParamPortDefinition, &def, sizeof(def));

Expand Down Expand Up @@ -4602,7 +4649,7 @@ status_t ACodec::setupAVCEncoderParameters(const sp<AMessage> &msg) {
h264type.eLevel = static_cast<OMX_VIDEO_AVCLEVELTYPE>(level);
} else {
h264type.eProfile = OMX_VIDEO_AVCProfileBaseline;
#if 0 /* DON'T YET DEFAULT TO HIGHEST PROFILE */
#if 1 /* DON'T YET DEFAULT TO HIGHEST PROFILE */
// Use largest supported profile for AVC recording if profile is not specified.
for (OMX_VIDEO_AVCPROFILETYPE profile : {
OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCProfileMain }) {
Expand Down Expand Up @@ -6681,6 +6728,11 @@ void ACodec::BaseState::onOutputBufferDrained(const sp<AMessage> &msg) {
int32_t discarded = 0;
msg->findInt32("discarded", &discarded);

// set AvSyncRefTime to omx +
mCodec->setAVSyncTime(mCodec->mComponentName.c_str(),buffer->meta(),
mCodec->mOMXNode, msg);
// set AvSyncRefTime to omx -

ssize_t index;
BufferInfo *info = mCodec->findBufferByID(kPortIndexOutput, bufferID, &index);
BufferInfo::Status status = BufferInfo::getSafeStatus(info);
Expand Down Expand Up @@ -7983,6 +8035,12 @@ status_t ACodec::setParameters(const sp<AMessage> &params) {
}
}

//set mtk parameters
status_t err_mtkparam = setMtkParameters(mOMXNode, params, mIsEncoder);
if (err_mtkparam != OK) {
return err_mtkparam;
}

return setVendorParameters(params);
}

Expand Down
Loading