1package com.googlecode.mp4parser.boxes;
2
3import com.googlecode.mp4parser.AbstractBox;
4import com.googlecode.mp4parser.boxes.mp4.objectdescriptors.BitReaderBuffer;
5import com.googlecode.mp4parser.boxes.mp4.objectdescriptors.BitWriterBuffer;
6
7import java.nio.ByteBuffer;
8import java.util.LinkedList;
9import java.util.List;
10
11/**
12 *
13 */
14public class EC3SpecificBox extends AbstractBox {
15    List<Entry> entries = new LinkedList<Entry>();
16    int dataRate;
17    int numIndSub;
18
19    public EC3SpecificBox() {
20        super("dec3");
21    }
22
23    @Override
24    public long getContentSize() {
25        long size = 2;
26        for (Entry entry : entries) {
27            if (entry.num_dep_sub > 0) {
28                size += 4;
29            } else {
30                size += 3;
31            }
32        }
33        return size;
34    }
35
36    @Override
37    public void _parseDetails(ByteBuffer content) {
38        BitReaderBuffer brb = new BitReaderBuffer(content);
39        dataRate = brb.readBits(13);
40        numIndSub = brb.readBits(3) + 1;
41        // This field indicates the number of independent substreams that are present in the Enhanced AC-3 bitstream. The value
42        // of this field is one less than the number of independent substreams present.
43
44
45        for (int i = 0; i < numIndSub; i++) {
46            Entry e = new Entry();
47            e.fscod = brb.readBits(2);
48            e.bsid = brb.readBits(5);
49            e.bsmod = brb.readBits(5);
50            e.acmod = brb.readBits(3);
51            e.lfeon = brb.readBits(1);
52            e.reserved = brb.readBits(3);
53            e.num_dep_sub = brb.readBits(4);
54            if (e.num_dep_sub > 0) {
55                e.chan_loc = brb.readBits(9);
56            } else {
57                e.reserved2 = brb.readBits(1);
58            }
59            entries.add(e);
60        }
61    }
62
63    @Override
64    public void getContent(ByteBuffer byteBuffer) {
65        BitWriterBuffer bwb = new BitWriterBuffer(byteBuffer);
66        bwb.writeBits(dataRate, 13);
67        bwb.writeBits(entries.size() - 1, 3);
68        for (Entry e : entries) {
69            bwb.writeBits(e.fscod, 2);
70            bwb.writeBits(e.bsid, 5);
71            bwb.writeBits(e.bsmod, 5);
72            bwb.writeBits(e.acmod, 3);
73            bwb.writeBits(e.lfeon, 1);
74            bwb.writeBits(e.reserved, 3);
75            bwb.writeBits(e.num_dep_sub, 4);
76            if (e.num_dep_sub > 0) {
77                bwb.writeBits(e.chan_loc, 9);
78            } else {
79                bwb.writeBits(e.reserved2, 1);
80            }
81        }
82    }
83
84
85    public List<Entry> getEntries() {
86        return entries;
87    }
88
89    public void setEntries(List<Entry> entries) {
90        this.entries = entries;
91    }
92
93    public void addEntry(Entry entry) {
94        this.entries.add(entry);
95    }
96
97    public int getDataRate() {
98        return dataRate;
99    }
100
101    public void setDataRate(int dataRate) {
102        this.dataRate = dataRate;
103    }
104
105    public int getNumIndSub() {
106        return numIndSub;
107    }
108
109    public void setNumIndSub(int numIndSub) {
110        this.numIndSub = numIndSub;
111    }
112
113    public static class Entry {
114        public int fscod;
115        public int bsid;
116        public int bsmod;
117        public int acmod;
118        public int lfeon;
119        public int reserved;
120        public int num_dep_sub;
121        public int chan_loc;
122        public int reserved2;
123
124
125        @Override
126        public String toString() {
127            return "Entry{" +
128                    "fscod=" + fscod +
129                    ", bsid=" + bsid +
130                    ", bsmod=" + bsmod +
131                    ", acmod=" + acmod +
132                    ", lfeon=" + lfeon +
133                    ", reserved=" + reserved +
134                    ", num_dep_sub=" + num_dep_sub +
135                    ", chan_loc=" + chan_loc +
136                    ", reserved2=" + reserved2 +
137                    '}';
138        }
139    }
140}
141