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> |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 22 | #include <utils/Log.h> |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 23 | #include <system/audio.h> |
| 24 | #include <audio_utils/spdif/SPDIFEncoder.h> |
| 25 | #include <tinyalsa/asoundlib.h> |
| 26 | #include <cutils/properties.h> |
| 27 | #include "SPDIFEncoderAD.h" |
| 28 | #include "aml_android_utils.h" |
| 29 | |
| 30 | extern "C" |
| 31 | { |
| 32 | //#include "audio_hw_utils.h" |
| 33 | } |
| 34 | |
| 35 | |
| 36 | namespace android |
| 37 | { |
| 38 | class SPDIFEncoderAD : public SPDIFEncoder |
| 39 | { |
| 40 | public: |
| 41 | SPDIFEncoderAD(audio_format_t format, const void *output, size_t max_output_size) |
| 42 | : SPDIFEncoder(format) |
| 43 | , mTotalBytes(0) |
| 44 | //, eac3_frame(0) |
| 45 | //, mformat(format) |
| 46 | , outBuf(output) |
| 47 | , outBufSize(max_output_size) |
| 48 | , outBufCurrentPos(0) |
| 49 | { |
| 50 | ALOGI("%s() format %#x outBuf %p outBufSize %zu\n", __FUNCTION__, format, outBuf, outBufSize); |
| 51 | }; |
| 52 | virtual ssize_t writeOutput(const void* buffer, size_t bytes) |
| 53 | { |
| 54 | // int ret = -1; |
| 55 | ALOGV("write size %zu , outBufCurrentPos %zu\n", bytes, outBufCurrentPos); |
| 56 | |
| 57 | // ret = pcm_write(buffer, bytes); |
| 58 | char *iec61937_buffer = (char *)outBuf + outBufCurrentPos; |
| 59 | size_t actual_write_size = |
| 60 | ((outBufSize - outBufCurrentPos) > bytes) ? (bytes) : (outBufSize - outBufCurrentPos); |
| 61 | if (outBuf && (actual_write_size > 0)) |
| 62 | memcpy((void *)iec61937_buffer, (const void*)buffer, actual_write_size); |
| 63 | else |
| 64 | return -1; |
| 65 | if (actual_write_size > 0) { |
| 66 | outBufCurrentPos += actual_write_size; |
| 67 | mTotalBytes += actual_write_size; |
| 68 | ALOGV("%s() actual_write_size %zu outBufCurrentPos %zu\n", __FUNCTION__, actual_write_size, outBufCurrentPos); |
| 69 | #if 1 |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 70 | if (aml_getprop_bool("vendor.media.audiohal.outdump")) { |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 71 | FILE *fp1 = fopen("/data/audio_out/enc_output.spdif", "a+"); |
| 72 | if (fp1) { |
| 73 | int flen = fwrite((char *)iec61937_buffer, 1, actual_write_size, fp1); |
| 74 | ALOGV("%s iec61937_buffer %p write_size %d\n", __FUNCTION__, iec61937_buffer, flen); |
| 75 | fclose(fp1); |
| 76 | } else { |
| 77 | //ALOGD("could not open file:/data/hdmi_audio_out.pcm"); |
| 78 | } |
| 79 | } |
| 80 | #endif |
| 81 | return actual_write_size; |
| 82 | } |
| 83 | else |
| 84 | return -1; |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 85 | } |
| 86 | /* |
| 87 | *@brief get current iec61937 data size |
| 88 | */ |
| 89 | virtual size_t getCurrentIEC61937DataSize(void) { |
| 90 | return outBufCurrentPos; |
| 91 | } |
| 92 | /* |
| 93 | *@brief flush output iec61937 data current position to zero! |
| 94 | */ |
| 95 | virtual void flushOutputCurrentPosition() { |
| 96 | outBufCurrentPos = 0; |
| 97 | } |
| 98 | /* |
| 99 | *@brief get total tytes that through the spdif encoder |
| 100 | */ |
| 101 | virtual uint64_t total_bytes() |
| 102 | { |
| 103 | return mTotalBytes; |
| 104 | } |
| 105 | protected: |
| 106 | |
| 107 | private: |
| 108 | uint64_t mTotalBytes; |
| 109 | // uint64_t eac3_frame; |
| 110 | // audio_format_t mformat; |
| 111 | const void *outBuf; |
| 112 | size_t outBufSize; |
| 113 | size_t outBufCurrentPos; |
| 114 | |
| 115 | }; |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 116 | |
| 117 | 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] | 118 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 119 | SPDIFEncoderAD *spdif_encoder_ad = NULL; |
| 120 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 121 | spdif_encoder_ad = new SPDIFEncoderAD(format, output, max_output_size); |
| 122 | if (spdif_encoder_ad == NULL) { |
| 123 | ALOGE("init SPDIFEncoderAD failed \n"); |
| 124 | return -1; |
| 125 | } |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 126 | *pphandle = (void *)spdif_encoder_ad; |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 127 | ALOGI("init SPDIFEncoderAD done\n"); |
| 128 | return 0; |
| 129 | } |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 130 | extern "C" int spdif_encoder_ad_deinit(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 131 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 132 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 133 | if (spdif_encoder_ad) { |
| 134 | delete spdif_encoder_ad; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | extern "C" int spdif_encoder_ad_write(void *phandle, const void *buffer, size_t numBytes) |
| 142 | { |
| 143 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 144 | if (phandle == NULL) { |
| 145 | return -1; |
| 146 | } |
| 147 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 148 | #if 1 |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 149 | if (aml_getprop_bool("vendor.media.audiohal.outdump")) { |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 150 | FILE *fp1 = fopen("/data/audio_out/enc_input.spdif", "a+"); |
| 151 | if (fp1) { |
| 152 | fwrite((char *)buffer, 1, numBytes, fp1); |
| 153 | fclose(fp1); |
| 154 | } |
| 155 | } |
| 156 | #endif |
| 157 | return spdif_encoder_ad->write(buffer, numBytes); |
| 158 | } |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 159 | extern "C" uint64_t spdif_encoder_ad_get_total(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 160 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 161 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 162 | if (phandle == NULL) { |
| 163 | return -1; |
| 164 | } |
| 165 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 166 | return spdif_encoder_ad->total_bytes(); |
| 167 | } |
| 168 | /* |
| 169 | *@brief get current iec61937 data size |
| 170 | */ |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 171 | extern "C" size_t spdif_encoder_ad_get_current_position(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 172 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 173 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 174 | if (phandle == NULL) { |
| 175 | return -1; |
| 176 | } |
| 177 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 178 | return spdif_encoder_ad->getCurrentIEC61937DataSize(); |
| 179 | } |
| 180 | /* |
| 181 | *@brief flush output iec61937 data current position to zero! |
| 182 | */ |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 183 | extern "C" void spdif_encoder_ad_flush_output_current_position(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 184 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 185 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 186 | if (phandle == NULL) { |
| 187 | return; |
| 188 | } |
| 189 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 190 | return spdif_encoder_ad->flushOutputCurrentPosition(); |
| 191 | } |
| 192 | |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 193 | extern "C" void spdif_encoder_ad_reset(void *phandle) |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 194 | { |
wei.du | 06ada4c | 2021-06-24 04:04:01 -0400 | [diff] [blame] | 195 | SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle; |
| 196 | if (phandle == NULL) { |
| 197 | return; |
| 198 | } |
| 199 | |
Lei Qian | 7bf9823 | 2018-09-20 17:56:38 +0800 | [diff] [blame] | 200 | return spdif_encoder_ad->reset(); |
| 201 | } |
| 202 | |
| 203 | } |