1 /*****************************************************************************
2  * desc_02.h: ISO/IEC 13818-1 Descriptor 0x02 (Video stream descriptor)
3  *****************************************************************************
4  * Copyright (C) 2011 Unix Solutions Ltd.
5  *
6  * Authors: Georgi Chorbadzhiyski <georgi@unixsol.org>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject
14  * to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *****************************************************************************/
27 
28 /*
29  * Normative references:
30  *  - ISO/IEC 13818-1:2007(E) (MPEG-2 Systems)
31  */
32 
33 #ifndef __BITSTREAM_MPEG_DESC_02_H__
34 #define __BITSTREAM_MPEG_DESC_02_H__
35 
36 #include <bitstream/common.h>
37 #include <bitstream/mpeg/psi/descriptors.h>
38 
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43 
44 /*****************************************************************************
45  * Descriptor 0x02: Video stream descriptor
46  *****************************************************************************/
47 #define DESC02_HEADER_SIZE      (DESC_HEADER_SIZE + 1)
48 
desc02_init(uint8_t * p_desc)49 static inline void desc02_init(uint8_t *p_desc)
50 {
51     desc_set_tag(p_desc, 0x02);
52     desc_set_length(p_desc, DESC02_HEADER_SIZE - DESC_HEADER_SIZE);
53 }
54 
desc02_get_multiple_frame_rate(const uint8_t * p_desc)55 static inline bool desc02_get_multiple_frame_rate(const uint8_t *p_desc) {
56     return (p_desc[2] & 0x80) == 0x80;
57 }
58 
desc02_set_multiple_frame_rate(uint8_t * p_desc,bool b_multiple_frame_rate)59 static inline void desc02_set_multiple_frame_rate(uint8_t *p_desc, bool b_multiple_frame_rate) {
60     p_desc[2] = b_multiple_frame_rate ? (p_desc[2] | 0x80) : (p_desc[2] &~ 0x80);
61 }
62 
desc02_get_frame_rate_code(const uint8_t * p_desc)63 static inline uint8_t desc02_get_frame_rate_code(const uint8_t *p_desc) {
64     return (p_desc[2] & 0x78) >> 3; /* 1xxxx111 */
65 }
66 
desc02_set_frame_rate_code(uint8_t * p_desc,uint8_t i_frame_rate_code)67 static inline void desc02_set_frame_rate_code(uint8_t *p_desc, uint8_t i_frame_rate_code) {
68     p_desc[2] = (p_desc[2] & 0x87) | (i_frame_rate_code & 0x0f) << 3;
69 }
70 
desc02_get_frame_rate_txt(const uint8_t frame_rate_code)71 static inline const char *desc02_get_frame_rate_txt(const uint8_t frame_rate_code) {
72     return frame_rate_code == 0 ? "forbidden" :
73            frame_rate_code == 1 ? "23.976" :
74            frame_rate_code == 2 ? "24.00" :
75            frame_rate_code == 3 ? "25.00" :
76            frame_rate_code == 4 ? "29.97" :
77            frame_rate_code == 5 ? "30.00" :
78            frame_rate_code == 6 ? "50.00" :
79            frame_rate_code == 7 ? "59.94" :
80            frame_rate_code == 8 ? "60.00" : "reserved";
81 }
82 
desc02_get_mpeg1_only_flag(const uint8_t * p_desc)83 static inline bool desc02_get_mpeg1_only_flag(const uint8_t *p_desc) {
84     return (p_desc[2] & 0x04) == 0x04;
85 }
86 
desc02_set_mpeg1_only_flag(uint8_t * p_desc,bool b_mpeg1_only_flag)87 static inline void desc02_set_mpeg1_only_flag(uint8_t *p_desc, bool b_mpeg1_only_flag) {
88     p_desc[2] = b_mpeg1_only_flag ? (p_desc[2] | 0x04) : (p_desc[2] &~ 0x04);
89     if (b_mpeg1_only_flag)
90         desc_set_length(p_desc, DESC02_HEADER_SIZE - DESC_HEADER_SIZE);
91     else
92         desc_set_length(p_desc, (DESC02_HEADER_SIZE + 2) - DESC_HEADER_SIZE);
93 }
94 
desc02_get_constraint_parameter(const uint8_t * p_desc)95 static inline bool desc02_get_constraint_parameter(const uint8_t *p_desc) {
96     return (p_desc[2] & 0x02) == 0x02;
97 }
98 
desc02_set_constraint_parameter(uint8_t * p_desc,bool b_constraint_parameter)99 static inline void desc02_set_constraint_parameter(uint8_t *p_desc, bool b_constraint_parameter) {
100     p_desc[2] = b_constraint_parameter ? (p_desc[2] | 0x02) : (p_desc[2] &~ 0x02);
101 }
102 
103 
desc02_get_still_picture(const uint8_t * p_desc)104 static inline bool desc02_get_still_picture(const uint8_t *p_desc) {
105     return (p_desc[2] & 0x01) == 0x01;
106 }
107 
desc02_set_still_picture(uint8_t * p_desc,bool b_still_picture)108 static inline void desc02_set_still_picture(uint8_t *p_desc, bool b_still_picture) {
109     p_desc[2] = b_still_picture ? (p_desc[2] | 0x01) : (p_desc[2] &~ 0x01);
110 }
111 
desc02_get_profile(const uint8_t * p_desc)112 static inline uint8_t desc02_get_profile(const uint8_t *p_desc) {
113    return p_desc[3] >> 4;
114 }
115 
desc02_set_profile(uint8_t * p_desc,uint8_t i_profile)116 static inline void desc02_set_profile(uint8_t *p_desc, uint8_t i_profile) {
117    p_desc[3] = ((i_profile & 0x0f) << 4) | (p_desc[3] & 0x0f);
118 }
119 
desc02_get_profile_txt(const uint8_t profile)120 static inline const char *desc02_get_profile_txt(const uint8_t profile) {
121     return profile == 1 ? "High"               :
122            profile == 2 ? "Spatially Scalable" :
123            profile == 3 ? "SNR Scalable"       :
124            profile == 4 ? "Main"               :
125            profile == 5 ? "Simple"             : "Reserved";
126 }
127 
desc02_get_level(const uint8_t * p_desc)128 static inline uint8_t desc02_get_level(const uint8_t *p_desc) {
129    return p_desc[3] & 0x0f;
130 }
131 
desc02_set_level(uint8_t * p_desc,uint8_t i_level)132 static inline void desc02_set_level(uint8_t *p_desc, uint8_t i_level) {
133    p_desc[3] = (p_desc[3] & 0xf0) | (i_level & 0x0f);
134 }
135 
desc02_get_level_txt(const uint8_t level)136 static inline const char *desc02_get_level_txt(const uint8_t level) {
137     return level == 4  ? "High"      :
138            level == 6  ? "High 1440" :
139            level == 8  ? "Main"      :
140            level == 10 ? "Low"       : "Reserved";
141 }
142 
desc02_get_chroma_format(const uint8_t * p_desc)143 static inline uint8_t desc02_get_chroma_format(const uint8_t *p_desc) {
144    return p_desc[4] >> 6;
145 }
146 
desc02_set_chroma_format(uint8_t * p_desc,uint8_t i_chroma_format)147 static inline void desc02_set_chroma_format(uint8_t *p_desc, uint8_t i_chroma_format) {
148    p_desc[4] = (i_chroma_format << 6) | (p_desc[4] & 0x20) | 0x1f;
149 }
150 
desc02_get_chroma_format_txt(const uint8_t chroma_format)151 static inline const char *desc02_get_chroma_format_txt(const uint8_t chroma_format) {
152     return chroma_format == 0 ? "reserved" :
153            chroma_format == 1 ? "4:2:0" :
154            chroma_format == 2 ? "4:2:2" :
155            chroma_format == 3 ? "4:4:4" : "unknown";
156 }
157 
desc02_get_frame_rate_extension(const uint8_t * p_desc)158 static inline bool desc02_get_frame_rate_extension(const uint8_t *p_desc) {
159     return (p_desc[4] & 0x20) == 0x20;
160 }
161 
desc02_set_frame_rate_extension(uint8_t * p_desc,bool b_frame_rate_extension)162 static inline void desc02_set_frame_rate_extension(uint8_t *p_desc, bool b_frame_rate_extension) {
163     p_desc[4] = (b_frame_rate_extension ? (p_desc[4] | 0x20) : (p_desc[4] &~ 0x20)) | 0x1f;
164 }
165 
desc02_validate(const uint8_t * p_desc)166 static inline bool desc02_validate(const uint8_t *p_desc)
167 {
168     if (desc02_get_mpeg1_only_flag(p_desc))
169         return desc_get_length(p_desc) >= DESC02_HEADER_SIZE - DESC_HEADER_SIZE;
170     else
171         return desc_get_length(p_desc) >= (DESC02_HEADER_SIZE + 2) - DESC_HEADER_SIZE;
172 }
173 
desc02_print(const uint8_t * p_desc,f_print pf_print,void * opaque,print_type_t i_print_type)174 static inline void desc02_print(const uint8_t *p_desc, f_print pf_print,
175                                 void *opaque, print_type_t i_print_type)
176 {
177     switch (i_print_type) {
178     case PRINT_XML:
179         if (desc02_get_mpeg1_only_flag(p_desc)) {
180             pf_print(opaque,
181                 "<VIDEO_STREAM_DESC multiple_frame_rate=\"%u\" frame_rate_code=\"%u\" frame_rate_txt=\"%s\""
182                 " mpeg1_only=\"%u\" constraint_parameter=\"%u\" still_picture=\"%u\"/>",
183                      desc02_get_multiple_frame_rate(p_desc),
184                      desc02_get_frame_rate_code(p_desc),
185                      desc02_get_frame_rate_txt(desc02_get_frame_rate_code(p_desc)),
186                      desc02_get_mpeg1_only_flag(p_desc),
187                      desc02_get_constraint_parameter(p_desc),
188                      desc02_get_still_picture(p_desc)
189             );
190         } else {
191             pf_print(opaque,
192                 "<VIDEO_STREAM_DESC multiple_frame_rate=\"%u\" frame_rate_code=\"%u\" frame_rate_txt=\"%s\""
193                 " mpeg1_only=\"%u\" constraint_parameter=\"%u\" still_picture=\"%u\""
194                 " profile=\"%u\" profile_txt=\"%s\" level=\"%u\" level_txt=\"%s\""
195                 " chroma_format=\"%u\" chroma_format_txt=\"%s\""
196                 " frame_rate_extension=\"%u\"/>",
197                      desc02_get_multiple_frame_rate(p_desc),
198                      desc02_get_frame_rate_code(p_desc),
199                      desc02_get_frame_rate_txt(desc02_get_frame_rate_code(p_desc)),
200                      desc02_get_mpeg1_only_flag(p_desc),
201                      desc02_get_constraint_parameter(p_desc),
202                      desc02_get_still_picture(p_desc),
203                      desc02_get_profile(p_desc),
204                      desc02_get_profile_txt(desc02_get_profile(p_desc)),
205                      desc02_get_level(p_desc),
206                      desc02_get_level_txt(desc02_get_level(p_desc)),
207                      desc02_get_chroma_format(p_desc),
208                      desc02_get_chroma_format_txt(desc02_get_chroma_format(p_desc)),
209                      desc02_get_frame_rate_extension(p_desc)
210             );
211         }
212         break;
213     default:
214         if (desc02_get_mpeg1_only_flag(p_desc)) {
215             pf_print(opaque,
216                 "    - desc 02 video_stream multiple_frame_rate=%u frame_rate_code=%u frame_rate_txt=\"%s\""
217                 " mpeg1_only=%u constraint_parameter=%u still_picture=%u",
218                      desc02_get_multiple_frame_rate(p_desc),
219                      desc02_get_frame_rate_code(p_desc),
220                      desc02_get_frame_rate_txt(desc02_get_frame_rate_code(p_desc)),
221                      desc02_get_mpeg1_only_flag(p_desc),
222                      desc02_get_constraint_parameter(p_desc),
223                      desc02_get_still_picture(p_desc)
224             );
225         } else {
226             pf_print(opaque,
227                 "    - desc 02 video_stream multiple_frame_rate=%u frame_rate_code=%u frame_rate_txt=\"%s\""
228                 " mpeg1_only=%u constraint_parameter=%u still_picture=%u"
229                 " profile=%u profile_txt=\"%s\" level=%u level_txt=\"%s\""
230                 " chroma_format=%u chroma_format_txt=\"%s\""
231                 " frame_rate_extension=%u",
232                      desc02_get_multiple_frame_rate(p_desc),
233                      desc02_get_frame_rate_code(p_desc),
234                      desc02_get_frame_rate_txt(desc02_get_frame_rate_code(p_desc)),
235                      desc02_get_mpeg1_only_flag(p_desc),
236                      desc02_get_constraint_parameter(p_desc),
237                      desc02_get_still_picture(p_desc),
238                      desc02_get_profile(p_desc),
239                      desc02_get_profile_txt(desc02_get_profile(p_desc)),
240                      desc02_get_level(p_desc),
241                      desc02_get_level_txt(desc02_get_level(p_desc)),
242                      desc02_get_chroma_format(p_desc),
243                      desc02_get_chroma_format_txt(desc02_get_chroma_format(p_desc)),
244                      desc02_get_frame_rate_extension(p_desc)
245             );
246         }
247     }
248 }
249 
250 #ifdef __cplusplus
251 }
252 #endif
253 
254 #endif
255