1da853ecaSopenharmony_ci/*
2da853ecaSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3da853ecaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4da853ecaSopenharmony_ci * you may not use this file except in compliance with the License.
5da853ecaSopenharmony_ci * You may obtain a copy of the License at
6da853ecaSopenharmony_ci *
7da853ecaSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8da853ecaSopenharmony_ci *
9da853ecaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10da853ecaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11da853ecaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12da853ecaSopenharmony_ci * See the License for the specific language governing permissions and
13da853ecaSopenharmony_ci * limitations under the License.
14da853ecaSopenharmony_ci */
15da853ecaSopenharmony_ci
16da853ecaSopenharmony_ci#include "audio_resample.h"
17da853ecaSopenharmony_ci#include "avcodec_log.h"
18da853ecaSopenharmony_ci#include "securec.h"
19da853ecaSopenharmony_ci#include "ffmpeg_converter.h"
20da853ecaSopenharmony_ci
21da853ecaSopenharmony_cinamespace {
22da853ecaSopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "AvCodec-AudioResample"};
23da853ecaSopenharmony_ci} // namespace
24da853ecaSopenharmony_ci
25da853ecaSopenharmony_cinamespace OHOS {
26da853ecaSopenharmony_cinamespace MediaAVCodec {
27da853ecaSopenharmony_ciint32_t AudioResample::Init(const ResamplePara& resamplePara)
28da853ecaSopenharmony_ci{
29da853ecaSopenharmony_ci    auto ret = InitSwrContext(resamplePara);
30da853ecaSopenharmony_ci    if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
31da853ecaSopenharmony_ci        return ret;
32da853ecaSopenharmony_ci    }
33da853ecaSopenharmony_ci    auto destFrameSize = av_samples_get_buffer_size(nullptr, resamplePara_.channels,
34da853ecaSopenharmony_ci                                                    resamplePara_.destSamplesPerFrame, resamplePara_.destFmt, 0);
35da853ecaSopenharmony_ci    resampleCache_.reserve(destFrameSize);
36da853ecaSopenharmony_ci    resampleChannelAddr_.reserve(resamplePara_.channels);
37da853ecaSopenharmony_ci    auto tmp = resampleChannelAddr_.data();
38da853ecaSopenharmony_ci    av_samples_fill_arrays(tmp, nullptr, resampleCache_.data(), resamplePara_.channels,
39da853ecaSopenharmony_ci                           resamplePara_.destSamplesPerFrame, resamplePara_.destFmt, 0);
40da853ecaSopenharmony_ci    return AVCodecServiceErrCode::AVCS_ERR_OK;
41da853ecaSopenharmony_ci}
42da853ecaSopenharmony_ci
43da853ecaSopenharmony_ciint32_t AudioResample::InitSwrContext(const ResamplePara& resamplePara)
44da853ecaSopenharmony_ci{
45da853ecaSopenharmony_ci    resamplePara_ = resamplePara;
46da853ecaSopenharmony_ci    SwrContext *swrContext = swr_alloc();
47da853ecaSopenharmony_ci    if (swrContext == nullptr) {
48da853ecaSopenharmony_ci        AVCODEC_LOGE("cannot allocate swr context");
49da853ecaSopenharmony_ci        return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
50da853ecaSopenharmony_ci    }
51da853ecaSopenharmony_ci    int error =
52da853ecaSopenharmony_ci        swr_alloc_set_opts2(&swrContext, &resamplePara_.channelLayout, resamplePara_.destFmt, resamplePara_.sampleRate,
53da853ecaSopenharmony_ci                            &resamplePara_.channelLayout, resamplePara_.srcFmt, resamplePara_.sampleRate, 0, nullptr);
54da853ecaSopenharmony_ci    if (error < 0) {
55da853ecaSopenharmony_ci        AVCODEC_LOGE("swr init error");
56da853ecaSopenharmony_ci        return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
57da853ecaSopenharmony_ci    }
58da853ecaSopenharmony_ci    if (swr_init(swrContext) != 0) {
59da853ecaSopenharmony_ci        AVCODEC_LOGE("swr init error");
60da853ecaSopenharmony_ci        return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
61da853ecaSopenharmony_ci    }
62da853ecaSopenharmony_ci    swrCtx_ = std::shared_ptr<SwrContext>(swrContext, [](SwrContext *ptr) {
63da853ecaSopenharmony_ci        if (ptr) {
64da853ecaSopenharmony_ci            swr_free(&ptr);
65da853ecaSopenharmony_ci        }
66da853ecaSopenharmony_ci    });
67da853ecaSopenharmony_ci    return AVCodecServiceErrCode::AVCS_ERR_OK;
68da853ecaSopenharmony_ci}
69da853ecaSopenharmony_ci
70da853ecaSopenharmony_ciint32_t AudioResample::Convert(const uint8_t* srcBuffer, const size_t srcLength, uint8_t*& destBuffer,
71da853ecaSopenharmony_ci                               size_t& destLength)
72da853ecaSopenharmony_ci{
73da853ecaSopenharmony_ci    size_t lineSize = srcLength / resamplePara_.channels;
74da853ecaSopenharmony_ci    std::vector<const uint8_t*> tmpInput(resamplePara_.channels);
75da853ecaSopenharmony_ci    tmpInput[0] = srcBuffer;
76da853ecaSopenharmony_ci    if (av_sample_fmt_is_planar(resamplePara_.srcFmt)) {
77da853ecaSopenharmony_ci        for (size_t i = 1; i < tmpInput.size(); ++i) {
78da853ecaSopenharmony_ci            tmpInput[i] = tmpInput[i-1] + lineSize;
79da853ecaSopenharmony_ci        }
80da853ecaSopenharmony_ci    }
81da853ecaSopenharmony_ci    int32_t samples = lineSize / av_get_bytes_per_sample(resamplePara_.srcFmt);
82da853ecaSopenharmony_ci    auto res = swr_convert(swrCtx_.get(), resampleChannelAddr_.data(), resamplePara_.destSamplesPerFrame,
83da853ecaSopenharmony_ci                           tmpInput.data(), samples);
84da853ecaSopenharmony_ci    if (res < 0) {
85da853ecaSopenharmony_ci        AVCODEC_LOGE("resample input failed");
86da853ecaSopenharmony_ci        destLength = 0;
87da853ecaSopenharmony_ci    } else {
88da853ecaSopenharmony_ci        destBuffer = resampleCache_.data();
89da853ecaSopenharmony_ci        destLength = static_cast<size_t>(res * av_get_bytes_per_sample(resamplePara_.destFmt) * resamplePara_.channels);
90da853ecaSopenharmony_ci    }
91da853ecaSopenharmony_ci    return AVCodecServiceErrCode::AVCS_ERR_OK;
92da853ecaSopenharmony_ci}
93da853ecaSopenharmony_ci
94da853ecaSopenharmony_ciint32_t AudioResample::ConvertFrame(AVFrame *outputFrame, const AVFrame *inputFrame)
95da853ecaSopenharmony_ci{
96da853ecaSopenharmony_ci    if (outputFrame == nullptr || inputFrame == nullptr) {
97da853ecaSopenharmony_ci        AVCODEC_LOGE("Frame null pointer");
98da853ecaSopenharmony_ci        return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
99da853ecaSopenharmony_ci    }
100da853ecaSopenharmony_ci    int planar = av_sample_fmt_is_planar(static_cast<AVSampleFormat>(inputFrame->format));
101da853ecaSopenharmony_ci    if (planar) {
102da853ecaSopenharmony_ci        for (auto i = 0; i < inputFrame->channels; i++) {
103da853ecaSopenharmony_ci            if (inputFrame->extended_data[i] == nullptr) {
104da853ecaSopenharmony_ci                AVCODEC_LOGE("this is a planar audio, inputFrame->channels: %{public}d, "
105da853ecaSopenharmony_ci                             "but inputFrame->extended_data[%{public}d] is nullptr", inputFrame->channels, i);
106da853ecaSopenharmony_ci                return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
107da853ecaSopenharmony_ci            }
108da853ecaSopenharmony_ci        }
109da853ecaSopenharmony_ci    } else {
110da853ecaSopenharmony_ci        if (inputFrame->extended_data[0] == nullptr) {
111da853ecaSopenharmony_ci            AVCODEC_LOGE("inputFrame->extended_data[0] is nullptr");
112da853ecaSopenharmony_ci            return AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY;
113da853ecaSopenharmony_ci        }
114da853ecaSopenharmony_ci    }
115da853ecaSopenharmony_ci
116da853ecaSopenharmony_ci    outputFrame->ch_layout = resamplePara_.channelLayout;
117da853ecaSopenharmony_ci    outputFrame->format = resamplePara_.destFmt;
118da853ecaSopenharmony_ci    outputFrame->sample_rate = resamplePara_.sampleRate;
119da853ecaSopenharmony_ci
120da853ecaSopenharmony_ci    auto ret = swr_convert_frame(swrCtx_.get(), outputFrame, inputFrame);
121da853ecaSopenharmony_ci    if (ret < 0) {
122da853ecaSopenharmony_ci        AVCODEC_LOGE("convert frame failed, %{public}s", FFMpegConverter::AVStrError(ret).c_str());
123da853ecaSopenharmony_ci        return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN;
124da853ecaSopenharmony_ci    }
125da853ecaSopenharmony_ci    return AVCodecServiceErrCode::AVCS_ERR_OK;
126da853ecaSopenharmony_ci}
127da853ecaSopenharmony_ci} // namespace MediaAVCodec
128da853ecaSopenharmony_ci} // namespace OHOS
129