1 /*****************************************************************
2  * gavl - a general purpose audio/video processing library
3  *
4  * Copyright (c) 2001 - 2011 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 /**
23  * @file gavl.h
24  * external api header.
25  */
26 
27 #ifndef GAVL_H_INCLUDED
28 #define GAVL_H_INCLUDED
29 
30 #include <inttypes.h>
31 
32 #include <gavl/gavldefs.h>
33 #include <gavl/gavltime.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #include <gavl/timecode.h>
40 
41 
42 /** \defgroup mt Multithreading
43  *  \brief Multithreading
44  *
45  *  gavl has generic multithreading support for video processing.
46  *  It's done by splitting the calculations in smaller pieces (usually
47  *  slices of the destination images) and calling user supplied functions,
48  *  which can transfer the tasks to worker threads. Multithreading is configured with
49  *  \ref gavl_video_options_set_num_threads, \ref gavl_video_options_set_run_func and
50  *  \ref gavl_video_options_set_stop_func
51  *
52  *  @{
53  */
54 
55 /** \brief Prototype of a process function
56  *  \param data Private data
57  *  \param start Where the function should start
58  *  \param end Where the function should end (exclusive)
59  *
60  *  This function is supplied by gavl and passed to the application,
61  *  which then executes multiple instances of the functions in multiple threads
62  */
63 
64 typedef void (*gavl_video_process_func)(void * data, int start, int end);
65 
66 /** \brief Run a piece of a calculation
67  *  \param func Function to execute
68  *  \param gavl_data 1. Argument for func
69  *  \param start 2. Argument for func
70  *  \param end   3. Argument for func
71  *  \param client_data Data passed with \ref gavl_video_options_set_run_func
72  *  \param thread Number of processing thread (starting with 0)
73  *
74  *  This function supplied by the application and passed to gavl via
75  *  \ref gavl_video_options_set_run_func. It should call func with the given parameters
76  *  in a worker thread.
77  */
78 
79 typedef void (*gavl_video_run_func)(gavl_video_process_func func,
80                                     void * gavl_data,
81                                     int start, int end,
82                                     void * client_data, int thread);
83 
84 /** \brief Wait until a piece of a calculation finished
85  *  \brief client_data Data passed with \ref gavl_video_options_set_stop_func
86  *  \param thread Number of processing thread (starting with 0)
87  *
88  *  This function must make sure that the task started by \ref gavl_video_run_func
89  *  is finished.
90  */
91 
92 typedef void (*gavl_video_stop_func)(void * client_data, int thread);
93 
94 /**
95  * @}
96  */
97 
98 /*! \ingroup video_format
99  *\brief Video format
100  */
101 
102 typedef struct gavl_video_format_s gavl_video_format_t;
103 
104 
105 /* Quality levels */
106 
107 /** \defgroup quality Quality settings
108     \brief Generic quality settings
109 
110     Gavl allows multiple versions of each conversion routine. Optimized routines often
111     have a worse precision, while highly accurate routines are too slow for live playback.
112     Quality level 3 enables the standard ANSI-C versions, which are always available, or
113     an optimized version of the same accuracy.
114     Qualities 1 and 2 choose optimized versions which are less accurate. Qualities 4 and 5
115     enable high quality versions.
116 
117     Not all routines are available for quality levels other than 3. In these cases, the
118     quality will be ignored.
119  */
120 
121 
122 
123 /*!
124     \ingroup quality
125     \brief Fastest processing
126 
127     Worst quality.
128 */
129 
130 #define GAVL_QUALITY_FASTEST 1
131 
132 /*! \ingroup quality
133     \brief Highest quality
134 
135     Slowest, may not work in realtime applications
136 */
137 
138 #define GAVL_QUALITY_BEST    5
139 
140 /*! \ingroup quality
141     \brief Default quality
142 
143     Default quality, most probably the choice for realtime playback applications.
144 */
145 
146 #define GAVL_QUALITY_DEFAULT 2
147 
148 /** \defgroup accel_flags Acceleration flags
149  *  \brief CPU specific acceleration flags
150  *
151  *  These flags are used internally by gavl to obtain the supported
152  *  acceleration mechanisms at runtime. Some applications however might
153  *  need them as well for other tasks, so they are exported into the
154  *  public API.
155  *
156  *  @{
157  */
158 
159 #define GAVL_ACCEL_MMX      (1<<0) //!< MMX
160 #define GAVL_ACCEL_MMXEXT   (1<<1) //!< Extended MMX (a.k.a MMX2)
161 #define GAVL_ACCEL_SSE      (1<<2) //!< Intel SSE
162 #define GAVL_ACCEL_SSE2     (1<<3) //!< Intel SSE2
163 #define GAVL_ACCEL_SSE3     (1<<4) //!< Intel SSE3
164 #define GAVL_ACCEL_3DNOW    (1<<5) //!< AMD 3Dnow
165 #define GAVL_ACCEL_3DNOWEXT (1<<6) //!< AMD 3Dnow ext
166 #define GAVL_ACCEL_SSSE3    (1<<7) //!< Intel SSSE3
167 
168 /** \brief Get the supported acceleration flags
169  *  \returns A combination of GAVL_ACCEL_* flags.
170  */
171 
172 GAVL_PUBLIC int gavl_accel_supported();
173 
174 /**
175  *  @}
176  */
177 
178 /** \defgroup audio Audio
179  *  \brief Audio support
180  */
181 
182 /* Sample formats: all multibyte numbers are native endian */
183 
184 /** \defgroup audio_format Audio format definitions
185  *  \ingroup audio
186  *
187  * \brief Definitions for several variations of audio data
188 */
189 
190 
191 /*! \def GAVL_MAX_CHANNELS
192  *  \ingroup audio_format
193     \brief Maximum number of audio channels
194 
195 */
196 #define GAVL_MAX_CHANNELS 128
197 
198 /** \ingroup audio_format
199  *  \brief Format of one audio sample
200  *
201  * For multibyte numbers, the byte order is always machine native endian
202  */
203 
204 typedef enum
205   {
206     GAVL_SAMPLE_NONE   = 0, /*!< Undefined */
207     GAVL_SAMPLE_U8     = 1, /*!< Unsigned 8 bit */
208     GAVL_SAMPLE_S8     = 2, /*!< Signed 8 bit */
209     GAVL_SAMPLE_U16    = 3, /*!< Unsigned 16 bit */
210     GAVL_SAMPLE_S16    = 4, /*!< Signed 16 bit */
211     GAVL_SAMPLE_S32    = 5, /*!< Signed 32 bit */
212     GAVL_SAMPLE_FLOAT  = 6,  /*!< Floating point (-1.0 .. 1.0) */
213     GAVL_SAMPLE_DOUBLE = 7  /*!< Double (-1.0 .. 1.0) */
214   } gavl_sample_format_t;
215 
216 /** \ingroup audio_format
217  *
218  *  Interleave mode of the channels
219  */
220 
221 typedef enum
222   {
223     GAVL_INTERLEAVE_NONE = 0, /*!< No interleaving, all channels separate */
224     GAVL_INTERLEAVE_2    = 1, /*!< Interleaved pairs of channels          */
225     GAVL_INTERLEAVE_ALL  = 2  /*!< Everything interleaved                 */
226   } gavl_interleave_mode_t;
227 
228 /** \ingroup audio_format
229  *  \brief Audio channel setup
230  *
231  * These are the channel locations used to identify the channel order
232  * for an audio format
233  */
234 
235 typedef enum
236   {
237     GAVL_CHID_NONE         = 0,   /*!< Undefined                                 */
238     GAVL_CHID_FRONT_CENTER,       /*!< For mono                                  */
239     GAVL_CHID_FRONT_LEFT,         /*!< Front left                                */
240     GAVL_CHID_FRONT_RIGHT,        /*!< Front right                               */
241     GAVL_CHID_FRONT_CENTER_LEFT,  /*!< Left of Center                            */
242     GAVL_CHID_FRONT_CENTER_RIGHT, /*!< Right of Center                           */
243     GAVL_CHID_REAR_LEFT,          /*!< Rear left                                 */
244     GAVL_CHID_REAR_RIGHT,         /*!< Rear right                                */
245     GAVL_CHID_REAR_CENTER,        /*!< Rear Center                               */
246     GAVL_CHID_SIDE_LEFT,          /*!< Side left                                 */
247     GAVL_CHID_SIDE_RIGHT,         /*!< Side right                                */
248     GAVL_CHID_LFE,                /*!< Subwoofer                                 */
249     GAVL_CHID_AUX,                /*!< Additional channel (can be more than one) */
250   } gavl_channel_id_t;
251 
252 /** \ingroup audio_format
253  *  \brief Audio Format
254  *
255  * Structure describing an audio format. The samples_per_frame member is used
256  * exclusively by \ref gavl_audio_frame_create to determine how many bytes to
257  * allocate.
258  */
259 
260 typedef struct
261   {
262   int samples_per_frame;  /*!< Maximum number of samples per frame */
263   int samplerate;         /*!< Samplerate */
264   int num_channels;         /*!< Number of channels */
265   gavl_sample_format_t   sample_format; /*!< Sample format */
266   gavl_interleave_mode_t interleave_mode; /*!< Interleave mode */
267 
268   float center_level; /*!< linear factor for mixing center to front */
269   float rear_level;   /*!< linear factor for mixing rear to front */
270 
271   gavl_channel_id_t channel_locations[GAVL_MAX_CHANNELS];   /*!< Which channel is stored where */
272 
273   } gavl_audio_format_t;
274 
275 
276 /* Audio format -> string conversions */
277 
278 /*!
279   \ingroup audio_format
280   \brief Convert a gavl_sample_format_t to a human readable string
281   \param format A sample format
282   \returns A string describing the format
283  */
284 
285 GAVL_PUBLIC
286 const char * gavl_sample_format_to_string(gavl_sample_format_t format);
287 
288 /*!
289   \ingroup audio_format
290   \brief Convert a string to a sample format
291   \param str String
292 
293   str must be one of the strings returned by \ref gavl_sample_format_to_string
294  */
295 
296 GAVL_PUBLIC
297 gavl_sample_format_t gavl_string_to_sample_format(const char * str);
298 
299 /*! \ingroup audio_format
300  * \brief Get total number of supported sample formats
301  * \returns total number of supported sample formats
302  */
303 
304 GAVL_PUBLIC
305 int gavl_num_sample_formats();
306 
307 /*! \ingroup audio_format
308  * \brief Get the sample format from index
309  * \param index index (must be between 0 and the result of \ref gavl_num_sample_formats)
310  * \returns The sample format corresponding to index or GAVL_SAMPLE_NONE.
311  */
312 
313 GAVL_PUBLIC
314 gavl_sample_format_t gavl_get_sample_format(int index);
315 
316 /*!
317   \ingroup audio_format
318   \brief Convert a gavl_channel_id_t to a human readable string
319   \param id A channel id
320  */
321 
322 GAVL_PUBLIC
323 const char * gavl_channel_id_to_string(gavl_channel_id_t id);
324 
325 
326 /*!
327   \ingroup audio_format
328   \brief Convert a gavl_interleave_mode_t to a human readable string
329   \param mode An interleave mode
330  */
331 
332 GAVL_PUBLIC
333 const char * gavl_interleave_mode_to_string(gavl_interleave_mode_t mode);
334 
335 /*!
336   \ingroup audio_format
337   \brief Dump an audio format to stderr
338   \param format An audio format
339  */
340 
341 GAVL_PUBLIC
342 void gavl_audio_format_dump(const gavl_audio_format_t * format);
343 
344 /*!
345   \ingroup audio_format
346   \brief Get the index of a particular channel for a given format.
347   \param format An audio format
348   \param id A channel id
349   \returns The index of the channel in the format or -1 if such a channel is not present
350  */
351 
352 GAVL_PUBLIC
353 int gavl_channel_index(const gavl_audio_format_t * format, gavl_channel_id_t id);
354 
355 /*!
356   \ingroup audio_format
357   \brief Get number of front channels for a given format
358   \param format An audio format
359  */
360 
361 GAVL_PUBLIC
362 int gavl_front_channels(const gavl_audio_format_t * format);
363 
364 /*!
365   \ingroup audio_format
366   \brief Get number of rear channels for a given format
367   \param format An audio format
368  */
369 
370 GAVL_PUBLIC
371 int gavl_rear_channels(const gavl_audio_format_t * format);
372 
373 /*!
374   \ingroup audio_format
375   \brief Get number of side channels for a given format
376   \param format An audio format
377  */
378 
379 GAVL_PUBLIC
380 int gavl_side_channels(const gavl_audio_format_t * format);
381 
382 /*!
383   \ingroup audio_format
384   \brief Get number of aux channels for a given format
385   \param format An audio format
386  */
387 
388 GAVL_PUBLIC
389 int gavl_aux_channels(const gavl_audio_format_t * format);
390 
391 
392 
393 /*!
394   \ingroup audio_format
395   \brief Get number of LFE channels for a given format
396   \param format An audio format
397  */
398 
399 GAVL_PUBLIC
400 int gavl_lfe_channels(const gavl_audio_format_t * format);
401 
402 /*!
403   \ingroup audio_format
404   \brief Copy one audio format to another
405   \param dst Destination format
406   \param src Source format
407  */
408 
409 GAVL_PUBLIC
410 void gavl_audio_format_copy(gavl_audio_format_t * dst,
411                             const gavl_audio_format_t * src);
412 
413 /*!
414   \ingroup audio_format
415   \brief Compare 2 audio formats
416   \param format_1 First format
417   \param format_2 Second format
418   \returns 1 if the formats are equal, 0 else
419 */
420 
421 GAVL_PUBLIC
422 int gavl_audio_formats_equal(const gavl_audio_format_t * format_1,
423                               const gavl_audio_format_t * format_2);
424 
425 /*!
426   \ingroup audio_format
427   \brief Set the default channel setup and indices
428   \param format An audio format
429 
430   Set a default channel setup and channel indices if only the number of channels
431   is known. The result might be wrong if you have
432   something else than mono or stereo from a stream, which has no informtions about the
433   speaker configurations.
434 */
435 
436 GAVL_PUBLIC
437 void gavl_set_channel_setup(gavl_audio_format_t * format);
438 
439 /*!
440   \ingroup audio_format
441   \brief Get the number of bytes per sample for a given sample format
442   \param format A sample format
443 */
444 
445 GAVL_PUBLIC
446 int gavl_bytes_per_sample(gavl_sample_format_t format);
447 
448 /** \defgroup audio_frame Audio frame
449  * \ingroup audio
450  *
451  * \brief Container for audio data
452  *
453  *
454  */
455 
456 
457 /*!
458   \ingroup audio_frame
459   \brief Container for interleaved audio samples
460  */
461 
462 typedef union
463   {
464   uint8_t * u_8; /*!< Unsigned 8 bit samples */
465   int8_t *  s_8; /*!< Signed 8 bit samples */
466 
467   uint16_t * u_16; /*!< Unsigned 16 bit samples */
468   int16_t  * s_16; /*!< Signed 16 bit samples */
469 
470   uint32_t * u_32; /*!< Unsigned 32 bit samples (used internally only) */
471   int32_t  * s_32; /*!< Signed 32 bit samples */
472 
473   float * f; /*!< Floating point samples */
474   double * d; /*!< Double samples */
475   } gavl_audio_samples_t;
476 
477 /*!
478   \ingroup audio_frame
479   \brief Container for noninterleaved audio samples
480  */
481 
482 typedef union
483   {
484   uint8_t * u_8[GAVL_MAX_CHANNELS];/*!< Unsigned 8 bit channels */
485   int8_t *  s_8[GAVL_MAX_CHANNELS];/*!< Signed 8 bit channels */
486 
487   uint16_t * u_16[GAVL_MAX_CHANNELS];/*!< Unsigned 16 bit channels */
488   int16_t  * s_16[GAVL_MAX_CHANNELS];/*!< Signed 16 bit channels */
489 
490   uint32_t * u_32[GAVL_MAX_CHANNELS];/*!< Unsigned 32 bit channels */
491   int32_t  * s_32[GAVL_MAX_CHANNELS];/*!< Signed 32 bit channels (used internally only) */
492 
493   float * f[GAVL_MAX_CHANNELS];/*!< Floating point channels */
494   double * d[GAVL_MAX_CHANNELS];/*!< Double channels */
495 
496   } gavl_audio_channels_t;
497 
498 /*!
499   \ingroup audio_frame
500   \brief Generic container for audio samples.
501 
502   This is the main container structure for audio data. The data are stored in unions,
503   so you can access the matching pointer type without the need for casts. If you have
504   noninterleaved channels, the i'th channel will be in channels[i].f (if you use floating
505   point samples). For noninterleaved formats, use the samples member. valid_samples must
506   be set by the source to the number of actually valid samples in this frame.
507 
508   Audio frames are created with \ref gavl_audio_frame_create and destroyed with
509   \ref gavl_audio_frame_destroy. The memory can either be allocated by gavl (memory
510   aligned) or by the caller.
511 
512  */
513 
514 typedef struct
515   {
516   gavl_audio_samples_t  samples; /*!< Sample pointer for interleaved formats         */
517   gavl_audio_channels_t channels;/*!< Channel pointer for non interleaved formats    */
518   int valid_samples;             /*!< Number of actually valid samples */
519   int64_t timestamp;             /*!< Timestamp in samplerate tics */
520   int channel_stride;            /*!< Byte offset between channels. Total allocated size is always num_channels * channel_stride */
521   } gavl_audio_frame_t;
522 
523 /*!
524   \ingroup audio_frame
525   \brief Create audio frame
526   \param format Format of the data to be stored in this frame or NULL
527 
528   Creates an audio frame for a given format and allocates buffers for the audio data. The buffer
529   size is determined by the samples_per_frame member of the format. To create an audio frame from
530   your custom memory, pass NULL for the format and you'll get an empty frame in which you can set the
531   pointers manually.
532 */
533 
534 GAVL_PUBLIC
535 gavl_audio_frame_t * gavl_audio_frame_create(const gavl_audio_format_t* format);
536 
537 /*!
538   \ingroup audio_frame
539   \brief Zero all pointers in the audio frame.
540   \param frame An audio frame
541 
542   Zero all pointers, so \ref gavl_audio_frame_destroy won't free them.
543   Call this for audio frames, which were created with a NULL format
544   before destroying them.
545 
546 */
547 
548 GAVL_PUBLIC
549 void gavl_audio_frame_null(gavl_audio_frame_t * frame);
550 
551 /*!
552   \ingroup audio_frame
553   \brief Destroy an audio frame.
554   \param frame An audio frame
555 
556   Destroys an audio frame and frees all associated memory. If you used your custom memory
557   to allocate the frame, call \ref gavl_audio_frame_null before.
558 */
559 
560 GAVL_PUBLIC
561 void gavl_audio_frame_destroy(gavl_audio_frame_t * frame);
562 
563 /*!
564   \ingroup audio_frame
565   \brief Mute an audio frame.
566   \param frame An audio frame
567   \param format The format of the frame
568 
569   Fills the frame with digital zero samples according to the audio format
570 */
571 
572 GAVL_PUBLIC
573 void gavl_audio_frame_mute(gavl_audio_frame_t * frame,
574                            const gavl_audio_format_t * format);
575 
576 /*!
577   \ingroup audio_frame
578   \brief Mute a number of samples at the start of an audio frame.
579   \param frame An audio frame
580   \param format The format of the frame
581   \param num_samples Number of samples to mute
582 
583   Fills the frame with digital zero samples according to the audio format
584 */
585 
586 GAVL_PUBLIC
587 void gavl_audio_frame_mute_samples(gavl_audio_frame_t * frame,
588                                    const gavl_audio_format_t * format,
589                                    int num_samples);
590 
591 
592 
593 /*!
594   \ingroup audio_frame
595   \brief Mute a single channel of an audio frame.
596   \param frame An audio frame
597   \param format The format of the frame
598   \param channel The channel to mute
599 
600   Fills the frame with digital zero samples according to the audio format
601 */
602 
603 GAVL_PUBLIC
604 void gavl_audio_frame_mute_channel(gavl_audio_frame_t * frame,
605                                    const gavl_audio_format_t * format,
606                                    int channel);
607 
608 /*!
609   \ingroup audio_frame
610   \brief Copy audio data from one frame to another.
611   \param format Format, must be equal for source and destination frames
612   \param dst Destination frame
613   \param src Source frame
614   \param dst_pos Offset (in samples) in the destination frame
615   \param src_pos Offset (in samples) in the source frame
616   \param dst_size Available samples in the destination frame
617   \param src_size Available samples in the source frame
618   \returns The actual number of copied samples
619 
620   This function copies audio samples from src (starting at src_offset) to dst
621   (starting at dst_offset). The number of copied samples will be the smaller one of
622   src_size and dst_size.
623 
624   You can use this function for creating a simple but effective audio buffer.
625 
626 */
627 
628 GAVL_PUBLIC
629 int gavl_audio_frame_copy(const gavl_audio_format_t * format,
630                           gavl_audio_frame_t * dst,
631                           const gavl_audio_frame_t * src,
632                           int dst_pos,
633                           int src_pos,
634                           int dst_size,
635                           int src_size);
636 
637 /*!
638   \ingroup audio_frame
639   \brief Copy audio data from one frame to another.
640   \param format Format, must be equal for source and destination frames
641   \param dst Destination frame
642   \param src Source frame
643 
644   This function copies the pointers of the frame, not the actual data
645 
646   Since 1.1.1
647 */
648 
649 GAVL_PUBLIC
650 void gavl_audio_frame_copy_ptrs(const gavl_audio_format_t * format,
651                                 gavl_audio_frame_t * dst,
652                                 const gavl_audio_frame_t * src);
653 
654 /*!
655   \ingroup audio_frame
656   \brief Set an audio frame to a subframe of another frame
657   \param format Format
658   \param src Source frame
659   \param dst Destination frame
660   \param start Start position in the source frame
661   \param len Length in samples
662 
663   This sets all pointers and the valid_samples member in dst to a
664   subframe of src. dst should be created with a NULL format and
665   \ref gavl_audio_frame_null should be called before destroying it.
666 
667   Since 1.1.2
668 */
669 
670 
671 GAVL_PUBLIC
672 void gavl_audio_frame_get_subframe(const gavl_audio_format_t * format,
673                                    gavl_audio_frame_t * src,
674                                    gavl_audio_frame_t * dst,
675                                    int start, int len);
676 
677 /*!
678   \ingroup audio_frame
679   \brief Check if 2 audio frames are bit-identical
680   \param format Format
681   \param f1 First frame
682   \param f2 Second frame
683   \returns 1 if the frames are equal, 0 else
684 
685   Since 1.2.0
686 */
687 
688 
689 GAVL_PUBLIC
690 int gavl_audio_frames_equal(const gavl_audio_format_t * format,
691                             const gavl_audio_frame_t * f1,
692                             const gavl_audio_frame_t * f2);
693 
694 /*!
695   \ingroup audio_frame
696   \brief Plot an audio frame to an ASCII file
697   \param format Format
698   \param frame An audio frame
699   \param name_base Filename base
700   \returns 1 in success, 0 in failure
701 
702   Plots an audio frame into an ascii file with one line per
703   sample in the format:
704   sample_number channel1 channel2 ...
705 
706   In addition, a file for making a plot with gnuplot is generated.
707   name_base is used for generating the filenames. For the data file,
708   the extension ".dat" is appended. For the gnuplot file it's ".gnu"
709 
710   Since 1.2.0
711 */
712 
713 GAVL_PUBLIC
714 int gavl_audio_frame_plot(const gavl_audio_format_t * format,
715                           const gavl_audio_frame_t * frame,
716                           const char * name_base);
717 
718 
719 /** \defgroup audio_options Audio conversion options
720     \ingroup audio
721 
722 */
723 
724 /** \defgroup audio_conversion_flags Audio conversion flags
725     \ingroup audio_options
726 
727     Flags for passing to \ref gavl_audio_options_set_conversion_flags
728 */
729 
730 /*! \ingroup audio_conversion_flags
731  */
732 
733 #define GAVL_AUDIO_FRONT_TO_REAR_COPY (1<<0) /*!< When mixing front to rear, just copy the front channels */
734 
735 /*! \ingroup audio_conversion_flags
736  */
737 
738 #define GAVL_AUDIO_FRONT_TO_REAR_MUTE (1<<1) /*!< When mixing front to rear, mute the rear channels */
739 
740 /*! \ingroup audio_conversion_flags
741  */
742 
743 #define GAVL_AUDIO_FRONT_TO_REAR_DIFF (1<<2) /*!< When mixing front to rear, send the difference between front to rear */
744 
745 /*! \ingroup audio_conversion_flags
746  */
747 
748 #define GAVL_AUDIO_FRONT_TO_REAR_MASK           \
749 (GAVL_AUDIO_FRONT_TO_REAR_COPY | \
750 GAVL_AUDIO_FRONT_TO_REAR_MUTE | \
751  GAVL_AUDIO_FRONT_TO_REAR_DIFF) /*!< Mask for front to rear mode */
752 
753 /* Options for mixing stereo to mono */
754 
755 /*! \ingroup audio_conversion_flags
756  */
757 #define GAVL_AUDIO_STEREO_TO_MONO_LEFT  (1<<3) /*!< When converting from stereo to mono, choose left channel */
758 /*! \ingroup audio_conversion_flags
759  */
760 #define GAVL_AUDIO_STEREO_TO_MONO_RIGHT (1<<4) /*!< When converting from stereo to mono, choose right channel      */
761 /*! \ingroup audio_conversion_flags
762  */
763 #define GAVL_AUDIO_STEREO_TO_MONO_MIX   (1<<5) /*!< When converting from stereo to mono, mix left and right */
764 
765 /*! \ingroup audio_conversion_flags
766  */
767 #define GAVL_AUDIO_STEREO_TO_MONO_MASK \
768 (GAVL_AUDIO_STEREO_TO_MONO_LEFT | \
769 GAVL_AUDIO_STEREO_TO_MONO_RIGHT | \
770 GAVL_AUDIO_STEREO_TO_MONO_MIX) /*!< Mask for converting stereo to mono */
771 
772 /*! \ingroup audio_conversion_flags
773  */
774 
775 #define GAVL_AUDIO_NORMALIZE_MIX_MATRIX (1<<6) /*!< Normalize the user defined mix matrix (since 1.1.2) */
776 
777 
778 /*! \ingroup audio_options
779  *  \brief Dither mode
780  */
781 
782 typedef enum
783   {
784     GAVL_AUDIO_DITHER_NONE   = 0,
785     GAVL_AUDIO_DITHER_AUTO   = 1,
786     GAVL_AUDIO_DITHER_RECT   = 2,
787     GAVL_AUDIO_DITHER_TRI    = 3,
788     GAVL_AUDIO_DITHER_SHAPED = 4,
789   } gavl_audio_dither_mode_t;
790 
791 /*! \ingroup audio_options
792  *  \brief Resample mode
793  */
794 
795 typedef enum
796   {
797     GAVL_RESAMPLE_AUTO        = 0, /*!< Set from quality */
798     GAVL_RESAMPLE_ZOH         = 1, /*!< Zero order hold interpolator, very fast, poor quality. */
799     GAVL_RESAMPLE_LINEAR      = 2, /*!< Linear interpolator, very fast, poor quality. */
800     GAVL_RESAMPLE_SINC_FAST   = 3, /*!< Band limited sinc interpolation, fastest, 97dB SNR, 80% BW. */
801     GAVL_RESAMPLE_SINC_MEDIUM = 4, /*!< Band limited sinc interpolation, medium quality, 97dB SNR, 90% BW. */
802     GAVL_RESAMPLE_SINC_BEST   = 5  /*!< Band limited sinc interpolation, best quality, 97dB SNR, 96% BW. */
803   } gavl_resample_mode_t;
804 
805 /*! \ingroup audio_options
806  *  \brief Opaque container for audio conversion options.
807  *
808  * You don't want to know what's inside.
809  */
810 
811 typedef struct gavl_audio_options_s gavl_audio_options_t;
812 
813 /*! \ingroup audio_options
814  *  \brief Set the quality level for the converter
815  *  \param opt Audio options
816  *  \param quality Quality level (see \ref quality)
817  */
818 
819 GAVL_PUBLIC
820 void gavl_audio_options_set_quality(gavl_audio_options_t * opt, int quality);
821 
822 /*! \ingroup audio_options
823  *  \brief Get the quality level for a converter
824  *  \param opt Audio options
825  *  \returns Quality level (see \ref quality)
826  */
827 
828 GAVL_PUBLIC
829 int gavl_audio_options_get_quality(gavl_audio_options_t * opt);
830 
831 /*! \ingroup audio_options
832  *  \brief Set the dither mode for the converter
833  *  \param opt Audio options
834  *  \param mode A dither mode
835  */
836 
837 GAVL_PUBLIC
838 void gavl_audio_options_set_dither_mode(gavl_audio_options_t * opt, gavl_audio_dither_mode_t mode);
839 
840 /*! \ingroup audio_options
841  *  \brief Get the dither mode for the converter
842  *  \param opt Audio options
843  *  \returns The dither mode
844  */
845 
846 GAVL_PUBLIC
847 gavl_audio_dither_mode_t gavl_audio_options_get_dither_mode(gavl_audio_options_t * opt);
848 
849 
850 /*! \ingroup audio_options
851  *  \brief Set the resample mode for the converter
852  *  \param opt Audio options
853  *  \param mode A resample mode
854  */
855 
856 GAVL_PUBLIC
857 void gavl_audio_options_set_resample_mode(gavl_audio_options_t * opt, gavl_resample_mode_t mode);
858 
859 /*! \ingroup audio_options
860  *  \brief Get the resample mode for the converter
861  *  \param opt Audio options
862  *  \returns The resample mode
863  */
864 
865 GAVL_PUBLIC
866 gavl_resample_mode_t gavl_audio_options_get_resample_mode(gavl_audio_options_t * opt);
867 
868 /*! \ingroup audio_options
869  *  \brief Set the conversion flags
870  *  \param opt Audio options
871  *  \param flags Flags (see \ref audio_conversion_flags)
872  */
873 
874 GAVL_PUBLIC
875 void gavl_audio_options_set_conversion_flags(gavl_audio_options_t * opt,
876                                              int flags);
877 
878 /*! \ingroup audio_options
879  *  \brief Get the conversion flags
880  *  \param opt Audio options
881  *  \returns Flags (see \ref audio_conversion_flags)
882  */
883 
884 GAVL_PUBLIC
885 int gavl_audio_options_get_conversion_flags(gavl_audio_options_t * opt);
886 
887 /*! \ingroup audio_options
888  *  \brief Set all options to their defaults
889  *  \param opt Audio options
890  */
891 
892 GAVL_PUBLIC
893 void gavl_audio_options_set_defaults(gavl_audio_options_t * opt);
894 
895 /*! \ingroup audio_options
896  *  \brief Set a user defined mix matrix
897  *  \param opt Audio options
898  *  \param matrix Mix matrix to use
899  *
900  *  The matrix consists of coefficients, where matrix[i][j]
901  *  is the factor for mixing input channel j to output channel i.
902  *  The matrix is not copied, so it should be valid at least until
903  *  the next call to \ref gavl_audio_converter_init.
904  *
905  *  To clear a previously defined mix matrix (restoring gavls default
906  *  behaviour) pass NULL.
907  *
908  *  Since 1.1.2
909  */
910 
911 GAVL_PUBLIC
912 void gavl_audio_options_set_mix_matrix(gavl_audio_options_t * opt,
913                                        const double ** matrix);
914 
915 /*! \ingroup audio_options
916  *  \brief Get the mix matrix
917  *  \param opt Audio options
918  *  \returns The user defined mix matrix
919  *
920  *  Since 1.1.2
921  */
922 
923 GAVL_PUBLIC
924 const double ** gavl_audio_options_get_mix_matrix(gavl_audio_options_t * opt);
925 
926 /*! \ingroup audio_options
927  *  \brief Create an options container
928  *  \returns Newly allocated udio options with default values
929  *
930  *  Use this to store options, which will apply for more than
931  *  one converter instance. Applying the options will be done by
932  *  gavl_*_get_options() followed by gavl_audio_options_copy().
933  */
934 
935 GAVL_PUBLIC
936 gavl_audio_options_t * gavl_audio_options_create();
937 
938 /*! \ingroup audio_options
939  *  \brief Copy audio options
940  *  \param dst Destination
941  *  \param src Source
942  */
943 
944 GAVL_PUBLIC
945 void gavl_audio_options_copy(gavl_audio_options_t * dst,
946                              const gavl_audio_options_t * src);
947 
948 /*! \ingroup audio_options
949  *  \brief Destroy audio options
950  *  \param opt Audio options
951  */
952 
953 GAVL_PUBLIC
954 void gavl_audio_options_destroy(gavl_audio_options_t * opt);
955 
956 
957 
958 /* Audio converter */
959 
960 /** \defgroup audio_converter Audio converter
961     \ingroup audio
962     \brief Audio format converter.
963 
964     This is a generic converter, which converts audio frames from one arbitrary format to
965     another. It does:
966 
967     - Up-/Downmixing of channel configurations
968     - Resampling
969     - Conversion of interleave modes
970     - Conversion of sample formats
971 
972     Quality levels below 3 mainly result if poor but fast resampling methods.
973     Quality levels above 3 will enable high quality resampling methods,
974     dithering and floating point mixing.
975 
976     Create an audio converter with \ref gavl_audio_converter_create. If you want to configure it,
977     get the options pointer with \ref gavl_audio_converter_get_options and change the options
978     (See \ref audio_options).
979     Call \ref gavl_audio_converter_init to initialize the converter for the input and output
980     formats. Audio frames are then converted with \ref gavl_audio_convert.
981 
982     When you are done, you can either reinitialize the converter or destroy it with
983     \ref gavl_audio_converter_destroy.
984 
985 */
986 
987 /*! \ingroup audio_converter
988  *  \brief Opaque audio converter structure
989  *
990  * You don't want to know what's inside.
991  */
992 
993 typedef struct gavl_audio_converter_s gavl_audio_converter_t;
994 
995 /*! \ingroup audio_converter
996  *  \brief Creates an audio converter
997  *  \returns A newly allocated audio converter
998  */
999 
1000 GAVL_PUBLIC
1001 gavl_audio_converter_t * gavl_audio_converter_create();
1002 
1003 /*! \ingroup audio_converter
1004  *  \brief Destroys an audio converter and frees all associated memory
1005  *  \param cnv An audio converter
1006  */
1007 
1008 GAVL_PUBLIC
1009 void gavl_audio_converter_destroy(gavl_audio_converter_t* cnv);
1010 
1011 /*! \ingroup audio_converter
1012  *  \brief gets options of an audio converter
1013  *  \param cnv An audio converter
1014  *
1015  * After you called this, you can use the gavl_audio_options_set_*() functions to change
1016  * the options. Options will become valid with the next call to \ref gavl_audio_converter_init or \ref gavl_audio_converter_reinit
1017  */
1018 
1019 GAVL_PUBLIC
1020 gavl_audio_options_t * gavl_audio_converter_get_options(gavl_audio_converter_t*cnv);
1021 
1022 
1023 /*! \ingroup audio_converter
1024  *  \brief Initialize an audio converter
1025  *  \param cnv An audio converter
1026  *  \param input_format Input format
1027  *  \param output_format Output format
1028  *  \returns The number of single conversion steps necessary to perform the
1029  *           conversion. It may be 0, in this case you must not use the
1030  *           converter and have to pass the audio frames directly.
1031  *           If something goes wrong (should never happen),
1032  *           -1 is returned.
1033  *
1034  * This function can be called multiple times with one instance
1035  */
1036 
1037 GAVL_PUBLIC
1038 int gavl_audio_converter_init(gavl_audio_converter_t* cnv,
1039                               const gavl_audio_format_t * input_format,
1040                               const gavl_audio_format_t * output_format);
1041 
1042 /*! \ingroup audio_converter
1043  *  \brief Initialize an audio converter just for resampling.
1044  *  \param cnv An audio converter
1045  *  \param format Input and output format. (They are the same except for output samplerate ratio will vary)
1046  *  \returns The number of single conversion steps necessary to perform the
1047  *           conversion.  Unlike, gavl_audio_converter_init, it should never
1048  *           return 0. If the sample_format < GALV_SAMPLE_FLOAT extra conversions
1049  *           will be added to the internal conversion chain.
1050  *
1051  * This function can be called multiple times with one instance
1052  *
1053  * Since 1.1.0.
1054  */
1055 
1056 GAVL_PUBLIC
1057 int gavl_audio_converter_init_resample(gavl_audio_converter_t * cnv,
1058                                    const gavl_audio_format_t * format);
1059 
1060 /*! \ingroup audio_converter
1061  *  \brief Reinitialize an audio converter
1062  *  \param cnv An audio converter
1063  *  \returns The number of single conversion steps necessary to perform the
1064  *           conversion. It may be 0, in this case you must not use the
1065  *           converter and have to pass the audio frames directly.
1066  *           If something goes wrong (should never happen),
1067  *           -1 is returned.
1068  *
1069  * This function can be called if the input and output formats didn't
1070  * change but the options did.
1071  */
1072 
1073 
1074 GAVL_PUBLIC
1075 int gavl_audio_converter_reinit(gavl_audio_converter_t* cnv);
1076 
1077 
1078 /*! \ingroup audio_converter
1079  *  \brief Convert audio
1080  *  \param cnv An audio converter
1081  *  \param input_frame Input frame
1082  *  \param output_frame Output frame
1083  *
1084  *  Be careful when resampling: gavl will
1085  *  assume, that your output frame is big enough.
1086  *  Minimum size is
1087  *  input_frame_size * output_samplerate / input_samplerate + 10
1088  *
1089  */
1090 
1091 GAVL_PUBLIC
1092 void gavl_audio_convert(gavl_audio_converter_t * cnv,
1093                         const gavl_audio_frame_t * input_frame,
1094                         gavl_audio_frame_t * output_frame);
1095 
1096 
1097 /*! \ingroup audio_converter
1098  *  \brief Set samplerate converstion ratio
1099  *  \param cnv An resample only audio converter created with gavl_audio_converter_init_resample
1100  *  \param ratio  desired src_ratio
1101  *
1102  *	 Max ratio = 256, Min ratio = 1/256 as defined by libsamplerate
1103  *	 You should set this function before you call gavl_audio_converter_resample
1104  *	 for the very first time.  If you call this function continually before subsequent
1105  *	 calls to gavl_audio_converter_resample, the internal libsamplrate will step sample
1106  *	 rate conversion up to the new ratio on your output audio frame in
1107  *	 gavl_audio_converter_resample.  If you do NOT call this
1108  *	 function before gavl_audio_converter_resample libsamplerate will linearly ramp
1109  *	 the previous src_ratio to the new ratio for the given output audio frame in
1110  *	 gavl_audio_converter_resample.
1111  *
1112  * Since 1.1.0.
1113  */
1114 
1115 GAVL_PUBLIC
1116 int gavl_audio_converter_set_resample_ratio(gavl_audio_converter_t * cnv,
1117 		double ratio ) ;
1118 
1119 
1120 /*! \ingroup audio_converter
1121  *  \brief Convert audio
1122  *  \param cnv An audio converter
1123  *  \param input_frame Input frame
1124  *  \param output_frame Output frame
1125  *  \param ratio  src_ratio  (output_frame.samplerate = ratio * input_frame.samplerate)
1126  *
1127  *  Be careful when resampling: gavl will
1128  *  assume, that your output frame is big enough.
1129  *  Minimum size for output_frame_size =
1130  *  input_frame_size * ratio  (where ratio can be max 256.0 as defined by libsamplerate)
1131  *
1132  * Since 1.1.0.
1133  */
1134 
1135 GAVL_PUBLIC
1136 void gavl_audio_converter_resample(gavl_audio_converter_t * cnv,
1137                               gavl_audio_frame_t * input_frame,
1138                               gavl_audio_frame_t * output_frame,
1139                               double ratio);
1140 
1141 
1142 /** \defgroup volume_control Volume control
1143     \ingroup audio
1144     \brief Simple volume control
1145 
1146     This is a very simple software volume control.
1147 */
1148 
1149 /*! \ingroup volume_control
1150  *  \brief Opaque structure for a volume control
1151  *
1152  * You don't want to know what's inside.
1153  */
1154 
1155 typedef struct gavl_volume_control_s gavl_volume_control_t;
1156 
1157 /* Create / destroy */
1158 
1159 /*! \ingroup volume_control
1160  *  \brief Create volume control
1161  *  \returns A newly allocated volume control
1162  */
1163 
1164 GAVL_PUBLIC
1165 gavl_volume_control_t * gavl_volume_control_create();
1166 
1167 /*! \ingroup volume_control
1168  *  \brief Destroys a volume control and frees all associated memory
1169  *  \param ctrl A volume control
1170  */
1171 
1172 GAVL_PUBLIC
1173 void gavl_volume_control_destroy(gavl_volume_control_t *ctrl);
1174 
1175 /*! \ingroup volume_control
1176  *  \brief Set format for a volume control
1177  *  \param ctrl A volume control
1178  *  \param format The format subsequent frames will be passed with
1179  * This function can be called multiple times with one instance
1180  */
1181 
1182 GAVL_PUBLIC
1183 void gavl_volume_control_set_format(gavl_volume_control_t *ctrl,
1184                                     const gavl_audio_format_t * format);
1185 
1186 /*! \ingroup volume_control
1187  *  \brief Set volume for a volume control
1188  *  \param ctrl A volume control
1189  *  \param volume Volume in dB (must be <= 0.0 to prevent overflows)
1190  */
1191 
1192 GAVL_PUBLIC
1193 void gavl_volume_control_set_volume(gavl_volume_control_t * ctrl,
1194                                     float volume);
1195 
1196 /*! \ingroup volume_control
1197  *  \brief Apply a volume control for an audio frame
1198  *  \param ctrl A volume control
1199  *  \param frame An audio frame
1200  */
1201 
1202 GAVL_PUBLIC
1203 void gavl_volume_control_apply(gavl_volume_control_t *ctrl,
1204                                gavl_audio_frame_t * frame);
1205 
1206 /** \defgroup peak_detection Peak detector
1207     \ingroup audio
1208     \brief Detect peaks in the volume for steering normalizers and
1209       dynamic range compressors
1210 
1211     While normalizers and dynamic range controls are out of the scope
1212     of gavl, some low-level functionality can be provided
1213 */
1214 
1215 /*! \ingroup peak_detection
1216  *  \brief Opaque structure for peak detector
1217  *
1218  * You don't want to know what's inside.
1219  */
1220 
1221 typedef struct gavl_peak_detector_s gavl_peak_detector_t;
1222 
1223 /* Create / destroy */
1224 
1225 /*! \ingroup peak_detection
1226  *  \brief Create peak detector
1227  *  \returns A newly allocated peak detector
1228  */
1229 
1230 GAVL_PUBLIC
1231 gavl_peak_detector_t * gavl_peak_detector_create();
1232 
1233 /*! \ingroup peak_detection
1234  *  \brief Destroys a peak detector and frees all associated memory
1235  *  \param pd A peak detector
1236  */
1237 
1238 GAVL_PUBLIC
1239 void gavl_peak_detector_destroy(gavl_peak_detector_t *pd);
1240 
1241 /*! \ingroup peak_detection
1242  *  \brief Set format for a peak detector
1243  *  \param pd A peak detector
1244  *  \param format The format subsequent frames will be passed with
1245  *
1246  * This function can be called multiple times with one instance. It also
1247  * calls \ref gavl_peak_detector_reset.
1248  */
1249 
1250 GAVL_PUBLIC
1251 void gavl_peak_detector_set_format(gavl_peak_detector_t *pd,
1252                                    const gavl_audio_format_t * format);
1253 
1254 /*! \ingroup peak_detection
1255  *  \brief Feed the peak detector with a new frame
1256  *  \param pd A peak detector
1257  *  \param frame An audio frame
1258  */
1259 
1260 GAVL_PUBLIC
1261 void gavl_peak_detector_update(gavl_peak_detector_t *pd,
1262                               gavl_audio_frame_t * frame);
1263 
1264 /*! \ingroup peak_detection
1265  *  \brief Get the peak volume across all channels
1266  *  \param pd A peak detector
1267  *  \param min Returns minimum amplitude
1268  *  \param max Returns maximum amplitude
1269  *  \param abs Returns maximum absolute amplitude
1270  *
1271  *  The returned amplitudes are normalized such that the
1272  *  minimum amplitude corresponds to -1.0, the maximum amplitude
1273  *  corresponds to 1.0.
1274  */
1275 
1276 GAVL_PUBLIC
1277 void gavl_peak_detector_get_peak(gavl_peak_detector_t * pd,
1278                                  double * min, double * max,
1279                                  double * abs);
1280 
1281 /*! \ingroup peak_detection
1282  *  \brief Get the peak volume for all channels separate
1283  *  \param pd A peak detector
1284  *  \param min Returns minimum amplitude
1285  *  \param max Returns maximum amplitude
1286  *  \param abs Returns maximum absolute amplitude
1287  *
1288  *  The returned amplitudes are normalized such that the
1289  *  minimum amplitude corresponds to -1.0, the maximum amplitude
1290  *  corresponds to 1.0.
1291  */
1292 
1293 GAVL_PUBLIC
1294 void gavl_peak_detector_get_peaks(gavl_peak_detector_t * pd,
1295                                   double * min, double * max,
1296                                   double * abs);
1297 
1298 /*! \ingroup peak_detection
1299  *  \brief Reset a peak detector
1300  *  \param pd A peak detector
1301  */
1302 
1303 GAVL_PUBLIC
1304 void gavl_peak_detector_reset(gavl_peak_detector_t * pd);
1305 
1306 /** \defgroup video Video
1307  *
1308  * Video support
1309  */
1310 
1311 /*! Maximum number of planes
1312  * \ingroup video
1313  */
1314 
1315 #define GAVL_MAX_PLANES 4 /*!< Maximum number of planes */
1316 
1317 /** \defgroup rectangle Rectangles
1318  * \ingroup video
1319  *
1320  * Define rectangular areas in a video frame
1321  */
1322 
1323 /*! Integer rectangle
1324  * \ingroup rectangle
1325  */
1326 
1327 typedef struct
1328   {
1329   int x; /*!< Horizontal offset from the left border of the frame */
1330   int y; /*!< Vertical offset from the top border of the frame */
1331   int w; /*!< Width */
1332   int h; /*!< Height */
1333   } gavl_rectangle_i_t;
1334 
1335 /*! Floating point rectangle
1336  * \ingroup rectangle
1337  */
1338 
1339 typedef struct
1340   {
1341   double x; /*!< Horizontal offset from the left border of the frame */
1342   double y; /*!< Vertical offset from the top border of the frame */
1343   double w; /*!< Width */
1344   double h; /*!< Height */
1345   } gavl_rectangle_f_t;
1346 
1347 /*! \brief Crop an integer rectangle so it fits into the image size of a video format
1348  * \ingroup rectangle
1349  * \param r An integer rectangle
1350  * \param format The video format into which the rectangle must fit
1351  */
1352 
1353 GAVL_PUBLIC
1354 void gavl_rectangle_i_crop_to_format(gavl_rectangle_i_t * r,
1355                                    const gavl_video_format_t * format);
1356 
1357 /*! \brief Crop a floating point rectangle so it fits into the image size of a video format
1358  * \ingroup rectangle
1359  * \param r A floating point rectangle
1360  * \param format The video format into which the rectangle must fit
1361  */
1362 
1363 GAVL_PUBLIC
1364 void gavl_rectangle_f_crop_to_format(gavl_rectangle_f_t * r,
1365                                      const gavl_video_format_t * format);
1366 
1367 /*! \brief Set 2 rectangles as source and destination when no scaling is available
1368  * \ingroup rectangle
1369  * \param src_rect Source rectangle
1370  * \param dst_rect Destination rectangle
1371  * \param src_format Source format
1372  * \param dst_format Destination format
1373  *
1374  * This shrinks src_rect and dest_rect that neither is outside the image
1375  * boundaries of the format. Both rectangles will have the same dimensions.
1376  *
1377  * This function can be used for fitting a video image into a window
1378  * for the case, that no scaling is available.
1379  */
1380 
1381 GAVL_PUBLIC
1382 void gavl_rectangle_crop_to_format_noscale(gavl_rectangle_i_t * src_rect,
1383                                            gavl_rectangle_i_t * dst_rect,
1384                                            const gavl_video_format_t * src_format,
1385                                            const gavl_video_format_t * dst_format);
1386 
1387 /*! \brief Crop 2 rectangles to their formats when scaling is available
1388  * \ingroup rectangle
1389  * \param src_rect Source rectangle
1390  * \param dst_rect Destination rectangle
1391  * \param src_format Source format
1392  * \param dst_format Destination format
1393  *
1394  * This shrinks src_rect and dest_rect that neither is outside the image
1395  * boundaries of the format.
1396  */
1397 
1398 GAVL_PUBLIC
1399 void gavl_rectangle_crop_to_format_scale(gavl_rectangle_f_t * src_rect,
1400                                          gavl_rectangle_i_t * dst_rect,
1401                                          const gavl_video_format_t * src_format,
1402                                          const gavl_video_format_t * dst_format);
1403 
1404 
1405 
1406 /*! \brief Let an integer rectangle span the whole image size of a video format
1407  * \ingroup rectangle
1408  * \param r An integer rectangle
1409  * \param format The video format into which the rectangle must fit
1410  */
1411 
1412 GAVL_PUBLIC
1413 void gavl_rectangle_i_set_all(gavl_rectangle_i_t * r, const gavl_video_format_t * format);
1414 
1415 /*! \brief Let a float rectangle span the whole image size of a video format
1416  * \ingroup rectangle
1417  * \param r A float rectangle
1418  * \param format The video format into which the rectangle must fit
1419  */
1420 
1421 GAVL_PUBLIC
1422 void gavl_rectangle_f_set_all(gavl_rectangle_f_t * r, const gavl_video_format_t * format);
1423 
1424 /*! \brief Crop an integer rectangle by some pixels from the left border
1425  * \ingroup rectangle
1426  * \param r An integer rectangle
1427  * \param num_pixels The number of pixels by which the rectangle gets smaller
1428  */
1429 
1430 GAVL_PUBLIC
1431 void gavl_rectangle_i_crop_left(gavl_rectangle_i_t * r,   int num_pixels);
1432 
1433 /*! \brief Crop an integer rectangle by some pixels from the right border
1434  * \ingroup rectangle
1435  * \param r An integer rectangle
1436  * \param num_pixels The number of pixels by which the rectangle gets smaller
1437  */
1438 
1439 GAVL_PUBLIC
1440 void gavl_rectangle_i_crop_right(gavl_rectangle_i_t * r,  int num_pixels);
1441 
1442 /*! \brief Crop an integer rectangle by some pixels from the top border
1443  * \ingroup rectangle
1444  * \param r An integer rectangle
1445  * \param num_pixels The number of pixels by which the rectangle gets smaller
1446  */
1447 
1448 GAVL_PUBLIC
1449 void gavl_rectangle_i_crop_top(gavl_rectangle_i_t * r,    int num_pixels);
1450 
1451 /*! \brief Crop an integer rectangle by some pixels from the bottom border
1452  * \ingroup rectangle
1453  * \param r An integer rectangle
1454  * \param num_pixels The number of pixels by which the rectangle gets smaller
1455  */
1456 
1457 GAVL_PUBLIC
1458 void gavl_rectangle_i_crop_bottom(gavl_rectangle_i_t * r, int num_pixels);
1459 
1460 /*! \brief Crop a float rectangle by some pixels from the left border
1461  * \ingroup rectangle
1462  * \param r A float rectangle
1463  * \param num_pixels The number of pixels by which the rectangle gets smaller
1464  */
1465 
1466 GAVL_PUBLIC
1467 void gavl_rectangle_f_crop_left(gavl_rectangle_f_t * r,   double num_pixels);
1468 
1469 /*! \brief Crop a float rectangle by some pixels from the right border
1470  * \ingroup rectangle
1471  * \param r A float rectangle
1472  * \param num_pixels The number of pixels by which the rectangle gets smaller
1473  */
1474 
1475 GAVL_PUBLIC
1476 void gavl_rectangle_f_crop_right(gavl_rectangle_f_t * r,  double num_pixels);
1477 
1478 /*! \brief Crop a float rectangle by some pixels from the top border
1479  * \ingroup rectangle
1480  * \param r A float rectangle
1481  * \param num_pixels The number of pixels by which the rectangle gets smaller
1482  */
1483 
1484 GAVL_PUBLIC
1485 void gavl_rectangle_f_crop_top(gavl_rectangle_f_t * r,    double num_pixels);
1486 
1487 /*! \brief Crop a float rectangle by some pixels from the bottom border
1488  * \ingroup rectangle
1489  * \param r A float rectangle
1490  * \param num_pixels The number of pixels by which the rectangle gets smaller
1491  */
1492 
1493 GAVL_PUBLIC
1494 void gavl_rectangle_f_crop_bottom(gavl_rectangle_f_t * r, double num_pixels);
1495 
1496 /*! \brief Align a rectangle
1497  * \ingroup rectangle
1498  * \param r An integer rectangle
1499  * \param h_align Horizontal alignment
1500  * \param v_align Vertical alignment
1501  *
1502  * This aligns a rectangle such that the horizontal and vertical coordinates
1503  * are multiples of h_align and v_align respectively. When dealing
1504  * with chroma subsampled formats, you must call this function with the
1505  * return values of \ref gavl_pixelformat_chroma_sub before taking subframes from
1506  * video frames.
1507  */
1508 
1509 GAVL_PUBLIC
1510 void gavl_rectangle_i_align(gavl_rectangle_i_t * r, int h_align, int v_align);
1511 
1512 /*! \brief Align a rectangle to a format
1513  * \ingroup rectangle
1514  * \param r An integer rectangle
1515  * \param format A video format
1516  *
1517  * The convenience function does the same as \ref gavl_rectangle_i_align
1518  * but takes a format as argument.
1519  */
1520 
1521 GAVL_PUBLIC
1522 void gavl_rectangle_i_align_to_format(gavl_rectangle_i_t * r,
1523                                       const gavl_video_format_t * format);
1524 
1525 
1526 /*! \brief Copy an integer rectangle
1527  * \ingroup rectangle
1528  * \param dst Destination rectangle
1529  * \param src Source rectangle
1530  */
1531 
1532 GAVL_PUBLIC
1533 void gavl_rectangle_i_copy(gavl_rectangle_i_t * dst, const gavl_rectangle_i_t * src);
1534 
1535 /*! \brief Copy a float rectangle
1536  * \ingroup rectangle
1537  * \param dst Destination rectangle
1538  * \param src Source rectangle
1539  */
1540 
1541 GAVL_PUBLIC
1542 void gavl_rectangle_f_copy(gavl_rectangle_f_t * dst, const gavl_rectangle_f_t * src);
1543 
1544 
1545 
1546 /*! \brief Convert an integer rectangle to a floating point rectangle
1547  * \ingroup rectangle
1548  * \param dst Destination rectangle
1549  * \param src Source rectangle
1550  */
1551 
1552 GAVL_PUBLIC
1553 void gavl_rectangle_i_to_f(gavl_rectangle_f_t * dst, const gavl_rectangle_i_t * src);
1554 
1555 /*! \brief Convert a floating point rectangle to an integer rectangle
1556  * \ingroup rectangle
1557  * \param dst Destination rectangle
1558  * \param src Source rectangle
1559  */
1560 
1561 GAVL_PUBLIC
1562 void gavl_rectangle_f_to_i(gavl_rectangle_i_t * dst, const gavl_rectangle_f_t * src);
1563 
1564 /*! \brief Check if an integer rectangle is empty
1565  * \ingroup rectangle
1566  * \param r Rectangle
1567  * \returns 1 if the rectangle is empty, 0 else.
1568  *
1569  * A rectangle is considered to be empty if the width or height are <= 0.
1570  */
1571 
1572 GAVL_PUBLIC
1573 int gavl_rectangle_i_is_empty(const gavl_rectangle_i_t * r);
1574 
1575 /*! \brief Check if a float rectangle is empty
1576  * \ingroup rectangle
1577  * \param r Rectangle
1578  * \returns 1 if the rectangle is empty, 0 else.
1579  *
1580  * A rectangle is considered to be empty if the width or height are <= 0.
1581  */
1582 
1583 GAVL_PUBLIC
1584 int gavl_rectangle_f_is_empty(const gavl_rectangle_f_t * r);
1585 
1586 /*!\brief Calculate a destination rectangle for scaling
1587  * \ingroup rectangle
1588  * \param dst_rect Destination rectangle
1589  * \param src_format Source format
1590  * \param src_rect Source rectangle
1591  * \param dst_format Destination format
1592  * \param zoom Zoom factor
1593  * \param squeeze Squeeze factor
1594  *
1595  * Assuming we take src_rect from a frame in src_format,
1596  * calculate the optimal dst_rect in dst_format. The source and destination
1597  * display aspect ratio will be preserved unless it is changed with the squeeze
1598  * parameter.
1599  *
1600  * Zoom is a zoom factor (1.0 = 100 %). Squeeze is a value between -1.0 and 1.0,
1601  * which changes the apsect ratio in both directions. 0.0 means unchanged.
1602  *
1603  * Note that dst_rect might be outside the image dimensions of dst_format. If you
1604  * don't like this, call \ref gavl_rectangle_crop_to_format_scale afterwards.
1605  *
1606  * Furthermore, the chroma subsampling is ignored by this function. If you you use
1607  * the rectangles to fire up a \ref gavl_video_scaler_t, this is no problem (the
1608  * scaler will align the rectangles internally). You can align the destination
1609  * rectangle manually using \ref gavl_rectangle_i_align or
1610  * \ref gavl_rectangle_i_align_to_format.
1611  */
1612 
1613 GAVL_PUBLIC
1614 void gavl_rectangle_fit_aspect(gavl_rectangle_i_t * dst_rect,
1615                                const gavl_video_format_t * src_format,
1616                                const gavl_rectangle_f_t * src_rect,
1617                                const gavl_video_format_t * dst_format,
1618                                float zoom, float squeeze);
1619 
1620 /*! \brief Dump a rectangle to stderr
1621  * \ingroup rectangle
1622  * \param r Rectangle
1623  */
1624 GAVL_PUBLIC
1625 void gavl_rectangle_i_dump(const gavl_rectangle_i_t * r);
1626 
1627 /*! \brief Dump a floating point rectangle to stderr
1628  * \ingroup rectangle
1629  * \param r Floating point rectangle
1630  */
1631 GAVL_PUBLIC
1632 void gavl_rectangle_f_dump(const gavl_rectangle_f_t * r);
1633 
1634 
1635 /** \defgroup video_format Video format definitions
1636  * \ingroup video
1637  *
1638  * \brief Definitions for several variations of video data
1639  */
1640 
1641 /** \ingroup video_format
1642  * Flag for planar pixelformats
1643  */
1644 #define GAVL_PIXFMT_PLANAR (1<<8)
1645 
1646 /** \ingroup video_format
1647  * Flag for rgb pixelformats
1648  */
1649 #define GAVL_PIXFMT_RGB    (1<<9)
1650 
1651 /** \ingroup video_format
1652  * Flag for yuv pixelformats
1653  */
1654 #define GAVL_PIXFMT_YUV    (1<<10)
1655 
1656 /** \ingroup video_format
1657  * Flag for yuvj pixelformats
1658  */
1659 #define GAVL_PIXFMT_YUVJ   (1<<11)
1660 
1661 /** \ingroup video_format
1662  * Alpha flag
1663  */
1664 #define GAVL_PIXFMT_ALPHA  (1<<12)
1665 
1666   /** \ingroup video_format
1667  * Flag for grayscale pixelformats
1668  */
1669 #define GAVL_PIXFMT_GRAY   (1<<13)
1670 
1671 /*! \ingroup video_format
1672  * \brief Pixelformat definition
1673  */
1674 
1675 typedef enum
1676   {
1677     /*! \brief Undefined
1678      */
1679     GAVL_PIXELFORMAT_NONE =  0,
1680 
1681     /*! 8 bit gray, scaled 0x00..0xff
1682      */
1683     GAVL_GRAY_8          =  1 | GAVL_PIXFMT_GRAY,
1684 
1685     /*! 16 bit gray, scaled 0x0000..0xffff
1686      */
1687     GAVL_GRAY_16          =  2 | GAVL_PIXFMT_GRAY,
1688 
1689     /*! floating point gray, scaled 0.0..1.0
1690      */
1691     GAVL_GRAY_FLOAT       =  3 | GAVL_PIXFMT_GRAY,
1692 
1693     /*! 8 bit gray + alpha, scaled 0x00..0xff
1694      */
1695     GAVL_GRAYA_16          =  1 | GAVL_PIXFMT_GRAY | GAVL_PIXFMT_ALPHA,
1696 
1697     /*! 16 bit gray + alpha, scaled 0x0000..0xffff
1698      */
1699     GAVL_GRAYA_32          =  2 | GAVL_PIXFMT_GRAY | GAVL_PIXFMT_ALPHA,
1700 
1701     /*! floating point gray + alpha, scaled 0.0..1.0
1702      */
1703     GAVL_GRAYA_FLOAT       =  3 | GAVL_PIXFMT_GRAY | GAVL_PIXFMT_ALPHA,
1704 
1705     /*! 15 bit RGB. Each pixel is a uint16_t in native byte order. Color masks are:
1706      * for red: 0x7C00, for green: 0x03e0, for blue: 0x001f
1707      */
1708     GAVL_RGB_15          =  1 | GAVL_PIXFMT_RGB,
1709     /*! 15 bit BGR. Each pixel is a uint16_t in native byte order. Color masks are:
1710      * for red: 0x001f, for green: 0x03e0, for blue: 0x7C00
1711      */
1712     GAVL_BGR_15          =  2 | GAVL_PIXFMT_RGB,
1713     /*! 16 bit RGB. Each pixel is a uint16_t in native byte order. Color masks are:
1714      * for red: 0xf800, for green: 0x07e0, for blue: 0x001f
1715      */
1716     GAVL_RGB_16          =  3 | GAVL_PIXFMT_RGB,
1717     /*! 16 bit BGR. Each pixel is a uint16_t in native byte order. Color masks are:
1718      * for red: 0x001f, for green: 0x07e0, for blue: 0xf800
1719      */
1720     GAVL_BGR_16          =  4 | GAVL_PIXFMT_RGB,
1721     /*! 24 bit RGB. Each color is an uint8_t. Color order is RGBRGB
1722      */
1723     GAVL_RGB_24          =  5 | GAVL_PIXFMT_RGB,
1724     /*! 24 bit BGR. Each color is an uint8_t. Color order is BGRBGR
1725      */
1726     GAVL_BGR_24          =  6 | GAVL_PIXFMT_RGB,
1727     /*! 32 bit RGB. Each color is an uint8_t. Color order is RGBXRGBX, where X is unused
1728      */
1729     GAVL_RGB_32          =  7 | GAVL_PIXFMT_RGB,
1730     /*! 32 bit BGR. Each color is an uint8_t. Color order is BGRXBGRX, where X is unused
1731      */
1732     GAVL_BGR_32          =  8 | GAVL_PIXFMT_RGB,
1733     /*! 32 bit RGBA. Each color is an uint8_t. Color order is RGBARGBA
1734      */
1735     GAVL_RGBA_32         =  9 | GAVL_PIXFMT_RGB | GAVL_PIXFMT_ALPHA,
1736 
1737     /*! 48 bit RGB. Each color is an uint16_t in native byte order. Color order is RGBRGB
1738      */
1739     GAVL_RGB_48       = 10 | GAVL_PIXFMT_RGB,
1740     /*! 64 bit RGBA. Each color is an uint16_t in native byte order. Color order is RGBARGBA
1741      */
1742     GAVL_RGBA_64      = 11 | GAVL_PIXFMT_RGB | GAVL_PIXFMT_ALPHA,
1743 
1744     /*! float RGB. Each color is a float (0.0 .. 1.0) in native byte order. Color order is RGBRGB
1745      */
1746     GAVL_RGB_FLOAT    = 12 | GAVL_PIXFMT_RGB,
1747     /*! float RGBA. Each color is a float (0.0 .. 1.0) in native byte order. Color order is RGBARGBA
1748      */
1749     GAVL_RGBA_FLOAT   = 13 | GAVL_PIXFMT_RGB  | GAVL_PIXFMT_ALPHA,
1750 
1751     /*! Packed YCbCr 4:2:2. Each component is an uint8_t. Component order is Y1 U1 Y2 V1
1752      */
1753     GAVL_YUY2            = 1 | GAVL_PIXFMT_YUV,
1754     /*! Packed YCbCr 4:2:2. Each component is an uint8_t. Component order is U1 Y1 V1 Y2
1755      */
1756     GAVL_UYVY            = 2 | GAVL_PIXFMT_YUV,
1757     /*! Packed YCbCrA 4:4:4:4. Each component is an uint8_t. Component order is YUVA. Luma and chroma are video scaled, alpha is 0..255. */
1758 
1759     GAVL_YUVA_32         = 3 | GAVL_PIXFMT_YUV | GAVL_PIXFMT_ALPHA,
1760     /*! Packed YCbCrA 4:4:4:4. Each component is an uint16_t. Component order is YUVA. Luma and chroma are video scaled, alpha is 0..65535. */
1761 
1762     GAVL_YUVA_64         = 4 | GAVL_PIXFMT_YUV | GAVL_PIXFMT_ALPHA,
1763     /*!
1764      * Packed YCbCr 4:4:4. Each component is a float. Luma is scaled 0.0..1.0, chroma is -0.5..0.5 */
1765     GAVL_YUV_FLOAT       = 5 | GAVL_PIXFMT_YUV,
1766 
1767     /*! Packed YCbCrA 4:4:4:4. Each component is a float. Luma is scaled 0.0..1.0, chroma is -0.5..0.5
1768      */
1769     GAVL_YUVA_FLOAT       = 6 | GAVL_PIXFMT_YUV | GAVL_PIXFMT_ALPHA,
1770 
1771     /*! Packed YCbCrA 4:4:4:4. Each component is an uint16_t. Component order is YUVA. Luma and chroma are video scaled, alpha is 0..65535.
1772      */
1773 
1774     GAVL_YUV_420_P       = 1 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV,
1775     /*! Planar YCbCr 4:2:2. Each component is an uint8_t
1776      */
1777     GAVL_YUV_422_P       = 2 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV,
1778     /*! Planar YCbCr 4:4:4. Each component is an uint8_t
1779      */
1780     GAVL_YUV_444_P       = 3 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV,
1781     /*! Planar YCbCr 4:1:1. Each component is an uint8_t
1782      */
1783     GAVL_YUV_411_P       = 4 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV,
1784     /*! Planar YCbCr 4:1:0. Each component is an uint8_t
1785      */
1786     GAVL_YUV_410_P       = 5 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV,
1787 
1788     /*! Planar YCbCr 4:2:0. Each component is an uint8_t, luma and chroma values are full range (0x00 .. 0xff)
1789      */
1790     GAVL_YUVJ_420_P      = 6 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV | GAVL_PIXFMT_YUVJ,
1791     /*! Planar YCbCr 4:2:2. Each component is an uint8_t, luma and chroma values are full range (0x00 .. 0xff)
1792      */
1793     GAVL_YUVJ_422_P      = 7 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV | GAVL_PIXFMT_YUVJ,
1794     /*! Planar YCbCr 4:4:4. Each component is an uint8_t, luma and chroma values are full range (0x00 .. 0xff)
1795      */
1796     GAVL_YUVJ_444_P      = 8 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV | GAVL_PIXFMT_YUVJ,
1797 
1798     /*! 16 bit Planar YCbCr 4:4:4. Each component is an uint16_t in native byte order.
1799      */
1800     GAVL_YUV_444_P_16 = 9 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV,
1801     /*! 16 bit Planar YCbCr 4:2:2. Each component is an uint16_t in native byte order.
1802      */
1803     GAVL_YUV_422_P_16 = 10 | GAVL_PIXFMT_PLANAR | GAVL_PIXFMT_YUV,
1804 
1805   } gavl_pixelformat_t;
1806 
1807 /*! \ingroup video_format
1808  *  \brief Pixelformat for storing 1-dimensional integer data with 8 bits each */
1809 #define GAVL_PIXELFORMAT_1D_8 GAVL_GRAY_8
1810 /*! \ingroup video_format
1811  *  \brief Pixelformat for storing 2-dimensional integer data with 8 bits each */
1812 #define GAVL_PIXELFORMAT_2D_8 GAVL_GRAYA_16
1813 /*! \ingroup video_format
1814  *  \brief Pixelformat for storing 3-dimensional integer data with 8 bits each */
1815 #define GAVL_PIXELFORMAT_3D_8 GAVL_RGB_24
1816 /*! \ingroup video_format
1817  *  \brief Pixelformat for storing 4-dimensional integer data with 8 bits each */
1818 #define GAVL_PIXELFORMAT_4D_8 GAVL_RGBA_32
1819 
1820 /*! \ingroup video_format
1821  *  \brief Pixelformat for storing 1-dimensional integer data with 16 bits each */
1822 #define GAVL_PIXELFORMAT_1D_16 GAVL_GRAY_16
1823 /*! \ingroup video_format
1824  *  \brief Pixelformat for storing 2-dimensional integer data with 16 bits each */
1825 #define GAVL_PIXELFORMAT_2D_16 GAVL_GRAYA_32
1826 /*! \ingroup video_format
1827  *  \brief Pixelformat for storing 3-dimensional integer data with 16 bits each */
1828 #define GAVL_PIXELFORMAT_3D_16 GAVL_RGB_48
1829 /*! \ingroup video_format
1830  *  \brief Pixelformat for storing 4-dimensional integer data with 16 bits each */
1831 #define GAVL_PIXELFORMAT_4D_16 GAVL_RGBA_64
1832 
1833 /*! \ingroup video_format
1834  *  \brief Pixelformat for storing 1-dimensional FLOAT data */
1835 #define GAVL_PIXELFORMAT_1D_FLOAT GAVL_GRAY_FLOAT
1836 /*! \ingroup video_format
1837  *  \brief Pixelformat for storing 2-dimensional FLOAT data */
1838 #define GAVL_PIXELFORMAT_2D_FLOAT GAVL_GRAYA_FLOAT
1839 /*! \ingroup video_format
1840  *  \brief Pixelformat for storing 3-dimensional FLOAT data */
1841 #define GAVL_PIXELFORMAT_3D_FLOAT GAVL_RGB_FLOAT
1842 /*! \ingroup video_format
1843  *  \brief Pixelformat for storing 4-dimensional FLOAT data */
1844 #define GAVL_PIXELFORMAT_4D_FLOAT GAVL_RGBA_FLOAT
1845 
1846 /*! \ingroup video_format
1847  *  \brief Color channel definitions
1848  *
1849  *  Enum definitions for color channels
1850  */
1851 
1852 typedef enum
1853   {
1854     GAVL_CCH_RED,    //!< Red
1855     GAVL_CCH_GREEN,  //!< Green
1856     GAVL_CCH_BLUE,   //!< Blue
1857     GAVL_CCH_Y,      //!< Luminance (also grayscale)
1858     GAVL_CCH_CB,     //!< Chrominance blue (aka U)
1859     GAVL_CCH_CR,     //!< Chrominance red (aka V)
1860     GAVL_CCH_ALPHA,  //!< Transparency (or, to be more precise opacity)
1861   } gavl_color_channel_t;
1862 
1863 /*
1864  *  Colormodel related functions
1865  */
1866 
1867 /*! \ingroup video_format
1868  * \brief Check if a pixelformat is grayscale
1869  * \param fmt A pixelformat
1870  * \returns 1 if the pixelformat is grayscale, 0 else
1871  */
1872 
1873 #define gavl_pixelformat_is_gray(fmt) ((fmt) & GAVL_PIXFMT_GRAY)
1874 
1875 
1876 /*! \ingroup video_format
1877  * \brief Check if a pixelformat is RGB based
1878  * \param fmt A pixelformat
1879  * \returns 1 if the pixelformat is RGB based, 0 else
1880  */
1881 
1882 #define gavl_pixelformat_is_rgb(fmt) ((fmt) & GAVL_PIXFMT_RGB)
1883 
1884 /*! \ingroup video_format
1885  * \brief Check if a pixelformat is YUV based
1886  * \param fmt A pixelformat
1887  * \returns 1 if the pixelformat is YUV based, 0 else
1888  */
1889 
1890 #define gavl_pixelformat_is_yuv(fmt) ((fmt) & GAVL_PIXFMT_YUV)
1891 
1892 /*! \ingroup video_format
1893  * \brief Check if a pixelformat is jpeg (full range) scaled
1894  * \param fmt A pixelformat
1895  * \returns 1 if the pixelformat is jpeg scaled, 0 else
1896  */
1897 
1898 #define gavl_pixelformat_is_jpeg_scaled(fmt) ((fmt) & GAVL_PIXFMT_YUVJ)
1899 
1900 /*! \ingroup video_format
1901  * \brief Check if a pixelformat has a transparency channel
1902  * \param fmt A pixelformat
1903  * \returns 1 if the pixelformat has a transparency channel, 0 else
1904  */
1905 
1906 #define gavl_pixelformat_has_alpha(fmt) ((fmt) & GAVL_PIXFMT_ALPHA)
1907 
1908 /*! \ingroup video_format
1909  * \brief Check if a pixelformat is planar
1910  * \param fmt A pixelformat
1911  * \returns 1 if the pixelformat is planar, 0 else
1912  */
1913 
1914 #define  gavl_pixelformat_is_planar(fmt) ((fmt) & GAVL_PIXFMT_PLANAR)
1915 
1916 /*! \ingroup video_format
1917  * \brief Get the number of planes
1918  * \param pixelformat A pixelformat
1919  * \returns The number of planes (1 for packet formats)
1920  */
1921 
1922 GAVL_PUBLIC
1923 int gavl_pixelformat_num_planes(gavl_pixelformat_t pixelformat);
1924 
1925 /*! \ingroup video_format
1926  * \brief Get the horizontal and vertical subsampling factors
1927  * \param pixelformat A pixelformat
1928  * \param sub_h returns the horizontal subsampling factor
1929  * \param sub_v returns the vertical subsampling factor
1930  *
1931  * E.g. for 4:2:0 subsampling: sub_h = 2, sub_v = 2
1932  */
1933 
1934 GAVL_PUBLIC
1935 void gavl_pixelformat_chroma_sub(gavl_pixelformat_t pixelformat, int * sub_h, int * sub_v);
1936 
1937 /*! \ingroup video_format
1938  * \brief Get bytes per component for planar formats
1939  * \param pixelformat A pixelformat
1940  * \returns The number of bytes per component for planar formats, 0 for packed formats
1941  */
1942 
1943 GAVL_PUBLIC
1944 int gavl_pixelformat_bytes_per_component(gavl_pixelformat_t pixelformat);
1945 
1946 /*! \ingroup video_format
1947  * \brief Get bytes per pixel for packed formats
1948  * \param pixelformat A pixelformat
1949  * \returns The number of bytes per pixel for packed formats, 0 for planar formats
1950  */
1951 
1952 GAVL_PUBLIC
1953 int gavl_pixelformat_bytes_per_pixel(gavl_pixelformat_t pixelformat);
1954 
1955 /*! \ingroup video_format
1956  *  \brief Get the effective number of bits for one pixel
1957  *  \param pixelformat A pixelformat
1958  *  \returns Number of bits per pixel
1959  */
1960 
1961 GAVL_PUBLIC
1962 int gavl_pixelformat_bits_per_pixel(gavl_pixelformat_t pixelformat);
1963 
1964 /*! \ingroup video_format
1965  *  \brief Get the conversion penalty for pixelformat conversions
1966  *  \param src Source pixelformat
1967  *  \param dst Destination pixelformat
1968  *  \returns A number denoting the "cost" of the conversion
1969  *
1970  *  The number (the larger the worse) is calculated from several criteria
1971  *  and considers both speed and quality issues. Don't ever rely on
1972  *  specific absolute values, since they can change from version
1973  *  to version (except 0, which is returned when and only when src and dst
1974  *  are equal). Instead, only compare values returned for different
1975  *  combinations among each other.
1976  */
1977 
1978 GAVL_PUBLIC
1979 int gavl_pixelformat_conversion_penalty(gavl_pixelformat_t src,
1980                                         gavl_pixelformat_t dst);
1981 
1982 /*! \ingroup video_format
1983  *  \brief Get the best destination format for a given source format
1984  *  \param src Source pixelformat
1985  *  \param dst_supported List of supported destination format
1986  *  \param penalty If non-null, returns the conversion penalty
1987  *  \returns The best supported destination pixelformat
1988  *
1989  *  This function takes a source format and a list of supported
1990  *  destination formats (terminated with \ref GAVL_PIXELFORMAT_NONE)
1991  *  and returns the format, which will result in the cheapest conversion
1992  *  (see \ref gavl_pixelformat_conversion_penalty).
1993  */
1994 
1995 GAVL_PUBLIC gavl_pixelformat_t
1996 gavl_pixelformat_get_best(gavl_pixelformat_t src,
1997                           const gavl_pixelformat_t * dst_supported,
1998                           int * penalty);
1999 
2000 
2001 
2002 /*! \ingroup video_format
2003  * \brief Translate a pixelformat into a human readable string
2004  * \param pixelformat A pixelformat
2005  * \returns A string describing the pixelformat
2006  */
2007 
2008 GAVL_PUBLIC
2009 const char * gavl_pixelformat_to_string(gavl_pixelformat_t pixelformat);
2010 
2011 /*! \ingroup video_format
2012  * \brief Translate a pixelformat name into a pixelformat
2013  * \param name A string describing the pixelformat (returnd by \ref gavl_pixelformat_to_string)
2014  * \returns The pixelformat or GAVL_PIXELFORMAT_NONE if no match.
2015  */
2016 
2017 GAVL_PUBLIC
2018 gavl_pixelformat_t gavl_string_to_pixelformat(const char * name);
2019 
2020 /*! \ingroup video_format
2021  * \brief Get total number of supported pixelformats
2022  * \returns total number of supported pixelformats
2023  */
2024 
2025 GAVL_PUBLIC
2026 int gavl_num_pixelformats();
2027 
2028 /*! \ingroup video_format
2029  * \brief Get the pixelformat from index
2030  * \param index index (must be between 0 and the result of \ref gavl_num_pixelformats)
2031  * \returns The pixelformat corresponding to index or GAVL_PIXELFORMAT_NONE.
2032  */
2033 
2034 GAVL_PUBLIC
2035 gavl_pixelformat_t gavl_get_pixelformat(int index);
2036 
2037 /*  */
2038 
2039 /*! \ingroup video_format
2040  * \brief Chroma placement
2041  *
2042  * Specification of the 3 variants of 4:2:0 YCbCr as described at
2043  * http://www.mir.com/DMG/chroma.html . For other pixelformats, it's meaningless
2044  * and should be set to GAVL_CHROMA_PLACEMENT_DEFAULT.
2045  */
2046 
2047 typedef enum
2048   {
2049     GAVL_CHROMA_PLACEMENT_DEFAULT = 0, /*!< MPEG-1/JPEG */
2050     GAVL_CHROMA_PLACEMENT_MPEG2,       /*!< MPEG-2 */
2051     GAVL_CHROMA_PLACEMENT_DVPAL        /*!< DV PAL */
2052   } gavl_chroma_placement_t;
2053 
2054 /*! \ingroup video_format
2055  * \brief Translate a chroma placement into a human readable string
2056  * \param mode A chroma placement
2057  * \returns A string describing the chroma placement
2058  */
2059 
2060 GAVL_PUBLIC
2061 const char * gavl_chroma_placement_to_string(gavl_chroma_placement_t mode);
2062 
2063 /*! \ingroup video_format
2064  * \brief Framerate mode
2065  */
2066 
2067 /* Changing the values alters the gmerlin-avdecoder index format */
2068 
2069 typedef enum
2070   {
2071     GAVL_FRAMERATE_UNKNOWN     = -1, /*!< Unknown (never use in public APIs) */
2072     GAVL_FRAMERATE_CONSTANT    = 0,  /*!< Constant framerate */
2073     GAVL_FRAMERATE_VARIABLE    = 1,  /*!< Variable framerate */
2074     GAVL_FRAMERATE_STILL       = 2,  /*!< Still image */
2075   } gavl_framerate_mode_t;
2076 
2077 /*! \ingroup video_format
2078  * \brief Translate a framerate mode into a human readable string
2079  * \param mode A framerate mode
2080  * \returns A string describing the framerate mode
2081  */
2082 
2083 GAVL_PUBLIC
2084 const char * gavl_framerate_mode_to_string(gavl_framerate_mode_t mode);
2085 
2086 /*! \ingroup video_format
2087  * \brief Interlace mode
2088  */
2089 
2090 /* Changing the values alters the gmerlin-avdecoder index format */
2091 
2092 typedef enum
2093   {
2094     GAVL_INTERLACE_UNKNOWN       = -1,/*!< Unknown interlacing (never use in public APIs) */
2095     GAVL_INTERLACE_NONE          = 0, /*!< Progressive */
2096     GAVL_INTERLACE_TOP_FIRST     = 1, /*!< Top field first */
2097     GAVL_INTERLACE_BOTTOM_FIRST  = 2, /*!< Bottom field first */
2098     GAVL_INTERLACE_MIXED         = 3, /*!< Use interlace_mode of the frames */
2099     GAVL_INTERLACE_MIXED_TOP     = 4, /*!< Progressive + top    */
2100     GAVL_INTERLACE_MIXED_BOTTOM  = 5, /*!< Progressive + bottom */
2101   } gavl_interlace_mode_t;
2102 
2103 /*! \ingroup video_format
2104  * \brief Translate an interlace mode into a human readable string
2105  * \param mode An interlace mode
2106  * \returns A string describing the interlace mode
2107  */
2108 
2109 GAVL_PUBLIC
2110 const char * gavl_interlace_mode_to_string(gavl_interlace_mode_t mode);
2111 
2112 
2113 /* Video format structure */
2114 
2115 /*! \ingroup video_format
2116  * \brief Video format
2117  */
2118 
2119 struct gavl_video_format_s
2120   {
2121   int frame_width;/*!< Width of the frame buffer in pixels, might be larger than image_width */
2122   int frame_height;/*!< Height of the frame buffer in pixels, might be larger than image_height */
2123 
2124   int image_width;/*!< Width of the image in pixels */
2125   int image_height;/*!< Height of the image in pixels */
2126 
2127   /* Support for nonsquare pixels */
2128 
2129   int pixel_width;/*!< Relative width of a pixel (pixel aspect ratio is pixel_width/pixel_height) */
2130   int pixel_height;/*!< Relative height of a pixel (pixel aspect ratio is pixel_width/pixel_height) */
2131 
2132   gavl_pixelformat_t pixelformat;/*!< Pixelformat */
2133 
2134   int frame_duration;/*!< Duration of a frame in timescale tics. Meaningful only if framerate_mode is
2135                        GAVL_FRAMERATE_CONSTANT */
2136   int timescale;/*!< Timescale in tics per second */
2137 
2138   gavl_framerate_mode_t   framerate_mode;/*!< Framerate mode */
2139   gavl_chroma_placement_t chroma_placement;/*!< Chroma placement */
2140 
2141   gavl_interlace_mode_t   interlace_mode;/*!< Interlace mode */
2142 
2143   gavl_timecode_format_t  timecode_format;/*!< Optional timecode format */
2144   };
2145 
2146 /*!
2147   \ingroup video_format
2148   \brief Copy one video format to another
2149   \param dst Destination format
2150   \param src Source format
2151  */
2152 
2153 GAVL_PUBLIC
2154 void gavl_video_format_copy(gavl_video_format_t * dst,
2155                             const gavl_video_format_t * src);
2156 
2157 /*!
2158   \ingroup video_format
2159   \brief Compare 2 video formats
2160   \param format_1 First format
2161   \param format_2 Second format
2162   \returns 1 if the formats are equal, 0 else
2163 */
2164 
2165 GAVL_PUBLIC
2166 int gavl_video_formats_equal(const gavl_video_format_t * format_1,
2167                              const gavl_video_format_t * format_2);
2168 
2169 
2170 /*!
2171   \ingroup video_format
2172   \brief Get the chroma offsets relative to the luma samples
2173   \param format A video format
2174   \param field Index of the field (0 = top, 1 = bottom). For progressive format, this is unused
2175   \param plane Index of the plane (1 = Cb, 2 = Cr)
2176   \param off_x Returns the offset in x-direction
2177   \param off_y Returns the offset in y-direction
2178 */
2179 
2180 GAVL_PUBLIC
2181 void gavl_video_format_get_chroma_offset(const gavl_video_format_t * format, int field, int plane,
2182                                          float * off_x, float * off_y);
2183 
2184 
2185 
2186 /*!
2187   \ingroup video_format
2188   \brief Set the image size of a destination format from a source format
2189   \param dst Destination format
2190   \param src Source format
2191 
2192   Sets the image size of dst according src. Before you call this function,
2193   you must set the pixel_width and pixel_height of dst. This function will
2194   preserve the display aspect ratio, i.e. when the pixel aspect ratios are different
2195   in source and destination, the images will be scaled.
2196  */
2197 
2198 GAVL_PUBLIC
2199 void gavl_video_format_fit_to_source(gavl_video_format_t * dst,
2200                                      const gavl_video_format_t * src);
2201 
2202 /*!
2203   \ingroup video_format
2204   \brief Get the unpadded image size
2205   \param format A video format
2206   \return The image size in bytes of an unpadded frame
2207  */
2208 
2209 GAVL_PUBLIC
2210 int gavl_video_format_get_image_size(const gavl_video_format_t * format);
2211 
2212 /*!
2213   \ingroup video_format
2214   \brief Get the video format for extracting/merging one channel
2215   \param frame_format   The video format of the full frame
2216   \param channel_format Format of the extracted channel (grayscale)
2217   \param ch Channel
2218   \returns 1 on success, 0 if the requested channel is not available in the format
2219 
2220   Use this function in conjunction with
2221   \ref gavl_video_frame_extract_channel and
2222   \ref gavl_video_frame_insert_channel
2223 
2224   Since 1.1.2
2225 */
2226 
2227 GAVL_PUBLIC
2228 int gavl_get_color_channel_format(const gavl_video_format_t * frame_format,
2229                                   gavl_video_format_t * channel_format,
2230                                   gavl_color_channel_t ch);
2231 
2232 
2233 /*!
2234   \ingroup video_format
2235   \brief Get the video format of one field
2236   \param frame_format The video format of the full frame
2237   \param field_format Format of the field
2238   \param field Field (0 or 1)
2239 
2240   Use this function if you need to split a frame into fields.
2241   It handles odd heights correctly
2242 
2243   Since 1.2.1
2244 */
2245 
2246 GAVL_PUBLIC
2247 void gavl_get_field_format(const gavl_video_format_t * frame_format,
2248                           gavl_video_format_t * field_format,
2249                           int field);
2250 
2251 
2252 /*!
2253   \ingroup video_format
2254   \brief Dump a video format to stderr
2255   \param format A video format
2256  */
2257 
2258 GAVL_PUBLIC
2259 void gavl_video_format_dump(const gavl_video_format_t * format);
2260 
2261 
2262 /** \defgroup video_frame Video frames
2263  * \ingroup video
2264  * \brief Container for video images
2265  *
2266  * This is the standardized method of storing one frame with video data. For planar
2267  * formats, the first scanline starts at planes[0], subsequent scanlines start in
2268  * intervalls of strides[0] bytes. For planar formats, planes[0] will contain the
2269  * luminance channel, planes[1] contains Cb (aka U), planes[2] contains Cr (aka V).
2270  *
2271  * Video frames are created with \ref gavl_video_frame_create and destroyed with
2272  * \ref gavl_video_frame_destroy. The memory can either be allocated by gavl (with
2273  * memory aligned scanlines) or by the caller.
2274  *
2275  * Gavl video frames are always oriented top->bottom left->right. If you must flip frames,
2276  * use the functions \ref gavl_video_frame_copy_flip_x, \ref gavl_video_frame_copy_flip_y or
2277  * \ref gavl_video_frame_copy_flip_xy .
2278  */
2279 
2280 /** \ingroup video_frame
2281  * Video frame
2282  */
2283 
2284 typedef struct
2285   {
2286   uint8_t * planes[GAVL_MAX_PLANES]; /*!< Pointers to the planes */
2287   int strides[GAVL_MAX_PLANES];      /*!< For each plane, this stores the byte offset between the scanlines */
2288 
2289   void * user_data;    /*!< For storing user data (e.g. the corresponding XImage) */
2290   int64_t timestamp; /*!< Timestamp in stream specific units (see \ref video_format) */
2291   int64_t duration; /*!< Duration in stream specific units (see \ref video_format) */
2292   gavl_interlace_mode_t   interlace_mode;/*!< Interlace mode */
2293   gavl_timecode_t timecode; /*!< Timecode associated with this frame */
2294   } gavl_video_frame_t;
2295 
2296 
2297 /*!
2298   \ingroup video_frame
2299   \brief Create video frame
2300   \param format Format of the data to be stored in this frame or NULL
2301 
2302   Creates a video frame for a given format and allocates buffers for the scanlines. To create a
2303   video frame from your custom memory, pass NULL for the format and you'll get an empty frame in
2304   which you can set the pointers manually. If scanlines are allocated, they are padded to that
2305   scanlines start at certain byte boundaries (currently 8).
2306 */
2307 
2308 GAVL_PUBLIC
2309 gavl_video_frame_t * gavl_video_frame_create(const gavl_video_format_t*format);
2310 
2311 /*!
2312   \ingroup video_frame
2313   \brief Create video frame without padding
2314   \param format Format of the data to be stored in this frame or NULL
2315 
2316   Same as \ref gavl_video_frame_create but omits padding, so scanlines will always be
2317   adjacent in memory.
2318 
2319 */
2320 
2321 GAVL_PUBLIC
2322 gavl_video_frame_t * gavl_video_frame_create_nopad(const gavl_video_format_t*format);
2323 
2324 
2325 
2326 /*!
2327   \ingroup video_frame
2328   \brief Destroy a video frame.
2329   \param frame A video frame
2330 
2331   Destroys a video frame and frees all associated memory. If you used your custom memory
2332   to allocate the frame, call \ref gavl_video_frame_null before.
2333 */
2334 
2335 GAVL_PUBLIC
2336 void gavl_video_frame_destroy(gavl_video_frame_t*frame);
2337 
2338 /*!
2339   \ingroup video_frame
2340   \brief Zero all pointers in the video frame.
2341   \param frame A video frame
2342 
2343   Zero all pointers, so \ref gavl_video_frame_destroy won't free them.
2344   Call this for video frames, which were created with a NULL format
2345   before destroying them.
2346 
2347 */
2348 
2349 GAVL_PUBLIC
2350 void gavl_video_frame_null(gavl_video_frame_t*frame);
2351 
2352 /*!
2353   \ingroup video_frame
2354   \brief Fill the frame with black color
2355   \param frame A video frame
2356   \param format Format of the data in the frame
2357 
2358 */
2359 
2360 GAVL_PUBLIC
2361 void gavl_video_frame_clear(gavl_video_frame_t * frame,
2362                             const gavl_video_format_t * format);
2363 
2364 /*!
2365   \ingroup video_frame
2366   \brief Fill the frame with a user spefified color
2367   \param frame A video frame
2368   \param format Format of the data in the frame
2369   \param color Color components in RGBA format scaled 0.0 .. 1.0
2370 
2371 */
2372 
2373 GAVL_PUBLIC
2374 void gavl_video_frame_fill(gavl_video_frame_t * frame,
2375                            const gavl_video_format_t * format,
2376                            const float * color);
2377 
2378 /*!
2379   \ingroup video_frame
2380   \brief Fill the frame with the absolute differene of 2 source frames
2381   \param format Format of the data in the frame
2382   \param dst A video frame
2383   \param src1 First source frame
2384   \param src2 Second source frame
2385 
2386   Since 1.1.1
2387 
2388 */
2389 
2390 GAVL_PUBLIC
2391 void gavl_video_frame_absdiff(gavl_video_frame_t * dst,
2392                               const gavl_video_frame_t * src1,
2393                               const gavl_video_frame_t * src2,
2394                               const gavl_video_format_t * format);
2395 
2396 /*!
2397   \ingroup video_frame
2398   \brief Calculate the PSNR of 2 source frames
2399   \param psnr Returns PSNR for all components (maximum 4)
2400   \param src1 First source frame
2401   \param src2 Second source frame
2402   \param format Format of the data in the frame
2403 
2404   Since 1.1.1
2405 
2406 */
2407 
2408 GAVL_PUBLIC
2409 void gavl_video_frame_psnr(double * psnr,
2410                            const gavl_video_frame_t * src1,
2411                            const gavl_video_frame_t * src2,
2412                            const gavl_video_format_t * format);
2413 
2414 /*!
2415   \ingroup video_frame
2416   \brief Calculate the SSIM of 2 source frames
2417   \param src1 First source frame
2418   \param src2 Second source frame
2419   \param dst Will contain the SSIM index for each pixel
2420   \param format Format of the data in the frame
2421   \returns 1 if the SSIM could be computed, 0 else
2422 
2423   This calculates the SSIM indices of each pixel for 2 source frames and
2424   stores them into dst. The source frames must have the pixelformat
2425   \ref GAVL_GRAY_FLOAT implying that only the luminance component is
2426   considered. The destination also has the pixelformat \ref GAVL_GRAY_FLOAT.
2427   If other pixelformats are passed to this function it will return 0 and
2428   nothing is done.
2429 
2430   The SSIM algorithm is taken from the paper "Image Quality Assessment:
2431   From Error Visibility to Structural Similarity" by Z. Want et. al.
2432   published in IEEE Transactions on image processing,
2433   VOL. 13, NO. 4, APRIL 2004. Homepage of the author:
2434   http://www.ece.uwaterloo.ca/~z70wang/research/ssim/
2435 
2436   Since 1.1.2
2437 
2438 */
2439 
2440 GAVL_PUBLIC
2441 int gavl_video_frame_ssim(const gavl_video_frame_t * src1,
2442                           const gavl_video_frame_t * src2,
2443                           gavl_video_frame_t * dst,
2444                           const gavl_video_format_t * format);
2445 
2446 /*!
2447   \ingroup video_frame
2448   \brief Copy one video frame to another
2449   \param format The format of the frames
2450   \param dst Destination
2451   \param src Source
2452 
2453   The source and destination formats must be identical, as this routine does no
2454   format conversion. The scanlines however can be padded differently in the source and destination.
2455   This function only copies the image data. For copying the metadata (timestamp etc.) use#
2456   \ref gavl_video_frame_copy_metadata.
2457 */
2458 
2459 GAVL_PUBLIC
2460 void gavl_video_frame_copy(const gavl_video_format_t * format,
2461                            gavl_video_frame_t * dst,
2462                            const gavl_video_frame_t * src);
2463 
2464 /*!
2465   \ingroup video_frame
2466   \brief Copy a single plane from one video frame to another
2467   \param format The format of the frames
2468   \param dst Destination
2469   \param src Source
2470   \param plane The plane to copy
2471 
2472   Like \ref gavl_video_frame_copy but copies only a single plane
2473 
2474 */
2475 
2476 GAVL_PUBLIC
2477 void gavl_video_frame_copy_plane(const gavl_video_format_t * format,
2478                                  gavl_video_frame_t * dst,
2479                                  const gavl_video_frame_t * src, int plane);
2480 
2481 /*!
2482   \ingroup video_frame
2483   \brief Copy one video frame to another with horizontal flipping
2484   \param format The format of the frames
2485   \param dst Destination
2486   \param src Source
2487 
2488   Like \ref gavl_video_frame_copy but flips the image horizontally
2489 
2490 */
2491 
2492 GAVL_PUBLIC
2493 void gavl_video_frame_copy_flip_x(const gavl_video_format_t * format,
2494                                   gavl_video_frame_t * dst,
2495                                   const gavl_video_frame_t * src);
2496 
2497 /*!
2498   \ingroup video_frame
2499   \brief Copy one video frame to another with vertical flipping
2500   \param format The format of the frames
2501   \param dst Destination
2502   \param src Source
2503 
2504   Like \ref gavl_video_frame_copy but flips the image vertically
2505 
2506 */
2507 
2508 GAVL_PUBLIC
2509 void gavl_video_frame_copy_flip_y(const gavl_video_format_t * format,
2510                                   gavl_video_frame_t * dst,
2511                                   const gavl_video_frame_t * src);
2512 
2513 /*!
2514   \ingroup video_frame
2515   \brief Copy one video frame to another with horizontal and vertical flipping
2516   \param format The format of the frames
2517   \param dst Destination
2518   \param src Source
2519 
2520   Like \ref gavl_video_frame_copy but flips the image both horizontally and vertically
2521 
2522 */
2523 
2524 GAVL_PUBLIC
2525 void gavl_video_frame_copy_flip_xy(const gavl_video_format_t * format,
2526                                    gavl_video_frame_t * dst,
2527                                   const gavl_video_frame_t * src);
2528 
2529 /*!
2530   \ingroup video_frame
2531   \brief Copy metadata of one video frame to another
2532   \param dst Destination
2533   \param src Source
2534 
2535   This function only copies the metadata (timestamp, duration, timecode). For copying the image data
2536   use \ref gavl_video_frame_copy.
2537 
2538   Since 1.1.0.
2539 */
2540 
2541 GAVL_PUBLIC
2542 void gavl_video_frame_copy_metadata(gavl_video_frame_t * dst,
2543                                     const gavl_video_frame_t * src);
2544 
2545 
2546 /*!
2547   \ingroup video_frame
2548   \brief Get a subframe of another frame
2549   \param pixelformat Pixelformat of the frames
2550   \param dst Destination
2551   \param src Source
2552   \param src_rect Rectangular area in the source, which will be in the destination frame
2553 
2554   This fills the pointers of dst from src such that the dst will represent the
2555   speficied rectangular area. Note that no data are copied here. This means that
2556   dst must be created with NULL as the format argument and \ref gavl_video_frame_null
2557   must be called before destroying dst.
2558 
2559   When dealing with chroma subsampled pixelformats, you must call
2560   \ref gavl_rectangle_i_align on src_rect before.
2561 */
2562 
2563 GAVL_PUBLIC
2564 void gavl_video_frame_get_subframe(gavl_pixelformat_t pixelformat,
2565                                    const gavl_video_frame_t * src,
2566                                    gavl_video_frame_t * dst,
2567                                    gavl_rectangle_i_t * src_rect);
2568 
2569 /*!
2570   \ingroup video_frame
2571   \brief Get a field from a frame
2572   \param pixelformat Pixelformat of the frames
2573   \param dst Destination
2574   \param src Source
2575   \param field Field index (0 = top field, 1 = bottom field)
2576 
2577   This fills the pointers and strides of the destination frame such that it
2578   will represent the speficied field of the source frame.
2579   Note that no data are copied here. This means that
2580   dst must be created with NULL as the format argument and \ref gavl_video_frame_null
2581   must be called before destroying dst.
2582 */
2583 
2584 GAVL_PUBLIC
2585 void gavl_video_frame_get_field(gavl_pixelformat_t pixelformat,
2586                                 const gavl_video_frame_t * src,
2587                                 gavl_video_frame_t * dst,
2588                                 int field);
2589 
2590 
2591 
2592 /*!
2593   \ingroup video_frame
2594   \brief Dump a video frame to files
2595   \param frame Video frame to dump
2596   \param format Format of the video data in the frame
2597   \param namebase Base for the output filenames
2598 
2599   This is purely for debugging purposes:
2600   It dumps each plane of the frame into files \<namebase\>.p1,
2601   \<namebase\>.p2 etc
2602 */
2603 
2604 GAVL_PUBLIC
2605 void gavl_video_frame_dump(gavl_video_frame_t * frame,
2606                            const gavl_video_format_t * format,
2607                            const char * namebase);
2608 
2609 /*!
2610   \ingroup video_frame
2611   \brief Set the strides according to the format
2612   \param frame Video frame
2613   \param format Format of the video data in the frame
2614 
2615   This sets the strides array according to the format under the assumption,
2616   that no padding is done.
2617 */
2618 
2619 GAVL_PUBLIC
2620 void gavl_video_frame_set_strides(gavl_video_frame_t * frame,
2621                                   const gavl_video_format_t * format);
2622 
2623 /*!
2624   \ingroup video_frame
2625   \brief Set the frames according to the format
2626   \param frame Video frame
2627   \param format Format of the video data in the frame
2628   \param buffer Start of the first scanline of the first plane
2629 
2630   This sets the planes array from a raw buffer. If frame->strides[0] is
2631   zero, \ref gavl_video_frame_set_strides is called before.
2632 */
2633 
2634 
2635 GAVL_PUBLIC
2636 void gavl_video_frame_set_planes(gavl_video_frame_t * frame,
2637                                  const gavl_video_format_t * format,
2638                                  uint8_t * buffer);
2639 
2640 /*!
2641   \ingroup video_frame
2642   \brief Extract one channel of a video frame into a grayscale image
2643   \param format Format of the source frame
2644   \param ch Channel to extract
2645   \param src Source frame
2646   \param dst Destination where the extracted channel will be copied
2647 
2648   This extracts one color channel into a grayscale image for separate
2649   processing. Use \ref gavl_get_color_channel_format to obtain the
2650   format for the destination frame.
2651 
2652 */
2653 
2654 GAVL_PUBLIC
2655 int gavl_video_frame_extract_channel(const gavl_video_format_t * format,
2656                                      gavl_color_channel_t ch,
2657                                      const gavl_video_frame_t * src,
2658                                      gavl_video_frame_t * dst);
2659 
2660 /*!
2661   \ingroup video_frame
2662   \brief Insert one channel from a grayscale image into a video frame
2663   \param format Format of the source frame
2664   \param ch Channel to merge
2665   \param src Source frame (grayscale image containing one chanel)
2666   \param dst Destination
2667 
2668   This inserts one color channel from a grayscale image into a video
2669   frame (overwriting the previous contents of that channel).
2670   Use \ref gavl_get_color_channel_format to obtain the
2671   format for the source grayscale frame.
2672 */
2673 
2674 
2675 GAVL_PUBLIC
2676 int gavl_video_frame_insert_channel(const gavl_video_format_t * format,
2677                                     gavl_color_channel_t ch,
2678                                     const gavl_video_frame_t * src,
2679                                     gavl_video_frame_t * dst);
2680 
2681 
2682 /*!
2683   \ingroup video_frame
2684   \brief Check if 2 video frames are bit-identical
2685   \param format Format
2686   \param f1 First frame
2687   \param f2 Second frame
2688   \returns 1 if the frames are equal, 0 else
2689 
2690   Since 1.2.0
2691 */
2692 
2693 GAVL_PUBLIC
2694 int gavl_video_frames_equal(const gavl_video_format_t * format,
2695                              const gavl_video_frame_t * f1,
2696                              const gavl_video_frame_t * f2);
2697 
2698 
2699 /*****************************
2700  Conversion options
2701 ******************************/
2702 
2703 /** \defgroup video_options Video conversion options
2704  * \ingroup video
2705  */
2706 
2707 /** \defgroup video_conversion_flags Video conversion flags
2708  * \ingroup video_options
2709  */
2710 
2711 /** \ingroup video_conversion_flags
2712  * \brief Force deinterlacing
2713  *
2714  * This option turns on deinterlacing by the converter in any case (i.e. also if the input format
2715  * pretends to be progressive or if the ouput format is interlaced).
2716  */
2717 
2718 #define GAVL_FORCE_DEINTERLACE (1<<0)
2719 
2720 /** \ingroup video_conversion_flags
2721  * \brief Pass chroma planes to the convolver
2722  */
2723 
2724 #define GAVL_CONVOLVE_CHROMA   (1<<1)
2725 
2726 /** \ingroup video_conversion_flags
2727  * \brief Normalize convolution matrices passed to the scaler
2728  */
2729 
2730 #define GAVL_CONVOLVE_NORMALIZE (1<<2)
2731 
2732 /** \ingroup video_conversion_flags
2733  * \brief Force chroma placement correction
2734  *
2735  *  Force chroma placement correction and resampling using a scaler.
2736  *  Usually this is done only for qualities above 3.
2737  */
2738 
2739 #define GAVL_RESAMPLE_CHROMA    (1<<3)
2740 
2741 /** \ingroup video_options
2742  * Alpha handling mode
2743  *
2744  * Set the desired behaviour if the source format has a transparency channel but the
2745  * destination doesn't.
2746  */
2747 
2748 typedef enum
2749   {
2750     GAVL_ALPHA_IGNORE      = 0, /*!< Ignore alpha channel      */
2751     GAVL_ALPHA_BLEND_COLOR      /*!< Blend over a specified background color */
2752   } gavl_alpha_mode_t;
2753 
2754 /** \ingroup video_options
2755  * Deinterlace mode
2756  *
2757  * Specifies a deinterlacing mode
2758  */
2759 
2760 typedef enum
2761   {
2762     GAVL_DEINTERLACE_NONE      = 0, /*!< Don't care about interlacing                */
2763     GAVL_DEINTERLACE_COPY      = 1, /*!< Take one field and copy it to the other     */
2764     GAVL_DEINTERLACE_SCALE     = 2, /*!< Take one field and scale it vertically by 2 */
2765     GAVL_DEINTERLACE_BLEND     = 3  /*!< Linear blend fields together */
2766   } gavl_deinterlace_mode_t;
2767 
2768 /** \ingroup video_options
2769  * \brief Specifies which field to drop when deinterlacing
2770  *
2771  * This is used for deinterlacing with GAVL_DEINTERLACE_COPY and GAVL_DEINTERLACE_SCALE.
2772  */
2773 
2774 typedef enum
2775   {
2776     GAVL_DEINTERLACE_DROP_TOP,    /*!< Drop top field, use bottom field */
2777     GAVL_DEINTERLACE_DROP_BOTTOM, /*!< Drop bottom field, use top field */
2778   } gavl_deinterlace_drop_mode_t;
2779 
2780 /** \ingroup video_options
2781  * Scaling algorithm
2782  */
2783 
2784 typedef enum
2785   {
2786     GAVL_SCALE_AUTO,          /*!< Take mode from conversion quality */
2787     GAVL_SCALE_NEAREST,       /*!< Nearest neighbor                  */
2788     GAVL_SCALE_BILINEAR,      /*!< Bilinear                          */
2789     GAVL_SCALE_QUADRATIC,     /*!< Quadratic                         */
2790     GAVL_SCALE_CUBIC_BSPLINE, /*!< Cubic B-Spline */
2791     GAVL_SCALE_CUBIC_MITCHELL,/*!< Cubic Mitchell-Netravali */
2792     GAVL_SCALE_CUBIC_CATMULL, /*!< Cubic Catmull-Rom */
2793     GAVL_SCALE_SINC_LANCZOS,  /*!< Sinc with Lanczos window. Set order with \ref gavl_video_options_set_scale_order */
2794     GAVL_SCALE_NONE,          /*!< Used internally when the scaler is used as a convolver */
2795   } gavl_scale_mode_t;
2796 
2797 /** \ingroup video_options
2798  *  Antialiasing filters
2799  *
2800  *  Specifies the antialiasing filter to be used
2801  *  when downscaling images.
2802  *
2803  *  Since 1.1.0
2804  */
2805 
2806 typedef enum
2807   {
2808     GAVL_DOWNSCALE_FILTER_AUTO = 0, //!< Auto selection based on quality
2809     GAVL_DOWNSCALE_FILTER_NONE, //!< Fastest method, might produce heavy aliasing artifacts
2810     GAVL_DOWNSCALE_FILTER_WIDE, //!< Widen the filter curve according to the scaling ratio.
2811     GAVL_DOWNSCALE_FILTER_GAUSS, //!< Do a Gaussian preblur
2812   } gavl_downscale_filter_t;
2813 
2814 /** \ingroup video_options
2815  * Opaque container for video conversion options
2816  *
2817  * You don't want to know what's inside.
2818  */
2819 
2820 typedef struct gavl_video_options_s gavl_video_options_t;
2821 
2822 /* Default Options */
2823 
2824 /*! \ingroup video_options
2825  *  \brief Set all options to their defaults
2826  *  \param opt Video options
2827  */
2828 
2829 GAVL_PUBLIC
2830 void gavl_video_options_set_defaults(gavl_video_options_t * opt);
2831 
2832 /*! \ingroup video_options
2833  *  \brief Create an options container
2834  *  \returns Newly allocated udio options with default values
2835  *
2836  *  Use this to store options, which will apply for more than
2837  *  one converter instance. Applying the options will be done by
2838  *  gavl_*_get_options() followed by gavl_video_options_copy().
2839  */
2840 
2841 GAVL_PUBLIC
2842 gavl_video_options_t * gavl_video_options_create();
2843 
2844 /*! \ingroup video_options
2845  *  \brief Copy video options
2846  *  \param dst Destination
2847  *  \param src Source
2848  */
2849 
2850 GAVL_PUBLIC
2851 void gavl_video_options_copy(gavl_video_options_t * dst,
2852                              const gavl_video_options_t * src);
2853 
2854 /*! \ingroup video_options
2855  *  \brief Destroy video options
2856  *  \param opt Video options
2857  */
2858 
2859 GAVL_PUBLIC
2860 void gavl_video_options_destroy(gavl_video_options_t * opt);
2861 
2862 
2863 /*! \ingroup video_options
2864  *  \brief Set source and destination rectangles
2865  *  \param opt Video options
2866  *  \param src_rect Rectangular area in the source frame or NULL
2867  *  \param dst_rect Rectangular area in the destination frame or NULL
2868  *
2869  *  Set the source and destination rectangles the converter or scaler will operate on.
2870  *  If you don't call this function, the rectangles will be set to the full image dimensions
2871  *  of the source and destination formats respectively. If one rectangle is NULL, BOTH rectangles
2872  *  will be cleared as if you never called this function. See \ref rectangle for convenience
2873  *  functions, which calculate the proper rectangles in some typical playback or transcoding
2874  *  situations.
2875  */
2876 
2877 GAVL_PUBLIC
2878 void gavl_video_options_set_rectangles(gavl_video_options_t * opt,
2879                                        const gavl_rectangle_f_t * src_rect,
2880                                        const gavl_rectangle_i_t * dst_rect);
2881 
2882 /*! \ingroup video_options
2883  *  \brief Get source and destination rectangles
2884  *  \param opt Video options
2885  *  \param src_rect Returns the rectangular area in the source frame
2886  *  \param dst_rect Returns the rectangular area in the destination frame
2887  */
2888 
2889 GAVL_PUBLIC
2890 void gavl_video_options_get_rectangles(gavl_video_options_t * opt,
2891                                        gavl_rectangle_f_t * src_rect,
2892                                        gavl_rectangle_i_t * dst_rect);
2893 
2894 /*! \ingroup video_options
2895  *  \brief Set the quality level for the converter
2896  *  \param opt Video options
2897  *  \param quality Quality level (see \ref quality)
2898  */
2899 
2900 GAVL_PUBLIC
2901 void gavl_video_options_set_quality(gavl_video_options_t * opt, int quality);
2902 
2903 /*! \ingroup video_options
2904  *  \brief Get the quality level for the converter
2905  *  \param opt Video options
2906  *  \returns Quality level (see \ref quality)
2907  */
2908 
2909 GAVL_PUBLIC
2910 int gavl_video_options_get_quality(gavl_video_options_t * opt);
2911 
2912 
2913 /*! \ingroup video_options
2914  *  \brief Set the conversion flags
2915  *  \param opt Video options
2916  *  \param conversion_flags Conversion flags (see \ref video_conversion_flags)
2917  */
2918 
2919 GAVL_PUBLIC
2920 void gavl_video_options_set_conversion_flags(gavl_video_options_t * opt,
2921                                              int conversion_flags);
2922 
2923 /*! \ingroup video_options
2924  *  \brief Get the conversion flags
2925  *  \param opt Video options
2926  *  \returns Flags (see \ref video_conversion_flags)
2927  */
2928 
2929 GAVL_PUBLIC
2930 int gavl_video_options_get_conversion_flags(gavl_video_options_t * opt);
2931 
2932 /*! \ingroup video_options
2933  *  \brief Set the alpha mode
2934  *  \param opt Video options
2935  *  \param alpha_mode Alpha mode
2936  */
2937 
2938 GAVL_PUBLIC
2939 void gavl_video_options_set_alpha_mode(gavl_video_options_t * opt,
2940                                        gavl_alpha_mode_t alpha_mode);
2941 
2942 /*! \ingroup video_options
2943  *  \brief Get the alpha mode
2944  *  \param opt Video options
2945  *  \returns Alpha mode
2946  */
2947 
2948 GAVL_PUBLIC gavl_alpha_mode_t
2949 gavl_video_options_get_alpha_mode(gavl_video_options_t * opt);
2950 
2951 
2952 /*! \ingroup video_options
2953  *  \brief Set the scale mode
2954  *  \param opt Video options
2955  *  \param scale_mode Scale mode
2956  */
2957 
2958 GAVL_PUBLIC
2959 void gavl_video_options_set_scale_mode(gavl_video_options_t * opt,
2960                                        gavl_scale_mode_t scale_mode);
2961 
2962 /*! \ingroup video_options
2963  *  \brief Get the scale mode
2964  *  \param opt Video options
2965  *  \returns Scale mode
2966  */
2967 
2968 GAVL_PUBLIC gavl_scale_mode_t
2969 gavl_video_options_get_scale_mode(gavl_video_options_t * opt);
2970 
2971 
2972 /*! \ingroup video_options
2973  *  \brief Set the scale order for GAVL_SCALE_SINC_LANCZOS
2974  *  \param opt Video options
2975  *  \param order Order (must be at least 4)
2976  */
2977 
2978 GAVL_PUBLIC
2979 void gavl_video_options_set_scale_order(gavl_video_options_t * opt,
2980                                         int order);
2981 
2982 /*! \ingroup video_options
2983  *  \brief Get the scale order for GAVL_SCALE_SINC_LANCZOS
2984  *  \param opt Video options
2985  *  \returns Order
2986  */
2987 
2988 GAVL_PUBLIC
2989 int gavl_video_options_get_scale_order(gavl_video_options_t * opt);
2990 
2991 
2992 /*! \ingroup video_options
2993  *  \brief Set the background color for alpha blending
2994  *  \param opt Video options
2995  *  \param color Array of 3 float values (0.0 .. 1.0) in RGB order
2996  */
2997 
2998 GAVL_PUBLIC
2999 void gavl_video_options_set_background_color(gavl_video_options_t * opt,
3000                                              const float * color);
3001 
3002 /*! \ingroup video_options
3003  *  \brief Get the background color for alpha blending
3004  *  \param opt Video options
3005  *  \param color Returns 3 float values (0.0 .. 1.0) in RGB order
3006  */
3007 
3008 GAVL_PUBLIC
3009 void gavl_video_options_get_background_color(gavl_video_options_t * opt,
3010                                              float * color);
3011 
3012 /*! \ingroup video_ptions
3013  *  \brief Set the deinterlace mode
3014  *  \param opt Video options
3015  *  \param deinterlace_mode Deinterlace mode
3016  */
3017 
3018 GAVL_PUBLIC
3019 void gavl_video_options_set_deinterlace_mode(gavl_video_options_t * opt,
3020                                              gavl_deinterlace_mode_t deinterlace_mode);
3021 
3022 /*! \ingroup video_ptions
3023  *  \brief Get the deinterlace mode
3024  *  \param opt Video options
3025  *  \returns Deinterlace mode
3026  */
3027 
3028 GAVL_PUBLIC gavl_deinterlace_mode_t
3029 gavl_video_options_get_deinterlace_mode(gavl_video_options_t * opt);
3030 
3031 /*! \ingroup video_options
3032  *  \brief Set the deinterlace drop mode
3033  *  \param opt Video options
3034  *  \param deinterlace_drop_mode Deinterlace drop mode
3035  */
3036 
3037 GAVL_PUBLIC
3038 void gavl_video_options_set_deinterlace_drop_mode(gavl_video_options_t * opt,
3039                                                   gavl_deinterlace_drop_mode_t deinterlace_drop_mode);
3040 
3041 /*! \ingroup video_options
3042  *  \brief Get the deinterlace drop mode
3043  *  \param opt Video options
3044  *  \returns Deinterlace drop mode
3045  */
3046 
3047 GAVL_PUBLIC gavl_deinterlace_drop_mode_t
3048 gavl_video_options_get_deinterlace_drop_mode(gavl_video_options_t * opt);
3049 
3050 /*!  \ingroup video_options
3051  *   \brief Set antialiasing filter for downscaling
3052  *   \param opt Video options
3053  *   \param f Filter type (see \ref gavl_downscale_filter_t)
3054  *
3055  *  Since 1.1.0
3056  */
3057 
3058 GAVL_PUBLIC
3059 void gavl_video_options_set_downscale_filter(gavl_video_options_t * opt,
3060                                              gavl_downscale_filter_t f);
3061 
3062 
3063 /*! \ingroup video_options
3064  *  \brief Get the antialiasing filter for downscaling
3065  *  \param opt Video options
3066  *  \returns antialiasing filter for downscaling
3067  *
3068  *  Since 1.1.0
3069  */
3070 
3071 GAVL_PUBLIC gavl_downscale_filter_t
3072 gavl_video_options_get_downscale_filter(gavl_video_options_t * opt);
3073 
3074 /*!  \ingroup video_options
3075  *   \brief Set blur factor for downscaling
3076  *   \param opt Video options
3077  *   \param f Factor
3078  *
3079  *   Specifies an additional blur-factor for downscaling. The
3080  *   default value of 1.0 calculates the preblur coefficients
3081  *   according the the downsample factor. Larger values mean
3082  *   more blurring (and slower scaling), smaller values mean
3083  *   less blurring (and probably more aliasing artifacts),
3084  *   0 is equivalent to
3085  *   calling \ref gavl_video_options_set_downscale_filter
3086  *   with GAVL_DOWNSCALE_FILTER_NONE as argument.
3087  *
3088  *  Since 1.1.0
3089  */
3090 
3091 GAVL_PUBLIC
3092 void gavl_video_options_set_downscale_blur(gavl_video_options_t * opt,
3093                                            float f);
3094 
3095 /*!  \ingroup video_options
3096  *   \brief Get blur factor for downscaling
3097  *   \param opt Video options
3098  *   \returns Factor
3099  *
3100  *  Since 1.1.0
3101  */
3102 
3103 GAVL_PUBLIC
3104 float gavl_video_options_get_downscale_blur(gavl_video_options_t * opt);
3105 
3106 /*!  \ingroup video_options
3107  *   \brief Set number of threads
3108  *   \param opt Video options
3109  *   \param n Number of threads
3110  *
3111  *  Since 1.1.1
3112  */
3113 
3114 GAVL_PUBLIC
3115 void gavl_video_options_set_num_threads(gavl_video_options_t * opt, int n);
3116 
3117 
3118 /*!  \ingroup video_options
3119  *   \brief Set number of threads
3120  *   \param opt Video options
3121  *   \returns Number of threads
3122  *
3123  *  Since 1.1.1
3124  */
3125 
3126 GAVL_PUBLIC
3127 int gavl_video_options_get_num_threads(gavl_video_options_t * opt);
3128 
3129 /*!  \ingroup video_options
3130  *   \brief Set function to be passed to each thread
3131  *   \param opt Video options
3132  *   \param func Function to be passed to each thread
3133  *   \param client_data Client data to be passed to the run function
3134  *
3135  *  Since 1.1.1
3136  */
3137 
3138 GAVL_PUBLIC
3139 void gavl_video_options_set_run_func(gavl_video_options_t * opt,
3140                                      gavl_video_run_func func,
3141                                      void * client_data);
3142 
3143 /*!  \ingroup video_options
3144  *   \brief Get function to be passed to each thread
3145  *   \param opt Video options
3146  *   \param client_data Returns client data
3147  *   \return The function
3148  *
3149  *  Since 1.1.1
3150  */
3151 
3152 GAVL_PUBLIC
3153 gavl_video_run_func gavl_video_options_get_run_func(gavl_video_options_t * opt,
3154                                                     void ** client_data);
3155 
3156 /*!  \ingroup video_options
3157  *   \brief Set function to be passed to each thread
3158  *   \param opt Video options
3159  *   \param func Function to be passed to each thread
3160  *   \param client_data Client data to be passed to the run function
3161  *
3162  *  Since 1.1.1
3163  */
3164 
3165 GAVL_PUBLIC
3166 void gavl_video_options_set_stop_func(gavl_video_options_t * opt,
3167                                       gavl_video_stop_func func,
3168                                       void * client_data);
3169 
3170 /*!  \ingroup video_options
3171  *   \brief Get function to be passed to each thread
3172  *   \param opt Video options
3173  *   \param client_data Returns client data
3174  *   \return The function
3175  *
3176  *  Since 1.1.1
3177  */
3178 
3179 GAVL_PUBLIC
3180 gavl_video_stop_func gavl_video_options_get_stop_func(gavl_video_options_t * opt,
3181                                                       void ** client_data);
3182 
3183 
3184 /***************************************************
3185  * Create and destroy video converters
3186  ***************************************************/
3187 
3188 /** \defgroup video_converter Video converter
3189  * \ingroup video
3190  * \brief Video format converter
3191  *
3192  * This is a generic converter, which converts video frames from one arbitrary format to
3193  * another. It can convert pixelformats, scale images and deinterlace.
3194  *
3195  * For quality levels of 3 and below, pixelformats are converted in one single step,
3196  * without the need for intermediate frames.
3197  * Quality levels of 4 and 5 will take care of chroma placement. For this, a
3198  * \ref gavl_video_scaler_t will be used.
3199  *
3200  * Deinterlacing is enabled if the input format is interlaced and the output format is
3201  * progressive and the deinterlace mode is something else than \ref GAVL_DEINTERLACE_NONE.
3202  * You can also force deinterlacing (\ref GAVL_FORCE_DEINTERLACE).
3203  *
3204  * Create a video converter with \ref gavl_video_converter_create. If you want to configure it,
3205  * get the options pointer with \ref gavl_video_converter_get_options and change the options
3206  * (See \ref video_options).
3207  * Call \ref gavl_video_converter_init to initialize the converter for the input and output
3208  * formats. Video frames are then converted with \ref gavl_video_convert.
3209  *
3210  * When you are done, you can either reinitialize the converter or destroy it with
3211  * \ref gavl_video_converter_destroy.
3212  */
3213 
3214 /*! \ingroup video_converter
3215  * \brief Opaque video converter structure
3216  *
3217  * You don't want to know what's inside.
3218  */
3219 
3220 typedef struct gavl_video_converter_s gavl_video_converter_t;
3221 
3222 /*! \ingroup video_converter
3223  *  \brief Creates a video converter
3224  *  \returns A newly allocated video converter
3225  */
3226 
3227 GAVL_PUBLIC
3228 gavl_video_converter_t * gavl_video_converter_create();
3229 
3230 /*! \ingroup video_converter
3231  *  \brief Destroys a video converter and frees all associated memory
3232  *  \param cnv A video converter
3233  */
3234 
3235 GAVL_PUBLIC
3236 void gavl_video_converter_destroy(gavl_video_converter_t*cnv);
3237 
3238 /**************************************************
3239  * Get options. Change the options with the gavl_video_options_set_*
3240  * functions above
3241  **************************************************/
3242 
3243 /*! \ingroup video_converter
3244  *  \brief gets options of a video converter
3245  *  \param cnv A video converter
3246  *
3247  * After you called this, you can use the gavl_video_options_set_*() functions to change
3248  * the options. Options will become valid with the next call to \ref gavl_video_converter_init or \ref gavl_video_converter_reinit.
3249  */
3250 
3251 GAVL_PUBLIC gavl_video_options_t *
3252 gavl_video_converter_get_options(gavl_video_converter_t*cnv);
3253 
3254 
3255 /*! \ingroup video_converter
3256  *  \brief Initialize a video converter
3257  *  \param cnv A video converter
3258  *  \param input_format Input format
3259  *  \param output_format Output format
3260  *  \returns The number of single conversion steps necessary to perform the
3261  *           conversion. It may be 0, in this case you must not use the converter and have to
3262  *           pass the video frames directly. If something goes wrong (should never happen),
3263  *           -1 is returned.
3264  *
3265  * This function can be called multiple times with one instance
3266  */
3267 
3268 GAVL_PUBLIC
3269 int gavl_video_converter_init(gavl_video_converter_t* cnv,
3270                               const gavl_video_format_t * input_format,
3271                               const gavl_video_format_t * output_format);
3272 
3273 /*! \ingroup video_converter
3274  *  \brief Reinitialize a video converter
3275  *  \param cnv A video converter
3276  *  \returns The number of single conversion steps necessary to perform the
3277  *           conversion. It may be 0, in this case you must not use the converter and have to
3278  *           pass the video frames directly. If something goes wrong (should never happen),
3279  *           -1 is returned.
3280  *
3281  * This function can be called if the input and output formats didn't
3282  * change but the options did.
3283  */
3284 
3285 GAVL_PUBLIC
3286 int gavl_video_converter_reinit(gavl_video_converter_t* cnv);
3287 
3288 
3289 /***************************************************
3290  * Convert a frame
3291  ***************************************************/
3292 
3293 /*! \ingroup video_converter
3294  *  \brief Convert video
3295  *  \param cnv A video converter
3296  *  \param input_frame Input frame
3297  *  \param output_frame Output frame
3298  */
3299 
3300 GAVL_PUBLIC
3301 void gavl_video_convert(gavl_video_converter_t * cnv,
3302                         const gavl_video_frame_t * input_frame,
3303                         gavl_video_frame_t * output_frame);
3304 
3305 /*! \defgroup video_scaler Scaler
3306  *  \ingroup video
3307  *  \brief Video scaler
3308  *
3309  *  The scaler does the elementary operation to take a rectangular area
3310  *  of the source image and scale it into a specified rectangular area of the
3311  *  destination image. The source rectangle has floating point coordinates, the destination
3312  *  rectangle must have integer coordinates, which are aligned to chroma subsampling factors.
3313  *
3314  *  The internal scale tables are created for each plane and field separately. This
3315  *  means that:
3316  *
3317  *  - We handle all flavors of chroma placement correctly
3318  *  - We can convert chroma subsampling by scaling the chroma planes and copying
3319  *    the luminance plane.
3320  *  - Since the scaler knows about fields, it will scale interlaced frames field-wise
3321  *    (not a good idea to scale interlaced frames vertically though).
3322  *  - Simple deinterlacing (See \ref GAVL_DEINTERLACE_SCALE)
3323  *    can be done by taking one source field and scale it vertically to the entire
3324  *    destination frame.
3325  *
3326  *  You can use the scaler directly (through \ref gavl_video_scaler_t). The generic video
3327  *  converter (\ref gavl_video_converter_t) will create an internal scaler if necessary.
3328  */
3329 
3330 /*! \ingroup video_scaler
3331  *  \brief Opaque scaler structure.
3332  *
3333  *  You don't want to know what's inside.
3334  */
3335 
3336 typedef struct gavl_video_scaler_s gavl_video_scaler_t;
3337 
3338 /*! \ingroup video_scaler
3339  *  \brief Create a video scaler
3340  *  \returns A newly allocated video scaler
3341  */
3342 
3343 GAVL_PUBLIC
3344 gavl_video_scaler_t * gavl_video_scaler_create();
3345 
3346 /*! \ingroup video_scaler
3347  *  \brief Destroy a video scaler
3348  *  \param scaler A video scaler
3349  */
3350 
3351 GAVL_PUBLIC
3352 void gavl_video_scaler_destroy(gavl_video_scaler_t * scaler);
3353 
3354 /*! \ingroup video_scaler
3355  *  \brief gets options of a scaler
3356  *  \param scaler A video scaler
3357  *
3358  * After you called this, you can use the gavl_video_options_set_*() functions to change
3359  * the options. Options will become valid with the next call to \ref gavl_video_scaler_init
3360  */
3361 
3362 GAVL_PUBLIC gavl_video_options_t *
3363 gavl_video_scaler_get_options(gavl_video_scaler_t * scaler);
3364 
3365 /*! \ingroup video_scaler
3366  *  \brief Initialize a video scaler
3367  *  \param scaler A video scaler
3368  *  \param src_format Input format
3369  *  \param dst_format Output format
3370  *  \returns If something goes wrong (should never happen), -1 is returned.
3371  *
3372  * You should have equal pixelformats in the source and destination.
3373  * This function can be called multiple times with one instance.
3374  */
3375 
3376 
3377 GAVL_PUBLIC
3378 int gavl_video_scaler_init(gavl_video_scaler_t * scaler,
3379                            const gavl_video_format_t * src_format,
3380                            const gavl_video_format_t * dst_format);
3381 
3382 /*! \ingroup video_scaler
3383  *  \brief Initialize a video scaler as a generic convolver
3384  *  \param scaler A video scaler
3385  *  \param format Format (must be the same for input and output)
3386  *  \param h_radius Horizontal radius
3387  *  \param h_coeffs Horizontal coefficients
3388  *  \param v_radius Vertical radius
3389  *  \param v_coeffs Vertical coefficients
3390  *  \returns If something goes wrong (should never happen), -1 is returned.
3391  *
3392  * This initialized a scaler for use as a generic convolver.
3393  * The h_radius and v_radius arguments denote the numbers of pixels,
3394  * which are taken in both left and right from the center pixel, i.e.
3395  * a value of 1 will result in a 3-tap filter. The coefficients
3396  * must be given for ALL taps (the convolver does not assume the
3397  * coeffitients to be symmetric)
3398  *
3399  * This function can be called multiple times with one instance.
3400  */
3401 
3402 
3403 GAVL_PUBLIC
3404 int gavl_video_scaler_init_convolve(gavl_video_scaler_t * scaler,
3405                                     const gavl_video_format_t * format,
3406                                     int h_radius, const float * h_coeffs,
3407                                     int v_radius, const float * v_coeffs);
3408 
3409 /*! \ingroup video_scaler
3410  *  \brief Scale video
3411  *  \param scaler A video scaler
3412  *  \param input_frame Input frame
3413  *  \param output_frame Output frame
3414  */
3415 
3416 GAVL_PUBLIC
3417 void gavl_video_scaler_scale(gavl_video_scaler_t * scaler,
3418                              const gavl_video_frame_t * input_frame,
3419                              gavl_video_frame_t * output_frame);
3420 
3421 /*! \defgroup video_deinterlacer Deinterlacer
3422  *  \ingroup video
3423  *  \brief Deinterlacer
3424  *
3425  *  Deinterlacing is supported either through the \ref gavl_video_converter_t
3426  *  or using a low level deinterlacer
3427  */
3428 
3429 /*! \ingroup video_deinterlacer
3430  *  \brief Opaque deinterlacer structure.
3431  *
3432  *  You don't want to know what's inside.
3433  */
3434 
3435 
3436 typedef struct gavl_video_deinterlacer_s gavl_video_deinterlacer_t;
3437 
3438 /*! \ingroup video_deinterlacer
3439  *  \brief Create a video deinterlacer
3440  *  \returns A newly allocated video deinterlacer
3441  */
3442 
3443 GAVL_PUBLIC
3444 gavl_video_deinterlacer_t * gavl_video_deinterlacer_create();
3445 
3446 /*! \ingroup video_deinterlacer
3447  *  \brief Destroy a video deinterlacer
3448  *  \param deinterlacer A video deinterlacer
3449  */
3450 
3451 GAVL_PUBLIC
3452 void gavl_video_deinterlacer_destroy(gavl_video_deinterlacer_t * deinterlacer);
3453 
3454 /*! \ingroup video_deinterlacer
3455  *  \brief gets options of a deinterlacer
3456  *  \param deinterlacer A video deinterlacer
3457  *
3458  * After you called this, you can use the gavl_video_options_set_*() functions to change
3459  * the options. Options will become valid with the next call to \ref gavl_video_deinterlacer_init
3460  */
3461 
3462 GAVL_PUBLIC gavl_video_options_t *
3463 gavl_video_deinterlacer_get_options(gavl_video_deinterlacer_t * deinterlacer);
3464 
3465 /*! \ingroup video_deinterlacer
3466  *  \brief Initialize a video deinterlacer
3467  *  \param deinterlacer A video deinterlacer
3468  *  \param src_format Input format
3469  *  \returns If something goes wrong (should never happen), -1 is returned.
3470  *
3471  * You should have equal pixelformats in the source and destination.
3472  * This function can be called multiple times with one instance.
3473  */
3474 
3475 GAVL_PUBLIC
3476 int gavl_video_deinterlacer_init(gavl_video_deinterlacer_t * deinterlacer,
3477                                  const gavl_video_format_t * src_format);
3478 
3479 
3480 /*! \ingroup video_deinterlacer
3481  *  \brief Deinterlace video
3482  *  \param deinterlacer A video deinterlacer
3483  *  \param input_frame Input frame
3484  *  \param output_frame Output frame
3485  */
3486 
3487 GAVL_PUBLIC
3488 void gavl_video_deinterlacer_deinterlace(gavl_video_deinterlacer_t * deinterlacer,
3489                                          const gavl_video_frame_t * input_frame,
3490                                          gavl_video_frame_t * output_frame);
3491 
3492 
3493 
3494 /**************************************************
3495  * Transparent overlays
3496  **************************************************/
3497 
3498 /* Overlay struct */
3499 
3500 /*! \defgroup video_blend Overlay blending
3501  * \ingroup video
3502  *
3503  *  Overlay blending does one elemental operation:
3504  *  Take a partly transparent overlay (in an alpha
3505  *  capable pixelformat) and blend it onto a video frame.
3506  *  Blending can be used for subtitles or OSD in playback applications,
3507  *  and also for lots of weird effects.
3508  *  In the current implementation, there is only one overlay
3509  *  pixelformat, which can be blended onto a cetrtain destination format.
3510  *  Therefore, the incoming overlay will be converted to the pixelformat
3511  *  necessary for the conversion. For OSD and Subtitle applications,
3512  *  this happens only once for each overlay, since the converted overlay is
3513  *  remembered by the blend context.
3514  *
3515  *  Note that gavl doesn't (and never will) support text subtitles. To
3516  *  blend text strings onto a video frame, you must render it into a
3517  *  gavl_overlay_t with some typesetting library (e.g. freetype) first.
3518  */
3519 
3520 /*! \ingroup video_blend
3521  *  \brief Overlay structure.
3522  *
3523  *  Structure, which holds an overlay. If the sizes of source and destination
3524  *  rectangles differ, the smaller one will be used.
3525  */
3526 
3527 typedef struct
3528   {
3529   gavl_video_frame_t * frame;    //!< Video frame in an alpha capable format */
3530   gavl_rectangle_i_t ovl_rect;   //!< Rectangle in the source frame     */
3531   int dst_x;                     //!< x offset in the destination frame. */
3532   int dst_y;                     //!< y offset in the destination frame. */
3533   } gavl_overlay_t;
3534 
3535 /*! \ingroup video_blend
3536  *  \brief Opaque blend context.
3537  *
3538  *  You don't want to know what's inside.
3539  */
3540 
3541 typedef struct gavl_overlay_blend_context_s gavl_overlay_blend_context_t;
3542 
3543 /*! \ingroup video_blend
3544  *  \brief Create a blend context
3545  *  \returns A newly allocated blend context.
3546  */
3547 
3548 GAVL_PUBLIC
3549 gavl_overlay_blend_context_t * gavl_overlay_blend_context_create();
3550 
3551 /*! \ingroup video_blend
3552  *  \brief Destroy a blend context and free all associated memory
3553  *  \param ctx A blend context
3554  */
3555 
3556 GAVL_PUBLIC
3557 void gavl_overlay_blend_context_destroy(gavl_overlay_blend_context_t * ctx);
3558 
3559 /*! \ingroup video_blend
3560  *  \brief Get options from a blend context
3561  *  \param ctx A blend context
3562  *  \returns Options (See \ref video_options)
3563  */
3564 
3565 GAVL_PUBLIC gavl_video_options_t *
3566 gavl_overlay_blend_context_get_options(gavl_overlay_blend_context_t * ctx);
3567 
3568 /*! \ingroup video_blend
3569  *  \brief Initialize the blend context
3570  *  \param ctx A blend context
3571  *  \param frame_format The format of the destination frames
3572  *  \param overlay_format The format of the overlays
3573  *
3574  *  Initialize a blend context for a given frame- and overlayformat.
3575  *  The image_width and image_height members for the overlay format represent
3576  *  the maximum overlay size. The actual displayed size will be determined
3577  *  by the ovl_rect of the overlay.
3578  *  The overlay_format might be changed to something, which can directly be blended.
3579  *  Make sure you have a \ref gavl_video_converter_t nearby.
3580  *
3581  */
3582 
3583 GAVL_PUBLIC
3584 int gavl_overlay_blend_context_init(gavl_overlay_blend_context_t * ctx,
3585                                     const gavl_video_format_t * frame_format,
3586                                     gavl_video_format_t * overlay_format);
3587 
3588 /*! \ingroup video_blend
3589  *  \brief Set a new overlay
3590  *  \param ctx A blend context
3591  *  \param ovl An overlay
3592  *
3593  *  This function sets a new overlay, regardless of whether the last one has expired
3594  *  or not.
3595  */
3596 
3597 GAVL_PUBLIC
3598 void gavl_overlay_blend_context_set_overlay(gavl_overlay_blend_context_t * ctx,
3599                                             gavl_overlay_t * ovl);
3600 
3601 /*! \ingroup video_blend
3602  *  \brief Blend overlay onto video frame
3603  *  \param ctx A blend context
3604  *  \param dst_frame Destination frame
3605  */
3606 
3607 GAVL_PUBLIC
3608 void gavl_overlay_blend(gavl_overlay_blend_context_t * ctx,
3609                         gavl_video_frame_t * dst_frame);
3610 
3611 /*! \defgroup video_transform Image transformation
3612  * \ingroup video
3613  *
3614  * gavl includes a generic image transformation engine.
3615  * You pass a function pointer to the init function, which
3616  * transforms the destination coordinates into source
3617  * coordinates.
3618  *
3619  * The interpolation method is set with
3620  * \ref gavl_video_options_set_scale_mode, but not all modes
3621  * are supported. When initialized with an invalid scale mode,
3622  * the transformation engine will silently choose the closest one.
3623  *
3624  * @{
3625  */
3626 
3627 /** \brief Opaque image transformation engine.
3628  *
3629  * You don't want to know what's inside.
3630  */
3631 
3632 typedef struct gavl_image_transform_s gavl_image_transform_t;
3633 
3634 /** \brief Function describing the method
3635  *  \param priv User data
3636  *  \param xdst X-coordinate of the destination
3637  *  \param ydst Y-coordinate of the destination
3638  *  \param xsrc Returns X-coordinate of the source
3639  *  \param ysrc Returns Y-coordinate of the source
3640  *
3641  *  All coordinates are in fractional pixels. 0,0 is the
3642  *  upper left corner. Return negative values or values
3643  *  larger than the dimesion to signal that a pixel is outside
3644  *  the source image.
3645  */
3646 
3647 typedef void (*gavl_image_transform_func)(void * priv,
3648                                           double xdst,
3649                                           double ydst,
3650                                           double * xsrc,
3651                                           double * ysrc);
3652 
3653 
3654 /** \brief Create a transformation engine
3655  *  \returns A newly allocated transformation engine
3656  *
3657  * Since 1.1.0.
3658  */
3659 
3660 GAVL_PUBLIC
3661 gavl_image_transform_t * gavl_image_transform_create();
3662 
3663 /** \brief Destroy a transformation engine
3664  *  \param t A transformation engine
3665  * Since 1.1.0.
3666  */
3667 
3668 GAVL_PUBLIC
3669 void gavl_image_transform_destroy(gavl_image_transform_t * t);
3670 
3671 /** \brief Initialize a transformation engine
3672  *  \param t A transformation engine
3673  *  \param format Format (can be changed)
3674  *  \param func Coordinate transform function
3675  *  \param priv The priv argument for func
3676  *  \returns 1 if the transform was sucessfully initialized, 0 else.
3677 
3678  * If you enabled multithreading support, func will be called
3679  * from multiple threads at the same time. Make sure, that it
3680  * doesn't access any global data.
3681  *
3682  * Return type was changed from void to in in version 1.1.2
3683  * Under normal circumstances, this function always returns 1.
3684  *
3685  * Since 1.1.0.
3686  */
3687 
3688 
3689 GAVL_PUBLIC
3690 int gavl_image_transform_init(gavl_image_transform_t * t,
3691                               gavl_video_format_t * format,
3692                               gavl_image_transform_func func, void * priv);
3693 
3694 /** \brief Transform an image
3695  *  \param t A transformation engine
3696  *  \param in_frame Input frame
3697  *  \param out_frame Output frame
3698  * Since 1.1.0.
3699  */
3700 
3701 GAVL_PUBLIC
3702 void gavl_image_transform_transform(gavl_image_transform_t * t,
3703                                     gavl_video_frame_t * in_frame,
3704                                     gavl_video_frame_t * out_frame);
3705 
3706 /** \brief Get transformation options
3707  *  \param t A transformation engine
3708  *  \returns Options
3709  *
3710  * After you called this, you can use the gavl_video_options_set_*() functions to change
3711  * the options. Options will become valid with the next call to \ref gavl_image_transform_init.
3712  *
3713  * Since 1.1.0.
3714  */
3715 
3716 GAVL_PUBLIC gavl_video_options_t *
3717 gavl_image_transform_get_options(gavl_image_transform_t * t);
3718 
3719 /**
3720  * @}
3721  */
3722 
3723 /*! \defgroup frame_table Frame table
3724  * \ingroup video
3725  *
3726  * This is a table, which tolds the complete timing information
3727  * of a video sequence. If is meant for e.g. for editing applications,
3728  * where the complete timing must be known in advance.
3729  *
3730  * A frame table is always associated with a \ref gavl_video_format_t,
3731  * which must be passed to most functions.
3732  *
3733  * @{
3734  */
3735 
3736 /** \brief frame table structure
3737  *
3738  * Since 1.1.2.
3739  */
3740 
3741 typedef struct
3742   {
3743   int64_t offset; //!< Timestamp of the first frame
3744   /* Primary */
3745   int64_t num_entries; //!< Number of entries
3746   int64_t entries_alloc; //!< Number of allocated entries (never touch this)
3747 
3748   struct
3749     {
3750     int64_t num_frames; //!< Number of frames
3751     int64_t duration;   //!< Duration of each of these frames
3752     } * entries;        //!< Frame table
3753 
3754   int num_timecodes; //!< Number of timecodes
3755   int timecodes_alloc; //!< Number of allocated timecodes (never touch this)
3756 
3757   struct
3758     {
3759     int64_t pts;          //!< Timestamp of this frame
3760     gavl_timecode_t tc;   //!< Timecode associated with this timestamp
3761     } * timecodes;        //!< Timecode table
3762 
3763   /* Secondary */
3764 
3765   } gavl_frame_table_t;
3766 
3767 /** \brief Create a frame table
3768  *  \returns A newly allocated frame table
3769  *
3770  * Since 1.1.2.
3771  */
3772 GAVL_PUBLIC gavl_frame_table_t * gavl_frame_table_create();
3773 
3774 /** \brief Create a frame table for an audio stream
3775  *  \param samplerate Samplerate for this stream
3776  *  \param offset PTS offset of this stream in samples
3777  *  \param duration Sample count
3778  *  \param fmt_ret If non-null, returns the timecode format
3779  *  \returns A newly allocated frame table
3780  *
3781  * Since 1.1.2.
3782  */
3783 
3784 GAVL_PUBLIC gavl_frame_table_t *
3785 gavl_frame_table_create_audio(int samplerate, int64_t offset, int64_t duration,
3786                               gavl_timecode_format_t * fmt_ret);
3787 
3788 /** \brief Create a frame table for constant framerate video
3789  *  \param offset Timestamp of the first frame
3790  *  \param frame_duration Duration of each frame
3791  *  \param num_frames Number of frames
3792  *  \param start_timecode Timecode of the first frame (or GAVL_TIMECODE_UNDEFINED)
3793  *  \returns A newly allocated frame table
3794  *
3795  * Since 1.1.2.
3796  */
3797 
3798 GAVL_PUBLIC gavl_frame_table_t *
3799 gavl_frame_table_create_cfr(int64_t offset, int64_t frame_duration,
3800                             int64_t num_frames,
3801                             gavl_timecode_t start_timecode);
3802 
3803 /** \brief Copy a frame table to another
3804  *  \param tab A frame table
3805  *  \returns A newly allocated copy
3806  *
3807  * Since 1.1.2.
3808  */
3809 
3810 GAVL_PUBLIC gavl_frame_table_t *
3811 gavl_frame_table_copy(const gavl_frame_table_t * tab);
3812 
3813 
3814 
3815 /** \brief Destroy a frame table and free all memory
3816  *  \param t A frame table
3817  *
3818  * Since 1.1.2.
3819  */
3820 
3821 GAVL_PUBLIC void gavl_frame_table_destroy(gavl_frame_table_t * t);
3822 
3823 /** \brief Append an entry
3824  *  \param t A frame table
3825  *  \param duration The duration of this frame
3826  *
3827  * Since 1.1.2.
3828  */
3829 
3830 GAVL_PUBLIC void gavl_frame_table_append_entry(gavl_frame_table_t * t, int64_t duration);
3831 
3832 /** \brief Append a timecodes
3833  *  \param t A frame table
3834  *  \param pts Presentation time of that frame
3835  *  \param tc  Timecode of that frame
3836  *
3837  * Since 1.1.2.
3838  */
3839 
3840 GAVL_PUBLIC void
3841 gavl_frame_table_append_timecode(gavl_frame_table_t * t,
3842                                  int64_t pts, gavl_timecode_t tc);
3843 
3844 /** \brief Convert a frame index to a timestamp
3845  *  \param t A frame table
3846  *  \param frame Frame index (starting with zero)
3847  *  \param duration If non NULL, returns the duration of that frame
3848  *  \returns The timestamp of that frame in video timescale or
3849  *           GAVL_TIME_UNDEFINED if such frame doesn't exist.
3850  *
3851  * Since 1.1.2.
3852  */
3853 
3854 GAVL_PUBLIC int64_t
3855 gavl_frame_table_frame_to_time(const gavl_frame_table_t * t,
3856                                int64_t frame, int * duration);
3857 
3858 /** \brief Convert a timestamp to a frame index
3859  *  \param t A frame table
3860  *  \param time Time in stream timescale
3861  *  \param start_time If non NULL, returns the start time of that frame
3862  *  \returns The index that frame (starting with 0)
3863  *           or -1 if such frame doesn't exist.
3864  *
3865  * Since 1.1.2.
3866  */
3867 
3868 GAVL_PUBLIC int64_t
3869 gavl_frame_table_time_to_frame(const gavl_frame_table_t * t,
3870                                int64_t time,
3871                                int64_t * start_time);
3872 
3873 /** \brief Convert a timestamp to a timecode
3874  *  \param t A frame table
3875  *  \param time Time in stream timescale
3876  *  \param start_time If non NULL, returns the start time of that frame
3877  *  \param fmt Timecode format
3878  *  \returns The interpolated timecode that frame or GAVL_TIMECODE_UNDEFINED if such frame doesn't exist.
3879  *
3880  * Since 1.1.2.
3881  */
3882 
3883 GAVL_PUBLIC gavl_timecode_t
3884 gavl_frame_table_time_to_timecode(const gavl_frame_table_t * t,
3885                                   int64_t time,
3886                                   int64_t * start_time,
3887                                   const gavl_timecode_format_t * fmt);
3888 
3889 /** \brief Convert a timecode to a timestamp
3890  *  \param t A frame table
3891  *  \param tc Timecode
3892  *  \param fmt Timecode format
3893  *  \returns The pts corresponding to that timecode or GAVL_TIME_UNDEFINED if such frame doesn't exist.
3894  *
3895  * Since 1.1.2.
3896  */
3897 
3898 GAVL_PUBLIC int64_t
3899 gavl_frame_table_timecode_to_time(const gavl_frame_table_t * t,
3900                                   gavl_timecode_t tc,
3901                                   const gavl_timecode_format_t * fmt);
3902 
3903 
3904 /** \brief Convert a frame index to a timecode
3905  *  \param t A frame table
3906  *  \param frame Frame index
3907  *  \param start_time If non NULL, returns the start time of that frame
3908  *  \param fmt Timecode format
3909  *  \returns The interpolated timecode that frame or GAVL_TIMECODE_UNDEFINED if such frame doesn't exist.
3910  *
3911  * Since 1.1.2.
3912  */
3913 
3914 GAVL_PUBLIC gavl_timecode_t
3915 gavl_frame_table_frame_to_timecode(const gavl_frame_table_t * t,
3916                                    int64_t frame,
3917                                    int64_t * start_time,
3918                                    const gavl_timecode_format_t * fmt);
3919 
3920 
3921 
3922 /** \brief get the total number of frames
3923  *  \param t A frame table
3924  *  \returns The total number of frames
3925  *
3926  * Since 1.1.2.
3927  */
3928 
3929 GAVL_PUBLIC int64_t
3930 gavl_frame_table_num_frames(const gavl_frame_table_t * t);
3931 
3932 /** \brief get the total duration of all frames
3933  *  \param t A frame table
3934  *  \returns Total duration
3935  *
3936  * Since 1.1.2.
3937  */
3938 
3939 GAVL_PUBLIC int64_t
3940 gavl_frame_table_duration(const gavl_frame_table_t * t);
3941 
3942 /** \brief get the end time of the last frame
3943  *  \param t A frame table
3944  *  \returns End time
3945  *
3946  * Since 1.1.2.
3947  */
3948 
3949 GAVL_PUBLIC int64_t
3950 gavl_frame_table_end_time(const gavl_frame_table_t * t);
3951 
3952 /** \brief Save a frame table to a file
3953  *  \param t Tab frame table
3954  *  \param filename Filename
3955  *  \returns 1 on success, 0 on error
3956  *
3957  * Since 1.1.2.
3958  */
3959 
3960 GAVL_PUBLIC
3961 int gavl_frame_table_save(const gavl_frame_table_t * t,
3962                           const char * filename);
3963 
3964 /** \brief Load a frame table from a file
3965  *  \param filename Filename
3966  *  \returns The loaded frame table or NULL if an error occurred
3967  *
3968  * Since 1.1.2.
3969  */
3970 
3971 GAVL_PUBLIC
3972 gavl_frame_table_t * gavl_frame_table_load(const char * filename);
3973 
3974 /** \brief Dump a frame table to stderr for debugging
3975  *  \param t A frame table
3976  *
3977  * Since 1.1.2.
3978  */
3979 
3980 GAVL_PUBLIC void
3981 gavl_frame_table_dump(const gavl_frame_table_t * t);
3982 
3983 
3984 
3985 
3986 
3987 
3988 /**
3989  * @}
3990  */
3991 
3992 
3993 #ifdef __cplusplus
3994 }
3995 #endif
3996 
3997 #endif /* GAVL_H_INCLUDED */
3998