1 /*--------------------------------------------------------------------------
2 Copyright (c) 2010 - 2018, The Linux Foundation. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the name of The Linux Foundation nor
12 the names of its contributors may be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 --------------------------------------------------------------------------*/
28 /*========================================================================
29
30 O p e n M M
31 V i d e o U t i l i t i e s
32
33 *//** @file VideoUtils.cpp
34 This module contains utilities and helper routines.
35
36 @par EXTERNALIZED FUNCTIONS
37
38 @par INITIALIZATION AND SEQUENCING REQUIREMENTS
39 (none)
40
41 *//*====================================================================== */
42
43 /* =======================================================================
44
45 INCLUDE FILES FOR MODULE
46
47 ========================================================================== */
48 #include "h264_utils.h"
49 #include "qc_omx_msg.h"
50 #include "extra_data_handler.h"
51 #include <string.h>
52 #include <stdlib.h>
53 #include <limits.h>
54 #include <sys/time.h>
55 #ifdef _ANDROID_
56 #include <cutils/properties.h>
57 extern "C" {
58 #include<utils/Log.h>
59 }
60
61 #endif
62
63 /* =======================================================================
64
65 DEFINITIONS AND DECLARATIONS FOR MODULE
66
67 This section contains definitions for constants, macros, types, variables
68 and other items needed by this module.
69
70 ========================================================================== */
71
RbspParser(const uint8 * _begin,const uint8 * _end)72 RbspParser::RbspParser (const uint8 *_begin, const uint8 *_end)
73 : begin (_begin), end(_end), pos (- 1), bit (0),
74 cursor (0xFFFFFF), advanceNeeded (true)
75 {
76 }
77
78 // Destructor
79 /*lint -e{1540} Pointer member neither freed nor zeroed by destructor
80 * No problem
81 */
~RbspParser()82 RbspParser::~RbspParser () {}
83
84 // Return next RBSP byte as a word
next()85 uint32 RbspParser::next ()
86 {
87 if (advanceNeeded) advance ();
88 //return static_cast<uint32> (*pos);
89 return static_cast<uint32> (begin[pos]);
90 }
91
92 // Advance RBSP decoder to next byte
advance()93 void RbspParser::advance ()
94 {
95 ++pos;
96 //if (pos >= stop)
97 if (begin + pos == end) {
98 /*lint -e{730} Boolean argument to function
99 * I don't see a problem here
100 */
101 //throw false;
102 DEBUG_PRINT("H264Parser-->NEED TO THROW THE EXCEPTION...");
103 }
104 cursor <<= 8;
105 //cursor |= static_cast<uint32> (*pos);
106 cursor |= static_cast<uint32> (begin[pos]);
107 if ((cursor & 0xFFFFFF) == 0x000003) {
108 advance ();
109 }
110 advanceNeeded = false;
111 }
112
113 // Decode unsigned integer
u(uint32 n)114 uint32 RbspParser::u (uint32 n)
115 {
116 uint32 i, s, x = 0;
117 for (i = 0; i < n; i += s) {
118 s = static_cast<uint32>STD_MIN(static_cast<int>(8 - bit),
119 static_cast<int>(n - i));
120 x <<= s;
121
122 x |= ((next () >> ((8 - static_cast<uint32>(bit)) - s)) &
123 ((1 << s) - 1));
124
125 bit = (bit + s) % 8;
126 if (!bit) {
127 advanceNeeded = true;
128 }
129 }
130 return x;
131 }
132
133 // Decode unsigned integer Exp-Golomb-coded syntax element
ue()134 uint32 RbspParser::ue ()
135 {
136 int leadingZeroBits = -1;
137 for (uint32 b = 0; !b; ++leadingZeroBits) {
138 b = u (1);
139 }
140 return ((1 << leadingZeroBits) - 1) +
141 u (static_cast<uint32>(leadingZeroBits));
142 }
143
144 // Decode signed integer Exp-Golomb-coded syntax element
se()145 int32 RbspParser::se ()
146 {
147 const uint32 x = ue ();
148 if (!x) return 0;
149 else if (x & 1) return static_cast<int32> ((x >> 1) + 1);
150 else return - static_cast<int32> (x >> 1);
151 }
152
allocate_rbsp_buffer(uint32 inputBufferSize)153 void H264_Utils::allocate_rbsp_buffer(uint32 inputBufferSize)
154 {
155 m_rbspBytes = (byte *) calloc(1,inputBufferSize);
156 m_prv_nalu.nal_ref_idc = 0;
157 m_prv_nalu.nalu_type = NALU_TYPE_UNSPECIFIED;
158 }
159
H264_Utils()160 H264_Utils::H264_Utils(): m_height(0),
161 m_width(0),
162 m_rbspBytes(NULL),
163 m_au_data (false)
164 {
165 initialize_frame_checking_environment();
166 }
167
~H264_Utils()168 H264_Utils::~H264_Utils()
169 {
170 if (m_rbspBytes) {
171 free(m_rbspBytes);
172 m_rbspBytes = NULL;
173 }
174 }
175
176 /***********************************************************************/
177 /*
178 FUNCTION:
179 H264_Utils::initialize_frame_checking_environment
180
181 DESCRIPTION:
182 Extract RBSP data from a NAL
183
184 INPUT/OUTPUT PARAMETERS:
185 None
186
187 RETURN VALUE:
188 boolean
189
190 SIDE EFFECTS:
191 None.
192 */
193 /***********************************************************************/
initialize_frame_checking_environment()194 void H264_Utils::initialize_frame_checking_environment()
195 {
196 m_forceToStichNextNAL = false;
197 m_au_data = false;
198 m_prv_nalu.nal_ref_idc = 0;
199 m_prv_nalu.nalu_type = NALU_TYPE_UNSPECIFIED;
200 }
201
202 /***********************************************************************/
203 /*
204 FUNCTION:
205 H264_Utils::extract_rbsp
206
207 DESCRIPTION:
208 Extract RBSP data from a NAL
209
210 INPUT/OUTPUT PARAMETERS:
211 <In>
212 buffer : buffer containing start code or nal length + NAL units
213 buffer_length : the length of the NAL buffer
214 start_code : If true, start code is detected,
215 otherwise size nal length is detected
216 size_of_nal_length_field: size of nal length field
217
218 <Out>
219 rbsp_bistream : extracted RBSP bistream
220 rbsp_length : the length of the RBSP bitstream
221 nal_unit : decoded NAL header information
222
223 RETURN VALUE:
224 boolean
225
226 SIDE EFFECTS:
227 None.
228 */
229 /***********************************************************************/
230
extract_rbsp(OMX_IN OMX_U8 * buffer,OMX_IN OMX_U32 buffer_length,OMX_IN OMX_U32 size_of_nal_length_field,OMX_OUT OMX_U8 * rbsp_bistream,OMX_OUT OMX_U32 * rbsp_length,OMX_OUT NALU * nal_unit)231 boolean H264_Utils::extract_rbsp(OMX_IN OMX_U8 *buffer,
232 OMX_IN OMX_U32 buffer_length,
233 OMX_IN OMX_U32 size_of_nal_length_field,
234 OMX_OUT OMX_U8 *rbsp_bistream,
235 OMX_OUT OMX_U32 *rbsp_length,
236 OMX_OUT NALU *nal_unit)
237 {
238 byte coef1, coef2, coef3;
239 uint32 pos = 0;
240 uint32 nal_len = buffer_length;
241 uint32 sizeofNalLengthField = 0;
242 uint32 zero_count;
243 boolean eRet = true;
244 boolean start_code = (size_of_nal_length_field==0)?true:false;
245
246 if (start_code) {
247 // Search start_code_prefix_one_3bytes (0x000001)
248 coef2 = buffer[pos++];
249 coef3 = buffer[pos++];
250 do {
251 if (pos >= buffer_length) {
252 DEBUG_PRINT_ERROR("ERROR: In %s() - line %d", __func__, __LINE__);
253 return false;
254 }
255
256 coef1 = coef2;
257 coef2 = coef3;
258 coef3 = buffer[pos++];
259 } while (coef1 || coef2 || coef3 != 1);
260 } else if (size_of_nal_length_field) {
261 /* This is the case to play multiple NAL units inside each access unit*/
262 /* Extract the NAL length depending on sizeOfNALength field */
263 sizeofNalLengthField = size_of_nal_length_field;
264 nal_len = 0;
265 while (size_of_nal_length_field--) {
266 nal_len |= buffer[pos++]<<(size_of_nal_length_field<<3);
267 }
268 if (nal_len >= buffer_length) {
269 DEBUG_PRINT_ERROR("ERROR: In %s() - line %d", __func__, __LINE__);
270 return false;
271 }
272 }
273
274 if (nal_len > buffer_length) {
275 DEBUG_PRINT_ERROR("ERROR: In %s() - line %d", __func__, __LINE__);
276 return false;
277 }
278 if (pos + 1 > (nal_len + sizeofNalLengthField)) {
279 DEBUG_PRINT_ERROR("ERROR: In %s() - line %d", __func__, __LINE__);
280 return false;
281 }
282 if ((nal_unit->forbidden_zero_bit = (buffer[pos] & 0x80)) != 0) {
283 DEBUG_PRINT_ERROR("ERROR: In %s() - line %d", __func__, __LINE__);
284 }
285 nal_unit->nal_ref_idc = (buffer[pos] & 0x60) >> 5;
286 nal_unit->nalu_type = buffer[pos++] & 0x1f;
287 DEBUG_PRINT("@#@# Pos = %x NalType = %x buflen = %d",
288 pos-1, nal_unit->nalu_type, buffer_length);
289 *rbsp_length = 0;
290
291
292 if ( nal_unit->nalu_type == NALU_TYPE_EOSEQ ||
293 nal_unit->nalu_type == NALU_TYPE_EOSTREAM)
294 return (nal_len + sizeofNalLengthField);
295
296 zero_count = 0;
297 while (pos < (nal_len+sizeofNalLengthField)) { //similar to for in p-42
298 if ( zero_count == 2 ) {
299 if ( buffer[pos] == 0x03 ) {
300 pos ++;
301 zero_count = 0;
302 continue;
303 }
304 if ( buffer[pos] <= 0x01 ) {
305 if ( start_code ) {
306 *rbsp_length -= 2;
307 pos -= 2;
308 return pos;
309 }
310 }
311 zero_count = 0;
312 }
313 zero_count ++;
314 if ( buffer[pos] != 0 )
315 zero_count = 0;
316
317 rbsp_bistream[(*rbsp_length)++] = buffer[pos++];
318 }
319
320 return eRet;
321 }
322
323 /*===========================================================================
324 FUNCTION:
325 H264_Utils::iSNewFrame
326
327 DESCRIPTION:
328 Returns true if NAL parsing successfull otherwise false.
329
330 INPUT/OUTPUT PARAMETERS:
331 <In>
332 buffer : buffer containing start code or nal length + NAL units
333 buffer_length : the length of the NAL buffer
334 start_code : If true, start code is detected,
335 otherwise size nal length is detected
336 size_of_nal_length_field: size of nal length field
337 <out>
338 isNewFrame: true if the NAL belongs to a differenet frame
339 false if the NAL belongs to a current frame
340
341 RETURN VALUE:
342 boolean true, if nal parsing is successful
343 false, if the nal parsing has errors
344
345 SIDE EFFECTS:
346 None.
347 ===========================================================================*/
isNewFrame(OMX_BUFFERHEADERTYPE * p_buf_hdr,OMX_IN OMX_U32 size_of_nal_length_field,OMX_OUT OMX_BOOL & isNewFrame)348 bool H264_Utils::isNewFrame(OMX_BUFFERHEADERTYPE *p_buf_hdr,
349 OMX_IN OMX_U32 size_of_nal_length_field,
350 OMX_OUT OMX_BOOL &isNewFrame)
351 {
352 NALU nal_unit;
353 uint16 first_mb_in_slice = 0;
354 OMX_IN OMX_U32 numBytesInRBSP = 0;
355 OMX_IN OMX_U8 *buffer = p_buf_hdr->pBuffer;
356 OMX_IN OMX_U32 buffer_length = p_buf_hdr->nFilledLen;
357 bool eRet = true;
358
359 DEBUG_PRINT("isNewFrame: buffer %p buffer_length %d "
360 "size_of_nal_length_field %d", buffer, buffer_length,
361 size_of_nal_length_field);
362
363 if ( false == extract_rbsp(buffer, buffer_length, size_of_nal_length_field,
364 m_rbspBytes, &numBytesInRBSP, &nal_unit) ) {
365 DEBUG_PRINT_ERROR("ERROR: In %s() - extract_rbsp() failed", __func__);
366 isNewFrame = OMX_FALSE;
367 eRet = false;
368 } else {
369 nalu_type = nal_unit.nalu_type;
370 switch (nal_unit.nalu_type) {
371 case NALU_TYPE_IDR:
372 case NALU_TYPE_NON_IDR: {
373 DEBUG_PRINT("AU Boundary with NAL type %d ",nal_unit.nalu_type);
374 if (m_forceToStichNextNAL) {
375 isNewFrame = OMX_FALSE;
376 } else {
377 RbspParser rbsp_parser(m_rbspBytes, (m_rbspBytes+numBytesInRBSP));
378 first_mb_in_slice = rbsp_parser.ue();
379
380 if ((!first_mb_in_slice) || /*(slice.prv_frame_num != slice.frame_num ) ||*/
381 ( (m_prv_nalu.nal_ref_idc != nal_unit.nal_ref_idc) && ( nal_unit.nal_ref_idc * m_prv_nalu.nal_ref_idc == 0 ) ) ||
382 /*( ((m_prv_nalu.nalu_type == NALU_TYPE_IDR) && (nal_unit.nalu_type == NALU_TYPE_IDR)) && (slice.idr_pic_id != slice.prv_idr_pic_id) ) || */
383 ( (m_prv_nalu.nalu_type != nal_unit.nalu_type ) && ((m_prv_nalu.nalu_type == NALU_TYPE_IDR) || (nal_unit.nalu_type == NALU_TYPE_IDR)) ) ) {
384 //DEBUG_PRINT("Found a New Frame due to NALU_TYPE_IDR/NALU_TYPE_NON_IDR");
385 isNewFrame = OMX_TRUE;
386 } else {
387 isNewFrame = OMX_FALSE;
388 }
389 }
390 m_au_data = true;
391 m_forceToStichNextNAL = false;
392 break;
393 }
394 case NALU_TYPE_SPS:
395 case NALU_TYPE_PPS:
396 case NALU_TYPE_SEI: {
397 DEBUG_PRINT("Non-AU boundary with NAL type %d", nal_unit.nalu_type);
398 if (m_au_data) {
399 isNewFrame = OMX_TRUE;
400 m_au_data = false;
401 } else {
402 isNewFrame = OMX_FALSE;
403 }
404
405 m_forceToStichNextNAL = true;
406 break;
407 }
408 case NALU_TYPE_ACCESS_DELIM:
409 case NALU_TYPE_UNSPECIFIED:
410 case NALU_TYPE_EOSEQ:
411 case NALU_TYPE_EOSTREAM:
412 default: {
413 isNewFrame = OMX_FALSE;
414 // Do not update m_forceToStichNextNAL
415 break;
416 }
417 } // end of switch
418 } // end of if
419 m_prv_nalu = nal_unit;
420 DEBUG_PRINT("get_h264_nal_type - newFrame value %d",isNewFrame);
421 return eRet;
422 }
423
start()424 void perf_metrics::start()
425 {
426 if (!active) {
427 start_time = get_act_time();
428 active = true;
429 }
430 }
431
stop()432 void perf_metrics::stop()
433 {
434 OMX_U64 stop_time = get_act_time();
435 if (active) {
436 proc_time += (stop_time - start_time);
437 active = false;
438 }
439 }
440
end(OMX_U32 units_cntr)441 void perf_metrics::end(OMX_U32 units_cntr)
442 {
443 stop();
444 DEBUG_PRINT("--> Processing time : [%.2f] Sec", (float)proc_time / 1e6);
445 if (units_cntr) {
446 DEBUG_PRINT("--> Avrg proc time : [%.2f] mSec", proc_time / (float)(units_cntr * 1e3));
447 }
448 }
449
reset()450 void perf_metrics::reset()
451 {
452 start_time = 0;
453 proc_time = 0;
454 active = false;
455 }
456
get_act_time()457 OMX_U64 perf_metrics::get_act_time()
458 {
459 struct timeval act_time = {0, 0};
460 gettimeofday(&act_time, NULL);
461 return (act_time.tv_usec + act_time.tv_sec * 1e6);
462 }
463
processing_time_us()464 OMX_U64 perf_metrics::processing_time_us()
465 {
466 return proc_time;
467 }
468
h264_stream_parser()469 h264_stream_parser::h264_stream_parser()
470 {
471 reset();
472 #ifdef PANSCAN_HDLR
473 panscan_hdl = new panscan_handler();
474 if (!panscan_hdl) {
475 DEBUG_PRINT_ERROR("ERROR: Panscan hdl was not allocated!");
476 } else if (!panscan_hdl->initialize(10)) {
477 DEBUG_PRINT_ERROR("ERROR: Allocating memory for panscan!");
478 delete panscan_hdl;
479 panscan_hdl = NULL;
480 }
481 #else
482 memset(&panscan_param, 0, sizeof(panscan_param));
483 panscan_param.rect_id = NO_PAN_SCAN_BIT;
484 #endif
485 }
486
~h264_stream_parser()487 h264_stream_parser::~h264_stream_parser()
488 {
489 #ifdef PANSCAN_HDLR
490 if (panscan_hdl) {
491 delete panscan_hdl;
492 panscan_hdl = NULL;
493 }
494 #endif
495 }
496
reset()497 void h264_stream_parser::reset()
498 {
499 curr_32_bit = 0;
500 bits_read = 0;
501 zero_cntr = 0;
502 emulation_code_skip_cntr = 0;
503 emulation_sc_enabled = true;
504 bitstream = NULL;
505 bitstream_bytes = 0;
506 memset(&vui_param, 0, sizeof(vui_param));
507 vui_param.fixed_fps_prev_ts = LLONG_MAX;
508 memset(&sei_buf_period, 0, sizeof(sei_buf_period));
509 memset(&sei_pic_timing, 0, sizeof(sei_pic_timing));
510 memset(&frame_packing_arrangement,0,sizeof(frame_packing_arrangement));
511 frame_packing_arrangement.cancel_flag = 1;
512 mbaff_flag = 0;
513 }
514
init_bitstream(OMX_U8 * data,OMX_U32 size)515 void h264_stream_parser::init_bitstream(OMX_U8* data, OMX_U32 size)
516 {
517 bitstream = data;
518 bitstream_bytes = size;
519 curr_32_bit = 0;
520 bits_read = 0;
521 zero_cntr = 0;
522 emulation_code_skip_cntr = 0;
523 }
524
parse_vui(bool vui_in_extradata)525 void h264_stream_parser::parse_vui(bool vui_in_extradata)
526 {
527 OMX_U32 value = 0;
528 DEBUG_PRINT("parse_vui: IN");
529 if (vui_in_extradata)
530 while (!extract_bits(1) && more_bits()); // Discard VUI enable flag
531 if (!more_bits())
532 return;
533
534 vui_param.aspect_ratio_info_present_flag = extract_bits(1); //aspect_ratio_info_present_flag
535 if (vui_param.aspect_ratio_info_present_flag) {
536 DEBUG_PRINT("Aspect Ratio Info present!");
537 aspect_ratio_info();
538 }
539
540 if (extract_bits(1)) //overscan_info_present_flag
541 extract_bits(1); //overscan_appropriate_flag
542 if (extract_bits(1)) { //video_signal_type_present_flag
543 extract_bits(3); //video_format
544 extract_bits(1); //video_full_range_flag
545 if (extract_bits(1)) { //colour_description_present_flag
546 extract_bits(8); //colour_primaries
547 extract_bits(8); //transfer_characteristics
548 extract_bits(8); //matrix_coefficients
549 }
550 }
551 if (extract_bits(1)) { //chroma_location_info_present_flag
552 uev(); //chroma_sample_loc_type_top_field
553 uev(); //chroma_sample_loc_type_bottom_field
554 }
555 vui_param.timing_info_present_flag = extract_bits(1);
556 if (vui_param.timing_info_present_flag) {
557 vui_param.num_units_in_tick = extract_bits(32);
558 vui_param.time_scale = extract_bits(32);
559 vui_param.fixed_frame_rate_flag = extract_bits(1);
560 DEBUG_PRINT("Timing info present in VUI!");
561 DEBUG_PRINT(" num units in tick : %u", vui_param.num_units_in_tick);
562 DEBUG_PRINT(" time scale : %u", vui_param.time_scale);
563 DEBUG_PRINT(" fixed frame rate : %u", vui_param.fixed_frame_rate_flag);
564 }
565 vui_param.nal_hrd_parameters_present_flag = extract_bits(1);
566 if (vui_param.nal_hrd_parameters_present_flag) {
567 DEBUG_PRINT("nal hrd params present!");
568 hrd_parameters(&vui_param.nal_hrd_parameters);
569 }
570 vui_param.vcl_hrd_parameters_present_flag = extract_bits(1);
571 if (vui_param.vcl_hrd_parameters_present_flag) {
572 DEBUG_PRINT("vcl hrd params present!");
573 hrd_parameters(&vui_param.vcl_hrd_parameters);
574 }
575 if (vui_param.nal_hrd_parameters_present_flag ||
576 vui_param.vcl_hrd_parameters_present_flag)
577 vui_param.low_delay_hrd_flag = extract_bits(1);
578 vui_param.pic_struct_present_flag = extract_bits(1);
579 DEBUG_PRINT("pic_struct_present_flag : %u", vui_param.pic_struct_present_flag);
580 if (extract_bits(1)) { //bitstream_restriction_flag
581 extract_bits(1); //motion_vectors_over_pic_boundaries_flag
582 uev(); //max_bytes_per_pic_denom
583 uev(); //max_bits_per_mb_denom
584 uev(); //log2_max_mv_length_vertical
585 uev(); //log2_max_mv_length_horizontal
586 uev(); //num_reorder_frames
587 uev(); //max_dec_frame_buffering
588 }
589 DEBUG_PRINT("parse_vui: OUT");
590 }
591
aspect_ratio_info()592 void h264_stream_parser::aspect_ratio_info()
593 {
594 DEBUG_PRINT("aspect_ratio_info: IN");
595 OMX_U32 aspect_ratio_idc = 0;
596 OMX_U32 aspect_ratio_x = 0;
597 OMX_U32 aspect_ratio_y = 0;
598 aspect_ratio_idc = extract_bits(8); //aspect_ratio_idc
599 switch (aspect_ratio_idc) {
600 case 1:
601 aspect_ratio_x = 1;
602 aspect_ratio_y = 1;
603 break;
604 case 2:
605 aspect_ratio_x = 12;
606 aspect_ratio_y = 11;
607 break;
608 case 3:
609 aspect_ratio_x = 10;
610 aspect_ratio_y = 11;
611 break;
612 case 4:
613 aspect_ratio_x = 16;
614 aspect_ratio_y = 11;
615 break;
616 case 5:
617 aspect_ratio_x = 40;
618 aspect_ratio_y = 33;
619 break;
620 case 6:
621 aspect_ratio_x = 24;
622 aspect_ratio_y = 11;
623 break;
624 case 7:
625 aspect_ratio_x = 20;
626 aspect_ratio_y = 11;
627 break;
628 case 8:
629 aspect_ratio_x = 32;
630 aspect_ratio_y = 11;
631 break;
632 case 9:
633 aspect_ratio_x = 80;
634 aspect_ratio_y = 33;
635 break;
636 case 10:
637 aspect_ratio_x = 18;
638 aspect_ratio_y = 11;
639 break;
640 case 11:
641 aspect_ratio_x = 15;
642 aspect_ratio_y = 11;
643 break;
644 case 12:
645 aspect_ratio_x = 64;
646 aspect_ratio_y = 33;
647 break;
648 case 13:
649 aspect_ratio_x = 160;
650 aspect_ratio_y = 99;
651 break;
652 case 14:
653 aspect_ratio_x = 4;
654 aspect_ratio_y = 3;
655 break;
656 case 15:
657 aspect_ratio_x = 3;
658 aspect_ratio_y = 2;
659 break;
660 case 16:
661 aspect_ratio_x = 2;
662 aspect_ratio_y = 1;
663 break;
664 case 255:
665 aspect_ratio_x = extract_bits(16); //sar_width
666 aspect_ratio_y = extract_bits(16); //sar_height
667 break;
668 default:
669 DEBUG_PRINT("-->aspect_ratio_idc: Reserved Value ");
670 break;
671 }
672 DEBUG_PRINT("-->aspect_ratio_idc : %u", aspect_ratio_idc);
673 DEBUG_PRINT("-->aspect_ratio_x : %u", aspect_ratio_x);
674 DEBUG_PRINT("-->aspect_ratio_y : %u", aspect_ratio_y);
675 vui_param.aspect_ratio_info.aspect_ratio_idc = aspect_ratio_idc;
676 vui_param.aspect_ratio_info.aspect_ratio_x = aspect_ratio_x;
677 vui_param.aspect_ratio_info.aspect_ratio_y = aspect_ratio_y;
678 DEBUG_PRINT("aspect_ratio_info: OUT");
679 }
680
hrd_parameters(h264_hrd_param * hrd_param)681 void h264_stream_parser::hrd_parameters(h264_hrd_param *hrd_param)
682 {
683 OMX_U32 idx;
684 DEBUG_PRINT("hrd_parameters: IN");
685 hrd_param->cpb_cnt = uev() + 1;
686 hrd_param->bit_rate_scale = extract_bits(4);
687 hrd_param->cpb_size_scale = extract_bits(4);
688 DEBUG_PRINT("-->cpb_cnt : %u", hrd_param->cpb_cnt);
689 DEBUG_PRINT("-->bit_rate_scale : %u", hrd_param->bit_rate_scale);
690 DEBUG_PRINT("-->cpb_size_scale : %u", hrd_param->cpb_size_scale);
691 if (hrd_param->cpb_cnt > MAX_CPB_COUNT) {
692 DEBUG_PRINT("ERROR: Invalid hrd_param->cpb_cnt [%u]!", hrd_param->cpb_cnt);
693 return;
694 }
695 for (idx = 0; idx < hrd_param->cpb_cnt && more_bits(); idx++) {
696 hrd_param->bit_rate_value[idx] = uev() + 1;
697 hrd_param->cpb_size_value[idx] = uev() + 1;
698 hrd_param->cbr_flag[idx] = extract_bits(1);
699 DEBUG_PRINT("-->bit_rate_value [%d] : %u", idx, hrd_param->bit_rate_value[idx]);
700 DEBUG_PRINT("-->cpb_size_value [%d] : %u", idx, hrd_param->cpb_size_value[idx]);
701 DEBUG_PRINT("-->cbr_flag [%d] : %u", idx, hrd_param->cbr_flag[idx]);
702 }
703 hrd_param->initial_cpb_removal_delay_length = extract_bits(5) + 1;
704 hrd_param->cpb_removal_delay_length = extract_bits(5) + 1;
705 hrd_param->dpb_output_delay_length = extract_bits(5) + 1;
706 hrd_param->time_offset_length = extract_bits(5);
707 DEBUG_PRINT("-->initial_cpb_removal_delay_length : %u", hrd_param->initial_cpb_removal_delay_length);
708 DEBUG_PRINT("-->cpb_removal_delay_length : %u", hrd_param->cpb_removal_delay_length);
709 DEBUG_PRINT("-->dpb_output_delay_length : %u", hrd_param->dpb_output_delay_length);
710 DEBUG_PRINT("-->time_offset_length : %u", hrd_param->time_offset_length);
711 DEBUG_PRINT("hrd_parameters: OUT");
712 }
713
parse_sei()714 void h264_stream_parser::parse_sei()
715 {
716 OMX_U32 value = 0, processed_bytes = 0;
717 OMX_U8 *sei_msg_start = bitstream;
718 OMX_U32 sei_unit_size = bitstream_bytes;
719 DEBUG_PRINT("@@parse_sei: IN sei_unit_size(%u)", sei_unit_size);
720 while ((processed_bytes + 2) < sei_unit_size && more_bits()) {
721 init_bitstream(sei_msg_start + processed_bytes, sei_unit_size - processed_bytes);
722 DEBUG_PRINT("-->NALU_TYPE_SEI");
723 OMX_U32 payload_type = 0, payload_size = 0, aux = 0;
724 do {
725 value = extract_bits(8);
726 payload_type += value;
727 processed_bytes++;
728 } while (value == 0xFF);
729 DEBUG_PRINT("-->payload_type : %u", payload_type);
730 do {
731 value = extract_bits(8);
732 payload_size += value;
733 processed_bytes++;
734 } while (value == 0xFF);
735 DEBUG_PRINT("-->payload_size : %u", payload_size);
736 if (payload_size > 0) {
737 switch (payload_type) {
738 case BUFFERING_PERIOD:
739 sei_buffering_period();
740 break;
741 case PIC_TIMING:
742 sei_picture_timing();
743 break;
744 case PAN_SCAN_RECT:
745 sei_pan_scan();
746 break;
747 case SEI_PAYLOAD_FRAME_PACKING_ARRANGEMENT:
748 parse_frame_pack();
749 break;
750 default:
751 DEBUG_PRINT("-->SEI payload type [%u] not implemented! size[%u]", payload_type, payload_size);
752 }
753 }
754 processed_bytes += (payload_size + emulation_code_skip_cntr);
755 DEBUG_PRINT("-->SEI processed_bytes[%u]", processed_bytes);
756 }
757 DEBUG_PRINT("@@parse_sei: OUT");
758 }
759
sei_buffering_period()760 void h264_stream_parser::sei_buffering_period()
761 {
762 OMX_U32 idx;
763 OMX_U32 value = 0;
764 h264_hrd_param *hrd_param = NULL;
765 DEBUG_PRINT("@@sei_buffering_period: IN");
766 value = uev(); // seq_parameter_set_id
767 DEBUG_PRINT("-->seq_parameter_set_id : %u", value);
768 if (value > 31) {
769 DEBUG_PRINT("ERROR: Invalid seq_parameter_set_id [%u]!", value);
770 return;
771 }
772 sei_buf_period.is_valid = false;
773 if (vui_param.nal_hrd_parameters_present_flag) {
774 hrd_param = &vui_param.nal_hrd_parameters;
775 if (hrd_param->cpb_cnt > MAX_CPB_COUNT) {
776 DEBUG_PRINT("ERROR: Invalid hrd_param->cpb_cnt [%u]!", hrd_param->cpb_cnt);
777 return;
778 }
779 for (idx = 0; idx < hrd_param->cpb_cnt ; idx++) {
780 sei_buf_period.is_valid = true;
781 sei_buf_period.initial_cpb_removal_delay[idx] = extract_bits(hrd_param->initial_cpb_removal_delay_length);
782 sei_buf_period.initial_cpb_removal_delay_offset[idx] = extract_bits(hrd_param->initial_cpb_removal_delay_length);
783 DEBUG_PRINT("-->initial_cpb_removal_delay : %u", sei_buf_period.initial_cpb_removal_delay[idx]);
784 DEBUG_PRINT("-->initial_cpb_removal_delay_offset : %u", sei_buf_period.initial_cpb_removal_delay_offset[idx]);
785 }
786 }
787 if (vui_param.vcl_hrd_parameters_present_flag) {
788 hrd_param = &vui_param.vcl_hrd_parameters;
789 if (hrd_param->cpb_cnt > MAX_CPB_COUNT) {
790 DEBUG_PRINT("ERROR: Invalid hrd_param->cpb_cnt [%u]!", hrd_param->cpb_cnt);
791 return;
792 }
793 for (idx = 0; idx < hrd_param->cpb_cnt ; idx++) {
794 sei_buf_period.is_valid = true;
795 sei_buf_period.initial_cpb_removal_delay[idx] = extract_bits(hrd_param->initial_cpb_removal_delay_length);
796 sei_buf_period.initial_cpb_removal_delay_offset[idx] = extract_bits(hrd_param->initial_cpb_removal_delay_length);
797 DEBUG_PRINT("-->initial_cpb_removal_delay : %u", sei_buf_period.initial_cpb_removal_delay[idx]);
798 DEBUG_PRINT("-->initial_cpb_removal_delay_offset : %u", sei_buf_period.initial_cpb_removal_delay_offset[idx]);
799 }
800 }
801 sei_buf_period.au_cntr = 0;
802 DEBUG_PRINT("@@sei_buffering_period: OUT");
803 }
804
sei_picture_timing()805 void h264_stream_parser::sei_picture_timing()
806 {
807 DEBUG_PRINT("@@sei_picture_timing: IN");
808 OMX_U32 time_offset_len = 0, cpb_removal_len = 24, dpb_output_len = 24;
809 OMX_U8 cbr_flag = 0;
810 sei_pic_timing.is_valid = true;
811 if (vui_param.nal_hrd_parameters_present_flag) {
812 cpb_removal_len = vui_param.nal_hrd_parameters.cpb_removal_delay_length;
813 dpb_output_len = vui_param.nal_hrd_parameters.dpb_output_delay_length;
814 time_offset_len = vui_param.nal_hrd_parameters.time_offset_length;
815 cbr_flag = vui_param.nal_hrd_parameters.cbr_flag[0];
816 } else if (vui_param.vcl_hrd_parameters_present_flag) {
817 cpb_removal_len = vui_param.vcl_hrd_parameters.cpb_removal_delay_length;
818 dpb_output_len = vui_param.vcl_hrd_parameters.dpb_output_delay_length;
819 time_offset_len = vui_param.vcl_hrd_parameters.time_offset_length;
820 cbr_flag = vui_param.vcl_hrd_parameters.cbr_flag[0];
821 }
822 sei_pic_timing.cpb_removal_delay = extract_bits(cpb_removal_len);
823 sei_pic_timing.dpb_output_delay = extract_bits(dpb_output_len);
824 DEBUG_PRINT("-->cpb_removal_len : %u", cpb_removal_len);
825 DEBUG_PRINT("-->dpb_output_len : %u", dpb_output_len);
826 DEBUG_PRINT("-->cpb_removal_delay : %u", sei_pic_timing.cpb_removal_delay);
827 DEBUG_PRINT("-->dpb_output_delay : %u", sei_pic_timing.dpb_output_delay);
828 if (vui_param.pic_struct_present_flag) {
829 sei_pic_timing.pic_struct = extract_bits(4);
830 sei_pic_timing.num_clock_ts = 0;
831 switch (sei_pic_timing.pic_struct) {
832 case 0:
833 case 1:
834 case 2:
835 sei_pic_timing.num_clock_ts = 1;
836 break;
837 case 3:
838 case 4:
839 case 7:
840 sei_pic_timing.num_clock_ts = 2;
841 break;
842 case 5:
843 case 6:
844 case 8:
845 sei_pic_timing.num_clock_ts = 3;
846 break;
847 default:
848 DEBUG_PRINT_ERROR("sei_picture_timing: pic_struct invalid!");
849 }
850 DEBUG_PRINT("-->num_clock_ts : %u", sei_pic_timing.num_clock_ts);
851 for (OMX_U32 i = 0; i < sei_pic_timing.num_clock_ts && more_bits(); i++) {
852 sei_pic_timing.clock_ts_flag = extract_bits(1);
853 if (sei_pic_timing.clock_ts_flag) {
854 DEBUG_PRINT("-->clock_timestamp present!");
855 sei_pic_timing.ct_type = extract_bits(2);
856 sei_pic_timing.nuit_field_based_flag = extract_bits(1);
857 sei_pic_timing.counting_type = extract_bits(5);
858 sei_pic_timing.full_timestamp_flag = extract_bits(1);
859 sei_pic_timing.discontinuity_flag = extract_bits(1);
860 sei_pic_timing.cnt_dropped_flag = extract_bits(1);
861 sei_pic_timing.n_frames = extract_bits(8);
862 DEBUG_PRINT("-->f_timestamp_flg : %u", sei_pic_timing.full_timestamp_flag);
863 DEBUG_PRINT("-->n_frames : %u", sei_pic_timing.n_frames);
864 sei_pic_timing.seconds_value = 0;
865 sei_pic_timing.minutes_value = 0;
866 sei_pic_timing.hours_value = 0;
867 if (sei_pic_timing.full_timestamp_flag) {
868 sei_pic_timing.seconds_value = extract_bits(6);
869 sei_pic_timing.minutes_value = extract_bits(6);
870 sei_pic_timing.hours_value = extract_bits(5);
871 } else if (extract_bits(1)) {
872 DEBUG_PRINT("-->seconds_flag enabled!");
873 sei_pic_timing.seconds_value = extract_bits(6);
874 if (extract_bits(1)) {
875 DEBUG_PRINT("-->minutes_flag enabled!");
876 sei_pic_timing.minutes_value = extract_bits(6);
877 if (extract_bits(1)) {
878 DEBUG_PRINT("-->hours_flag enabled!");
879 sei_pic_timing.hours_value = extract_bits(5);
880 }
881 }
882 }
883 sei_pic_timing.time_offset = 0;
884 if (time_offset_len > 0)
885 sei_pic_timing.time_offset = iv(time_offset_len);
886 DEBUG_PRINT("-->seconds_value : %u", sei_pic_timing.seconds_value);
887 DEBUG_PRINT("-->minutes_value : %u", sei_pic_timing.minutes_value);
888 DEBUG_PRINT("-->hours_value : %u", sei_pic_timing.hours_value);
889 DEBUG_PRINT("-->time_offset : %d", sei_pic_timing.time_offset);
890 }
891 }
892 }
893 DEBUG_PRINT("@@sei_picture_timing: OUT");
894 }
895
sei_pan_scan()896 void h264_stream_parser::sei_pan_scan()
897 {
898 #ifdef _ANDROID_
899 char property_value[PROPERTY_VALUE_MAX] = {0};
900 OMX_S32 enable_panscan_log = 0;
901 property_get("vendor.vidc.dec.debug.panframedata", property_value, "0");
902 enable_panscan_log = atoi(property_value);
903 #endif
904 #ifdef PANSCAN_HDLR
905 h264_pan_scan *pan_scan_param = panscan_hdl->get_free();
906 #else
907 h264_pan_scan *pan_scan_param = &panscan_param;
908 #endif
909
910 if (!pan_scan_param) {
911 DEBUG_PRINT_ERROR("sei_pan_scan: ERROR: Invalid pointer!");
912 return;
913 }
914
915 pan_scan_param->rect_id = uev();
916 if (pan_scan_param->rect_id > 0xFF) {
917 DEBUG_PRINT_ERROR("sei_pan_scan: ERROR: Invalid rect_id[%u]!", (unsigned int)pan_scan_param->rect_id);
918 pan_scan_param->rect_id = NO_PAN_SCAN_BIT;
919 return;
920 }
921
922 pan_scan_param->rect_cancel_flag = extract_bits(1);
923
924 if (pan_scan_param->rect_cancel_flag)
925 pan_scan_param->rect_id = NO_PAN_SCAN_BIT;
926 else {
927 pan_scan_param->cnt = uev() + 1;
928 if (pan_scan_param->cnt > MAX_PAN_SCAN_RECT) {
929 DEBUG_PRINT_ERROR("sei_pan_scan: ERROR: Invalid num of rect [%u]!", (unsigned int)pan_scan_param->cnt);
930 pan_scan_param->rect_id = NO_PAN_SCAN_BIT;
931 return;
932 }
933
934 for (OMX_U32 i = 0; i < pan_scan_param->cnt; i++) {
935 pan_scan_param->rect_left_offset[i] = sev();
936 pan_scan_param->rect_right_offset[i] = sev();
937 pan_scan_param->rect_top_offset[i] = sev();
938 pan_scan_param->rect_bottom_offset[i] = sev();
939
940 }
941 pan_scan_param->rect_repetition_period = uev();
942 #ifdef PANSCAN_HDLR
943 if (pan_scan_param->rect_repetition_period > 1)
944 // Repetition period is decreased by 2 each time panscan data is used
945 pan_scan_param->rect_repetition_period *= 2;
946 #endif
947 #ifdef _ANDROID_
948 if (enable_panscan_log) {
949 print_pan_data(pan_scan_param);
950 }
951 #endif
952 }
953 }
954
print_pan_data(h264_pan_scan * pan_scan_param)955 void h264_stream_parser::print_pan_data(h264_pan_scan *pan_scan_param)
956 {
957 DEBUG_PRINT_ERROR("@@print_pan_data: IN");
958
959 DEBUG_PRINT_ERROR("-->rect_id : %u", (unsigned int)pan_scan_param->rect_id);
960 DEBUG_PRINT_ERROR("-->rect_cancel_flag : %u", pan_scan_param->rect_cancel_flag);
961
962 DEBUG_PRINT_ERROR("-->cnt : %u", (unsigned int)pan_scan_param->cnt);
963
964 for (OMX_U32 i = 0; i < pan_scan_param->cnt; i++) {
965 DEBUG_PRINT_ERROR("-->rect_left_offset : %d", (int)pan_scan_param->rect_left_offset[i]);
966 DEBUG_PRINT_ERROR("-->rect_right_offset : %d", (int)pan_scan_param->rect_right_offset[i]);
967 DEBUG_PRINT_ERROR("-->rect_top_offset : %d", (int)pan_scan_param->rect_top_offset[i]);
968 DEBUG_PRINT_ERROR("-->rect_bottom_offset : %d", (int)pan_scan_param->rect_bottom_offset[i]);
969 }
970 DEBUG_PRINT_ERROR("-->repetition_period : %u", (unsigned int)pan_scan_param->rect_repetition_period);
971
972 DEBUG_PRINT_ERROR("@@print_pan_data: OUT");
973 }
974
parse_sps()975 void h264_stream_parser::parse_sps()
976 {
977 OMX_U32 value = 0, scaling_matrix_limit;
978 DEBUG_PRINT("@@parse_sps: IN");
979 value = extract_bits(8); //profile_idc
980 extract_bits(8); //constraint flags and reserved bits
981 extract_bits(8); //level_idc
982 uev(); //sps id
983 if (value == 100 || value == 110 || value == 122 || value == 244 ||
984 value == 44 || value == 83 || value == 86 || value == 118) {
985 if (uev() == 3) { //chroma_format_idc
986 extract_bits(1); //separate_colour_plane_flag
987 scaling_matrix_limit = 12;
988 } else
989 scaling_matrix_limit = 12;
990 uev(); //bit_depth_luma_minus8
991 uev(); //bit_depth_chroma_minus8
992 extract_bits(1); //qpprime_y_zero_transform_bypass_flag
993 if (extract_bits(1)) { //seq_scaling_matrix_present_flag
994 for (unsigned int i = 0; i < scaling_matrix_limit && more_bits(); i++) {
995 if (extract_bits(1)) { ////seq_scaling_list_present_flag[ i ]
996 if (i < 6)
997 scaling_list(16);
998 else
999 scaling_list(64);
1000 }
1001 }
1002 }
1003 }
1004 uev(); //log2_max_frame_num_minus4
1005 value = uev(); //pic_order_cnt_type
1006 if (value == 0)
1007 uev(); //log2_max_pic_order_cnt_lsb_minus4
1008 else if (value == 1) {
1009 extract_bits(1); //delta_pic_order_always_zero_flag
1010 sev(); //offset_for_non_ref_pic
1011 sev(); //offset_for_top_to_bottom_field
1012 value = uev(); // num_ref_frames_in_pic_order_cnt_cycle
1013 for (unsigned int i = 0; i < value; i++)
1014 sev(); //offset_for_ref_frame[ i ]
1015 }
1016 uev(); //max_num_ref_frames
1017 extract_bits(1); //gaps_in_frame_num_value_allowed_flag
1018 value = uev(); //pic_width_in_mbs_minus1
1019 value = uev(); //pic_height_in_map_units_minus1
1020 if (!extract_bits(1)) //frame_mbs_only_flag
1021 mbaff_flag = extract_bits(1); //mb_adaptive_frame_field_flag
1022 extract_bits(1); //direct_8x8_inference_flag
1023 if (extract_bits(1)) { //frame_cropping_flag
1024 uev(); //frame_crop_left_offset
1025 uev(); //frame_crop_right_offset
1026 uev(); //frame_crop_top_offset
1027 uev(); //frame_crop_bottom_offset
1028 }
1029 if (extract_bits(1)) //vui_parameters_present_flag
1030 parse_vui(false);
1031 DEBUG_PRINT("@@parse_sps: OUT");
1032 }
1033
scaling_list(OMX_U32 size_of_scaling_list)1034 void h264_stream_parser::scaling_list(OMX_U32 size_of_scaling_list)
1035 {
1036 OMX_S32 last_scale = 8, next_scale = 8, delta_scale;
1037 for (unsigned int j = 0; j < size_of_scaling_list; j++) {
1038 if (next_scale != 0) {
1039 delta_scale = sev();
1040 next_scale = (last_scale + delta_scale + 256) % 256;
1041 }
1042 last_scale = (next_scale == 0)? last_scale : next_scale;
1043 }
1044 }
1045
extract_bits(OMX_U32 n)1046 OMX_U32 h264_stream_parser::extract_bits(OMX_U32 n)
1047 {
1048 OMX_U32 value = 0;
1049 if (n > 32) {
1050 DEBUG_PRINT_ERROR("ERROR: extract_bits limit to 32 bits!");
1051 return value;
1052 }
1053 value = curr_32_bit >> (32 - n);
1054 if (bits_read < n) {
1055 n -= bits_read;
1056 read_word();
1057 value |= (curr_32_bit >> (32 - n));
1058 if (bits_read < n) {
1059 DEBUG_PRINT("ERROR: extract_bits underflow!");
1060 value >>= (n - bits_read);
1061 n = bits_read;
1062 }
1063 }
1064 bits_read -= n;
1065 curr_32_bit <<= n;
1066 return value;
1067 }
1068
read_word()1069 void h264_stream_parser::read_word()
1070 {
1071 curr_32_bit = 0;
1072 bits_read = 0;
1073 while (bitstream_bytes && bits_read < 32) {
1074 if (*bitstream == EMULATION_PREVENTION_THREE_BYTE &&
1075 zero_cntr >= 2 && emulation_sc_enabled) {
1076 DEBUG_PRINT("EMULATION_PREVENTION_THREE_BYTE: Skip 0x03 byte aligned!");
1077 emulation_code_skip_cntr++;
1078 } else {
1079 curr_32_bit <<= 8;
1080 curr_32_bit |= *bitstream;
1081 bits_read += 8;
1082 }
1083 if (*bitstream == 0)
1084 zero_cntr++;
1085 else
1086 zero_cntr = 0;
1087 bitstream++;
1088 bitstream_bytes--;
1089 }
1090 curr_32_bit <<= (32 - bits_read);
1091 }
1092
uev()1093 OMX_U32 h264_stream_parser::uev()
1094 {
1095 OMX_U32 lead_zero_bits = 0, code_num = 0;
1096 while (!extract_bits(1) && more_bits())
1097 lead_zero_bits++;
1098 code_num = lead_zero_bits == 0 ? 0 :
1099 (1 << lead_zero_bits) - 1 + extract_bits(lead_zero_bits);
1100 return code_num;
1101 }
1102
more_bits()1103 bool h264_stream_parser::more_bits()
1104 {
1105 return (bitstream_bytes > 0 || bits_read > 0);
1106 }
1107
sev()1108 OMX_S32 h264_stream_parser::sev()
1109 {
1110 OMX_U32 code_num = uev();
1111 OMX_S32 ret;
1112 ret = (code_num + 1) >> 1;
1113 return ((code_num & 1) ? ret : -ret);
1114 }
1115
iv(OMX_U32 n_bits)1116 OMX_S32 h264_stream_parser::iv(OMX_U32 n_bits)
1117 {
1118 OMX_U32 code_num = extract_bits(n_bits);
1119 OMX_S32 ret = (code_num >> (n_bits - 1))? (-1)*(~(code_num & ~(0x1 << (n_bits - 1))) + 1) : code_num;
1120 return ret;
1121 }
1122
get_nal_unit_type(OMX_U32 * nal_unit_type)1123 OMX_U32 h264_stream_parser::get_nal_unit_type(OMX_U32 *nal_unit_type)
1124 {
1125 OMX_U32 value = 0, consumed_bytes = 3;
1126 *nal_unit_type = NALU_TYPE_UNSPECIFIED;
1127 DEBUG_PRINT("-->get_nal_unit_type: IN");
1128 value = extract_bits(24);
1129 while (value != 0x00000001 && more_bits()) {
1130 value <<= 8;
1131 value |= extract_bits(8);
1132 consumed_bytes++;
1133 }
1134 if (value != 0x00000001) {
1135 DEBUG_PRINT_ERROR("ERROR in get_nal_unit_type: Start code not found!");
1136 } else {
1137 if (extract_bits(1)) { // forbidden_zero_bit
1138 DEBUG_PRINT_ERROR("WARNING: forbidden_zero_bit should be zero!");
1139 }
1140 value = extract_bits(2);
1141 DEBUG_PRINT("-->nal_ref_idc : %x", value);
1142 *nal_unit_type = extract_bits(5);
1143 DEBUG_PRINT("-->nal_unit_type : %x", *nal_unit_type);
1144 consumed_bytes++;
1145 if (consumed_bytes > 5) {
1146 DEBUG_PRINT_ERROR("-->WARNING: Startcode was found after the first 4 bytes!");
1147 }
1148 }
1149 DEBUG_PRINT("-->get_nal_unit_type: OUT");
1150 return consumed_bytes;
1151 }
1152
calculate_buf_period_ts(OMX_S64 timestamp)1153 OMX_S64 h264_stream_parser::calculate_buf_period_ts(OMX_S64 timestamp)
1154 {
1155 OMX_S64 clock_ts = timestamp;
1156 DEBUG_PRINT("calculate_ts(): IN");
1157 if (sei_buf_period.au_cntr == 0)
1158 clock_ts = sei_buf_period.reference_ts = timestamp;
1159 else if (sei_pic_timing.is_valid && VALID_TS(sei_buf_period.reference_ts)) {
1160 clock_ts = sei_buf_period.reference_ts + sei_pic_timing.cpb_removal_delay *
1161 1e6 * vui_param.num_units_in_tick / vui_param.time_scale;
1162 }
1163 sei_buf_period.au_cntr++;
1164 DEBUG_PRINT("calculate_ts(): OUT");
1165 return clock_ts;
1166 }
1167
calculate_fixed_fps_ts(OMX_S64 timestamp,OMX_U32 DeltaTfiDivisor)1168 OMX_S64 h264_stream_parser::calculate_fixed_fps_ts(OMX_S64 timestamp, OMX_U32 DeltaTfiDivisor)
1169 {
1170 if (VALID_TS(timestamp))
1171 vui_param.fixed_fps_prev_ts = timestamp;
1172 else if (VALID_TS(vui_param.fixed_fps_prev_ts))
1173 vui_param.fixed_fps_prev_ts += DeltaTfiDivisor * 1e6 *
1174 vui_param.num_units_in_tick / vui_param.time_scale;
1175 return vui_param.fixed_fps_prev_ts;
1176 }
1177
parse_frame_pack()1178 void h264_stream_parser::parse_frame_pack()
1179 {
1180 #ifdef _ANDROID_
1181 char property_value[PROPERTY_VALUE_MAX] = {0};
1182 OMX_S32 enable_framepack_log = 0;
1183
1184 property_get("vendor.vidc.dec.debug.panframedata", property_value, "0");
1185 enable_framepack_log = atoi(property_value);
1186 #endif
1187 DEBUG_PRINT("%s:%d parse_frame_pack", __func__, __LINE__);
1188
1189 frame_packing_arrangement.id = uev();
1190
1191 frame_packing_arrangement.cancel_flag = extract_bits(1);
1192 if (!frame_packing_arrangement.cancel_flag) {
1193 frame_packing_arrangement.type = extract_bits(7);
1194 frame_packing_arrangement.quincunx_sampling_flag = extract_bits(1);
1195 frame_packing_arrangement.content_interpretation_type = extract_bits(6);
1196 frame_packing_arrangement.spatial_flipping_flag = extract_bits(1);
1197 frame_packing_arrangement.frame0_flipped_flag = extract_bits(1);
1198 frame_packing_arrangement.field_views_flag = extract_bits(1);
1199 frame_packing_arrangement.current_frame_is_frame0_flag = extract_bits(1);
1200 frame_packing_arrangement.frame0_self_contained_flag = extract_bits(1);
1201 frame_packing_arrangement.frame1_self_contained_flag = extract_bits(1);
1202
1203 if (!frame_packing_arrangement.quincunx_sampling_flag &&
1204 frame_packing_arrangement.type != 5) {
1205 frame_packing_arrangement.frame0_grid_position_x = extract_bits(4);
1206 frame_packing_arrangement.frame0_grid_position_y = extract_bits(4);
1207 frame_packing_arrangement.frame1_grid_position_x = extract_bits(4);
1208 frame_packing_arrangement.frame1_grid_position_y = extract_bits(4);
1209 }
1210 frame_packing_arrangement.reserved_byte = extract_bits(8);
1211 frame_packing_arrangement.repetition_period = uev();
1212 }
1213 frame_packing_arrangement.extension_flag = extract_bits(1);
1214
1215 #ifdef _ANDROID_
1216 if (enable_framepack_log) {
1217 print_frame_pack();
1218 }
1219 #endif
1220 }
1221
print_frame_pack()1222 void h264_stream_parser::print_frame_pack()
1223 {
1224 DEBUG_PRINT("## frame_packing_arrangement.id = %u", frame_packing_arrangement.id);
1225 DEBUG_PRINT("## frame_packing_arrangement.cancel_flag = %u",
1226 frame_packing_arrangement.cancel_flag);
1227 if (!frame_packing_arrangement.cancel_flag) {
1228 DEBUG_PRINT("## frame_packing_arrangement.type = %u",
1229 frame_packing_arrangement.type);
1230 DEBUG_PRINT("## frame_packing_arrangement.quincunx_sampling_flag = %u",
1231 frame_packing_arrangement.quincunx_sampling_flag);
1232 DEBUG_PRINT("## frame_packing_arrangement.content_interpretation_type = %u",
1233 frame_packing_arrangement.content_interpretation_type);
1234 DEBUG_PRINT("## frame_packing_arrangement.spatial_flipping_flag = %u",
1235 frame_packing_arrangement.spatial_flipping_flag);
1236 DEBUG_PRINT("## frame_packing_arrangement.frame0_flipped_flag = %u",
1237 frame_packing_arrangement.frame0_flipped_flag);
1238 DEBUG_PRINT("## frame_packing_arrangement.field_views_flag = %u",
1239 frame_packing_arrangement.field_views_flag);
1240 DEBUG_PRINT("## frame_packing_arrangement.current_frame_is_frame0_flag = %u",
1241 frame_packing_arrangement.current_frame_is_frame0_flag);
1242 DEBUG_PRINT("## frame_packing_arrangement.frame0_self_contained_flag = %u",
1243 frame_packing_arrangement.frame0_self_contained_flag);
1244 DEBUG_PRINT("## frame_packing_arrangement.frame1_self_contained_flag = %u",
1245 frame_packing_arrangement.frame1_self_contained_flag);
1246 DEBUG_PRINT("## frame_packing_arrangement.reserved_byte = %u",
1247 frame_packing_arrangement.reserved_byte);
1248 DEBUG_PRINT("## frame_packing_arrangement.repetition_period = %u",
1249 frame_packing_arrangement.repetition_period);
1250 DEBUG_PRINT("## frame_packing_arrangement.extension_flag = %u",
1251 frame_packing_arrangement.extension_flag);
1252 }
1253 }
1254 /* API'S EXPOSED TO OMX COMPONENT */
1255
get_frame_pack_data(OMX_QCOM_FRAME_PACK_ARRANGEMENT * frame_pack)1256 void h264_stream_parser::get_frame_pack_data(
1257 OMX_QCOM_FRAME_PACK_ARRANGEMENT *frame_pack)
1258 {
1259 DEBUG_PRINT("%s:%d get frame data", __func__, __LINE__);
1260 memcpy(&frame_pack->id,&frame_packing_arrangement.id,
1261 FRAME_PACK_SIZE*sizeof(OMX_U32));
1262 return;
1263 }
1264
1265
is_mbaff()1266 bool h264_stream_parser::is_mbaff()
1267 {
1268 DEBUG_PRINT("%s:%d MBAFF flag=%d", __func__, __LINE__,mbaff_flag);
1269 return mbaff_flag;
1270 }
1271
get_frame_rate(OMX_U32 * frame_rate)1272 void h264_stream_parser::get_frame_rate(OMX_U32 *frame_rate)
1273 {
1274 if (vui_param.num_units_in_tick != 0)
1275 *frame_rate = vui_param.time_scale / (2 * vui_param.num_units_in_tick);
1276 }
1277
parse_nal(OMX_U8 * data_ptr,OMX_U32 data_len,OMX_U32 nal_type,bool enable_emu_sc)1278 void h264_stream_parser::parse_nal(OMX_U8* data_ptr, OMX_U32 data_len, OMX_U32 nal_type, bool enable_emu_sc)
1279 {
1280 OMX_U32 nal_unit_type = NALU_TYPE_UNSPECIFIED, cons_bytes = 0;
1281 DEBUG_PRINT("parse_nal(): IN nal_type(%u)", nal_type);
1282 if (!data_len)
1283 return;
1284 init_bitstream(data_ptr, data_len);
1285 emulation_sc_enabled = enable_emu_sc;
1286 if (nal_type != NALU_TYPE_VUI) {
1287 cons_bytes = get_nal_unit_type(&nal_unit_type);
1288 if (nal_type != nal_unit_type && nal_type != NALU_TYPE_UNSPECIFIED) {
1289 DEBUG_PRINT("Unexpected nal_type(%x) expected(%x)", nal_unit_type, nal_type);
1290 return;
1291 }
1292 }
1293 switch (nal_type) {
1294 case NALU_TYPE_SPS:
1295 if (more_bits())
1296 parse_sps();
1297 #ifdef PANSCAN_HDLR
1298 panscan_hdl->get_free();
1299 #endif
1300 break;
1301 case NALU_TYPE_SEI:
1302 init_bitstream(data_ptr + cons_bytes, data_len - cons_bytes);
1303 parse_sei();
1304 break;
1305 case NALU_TYPE_VUI:
1306 parse_vui(true);
1307 break;
1308 default:
1309 DEBUG_PRINT("nal_unit_type received : %u", nal_type);
1310 }
1311 DEBUG_PRINT("parse_nal(): OUT");
1312 }
1313
1314 #ifdef PANSCAN_HDLR
update_panscan_data(OMX_S64 timestamp)1315 void h264_stream_parser::update_panscan_data(OMX_S64 timestamp)
1316 {
1317 panscan_hdl->update_last(timestamp);
1318 }
1319 #endif
1320
fill_aspect_ratio_info(OMX_QCOM_ASPECT_RATIO * dest_aspect_ratio)1321 void h264_stream_parser::fill_aspect_ratio_info(OMX_QCOM_ASPECT_RATIO *dest_aspect_ratio)
1322 {
1323 if (dest_aspect_ratio && vui_param.aspect_ratio_info_present_flag) {
1324 dest_aspect_ratio->aspectRatioX = vui_param.aspect_ratio_info.aspect_ratio_x;
1325 dest_aspect_ratio->aspectRatioY = vui_param.aspect_ratio_info.aspect_ratio_y;
1326 }
1327 }
1328
fill_pan_scan_data(OMX_QCOM_PANSCAN * dest_pan_scan,OMX_S64 timestamp)1329 void h264_stream_parser::fill_pan_scan_data(OMX_QCOM_PANSCAN *dest_pan_scan, OMX_S64 timestamp)
1330 {
1331 #ifdef PANSCAN_HDLR
1332 h264_pan_scan *pan_scan_param = panscan_hdl->get_populated(timestamp);
1333 #else
1334 h264_pan_scan *pan_scan_param = &panscan_param;
1335 #endif
1336 if (pan_scan_param) {
1337 if (!(pan_scan_param->rect_id & NO_PAN_SCAN_BIT)) {
1338 PRINT_PANSCAN_PARAM(*pan_scan_param);
1339 dest_pan_scan->numWindows = pan_scan_param->cnt;
1340 for (unsigned int i = 0; i < dest_pan_scan->numWindows; i++) {
1341 dest_pan_scan->window[i].x = pan_scan_param->rect_left_offset[i];
1342 dest_pan_scan->window[i].y = pan_scan_param->rect_top_offset[i];
1343 dest_pan_scan->window[i].dx = pan_scan_param->rect_right_offset[i];
1344 dest_pan_scan->window[i].dy = pan_scan_param->rect_bottom_offset[i];
1345 }
1346 #ifndef PANSCAN_HDLR
1347 if (pan_scan_param->rect_repetition_period == 0)
1348 pan_scan_param->rect_id = NO_PAN_SCAN_BIT;
1349 else if (pan_scan_param->rect_repetition_period > 1)
1350 pan_scan_param->rect_repetition_period =
1351 (pan_scan_param->rect_repetition_period == 2)? 0 :
1352 (pan_scan_param->rect_repetition_period - 1);
1353 #endif
1354 } else
1355 pan_scan_param->rect_repetition_period = 0;
1356 }
1357 }
1358
process_ts_with_sei_vui(OMX_S64 timestamp)1359 OMX_S64 h264_stream_parser::process_ts_with_sei_vui(OMX_S64 timestamp)
1360 {
1361 bool clock_ts_flag = false;
1362 OMX_S64 clock_ts = timestamp;
1363 OMX_U32 deltaTfiDivisor = 2;
1364 if (vui_param.timing_info_present_flag) {
1365 if (vui_param.pic_struct_present_flag) {
1366 if (sei_pic_timing.clock_ts_flag) {
1367 clock_ts = ((sei_pic_timing.hours_value * 60 + sei_pic_timing.minutes_value) * 60 + sei_pic_timing.seconds_value) * 1e6 +
1368 (sei_pic_timing.n_frames * (vui_param.num_units_in_tick * (1 + sei_pic_timing.nuit_field_based_flag)) + sei_pic_timing.time_offset) *
1369 1e6 / vui_param.time_scale;
1370 DEBUG_PRINT("-->CLOCK TIMESTAMP : %lld", clock_ts);
1371 clock_ts_flag = true;
1372 }
1373 if (vui_param.fixed_frame_rate_flag) {
1374 switch (sei_pic_timing.pic_struct) {
1375 case 1:
1376 case 2:
1377 deltaTfiDivisor = 1;
1378 break;
1379 case 0:
1380 case 3:
1381 case 4:
1382 deltaTfiDivisor = 2;
1383 break;
1384 case 5:
1385 case 6:
1386 deltaTfiDivisor = 3;
1387 break;
1388 case 7:
1389 deltaTfiDivisor = 4;
1390 break;
1391 case 8:
1392 deltaTfiDivisor = 6;
1393 break;
1394 default:
1395 DEBUG_PRINT_ERROR("process_ts_with_sei_vui: pic_struct invalid!");
1396 }
1397 }
1398 }
1399 if (!clock_ts_flag) {
1400 if (vui_param.fixed_frame_rate_flag)
1401 clock_ts = calculate_fixed_fps_ts(timestamp, deltaTfiDivisor);
1402 else if (sei_buf_period.is_valid)
1403 clock_ts = calculate_buf_period_ts(timestamp);
1404 }
1405 } else {
1406 DEBUG_PRINT("NO TIMING information present in VUI!");
1407 }
1408 sei_pic_timing.is_valid = false; // SEI data is valid only for current frame
1409 return clock_ts;
1410 }
1411
1412 #ifdef PANSCAN_HDLR
1413
panscan_handler()1414 panscan_handler::panscan_handler() : panscan_data(NULL) {}
1415
~panscan_handler()1416 panscan_handler::~panscan_handler()
1417 {
1418 if (panscan_data) {
1419 free(panscan_data);
1420 panscan_data = NULL;
1421 }
1422 }
1423
initialize(int num_data)1424 bool panscan_handler::initialize(int num_data)
1425 {
1426 bool ret = false;
1427 if (!panscan_data) {
1428 panscan_data = (PANSCAN_NODE *) malloc (sizeof(PANSCAN_NODE) * num_data);
1429 if (panscan_data) {
1430 panscan_free.add_multiple(panscan_data, num_data);
1431 ret = true;
1432 }
1433 } else {
1434 DEBUG_PRINT_ERROR("ERROR: Old panscan memory must be freed to allocate new");
1435 }
1436 return ret;
1437 }
1438
get_free()1439 h264_pan_scan *panscan_handler::get_free()
1440 {
1441 h264_pan_scan *data = NULL;
1442 PANSCAN_NODE *panscan_node = panscan_used.watch_last();
1443 panscan_node = (!panscan_node || VALID_TS(panscan_node->start_ts))?
1444 panscan_free.remove_first() :
1445 panscan_used.remove_last();
1446 if (panscan_node) {
1447 panscan_node->start_ts = LLONG_MAX;
1448 panscan_node->end_ts = LLONG_MAX;
1449 panscan_node->pan_scan_param.rect_id = NO_PAN_SCAN_BIT;
1450 panscan_node->active = false;
1451 panscan_used.add_last(panscan_node);
1452 data = &panscan_node->pan_scan_param;
1453 }
1454 return data;
1455 }
1456
get_populated(OMX_S64 frame_ts)1457 h264_pan_scan *panscan_handler::get_populated(OMX_S64 frame_ts)
1458 {
1459 h264_pan_scan *data = NULL;
1460 PANSCAN_NODE *panscan_node = panscan_used.watch_first();
1461 while (panscan_node && !data) {
1462 if (VALID_TS(panscan_node->start_ts)) {
1463 if (panscan_node->active && frame_ts < panscan_node->start_ts)
1464 panscan_node->start_ts = frame_ts;
1465 if (frame_ts >= panscan_node->start_ts)
1466 if (frame_ts < panscan_node->end_ts) {
1467 data = &panscan_node->pan_scan_param;
1468 panscan_node->active = true;
1469 } else {
1470 panscan_free.add_last(panscan_used.remove_first());
1471 panscan_node = panscan_used.watch_first();
1472 }
1473 else
1474 // Finish search if current timestamp has not reached
1475 // start timestamp of first panscan data.
1476 panscan_node = NULL;
1477 } else {
1478 // Only one panscan data is stored for clips
1479 // with invalid timestamps in every frame
1480 data = &panscan_node->pan_scan_param;
1481 panscan_node->active = true;
1482 }
1483 }
1484 if (data) {
1485 if (data->rect_repetition_period == 0)
1486 panscan_free.add_last(panscan_used.remove_first());
1487 else if (data->rect_repetition_period > 1)
1488 data->rect_repetition_period -= 2;
1489 }
1490 PRINT_PANSCAN_DATA(panscan_node);
1491 return data;
1492 }
1493
update_last(OMX_S64 frame_ts)1494 void panscan_handler::update_last(OMX_S64 frame_ts)
1495 {
1496 PANSCAN_NODE *panscan_node = panscan_used.watch_last();
1497 if (panscan_node && !VALID_TS(panscan_node->start_ts)) {
1498 panscan_node->start_ts = frame_ts;
1499 PRINT_PANSCAN_DATA(panscan_node);
1500 if (panscan_node->prev) {
1501 if (frame_ts < panscan_node->prev->end_ts)
1502 panscan_node->prev->end_ts = frame_ts;
1503 else if (!VALID_TS(frame_ts))
1504 panscan_node->prev->pan_scan_param.rect_repetition_period = 0;
1505 PRINT_PANSCAN_DATA(panscan_node->prev);
1506 }
1507 }
1508 }
1509
1510 template <class NODE_STRUCT>
add_multiple(NODE_STRUCT * data_arr,int data_num)1511 void omx_dl_list<NODE_STRUCT>::add_multiple(NODE_STRUCT *data_arr, int data_num)
1512 {
1513 for (int idx = 0; idx < data_num; idx++)
1514 add_last(&data_arr[idx]);
1515 }
1516
1517 template <class NODE_STRUCT>
remove_first()1518 NODE_STRUCT *omx_dl_list<NODE_STRUCT>::remove_first()
1519 {
1520 NODE_STRUCT *data = head;
1521 if (head) {
1522 if (head->next) {
1523 head = head->next;
1524 head->prev = NULL;
1525 } else
1526 head = tail = NULL;
1527 data->next = data->prev = NULL;
1528 }
1529 return data;
1530 }
1531
1532 template <class NODE_STRUCT>
remove_last()1533 NODE_STRUCT *omx_dl_list<NODE_STRUCT>::remove_last()
1534 {
1535 NODE_STRUCT *data = tail;
1536 if (tail) {
1537 if (tail->prev) {
1538 tail = tail->prev;
1539 tail->next = NULL;
1540 } else
1541 head = tail = NULL;
1542 data->next = data->prev = NULL;
1543 }
1544 return data;
1545 }
1546
1547 template <class NODE_STRUCT>
add_last(NODE_STRUCT * data_ptr)1548 void omx_dl_list<NODE_STRUCT>::add_last(NODE_STRUCT* data_ptr)
1549 {
1550 if (data_ptr) {
1551 data_ptr->next = NULL;
1552 data_ptr->prev = tail;
1553 if (tail) {
1554 tail->next = data_ptr;
1555 tail = data_ptr;
1556 } else
1557 head = tail = data_ptr;
1558 }
1559 }
1560
1561 template <class NODE_STRUCT>
watch_first()1562 NODE_STRUCT* omx_dl_list<NODE_STRUCT>::watch_first()
1563 {
1564 return head;
1565 }
1566
1567 template <class NODE_STRUCT>
watch_last()1568 NODE_STRUCT* omx_dl_list<NODE_STRUCT>::watch_last()
1569 {
1570 return tail;
1571 }
1572
1573 #endif
1574