/src/vlc/modules/packetizer/h264_slice.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * h264_slice.c: h264 slice parser |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2001-17 VLC authors and VideoLAN |
5 | | * |
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. |
10 | | * |
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. |
15 | | * |
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 |
23 | | |
24 | | #include <vlc_common.h> |
25 | | #include <vlc_bits.h> |
26 | | |
27 | | #include "h264_nal.h" |
28 | | #include "h264_slice.h" |
29 | | #include "hxxx_nal.h" |
30 | | #include "hxxx_ep3b.h" |
31 | | |
32 | | struct h264_slice_s |
33 | | { |
34 | | int i_nal_type; |
35 | | int i_nal_ref_idc; |
36 | | |
37 | | int type; |
38 | | int i_pic_parameter_set_id; |
39 | | unsigned i_frame_num; |
40 | | |
41 | | int i_field_pic_flag; |
42 | | int i_bottom_field_flag; |
43 | | |
44 | | int i_idr_pic_id; |
45 | | |
46 | | int i_pic_order_cnt_type; |
47 | | int i_pic_order_cnt_lsb; |
48 | | int i_delta_pic_order_cnt_bottom; |
49 | | |
50 | | int i_delta_pic_order_cnt0; |
51 | | int i_delta_pic_order_cnt1; |
52 | | |
53 | | bool no_output_of_prior_pics_flag; |
54 | | bool has_mmco5; |
55 | | }; |
56 | | |
57 | | enum h264_slice_type_e h264_get_slice_type( const h264_slice_t *p_slice ) |
58 | 822k | { |
59 | 822k | return p_slice->type; |
60 | 822k | } |
61 | | |
62 | | bool h264_has_mmco5( const h264_slice_t *p_slice ) |
63 | 0 | { |
64 | 0 | return p_slice->has_mmco5; |
65 | 0 | } |
66 | | |
67 | | bool h264_is_field_pic( const h264_slice_t *p_slice ) |
68 | 0 | { |
69 | 0 | return p_slice->i_field_pic_flag; |
70 | 0 | } |
71 | | |
72 | | int h264_get_slice_pps_id( const h264_slice_t *p_slice ) |
73 | 257k | { |
74 | 257k | return p_slice->i_pic_parameter_set_id; |
75 | 257k | } |
76 | | |
77 | | unsigned h264_get_frame_num( const h264_slice_t *p_slice ) |
78 | 224k | { |
79 | 224k | return p_slice->i_frame_num; |
80 | 224k | } |
81 | | |
82 | | unsigned h264_get_nal_ref_idc( const h264_slice_t *p_slice ) |
83 | 0 | { |
84 | 0 | return p_slice->i_nal_ref_idc; |
85 | 0 | } |
86 | | |
87 | | void h264_slice_release( h264_slice_t *p_slice ) |
88 | 576k | { |
89 | 576k | free( p_slice ); |
90 | 576k | } |
91 | | |
92 | | void h264_slice_copy_idr_id( const h264_slice_t *src, h264_slice_t *dst ) |
93 | 257k | { |
94 | 257k | if( !src || src->i_nal_type != H264_NAL_SLICE_IDR ) /* value only set on IDR */ |
95 | 114k | return; |
96 | 143k | dst->i_idr_pic_id = src->i_idr_pic_id; |
97 | 143k | } |
98 | | |
99 | | h264_slice_t * h264_decode_slice( const uint8_t *p_buffer, size_t i_buffer, |
100 | | void (* get_sps_pps)(uint8_t, void *, |
101 | | const h264_sequence_parameter_set_t **, |
102 | | const h264_picture_parameter_set_t ** ), |
103 | | void *priv ) |
104 | 338k | { |
105 | 338k | h264_slice_t *p_slice = calloc( 1, sizeof(*p_slice) ); |
106 | 338k | if( !p_slice ) |
107 | 0 | return NULL; |
108 | | |
109 | 338k | int i_slice_type; |
110 | 338k | bs_t s; |
111 | 338k | struct hxxx_bsfw_ep3b_ctx_s bsctx; |
112 | 338k | hxxx_bsfw_ep3b_ctx_init( &bsctx ); |
113 | 338k | bs_init_custom( &s, p_buffer, i_buffer, &hxxx_bsfw_ep3b_callbacks, &bsctx ); |
114 | | |
115 | | /* nal unit header */ |
116 | 338k | bs_skip( &s, 1 ); |
117 | 338k | const uint8_t i_nal_ref_idc = bs_read( &s, 2 ); |
118 | 338k | const uint8_t i_nal_type = bs_read( &s, 5 ); |
119 | | |
120 | | /* first_mb_in_slice */ |
121 | | /* int i_first_mb = */ bs_read_ue( &s ); |
122 | | |
123 | | /* slice_type */ |
124 | 338k | i_slice_type = bs_read_ue( &s ); |
125 | 338k | if( i_slice_type > 9 ) |
126 | 8.09k | goto error; |
127 | 330k | p_slice->type = i_slice_type % 5; |
128 | | |
129 | | /* */ |
130 | 330k | p_slice->i_nal_type = i_nal_type; |
131 | 330k | p_slice->i_nal_ref_idc = i_nal_ref_idc; |
132 | | |
133 | 330k | p_slice->i_pic_parameter_set_id = bs_read_ue( &s ); |
134 | 330k | if( p_slice->i_pic_parameter_set_id > H264_PPS_ID_MAX ) |
135 | 1.30k | goto error; |
136 | | |
137 | 329k | const h264_sequence_parameter_set_t *p_sps; |
138 | 329k | const h264_picture_parameter_set_t *p_pps; |
139 | | |
140 | | /* Bind matched/referred PPS and SPS */ |
141 | 329k | get_sps_pps( p_slice->i_pic_parameter_set_id, priv, &p_sps, &p_pps ); |
142 | 329k | if( !p_sps || !p_pps ) |
143 | 46.1k | goto error; |
144 | | |
145 | 283k | p_slice->i_frame_num = bs_read( &s, p_sps->i_log2_max_frame_num + 4 ); |
146 | | |
147 | 283k | if( !p_sps->frame_mbs_only_flag ) |
148 | 197k | { |
149 | | /* field_pic_flag */ |
150 | 197k | p_slice->i_field_pic_flag = bs_read( &s, 1 ); |
151 | 197k | if( p_slice->i_field_pic_flag ) |
152 | 35.5k | p_slice->i_bottom_field_flag = bs_read( &s, 1 ); |
153 | 197k | } |
154 | | |
155 | 283k | if( p_slice->i_nal_type == H264_NAL_SLICE_IDR ) |
156 | 159k | { |
157 | 159k | p_slice->i_idr_pic_id = bs_read_ue( &s ); |
158 | 159k | if( p_slice->i_idr_pic_id > 65535 ) |
159 | 420 | goto error; |
160 | 159k | } |
161 | | |
162 | 282k | p_slice->i_pic_order_cnt_type = p_sps->i_pic_order_cnt_type; |
163 | 282k | if( p_sps->i_pic_order_cnt_type == 0 ) |
164 | 199k | { |
165 | 199k | p_slice->i_pic_order_cnt_lsb = bs_read( &s, p_sps->i_log2_max_pic_order_cnt_lsb + 4 ); |
166 | 199k | if( p_pps->i_pic_order_present_flag && !p_slice->i_field_pic_flag ) |
167 | 72.6k | p_slice->i_delta_pic_order_cnt_bottom = bs_read_se( &s ); |
168 | 199k | } |
169 | 83.5k | else if( (p_sps->i_pic_order_cnt_type == 1) && |
170 | 43.9k | (!p_sps->i_delta_pic_order_always_zero_flag) ) |
171 | 16.3k | { |
172 | 16.3k | p_slice->i_delta_pic_order_cnt0 = bs_read_se( &s ); |
173 | 16.3k | if( p_pps->i_pic_order_present_flag && !p_slice->i_field_pic_flag ) |
174 | 10.2k | p_slice->i_delta_pic_order_cnt1 = bs_read_se( &s ); |
175 | 16.3k | } |
176 | | |
177 | 282k | if( p_pps->i_redundant_pic_present_flag ) |
178 | 41.7k | bs_read_ue( &s ); /* redudant_pic_count */ |
179 | | |
180 | 282k | uint32_t num_ref_idx_l01_active_minus1[2] = { p_pps->num_ref_idx_l01_default_active_minus1[0], |
181 | 282k | p_pps->num_ref_idx_l01_default_active_minus1[1] }; |
182 | | |
183 | 282k | if( i_slice_type == 1 || i_slice_type == 6 ) /* B slices */ |
184 | 22.1k | bs_skip( &s, 1 ); /* direct_spatial_mv_pred_flag */ |
185 | 282k | if( i_slice_type == 0 || i_slice_type == 5 || |
186 | 56.2k | i_slice_type == 3 || i_slice_type == 8 || |
187 | 52.5k | i_slice_type == 1 || i_slice_type == 6 ) /* P SP B slices */ |
188 | 252k | { |
189 | 252k | if( bs_read1( &s ) ) /* num_ref_idx_active_override_flag */ |
190 | 31.1k | { |
191 | 31.1k | num_ref_idx_l01_active_minus1[0] = bs_read_ue( &s ); |
192 | 31.1k | if (num_ref_idx_l01_active_minus1[0] > 31) |
193 | 1.84k | num_ref_idx_l01_active_minus1[0] = p_pps->num_ref_idx_l01_default_active_minus1[0]; |
194 | 31.1k | if( i_slice_type == 1 || i_slice_type == 6 ) /* B slices */ |
195 | 9.98k | { |
196 | 9.98k | num_ref_idx_l01_active_minus1[1] = bs_read_ue( &s ); |
197 | 9.98k | if (num_ref_idx_l01_active_minus1[1] > 31) |
198 | 718 | num_ref_idx_l01_active_minus1[1] = p_pps->num_ref_idx_l01_default_active_minus1[1]; |
199 | 9.98k | } |
200 | 31.1k | } |
201 | 252k | } |
202 | | |
203 | | /* BELOW, Further processing up to assert MMCO 5 presence for POC */ |
204 | 282k | if( p_slice->i_nal_type == 5 || p_slice->i_nal_ref_idc == 0 ) |
205 | 226k | { |
206 | | /* Early END, don't waste parsing below */ |
207 | 226k | p_slice->has_mmco5 = false; |
208 | 226k | return p_slice; |
209 | 226k | } |
210 | | |
211 | | /* ref_pic_list_[mvc_]modification() */ |
212 | 55.9k | const bool b_mvc = (p_slice->i_nal_type == 20 || p_slice->i_nal_type == 21 ); |
213 | 55.9k | unsigned i = 0; |
214 | 55.9k | if( i_slice_type % 5 != 2 && i_slice_type % 5 != 4 ) |
215 | 47.1k | i++; |
216 | 55.9k | if( i_slice_type % 5 == 1 ) |
217 | 2.96k | i++; |
218 | | |
219 | 106k | for( ; i>0; i-- ) |
220 | 50.1k | { |
221 | 50.1k | if( bs_read1( &s ) ) /* ref_pic_list_modification_flag_l{0,1} */ |
222 | 12.3k | { |
223 | 12.3k | uint32_t mod; |
224 | 12.3k | do |
225 | 161k | { |
226 | 161k | mod = bs_read_ue( &s ); |
227 | 161k | if( mod < 3 || ( b_mvc && (mod == 4 || mod == 5) ) ) |
228 | 120k | bs_read_ue( &s ); /* abs_diff_pic_num_minus1, long_term_pic_num, abs_diff_view_idx_min1 */ |
229 | 161k | } |
230 | 161k | while( mod != 3 && !bs_eof( &s ) ); |
231 | 12.3k | } |
232 | 50.1k | } |
233 | | |
234 | 55.9k | if( bs_error( &s ) ) |
235 | 13.1k | goto error; |
236 | | |
237 | | /* pred_weight_table() */ |
238 | 42.8k | if( ( p_pps->weighted_pred_flag && ( i_slice_type == 0 || i_slice_type == 5 || /* P, SP */ |
239 | 6.97k | i_slice_type == 3 || i_slice_type == 8 ) ) || |
240 | 24.1k | ( p_pps->weighted_bipred_idc == 1 && ( i_slice_type == 1 || i_slice_type == 6 ) /* B */ ) ) |
241 | 19.2k | { |
242 | 19.2k | bs_read_ue( &s ); /* luma_log2_weight_denom */ |
243 | 19.2k | if( !p_sps->b_separate_colour_planes_flag ) /* ChromaArrayType != 0 */ |
244 | 17.9k | bs_read_ue( &s ); /* chroma_log2_weight_denom */ |
245 | | |
246 | 19.2k | const unsigned i_num_layers = ( i_slice_type % 5 == 1 ) ? 2 : 1; |
247 | 39.1k | for( unsigned j=0; j < i_num_layers; j++ ) |
248 | 19.8k | { |
249 | 97.9k | for( unsigned k=0; k<=num_ref_idx_l01_active_minus1[j]; k++ ) |
250 | 78.0k | { |
251 | 78.0k | if( bs_read1( &s ) ) /* luma_weight_l{0,1}_flag */ |
252 | 17.9k | { |
253 | 17.9k | bs_read_se( &s ); |
254 | 17.9k | bs_read_se( &s ); |
255 | 17.9k | } |
256 | 78.0k | if( !p_sps->b_separate_colour_planes_flag ) /* ChromaArrayType != 0 */ |
257 | 73.2k | { |
258 | 73.2k | if( bs_read1( &s ) ) /* chroma_weight_l{0,1}_flag */ |
259 | 13.0k | { |
260 | 13.0k | bs_read_se( &s ); |
261 | 13.0k | bs_read_se( &s ); |
262 | 13.0k | bs_read_se( &s ); |
263 | 13.0k | bs_read_se( &s ); |
264 | 13.0k | } |
265 | 73.2k | } |
266 | 78.0k | } |
267 | 19.8k | } |
268 | 19.2k | } |
269 | | |
270 | | /* dec_ref_pic_marking() */ |
271 | 42.8k | if( p_slice->i_nal_type == 5 ) /* IdrFlag */ |
272 | 0 | { |
273 | 0 | p_slice->no_output_of_prior_pics_flag = bs_read1( &s ); |
274 | 0 | bs_skip( &s, 1 ); /* long_term_reference_flag */ |
275 | 0 | } |
276 | 42.8k | else |
277 | 42.8k | { |
278 | 42.8k | if( bs_read1( &s ) ) /* adaptive_ref_pic_marking_mode_flag */ |
279 | 16.0k | { |
280 | 16.0k | uint32_t mmco; |
281 | 16.0k | do |
282 | 26.7k | { |
283 | 26.7k | mmco = bs_read_ue( &s ); |
284 | 26.7k | if( mmco > 6 ) |
285 | 1.99k | goto error; |
286 | 24.7k | if( mmco == 1 || mmco == 3 ) |
287 | 5.35k | bs_read_ue( &s ); /* diff_pics_minus1 */ |
288 | 24.7k | if( mmco == 2 ) |
289 | 2.60k | bs_read_ue( &s ); /* long_term_pic_num */ |
290 | 24.7k | if( mmco == 3 || mmco == 6 ) |
291 | 1.94k | bs_read_ue( &s ); /* long_term_frame_idx */ |
292 | 24.7k | if( mmco == 4 ) |
293 | 1.82k | bs_read_ue( &s ); /* max_long_term_frame_idx_plus1 */ |
294 | 24.7k | if( mmco == 5 ) |
295 | 1.61k | { |
296 | 1.61k | p_slice->has_mmco5 = true; |
297 | 1.61k | break; /* Early END */ |
298 | 1.61k | } |
299 | 24.7k | } |
300 | 23.1k | while( mmco > 0 ); |
301 | 16.0k | } |
302 | 42.8k | } |
303 | | |
304 | | /* If you need to store anything else than MMCO presence above, care of "Early END" cases */ |
305 | | |
306 | 40.8k | if(bs_error( &s )) |
307 | 10.5k | goto error; |
308 | 30.3k | return p_slice; |
309 | | |
310 | 81.6k | error: |
311 | 81.6k | h264_slice_release( p_slice ); |
312 | 81.6k | return NULL; |
313 | 40.8k | } |
314 | | |
315 | | |
316 | | void h264_compute_poc( const h264_sequence_parameter_set_t *p_sps, |
317 | | const h264_slice_t *p_slice, h264_poc_context_t *p_ctx, |
318 | | int *p_PictureOrderCount, int *p_tFOC, int *p_bFOC ) |
319 | 224k | { |
320 | 224k | *p_tFOC = *p_bFOC = 0; |
321 | | |
322 | 224k | if( p_sps->i_pic_order_cnt_type == 0 ) |
323 | 165k | { |
324 | 165k | unsigned maxPocLSB = 1U << (p_sps->i_log2_max_pic_order_cnt_lsb + 4); |
325 | | |
326 | | /* POC reference */ |
327 | 165k | if( p_slice->i_nal_type == H264_NAL_SLICE_IDR ) |
328 | 128k | { |
329 | 128k | p_ctx->prevPicOrderCnt.lsb = 0; |
330 | 128k | p_ctx->prevPicOrderCnt.msb = 0; |
331 | 128k | } |
332 | 36.8k | else if( p_ctx->prevRefPictureHasMMCO5 ) |
333 | 610 | { |
334 | 610 | p_ctx->prevPicOrderCnt.msb = 0; |
335 | 610 | if( !p_ctx->prevRefPictureIsBottomField ) |
336 | 316 | p_ctx->prevPicOrderCnt.lsb = p_ctx->prevRefPictureTFOC; |
337 | 294 | else |
338 | 294 | p_ctx->prevPicOrderCnt.lsb = 0; |
339 | 610 | } |
340 | | |
341 | | /* 8.2.1.1 */ |
342 | 165k | int pocMSB = p_ctx->prevPicOrderCnt.msb; |
343 | 165k | int64_t orderDiff = p_slice->i_pic_order_cnt_lsb - p_ctx->prevPicOrderCnt.lsb; |
344 | 165k | if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 ) |
345 | 2.65k | pocMSB += maxPocLSB; |
346 | 162k | else if( orderDiff > maxPocLSB / 2 ) |
347 | 7.60k | pocMSB -= maxPocLSB; |
348 | | |
349 | 165k | *p_tFOC = *p_bFOC = pocMSB + p_slice->i_pic_order_cnt_lsb; |
350 | 165k | if( p_slice->i_field_pic_flag ) |
351 | 17.1k | *p_bFOC += p_slice->i_delta_pic_order_cnt_bottom; |
352 | | |
353 | | /* Save from ref picture */ |
354 | 165k | if( p_slice->i_nal_ref_idc /* Is reference */ ) |
355 | 140k | { |
356 | 140k | p_ctx->prevRefPictureIsBottomField = (p_slice->i_field_pic_flag && |
357 | 5.50k | p_slice->i_bottom_field_flag); |
358 | 140k | p_ctx->prevRefPictureHasMMCO5 = p_slice->has_mmco5; |
359 | 140k | p_ctx->prevRefPictureTFOC = *p_tFOC; |
360 | 140k | p_ctx->prevPicOrderCnt.lsb = p_slice->i_pic_order_cnt_lsb; |
361 | 140k | p_ctx->prevPicOrderCnt.msb = pocMSB; |
362 | 140k | } |
363 | 165k | } |
364 | 59.3k | else |
365 | 59.3k | { |
366 | 59.3k | unsigned maxFrameNum = 1 << (p_sps->i_log2_max_frame_num + 4); |
367 | 59.3k | unsigned frameNumOffset; |
368 | 59.3k | unsigned expectedPicOrderCnt = 0; |
369 | | |
370 | 59.3k | if( p_slice->i_nal_type == H264_NAL_SLICE_IDR ) |
371 | 24.3k | frameNumOffset = 0; |
372 | 34.9k | else if( p_ctx->prevFrameNum > p_slice->i_frame_num ) |
373 | 14.8k | frameNumOffset = p_ctx->prevFrameNumOffset + maxFrameNum; |
374 | 20.1k | else |
375 | 20.1k | frameNumOffset = p_ctx->prevFrameNumOffset; |
376 | | |
377 | 59.3k | if( p_sps->i_pic_order_cnt_type == 1 ) |
378 | 33.5k | { |
379 | 33.5k | unsigned absFrameNum; |
380 | | |
381 | 33.5k | if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle > 0 ) |
382 | 24.9k | absFrameNum = frameNumOffset + p_slice->i_frame_num; |
383 | 8.62k | else |
384 | 8.62k | absFrameNum = 0; |
385 | | |
386 | 33.5k | if( p_slice->i_nal_ref_idc == 0 && absFrameNum > 0 ) |
387 | 20.3k | absFrameNum--; |
388 | | |
389 | 33.5k | if( absFrameNum > 0 ) |
390 | 24.7k | { |
391 | 24.7k | int32_t expectedDeltaPerPicOrderCntCycle = 0; |
392 | 135k | for( int i=0; i<p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; i++ ) |
393 | 111k | expectedDeltaPerPicOrderCntCycle += p_sps->offset_for_ref_frame[i]; |
394 | | |
395 | 24.7k | unsigned picOrderCntCycleCnt = 0; |
396 | 24.7k | unsigned frameNumInPicOrderCntCycle = 0; |
397 | 24.7k | if( p_sps->i_num_ref_frames_in_pic_order_cnt_cycle ) |
398 | 24.7k | { |
399 | 24.7k | picOrderCntCycleCnt = ( absFrameNum - 1 ) / p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; |
400 | 24.7k | frameNumInPicOrderCntCycle = ( absFrameNum - 1 ) % p_sps->i_num_ref_frames_in_pic_order_cnt_cycle; |
401 | 24.7k | } |
402 | | |
403 | 24.7k | expectedPicOrderCnt = picOrderCntCycleCnt * expectedDeltaPerPicOrderCntCycle; |
404 | 79.0k | for( unsigned i=0; i <= frameNumInPicOrderCntCycle; i++ ) |
405 | 54.3k | expectedPicOrderCnt = expectedPicOrderCnt + p_sps->offset_for_ref_frame[i]; |
406 | 24.7k | } |
407 | | |
408 | 33.5k | if( p_slice->i_nal_ref_idc == 0 ) |
409 | 22.8k | expectedPicOrderCnt = expectedPicOrderCnt + p_sps->offset_for_non_ref_pic; |
410 | | |
411 | 33.5k | *p_tFOC = expectedPicOrderCnt + p_slice->i_delta_pic_order_cnt0; |
412 | 33.5k | if( !p_slice->i_field_pic_flag ) |
413 | 30.1k | *p_bFOC = *p_tFOC + p_sps->offset_for_top_to_bottom_field + p_slice->i_delta_pic_order_cnt1; |
414 | 3.39k | else if( p_slice->i_bottom_field_flag ) |
415 | 1.88k | *p_bFOC = expectedPicOrderCnt + p_sps->offset_for_top_to_bottom_field + p_slice->i_delta_pic_order_cnt0; |
416 | 33.5k | } |
417 | 25.7k | else if( p_sps->i_pic_order_cnt_type == 2 ) |
418 | 10.5k | { |
419 | 10.5k | unsigned tempPicOrderCnt; |
420 | | |
421 | 10.5k | if( p_slice->i_nal_type == H264_NAL_SLICE_IDR ) |
422 | 3.07k | tempPicOrderCnt = 0; |
423 | 7.46k | else if( p_slice->i_nal_ref_idc == 0 ) |
424 | 5.24k | tempPicOrderCnt = 2 * ( frameNumOffset + p_slice->i_frame_num ) - 1; |
425 | 2.21k | else |
426 | 2.21k | tempPicOrderCnt = 2 * ( frameNumOffset + p_slice->i_frame_num ); |
427 | | |
428 | 10.5k | *p_bFOC = *p_tFOC = tempPicOrderCnt; |
429 | 10.5k | } |
430 | | |
431 | 59.3k | p_ctx->prevFrameNum = p_slice->i_frame_num; |
432 | 59.3k | if( p_slice->has_mmco5 ) |
433 | 585 | p_ctx->prevFrameNumOffset = 0; |
434 | 58.7k | else |
435 | 58.7k | p_ctx->prevFrameNumOffset = frameNumOffset; |
436 | 59.3k | } |
437 | | |
438 | | /* 8.2.1 (8-1) */ |
439 | 224k | if( !p_slice->i_field_pic_flag ) /* progressive or contains both fields */ |
440 | 198k | *p_PictureOrderCount = __MIN( *p_bFOC, *p_tFOC ); |
441 | 25.7k | else /* split top or bottom field */ |
442 | 25.7k | if ( p_slice->i_bottom_field_flag ) |
443 | 4.29k | *p_PictureOrderCount = *p_bFOC; |
444 | 21.4k | else |
445 | 21.4k | *p_PictureOrderCount = *p_tFOC; |
446 | 224k | } |
447 | | |
448 | | static uint8_t h264_infer_pic_struct( const h264_sequence_parameter_set_t *p_sps, |
449 | | const h264_slice_t *p_slice, |
450 | | uint8_t i_pic_struct, int tFOC, int bFOC ) |
451 | 224k | { |
452 | | /* See D-1 and note 6 */ |
453 | 224k | if( !p_sps->vui.b_pic_struct_present_flag || i_pic_struct >= 9 ) |
454 | 221k | { |
455 | 221k | if( p_slice->i_field_pic_flag ) |
456 | 24.4k | i_pic_struct = 1 + p_slice->i_bottom_field_flag; |
457 | 197k | else if( tFOC == bFOC ) |
458 | 188k | i_pic_struct = 0; |
459 | 8.63k | else if( tFOC < bFOC ) |
460 | 2.84k | i_pic_struct = 3; |
461 | 5.79k | else |
462 | 5.79k | i_pic_struct = 4; |
463 | 221k | } |
464 | | |
465 | 224k | return i_pic_struct; |
466 | 224k | } |
467 | | |
468 | | uint8_t h264_get_num_ts( const h264_sequence_parameter_set_t *p_sps, |
469 | | const h264_slice_t *p_slice, uint8_t i_pic_struct, |
470 | | int tFOC, int bFOC ) |
471 | 224k | { |
472 | 224k | i_pic_struct = h264_infer_pic_struct( p_sps, p_slice, i_pic_struct, tFOC, bFOC ); |
473 | | /* !WARN modified with nuit field based multiplier for values 0, 7 and 8 */ |
474 | 224k | const uint8_t rgi_numclock[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 }; |
475 | 224k | return rgi_numclock[ i_pic_struct ]; |
476 | 224k | } |
477 | | |
478 | | bool h264_slice_top_field( const h264_slice_t *p_slice ) |
479 | 526 | { |
480 | 526 | return !p_slice->i_bottom_field_flag; |
481 | 526 | } |
482 | | |
483 | | bool h264_IsFirstVCLNALUnit( const h264_slice_t *p_prev, const h264_slice_t *p_cur ) |
484 | 257k | { |
485 | | /* Detection of the first VCL NAL unit of a primary coded picture |
486 | | * (cf. 7.4.1.2.4) */ |
487 | 257k | if( !p_prev ) |
488 | 39.8k | return true; |
489 | 217k | if( p_cur->i_frame_num != p_prev->i_frame_num || |
490 | 33.8k | p_cur->i_pic_parameter_set_id != p_prev->i_pic_parameter_set_id || |
491 | 33.6k | p_cur->i_field_pic_flag != p_prev->i_field_pic_flag || |
492 | 32.0k | !p_cur->i_nal_ref_idc != !p_prev->i_nal_ref_idc ) |
493 | 189k | return true; |
494 | 28.2k | if( p_cur->i_field_pic_flag && /* present in both and differs in value */ |
495 | 4.33k | p_cur->i_bottom_field_flag != p_prev->i_bottom_field_flag ) |
496 | 992 | return true; |
497 | 27.3k | if( p_cur->i_pic_order_cnt_type == p_prev->i_pic_order_cnt_type ) |
498 | 27.3k | { |
499 | 27.3k | if( p_cur->i_pic_order_cnt_type == 0 && |
500 | 17.4k | ( p_cur->i_pic_order_cnt_lsb != p_prev->i_pic_order_cnt_lsb || |
501 | 13.8k | p_cur->i_delta_pic_order_cnt_bottom != p_prev->i_delta_pic_order_cnt_bottom ) ) |
502 | 4.23k | return true; |
503 | 23.0k | else if( p_cur->i_pic_order_cnt_type == 1 && |
504 | 5.00k | ( p_cur->i_delta_pic_order_cnt0 != p_prev->i_delta_pic_order_cnt0 || |
505 | 3.91k | p_cur->i_delta_pic_order_cnt1 != p_prev->i_delta_pic_order_cnt1 ) ) |
506 | 1.81k | return true; |
507 | 27.3k | } |
508 | 21.2k | if( ( p_cur->i_nal_type == H264_NAL_SLICE_IDR || p_prev->i_nal_type == H264_NAL_SLICE_IDR ) && |
509 | 4.74k | ( p_cur->i_nal_type != p_prev->i_nal_type || p_cur->i_idr_pic_id != p_prev->i_idr_pic_id ) ) |
510 | 1.44k | return true; |
511 | 19.8k | return false; |
512 | 21.2k | } |
513 | | |
514 | | bool h264_CanSwapPTSWithDTS( const h264_slice_t *p_slice, const h264_sequence_parameter_set_t *p_sps ) |
515 | 150k | { |
516 | 150k | if( p_slice->i_nal_ref_idc == 0 && p_slice->type == H264_SLICE_TYPE_B ) |
517 | 834 | return true; |
518 | 149k | else if( p_sps->vui_parameters_present_flag ) |
519 | 22.4k | return p_sps->vui.i_max_num_reorder_frames == 0; |
520 | 126k | else |
521 | 126k | return p_sps->i_profile == PROFILE_H264_CAVLC_INTRA; |
522 | 150k | } |