1 /* common.h
2 
3    Copyright (c) 2003-2021 HandBrake Team
4    This file is part of the HandBrake source code
5    Homepage: <http://handbrake.fr/>.
6    It may be used under the terms of the GNU General Public License v2.
7    For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8  */
9 
10 #ifndef HANDBRAKE_COMMON_H
11 #define HANDBRAKE_COMMON_H
12 
13 #include "handbrake/project.h"
14 #include "handbrake/hbtypes.h"
15 #include "handbrake/hb_dict.h"
16 #include <math.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <inttypes.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <dirent.h>
26 
27 /*
28  * It seems WinXP doesn't align the stack of new threads to 16 bytes.
29  * To prevent crashes in SSE functions, we need to force stack alignment
30  * of new threads.
31  */
32 #if defined( __GNUC__ ) && (defined( _WIN32 ) || defined( __MINGW32__ ))
33 #    define attribute_align_thread __attribute__((force_align_arg_pointer))
34 #else
35 #    define attribute_align_thread
36 #endif
37 
38 #if defined( __GNUC__ ) && !(defined( _WIN32 ) || defined( __MINGW32__ ))
39 #   define HB_WPRINTF(s,v) __attribute__((format(printf,s,v)))
40 #else
41 #   define HB_WPRINTF(s,v)
42 #endif
43 
44 #if defined( SYS_MINGW )
45 #   define fseek fseeko64
46 #   define ftell ftello64
47 #   undef  fseeko
48 #   define fseeko fseeko64
49 #   undef  ftello
50 #   define ftello ftello64
51 #   define flockfile(...)
52 #   define funlockfile(...)
53 #   define getc_unlocked getc
54 #   undef  off_t
55 #   define off_t off64_t
56 #endif
57 
58 #ifndef MIN
59 #define MIN(a,b) (((a)<(b))?(a):(b))
60 #endif
61 #ifndef MAX
62 #define MAX(a,b) (((a)>(b))?(a):(b))
63 #endif
64 #ifndef ABS
65 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
66 #endif
67 
68 #define HB_ALIGN(x, a) (((x)+(a)-1)&~((a)-1))
69 
70 #ifndef HB_DEBUG_ASSERT
71 #define HB_DEBUG_ASSERT(x, y) { if ((x)) { hb_error("ASSERT: %s", y); exit(1); } }
72 #endif
73 
74 #define EVEN( a )               ((a) + ((a) & 1))
75 #define MULTIPLE_MOD(a, b)      (((b) * (int)(((a) + ((b) / 2)) / (b))))
76 #define MULTIPLE_MOD_UP(a, b)   (((b) * (int)(((a) + ((b) - 1)) / (b))))
77 #define MULTIPLE_MOD_DOWN(a, b) (((b) * (int)((a) / (b))))
78 
79 #define HB_DVD_READ_BUFFER_SIZE 2048
80 
81 #define HB_MIN_WIDTH    32
82 #define HB_MIN_HEIGHT   32
83 #define HB_MAX_WIDTH    20480
84 #define HB_MAX_HEIGHT   20480
85 
86 typedef enum
87 {
88      HB_ERROR_NONE         = 0,
89      HB_ERROR_CANCELED     = 1,
90      HB_ERROR_WRONG_INPUT  = 2,
91      HB_ERROR_INIT         = 3,
92      HB_ERROR_UNKNOWN      = 4,
93      HB_ERROR_READ         = 5
94 } hb_error_code;
95 
96 #include "ports.h"
97 #ifdef __LIBHB__
98 #include "internal.h"
99 #define PRIVATE
100 #else
101 #define PRIVATE const
102 #endif
103 #include "audio_remap.h"
104 #include "libavutil/channel_layout.h"
105 
106 #if HB_PROJECT_FEATURE_QSV
107 #include "qsv_libav.h"
108 #endif
109 
110 #ifdef __LIBHB__
111 struct hb_buffer_list_s
112 {
113     hb_buffer_t *head;
114     hb_buffer_t *tail;
115     int count;
116     int size;
117 };
118 
119 void hb_buffer_list_append(hb_buffer_list_t *list, hb_buffer_t *buf);
120 void hb_buffer_list_prepend(hb_buffer_list_t *list, hb_buffer_t *buf);
121 hb_buffer_t* hb_buffer_list_head(hb_buffer_list_t *list);
122 hb_buffer_t* hb_buffer_list_rem_head(hb_buffer_list_t *list);
123 hb_buffer_t* hb_buffer_list_tail(hb_buffer_list_t *list);
124 hb_buffer_t* hb_buffer_list_rem_tail(hb_buffer_list_t *list);
125 hb_buffer_t* hb_buffer_list_rem(hb_buffer_list_t *list, hb_buffer_t * b);
126 hb_buffer_t* hb_buffer_list_clear(hb_buffer_list_t *list);
127 hb_buffer_t* hb_buffer_list_set(hb_buffer_list_t *list, hb_buffer_t *buf);
128 void hb_buffer_list_close(hb_buffer_list_t *list);
129 int hb_buffer_list_count(hb_buffer_list_t *list);
130 int hb_buffer_list_size(hb_buffer_list_t *list);
131 #endif // __LIBHB__
132 
133 hb_list_t * hb_list_init(void);
134 int         hb_list_count( const hb_list_t * );
135 void        hb_list_add( hb_list_t *, void * );
136 void        hb_list_insert( hb_list_t * l, int pos, void * p );
137 void        hb_list_rem( hb_list_t *, void * );
138 void      * hb_list_item( const hb_list_t *, int );
139 void        hb_list_close( hb_list_t ** );
140 
141 void hb_reduce( int *x, int *y, int num, int den );
142 void hb_limit_rational( int *x, int *y, int64_t num, int64_t den, int limit );
143 void hb_reduce64( int64_t *x, int64_t *y, int64_t num, int64_t den );
144 void hb_limit_rational64( int64_t *x, int64_t *y, int64_t num, int64_t den, int64_t limit );
145 
146 void hb_job_set_encoder_preset (hb_job_t *job, const char *preset);
147 void hb_job_set_encoder_tune   (hb_job_t *job, const char *tune);
148 void hb_job_set_encoder_options(hb_job_t *job, const char *options);
149 void hb_job_set_encoder_profile(hb_job_t *job, const char *profile);
150 void hb_job_set_encoder_level  (hb_job_t *job, const char *level);
151 void hb_job_set_file           (hb_job_t *job, const char *file);
152 
153 hb_audio_t *hb_audio_copy(const hb_audio_t *src);
154 hb_list_t *hb_audio_list_copy(const hb_list_t *src);
155 void hb_audio_close(hb_audio_t **audio);
156 void hb_audio_config_init(hb_audio_config_t * audiocfg);
157 int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg);
158 hb_audio_config_t * hb_list_audio_config_item(hb_list_t * list, int i);
159 
160 int hb_subtitle_add_ssa_header(hb_subtitle_t *subtitle, const char *font,
161                                int fs, int width, int height);
162 hb_subtitle_t *hb_subtitle_copy(const hb_subtitle_t *src);
163 hb_list_t *hb_subtitle_list_copy(const hb_list_t *src);
164 void hb_subtitle_close( hb_subtitle_t **sub );
165 int hb_subtitle_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg, int track);
166 int hb_import_subtitle_add( const hb_job_t * job,
167                 const hb_subtitle_config_t * subtitlecfg,
168                 const char *lang_code, int source );
169 int hb_srt_add(const hb_job_t * job, const hb_subtitle_config_t * subtitlecfg,
170                const char *lang);
171 int hb_subtitle_can_force( int source );
172 int hb_subtitle_can_burn( int source );
173 int hb_subtitle_can_pass( int source, int mux );
174 
175 int hb_audio_can_apply_drc(uint32_t codec, uint32_t codec_param, int encoder);
176 int hb_audio_can_apply_drc2(hb_handle_t *h, int title_idx,
177                             int audio_idx, int encoder);
178 
179 hb_attachment_t *hb_attachment_copy(const hb_attachment_t *src);
180 
181 hb_list_t *hb_attachment_list_copy(const hb_list_t *src);
182 void hb_attachment_close(hb_attachment_t **attachment);
183 
184 hb_metadata_t * hb_metadata_init(void);
185 hb_metadata_t * hb_metadata_copy(const hb_metadata_t *src);
186 void hb_metadata_close(hb_metadata_t **metadata);
187 void hb_update_meta_dict(hb_dict_t * dict, const char * key, const char * value);
188 const char * hb_lookup_meta_key(const char * mux_key);
189 void hb_metadata_add_coverart( hb_metadata_t *metadata, const uint8_t *data, int size, int type );
190 void hb_metadata_rem_coverart( hb_metadata_t *metadata, int ii );
191 
192 hb_chapter_t *hb_chapter_copy(const hb_chapter_t *src);
193 hb_list_t *hb_chapter_list_copy(const hb_list_t *src);
194 void hb_chapter_close(hb_chapter_t **chapter);
195 void hb_chapter_set_title(hb_chapter_t *chapter, const char *title);
196 
197 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_rate_s.cs when changing this struct
198 struct hb_rate_s
199 {
200     const char *name;
201     int         rate;
202 };
203 
204 struct hb_dither_s
205 {
206     const char *description;
207     const char *short_name;
208     int         method;
209 };
210 
211 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_mixdown_s.cs when changing this struct
212 struct hb_mixdown_s
213 {
214     const char *name;
215     const char *short_name;
216     int         amixdown;
217 };
218 
219 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_encoder_s.cs when changing this struct
220 struct hb_encoder_s
221 {
222     const char *name;       // note: used in presets
223     const char *short_name; // note: used in CLI
224     const char *long_name;  // used in log
225     int         codec;      // HB_*CODEC_* define
226     int         muxers;     // supported muxers
227 };
228 
229 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_container_s.cs when changing this struct
230 struct hb_container_s
231 {
232     const char *name;
233     const char *short_name;
234     const char *long_name;
235     const char *default_extension;
236     int         format;
237 };
238 
239 struct hb_rational_s
240 {
241     int num;
242     int den;
243 };
244 
hb_make_q(int num,int den)245 static inline hb_rational_t hb_make_q(int num, int den)
246 {
247     hb_rational_t r = { num, den };
248     return r;
249 }
250 
hb_q2d(hb_rational_t a)251 static inline double hb_q2d(hb_rational_t a){
252     return a.num / (double) a.den;
253 }
254 
255 struct hb_geometry_s
256 {
257     int width;
258     int height;
259     hb_rational_t par;
260 };
261 
262 struct hb_geometry_crop_s
263 {
264     int crop[4];
265     int pad[4];
266     hb_geometry_t geometry;
267 };
268 
269 // hb_geometry_settings_s 'keep' flags
270 #define HB_KEEP_WIDTH           0x01
271 #define HB_KEEP_HEIGHT          0x02
272 #define HB_KEEP_DISPLAY_ASPECT  0x04
273 #define HB_KEEP_DISPLAY_WIDTH   0x08
274 #define HB_KEEP_PAD             0x10
275 
276 // hb_geometry_settings_s 'flags'
277 #define HB_GEO_SCALE_UP         0x01
278 #define HB_GEO_SCALE_BEST       0x02
279 
280 struct hb_geometry_settings_s
281 {
282     int mode;               // Anamorphic mode, see job struct anamorphic
283     int keep;               // Specifies settings that shouldn't be changed
284     int flags;              // Flags that affect calculations
285     int itu_par;            // use dvd dimensions to determine PAR
286     int modulus;            // pixel alignment for loose anamorphic
287     int crop[4];            // Pixels cropped from source before scaling
288     int pad[4];             // Pixels cropped from source before scaling
289     int maxWidth;           // max destination storage width
290     int maxHeight;          // max destination storage height
291     int displayWidth;       // display width, used with !HB_KEEP_DISPLAY_ASPECT
292     int displayHeight;      // display height, used with !HB_KEEP_DISPLAY_ASPECT
293                             // Note that display size includes pad!
294     hb_geometry_t geometry;
295 };
296 
297 // Update win/CS/HandBrake.Interop/Interop/HbLib/hb_image_s.cs when changing this struct
298 struct hb_image_s
299 {
300     int format;
301     int max_plane;
302     int width;
303     int height;
304     int color_prim;
305     int color_transfer;
306     int color_matrix;
307     uint8_t *data;
308 
309     struct image_plane
310     {
311         uint8_t *data;
312         int width;
313         int height;
314         int stride;
315         int height_stride;
316         int size;
317     } plane[4];
318 };
319 
320 void hb_image_close(hb_image_t **_image);
321 
322 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_subtitle_config_s.cs when changing this struct
323 struct hb_subtitle_config_s
324 {
325     enum subdest { RENDERSUB, PASSTHRUSUB } dest;
326     int          force;
327     int          default_track;
328     const char * name;
329 
330     /* SRT subtitle tracks only */
331     const char * src_filename;
332     char         src_codeset[40];
333     int64_t      offset;
334 };
335 
336 struct hb_mastering_display_metadata_s {
337     hb_rational_t display_primaries[3][2];
338     hb_rational_t white_point[2];
339     hb_rational_t min_luminance;
340     hb_rational_t max_luminance;
341     int has_primaries;
342     int has_luminance;
343 };
344 
345 struct hb_content_light_metadata_s {
346     unsigned max_cll;
347     unsigned max_fall;
348 };
349 
350 /*******************************************************************************
351  * Lists of rates, mixdowns, encoders etc.
352  *******************************************************************************
353  *
354  * Use hb_*_get_next() to get the next list item (use NULL to get the first).
355  *
356  * Use hb_*_get_from_name() to get the value corresponding to a name.
357  * The name can be either the short or full name.
358  * Legacy names are sanitized to currently-supported values whenever possible.
359  * Returns 0 or -1 if no value could be found.
360  *
361  * Use hb_*_get_name() and hb_*_get_short_name() to get the corresponding value.
362  * Returns NULL if the value is invalid.
363  *
364  * Use hb_*_get_long_name() when the name is not descriptive enough for you.
365  *
366  * hb_*_sanitize_name() are convenience functions for use when dealing
367  * with full names (e.g. to translate legacy values while loading a preset).
368  *
369  * Names are case-insensitive; libhb will ensure that the lists do not contain
370  * more than one entry with the same name.
371  *
372  * Use hb_*_get_limits() to get the minimum/maximum for lists with numerically
373  * ordered values.
374  *
375  * Use hb_*_get_best() to sanitize a value based on other relevant parameters.
376  *
377  * Use hb_*_get_default() to get the default based on other relevant parameters.
378  *
379  */
380 
381 void hb_common_global_init(int);
382 
383 int              hb_video_framerate_get_from_name(const char *name);
384 const char*      hb_video_framerate_get_name(int framerate);
385 const char*      hb_video_framerate_sanitize_name(const char *name);
386 void             hb_video_framerate_get_limits(int *low, int *high, int *clock);
387 const hb_rate_t* hb_video_framerate_get_next(const hb_rate_t *last);
388 int              hb_video_framerate_get_close(hb_rational_t *framerate,
389                                               double thresh);
390 
391 int              hb_audio_samplerate_is_supported(int samplerate,
392                                                   uint32_t codec);
393 int              hb_audio_samplerate_find_closest(int samplerate,
394                                                   uint32_t codec);
395 int              hb_audio_samplerate_get_sr_shift(int samplerate);
396 int              hb_audio_samplerate_get_from_name(const char *name);
397 const char*      hb_audio_samplerate_get_name(int samplerate);
398 const hb_rate_t* hb_audio_samplerate_get_next(const hb_rate_t *last);
399 const hb_rate_t* hb_audio_samplerate_get_next_for_codec(const hb_rate_t *last,
400                                                         uint32_t codec);
401 
402 int              hb_audio_bitrate_get_best(uint32_t codec, int bitrate, int samplerate, int mixdown);
403 int              hb_audio_bitrate_get_default(uint32_t codec, int samplerate, int mixdown);
404 void             hb_audio_bitrate_get_limits(uint32_t codec, int samplerate, int mixdown, int *low, int *high);
405 const hb_rate_t* hb_audio_bitrate_get_next(const hb_rate_t *last);
406 
407 void        hb_video_quality_get_limits(uint32_t codec, float *low, float *high, float *granularity, int *direction);
408 const char* hb_video_quality_get_name(uint32_t codec);
409 int         hb_video_quality_is_supported(uint32_t codec);
410 
411 int                hb_video_encoder_is_supported(int encoder);
412 int                hb_video_encoder_pix_fmt_is_supported(int encoder, int pix_fmt);
413 int                hb_video_encoder_get_depth   (int encoder);
414 const char* const* hb_video_encoder_get_presets (int encoder);
415 const char* const* hb_video_encoder_get_tunes   (int encoder);
416 const char* const* hb_video_encoder_get_profiles(int encoder);
417 const char* const* hb_video_encoder_get_levels  (int encoder);
418 const int*         hb_video_encoder_get_pix_fmts(int encoder);
419 
420 void  hb_audio_quality_get_limits(uint32_t codec, float *low, float *high, float *granularity, int *direction);
421 float hb_audio_quality_get_best(uint32_t codec, float quality);
422 float hb_audio_quality_get_default(uint32_t codec);
423 
424 void  hb_audio_compression_get_limits(uint32_t codec, float *low, float *high, float *granularity, int *direction);
425 float hb_audio_compression_get_best(uint32_t codec, float compression);
426 float hb_audio_compression_get_default(uint32_t codec);
427 
428 int                hb_audio_dither_get_default(void);
429 int                hb_audio_dither_get_default_method(void); // default method, if enabled && supported
430 int                hb_audio_dither_is_supported(uint32_t codec, int depth);
431 int                hb_audio_dither_get_from_name(const char *name);
432 const char*        hb_audio_dither_get_description(int method);
433 const hb_dither_t* hb_audio_dither_get_next(const hb_dither_t *last);
434 
435 int                 hb_mixdown_is_supported(int mixdown, uint32_t codec, uint64_t layout);
436 int                 hb_mixdown_has_codec_support(int mixdown, uint32_t codec);
437 int                 hb_mixdown_has_remix_support(int mixdown, uint64_t layout);
438 int                 hb_mixdown_get_discrete_channel_count(int mixdown);
439 int                 hb_mixdown_get_low_freq_channel_count(int mixdown);
440 int                 hb_mixdown_get_best(uint32_t codec, uint64_t layout, int mixdown);
441 int                 hb_mixdown_get_default(uint32_t codec, uint64_t layout);
442 hb_mixdown_t*       hb_mixdown_get_from_mixdown(int mixdown);
443 int                 hb_mixdown_get_from_name(const char *name);
444 const char*         hb_mixdown_get_name(int mixdown);
445 const char*         hb_mixdown_get_short_name(int mixdown);
446 const char*         hb_mixdown_sanitize_name(const char *name);
447 const hb_mixdown_t* hb_mixdown_get_next(const hb_mixdown_t *last);
448 
449 void                hb_layout_get_name(char * name, int size, int64_t layout);
450 int                 hb_layout_get_discrete_channel_count(int64_t layout);
451 int                 hb_layout_get_low_freq_channel_count(int64_t layout);
452 
453 int                 hb_video_encoder_get_default(int muxer);
454 hb_encoder_t*       hb_video_encoder_get_from_codec(int codec);
455 int                 hb_video_encoder_get_from_name(const char *name);
456 const char*         hb_video_encoder_get_name(int encoder);
457 const char*         hb_video_encoder_get_short_name(int encoder);
458 const char*         hb_video_encoder_get_long_name(int encoder);
459 const char*         hb_video_encoder_sanitize_name(const char *name);
460 const hb_encoder_t* hb_video_encoder_get_next(const hb_encoder_t *last);
461 
462 /*
463  * hb_audio_encoder_get_fallback_for_passthru() will sanitize a passthru codec
464  * to the matching audio encoder (if any is available).
465  *
466  * hb_audio_encoder_get_from_name(), hb_audio_encoder_sanitize_name() will
467  * sanitize legacy encoder names, but won't convert passthru to an encoder.
468  */
469 int                 hb_audio_encoder_get_fallback_for_passthru(int passthru);
470 int                 hb_audio_encoder_get_default(int muxer);
471 hb_encoder_t*       hb_audio_encoder_get_from_codec(int codec);
472 int                 hb_audio_encoder_get_from_name(const char *name);
473 const char*         hb_audio_encoder_get_name(int encoder);
474 const char*         hb_audio_encoder_get_short_name(int encoder);
475 const char*         hb_audio_encoder_get_long_name(int encoder);
476 const char*         hb_audio_encoder_sanitize_name(const char *name);
477 const hb_encoder_t* hb_audio_encoder_get_next(const hb_encoder_t *last);
478 
479 const char*         hb_audio_decoder_get_name(int codec, int codec_param);
480 
481 /*
482  * Not typically used by the UIs
483  * (set hb_job_t.acodec_copy_mask, hb_job_t.acodec_fallback instead).
484  */
485 void hb_autopassthru_apply_settings(hb_job_t *job);
486 void hb_autopassthru_print_settings(hb_job_t *job);
487 int  hb_autopassthru_get_encoder(int in_codec, int copy_mask, int fallback, int muxer);
488 
489 hb_container_t*       hb_container_get_from_format(int format);
490 int                   hb_container_get_from_name(const char *name);
491 int                   hb_container_get_from_extension(const char *extension); // not really a container name
492 const char*           hb_container_get_name(int format);
493 const char*           hb_container_get_short_name(int format);
494 const char*           hb_container_get_long_name(int format);
495 const char*           hb_container_get_default_extension(int format);
496 const char*           hb_container_sanitize_name(const char *name);
497 const hb_container_t* hb_container_get_next(const hb_container_t *last);
498 
499 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_title_set_s.cs when changing this struct
500 struct hb_title_set_s
501 {
502     hb_list_t   * list_title;
503     int           feature;    // Detected DVD feature title
504     const char  * path;
505 };
506 
507 typedef enum
508 {
509     HB_ANAMORPHIC_NONE,
510     HB_ANAMORPHIC_STRICT,
511     HB_ANAMORPHIC_LOOSE,
512     HB_ANAMORPHIC_CUSTOM,
513     HB_ANAMORPHIC_AUTO
514 } hb_anamorphic_mode_t;
515 
516 /******************************************************************************
517  * hb_job_t: settings to be filled by the UI
518  * Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_job_s.cs when changing this struct
519  *****************************************************************************/
520 struct hb_job_s
521 {
522     const char  * json;   // JSON encoded job string
523 
524     /* ID assigned by UI so it can groups job passes together */
525     int             sequence_id;
526 
527     /* Pointer to the title to be ripped */
528     hb_title_t    * title;
529     int             feature; // Detected DVD feature title
530 
531     /* Chapter selection */
532     int             chapter_start;
533     int             chapter_end;
534 
535     /* Include chapter marker track in mp4? */
536     int             chapter_markers;
537 
538     // Video filters
539     int             grayscale;      // Black and white encoding
540     hb_list_t     * list_filter;
541 
542     PRIVATE int             crop[4];
543     PRIVATE int             width;
544     PRIVATE int             height;
545     hb_rational_t           par;
546 
547     /* Video settings:
548          vcodec:            output codec
549          vquality:          output quality (if invalid, bitrate is used instead)
550          vbitrate:          output bitrate (Kbps)
551          vrate:             output framerate
552          cfr:               0 (vfr), 1 (cfr), 2 (pfr) [see render.c]
553          pass:              0, 1 or 2 (or -1 for scan)
554          areBframes:        boolean to note if b-frames are used */
555 #define HB_VCODEC_MASK         0x7FFFFFF
556 #define HB_VCODEC_INVALID      0x0000000
557 #define HB_VCODEC_THEORA       0x0000002
558 #define HB_VCODEC_FFMPEG_MPEG4 0x0000010
559 #define HB_VCODEC_FFMPEG_MPEG2 0x0000020
560 #define HB_VCODEC_FFMPEG_VP8   0x0000040
561 #define HB_VCODEC_FFMPEG_VP9   0x0000080
562 #define HB_VCODEC_FFMPEG_VCE_H264 0x00040000
563 #define HB_VCODEC_FFMPEG_VCE_H265 0x00080000
564 #define HB_VCODEC_FFMPEG_NVENC_H264 0x00100000
565 #define HB_VCODEC_FFMPEG_NVENC_H265 0x00200000
566 #define HB_VCODEC_FFMPEG_VT_H264 0x00400000
567 #define HB_VCODEC_FFMPEG_VT_H265 0x00800000
568 #define HB_VCODEC_FFMPEG_VT_H265_10BIT 0x01000000
569 #define HB_VCODEC_FFMPEG_MF_H264 0x02000000
570 #define HB_VCODEC_FFMPEG_MF_H265 0x04000000
571 #define HB_VCODEC_FFMPEG_MASK  (0x00000F0|HB_VCODEC_FFMPEG_VCE_H264|HB_VCODEC_FFMPEG_VCE_H265|HB_VCODEC_FFMPEG_NVENC_H264|HB_VCODEC_FFMPEG_NVENC_H265|HB_VCODEC_FFMPEG_VT_H264|HB_VCODEC_FFMPEG_VT_H265|HB_VCODEC_FFMPEG_VT_H265_10BIT|HB_VCODEC_FFMPEG_MF_H264|HB_VCODEC_FFMPEG_MF_H265)
572 #define HB_VCODEC_QSV_H264     0x0000100
573 #define HB_VCODEC_QSV_H265_8BIT     0x0000200
574 #define HB_VCODEC_QSV_H265_10BIT    0x0000400
575 #define HB_VCODEC_QSV_H265_MASK     0x0000600
576 #define HB_VCODEC_QSV_H265     HB_VCODEC_QSV_H265_8BIT
577 #define HB_VCODEC_QSV_MASK     0x0000F00
578 #define HB_VCODEC_X264_8BIT    0x0010000
579 #define HB_VCODEC_X264         HB_VCODEC_X264_8BIT
580 #define HB_VCODEC_X264_10BIT   0x0020000
581 #define HB_VCODEC_X264_MASK    0x0030000
582 #define HB_VCODEC_H264_MASK    (HB_VCODEC_X264_MASK|HB_VCODEC_QSV_H264|HB_VCODEC_FFMPEG_VCE_H264|HB_VCODEC_FFMPEG_NVENC_H264|HB_VCODEC_FFMPEG_VT_H264|HB_VCODEC_FFMPEG_MF_H264)
583 #define HB_VCODEC_X265_8BIT    0x0001000
584 #define HB_VCODEC_X265         HB_VCODEC_X265_8BIT
585 #define HB_VCODEC_X265_10BIT   0x0002000
586 #define HB_VCODEC_X265_12BIT   0x0004000
587 #define HB_VCODEC_X265_16BIT   0x0008000
588 #define HB_VCODEC_X265_MASK    0x000F000
589 #define HB_VCODEC_H265_MASK    (HB_VCODEC_X265_MASK|HB_VCODEC_QSV_H265_MASK|HB_VCODEC_FFMPEG_VCE_H265|HB_VCODEC_FFMPEG_NVENC_H265|HB_VCODEC_FFMPEG_VT_H265|HB_VCODEC_FFMPEG_VT_H265_10BIT|HB_VCODEC_FFMPEG_MF_H265)
590 
591 /* define an invalid CQ value compatible with all CQ-capable codecs */
592 #define HB_INVALID_VIDEO_QUALITY (-1000.)
593 
594     int             vcodec;
595     double          vquality;
596     int             vbitrate;
597     hb_rational_t   vrate;
598     // Some parameters that depend on vrate (like keyint) can't change
599     // between encoding passes. So orig_vrate is used to store the
600     // 1st pass rate.
601     hb_rational_t   orig_vrate;
602     int             cfr;
603     PRIVATE int     pass_id;
604     int             twopass;        // Enable 2-pass encode. Boolean
605     int             fastfirstpass;
606     char           *encoder_preset;
607     char           *encoder_tune;
608     char           *encoder_options;
609     char           *encoder_profile;
610     char           *encoder_level;
611     int             areBframes;
612 
613     // Pixel format from decoder to the end of the filters chain
614     int             input_pix_fmt;
615     // Pixel format from the end of filters chain to the encoder
616     int             output_pix_fmt;
617     int             color_prim;
618     int             color_transfer;
619     int             color_matrix;
620     int             color_range;
621 
622     int             color_prim_override;
623     int             color_transfer_override;
624     int             color_matrix_override;
625 // see https://developer.apple.com/library/content/technotes/tn2162/_index.html
626 //     https://developer.apple.com/library/content/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html#//apple_ref/doc/uid/TP40000939-CH205-125526
627 //     libav pixfmt.h
628 #define HB_COLR_PRI_BT709        1
629 #define HB_COLR_PRI_UNDEF        2
630 #define HB_COLR_PRI_BT470M       4
631 #define HB_COLR_PRI_EBUTECH      5 // use for bt470bg
632 #define HB_COLR_PRI_SMPTEC       6 // smpte170m
633 #define HB_COLR_PRI_SMPTE240M    7
634 #define HB_COLR_PRI_FILM         8 // Illuminant C
635 #define HB_COLR_PRI_BT2020       9
636 #define HB_COLR_PRI_SMPTE428     10
637 #define HB_COLR_PRI_SMPTE431     11
638 #define HB_COLR_PRI_SMPTE432     12
639 #define HB_COLR_PRI_JEDEC_P22    22
640 // 0, 3-4, 7-8, 10-65535: reserved/not implemented
641 #define HB_COLR_TRA_BT709        1 // also use for bt470m, bt470bg, smpte170m, bt2020_10 and bt2020_12
642 #define HB_COLR_TRA_UNDEF        2
643 #define HB_COLR_TRA_GAMMA22      4
644 #define HB_COLR_TRA_GAMMA28      5
645 #define HB_COLR_TRA_SMPTE170M    6
646 #define HB_COLR_TRA_SMPTE240M    7
647 #define HB_COLR_TRA_LINEAR       8
648 #define HB_COLR_TRA_LOG          9
649 #define HB_COLR_TRA_LOG_SQRT     10
650 #define HB_COLR_TRA_IEC61966_2_4 11
651 #define HB_COLR_TRA_BT1361_ECG   12
652 #define HB_COLR_TRA_IEC61966_2_1 13
653 #define HB_COLR_TRA_BT2020_10    14
654 #define HB_COLR_TRA_BT2020_12    15
655 #define HB_COLR_TRA_SMPTEST2084  16
656 #define HB_COLR_TRA_SMPTE428     17
657 #define HB_COLR_TRA_ARIB_STD_B67 18 //known as "Hybrid log-gamma"
658 // 0, 3-6, 8-15, 17-65535: reserved/not implemented
659 #define HB_COLR_MAT_RGB          0
660 #define HB_COLR_MAT_BT709        1
661 #define HB_COLR_MAT_UNDEF        2
662 #define HB_COLR_MAT_FCC          4
663 #define HB_COLR_MAT_BT470BG      5
664 #define HB_COLR_MAT_SMPTE170M    6 // also use for fcc and bt470bg
665 #define HB_COLR_MAT_SMPTE240M    7
666 #define HB_COLR_MAT_YCGCO        8
667 #define HB_COLR_MAT_BT2020_NCL   9
668 #define HB_COLR_MAT_BT2020_CL    10
669 #define HB_COLR_MAT_SMPTE2085    11
670 #define HB_COLR_MAT_CD_NCL       12 // chromaticity derived non-constant lum
671 #define HB_COLR_MAT_CD_CL        13 // chromaticity derived constant lum
672 #define HB_COLR_MAT_ICTCP        14 // ITU-R BT.2100-0, ICtCp
673 // 0, 3-5, 8, 11-65535: reserved/not implemented
674 
675     hb_mastering_display_metadata_t mastering;
676     hb_content_light_metadata_t coll;
677 
678     hb_list_t     * list_chapter;
679 
680     /* List of audio settings. */
681     hb_list_t     * list_audio;
682     int             acodec_copy_mask; // Auto Passthru allowed codecs
683     int             acodec_fallback;  // Auto Passthru fallback encoder
684 
685     /* Subtitles */
686     hb_list_t     * list_subtitle;
687 
688     hb_list_t     * list_attachment;
689 
690     hb_metadata_t * metadata;
691 
692     /*
693      * Muxer settings
694      *     mux:  output file format
695      *     file: file path
696      */
697 #define HB_MUX_MASK      0xFF0001
698 #define HB_MUX_INVALID   0x000000
699 #define HB_MUX_MP4V2     0x010000
700 #define HB_MUX_AV_MP4    0x020000
701 #define HB_MUX_MASK_MP4  0x030000
702 #define HB_MUX_LIBMKV    0x100000
703 #define HB_MUX_AV_MKV    0x200000
704 #define HB_MUX_AV_WEBM   0x400000
705 #define HB_MUX_MASK_MKV  0x300000
706 #define HB_MUX_MASK_AV   0x620000
707 #define HB_MUX_MASK_WEBM 0x400000
708 
709 /* default muxer for each container */
710 #define HB_MUX_MP4       HB_MUX_AV_MP4
711 #define HB_MUX_MKV       HB_MUX_AV_MKV
712 #define HB_MUX_WEBM      HB_MUX_AV_WEBM
713 
714     int             mux;
715     char          * file;
716 
717     int             inline_parameter_sets;
718                                         // Put h.264/h.265 SPS and PPS
719                                         // inline in the stream. This
720                                         // is necessary when constructing
721                                         // adaptive streaming dash files.
722     int             align_av_start;     // align A/V stream start times.
723                                         // This is used to work around mp4
724                                         // players that do not support edit
725                                         // lists. When this option is used
726                                         // the resulting stream is not a
727                                         // faithful reproduction of the source
728                                         // stream and may have blank frames
729                                         // added or initial frames dropped.
730     int             mp4_optimize;
731     int             ipod_atom;
732 
733     int                     indepth_scan;
734     hb_subtitle_config_t    select_subtitle_config;
735 
736     int             angle;              // dvd angle to encode
737     int             frame_to_start;     // declare eof when we hit this frame
738     int64_t         pts_to_start;       // drop frames until  we pass this pts
739                                         //  in the time-linearized input stream
740     int             frame_to_stop;      // declare eof when we hit this frame
741     int64_t         pts_to_stop;        // declare eof when we pass this pts in
742                                         //  the time-linearized input stream
743     int             start_at_preview;   // if non-zero, encoding will start
744                                         //  at the position of preview n
745     int             seek_points;        //  out of N previews
746     uint32_t        frames_to_skip;     // decode but discard this many frames
747                                         //  initially (for frame accurate positioning
748                                         //  to non-I frames).
749     PRIVATE int use_decomb;
750     PRIVATE int use_detelecine;
751 
752     // QSV-specific settings
753     struct
754     {
755         int decode;
756         int async_depth;
757 #if HB_PROJECT_FEATURE_QSV
758         hb_qsv_context *ctx;
759 #endif
760         // shared encoding parameters
761         // initialized by the QSV encoder, then used upstream (e.g. by filters)
762         // to configure their output so that it matches what the encoder expects
763         struct
764         {
765             int pic_struct;
766             int align_width;
767             int align_height;
768             int is_init_done;
769         } enc_info;
770     } qsv;
771 
772 #ifdef __LIBHB__
773     /* Internal data */
774     hb_handle_t   * h;
775     volatile hb_error_code * done_error;
776     volatile int  * die;
777     volatile int    done;
778 
779     uint64_t        st_paused;
780 
781     hb_fifo_t     * fifo_mpeg2;   /* MPEG-2 video ES */
782     hb_fifo_t     * fifo_raw;     /* Raw pictures */
783     hb_fifo_t     * fifo_sync;    /* Raw pictures, framerate corrected */
784     hb_fifo_t     * fifo_render;  /* Raw pictures, scaled */
785     hb_fifo_t     * fifo_mpeg4;   /* MPEG-4 video ES */
786 
787     hb_list_t     * list_work;
788 
789     hb_esconfig_t config;
790 
791     hb_mux_data_t * mux_data;
792 
793     int64_t         reader_pts_offset; // Reader can discard some video.
794                                        // Other pipeline stages need to know
795                                        // this.  E.g. sync and decsrtsub
796 #endif
797 };
798 
799 /* Audio starts here */
800 /* Audio Codecs: Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/NativeConstants.cs when changing these consts */
801 #define HB_ACODEC_INVALID   0x00000000
802 #define HB_ACODEC_NONE      0x00000001
803 #define HB_ACODEC_MASK      0x07FFFF01
804 #define HB_ACODEC_LAME      0x00000200
805 #define HB_ACODEC_VORBIS    0x00000400
806 #define HB_ACODEC_AC3       0x00000800
807 #define HB_ACODEC_LPCM      0x00001000
808 #define HB_ACODEC_DCA       0x00002000
809 #define HB_ACODEC_CA_AAC    0x00004000
810 #define HB_ACODEC_CA_HAAC   0x00008000
811 #define HB_ACODEC_FFAAC     0x00010000
812 #define HB_ACODEC_FFMPEG    0x00020000
813 #define HB_ACODEC_DCA_HD    0x00040000
814 #define HB_ACODEC_MP2       0x06000000
815 #define HB_ACODEC_MP3       0x00080000
816 #define HB_ACODEC_FFFLAC    0x00100000
817 #define HB_ACODEC_FFFLAC24  0x00200000
818 #define HB_ACODEC_FDK_AAC   0x00400000
819 #define HB_ACODEC_FDK_HAAC  0x00800000
820 #define HB_ACODEC_FFEAC3    0x01000000
821 #define HB_ACODEC_FFTRUEHD  0x02000000
822 #define HB_ACODEC_OPUS      0x04000000
823 #define HB_ACODEC_FF_MASK   0x07FF2800
824 #define HB_ACODEC_PASS_FLAG 0x40000000
825 #define HB_ACODEC_PASS_MASK   (HB_ACODEC_AC3 | HB_ACODEC_DCA | HB_ACODEC_DCA_HD | HB_ACODEC_FFAAC | HB_ACODEC_FFEAC3 | HB_ACODEC_FFFLAC | HB_ACODEC_MP2 | HB_ACODEC_MP3 | HB_ACODEC_FFTRUEHD)
826 #define HB_ACODEC_AUTO_PASS   (HB_ACODEC_PASS_FLAG | HB_ACODEC_PASS_MASK)
827 #define HB_ACODEC_ANY         (HB_ACODEC_PASS_FLAG | HB_ACODEC_MASK)
828 #define HB_ACODEC_AAC_PASS    (HB_ACODEC_PASS_FLAG | HB_ACODEC_FFAAC)
829 #define HB_ACODEC_AC3_PASS    (HB_ACODEC_PASS_FLAG | HB_ACODEC_AC3)
830 #define HB_ACODEC_DCA_PASS    (HB_ACODEC_PASS_FLAG | HB_ACODEC_DCA)
831 #define HB_ACODEC_DCA_HD_PASS (HB_ACODEC_PASS_FLAG | HB_ACODEC_DCA_HD)
832 #define HB_ACODEC_EAC3_PASS   (HB_ACODEC_PASS_FLAG | HB_ACODEC_FFEAC3)
833 #define HB_ACODEC_FLAC_PASS   (HB_ACODEC_PASS_FLAG | HB_ACODEC_FFFLAC)
834 #define HB_ACODEC_MP2_PASS    (HB_ACODEC_PASS_FLAG | HB_ACODEC_MP2)
835 #define HB_ACODEC_MP3_PASS    (HB_ACODEC_PASS_FLAG | HB_ACODEC_MP3)
836 #define HB_ACODEC_TRUEHD_PASS (HB_ACODEC_PASS_FLAG | HB_ACODEC_FFTRUEHD)
837 
838 #define HB_SUBSTREAM_BD_TRUEHD  0x72
839 #define HB_SUBSTREAM_BD_AC3     0x76
840 #define HB_SUBSTREAM_BD_DTSHD   0x72
841 #define HB_SUBSTREAM_BD_DTS     0x71
842 
843 /* define an invalid VBR quality compatible with all VBR-capable codecs */
844 #define HB_INVALID_AUDIO_QUALITY (-3.)
845 
846 #define HB_AUDIO_ATTR_NONE              0x00
847 #define HB_AUDIO_ATTR_NORMAL            0x01
848 #define HB_AUDIO_ATTR_VISUALLY_IMPAIRED 0x02
849 #define HB_AUDIO_ATTR_COMMENTARY        0x04
850 #define HB_AUDIO_ATTR_ALT_COMMENTARY    0x08
851 #define HB_AUDIO_ATTR_SECONDARY         0x10
852 #define HB_AUDIO_ATTR_DEFAULT           0x20
853 // This mask should contain all attributes that are allowed for default
854 // audio track selection
855 #define HB_AUDIO_ATTR_REGULAR_MASK      0x21
856 
857 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_audio_config_s.cs when changing this struct
858 struct hb_audio_config_s
859 {
860     /* Output */
861     struct
862     {
863         enum
864         {
865             // make sure audio->config.out.mixdown isn't treated as unsigned
866             HB_INVALID_AMIXDOWN = -1,
867             HB_AMIXDOWN_NONE = 0,
868             HB_AMIXDOWN_MONO,
869             HB_AMIXDOWN_LEFT,
870             HB_AMIXDOWN_RIGHT,
871             HB_AMIXDOWN_STEREO,
872             HB_AMIXDOWN_DOLBY,
873             HB_AMIXDOWN_DOLBYPLII,
874             HB_AMIXDOWN_5POINT1,
875             HB_AMIXDOWN_6POINT1,
876             HB_AMIXDOWN_7POINT1,
877             HB_AMIXDOWN_5_2_LFE,
878         } mixdown; /* Audio mixdown */
879         int      track; /* Output track number */
880         uint32_t codec; /* Output audio codec */
881         int      samplerate; /* Output sample rate (Hz) */
882         int      samples_per_frame; /* Number of samples per frame */
883         int      bitrate; /* Output bitrate (Kbps) */
884         double   quality; /* Output quality (encoder-specific) */
885         double   compression_level;  /* Output compression level (encoder-specific) */
886         double   dynamic_range_compression; /* Amount of DRC applied to this track */
887         double   gain; /* Gain (in dB), negative is quieter */
888         int      normalize_mix_level; /* mix level normalization (boolean) */
889         int      dither_method; /* dither algorithm */
890         const char * name; /* Output track name */
891     } out;
892 
893     /* Input */
894     struct
895     {
896         int track; /* Input track number */
897         PRIVATE uint32_t codec; /* Input audio codec */
898         PRIVATE uint32_t codec_param; /* Per-codec config info */
899         PRIVATE uint32_t reg_desc; /* Registration descriptor of source */
900         PRIVATE uint32_t stream_type; /* Stream type from source stream */
901         PRIVATE uint32_t substream_type; /* Substream type for multiplexed streams */
902         PRIVATE uint32_t version; /* Bitstream version */
903         PRIVATE uint32_t flags; /* Bitstream flags, codec-specific */
904         PRIVATE uint32_t mode; /* Bitstream mode, codec-specific */
905         PRIVATE int samplerate; /* Input sample rate (Hz) */
906         PRIVATE int sample_bit_depth; /* Input samples' bit depth (0 if unknown) */
907         PRIVATE int samples_per_frame; /* Number of samples per frame */
908         PRIVATE int bitrate; /* Input bitrate (bps) */
909         PRIVATE int matrix_encoding; /* Source matrix encoding mode, set by the audio decoder */
910         PRIVATE uint64_t channel_layout; /* Source channel layout, set by the audio decoder */
911         PRIVATE hb_chan_map_t * channel_map; /* Source channel map, set by the audio decoder */
912         PRIVATE int encoder_delay; /* Encoder delay in samples.
913                                     * These samples should be dropped
914                                     * when decoding */
915         PRIVATE hb_rational_t timebase;
916         const char * name;
917     } in;
918 
919     struct
920     {
921         PRIVATE char description[1024];
922         PRIVATE char simple[1024];
923         PRIVATE char iso639_2[4];
924         PRIVATE uint32_t attributes; /* normal, visually impaired, director's commentary */
925     } lang;
926 };
927 
928 #ifdef __LIBHB__
929 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_audio_s.cs when changing this struct
930 struct hb_audio_s
931 {
932     int id;
933 
934     hb_audio_config_t config;
935 
936     struct {
937         hb_fifo_t * fifo_in;   /* AC3/MPEG/LPCM ES */
938         hb_fifo_t * fifo_raw;  /* Raw audio */
939         hb_fifo_t * fifo_sync; /* Resampled, synced raw audio */
940         hb_fifo_t * fifo_out;  /* MP3/AAC/Vorbis ES */
941 
942         hb_esconfig_t config;
943         hb_mux_data_t * mux_data;
944         hb_fifo_t     * scan_cache;
945     } priv;
946 };
947 #endif
948 
949 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_chapter_s.cs when changing this struct
950 struct hb_chapter_s
951 {
952     int      index;
953 
954     /* Visual-friendly duration */
955     int      hours;
956     int      minutes;
957     int      seconds;
958 
959     /* Exact duration (in 1/90000s) */
960     uint64_t duration;
961 
962     /* Optional chapter title */
963     char     *title;
964 };
965 
966 /*
967  * A subtitle track.
968  *
969  * Required fields when a demuxer creates a subtitle track are:
970  * > id
971  *     - ID of this track
972  *     - must be unique for all tracks within a single job,
973  *       since it is used to look up the appropriate in-FIFO with GetFifoForId()
974  * > format
975  *     - format of the packets the subtitle decoder work-object sends to sub->fifo_raw
976  *     - for passthru subtitles, is also the format of the final packets sent to sub->fifo_out
977  *     - PICTURESUB for banded 8-bit YAUV pixels; see decvobsub.c documentation for more info
978  *     - TEXTSUB for UTF-8 text marked up with <b>, <i>, or <u>
979  *     - read by the muxers, and by the subtitle burn-in logic in the hb_sync_video work-object
980  * > source
981  *     - used to create the appropriate subtitle decoder work-object in do_job()
982  * > config.dest
983  *     - whether to render the subtitle on the video track (RENDERSUB) or
984  *       to pass it through its own subtitle track in the output container (PASSTHRUSUB)
985  *     - all newly created non-VOBSUB tracks should default to PASSTHRUSUB
986  *     - all newly created VOBSUB tracks should default to RENDERSUB, for legacy compatibility
987  * > lang
988  *     - user-readable description of the subtitle track
989  *     - may correspond to the language of the track (see the 'iso639_2' field)
990  *     - may correspond to the type of track (see the 'type' field; ex: "Closed Captions")
991  * > iso639_2
992  *     - language code for the subtitle, or "und" if unknown
993  *
994  * Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_subtitle_s.cs when changing this struct
995  */
996 
997 #define HB_SUBTITLE_ATTR_UNKNOWN    0x0000
998 #define HB_SUBTITLE_ATTR_NORMAL     0x0001
999 #define HB_SUBTITLE_ATTR_LARGE      0x0002
1000 #define HB_SUBTITLE_ATTR_CHILDREN   0x0004
1001 #define HB_SUBTITLE_ATTR_CC         0x0008
1002 #define HB_SUBTITLE_ATTR_FORCED     0x0010
1003 #define HB_SUBTITLE_ATTR_COMMENTARY 0x0020
1004 #define HB_SUBTITLE_ATTR_4_3        0x0040
1005 #define HB_SUBTITLE_ATTR_WIDE       0x0080
1006 #define HB_SUBTITLE_ATTR_LETTERBOX  0x0100
1007 #define HB_SUBTITLE_ATTR_PANSCAN    0x0200
1008 #define HB_SUBTITLE_ATTR_DEFAULT    0x0400
1009 
1010 #define HB_SUBTITLE_IMPORT_TAG      0xFF000000
1011 #define HB_SUBTITLE_EMBEDDED_CC_TAG 0xFE000000
1012 
1013 struct hb_subtitle_s
1014 {
1015     int  id;
1016     int  track;
1017     int  out_track;
1018 
1019     hb_subtitle_config_t config;
1020 
1021     enum subtype { PICTURESUB, TEXTSUB } format;
1022     enum subsource {
1023         VOBSUB,
1024         CC608SUB,
1025         CC708SUB, // unused
1026         UTF8SUB,
1027         TX3GSUB,
1028         SSASUB,
1029         PGSSUB,
1030         IMPORTSRT,
1031         IMPORTSSA,
1032         DVBSUB,
1033         SRTSUB = IMPORTSRT
1034     } source;
1035     const char * name;
1036     char         lang[1024];
1037     char         iso639_2[4];
1038     uint32_t     attributes; /* Closed Caption, Children, Directors etc */
1039 
1040     // Color lookup table for VOB subtitle tracks. Each entry is in YCbCr format.
1041     // Must be filled out by the demuxer for VOB subtitle tracks.
1042     uint32_t    palette[16];
1043     uint8_t     palette_set;
1044     int         width;
1045     int         height;
1046 
1047     // Codec private data for subtitles originating from FFMPEG sources
1048     uint8_t *   extradata;
1049     int         extradata_size;
1050 
1051     int hits;     /* How many hits/occurrences of this subtitle */
1052     int forced_hits; /* How many forced hits in this subtitle */
1053 
1054 #ifdef __LIBHB__
1055     /* Internal data */
1056     uint32_t        codec;          /* Input "codec" */
1057     uint32_t        codec_param;    /* Per-codec config info */
1058     uint32_t        reg_desc;       /* registration descriptor of source */
1059     uint32_t        stream_type;    /* stream type from source stream */
1060     uint32_t        substream_type; /* substream for multiplexed streams */
1061     hb_rational_t   timebase;
1062 
1063     hb_fifo_t     * fifo_in;        /* SPU ES */
1064     hb_fifo_t     * fifo_raw;       /* Decoded SPU */
1065     hb_fifo_t     * fifo_out;       /* Correct Timestamps, ready to be muxed */
1066     hb_mux_data_t * mux_data;
1067 #endif
1068 };
1069 
1070 /*
1071  * An attachment.
1072  *
1073  * These are usually used for attaching embedded fonts to movies containing SSA subtitles.
1074  */
1075 struct hb_attachment_s
1076 {
1077     enum attachtype { FONT_TTF_ATTACH, FONT_OTF_ATTACH, HB_ART_ATTACH } type;
1078     char *  name;
1079     char *  data;
1080     int     size;
1081 };
1082 
1083 struct hb_coverart_s
1084 {
1085     uint8_t *data;
1086     uint32_t size;
1087     enum arttype {
1088         HB_ART_UNDEFINED,
1089         HB_ART_BMP,
1090         HB_ART_GIF,
1091         HB_ART_PNG,
1092         HB_ART_JPEG
1093     } type;
1094 };
1095 
1096 struct hb_metadata_s
1097 {
1098     hb_dict_t * dict;
1099     hb_list_t * list_coverart;
1100 };
1101 
1102 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_title_s.cs when changing this struct
1103 struct hb_title_s
1104 {
1105     enum { HB_DVD_TYPE, HB_BD_TYPE, HB_STREAM_TYPE, HB_FF_STREAM_TYPE } type;
1106     uint32_t        reg_desc;
1107     const char    * path;
1108     const char    * name;
1109     int             index;
1110     int             playlist;
1111     int             angle_count;
1112     void          * opaque_priv;
1113 
1114     /* Visual-friendly duration */
1115     int             hours;
1116     int             minutes;
1117     int             seconds;
1118 
1119     /* Exact duration (in 1/90000s) */
1120     uint64_t        duration;
1121 
1122     int             preview_count;
1123     int             has_resolution_change;
1124     enum { HB_ROTATION_0, HB_ROTATION_90, HB_ROTATION_180, HB_ROTATION_270 } rotation;
1125     hb_geometry_t   geometry;
1126     hb_rational_t   dar;             // aspect ratio for the title's video
1127     hb_rational_t   container_dar;   // aspect ratio from container (0 if none)
1128     int             pix_fmt;
1129     int             color_prim;
1130     int             color_transfer;
1131     int             color_matrix;
1132     int             color_range;
1133     hb_mastering_display_metadata_t mastering;
1134     hb_content_light_metadata_t     coll;
1135     hb_rational_t   vrate;
1136     int             crop[4];
1137     enum {HB_DVD_DEMUXER, HB_TS_DEMUXER, HB_PS_DEMUXER, HB_NULL_DEMUXER} demuxer;
1138     int             detected_interlacing;
1139     int             pcr_pid;                /* PCR PID for TS streams */
1140     int             video_id;               /* demuxer stream id for video */
1141     int             video_codec;            /* worker object id of video codec */
1142     uint32_t        video_stream_type;      /* stream type from source stream */
1143     int             video_codec_param;      /* codec specific config */
1144     char          * video_codec_name;
1145     int             video_bitrate;
1146     hb_rational_t   video_timebase;
1147     char          * container_name;
1148     int             data_rate;
1149 
1150     // additional supported video decoders (e.g. HW-accelerated implementations)
1151     int           video_decode_support;
1152 #define HB_DECODE_SUPPORT_SW    0x01 // software (libavcodec or mpeg2dec)
1153 #define HB_DECODE_SUPPORT_QSV   0x02 // Intel Quick Sync Video
1154 
1155     hb_metadata_t * metadata;
1156 
1157     hb_list_t     * list_chapter;
1158     hb_list_t     * list_audio;
1159     hb_list_t     * list_subtitle;
1160     hb_list_t     * list_attachment;
1161 
1162     uint32_t        flags;
1163                 // set if video stream doesn't have IDR frames
1164 #define         HBTF_NO_IDR (1 << 0)
1165 #define         HBTF_SCAN_COMPLETE (1 << 1)
1166 #define         HBTF_RAW_VIDEO (1 << 2)
1167 };
1168 
1169 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_state_s.cs when changing this struct
1170 struct hb_state_s
1171 {
1172 #define HB_STATE_IDLE     1
1173 #define HB_STATE_SCANNING 2
1174 #define HB_STATE_SCANDONE 4
1175 #define HB_STATE_WORKING  8
1176 #define HB_STATE_PAUSED   16
1177 #define HB_STATE_WORKDONE 32
1178 #define HB_STATE_MUXING   64
1179 #define HB_STATE_SEARCHING 128
1180     int         state;
1181     int         sequence_id;
1182 
1183     struct
1184     {
1185         struct
1186         {
1187             /* HB_STATE_SCANNING */
1188             float progress;
1189             int preview_cur;
1190             int preview_count;
1191             int title_cur;
1192             int title_count;
1193         } scanning;
1194 
1195         struct
1196         {
1197             /* HB_STATE_WORKING || HB_STATE_SEARCHING || HB_STATE_WORKDONE */
1198 #define HB_PASS_SUBTITLE    -1
1199 #define HB_PASS_ENCODE      0
1200 #define HB_PASS_ENCODE_1ST  1   // Some code depends on these values being
1201 #define HB_PASS_ENCODE_2ND  2   // 1 and 2.  Do not change.
1202             int           pass_id;
1203             int           pass;
1204             int           pass_count;
1205             float         progress;
1206             float         rate_cur;
1207             float         rate_avg;
1208             int64_t       eta_seconds;
1209             int           hours;
1210             int           minutes;
1211             int           seconds;
1212             uint64_t      paused;
1213             hb_error_code error;
1214         } working;
1215 
1216         struct
1217         {
1218             /* HB_STATE_MUXING */
1219             float progress;
1220         } muxing;
1221     } param;
1222 };
1223 
1224 typedef struct hb_work_info_s
1225 {
1226     const char  * name;
1227     int           profile;
1228     int           level;
1229     int           bitrate;
1230     hb_rational_t rate;
1231     uint32_t      version;
1232     uint32_t      flags;
1233     uint32_t      mode;
1234     union
1235     {
1236         struct
1237         {    // info only valid for video decoders
1238             hb_geometry_t geometry;
1239             int           pix_fmt;
1240             int           color_prim;
1241             int           color_transfer;
1242             int           color_matrix;
1243             int           color_range;
1244             int           video_decode_support;
1245         };
1246         struct
1247         {    // info only valid for audio decoders
1248             uint64_t channel_layout;
1249             hb_chan_map_t * channel_map;
1250             int samples_per_frame;
1251             int sample_bit_depth;
1252             int matrix_encoding;
1253         };
1254     };
1255 } hb_work_info_t;
1256 
1257 struct hb_work_object_s
1258 {
1259     int                 id;
1260     char              * name;
1261 
1262 #ifdef __LIBHB__
1263     int              (* init)  ( hb_work_object_t *, hb_job_t * );
1264     int              (* work)  ( hb_work_object_t *, hb_buffer_t **,
1265                                  hb_buffer_t ** );
1266     void             (* close) ( hb_work_object_t * );
1267     /* the info entry point is used by scan to get bitstream information
1268      * during a decode (i.e., it should only be called after at least one
1269      * call to the 'work' entry point). currently it's only called for
1270      * video streams & can be null for other work objects. */
1271     int              (* info)  ( hb_work_object_t *, hb_work_info_t * );
1272     /* the bitstream info entry point is used by scan to get bitstream
1273      * information from a buffer. it doesn't have to be called during a
1274      * decode (it can be called even if init & work haven't been).
1275      * currently it's only called for audio streams & can be null for
1276      * other work objects. */
1277     int              (* bsinfo)  ( hb_work_object_t *, const hb_buffer_t *,
1278                                    hb_work_info_t * );
1279     void             (* flush)   ( hb_work_object_t * );
1280 
1281     hb_fifo_t         * fifo_in;
1282     hb_fifo_t         * fifo_out;
1283     hb_esconfig_t     * config;
1284 
1285     /* Pointer hb_audio_t so we have access to the info in the audio worker threads. */
1286     hb_audio_t        * audio;
1287 
1288     /* Pointer hb_subtitle_t so we have access to the info in the subtitle worker threads. */
1289     hb_subtitle_t     * subtitle;
1290 
1291     hb_work_private_t * private_data;
1292 
1293     hb_thread_t       * thread;
1294     volatile int      * done;
1295     volatile int      * die;
1296     int                 status;
1297     int                 frame_count;
1298     int                 codec_param;
1299     hb_title_t        * title;
1300 
1301     hb_work_object_t  * next;
1302 
1303     hb_handle_t       * h;
1304 #endif
1305 };
1306 
1307 extern hb_work_object_t hb_sync_video;
1308 extern hb_work_object_t hb_sync_audio;
1309 extern hb_work_object_t hb_sync_subtitle;
1310 extern hb_work_object_t hb_decvobsub;
1311 extern hb_work_object_t hb_decsrtsub;
1312 extern hb_work_object_t hb_decutf8sub;
1313 extern hb_work_object_t hb_dectx3gsub;
1314 extern hb_work_object_t hb_decssasub;
1315 extern hb_work_object_t hb_decavsub;
1316 extern hb_work_object_t hb_encavcodec;
1317 extern hb_work_object_t hb_encqsv;
1318 extern hb_work_object_t hb_encx264;
1319 extern hb_work_object_t hb_enctheora;
1320 extern hb_work_object_t hb_encx265;
1321 extern hb_work_object_t hb_decavcodeca;
1322 extern hb_work_object_t hb_decavcodecv;
1323 extern hb_work_object_t hb_declpcm;
1324 extern hb_work_object_t hb_encvorbis;
1325 extern hb_work_object_t hb_muxer;
1326 extern hb_work_object_t hb_encca_aac;
1327 extern hb_work_object_t hb_encca_haac;
1328 extern hb_work_object_t hb_encavcodeca;
1329 extern hb_work_object_t hb_reader;
1330 
1331 #define HB_FILTER_OK      0
1332 #define HB_FILTER_DELAY   1
1333 #define HB_FILTER_FAILED  2
1334 #define HB_FILTER_DROP    3
1335 #define HB_FILTER_DONE    4
1336 
1337 typedef struct hb_filter_init_s
1338 {
1339     hb_job_t      * job;
1340     int             pix_fmt;
1341     int             color_prim;
1342     int             color_transfer;
1343     int             color_matrix;
1344     int             color_range;
1345     hb_geometry_t   geometry;
1346     int             crop[4];
1347     hb_rational_t   vrate;
1348     int             cfr;
1349     int             grayscale;
1350     hb_rational_t   time_base;
1351 } hb_filter_init_t;
1352 
1353 typedef struct hb_filter_info_s
1354 {
1355     char             * human_readable_desc;
1356     hb_filter_init_t   output;
1357 } hb_filter_info_t;
1358 
1359 struct hb_filter_object_s
1360 {
1361     int                   id;
1362     int                   enforce_order;
1363     int                   skip;
1364     int                   aliased;
1365     char                * name;
1366     hb_dict_t           * settings;
1367 
1368 #ifdef __LIBHB__
1369     int                (* init)       ( hb_filter_object_t *, hb_filter_init_t * );
1370     int                (* init_thread)( hb_filter_object_t *, int );
1371     int                (* post_init)  ( hb_filter_object_t *, hb_job_t * );
1372     int                (* work)       ( hb_filter_object_t *,
1373                                         hb_buffer_t **, hb_buffer_t ** );
1374     int                (* work_thread)( hb_filter_object_t *,
1375                                         hb_buffer_t **, hb_buffer_t **, int );
1376     void               (* close)      ( hb_filter_object_t * );
1377     hb_filter_info_t * (* info)       ( hb_filter_object_t * );
1378 
1379     const char          * settings_template;
1380 
1381     hb_fifo_t           * fifo_in;
1382     hb_fifo_t           * fifo_out;
1383 
1384     hb_subtitle_t       * subtitle;
1385 
1386     hb_filter_private_t * private_data;
1387 
1388     hb_thread_t         * thread;
1389     volatile int        * done;
1390     int                   status;
1391 
1392     // Filters can drop frames and thus chapter marks
1393     // These are used to bridge the chapter to the next buffer
1394     int                   chapter_val;
1395     int64_t               chapter_time;
1396 
1397     hb_filter_object_t  * sub_filter;
1398 #endif
1399 };
1400 
1401 // Update win/CS/HandBrake.Interop/HandBrakeInterop/HbLib/hb_filter_ids.cs when changing this enum
1402 enum
1403 {
1404     HB_FILTER_INVALID = 0,
1405     // for QSV - important to have before other filters
1406     HB_FILTER_FIRST = 1,
1407     HB_FILTER_QSV_PRE = 1,
1408 
1409     // First, filters that may change the framerate (drop or dup frames)
1410     HB_FILTER_DETELECINE,
1411     HB_FILTER_COMB_DETECT,
1412     HB_FILTER_DECOMB,
1413     HB_FILTER_DEINTERLACE,
1414     HB_FILTER_VFR,
1415     // Filters that must operate on the original source image are next
1416     HB_FILTER_DEBLOCK,
1417     HB_FILTER_DENOISE,
1418     HB_FILTER_HQDN3D = HB_FILTER_DENOISE,
1419     HB_FILTER_NLMEANS,
1420     HB_FILTER_CHROMA_SMOOTH,
1421     HB_FILTER_ROTATE,
1422     HB_FILTER_RENDER_SUB,
1423     HB_FILTER_CROP_SCALE,
1424     HB_FILTER_LAPSHARP,
1425     HB_FILTER_UNSHARP,
1426     HB_FILTER_GRAYSCALE,
1427     HB_FILTER_PAD,
1428     HB_FILTER_COLORSPACE,
1429     HB_FILTER_FORMAT,
1430 
1431     // Finally filters that don't care what order they are in,
1432     // except that they must be after the above filters
1433     HB_FILTER_AVFILTER,
1434 
1435     // for QSV - important to have as a last one
1436     HB_FILTER_QSV_POST,
1437     // default MSDK VPP filter
1438     HB_FILTER_QSV,
1439     HB_FILTER_LAST = HB_FILTER_QSV,
1440     // wrapper filter for frame based multi-threading of simple filters
1441     HB_FILTER_MT_FRAME
1442 };
1443 
1444 hb_filter_object_t * hb_filter_get( int filter_id );
1445 hb_filter_object_t * hb_filter_init( int filter_id );
1446 hb_filter_object_t * hb_filter_copy( hb_filter_object_t * filter );
1447 hb_list_t          * hb_filter_list_copy(const hb_list_t *src);
1448 hb_dict_t          * hb_filter_dict_find(const hb_value_array_t * list,
1449                                          int filter_id);
1450 hb_filter_object_t * hb_filter_find(const hb_list_t *list, int filter_id);
1451 void                 hb_filter_close( hb_filter_object_t ** );
1452 void                 hb_filter_info_close( hb_filter_info_t ** );
1453 hb_dict_t          * hb_parse_filter_settings(const char * settings);
1454 char               * hb_parse_filter_settings_json(const char * settings_str);
1455 char               * hb_filter_settings_string(int filter_id,
1456                                                hb_value_t * value);
1457 char               * hb_filter_settings_string_json(int filter_id,
1458                                                     const char * json);
1459 
1460 typedef void hb_error_handler_t( const char *errmsg );
1461 
1462 extern void hb_register_error_handler( hb_error_handler_t * handler );
1463 
1464 char * hb_strdup_vaprintf( const char * fmt, va_list args );
1465 char * hb_strdup_printf(const char *fmt, ...) HB_WPRINTF(1, 2);
1466 char * hb_strncat_dup( const char * s1, const char * s2, size_t n );
1467 
1468 // free array of strings
1469 void    hb_str_vfree( char ** strv );
1470 // count number of strings in array of strings
1471 int     hb_str_vlen(char ** strv);
1472 // split string into array of strings
1473 char ** hb_str_vsplit( const char * str, char delem );
1474 
1475 int hb_yuv2rgb(int yuv);
1476 int hb_rgb2yuv(int rgb);
1477 int hb_rgb2yuv_bt709(int rgb);
1478 
1479 const char * hb_subsource_name( int source );
1480 
1481 // unparse a set of x264 settings to an HB encopts string
1482 char * hb_x264_param_unparse(int bit_depth, const char *x264_preset,
1483                              const char *x264_tune, const char *x264_encopts,
1484                              const char *h264_profile, const char *h264_level,
1485                              int width, int height);
1486 
1487 // x264 option name/synonym helper
1488 const char * hb_x264_encopt_name( const char * name );
1489 
1490 #if HB_PROJECT_FEATURE_X265
1491 // x265 option name/synonym helper
1492 const char * hb_x265_encopt_name( const char * name );
1493 #endif
1494 
1495 int hb_output_color_prim(hb_job_t * job);
1496 int hb_output_color_transfer(hb_job_t * job);
1497 int hb_output_color_matrix(hb_job_t * job);
1498 
1499 int hb_get_bit_depth(int format);
1500 int hb_get_best_pix_fmt(hb_job_t * job);
1501 
1502 #define HB_NEG_FLOAT_REG "(([-])?(([0-9]+([.,][0-9]+)?)|([.,][0-9]+))"
1503 #define HB_FLOAT_REG     "(([0-9]+([.,][0-9]+)?)|([.,][0-9]+))"
1504 #define HB_NEG_INT_REG   "(([-]?[0-9]+)"
1505 #define HB_INT_REG       "([0-9]+)"
1506 #define HB_RATIONAL_REG  "([0-9]+/[0-9]+)"
1507 #define HB_BOOL_REG      "(yes|no|true|false|[01])"
1508 #define HB_ALL_REG       "(.*)"
1509 
1510 #endif // HANDBRAKE_COMMON_H
1511