/src/vlc/modules/packetizer/h264.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * h264.c: h264/avc video packetizer |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Laurent Aimar <fenrir@via.ecp.fr> |
7 | | * Eric Petit <titer@videolan.org> |
8 | | * Gildas Bazin <gbazin@videolan.org> |
9 | | * Derk-Jan Hartman <hartman at videolan dot org> |
10 | | * |
11 | | * This program is free software; you can redistribute it and/or modify it |
12 | | * under the terms of the GNU Lesser General Public License as published by |
13 | | * the Free Software Foundation; either version 2.1 of the License, or |
14 | | * (at your option) any later version. |
15 | | * |
16 | | * This program is distributed in the hope that it will be useful, |
17 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | | * GNU Lesser General Public License for more details. |
20 | | * |
21 | | * You should have received a copy of the GNU Lesser General Public License |
22 | | * along with this program; if not, write to the Free Software Foundation, |
23 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
24 | | *****************************************************************************/ |
25 | | |
26 | | /***************************************************************************** |
27 | | * Preamble |
28 | | *****************************************************************************/ |
29 | | |
30 | | #ifdef HAVE_CONFIG_H |
31 | | # include "config.h" |
32 | | #endif |
33 | | |
34 | | #include <vlc_common.h> |
35 | | #include <vlc_plugin.h> |
36 | | #include <vlc_sout.h> |
37 | | #include <vlc_codec.h> |
38 | | #include <vlc_block.h> |
39 | | |
40 | | #include <vlc_block_helper.h> |
41 | | #include <vlc_bits.h> |
42 | | #include "h264_nal.h" |
43 | | #include "h264_slice.h" |
44 | | #include "hxxx_nal.h" |
45 | | #include "hxxx_sei.h" |
46 | | #include "hxxx_common.h" |
47 | | #include "packetizer_helper.h" |
48 | | #include "startcode_helper.h" |
49 | | |
50 | | #include <limits.h> |
51 | | |
52 | | /***************************************************************************** |
53 | | * Module descriptor |
54 | | *****************************************************************************/ |
55 | | static int Open ( vlc_object_t * ); |
56 | | static void Close( vlc_object_t * ); |
57 | | |
58 | 150 | vlc_module_begin () |
59 | 75 | set_subcategory( SUBCAT_SOUT_PACKETIZER ) |
60 | 75 | set_description( N_("H.264 video packetizer") ) |
61 | 75 | set_capability( "video packetizer", 50 ) |
62 | 150 | set_callbacks( Open, Close ) |
63 | 75 | vlc_module_end () |
64 | | |
65 | | |
66 | | /**************************************************************************** |
67 | | * Local prototypes |
68 | | ****************************************************************************/ |
69 | | |
70 | | typedef struct |
71 | | { |
72 | | /* */ |
73 | | packetizer_t packetizer; |
74 | | |
75 | | /* */ |
76 | | struct |
77 | | { |
78 | | block_t *p_head; |
79 | | block_t **pp_append; |
80 | | } frame, leading; |
81 | | |
82 | | /* a new sps/pps can be transmitted outside of iframes */ |
83 | | bool b_new_sps; |
84 | | bool b_new_pps; |
85 | | |
86 | | struct |
87 | | { |
88 | | block_t *p_block; |
89 | | h264_sequence_parameter_set_t *p_sps; |
90 | | } sps[H264_MAX_NUM_SPS]; |
91 | | struct |
92 | | { |
93 | | block_t *p_block; |
94 | | h264_picture_parameter_set_t *p_pps; |
95 | | } pps[H264_MAX_NUM_PPS]; |
96 | | struct |
97 | | { |
98 | | block_t *p_block; |
99 | | } spsext[H264_MAX_NUM_SPSEXT]; |
100 | | const h264_sequence_parameter_set_t *p_active_sps; |
101 | | const h264_picture_parameter_set_t *p_active_pps; |
102 | | |
103 | | /* avcC data */ |
104 | | uint8_t i_avcC_length_size; |
105 | | |
106 | | /* From SEI for current frame */ |
107 | | uint8_t i_pic_struct; |
108 | | uint8_t i_dpb_output_delay; |
109 | | unsigned i_recovery_frame_cnt; |
110 | | |
111 | | /* Current Slice Header */ |
112 | | h264_slice_t *p_slice; |
113 | | |
114 | | /* */ |
115 | | int i_next_block_flags; |
116 | | bool b_recovered; |
117 | | unsigned i_recoveryfnum; |
118 | | unsigned i_recoverystartfnum; |
119 | | |
120 | | /* POC */ |
121 | | h264_poc_context_t pocctx; |
122 | | struct |
123 | | { |
124 | | vlc_tick_t pts; |
125 | | int num; |
126 | | } prevdatedpoc; |
127 | | |
128 | | vlc_tick_t i_frame_pts; |
129 | | vlc_tick_t i_frame_dts; |
130 | | |
131 | | date_t dts; |
132 | | |
133 | | /* */ |
134 | | cc_storage_t *p_ccs; |
135 | | } decoder_sys_t; |
136 | | |
137 | 500k | #define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT) |
138 | 590k | #define BLOCK_FLAG_PRIVATE_SEI (2 << BLOCK_FLAG_PRIVATE_SHIFT) |
139 | 445k | #define BLOCK_FLAG_DROP (4 << BLOCK_FLAG_PRIVATE_SHIFT) |
140 | | |
141 | | static block_t *Packetize( decoder_t *, block_t ** ); |
142 | | static block_t *PacketizeAVC1( decoder_t *, block_t ** ); |
143 | | static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t * ); |
144 | | static void PacketizeFlush( decoder_t * ); |
145 | | |
146 | | static void PacketizeReset( void *p_private, bool b_broken ); |
147 | | static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * ); |
148 | | static int PacketizeValidate( void *p_private, block_t * ); |
149 | | static block_t * PacketizeDrain( void *p_private ); |
150 | | |
151 | | static block_t *ParseNALBlock( decoder_t *, bool *pb_ts_used, block_t * ); |
152 | | static inline block_t *ParseNALBlockW( void *opaque, bool *pb_ts_used, block_t *p_frag ) |
153 | 0 | { |
154 | 0 | return ParseNALBlock( (decoder_t *) opaque, pb_ts_used, p_frag ); |
155 | 0 | } |
156 | | |
157 | | static block_t *OutputPicture( decoder_t *p_dec ); |
158 | | static void ReleaseXPS( decoder_sys_t *p_sys ); |
159 | | static bool PutXPS( decoder_t *p_dec, uint8_t i_nal_type, block_t *p_frag ); |
160 | | static h264_slice_t * ParseSliceHeader( decoder_t *p_dec, const block_t *p_frag ); |
161 | | static bool ParseSeiCallback( const hxxx_sei_data_t *, void * ); |
162 | | |
163 | | |
164 | | |
165 | | /***************************************************************************** |
166 | | * Helpers |
167 | | *****************************************************************************/ |
168 | | static void LastAppendXPSCopy( const block_t *p_block, block_t ***ppp_last ) |
169 | 54.5M | { |
170 | 54.5M | if( !p_block ) |
171 | 54.0M | return; |
172 | 497k | block_t *p_dup = block_Alloc( 4 + p_block->i_buffer ); |
173 | 497k | if( p_dup ) |
174 | 497k | { |
175 | 497k | memcpy( &p_dup->p_buffer[0], annexb_startcode4, 4 ); |
176 | 497k | memcpy( &p_dup->p_buffer[4], p_block->p_buffer, p_block->i_buffer ); |
177 | 497k | block_ChainLastAppend( ppp_last, p_dup ); |
178 | 497k | } |
179 | 497k | } |
180 | | |
181 | | static block_t * GatherSets( decoder_sys_t *p_sys, bool b_need_sps, bool b_need_pps ) |
182 | 428k | { |
183 | 428k | block_t *p_xpsnal = NULL; |
184 | 428k | block_t **pp_xpsnal_tail = &p_xpsnal; |
185 | 5.51M | for( int i = 0; i <= H264_SPS_ID_MAX && b_need_sps; i++ ) |
186 | 5.08M | { |
187 | 5.08M | LastAppendXPSCopy( p_sys->sps[i].p_block, &pp_xpsnal_tail ); |
188 | | /* 7.4.1.2.3, shall be the next NAL unit after a sequence parameter set NAL unit |
189 | | * having the same value of seq_parameter_set_id */ |
190 | 5.08M | LastAppendXPSCopy( p_sys->spsext[i].p_block, &pp_xpsnal_tail ); |
191 | 5.08M | } |
192 | 44.7M | for( int i = 0; i < H264_PPS_ID_MAX && b_need_pps; i++ ) |
193 | 44.3M | LastAppendXPSCopy( p_sys->pps[i].p_block, &pp_xpsnal_tail ); |
194 | 428k | return p_xpsnal; |
195 | 428k | } |
196 | | |
197 | | static void ActivateSets( decoder_t *p_dec, const h264_sequence_parameter_set_t *p_sps, |
198 | | const h264_picture_parameter_set_t *p_pps ) |
199 | 500k | { |
200 | 500k | decoder_sys_t *p_sys = p_dec->p_sys; |
201 | | |
202 | 500k | p_sys->p_active_pps = p_pps; |
203 | 500k | p_sys->p_active_sps = p_sps; |
204 | | |
205 | 500k | if( p_sps ) |
206 | 500k | { |
207 | 500k | uint8_t pl[2]; |
208 | 500k | if( h264_get_sps_profile_tier_level( p_sps, pl, &pl[1] ) ) |
209 | 500k | { |
210 | 500k | p_dec->fmt_out.i_profile = pl[0]; |
211 | 500k | p_dec->fmt_out.i_level = pl[1]; |
212 | 500k | } |
213 | | |
214 | 500k | unsigned x_offset, y_offset, width, height, visible_width, visible_height; |
215 | 500k | if( h264_get_picture_size( p_sps, |
216 | 500k | &x_offset, &y_offset, |
217 | 500k | &width, &height, |
218 | 500k | &visible_width, &visible_height ) ) |
219 | 466k | { |
220 | 466k | p_dec->fmt_out.video.i_x_offset = x_offset; |
221 | 466k | p_dec->fmt_out.video.i_y_offset = y_offset; |
222 | 466k | p_dec->fmt_out.video.i_width = width; |
223 | 466k | p_dec->fmt_out.video.i_height = height; |
224 | 466k | p_dec->fmt_out.video.i_visible_width = visible_width; |
225 | 466k | p_dec->fmt_out.video.i_visible_height = visible_height; |
226 | 466k | } |
227 | | |
228 | 500k | h264_get_aspect_ratio( p_sps, |
229 | 500k | &p_dec->fmt_out.video.i_sar_num, |
230 | 500k | &p_dec->fmt_out.video.i_sar_den ); |
231 | | |
232 | 500k | if( !p_dec->fmt_out.video.i_frame_rate || |
233 | 490k | !p_dec->fmt_out.video.i_frame_rate_base ) |
234 | 9.73k | { |
235 | | /* on first run == if fmt_in does not provide frame rate info */ |
236 | | /* If we have frame rate info in the stream */ |
237 | 9.73k | unsigned nd[2]; |
238 | 9.73k | if( h264_get_frame_rate( p_sps, nd, &nd[1] ) ) |
239 | 2.18k | date_Change( &p_sys->dts, nd[0], nd[1] ); |
240 | | /* else use the default num/den */ |
241 | 9.73k | p_dec->fmt_out.video.i_frame_rate = p_sys->dts.i_divider_num >> 1; /* num_clock_ts == 2 */ |
242 | 9.73k | p_dec->fmt_out.video.i_frame_rate_base = p_sys->dts.i_divider_den; |
243 | 9.73k | } |
244 | | |
245 | 500k | if( p_dec->fmt_in->video.primaries == COLOR_PRIMARIES_UNDEF ) |
246 | 497k | { |
247 | 497k | h264_get_colorimetry( p_sps, &p_dec->fmt_out.video.primaries, |
248 | 497k | &p_dec->fmt_out.video.transfer, |
249 | 497k | &p_dec->fmt_out.video.space, |
250 | 497k | &p_dec->fmt_out.video.color_range ); |
251 | 497k | } |
252 | | |
253 | 500k | if( p_dec->fmt_out.i_extra == 0 && p_pps ) |
254 | 5.59k | { |
255 | 5.59k | block_t *p_xpsblocks = GatherSets( p_sys, true, true ); |
256 | 5.59k | if( p_xpsblocks ) |
257 | 5.59k | { |
258 | 5.59k | size_t i_total; |
259 | 5.59k | block_ChainProperties( p_xpsblocks, NULL, &i_total, NULL ); |
260 | 5.59k | p_dec->fmt_out.p_extra = malloc( i_total ); |
261 | 5.59k | if( p_dec->fmt_out.p_extra ) |
262 | 5.59k | { |
263 | 5.59k | p_dec->fmt_out.i_extra = i_total; |
264 | 5.59k | block_ChainExtract( p_xpsblocks, p_dec->fmt_out.p_extra, i_total ); |
265 | 5.59k | } |
266 | 5.59k | block_ChainRelease( p_xpsblocks ); |
267 | 5.59k | } |
268 | 5.59k | } |
269 | 500k | } |
270 | 500k | } |
271 | | |
272 | | static void DropStoredNAL( decoder_sys_t *p_sys ) |
273 | 250k | { |
274 | 250k | block_ChainRelease( p_sys->frame.p_head ); |
275 | 250k | block_ChainRelease( p_sys->leading.p_head ); |
276 | 250k | p_sys->frame.p_head = NULL; |
277 | 250k | p_sys->frame.pp_append = &p_sys->frame.p_head; |
278 | 250k | p_sys->leading.p_head = NULL; |
279 | 250k | p_sys->leading.pp_append = &p_sys->leading.p_head; |
280 | 250k | } |
281 | | |
282 | | /***************************************************************************** |
283 | | * Open: probe the packetizer and return score |
284 | | * When opening after demux, the packetizer is only loaded AFTER the decoder |
285 | | * That means that what you set in fmt_out is ignored by the decoder in this special case |
286 | | *****************************************************************************/ |
287 | | static int Open( vlc_object_t *p_this ) |
288 | 164k | { |
289 | 164k | decoder_t *p_dec = (decoder_t*)p_this; |
290 | 164k | decoder_sys_t *p_sys; |
291 | 164k | int i; |
292 | | |
293 | 164k | const bool b_avc = (p_dec->fmt_in->i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1' )); |
294 | | |
295 | 164k | if( p_dec->fmt_in->i_codec != VLC_CODEC_H264 ) |
296 | 91.3k | return VLC_EGENERIC; |
297 | 73.0k | if( b_avc && p_dec->fmt_in->i_extra < 7 ) |
298 | 18 | return VLC_EGENERIC; |
299 | | |
300 | | /* Allocate the memory needed to store the decoder's structure */ |
301 | 73.0k | if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL ) |
302 | 0 | { |
303 | 0 | return VLC_ENOMEM; |
304 | 0 | } |
305 | | |
306 | 73.0k | p_sys->p_ccs = cc_storage_new(); |
307 | 73.0k | if( unlikely(!p_sys->p_ccs) ) |
308 | 0 | { |
309 | 0 | free( p_sys ); |
310 | 0 | return VLC_ENOMEM; |
311 | 0 | } |
312 | | |
313 | 73.0k | packetizer_Init( &p_sys->packetizer, |
314 | 73.0k | annexb_startcode3, 3, startcode_FindAnnexB, |
315 | 73.0k | annexb_startcode3, 1, 5, |
316 | 73.0k | PacketizeReset, PacketizeParse, PacketizeValidate, PacketizeDrain, |
317 | 73.0k | p_dec ); |
318 | | |
319 | 73.0k | p_sys->p_slice = NULL; |
320 | 73.0k | p_sys->frame.p_head = NULL; |
321 | 73.0k | p_sys->frame.pp_append = &p_sys->frame.p_head; |
322 | 73.0k | p_sys->leading.p_head = NULL; |
323 | 73.0k | p_sys->leading.pp_append = &p_sys->leading.p_head; |
324 | 73.0k | p_sys->b_new_sps = false; |
325 | 73.0k | p_sys->b_new_pps = false; |
326 | | |
327 | 2.41M | for( i = 0; i < H264_MAX_NUM_SPS; i++ ) |
328 | 2.33M | { |
329 | 2.33M | p_sys->sps[i].p_sps = NULL; |
330 | 2.33M | p_sys->sps[i].p_block = NULL; |
331 | 2.33M | } |
332 | 73.0k | p_sys->p_active_sps = NULL; |
333 | 18.7M | for( i = 0; i < H264_MAX_NUM_PPS; i++ ) |
334 | 18.7M | { |
335 | 18.7M | p_sys->pps[i].p_pps = NULL; |
336 | 18.7M | p_sys->pps[i].p_block = NULL; |
337 | 18.7M | } |
338 | 73.0k | p_sys->p_active_pps = NULL; |
339 | 2.41M | for( i = 0; i < H264_MAX_NUM_SPSEXT; i++ ) |
340 | 2.33M | p_sys->spsext[i].p_block = NULL; |
341 | 73.0k | p_sys->i_recovery_frame_cnt = UINT_MAX; |
342 | | |
343 | 73.0k | p_sys->i_next_block_flags = 0; |
344 | 73.0k | p_sys->b_recovered = false; |
345 | 73.0k | p_sys->i_recoveryfnum = UINT_MAX; |
346 | 73.0k | p_sys->i_frame_dts = VLC_TICK_INVALID; |
347 | 73.0k | p_sys->i_frame_pts = VLC_TICK_INVALID; |
348 | 73.0k | p_sys->i_dpb_output_delay = 0; |
349 | | |
350 | | /* POC */ |
351 | 73.0k | h264_poc_context_init( &p_sys->pocctx ); |
352 | 73.0k | p_sys->prevdatedpoc.pts = VLC_TICK_INVALID; |
353 | | |
354 | 73.0k | date_Init( &p_sys->dts, 30000 * 2, 1001 ); |
355 | | |
356 | | /* Setup properties */ |
357 | 73.0k | es_format_Copy( &p_dec->fmt_out, p_dec->fmt_in ); |
358 | 73.0k | p_dec->fmt_out.i_codec = VLC_CODEC_H264; |
359 | 73.0k | p_dec->fmt_out.b_packetized = true; |
360 | | |
361 | 73.0k | if( p_dec->fmt_in->video.i_frame_rate_base && |
362 | 4.03k | p_dec->fmt_in->video.i_frame_rate && |
363 | 4.03k | p_dec->fmt_in->video.i_frame_rate <= UINT_MAX / 2 ) |
364 | 4.03k | { |
365 | 4.03k | date_Change( &p_sys->dts, p_dec->fmt_in->video.i_frame_rate * 2, |
366 | 4.03k | p_dec->fmt_in->video.i_frame_rate_base ); |
367 | 4.03k | } |
368 | | |
369 | 73.0k | if( b_avc ) |
370 | 0 | { |
371 | | /* This type of stream is produced by mp4 and matroska |
372 | | * when we want to store it in another streamformat, you need to convert |
373 | | * The fmt_in.p_extra should ALWAYS contain the avcC |
374 | | * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */ |
375 | 0 | if( h264_isavcC( p_dec->fmt_in->p_extra, p_dec->fmt_in->i_extra ) ) |
376 | 0 | { |
377 | 0 | size_t i_newextra_size; uint8_t *p_newextra; |
378 | 0 | if( !h264_avcC_to_AnnexB_NAL( p_dec->fmt_in->p_extra, |
379 | 0 | p_dec->fmt_in->i_extra, |
380 | 0 | &p_newextra, &i_newextra_size, |
381 | 0 | &p_sys->i_avcC_length_size ) ) |
382 | 0 | { |
383 | 0 | msg_Err( p_dec, "Invalid AVC extradata"); |
384 | 0 | Close( p_this ); |
385 | 0 | return VLC_EGENERIC; |
386 | 0 | } |
387 | 0 | free( p_dec->fmt_out.p_extra ); |
388 | 0 | p_dec->fmt_out.p_extra = p_newextra; |
389 | 0 | p_dec->fmt_out.i_extra = i_newextra_size; |
390 | 0 | p_sys->b_recovered = !!p_dec->fmt_out.i_extra; |
391 | 0 | } |
392 | 0 | else |
393 | 0 | { |
394 | 0 | msg_Err( p_dec, "Invalid or missing AVC extradata"); |
395 | 0 | Close( p_this ); |
396 | 0 | return VLC_EGENERIC; |
397 | 0 | } |
398 | | |
399 | | /* Set callback */ |
400 | 0 | p_dec->pf_packetize = PacketizeAVC1; |
401 | 0 | } |
402 | 73.0k | else |
403 | 73.0k | { |
404 | | /* This type of stream contains data with 3 of 4 byte startcodes |
405 | | * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes |
406 | | * The fmt_out.p_extra should be the same */ |
407 | | |
408 | | /* Set callback */ |
409 | 73.0k | p_dec->pf_packetize = Packetize; |
410 | 73.0k | } |
411 | | |
412 | | /* */ |
413 | 73.0k | if( p_dec->fmt_out.i_extra > 0 ) |
414 | 44.5k | { |
415 | 44.5k | packetizer_Header( &p_sys->packetizer, |
416 | 44.5k | p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra ); |
417 | 44.5k | } |
418 | | |
419 | 73.0k | if( b_avc ) |
420 | 0 | { |
421 | | /* FIXME: that's not correct for every AVC */ |
422 | 0 | if( !p_sys->b_new_pps || !p_sys->b_new_sps ) |
423 | 0 | { |
424 | 0 | msg_Err( p_dec, "Invalid or missing SPS %d or PPS %d in AVC extradata", |
425 | 0 | p_sys->b_new_sps, p_sys->b_new_pps ); |
426 | 0 | Close( p_this ); |
427 | 0 | return VLC_EGENERIC; |
428 | 0 | } |
429 | | |
430 | 0 | msg_Dbg( p_dec, "Packetizer fed with AVC, nal length size=%d", |
431 | 0 | p_sys->i_avcC_length_size ); |
432 | 0 | } |
433 | | |
434 | | /* CC are the same for H264/AVC in T35 sections (ETSI TS 101 154) */ |
435 | 73.0k | p_dec->pf_get_cc = GetCc; |
436 | 73.0k | p_dec->pf_flush = PacketizeFlush; |
437 | | |
438 | 73.0k | return VLC_SUCCESS; |
439 | 73.0k | } |
440 | | |
441 | | /***************************************************************************** |
442 | | * Close: clean up the packetizer |
443 | | *****************************************************************************/ |
444 | | static void Close( vlc_object_t *p_this ) |
445 | 73.0k | { |
446 | 73.0k | decoder_t *p_dec = (decoder_t*)p_this; |
447 | 73.0k | decoder_sys_t *p_sys = p_dec->p_sys; |
448 | | |
449 | 73.0k | DropStoredNAL( p_sys ); |
450 | 73.0k | ReleaseXPS( p_sys ); |
451 | | |
452 | 73.0k | if( p_sys->p_slice ) |
453 | 9 | h264_slice_release( p_sys->p_slice ); |
454 | | |
455 | 73.0k | packetizer_Clean( &p_sys->packetizer ); |
456 | | |
457 | 73.0k | cc_storage_delete( p_sys->p_ccs ); |
458 | | |
459 | 73.0k | free( p_sys ); |
460 | 73.0k | } |
461 | | |
462 | | static void PacketizeFlush( decoder_t *p_dec ) |
463 | 0 | { |
464 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
465 | |
|
466 | 0 | packetizer_Flush( &p_sys->packetizer ); |
467 | 0 | } |
468 | | |
469 | | /**************************************************************************** |
470 | | * Packetize: the whole thing |
471 | | * Search for the startcodes 3 or more bytes |
472 | | * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs |
473 | | ****************************************************************************/ |
474 | | static block_t *Packetize( decoder_t *p_dec, block_t **pp_block ) |
475 | 924k | { |
476 | 924k | decoder_sys_t *p_sys = p_dec->p_sys; |
477 | | |
478 | 924k | return packetizer_Packetize( &p_sys->packetizer, pp_block ); |
479 | 924k | } |
480 | | |
481 | | /**************************************************************************** |
482 | | * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream |
483 | | * Will always use 4 byte 0 0 0 1 startcodes |
484 | | * Will prepend a SPS and PPS before each keyframe |
485 | | ****************************************************************************/ |
486 | | static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block ) |
487 | 0 | { |
488 | 0 | decoder_sys_t *p_sys = p_dec->p_sys; |
489 | |
|
490 | 0 | return PacketizeXXC1( p_dec, p_dec->obj.logger, |
491 | 0 | p_sys->i_avcC_length_size, pp_block, |
492 | 0 | ParseNALBlockW, PacketizeDrain ); |
493 | 0 | } |
494 | | |
495 | | /***************************************************************************** |
496 | | * GetCc: |
497 | | *****************************************************************************/ |
498 | | static block_t *GetCc( decoder_t *p_dec, decoder_cc_desc_t *p_desc ) |
499 | 243k | { |
500 | 243k | decoder_sys_t *p_sys = p_dec->p_sys; |
501 | 243k | return cc_storage_get_current( p_sys->p_ccs, p_desc ); |
502 | 243k | } |
503 | | |
504 | | /**************************************************************************** |
505 | | * Helpers |
506 | | ****************************************************************************/ |
507 | | static void ResetOutputVariables( decoder_sys_t *p_sys ) |
508 | 582k | { |
509 | 582k | p_sys->i_frame_dts = VLC_TICK_INVALID; |
510 | 582k | p_sys->i_frame_pts = VLC_TICK_INVALID; |
511 | 582k | if( p_sys->p_slice ) |
512 | 453k | h264_slice_release( p_sys->p_slice ); |
513 | 582k | p_sys->p_slice = NULL; |
514 | 582k | p_sys->b_new_sps = false; |
515 | 582k | p_sys->b_new_pps = false; |
516 | | /* From SEI */ |
517 | 582k | p_sys->i_dpb_output_delay = 0; |
518 | 582k | p_sys->i_pic_struct = UINT8_MAX; |
519 | 582k | p_sys->i_recovery_frame_cnt = UINT_MAX; |
520 | 582k | } |
521 | | |
522 | | static void PacketizeReset( void *p_private, bool b_flush ) |
523 | 182k | { |
524 | 182k | decoder_t *p_dec = p_private; |
525 | 182k | decoder_sys_t *p_sys = p_dec->p_sys; |
526 | | |
527 | 182k | if( b_flush || !p_sys->p_slice ) |
528 | 128k | { |
529 | 128k | DropStoredNAL( p_sys ); |
530 | 128k | ResetOutputVariables( p_sys ); |
531 | 128k | p_sys->p_active_pps = NULL; |
532 | 128k | p_sys->p_active_sps = NULL; |
533 | 128k | p_sys->b_recovered = false; |
534 | 128k | p_sys->i_recoveryfnum = UINT_MAX; |
535 | | /* POC */ |
536 | 128k | h264_poc_context_init( &p_sys->pocctx ); |
537 | 128k | p_sys->prevdatedpoc.pts = VLC_TICK_INVALID; |
538 | 128k | } |
539 | 182k | p_sys->i_next_block_flags = BLOCK_FLAG_DISCONTINUITY; |
540 | 182k | date_Set( &p_sys->dts, VLC_TICK_INVALID ); |
541 | 182k | } |
542 | | static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block ) |
543 | 2.40M | { |
544 | 2.40M | decoder_t *p_dec = p_private; |
545 | | |
546 | | /* Remove trailing 0 bytes */ |
547 | 26.2M | while( p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 ) |
548 | 23.8M | p_block->i_buffer--; |
549 | | |
550 | 2.40M | return ParseNALBlock( p_dec, pb_ts_used, p_block ); |
551 | 2.40M | } |
552 | | static int PacketizeValidate( void *p_private, block_t *p_au ) |
553 | 399k | { |
554 | 399k | VLC_UNUSED(p_private); |
555 | 399k | VLC_UNUSED(p_au); |
556 | 399k | return VLC_SUCCESS; |
557 | 399k | } |
558 | | |
559 | | static block_t * PacketizeDrain( void *p_private ) |
560 | 127k | { |
561 | 127k | decoder_t *p_dec = p_private; |
562 | 127k | decoder_sys_t *p_sys = p_dec->p_sys; |
563 | | |
564 | 127k | if( !p_sys->p_slice ) |
565 | 115k | return NULL; |
566 | | |
567 | 12.0k | block_t *p_out = OutputPicture( p_dec ); |
568 | 12.0k | if( p_out && (p_out->i_flags & BLOCK_FLAG_DROP) ) |
569 | 1.30k | { |
570 | 1.30k | block_Release( p_out ); |
571 | 1.30k | p_out = NULL; |
572 | 1.30k | } |
573 | | |
574 | 12.0k | return p_out; |
575 | 127k | } |
576 | | |
577 | | /***************************************************************************** |
578 | | * ParseNALBlock: parses annexB type NALs |
579 | | * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode |
580 | | *****************************************************************************/ |
581 | | static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag ) |
582 | 2.40M | { |
583 | 2.40M | decoder_sys_t *p_sys = p_dec->p_sys; |
584 | 2.40M | block_t *p_pic = NULL; |
585 | | |
586 | 2.40M | const enum h264_nal_unit_type_e i_nal_type = h264_getNALType( &p_frag->p_buffer[4] ); |
587 | 2.40M | const vlc_tick_t i_frag_dts = p_frag->i_dts; |
588 | 2.40M | const vlc_tick_t i_frag_pts = p_frag->i_pts; |
589 | 2.40M | bool b_au_end = p_frag->i_flags & BLOCK_FLAG_AU_END; |
590 | 2.40M | p_frag->i_flags &= ~BLOCK_FLAG_AU_END; |
591 | | |
592 | 2.40M | if( p_sys->p_slice && (!p_sys->p_active_pps || !p_sys->p_active_sps) ) |
593 | 30.4k | { |
594 | 30.4k | msg_Warn( p_dec, "waiting for SPS/PPS" ); |
595 | | |
596 | | /* Reset context */ |
597 | 30.4k | DropStoredNAL( p_sys ); |
598 | 30.4k | ResetOutputVariables( p_sys ); |
599 | 30.4k | cc_storage_reset( p_sys->p_ccs ); |
600 | 30.4k | } |
601 | | |
602 | 2.40M | switch( i_nal_type ) |
603 | 2.40M | { |
604 | | /*** Slices ***/ |
605 | 268k | case H264_NAL_SLICE: |
606 | 305k | case H264_NAL_SLICE_DPA: |
607 | 557k | case H264_NAL_SLICE_DPB: |
608 | 599k | case H264_NAL_SLICE_DPC: |
609 | 890k | case H264_NAL_SLICE_IDR: |
610 | 890k | { |
611 | 890k | h264_slice_t *p_newslice; |
612 | | |
613 | 890k | if( i_nal_type == H264_NAL_SLICE_IDR ) |
614 | 291k | { |
615 | 291k | p_sys->b_recovered = true; |
616 | 291k | p_sys->i_recovery_frame_cnt = UINT_MAX; |
617 | 291k | p_sys->i_recoveryfnum = UINT_MAX; |
618 | 291k | } |
619 | | |
620 | 890k | if( (p_newslice = ParseSliceHeader( p_dec, p_frag )) ) |
621 | 500k | { |
622 | | /* Only IDR carries the id, to be propagated */ |
623 | 500k | h264_slice_copy_idr_id( p_sys->p_slice, p_newslice ); |
624 | | |
625 | 500k | bool b_new_picture = h264_IsFirstVCLNALUnit( p_sys->p_slice, p_newslice ); |
626 | 500k | if( b_new_picture ) |
627 | 453k | { |
628 | | /* Parse SEI for that frame now we should have matched SPS/PPS */ |
629 | 816k | for( block_t *p_sei = p_sys->leading.p_head; p_sei; p_sei = p_sei->p_next ) |
630 | 363k | { |
631 | 363k | if( (p_sei->i_flags & BLOCK_FLAG_PRIVATE_SEI) == 0 ) |
632 | 59.9k | continue; |
633 | 303k | HxxxParse_AnnexB_SEI( p_sei->p_buffer, p_sei->i_buffer, |
634 | 303k | 1 /* nal header */, ParseSeiCallback, p_dec ); |
635 | 303k | } |
636 | | |
637 | 453k | if( p_sys->p_slice ) |
638 | 253k | p_pic = OutputPicture( p_dec ); |
639 | 453k | } |
640 | | |
641 | | /* */ |
642 | 500k | h264_slice_release( p_sys->p_slice ); |
643 | 500k | p_sys->p_slice = p_newslice; |
644 | 500k | } |
645 | 390k | else |
646 | 390k | { |
647 | 390k | p_sys->p_active_pps = NULL; |
648 | | /* Fragment will be discarded later on */ |
649 | 390k | } |
650 | | |
651 | 890k | block_ChainLastAppend( &p_sys->frame.pp_append, p_frag ); |
652 | 890k | } break; |
653 | | |
654 | | /*** Prefix NALs ***/ |
655 | | |
656 | 17.7k | case H264_NAL_AU_DELIMITER: |
657 | 17.7k | if( p_sys->p_slice ) |
658 | 9.04k | p_pic = OutputPicture( p_dec ); |
659 | | |
660 | | /* clear junk if no pic, we're always the first nal */ |
661 | 17.7k | DropStoredNAL( p_sys ); |
662 | | |
663 | 17.7k | p_frag->i_flags |= BLOCK_FLAG_PRIVATE_AUD; |
664 | | |
665 | 17.7k | block_ChainLastAppend( &p_sys->leading.pp_append, p_frag ); |
666 | 17.7k | break; |
667 | | |
668 | 270k | case H264_NAL_SPS: |
669 | 539k | case H264_NAL_PPS: |
670 | 539k | if( p_sys->p_slice ) |
671 | 116k | p_pic = OutputPicture( p_dec ); |
672 | | |
673 | | /* Stored for insert on keyframes */ |
674 | 539k | if( i_nal_type == H264_NAL_SPS ) |
675 | 270k | p_sys->b_new_sps |= PutXPS( p_dec, i_nal_type, p_frag ); |
676 | 269k | else |
677 | 269k | p_sys->b_new_pps |= PutXPS( p_dec, i_nal_type, p_frag ); |
678 | 539k | break; |
679 | | |
680 | 227k | case H264_NAL_SEI: |
681 | 227k | if( p_sys->p_slice ) |
682 | 20.8k | p_pic = OutputPicture( p_dec ); |
683 | | |
684 | 227k | p_frag->i_flags |= BLOCK_FLAG_PRIVATE_SEI; |
685 | 227k | block_ChainLastAppend( &p_sys->leading.pp_append, p_frag ); |
686 | 227k | break; |
687 | | |
688 | 24.3k | case H264_NAL_SPS_EXT: |
689 | 24.3k | PutXPS( p_dec, i_nal_type, p_frag ); |
690 | 24.3k | if( p_sys->p_slice ) |
691 | 1.42k | p_pic = OutputPicture( p_dec ); |
692 | 24.3k | break; |
693 | | |
694 | 8.75k | case H264_NAL_PREFIX: /* first slice/VCL associated data */ |
695 | 18.7k | case H264_NAL_SUBSET_SPS: |
696 | 31.3k | case H264_NAL_DEPTH_PS: |
697 | 41.2k | case H264_NAL_RESERVED_17: |
698 | 46.9k | case H264_NAL_RESERVED_18: |
699 | 46.9k | if( p_sys->p_slice ) |
700 | 6.30k | p_pic = OutputPicture( p_dec ); |
701 | | |
702 | 46.9k | block_ChainLastAppend( &p_sys->leading.pp_append, p_frag ); |
703 | 46.9k | break; |
704 | | |
705 | | /*** Suffix NALs ***/ |
706 | | |
707 | 3.24k | case H264_NAL_END_OF_SEQ: |
708 | 24.1k | case H264_NAL_END_OF_STREAM: |
709 | | /* Early end of packetization */ |
710 | 24.1k | block_ChainLastAppend( &p_sys->frame.pp_append, p_frag ); |
711 | | |
712 | | /* important for still pictures/menus */ |
713 | 24.1k | p_sys->i_next_block_flags |= BLOCK_FLAG_END_OF_SEQUENCE; |
714 | 24.1k | if( p_sys->p_slice ) |
715 | 3.74k | p_pic = OutputPicture( p_dec ); |
716 | 24.1k | break; |
717 | | |
718 | 4.33k | case H264_NAL_SLICE_WP: // post |
719 | 517k | case H264_NAL_UNKNOWN: |
720 | 524k | case H264_NAL_FILLER_DATA: |
721 | 525k | case H264_NAL_SLICE_EXT: |
722 | 528k | case H264_NAL_SLICE_3D_EXT: |
723 | 537k | case H264_NAL_RESERVED_22: |
724 | 538k | case H264_NAL_RESERVED_23: |
725 | 630k | default: /* others 24..31, including unknown */ |
726 | 630k | block_ChainLastAppend( &p_sys->frame.pp_append, p_frag ); |
727 | 630k | break; |
728 | 2.40M | } |
729 | | |
730 | 2.40M | *pb_ts_used = false; |
731 | 2.40M | if( p_sys->i_frame_dts == VLC_TICK_INVALID && |
732 | 1.37M | p_sys->i_frame_pts == VLC_TICK_INVALID ) |
733 | 1.22M | { |
734 | 1.22M | p_sys->i_frame_dts = i_frag_dts; |
735 | 1.22M | p_sys->i_frame_pts = i_frag_pts; |
736 | 1.22M | *pb_ts_used = true; |
737 | 1.22M | if( i_frag_dts != VLC_TICK_INVALID ) |
738 | 307k | date_Set( &p_sys->dts, i_frag_dts ); |
739 | 1.22M | } |
740 | | |
741 | 2.40M | if( p_sys->p_slice && b_au_end && !p_pic ) |
742 | 0 | { |
743 | 0 | p_pic = OutputPicture( p_dec ); |
744 | 0 | } |
745 | | |
746 | 2.40M | if( p_pic && (p_pic->i_flags & BLOCK_FLAG_DROP) ) |
747 | 21.5k | { |
748 | 21.5k | block_Release( p_pic ); |
749 | 21.5k | p_pic = NULL; |
750 | 21.5k | } |
751 | | |
752 | 2.40M | return p_pic; |
753 | 2.40M | } |
754 | | |
755 | | static block_t *OutputPicture( decoder_t *p_dec ) |
756 | 423k | { |
757 | 423k | decoder_sys_t *p_sys = p_dec->p_sys; |
758 | 423k | block_t *p_pic = NULL; |
759 | 423k | block_t **pp_pic_last = &p_pic; |
760 | | |
761 | 423k | if( unlikely(!p_sys->frame.p_head) ) |
762 | 0 | { |
763 | 0 | assert( p_sys->frame.p_head ); |
764 | 0 | DropStoredNAL( p_sys ); |
765 | 0 | ResetOutputVariables( p_sys ); |
766 | 0 | cc_storage_reset( p_sys->p_ccs ); |
767 | 0 | return NULL; |
768 | 0 | } |
769 | | |
770 | | /* Bind matched/referred PPS and SPS */ |
771 | 423k | const h264_picture_parameter_set_t *p_pps = p_sys->p_active_pps; |
772 | 423k | const h264_sequence_parameter_set_t *p_sps = p_sys->p_active_sps; |
773 | 423k | if( !p_pps || !p_sps ) |
774 | 466 | { |
775 | 466 | DropStoredNAL( p_sys ); |
776 | 466 | ResetOutputVariables( p_sys ); |
777 | 466 | cc_storage_reset( p_sys->p_ccs ); |
778 | 466 | return NULL; |
779 | 466 | } |
780 | | |
781 | 422k | if( !p_sys->b_recovered && p_sys->i_recoveryfnum == UINT_MAX && |
782 | 28.5k | p_sys->i_recovery_frame_cnt == UINT_MAX && h264_get_slice_type(p_sys->p_slice) == H264_SLICE_TYPE_I ) |
783 | 2.98k | { |
784 | | /* No way to recover using SEI, just sync on I Slice */ |
785 | 2.98k | p_sys->b_recovered = true; |
786 | 2.98k | } |
787 | | |
788 | 422k | bool b_need_sps_pps = h264_get_slice_type(p_sys->p_slice) == H264_SLICE_TYPE_I && |
789 | 136k | p_sys->p_active_pps && p_sys->p_active_sps; |
790 | | |
791 | 422k | const unsigned i_frame_num = h264_get_frame_num(p_sys->p_slice); |
792 | | |
793 | | /* Handle SEI recovery */ |
794 | 422k | if ( !p_sys->b_recovered && p_sys->i_recovery_frame_cnt != UINT_MAX && |
795 | 5.22k | p_sys->i_recoveryfnum == UINT_MAX ) |
796 | 2.88k | { |
797 | 2.88k | p_sys->i_recoveryfnum = i_frame_num + p_sys->i_recovery_frame_cnt; |
798 | 2.88k | p_sys->i_recoverystartfnum = i_frame_num; |
799 | 2.88k | b_need_sps_pps = true; /* SPS/PPS must be inserted for SEI recovery */ |
800 | 2.88k | msg_Dbg( p_dec, "Recovering using SEI, prerolling %u reference pics", p_sys->i_recovery_frame_cnt ); |
801 | 2.88k | } |
802 | | |
803 | 422k | if( p_sys->i_recoveryfnum != UINT_MAX ) |
804 | 12.5k | { |
805 | 12.5k | assert(p_sys->b_recovered == false); |
806 | 12.5k | const unsigned maxFrameNum = h264_get_max_frame_num( p_sps ); |
807 | | |
808 | 12.5k | if( ( p_sys->i_recoveryfnum > maxFrameNum && |
809 | 10.9k | i_frame_num < p_sys->i_recoverystartfnum && |
810 | 3.31k | i_frame_num >= p_sys->i_recoveryfnum % maxFrameNum ) || |
811 | 12.3k | ( p_sys->i_recoveryfnum <= maxFrameNum && |
812 | 1.58k | i_frame_num >= p_sys->i_recoveryfnum ) ) |
813 | 686 | { |
814 | 686 | p_sys->i_recoveryfnum = UINT_MAX; |
815 | 686 | p_sys->b_recovered = true; |
816 | 686 | msg_Dbg( p_dec, "Recovery from SEI recovery point complete" ); |
817 | 686 | } |
818 | 12.5k | } |
819 | | |
820 | | /* Gather PPS/SPS if required */ |
821 | 422k | block_t *p_xpsnal = GatherSets( p_sys, b_need_sps_pps|p_sys->b_new_sps, |
822 | 422k | b_need_sps_pps|p_sys->b_new_pps ); |
823 | | |
824 | | /* Now rebuild NAL Sequence, inserting PPS/SPS if any */ |
825 | 422k | if( p_sys->leading.p_head && |
826 | 60.4k | (p_sys->leading.p_head->i_flags & BLOCK_FLAG_PRIVATE_AUD) ) |
827 | 11.3k | { |
828 | 11.3k | block_t *p_au = p_sys->leading.p_head; |
829 | 11.3k | p_sys->leading.p_head = p_au->p_next; |
830 | 11.3k | p_au->p_next = NULL; |
831 | 11.3k | block_ChainLastAppend( &pp_pic_last, p_au ); |
832 | 11.3k | } |
833 | | |
834 | 422k | if( p_xpsnal ) |
835 | 172k | block_ChainLastAppend( &pp_pic_last, p_xpsnal ); |
836 | | |
837 | 422k | if( p_sys->leading.p_head ) |
838 | 58.4k | block_ChainLastAppend( &pp_pic_last, p_sys->leading.p_head ); |
839 | | |
840 | 422k | assert( p_sys->frame.p_head ); |
841 | 422k | if( p_sys->frame.p_head ) |
842 | 422k | block_ChainLastAppend( &pp_pic_last, p_sys->frame.p_head ); |
843 | | |
844 | | /* Reset chains, now empty */ |
845 | 422k | p_sys->frame.p_head = NULL; |
846 | 422k | p_sys->frame.pp_append = &p_sys->frame.p_head; |
847 | 422k | p_sys->leading.p_head = NULL; |
848 | 422k | p_sys->leading.pp_append = &p_sys->leading.p_head; |
849 | | |
850 | 422k | p_pic = block_ChainGather( p_pic ); |
851 | | |
852 | 422k | if( !p_pic ) |
853 | 0 | { |
854 | 0 | ResetOutputVariables( p_sys ); |
855 | 0 | cc_storage_reset( p_sys->p_ccs ); |
856 | 0 | return NULL; |
857 | 0 | } |
858 | | |
859 | | /* clear up flags gathered */ |
860 | 422k | p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_MASK; |
861 | | |
862 | | /* for PTS Fixup, interlaced fields (multiple AU/block) */ |
863 | 422k | int tFOC = 0, bFOC = 0, PictureOrderCount = 0; |
864 | 422k | h264_compute_poc( p_sps, p_sys->p_slice, &p_sys->pocctx, &PictureOrderCount, &tFOC, &bFOC ); |
865 | | |
866 | 422k | unsigned i_num_clock_ts = h264_get_num_ts( p_sps, p_sys->p_slice, p_sys->i_pic_struct, tFOC, bFOC ); |
867 | | |
868 | 422k | if( !h264_is_frames_only( p_sps ) && p_sys->i_pic_struct != UINT8_MAX ) |
869 | 4.84k | { |
870 | 4.84k | switch( p_sys->i_pic_struct ) |
871 | 4.84k | { |
872 | | /* Top and Bottom field slices */ |
873 | 474 | case 1: |
874 | 713 | case 2: |
875 | 713 | p_pic->i_flags |= BLOCK_FLAG_SINGLE_FIELD; |
876 | 713 | p_pic->i_flags |= h264_slice_top_field(p_sys->p_slice) ? BLOCK_FLAG_TOP_FIELD_FIRST |
877 | 713 | : BLOCK_FLAG_BOTTOM_FIELD_FIRST; |
878 | 713 | break; |
879 | | /* Each of the following slices contains multiple fields */ |
880 | 705 | case 3: |
881 | 705 | p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST; |
882 | 705 | break; |
883 | 550 | case 4: |
884 | 550 | p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST; |
885 | 550 | break; |
886 | 260 | case 5: |
887 | 260 | p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST; |
888 | 260 | break; |
889 | 427 | case 6: |
890 | 427 | p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST; |
891 | 427 | break; |
892 | 2.19k | default: |
893 | 2.19k | break; |
894 | 4.84k | } |
895 | 4.84k | } |
896 | | |
897 | | /* set dts/pts to current block timestamps */ |
898 | 422k | p_pic->i_dts = p_sys->i_frame_dts; |
899 | 422k | p_pic->i_pts = p_sys->i_frame_pts; |
900 | | |
901 | | /* Fixup missing timestamps after split (multiple AU/block)*/ |
902 | 422k | if( p_pic->i_dts == VLC_TICK_INVALID ) |
903 | 157k | p_pic->i_dts = date_Get( &p_sys->dts ); |
904 | | |
905 | 422k | if( h264_get_slice_type( p_sys->p_slice ) == H264_SLICE_TYPE_I ) |
906 | 136k | p_sys->prevdatedpoc.pts = VLC_TICK_INVALID; |
907 | | |
908 | 422k | if( p_pic->i_pts == VLC_TICK_INVALID ) |
909 | 267k | { |
910 | 267k | if( p_sys->prevdatedpoc.pts != VLC_TICK_INVALID && |
911 | 113k | date_Get( &p_sys->dts ) != VLC_TICK_INVALID ) |
912 | 109k | { |
913 | 109k | date_t pts = p_sys->dts; |
914 | 109k | date_Set( &pts, p_sys->prevdatedpoc.pts ); |
915 | | |
916 | 109k | int diff = tFOC - p_sys->prevdatedpoc.num; |
917 | 109k | if( diff > 0 ) |
918 | 28.5k | date_Increment( &pts, diff ); |
919 | 80.8k | else |
920 | 80.8k | date_Decrement( &pts, -diff ); |
921 | | |
922 | 109k | p_pic->i_pts = date_Get( &pts ); |
923 | | /* non monotonically increasing dts on some videos 33333 33333...35000 */ |
924 | 109k | if( p_pic->i_pts < p_pic->i_dts ) |
925 | 54.7k | p_pic->i_pts = p_pic->i_dts; |
926 | 109k | } |
927 | | /* In case there's no PTS at all */ |
928 | 157k | else if( h264_CanSwapPTSWithDTS( p_sys->p_slice, p_sps ) ) |
929 | 34.1k | { |
930 | 34.1k | p_pic->i_pts = p_pic->i_dts; |
931 | 34.1k | } |
932 | 123k | else if( h264_get_slice_type( p_sys->p_slice ) == H264_SLICE_TYPE_I && |
933 | 43.7k | date_Get( &p_sys->dts ) != VLC_TICK_INVALID ) |
934 | 33.8k | { |
935 | | /* Hell no PTS on IDR. We're totally blind */ |
936 | 33.8k | date_t pts = p_sys->dts; |
937 | 33.8k | date_Increment( &pts, 2 ); |
938 | 33.8k | p_pic->i_pts = date_Get( &pts ); |
939 | 33.8k | } |
940 | 267k | } |
941 | 155k | else if( p_pic->i_dts == VLC_TICK_INVALID && |
942 | 30.3k | h264_CanSwapPTSWithDTS( p_sys->p_slice, p_sps ) ) |
943 | 13.5k | { |
944 | 13.5k | p_pic->i_dts = p_pic->i_pts; |
945 | 13.5k | if( date_Get( &p_sys->dts ) == VLC_TICK_INVALID ) |
946 | 13.5k | date_Set( &p_sys->dts, p_pic->i_pts ); |
947 | 13.5k | } |
948 | | |
949 | 422k | if( p_pic->i_pts != VLC_TICK_INVALID ) |
950 | 322k | { |
951 | 322k | p_sys->prevdatedpoc.pts = p_pic->i_pts; |
952 | 322k | p_sys->prevdatedpoc.num = PictureOrderCount; |
953 | 322k | } |
954 | | |
955 | 422k | if( p_pic->i_length == 0 ) |
956 | 422k | { |
957 | 422k | date_t next = p_sys->dts; |
958 | 422k | date_Increment( &next, i_num_clock_ts ); |
959 | 422k | p_pic->i_length = date_Get( &next ) - date_Get( &p_sys->dts ); |
960 | 422k | } |
961 | | |
962 | | #if 0 |
963 | | msg_Err(p_dec, "F/BOC %d/%d POC %d %d rec %d flags %x ref%d fn %d fp %d %ld pts %ld len %ld", |
964 | | tFOC, bFOC, PictureOrderCount, |
965 | | h264_get_slice_type(p_sys->p_slice), p_sys->b_recovered, p_pic->i_flags, |
966 | | h264_get_nal_ref_idc(p_sys->p_slice), h264_get_frame_num(p_sys->p_slice), |
967 | | h264_is_field_pic(p_sys->p_slice), |
968 | | p_pic->i_pts - p_pic->i_dts, p_pic->i_pts % VLC_TICK_FROM_SEC(100), p_pic->i_length); |
969 | | #endif |
970 | | |
971 | | /* save for next pic fixups */ |
972 | 422k | if( date_Get( &p_sys->dts ) != VLC_TICK_INVALID ) |
973 | 376k | { |
974 | 376k | if( p_sys->i_next_block_flags & BLOCK_FLAG_DISCONTINUITY ) |
975 | 14.5k | date_Set( &p_sys->dts, VLC_TICK_INVALID ); |
976 | 361k | else |
977 | 361k | date_Increment( &p_sys->dts, i_num_clock_ts ); |
978 | 376k | } |
979 | | |
980 | 422k | if( p_pic ) |
981 | 422k | { |
982 | 422k | p_pic->i_flags |= p_sys->i_next_block_flags; |
983 | 422k | p_sys->i_next_block_flags = 0; |
984 | 422k | } |
985 | | |
986 | 422k | switch( h264_get_slice_type( p_sys->p_slice ) ) |
987 | 422k | { |
988 | 241k | case H264_SLICE_TYPE_P: |
989 | 241k | p_pic->i_flags |= BLOCK_FLAG_TYPE_P; |
990 | 241k | break; |
991 | 28.9k | case H264_SLICE_TYPE_B: |
992 | 28.9k | p_pic->i_flags |= BLOCK_FLAG_TYPE_B; |
993 | 28.9k | break; |
994 | 136k | case H264_SLICE_TYPE_I: |
995 | 136k | p_pic->i_flags |= BLOCK_FLAG_TYPE_I; |
996 | 152k | default: |
997 | 152k | break; |
998 | 422k | } |
999 | | |
1000 | 422k | if( !p_sys->b_recovered ) |
1001 | 34.7k | { |
1002 | 34.7k | if( p_sys->i_recoveryfnum != UINT_MAX ) /* recovering from SEI */ |
1003 | 11.8k | p_pic->i_flags |= BLOCK_FLAG_PREROLL; |
1004 | 22.8k | else |
1005 | 22.8k | p_pic->i_flags |= BLOCK_FLAG_DROP; |
1006 | 34.7k | } |
1007 | | |
1008 | 422k | p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD; |
1009 | | |
1010 | | /* reset after output */ |
1011 | 422k | ResetOutputVariables( p_sys ); |
1012 | | |
1013 | | /* CC */ |
1014 | 422k | cc_storage_commit( p_sys->p_ccs, p_pic ); |
1015 | | |
1016 | 422k | return p_pic; |
1017 | 422k | } |
1018 | | |
1019 | | static int CmpXPS( const block_t *p_ref, const block_t *p_nal ) |
1020 | 542k | { |
1021 | 542k | return p_ref == NULL || |
1022 | 436k | p_ref->i_buffer != p_nal->i_buffer || |
1023 | 256k | memcmp( p_ref->p_buffer, p_nal->p_buffer, p_nal->i_buffer ); |
1024 | 542k | } |
1025 | | |
1026 | | #define wrap_h264_xps_decode(funcname ) \ |
1027 | | static void *funcname ## _wrapper ( const uint8_t *a, size_t b, bool c ) \ |
1028 | 295k | { return funcname(a,b,c); }h264.c:h264_decode_sps_wrapper Line | Count | Source | 1028 | 154k | { return funcname(a,b,c); } |
h264.c:h264_decode_pps_wrapper Line | Count | Source | 1028 | 141k | { return funcname(a,b,c); } |
|
1029 | | |
1030 | | wrap_h264_xps_decode(h264_decode_sps) |
1031 | | wrap_h264_xps_decode(h264_decode_pps) |
1032 | | |
1033 | | #define wrap_h264_xps_release(funcname, typecast) \ |
1034 | 137k | static void funcname ## _wrapper ( void *a ) { funcname((typecast *)a); }h264.c:h264_release_sps_wrapper Line | Count | Source | 1034 | 41.6k | static void funcname ## _wrapper ( void *a ) { funcname((typecast *)a); } |
h264.c:h264_release_pps_wrapper Line | Count | Source | 1034 | 95.9k | static void funcname ## _wrapper ( void *a ) { funcname((typecast *)a); } |
|
1035 | | |
1036 | | wrap_h264_xps_release(h264_release_sps, h264_sequence_parameter_set_t) |
1037 | | wrap_h264_xps_release(h264_release_pps, h264_picture_parameter_set_t) |
1038 | | |
1039 | | static void ReleaseXPS( decoder_sys_t *p_sys ) |
1040 | 73.0k | { |
1041 | 2.41M | for( int i = 0; i < H264_MAX_NUM_SPS; i++ ) |
1042 | 2.33M | { |
1043 | 2.33M | if( !p_sys->sps[i].p_block ) |
1044 | 2.31M | continue; |
1045 | 22.1k | block_Release( p_sys->sps[i].p_block ); |
1046 | 22.1k | h264_release_sps( p_sys->sps[i].p_sps ); |
1047 | 22.1k | } |
1048 | 18.7M | for( int i = 0; i < H264_MAX_NUM_PPS; i++ ) |
1049 | 18.7M | { |
1050 | 18.7M | if( !p_sys->pps[i].p_block ) |
1051 | 18.6M | continue; |
1052 | 24.1k | block_Release( p_sys->pps[i].p_block ); |
1053 | 24.1k | h264_release_pps( p_sys->pps[i].p_pps ); |
1054 | 24.1k | } |
1055 | 2.41M | for( int i = 0; i < H264_MAX_NUM_SPSEXT; i++ ) |
1056 | 2.33M | { |
1057 | 2.33M | if( p_sys->spsext[i].p_block ) |
1058 | 2.25k | block_Release( p_sys->spsext[i].p_block ); |
1059 | 2.33M | } |
1060 | 73.0k | p_sys->p_active_sps = NULL; |
1061 | 73.0k | p_sys->p_active_pps = NULL; |
1062 | 73.0k | } |
1063 | | |
1064 | | static bool PutXPS( decoder_t *p_dec, uint8_t i_nal_type, block_t *p_frag ) |
1065 | 564k | { |
1066 | 564k | decoder_sys_t *p_sys = p_dec->p_sys; |
1067 | | |
1068 | 564k | uint8_t i_id; |
1069 | 564k | if( !hxxx_strip_AnnexB_startcode( (const uint8_t **)&p_frag->p_buffer, |
1070 | 564k | &p_frag->i_buffer ) || |
1071 | 564k | !h264_get_xps_id( p_frag->p_buffer, p_frag->i_buffer, &i_id ) ) |
1072 | 22.0k | { |
1073 | 22.0k | block_Release( p_frag ); |
1074 | 22.0k | return false; |
1075 | 22.0k | } |
1076 | | |
1077 | 542k | const char * rgsz_types[3] = {"SPS", "PPS", "SPSEXT"}; |
1078 | 542k | const char *psz_type; |
1079 | 542k | block_t **pp_block_dst; |
1080 | | /* all depend on pp_xps_dst */ |
1081 | 542k | void **pp_xps_dst = NULL; |
1082 | 542k | const void **pp_active = NULL; /* optional */ |
1083 | 542k | void * (* pf_decode_xps)(const uint8_t *, size_t, bool) = NULL; |
1084 | 542k | void (* pf_release_xps)(void *) = NULL; |
1085 | | |
1086 | 542k | switch( i_nal_type ) |
1087 | 542k | { |
1088 | 259k | case H264_NAL_SPS: |
1089 | 259k | psz_type = rgsz_types[0]; |
1090 | 259k | pp_active = (const void **) &p_sys->p_active_sps; |
1091 | 259k | pp_block_dst = &p_sys->sps[i_id].p_block; |
1092 | 259k | pp_xps_dst = (void **) &p_sys->sps[i_id].p_sps; |
1093 | 259k | pf_decode_xps = h264_decode_sps_wrapper; |
1094 | 259k | pf_release_xps = h264_release_sps_wrapper; |
1095 | 259k | break; |
1096 | 265k | case H264_NAL_PPS: |
1097 | 265k | psz_type = rgsz_types[1]; |
1098 | 265k | pp_active = (const void **) &p_sys->p_active_pps; |
1099 | 265k | pp_block_dst = &p_sys->pps[i_id].p_block; |
1100 | 265k | pp_xps_dst = (void **) &p_sys->pps[i_id].p_pps; |
1101 | 265k | pf_decode_xps = h264_decode_pps_wrapper; |
1102 | 265k | pf_release_xps = h264_release_pps_wrapper; |
1103 | 265k | break; |
1104 | 16.6k | case H264_NAL_SPS_EXT: |
1105 | 16.6k | psz_type = rgsz_types[2]; |
1106 | 16.6k | pp_block_dst = &p_sys->spsext[i_id].p_block; |
1107 | 16.6k | break; |
1108 | 0 | default: |
1109 | 0 | block_Release( p_frag ); |
1110 | 0 | return false; |
1111 | 542k | } |
1112 | | |
1113 | 542k | if( !CmpXPS( *pp_block_dst, p_frag ) ) |
1114 | 237k | { |
1115 | 237k | block_Release( p_frag ); |
1116 | 237k | return false; |
1117 | 237k | } |
1118 | | |
1119 | 304k | msg_Dbg( p_dec, "found NAL_%s (id=%" PRIu8 ")", psz_type, i_id ); |
1120 | | |
1121 | 304k | if( pp_xps_dst != NULL ) |
1122 | 295k | { |
1123 | 295k | void *p_xps = pf_decode_xps( p_frag->p_buffer, p_frag->i_buffer, true ); |
1124 | 295k | if( !p_xps ) |
1125 | 111k | { |
1126 | 111k | block_Release( p_frag ); |
1127 | 111k | return false; |
1128 | 111k | } |
1129 | 183k | if( *pp_xps_dst ) |
1130 | 137k | { |
1131 | 137k | if( pp_active && *pp_active == *pp_xps_dst ) |
1132 | 30.5k | *pp_active = NULL; |
1133 | 137k | pf_release_xps( *pp_xps_dst ); |
1134 | 137k | } |
1135 | 183k | *pp_xps_dst = p_xps; |
1136 | 183k | } |
1137 | | |
1138 | 192k | if( *pp_block_dst ) |
1139 | 144k | block_Release( *pp_block_dst ); |
1140 | 192k | *pp_block_dst = p_frag; |
1141 | | |
1142 | 192k | return true; |
1143 | 304k | } |
1144 | | |
1145 | | static void GetSPSPPS( uint8_t i_pps_id, void *priv, |
1146 | | const h264_sequence_parameter_set_t **pp_sps, |
1147 | | const h264_picture_parameter_set_t **pp_pps ) |
1148 | 1.19M | { |
1149 | 1.19M | decoder_sys_t *p_sys = priv; |
1150 | | |
1151 | 1.19M | *pp_pps = p_sys->pps[i_pps_id].p_pps; |
1152 | 1.19M | if( *pp_pps == NULL ) |
1153 | 40.1k | *pp_sps = NULL; |
1154 | 1.15M | else |
1155 | 1.15M | *pp_sps = p_sys->sps[h264_get_pps_sps_id(*pp_pps)].p_sps; |
1156 | 1.19M | } |
1157 | | |
1158 | | static h264_slice_t * ParseSliceHeader( decoder_t *p_dec, const block_t *p_frag ) |
1159 | 890k | { |
1160 | 890k | decoder_sys_t *p_sys = p_dec->p_sys; |
1161 | | |
1162 | 890k | const uint8_t *p_stripped = p_frag->p_buffer; |
1163 | 890k | size_t i_stripped = p_frag->i_buffer; |
1164 | | |
1165 | 890k | if( !hxxx_strip_AnnexB_startcode( &p_stripped, &i_stripped ) || i_stripped < 2 ) |
1166 | 142k | return NULL; |
1167 | | |
1168 | 748k | h264_slice_t *p_slice = h264_decode_slice( p_stripped, i_stripped, GetSPSPPS, p_sys ); |
1169 | 748k | if( !p_slice ) |
1170 | 248k | return NULL; |
1171 | | |
1172 | 500k | const h264_sequence_parameter_set_t *p_sps; |
1173 | 500k | const h264_picture_parameter_set_t *p_pps; |
1174 | 500k | GetSPSPPS( h264_get_slice_pps_id( p_slice ), p_sys, &p_sps, &p_pps ); |
1175 | 500k | if( unlikely( !p_sps || !p_pps) ) |
1176 | 0 | { |
1177 | 0 | h264_slice_release( p_slice ); |
1178 | 0 | return NULL; |
1179 | 0 | } |
1180 | | |
1181 | 500k | ActivateSets( p_dec, p_sps, p_pps ); |
1182 | | |
1183 | 500k | return p_slice; |
1184 | 500k | } |
1185 | | |
1186 | | static bool ParseSeiCallback( const hxxx_sei_data_t *p_sei_data, void *cbdata ) |
1187 | 133k | { |
1188 | 133k | decoder_t *p_dec = (decoder_t *) cbdata; |
1189 | 133k | decoder_sys_t *p_sys = p_dec->p_sys; |
1190 | | |
1191 | 133k | switch( p_sei_data->i_type ) |
1192 | 133k | { |
1193 | | /* Look for pic timing */ |
1194 | 50.2k | case HXXX_SEI_PIC_TIMING: |
1195 | 50.2k | { |
1196 | 50.2k | const h264_sequence_parameter_set_t *p_sps = p_sys->p_active_sps; |
1197 | 50.2k | if( unlikely( p_sps == NULL ) ) |
1198 | 0 | { |
1199 | 0 | assert( p_sps ); |
1200 | 0 | break; |
1201 | 0 | } |
1202 | | |
1203 | 50.2k | h264_decode_sei_pic_timing( p_sei_data->p_bs, p_sps, |
1204 | 50.2k | &p_sys->i_pic_struct, |
1205 | 50.2k | &p_sys->i_dpb_output_delay ); |
1206 | 50.2k | } break; |
1207 | | |
1208 | | /* Look for user_data_registered_itu_t_t35 */ |
1209 | 37.2k | case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35: |
1210 | 37.2k | { |
1211 | 37.2k | if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC ) |
1212 | 37.2k | { |
1213 | 37.2k | cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data, |
1214 | 37.2k | p_sei_data->itu_t35.u.cc.i_data ); |
1215 | 37.2k | } |
1216 | 37.2k | } break; |
1217 | | |
1218 | 0 | case HXXX_SEI_FRAME_PACKING_ARRANGEMENT: |
1219 | 0 | { |
1220 | 0 | if( p_dec->fmt_in->video.multiview_mode == MULTIVIEW_2D ) |
1221 | 0 | { |
1222 | 0 | video_multiview_mode_t mode; |
1223 | 0 | switch( p_sei_data->frame_packing.type ) |
1224 | 0 | { |
1225 | 0 | case FRAME_PACKING_INTERLEAVED_CHECKERBOARD: |
1226 | 0 | mode = MULTIVIEW_STEREO_CHECKERBOARD; break; |
1227 | 0 | case FRAME_PACKING_INTERLEAVED_COLUMN: |
1228 | 0 | mode = MULTIVIEW_STEREO_COL; break; |
1229 | 0 | case FRAME_PACKING_INTERLEAVED_ROW: |
1230 | 0 | mode = MULTIVIEW_STEREO_ROW; break; |
1231 | 0 | case FRAME_PACKING_SIDE_BY_SIDE: |
1232 | 0 | mode = MULTIVIEW_STEREO_SBS; break; |
1233 | 0 | case FRAME_PACKING_TOP_BOTTOM: |
1234 | 0 | mode = MULTIVIEW_STEREO_TB; break; |
1235 | 0 | case FRAME_PACKING_TEMPORAL: |
1236 | 0 | mode = MULTIVIEW_STEREO_FRAME; break; |
1237 | 0 | case FRAME_PACKING_TILED: |
1238 | 0 | default: |
1239 | 0 | mode = MULTIVIEW_2D; break; |
1240 | 0 | } |
1241 | 0 | p_dec->fmt_out.video.multiview_mode = mode; |
1242 | 0 | } |
1243 | 0 | } break; |
1244 | | |
1245 | | /* Look for SEI recovery point */ |
1246 | 38.2k | case HXXX_SEI_RECOVERY_POINT: |
1247 | 38.2k | { |
1248 | 38.2k | h264_sei_recovery_point_t reco; |
1249 | 38.2k | if( !p_sys->b_recovered && |
1250 | 35.3k | h264_decode_sei_recovery_point( p_sei_data->p_bs, &reco ) ) |
1251 | 35.3k | { |
1252 | 35.3k | msg_Dbg( p_dec, "Seen SEI recovery point, %u recovery frames", reco.i_frames ); |
1253 | 35.3k | p_sys->i_recovery_frame_cnt = reco.i_frames; |
1254 | 35.3k | } |
1255 | 38.2k | } break; |
1256 | | |
1257 | 7.62k | default: |
1258 | | /* Will skip */ |
1259 | 7.62k | break; |
1260 | 133k | } |
1261 | | |
1262 | 133k | return true; |
1263 | 133k | } |