3 Copyright (c) 2003-2015 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
11 #include "extras/cl.h"
13 /***********************************************************************
14 * Hardware Decode Context
15 **********************************************************************/
21 /***********************************************************************
23 **********************************************************************/
24 void hb_log( char * log
, ... ) HB_WPRINTF(1,2);
25 extern int global_verbosity_level
; // Global variable for hb_deep_log
26 typedef enum hb_debug_level_s
28 HB_SUPPORT_LOG
= 1, // helpful in tech support
29 HB_HOUSEKEEPING_LOG
= 2, // stuff we hate scrolling through
30 HB_GRANULAR_LOG
= 3 // sample-by-sample
32 void hb_valog( hb_debug_level_t level
, const char * prefix
, const char * log
, va_list args
) HB_WPRINTF(3,0);
33 void hb_deep_log( hb_debug_level_t level
, char * log
, ... ) HB_WPRINTF(2,3);
34 void hb_error( char * fmt
, ...) HB_WPRINTF(1,2);
35 void hb_hexdump( hb_debug_level_t level
, const char * label
, const uint8_t * data
, int len
);
37 int hb_list_bytes( hb_list_t
* );
38 void hb_list_seebytes( hb_list_t
* l
, uint8_t * dst
, int size
);
39 void hb_list_getbytes( hb_list_t
* l
, uint8_t * dst
, int size
,
40 uint64_t * pts
, uint64_t * pos
);
41 void hb_list_empty( hb_list_t
** );
43 hb_title_t
* hb_title_init( char * dvd
, int index
);
44 void hb_title_close( hb_title_t
** );
46 /***********************************************************************
48 **********************************************************************/
49 int hb_get_pid( hb_handle_t
* );
50 void hb_set_state( hb_handle_t
*, hb_state_t
* );
51 void hb_set_work_error( hb_handle_t
* h
, hb_error_code err
);
52 void hb_job_setup_passes(hb_handle_t
*h
, hb_job_t
*job
, hb_list_t
*list_pass
);
54 /***********************************************************************
56 **********************************************************************/
59 * Holds a packet of data that is moving through the transcoding process.
61 * May have metadata associated with it via extra fields
62 * that are conditionally used depending on the type of packet.
64 struct hb_buffer_settings_s
66 enum { AUDIO_BUF
, VIDEO_BUF
, SUBTITLE_BUF
, FRAME_BUF
, OTHER_BUF
} type
;
68 int id
; // ID of the track that the packet comes from
69 int64_t start
; // start time of frame
70 double duration
; // Actual duration, may be fractional ticks
71 int64_t stop
; // stop time of frame
72 int64_t renderOffset
; // DTS used by b-frame offsets in muxmp4
74 uint8_t discontinuity
;
75 int new_chap
; // Video packets: if non-zero, is the index of the chapter whose boundary was crossed
77 #define HB_FRAME_IDR 0x01
78 #define HB_FRAME_I 0x02
79 #define HB_FRAME_AUDIO 0x04
80 #define HB_FRAME_SUBTITLE 0x08
81 #define HB_FRAME_P 0x10
82 #define HB_FRAME_B 0x20
83 #define HB_FRAME_BREF 0x40
84 #define HB_FRAME_KEY 0x0F
85 #define HB_FRAME_REF 0xF0
88 // Picture flags used by filters
89 #ifndef PIC_FLAG_REPEAT_FIRST_FIELD
90 #define PIC_FLAG_REPEAT_FIRST_FIELD 0x0100
92 #ifndef PIC_FLAG_TOP_FIELD_FIRST
93 #define PIC_FLAG_TOP_FIELD_FIRST 0x0008
95 #ifndef PIC_FLAG_PROGRESSIVE_FRAME
96 #define PIC_FLAG_PROGRESSIVE_FRAME 0x0010
98 #define PIC_FLAG_REPEAT_FRAME 0x0200
99 #define HB_BUF_FLAG_EOF 0x0400
103 struct hb_image_format_s
114 int size
; // size of this packet
115 int alloc
; // used internally by the packet allocator (hb_buffer_init)
116 uint8_t * data
; // packet data
117 int offset
; // used internally by packet lists (hb_list_t)
120 * Corresponds to the order that this packet was read from the demuxer.
122 * It is important that video decoder work-objects pass this value through
123 * from their input packets to the output packets they generate. Otherwise
124 * RENDERSUB subtitles (especially VOB subtitles) will break.
126 * Subtitle decoder work-objects that output a renderable subtitle
127 * format (ex: PICTURESUB) must also be careful to pass the sequence number
128 * through for the same reason.
132 hb_buffer_settings_t s
;
143 } plane
[4]; // 3 Color components + alpha
148 void *filter_details
;
156 enum { HOST
, DEVICE
} buffer_location
;
159 // libav may attach AV_PKT_DATA_PALETTE side data to some AVPackets
160 // Store this data here when read and pass to decoder.
161 hb_buffer_t
* palette
;
163 // PICTURESUB subtitle packets:
165 // Video packets (after processing by the hb_sync_video work-object):
166 // A (copy of a) PICTURESUB subtitle packet that needs to be burned into
167 // this video packet by the vobsub renderer filter
169 // Subtitles that are simply passed thru are NOT attached to the
170 // associated video packets.
173 // Packets in a list:
174 // the next packet in the list
178 void hb_buffer_pool_init( void );
179 void hb_buffer_pool_free( void );
181 hb_buffer_t
* hb_buffer_init( int size
);
182 hb_buffer_t
* hb_buffer_eof_init( void );
183 hb_buffer_t
* hb_frame_buffer_init( int pix_fmt
, int w
, int h
);
184 void hb_buffer_init_planes( hb_buffer_t
* b
);
185 void hb_buffer_realloc( hb_buffer_t
*, int size
);
186 void hb_video_buffer_realloc( hb_buffer_t
* b
, int w
, int h
);
187 void hb_buffer_reduce( hb_buffer_t
* b
, int size
);
188 void hb_buffer_close( hb_buffer_t
** );
189 hb_buffer_t
* hb_buffer_dup( const hb_buffer_t
* src
);
190 int hb_buffer_copy( hb_buffer_t
* dst
, const hb_buffer_t
* src
);
191 void hb_buffer_swap_copy( hb_buffer_t
*src
, hb_buffer_t
*dst
);
192 void hb_buffer_move_subs( hb_buffer_t
* dst
, hb_buffer_t
* src
);
193 hb_image_t
* hb_image_init(int pix_fmt
, int width
, int height
);
194 hb_image_t
* hb_buffer_to_image(hb_buffer_t
*buf
);
196 hb_fifo_t
* hb_fifo_init( int capacity
, int thresh
);
197 void hb_fifo_register_full_cond( hb_fifo_t
* f
, hb_cond_t
* c
);
198 int hb_fifo_size( hb_fifo_t
* );
199 int hb_fifo_size_bytes( hb_fifo_t
* );
200 int hb_fifo_is_full( hb_fifo_t
* );
201 float hb_fifo_percent_full( hb_fifo_t
* f
);
202 hb_buffer_t
* hb_fifo_get( hb_fifo_t
* );
203 hb_buffer_t
* hb_fifo_get_wait( hb_fifo_t
* );
204 hb_buffer_t
* hb_fifo_see( hb_fifo_t
* );
205 hb_buffer_t
* hb_fifo_see_wait( hb_fifo_t
* );
206 hb_buffer_t
* hb_fifo_see2( hb_fifo_t
* );
207 void hb_fifo_push( hb_fifo_t
*, hb_buffer_t
* );
208 void hb_fifo_push_wait( hb_fifo_t
*, hb_buffer_t
* );
209 int hb_fifo_full_wait( hb_fifo_t
* f
);
210 void hb_fifo_push_head( hb_fifo_t
*, hb_buffer_t
* );
211 void hb_fifo_push_list_element( hb_fifo_t
*fifo
, hb_buffer_t
*buffer_list
);
212 hb_buffer_t
* hb_fifo_get_list_element( hb_fifo_t
*fifo
);
213 void hb_fifo_close( hb_fifo_t
** );
214 void hb_fifo_flush( hb_fifo_t
* f
);
216 static inline int hb_image_stride( int pix_fmt
, int width
, int plane
)
218 int linesize
= av_image_get_linesize( pix_fmt
, width
, plane
);
220 // Make buffer SIMD friendly.
221 // Decomb requires stride aligned to 32 bytes
222 // TODO: eliminate extra buffer copies in decomb
223 linesize
= MULTIPLE_MOD_UP( linesize
, 32 );
227 static inline int hb_image_width(int pix_fmt
, int width
, int plane
)
229 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(pix_fmt
);
231 if (desc
!= NULL
&& (plane
== 1 || plane
== 2))
233 // The wacky arithmatic assures rounding up.
234 width
= -((-width
) >> desc
->log2_chroma_w
);
240 static inline int hb_image_height_stride(int pix_fmt
, int height
, int plane
)
242 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(pix_fmt
);
244 // Decomb requires 6 extra lines and stride aligned to 32 bytes
245 height
= MULTIPLE_MOD_UP(height
+ 6, 32);
246 if (desc
!= NULL
&& (plane
== 1 || plane
== 2))
248 height
= height
>> desc
->log2_chroma_h
;
254 static inline int hb_image_height(int pix_fmt
, int height
, int plane
)
256 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(pix_fmt
);
258 if (desc
!= NULL
&& (plane
== 1 || plane
== 2))
260 // The wacky arithmatic assures rounding up.
261 height
= -((-height
) >> desc
->log2_chroma_h
);
267 // this routine gets a buffer for an uncompressed YUV420 video frame
268 // with dimensions width x height.
269 static inline hb_buffer_t
* hb_video_buffer_init( int width
, int height
)
271 return hb_frame_buffer_init( AV_PIX_FMT_YUV420P
, width
, height
);
274 /***********************************************************************
275 * Threads: update.c, scan.c, work.c, reader.c, muxcommon.c
276 **********************************************************************/
277 hb_thread_t
* hb_update_init( int * build
, char * version
);
278 hb_thread_t
* hb_scan_init( hb_handle_t
*, volatile int * die
,
279 const char * path
, int title_index
,
280 hb_title_set_t
* title_set
, int preview_count
,
281 int store_previews
, uint64_t min_duration
);
282 hb_thread_t
* hb_work_init( hb_list_t
* jobs
,
283 volatile int * die
, hb_error_code
* error
, hb_job_t
** job
);
284 void ReadLoop( void * _w
);
285 hb_work_object_t
* hb_muxer_init( hb_job_t
* );
286 hb_work_object_t
* hb_get_work( hb_handle_t
*, int );
287 hb_work_object_t
* hb_codec_decoder( hb_handle_t
*, int );
288 hb_work_object_t
* hb_codec_encoder( hb_handle_t
*, int );
290 /***********************************************************************
292 **********************************************************************/
293 hb_work_object_t
* hb_sync_init( hb_job_t
* job
);
295 /***********************************************************************
297 **********************************************************************/
299 int64_t last_scr
; /* unadjusted SCR from most recent pack */
301 int64_t last_pts
; /* last pts we saw */
302 int scr_changes
; /* number of SCR discontinuities */
306 typedef void (*hb_muxer_t
)(hb_buffer_t
*, hb_list_t
*, hb_psdemux_t
*);
308 void hb_demux_ps( hb_buffer_t
* ps_buf
, hb_list_t
* es_list
, hb_psdemux_t
* );
309 void hb_demux_ts( hb_buffer_t
* ps_buf
, hb_list_t
* es_list
, hb_psdemux_t
* );
310 void hb_demux_null( hb_buffer_t
* ps_buf
, hb_list_t
* es_list
, hb_psdemux_t
* );
312 extern const hb_muxer_t hb_demux
[];
314 /***********************************************************************
316 **********************************************************************/
317 typedef struct hb_batch_s hb_batch_t
;
319 hb_batch_t
* hb_batch_init( hb_handle_t
*h
, char * path
);
320 void hb_batch_close( hb_batch_t
** _d
);
321 int hb_batch_title_count( hb_batch_t
* d
);
322 hb_title_t
* hb_batch_title_scan( hb_batch_t
* d
, int t
);
324 /***********************************************************************
326 **********************************************************************/
327 typedef struct hb_bd_s hb_bd_t
;
328 typedef union hb_dvd_s hb_dvd_t
;
329 typedef struct hb_stream_s hb_stream_t
;
331 hb_dvd_t
* hb_dvd_init( hb_handle_t
* h
, char * path
);
332 int hb_dvd_title_count( hb_dvd_t
* );
333 hb_title_t
* hb_dvd_title_scan( hb_dvd_t
*, int title
, uint64_t min_duration
);
334 int hb_dvd_start( hb_dvd_t
*, hb_title_t
*title
, int chapter
);
335 void hb_dvd_stop( hb_dvd_t
* );
336 int hb_dvd_seek( hb_dvd_t
*, float );
337 hb_buffer_t
* hb_dvd_read( hb_dvd_t
* );
338 int hb_dvd_chapter( hb_dvd_t
* );
339 int hb_dvd_is_break( hb_dvd_t
* d
);
340 void hb_dvd_close( hb_dvd_t
** );
341 int hb_dvd_angle_count( hb_dvd_t
* d
);
342 void hb_dvd_set_angle( hb_dvd_t
* d
, int angle
);
343 int hb_dvd_main_feature( hb_dvd_t
* d
, hb_list_t
* list_title
);
345 hb_bd_t
* hb_bd_init( hb_handle_t
*h
, char * path
);
346 int hb_bd_title_count( hb_bd_t
* d
);
347 hb_title_t
* hb_bd_title_scan( hb_bd_t
* d
, int t
, uint64_t min_duration
);
348 int hb_bd_start( hb_bd_t
* d
, hb_title_t
*title
);
349 void hb_bd_stop( hb_bd_t
* d
);
350 int hb_bd_seek( hb_bd_t
* d
, float f
);
351 int hb_bd_seek_pts( hb_bd_t
* d
, uint64_t pts
);
352 int hb_bd_seek_chapter( hb_bd_t
* d
, int chapter
);
353 hb_buffer_t
* hb_bd_read( hb_bd_t
* d
);
354 int hb_bd_chapter( hb_bd_t
* d
);
355 void hb_bd_close( hb_bd_t
** _d
);
356 void hb_bd_set_angle( hb_bd_t
* d
, int angle
);
357 int hb_bd_main_feature( hb_bd_t
* d
, hb_list_t
* list_title
);
359 hb_stream_t
* hb_bd_stream_open( hb_handle_t
*h
, hb_title_t
*title
);
360 void hb_ts_stream_reset(hb_stream_t
*stream
);
361 hb_stream_t
* hb_stream_open(hb_handle_t
*h
, char * path
,
362 hb_title_t
*title
, int scan
);
363 void hb_stream_close( hb_stream_t
** );
364 hb_title_t
* hb_stream_title_scan( hb_stream_t
*, hb_title_t
*);
365 hb_buffer_t
* hb_stream_read( hb_stream_t
* );
366 int hb_stream_seek( hb_stream_t
*, float );
367 int hb_stream_seek_ts( hb_stream_t
* stream
, int64_t ts
);
368 int hb_stream_seek_chapter( hb_stream_t
*, int );
369 int hb_stream_chapter( hb_stream_t
* );
371 hb_buffer_t
* hb_ts_decode_pkt( hb_stream_t
*stream
, const uint8_t * pkt
,
372 int chapter
, int discontinuity
);
373 void hb_stream_set_need_keyframe( hb_stream_t
*stream
, int need_keyframe
);
376 #define STR4_TO_UINT32(p) \
377 ((((const uint8_t*)(p))[0] << 24) | \
378 (((const uint8_t*)(p))[1] << 16) | \
379 (((const uint8_t*)(p))[2] << 8) | \
380 ((const uint8_t*)(p))[3])
382 /***********************************************************************
384 **********************************************************************/
385 #define HB_CONFIG_MAX_SIZE (2*8192)
391 uint8_t bytes
[HB_CONFIG_MAX_SIZE
];
397 uint8_t sps
[HB_CONFIG_MAX_SIZE
];
399 uint8_t pps
[HB_CONFIG_MAX_SIZE
];
406 uint8_t headers
[HB_CONFIG_MAX_SIZE
];
412 uint8_t headers
[3][HB_CONFIG_MAX_SIZE
];
417 uint8_t bytes
[HB_CONFIG_MAX_SIZE
];
423 uint8_t headers
[3][HB_CONFIG_MAX_SIZE
];
453 WORK_ENCAVCODEC_AUDIO
,
459 extern hb_filter_object_t hb_filter_detelecine
;
460 extern hb_filter_object_t hb_filter_deinterlace
;
461 extern hb_filter_object_t hb_filter_deblock
;
462 extern hb_filter_object_t hb_filter_denoise
;
463 extern hb_filter_object_t hb_filter_nlmeans
;
464 extern hb_filter_object_t hb_filter_decomb
;
465 extern hb_filter_object_t hb_filter_rotate
;
466 extern hb_filter_object_t hb_filter_crop_scale
;
467 extern hb_filter_object_t hb_filter_render_sub
;
468 extern hb_filter_object_t hb_filter_vfr
;
471 extern hb_filter_object_t hb_filter_qsv
;
472 extern hb_filter_object_t hb_filter_qsv_pre
;
473 extern hb_filter_object_t hb_filter_qsv_post
;
476 extern hb_work_object_t
* hb_objects
;
478 #define HB_WORK_IDLE 0
480 #define HB_WORK_ERROR 2
481 #define HB_WORK_DONE 3
483 /***********************************************************************
485 **********************************************************************/
486 typedef struct hb_mux_object_s hb_mux_object_t
;
487 typedef struct hb_mux_data_s hb_mux_data_t
;
489 #define HB_MUX_COMMON \
490 int (*init) ( hb_mux_object_t * ); \
491 int (*mux) ( hb_mux_object_t *, hb_mux_data_t *, \
493 int (*end) ( hb_mux_object_t * );
495 #define DECLARE_MUX( a ) \
496 hb_mux_object_t * hb_mux_##a##_init( hb_job_t * );
500 DECLARE_MUX( avformat
);
502 void hb_muxmp4_process_subtitle_style( uint8_t *input
,
504 uint8_t *style
, uint16_t *stylesize
);
506 void hb_deinterlace(hb_buffer_t
*dst
, hb_buffer_t
*src
);