• 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_mp3_decoder_plugin.h"
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19 #include "media_description.h"
20 #include "avcodec_mime_type.h"
21 
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "AvCodec-AudioFFMpegMp3DecoderPlugin"};
24 constexpr int MIN_CHANNELS = 1;
25 constexpr int MAX_CHANNELS = 2;
26 constexpr int SAMPLE_RATE_RATIO = 31;
27 constexpr int SUPPORT_SAMPLE_RATE = 9;
28 constexpr int BUFFER_DIFF = 128;
29 constexpr int MIN_OUTBUF_SIZE = 2500;
30 constexpr int INPUT_BUFFER_SIZE_DEFAULT = 8192;
31 } // namespace
32 
33 namespace OHOS {
34 namespace MediaAVCodec {
35 constexpr std::string_view AUDIO_CODEC_NAME = "mp3";
AudioFFMpegMp3DecoderPlugin()36 AudioFFMpegMp3DecoderPlugin::AudioFFMpegMp3DecoderPlugin() : basePlugin(std::make_unique<AudioFfmpegDecoderPlugin>())
37 {
38     channels = 0;
39     sampleRate = 0;
40 }
41 
~AudioFFMpegMp3DecoderPlugin()42 AudioFFMpegMp3DecoderPlugin::~AudioFFMpegMp3DecoderPlugin()
43 {
44     basePlugin->Release();
45     basePlugin.reset();
46     basePlugin = nullptr;
47 }
48 
Init(const Format & format)49 int32_t AudioFFMpegMp3DecoderPlugin::Init(const Format &format)
50 {
51     int32_t ret = basePlugin->AllocateContext("mp3");
52     int32_t checkresult = AudioFFMpegMp3DecoderPlugin::Checkinit(format);
53     if (checkresult != AVCodecServiceErrCode::AVCS_ERR_OK) {
54         return checkresult;
55     }
56     if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
57         AVCODEC_LOGE("mp3 init error.");
58         return ret;
59     }
60     ret = basePlugin->InitContext(format);
61     if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
62         AVCODEC_LOGE("mp3 init error.");
63         return ret;
64     }
65     return basePlugin->OpenContext();
66 }
67 
ProcessSendData(const std::shared_ptr<AudioBufferInfo> & inputBuffer)68 int32_t AudioFFMpegMp3DecoderPlugin::ProcessSendData(const std::shared_ptr<AudioBufferInfo> &inputBuffer)
69 {
70     return basePlugin->ProcessSendData(inputBuffer);
71 }
72 
ProcessRecieveData(std::shared_ptr<AudioBufferInfo> & outBuffer)73 int32_t AudioFFMpegMp3DecoderPlugin::ProcessRecieveData(std::shared_ptr<AudioBufferInfo> &outBuffer)
74 {
75     return basePlugin->ProcessRecieveData(outBuffer);
76 }
77 
Reset()78 int32_t AudioFFMpegMp3DecoderPlugin::Reset()
79 {
80     return basePlugin->Reset();
81 }
82 
Release()83 int32_t AudioFFMpegMp3DecoderPlugin::Release()
84 {
85     return basePlugin->Release();
86 }
87 
Flush()88 int32_t AudioFFMpegMp3DecoderPlugin::Flush()
89 {
90     return basePlugin->Flush();
91 }
92 
GetInputBufferSize() const93 int32_t AudioFFMpegMp3DecoderPlugin::GetInputBufferSize() const
94 {
95     int32_t maxSize = basePlugin->GetMaxInputSize();
96     if (maxSize < 0 || maxSize > INPUT_BUFFER_SIZE_DEFAULT) {
97         maxSize = INPUT_BUFFER_SIZE_DEFAULT;
98     }
99     return maxSize;
100 }
101 
GetOutputBufferSize() const102 int32_t AudioFFMpegMp3DecoderPlugin::GetOutputBufferSize() const
103 {
104     int32_t maxSize = (sampleRate / SAMPLE_RATE_RATIO + BUFFER_DIFF) * channels * sizeof(short);
105     int32_t minSize = MIN_OUTBUF_SIZE * channels * sizeof(short);
106     if (maxSize < minSize) {
107         maxSize = minSize;
108     }
109     return maxSize;
110 }
111 
GetFormat() const112 Format AudioFFMpegMp3DecoderPlugin::GetFormat() const noexcept
113 {
114     auto format = basePlugin->GetFormat();
115     format.PutStringValue(MediaDescriptionKey::MD_KEY_CODEC_MIME, AVCodecMimeType::MEDIA_MIMETYPE_AUDIO_MPEG);
116     return format;
117 }
118 
Checkinit(const Format & format)119 int32_t AudioFFMpegMp3DecoderPlugin::Checkinit(const Format &format)
120 {
121     int sampleRatePick[SUPPORT_SAMPLE_RATE] = {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000};
122     format.GetIntValue(MediaDescriptionKey::MD_KEY_CHANNEL_COUNT, channels);
123     format.GetIntValue(MediaDescriptionKey::MD_KEY_SAMPLE_RATE, sampleRate);
124     if (channels < MIN_CHANNELS || channels > MAX_CHANNELS) {
125         return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
126     }
127 
128     for (int i = 0; i < SUPPORT_SAMPLE_RATE; i++) {
129         if (sampleRate == sampleRatePick[i]) {
130             break;
131         } else if (i == SUPPORT_SAMPLE_RATE - 1) {
132             return AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL;
133         }
134     }
135 
136     return AVCodecServiceErrCode::AVCS_ERR_OK;
137 }
138 
GetCodecType() const139 std::string_view AudioFFMpegMp3DecoderPlugin::GetCodecType() const noexcept
140 {
141     return AUDIO_CODEC_NAME;
142 }
143 } // namespace MediaAVCodec
144 } // namespace OHOS