dlna: add PrepareForConnection action
[vlc.git] / modules / packetizer / hxxx_sei.c
blob79cfa91b3de03778b9f7ad34dcdf1d0e92649d1e
1 /*****************************************************************************
2 * hxxx_sei.c: AVC/HEVC packetizers SEI handling
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
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.
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.
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
24 #include <vlc_common.h>
25 #include <vlc_block.h>
26 #include <vlc_bits.h>
28 #include "hxxx_sei.h"
29 #include "hxxx_nal.h"
30 #include "hxxx_ep3b.h"
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)
35 if( hxxx_strip_AnnexB_startcode( &p_buf, &i_buf ) )
36 HxxxParseSEI(p_buf, i_buf, i_header, cb, cbdata);
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)
42 bs_t s;
43 bool b_continue = true;
45 if( i_buf <= i_header )
46 return;
48 struct hxxx_bsfw_ep3b_ctx_s bsctx;
49 hxxx_bsfw_ep3b_ctx_init( &bsctx );
50 bs_init_custom( &s, &p_buf[i_header], i_buf - i_header, /* skip nal unit header */
51 &hxxx_bsfw_ep3b_callbacks, &bsctx );
54 while( bs_remain( &s ) >= 8 && bs_aligned( &s ) && b_continue )
56 /* Read type */
57 unsigned i_type = 0;
58 while( bs_remain( &s ) >= 8 )
60 const uint8_t i_byte = bs_read( &s, 8 );
61 i_type += i_byte;
62 if( i_byte != 0xff )
63 break;
66 /* Read size */
67 unsigned i_size = 0;
68 while( bs_remain( &s ) >= 8 )
70 const uint8_t i_byte = bs_read( &s, 8 );
71 i_size += i_byte;
72 if( i_byte != 0xff )
73 break;
76 /* Check room */
77 if( bs_remain( &s ) < 8 )
78 break;
80 hxxx_sei_data_t sei_data;
81 sei_data.i_type = i_type;
83 /* Save start offset */
84 const unsigned i_start_bit_pos = bs_pos( &s );
85 switch( i_type )
87 /* Look for pic timing, do not decode locally */
88 case HXXX_SEI_PIC_TIMING:
90 sei_data.p_bs = &s;
91 b_continue = pf_callback( &sei_data, cbdata );
92 } break;
94 /* Look for user_data_registered_itu_t_t35 */
95 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
97 size_t i_t35;
98 uint8_t *p_t35 = malloc( i_size );
99 if( !p_t35 )
100 break;
102 for( i_t35 = 0; i_t35<i_size && bs_remain( &s ) >= 8; i_t35++ )
103 p_t35[i_t35] = bs_read( &s, 8 );
105 /* TS 101 154 Auxiliary Data and H264/AVC video */
106 if( i_t35 > 4 && p_t35[0] == 0xb5 /* United States */ )
108 if( p_t35[1] == 0x00 && p_t35[2] == 0x31 && /* US provider code for ATSC / DVB1 */
109 i_t35 > 7 )
111 switch( VLC_FOURCC(p_t35[3],p_t35[4],p_t35[5],p_t35[6]) )
113 case VLC_FOURCC('G', 'A', '9', '4'):
114 if( p_t35[7] == 0x03 )
116 sei_data.itu_t35.type = HXXX_ITU_T35_TYPE_CC;
117 sei_data.itu_t35.u.cc.i_data = i_t35 - 8;
118 sei_data.itu_t35.u.cc.p_data = &p_t35[8];
119 b_continue = pf_callback( &sei_data, cbdata );
121 break;
122 default:
123 break;
126 else if( p_t35[1] == 0x00 && p_t35[2] == 0x2f && /* US provider code for DirecTV */
127 p_t35[3] == 0x03 && i_t35 > 5 )
129 /* DirecTV does not use GA94 user_data identifier */
130 sei_data.itu_t35.type = HXXX_ITU_T35_TYPE_CC;
131 sei_data.itu_t35.u.cc.i_data = i_t35 - 5;
132 sei_data.itu_t35.u.cc.p_data = &p_t35[5];
133 b_continue = pf_callback( &sei_data, cbdata );
137 free( p_t35 );
138 } break;
140 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT:
142 bs_read_ue( &s );
143 if ( !bs_read1( &s ) )
145 sei_data.frame_packing.type = bs_read( &s, 7 );
146 bs_read( &s, 1 );
147 if( bs_read( &s, 6 ) == 2 ) /*intpr type*/
148 sei_data.frame_packing.b_left_first = false;
149 else
150 sei_data.frame_packing.b_left_first = true;
151 sei_data.frame_packing.b_flipped = bs_read1( &s );
152 sei_data.frame_packing.b_fields = bs_read1( &s );
153 sei_data.frame_packing.b_frame0 = bs_read1( &s );
155 else sei_data.frame_packing.type = FRAME_PACKING_CANCEL;
157 } break;
159 /* Look for SEI recovery point */
160 case HXXX_SEI_RECOVERY_POINT:
162 sei_data.recovery.i_frames = bs_read_ue( &s );
163 //bool b_exact_match = bs_read( &s, 1 );
164 //bool b_broken_link = bs_read( &s, 1 );
165 //int i_changing_slice_group = bs_read( &s, 2 );
166 b_continue = pf_callback( &sei_data, cbdata );
167 } break;
169 case HXXX_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
171 if ( bs_remain( &s ) < (16*6+16*2+32+32) )
172 /* not enough data */
173 break;
174 for ( size_t i = 0; i < 6 ; ++i)
175 sei_data.colour_volume.primaries[i] = bs_read( &s, 16 );
176 for ( size_t i = 0; i < 2 ; ++i)
177 sei_data.colour_volume.white_point[i] = bs_read( &s, 16 );
178 sei_data.colour_volume.max_luminance = bs_read( &s, 32 );
179 sei_data.colour_volume.min_luminance = bs_read( &s, 32 );
180 b_continue = pf_callback( &sei_data, cbdata );
181 } break;
183 case HXXX_SEI_CONTENT_LIGHT_LEVEL:
185 if ( bs_remain( &s ) < (16+16) )
186 /* not enough data */
187 break;
188 sei_data.content_light_lvl.MaxCLL = bs_read( &s, 16 );
189 sei_data.content_light_lvl.MaxFALL = bs_read( &s, 16 );
190 b_continue = pf_callback( &sei_data, cbdata );
191 } break;
193 default:
194 /* Will skip */
195 break;
197 const unsigned i_end_bit_pos = bs_pos( &s );
199 /* Skip unsparsed content */
200 if( i_end_bit_pos - i_start_bit_pos > i_size * 8 ) /* Something went wrong with _ue reads */
201 break;
202 bs_skip( &s, i_size * 8 - ( i_end_bit_pos - i_start_bit_pos ) );