blob: 3007ba23727b789a56bc1e8b4d0feabbe1bc9609 [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>
wei.du06ada4c2021-06-24 04:04:01 -040022#include <utils/Log.h>
Lei Qian7bf98232018-09-20 17:56:38 +080023#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
30extern "C"
31{
32//#include "audio_hw_utils.h"
33}
34
35
36namespace android
37{
38class SPDIFEncoderAD : public SPDIFEncoder
39{
40public:
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.du06ada4c2021-06-24 04:04:01 -040070 if (aml_getprop_bool("vendor.media.audiohal.outdump")) {
Lei Qian7bf98232018-09-20 17:56:38 +080071 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 Qian7bf98232018-09-20 17:56:38 +080085 }
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 }
105protected:
106
107private:
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.du06ada4c2021-06-24 04:04:01 -0400116
117extern "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 +0800118{
wei.du06ada4c2021-06-24 04:04:01 -0400119 SPDIFEncoderAD *spdif_encoder_ad = NULL;
120
Lei Qian7bf98232018-09-20 17:56:38 +0800121 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.du06ada4c2021-06-24 04:04:01 -0400126 *pphandle = (void *)spdif_encoder_ad;
Lei Qian7bf98232018-09-20 17:56:38 +0800127 ALOGI("init SPDIFEncoderAD done\n");
128 return 0;
129}
wei.du06ada4c2021-06-24 04:04:01 -0400130extern "C" int spdif_encoder_ad_deinit(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800131{
wei.du06ada4c2021-06-24 04:04:01 -0400132 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
133 if (spdif_encoder_ad) {
134 delete spdif_encoder_ad;
135 }
136
137 return 0;
138}
139
140
141extern "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 Qian7bf98232018-09-20 17:56:38 +0800148#if 1
wei.du06ada4c2021-06-24 04:04:01 -0400149 if (aml_getprop_bool("vendor.media.audiohal.outdump")) {
Lei Qian7bf98232018-09-20 17:56:38 +0800150 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.du06ada4c2021-06-24 04:04:01 -0400159extern "C" uint64_t spdif_encoder_ad_get_total(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800160{
wei.du06ada4c2021-06-24 04:04:01 -0400161 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
162 if (phandle == NULL) {
163 return -1;
164 }
165
Lei Qian7bf98232018-09-20 17:56:38 +0800166 return spdif_encoder_ad->total_bytes();
167}
168/*
169 *@brief get current iec61937 data size
170 */
wei.du06ada4c2021-06-24 04:04:01 -0400171extern "C" size_t spdif_encoder_ad_get_current_position(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800172{
wei.du06ada4c2021-06-24 04:04:01 -0400173 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
174 if (phandle == NULL) {
175 return -1;
176 }
177
Lei Qian7bf98232018-09-20 17:56:38 +0800178 return spdif_encoder_ad->getCurrentIEC61937DataSize();
179}
180/*
181 *@brief flush output iec61937 data current position to zero!
182 */
wei.du06ada4c2021-06-24 04:04:01 -0400183extern "C" void spdif_encoder_ad_flush_output_current_position(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800184{
wei.du06ada4c2021-06-24 04:04:01 -0400185 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
186 if (phandle == NULL) {
187 return;
188 }
189
Lei Qian7bf98232018-09-20 17:56:38 +0800190 return spdif_encoder_ad->flushOutputCurrentPosition();
191}
192
wei.du06ada4c2021-06-24 04:04:01 -0400193extern "C" void spdif_encoder_ad_reset(void *phandle)
Lei Qian7bf98232018-09-20 17:56:38 +0800194{
wei.du06ada4c2021-06-24 04:04:01 -0400195 SPDIFEncoderAD *spdif_encoder_ad = (SPDIFEncoderAD *) phandle;
196 if (phandle == NULL) {
197 return;
198 }
199
Lei Qian7bf98232018-09-20 17:56:38 +0800200 return spdif_encoder_ad->reset();
201}
202
203}