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
29extern "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
62#define GAVL_MK_FOURCC(a, b, c, d) ((a<<24)|(b<<16)|(c<<8)|d)
63
64
110
111#define GAVL_BITRATE_VBR -1
112#define GAVL_BITRATE_LOSSLESS -2
113
124typedef struct
125 {
126 int flags;
128
129 gavl_buffer_t codec_header;
130
134
136 uint32_t codec_tag;
138
146GAVL_PUBLIC
148
153GAVL_PUBLIC
155
162GAVL_PUBLIC
164
173GAVL_PUBLIC
175
182GAVL_PUBLIC
184 const gavl_compression_info_t * src);
185
186
187GAVL_PUBLIC
188void gavl_compression_info_set_global_header(gavl_compression_info_t * dst,
189 const uint8_t * data, int len);
190
191
192GAVL_PUBLIC
193void gavl_compression_info_append_global_header(gavl_compression_info_t * dst,
194 const uint8_t * data, int len);
195
196
204GAVL_PUBLIC
205void gavl_append_xiph_header(gavl_buffer_t * codec_header,
206 uint8_t * header,
207 int header_len);
208
217GAVL_PUBLIC
218uint8_t * gavl_extract_xiph_header(gavl_buffer_t * codec_header,
219 int idx,
220 int * header_len);
221
222
238GAVL_PUBLIC
239const char * gavl_compression_get_extension(gavl_codec_id_t id, int * separate);
240
250GAVL_PUBLIC
252
259
266GAVL_PUBLIC
268
274GAVL_PUBLIC
276
282GAVL_PUBLIC const char *
284
290GAVL_PUBLIC const char *
292
298GAVL_PUBLIC gavl_codec_id_t
300
308GAVL_PUBLIC
310
319GAVL_PUBLIC
321
322
323#define GAVL_PACKET_TYPE_I 0x01
324#define GAVL_PACKET_TYPE_P 0x02
325#define GAVL_PACKET_TYPE_B 0x03
326#define GAVL_PACKET_TYPE_MASK 0x03
327
328#define GAVL_PACKET_KEYFRAME (1<<2)
329#define GAVL_PACKET_LAST (1<<3)
330#define GAVL_PACKET_EXT (1<<4)
331#define GAVL_PACKET_REF (1<<5)
332#define GAVL_PACKET_NOOUTPUT (1<<6)
333
334#define GAVL_PACKET_HAS_FDS (1<<7)
335
336#define GAVL_PACKET_FIELD_PIC (1<<8)
337#define GAVL_PACKET_SKIP (1<<9)
338
339
340#define GAVL_PACKET_FLAG_PRIV (1<<16)
341
342#define GAVL_PACKET_PADDING 32
343
344/* Extra data which can carried within a packet */
345
346typedef enum
347 {
348 GAVL_PACKET_EXTRADATA_NONE = 0,
349 GAVL_PACKET_EXTRADATA_PALETTE,
350 GAVL_PACKET_EXTRADATA_FDS,
351 } gavl_packet_extradata_type_t;
352
353#define GAVL_PACKET_MAX_EXTRADATA 2
354
355typedef struct
356 {
357 int fds[GAVL_MAX_PLANES];
358 int num_fds;
360
361typedef struct
362 {
363 uint16_t r;
364 uint16_t g;
365 uint16_t b;
366 uint16_t a;
367 }
369
370typedef struct
371 {
372 gavl_palette_entry_t * entries;
373 int num_entries;
375
376GAVL_PUBLIC gavl_palette_t *
377gavl_palette_create(void);
378
379GAVL_PUBLIC void
380gavl_palette_alloc(gavl_palette_t * pal, int num_colors);
381
382GAVL_PUBLIC void
383gavl_palette_init(gavl_palette_t * pal);
384
385GAVL_PUBLIC void
386gavl_palette_free(gavl_palette_t * pal);
387
388GAVL_PUBLIC void
389gavl_palette_destroy(gavl_palette_t * pal);
390
391GAVL_PUBLIC void
392gavl_palette_move(gavl_palette_t * dst, gavl_palette_t * src);
393
394typedef struct
395 {
396 gavl_packet_extradata_type_t type;
397 void * data;
399
414typedef struct
415 {
416 gavl_buffer_t buf;
417
418 uint32_t flags;
419
420 int64_t position;
421
422 int64_t pts;
423 int64_t dts;
424 int64_t pes_pts;
425
426 int64_t duration;
427
428 uint32_t field2_offset;
429 uint32_t header_size;
431
434
436 int32_t dst_x;
437 int32_t dst_y;
438
439 int32_t id;
440 int buf_idx;
441
442 gavl_packet_extradata_t ext_data[GAVL_PACKET_MAX_EXTRADATA];
443
444 // int fds[GAVL_MAX_PLANES];
445 // int num_fds;
447
453GAVL_PUBLIC
455
456GAVL_PUBLIC
457gavl_packet_t * gavl_packet_create();
458
459
460
461GAVL_PUBLIC
462void gavl_packet_destroy(gavl_packet_t * p);
463
464
473GAVL_PUBLIC
475
480GAVL_PUBLIC
482
483GAVL_PUBLIC
484void * gavl_packet_add_extradata(gavl_packet_t * p, gavl_packet_extradata_type_t type);
485
486GAVL_PUBLIC
487void * gavl_packet_get_extradata(gavl_packet_t * p, gavl_packet_extradata_type_t type);
488
489/* Merge two consecutive fields into one */
490GAVL_PUBLIC
491void gavl_packet_merge_field2(gavl_packet_t * p, const gavl_packet_t * field2);
492
493
499GAVL_PUBLIC
501 const gavl_packet_t * src);
502
513GAVL_PUBLIC
515 const gavl_packet_t * src);
516
517
524GAVL_PUBLIC
526
527
534GAVL_PUBLIC
536
544GAVL_PUBLIC
546 const char * filename);
547
548
549GAVL_PUBLIC
550void
551gavl_packet_to_videoframe(const gavl_packet_t * p,
553
554
555GAVL_PUBLIC
556void gavl_video_frame_to_packet_metadata(const gavl_video_frame_t * frame,
557 gavl_packet_t * pkt);
558
559GAVL_PUBLIC
560void gavl_packet_to_video_frame_metadata(const gavl_packet_t * p, gavl_video_frame_t * frame);
561
562
563typedef struct
564 {
565 int32_t size_min;
566 int32_t size_max;
567 int64_t duration_min;
568 int64_t duration_max;
569 int64_t pts_start;
570 int64_t pts_end;
571
572 int64_t total_bytes; // For average bitrate
573 int64_t total_packets; // For average framerate
574
576
577GAVL_PUBLIC
578void gavl_stream_stats_init(gavl_stream_stats_t*);
579
580GAVL_PUBLIC
581void gavl_stream_stats_dump(const gavl_stream_stats_t*, int indent);
582
583GAVL_PUBLIC
584void gavl_stream_stats_update(gavl_stream_stats_t*,const gavl_packet_t*p);
585
586GAVL_PUBLIC
587void gavl_stream_stats_update_end(gavl_stream_stats_t * f, const gavl_packet_t * p);
588
589GAVL_PUBLIC
590void gavl_stream_stats_update_params(gavl_stream_stats_t * f,
591 int64_t pts, int64_t duration, int data_len,
592 int flags);
593
594
595GAVL_PUBLIC
596void gavl_stream_stats_apply_audio(gavl_stream_stats_t * f,
597 const gavl_audio_format_t * fmt,
599 gavl_dictionary_t * m);
600
601GAVL_PUBLIC
602void gavl_stream_stats_apply_video(gavl_stream_stats_t * f,
605 gavl_dictionary_t * m);
606
607GAVL_PUBLIC
608void gavl_stream_stats_apply_subtitle(gavl_stream_stats_t * f,
609 gavl_dictionary_t * m);
610
611GAVL_PUBLIC
612void gavl_stream_stats_apply_generic(gavl_stream_stats_t * f,
614 gavl_dictionary_t * m);
615
616
617GAVL_PUBLIC
618int gavl_stream_get_stats(const gavl_dictionary_t * s, gavl_stream_stats_t * stats);
619
620GAVL_PUBLIC
621void gavl_stream_set_stats(gavl_dictionary_t * s, const gavl_stream_stats_t * stats);
622
623/* PTS Cache */
624
625typedef struct gavl_packet_pts_cache_s gavl_packet_pts_cache_t;
626
627GAVL_PUBLIC
628gavl_packet_pts_cache_t * gavl_packet_pts_cache_create(int size);
629
630GAVL_PUBLIC
631void gavl_packet_pts_cache_destroy(gavl_packet_pts_cache_t *);
632
633GAVL_PUBLIC
634void gavl_packet_pts_cache_push(gavl_packet_pts_cache_t *m, const gavl_packet_t * pkt);
635
636GAVL_PUBLIC
637int gavl_packet_pts_cache_get_first(gavl_packet_pts_cache_t *m, gavl_packet_t * pkt);
638
639GAVL_PUBLIC
640int gavl_packet_pts_cache_get_by_pts(gavl_packet_pts_cache_t *m, gavl_packet_t * pkt,
641 int64_t pts);
642
643GAVL_PUBLIC
644void gavl_packet_pts_cache_clear(gavl_packet_pts_cache_t *m);
645
646
647#if 0
648GAVL_PUBLIC
649void gavl_compression_info_to_dictionary(const gavl_compression_info_t * info, gavl_dictionary_t * dict);
650
651GAVL_PUBLIC
652void gavl_compression_info_from_dictionary(gavl_compression_info_t * info, const gavl_dictionary_t * dict);
653#endif
654
655typedef struct
656 {
657 int num_entries;
658 int entries_alloc;
659
660 struct
661 {
662 int64_t position;
663 int64_t pts;
664 } * entries;
665
667
668GAVL_PUBLIC
669void gavl_seek_index_append_packet(gavl_seek_index_t * idx,
670 const gavl_packet_t * pkt, int compression_flags);
671
672GAVL_PUBLIC
673void gavl_seek_index_append_pos_pts(gavl_seek_index_t * idx, int64_t position, int64_t pts);
674
675GAVL_PUBLIC
676int gavl_seek_index_seek(const gavl_seek_index_t * idx,
677 int64_t pts);
678
679GAVL_PUBLIC
680void gavl_seek_index_free(gavl_seek_index_t * idx);
681
682GAVL_PUBLIC
683void gavl_seek_index_dump(gavl_seek_index_t * idx);
684
685GAVL_PUBLIC
686void gavl_seek_index_to_buffer(const gavl_seek_index_t * idx, gavl_buffer_t * buf);
687
688GAVL_PUBLIC
689int gavl_seek_index_from_buffer(gavl_seek_index_t * idx, const gavl_buffer_t * buf);
690
695#ifdef __cplusplus
696}
697#endif
698
699
700#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 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:71
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 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:84
@ GAVL_CODEC_ID_SPEEX
Speex.
Definition compression.h:87
@ GAVL_CODEC_ID_DTS
DTS.
Definition compression.h:88
@ GAVL_CODEC_ID_MPEG1
MPEG-1 video.
Definition compression.h:95
@ GAVL_CODEC_ID_DV
DV (several variants)
Definition compression.h:101
@ GAVL_CODEC_ID_AC3
AC3.
Definition compression.h:82
@ GAVL_CODEC_ID_DIRAC
Complete DIRAC frames, sequence end code appended to last packet.
Definition compression.h:100
@ GAVL_CODEC_ID_TGA
TGA image.
Definition compression.h:94
@ GAVL_CODEC_ID_MPEG2
MPEG-2 video.
Definition compression.h:96
@ GAVL_CODEC_ID_AAC
AAC as stored in quicktime/mp4.
Definition compression.h:83
@ GAVL_CODEC_ID_VP8
VP8 (as used in webm)
Definition compression.h:102
@ GAVL_CODEC_ID_MPEG4_ASP
MPEG-4 ASP (a.k.a. Divx4)
Definition compression.h:97
@ GAVL_CODEC_ID_JPEG
JPEG image.
Definition compression.h:91
@ GAVL_CODEC_ID_H264
H.264 (Annex B)
Definition compression.h:98
@ GAVL_CODEC_ID_MP2
MPEG-1 audio layer II.
Definition compression.h:80
@ GAVL_CODEC_ID_TIFF
TIFF image.
Definition compression.h:93
@ GAVL_CODEC_ID_FLAC
Flac (extradata contain a file header without comment and seektable)
Definition compression.h:85
@ GAVL_CODEC_ID_THEORA
Theora (segmented extradata)
Definition compression.h:99
@ GAVL_CODEC_ID_DIV3
Old style Divx (aka MSMPEG4V3)
Definition compression.h:103
@ GAVL_CODEC_ID_MP3
MPEG-1/2 audio layer 3 CBR/VBR.
Definition compression.h:81
@ GAVL_CODEC_ID_DVDSUB
DVD subtitles, palette is in header.
Definition compression.h:106
@ GAVL_CODEC_ID_PNG
PNG image.
Definition compression.h:92
@ GAVL_CODEC_ID_ALAW
alaw 2:1
Definition compression.h:78
@ GAVL_CODEC_ID_NONE
Unknown/unsupported compression format. In gavf files this signals that the stream consists of uncomp...
Definition compression.h:76
@ GAVL_CODEC_ID_OPUS
Opus.
Definition compression.h:86
@ GAVL_CODEC_ID_EXTENDED
Separate compression id.
Definition compression.h:107
@ GAVL_CODEC_ID_ULAW
mu-law 2:1
Definition compression.h:79
uint64_t gavl_timecode_t
Typedef for timecodes.
Definition timecode.h:43
gavl_interlace_mode_t
Interlace mode.
Definition gavl.h:2301
#define GAVL_MAX_PLANES
Definition gavl.h:1418
Audio Format.
Definition gavl.h:272
Compression format.
Definition compression.h:125
gavl_codec_id_t id
Codec ID.
Definition compression.h:127
gavl_buffer_t codec_header
Global header.
Definition compression.h:129
int flags
ORed combination of GAVL_COMPRESSION_* flags.
Definition compression.h:126
int video_buffer_size
VBV buffer size for video (in BYTES)
Definition compression.h:135
int bitrate
Needed by some codecs, negative values mean VBR.
Definition compression.h:131
int pre_skip
Samples to skip at the start.
Definition compression.h:133
int palette_size
Size of the embedded palette for image codecs.
Definition compression.h:132
Definition compression.h:395
Definition compression.h:356
Packet structure.
Definition compression.h:415
int64_t pes_pts
PTS from the PES stream (probably in another scale)
Definition compression.h:424
gavl_rectangle_i_t src_rect
Rectangle to take from a video frame.
Definition compression.h:435
gavl_timecode_t timecode
Timecode.
Definition compression.h:433
int64_t dts
Decoding time.
Definition compression.h:423
int64_t pts
Presentation time.
Definition compression.h:422
uint32_t field2_offset
Offset of field 2 for field pictures.
Definition compression.h:428
gavl_interlace_mode_t interlace_mode
Interlace mode for mixed interlacing.
Definition compression.h:432
uint32_t sequence_end_pos
Position of sequence end code if any.
Definition compression.h:430
uint32_t header_size
Size of a repeated global header (or 0)
Definition compression.h:429
gavl_buffer_t buf
Data.
Definition compression.h:416
int32_t dst_y
Y-coordinate in the destination frame (for overlays)
Definition compression.h:437
int64_t position
Position of the packet in the file. The exact meaning is format dependent.
Definition compression.h:420
int64_t duration
Duration of the contained frame.
Definition compression.h:426
int32_t id
ID of the gavf stream where this packet belongs.
Definition compression.h:439
int32_t dst_x
X-coordinate in the destination frame (for overlays)
Definition compression.h:436
uint32_t flags
ORed combination of GAVL_PACKET_* flags.
Definition compression.h:418
Definition compression.h:362
Definition compression.h:371
Integer rectangle.
Definition gavl.h:1431
Definition compression.h:656
Definition compression.h:564
Video format.
Definition gavl.h:2375
Definition gavl.h:2587