1 /*
2 *			GPAC - Multimedia Framework C SDK
3 *
4 *			Authors: Jean Le Feuvre
5 *			Copyright (c) Telecom ParisTech 2000-2012
6 *					All rights reserved
7 *
8 *  This file is part of GPAC / Media Tools sub-project
9 *
10 *  GPAC is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License as published by
12 *  the Free Software Foundation; either version 2, or (at your option)
13 *  any later version.
14 *
15 *  GPAC is distributed in the hope that it will be useful,
16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 *  GNU Lesser General Public License for more details.
19 *
20 *  You should have received a copy of the GNU Lesser General Public
21 *  License along with this library; see the file COPYING.  If not, write to
22 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 
26 
27 #ifndef _GF_MEDIA_DEV_H_
28 #define _GF_MEDIA_DEV_H_
29 
30 #include <gpac/media_tools.h>
31 
32 #ifndef GPAC_DISABLE_STREAMING
33 #include <gpac/ietf.h>
34 #endif
35 
36 #ifndef GPAC_DISABLE_ISOM
37 void gf_media_get_sample_average_infos(GF_ISOFile *file, u32 Track, u32 *avgSize, u32 *MaxSize, u32 *TimeDelta, u32 *maxCTSDelta, u32 *const_duration, u32 *bandwidth);
38 #endif
39 
40 
41 #ifndef GPAC_DISABLE_MEDIA_IMPORT
42 GF_Err gf_import_message(GF_MediaImporter *import, GF_Err e, char *format, ...);
43 #endif /*GPAC_DISABLE_MEDIA_IMPORT*/
44 
45 #ifndef GPAC_DISABLE_AV_PARSERS
46 
47 u32 gf_latm_get_value(GF_BitStream *bs);
48 
49 #define GF_SVC_SSPS_ID_SHIFT	16
50 
51 /*returns 0 if not a start code, or size of start code (3 or 4 bytes). If start code, bitstream
52 is positionned AFTER start code*/
53 u32 gf_media_nalu_is_start_code(GF_BitStream *bs);
54 
55 /*returns size of chunk between current and next startcode (excluding startcode sizes), 0 if no more startcodes (eos)*/
56 u32 gf_media_nalu_next_start_code_bs(GF_BitStream *bs);
57 
58 /*returns size of chunk between current startcode and end of payload (0x000000) - if no trailing bytes are found, returns the same as gf_media_nalu_next_start_code_bs*/
59 u32 gf_media_nalu_payload_end_bs(GF_BitStream *bs);
60 
61 /*return nb bytes from current data until the next start code and set the size of the next start code (3 or 4 bytes)
62 returns data_len if no startcode found and sets sc_size to 0 (last nal in payload)*/
63 u32 gf_media_nalu_next_start_code(const u8 *data, u32 data_len, u32 *sc_size);
64 
65 /*returns NAL unit type - bitstream must be sync'ed!!*/
66 u8 AVC_NALUType(GF_BitStream *bs);
67 Bool SVC_NALUIsSlice(u8 type);
68 
69 
70 enum
71 {
72 	/*SPS has been parsed*/
73 	AVC_SPS_PARSED = 1,
74 	/*SPS has been declared to the upper layer*/
75 	AVC_SPS_DECLARED = 1 << 1,
76 	/*SUB-SPS has been parsed*/
77 	AVC_SUBSPS_PARSED = 1 << 2,
78 	/*SUB-SPS has been declared to the upper layer*/
79 	AVC_SUBSPS_DECLARED = 1 << 3,
80 	/*SPS extension has been parsed*/
81 	AVC_SPS_EXT_DECLARED = 1 << 4,
82 };
83 
84 typedef struct
85 {
86 	u8 cpb_removal_delay_length_minus1;
87 	u8 dpb_output_delay_length_minus1;
88 	u8 time_offset_length;
89 	/*to be eventually completed by other hrd members*/
90 } AVC_HRD;
91 
92 typedef struct
93 {
94 	s32 timing_info_present_flag;
95 	u32 num_units_in_tick;
96 	u32 time_scale;
97 	s32 fixed_frame_rate_flag;
98 
99 	u32 par_num, par_den;
100 
101 	Bool nal_hrd_parameters_present_flag;
102 	Bool vcl_hrd_parameters_present_flag;
103 	AVC_HRD hrd;
104 
105 	Bool pic_struct_present_flag;
106 	/*to be eventually completed by other vui members*/
107 } AVC_VUI;
108 
109 typedef struct
110 {
111 	s32 profile_idc;
112 	s32 level_idc;
113 	s32 prof_compat;
114 	s32 log2_max_frame_num;
115 	u32 poc_type, poc_cycle_length;
116 	s32 log2_max_poc_lsb;
117 	s32 delta_pic_order_always_zero_flag;
118 	s32 offset_for_non_ref_pic, offset_for_top_to_bottom_field;
119 	Bool frame_mbs_only_flag;
120 	u8 chroma_format;
121 	u8 luma_bit_depth_m8;
122 	u8 chroma_bit_depth_m8;
123 
124 	s16 offset_for_ref_frame[256];
125 
126 	u32 width, height;
127 
128 	AVC_VUI vui;
129 
130 	/*used to discard repeated SPSs - 0: not parsed, 1 parsed, 2 sent*/
131 	u32 state;
132 
133 	/*for SVC stats during import*/
134 	u32 nb_ei, nb_ep, nb_eb;
135 } AVC_SPS;
136 
137 typedef struct
138 {
139 	s32 id; /* used to compare pps when storing SVC PSS */
140 	s32 sps_id;
141 	s32 pic_order_present;			/* pic_order_present_flag*/
142 	s32 redundant_pic_cnt_present;	/* redundant_pic_cnt_present_flag */
143 	u32 slice_group_count;			/* num_slice_groups_minus1 + 1*/
144 									/*used to discard repeated SPSs - 0: not parsed, 1 parsed, 2 sent*/
145 	u32 status;
146 
147 } AVC_PPS;
148 
149 typedef struct
150 {
151 	s32 idr_pic_flag;
152 	u8 temporal_id, priority_id, dependency_id, quality_id;
153 } SVC_NALUHeader;
154 
155 typedef struct
156 {
157 	u8 nal_ref_idc, nal_unit_type, field_pic_flag, bottom_field_flag;
158 	u32 frame_num, idr_pic_id, poc_lsb, slice_type;
159 	s32 delta_poc_bottom;
160 	s32 delta_poc[2];
161 	s32 redundant_pic_cnt;
162 
163 	s32 poc;
164 	u32 poc_msb, poc_msb_prev, poc_lsb_prev, frame_num_prev;
165 	s32 frame_num_offset, frame_num_offset_prev;
166 
167 	AVC_SPS *sps;
168 	AVC_PPS *pps;
169 	SVC_NALUHeader NalHeader;
170 } AVCSliceInfo;
171 
172 
173 typedef struct
174 {
175 	u32 frame_cnt;
176 	u8 exact_match_flag;
177 	u8 broken_link_flag;
178 	u8 changing_slice_group_idc;
179 	u8 valid;
180 } AVCSeiRecoveryPoint;
181 
182 typedef struct
183 {
184 	u8 pic_struct;
185 	/*to be eventually completed by other pic_timing members*/
186 } AVCSeiPicTiming;
187 
188 typedef struct
189 {
190 	AVCSeiRecoveryPoint recovery_point;
191 	AVCSeiPicTiming pic_timing;
192 	/*to be eventually completed by other sei*/
193 } AVCSei;
194 
195 typedef struct
196 {
197 	AVC_SPS sps[32]; /* range allowed in the spec is 0..31 */
198 	s8 sps_active_idx;	/*currently active sps; must be initalized to -1 in order to discard not yet decodable SEIs*/
199 
200 	AVC_PPS pps[255];
201 
202 	AVCSliceInfo s_info;
203 	AVCSei sei;
204 
205 	Bool is_svc;
206 } AVCState;
207 
208 typedef struct
209 {
210 	u32 NALUnitHeader;
211 	u8 track_ref_index;
212 	s8 sample_offset;
213 	u32 data_offset;
214 	u32 data_length;
215 } SVC_Extractor;
216 
217 
218 /*return sps ID or -1 if error*/
219 s32 gf_media_avc_read_sps(const char *sps_data, u32 sps_size, AVCState *avc, u32 subseq_sps, u32 *vui_flag_pos);
220 /*return pps ID or -1 if error*/
221 s32 gf_media_avc_read_pps(const char *pps_data, u32 pps_size, AVCState *avc);
222 /*return sps ID or -1 if error*/
223 s32 gf_media_avc_read_sps_ext(const char *spse_data, u32 spse_size);
224 /*is slice an IDR*/
225 Bool gf_media_avc_slice_is_IDR(AVCState *avc);
226 /*is slice containing intra MB only*/
227 Bool gf_media_avc_slice_is_intra(AVCState *avc);
228 /*parses NALU, updates avc state and returns:
229 1 if NALU part of new frame
230 0 if NALU part of prev frame
231 -1 if bitstream error
232 */
233 s32 gf_media_avc_parse_nalu(GF_BitStream *bs, u32 nal_hdr, AVCState *avc);
234 /*remove SEI messages not allowed in MP4*/
235 /*nota: 'buffer' remains unmodified but cannot be set const*/
236 u32 gf_media_avc_reformat_sei(char *buffer, u32 nal_size, AVCState *avc);
237 
238 #ifndef GPAC_DISABLE_ISOM
239 GF_Err gf_media_avc_change_par(GF_AVCConfig *avcc, s32 ar_n, s32 ar_d);
240 GF_Err gf_media_hevc_change_par(GF_HEVCConfig *hvcc, s32 ar_n, s32 ar_d);
241 #endif
242 
243 
244 
245 typedef struct
246 {
247 	Bool profile_present_flag, level_present_flag, tier_flag;
248 	u8 profile_space;
249 	u8 profile_idc;
250 	u32 profile_compatibility_flag;
251 	u8 level_idc;
252 } HEVC_SublayerPTL;
253 
254 typedef struct
255 {
256 	u8 profile_space, tier_flag, profile_idc, level_idc;
257 	u32 profile_compatibility_flag;
258 	Bool general_progressive_source_flag;
259 	Bool general_interlaced_source_flag;
260 	Bool general_non_packed_constraint_flag;
261 	Bool general_frame_only_constraint_flag;
262 	u64 general_reserved_44bits;
263 
264 	HEVC_SublayerPTL sub_ptl[8];
265 } HEVC_ProfileTierLevel;
266 
267 typedef struct
268 {
269 	u32 num_negative_pics;
270 	u32 num_positive_pics;
271 	s32 delta_poc[16];
272 } HEVC_ReferencePictureSets;
273 
274 typedef struct
275 {
276 	s32 id, vps_id;
277 	/*used to discard repeated SPSs - 0: not parsed, 1 parsed, 2 stored*/
278 	u32 state;
279 	u32 crc;
280 	u32 width, height;
281 
282 	HEVC_ProfileTierLevel ptl;
283 
284 	u8 chroma_format_idc;
285 	Bool cw_flag;
286 	u32 cw_left, cw_right, cw_top, cw_bottom;
287 	u8 bit_depth_luma;
288 	u8 bit_depth_chroma;
289 	u8 log2_max_pic_order_cnt_lsb;
290 	Bool separate_colour_plane_flag;
291 
292 	u32 max_CU_width, max_CU_height, max_CU_depth;
293 	u32 bitsSliceSegmentAddress;
294 
295 	u32 num_short_term_ref_pic_sets, num_long_term_ref_pic_sps;
296 	HEVC_ReferencePictureSets rps[64];
297 
298 
299 	Bool aspect_ratio_info_present_flag, long_term_ref_pics_present_flag, temporal_mvp_enable_flag, sample_adaptive_offset_enabled_flag;
300 	u8 sar_idc;
301 	u16 sar_width, sar_height;
302 	Bool has_timing_info;
303 	u32 num_units_in_tick, time_scale;
304 	Bool poc_proportional_to_timing_flag;
305 	u32 num_ticks_poc_diff_one_minus1;
306 
307 	u32 rep_format_idx;
308 } HEVC_SPS;
309 
310 typedef struct
311 {
312 	s32 id;
313 	u32 sps_id;
314 	/*used to discard repeated SPSs - 0: not parsed, 1 parsed, 2 stored*/
315 	u32 state;
316 	u32 crc;
317 
318 	Bool dependent_slice_segments_enabled_flag, tiles_enabled_flag, uniform_spacing_flag;
319 	u32 num_extra_slice_header_bits, num_ref_idx_l0_default_active, num_ref_idx_l1_default_active;
320 	Bool slice_segment_header_extension_present_flag, output_flag_present_flag, lists_modification_present_flag, cabac_init_present_flag;
321 	Bool weighted_pred_flag, weighted_bipred_flag, slice_chroma_qp_offsets_present_flag, deblocking_filter_override_enabled_flag, loop_filter_across_slices_enabled_flag, entropy_coding_sync_enabled_flag;
322 	Bool loop_filter_across_tiles_enabled_flag;
323 
324 	u32 num_tile_columns, num_tile_rows;
325 	u32 column_width[22], row_height[20];
326 } HEVC_PPS;
327 
328 typedef struct RepFormat
329 {
330 	u32 chroma_format_idc;
331 	u32 pic_width_luma_samples;
332 	u32 pic_height_luma_samples;
333 	u32 bit_depth_luma;
334 	u32 bit_depth_chroma;
335 	u8 separate_colour_plane_flag;
336 } HEVC_RepFormat;
337 
338 typedef struct
339 {
340 	u16 avg_bit_rate, max_bit_rate, avg_pic_rate;
341 	u8 constand_pic_rate_idc;
342 } HEVC_RateInfo;
343 
344 
345 #define MAX_LHVC_LAYERS	4
346 #define MAX_NUM_LAYER_SETS 1024
347 typedef struct
348 {
349 	s32 id;
350 	/*used to discard repeated SPSs - 0: not parsed, 1 parsed, 2 stored*/
351 	u32 state;
352 	u32 crc;
353 	Bool vps_extension_found;
354 	u32 max_layers, max_sub_layers, max_layer_id, num_layer_sets;
355 	Bool temporal_id_nesting;
356 	HEVC_ProfileTierLevel ptl;
357 
358 	HEVC_SublayerPTL sub_ptl[8];
359 	HEVC_RateInfo rates[8];
360 
361 
362 	u32 scalability_mask[16];
363 	u32 dimension_id[MAX_LHVC_LAYERS][16];
364 	u32 layer_id_in_nuh[MAX_LHVC_LAYERS];
365 	u32 layer_id_in_vps[MAX_LHVC_LAYERS];
366 
367 	u8 num_profile_tier_level, num_output_layer_sets;
368 	u32 profile_level_tier_idx[MAX_LHVC_LAYERS];
369 	HEVC_ProfileTierLevel ext_ptl[MAX_LHVC_LAYERS];
370 
371 	u32 num_rep_formats;
372 	HEVC_RepFormat rep_formats[16];
373 	u32 rep_format_idx[16];
374 	Bool base_layer_internal_flag, base_layer_available_flag;
375 	u8 num_layers_in_id_list[MAX_NUM_LAYER_SETS];
376 	u8 direct_dependency_flag[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS];
377 	Bool output_layer_flag[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS];
378 	u8 profile_tier_level_idx[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS];
379 	Bool alt_output_layer_flag[MAX_LHVC_LAYERS];
380 	u8 num_necessary_layers[MAX_LHVC_LAYERS];
381 	Bool necessary_layers_flag[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS];
382 	u8 LayerSetLayerIdList[MAX_LHVC_LAYERS][MAX_LHVC_LAYERS];
383 	u8 LayerSetLayerIdListMax[MAX_LHVC_LAYERS]; //the highest value in LayerSetLayerIdList[i]
384 } HEVC_VPS;
385 
386 typedef struct
387 {
388 	AVCSeiRecoveryPoint recovery_point;
389 	AVCSeiPicTiming pic_timing;
390 
391 } HEVC_SEI;
392 
393 typedef struct
394 {
395 	u8 nal_unit_type;
396 	s8 temporal_id;
397 
398 	u32 frame_num, poc_lsb, slice_type;
399 
400 	s32 redundant_pic_cnt;
401 
402 	s32 poc;
403 	u32 poc_msb, poc_msb_prev, poc_lsb_prev, frame_num_prev;
404 	s32 frame_num_offset, frame_num_offset_prev;
405 
406 	Bool dependent_slice_segment_flag;
407 	Bool first_slice_segment_in_pic_flag;
408 	u32 slice_segment_address;
409 	u8 prev_layer_id_plus1;
410 
411 	HEVC_SPS *sps;
412 	HEVC_PPS *pps;
413 } HEVCSliceInfo;
414 
415 typedef struct _hevc_state
416 {
417 	HEVC_SPS sps[16]; /* range allowed in the spec is 0..15 */
418 	s8 sps_active_idx;	/*currently active sps; must be initalized to -1 in order to discard not yet decodable SEIs*/
419 
420 	HEVC_PPS pps[64];
421 
422 	HEVC_VPS vps[16];
423 
424 	HEVCSliceInfo s_info;
425 	HEVC_SEI sei;
426 
427 	Bool is_svc;
428 } HEVCState;
429 
430 enum
431 {
432 	GF_HEVC_SLICE_TYPE_B = 0,
433 	GF_HEVC_SLICE_TYPE_P = 1,
434 	GF_HEVC_SLICE_TYPE_I = 2,
435 };
436 s32 gf_media_hevc_read_vps(char *data, u32 size, HEVCState *hevc);
437 s32 gf_media_hevc_read_sps(char *data, u32 size, HEVCState *hevc);
438 s32 gf_media_hevc_read_pps(char *data, u32 size, HEVCState *hevc);
439 s32 gf_media_hevc_parse_nalu(GF_BitStream *bs, HEVCState *hevc, u8 *nal_unit_type, u8 *temporal_id, u8 *layer_id);
440 Bool gf_media_hevc_slice_is_intra(HEVCState *hevc);
441 Bool gf_media_hevc_slice_is_IDR(HEVCState *hevc);
442 //parses VPS and rewrites data buffer after removing VPS extension
443 s32 gf_media_hevc_read_vps_ex(char *data, u32 *size, HEVCState *hevc, Bool remove_extensions);
444 
445 
446 GF_Err gf_hevc_get_sps_info_with_state(HEVCState *hevc_state, char *sps_data, u32 sps_size, u32 *sps_id, u32 *width, u32 *height, s32 *par_n, s32 *par_d);
447 
448 #endif /*GPAC_DISABLE_AV_PARSERS*/
449 
450 typedef struct
451 {
452 	u8 rate_idx;
453 	u8 pck_size;
454 } QCPRateTable;
455 
456 
457 #if !defined(GPAC_DISABLE_ISOM) && !defined(GPAC_DISABLE_STREAMING)
458 
459 GP_RTPPacketizer *gf_rtp_packetizer_create_and_init_from_file(GF_ISOFile *file,
460 	u32 TrackNum,
461 	void *cbk_obj,
462 	void(*OnNewPacket)(void *cbk, GF_RTPHeader *header),
463 	void(*OnPacketDone)(void *cbk, GF_RTPHeader *header),
464 	void(*OnDataReference)(void *cbk, u32 payload_size, u32 offset_from_orig),
465 	void(*OnData)(void *cbk, char *data, u32 data_size, Bool is_head),
466 	u32 Path_MTU,
467 	u32 max_ptime,
468 	u32 default_rtp_rate,
469 	u32 flags,
470 	u8 PayloadID,
471 	Bool copy_media,
472 	u32 InterleaveGroupID,
473 	u8 InterleaveGroupPriority);
474 
475 void gf_media_format_ttxt_sdp(GP_RTPPacketizer *builder, char *payload_name, char *sdpLine, GF_ISOFile *file, u32 track);
476 
477 #endif
478 
479 
480 typedef enum
481 {
482 	GF_DASH_TEMPLATE_SEGMENT = 0,
483 	GF_DASH_TEMPLATE_INITIALIZATION,
484 	GF_DASH_TEMPLATE_TEMPLATE,
485 	GF_DASH_TEMPLATE_INITIALIZATION_TEMPLATE,
486 	GF_DASH_TEMPLATE_REPINDEX,
487 } GF_DashTemplateSegmentType;
488 
489 GF_Err gf_media_mpd_format_segment_name(GF_DashTemplateSegmentType seg_type, Bool is_bs_switching, char *segment_name, const char *output_file_name, const char *rep_id, const char *base_url, const char *seg_rad_name, const char *seg_ext, u64 start_time, u32 bandwidth, u32 segment_number, Bool use_segment_timeline);
490 
491 
492 #endif		/*_GF_MEDIA_DEV_H_*/
493 
494