1/*
2* Copyright (c) 2009-2011 Intel Corporation.  All rights reserved.
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17#ifndef SEC_VIDEO_PARSER_H_
18#define SEC_VIDEO_PARSER_H_
19
20#include <stdint.h>
21
22/* H264 start code values */
23typedef enum _h264_nal_unit_type
24{
25    h264_NAL_UNIT_TYPE_unspecified = 0,
26    h264_NAL_UNIT_TYPE_SLICE,
27    h264_NAL_UNIT_TYPE_DPA,
28    h264_NAL_UNIT_TYPE_DPB,
29    h264_NAL_UNIT_TYPE_DPC,
30    h264_NAL_UNIT_TYPE_IDR,
31    h264_NAL_UNIT_TYPE_SEI,
32    h264_NAL_UNIT_TYPE_SPS,
33    h264_NAL_UNIT_TYPE_PPS,
34    h264_NAL_UNIT_TYPE_Acc_unit_delimiter,
35    h264_NAL_UNIT_TYPE_EOSeq,
36    h264_NAL_UNIT_TYPE_EOstream,
37    h264_NAL_UNIT_TYPE_filler_data,
38    h264_NAL_UNIT_TYPE_SPS_extension,
39    h264_NAL_UNIT_TYPE_ACP = 19,
40    h264_NAL_UNIT_TYPE_Slice_extension = 20
41} h264_nal_unit_type_t;
42
43#define MAX_OP  16
44
45enum dec_ref_pic_marking_flags {
46    IDR_PIC_FLAG = 0,
47    NO_OUTPUT_OF_PRIOR_PICS_FLAG,
48    LONG_TERM_REFERENCE_FLAG,
49    ADAPTIVE_REF_PIC_MARKING_MODE_FLAG
50};
51
52typedef struct _dec_ref_pic_marking_t {
53    union {
54        uint8_t flags;
55        struct {
56            uint8_t idr_pic_flag:1;
57            uint8_t no_output_of_prior_pics_flag:1;
58            uint8_t long_term_reference_flag:1;
59            uint8_t adaptive_ref_pic_marking_mode_flag:1;
60        };
61    };
62    struct {
63        uint8_t memory_management_control_operation;
64        union {
65            struct {
66                uint8_t difference_of_pic_nums_minus1;
67            } op1;
68            struct {
69                uint8_t long_term_pic_num;
70            } op2;
71            struct {
72                uint8_t difference_of_pic_nums_minus1;
73                uint8_t long_term_frame_idx;
74            } op3;
75            struct {
76                uint8_t max_long_term_frame_idx_plus1;
77            } op4;
78            struct {
79                uint8_t long_term_frame_idx;
80            } op6;
81        };
82    } op[MAX_OP];
83} dec_ref_pic_marking_t;
84
85enum slice_header_flags {
86    FIELD_PIC_FLAG = 0,
87    BOTTOM_FIELD_FLAG
88};
89
90typedef struct _slice_header_t {
91    uint8_t nal_unit_type;
92    uint8_t pps_id;
93    uint8_t padding;    // TODO: padding needed because flags in secfw impl. is a big-endian uint16_t
94    union {
95        uint8_t flags;
96        struct {
97            uint8_t field_pic_flag:1;
98            uint8_t bottom_field_flag:1;
99        };
100    };
101    uint32_t first_mb_in_slice;
102    uint32_t frame_num;
103    uint16_t idr_pic_id;
104    uint16_t pic_order_cnt_lsb;
105    int32_t delta_pic_order_cnt[2];
106    int32_t delta_pic_order_cnt_bottom;
107} slice_header_t;
108
109typedef struct {
110    uint8_t type;
111    uint32_t offset;
112    uint8_t* data;
113    uint32_t length;
114    slice_header_t* slice_header;
115} nalu_info_t;
116
117typedef struct {
118    uint32_t iv[4];
119    uint32_t mode;
120    uint32_t app_id;
121} pavp_info_t;
122
123#define MAX_NUM_NALUS   20
124
125typedef struct {
126    uint8_t* data;
127    uint32_t length;
128    pavp_info_t* pavp;
129    dec_ref_pic_marking_t* dec_ref_pic_marking;
130    uint32_t num_nalus;
131    nalu_info_t nalus[MAX_NUM_NALUS];
132} frame_info_t;
133
134int parser_init(void);
135int parse_frame(uint8_t* frame, uint32_t frame_size, uint8_t* nalu_data, uint32_t* nalu_data_size);
136
137// DEBUG PRINTING
138void print_slice_header(slice_header_t* slice_header);
139void print_dec_ref_pic_marking(dec_ref_pic_marking_t* dec_ref_pic_marking);
140void print_data_bytes(uint8_t* data, uint32_t count);
141void print_nalu_data(uint8_t* nalu_data, uint32_t nalu_data_size);
142
143// BYTESWAPPING
144uint16_t byteswap_16(uint16_t word);
145uint32_t byteswap_32(uint32_t dword);
146void byteswap_slice_header(slice_header_t* slice_header);
147void byteswap_dec_ref_pic_marking(dec_ref_pic_marking_t* dec_ref_pic_marking);
148void byteswap_nalu_data(uint8_t* nalu_data, uint32_t nalu_data_size);
149
150#endif /* SEC_VIDEO_PARSER_H_ */
151