blob: 84c7b2423191f1936f1e98e2a90368b49f4db086 [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>
Tim Yaoe004da02019-12-03 11:59:05 -080022#include <cutils/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;
Tim Yao20c2bf62020-07-20 01:56:44 -070065#if 0
Lei Qian7bf98232018-09-20 17:56:38 +080066 if (actual_write_size > 0) {
Tim Yao20c2bf62020-07-20 01:56:44 -070067#endif
Lei Qian7bf98232018-09-20 17:56:38 +080068 outBufCurrentPos += actual_write_size;
69 mTotalBytes += actual_write_size;
70 ALOGV("%s() actual_write_size %zu outBufCurrentPos %zu\n", __FUNCTION__, actual_write_size, outBufCurrentPos);
71#if 1
72 if (aml_getprop_bool("media.audiohal.outdump")) {
73 FILE *fp1 = fopen("/data/audio_out/enc_output.spdif", "a+");
74 if (fp1) {
75 int flen = fwrite((char *)iec61937_buffer, 1, actual_write_size, fp1);
76 ALOGV("%s iec61937_buffer %p write_size %d\n", __FUNCTION__, iec61937_buffer, flen);
77 fclose(fp1);
78 } else {
79 //ALOGD("could not open file:/data/hdmi_audio_out.pcm");
80 }
81 }
82#endif
83 return actual_write_size;
Tim Yao20c2bf62020-07-20 01:56:44 -070084#if 0
Lei Qian7bf98232018-09-20 17:56:38 +080085 }
86 else
87 return -1;
Tim Yao20c2bf62020-07-20 01:56:44 -070088#endif
Lei Qian7bf98232018-09-20 17:56:38 +080089 }
90 /*
91 *@brief get current iec61937 data size
92 */
93 virtual size_t getCurrentIEC61937DataSize(void) {
94 return outBufCurrentPos;
95 }
96 /*
97 *@brief flush output iec61937 data current position to zero!
98 */
99 virtual void flushOutputCurrentPosition() {
100 outBufCurrentPos = 0;
101 }
102 /*
103 *@brief get total tytes that through the spdif encoder
104 */
105 virtual uint64_t total_bytes()
106 {
107 return mTotalBytes;
108 }
109protected:
110
111private:
112 uint64_t mTotalBytes;
113// uint64_t eac3_frame;
114// audio_format_t mformat;
115 const void *outBuf;
116 size_t outBufSize;
117 size_t outBufCurrentPos;
118
119};
120static SPDIFEncoderAD *spdif_encoder_ad = NULL;
121extern "C" int spdif_encoder_ad_init(audio_format_t format, const void *output, int max_output_size)
122{
123 if (spdif_encoder_ad) {
124 delete spdif_encoder_ad;
125 spdif_encoder_ad = NULL;
126 }
127 spdif_encoder_ad = new SPDIFEncoderAD(format, output, max_output_size);
128 if (spdif_encoder_ad == NULL) {
129 ALOGE("init SPDIFEncoderAD failed \n");
130 return -1;
131 }
132 ALOGI("init SPDIFEncoderAD done\n");
133 return 0;
134}
135extern "C" int spdif_encoder_ad_write(const void *buffer, size_t numBytes)
136{
137#if 1
138 if (aml_getprop_bool("media.audiohal.outdump")) {
139 FILE *fp1 = fopen("/data/audio_out/enc_input.spdif", "a+");
140 if (fp1) {
141 fwrite((char *)buffer, 1, numBytes, fp1);
142 fclose(fp1);
143 }
144 }
145#endif
146 return spdif_encoder_ad->write(buffer, numBytes);
147}
148extern "C" uint64_t spdif_encoder_ad_get_total()
149{
150 return spdif_encoder_ad->total_bytes();
151}
152/*
153 *@brief get current iec61937 data size
154 */
155extern "C" size_t spdif_encoder_ad_get_current_position(void)
156{
157 return spdif_encoder_ad->getCurrentIEC61937DataSize();
158}
159/*
160 *@brief flush output iec61937 data current position to zero!
161 */
162extern "C" void spdif_encoder_ad_flush_output_current_position(void)
163{
164 return spdif_encoder_ad->flushOutputCurrentPosition();
165}
166
167extern "C" void spdif_encoder_ad_reset(void)
168{
169 return spdif_encoder_ad->reset();
170}
171
172}