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.model;
22
23import com.googlecode.mp4parser.h264.read.CAVLCReader;
24import com.googlecode.mp4parser.h264.write.CAVLCWriter;
25
26import java.io.IOException;
27
28/**
29 * Scaling list entity
30 * <p/>
31 * capable to serialize / deserialize with CAVLC bitstream
32 *
33 * @author Stanislav Vitvitskiy
34 */
35public class ScalingList {
36
37    public int[] scalingList;
38    public boolean useDefaultScalingMatrixFlag;
39
40    public void write(CAVLCWriter out) throws IOException {
41        if (useDefaultScalingMatrixFlag) {
42            out.writeSE(0, "SPS: ");
43            return;
44        }
45
46        int lastScale = 8;
47        int nextScale = 8;
48        for (int j = 0; j < scalingList.length; j++) {
49            if (nextScale != 0) {
50                int deltaScale = scalingList[j] - lastScale - 256;
51                out.writeSE(deltaScale, "SPS: ");
52            }
53            lastScale = scalingList[j];
54        }
55    }
56
57    public static ScalingList read(CAVLCReader is, int sizeOfScalingList)
58            throws IOException {
59
60        ScalingList sl = new ScalingList();
61        sl.scalingList = new int[sizeOfScalingList];
62        int lastScale = 8;
63        int nextScale = 8;
64        for (int j = 0; j < sizeOfScalingList; j++) {
65            if (nextScale != 0) {
66                int deltaScale = is.readSE("deltaScale");
67                nextScale = (lastScale + deltaScale + 256) % 256;
68                sl.useDefaultScalingMatrixFlag = (j == 0 && nextScale == 0);
69            }
70            sl.scalingList[j] = nextScale == 0 ? lastScale : nextScale;
71            lastScale = sl.scalingList[j];
72        }
73        return sl;
74    }
75
76    @Override
77    public String toString() {
78        return "ScalingList{" +
79                "scalingList=" + scalingList +
80                ", useDefaultScalingMatrixFlag=" + useDefaultScalingMatrixFlag +
81                '}';
82    }
83}