1 package org
.diracvideo
.Schroedinger
;
3 public class VideoFormat
{
5 public int width
, height
;
6 public int chroma_format
;
8 public int version_major
, version_minor
, profile
, level
;
9 public boolean interlaced
, top_field_first
;
10 public int frame_rate_numerator
, frame_rate_denominator
,
11 aspect_ratio_numerator
, aspect_ratio_denominator
;
12 public int clean_width
, clean_height
, left_offset
, top_offset
;
13 public int luma_offset
, luma_excursion
, chroma_offset
, chroma_excursion
;
14 public int transfer_function
, colour_primaries
, colour_matrix
;
15 public int interlaced_coding
;
16 public ColourSpace colour
;
18 private static int[][] defaultFormats
= {
19 { 0, 640, 480, 0420, 0, 0, 24000, 1001, /*custom */
20 1, 1, 640, 480, 0, 0, 0, 255, 128, 255, 0, 0, 0 },
21 { 1, 176, 120, 0420,0,0, 15000, 1001, 10, /* QSIF525 */
22 11, 176, 120, 0, 0, 0, 255, 128, 255, 1, 1, 0 },
23 { 2, 176, 144, 0420, 0, 1, 25, 2, 12, 11, /* QCIF */
24 176, 144, 0, 0, 0, 255, 128, 255, 2, 1, 0 },
25 { 3, 352, 240, 0420, 0, 0, 15000, 1001, 10, /* SIF525 */
26 11, 352, 240, 0, 0, 0, 255, 128, 255, 1, 1, 0 },
27 { 4, 352, 288, 0420, 0, 1, 25, 2, 12, 11, /* CIF */
28 352, 288, 0, 0, 0, 255, 128, 255, 2, 1, 0 },
29 { 5, 704, 480, 0420, 0, 0, 15000, 1001, 10, /* 4SIF525 */
30 11, 704, 480, 0, 0, 0, 255, 128, 255, 1, 1, 0 },
31 { 6, 704, 576, 0420, 0, 1, 25, 2, 12, 11, /* 4CIF */
32 704, 576, 0, 0, 0, 255, 128, 255, 2, 1, 0 },
33 { 7, 720, 480, 0422, 1, 0, 30000, 1001, 10, 11, /* SD480I-60 */
34 704, 480, 8, 0, 64, 876, 512, 896, 1, 1, 0 },
35 { 8, 720, 576, 0422, 1, 1, 25, 1, 12, 11, 704, 576, /* SD576I-50 */
36 8, 0, 64, 876, 512, 896, 2, 1, 0 },
37 { 9, 1280, 720, 0422, 0, 1, 60000, 1001, 1, 1, /* HD720P-60 */
38 1280, 720, 0, 0, 64, 876, 512, 896, 0, 0, 0 },
39 { 10, 1280, 720, 0422, 0, 1, 50, 1, 1, 1, /* HD720P-50 */
40 1280, 720, 0, 0, 64, 876, 512, 896, 0, 0, 0 },
41 { 11, 1920, 1080, 0422, 1, 1,30000, 1001, 1, 1, /* HD1080I-60 */
42 1920, 1080, 0, 0, 64, 876, 512, 896, 0, 0, 0 },
43 { 12, 1920, 1080, 0422, 1, 1, 25, 1, 1, 1, /* HD1080I-50 */
44 1920, 1080, 0, 0, 64, 876, 512, 896, 0, 0, 0 },
45 { 13, 1920, 1080, 0422, 0, 1, 60000, 1001, 1, 1, /* HD1080P-60 */
46 1920, 1080, 0, 0, 64, 876, 512, 896, 0, 0, 0 },
47 { 14, 1920, 1080, 0422, 0, 1, 50, 1, 1, 1, /* HD1080P-50 */
48 1920, 1080, 0, 0, 64, 876, 512, 896, 0, 0, 0 },
49 { 15, 2048, 1080, 0444, 0, 1, 24, 1, 1, 1, /* DC2K */
50 2048, 1080, 0, 0, 256, 3504, 2048, 3584, 3, 0, 0 },
51 { 16, 4096, 2160, 0444, 0, 1, 24, 1, 1, 1, /* DC4K */
52 2048, 1536, 0, 0, 256, 3504, 2048, 3584, 3, 0, 0 }
55 private static int [][] defaultFrameRates
= {
56 { 0, 0 }, { 24000, 1001 },
58 { 30000, 1001 }, { 30, 1 },
59 { 50, 1 }, { 60000, 1001 },
60 { 60, 1 }, { 15000, 1001 }, { 25, 2 }
63 private static int [][] defaultAspectRatios
= {
65 { 10, 11 }, { 12, 11 },
66 { 40, 33 }, { 16, 11 }, { 4, 3 }
69 private static int [][] defaultSignalRanges
= {
72 { 16, 219, 128, 224 },
73 { 64, 876, 512, 896, },
74 { 256, 3504, 2048, 3584 }
77 private void setDefaultVideoFormat(int i
) throws Exception
{
78 if (i
>= VideoFormat
.defaultFormats
.length
) {
79 throw new Exception("Unsupported Video Format");
81 int a
[] = VideoFormat
.defaultFormats
[i
];
85 this.chroma_format
= a
[3];
86 this.interlaced
= ( a
[4] == 0 ?
false : true);
87 this.top_field_first
= (a
[5] == 0 ?
false : true);
88 this.frame_rate_numerator
= a
[6];
89 this.frame_rate_denominator
= a
[7];
90 this.aspect_ratio_numerator
= a
[8];
91 this.aspect_ratio_denominator
= a
[9];
92 this.clean_width
= a
[10];
93 this.clean_height
= a
[11];
94 this.left_offset
= a
[12];
95 this.top_offset
= a
[13];
96 this.luma_offset
= a
[14];
97 this.luma_excursion
= a
[15];
98 this.chroma_offset
= a
[16];
99 this.chroma_offset
= a
[17];
102 private void setDefaultFrameRate(int i
) throws Exception
{
103 if(i
>= VideoFormat
.defaultFrameRates
.length
) {
104 throw new Exception("Unsuported Frame Rate");
106 int a
[] = VideoFormat
.defaultFrameRates
[i
];
107 this.frame_rate_numerator
= a
[0];
108 this.frame_rate_denominator
= a
[1];
111 private void setDefaultAspectRatio(int i
) throws Exception
{
112 if(i
>= VideoFormat
.defaultAspectRatios
.length
) {
113 throw new Exception("Unsupported Aspect Ratio");
115 int a
[] = VideoFormat
.defaultAspectRatios
[i
];
116 this.aspect_ratio_numerator
= a
[0];
117 this.aspect_ratio_denominator
= a
[1];
120 private void setDefaultSignalRange(int i
) throws Exception
{
121 if (i
>= VideoFormat
.defaultSignalRanges
.length
) {
122 throw new Exception("Unsupported Signal Range");
124 int a
[] = VideoFormat
.defaultSignalRanges
[i
];
125 this.luma_offset
= a
[0];
126 this.luma_excursion
= a
[1];
127 this.chroma_offset
= a
[2];
128 this.chroma_excursion
= a
[3];
131 private void setDefaultColourSpec(int i
) {
132 colour
= new ColourSpace(i
, this);
136 * @param b Buffer of a video format as packed in
137 * the dirac stream */
139 public VideoFormat(Buffer b
) throws Exception
{
140 Unpack u
= new Unpack(b
);
141 u
.skip(104); /* 13 * 8 */
142 version_major
= u
.decodeUint();
143 version_minor
= u
.decodeUint();
144 profile
= u
.decodeUint();
145 level
= u
.decodeUint();
147 setDefaultVideoFormat(u
.decodeUint());
148 if(u
.decodeBool()) { /* frame dimensions */
149 this.width
= u
.decodeUint();
150 this.height
= u
.decodeUint();
152 if(u
.decodeBool()) { /* chroma format */
153 chroma_format
= u
.decodeUint();
156 if(u
.decodeBool()) { /* scan format */
157 this.interlaced
= u
.decodeBool();
158 if(this.interlaced
) {
159 this.top_field_first
= u
.decodeBool();
163 if(u
.decodeBool()) { /* frame rate */
164 int i
= u
.decodeUint();
166 this.frame_rate_numerator
= u
.decodeUint();
167 this.frame_rate_denominator
= u
.decodeUint();
169 setDefaultFrameRate(i
);
173 if(u
.decodeBool()) { /* aspect ratio */
174 int i
= u
.decodeUint();
176 this.aspect_ratio_numerator
= u
.decodeUint();
177 this.aspect_ratio_denominator
= u
.decodeUint();
179 setDefaultAspectRatio(i
);
183 if(u
.decodeBool()) { /* clean area */
184 this.clean_width
= u
.decodeUint();
185 this.clean_height
= u
.decodeUint();
186 this.left_offset
= u
.decodeUint();
187 this.top_offset
= u
.decodeUint();
190 if(u
.decodeBool()) { /* signal range */
191 int i
= u
.decodeUint();
193 this.luma_offset
= u
.decodeUint();
194 this.luma_excursion
= u
.decodeUint();
195 this.chroma_offset
= u
.decodeUint();
196 this.chroma_excursion
= u
.decodeUint();
198 setDefaultSignalRange(i
);
202 if(u
.decodeBool()) { /* colour spec */
203 int i
= u
.decodeUint();
204 setDefaultColourSpec(i
);
207 colour_primaries
= u
.decodeUint();
210 colour_matrix
= u
.decodeUint();
213 transfer_function
= u
.decodeUint();
217 colour
= new ColourSpace(0,this);
219 interlaced_coding
= u
.decodeUint();
223 private void validate() {
224 if(0 == this.aspect_ratio_numerator
) {
225 this.aspect_ratio_numerator
= 1;
227 if(0 == this.aspect_ratio_denominator
) {
228 this.aspect_ratio_denominator
= 1;
230 int max
= java
.lang
.Math
.max(this.chroma_excursion
,
231 this.luma_excursion
);
232 if(max
> 255 || max
< 128) {
233 /* well, now what? */
238 public boolean equals(VideoFormat o
) {
239 boolean diff
= false;
240 diff
= diff
|| (o
.index
!= this.index
);
241 diff
= diff
|| (o
.width
!= this.width
);
242 diff
= diff
|| (o
.height
!= this.height
);
243 diff
= diff
|| (o
.chroma_format
!= this.chroma_format
);
244 diff
= diff
|| (o
.interlaced
!= this.interlaced
);
245 diff
= diff
|| (o
.top_field_first
!= this.top_field_first
);
246 diff
= diff
|| (o
.frame_rate_numerator
!= this.frame_rate_numerator
);
247 diff
= diff
|| (o
.frame_rate_denominator
!= this.frame_rate_denominator
);
248 diff
= diff
|| (o
.aspect_ratio_numerator
!= this.aspect_ratio_numerator
);
249 diff
= diff
|| (o
.clean_width
!= this.clean_width
);
250 diff
= diff
|| (o
.clean_height
!= this.clean_height
);
251 diff
= diff
|| (o
.luma_offset
!= this.luma_offset
);
252 diff
= diff
|| (o
.luma_excursion
!= this.luma_excursion
);
253 diff
= diff
|| (o
.chroma_offset
!= this.chroma_offset
);
254 diff
= diff
|| (o
.chroma_excursion
!= this.chroma_excursion
);
255 diff
= diff
|| (o
.interlaced_coding
!= this.interlaced_coding
);
256 return diff
== false;
259 public void getPictureLumaSize(int[] out
) {
261 out
[1] = Util
.roundUpShift(this.height
, this.interlaced_coding
);
264 public void getPictureChromaSize(int[] out
) {
265 out
[0] = Util
.roundUpShift(this.width
, chromaHShift());
266 out
[1] = Util
.roundUpShift(this.height
,chromaVShift());
269 public int chromaHShift() {
270 return (chroma_format
> 0 ?
1 : 0);
273 public int chromaVShift() {
274 return (chroma_format
> 1 ?
1 : 0) + interlaced_coding
;