es: pass no cc reorder in es fmt
[vlc.git] / modules / packetizer / h264.c
bloba6814229c1d4785ba4db20cef5f4b88474b52a20
1 /*****************************************************************************
2 * h264.c: h264/avc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_sout.h>
38 #include <vlc_codec.h>
39 #include <vlc_block.h>
41 #include <vlc_block_helper.h>
42 #include <vlc_bits.h>
43 #include "h264_nal.h"
44 #include "h264_slice.h"
45 #include "hxxx_nal.h"
46 #include "hxxx_sei.h"
47 #include "hxxx_common.h"
48 #include "packetizer_helper.h"
49 #include "startcode_helper.h"
51 #include <limits.h>
53 /*****************************************************************************
54 * Module descriptor
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 vlc_module_begin ()
60 set_category( CAT_SOUT )
61 set_subcategory( SUBCAT_SOUT_PACKETIZER )
62 set_description( N_("H.264 video packetizer") )
63 set_capability( "packetizer", 50 )
64 set_callbacks( Open, Close )
65 vlc_module_end ()
68 /****************************************************************************
69 * Local prototypes
70 ****************************************************************************/
72 struct decoder_sys_t
74 /* */
75 packetizer_t packetizer;
77 /* */
78 bool b_slice;
79 block_t *p_frame;
80 block_t **pp_frame_last;
81 block_t *p_sei;
82 block_t **pp_sei_last;
83 /* a new sps/pps can be transmitted outside of iframes */
84 bool b_new_sps;
85 bool b_new_pps;
87 struct
89 block_t *p_block;
90 h264_sequence_parameter_set_t *p_sps;
91 } sps[H264_SPS_ID_MAX + 1];
92 struct
94 block_t *p_block;
95 h264_picture_parameter_set_t *p_pps;
96 } pps[H264_PPS_ID_MAX + 1];
97 const h264_sequence_parameter_set_t *p_active_sps;
98 const h264_picture_parameter_set_t *p_active_pps;
100 /* avcC data */
101 uint8_t i_avcC_length_size;
103 /* From SEI for current frame */
104 uint8_t i_pic_struct;
105 uint8_t i_dpb_output_delay;
106 unsigned i_recovery_frame_cnt;
108 /* Useful values of the Slice Header */
109 h264_slice_t slice;
111 /* */
112 int i_next_block_flags;
113 bool b_recovered;
114 unsigned i_recoveryfnum;
116 /* POC */
117 poc_context_t pocctx;
118 struct
120 mtime_t pts;
121 int num;
122 } prevdatedpoc;
124 mtime_t i_frame_pts;
125 mtime_t i_frame_dts;
127 date_t dts;
129 /* */
130 cc_storage_t *p_ccs;
133 #define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
134 #define BLOCK_FLAG_DROP (2 << BLOCK_FLAG_PRIVATE_SHIFT)
136 static block_t *Packetize( decoder_t *, block_t ** );
137 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
138 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4], int * );
139 static void PacketizeFlush( decoder_t * );
141 static void PacketizeReset( void *p_private, bool b_broken );
142 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
143 static int PacketizeValidate( void *p_private, block_t * );
145 static block_t *ParseNALBlock( decoder_t *, bool *pb_ts_used, block_t * );
147 static block_t *OutputPicture( decoder_t *p_dec );
148 static void PutSPS( decoder_t *p_dec, block_t *p_frag );
149 static void PutPPS( decoder_t *p_dec, block_t *p_frag );
150 static bool ParseSliceHeader( decoder_t *p_dec, const block_t *p_frag, h264_slice_t *p_slice );
151 static bool ParseSeiCallback( const hxxx_sei_data_t *, void * );
154 static const uint8_t p_h264_startcode[3] = { 0x00, 0x00, 0x01 };
156 /*****************************************************************************
157 * Helpers
158 *****************************************************************************/
160 static void StoreSPS( decoder_sys_t *p_sys, uint8_t i_id,
161 block_t *p_block, h264_sequence_parameter_set_t *p_sps )
163 if( p_sys->sps[i_id].p_block )
164 block_Release( p_sys->sps[i_id].p_block );
165 if( p_sys->sps[i_id].p_sps )
166 h264_release_sps( p_sys->sps[i_id].p_sps );
167 if( p_sys->sps[i_id].p_sps == p_sys->p_active_sps )
168 p_sys->p_active_sps = NULL;
169 p_sys->sps[i_id].p_block = p_block;
170 p_sys->sps[i_id].p_sps = p_sps;
173 static void StorePPS( decoder_sys_t *p_sys, uint8_t i_id,
174 block_t *p_block, h264_picture_parameter_set_t *p_pps )
176 if( p_sys->pps[i_id].p_block )
177 block_Release( p_sys->pps[i_id].p_block );
178 if( p_sys->pps[i_id].p_pps )
179 h264_release_pps( p_sys->pps[i_id].p_pps );
180 if( p_sys->pps[i_id].p_pps == p_sys->p_active_pps )
181 p_sys->p_active_pps = NULL;
182 p_sys->pps[i_id].p_block = p_block;
183 p_sys->pps[i_id].p_pps = p_pps;
186 static void ActivateSets( decoder_t *p_dec, const h264_sequence_parameter_set_t *p_sps,
187 const h264_picture_parameter_set_t *p_pps )
189 decoder_sys_t *p_sys = p_dec->p_sys;
191 p_sys->p_active_pps = p_pps;
192 p_sys->p_active_sps = p_sps;
194 if( p_sps )
196 p_dec->fmt_out.i_profile = p_sps->i_profile;
197 p_dec->fmt_out.i_level = p_sps->i_level;
199 (void) h264_get_picture_size( p_sps, &p_dec->fmt_out.video.i_width,
200 &p_dec->fmt_out.video.i_height,
201 &p_dec->fmt_out.video.i_visible_width,
202 &p_dec->fmt_out.video.i_visible_height );
204 if( p_sps->vui.i_sar_num != 0 && p_sps->vui.i_sar_den != 0 )
206 p_dec->fmt_out.video.i_sar_num = p_sps->vui.i_sar_num;
207 p_dec->fmt_out.video.i_sar_den = p_sps->vui.i_sar_den;
210 if( p_sps->vui.b_valid )
212 if( !p_dec->fmt_in.video.i_frame_rate_base &&
213 p_sps->vui.i_num_units_in_tick > 0 && p_sps->vui.i_time_scale > 1 )
215 const unsigned i_rate_base = p_sps->vui.i_num_units_in_tick;
216 const unsigned i_rate = p_sps->vui.i_time_scale >> 1; /* num_clock_ts == 2 */
217 if( i_rate_base != p_dec->fmt_out.video.i_frame_rate_base ||
218 i_rate != p_dec->fmt_out.video.i_frame_rate )
220 p_dec->fmt_out.video.i_frame_rate_base = i_rate_base;
221 p_dec->fmt_out.video.i_frame_rate = i_rate;
222 date_Change( &p_sys->dts, p_sps->vui.i_time_scale, p_sps->vui.i_num_units_in_tick );
225 if( p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF )
226 h264_get_colorimetry( p_sps, &p_dec->fmt_out.video.primaries,
227 &p_dec->fmt_out.video.transfer,
228 &p_dec->fmt_out.video.space,
229 &p_dec->fmt_out.video.b_color_range_full );
234 static bool IsFirstVCLNALUnit( const h264_slice_t *p_prev, const h264_slice_t *p_cur )
236 /* Detection of the first VCL NAL unit of a primary coded picture
237 * (cf. 7.4.1.2.4) */
238 if( p_cur->i_frame_num != p_prev->i_frame_num ||
239 p_cur->i_pic_parameter_set_id != p_prev->i_pic_parameter_set_id ||
240 p_cur->i_field_pic_flag != p_prev->i_field_pic_flag ||
241 !p_cur->i_nal_ref_idc != !p_prev->i_nal_ref_idc )
242 return true;
243 if( (p_cur->i_bottom_field_flag != -1) &&
244 (p_prev->i_bottom_field_flag != -1) &&
245 (p_cur->i_bottom_field_flag != p_prev->i_bottom_field_flag) )
246 return true;
247 if( p_cur->i_pic_order_cnt_type == 0 &&
248 ( p_cur->i_pic_order_cnt_lsb != p_prev->i_pic_order_cnt_lsb ||
249 p_cur->i_delta_pic_order_cnt_bottom != p_prev->i_delta_pic_order_cnt_bottom ) )
250 return true;
251 else if( p_cur->i_pic_order_cnt_type == 1 &&
252 ( p_cur->i_delta_pic_order_cnt0 != p_prev->i_delta_pic_order_cnt0 ||
253 p_cur->i_delta_pic_order_cnt1 != p_prev->i_delta_pic_order_cnt1 ) )
254 return true;
255 if( ( p_cur->i_nal_type == H264_NAL_SLICE_IDR || p_prev->i_nal_type == H264_NAL_SLICE_IDR ) &&
256 ( p_cur->i_nal_type != p_prev->i_nal_type || p_cur->i_idr_pic_id != p_prev->i_idr_pic_id ) )
257 return true;
259 return false;
262 static void DropStoredNAL( decoder_sys_t *p_sys )
264 block_ChainRelease( p_sys->p_frame );
265 block_ChainRelease( p_sys->p_sei );
266 p_sys->p_frame = NULL;
267 p_sys->pp_frame_last = &p_sys->p_frame;
268 p_sys->p_sei = NULL;
269 p_sys->pp_sei_last = &p_sys->p_sei;
272 /*****************************************************************************
273 * Open: probe the packetizer and return score
274 * When opening after demux, the packetizer is only loaded AFTER the decoder
275 * That means that what you set in fmt_out is ignored by the decoder in this special case
276 *****************************************************************************/
277 static int Open( vlc_object_t *p_this )
279 decoder_t *p_dec = (decoder_t*)p_this;
280 decoder_sys_t *p_sys;
281 int i;
283 const bool b_avc = (p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1' ));
285 if( p_dec->fmt_in.i_codec != VLC_CODEC_H264 )
286 return VLC_EGENERIC;
287 if( b_avc && p_dec->fmt_in.i_extra < 7 )
288 return VLC_EGENERIC;
290 /* Allocate the memory needed to store the decoder's structure */
291 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
293 return VLC_ENOMEM;
296 p_sys->p_ccs = cc_storage_new();
297 if( unlikely(!p_sys->p_ccs) )
299 free( p_dec->p_sys );
300 return VLC_ENOMEM;
303 packetizer_Init( &p_sys->packetizer,
304 p_h264_startcode, sizeof(p_h264_startcode), startcode_FindAnnexB,
305 p_h264_startcode, 1, 5,
306 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
308 p_sys->b_slice = false;
309 p_sys->p_frame = NULL;
310 p_sys->pp_frame_last = &p_sys->p_frame;
311 p_sys->p_sei = NULL;
312 p_sys->pp_sei_last = &p_sys->p_sei;
313 p_sys->b_new_sps = false;
314 p_sys->b_new_pps = false;
316 for( i = 0; i <= H264_SPS_ID_MAX; i++ )
318 p_sys->sps[i].p_sps = NULL;
319 p_sys->sps[i].p_block = NULL;
321 p_sys->p_active_sps = NULL;
322 for( i = 0; i <= H264_PPS_ID_MAX; i++ )
324 p_sys->pps[i].p_pps = NULL;
325 p_sys->pps[i].p_block = NULL;
327 p_sys->p_active_pps = NULL;
328 p_sys->i_recovery_frame_cnt = UINT_MAX;
330 h264_slice_init( &p_sys->slice );
332 p_sys->i_next_block_flags = 0;
333 p_sys->b_recovered = false;
334 p_sys->i_recoveryfnum = UINT_MAX;
335 p_sys->i_frame_dts = VLC_TS_INVALID;
336 p_sys->i_frame_pts = VLC_TS_INVALID;
337 p_sys->i_dpb_output_delay = 0;
339 /* POC */
340 h264_poc_context_init( &p_sys->pocctx );
341 p_sys->prevdatedpoc.pts = VLC_TS_INVALID;
343 date_Init( &p_sys->dts, 30000 * 2, 1001 );
344 date_Set( &p_sys->dts, VLC_TS_INVALID );
346 /* Setup properties */
347 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
348 p_dec->fmt_out.i_codec = VLC_CODEC_H264;
349 p_dec->fmt_out.b_packetized = true;
351 if( p_dec->fmt_in.video.i_frame_rate_base &&
352 p_dec->fmt_in.video.i_frame_rate &&
353 p_dec->fmt_in.video.i_frame_rate <= UINT_MAX / 2 )
355 date_Change( &p_sys->dts, p_dec->fmt_in.video.i_frame_rate * 2,
356 p_dec->fmt_in.video.i_frame_rate_base );
359 if( b_avc )
361 /* This type of stream is produced by mp4 and matroska
362 * when we want to store it in another streamformat, you need to convert
363 * The fmt_in.p_extra should ALWAYS contain the avcC
364 * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
365 if( h264_isavcC( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ) )
367 free( p_dec->fmt_out.p_extra );
368 size_t i_size;
369 p_dec->fmt_out.p_extra = h264_avcC_to_AnnexB_NAL( p_dec->fmt_in.p_extra,
370 p_dec->fmt_in.i_extra,
371 &i_size,
372 &p_sys->i_avcC_length_size );
373 p_dec->fmt_out.i_extra = i_size;
374 p_sys->b_recovered = !!p_dec->fmt_out.i_extra;
376 if(!p_dec->fmt_out.p_extra)
378 msg_Err( p_dec, "Invalid AVC extradata");
379 Close( p_this );
380 return VLC_EGENERIC;
383 else
385 msg_Err( p_dec, "Invalid or missing AVC extradata");
386 Close( p_this );
387 return VLC_EGENERIC;
390 /* Set callback */
391 p_dec->pf_packetize = PacketizeAVC1;
393 else
395 /* This type of stream contains data with 3 of 4 byte startcodes
396 * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
397 * The fmt_out.p_extra should be the same */
399 /* Set callback */
400 p_dec->pf_packetize = Packetize;
403 /* */
404 if( p_dec->fmt_out.i_extra > 0 )
406 packetizer_Header( &p_sys->packetizer,
407 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
410 if( b_avc )
412 /* FIXME: that's not correct for every AVC */
413 if( !p_sys->b_new_pps || !p_sys->b_new_sps )
415 msg_Err( p_dec, "Invalid or missing SPS %d or PPS %d in AVC extradata",
416 p_sys->b_new_sps, p_sys->b_new_pps );
417 Close( p_this );
418 return VLC_EGENERIC;
421 msg_Dbg( p_dec, "Packetizer fed with AVC, nal length size=%d",
422 p_sys->i_avcC_length_size );
425 /* CC are the same for H264/AVC in T35 sections (ETSI TS 101 154) */
426 p_dec->pf_get_cc = GetCc;
427 p_dec->pf_flush = PacketizeFlush;
429 return VLC_SUCCESS;
432 /*****************************************************************************
433 * Close: clean up the packetizer
434 *****************************************************************************/
435 static void Close( vlc_object_t *p_this )
437 decoder_t *p_dec = (decoder_t*)p_this;
438 decoder_sys_t *p_sys = p_dec->p_sys;
439 int i;
441 DropStoredNAL( p_sys );
442 for( i = 0; i <= H264_SPS_ID_MAX; i++ )
443 StoreSPS( p_sys, i, NULL, NULL );
444 for( i = 0; i <= H264_PPS_ID_MAX; i++ )
445 StorePPS( p_sys, i, NULL, NULL );
447 packetizer_Clean( &p_sys->packetizer );
449 cc_storage_delete( p_sys->p_ccs );
451 free( p_sys );
454 static void PacketizeFlush( decoder_t *p_dec )
456 decoder_sys_t *p_sys = p_dec->p_sys;
458 packetizer_Flush( &p_sys->packetizer );
461 /****************************************************************************
462 * Packetize: the whole thing
463 * Search for the startcodes 3 or more bytes
464 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
465 ****************************************************************************/
466 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
468 decoder_sys_t *p_sys = p_dec->p_sys;
470 return packetizer_Packetize( &p_sys->packetizer, pp_block );
473 /****************************************************************************
474 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
475 * Will always use 4 byte 0 0 0 1 startcodes
476 * Will prepend a SPS and PPS before each keyframe
477 ****************************************************************************/
478 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
480 decoder_sys_t *p_sys = p_dec->p_sys;
482 return PacketizeXXC1( p_dec, p_sys->i_avcC_length_size,
483 pp_block, ParseNALBlock );
486 /*****************************************************************************
487 * GetCc:
488 *****************************************************************************/
489 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4], int *pi_reorder_depth )
491 return cc_storage_get_current( p_dec->p_sys->p_ccs, pb_present, pi_reorder_depth );
494 /****************************************************************************
495 * Helpers
496 ****************************************************************************/
497 static void PacketizeReset( void *p_private, bool b_broken )
499 decoder_t *p_dec = p_private;
500 decoder_sys_t *p_sys = p_dec->p_sys;
502 if( b_broken || !p_sys->b_slice )
504 DropStoredNAL( p_sys );
505 p_sys->b_new_sps = false;
506 p_sys->b_new_pps = false;
507 p_sys->p_active_pps = NULL;
508 p_sys->p_active_sps = NULL;
509 p_sys->i_frame_pts = VLC_TS_INVALID;
510 p_sys->i_frame_dts = VLC_TS_INVALID;
511 p_sys->slice.type = H264_SLICE_TYPE_UNKNOWN;
512 p_sys->b_slice = false;
513 /* POC */
514 h264_poc_context_init( &p_sys->pocctx );
515 p_sys->prevdatedpoc.pts = VLC_TS_INVALID;
516 /* From SEI */
517 p_sys->i_dpb_output_delay = 0;
518 p_sys->i_pic_struct = UINT8_MAX;
519 p_sys->i_recovery_frame_cnt = UINT_MAX;
521 p_sys->i_next_block_flags = BLOCK_FLAG_DISCONTINUITY;
522 p_sys->b_recovered = false;
523 p_sys->i_recoveryfnum = UINT_MAX;
524 date_Set( &p_sys->dts, VLC_TS_INVALID );
526 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
528 decoder_t *p_dec = p_private;
530 /* Remove trailing 0 bytes */
531 while( p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
532 p_block->i_buffer--;
534 return ParseNALBlock( p_dec, pb_ts_used, p_block );
536 static int PacketizeValidate( void *p_private, block_t *p_au )
538 VLC_UNUSED(p_private);
539 VLC_UNUSED(p_au);
540 return VLC_SUCCESS;
543 /*****************************************************************************
544 * ParseNALBlock: parses annexB type NALs
545 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
546 *****************************************************************************/
547 static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag )
549 decoder_sys_t *p_sys = p_dec->p_sys;
550 block_t *p_pic = NULL;
551 bool b_new_picture = false;
553 const int i_nal_type = p_frag->p_buffer[4]&0x1f;
554 const mtime_t i_frag_dts = p_frag->i_dts;
555 const mtime_t i_frag_pts = p_frag->i_pts;
557 if( p_sys->b_slice && (!p_sys->p_active_pps || !p_sys->p_active_sps) )
559 msg_Warn( p_dec, "waiting for SPS/PPS" );
561 /* Reset context */
562 p_sys->slice.type = H264_SLICE_TYPE_UNKNOWN;
563 p_sys->b_slice = false;
564 DropStoredNAL( p_sys );
565 /* From SEI */
566 p_sys->i_dpb_output_delay = 0;
567 p_sys->i_pic_struct = UINT8_MAX;
568 cc_storage_reset( p_sys->p_ccs );
571 if( i_nal_type >= H264_NAL_SLICE && i_nal_type <= H264_NAL_SLICE_IDR )
573 h264_slice_t newslice;
575 if( i_nal_type == H264_NAL_SLICE_IDR )
577 p_sys->b_recovered = true;
578 p_sys->i_recovery_frame_cnt = UINT_MAX;
579 p_sys->i_recoveryfnum = UINT_MAX;
582 if( ParseSliceHeader( p_dec, p_frag, &newslice ) )
584 /* Only IDR carries the id, to be propagated */
585 if( newslice.i_idr_pic_id == -1 )
586 newslice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
588 b_new_picture = IsFirstVCLNALUnit( &p_sys->slice, &newslice );
589 if( b_new_picture )
591 /* Parse SEI for that frame now we should have matched SPS/PPS */
592 for( block_t *p_sei = p_sys->p_sei; p_sei; p_sei = p_sei->p_next )
594 HxxxParse_AnnexB_SEI( p_sei->p_buffer, p_sei->i_buffer,
595 1 /* nal header */, ParseSeiCallback, p_dec );
598 if( p_sys->b_slice )
599 p_pic = OutputPicture( p_dec );
602 /* */
603 p_sys->slice = newslice;
605 else
607 p_sys->p_active_pps = NULL;
608 /* Fragment will be discarded later on */
610 p_sys->b_slice = true;
612 else if( i_nal_type == H264_NAL_SPS )
614 if( p_sys->b_slice )
615 p_pic = OutputPicture( p_dec );
617 PutSPS( p_dec, p_frag );
618 p_sys->b_new_sps = true;
620 /* Do not append the SPS because we will insert it on keyframes */
621 p_frag = NULL;
623 else if( i_nal_type == H264_NAL_PPS )
625 if( p_sys->b_slice )
626 p_pic = OutputPicture( p_dec );
628 PutPPS( p_dec, p_frag );
629 p_sys->b_new_pps = true;
631 /* Do not append the PPS because we will insert it on keyframes */
632 p_frag = NULL;
634 else if( i_nal_type == H264_NAL_SEI )
636 if( p_sys->b_slice )
637 p_pic = OutputPicture( p_dec );
639 block_ChainLastAppend( &p_sys->pp_sei_last, p_frag );
640 p_frag = NULL;
642 else if( i_nal_type == H264_NAL_END_OF_SEQ || i_nal_type == H264_NAL_END_OF_STREAM )
644 /* Early end of packetization */
645 block_ChainLastAppend( &p_sys->pp_sei_last, p_frag );
646 p_frag = NULL;
647 /* important for still pictures/menus */
648 p_sys->i_next_block_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
649 if( p_sys->b_slice )
650 p_pic = OutputPicture( p_dec );
652 else if( i_nal_type == H264_NAL_AU_DELIMITER ||
653 ( i_nal_type >= H264_NAL_PREFIX && i_nal_type <= H264_NAL_RESERVED_18 ) )
655 if( p_sys->b_slice )
656 p_pic = OutputPicture( p_dec );
658 if( i_nal_type == H264_NAL_AU_DELIMITER )
660 if( p_sys->p_frame && (p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD) )
662 block_Release( p_frag );
663 p_frag = NULL;
665 else
667 p_frag->i_flags |= BLOCK_FLAG_PRIVATE_AUD;
672 /* Append the block */
673 if( p_frag )
674 block_ChainLastAppend( &p_sys->pp_frame_last, p_frag );
676 *pb_ts_used = false;
677 if( p_sys->i_frame_dts <= VLC_TS_INVALID &&
678 p_sys->i_frame_pts <= VLC_TS_INVALID && b_new_picture )
680 p_sys->i_frame_dts = i_frag_dts;
681 p_sys->i_frame_pts = i_frag_pts;
682 *pb_ts_used = true;
683 if( i_frag_dts > VLC_TS_INVALID )
684 date_Set( &p_sys->dts, i_frag_dts );
687 if( p_pic && (p_pic->i_flags & BLOCK_FLAG_DROP) )
689 block_Release( p_pic );
690 p_pic = NULL;
693 return p_pic;
696 static block_t *OutputPicture( decoder_t *p_dec )
698 decoder_sys_t *p_sys = p_dec->p_sys;
699 block_t *p_pic = NULL;
700 block_t **pp_pic_last = &p_pic;
702 if( unlikely(!p_sys->p_frame) )
704 assert( p_sys->p_frame );
705 return NULL;
708 /* Bind matched/referred PPS and SPS */
709 const h264_picture_parameter_set_t *p_pps = p_sys->p_active_pps;
710 const h264_sequence_parameter_set_t *p_sps = p_sys->p_active_sps;
711 if( !p_pps || !p_sps )
713 DropStoredNAL( p_sys );
714 return NULL;
717 if( !p_sys->b_recovered && p_sys->i_recoveryfnum == UINT_MAX &&
718 p_sys->i_recovery_frame_cnt == UINT_MAX && p_sys->slice.type == H264_SLICE_TYPE_I )
720 /* No way to recover using SEI, just sync on I Slice */
721 p_sys->b_recovered = true;
724 bool b_need_sps_pps = p_sys->slice.type == H264_SLICE_TYPE_I &&
725 p_sys->p_active_pps && p_sys->p_active_sps;
727 /* Handle SEI recovery */
728 if ( !p_sys->b_recovered && p_sys->i_recovery_frame_cnt != UINT_MAX &&
729 p_sys->i_recoveryfnum == UINT_MAX )
731 p_sys->i_recoveryfnum = p_sys->slice.i_frame_num + p_sys->i_recovery_frame_cnt;
732 b_need_sps_pps = true; /* SPS/PPS must be inserted for SEI recovery */
733 msg_Dbg( p_dec, "Recovering using SEI, prerolling %u reference pics", p_sys->i_recovery_frame_cnt );
736 if( p_sys->i_recoveryfnum != UINT_MAX )
738 assert(p_sys->b_recovered == false);
739 const unsigned maxFrameNum = 1 << (p_sps->i_log2_max_frame_num + 4);
740 if( (p_sys->i_recoveryfnum > maxFrameNum &&
741 (unsigned)p_sys->slice.i_frame_num <= maxFrameNum / 2 &&
742 (unsigned)p_sys->slice.i_frame_num >= p_sys->i_recoveryfnum % maxFrameNum ) ||
743 (unsigned)p_sys->slice.i_frame_num >= p_sys->i_recoveryfnum )
745 p_sys->i_recoveryfnum = UINT_MAX;
746 p_sys->b_recovered = true;
747 msg_Dbg( p_dec, "Recovery from SEI recovery point complete" );
751 /* Gather PPS/SPS if required */
752 block_t *p_xpsnal = NULL;
753 block_t **pp_xpsnal_tail = &p_xpsnal;
754 if( b_need_sps_pps || p_sys->b_new_sps || p_sys->b_new_pps )
756 for( int i = 0; i <= H264_SPS_ID_MAX && (b_need_sps_pps || p_sys->b_new_sps); i++ )
758 if( p_sys->sps[i].p_block )
759 block_ChainLastAppend( &pp_xpsnal_tail, block_Duplicate( p_sys->sps[i].p_block ) );
761 for( int i = 0; i < H264_PPS_ID_MAX && (b_need_sps_pps || p_sys->b_new_pps); i++ )
763 if( p_sys->pps[i].p_block )
764 block_ChainLastAppend( &pp_xpsnal_tail, block_Duplicate( p_sys->pps[i].p_block ) );
768 /* Now rebuild NAL Sequence, inserting PPS/SPS if any */
769 if( p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD )
771 block_t *p_au = p_sys->p_frame;
772 p_sys->p_frame = p_au->p_next;
773 p_au->p_next = NULL;
774 p_au->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD;
775 block_ChainLastAppend( &pp_pic_last, p_au );
778 if( p_xpsnal )
779 block_ChainLastAppend( &pp_pic_last, p_xpsnal );
781 if( p_sys->p_sei )
782 block_ChainLastAppend( &pp_pic_last, p_sys->p_sei );
784 assert( p_sys->p_frame );
785 if( p_sys->p_frame )
786 block_ChainLastAppend( &pp_pic_last, p_sys->p_frame );
788 /* Reset chains, now empty */
789 p_sys->p_frame = NULL;
790 p_sys->pp_frame_last = &p_sys->p_frame;
791 p_sys->p_sei = NULL;
792 p_sys->pp_sei_last = &p_sys->p_sei;
794 p_pic = block_ChainGather( p_pic );
796 if( !p_pic )
797 return NULL;
799 /* for PTS Fixup, interlaced fields (multiple AU/block) */
800 int tFOC = 0, bFOC = 0, PictureOrderCount = 0;
801 h264_compute_poc( p_sps, &p_sys->slice, &p_sys->pocctx, &PictureOrderCount, &tFOC, &bFOC );
803 unsigned i_num_clock_ts = h264_get_num_ts( p_sps, &p_sys->slice, p_sys->i_pic_struct, tFOC, bFOC );
805 if( p_sps->frame_mbs_only_flag == 0 && p_sps->vui.b_pic_struct_present_flag )
807 switch( p_sys->i_pic_struct )
809 /* Top and Bottom field slices */
810 case 1:
811 case 2:
812 p_pic->i_flags |= BLOCK_FLAG_SINGLE_FIELD;
813 p_pic->i_flags |= (!p_sys->slice.i_bottom_field_flag) ? BLOCK_FLAG_TOP_FIELD_FIRST
814 : BLOCK_FLAG_BOTTOM_FIELD_FIRST;
815 break;
816 /* Each of the following slices contains multiple fields */
817 case 3:
818 p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST;
819 break;
820 case 4:
821 p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST;
822 break;
823 case 5:
824 p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST;
825 break;
826 case 6:
827 p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST;
828 break;
829 default:
830 break;
834 /* set dts/pts to current block timestamps */
835 p_pic->i_dts = p_sys->i_frame_dts;
836 p_pic->i_pts = p_sys->i_frame_pts;
838 /* Fixup missing timestamps after split (multiple AU/block)*/
839 if( p_pic->i_dts <= VLC_TS_INVALID )
840 p_pic->i_dts = date_Get( &p_sys->dts );
842 if( p_sys->slice.type == H264_SLICE_TYPE_I )
843 p_sys->prevdatedpoc.pts = VLC_TS_INVALID;
845 if( p_pic->i_pts == VLC_TS_INVALID )
847 if( p_sys->prevdatedpoc.pts > VLC_TS_INVALID &&
848 date_Get( &p_sys->dts ) != VLC_TS_INVALID )
850 date_t pts = p_sys->dts;
851 date_Set( &pts, p_sys->prevdatedpoc.pts );
853 int diff = tFOC - p_sys->prevdatedpoc.num;
854 if( diff > 0 )
855 date_Increment( &pts, diff );
856 else
857 date_Decrement( &pts, -diff );
859 p_pic->i_pts = date_Get( &pts );
861 /* In case there's no PTS at all */
862 else if( p_sys->slice.i_nal_ref_idc == 0 &&
863 p_sys->slice.type == H264_SLICE_TYPE_B )
865 p_pic->i_pts = p_pic->i_dts;
867 else if( p_sys->slice.type == H264_SLICE_TYPE_I &&
868 date_Get( &p_sys->dts ) != VLC_TS_INVALID )
870 /* Hell no PTS on IDR. We're totally blind */
871 date_t pts = p_sys->dts;
872 date_Increment( &pts, 2 );
873 p_pic->i_pts = date_Get( &pts );
877 if( p_pic->i_pts > VLC_TS_INVALID )
879 p_sys->prevdatedpoc.pts = p_pic->i_pts;
880 p_sys->prevdatedpoc.num = PictureOrderCount;
883 if( p_pic->i_length == 0 )
885 if( p_sps->vui.i_time_scale )
887 p_pic->i_length = CLOCK_FREQ * i_num_clock_ts *
888 p_sps->vui.i_num_units_in_tick / p_sps->vui.i_time_scale;
890 else
892 date_t next = p_sys->dts;
893 date_Increment( &next, i_num_clock_ts );
894 p_pic->i_length = date_Get( &next ) - date_Get( &p_sys->dts );
898 #if 0
899 msg_Err(p_dec, "F/BOC %d/%d POC %d %d rec %d flags %x ref%d fn %d fp %d %d pts %ld len %ld",
900 tFOC, bFOC, PictureOrderCount,
901 p_sys->slice.type, p_sys->b_recovered, p_pic->i_flags,
902 p_sys->slice.i_nal_ref_idc, p_sys->slice.i_frame_num, p_sys->slice.i_field_pic_flag,
903 p_pic->i_pts - p_pic->i_dts, p_pic->i_pts % (100*CLOCK_FREQ), p_pic->i_length);
904 #endif
906 /* save for next pic fixups */
907 if( date_Get( &p_sys->dts ) != VLC_TS_INVALID )
909 if( p_sys->i_next_block_flags & BLOCK_FLAG_DISCONTINUITY )
910 date_Set( &p_sys->dts, VLC_TS_INVALID );
911 else
912 date_Increment( &p_sys->dts, i_num_clock_ts );
915 if( p_pic )
917 p_pic->i_flags |= p_sys->i_next_block_flags;
918 p_sys->i_next_block_flags = 0;
921 switch( p_sys->slice.type )
923 case H264_SLICE_TYPE_P:
924 p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
925 break;
926 case H264_SLICE_TYPE_B:
927 p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
928 break;
929 case H264_SLICE_TYPE_I:
930 p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
931 default:
932 break;
935 if( !p_sys->b_recovered )
937 if( p_sys->i_recoveryfnum != UINT_MAX ) /* recovering from SEI */
938 p_pic->i_flags |= BLOCK_FLAG_PREROLL;
939 else
940 p_pic->i_flags |= BLOCK_FLAG_DROP;
943 p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD;
945 /* reset after output */
946 p_sys->i_frame_dts = VLC_TS_INVALID;
947 p_sys->i_frame_pts = VLC_TS_INVALID;
948 p_sys->i_dpb_output_delay = 0;
949 p_sys->i_pic_struct = UINT8_MAX;
950 p_sys->i_recovery_frame_cnt = UINT_MAX;
951 p_sys->slice.type = H264_SLICE_TYPE_UNKNOWN;
952 p_sys->p_sei = NULL;
953 p_sys->pp_sei_last = &p_sys->p_sei;
954 p_sys->b_new_sps = false;
955 p_sys->b_new_pps = false;
956 p_sys->b_slice = false;
958 /* CC */
959 cc_storage_commit( p_sys->p_ccs, p_pic );
961 return p_pic;
964 static void PutSPS( decoder_t *p_dec, block_t *p_frag )
966 decoder_sys_t *p_sys = p_dec->p_sys;
968 const uint8_t *p_buffer = p_frag->p_buffer;
969 size_t i_buffer = p_frag->i_buffer;
971 if( !hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
973 block_Release( p_frag );
974 return;
977 h264_sequence_parameter_set_t *p_sps = h264_decode_sps( p_buffer, i_buffer, true );
978 if( !p_sps )
980 msg_Warn( p_dec, "invalid SPS" );
981 block_Release( p_frag );
982 return;
985 /* We have a new SPS */
986 if( !p_sys->sps[p_sps->i_id].p_sps )
987 msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", p_sps->i_id );
989 StoreSPS( p_sys, p_sps->i_id, p_frag, p_sps );
992 static void PutPPS( decoder_t *p_dec, block_t *p_frag )
994 decoder_sys_t *p_sys = p_dec->p_sys;
995 const uint8_t *p_buffer = p_frag->p_buffer;
996 size_t i_buffer = p_frag->i_buffer;
998 if( !hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
1000 block_Release( p_frag );
1001 return;
1004 h264_picture_parameter_set_t *p_pps = h264_decode_pps( p_buffer, i_buffer, true );
1005 if( !p_pps )
1007 msg_Warn( p_dec, "invalid PPS" );
1008 block_Release( p_frag );
1009 return;
1012 /* We have a new PPS */
1013 if( !p_sys->pps[p_pps->i_id].p_pps )
1014 msg_Dbg( p_dec, "found NAL_PPS (pps_id=%d sps_id=%d)", p_pps->i_id, p_pps->i_sps_id );
1016 StorePPS( p_sys, p_pps->i_id, p_frag, p_pps );
1019 static void GetSPSPPS( uint8_t i_pps_id, void *priv,
1020 const h264_sequence_parameter_set_t **pp_sps,
1021 const h264_picture_parameter_set_t **pp_pps )
1023 decoder_sys_t *p_sys = priv;
1025 *pp_pps = p_sys->pps[i_pps_id].p_pps;
1026 if( *pp_pps == NULL )
1027 *pp_sps = NULL;
1028 else
1029 *pp_sps = p_sys->sps[(*pp_pps)->i_sps_id].p_sps;
1032 static bool ParseSliceHeader( decoder_t *p_dec, const block_t *p_frag, h264_slice_t *p_slice )
1034 decoder_sys_t *p_sys = p_dec->p_sys;
1036 const uint8_t *p_stripped = p_frag->p_buffer;
1037 size_t i_stripped = p_frag->i_buffer;
1039 if( !hxxx_strip_AnnexB_startcode( &p_stripped, &i_stripped ) || i_stripped < 2 )
1040 return false;
1042 if( !h264_decode_slice( p_stripped, i_stripped, GetSPSPPS, p_sys, p_slice ) )
1043 return false;
1045 const h264_sequence_parameter_set_t *p_sps;
1046 const h264_picture_parameter_set_t *p_pps;
1047 GetSPSPPS( p_slice->i_pic_parameter_set_id, p_sys, &p_sps, &p_pps );
1048 if( unlikely( !p_sps || !p_pps) )
1049 return false;
1051 ActivateSets( p_dec, p_sps, p_pps );
1053 return true;
1056 static bool ParseSeiCallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
1058 decoder_t *p_dec = (decoder_t *) cbdata;
1059 decoder_sys_t *p_sys = p_dec->p_sys;
1061 switch( p_sei_data->i_type )
1063 /* Look for pic timing */
1064 case HXXX_SEI_PIC_TIMING:
1066 const h264_sequence_parameter_set_t *p_sps = p_sys->p_active_sps;
1067 if( unlikely( p_sps == NULL ) )
1069 assert( p_sps );
1070 break;
1073 if( p_sps->vui.b_valid )
1075 if( p_sps->vui.b_hrd_parameters_present_flag )
1077 bs_read( p_sei_data->p_bs, p_sps->vui.i_cpb_removal_delay_length_minus1 + 1 );
1078 p_sys->i_dpb_output_delay =
1079 bs_read( p_sei_data->p_bs, p_sps->vui.i_dpb_output_delay_length_minus1 + 1 );
1082 if( p_sps->vui.b_pic_struct_present_flag )
1083 p_sys->i_pic_struct = bs_read( p_sei_data->p_bs, 4 );
1084 /* + unparsed remains */
1086 } break;
1088 /* Look for user_data_registered_itu_t_t35 */
1089 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
1091 if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
1093 cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
1094 p_sei_data->itu_t35.u.cc.i_data );
1096 } break;
1098 case HXXX_SEI_FRAME_PACKING_ARRANGEMENT:
1100 if( p_dec->fmt_in.video.multiview_mode == MULTIVIEW_2D )
1102 video_multiview_mode_t mode;
1103 switch( p_sei_data->frame_packing.type )
1105 case FRAME_PACKING_INTERLEAVED_CHECKERBOARD:
1106 mode = MULTIVIEW_STEREO_CHECKERBOARD; break;
1107 case FRAME_PACKING_INTERLEAVED_COLUMN:
1108 mode = MULTIVIEW_STEREO_COL; break;
1109 case FRAME_PACKING_INTERLEAVED_ROW:
1110 mode = MULTIVIEW_STEREO_ROW; break;
1111 case FRAME_PACKING_SIDE_BY_SIDE:
1112 mode = MULTIVIEW_STEREO_SBS; break;
1113 case FRAME_PACKING_TOP_BOTTOM:
1114 mode = MULTIVIEW_STEREO_TB; break;
1115 case FRAME_PACKING_TEMPORAL:
1116 mode = MULTIVIEW_STEREO_FRAME; break;
1117 case FRAME_PACKING_TILED:
1118 default:
1119 mode = MULTIVIEW_2D; break;
1121 p_dec->fmt_out.video.multiview_mode = mode;
1123 } break;
1125 /* Look for SEI recovery point */
1126 case HXXX_SEI_RECOVERY_POINT:
1128 if( !p_sys->b_recovered )
1129 msg_Dbg( p_dec, "Seen SEI recovery point, %d recovery frames", p_sei_data->recovery.i_frames );
1130 p_sys->i_recovery_frame_cnt = p_sei_data->recovery.i_frames;
1131 } break;
1133 default:
1134 /* Will skip */
1135 break;
1138 return true;