Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Amlogic Corporation. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "SPDIFEncoderAD" |
| 21 | #include <stdint.h> |
xingri.gao | 989a73a | 2022-12-28 06:58:14 +0000 | [diff] [blame] | 22 | #include <cutils/log.h> |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 23 | #include <system/audio.h> |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 24 | #include <tinyalsa/asoundlib.h> |
| 25 | #include <cutils/properties.h> |
xingri.gao | c906c03 | 2023-09-28 03:05:28 +0000 | [diff] [blame] | 26 | #include "SPDIFEncoder.h" |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 27 | #include "SPDIFEncoderAD.h" |
| 28 | #include "aml_android_utils.h" |
dongyang.zhang | 20cd114 | 2024-07-18 16:20:14 +0800 | [diff] [blame] | 29 | #include "aml_dump_debug.h" |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 30 | |
| 31 | extern "C" |
| 32 | { |
| 33 | //#include "audio_hw_utils.h" |
| 34 | } |
| 35 | |
| 36 | |
| 37 | namespace android |
| 38 | { |
| 39 | class SPDIFEncoderAD : public SPDIFEncoder |
| 40 | { |
| 41 | public: |
| 42 | SPDIFEncoderAD(audio_format_t format, const void *output, size_t max_output_size) |
| 43 | : SPDIFEncoder(format) |
| 44 | , mTotalBytes(0) |
| 45 | //, eac3_frame(0) |
| 46 | //, mformat(format) |
| 47 | , outBuf(output) |
| 48 | , outBufSize(max_output_size) |
| 49 | , outBufCurrentPos(0) |
| 50 | { |
| 51 | ALOGI("%s() format %#x outBuf %p outBufSize %zu\n", __FUNCTION__, format, outBuf, outBufSize); |
| 52 | }; |
| 53 | virtual ssize_t writeOutput(const void* buffer, size_t bytes) |
| 54 | { |
| 55 | // int ret = -1; |
| 56 | ALOGV("write size %zu , outBufCurrentPos %zu\n", bytes, outBufCurrentPos); |
| 57 | |
| 58 | // ret = pcm_write(buffer, bytes); |
| 59 | char *iec61937_buffer = (char *)outBuf + outBufCurrentPos; |
| 60 | size_t actual_write_size = |
| 61 | ((outBufSize - outBufCurrentPos) > bytes) ? (bytes) : (outBufSize - outBufCurrentPos); |
| 62 | if (outBuf && (actual_write_size > 0)) |
| 63 | memcpy((void *)iec61937_buffer, (const void*)buffer, actual_write_size); |
| 64 | else |
| 65 | return -1; |
| 66 | if (actual_write_size > 0) { |
| 67 | outBufCurrentPos += actual_write_size; |
| 68 | mTotalBytes += actual_write_size; |
| 69 | ALOGV("%s() actual_write_size %zu outBufCurrentPos %zu\n", __FUNCTION__, actual_write_size, outBufCurrentPos); |
dongyang.zhang | 20cd114 | 2024-07-18 16:20:14 +0800 | [diff] [blame] | 70 | if (get_debug_value(AML_DUMP_AUDIOHAL_SPDIF)) { |
| 71 | aml_dump_audio_bitstreams("enc_output.spdif", iec61937_buffer, actual_write_size); |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 72 | } |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 73 | return actual_write_size; |
| 74 | } |
| 75 | else |
| 76 | return -1; |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 77 | } |
| 78 | /* |
| 79 | *@brief get current iec61937 data size |
| 80 | */ |
| 81 | virtual size_t getCurrentIEC61937DataSize(void) { |
| 82 | return outBufCurrentPos; |
| 83 | } |
| 84 | /* |
| 85 | *@brief flush output iec61937 data current position to zero! |
| 86 | */ |
| 87 | virtual void flushOutputCurrentPosition() { |
| 88 | outBufCurrentPos = 0; |
| 89 | } |
| 90 | /* |
| 91 | *@brief get total tytes that through the spdif encoder |
| 92 | */ |
| 93 | virtual uint64_t total_bytes() |
| 94 | { |
| 95 | return mTotalBytes; |
| 96 | } |
| 97 | protected: |
| 98 | |
| 99 | private: |
| 100 | uint64_t mTotalBytes; |
| 101 | // uint64_t eac3_frame; |
| 102 | // audio_format_t mformat; |
| 103 | const void *outBuf; |
| 104 | size_t outBufSize; |
| 105 | size_t outBufCurrentPos; |
| 106 | |
| 107 | }; |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 108 | |
| 109 | extern "C" int spdif_encoder_ad_init(void **pphandle, audio_format_t format, const void *output, int max_output_size) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 110 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 111 | SPDIFEncoderAD *spdif_encoder_ad = NULL; |
| 112 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 113 | spdif_encoder_ad = new SPDIFEncoderAD(format, output, max_output_size); |
| 114 | if (spdif_encoder_ad == NULL) { |
| 115 | ALOGE("init SPDIFEncoderAD failed \n"); |
| 116 | return -1; |
| 117 | } |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 118 | *pphandle = (void *)spdif_encoder_ad; |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 119 | ALOGI("init SPDIFEncoderAD done\n"); |
| 120 | return 0; |
| 121 | } |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 122 | extern "C" int spdif_encoder_ad_deinit(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 123 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 124 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 125 | if (spdif_encoder_ad) { |
| 126 | delete spdif_encoder_ad; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | extern "C" int spdif_encoder_ad_write(void *phandle, const void *buffer, size_t numBytes) |
| 134 | { |
| 135 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 136 | if (phandle == NULL) { |
| 137 | return -1; |
| 138 | } |
| 139 | |
dongyang.zhang | 20cd114 | 2024-07-18 16:20:14 +0800 | [diff] [blame] | 140 | if (get_debug_value(AML_DUMP_AUDIOHAL_SPDIF)) { |
| 141 | aml_dump_audio_bitstreams("enc_input.spdif", buffer, numBytes); |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 142 | } |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 143 | return spdif_encoder_ad->write(buffer, numBytes); |
| 144 | } |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 145 | extern "C" uint64_t spdif_encoder_ad_get_total(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 146 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 147 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 148 | if (phandle == NULL) { |
| 149 | return -1; |
| 150 | } |
| 151 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 152 | return spdif_encoder_ad->total_bytes(); |
| 153 | } |
| 154 | /* |
| 155 | *@brief get current iec61937 data size |
| 156 | */ |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 157 | extern "C" size_t spdif_encoder_ad_get_current_position(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 158 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 159 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 160 | if (phandle == NULL) { |
| 161 | return -1; |
| 162 | } |
| 163 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 164 | return spdif_encoder_ad->getCurrentIEC61937DataSize(); |
| 165 | } |
| 166 | /* |
| 167 | *@brief flush output iec61937 data current position to zero! |
| 168 | */ |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 169 | extern "C" void spdif_encoder_ad_flush_output_current_position(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 170 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 171 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 172 | if (phandle == NULL) { |
| 173 | return; |
| 174 | } |
| 175 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 176 | return spdif_encoder_ad->flushOutputCurrentPosition(); |
| 177 | } |
| 178 | |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 179 | extern "C" void spdif_encoder_ad_reset(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 180 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 181 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 182 | if (phandle == NULL) { |
| 183 | return; |
| 184 | } |
| 185 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 186 | return spdif_encoder_ad->reset(); |
| 187 | } |
| 188 | |
| 189 | } |