1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Jean Le Feuvre
5  *			Copyright (c) Telecom ParisTech 2000-2019
6  *					All rights reserved
7  *
8  *  This file is part of GPAC / Authoring 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 #ifndef _GF_PARSERS_AV_H_
27 #define _GF_PARSERS_AV_H_
28 
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /*!
35 \file <gpac/avparse.h>
36 \brief Utility tools for audio and video raw media parsing.
37 */
38 
39 /*!
40 \addtogroup media_grp
41 
42 You will find in this module the documentation of all media tools in GPAC.
43 */
44 
45 
46 /*!
47 \addtogroup avp_grp AV Parsing
48 \ingroup media_grp
49 \brief Utility tools for audio and video raw media parsing.
50 
51 This section documents the audio and video parsing functions of the GPAC framework.
52 @{
53 */
54 
55 
56 #include <gpac/bitstream.h>
57 
58 
59 
60 /*!
61   Reduces input width/height to common aspect ration num/denum values
62 \param width width of the aspect ratio
63 \param height height of the aspect ratio
64  */
65 void gf_media_reduce_aspect_ratio(u32 *width, u32 *height);
66 
67 /*!
68  Reduces input FPS to a more compact value (eg 25000/1000 -> 25/1)
69 \param timescale timescale of the aspect ratio
70 \param sample_dur sample duration of the aspect ratio in the given timescale
71  */
72 void gf_media_get_reduced_frame_rate(u32 *timescale, u32 *sample_dur);
73 
74 /*! inserts emulation prevention bytes from buffer_src into buffer_dst
75 \param buffer_src source buffer (NAL without EPB)
76 \param buffer_dst destination buffer (NAL with EPB)
77 \param nal_size source buffer size
78 \return size of buffer after adding emulation prevention bytes
79 */
80 u32 gf_media_nalu_add_emulation_bytes(const u8 *buffer_src, u8 *buffer_dst, u32 nal_size);
81 
82 /*! gets the number of emulation prevention bytes to add to a non emulated buffer
83 \param buffer source buffer (NAL without EPB)
84 \param nal_size source buffer size
85 \return the number of emulation prevention bytes to add
86 */
87 u32 gf_media_nalu_emulation_bytes_add_count(u8 *buffer, u32 nal_size);
88 
89 /*! MPEG-1, MPEG-1, MPEG-4 part 2 decoder specific info (only serialized for MPEG-4) */
90 typedef struct
91 {
92 	/*! video PL*/
93 	u8 VideoPL;
94 	/*! set if stream is RAP only*/
95 	u8 RAP_stream;
96 	/*! MPEG-4 part 2 video object type*/
97 	u8 objectType;
98 	/*! set if object has shape coding*/
99 	u8 has_shape;
100 	/*! set if object is an enhancement layer*/
101 	u8 enh_layer;
102 	/*! video horizontal size*/
103 	u16 width;
104 	/*! video vertical size*/
105 	u16 height;
106 	/*! pixel aspect ratio numerator*/
107 	u8 par_num;
108 	/*! pixel aspect ratio denominator*/
109 	u8 par_den;
110 
111 	/*! video clock rate - frames are spaced by time_increment/clock_rate seconds*/
112 	u16 clock_rate;
113 	/*! number of bits to code the time increment (internal use only)*/
114 	u8 NumBitsTimeIncrement;
115 	/*! time increment between frames*/
116 	u32 time_increment;
117 	/*! framerate, for MPEG 1/2*/
118 	Double fps;
119 	/*! position of next object in the bitstream*/
120 	u32 next_object_start;
121 } GF_M4VDecSpecInfo;
122 
123 
124 /*! MPEG (1,2,4) visual object parser (DSI extraction and timing/framing)*/
125 typedef struct __tag_m4v_parser GF_M4VParser;
126 
127 #ifndef GPAC_DISABLE_AV_PARSERS
128 /*! Creates a new MPEG-1/2/4 video parser
129 \param data buffer to parse
130 \param data_size size of buffer to parse
131 \param mpeg12video if set, parses as MPEG-1 or MPEG-2
132 \return the created parser
133 */
134 GF_M4VParser *gf_m4v_parser_new(u8 *data, u64 data_size, Bool mpeg12video);
135 /*! Creates a new MPEG-1/2/4 video parser from a bitstream object
136 \param bs the bitstream object to use for parsing
137 \param mpeg12video if set, parses as MPEG-1 or MPEG-2
138 \return the created parser
139 */
140 GF_M4VParser *gf_m4v_parser_bs_new(GF_BitStream *bs, Bool mpeg12video);
141 /*! Deletes a MPEG-1/2/4 video parser
142 \param m4v the mpeg video parser
143 */
144 void gf_m4v_parser_del(GF_M4VParser *m4v);
145 /*! Deletes a MPEG-1/2/4 video parser without destroying associated bitstream
146 \param m4v the mpeg video parser
147 */
148 void gf_m4v_parser_del_no_bs(GF_M4VParser *m4v);
149 /*! parses the decoder specific info (if found)
150 \param m4v the mpeg video parser
151 \param dsi the decoder spcific info structure to fill
152 \return GF_OK if found, GF_EOS if not enough data, error otherwise
153 */
154 GF_Err gf_m4v_parse_config(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi);
155 /*! resets the parser
156 \param m4v the mpeg video parser
157 \param obj_type if not 0, skip next start code and use (obj_type-1) for next obj parsing
158 */
159 void gf_m4v_parser_reset(GF_M4VParser *m4v, u8 obj_type);
160 
161 /*! parses a frame. The parser ALWAYS resync on the next object in the bitstream
162 thus you can seek the bitstream to copy the payload without re-seeking it
163 \param m4v the mpeg video parser
164 \param dsi pointer to the decoder specific info parsed
165 \param frame_type set to the frame type (I:0, P:1, B:2)
166 \param time_inc set to the time increment since last frame
167 \param size set to the size of the compressed frame
168 \param start set to the position of the first byte in the buffer/bitstream
169 \param is_coded set to 1 if frame is coded, 0 if skip frame
170 \return error if any
171 */
172 GF_Err gf_m4v_parse_frame(GF_M4VParser *m4v, GF_M4VDecSpecInfo *dsi, u8 *frame_type, u32 *time_inc, u64 *size, u64 *start, Bool *is_coded);
173 /*! returns current object start in bitstream
174 \param m4v the mpeg video parser
175 \return the the position in the buffer/bitstream
176 */
177 u64 gf_m4v_get_object_start(GF_M4VParser *m4v);
178 /*! decodes DSI/VOSHeader for MPEG4
179 \param rawdsi encoded MPEG-4 decoder config
180 \param rawdsi_size size of encoded MPEG-4 decoder config
181 \param dsi the decoder spcific info structure to fill
182 \return error if any
183 */
184 GF_Err gf_m4v_get_config(u8 *rawdsi, u32 rawdsi_size, GF_M4VDecSpecInfo *dsi);
185 /*! decodes DSI/VOSHeader for MPEG12
186 \param rawdsi encoded MPEG-1/2 decoder config
187 \param rawdsi_size size of encoded MPEG-1/2 decoder config
188 \param dsi the decoder spcific info structure to fill
189 \return error if any
190 */
191 GF_Err gf_mpegv12_get_config(u8 *rawdsi, u32 rawdsi_size, GF_M4VDecSpecInfo *dsi);
192 
193 /*! rewrites Profile and Level indicator in MPEG-4 DSI
194 \param io_dsi encoded MPEG-4 decoder config
195 \param io_dsi_len size of encoded MPEG-4 decoder config
196 \param PL the new Profile/level to set
197 */
198 void gf_m4v_rewrite_pl(u8 **io_dsi, u32 *io_dsi_len, u8 PL);
199 /*! rewrites PAR code in DSI. Negative values will remove the par
200 \param io_dsi encoded MPEG-4 decoder config
201 \param io_dsi_len size of encoded MPEG-4 decoder config
202 \param par_n the numerator of the new aspect ratio to write
203 \param par_d the denominator of the new aspect ratio to write
204 \return error if any
205 */
206 GF_Err gf_m4v_rewrite_par(u8 **io_dsi, u32 *io_dsi_len, s32 par_n, s32 par_d);
207 
208 #endif /*GPAC_DISABLE_AV_PARSERS*/
209 
210 /*! returns readable description of profile
211 \param video_pl the Profile/level
212 \return name of the profile (never NULL)
213 */
214 const char *gf_m4v_get_profile_name(u8 video_pl);
215 
216 #ifndef GPAC_DISABLE_AV_PARSERS
217 /*! returns next start code in mpeg 1/2 video buffer
218 \param pbuffer the video buffer
219 \param buflen size of the video buffer
220 \param optr set to the byte offset in the buffer
221 \param scode set to the start code value if found
222 \return 0 if found, -1 otherwise
223 */
224 s32 gf_mv12_next_start_code(u8 *pbuffer, u32 buflen, u32 *optr, u32 *scode);
225 /*! returns next slice start in mpeg 1/2 video buffer
226 \param pbuffer the video buffer
227 \param startoffset the offset in the buffer at which analysis shall begin
228 \param buflen size of the video buffer
229 \param slice_offset set to the byte offset of the slice start
230 \return 0 if found, -1 otherwise
231 */
232 s32 gf_mv12_next_slice_start(u8 *pbuffer, u32 startoffset, u32 buflen, u32 *slice_offset);
233 
234 #endif /* GPAC_DISABLE_AV_PARSERS*/
235 
236 #ifndef GPAC_DISABLE_AV_PARSERS
237 
238 /*MP3 tools*/
239 /*! gets the number of channels in an MPEG-1/2/3 audio frame
240 \param hdr the frame header
241 \return the number of channels
242 */
243 u8 gf_mp3_num_channels(u32 hdr);
244 /*! gets the sampling rate of an MPEG-1/2/3 audio frame
245 \param hdr the frame header
246 \return the sampling rate
247 */
248 u16 gf_mp3_sampling_rate(u32 hdr);
249 /*! gets the window size (number of samples) in an MPEG-1/2/3 audio frame
250 \param hdr the frame header
251 \return the window size
252 */
253 u16 gf_mp3_window_size(u32 hdr);
254 /*! gets the bitrate of an MPEG-1/2/3 audio frame
255 \param hdr the frame header
256 \return the bit rate
257 */
258 u32 gf_mp3_bit_rate(u32 hdr);
259 /*! gets the MPEG-4 object type indication of an MPEG-1/2/3 audio frame
260 \param hdr the frame header
261 \return the object type indication
262 */
263 u8 gf_mp3_object_type_indication(u32 hdr);
264 /*! gets the layer of an MPEG-1/2/3 audio frame
265 \param hdr the frame header
266 \return the layer
267 */
268 u8 gf_mp3_layer(u32 hdr);
269 /*! gets the frame size of an MPEG-1/2/3 audio frame
270 \param hdr the frame header
271 \return the size in bytes (includes the 4 bytes header size)
272 */
273 u16 gf_mp3_frame_size(u32 hdr);
274 /*! locates the next frame start in an MPEG-1/2/3 audio stream file
275 \param fin file to search
276 \return the frame header value, or 0 if not found
277 */
278 u32 gf_mp3_get_next_header(FILE* fin);
279 /*! locates the next frame start in an MPEG-1/2/3 audio buffer
280 \param buffer buffer to search
281 \param size size of buffer to search
282 \param pos set to the start position of the frame header in the buffer
283 \return the frame header value, or 0 if not found
284 */
285 u32 gf_mp3_get_next_header_mem(const u8 *buffer, u32 size, u32 *pos);
286 
287 #endif /*GPAC_DISABLE_AV_PARSERS*/
288 
289 /*! gets the version size of an MPEG-1/2/3 audio frame
290 \param hdr the frame header
291 \return the version
292 */
293 u8 gf_mp3_version(u32 hdr);
294 /*! gets the version name of an MPEG-1/2/3 audio frame
295 \param hdr the frame header
296 \return the version name
297 */
298 const char *gf_mp3_version_name(u32 hdr);
299 
300 
301 #if !defined(GPAC_DISABLE_AV_PARSERS) && !defined (GPAC_DISABLE_OGG)
302 
303 /*! OGG audio codec descriptor*/
304 typedef struct ogg_audio_codec_desc_t
305 {
306 	/*! name of the codec*/
307 	const char* codec_name;
308 	/*! private*/
309 	void *parserPrivateState;
310 	/*! number of channels*/
311 	int channels;
312 	/*! samplerate*/
313 	int sample_rate;
314 
315 	/*! process the data and returns the sample block_size (i.e. duration)*/
316 	GF_Err (*process)(struct ogg_audio_codec_desc_t *parserState, u8 *data, u32 data_length, void *importer, Bool *destroy_esd, u32 *track, u32 *di, u64 *duration, int *block_size);
317 	/*! release private state*/
318 	void (*release)(struct ogg_audio_codec_desc_t *parserState);
319 } ogg_audio_codec_desc;
320 
321 /*! OGG Vorbis parser*/
322 typedef struct
323 {
324 	u32 version, num_headers;
325 	u32 min_block, max_block;
326 	u32 max_r, avg_r, low_r;
327 
328 	/*do not touch, parser private*/
329 	u32 modebits;
330 	Bool mode_flag[64];
331 	u32 nb_init;
332 	GF_BitStream *vbs;
333 
334 	u32 sample_rate, channels;
335 } GF_VorbisParser;
336 
337 /*! parses vorbis header packets
338  	initializes the parser on success, leave it to NULL otherwise
339 \param vp pointer to a vorbis parser to use
340 \param data source buffer
341 \param data_len size of buffer
342 \return GF_TRUE if success, GF_FALSE otherwise
343 */
344 Bool gf_vorbis_parse_header(GF_VorbisParser *vp, u8 *data, u32 data_len);
345 
346 /*! checks vorbis frame
347 \param vp the vorbis parser to use
348 \param data source buffer
349 \param data_len size of buffer
350 \return 0 if init error or not a vorbis frame, otherwise returns the number of audio samples in this frame
351 */
352 u32 gf_vorbis_check_frame(GF_VorbisParser *vp, u8 *data, u32 data_len);
353 
354 /*! OPUS parser*/
355 typedef struct
356 {
357 	u32 version;
358 
359 	u32 sample_rate, channels;
360 	u8 OutputChannelCount;
361 	u16 PreSkip;
362 	u32 InputSampleRate;
363 	u16 OutputGain;
364 
365 	u8 ChannelMappingFamily, StreamCount, CoupledCount;
366 	u8 ChannelMapping[255];
367 } GF_OpusParser;
368 
369 /*! parses opus header packets - initializes the parser on success, leave it to NULL otherwise
370 \param op pointer to a vorbis parser to use
371 \param data opus header buffer to parse
372 \param data_len size of opus header buffer
373 \return 1 if success, 0 if error
374 */
375 Bool gf_opus_parse_header(GF_OpusParser *op, u8 *data, u32 data_len);
376 
377 /*! checks if an opus frame is valid
378 \param op the vorbis parser to use
379 \param data source buffer
380 \param data_len size of buffer
381 \return 0 if init error or not a vorbis frame, otherwise returns the number of audio samples in this frame*/
382 u32 gf_opus_check_frame(GF_OpusParser *op, u8 *data, u32 data_len);
383 
384 #endif /*!defined(GPAC_DISABLE_AV_PARSERS) && !defined (GPAC_DISABLE_OGG)*/
385 
386 /*! reads a 32 bit sync safe integer of id3v2 from a bitstream object
387 \param bs the bitstream object to use - has to be positioned on the start if an id3v2 size field
388 \return the id3v2 size field read
389 */
390 u32 gf_id3_read_size(GF_BitStream *bs);
391 
392 /*! MPEG-4 audio object types*/
393 enum
394 {
395 	/*! AAC main*/
396 	GF_M4A_AAC_MAIN = 1,
397 	/*! AAC LC*/
398 	GF_M4A_AAC_LC = 2,
399 	/*! AAC SSR*/
400 	GF_M4A_AAC_SSR = 3,
401 	/*! AAC LTP*/
402 	GF_M4A_AAC_LTP = 4,
403 	/*! AAC SBR*/
404 	GF_M4A_AAC_SBR = 5,
405 	/*! AAC scalable*/
406 	GF_M4A_AAC_SCALABLE = 6,
407 	/*! TwinVQ*/
408 	GF_M4A_TWINVQ = 7,
409 	/*! CELP*/
410 	GF_M4A_CELP = 8,
411 	/*! HVXC*/
412 	GF_M4A_HVXC = 9,
413 	/*! TTSI*/
414 	GF_M4A_TTSI = 12,
415 	/*! Main Synthetic*/
416 	GF_M4A_MAIN_SYNTHETIC = 13,
417 	/*! Wavetable synthesis*/
418 	GF_M4A_WAVETABLE_SYNTHESIS = 14,
419 	/*! General Midi*/
420 	GF_M4A_GENERAL_MIDI = 15,
421 	/*! AudioFX synthesis*/
422 	GF_M4A_ALGO_SYNTH_AUDIO_FX = 16,
423 	/*! Error Resilient AAC LC*/
424 	GF_M4A_ER_AAC_LC = 17,
425 	/*! Error Resilient AAC LTP*/
426 	GF_M4A_ER_AAC_LTP = 19,
427 	/*! Error Resilient AAC Scalable*/
428 	GF_M4A_ER_AAC_SCALABLE = 20,
429 	/*! Error Resilient TwinVQ*/
430 	GF_M4A_ER_TWINVQ = 21,
431 	/*! Error Resilient BSAC*/
432 	GF_M4A_ER_BSAC = 22,
433 	/*! Error Resilient AAC LD*/
434 	GF_M4A_ER_AAC_LD = 23,
435 	/*! Error Resilient CELP*/
436 	GF_M4A_ER_CELP = 24,
437 	/*! Error Resilient HVXC*/
438 	GF_M4A_ER_HVXC = 25,
439 	/*! Error Resilient HILN*/
440 	GF_M4A_ER_HILN = 26,
441 	/*! Error Resilient Parametric*/
442 	GF_M4A_ER_PARAMETRIC = 27,
443 	/*! SSC*/
444 	GF_M4A_SSC = 28,
445 	/*! AAC PS*/
446 	GF_M4A_AAC_PS = 29,
447 	/*! MPEG-1/2 layer 1*/
448 	GF_M4A_LAYER1 = 32,
449 	/*! MPEG-1/2 layer 2*/
450 	GF_M4A_LAYER2 = 33,
451 	/*! MPEG-1/2 layer 3*/
452 	GF_M4A_LAYER3 = 34,
453 	/*! DST*/
454 	GF_M4A_DST = 35,
455 	/*! ALS*/
456 	GF_M4A_ALS = 36
457 };
458 
459 /*! AAC sample rates*/
460 static const u32 GF_M4ASampleRates[] =
461 {
462 	96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
463 	16000, 12000, 11025, 8000, 7350, 0, 0, 0
464 };
465 
466 /*! AAC channel configurations*/
467 static const u32 GF_M4ANumChannels[] =
468 {
469 	1, 2, 3, 4, 5, 6, 8, 2, 3, 4, 7, 8, 24, 8, 12, 10, 12, 14
470 };
471 
472 #ifndef GPAC_DISABLE_AV_PARSERS
473 
474 /*! returns channel config value (as written in AAC DSI) for the given number of channels
475 \param nb_chan the number of channels
476 \return the MPEG-4 AAC channel config value
477 */
478 u32 gf_m4a_get_channel_cfg(u32 nb_chan);
479 
480 /*! MPEG-4 Audio decoder specific info*/
481 typedef struct
482 {
483 	/*Number of channels*/
484 	u32 nb_chan;
485 	/*base audio object type*/
486 	u32 base_object_type;
487 	/*base sample rate*/
488 	u32 base_sr;
489 	/*index of base sample rate*/
490 	u32 base_sr_index;
491 	/*set if SBR (Spectral Band Replication) is present*/
492 	Bool has_sbr;
493 	/*sbr audio object type*/
494 	u32 sbr_object_type;
495 	/*SBR sample rate*/
496 	u32 sbr_sr;
497 	/*SBR sample index*/
498 	u32 sbr_sr_index;
499 	/*set if PS (Parametric Stereo) is present*/
500 	Bool has_ps;
501 	/*audio Profile level indication*/
502 	u8 audioPL;
503 
504 	/*set if program config element is present - members until end of struct are ignored/invalid if this is not set*/
505 	Bool program_config_element_present;
506 	/*set if mono mixdown is present*/
507 	Bool mono_mixdown_present;
508 	/*set if stereo mixdown is present*/
509 	Bool stereo_mixdown_present;
510 	/*set if matrix mixdown is present*/
511 	Bool matrix_mixdown_idx_present;
512 	/*set if pseudo surround is present*/
513 	Bool pseudo_surround_enable;
514 	/*element instance*/
515 	u8 element_instance_tag;
516 	/*object type*/
517 	u8 object_type;
518 	/*samplerate index*/
519 	u8 sampling_frequency_index;
520 	/*number of front channels*/
521 	u8 num_front_channel_elements;
522 	/*number of side channels*/
523 	u8 num_side_channel_elements;
524 	/*number of back channels*/
525 	u8 num_back_channel_elements;
526 	/*number of LFE channels*/
527 	u8 num_lfe_channel_elements;
528 	/*number of associated data channels*/
529 	u8 num_assoc_data_elements;
530 	/*number of valid CC elements*/
531 	u8 num_valid_cc_elements;
532 	/*id of the mono mixdown element*/
533 	u8 mono_mixdown_element_number;
534 	/*id of the stereo mixdown element*/
535 	u8 stereo_mixdown_element_number;
536 	/*id of the matrix mixdown element*/
537 	u8 matrix_mixdown_idx;
538 	/*front channels cpe flags*/
539 	u8 front_element_is_cpe[15];
540 	/*front channels select flags*/
541 	u8 front_element_tag_select[15];
542 	/*side channels cpe flags*/
543 	u8 side_element_is_cpe[15];
544 	/*side channels select flags*/
545 	u8 side_element_tag_select[15];
546 	/*back channels cpe flags*/
547 	u8 back_element_is_cpe[15];
548 	/*back channels select flags*/
549 	u8 back_element_tag_select[15];
550 	/*LFE channels select flags*/
551 	u8 lfe_element_tag_select[15];
552 	/*associated data select flags*/
553 	u8 assoc_data_element_tag_select[15];
554 	/*CC elements indep flags*/
555 	u8 cc_element_is_ind_sw[15];
556 	/*CC elements select flags*/
557 	u8 valid_cc_element_tag_select[15];
558 	/*size of comment field*/
559 	u8 comment_field_bytes;
560 	/*comment field*/
561 	u8 comments[255];
562 } GF_M4ADecSpecInfo;
563 
564 /*! parses MPEG-4 audio dsi
565 \param dsi the buffer containing the decoder config
566 \param dsi_size size of the buffer containing the decoder config
567 \param cfg will be filled with the parsed value
568 \return error code if any
569 */
570 GF_Err gf_m4a_get_config(u8 *dsi, u32 dsi_size, GF_M4ADecSpecInfo *cfg);
571 /*! gets audio profile and level for a given configuration
572 \param cfg the parsed MPEG-4 audio configuration
573 \return audio PL
574 */
575 u32 gf_m4a_get_profile(GF_M4ADecSpecInfo *cfg);
576 
577 /*! writes MPEG-4 audio dsi in a byte buffer
578 \param cfg the configuration to write
579 \param dsi set to the encoded buffer (to be freed by caller)
580 \param dsi_size set to the size of the encoded buffer
581 \return error code if any
582 */
583 GF_Err gf_m4a_write_config(GF_M4ADecSpecInfo *cfg, u8 **dsi, u32 *dsi_size);
584 /*! writes MPEG-4 audio dsi in a bitstream object
585 \param bs the bitstream object to write to
586 \param cfg the configuration to write
587 \return error code if any
588 */
589 GF_Err gf_m4a_write_config_bs(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg);
590 /*! parses MPEG-4 audio dsi from bitstream
591 \param bs the bitstream object to use (shall start in the begining of the dsi)
592 \param cfg will be filled with the parsed value
593 \param size_known set to GF_TRUE if the bitstream contains the complete DSI (and only it)
594 \return error code if any
595 */
596 GF_Err gf_m4a_parse_config(GF_BitStream *bs, GF_M4ADecSpecInfo *cfg, Bool size_known);
597 
598 #endif /*GPAC_DISABLE_AV_PARSERS*/
599 
600 /*! gets the name of a given MPEG-4 audio object type
601 \param objectType the object type
602 \return the MPEG-4 audio object type name
603 */
604 const char *gf_m4a_object_type_name(u32 objectType);
605 /*! gets the name of the the given MPEG-4 audio profile
606 \param audio_pl the profile/level
607 \return the MPEG-4 audio profile name
608 */
609 const char *gf_m4a_get_profile_name(u8 audio_pl);
610 
611 #ifndef GPAC_DISABLE_AV_PARSERS
612 
613 /*! AC-3 header*/
614 typedef struct
615 {
616 	u8 fscod, bsid, bsmod, acmod, lfon, brcode;
617 	u8 asvc, num_dep_sub;
618 	u16 chan_loc;
619 } GF_AC3StreamInfo;
620 
621 /*! AC-3 header*/
622 typedef struct
623 {
624 	u32 bitrate;
625 	u32 sample_rate;
626 	u32 framesize;
627 	u32 channels;
628 	u16 substreams; //bit-mask, used for channel map > 5.1
629 	/*only set if full parse*/
630 	GF_AC3StreamInfo streams[8]; //0->7 sibstream ids
631 	u8 nb_streams; //main and substream, only independent ones
632 	//data rate in kbps
633 	u32 data_rate;
634 } GF_AC3Header;
635 
636 /*! parses an AC-3 header from a buffer
637 \param buffer buffer to parse
638 \param buffer_size size of buffer to parse
639 \param pos set to start offset (in bytes) of the AC3 header parsed
640 \param out_hdr will be filled by parser
641 \param full_parse if GF_TRUE, complete parsing of the header will be done
642 \return GF_TRUE if success
643 */
644 Bool gf_ac3_parser(u8 *buffer, u32 buffer_size, u32 *pos, GF_AC3Header *out_hdr, Bool full_parse);
645 /*! parses an AC-3 header from a bitstream
646 \param bs bitstream to parse
647 \param hdr will be filled by parser
648 \param full_parse if GF_TRUE, complete parsing of the header will be done
649 \return GF_TRUE if success
650 */
651 Bool gf_ac3_parser_bs(GF_BitStream *bs, GF_AC3Header *hdr, Bool full_parse);
652 /*! parses an EAC-3 header from a bitstream
653 \param bs bitstream to parse
654 \param hdr will be filled by parser
655 \param full_parse if GF_TRUE, complete parsing of the header will be done
656 \return GF_TRUE if success
657 */
658 Bool gf_eac3_parser_bs(GF_BitStream *bs, GF_AC3Header *hdr, Bool full_parse);
659 /*! gets the number of channels in an AC3 frame
660 \param acmod acmod of the associated frame header
661 \return number of channels
662 */
663 u32 gf_ac3_get_channels(u32 acmod);
664 /*! gets the bitrate of an AC3 frame
665 \param brcode brcode of the associated frame header
666 \return bitrate
667 */
668 u32 gf_ac3_get_bitrate(u32 brcode);
669 
670 /*! gets basic information from an AVC Sequence Parameter Set
671 \param sps SPS NAL buffer
672 \param sps_size size of buffer
673 \param sps_id set to the ID
674 \param width set to the width
675 \param height set to the height
676 \param par_n set to the pixel aspect ratio numerator
677 \param par_d set to the pixel aspect ratio denominator
678 \return error code if any
679 */
680 GF_Err gf_avc_get_sps_info(u8 *sps, u32 sps_size, u32 *sps_id, u32 *width, u32 *height, s32 *par_n, s32 *par_d);
681 /*! gets basic information from an AVC Picture Parameter Set
682 \param pps PPS NAL buffer
683 \param pps_size size of buffer
684 \param pps_id set to the ID of the PPS
685 \param sps_id set to the SPS ID of the PPS
686 \return error code if any
687 */
688 GF_Err gf_avc_get_pps_info(u8 *pps, u32 pps_size, u32 *pps_id, u32 *sps_id);
689 
690 /*! gets basic information from an HEVC Sequence Parameter Set
691 \param sps_data SPS NAL buffer
692 \param sps_size size of buffer
693 \param sps_id set to the ID
694 \param width set to the width
695 \param height set to the height
696 \param par_n set to the pixel aspect ratio numerator
697 \param par_d set to the pixel aspect ratio denominator
698 \return error code if any
699 */
700 GF_Err gf_hevc_get_sps_info(u8 *sps_data, u32 sps_size, u32 *sps_id, u32 *width, u32 *height, s32 *par_n, s32 *par_d);
701 
702 #endif /*GPAC_DISABLE_AV_PARSERS*/
703 
704 /*! gets chroma format name from MPEG chroma format
705 \param chroma_format the chroma format to query (1: 420, 2: 422, 3: 444)
706 \return the name of the format
707 */
708 const char * gf_avc_hevc_get_chroma_format_name(u8 chroma_format);
709 /*! gets AVC profile name from profile indication
710 \param profile_idc the PL indication
711 \return the name of the profile
712 */
713 const char *gf_avc_get_profile_name(u8 profile_idc);
714 /*! checks if an AVC profile is a range extension profile
715 \param profile_idc the PL indication
716 \return GF_TRUE if given profile is in range extensions
717 */
718 Bool gf_avc_is_rext_profile(u8 profile_idc);
719 
720 /*! gets HEVC profile name from profile indication
721 \param profile_idc the PL indication
722 \return the name of the profile
723 */
724 const char *gf_hevc_get_profile_name(u8 profile_idc);
725 
726 
727 /*! parses an image from a bitstream oject. The bitstream must contain the whole image. If a thumbnail is included in the image, the indicated resolution is the one of the main image.
728 \param bs the source bitstream
729 \param codecid set to the codec ID of the image
730 \param width set to the width of the image
731 \param height set to the height of the image
732 \param dsi set to a buffer containing the decoder config of the image if any (in whihc case this buffer shall be freed by the caller)
733 \param dsi_len set to the allocated buffer size
734 */
735 void gf_img_parse(GF_BitStream *bs, u32 *codecid, u32 *width, u32 *height, u8 **dsi, u32 *dsi_len);
736 
737 /*! decodes a JPEG image in a preallocated buffer
738 \param jpg the JPEG buffer
739 \param jpg_size size of the JPEG buffer
740 \param width set to width of the image
741 \param height set to height of the image
742 \param pixel_format set to pixel format of the image
743 \param dst buffer to hold the decoded pixels (may be NULL)
744 \param dst_size size in bytes of the buffer to hold the decoded pixels (may be 0)
745 \param dst_nb_comp number of components in destination buffer
746 \return GF_BUFFER_TOO_SMALL if destination buffer is too small or error if any
747 */
748 GF_Err gf_img_jpeg_dec(u8 *jpg, u32 jpg_size, u32 *width, u32 *height, u32 *pixel_format, u8 *dst, u32 *dst_size, u32 dst_nb_comp);
749 
750 /*! decodes a PNG image in a preallocated buffer
751 \param png the PNG buffer
752 \param png_size size of the PNG buffer
753 \param width set to width of the image
754 \param height set to height of the image
755 \param pixel_format set to pixel format of the image
756 \param dst buffer to hold the decoded pixels (may be NULL)
757 \param dst_size size in bytes of the buffer to hold the decoded pixels (may be 0)
758 \return GF_BUFFER_TOO_SMALL if destination buffer is too small or error if any
759 */
760 GF_Err gf_img_png_dec(u8 *png, u32 png_size, u32 *width, u32 *height, u32 *pixel_format, u8 *dst, u32 *dst_size);
761 /*! encodes a raw image into a PNG image
762 \param data the pixel data
763 \param width the pixel width
764 \param height the pixel height
765 \param stride the pixel horizontal stride
766 \param pixel_format pixel format of the image
767 \param dst buffer to hold the decoded pixels (may be NULL)
768 \param dst_size set to the size in bytes of the buffer to hold the decoded pixels (may be 0)
769 \return GF_BUFFER_TOO_SMALL if destination buffer is too small or error if any
770 */
771 GF_Err gf_img_png_enc(u8 *data, u32 width, u32 height, s32 stride, u32 pixel_format, u8 *dst, u32 *dst_size);
772 
773 
774 /*!\brief OBU types*/
775 typedef enum {
776 	OBU_RESERVED_0 = 0,
777 	OBU_SEQUENCE_HEADER = 1,
778 	OBU_TEMPORAL_DELIMITER = 2,
779 	OBU_FRAME_HEADER = 3,
780 	OBU_TILE_GROUP = 4,
781 	OBU_METADATA = 5,
782 	OBU_FRAME = 6,
783 	OBU_REDUNDANT_FRAME_HEADER = 7,
784 	OBU_TILE_LIST = 8,
785 	OBU_RESERVED_9 = 9,
786 	OBU_RESERVED_10 = 10,
787 	OBU_RESERVED_11 = 11,
788 	OBU_RESERVED_12 = 12,
789 	OBU_RESERVED_13 = 13,
790 	OBU_RESERVED_14 = 14,
791 	OBU_PADDING = 15,
792 } ObuType;
793 
794 /*!\brief OBU metadata types*/
795 typedef enum {
796 	OBU_METADATA_TYPE_HDR_CLL = 1,
797 	OBU_METADATA_TYPE_HDR_MDCV = 2,
798 	OBU_METADATA_TYPE_SCALABILITY = 3,
799 	OBU_METADATA_TYPE_ITUT_T35 = 4,
800 	OBU_METADATA_TYPE_TIMECODE = 5
801 } ObuMetadataType;
802 
803 /*! gets the name of a given OBU type
804 \param obu_type the OBU type
805 \return the OBU name
806 */
807 const char *gf_av1_get_obu_name(ObuType obu_type);
808 
809 /*! @} */
810 
811 #ifdef __cplusplus
812 }
813 #endif
814 
815 
816 #endif	/*_GF_PARSERS_AV_H_*/
817 
818