packetizer: hevc: add poc debug
[vlc.git] / modules / packetizer / h264_slice.c
blob964fcafe882987c02be4365f8049d265dc8c6966
1 /*****************************************************************************
2 * h264_slice.c: h264 slice parser
3 *****************************************************************************
4 * Copyright (C) 2001-17 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <vlc_common.h>
25 #include <vlc_bits.h>
27 #include "h264_nal.h"
28 #include "h264_slice.h"
29 #include "hxxx_nal.h"
31 bool h264_decode_slice( const uint8_t *p_buffer, size_t i_buffer,
32 void (* get_sps_pps)(uint8_t, void *,
33 const h264_sequence_parameter_set_t **,
34 const h264_picture_parameter_set_t ** ),
35 void *priv, h264_slice_t *p_slice )
37 int i_slice_type;
38 h264_slice_init( p_slice );
39 bs_t s;
40 unsigned i_bitflow = 0;
41 bs_init( &s, p_buffer, i_buffer );
42 s.p_fwpriv = &i_bitflow;
43 s.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
45 /* nal unit header */
46 bs_skip( &s, 1 );
47 const uint8_t i_nal_ref_idc = bs_read( &s, 2 );
48 const uint8_t i_nal_type = bs_read( &s, 5 );
50 /* first_mb_in_slice */
51 /* int i_first_mb = */ bs_read_ue( &s );
53 /* slice_type */
54 i_slice_type = bs_read_ue( &s );
55 p_slice->type = i_slice_type % 5;
57 /* */
58 p_slice->i_nal_type = i_nal_type;
59 p_slice->i_nal_ref_idc = i_nal_ref_idc;
61 p_slice->i_pic_parameter_set_id = bs_read_ue( &s );
62 if( p_slice->i_pic_parameter_set_id > H264_PPS_ID_MAX )
63 return false;
65 const h264_sequence_parameter_set_t *p_sps;
66 const h264_picture_parameter_set_t *p_pps;
68 /* Bind matched/referred PPS and SPS */
69 get_sps_pps( p_slice->i_pic_parameter_set_id, priv, &p_sps, &p_pps );
70 if( !p_sps || !p_pps )
71 return false;
73 p_slice->i_frame_num = bs_read( &s, p_sps->i_log2_max_frame_num + 4 );
75 if( !p_sps->frame_mbs_only_flag )
77 /* field_pic_flag */
78 p_slice->i_field_pic_flag = bs_read( &s, 1 );
79 if( p_slice->i_field_pic_flag )
80 p_slice->i_bottom_field_flag = bs_read( &s, 1 );
83 if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
84 p_slice->i_idr_pic_id = bs_read_ue( &s );
86 p_slice->i_pic_order_cnt_type = p_sps->i_pic_order_cnt_type;
87 if( p_sps->i_pic_order_cnt_type == 0 )
89 p_slice->i_pic_order_cnt_lsb = bs_read( &s, p_sps->i_log2_max_pic_order_cnt_lsb + 4 );
90 if( p_pps->i_pic_order_present_flag && !p_slice->i_field_pic_flag )
91 p_slice->i_delta_pic_order_cnt_bottom = bs_read_se( &s );
93 else if( (p_sps->i_pic_order_cnt_type == 1) &&
94 (!p_sps->i_delta_pic_order_always_zero_flag) )
96 p_slice->i_delta_pic_order_cnt0 = bs_read_se( &s );
97 if( p_pps->i_pic_order_present_flag && !p_slice->i_field_pic_flag )
98 p_slice->i_delta_pic_order_cnt1 = bs_read_se( &s );
101 if( p_pps->i_redundant_pic_present_flag )
102 bs_read_ue( &s ); /* redudant_pic_count */
104 unsigned num_ref_idx_l01_active_minus1[2] = {0 , 0};
106 if( i_slice_type == 1 || i_slice_type == 6 ) /* B slices */
107 bs_read1( &s ); /* direct_spatial_mv_pred_flag */
108 if( i_slice_type == 0 || i_slice_type == 5 ||
109 i_slice_type == 3 || i_slice_type == 8 ||
110 i_slice_type == 1 || i_slice_type == 6 ) /* P SP B slices */
112 if( bs_read1( &s ) ) /* num_ref_idx_active_override_flag */
114 num_ref_idx_l01_active_minus1[0] = bs_read_ue( &s );
115 if( i_slice_type == 1 || i_slice_type == 6 ) /* B slices */
116 num_ref_idx_l01_active_minus1[1] = bs_read_ue( &s );
120 /* BELOW, Further processing up to assert MMCO 5 presence for POC */
121 if( p_slice->i_nal_type == 5 || p_slice->i_nal_ref_idc == 0 )
123 /* Early END, don't waste parsing below */
124 p_slice->has_mmco5 = false;
125 return true;
128 /* ref_pic_list_[mvc_]modification() */
129 const bool b_mvc = (p_slice->i_nal_type == 20 || p_slice->i_nal_type == 21 );
130 unsigned i = 0;
131 if( i_slice_type % 5 != 2 && i_slice_type % 5 != 4 )
132 i++;
133 if( i_slice_type % 5 == 1 )
134 i++;
136 for( ; i>0; i-- )
138 if( bs_read1( &s ) ) /* ref_pic_list_modification_flag_l{0,1} */
140 uint32_t mod;
143 mod = bs_read_ue( &s );
144 if( mod < 3 || ( b_mvc && (mod == 4 || mod == 5) ) )
145 bs_read_ue( &s ); /* abs_diff_pic_num_minus1, long_term_pic_num, abs_diff_view_idx_min1 */
147 while( mod != 3 && bs_remain( &s ) );
151 /* pred_weight_table() */
152 if( ( p_pps->weighted_pred_flag && ( i_slice_type == 0 || i_slice_type == 5 || /* P, SP */
153 i_slice_type == 3 || i_slice_type == 8 ) ) ||
154 ( p_pps->weighted_bipred_idc == 1 && ( i_slice_type == 1 || i_slice_type == 6 ) /* B */ ) )
156 bs_read_ue( &s ); /* luma_log2_weight_denom */
157 if( !p_sps->b_separate_colour_planes_flag ) /* ChromaArrayType != 0 */
158 bs_read_ue( &s ); /* chroma_log2_weight_denom */
160 const unsigned i_num_layers = ( i_slice_type % 5 == 1 ) ? 2 : 1;
161 for( unsigned j=0; j < i_num_layers; j++ )
163 for( unsigned k=0; k<=num_ref_idx_l01_active_minus1[j]; k++ )
165 if( bs_read1( &s ) ) /* luma_weight_l{0,1}_flag */
167 bs_read_se( &s );
168 bs_read_se( &s );
170 if( !p_sps->b_separate_colour_planes_flag ) /* ChromaArrayType != 0 */
172 if( bs_read1( &s ) ) /* chroma_weight_l{0,1}_flag */
174 bs_read_se( &s );
175 bs_read_se( &s );
176 bs_read_se( &s );
177 bs_read_se( &s );
184 /* dec_ref_pic_marking() */
185 if( p_slice->i_nal_type != 5 ) /* IdrFlag */
187 if( bs_read1( &s ) ) /* adaptive_ref_pic_marking_mode_flag */
189 uint32_t mmco;
192 mmco = bs_read_ue( &s );
193 if( mmco == 1 || mmco == 3 )
194 bs_read_ue( &s ); /* diff_pics_minus1 */
195 if( mmco == 2 )
196 bs_read_ue( &s ); /* long_term_pic_num */
197 if( mmco == 3 || mmco == 6 )
198 bs_read_ue( &s ); /* long_term_frame_idx */
199 if( mmco == 4 )
200 bs_read_ue( &s ); /* max_long_term_frame_idx_plus1 */
201 if( mmco == 5 )
203 p_slice->has_mmco5 = true;
204 break; /* Early END */
207 while( mmco > 0 );
211 /* If you need to store anything else than MMCO presence above, care of "Early END" cases */
213 return true;
217 void h264_compute_poc( const h264_sequence_parameter_set_t *p_sps,
218 const h264_slice_t *p_slice, h264_poc_context_t *p_ctx,
219 int *p_PictureOrderCount, int *p_tFOC, int *p_bFOC )
221 *p_tFOC = *p_bFOC = 0;
223 if( p_sps->i_pic_order_cnt_type == 0 )
225 unsigned maxPocLSB = 1U << (p_sps->i_log2_max_pic_order_cnt_lsb + 4);
227 /* POC reference */
228 if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
230 p_ctx->prevPicOrderCnt.lsb = 0;
231 p_ctx->prevPicOrderCnt.msb = 0;
233 else if( p_ctx->prevRefPictureHasMMCO5 )
235 p_ctx->prevPicOrderCnt.msb = 0;
236 if( !p_ctx->prevRefPictureIsBottomField )
237 p_ctx->prevPicOrderCnt.lsb = p_ctx->prevRefPictureTFOC;
238 else
239 p_ctx->prevPicOrderCnt.lsb = 0;
242 /* 8.2.1.1 */
243 int pocMSB = p_ctx->prevPicOrderCnt.msb;
244 int64_t orderDiff = p_slice->i_pic_order_cnt_lsb - p_ctx->prevPicOrderCnt.lsb;
245 if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 )
246 pocMSB += maxPocLSB;
247 else if( orderDiff > maxPocLSB / 2 )
248 pocMSB -= maxPocLSB;
250 *p_tFOC = *p_bFOC = pocMSB + p_slice->i_pic_order_cnt_lsb;
251 if( p_slice->i_field_pic_flag )
252 *p_bFOC += p_slice->i_delta_pic_order_cnt_bottom;
254 /* Save from ref picture */
255 if( p_slice->i_nal_ref_idc /* Is reference */ )
257 p_ctx->prevRefPictureIsBottomField = (p_slice->i_field_pic_flag &&
258 p_slice->i_bottom_field_flag);
259 p_ctx->prevRefPictureHasMMCO5 = p_slice->has_mmco5;
260 p_ctx->prevRefPictureTFOC = *p_tFOC;
261 p_ctx->prevPicOrderCnt.lsb = p_slice->i_pic_order_cnt_lsb;
262 p_ctx->prevPicOrderCnt.msb = pocMSB;
265 else
267 unsigned maxFrameNum = 1 << (p_sps->i_log2_max_frame_num + 4);
268 unsigned frameNumOffset;
269 unsigned expectedPicOrderCnt = 0;
271 if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
272 frameNumOffset = 0;
273 else if( p_ctx->prevFrameNum > (unsigned) p_slice->i_frame_num )
274 frameNumOffset = p_ctx->prevFrameNumOffset + maxFrameNum;
275 else
276 frameNumOffset = p_ctx->prevFrameNumOffset;
278 if( p_sps->i_pic_order_cnt_type == 1 )
280 unsigned absFrameNum;
282 if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 0 )
283 absFrameNum = frameNumOffset + p_slice->i_frame_num;
284 else
285 absFrameNum = 0;
287 if( p_slice->i_nal_ref_idc == 0 && absFrameNum > 0 )
288 absFrameNum--;
290 if( absFrameNum > 0 )
292 int32_t expectedDeltaPerPicOrderCntCycle = 0;
293 for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ )
294 expectedDeltaPerPicOrderCntCycle += p_sps->offset_for_ref_frame[i];
296 unsigned picOrderCntCycleCnt = 0;
297 unsigned frameNumInPicOrderCntCycle = 0;
298 if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle )
300 picOrderCntCycleCnt = ( absFrameNum - 1 ) / p_sps->i_num_ref_frames_in_pic_order_cnt_cycle;
301 frameNumInPicOrderCntCycle = ( absFrameNum - 1 ) % p_sps->i_num_ref_frames_in_pic_order_cnt_cycle;
304 expectedPicOrderCnt = picOrderCntCycleCnt * expectedDeltaPerPicOrderCntCycle;
305 for( unsigned i=0; i <= frameNumInPicOrderCntCycle; i++ )
306 expectedPicOrderCnt = expectedPicOrderCnt + p_sps->offset_for_ref_frame[i];
309 if( p_slice->i_nal_ref_idc == 0 )
310 expectedPicOrderCnt = expectedPicOrderCnt + p_sps->offset_for_non_ref_pic;
312 *p_tFOC = expectedPicOrderCnt + p_slice->i_delta_pic_order_cnt0;
313 if( !p_slice->i_field_pic_flag )
314 *p_bFOC = *p_tFOC + p_sps->offset_for_top_to_bottom_field + p_slice->i_delta_pic_order_cnt1;
315 else if( p_slice->i_bottom_field_flag )
316 *p_bFOC = expectedPicOrderCnt + p_sps->offset_for_top_to_bottom_field + p_slice->i_delta_pic_order_cnt0;
318 else if( p_sps->i_pic_order_cnt_type == 2 )
320 unsigned tempPicOrderCnt;
322 if( p_slice->i_nal_type == H264_NAL_SLICE_IDR )
323 tempPicOrderCnt = 0;
324 else if( p_slice->i_nal_ref_idc == 0 )
325 tempPicOrderCnt = 2 * ( frameNumOffset + p_slice->i_frame_num ) - 1;
326 else
327 tempPicOrderCnt = 2 * ( frameNumOffset + p_slice->i_frame_num );
329 *p_bFOC = *p_tFOC = tempPicOrderCnt;
332 p_ctx->prevFrameNum = p_slice->i_frame_num;
333 if( p_slice->has_mmco5 )
334 p_ctx->prevFrameNumOffset = 0;
335 else
336 p_ctx->prevFrameNumOffset = frameNumOffset;
339 /* 8.2.1 (8-1) */
340 if( !p_slice->i_field_pic_flag ) /* progressive or contains both fields */
341 *p_PictureOrderCount = __MIN( *p_bFOC, *p_tFOC );
342 else /* split top or bottom field */
343 if ( p_slice->i_bottom_field_flag )
344 *p_PictureOrderCount = *p_bFOC;
345 else
346 *p_PictureOrderCount = *p_tFOC;
349 static uint8_t h264_infer_pic_struct( const h264_sequence_parameter_set_t *p_sps,
350 const h264_slice_t *p_slice,
351 uint8_t i_pic_struct, int tFOC, int bFOC )
353 /* See D-1 and note 6 */
354 if( !p_sps->vui.b_pic_struct_present_flag || i_pic_struct >= 9 )
356 if( p_slice->i_field_pic_flag )
357 i_pic_struct = 1 + p_slice->i_bottom_field_flag;
358 else if( tFOC == bFOC )
359 i_pic_struct = 0;
360 else if( tFOC < bFOC )
361 i_pic_struct = 3;
362 else
363 i_pic_struct = 4;
366 return i_pic_struct;
369 uint8_t h264_get_num_ts( const h264_sequence_parameter_set_t *p_sps,
370 const h264_slice_t *p_slice, uint8_t i_pic_struct,
371 int tFOC, int bFOC )
373 i_pic_struct = h264_infer_pic_struct( p_sps, p_slice, i_pic_struct, tFOC, bFOC );
374 /* !WARN modified with nuit field based multiplier for values 0, 7 and 8 */
375 const uint8_t rgi_numclock[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
376 return rgi_numclock[ i_pic_struct ];