00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef H264PARSER_H
00024 #define H264PARSER_H
00025
00026 #include <QString>
00027 #include <stdint.h>
00028 #include "mythconfig.h"
00029 #include "compat.h"
00030
00031 #ifndef INT_BIT
00032 #define INT_BIT (CHAR_BIT * sizeof(int))
00033 #endif
00034
00035
00036 extern "C" {
00037 #include "libavutil/common.h"
00038 }
00039 #ifndef av_alias
00040 #if HAVE_ATTRIBUTE_MAY_ALIAS && (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(3,3)
00041 # define av_alias __attribute__((may_alias))
00042 #else
00043 # define av_alias
00044 #endif
00045 #endif
00046
00047 extern "C" {
00048 #include "libavcodec/get_bits.h"
00049 }
00050
00051 class H264Parser {
00052 public:
00053
00054 enum {
00055 MAX_SLICE_HEADER_SIZE = 256
00056 };
00057
00058
00059 enum NAL_unit_type {
00060 UNKNOWN = 0,
00061 SLICE = 1,
00062 SLICE_DPA = 2,
00063 SLICE_DPB = 3,
00064 SLICE_DPC = 4,
00065 SLICE_IDR = 5,
00066 SEI = 6,
00067 SPS = 7,
00068 PPS = 8,
00069 AU_DELIMITER = 9,
00070 END_SEQUENCE = 10,
00071 END_STREAM = 11,
00072 FILLER_DATA = 12,
00073 SPS_EXT = 13,
00074 AUXILIARY_SLICE = 19
00075 };
00076
00077 enum SEI_type {
00078 SEI_TYPE_PIC_TIMING = 1,
00079 SEI_TYPE_USER_DATA_UNREGISTERED = 5,
00080 SEI_TYPE_RECOVERY_POINT = 6
00081 };
00082
00083
00084
00085
00086
00087
00088
00089
00090 enum SLICE_type {
00091 SLICE_P = 0,
00092 SLICE_B = 1,
00093 SLICE_I = 2,
00094 SLICE_SP = 3,
00095 SLICE_SI = 4,
00096 SLICE_P_a = 5,
00097 SLICE_B_a = 6,
00098 SLICE_I_a = 7,
00099 SLICE_SP_a = 8,
00100 SLICE_SI_a = 9,
00101 SLICE_UNDEF = 10
00102 };
00103
00104 enum frame_type {
00105 FRAME = 'F',
00106 FIELD_TOP = 'T',
00107 FIELD_BOTTOM = 'B'
00108 };
00109
00110 H264Parser(void);
00111 ~H264Parser(void) {delete [] rbsp_buffer;}
00112
00113 uint32_t addBytes(const uint8_t *bytes,
00114 const uint32_t byte_count,
00115 const uint64_t stream_offset);
00116 void Reset(void);
00117
00118 QString NAL_type_str(uint8_t type);
00119
00120 bool stateChanged(void) const { return state_changed; }
00121
00122 uint8_t lastNALtype(void) const { return nal_unit_type; }
00123
00124 frame_type FieldType(void) const
00125 {
00126 if (bottom_field_flag == -1)
00127 return FRAME;
00128 else
00129 return bottom_field_flag ? FIELD_BOTTOM : FIELD_TOP;
00130 }
00131
00132 bool onFrameStart(void) const { return on_frame; }
00133 bool onKeyFrameStart(void) const { return on_key_frame; }
00134
00135 uint pictureWidth(void) const { return pic_width; }
00136 uint pictureHeight(void) const { return pic_height; }
00137
00140 uint aspectRatio(void) const;
00141 uint frameRate(void) const;
00142
00143 uint64_t frameAUstreamOffset(void) const {return frame_start_offset;}
00144 uint64_t keyframeAUstreamOffset(void) const {return keyframe_start_offset;}
00145
00146 static int isKeySlice(uint slice_type)
00147 {
00148 return (slice_type == SLICE_I ||
00149 slice_type == SLICE_SI ||
00150 slice_type == SLICE_I_a ||
00151 slice_type == SLICE_SI_a);
00152 }
00153
00154 static bool NALisSlice(uint8_t nal_type)
00155 {
00156 return (nal_type == SLICE ||
00157 nal_type == SLICE_DPA ||
00158 nal_type == SLICE_IDR);
00159 }
00160
00161 void use_I_forKeyframes(bool val) { I_is_keyframe = val; }
00162
00163 uint32_t GetTimeScale(void) const { return timeScale; }
00164
00165 uint32_t GetUnitsInTick(void) const { return unitsInTick; }
00166
00167 void parse_SPS(uint8_t *sps, uint32_t sps_size,
00168 bool& interlaced, int32_t& max_ref_frames);
00169
00170 private:
00171 enum constants {EXTENDED_SAR = 255};
00172
00173 inline void set_AU_pending(void)
00174 {
00175 if (!AU_pending)
00176 {
00177 AU_pending = true;
00178 AU_offset = pkt_offset;
00179 au_contains_keyframe_message = false;
00180 }
00181 }
00182
00183 bool new_AU(void);
00184 void resetRBSP(void);
00185 bool fillRBSP(const uint8_t *byteP, uint32_t byte_count,
00186 bool found_start_code);
00187 void processRBSP(bool rbsp_complete);
00188 bool decode_Header(GetBitContext *gb);
00189 void decode_SPS(GetBitContext *gb);
00190 void decode_PPS(GetBitContext * gb);
00191 void decode_SEI(GetBitContext * gb);
00192 void vui_parameters(GetBitContext * gb);
00193
00194 bool AU_pending;
00195 bool state_changed;
00196 bool seen_sps;
00197 bool au_contains_keyframe_message;
00198 bool is_keyframe;
00199 bool I_is_keyframe;
00200
00201 uint32_t sync_accumulator;
00202 uint8_t *rbsp_buffer;
00203 uint32_t rbsp_buffer_size;
00204 uint32_t rbsp_index;
00205 uint32_t consecutive_zeros;
00206 bool have_unfinished_NAL;
00207
00208 int prev_frame_num, frame_num;
00209 uint slice_type;
00210 int prev_pic_parameter_set_id, pic_parameter_set_id;
00211 int8_t prev_field_pic_flag, field_pic_flag;
00212 int8_t prev_bottom_field_flag, bottom_field_flag;
00213 uint8_t prev_nal_ref_idc, nal_ref_idc;
00214 uint8_t prev_pic_order_cnt_type, pic_order_cnt_type;
00215 int prev_pic_order_cnt_lsb, pic_order_cnt_lsb;
00216 int prev_delta_pic_order_cnt_bottom, delta_pic_order_cnt_bottom;
00217 int prev_delta_pic_order_cnt[2], delta_pic_order_cnt[2];
00218 uint8_t prev_nal_unit_type, nal_unit_type;
00219 uint prev_idr_pic_id, idr_pic_id;
00220
00221 uint log2_max_frame_num, log2_max_pic_order_cnt_lsb;
00222 uint seq_parameter_set_id;
00223
00224 uint8_t delta_pic_order_always_zero_flag;
00225 uint8_t separate_colour_plane_flag;
00226 int8_t frame_mbs_only_flag;
00227 int8_t pic_order_present_flag;
00228 int8_t redundant_pic_cnt_present_flag;
00229
00230 uint num_ref_frames;
00231 uint redundant_pic_cnt;
00232
00233 uint pic_width, pic_height;
00234 uint frame_crop_left_offset;
00235 uint frame_crop_right_offset;
00236 uint frame_crop_top_offset;
00237 uint frame_crop_bottom_offset;
00238 uint8_t aspect_ratio_idc;
00239 uint sar_width, sar_height;
00240 uint32_t unitsInTick, timeScale;
00241 bool fixedRate;
00242
00243 uint64_t pkt_offset, AU_offset, frame_start_offset, keyframe_start_offset;
00244 bool on_frame, on_key_frame;
00245 };
00246
00247 #endif