• 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_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, "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     auto swrContext = swr_alloc();
47     if (swrContext == nullptr) {
48         AVCODEC_LOGE("cannot allocate swr context");
49         return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
50     }
51     swrContext = swr_alloc_set_opts(swrContext, resamplePara_.channelLayout, resamplePara_.destFmt,
52                                     resamplePara_.sampleRate, resamplePara_.channelLayout,
53                                     resamplePara_.srcFmt, resamplePara_.sampleRate, 0, nullptr);
54     if (swr_init(swrContext) != 0) {
55         AVCODEC_LOGE("swr init error");
56         return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
57     }
58     swrCtx_ = std::shared_ptr<SwrContext>(swrContext, [](SwrContext *ptr) {
59         if (ptr) {
60             swr_free(&ptr);
61         }
62     });
63     return AVCodecServiceErrCode::AVCS_ERR_OK;
64 }
65 
Convert(const uint8_t * srcBuffer,const size_t srcLength,uint8_t * & destBuffer,size_t & destLength)66 int32_t AudioResample::Convert(const uint8_t* srcBuffer, const size_t srcLength, uint8_t*& destBuffer,
67                                size_t& destLength)
68 {
69     size_t lineSize = srcLength / resamplePara_.channels;
70     std::vector<const uint8_t*> tmpInput(resamplePara_.channels);
71     tmpInput[0] = srcBuffer;
72     if (av_sample_fmt_is_planar(resamplePara_.srcFmt)) {
73         for (size_t i = 1; i < tmpInput.size(); ++i) {
74             tmpInput[i] = tmpInput[i-1] + lineSize;
75         }
76     }
77     auto samples = lineSize / av_get_bytes_per_sample(resamplePara_.srcFmt);
78     auto res = swr_convert(swrCtx_.get(), resampleChannelAddr_.data(), resamplePara_.destSamplesPerFrame,
79                            tmpInput.data(), samples);
80     if (res < 0) {
81         AVCODEC_LOGE("resample input failed");
82         destLength = 0;
83     } else {
84         destBuffer = resampleCache_.data();
85         destLength = res * av_get_bytes_per_sample(resamplePara_.destFmt) * resamplePara_.channels;
86     }
87     return AVCodecServiceErrCode::AVCS_ERR_OK;
88 }
89 
ConvertFrame(AVFrame * outputFrame,const AVFrame * inputFrame)90 int32_t AudioResample::ConvertFrame(AVFrame *outputFrame, const AVFrame *inputFrame)
91 {
92     if (outputFrame == nullptr || inputFrame == nullptr) {
93         AVCODEC_LOGE("Frame null pointer");
94         return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
95     }
96 
97     outputFrame->channel_layout = resamplePara_.channelLayout;
98     outputFrame->format = resamplePara_.destFmt;
99     outputFrame->sample_rate = resamplePara_.sampleRate;
100 
101     auto ret = swr_convert_frame(swrCtx_.get(), outputFrame, inputFrame);
102     if (ret < 0) {
103         AVCODEC_LOGE("convert frame failed, %{public}s", FFMpegConverter::AVStrError(ret).c_str());
104         return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
105     }
106     return AVCodecServiceErrCode::AVCS_ERR_OK;
107 }
108 } // namespace MediaAVCodec
109 } // namespace OHOS
110