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