blob: 91f6f23a1cbdeb09591eb5d484633174ff7f4c8f [file] [log] [blame]
Lei Qian7bf98232018-09-20 17:56:38 +08001/*
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.gao989a73a2022-12-28 06:58:14 +000022#include <cutils/log.h>
Lei Qian7bf98232018-09-20 17:56:38 +080023#include <system/audio.h>
Lei Qian7bf98232018-09-20 17:56:38 +080024#include <tinyalsa/asoundlib.h>
25#include <cutils/properties.h>
xingri.gaoc906c032023-09-28 03:05:28 +000026#include "SPDIFEncoder.h"
Lei Qian7bf98232018-09-20 17:56:38 +080027#include "SPDIFEncoderAD.h"
28#include "aml_android_utils.h"
dongyang.zhang20cd1142024-07-18 16:20:14 +080029#include "aml_dump_debug.h"
Lei Qian7bf98232018-09-20 17:56:38 +080030
31extern "C"
32{
33//#include "audio_hw_utils.h"
34}
35
36
37namespace android
38{
39class SPDIFEncoderAD : public SPDIFEncoder
40{
41public:
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.zhang20cd1142024-07-18 16:20:14 +080070 if (get_debug_value(AML_DUMP_AUDIOHAL_SPDIF)) {
71 aml_dump_audio_bitstreams("enc_output.spdif", iec61937_buffer, actual_write_size);
Lei Qian7bf98232018-09-20 17:56:38 +080072 }
Lei Qian7bf98232018-09-20 17:56:38 +080073 return actual_write_size;
74 }
75 else
76 return -1;
Lei Qian7bf98232018-09-20 17:56:38 +080077 }
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 }
97protected:
98
99private:
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.du06ada4c2021-06-24 04:04:01 -0400108
109extern "C" int spdif_encoder_ad_init(void **pphandle, audio_format_t format, const void *output, int max_output_size)
Lei Qian7bf98232018-09-20 17:56:38 +0800110{
wei.du06ada4c2021-06-24 04:04:01 -0400111 SPDIFEncoderAD *spdif_encoder_ad = NULL;
112
Lei Qian7bf98232018-09-20 17:56:38 +0800113 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.du06ada4c2021-06-24 04:04:01 -0400118 *pphandle = (void *)spdif_encoder_ad;
Lei Qian7bf98232018-09-20 17:56:38 +0800119 ALOGI("init SPDIFEncoderAD done\n");
120 return 0;
121}
wei.du06ada4c2021-06-24 04:04:01 -0400122extern "C" int spdif_encoder_ad_deinit(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800123{
wei.du06ada4c2021-06-24 04:04:01 -0400124 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
125 if (spdif_encoder_ad) {
126 delete spdif_encoder_ad;
127 }
128
129 return 0;
130}
131
132
133extern "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.zhang20cd1142024-07-18 16:20:14 +0800140 if (get_debug_value(AML_DUMP_AUDIOHAL_SPDIF)) {
141 aml_dump_audio_bitstreams("enc_input.spdif", buffer, numBytes);
Lei Qian7bf98232018-09-20 17:56:38 +0800142 }
Lei Qian7bf98232018-09-20 17:56:38 +0800143 return spdif_encoder_ad->write(buffer, numBytes);
144}
wei.du06ada4c2021-06-24 04:04:01 -0400145extern "C" uint64_t spdif_encoder_ad_get_total(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800146{
wei.du06ada4c2021-06-24 04:04:01 -0400147 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
148 if (phandle == NULL) {
149 return -1;
150 }
151
Lei Qian7bf98232018-09-20 17:56:38 +0800152 return spdif_encoder_ad->total_bytes();
153}
154/*
155 *@brief get current iec61937 data size
156 */
wei.du06ada4c2021-06-24 04:04:01 -0400157extern "C" size_t spdif_encoder_ad_get_current_position(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800158{
wei.du06ada4c2021-06-24 04:04:01 -0400159 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
160 if (phandle == NULL) {
161 return -1;
162 }
163
Lei Qian7bf98232018-09-20 17:56:38 +0800164 return spdif_encoder_ad->getCurrentIEC61937DataSize();
165}
166/*
167 *@brief flush output iec61937 data current position to zero!
168 */
wei.du06ada4c2021-06-24 04:04:01 -0400169extern "C" void spdif_encoder_ad_flush_output_current_position(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800170{
wei.du06ada4c2021-06-24 04:04:01 -0400171 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
172 if (phandle == NULL) {
173 return;
174 }
175
Lei Qian7bf98232018-09-20 17:56:38 +0800176 return spdif_encoder_ad->flushOutputCurrentPosition();
177}
178
wei.du06ada4c2021-06-24 04:04:01 -0400179extern "C" void spdif_encoder_ad_reset(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800180{
wei.du06ada4c2021-06-24 04:04:01 -0400181 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
182 if (phandle == NULL) {
183 return;
184 }
185
Lei Qian7bf98232018-09-20 17:56:38 +0800186 return spdif_encoder_ad->reset();
187}
188
189}