• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "audio_ffmpeg_aac_decoder_plugin.h"
17 #include <set>
18 #include "avcodec_trace.h"
19 #include "avcodec_errors.h"
20 #include "avcodec_log.h"
21 #include "media_description.h"
22 #include "avcodec_mime_type.h"
23 #include "avcodec_audio_common.h"
24 #include "ffmpeg_converter.h"
25 
26 namespace {
27 constexpr int32_t MIN_CHANNELS = 1;
28 constexpr int32_t MAX_CHANNELS = 8;
29 constexpr AVSampleFormat DEFAULT_FFMPEG_SAMPLE_FORMAT = AV_SAMPLE_FMT_FLT;
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "AvCodec-AudioFFMpegAacDecoderPlugin"};
31 constexpr int32_t INPUT_BUFFER_SIZE_DEFAULT = 8192;
32 constexpr int32_t OUTPUT_BUFFER_SIZE_DEFAULT = 4 * 1024 * 8;
33 constexpr std::string_view AUDIO_CODEC_NAME = "aac";
34 static std::set<int32_t> supportedSampleRate = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000,
35                                                 11025, 8000, 7350};
36 static std::set<OHOS::MediaAVCodec::AudioSampleFormat> supportedSampleFormats = {
37     OHOS::MediaAVCodec::AudioSampleFormat::SAMPLE_S16LE,
38     OHOS::MediaAVCodec::AudioSampleFormat::SAMPLE_F32LE};
39 static std::set<int32_t> supportedSampleRates = {7350, 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
40                                                  64000, 88200, 96000};
41 }
42 
43 namespace OHOS {
44 namespace MediaAVCodec {
AudioFFMpegAacDecoderPlugin()45 AudioFFMpegAacDecoderPlugin::AudioFFMpegAacDecoderPlugin()
46     : basePlugin(std::make_unique<AudioFfmpegDecoderPlugin>()), channels_(0) {}
47 
~AudioFFMpegAacDecoderPlugin()48 AudioFFMpegAacDecoderPlugin::~AudioFFMpegAacDecoderPlugin()
49 {
50     if (basePlugin != nullptr) {
51         basePlugin->Release();
52         basePlugin.reset();
53     }
54 }
55 
CheckAdts(const Format & format)56 bool AudioFFMpegAacDecoderPlugin::CheckAdts(const Format &format)
57 {
58     int type;
59     if (format.GetIntValue(MediaDescriptionKey::MD_KEY_AAC_IS_ADTS, type)) {
60         if (type != 1 && type != 0) {
61             AVCODEC_LOGE("aac_is_adts value invalid");
62             return false;
63         }
64     } else {
65         AVCODEC_LOGW("aac_is_adts parameter is missing");
66         type = 1;
67     }
68     aacName_ = (type == 1 ? "aac" : "aac_latm");
69 
70     return true;
71 }
72 
CheckSampleFormat(const Format & format)73 bool AudioFFMpegAacDecoderPlugin::CheckSampleFormat(const Format &format)
74 {
75     int32_t sampleFormat;
76     if (!format.GetIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, sampleFormat)) {
77         AVCODEC_LOGW("Sample format missing, set to default f32le");
78         if (channels_ != 1) {
79             basePlugin->EnableResample(DEFAULT_FFMPEG_SAMPLE_FORMAT);
80         }
81         return true;
82     }
83 
84     if (supportedSampleFormats.find(static_cast<AudioSampleFormat>(sampleFormat)) == supportedSampleFormats.end()) {
85         AVCODEC_LOGE("Output sample format not support");
86         return false;
87     }
88     if (channels_ == 1 && sampleFormat == AudioSampleFormat::SAMPLE_F32LE) {
89         return true;
90     }
91     auto destFmt = FFMpegConverter::ConvertOHAudioFormatToFFMpeg(static_cast<AudioSampleFormat>(sampleFormat));
92     basePlugin->EnableResample(destFmt);
93     return true;
94 }
95 
CheckChannelCount(const Format & format)96 bool AudioFFMpegAacDecoderPlugin::CheckChannelCount(const Format &format)
97 {
98     if (!format.GetIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, channels_)) {
99         AVCODEC_LOGE("parameter channel_count missing");
100         return false;
101     }
102     if (channels_ < MIN_CHANNELS || channels_ > MAX_CHANNELS) {
103         AVCODEC_LOGE("parameter channel_count invaild");
104         return false;
105     }
106     return true;
107 }
108 
CheckSampleRate(const Format & format) const109 bool AudioFFMpegAacDecoderPlugin::CheckSampleRate(const Format &format) const
110 {
111     int32_t sampleRate;
112     if (!format.GetIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, sampleRate)) {
113         AVCODEC_LOGE("parameter sample_rate missing");
114         return false;
115     }
116     if (supportedSampleRates.find(sampleRate) == supportedSampleRates.end()) {
117         AVCODEC_LOGE("parameter sample_rate invalid, %{public}d", sampleRate);
118         return false;
119     }
120     return true;
121 }
122 
CheckFormat(const Format & format)123 bool AudioFFMpegAacDecoderPlugin::CheckFormat(const Format &format)
124 {
125     if (!CheckAdts(format) || !CheckChannelCount(format) || !CheckSampleFormat(format) || !CheckSampleRate(format)) {
126         return false;
127     }
128     return true;
129 }
130 
Init(const Format & format)131 int32_t AudioFFMpegAacDecoderPlugin::Init(const Format &format)
132 {
133     if (!CheckFormat(format)) {
134         return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
135     }
136     int32_t ret = basePlugin->AllocateContext(aacName_);
137     if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
138         AVCODEC_LOGE("AllocateContext failed, ret=%{public}d", ret);
139         return ret;
140     }
141     ret = basePlugin->InitContext(format);
142     if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
143         AVCODEC_LOGE("InitContext failed, ret=%{public}d", ret);
144         return ret;
145     }
146     return basePlugin->OpenContext();
147 }
148 
ProcessSendData(const std::shared_ptr<AudioBufferInfo> & inputBuffer)149 int32_t AudioFFMpegAacDecoderPlugin::ProcessSendData(const std::shared_ptr<AudioBufferInfo> &inputBuffer)
150 {
151     return basePlugin->ProcessSendData(inputBuffer);
152 }
153 
ProcessRecieveData(std::shared_ptr<AudioBufferInfo> & outBuffer)154 int32_t AudioFFMpegAacDecoderPlugin::ProcessRecieveData(std::shared_ptr<AudioBufferInfo> &outBuffer)
155 {
156     return basePlugin->ProcessRecieveData(outBuffer);
157 }
158 
Reset()159 int32_t AudioFFMpegAacDecoderPlugin::Reset()
160 {
161     return basePlugin->Reset();
162 }
163 
Release()164 int32_t AudioFFMpegAacDecoderPlugin::Release()
165 {
166     return basePlugin->Release();
167 }
168 
Flush()169 int32_t AudioFFMpegAacDecoderPlugin::Flush()
170 {
171     return basePlugin->Flush();
172 }
173 
GetInputBufferSize() const174 int32_t AudioFFMpegAacDecoderPlugin::GetInputBufferSize() const
175 {
176     int32_t maxSize = basePlugin->GetMaxInputSize();
177     if (maxSize < 0 || maxSize > INPUT_BUFFER_SIZE_DEFAULT) {
178         maxSize = INPUT_BUFFER_SIZE_DEFAULT;
179     }
180     return maxSize;
181 }
182 
GetOutputBufferSize() const183 int32_t AudioFFMpegAacDecoderPlugin::GetOutputBufferSize() const
184 {
185     return OUTPUT_BUFFER_SIZE_DEFAULT;
186 }
187 
GetFormat() const188 Format AudioFFMpegAacDecoderPlugin::GetFormat() const noexcept
189 {
190     auto format = basePlugin->GetFormat();
191     format.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, AVCodecMimeType::MEDIA_MIMETYPE_AUDIO_AAC);
192     return format;
193 }
194 
GetCodecType() const195 std::string_view AudioFFMpegAacDecoderPlugin::GetCodecType() const noexcept
196 {
197     return AUDIO_CODEC_NAME;
198 }
199 } // namespace MediaAVCodec
200 } // namespace OHOS