1 /*****************************************************************************
2  * desc_27.h: ISO/IEC 13818-1 Descriptor 0x27 (Metadata STD 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_27_H__
34 #define __BITSTREAM_MPEG_DESC_27_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 0x27 (Metadata STD descriptor)
46  *****************************************************************************/
47 #define DESC27_HEADER_SIZE      (DESC_HEADER_SIZE + 9)
48 
desc27_init(uint8_t * p_desc)49 static inline void desc27_init(uint8_t *p_desc)
50 {
51     desc_set_tag(p_desc, 0x27);
52     desc_set_length(p_desc, DESC27_HEADER_SIZE - DESC_HEADER_SIZE);
53 }
54 
desc27_validate(const uint8_t * p_desc)55 static inline bool desc27_validate(const uint8_t *p_desc)
56 {
57     return desc_get_length(p_desc) >= DESC27_HEADER_SIZE - DESC_HEADER_SIZE;
58 }
59 
desc27_get_input_leak_rate(const uint8_t * p_desc)60 static inline uint32_t desc27_get_input_leak_rate(const uint8_t *p_desc)
61 {
62     return ((p_desc[2] & 0x3f) << 16) | (p_desc[3] << 8) | p_desc[4];
63 }
64 
desc27_set_input_leak_rate(uint8_t * p_desc,uint32_t i_input_leak_rate)65 static inline void desc27_set_input_leak_rate(uint8_t *p_desc, uint32_t i_input_leak_rate)
66 {
67     p_desc[2] = ((i_input_leak_rate >> 16) & 0xff) | 0xc0;
68     p_desc[3] =  (i_input_leak_rate >>  8) & 0xff;
69     p_desc[4] =   i_input_leak_rate        & 0xff;
70 }
71 
desc27_get_buffer_size(const uint8_t * p_desc)72 static inline uint32_t desc27_get_buffer_size(const uint8_t *p_desc)
73 {
74     return ((p_desc[5] & 0x3f) << 16) | (p_desc[6] << 8) | p_desc[7];
75 }
76 
desc27_set_buffer_size(uint8_t * p_desc,uint32_t i_buffer_size)77 static inline void desc27_set_buffer_size(uint8_t *p_desc, uint32_t i_buffer_size)
78 {
79     p_desc[5] = ((i_buffer_size >> 16) & 0xff) | 0xc0;
80     p_desc[6] =  (i_buffer_size >>  8) & 0xff;
81     p_desc[7] =   i_buffer_size        & 0xff;
82 }
83 
desc27_get_output_leak_rate(const uint8_t * p_desc)84 static inline uint32_t desc27_get_output_leak_rate(const uint8_t *p_desc)
85 {
86     return ((p_desc[8] & 0x3f) << 16) | (p_desc[9] << 8) | p_desc[10];
87 }
88 
desc27_set_output_leak_rate(uint8_t * p_desc,uint32_t i_output_leak_rate)89 static inline void desc27_set_output_leak_rate(uint8_t *p_desc, uint32_t i_output_leak_rate)
90 {
91     p_desc[ 8] = ((i_output_leak_rate >> 16) & 0xff) | 0xc0;
92     p_desc[ 9] =  (i_output_leak_rate >>  8) & 0xff;
93     p_desc[10] =   i_output_leak_rate        & 0xff;
94 }
95 
desc27_print(const uint8_t * p_desc,f_print pf_print,void * opaque,print_type_t i_print_type)96 static inline void desc27_print(const uint8_t *p_desc, f_print pf_print,
97                                 void *opaque, print_type_t i_print_type)
98 {
99     switch (i_print_type) {
100     case PRINT_XML:
101         pf_print(opaque, "<METADATA_STD_DESC input_leak_rate=\"%u\" buffer_size=\"%u\" output_leak_rate=\"%u\"/>",
102                  desc27_get_input_leak_rate(p_desc),
103                  desc27_get_buffer_size(p_desc),
104                  desc27_get_output_leak_rate(p_desc));
105         break;
106     default:
107         pf_print(opaque,"    - desc 27 metadata_std input_leak_rate=%u buffer_size=%u output_leak_rate=%u",
108                  desc27_get_input_leak_rate(p_desc),
109                  desc27_get_buffer_size(p_desc),
110                  desc27_get_output_leak_rate(p_desc));
111     }
112 }
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif
119