/src/vlc/modules/packetizer/hxxx_sei.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * hxxx_sei.c: AVC/HEVC packetizers SEI handling |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2001-2016 VLC authors and VideoLAN |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation; either version 2.1 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
19 | | *****************************************************************************/ |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include "config.h" |
22 | | #endif |
23 | | |
24 | | #include <vlc_common.h> |
25 | | #include <vlc_block.h> |
26 | | #include <vlc_bits.h> |
27 | | |
28 | | #include "hxxx_sei.h" |
29 | | #include "hxxx_nal.h" |
30 | | #include "hxxx_ep3b.h" |
31 | | |
32 | | void HxxxParse_AnnexB_SEI(const uint8_t *p_buf, size_t i_buf, |
33 | | uint8_t i_header, pf_hxxx_sei_callback cb, void *cbdata) |
34 | 486k | { |
35 | 486k | if( hxxx_strip_AnnexB_startcode( &p_buf, &i_buf ) ) |
36 | 486k | HxxxParseSEI(p_buf, i_buf, i_header, cb, cbdata); |
37 | 486k | } |
38 | | |
39 | | void HxxxParseSEI(const uint8_t *p_buf, size_t i_buf, |
40 | | uint8_t i_header, pf_hxxx_sei_callback pf_callback, void *cbdata) |
41 | 486k | { |
42 | 486k | bs_t s; |
43 | 486k | bool b_continue = true; |
44 | | |
45 | 486k | if( i_buf <= i_header ) |
46 | 28.3k | return; |
47 | | |
48 | 458k | struct hxxx_bsfw_ep3b_ctx_s bsctx; |
49 | 458k | hxxx_bsfw_ep3b_ctx_init( &bsctx ); |
50 | 458k | bs_init_custom( &s, &p_buf[i_header], i_buf - i_header, /* skip nal unit header */ |
51 | 458k | &hxxx_bsfw_ep3b_callbacks, &bsctx ); |
52 | | |
53 | | |
54 | 14.8M | while( !bs_eof( &s ) && bs_aligned( &s ) && b_continue ) |
55 | 14.6M | { |
56 | | /* Read type */ |
57 | 14.6M | unsigned i_type = 0; |
58 | 15.1M | while( !bs_eof( &s ) ) |
59 | 15.0M | { |
60 | 15.0M | const uint8_t i_byte = bs_read( &s, 8 ); |
61 | 15.0M | i_type += i_byte; |
62 | 15.0M | if( i_byte != 0xff ) |
63 | 14.6M | break; |
64 | 15.0M | } |
65 | | |
66 | 14.6M | if( bs_error( &s ) ) |
67 | 0 | return; |
68 | | |
69 | | /* Read size */ |
70 | 14.6M | unsigned i_size = 0; |
71 | 15.2M | while( !bs_eof( &s ) ) |
72 | 15.1M | { |
73 | 15.1M | const uint8_t i_byte = bs_read( &s, 8 ); |
74 | 15.1M | i_size += i_byte; |
75 | 15.1M | if( i_byte != 0xff ) |
76 | 14.5M | break; |
77 | 15.1M | } |
78 | | |
79 | 14.6M | if( bs_error( &s ) ) |
80 | 0 | return; |
81 | | |
82 | | /* Check room */ |
83 | 14.6M | if( bs_eof( &s ) ) |
84 | 159k | break; |
85 | | |
86 | 14.5M | hxxx_sei_data_t sei_data; |
87 | 14.5M | sei_data.i_type = i_type; |
88 | | |
89 | | /* Save start offset */ |
90 | 14.5M | const unsigned i_start_bit_pos = bs_pos( &s ); |
91 | 14.5M | switch( i_type ) |
92 | 14.5M | { |
93 | | /* Look for pic timing, do not decode locally */ |
94 | 86.1k | case HXXX_SEI_PIC_TIMING: |
95 | 86.1k | { |
96 | 86.1k | sei_data.p_bs = &s; |
97 | 86.1k | b_continue = pf_callback( &sei_data, cbdata ); |
98 | 86.1k | } break; |
99 | | |
100 | | /* Look for user_data_registered_itu_t_t35 */ |
101 | 127k | case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35: |
102 | 127k | { |
103 | 127k | size_t i_t35; |
104 | 127k | uint8_t *p_t35 = malloc( i_size ); |
105 | 127k | if( !p_t35 ) |
106 | 0 | break; |
107 | | |
108 | 2.46M | for( i_t35 = 0; i_t35<i_size && !bs_eof( &s ); i_t35++ ) |
109 | 2.33M | p_t35[i_t35] = bs_read( &s, 8 ); |
110 | | |
111 | 127k | if( bs_error( &s ) ) |
112 | 0 | { |
113 | 0 | free( p_t35 ); |
114 | 0 | break; |
115 | 0 | } |
116 | | |
117 | | /* TS 101 154 Auxiliary Data and H264/AVC video */ |
118 | 127k | if( i_t35 > 4 && p_t35[0] == 0xb5 /* United States */ ) |
119 | 87.9k | { |
120 | 87.9k | if( p_t35[1] == 0x00 && p_t35[2] == 0x31 && /* US provider code for ATSC / DVB1 */ |
121 | 65.5k | i_t35 > 7 ) |
122 | 64.7k | { |
123 | 64.7k | switch( VLC_FOURCC(p_t35[3],p_t35[4],p_t35[5],p_t35[6]) ) |
124 | 64.7k | { |
125 | 45.6k | case VLC_FOURCC('G', 'A', '9', '4'): |
126 | 45.6k | if( p_t35[7] == 0x03 ) |
127 | 44.8k | { |
128 | 44.8k | sei_data.itu_t35.type = HXXX_ITU_T35_TYPE_CC; |
129 | 44.8k | sei_data.itu_t35.u.cc.i_data = i_t35 - 8; |
130 | 44.8k | sei_data.itu_t35.u.cc.p_data = &p_t35[8]; |
131 | 44.8k | b_continue = pf_callback( &sei_data, cbdata ); |
132 | 44.8k | } |
133 | 45.6k | break; |
134 | 19.1k | default: |
135 | 19.1k | break; |
136 | 64.7k | } |
137 | 64.7k | } |
138 | 23.1k | else if( p_t35[1] == 0x00 && p_t35[2] == 0x2f && /* US provider code for DirecTV */ |
139 | 15.6k | p_t35[3] == 0x03 && i_t35 > 5 ) |
140 | 14.6k | { |
141 | | /* DirecTV does not use GA94 user_data identifier */ |
142 | 14.6k | sei_data.itu_t35.type = HXXX_ITU_T35_TYPE_CC; |
143 | 14.6k | sei_data.itu_t35.u.cc.i_data = i_t35 - 5; |
144 | 14.6k | sei_data.itu_t35.u.cc.p_data = &p_t35[5]; |
145 | 14.6k | b_continue = pf_callback( &sei_data, cbdata ); |
146 | 14.6k | } |
147 | 87.9k | } |
148 | | |
149 | 127k | free( p_t35 ); |
150 | 127k | } break; |
151 | | |
152 | 109k | case HXXX_SEI_FRAME_PACKING_ARRANGEMENT: |
153 | 109k | { |
154 | 109k | bs_read_ue( &s ); |
155 | 109k | if ( !bs_read1( &s ) ) |
156 | 106k | { |
157 | 106k | sei_data.frame_packing.type = bs_read( &s, 7 ); |
158 | 106k | bs_read( &s, 1 ); |
159 | 106k | if( bs_read( &s, 6 ) == 2 ) /*intpr type*/ |
160 | 666 | sei_data.frame_packing.b_left_first = false; |
161 | 105k | else |
162 | 105k | sei_data.frame_packing.b_left_first = true; |
163 | 106k | sei_data.frame_packing.b_flipped = bs_read1( &s ); |
164 | 106k | sei_data.frame_packing.b_fields = bs_read1( &s ); |
165 | 106k | sei_data.frame_packing.b_frame0 = bs_read1( &s ); |
166 | 106k | } |
167 | 2.87k | else sei_data.frame_packing.type = FRAME_PACKING_CANCEL; |
168 | | |
169 | 109k | } break; |
170 | | |
171 | | /* Look for SEI recovery point */ |
172 | 36.6k | case HXXX_SEI_RECOVERY_POINT: |
173 | 36.6k | { |
174 | 36.6k | sei_data.p_bs = &s; |
175 | 36.6k | b_continue = pf_callback( &sei_data, cbdata ); |
176 | 36.6k | } break; |
177 | | |
178 | 27.8k | case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME: |
179 | 27.8k | { |
180 | 194k | for ( size_t i = 0; i < 6 ; ++i) |
181 | 166k | sei_data.colour_volume.primaries[i] = bs_read( &s, 16 ); |
182 | 83.4k | for ( size_t i = 0; i < 2 ; ++i) |
183 | 55.6k | sei_data.colour_volume.white_point[i] = bs_read( &s, 16 ); |
184 | 27.8k | sei_data.colour_volume.max_luminance = bs_read( &s, 32 ); |
185 | 27.8k | sei_data.colour_volume.min_luminance = bs_read( &s, 32 ); |
186 | 27.8k | if( bs_error( &s ) ) /* not enough data */ |
187 | 16.6k | break; |
188 | 11.1k | b_continue = pf_callback( &sei_data, cbdata ); |
189 | 11.1k | } break; |
190 | | |
191 | 11.0k | case HXXX_SEI_CONTENT_LIGHT_LEVEL: |
192 | 11.0k | { |
193 | 11.0k | sei_data.content_light_lvl.MaxCLL = bs_read( &s, 16 ); |
194 | 11.0k | sei_data.content_light_lvl.MaxFALL = bs_read( &s, 16 ); |
195 | 11.0k | if( bs_error( &s ) ) /* not enough data */ |
196 | 3.78k | break; |
197 | 7.23k | b_continue = pf_callback( &sei_data, cbdata ); |
198 | 7.23k | } break; |
199 | | |
200 | 14.1M | default: |
201 | | /* Will skip */ |
202 | 14.1M | break; |
203 | 14.5M | } |
204 | 14.5M | const unsigned i_end_bit_pos = bs_pos( &s ); |
205 | | |
206 | | /* Skip unsparsed content */ |
207 | 14.5M | if( i_end_bit_pos - i_start_bit_pos > i_size * 8 ) /* Something went wrong with _ue reads */ |
208 | 117k | break; |
209 | 14.3M | bs_skip( &s, i_size * 8 - ( i_end_bit_pos - i_start_bit_pos ) ); |
210 | 14.3M | } |
211 | 458k | } |