gavl
compression.h
1 /*****************************************************************
2  * gavl - a general purpose audio/video processing library
3  *
4  * Copyright (c) 2001 - 2012 Members of the Gmerlin project
5  * gmerlin-general@lists.sourceforge.net
6  * http://gmerlin.sourceforge.net
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  * *****************************************************************/
21 
22 #ifndef GAVL_COMPRESSION_H_INCLUDED
23 #define GAVL_COMPRESSION_H_INCLUDED
24 
25 #include <gavl/gavldefs.h>
26 #include <gavl/value.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
48 #define GAVL_COMPRESSION_HAS_P_FRAMES (1<<0)
49 
51 #define GAVL_COMPRESSION_HAS_B_FRAMES (1<<1)
52 
54 #define GAVL_COMPRESSION_HAS_FIELD_PICTURES (1<<2)
55 
57 #define GAVL_COMPRESSION_SBR (1<<3)
58 
60 #define GAVL_COMPRESSION_BIG_ENDIAN (1<<4)
61 
67 typedef enum
68  {
69 
74  /* Audio */
86 
87  /* Video */
88  GAVL_CODEC_ID_JPEG = 0x10000,
101 
102  /* Subtitle (some video codecs can handle subtitles also */
104 
105  } gavl_codec_id_t;
106 
107 #define GAVL_BITRATE_VBR -1
108 #define GAVL_BITRATE_LOSSLESS -2
109 
120 typedef struct
121  {
122  int flags;
124 
125  uint8_t * global_header;
126  uint32_t global_header_len;
127 
128  int bitrate;
130  int pre_skip;
131 
135 
137 
145 GAVL_PUBLIC
147 
152 GAVL_PUBLIC
154 
161 GAVL_PUBLIC
163 
172 GAVL_PUBLIC
174 
181 GAVL_PUBLIC
183  const gavl_compression_info_t * src);
184 
192 GAVL_PUBLIC
193 void gavl_append_xiph_header(uint8_t ** global_header,
194  uint32_t * global_header_len,
195  uint8_t * header,
196  int header_len);
197 
206 GAVL_PUBLIC
207 uint8_t * gavl_extract_xiph_header(uint8_t * global_header,
208  int global_header_len,
209  int idx,
210  int * header_len);
211 
212 
228 GAVL_PUBLIC
229 const char * gavl_compression_get_extension(gavl_codec_id_t id, int * separate);
230 
240 GAVL_PUBLIC
242 
249 
256 GAVL_PUBLIC
258 
264 GAVL_PUBLIC
266 
272 GAVL_PUBLIC const char *
274 
280 GAVL_PUBLIC const char *
282 
288 GAVL_PUBLIC gavl_codec_id_t
290 
298 GAVL_PUBLIC
300 
309 GAVL_PUBLIC
311 
312 
313 #define GAVL_PACKET_TYPE_I 0x01
314 #define GAVL_PACKET_TYPE_P 0x02
315 #define GAVL_PACKET_TYPE_B 0x03
316 #define GAVL_PACKET_TYPE_MASK 0x03
317 
318 #define GAVL_PACKET_KEYFRAME (1<<2)
319 #define GAVL_PACKET_LAST (1<<3)
320 #define GAVL_PACKET_EXT (1<<4)
321 #define GAVL_PACKET_REF (1<<5)
322 #define GAVL_PACKET_NOOUTPUT (1<<6)
323 
324 #define GAVL_PACKET_FLAG_PRIV (1<<16)
325 
326 #define GAVL_PACKET_PADDING 32
327 
341 typedef struct
342  {
343  uint8_t * data;
344  int data_len;
346 
347  uint32_t flags;
348 
349  int64_t pts;
350  int64_t duration;
351 
352  uint32_t field2_offset;
353  uint32_t header_size;
354  uint32_t sequence_end_pos;
355 
358 
360  int32_t dst_x;
361  int32_t dst_y;
362 
363  uint32_t id;
364 
365  } gavl_packet_t;
366 
372 GAVL_PUBLIC
374 
375 
384 GAVL_PUBLIC
385 void gavl_packet_alloc(gavl_packet_t * p, int len);
386 
391 GAVL_PUBLIC
393 
399 GAVL_PUBLIC
401  const gavl_packet_t * src);
402 
413 GAVL_PUBLIC
415  const gavl_packet_t * src);
416 
417 
424 GAVL_PUBLIC
426 
427 
434 GAVL_PUBLIC
436 
444 GAVL_PUBLIC
446  const char * filename);
447 
448 
449 GAVL_PUBLIC
450 void
451 gavl_packet_to_videoframe(const gavl_packet_t * p,
452  gavl_video_frame_t * f);
453 
454 
455 
456 
457 typedef struct
458  {
459  int32_t size_min;
460  int32_t size_max;
461  int64_t duration_min;
462  int64_t duration_max;
463  int64_t pts_start;
464  int64_t pts_end;
465 
466  int64_t total_bytes; // For average bitrate
467  int64_t total_packets; // For average framerate
468 
470 
471 GAVL_PUBLIC
472 void gavl_stream_stats_init(gavl_stream_stats_t*);
473 
474 GAVL_PUBLIC
475 void gavl_stream_stats_dump(const gavl_stream_stats_t*, int indent);
476 
477 GAVL_PUBLIC
478 void gavl_stream_stats_update(gavl_stream_stats_t*,const gavl_packet_t*p);
479 
480 GAVL_PUBLIC
481 void gavl_stream_stats_update_params(gavl_stream_stats_t * f,
482  int64_t pts, int64_t duration, int data_len,
483  int flags);
484 
485 
486 GAVL_PUBLIC
487 void gavl_stream_stats_apply_audio(gavl_stream_stats_t * f,
488  const gavl_audio_format_t * fmt,
490  gavl_dictionary_t * m);
491 
492 GAVL_PUBLIC
493 void gavl_stream_stats_apply_video(gavl_stream_stats_t * f,
494  gavl_video_format_t * fmt,
496  gavl_dictionary_t * m);
497 
498 GAVL_PUBLIC
499 void gavl_stream_stats_apply_subtitle(gavl_stream_stats_t * f,
500  gavl_dictionary_t * m);
501 
502 GAVL_PUBLIC
503 void gavl_stream_stats_apply_generic(gavl_stream_stats_t * f,
505  gavl_dictionary_t * m);
506 
507 
508 GAVL_PUBLIC
509 int gavl_stream_get_stats(const gavl_dictionary_t * s, gavl_stream_stats_t * stats);
510 
511 GAVL_PUBLIC
512 void gavl_stream_set_stats(gavl_dictionary_t * s, const gavl_stream_stats_t * stats);
513 
514 /* PTS Cache */
515 
516 typedef struct gavl_packet_pts_cache_s gavl_packet_pts_cache_t;
517 
518 GAVL_PUBLIC
519 gavl_packet_pts_cache_t * gavl_packet_pts_cache_create(int size);
520 
521 GAVL_PUBLIC
522 void gavl_packet_pts_cache_destroy(gavl_packet_pts_cache_t *);
523 
524 GAVL_PUBLIC
525 void gavl_packet_pts_cache_push(gavl_packet_pts_cache_t *m, const gavl_packet_t * pkt);
526 
527 GAVL_PUBLIC
528 int gavl_packet_pts_cache_get_first(gavl_packet_pts_cache_t *m, gavl_packet_t * pkt);
529 
530 GAVL_PUBLIC
531 int gavl_packet_pts_cache_get_by_pts(gavl_packet_pts_cache_t *m, gavl_packet_t * pkt,
532  int64_t pts);
533 
534 GAVL_PUBLIC
535 void gavl_packet_pts_cache_clear(gavl_packet_pts_cache_t *m);
536 
537 
538 #if 0
539 GAVL_PUBLIC
540 void gavl_compression_info_to_dictionary(const gavl_compression_info_t * info, gavl_dictionary_t * dict);
541 
542 GAVL_PUBLIC
543 void gavl_compression_info_from_dictionary(gavl_compression_info_t * info, const gavl_dictionary_t * dict);
544 
545 #endif
546 
551 #ifdef __cplusplus
552 }
553 #endif
554 
555 
556 #endif // GAVL_COMPRESSION_H_INCLUDED
GAVL_PUBLIC void gavl_compression_info_dump(const gavl_compression_info_t *info)
Dump a compression info to stderr.
GAVL_PUBLIC void gavl_packet_dump(const gavl_packet_t *p)
Dump a packet to stderr.
GAVL_PUBLIC int gavl_compression_need_pixelformat(gavl_codec_id_t id)
Check if the compression supports multiple pixelformats.
GAVL_PUBLIC void gavl_compression_info_dumpi(const gavl_compression_info_t *info, int num)
Dump a compression info to stderr.
GAVL_PUBLIC uint8_t * gavl_extract_xiph_header(uint8_t *global_header, int global_header_len, int idx, int *header_len)
Extract a Xiph packet to a global header.
GAVL_PUBLIC gavl_codec_id_t gavl_get_compression(int index)
Get a compression format for a specified index.
GAVL_PUBLIC void gavl_packet_copy_metadata(gavl_packet_t *dst, const gavl_packet_t *src)
Copy metadata of a packet.
GAVL_PUBLIC void gavl_packet_reset(gavl_packet_t *p)
Reset a packet.
GAVL_PUBLIC void gavl_append_xiph_header(uint8_t **global_header, uint32_t *global_header_len, uint8_t *header, int header_len)
Append a Xiph packet to a global header.
gavl_codec_id_t
Codec ID.
Definition: compression.h:68
GAVL_PUBLIC void gavl_compression_info_init(gavl_compression_info_t *info)
Initialize a compression info.
GAVL_PUBLIC int gavl_compression_get_sample_size(gavl_codec_id_t id)
Check if an audio compression size for each samples.
GAVL_PUBLIC void gavl_packet_free(gavl_packet_t *p)
Free memory of a packet.
GAVL_PUBLIC int gavl_num_compressions()
Get the number of compression formats.
GAVL_PUBLIC void gavl_packet_alloc(gavl_packet_t *p, int len)
Allocate memory for a packet.
GAVL_PUBLIC void gavl_packet_copy(gavl_packet_t *dst, const gavl_packet_t *src)
Copy a packet.
GAVL_PUBLIC gavl_codec_id_t gavl_compression_from_short_name(const char *name)
Get a compression from the short name.
GAVL_PUBLIC const char * gavl_compression_get_extension(gavl_codec_id_t id, int *separate)
Get the file extension of the corresponding raw format.
GAVL_PUBLIC void gavl_compression_info_copy(gavl_compression_info_t *dst, const gavl_compression_info_t *src)
Copy a compression info.
const char * gavl_compression_get_mimetype(const gavl_compression_info_t *ci)
Get the mimetype for the corresponding elementary format.
GAVL_PUBLIC void gavl_packet_save(const gavl_packet_t *p, const char *filename)
Save a packet to a file.
GAVL_PUBLIC int gavl_compression_constant_frame_samples(gavl_codec_id_t id)
Check if an audio compression constant frame samples.
GAVL_PUBLIC void gavl_packet_init(gavl_packet_t *p)
Initialize a packet.
GAVL_PUBLIC void gavl_compression_info_free(gavl_compression_info_t *info)
Free all dynamically allocated memory of a compression info.
GAVL_PUBLIC const char * gavl_compression_get_long_name(gavl_codec_id_t id)
Return the long name of the compression.
GAVL_PUBLIC const char * gavl_compression_get_short_name(gavl_codec_id_t id)
Return the short name of the compression.
@ GAVL_CODEC_ID_VORBIS
Vorbis (segmented extradata and packets)
Definition: compression.h:81
@ GAVL_CODEC_ID_SPEEX
Speex.
Definition: compression.h:84
@ GAVL_CODEC_ID_DTS
DTS.
Definition: compression.h:85
@ GAVL_CODEC_ID_MPEG1
MPEG-1 video.
Definition: compression.h:92
@ GAVL_CODEC_ID_DV
DV (several variants)
Definition: compression.h:98
@ GAVL_CODEC_ID_AC3
AC3.
Definition: compression.h:79
@ GAVL_CODEC_ID_DIRAC
Complete DIRAC frames, sequence end code appended to last packet.
Definition: compression.h:97
@ GAVL_CODEC_ID_TGA
TGA image.
Definition: compression.h:91
@ GAVL_CODEC_ID_MPEG2
MPEG-2 video.
Definition: compression.h:93
@ GAVL_CODEC_ID_AAC
AAC as stored in quicktime/mp4.
Definition: compression.h:80
@ GAVL_CODEC_ID_VP8
VP8 (as used in webm)
Definition: compression.h:99
@ GAVL_CODEC_ID_MPEG4_ASP
MPEG-4 ASP (a.k.a. Divx4)
Definition: compression.h:94
@ GAVL_CODEC_ID_JPEG
JPEG image.
Definition: compression.h:88
@ GAVL_CODEC_ID_H264
H.264 (Annex B)
Definition: compression.h:95
@ GAVL_CODEC_ID_MP2
MPEG-1 audio layer II.
Definition: compression.h:77
@ GAVL_CODEC_ID_TIFF
TIFF image.
Definition: compression.h:90
@ GAVL_CODEC_ID_FLAC
Flac (extradata contain a file header without comment and seektable)
Definition: compression.h:82
@ GAVL_CODEC_ID_THEORA
Theora (segmented extradata)
Definition: compression.h:96
@ GAVL_CODEC_ID_DIV3
Old style Divx (aka MSMPEG4V3)
Definition: compression.h:100
@ GAVL_CODEC_ID_MP3
MPEG-1/2 audio layer 3 CBR/VBR.
Definition: compression.h:78
@ GAVL_CODEC_ID_DVDSUB
DVD subtitles, palette is in header.
Definition: compression.h:103
@ GAVL_CODEC_ID_PNG
PNG image.
Definition: compression.h:89
@ GAVL_CODEC_ID_ALAW
alaw 2:1
Definition: compression.h:75
@ GAVL_CODEC_ID_NONE
Unknown/unsupported compression format. In gavf files this signals that the stream consists of uncomp...
Definition: compression.h:73
@ GAVL_CODEC_ID_OPUS
Opus.
Definition: compression.h:83
@ GAVL_CODEC_ID_ULAW
mu-law 2:1
Definition: compression.h:76
uint64_t gavl_timecode_t
Typedef for timecodes.
Definition: timecode.h:43
gavl_interlace_mode_t
Interlace mode.
Definition: gavl.h:2323
Audio Format.
Definition: gavl.h:321
Compression format.
Definition: compression.h:121
uint32_t global_header_len
Length of global header.
Definition: compression.h:126
gavl_codec_id_t id
Codec ID.
Definition: compression.h:123
uint8_t * global_header
Global header.
Definition: compression.h:125
int max_packet_size
Maximum packet size or 0 if unknown.
Definition: compression.h:133
int max_ref_frames
Maximum reference frames (if > 2)
Definition: compression.h:134
int flags
ORed combination of GAVL_COMPRESSION_* flags.
Definition: compression.h:122
int video_buffer_size
VBV buffer size for video (in BYTES)
Definition: compression.h:132
int bitrate
Needed by some codecs, negative values mean VBR.
Definition: compression.h:128
int pre_skip
Samples to skip at the start.
Definition: compression.h:130
int palette_size
Size of the embedded palette for image codecs.
Definition: compression.h:129
Packet structure.
Definition: compression.h:342
int data_len
Length of data.
Definition: compression.h:344
uint32_t id
ID of the gavf stream where this packet belongs.
Definition: compression.h:363
gavl_rectangle_i_t src_rect
Rectangle to take from a video frame.
Definition: compression.h:359
gavl_timecode_t timecode
Timecode.
Definition: compression.h:357
uint8_t * data
Data.
Definition: compression.h:343
int64_t pts
Presentation time.
Definition: compression.h:349
uint32_t field2_offset
Offset of field 2 for field pictures.
Definition: compression.h:352
gavl_interlace_mode_t interlace_mode
Interlace mode for mixed interlacing.
Definition: compression.h:356
uint32_t sequence_end_pos
Position of sequence end code if any.
Definition: compression.h:354
uint32_t header_size
Size of a repeated global header (or 0)
Definition: compression.h:353
int32_t dst_y
Y-coordinate in the destination frame (for overlays)
Definition: compression.h:361
int64_t duration
Duration of the contained frame.
Definition: compression.h:350
int32_t dst_x
X-coordinate in the destination frame (for overlays)
Definition: compression.h:360
uint32_t flags
ORed combination of GAVL_PACKET_* flags.
Definition: compression.h:347
int data_alloc
How many bytes got allocated.
Definition: compression.h:345
Integer rectangle.
Definition: gavl.h:1460
Definition: compression.h:458
Video format.
Definition: gavl.h:2380
Definition: gavl.h:2584