• 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_amrnb_decoder_plugin.h"
17 #include <set>
18 #include "avcodec_errors.h"
19 #include "avcodec_log.h"
20 #include "media_description.h"
21 #include "avcodec_mime_type.h"
22 #include "avcodec_audio_common.h"
23 #include "ffmpeg_converter.h"
24 
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "AvCodec-AudioFFMpegAmrnbDecoderPlugin"};
27 constexpr int SUPPORT_CHANNELS = 1;
28 constexpr int SUPPORT_SAMPLE_RATE = 8000;
29 constexpr AVSampleFormat DEFAULT_FFMPEG_SAMPLE_FORMAT = AV_SAMPLE_FMT_S16;
30 constexpr int INPUT_BUFFER_SIZE_DEFAULT = 150;
31 constexpr int OUTPUT_BUFFER_SIZE_DEFAULT = 1280;
32 static std::set<OHOS::MediaAVCodec::AudioSampleFormat> supportedSampleFormats = {
33     OHOS::MediaAVCodec::AudioSampleFormat::SAMPLE_S16LE,
34     OHOS::MediaAVCodec::AudioSampleFormat::SAMPLE_F32LE};
35 } // namespace
36 
37 namespace OHOS {
38 namespace MediaAVCodec {
39 constexpr std::string_view AUDIO_CODEC_NAME = "amrnb";
AudioFFMpegAmrnbDecoderPlugin()40 AudioFFMpegAmrnbDecoderPlugin::AudioFFMpegAmrnbDecoderPlugin()
41     : basePlugin(std::make_unique<AudioFfmpegDecoderPlugin>())
42 {
43     channels = 0;
44     sampleRate = 0;
45     bitRate = 0;
46 }
47 
~AudioFFMpegAmrnbDecoderPlugin()48 AudioFFMpegAmrnbDecoderPlugin::~AudioFFMpegAmrnbDecoderPlugin()
49 {
50     basePlugin->Release();
51     basePlugin.reset();
52     basePlugin = nullptr;
53 }
54 
Init(const Format & format)55 int32_t AudioFFMpegAmrnbDecoderPlugin::Init(const Format &format)
56 {
57     int32_t ret = basePlugin->AllocateContext("amrnb");
58     int32_t checkresult = AudioFFMpegAmrnbDecoderPlugin::Checkinit(format);
59     if (checkresult != AVCodecServiceErrCode::AVCS_ERR_OK) {
60         return checkresult;
61     }
62     if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
63         AVCODEC_LOGE("amrwb init error.");
64         return ret;
65     }
66     ret = basePlugin->InitContext(format);
67     if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
68         AVCODEC_LOGE("amrwb init error.");
69         return ret;
70     }
71     return basePlugin->OpenContext();
72 }
73 
ProcessSendData(const std::shared_ptr<AudioBufferInfo> & inputBuffer)74 int32_t AudioFFMpegAmrnbDecoderPlugin::ProcessSendData(const std::shared_ptr<AudioBufferInfo> &inputBuffer)
75 {
76     return basePlugin->ProcessSendData(inputBuffer);
77 }
78 
ProcessRecieveData(std::shared_ptr<AudioBufferInfo> & outBuffer)79 int32_t AudioFFMpegAmrnbDecoderPlugin::ProcessRecieveData(std::shared_ptr<AudioBufferInfo> &outBuffer)
80 {
81     return basePlugin->ProcessRecieveData(outBuffer);
82 }
83 
Reset()84 int32_t AudioFFMpegAmrnbDecoderPlugin::Reset()
85 {
86     return basePlugin->Reset();
87 }
88 
Release()89 int32_t AudioFFMpegAmrnbDecoderPlugin::Release()
90 {
91     return basePlugin->Release();
92 }
93 
Flush()94 int32_t AudioFFMpegAmrnbDecoderPlugin::Flush()
95 {
96     return basePlugin->Flush();
97 }
98 
GetInputBufferSize() const99 int32_t AudioFFMpegAmrnbDecoderPlugin::GetInputBufferSize() const
100 {
101     int32_t maxSize = basePlugin->GetMaxInputSize();
102     if (maxSize < 0 || maxSize > INPUT_BUFFER_SIZE_DEFAULT) {
103         maxSize = INPUT_BUFFER_SIZE_DEFAULT;
104     }
105     return maxSize;
106 }
107 
GetOutputBufferSize() const108 int32_t AudioFFMpegAmrnbDecoderPlugin::GetOutputBufferSize() const
109 {
110     return OUTPUT_BUFFER_SIZE_DEFAULT;
111 }
112 
GetFormat() const113 Format AudioFFMpegAmrnbDecoderPlugin::GetFormat() const noexcept
114 {
115     auto format = basePlugin->GetFormat();
116     format.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, AVCodecMimeType::MEDIA_MIMETYPE_AUDIO_MPEG);
117     return format;
118 }
119 
Checkinit(const Format & format)120 int32_t AudioFFMpegAmrnbDecoderPlugin::Checkinit(const Format &format)
121 {
122     format.GetIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, channels);
123     format.GetIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, sampleRate);
124     format.GetLongValue(MediaDescriptionKey::MD_KEY_BITRATE, bitRate);
125     if (channels != SUPPORT_CHANNELS) {
126         return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
127     }
128 
129     if (sampleRate != SUPPORT_SAMPLE_RATE) {
130         return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
131     }
132     if (!CheckSampleFormat(format)) {
133         return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
134     }
135     return AVCodecServiceErrCode::AVCS_ERR_OK;
136 }
137 
CheckSampleFormat(const Format & format)138 bool AudioFFMpegAmrnbDecoderPlugin::CheckSampleFormat(const Format &format)
139 {
140     int32_t sampleFormat;
141     if (!format.GetIntValue(MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT, sampleFormat)) {
142         AVCODEC_LOGW("Sample format missing, set to default s16le");
143         basePlugin->EnableResample(DEFAULT_FFMPEG_SAMPLE_FORMAT);
144         return true;
145     }
146     if (supportedSampleFormats.find(static_cast<AudioSampleFormat>(sampleFormat)) == supportedSampleFormats.end()) {
147         AVCODEC_LOGE("Output sample format not support");
148         return false;
149     }
150     if (sampleFormat == SAMPLE_F32LE) {
151         return true;
152     }
153     auto destFmt = FFMpegConverter::ConvertOHAudioFormatToFFMpeg(static_cast<AudioSampleFormat>(sampleFormat));
154     basePlugin->EnableResample(destFmt);
155     return true;
156 }
157 
GetCodecType() const158 std::string_view AudioFFMpegAmrnbDecoderPlugin::GetCodecType() const noexcept
159 {
160     return AUDIO_CODEC_NAME;
161 }
162 } // namespace MediaAVCodec
163 } // namespace OHOS