summaryrefslogtreecommitdiffstats
path: root/audio_utils/include/audio_utils/spdif/SPDIFEncoder.h
blob: 1a432d820fc9e45495a27c62bca7e1f4cc11e2fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2014, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_AUDIO_SPDIF_ENCODER_H
#define ANDROID_AUDIO_SPDIF_ENCODER_H

#include <stdint.h>
#include <system/audio.h>
#include <audio_utils/spdif/FrameScanner.h>

namespace android {

/**
 * Scan the incoming byte stream for a frame sync.
 * Then wrap the encoded frame in a data burst and send it as if it were PCM.
 * The receiver will see the data burst header and decode the wrapped frame.
 */
#define SPDIF_MAX_CHANNELS          8
#define SPDIF_ENCODED_CHANNEL_COUNT 2

class SPDIFEncoder {
public:

    explicit SPDIFEncoder(audio_format_t format);
    // Defaults to AC3 format. Was in original API.
    SPDIFEncoder();

    virtual ~SPDIFEncoder();

    /**
     * Write encoded data to be wrapped for SPDIF.
     * The compressed frames do not have to be aligned.
     * @return number of bytes written or negative error
     */
    ssize_t write( const void* buffer, size_t numBytes );

    /**
     * Called by SPDIFEncoder when it is ready to output a data burst.
     * Must be implemented in the subclass.
     * @return number of bytes written or negative error
     */
    virtual ssize_t writeOutput( const void* buffer, size_t numBytes ) = 0;

    /**
     * Get ratio of the encoded data burst sample rate to the encoded rate.
     * For example, EAC3 data bursts are 4X the encoded rate.
     */
    uint32_t getRateMultiplier() const { return mRateMultiplier; }

    /**
     * @return number of PCM frames in a data burst
     */
    uint32_t getBurstFrames() const { return mBurstFrames; }

    /**
     * @return number of bytes per PCM frame for the data burst
     */
    int      getBytesPerOutputFrame();

    /**
     * @return  true if we can wrap this format in an SPDIF stream
     */
    static bool isFormatSupported(audio_format_t format);

    /**
     * Discard any data in the buffer. Reset frame scanners.
     * This should be called when seeking to a new position in the stream.
     */
    void reset();

protected:
    void   clearBurstBuffer();
    bool   wouldOverflowBuffer(size_t numBytes) const; // Would this many bytes cause an overflow?
    void   writeBurstBufferShorts(const uint16_t* buffer, size_t numBytes);
    void   writeBurstBufferBytes(const uint8_t* buffer, size_t numBytes);
    void   sendZeroPad();
    void   flushBurstBuffer();
    void   startDataBurst();
    size_t startSyncFrame();

    // Works with various formats including AC3.
    FrameScanner *mFramer;

    uint32_t  mSampleRate;
    size_t    mFrameSize;   // size of sync frame in bytes
    uint16_t *mBurstBuffer; // ALSA wants to get SPDIF data as shorts.
    size_t    mBurstBufferSizeBytes;
    uint32_t  mRateMultiplier;
    uint32_t  mBurstFrames;
    size_t    mByteCursor;  // cursor into data burst
    int       mBitstreamNumber;
    size_t    mPayloadBytesPending; // number of bytes needed to finish burst
    // state variable, true if scanning for start of frame
    bool      mScanning;

    static const uint16_t kSPDIFSync1; // Pa
    static const uint16_t kSPDIFSync2; // Pb
};

}  // namespace android

#endif  // ANDROID_AUDIO_SPDIF_ENCODER_H