1 /*****************************************************************************
2  * desc_46.h: ETSI EN 300 468 Descriptor 0x46: VBI teletext descriptor
3  *****************************************************************************
4  * Copyright (C) 2009-2010 VideoLAN
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *          Georgi Chorbadzhiyski <georgi@unixsol.org>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject
15  * to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *****************************************************************************/
28 
29 /*
30  * Normative references:
31  *  - ETSI EN 300 468 V1.11.1 (2010-04) (SI in DVB systems)
32  */
33 
34 #ifndef __BITSTREAM_DVB_DESC_46_H__
35 #define __BITSTREAM_DVB_DESC_46_H__
36 
37 #include <bitstream/common.h>
38 #include <bitstream/mpeg/psi/descriptors.h>
39 #include <bitstream/mpeg/psi/desc_0a.h>
40 
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45 
46 /*****************************************************************************
47  * Descriptor 0x46: VBI teletext descriptor
48  *****************************************************************************/
49 #define DESC46_HEADER_SIZE      DESC_HEADER_SIZE
50 #define DESC46_LANGUAGE_SIZE    5
51 
52 #define DESC46_TELETEXTTYPE_INITIAL     1
53 #define DESC46_TELETEXTTYPE_SUBTITLE    2
54 #define DESC46_TELETEXTTYPE_INFORMATION 3
55 #define DESC46_TELETEXTTYPE_SCHEDULE    4
56 #define DESC46_TELETEXTTYPE_SUBTITLE_H  5
57 
desc46_init(uint8_t * p_desc)58 static inline void desc46_init(uint8_t *p_desc)
59 {
60     desc_set_tag(p_desc, 0x46);
61 }
62 
desc46_get_language(uint8_t * p_desc,uint8_t n)63 static inline uint8_t *desc46_get_language(uint8_t *p_desc, uint8_t n)
64 {
65     uint8_t *p_desc_n = p_desc + DESC46_HEADER_SIZE + n * DESC46_LANGUAGE_SIZE;
66     if (p_desc_n + DESC46_LANGUAGE_SIZE - p_desc
67          > desc_get_length(p_desc) + DESC46_HEADER_SIZE)
68         return NULL;
69     return p_desc_n;
70 }
71 
72 #define desc46n_set_code desc0an_set_code
73 #define desc46n_get_code desc0an_get_code
74 
desc46n_set_teletexttype(uint8_t * p_desc_n,uint8_t i_type)75 static inline void desc46n_set_teletexttype(uint8_t *p_desc_n, uint8_t i_type)
76 {
77     p_desc_n[3] &= ~0xf8;
78     p_desc_n[3] |= (i_type << 3) & 0xf8;
79 }
80 
desc46n_get_teletexttype(const uint8_t * p_desc_n)81 static inline uint8_t desc46n_get_teletexttype(const uint8_t *p_desc_n)
82 {
83     return p_desc_n[3] >> 3;
84 }
85 
desc46_get_teletexttype_txt(uint8_t i_type)86 static inline const char *desc46_get_teletexttype_txt(uint8_t i_type)
87 {
88     return i_type == 0x00 ? "Reserved" :
89            i_type == 0x01 ? "Initial teletext page" :
90            i_type == 0x02 ? "Teletext subtitle page" :
91            i_type == 0x03 ? "Additional information page" :
92            i_type == 0x04 ? "Programme schedule page" :
93            i_type == 0x05 ? "Teletext subtitle page for hearing impaired people" : "Reserved";
94 }
95 
desc46n_set_teletextmagazine(uint8_t * p_desc_n,uint8_t i_magazine)96 static inline void desc46n_set_teletextmagazine(uint8_t *p_desc_n,
97                                                 uint8_t i_magazine)
98 {
99     p_desc_n[3] &= ~0x7;
100     p_desc_n[3] |= (i_magazine & 0x7);
101 }
102 
desc46n_get_teletextmagazine(const uint8_t * p_desc_n)103 static inline uint8_t desc46n_get_teletextmagazine(const uint8_t *p_desc_n)
104 {
105     return p_desc_n[3] & 0x7;
106 }
107 
desc46n_set_teletextpage(uint8_t * p_desc_n,uint8_t i_page)108 static inline void desc46n_set_teletextpage(uint8_t *p_desc_n, uint8_t i_page)
109 {
110     p_desc_n[4] = i_page;
111 }
112 
desc46n_get_teletextpage(const uint8_t * p_desc_n)113 static inline uint8_t desc46n_get_teletextpage(const uint8_t *p_desc_n)
114 {
115     return p_desc_n[4];
116 }
117 
desc46_validate(const uint8_t * p_desc)118 static inline bool desc46_validate(const uint8_t *p_desc)
119 {
120     return !(desc_get_length(p_desc) % DESC46_LANGUAGE_SIZE);
121 }
122 
desc46_print(uint8_t * p_desc,f_print pf_print,void * opaque,print_type_t i_print_type)123 static inline void desc46_print(uint8_t *p_desc, f_print pf_print,
124                                 void *opaque, print_type_t i_print_type)
125 {
126     uint8_t j = 0;
127     uint8_t *p_desc_n;
128 
129     while ((p_desc_n = desc46_get_language(p_desc, j)) != NULL) {
130         j++;
131         switch (i_print_type) {
132         case PRINT_XML:
133             pf_print(opaque,
134                  "<%s language=\"%3.3s\" type=\"0x%hhx\" type_txt=\"%s\" mag=\"%hhu\" page=\"0x%hhx\"/>",
135                  desc_get_tag(p_desc) == 0x46 ? "VBI_TELX_DESC" : "TELX_DESC",
136                  (const char *)desc46n_get_code(p_desc_n),
137                  desc46n_get_teletexttype(p_desc_n),
138                  desc46_get_teletexttype_txt(desc46n_get_teletexttype(p_desc_n)),
139                  desc46n_get_teletextmagazine(p_desc_n),
140                  desc46n_get_teletextpage(p_desc_n));
141             break;
142         default:
143             pf_print(opaque,
144                  "    - desc %x %s language=%3.3s type=0x%hhx type_txt=\"%s\" mag=%hhu page=0x%hhx",
145                  desc_get_tag(p_desc),
146                  desc_get_tag(p_desc) == 0x46 ? "vbi_telx" : "telx",
147                  (const char *)desc46n_get_code(p_desc_n),
148                  desc46n_get_teletexttype(p_desc_n),
149                  desc46_get_teletexttype_txt(desc46n_get_teletexttype(p_desc_n)),
150                  desc46n_get_teletextmagazine(p_desc_n),
151                  desc46n_get_teletextpage(p_desc_n));
152         }
153     }
154 }
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif
161