1 /* internal.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_INTERNAL_H
11 #define HANDBRAKE_INTERNAL_H
12
13 #include "libavutil/imgutils.h"
14 #include "libavutil/pixdesc.h"
15 #include "handbrake/project.h"
16
17 /***********************************************************************
18 * common.c
19 **********************************************************************/
20 void hb_log( char * log, ... ) HB_WPRINTF(1,2);
21 extern int global_verbosity_level; // Global variable for hb_deep_log
22 typedef enum hb_debug_level_s
23 {
24 HB_SUPPORT_LOG = 1, // helpful in tech support
25 HB_HOUSEKEEPING_LOG = 2, // stuff we hate scrolling through
26 HB_GRANULAR_LOG = 3 // sample-by-sample
27 } hb_debug_level_t;
28 void hb_valog( hb_debug_level_t level, const char * prefix, const char * log, va_list args) HB_WPRINTF(3,0);
29 void hb_deep_log( hb_debug_level_t level, char * log, ... ) HB_WPRINTF(2,3);
30 void hb_error( char * fmt, ...) HB_WPRINTF(1,2);
31 void hb_hexdump( hb_debug_level_t level, const char * label, const uint8_t * data, int len );
32
33 int hb_list_bytes( hb_list_t * );
34 void hb_list_seebytes( hb_list_t * l, uint8_t * dst, int size );
35 void hb_list_getbytes( hb_list_t * l, uint8_t * dst, int size,
36 uint64_t * pts, uint64_t * pos );
37 void hb_list_empty( hb_list_t ** );
38
39 hb_title_t * hb_title_init( char * dvd, int index );
40 void hb_title_close( hb_title_t ** );
41
42 /***********************************************************************
43 * hb.c
44 **********************************************************************/
45 int hb_get_pid( hb_handle_t * );
46 void hb_set_state( hb_handle_t *, hb_state_t * );
47 void hb_set_work_error( hb_handle_t * h, hb_error_code err );
48 void hb_job_setup_passes(hb_handle_t *h, hb_job_t *job, hb_list_t *list_pass);
49
50 /***********************************************************************
51 * fifo.c
52 **********************************************************************/
53
54 /*
55 * Holds a packet of data that is moving through the transcoding process.
56 *
57 * May have metadata associated with it via extra fields
58 * that are conditionally used depending on the type of packet.
59 */
60 typedef struct hb_buffer_s hb_buffer_t;
61
62 #if HB_PROJECT_FEATURE_QSV
63 #include "handbrake/qsv_libav.h"
64 #endif
65
66 struct hb_buffer_settings_s
67 {
68 enum { OTHER_BUF, AUDIO_BUF, VIDEO_BUF, SUBTITLE_BUF, FRAME_BUF } type;
69
70 int id; // ID of the track that the packet comes from
71 int64_t start; // start time of frame
72 double duration; // Actual duration, may be fractional ticks
73 int64_t stop; // stop time of frame
74 int64_t renderOffset; // DTS used by b-frame offsets in muxmp4
75 int64_t pcr;
76 int scr_sequence; // The SCR sequence that this buffer's
77 // timestamps are referenced to
78 int split;
79 uint8_t discontinuity;
80 int new_chap; // Video packets: if non-zero, is the index of the chapter whose boundary was crossed
81
82 #define HB_FRAME_IDR 0x01
83 #define HB_FRAME_I 0x02
84 #define HB_FRAME_AUDIO 0x04
85 #define HB_FRAME_SUBTITLE 0x08
86 #define HB_FRAME_P 0x10
87 #define HB_FRAME_B 0x20
88 #define HB_FRAME_BREF 0x40
89 #define HB_FRAME_MASK_KEY 0x0F
90 #define HB_FRAME_MASK_REF 0xF0
91 uint8_t frametype;
92
93 // Picture flags used by filters
94 #ifndef PIC_FLAG_TOP_FIELD_FIRST
95 #define PIC_FLAG_TOP_FIELD_FIRST 0x0008
96 #endif
97 #ifndef PIC_FLAG_PROGRESSIVE_FRAME
98 #define PIC_FLAG_PROGRESSIVE_FRAME 0x0010
99 #endif
100 #ifndef PIC_FLAG_REPEAT_FIRST_FIELD
101 #define PIC_FLAG_REPEAT_FIRST_FIELD 0x0100
102 #endif
103 #define PIC_FLAG_REPEAT_FRAME 0x0200
104 #define HB_BUF_FLAG_EOF 0x0400
105 #define HB_BUF_FLAG_EOS 0x0800
106 #define HB_FLAG_FRAMETYPE_KEY 0x1000
107 #define HB_FLAG_FRAMETYPE_REF 0x2000
108 #define HB_FLAG_DISCARD 0x4000
109 uint16_t flags;
110
111 #define HB_COMB_NONE 0
112 #define HB_COMB_LIGHT 1
113 #define HB_COMB_HEAVY 2
114 uint8_t combed;
115 };
116
117 struct hb_image_format_s
118 {
119 int x;
120 int y;
121 int width;
122 int height;
123 int fmt;
124 int color_prim;
125 int color_transfer;
126 int color_matrix;
127 int color_range;
128 int max_plane;
129 int window_width;
130 int window_height;
131 };
132
133 struct hb_buffer_s
134 {
135 int size; // size of this packet
136 int alloc; // used internally by the packet allocator (hb_buffer_init)
137 uint8_t * data; // packet data
138 int offset; // used internally by packet lists (hb_list_t)
139
140 hb_buffer_settings_t s;
141 hb_image_format_t f;
142
143 struct buffer_plane
144 {
145 uint8_t * data;
146 int stride;
147 int width;
148 int height;
149 int height_stride;
150 int size;
151 } plane[4]; // 3 Color components + alpha
152
153 #if HB_PROJECT_FEATURE_QSV
154 struct qsv
155 {
156 void * qsv_atom;
157 AVFrame * frame;
158 void * filter_details;
159 hb_qsv_context * ctx;
160 HBQSVFramesContext * qsv_frames_ctx;
161 } qsv_details;
162 #endif
163
164 // libav may attach AV_PKT_DATA_PALETTE side data to some AVPackets
165 // Store this data here when read and pass to decoder.
166 hb_buffer_t * palette;
167
168 // Packets in a list:
169 // the next packet in the list
170 hb_buffer_t * next;
171 };
172
173 void hb_buffer_pool_init( void );
174 void hb_buffer_pool_free( void );
175
176 hb_buffer_t * hb_buffer_init( int size );
177 hb_buffer_t * hb_buffer_eof_init( void );
178 hb_buffer_t * hb_frame_buffer_init( int pix_fmt, int w, int h);
179 void hb_frame_buffer_blank_stride(hb_buffer_t * buf);
180 void hb_frame_buffer_mirror_stride(hb_buffer_t * buf);
181 void hb_buffer_init_planes( hb_buffer_t * b );
182 void hb_buffer_realloc( hb_buffer_t *, int size );
183 void hb_video_buffer_realloc( hb_buffer_t * b, int w, int h );
184 void hb_buffer_reduce( hb_buffer_t * b, int size );
185 void hb_buffer_close( hb_buffer_t ** );
186 hb_buffer_t * hb_buffer_dup( const hb_buffer_t * src );
187 int hb_buffer_copy( hb_buffer_t * dst, const hb_buffer_t * src );
188 void hb_buffer_swap_copy( hb_buffer_t *src, hb_buffer_t *dst );
189 hb_image_t * hb_image_init(int pix_fmt, int width, int height);
190 hb_image_t * hb_buffer_to_image(hb_buffer_t *buf);
191 int hb_picture_fill(uint8_t *data[], int stride[], hb_buffer_t *b);
192 int hb_picture_crop(uint8_t *data[], int stride[], hb_buffer_t *b,
193 int top, int left);
194
195 hb_fifo_t * hb_fifo_init( int capacity, int thresh );
196 void hb_fifo_register_full_cond( hb_fifo_t * f, hb_cond_t * c );
197 int hb_fifo_size( hb_fifo_t * );
198 int hb_fifo_size_bytes( hb_fifo_t * );
199 int hb_fifo_is_full( hb_fifo_t * );
200 float hb_fifo_percent_full( hb_fifo_t * f );
201 hb_buffer_t * hb_fifo_get( hb_fifo_t * );
202 hb_buffer_t * hb_fifo_get_wait( hb_fifo_t * );
203 hb_buffer_t * hb_fifo_see( hb_fifo_t * );
204 hb_buffer_t * hb_fifo_see_wait( hb_fifo_t * );
205 hb_buffer_t * hb_fifo_see2( hb_fifo_t * );
206 void hb_fifo_push( hb_fifo_t *, hb_buffer_t * );
207 void hb_fifo_push_wait( hb_fifo_t *, hb_buffer_t * );
208 int hb_fifo_full_wait( hb_fifo_t * f );
209 void hb_fifo_push_head( hb_fifo_t *, hb_buffer_t * );
210 void hb_fifo_close( hb_fifo_t ** );
211 void hb_fifo_flush( hb_fifo_t * f );
212
hb_image_stride(int pix_fmt,int width,int plane)213 static inline int hb_image_stride( int pix_fmt, int width, int plane )
214 {
215 int linesize = av_image_get_linesize( pix_fmt, width, plane );
216
217 // Make buffer SIMD friendly.
218 // Decomb requires stride aligned to 32 bytes
219 // TODO: eliminate extra buffer copies in decomb
220 linesize = MULTIPLE_MOD_UP( linesize, 32 );
221 return linesize;
222 }
223
hb_image_width(int pix_fmt,int width,int plane)224 static inline int hb_image_width(int pix_fmt, int width, int plane)
225 {
226 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
227
228 if (desc != NULL && (plane == 1 || plane == 2))
229 {
230 // The wacky arithmetic assures rounding up.
231 width = -((-width) >> desc->log2_chroma_w);
232 }
233
234 return width;
235 }
236
hb_image_height_stride(int pix_fmt,int height,int plane)237 static inline int hb_image_height_stride(int pix_fmt, int height, int plane)
238 {
239 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
240
241 // Decomb requires 6 extra lines and stride aligned to 32 bytes
242 height = MULTIPLE_MOD_UP(height + 6, 32);
243 if (desc != NULL && (plane == 1 || plane == 2))
244 {
245 height = height >> desc->log2_chroma_h;
246 }
247
248 return height;
249 }
250
hb_image_height(int pix_fmt,int height,int plane)251 static inline int hb_image_height(int pix_fmt, int height, int plane)
252 {
253 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
254
255 if (desc != NULL && (plane == 1 || plane == 2))
256 {
257 // The wacky arithmetic assures rounding up.
258 height = -((-height) >> desc->log2_chroma_h);
259 }
260
261 return height;
262 }
263
264 // this routine gets a buffer for an uncompressed YUV420 video frame
265 // with dimensions width x height.
hb_video_buffer_init(int width,int height)266 static inline hb_buffer_t * hb_video_buffer_init( int width, int height )
267 {
268 return hb_frame_buffer_init( AV_PIX_FMT_YUV420P, width, height );
269 }
270
271 /***********************************************************************
272 * Threads: scan.c, work.c, reader.c, muxcommon.c
273 **********************************************************************/
274 hb_thread_t * hb_scan_init( hb_handle_t *, volatile int * die,
275 const char * path, int title_index,
276 hb_title_set_t * title_set, int preview_count,
277 int store_previews, uint64_t min_duration );
278 hb_thread_t * hb_work_init( hb_list_t * jobs,
279 volatile int * die, hb_error_code * error, hb_job_t ** job );
280 void ReadLoop( void * _w );
281 void hb_work_loop( void * );
282 hb_work_object_t * hb_muxer_init( hb_job_t * );
283 hb_work_object_t * hb_get_work( hb_handle_t *, int );
284 hb_work_object_t * hb_audio_decoder( hb_handle_t *, int );
285 hb_work_object_t * hb_audio_encoder( hb_handle_t *, int );
286 hb_work_object_t * hb_video_decoder( hb_handle_t *, int, int );
287 hb_work_object_t * hb_video_encoder( hb_handle_t *, int );
288
289 /***********************************************************************
290 * sync.c
291 **********************************************************************/
292 hb_work_object_t * hb_sync_init( hb_job_t * job );
293
294 /***********************************************************************
295 * mpegdemux.c
296 **********************************************************************/
297 typedef struct {
298 int64_t last_scr; /* unadjusted SCR from most recent pack */
299 int64_t scr_delta;
300 int64_t last_pts; /* last pts we saw */
301 int scr_changes; /* number of SCR discontinuities */
302 int new_chap;
303 } hb_psdemux_t;
304
305 typedef void (*hb_muxer_t)(hb_buffer_t *, hb_buffer_list_t *, hb_psdemux_t*);
306
307 void hb_demux_ps(hb_buffer_t * ps_buf, hb_buffer_list_t * es_list, hb_psdemux_t *);
308 void hb_demux_ts(hb_buffer_t * ps_buf, hb_buffer_list_t * es_list, hb_psdemux_t *);
309 void hb_demux_null(hb_buffer_t * ps_buf, hb_buffer_list_t * es_list, hb_psdemux_t *);
310
311 extern const hb_muxer_t hb_demux[];
312
313 /***********************************************************************
314 * batch.c
315 **********************************************************************/
316 typedef struct hb_batch_s hb_batch_t;
317
318 hb_batch_t * hb_batch_init( hb_handle_t *h, char * path );
319 void hb_batch_close( hb_batch_t ** _d );
320 int hb_batch_title_count( hb_batch_t * d );
321 hb_title_t * hb_batch_title_scan( hb_batch_t * d, int t );
322
323 /***********************************************************************
324 * dvd.c
325 **********************************************************************/
326 typedef struct hb_bd_s hb_bd_t;
327 typedef union hb_dvd_s hb_dvd_t;
328 typedef struct hb_stream_s hb_stream_t;
329
330 hb_dvd_t * hb_dvd_init( hb_handle_t * h, const char * path );
331 int hb_dvd_title_count( hb_dvd_t * );
332 hb_title_t * hb_dvd_title_scan( hb_dvd_t *, int title, uint64_t min_duration );
333 int hb_dvd_start( hb_dvd_t *, hb_title_t *title, int chapter );
334 void hb_dvd_stop( hb_dvd_t * );
335 int hb_dvd_seek( hb_dvd_t *, float );
336 hb_buffer_t * hb_dvd_read( hb_dvd_t * );
337 int hb_dvd_chapter( hb_dvd_t * );
338 int hb_dvd_is_break( hb_dvd_t * d );
339 void hb_dvd_close( hb_dvd_t ** );
340 int hb_dvd_angle_count( hb_dvd_t * d );
341 void hb_dvd_set_angle( hb_dvd_t * d, int angle );
342 int hb_dvd_main_feature( hb_dvd_t * d, hb_list_t * list_title );
343
344 hb_bd_t * hb_bd_init( hb_handle_t *h, const char * path );
345 int hb_bd_title_count( hb_bd_t * d );
346 hb_title_t * hb_bd_title_scan( hb_bd_t * d, int t, uint64_t min_duration );
347 int hb_bd_start( hb_bd_t * d, hb_title_t *title );
348 void hb_bd_stop( hb_bd_t * d );
349 int hb_bd_seek( hb_bd_t * d, float f );
350 int hb_bd_seek_pts( hb_bd_t * d, uint64_t pts );
351 int hb_bd_seek_chapter( hb_bd_t * d, int chapter );
352 hb_buffer_t * hb_bd_read( hb_bd_t * d );
353 int hb_bd_chapter( hb_bd_t * d );
354 void hb_bd_close( hb_bd_t ** _d );
355 void hb_bd_set_angle( hb_bd_t * d, int angle );
356 int hb_bd_main_feature( hb_bd_t * d, hb_list_t * list_title );
357
358 hb_stream_t * hb_bd_stream_open( hb_handle_t *h, hb_title_t *title );
359 void hb_ts_stream_reset(hb_stream_t *stream);
360 hb_stream_t * hb_stream_open(hb_handle_t *h, const char * path,
361 hb_title_t *title, int scan);
362 void hb_stream_close( hb_stream_t ** );
363 hb_title_t * hb_stream_title_scan( hb_stream_t *, hb_title_t *);
364 hb_buffer_t * hb_stream_read( hb_stream_t * );
365 int hb_stream_seek( hb_stream_t *, float );
366 int hb_stream_seek_ts( hb_stream_t * stream, int64_t ts );
367 int hb_stream_seek_chapter( hb_stream_t *, int );
368 int hb_stream_chapter( hb_stream_t * );
369
370 hb_buffer_t * hb_ts_decode_pkt( hb_stream_t *stream, const uint8_t * pkt,
371 int chapter, int discontinuity );
372 void hb_stream_set_need_keyframe( hb_stream_t *stream, int need_keyframe );
373
374
375 #define STR4_TO_UINT32(p) \
376 ((((const uint8_t*)(p))[0] << 24) | \
377 (((const uint8_t*)(p))[1] << 16) | \
378 (((const uint8_t*)(p))[2] << 8) | \
379 ((const uint8_t*)(p))[3])
380
381 /***********************************************************************
382 * Work objects
383 **********************************************************************/
384 #define HB_CONFIG_MAX_SIZE (2*8192)
385 struct hb_esconfig_s
386 {
387 int init_delay;
388
389 union
390 {
391
392 struct
393 {
394 uint8_t bytes[HB_CONFIG_MAX_SIZE];
395 int length;
396 } mpeg4;
397
398 struct
399 {
400 uint8_t sps[HB_CONFIG_MAX_SIZE];
401 int sps_length;
402 uint8_t pps[HB_CONFIG_MAX_SIZE];
403 int pps_length;
404 } h264;
405
406 struct
407 {
408 uint8_t headers[HB_CONFIG_MAX_SIZE];
409 int headers_length;
410 } h265;
411
412 struct
413 {
414 uint8_t headers[3][HB_CONFIG_MAX_SIZE];
415 } theora;
416
417 struct
418 {
419 uint8_t bytes[HB_CONFIG_MAX_SIZE];
420 int length;
421 } extradata;
422
423 struct
424 {
425 uint8_t headers[3][HB_CONFIG_MAX_SIZE];
426 char *language;
427 } vorbis;
428 };
429 };
430
431 enum
432 {
433 WORK_NONE = 0,
434 WORK_SYNC_VIDEO,
435 WORK_SYNC_AUDIO,
436 WORK_SYNC_SUBTITLE,
437 WORK_DECVOBSUB,
438 WORK_DECSRTSUB,
439 WORK_DECTX3GSUB,
440 WORK_DECSSASUB,
441 WORK_RENDER,
442 WORK_ENCAVCODEC,
443 WORK_ENCQSV,
444 WORK_ENCX264,
445 WORK_ENCX265,
446 WORK_ENCTHEORA,
447 WORK_DECAVCODEC,
448 WORK_DECAVCODECV,
449 WORK_DECLPCM,
450 WORK_ENCLAME,
451 WORK_ENCVORBIS,
452 WORK_ENC_CA_AAC,
453 WORK_ENC_CA_HAAC,
454 WORK_ENCAVCODEC_AUDIO,
455 WORK_MUX,
456 WORK_READER,
457 WORK_DECAVSUB
458 };
459
460 extern hb_filter_object_t hb_filter_detelecine;
461 extern hb_filter_object_t hb_filter_comb_detect;
462 extern hb_filter_object_t hb_filter_decomb;
463 extern hb_filter_object_t hb_filter_deinterlace;
464 extern hb_filter_object_t hb_filter_vfr;
465 extern hb_filter_object_t hb_filter_deblock;
466 extern hb_filter_object_t hb_filter_denoise;
467 extern hb_filter_object_t hb_filter_nlmeans;
468 extern hb_filter_object_t hb_filter_chroma_smooth;
469 extern hb_filter_object_t hb_filter_render_sub;
470 extern hb_filter_object_t hb_filter_crop_scale;
471 extern hb_filter_object_t hb_filter_rotate;
472 extern hb_filter_object_t hb_filter_grayscale;
473 extern hb_filter_object_t hb_filter_pad;
474 extern hb_filter_object_t hb_filter_lapsharp;
475 extern hb_filter_object_t hb_filter_unsharp;
476 extern hb_filter_object_t hb_filter_avfilter;
477 extern hb_filter_object_t hb_filter_mt_frame;
478 extern hb_filter_object_t hb_filter_colorspace;
479 extern hb_filter_object_t hb_filter_format;
480
481 #if HB_PROJECT_FEATURE_QSV
482 extern hb_filter_object_t hb_filter_qsv;
483 extern hb_filter_object_t hb_filter_qsv_pre;
484 extern hb_filter_object_t hb_filter_qsv_post;
485 #endif
486
487 extern hb_work_object_t * hb_objects;
488
489 #define HB_WORK_IDLE 0
490 #define HB_WORK_OK 1
491 #define HB_WORK_ERROR 2
492 #define HB_WORK_DONE 3
493
494 /***********************************************************************
495 * Muxers
496 **********************************************************************/
497 typedef struct hb_mux_object_s hb_mux_object_t;
498 typedef struct hb_mux_data_s hb_mux_data_t;
499
500 #define HB_MUX_COMMON \
501 int (*init) ( hb_mux_object_t * ); \
502 int (*mux) ( hb_mux_object_t *, hb_mux_data_t *, \
503 hb_buffer_t * ); \
504 int (*end) ( hb_mux_object_t * );
505
506 #define DECLARE_MUX( a ) \
507 hb_mux_object_t * hb_mux_##a##_init( hb_job_t * );
508
509 DECLARE_MUX( mp4 );
510 DECLARE_MUX( mkv );
511 DECLARE_MUX( webm );
512 DECLARE_MUX( avformat );
513
514 void hb_deinterlace(hb_buffer_t *dst, hb_buffer_t *src);
515
516 struct hb_chapter_queue_item_s
517 {
518 int64_t start;
519 int new_chap;
520 };
521
522 struct hb_chapter_queue_s
523 {
524 hb_list_t * list_chapter;
525 };
526
527 typedef struct hb_chapter_queue_item_s hb_chapter_queue_item_t;
528 typedef struct hb_chapter_queue_s hb_chapter_queue_t;
529
530 hb_chapter_queue_t * hb_chapter_queue_init(void);
531 void hb_chapter_queue_close(hb_chapter_queue_t **_q);
532 void hb_chapter_enqueue(hb_chapter_queue_t *q, hb_buffer_t *b);
533 void hb_chapter_dequeue(hb_chapter_queue_t *q, hb_buffer_t *b);
534
535 /* Font names used for rendering subtitles */
536 #if defined(SYS_MINGW)
537 #define HB_FONT_MONO "Lucida Console"
538 #define HB_FONT_SANS "sans-serif"
539 #else
540 #define HB_FONT_MONO "monospace"
541 #define HB_FONT_SANS "sans-serif"
542 #endif
543
544 #endif // HANDBRAKE_INTERNAL_H
545