1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tv.tuner.data;
18 
19 import android.graphics.Color;
20 import android.support.annotation.NonNull;
21 
22 /** Collection of CEA-708 structures. */
23 public class Cea708Data {
24 
Cea708Data()25     private Cea708Data() {}
26 
27     // According to CEA-708B, the range of valid service number is between 1 and 63.
28     public static final int EMPTY_SERVICE_NUMBER = 0;
29 
30     // For the details of the ranges of DTVCC code groups, see CEA-708B Table 6.
31     public static final int CODE_C0_RANGE_START = 0x00;
32     public static final int CODE_C0_RANGE_END = 0x1f;
33     public static final int CODE_C1_RANGE_START = 0x80;
34     public static final int CODE_C1_RANGE_END = 0x9f;
35     public static final int CODE_G0_RANGE_START = 0x20;
36     public static final int CODE_G0_RANGE_END = 0x7f;
37     public static final int CODE_G1_RANGE_START = 0xa0;
38     public static final int CODE_G1_RANGE_END = 0xff;
39     public static final int CODE_C2_RANGE_START = 0x00;
40     public static final int CODE_C2_RANGE_END = 0x1f;
41     public static final int CODE_C3_RANGE_START = 0x80;
42     public static final int CODE_C3_RANGE_END = 0x9f;
43     public static final int CODE_G2_RANGE_START = 0x20;
44     public static final int CODE_G2_RANGE_END = 0x7f;
45     public static final int CODE_G3_RANGE_START = 0xa0;
46     public static final int CODE_G3_RANGE_END = 0xff;
47 
48     // The following ranges are defined in CEA-708B Section 7.4.1.
49     public static final int CODE_C0_SKIP2_RANGE_START = 0x18;
50     public static final int CODE_C0_SKIP2_RANGE_END = 0x1f;
51     public static final int CODE_C0_SKIP1_RANGE_START = 0x10;
52     public static final int CODE_C0_SKIP1_RANGE_END = 0x17;
53 
54     // The following ranges are defined in CEA-708B Section 7.4.7.
55     public static final int CODE_C2_SKIP0_RANGE_START = 0x00;
56     public static final int CODE_C2_SKIP0_RANGE_END = 0x07;
57     public static final int CODE_C2_SKIP1_RANGE_START = 0x08;
58     public static final int CODE_C2_SKIP1_RANGE_END = 0x0f;
59     public static final int CODE_C2_SKIP2_RANGE_START = 0x10;
60     public static final int CODE_C2_SKIP2_RANGE_END = 0x17;
61     public static final int CODE_C2_SKIP3_RANGE_START = 0x18;
62     public static final int CODE_C2_SKIP3_RANGE_END = 0x1f;
63 
64     // The following ranges are defined in CEA-708B Section 7.4.8.
65     public static final int CODE_C3_SKIP4_RANGE_START = 0x80;
66     public static final int CODE_C3_SKIP4_RANGE_END = 0x87;
67     public static final int CODE_C3_SKIP5_RANGE_START = 0x88;
68     public static final int CODE_C3_SKIP5_RANGE_END = 0x8f;
69 
70     // The following values are the special characters of CEA-708 spec.
71     public static final int CODE_C0_NUL = 0x00;
72     public static final int CODE_C0_ETX = 0x03;
73     public static final int CODE_C0_BS = 0x08;
74     public static final int CODE_C0_FF = 0x0c;
75     public static final int CODE_C0_CR = 0x0d;
76     public static final int CODE_C0_HCR = 0x0e;
77     public static final int CODE_C0_EXT1 = 0x10;
78     public static final int CODE_C0_P16 = 0x18;
79     public static final int CODE_G0_MUSICNOTE = 0x7f;
80     public static final int CODE_G2_TSP = 0x20;
81     public static final int CODE_G2_NBTSP = 0x21;
82     public static final int CODE_G2_BLK = 0x30;
83     public static final int CODE_G3_CC = 0xa0;
84 
85     // The following values are the command bits of CEA-708 spec.
86     public static final int CODE_C1_CW0 = 0x80;
87     public static final int CODE_C1_CW1 = 0x81;
88     public static final int CODE_C1_CW2 = 0x82;
89     public static final int CODE_C1_CW3 = 0x83;
90     public static final int CODE_C1_CW4 = 0x84;
91     public static final int CODE_C1_CW5 = 0x85;
92     public static final int CODE_C1_CW6 = 0x86;
93     public static final int CODE_C1_CW7 = 0x87;
94     public static final int CODE_C1_CLW = 0x88;
95     public static final int CODE_C1_DSW = 0x89;
96     public static final int CODE_C1_HDW = 0x8a;
97     public static final int CODE_C1_TGW = 0x8b;
98     public static final int CODE_C1_DLW = 0x8c;
99     public static final int CODE_C1_DLY = 0x8d;
100     public static final int CODE_C1_DLC = 0x8e;
101     public static final int CODE_C1_RST = 0x8f;
102     public static final int CODE_C1_SPA = 0x90;
103     public static final int CODE_C1_SPC = 0x91;
104     public static final int CODE_C1_SPL = 0x92;
105     public static final int CODE_C1_SWA = 0x97;
106     public static final int CODE_C1_DF0 = 0x98;
107     public static final int CODE_C1_DF1 = 0x99;
108     public static final int CODE_C1_DF2 = 0x9a;
109     public static final int CODE_C1_DF3 = 0x9b;
110     public static final int CODE_C1_DF4 = 0x9c;
111     public static final int CODE_C1_DF5 = 0x9d;
112     public static final int CODE_C1_DF6 = 0x9e;
113     public static final int CODE_C1_DF7 = 0x9f;
114 
115     public static class CcPacket implements Comparable<CcPacket> {
116         public final byte[] bytes;
117         public final int ccCount;
118         public final long pts;
119 
CcPacket(byte[] bytes, int ccCount, long pts)120         public CcPacket(byte[] bytes, int ccCount, long pts) {
121             this.bytes = bytes;
122             this.ccCount = ccCount;
123             this.pts = pts;
124         }
125 
126         @Override
compareTo(@onNull CcPacket another)127         public int compareTo(@NonNull CcPacket another) {
128             return Long.compare(pts, another.pts);
129         }
130     }
131 
132     /** CEA-708B-specific color. */
133     public static class CaptionColor {
134         public static final int OPACITY_SOLID = 0;
135         public static final int OPACITY_FLASH = 1;
136         public static final int OPACITY_TRANSLUCENT = 2;
137         public static final int OPACITY_TRANSPARENT = 3;
138 
139         private static final int[] COLOR_MAP = new int[] {0x00, 0x0f, 0xf0, 0xff};
140         private static final int[] OPACITY_MAP = new int[] {0xff, 0xfe, 0x80, 0x00};
141 
142         public final int opacity;
143         public final int red;
144         public final int green;
145         public final int blue;
146 
CaptionColor(int opacity, int red, int green, int blue)147         public CaptionColor(int opacity, int red, int green, int blue) {
148             this.opacity = opacity;
149             this.red = red;
150             this.green = green;
151             this.blue = blue;
152         }
153 
getArgbValue()154         public int getArgbValue() {
155             return Color.argb(
156                     OPACITY_MAP[opacity], COLOR_MAP[red], COLOR_MAP[green], COLOR_MAP[blue]);
157         }
158     }
159 
160     /** Caption event generated by {@link Cea708Parser}. */
161     public static class CaptionEvent {
162         @Cea708Parser.CaptionEmitType public final int type;
163         public final Object obj;
164 
CaptionEvent(int type, Object obj)165         public CaptionEvent(int type, Object obj) {
166             this.type = type;
167             this.obj = obj;
168         }
169     }
170 
171     /** Pen style information. */
172     public static class CaptionPenAttr {
173         // Pen sizes
174         public static final int PEN_SIZE_SMALL = 0;
175         public static final int PEN_SIZE_STANDARD = 1;
176         public static final int PEN_SIZE_LARGE = 2;
177 
178         // Offsets
179         public static final int OFFSET_SUBSCRIPT = 0;
180         public static final int OFFSET_NORMAL = 1;
181         public static final int OFFSET_SUPERSCRIPT = 2;
182 
183         public final int penSize;
184         public final int penOffset;
185         public final int textTag;
186         public final int fontTag;
187         public final int edgeType;
188         public final boolean underline;
189         public final boolean italic;
190 
CaptionPenAttr( int penSize, int penOffset, int textTag, int fontTag, int edgeType, boolean underline, boolean italic)191         public CaptionPenAttr(
192                 int penSize,
193                 int penOffset,
194                 int textTag,
195                 int fontTag,
196                 int edgeType,
197                 boolean underline,
198                 boolean italic) {
199             this.penSize = penSize;
200             this.penOffset = penOffset;
201             this.textTag = textTag;
202             this.fontTag = fontTag;
203             this.edgeType = edgeType;
204             this.underline = underline;
205             this.italic = italic;
206         }
207     }
208 
209     /**
210      * {@link CaptionColor} objects that indicate the foreground, background, and edge color of a
211      * pen.
212      */
213     public static class CaptionPenColor {
214         public final CaptionColor foregroundColor;
215         public final CaptionColor backgroundColor;
216         public final CaptionColor edgeColor;
217 
CaptionPenColor( CaptionColor foregroundColor, CaptionColor backgroundColor, CaptionColor edgeColor)218         public CaptionPenColor(
219                 CaptionColor foregroundColor,
220                 CaptionColor backgroundColor,
221                 CaptionColor edgeColor) {
222             this.foregroundColor = foregroundColor;
223             this.backgroundColor = backgroundColor;
224             this.edgeColor = edgeColor;
225         }
226     }
227 
228     /** Location information of a pen. */
229     public static class CaptionPenLocation {
230         public final int row;
231         public final int column;
232 
CaptionPenLocation(int row, int column)233         public CaptionPenLocation(int row, int column) {
234             this.row = row;
235             this.column = column;
236         }
237     }
238 
239     /** Attributes of a caption window, which is defined in CEA-708B. */
240     public static class CaptionWindowAttr {
241         public static final int JUSTIFY_LEFT = 0;
242         public static final int JUSTIFY_CENTER = 2;
243         public static final int PRINT_LEFT_TO_RIGHT = 0;
244         public static final int PRINT_RIGHT_TO_LEFT = 1;
245         public static final int PRINT_TOP_TO_BOTTOM = 2;
246         public static final int PRINT_BOTTOM_TO_TOP = 3;
247 
248         public final CaptionColor fillColor;
249         public final CaptionColor borderColor;
250         public final int borderType;
251         public final boolean wordWrap;
252         public final int printDirection;
253         public final int scrollDirection;
254         public final int justify;
255         public final int effectDirection;
256         public final int effectSpeed;
257         public final int displayEffect;
258 
CaptionWindowAttr( CaptionColor fillColor, CaptionColor borderColor, int borderType, boolean wordWrap, int printDirection, int scrollDirection, int justify, int effectDirection, int effectSpeed, int displayEffect)259         public CaptionWindowAttr(
260                 CaptionColor fillColor,
261                 CaptionColor borderColor,
262                 int borderType,
263                 boolean wordWrap,
264                 int printDirection,
265                 int scrollDirection,
266                 int justify,
267                 int effectDirection,
268                 int effectSpeed,
269                 int displayEffect) {
270             this.fillColor = fillColor;
271             this.borderColor = borderColor;
272             this.borderType = borderType;
273             this.wordWrap = wordWrap;
274             this.printDirection = printDirection;
275             this.scrollDirection = scrollDirection;
276             this.justify = justify;
277             this.effectDirection = effectDirection;
278             this.effectSpeed = effectSpeed;
279             this.displayEffect = displayEffect;
280         }
281     }
282 
283     /** Construction information of the caption window of CEA-708B. */
284     public static class CaptionWindow {
285         public final int id;
286         public final boolean visible;
287         public final boolean rowLock;
288         public final boolean columnLock;
289         public final int priority;
290         public final boolean relativePositioning;
291         public final int anchorVertical;
292         public final int anchorHorizontal;
293         public final int anchorId;
294         public final int rowCount;
295         public final int columnCount;
296         public final int penStyle;
297         public final int windowStyle;
298 
CaptionWindow( int id, boolean visible, boolean rowLock, boolean columnLock, int priority, boolean relativePositioning, int anchorVertical, int anchorHorizontal, int anchorId, int rowCount, int columnCount, int penStyle, int windowStyle)299         public CaptionWindow(
300                 int id,
301                 boolean visible,
302                 boolean rowLock,
303                 boolean columnLock,
304                 int priority,
305                 boolean relativePositioning,
306                 int anchorVertical,
307                 int anchorHorizontal,
308                 int anchorId,
309                 int rowCount,
310                 int columnCount,
311                 int penStyle,
312                 int windowStyle) {
313             this.id = id;
314             this.visible = visible;
315             this.rowLock = rowLock;
316             this.columnLock = columnLock;
317             this.priority = priority;
318             this.relativePositioning = relativePositioning;
319             this.anchorVertical = anchorVertical;
320             this.anchorHorizontal = anchorHorizontal;
321             this.anchorId = anchorId;
322             this.rowCount = rowCount;
323             this.columnCount = columnCount;
324             this.penStyle = penStyle;
325             this.windowStyle = windowStyle;
326         }
327     }
328 }
329