packetizer/h264: fix resource deallocation during Close
[vlc.git] / modules / packetizer / h264.c
blob4d2db65f76e7769d9f3f040388e375063dc060f6
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 "hxxx_nal.h"
45 #include "hxxx_sei.h"
46 #include "hxxx_common.h"
47 #include "packetizer_helper.h"
48 #include "startcode_helper.h"
50 /*****************************************************************************
51 * Module descriptor
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
56 vlc_module_begin ()
57 set_category( CAT_SOUT )
58 set_subcategory( SUBCAT_SOUT_PACKETIZER )
59 set_description( N_("H.264 video packetizer") )
60 set_capability( "packetizer", 50 )
61 set_callbacks( Open, Close )
62 vlc_module_end ()
65 /****************************************************************************
66 * Local prototypes
67 ****************************************************************************/
68 typedef struct
70 int i_nal_type;
71 int i_nal_ref_idc;
73 int i_frame_type;
74 int i_pic_parameter_set_id;
75 int i_frame_num;
77 int i_field_pic_flag;
78 int i_bottom_field_flag;
80 int i_idr_pic_id;
82 int i_pic_order_cnt_lsb;
83 int i_delta_pic_order_cnt_bottom;
85 int i_delta_pic_order_cnt0;
86 int i_delta_pic_order_cnt1;
87 } slice_t;
89 struct decoder_sys_t
91 /* */
92 packetizer_t packetizer;
94 /* */
95 bool b_slice;
96 block_t *p_frame;
97 block_t **pp_frame_last;
98 bool b_frame_sps;
99 bool b_frame_pps;
101 bool b_header;
102 bool b_sps;
103 bool b_pps;
104 block_t *pp_sps[H264_SPS_ID_MAX + 1];
105 block_t *pp_pps[H264_PPS_ID_MAX + 1];
106 int i_recovery_frames; /* -1 = no recovery */
108 /* avcC data */
109 uint8_t i_avcC_length_size;
111 /* Useful values of the Sequence Parameter Set */
112 int i_log2_max_frame_num;
113 int b_frame_mbs_only;
114 int i_pic_order_cnt_type;
115 int i_delta_pic_order_always_zero_flag;
116 int i_log2_max_pic_order_cnt_lsb;
118 /* Value from Picture Parameter Set */
119 int i_pic_order_present_flag;
121 /* VUI */
122 bool b_timing_info_present_flag;
123 uint32_t i_num_units_in_tick;
124 uint32_t i_time_scale;
125 bool b_fixed_frame_rate;
126 bool b_pic_struct_present_flag;
127 uint8_t i_pic_struct;
128 bool b_cpb_dpb_delays_present_flag;
129 uint8_t i_cpb_removal_delay_length_minus1;
130 uint8_t i_dpb_output_delay_length_minus1;
132 /* Useful values of the Slice Header */
133 slice_t slice;
135 /* */
136 bool b_even_frame;
137 mtime_t i_frame_pts;
138 mtime_t i_frame_dts;
139 mtime_t i_prev_pts;
140 mtime_t i_prev_dts;
141 uint8_t i_dpb_output_delay;
143 /* */
144 cc_storage_t *p_ccs;
147 #define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
149 static block_t *Packetize( decoder_t *, block_t ** );
150 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
151 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
152 static void PacketizeFlush( decoder_t * );
154 static void PacketizeReset( void *p_private, bool b_broken );
155 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
156 static int PacketizeValidate( void *p_private, block_t * );
158 static block_t *ParseNALBlock( decoder_t *, bool *pb_ts_used, block_t * );
160 static block_t *OutputPicture( decoder_t *p_dec );
161 static void PutSPS( decoder_t *p_dec, block_t *p_frag );
162 static void PutPPS( decoder_t *p_dec, block_t *p_frag );
163 static bool ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
164 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag );
165 static bool ParseSeiCallback( const hxxx_sei_data_t *, void * );
168 static const uint8_t p_h264_startcode[3] = { 0x00, 0x00, 0x01 };
170 /*****************************************************************************
171 * Open: probe the packetizer and return score
172 * When opening after demux, the packetizer is only loaded AFTER the decoder
173 * That means that what you set in fmt_out is ignored by the decoder in this special case
174 *****************************************************************************/
175 static int Open( vlc_object_t *p_this )
177 decoder_t *p_dec = (decoder_t*)p_this;
178 decoder_sys_t *p_sys;
179 int i;
181 const bool b_avc = (p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'a', 'v', 'c', '1' ));
183 if( p_dec->fmt_in.i_codec != VLC_CODEC_H264 )
184 return VLC_EGENERIC;
185 if( b_avc && p_dec->fmt_in.i_extra < 7 )
186 return VLC_EGENERIC;
188 /* Allocate the memory needed to store the decoder's structure */
189 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
191 return VLC_ENOMEM;
194 p_sys->p_ccs = cc_storage_new();
195 if( unlikely(!p_sys->p_ccs) )
197 free( p_dec->p_sys );
198 return VLC_ENOMEM;
201 packetizer_Init( &p_sys->packetizer,
202 p_h264_startcode, sizeof(p_h264_startcode), startcode_FindAnnexB,
203 p_h264_startcode, 1, 5,
204 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
206 p_sys->b_slice = false;
207 p_sys->p_frame = NULL;
208 p_sys->pp_frame_last = &p_sys->p_frame;
209 p_sys->b_frame_sps = false;
210 p_sys->b_frame_pps = false;
212 p_sys->b_header= false;
213 p_sys->b_sps = false;
214 p_sys->b_pps = false;
215 for( i = 0; i <= H264_SPS_ID_MAX; i++ )
216 p_sys->pp_sps[i] = NULL;
217 for( i = 0; i <= H264_PPS_ID_MAX; i++ )
218 p_sys->pp_pps[i] = NULL;
219 p_sys->i_recovery_frames = -1;
221 p_sys->slice.i_nal_type = -1;
222 p_sys->slice.i_nal_ref_idc = -1;
223 p_sys->slice.i_idr_pic_id = -1;
224 p_sys->slice.i_frame_num = -1;
225 p_sys->slice.i_frame_type = 0;
226 p_sys->slice.i_pic_parameter_set_id = -1;
227 p_sys->slice.i_field_pic_flag = 0;
228 p_sys->slice.i_bottom_field_flag = -1;
229 p_sys->slice.i_pic_order_cnt_lsb = -1;
230 p_sys->slice.i_delta_pic_order_cnt_bottom = -1;
232 p_sys->b_timing_info_present_flag = false;
233 p_sys->b_pic_struct_present_flag = false;
234 p_sys->b_cpb_dpb_delays_present_flag = false;
235 p_sys->i_cpb_removal_delay_length_minus1 = 0;
236 p_sys->i_dpb_output_delay_length_minus1 = 0;
238 p_sys->b_even_frame = false;
239 p_sys->i_frame_dts = VLC_TS_INVALID;
240 p_sys->i_frame_pts = VLC_TS_INVALID;
241 p_sys->i_prev_dts = VLC_TS_INVALID;
242 p_sys->i_prev_pts = VLC_TS_INVALID;
243 p_sys->i_dpb_output_delay = 0;
245 /* Setup properties */
246 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
247 p_dec->fmt_out.i_codec = VLC_CODEC_H264;
248 p_dec->fmt_out.b_packetized = true;
250 if( b_avc )
252 /* This type of stream is produced by mp4 and matroska
253 * when we want to store it in another streamformat, you need to convert
254 * The fmt_in.p_extra should ALWAYS contain the avcC
255 * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
256 if( h264_isavcC( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ) )
258 free( p_dec->fmt_out.p_extra );
259 size_t i_size;
260 p_dec->fmt_out.p_extra = h264_avcC_to_AnnexB_NAL( p_dec->fmt_in.p_extra,
261 p_dec->fmt_in.i_extra,
262 &i_size,
263 &p_sys->i_avcC_length_size );
264 p_dec->fmt_out.i_extra = i_size;
265 p_sys->b_header = !!p_dec->fmt_out.i_extra;
267 if(!p_dec->fmt_out.p_extra)
269 msg_Err( p_dec, "Invalid AVC extradata");
270 Close( p_this );
271 return VLC_EGENERIC;
274 else
276 msg_Err( p_dec, "Invalid or missing AVC extradata");
277 Close( p_this );
278 return VLC_EGENERIC;
281 /* Set callback */
282 p_dec->pf_packetize = PacketizeAVC1;
284 else
286 /* This type of stream contains data with 3 of 4 byte startcodes
287 * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
288 * The fmt_out.p_extra should be the same */
290 /* Set callback */
291 p_dec->pf_packetize = Packetize;
294 /* */
295 if( p_dec->fmt_out.i_extra > 0 )
297 packetizer_Header( &p_sys->packetizer,
298 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
301 if( b_avc )
303 if( !p_sys->b_sps || !p_sys->b_pps )
305 msg_Err( p_dec, "Invalid or missing SPS %d or PPS %d in AVC extradata",
306 p_sys->b_sps, p_sys->b_pps );
307 Close( p_this );
308 return VLC_EGENERIC;
311 msg_Dbg( p_dec, "Packetizer fed with AVC, nal length size=%d",
312 p_sys->i_avcC_length_size );
315 /* CC are the same for H264/AVC in T35 sections (ETSI TS 101 154) */
316 p_dec->pf_get_cc = GetCc;
317 p_dec->pf_flush = PacketizeFlush;
319 return VLC_SUCCESS;
322 /*****************************************************************************
323 * Close: clean up the packetizer
324 *****************************************************************************/
325 static void Close( vlc_object_t *p_this )
327 decoder_t *p_dec = (decoder_t*)p_this;
328 decoder_sys_t *p_sys = p_dec->p_sys;
329 int i;
331 if( p_sys->p_frame )
332 block_ChainRelease( p_sys->p_frame );
333 for( i = 0; i <= H264_SPS_ID_MAX; i++ )
335 if( p_sys->pp_sps[i] )
336 block_Release( p_sys->pp_sps[i] );
338 for( i = 0; i <= H264_PPS_ID_MAX; i++ )
340 if( p_sys->pp_pps[i] )
341 block_Release( p_sys->pp_pps[i] );
343 packetizer_Clean( &p_sys->packetizer );
345 cc_storage_delete( p_sys->p_ccs );
347 free( p_sys );
350 static void PacketizeFlush( decoder_t *p_dec )
352 decoder_sys_t *p_sys = p_dec->p_sys;
354 packetizer_Flush( &p_sys->packetizer );
357 /****************************************************************************
358 * Packetize: the whole thing
359 * Search for the startcodes 3 or more bytes
360 * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
361 ****************************************************************************/
362 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
364 decoder_sys_t *p_sys = p_dec->p_sys;
366 return packetizer_Packetize( &p_sys->packetizer, pp_block );
369 /****************************************************************************
370 * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
371 * Will always use 4 byte 0 0 0 1 startcodes
372 * Will prepend a SPS and PPS before each keyframe
373 ****************************************************************************/
374 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
376 decoder_sys_t *p_sys = p_dec->p_sys;
378 return PacketizeXXC1( p_dec, p_sys->i_avcC_length_size,
379 pp_block, ParseNALBlock );
382 /*****************************************************************************
383 * GetCc:
384 *****************************************************************************/
385 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
387 return cc_storage_get_current( p_dec->p_sys->p_ccs, pb_present );
390 /****************************************************************************
391 * Helpers
392 ****************************************************************************/
393 static void PacketizeReset( void *p_private, bool b_broken )
395 decoder_t *p_dec = p_private;
396 decoder_sys_t *p_sys = p_dec->p_sys;
398 if( b_broken )
400 if( p_sys->p_frame )
401 block_ChainRelease( p_sys->p_frame );
402 p_sys->p_frame = NULL;
403 p_sys->pp_frame_last = &p_sys->p_frame;
404 p_sys->b_frame_sps = false;
405 p_sys->b_frame_pps = false;
406 p_sys->slice.i_frame_type = 0;
407 p_sys->b_slice = false;
409 p_sys->i_frame_pts = VLC_TS_INVALID;
410 p_sys->i_frame_dts = VLC_TS_INVALID;
411 p_sys->i_prev_dts = VLC_TS_INVALID;
412 p_sys->i_prev_pts = VLC_TS_INVALID;
413 p_sys->b_even_frame = false;
415 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
417 decoder_t *p_dec = p_private;
419 /* Remove trailing 0 bytes */
420 while( p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
421 p_block->i_buffer--;
423 return ParseNALBlock( p_dec, pb_ts_used, p_block );
425 static int PacketizeValidate( void *p_private, block_t *p_au )
427 VLC_UNUSED(p_private);
428 VLC_UNUSED(p_au);
429 return VLC_SUCCESS;
432 /*****************************************************************************
433 * ParseNALBlock: parses annexB type NALs
434 * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
435 *****************************************************************************/
436 static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_ts_used, block_t *p_frag )
438 decoder_sys_t *p_sys = p_dec->p_sys;
439 block_t *p_pic = NULL;
440 bool b_new_picture = false;
442 const int i_nal_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
443 const int i_nal_type = p_frag->p_buffer[4]&0x1f;
444 const mtime_t i_frag_dts = p_frag->i_dts;
445 const mtime_t i_frag_pts = p_frag->i_pts;
447 if( p_sys->b_slice && ( !p_sys->b_sps || !p_sys->b_pps ) )
449 block_ChainRelease( p_sys->p_frame );
450 msg_Warn( p_dec, "waiting for SPS/PPS" );
452 /* Reset context */
453 p_sys->slice.i_frame_type = 0;
454 p_sys->p_frame = NULL;
455 p_sys->pp_frame_last = &p_sys->p_frame;
456 p_sys->b_frame_sps = false;
457 p_sys->b_frame_pps = false;
458 p_sys->b_slice = false;
459 cc_storage_reset( p_sys->p_ccs );
462 if( ( !p_sys->b_sps || !p_sys->b_pps ) &&
463 i_nal_type >= H264_NAL_SLICE && i_nal_type <= H264_NAL_SLICE_IDR )
465 p_sys->b_slice = true;
466 /* Fragment will be discarded later on */
468 else if( i_nal_type >= H264_NAL_SLICE && i_nal_type <= H264_NAL_SLICE_IDR )
470 slice_t slice;
472 if(ParseSlice( p_dec, &b_new_picture, &slice, i_nal_ref_idc, i_nal_type, p_frag ))
474 /* */
475 if( b_new_picture && p_sys->b_slice )
476 p_pic = OutputPicture( p_dec );
478 /* */
479 p_sys->slice = slice;
480 p_sys->b_slice = true;
483 else if( i_nal_type == H264_NAL_SPS )
485 if( p_sys->b_slice )
486 p_pic = OutputPicture( p_dec );
487 p_sys->b_frame_sps = true;
489 PutSPS( p_dec, p_frag );
491 /* Do not append the SPS because we will insert it on keyframes */
492 p_frag = NULL;
494 else if( i_nal_type == H264_NAL_PPS )
496 if( p_sys->b_slice )
497 p_pic = OutputPicture( p_dec );
498 p_sys->b_frame_pps = true;
500 PutPPS( p_dec, p_frag );
502 /* Do not append the PPS because we will insert it on keyframes */
503 p_frag = NULL;
505 else if( i_nal_type == H264_NAL_AU_DELIMITER ||
506 i_nal_type == H264_NAL_SEI ||
507 ( i_nal_type >= H264_NAL_PREFIX && i_nal_type <= H264_NAL_RESERVED_18 ) )
509 if( p_sys->b_slice )
510 p_pic = OutputPicture( p_dec );
512 /* Parse SEI for CC support */
513 if( i_nal_type == H264_NAL_SEI )
515 HxxxParse_AnnexB_SEI( p_frag->p_buffer, p_frag->i_buffer,
516 1 /* nal header */, ParseSeiCallback, p_dec );
518 else if( i_nal_type == H264_NAL_AU_DELIMITER )
520 if( p_sys->p_frame && (p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD) )
522 block_Release( p_frag );
523 p_frag = NULL;
525 else
527 p_frag->i_flags |= BLOCK_FLAG_PRIVATE_AUD;
532 /* Append the block */
533 if( p_frag )
534 block_ChainLastAppend( &p_sys->pp_frame_last, p_frag );
536 *pb_ts_used = false;
537 if( p_sys->i_frame_dts <= VLC_TS_INVALID &&
538 p_sys->i_frame_pts <= VLC_TS_INVALID && b_new_picture )
540 p_sys->i_frame_dts = i_frag_dts;
541 p_sys->i_frame_pts = i_frag_pts;
542 *pb_ts_used = true;
544 return p_pic;
547 static block_t *OutputPicture( decoder_t *p_dec )
549 decoder_sys_t *p_sys = p_dec->p_sys;
550 block_t *p_pic;
552 if ( !p_sys->b_header && p_sys->i_recovery_frames != -1 )
554 if( p_sys->i_recovery_frames == 0 )
556 msg_Dbg( p_dec, "Recovery from SEI recovery point complete" );
557 p_sys->b_header = true;
559 --p_sys->i_recovery_frames;
562 if( !p_sys->b_header && p_sys->i_recovery_frames == -1 &&
563 p_sys->slice.i_frame_type != BLOCK_FLAG_TYPE_I)
564 return NULL;
566 const bool b_sps_pps_i = p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I &&
567 p_sys->b_sps &&
568 p_sys->b_pps;
569 if( b_sps_pps_i || p_sys->b_frame_sps || p_sys->b_frame_pps )
571 block_t *p_head = NULL;
572 if( p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD )
574 p_head = p_sys->p_frame;
575 p_sys->p_frame = p_sys->p_frame->p_next;
576 if( p_sys->p_frame == NULL )
577 p_sys->pp_frame_last = &p_sys->p_frame;
578 p_head->p_next = NULL;
581 block_t *p_list = NULL;
582 block_t **pp_list_tail = &p_list;
583 for( int i = 0; i <= H264_SPS_ID_MAX && (b_sps_pps_i || p_sys->b_frame_sps); i++ )
585 if( p_sys->pp_sps[i] )
586 block_ChainLastAppend( &pp_list_tail, block_Duplicate( p_sys->pp_sps[i] ) );
588 for( int i = 0; i < H264_PPS_ID_MAX && (b_sps_pps_i || p_sys->b_frame_pps); i++ )
590 if( p_sys->pp_pps[i] )
591 block_ChainLastAppend( &pp_list_tail, block_Duplicate( p_sys->pp_pps[i] ) );
593 if( b_sps_pps_i && p_list )
594 p_sys->b_header = true;
596 if( p_list )
597 block_ChainAppend( &p_head, p_list );
599 if( p_sys->p_frame )
600 block_ChainAppend( &p_head, p_sys->p_frame );
602 p_pic = block_ChainGather( p_head );
604 else
606 p_pic = block_ChainGather( p_sys->p_frame );
609 unsigned i_num_clock_ts = 2;
610 if( p_sys->b_frame_mbs_only == 0 )
612 if( p_sys->b_pic_struct_present_flag && p_sys->i_pic_struct < 9 )
614 const uint8_t rgi_numclock[9] = { 1, 1, 1, 2, 2, 3, 3, 2, 3 };
615 i_num_clock_ts = rgi_numclock[ p_sys->i_pic_struct ];
617 else if( p_sys->slice.i_field_pic_flag ) /* See D-1 and E-6 */
619 i_num_clock_ts = 1;
623 if( p_sys->i_time_scale && p_pic->i_length == 0 )
625 p_pic->i_length = CLOCK_FREQ * i_num_clock_ts *
626 p_sys->i_num_units_in_tick / p_sys->i_time_scale;
629 mtime_t i_field_pts_diff = -1;
630 if( p_sys->b_frame_mbs_only == 0 && p_sys->b_pic_struct_present_flag )
632 switch( p_sys->i_pic_struct )
634 /* Top and Bottom field slices */
635 case 1:
636 case 2:
637 if( !p_sys->b_even_frame )
639 p_pic->i_flags |= (p_sys->i_pic_struct == 1) ? BLOCK_FLAG_TOP_FIELD_FIRST
640 : BLOCK_FLAG_BOTTOM_FIELD_FIRST;
642 else if( p_pic->i_pts <= VLC_TS_INVALID && p_sys->i_prev_pts > VLC_TS_INVALID && p_pic->i_length )
644 /* interpolate from even frame */
645 i_field_pts_diff = p_pic->i_length;
648 p_sys->b_even_frame = !p_sys->b_even_frame;
649 break;
650 /* Each of the following slices contains multiple fields */
651 case 3:
652 p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST;
653 p_sys->b_even_frame = false;
654 break;
655 case 4:
656 p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST;
657 p_sys->b_even_frame = false;
658 break;
659 case 5:
660 p_pic->i_flags |= BLOCK_FLAG_TOP_FIELD_FIRST;
661 break;
662 case 6:
663 p_pic->i_flags |= BLOCK_FLAG_BOTTOM_FIELD_FIRST;
664 break;
665 default:
666 p_sys->b_even_frame = false;
667 break;
671 /* set dts/pts to current block timestamps */
672 p_pic->i_dts = p_sys->i_frame_dts;
673 p_pic->i_pts = p_sys->i_frame_pts;
675 /* Fixup missing timestamps after split (multiple AU/block)*/
676 if( p_pic->i_dts <= VLC_TS_INVALID )
677 p_pic->i_dts = p_sys->i_prev_dts;
679 /* PTS Fixup, interlaced fields (multiple AU/block) */
680 if( p_pic->i_pts <= VLC_TS_INVALID && p_sys->i_time_scale )
682 mtime_t i_pts_delay = CLOCK_FREQ * p_sys->i_dpb_output_delay *
683 p_sys->i_num_units_in_tick / p_sys->i_time_scale;
684 p_pic->i_pts = p_pic->i_dts + i_pts_delay;
685 if( i_field_pts_diff >= 0 )
686 p_pic->i_pts += i_field_pts_diff;
689 /* save for next pic fixups */
690 p_sys->i_prev_dts = p_pic->i_dts;
691 p_sys->i_prev_pts = p_pic->i_pts;
693 p_pic->i_flags |= p_sys->slice.i_frame_type;
694 p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD;
695 if( !p_sys->b_header )
696 p_pic->i_flags |= BLOCK_FLAG_PREROLL;
698 /* reset after output */
699 p_sys->i_frame_dts = VLC_TS_INVALID;
700 p_sys->i_frame_pts = VLC_TS_INVALID;
701 p_sys->i_dpb_output_delay = 0;
702 p_sys->slice.i_frame_type = 0;
703 p_sys->p_frame = NULL;
704 p_sys->pp_frame_last = &p_sys->p_frame;
705 p_sys->b_frame_sps = false;
706 p_sys->b_frame_pps = false;
707 p_sys->b_slice = false;
709 /* CC */
710 cc_storage_commit( p_sys->p_ccs, p_pic );
712 return p_pic;
715 static void PutSPS( decoder_t *p_dec, block_t *p_frag )
717 decoder_sys_t *p_sys = p_dec->p_sys;
719 const uint8_t *p_buffer = p_frag->p_buffer;
720 size_t i_buffer = p_frag->i_buffer;
722 if( !hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
723 return;
725 h264_sequence_parameter_set_t *p_sps = h264_decode_sps( p_buffer, i_buffer, true );
726 if( !p_sps )
728 msg_Warn( p_dec, "invalid SPS" );
729 block_Release( p_frag );
730 return;
733 p_dec->fmt_out.i_profile = p_sps->i_profile;
734 p_dec->fmt_out.i_level = p_sps->i_level;
736 (void) h264_get_picture_size( p_sps, &p_dec->fmt_out.video.i_width,
737 &p_dec->fmt_out.video.i_height,
738 &p_dec->fmt_out.video.i_visible_width,
739 &p_dec->fmt_out.video.i_visible_height );
741 if( p_sps->vui.i_sar_num != 0 && p_sps->vui.i_sar_den != 0 )
743 p_dec->fmt_out.video.i_sar_num = p_sps->vui.i_sar_num;
744 p_dec->fmt_out.video.i_sar_den = p_sps->vui.i_sar_den;
747 p_sys->i_log2_max_frame_num = p_sps->i_log2_max_frame_num;
748 p_sys->b_frame_mbs_only = p_sps->frame_mbs_only_flag;
749 p_sys->i_pic_order_cnt_type = p_sps->i_pic_order_cnt_type;
750 p_sys->i_delta_pic_order_always_zero_flag = p_sps->i_delta_pic_order_always_zero_flag;
751 p_sys->i_log2_max_pic_order_cnt_lsb = p_sps->i_log2_max_pic_order_cnt_lsb;
753 if( p_sps->vui.b_valid )
755 p_sys->b_timing_info_present_flag = p_sps->vui.b_timing_info_present_flag;
756 p_sys->i_num_units_in_tick = p_sps->vui.i_num_units_in_tick;
757 p_sys->i_time_scale = p_sps->vui.i_time_scale;
758 p_sys->b_fixed_frame_rate = p_sps->vui.b_fixed_frame_rate;
759 p_sys->b_pic_struct_present_flag = p_sps->vui.b_pic_struct_present_flag;
760 p_sys->b_cpb_dpb_delays_present_flag = p_sps->vui.b_hrd_parameters_present_flag;
761 p_sys->i_cpb_removal_delay_length_minus1 = p_sps->vui.i_cpb_removal_delay_length_minus1;
762 p_sys->i_dpb_output_delay_length_minus1 = p_sps->vui.i_dpb_output_delay_length_minus1;
764 if( p_sps->vui.b_fixed_frame_rate && !p_dec->fmt_out.video.i_frame_rate_base )
766 p_dec->fmt_out.video.i_frame_rate_base = p_sps->vui.i_num_units_in_tick;
767 p_dec->fmt_out.video.i_frame_rate = p_sps->vui.i_time_scale >> 1 /* num_clock_ts == 2 */;
769 if( p_dec->fmt_out.video.primaries == COLOR_PRIMARIES_UNDEF )
771 p_dec->fmt_out.video.primaries =
772 hxxx_colour_primaries_to_vlc( p_sps->vui.colour.i_colour_primaries );
773 p_dec->fmt_out.video.transfer =
774 hxxx_transfer_characteristics_to_vlc( p_sps->vui.colour.i_transfer_characteristics );
775 p_dec->fmt_out.video.space =
776 hxxx_matrix_coeffs_to_vlc( p_sps->vui.colour.i_matrix_coefficients );
777 p_dec->fmt_out.video.b_color_range_full = p_sps->vui.colour.b_full_range;
780 /* We have a new SPS */
781 if( !p_sys->b_sps )
782 msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", p_sps->i_id );
783 p_sys->b_sps = true;
785 if( p_sys->pp_sps[p_sps->i_id] )
786 block_Release( p_sys->pp_sps[p_sps->i_id] );
787 p_sys->pp_sps[p_sps->i_id] = p_frag;
789 h264_release_sps( p_sps );
792 static void PutPPS( decoder_t *p_dec, block_t *p_frag )
794 decoder_sys_t *p_sys = p_dec->p_sys;
795 const uint8_t *p_buffer = p_frag->p_buffer;
796 size_t i_buffer = p_frag->i_buffer;
798 if( !hxxx_strip_AnnexB_startcode( &p_buffer, &i_buffer ) )
799 return;
801 h264_picture_parameter_set_t *p_pps = h264_decode_pps( p_buffer, i_buffer, true );
802 if( !p_pps )
804 msg_Warn( p_dec, "invalid PPS" );
805 block_Release( p_frag );
806 return;
808 p_sys->i_pic_order_present_flag = p_pps->i_pic_order_present_flag;
810 /* We have a new PPS */
811 if( !p_sys->b_pps )
812 msg_Dbg( p_dec, "found NAL_PPS (pps_id=%d sps_id=%d)", p_pps->i_id, p_pps->i_sps_id );
813 p_sys->b_pps = true;
815 if( p_sys->pp_pps[p_pps->i_id] )
816 block_Release( p_sys->pp_pps[p_pps->i_id] );
817 p_sys->pp_pps[p_pps->i_id] = p_frag;
819 h264_release_pps( p_pps );
822 static bool ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,
823 int i_nal_ref_idc, int i_nal_type, const block_t *p_frag )
825 decoder_sys_t *p_sys = p_dec->p_sys;
826 int i_slice_type;
827 slice_t slice;
828 bs_t s;
829 unsigned i_bitflow = 0;
831 const uint8_t *p_stripped = p_frag->p_buffer;
832 size_t i_stripped = p_frag->i_buffer;
834 if( !hxxx_strip_AnnexB_startcode( &p_stripped, &i_stripped ) || i_stripped < 2 )
835 return false;
837 bs_init( &s, p_stripped, i_stripped );
838 s.p_fwpriv = &i_bitflow;
839 s.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
840 bs_skip( &s, 8 ); /* nal unit header */
842 /* first_mb_in_slice */
843 /* int i_first_mb = */ bs_read_ue( &s );
845 /* slice_type */
846 switch( (i_slice_type = bs_read_ue( &s )) )
848 case 0: case 5:
849 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
850 break;
851 case 1: case 6:
852 slice.i_frame_type = BLOCK_FLAG_TYPE_B;
853 break;
854 case 2: case 7:
855 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
856 break;
857 case 3: case 8: /* SP */
858 slice.i_frame_type = BLOCK_FLAG_TYPE_P;
859 break;
860 case 4: case 9:
861 slice.i_frame_type = BLOCK_FLAG_TYPE_I;
862 break;
863 default:
864 slice.i_frame_type = 0;
865 break;
868 /* */
869 slice.i_nal_type = i_nal_type;
870 slice.i_nal_ref_idc = i_nal_ref_idc;
872 slice.i_pic_parameter_set_id = bs_read_ue( &s );
873 slice.i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
875 slice.i_field_pic_flag = 0;
876 slice.i_bottom_field_flag = -1;
877 if( !p_sys->b_frame_mbs_only )
879 /* field_pic_flag */
880 slice.i_field_pic_flag = bs_read( &s, 1 );
881 if( slice.i_field_pic_flag )
882 slice.i_bottom_field_flag = bs_read( &s, 1 );
885 slice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
886 if( slice.i_nal_type == H264_NAL_SLICE_IDR )
887 slice.i_idr_pic_id = bs_read_ue( &s );
889 slice.i_pic_order_cnt_lsb = -1;
890 slice.i_delta_pic_order_cnt_bottom = -1;
891 slice.i_delta_pic_order_cnt0 = 0;
892 slice.i_delta_pic_order_cnt1 = 0;
893 if( p_sys->i_pic_order_cnt_type == 0 )
895 slice.i_pic_order_cnt_lsb = bs_read( &s, p_sys->i_log2_max_pic_order_cnt_lsb + 4 );
896 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
897 slice.i_delta_pic_order_cnt_bottom = bs_read_se( &s );
899 else if( (p_sys->i_pic_order_cnt_type == 1) &&
900 (!p_sys->i_delta_pic_order_always_zero_flag) )
902 slice.i_delta_pic_order_cnt0 = bs_read_se( &s );
903 if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
904 slice.i_delta_pic_order_cnt1 = bs_read_se( &s );
907 /* Detection of the first VCL NAL unit of a primary coded picture
908 * (cf. 7.4.1.2.4) */
909 bool b_pic = false;
910 if( slice.i_frame_num != p_sys->slice.i_frame_num ||
911 slice.i_pic_parameter_set_id != p_sys->slice.i_pic_parameter_set_id ||
912 slice.i_field_pic_flag != p_sys->slice.i_field_pic_flag ||
913 !slice.i_nal_ref_idc != !p_sys->slice.i_nal_ref_idc )
914 b_pic = true;
915 if( (slice.i_bottom_field_flag != -1) &&
916 (p_sys->slice.i_bottom_field_flag != -1) &&
917 (slice.i_bottom_field_flag != p_sys->slice.i_bottom_field_flag) )
918 b_pic = true;
919 if( p_sys->i_pic_order_cnt_type == 0 &&
920 ( slice.i_pic_order_cnt_lsb != p_sys->slice.i_pic_order_cnt_lsb ||
921 slice.i_delta_pic_order_cnt_bottom != p_sys->slice.i_delta_pic_order_cnt_bottom ) )
922 b_pic = true;
923 else if( p_sys->i_pic_order_cnt_type == 1 &&
924 ( slice.i_delta_pic_order_cnt0 != p_sys->slice.i_delta_pic_order_cnt0 ||
925 slice.i_delta_pic_order_cnt1 != p_sys->slice.i_delta_pic_order_cnt1 ) )
926 b_pic = true;
927 if( ( slice.i_nal_type == H264_NAL_SLICE_IDR || p_sys->slice.i_nal_type == H264_NAL_SLICE_IDR ) &&
928 ( slice.i_nal_type != p_sys->slice.i_nal_type || slice.i_idr_pic_id != p_sys->slice.i_idr_pic_id ) )
929 b_pic = true;
931 /* */
932 *pb_new_picture = b_pic;
933 *p_slice = slice;
935 return true;
938 static bool ParseSeiCallback( const hxxx_sei_data_t *p_sei_data, void *cbdata )
940 decoder_t *p_dec = (decoder_t *) cbdata;
941 decoder_sys_t *p_sys = p_dec->p_sys;
943 switch( p_sei_data->i_type )
945 /* Look for pic timing */
946 case HXXX_SEI_PIC_TIMING:
948 if( p_sys->b_cpb_dpb_delays_present_flag )
950 bs_read( p_sei_data->p_bs, p_sys->i_cpb_removal_delay_length_minus1 + 1 );
951 p_sys->i_dpb_output_delay =
952 bs_read( p_sei_data->p_bs, p_sys->i_dpb_output_delay_length_minus1 + 1 );
955 if( p_sys->b_pic_struct_present_flag )
956 p_sys->i_pic_struct = bs_read( p_sei_data->p_bs, 4 );
957 /* + unparsed remains */
958 } break;
960 /* Look for user_data_registered_itu_t_t35 */
961 case HXXX_SEI_USER_DATA_REGISTERED_ITU_T_T35:
963 if( p_sei_data->itu_t35.type == HXXX_ITU_T35_TYPE_CC )
965 cc_storage_append( p_sys->p_ccs, true, p_sei_data->itu_t35.u.cc.p_data,
966 p_sei_data->itu_t35.u.cc.i_data );
968 } break;
970 /* Look for SEI recovery point */
971 case HXXX_SEI_RECOVERY_POINT:
973 if( !p_sys->b_header )
975 msg_Dbg( p_dec, "Seen SEI recovery point, %d recovery frames", p_sei_data->recovery.i_frames );
976 if ( p_sys->i_recovery_frames == -1 || p_sei_data->recovery.i_frames < p_sys->i_recovery_frames )
977 p_sys->i_recovery_frames = p_sei_data->recovery.i_frames;
979 } break;
981 default:
982 /* Will skip */
983 break;
986 return true;