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