gavl
compression.h
1/*****************************************************************
2 * gavl - a general purpose audio/video processing library
3 *
4 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
5 * http://github.com/bplaum
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * *****************************************************************/
20
21
22
23#ifndef GAVL_COMPRESSION_H_INCLUDED
24#define GAVL_COMPRESSION_H_INCLUDED
25
26#include <gavl/gavldefs.h>
27#include <gavl/value.h>
28
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34typedef struct gavl_io_s gavl_io_t;
35
36
37
50
51
53
54#define GAVL_COMPRESSION_HAS_P_FRAMES (1<<0)
55
57#define GAVL_COMPRESSION_HAS_B_FRAMES (1<<1)
58
60#define GAVL_COMPRESSION_HAS_FIELD_PICTURES (1<<2)
61
63#define GAVL_COMPRESSION_SBR (1<<3)
64
66#define GAVL_COMPRESSION_BIG_ENDIAN (1<<4)
67
68#define GAVL_MK_FOURCC(a, b, c, d) ((a<<24)|(b<<16)|(c<<8)|d)
69
70typedef struct gavl_compression_info_s gavl_compression_info_t;
71
72
77
119
120#define GAVL_BITRATE_VBR -1
121#define GAVL_BITRATE_LOSSLESS -2
122
132
134 {
135 int flags;
137
138 gavl_buffer_t codec_header;
139
143
145 uint32_t codec_tag;
146 int block_align; // Needed by MS audio codecs
147 };
148
155
156GAVL_PUBLIC
157void gavl_compression_info_init(gavl_compression_info_t * info);
158
162
163GAVL_PUBLIC
164void gavl_compression_info_free(gavl_compression_info_t* info);
165
171
172GAVL_PUBLIC
173void gavl_compression_info_dump(const gavl_compression_info_t * info);
174
182
183GAVL_PUBLIC
184void gavl_compression_info_dumpi(const gavl_compression_info_t * info, int num);
185
191
192GAVL_PUBLIC
193void gavl_compression_info_copy(gavl_compression_info_t * dst,
194 const gavl_compression_info_t * src);
195
196
197GAVL_PUBLIC
198void gavl_compression_info_set_global_header(gavl_compression_info_t * dst,
199 const uint8_t * data, int len);
200
201
202GAVL_PUBLIC
203void gavl_compression_info_append_global_header(gavl_compression_info_t * dst,
204 const uint8_t * data, int len);
205
206
213
214GAVL_PUBLIC
215void gavl_append_xiph_header(gavl_buffer_t * codec_header,
216 uint8_t * header,
217 int header_len);
218
226
227GAVL_PUBLIC
228uint8_t * gavl_extract_xiph_header(gavl_buffer_t * codec_header,
229 int idx,
230 int * header_len);
231
232
247
248GAVL_PUBLIC
249const char * gavl_compression_get_extension(gavl_codec_id_t id, int * separate);
250
259
260GAVL_PUBLIC
262
267
268const char * gavl_compression_get_mimetype(const gavl_compression_info_t * ci);
269
275
276GAVL_PUBLIC
278
283
284GAVL_PUBLIC
286
287
288
293
294GAVL_PUBLIC const char *
296
301
302GAVL_PUBLIC const char *
304
309
310GAVL_PUBLIC gavl_codec_id_t
312
319
320GAVL_PUBLIC
322
330
331GAVL_PUBLIC
333
334#define GAVL_PACKET_TYPE_I 0x01
335#define GAVL_PACKET_TYPE_P 0x02
336#define GAVL_PACKET_TYPE_B 0x03
337#define GAVL_PACKET_TYPE_MASK 0x03
338
339#define GAVL_PACKET_KEYFRAME (1<<2)
340#define GAVL_PACKET_LAST (1<<3)
341#define GAVL_PACKET_EXT (1<<4)
342#define GAVL_PACKET_REF (1<<5)
343#define GAVL_PACKET_NOOUTPUT (1<<6)
344
345#define GAVL_PACKET_HAS_FDS (1<<7)
346
347#define GAVL_PACKET_FIELD_PIC (1<<8)
348#define GAVL_PACKET_SKIP (1<<9)
349
350
351#define GAVL_PACKET_FLAG_PRIV (1<<16)
352#define GAVL_PACKET_FLAG_PUBLIC_MASK 0x0000ffff
353#define GAVL_PACKET_FLAG_PRIVATE_MASK 0xffff0000
354
355#define GAVL_PACKET_PADDING 32
356
357GAVL_PUBLIC
358const char * gavl_coding_type_to_string(int flags);
359
360/* Extra data which can carried within a packet */
361
362typedef enum
363 {
364 GAVL_PACKET_EXTRADATA_NONE = 0,
365 GAVL_PACKET_EXTRADATA_PALETTE,
366 } gavl_packet_extradata_type_t;
367
368#define GAVL_PACKET_MAX_EXTRADATA 1
369
370
371typedef struct
372 {
373 uint16_t r;
374 uint16_t g;
375 uint16_t b;
376 uint16_t a;
377 }
379
380typedef struct
381 {
382 gavl_palette_entry_t * entries;
383 int num_entries;
385
386GAVL_PUBLIC gavl_palette_t *
387gavl_palette_create(void);
388
389GAVL_PUBLIC void
390gavl_palette_alloc(gavl_palette_t * pal, int num_colors);
391
392GAVL_PUBLIC void
393gavl_palette_init(gavl_palette_t * pal);
394
395GAVL_PUBLIC void
396gavl_palette_free(gavl_palette_t * pal);
397
398GAVL_PUBLIC void
399gavl_palette_destroy(gavl_palette_t * pal);
400
401GAVL_PUBLIC void
402gavl_palette_move(gavl_palette_t * dst, gavl_palette_t * src);
403
404typedef struct
405 {
406 gavl_packet_extradata_type_t type;
407 void * data;
409
422
423
424typedef struct gavl_packet_s
425 {
426 gavl_buffer_t buf;
427
428 uint32_t flags;
429
430 int64_t position;
431
432 int64_t pts;
433 int64_t dts;
434 int64_t pes_pts;
435
436 int64_t duration;
437
438 uint32_t field2_offset;
439 uint32_t header_size;
441
444
446 int32_t dst_x;
447 int32_t dst_y;
448
449 int32_t id;
450 int buf_idx;
451
452 gavl_packet_extradata_t ext_data[GAVL_PACKET_MAX_EXTRADATA];
453
454 gavl_hw_context_t * hwctx;
455 void * storage;
456
457
458
459 } gavl_packet_t;
460
465
466GAVL_PUBLIC
467void gavl_packet_init(gavl_packet_t * p);
468
469GAVL_PUBLIC
470gavl_packet_t * gavl_packet_create(void);
471
472GAVL_PUBLIC
473void gavl_packet_destroy(gavl_packet_t * p);
474
475
483
484GAVL_PUBLIC
485void gavl_packet_alloc(gavl_packet_t * p, int len);
486
490
491GAVL_PUBLIC
492void gavl_packet_free(gavl_packet_t * p);
493
494GAVL_PUBLIC
495void * gavl_packet_add_extradata(gavl_packet_t * p, gavl_packet_extradata_type_t type);
496
497GAVL_PUBLIC
498void * gavl_packet_get_extradata(gavl_packet_t * p, gavl_packet_extradata_type_t type);
499
500/* Merge two consecutive fields into one */
501GAVL_PUBLIC
502void gavl_packet_merge_field2(gavl_packet_t * p, const gavl_packet_t * field2);
503
504
509
510GAVL_PUBLIC
511void gavl_packet_copy(gavl_packet_t * dst,
512 const gavl_packet_t * src);
513
523
524GAVL_PUBLIC
525void gavl_packet_copy_metadata(gavl_packet_t * dst,
526 const gavl_packet_t * src);
527
533
534GAVL_PUBLIC
535void gavl_packet_reset(gavl_packet_t * p);
536
537
543
544GAVL_PUBLIC
545void gavl_packet_dump(const gavl_packet_t * p);
546
553
554GAVL_PUBLIC
555void gavl_packet_save(const gavl_packet_t * p,
556 const char * filename);
557
558
559
560typedef struct
561 {
562 int32_t size_min;
563 int32_t size_max;
564 int64_t duration_min;
565 int64_t duration_max;
566 int64_t pts_start;
567 int64_t pts_end;
568
569 int64_t total_bytes; // For average bitrate
570 int64_t total_packets; // For average framerate
571
572 /* delay = pts - dts */
573
574 int64_t delay_min;
575 int64_t delay_max;
576
578
579GAVL_PUBLIC
580void gavl_stream_stats_init(gavl_stream_stats_t*);
581
582GAVL_PUBLIC
583void gavl_stream_stats_dump(const gavl_stream_stats_t*, int indent);
584
585GAVL_PUBLIC
586void gavl_stream_stats_update(gavl_stream_stats_t*,const gavl_packet_t*p);
587
588GAVL_PUBLIC
589void gavl_stream_stats_update_end(gavl_stream_stats_t * f, const gavl_packet_t * p);
590
591GAVL_PUBLIC
592void gavl_stream_stats_update_params(gavl_stream_stats_t * f,
593 int64_t dts, int64_t pts, int64_t duration, int data_len,
594 int flags);
595
596
597GAVL_PUBLIC
598void gavl_stream_stats_apply_audio(gavl_stream_stats_t * f,
599 const gavl_audio_format_t * fmt,
600 gavl_compression_info_t * ci,
601 gavl_dictionary_t * m);
602
603GAVL_PUBLIC
604void gavl_stream_stats_apply_video(gavl_stream_stats_t * f,
606 gavl_compression_info_t * ci,
607 gavl_dictionary_t * m);
608
609GAVL_PUBLIC
610void gavl_stream_stats_apply_subtitle(gavl_stream_stats_t * f,
611 gavl_dictionary_t * m);
612
613GAVL_PUBLIC
614void gavl_stream_stats_apply_generic(gavl_stream_stats_t * f,
615 gavl_compression_info_t * ci,
616 gavl_dictionary_t * m);
617
618
619GAVL_PUBLIC
620int gavl_stream_get_stats(const gavl_dictionary_t * s, gavl_stream_stats_t * stats);
621
622GAVL_PUBLIC
623void gavl_stream_set_stats(gavl_dictionary_t * s, const gavl_stream_stats_t * stats);
624
625/* PTS Cache */
626
627typedef struct gavl_packet_pts_cache_s gavl_packet_pts_cache_t;
628
629GAVL_PUBLIC
630gavl_packet_pts_cache_t * gavl_packet_pts_cache_create(int size);
631
632GAVL_PUBLIC
633void gavl_packet_pts_cache_destroy(gavl_packet_pts_cache_t *);
634
635GAVL_PUBLIC
636void gavl_packet_pts_cache_push_packet(gavl_packet_pts_cache_t *m, const gavl_packet_t * pkt);
637
638GAVL_PUBLIC
639void gavl_packet_pts_cache_push_frame(gavl_packet_pts_cache_t *m, const gavl_video_frame_t * f);
640
641GAVL_PUBLIC
642int gavl_packet_pts_cache_get_first(gavl_packet_pts_cache_t *m, gavl_video_frame_t * f);
643
644GAVL_PUBLIC
645int gavl_packet_pts_cache_get_by_pts(gavl_packet_pts_cache_t *m, gavl_packet_t * pkt,
646 int64_t pts);
647
648GAVL_PUBLIC
649void gavl_packet_pts_cache_clear(gavl_packet_pts_cache_t *m);
650
651
652
656
657#ifdef __cplusplus
658}
659#endif
660
661
662#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 int gavl_num_compressions(void)
Get the number of compression formats.
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 const char * gavl_compression_get_extension(gavl_codec_id_t id, int *separate)
Get the file extension of the corresponding raw format.
gavl_codec_id_t
Codec ID.
Definition compression.h:79
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 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 void gavl_compression_info_copy(gavl_compression_info_t *dst, const gavl_compression_info_t *src)
Copy a compression info.
GAVL_PUBLIC void gavl_packet_save(const gavl_packet_t *p, const char *filename)
Save a packet to a file.
GAVL_PUBLIC uint8_t * gavl_extract_xiph_header(gavl_buffer_t *codec_header, int idx, int *header_len)
Extract a Xiph packet to a global header.
GAVL_PUBLIC const char * gavl_compression_get_long_name(gavl_codec_id_t id)
Return the long name of the compression.
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_append_xiph_header(gavl_buffer_t *codec_header, uint8_t *header, int header_len)
Append a Xiph packet to a global header.
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_short_name(gavl_codec_id_t id)
Return the short name of the compression.
const char * gavl_compression_get_mimetype(const gavl_compression_info_t *ci)
Get the mimetype for the corresponding elementary format.
@ GAVL_CODEC_ID_VORBIS
Vorbis (segmented extradata and packets).
Definition compression.h:92
@ GAVL_CODEC_ID_SPEEX
Speex.
Definition compression.h:95
@ GAVL_CODEC_ID_H265
H.265 (Annex B).
Definition compression.h:112
@ GAVL_CODEC_ID_DTS
DTS.
Definition compression.h:96
@ GAVL_CODEC_ID_MPEG1
MPEG-1 video.
Definition compression.h:103
@ GAVL_CODEC_ID_DV
DV (several variants).
Definition compression.h:109
@ GAVL_CODEC_ID_AC3
AC3.
Definition compression.h:90
@ GAVL_CODEC_ID_DIRAC
Complete DIRAC frames, sequence end code appended to last packet.
Definition compression.h:108
@ GAVL_CODEC_ID_TGA
TGA image.
Definition compression.h:102
@ GAVL_CODEC_ID_MPEG2
MPEG-2 video.
Definition compression.h:104
@ GAVL_CODEC_ID_AAC
AAC as stored in quicktime/mp4.
Definition compression.h:91
@ GAVL_CODEC_ID_VP8
VP8 (as used in webm).
Definition compression.h:110
@ GAVL_CODEC_ID_MPEG4_ASP
MPEG-4 ASP (a.k.a. Divx4).
Definition compression.h:105
@ GAVL_CODEC_ID_JPEG
JPEG image.
Definition compression.h:99
@ GAVL_CODEC_ID_H264
H.264 (Annex B).
Definition compression.h:106
@ GAVL_CODEC_ID_MP2
MPEG-1 audio layer II.
Definition compression.h:88
@ GAVL_CODEC_ID_TIFF
TIFF image.
Definition compression.h:101
@ GAVL_CODEC_ID_FLAC
Flac (extradata contain a file header without comment and seektable).
Definition compression.h:93
@ GAVL_CODEC_ID_THEORA
Theora (segmented extradata).
Definition compression.h:107
@ GAVL_CODEC_ID_DIV3
Old style Divx (aka MSMPEG4V3).
Definition compression.h:111
@ GAVL_CODEC_ID_MP3
MPEG-1/2 audio layer 3 CBR/VBR.
Definition compression.h:89
@ GAVL_CODEC_ID_DVDSUB
DVD subtitles, palette is in header.
Definition compression.h:115
@ GAVL_CODEC_ID_PNG
PNG image.
Definition compression.h:100
@ GAVL_CODEC_ID_ALAW
alaw 2:1
Definition compression.h:86
@ GAVL_CODEC_ID_NONE
Unknown/unsupported compression format. In gavf files this signals that the stream consists of uncomp...
Definition compression.h:84
@ GAVL_CODEC_ID_OPUS
Opus.
Definition compression.h:94
@ GAVL_CODEC_ID_EXTENDED
Separate compression id.
Definition compression.h:116
@ GAVL_CODEC_ID_ULAW
mu-law 2:1
Definition compression.h:87
uint64_t gavl_timecode_t
Typedef for timecodes.
Definition timecode.h:44
struct gavl_video_format_s gavl_video_format_t
Video format.
Definition gavl.h:102
gavl_interlace_mode_t
Interlace mode.
Definition gavl.h:2345
Audio Format.
Definition gavl.h:275
Compression format.
Definition compression.h:134
int pre_skip
Samples to skip at the start.
Definition compression.h:142
gavl_codec_id_t id
Codec ID.
Definition compression.h:136
int bitrate
Needed by some codecs, negative values mean VBR.
Definition compression.h:140
gavl_buffer_t codec_header
Global header.
Definition compression.h:138
int palette_size
Size of the embedded palette for image codecs.
Definition compression.h:141
int video_buffer_size
VBV buffer size for video (in BYTES).
Definition compression.h:144
int flags
ORed combination of GAVL_COMPRESSION_* flags.
Definition compression.h:135
Definition compression.h:405
Packet structure.
Definition compression.h:425
int64_t dts
Decoding time.
Definition compression.h:433
int64_t pes_pts
PTS from the PES stream (probably in another scale).
Definition compression.h:434
gavl_timecode_t timecode
Timecode.
Definition compression.h:443
uint32_t sequence_end_pos
Position of sequence end code if any.
Definition compression.h:440
gavl_rectangle_i_t src_rect
Rectangle to take from a video frame.
Definition compression.h:445
int64_t position
Position of the packet in the file. The exact meaning is format dependent.
Definition compression.h:430
int64_t pts
Presentation time.
Definition compression.h:432
int32_t id
ID of the gavf stream where this packet belongs.
Definition compression.h:449
int32_t dst_y
Y-coordinate in the destination frame (for overlays).
Definition compression.h:447
gavl_buffer_t buf
Data.
Definition compression.h:426
int32_t dst_x
X-coordinate in the destination frame (for overlays).
Definition compression.h:446
gavl_interlace_mode_t interlace_mode
Interlace mode for mixed interlacing.
Definition compression.h:442
uint32_t flags
ORed combination of GAVL_PACKET_* flags.
Definition compression.h:428
uint32_t header_size
Size of a repeated global header (or 0).
Definition compression.h:439
uint32_t field2_offset
Offset of field 2 for field pictures.
Definition compression.h:438
int64_t duration
Duration of the contained frame.
Definition compression.h:436
Definition compression.h:372
Definition compression.h:381
Integer rectangle.
Definition gavl.h:1453
Definition compression.h:561