1/*
2Copyright (c) 2011 Stanislav Vitvitskiy
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this
5software and associated documentation files (the "Software"), to deal in the Software
6without restriction, including without limitation the rights to use, copy, modify,
7merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8permit persons to whom the Software is furnished to do so, subject to the following
9conditions:
10
11The above copyright notice and this permission notice shall be included in all copies or
12substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19OR OTHER DEALINGS IN THE SOFTWARE.
20*/
21package com.googlecode.mp4parser.h264.write;
22
23import com.googlecode.mp4parser.h264.Debug;
24
25import java.io.IOException;
26import java.io.OutputStream;
27
28
29/**
30 * A class responsible for outputting exp-Golumb values into binary stream
31 *
32 * @author Stanislav Vitvitskiy
33 */
34public class CAVLCWriter extends BitstreamWriter {
35
36    public CAVLCWriter(OutputStream out) {
37        super(out);
38    }
39
40    public void writeU(int value, int n, String string) throws IOException {
41        Debug.print(string + "\t");
42        writeNBit(value, n);
43        Debug.println("\t" + value);
44    }
45
46    public void writeUE(int value) throws IOException {
47        int bits = 0;
48        int cumul = 0;
49        for (int i = 0; i < 15; i++) {
50            if (value < cumul + (1 << i)) {
51                bits = i;
52                break;
53            }
54            cumul += (1 << i);
55        }
56        writeNBit(0, bits);
57        write1Bit(1);
58        writeNBit(value - cumul, bits);
59    }
60
61    public void writeUE(int value, String string) throws IOException {
62        Debug.print(string + "\t");
63        writeUE(value);
64        Debug.println("\t" + value);
65    }
66
67    public void writeSE(int value, String string) throws IOException {
68        Debug.print(string + "\t");
69        writeUE((value << 1) * (value < 0 ? -1 : 1) + (value > 0 ? 1 : 0));
70        Debug.println("\t" + value);
71    }
72
73    public void writeBool(boolean value, String string) throws IOException {
74        Debug.print(string + "\t");
75        write1Bit(value ? 1 : 0);
76        Debug.println("\t" + value);
77    }
78
79    public void writeU(int i, int n) throws IOException {
80        writeNBit(i, n);
81    }
82
83    public void writeNBit(long value, int n, String string) throws IOException {
84        Debug.print(string + "\t");
85        for (int i = 0; i < n; i++) {
86            write1Bit((int) (value >> (n - i - 1)) & 0x1);
87        }
88        Debug.println("\t" + value);
89    }
90
91    public void writeTrailingBits() throws IOException {
92        write1Bit(1);
93        writeRemainingZero();
94        flush();
95    }
96
97    public void writeSliceTrailingBits() {
98        throw new IllegalStateException("todo");
99    }
100}