2 #ifndef __SCHRO_ENCODER_H__
3 #define __SCHRO_ENCODER_H__
5 #include <schroedinger/schroutils.h>
6 #include <schroedinger/schropack.h>
7 #include <schroedinger/schrobuffer.h>
8 #include <schroedinger/schroparams.h>
9 #include <schroedinger/schroframe.h>
10 #include <schroedinger/schroasync.h>
11 #include <schroedinger/schroqueue.h>
12 #include <schroedinger/schromotion.h>
13 #include <schroedinger/schrohistogram.h>
14 #include <schroedinger/schrolist.h>
18 typedef struct _SchroEncoder SchroEncoder
;
19 typedef struct _SchroEncoderFrame SchroEncoderFrame
;
20 typedef struct _SchroEncoderSetting SchroEncoderSetting
;
23 SCHRO_STATE_NEED_FRAME
,
24 SCHRO_STATE_HAVE_BUFFER
,
26 SCHRO_STATE_END_OF_STREAM
29 #ifdef SCHRO_ENABLE_UNSTABLE_API
31 SCHRO_ENCODER_FRAME_STAGE_NEW
= 0,
32 SCHRO_ENCODER_FRAME_STAGE_ANALYSE
,
33 SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_1
,
34 SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_2
,
35 SCHRO_ENCODER_FRAME_STAGE_HAVE_GOP
,
36 SCHRO_ENCODER_FRAME_STAGE_HAVE_PARAMS
,
37 SCHRO_ENCODER_FRAME_STAGE_PREDICT_ROUGH
,
38 SCHRO_ENCODER_FRAME_STAGE_PREDICT_PEL
,
39 SCHRO_ENCODER_FRAME_STAGE_PREDICT_SUBPEL
,
40 SCHRO_ENCODER_FRAME_STAGE_MODE_DECISION
,
41 SCHRO_ENCODER_FRAME_STAGE_HAVE_REFS
,
42 SCHRO_ENCODER_FRAME_STAGE_HAVE_QUANTS
,
43 SCHRO_ENCODER_FRAME_STAGE_ENCODING
,
44 SCHRO_ENCODER_FRAME_STAGE_RECONSTRUCT
,
45 SCHRO_ENCODER_FRAME_STAGE_POSTANALYSE
,
46 SCHRO_ENCODER_FRAME_STAGE_DONE
,
47 SCHRO_ENCODER_FRAME_STAGE_FREE
,
48 SCHRO_ENCODER_FRAME_STAGE_LAST
/* this should be last */
49 } SchroEncoderFrameStateEnum
;
53 SCHRO_ENCODER_PERCEPTUAL_CONSTANT
,
54 SCHRO_ENCODER_PERCEPTUAL_CCIR959
,
55 SCHRO_ENCODER_PERCEPTUAL_MOO
,
56 SCHRO_ENCODER_PERCEPTUAL_MANOS_SAKRISON
57 } SchroEncoderPerceptualEnum
;
60 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD
,
61 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
,
62 SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY
,
63 SCHRO_ENCODER_RATE_CONTROL_LOSSLESS
,
64 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_LAMBDA
,
65 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_ERROR
,
66 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY
67 } SchroEncoderRateControlEnum
;
70 SCHRO_ENCODER_GOP_ADAPTIVE
,
71 SCHRO_ENCODER_GOP_INTRA_ONLY
,
72 SCHRO_ENCODER_GOP_BACKREF
,
73 SCHRO_ENCODER_GOP_CHAINED_BACKREF
,
74 SCHRO_ENCODER_GOP_BIREF
,
75 SCHRO_ENCODER_GOP_CHAINED_BIREF
,
76 } SchroEncoderGOPEnum
;
79 SCHRO_ENCODER_PROFILE_AUTO
,
80 SCHRO_ENCODER_PROFILE_VC2_LOW_DELAY
,
81 SCHRO_ENCODER_PROFILE_VC2_SIMPLE
,
82 SCHRO_ENCODER_PROFILE_VC2_MAIN
,
83 SCHRO_ENCODER_PROFILE_MAIN
84 } SchroEncoderProfile
;
86 #ifdef SCHRO_ENABLE_UNSTABLE_API
87 typedef int (*SchroEngineIterateFunc
) (SchroEncoder
*encoder
);
89 /* forward declaration */
90 struct _SchroMotionEst
;
93 struct _SchroEncoderFrame
{
96 //SchroEncoderFrameStateEnum state;
97 //SchroEncoderFrameStateEnum needed_state;
98 SchroEncoderFrameStateEnum working
;
103 unsigned int expired_reference
;
105 /* Bits telling the engine stages which stuff needs to happen */
106 unsigned int need_extension
;
107 unsigned int need_downsampling
;
108 unsigned int need_upsampling
;
109 unsigned int need_filtering
;
110 unsigned int need_average_luma
;
111 unsigned int need_mad
;
113 /* bits indicating that a particular analysis has happened. Mainly
114 * for verification */
115 unsigned int have_estimate_tables
;
116 unsigned int have_histograms
;
117 unsigned int have_scene_change_score
;
118 unsigned int have_downsampling
;
119 unsigned int have_upsampling
;
120 unsigned int have_average_luma
;
121 unsigned int have_mad
;
123 SchroAsyncStage stages
[SCHRO_ENCODER_FRAME_STAGE_LAST
];
127 int start_sequence_header
;
130 SchroPictureNumber frame_number
;
131 SchroFrame
*original_frame
;
132 SchroFrame
*filtered_frame
;
133 SchroFrame
*downsampled_frames
[8];
134 SchroFrame
*reconstructed_frame
;
135 SchroFrame
*upsampled_original_frame
;
137 int sc_mad
; /* shot change mean absolute difference */
138 double sc_threshold
; /* shot change threshold */
140 SchroBuffer
*sequence_header_buffer
;
141 SchroList
*inserted_buffers
;
142 int output_buffer_size
;
143 SchroBuffer
*output_buffer
;
144 int presentation_frame
;
150 SchroPictureNumber picture_number_ref
[2];
151 SchroPictureNumber retired_picture_number
;
153 int16_t slice_y_dc_values
[100];
154 int16_t slice_u_dc_values
[100];
155 int16_t slice_v_dc_values
[100];
160 int slice_y_trailing_zeros
;
161 int slice_uv_trailing_zeros
;
162 SchroFrameData luma_subbands
[SCHRO_LIMIT_SUBBANDS
];
163 SchroFrameData chroma1_subbands
[SCHRO_LIMIT_SUBBANDS
];
164 SchroFrameData chroma2_subbands
[SCHRO_LIMIT_SUBBANDS
];
166 /* from the old SchroEncoderTask */
173 SchroBuffer
*subband_buffer
;
177 int *quant_indices
[3][SCHRO_LIMIT_SUBBANDS
];
179 double est_entropy
[3][SCHRO_LIMIT_SUBBANDS
][60];
180 double actual_subband_bits
[3][SCHRO_LIMIT_SUBBANDS
];
181 double est_error
[3][SCHRO_LIMIT_SUBBANDS
][60];
184 SchroEncoder
*encoder
;
185 SchroFrame
*iwt_frame
;
186 SchroFrame
*quant_frame
;
187 SchroFrame
*prediction_frame
;
189 SchroEncoderFrame
*previous_frame
;
190 SchroEncoderFrame
*ref_frame
[2];
192 struct _SchroMotionEst
*me
;
193 struct _SchroRoughME
*rme
[2];
194 struct _SchroPhaseCorr
*phasecorr
[2];
195 struct _SchroHierBm
*hier_bm
[2];
196 struct _SchroMe
*deep_me
;
200 SchroHistogram subband_hists
[3][SCHRO_LIMIT_SUBBANDS
];
201 SchroHistogram hist_test
;
205 double picture_weight
;
206 double scene_change_score
;
210 int allocated_residual_bits
;
211 int allocated_mc_bits
;
213 double frame_me_lambda
;
214 int estimated_residual_bits
;
215 int estimated_mc_bits
;
217 int actual_residual_bits
;
220 double mean_squared_error_luma
;
221 double mean_squared_error_chroma
;
224 double estimated_arith_context_ratio
;
226 double badblock_ratio
;
227 double dcblock_ratio
;
231 struct _SchroEncoder
{
235 SchroPictureNumber next_frame_number
;
237 SchroQueue
*frame_queue
;
239 SchroEncoderFrame
*reference_pictures
[SCHRO_LIMIT_REFERENCE_FRAMES
];
240 SchroEncoderFrame
*last_frame
;
242 int assemble_packets
;
245 SchroVideoFormat video_format
;
250 int input_frame_depth
;
251 int intermediate_frame_depth
;
259 // Buffer model parameters for CBR and (TODO) constrained VBR coding
263 double noise_threshold
;
266 int perceptual_weighting
;
267 double perceptual_distance
;
270 SchroEncoderProfile force_profile
;
276 schro_bool enable_psnr
;
277 schro_bool enable_ssim
;
278 schro_bool enable_md5
;
284 int motion_block_size
;
285 int motion_block_overlap
;
286 schro_bool interlaced_coding
;
287 schro_bool enable_internal_testing
;
288 schro_bool enable_noarith
;
289 schro_bool enable_fullscan_estimation
;
290 schro_bool enable_hierarchical_estimation
;
291 schro_bool enable_zero_estimation
;
292 schro_bool enable_phasecorr_estimation
;
293 schro_bool enable_bigblock_estimation
;
294 schro_bool enable_multiquant
;
295 schro_bool enable_dc_multiquant
;
296 schro_bool enable_global_motion
;
297 schro_bool enable_scene_change_detection
;
298 schro_bool enable_deep_estimation
;
299 schro_bool enable_rdo_cbr
;
300 schro_bool enable_chroma_me
;
305 double magic_dc_metric_offset
;
306 double magic_subband0_lambda_scale
;
307 double magic_chroma_lambda_scale
;
308 double magic_nonref_lambda_scale
;
309 double magic_I_lambda_scale
;
310 double magic_P_lambda_scale
;
311 double magic_B_lambda_scale
;
312 double magic_me_lambda_scale
;
313 double magic_allocation_scale
;
314 double magic_inter_cpd_scale
;
315 double magic_keyframe_weight
;
316 double magic_scene_change_threshold
;
317 double magic_inter_p_weight
;
318 double magic_inter_b_weight
;
319 double magic_me_bailout_limit
;
320 double magic_bailout_weight
;
321 double magic_error_power
;
322 double magic_subgroup_length
;
323 double magic_badblock_multiplier_nonref
;
324 double magic_badblock_multiplier_ref
;
325 double magic_block_search_threshold
;
326 double magic_scan_distance
;
327 double magic_diagonal_lambda_scale
;
331 void (*init_frame
) (SchroEncoderFrame
*frame
);
332 void (*handle_gop
) (SchroEncoder
*encoder
, int i
);
333 int (*setup_frame
) (SchroEncoderFrame
*frame
);
334 int (*handle_quants
) (SchroEncoder
*encoder
, int i
);
339 int end_of_stream_handled
;
340 int end_of_stream_pulled
;
343 int force_sequence_header
;
345 SchroPictureNumber au_frame
;
350 SchroList
*inserted_buffers
;
354 SchroEngineIterateFunc engine_iterate
;
355 int quantiser_engine
;
358 int downsample_levels
;
362 double cycles_per_degree_horiz
;
363 double cycles_per_degree_vert
;
365 double intra_subband_weights
[SCHRO_N_WAVELETS
][SCHRO_LIMIT_TRANSFORM_DEPTH
][SCHRO_LIMIT_SUBBANDS
];
366 double inter_subband_weights
[SCHRO_N_WAVELETS
][SCHRO_LIMIT_TRANSFORM_DEPTH
][SCHRO_LIMIT_SUBBANDS
];
367 SchroHistogramTable intra_hist_tables
[60];
372 double average_arith_context_ratios_intra
[3][SCHRO_LIMIT_SUBBANDS
];
373 double average_arith_context_ratios_inter
[3][SCHRO_LIMIT_SUBBANDS
];
375 double frame_stats
[21];
377 /* engine specific stuff */
379 int bits_per_picture
;
380 int subgroup_position
;
384 int B_complexity_sum
;
385 long int I_frame_alloc
;
386 long int P_frame_alloc
;
387 long int B_frame_alloc
;
390 // Current qf, from which is derived ...
392 // lambda to use for intra pictures in CBR mode
393 double intra_cbr_lambda
;
403 SCHRO_ENCODER_SETTING_TYPE_BOOLEAN
,
404 SCHRO_ENCODER_SETTING_TYPE_INT
,
405 SCHRO_ENCODER_SETTING_TYPE_ENUM
,
406 SCHRO_ENCODER_SETTING_TYPE_DOUBLE
,
407 SCHRO_ENCODER_SETTING_TYPE_LAST
408 } SchroEncoderSettingTypeEnum
;
410 struct _SchroEncoderSetting
{
412 SchroEncoderSettingTypeEnum type
;
416 double default_value
;
418 const char **enum_list
;
422 SCHRO_MOTION_FIELD_HIER_REF0
,
423 SCHRO_MOTION_FIELD_HIER1_REF0
,
424 SCHRO_MOTION_FIELD_HIER2_REF0
,
425 SCHRO_MOTION_FIELD_HIER3_REF0
,
426 SCHRO_MOTION_FIELD_HIER_REF1
,
427 SCHRO_MOTION_FIELD_HIER1_REF1
,
428 SCHRO_MOTION_FIELD_HIER2_REF1
,
429 SCHRO_MOTION_FIELD_HIER3_REF1
,
430 SCHRO_MOTION_FIELD_PHASECORR_REF0
,
431 SCHRO_MOTION_FIELD_PHASECORR_REF1
,
432 SCHRO_MOTION_FIELD_DC
,
433 SCHRO_MOTION_FIELD_GLOBAL_REF0
,
434 SCHRO_MOTION_FIELD_GLOBAL_REF1
,
435 SCHRO_MOTION_FIELD_ZERO_REF0
,
436 SCHRO_MOTION_FIELD_ZERO_REF1
,
437 SCHRO_MOTION_FIELD_FULLSCAN_REF0
,
438 SCHRO_MOTION_FIELD_FULLSCAN_REF1
,
439 SCHRO_MOTION_FIELD_LAST
442 SchroEncoder
* schro_encoder_new (void);
443 void schro_encoder_free (SchroEncoder
*encoder
);
444 SchroVideoFormat
* schro_encoder_get_video_format (SchroEncoder
*encoder
);
445 void schro_encoder_set_video_format (SchroEncoder
*encoder
,
446 SchroVideoFormat
*video_format
);
447 void schro_encoder_set_frame_lambda (SchroEncoderFrame
* frame
);
448 void schro_encoder_end_of_stream (SchroEncoder
*encoder
);
449 int schro_encoder_push_ready (SchroEncoder
*encoder
);
450 void schro_encoder_push_frame (SchroEncoder
*encoder
, SchroFrame
*frame
);
451 void schro_encoder_push_frame_full (SchroEncoder
*encoder
, SchroFrame
*frame
, void *priv
);
452 void schro_encoder_force_sequence_header (SchroEncoder
*encoder
);
454 SchroBuffer
* schro_encoder_encode_auxiliary_data (SchroEncoder
*encoder
,
455 SchroAuxiliaryDataID id
, const void *data
, int size
);
456 void schro_encoder_encode_parse_info (SchroPack
*bits
, int parse_code
);
457 void schro_encoder_insert_buffer (SchroEncoder
*encoder
, SchroBuffer
*buffer
);
458 void schro_encoder_frame_insert_buffer (SchroEncoderFrame
*frame
, SchroBuffer
*buffer
);
459 void schro_encoder_start (SchroEncoder
*encoder
);
461 void schro_encoder_set_packet_assembly (SchroEncoder
*encoder
, int value
);
462 SchroStateEnum
schro_encoder_wait (SchroEncoder
*encoder
);
463 SchroBuffer
* schro_encoder_pull (SchroEncoder
*encoder
,
464 int *n_decodable_frames
);
465 SchroBuffer
* schro_encoder_pull_full (SchroEncoder
*encoder
, int *presentation_frame
,
467 SchroBuffer
* schro_encoder_encode_sequence_header (SchroEncoder
*encoder
);
469 int schro_encoder_get_n_settings (void);
470 const SchroEncoderSetting
*schro_encoder_get_setting_info (int i
);
471 void schro_encoder_setting_set_double (SchroEncoder
*encoder
, const char *name
,
473 double schro_encoder_setting_get_double (SchroEncoder
*encoder
, const char *name
);
475 int schro_encoder_get_frame_stats_size (SchroEncoder
*encoder
);
476 void schro_encoder_get_frame_stats (SchroEncoder
*encoder
, double *dest
, int n
);
478 #ifdef SCHRO_ENABLE_UNSTABLE_API
480 void schro_encoder_set_default_subband_weights (SchroEncoder
*encoder
);
481 void schro_encoder_calculate_subband_weights (SchroEncoder
*encoder
,
482 double (*perceptual_weight
)(double));
483 double schro_encoder_perceptual_weight_constant (double cpd
);
484 double schro_encoder_perceptual_weight_ccir959 (double cpd
);
485 double schro_encoder_perceptual_weight_moo (double cpd
);
486 double schro_encoder_perceptual_weight_manos_sakrison (double cpd
);
488 void schro_encoder_init_subbands (SchroEncoderFrame
*frame
);
489 void schro_encoder_encode_subband (SchroEncoderFrame
*frame
, int component
, int index
);
490 void schro_encoder_encode_subband_noarith (SchroEncoderFrame
*frame
, int component
, int index
);
492 void schro_encoder_analyse_picture (SchroAsyncStage
*stage
);
493 void schro_encoder_predict_rough_picture (SchroAsyncStage
*stage
);
494 void schro_encoder_predict_pel_picture (SchroAsyncStage
*stage
);
495 void schro_encoder_predict_subpel_picture (SchroAsyncStage
*stage
);
497 void schro_encoder_fullpel_predict_picture (SchroAsyncStage
*stage
);
498 void schro_encoder_mode_decision (SchroAsyncStage
*stage
);
500 void schro_encoder_encode_picture (SchroAsyncStage
*stage
);
501 void schro_encoder_reconstruct_picture (SchroAsyncStage
*stage
);
502 void schro_encoder_postanalyse_picture (SchroAsyncStage
*stage
);
504 SchroFrame
* schro_encoder_frame_queue_get (SchroEncoder
*encoder
,
505 SchroPictureNumber frame_number
);
506 void schro_encoder_frame_queue_remove (SchroEncoder
*encoder
,
507 SchroPictureNumber frame_number
);
508 SchroEncoderFrame
* schro_encoder_reference_get (SchroEncoder
*encoder
,
509 SchroPictureNumber frame_number
);
510 void schro_encoder_encode_picture_header (SchroEncoderFrame
*frame
);
511 SchroBuffer
* schro_encoder_encode_end_of_stream (SchroEncoder
*encoder
);
512 void schro_encoder_clean_up_transform (SchroEncoderFrame
*frame
);
513 void schro_encoder_choose_quantisers (SchroEncoderFrame
*frame
);
514 void schro_encoder_output_push (SchroEncoder
*encoder
,
515 SchroBuffer
*buffer
, int slot
, int presentation_frame
);
517 SchroEncoderFrame
* schro_encoder_frame_new (SchroEncoder
*encoder
);
518 void schro_encoder_frame_ref (SchroEncoderFrame
*frame
);
519 void schro_encoder_frame_unref (SchroEncoderFrame
*frame
);
520 int schro_encoder_frame_is_B_frame (SchroEncoderFrame
* frame
);
521 void schro_encoder_encode_lowdelay_transform_data (SchroEncoderFrame
*frame
);
522 void schro_encoder_estimate_entropy (SchroEncoderFrame
*frame
);
523 void schro_encoder_recalculate_allocations (SchroEncoder
*encoder
);
525 void schro_encoder_calculate_test_info (SchroEncoderFrame
*frame
);
527 void schro_encoder_init_error_tables (SchroEncoder
*encoder
);
529 int schro_encoder_frame_get_quant_index (SchroEncoderFrame
*frame
, int component
,
530 int index
, int x
, int y
);
531 void schro_encoder_frame_set_quant_index (SchroEncoderFrame
*frame
, int component
,
532 int index
, int x
, int y
, int quant_index
);