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_resample.h"
17 #include "avcodec_log.h"
18 #include "securec.h"
19 #include "ffmpeg_converter.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "AvCodec-AudioResample"};
23 } // namespace
24 
25 namespace OHOS {
26 namespace MediaAVCodec {
Init(const ResamplePara & resamplePara)27 int32_t AudioResample::Init(const ResamplePara& resamplePara)
28 {
29     auto ret = InitSwrContext(resamplePara);
30     if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
31         return ret;
32     }
33     auto destFrameSize = av_samples_get_buffer_size(nullptr, resamplePara_.channels,
34                                                     resamplePara_.destSamplesPerFrame, resamplePara_.destFmt, 0);
35     resampleCache_.reserve(destFrameSize);
36     resampleChannelAddr_.reserve(resamplePara_.channels);
37     auto tmp = resampleChannelAddr_.data();
38     av_samples_fill_arrays(tmp, nullptr, resampleCache_.data(), resamplePara_.channels,
39                            resamplePara_.destSamplesPerFrame, resamplePara_.destFmt, 0);
40     return AVCodecServiceErrCode::AVCS_ERR_OK;
41 }
42 
InitSwrContext(const ResamplePara & resamplePara)43 int32_t AudioResample::InitSwrContext(const ResamplePara& resamplePara)
44 {
45     resamplePara_ = resamplePara;
46     SwrContext *swrContext = swr_alloc();
47     if (swrContext == nullptr) {
48         AVCODEC_LOGE("cannot allocate swr context");
49         return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
50     }
51     int error =
52         swr_alloc_set_opts2(&swrContext, &resamplePara_.channelLayout, resamplePara_.destFmt, resamplePara_.sampleRate,
53                             &resamplePara_.channelLayout, resamplePara_.srcFmt, resamplePara_.sampleRate, 0, nullptr);
54     if (error < 0) {
55         AVCODEC_LOGE("swr init error");
56         return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
57     }
58     if (swr_init(swrContext) != 0) {
59         AVCODEC_LOGE("swr init error");
60         swr_free(&swrContext);
61         return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
62     }
63     swrCtx_ = std::shared_ptr<SwrContext>(swrContext, [](SwrContext *ptr) {
64         if (ptr) {
65             swr_free(&ptr);
66         }
67     });
68     return AVCodecServiceErrCode::AVCS_ERR_OK;
69 }
70 
Convert(const uint8_t * srcBuffer,const size_t srcLength,uint8_t * & destBuffer,size_t & destLength)71 int32_t AudioResample::Convert(const uint8_t* srcBuffer, const size_t srcLength, uint8_t*& destBuffer,
72                                size_t& destLength)
73 {
74     size_t lineSize = srcLength / resamplePara_.channels;
75     std::vector<const uint8_t*> tmpInput(resamplePara_.channels);
76     tmpInput[0] = srcBuffer;
77     if (av_sample_fmt_is_planar(resamplePara_.srcFmt)) {
78         for (size_t i = 1; i < tmpInput.size(); ++i) {
79             tmpInput[i] = tmpInput[i-1] + lineSize;
80         }
81     }
82     int32_t samples = lineSize / av_get_bytes_per_sample(resamplePara_.srcFmt);
83     auto res = swr_convert(swrCtx_.get(), resampleChannelAddr_.data(), resamplePara_.destSamplesPerFrame,
84                            tmpInput.data(), samples);
85     if (res < 0) {
86         AVCODEC_LOGE("resample input failed");
87         destLength = 0;
88     } else {
89         destBuffer = resampleCache_.data();
90         destLength = static_cast<size_t>(res * av_get_bytes_per_sample(resamplePara_.destFmt) * resamplePara_.channels);
91     }
92     return AVCodecServiceErrCode::AVCS_ERR_OK;
93 }
94 
ConvertFrame(AVFrame * outputFrame,const AVFrame * inputFrame)95 int32_t AudioResample::ConvertFrame(AVFrame *outputFrame, const AVFrame *inputFrame)
96 {
97     if (outputFrame == nullptr || inputFrame == nullptr) {
98         AVCODEC_LOGE("Frame null pointer");
99         return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
100     }
101     int planar = av_sample_fmt_is_planar(static_cast<AVSampleFormat>(inputFrame->format));
102     if (planar) {
103         for (auto i = 0; i < inputFrame->channels; i++) {
104             if (inputFrame->extended_data[i] == nullptr) {
105                 AVCODEC_LOGE("this is a planar audio, inputFrame->channels: %{public}d, "
106                              "but inputFrame->extended_data[%{public}d] is nullptr", inputFrame->channels, i);
107                 return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
108             }
109         }
110     } else {
111         if (inputFrame->extended_data[0] == nullptr) {
112             AVCODEC_LOGE("inputFrame->extended_data[0] is nullptr");
113             return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
114         }
115     }
116 
117     outputFrame->ch_layout = resamplePara_.channelLayout;
118     outputFrame->format = resamplePara_.destFmt;
119     outputFrame->sample_rate = resamplePara_.sampleRate;
120 
121     auto ret = swr_convert_frame(swrCtx_.get(), outputFrame, inputFrame);
122     if (ret < 0) {
123         AVCODEC_LOGE("convert frame failed, %{public}s", FFMpegConverter::AVStrError(ret).c_str());
124         return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
125     }
126     return AVCodecServiceErrCode::AVCS_ERR_OK;
127 }
128 } // namespace MediaAVCodec
129 } // namespace OHOS
130