5 #define SCHRO_ARITH_DEFINE_INLINE
6 #include <schroedinger/schro.h>
14 /* Used for testing bitstream */
15 #define MARKER(pack) schro_pack_encode_uint (pack, 1234567)
20 void schro_encoder_render_picture (SchroEncoderFrame
* frame
);
22 schro_encoder_encode_picture_prediction_parameters (SchroEncoderFrame
* frame
);
23 static void schro_encoder_encode_superblock_split (SchroEncoderFrame
* frame
);
24 static void schro_encoder_encode_prediction_modes (SchroEncoderFrame
* frame
);
25 static void schro_encoder_encode_vector_data (SchroEncoderFrame
* frame
,
27 static void schro_encoder_encode_dc_data (SchroEncoderFrame
* frame
, int comp
);
28 static void schro_encoder_encode_transform_parameters (SchroEncoderFrame
*
30 static void schro_encoder_encode_transform_data (SchroEncoderFrame
* frame
);
31 static int schro_encoder_pull_is_ready_locked (SchroEncoder
* encoder
);
32 static void schro_encoder_encode_codec_comment (SchroEncoder
* encoder
);
33 static void schro_encoder_encode_bitrate_comment (SchroEncoder
* encoder
,
34 unsigned int bitrate
);
35 static int schro_encoder_encode_padding (SchroEncoder
* encoder
, int n
);
36 static void schro_encoder_clean_up_transform_subband (SchroEncoderFrame
* frame
,
37 int component
, int index
);
38 static void schro_encoder_fixup_offsets (SchroEncoder
* encoder
,
39 SchroBuffer
* buffer
, schro_bool is_eos
);
40 static void schro_encoder_frame_complete (SchroAsyncStage
* stage
);
41 static int schro_encoder_async_schedule (SchroEncoder
* encoder
,
42 SchroExecDomain exec_domain
);
43 static void schro_encoder_init_perceptual_weighting (SchroEncoder
* encoder
);
44 void schro_encoder_encode_sequence_header_header (SchroEncoder
* encoder
,
46 static schro_bool
schro_frame_data_is_zero (SchroFrameData
* fd
);
47 static void schro_encoder_setting_set_defaults (SchroEncoder
* encoder
);
50 * Set a frame lambda based on the encoder lambda
53 schro_encoder_set_frame_lambda (SchroEncoderFrame
* frame
)
55 SchroEncoder
*encoder
= frame
->encoder
;
58 SCHRO_ASSERT (frame
->encoder
);
59 switch (frame
->encoder
->rate_control
) {
60 case SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
:
61 if (frame
->encoder
->enable_rdo_cbr
) {
64 //frame->frame_lambda = pow( 10.0 , -(12.0-frame->encoder->qf )/2.5 )/16.0;
65 frame
->frame_lambda
= exp (0.921034 * encoder
->qf
- 13.825);
67 q
= (log (frame
->frame_lambda
) + 16.2826) / 1.6447;
69 frame
->frame_me_lambda
= frame
->encoder
->magic_me_lambda_scale
*
70 sqrt (frame
->frame_lambda
);
72 frame
->frame_me_lambda
= MIN (0.002 * exp (q
* 0.2 * M_LN10
), 1.0);
73 if (frame
->frame_me_lambda
> 1.0) {
74 frame
->frame_me_lambda
= 1.0;
76 frame
->frame_me_lambda
*= encoder
->magic_me_lambda_scale
;
78 /* overwritten in schro_encoder_choose_quantisers_rdo_bit_allocation */
79 frame
->frame_lambda
= 0;
80 frame
->frame_me_lambda
= 0.1; /* used to be magic_mc_lambda */
83 case SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY
:
85 double q
= encoder
->quality
;
87 q
+= -3.5 * (frame
->encoder
->magic_error_power
- 4);
88 q
*= 1.0 + (frame
->encoder
->magic_error_power
- 4) * 0.2;
89 if (frame
->encoder
->magic_error_power
< 2.5) {
92 //frame->frame_lambda = exp(((q-5)/0.7 - 7.0)*M_LN10*0.5);
93 frame
->frame_lambda
= exp (1.6447 * q
- 16.2826);
95 frame
->frame_me_lambda
= MIN (0.002 * exp (q
* 0.2 * M_LN10
), 1.0);
96 if (frame
->frame_me_lambda
> 1.0) {
97 frame
->frame_me_lambda
= 1.0;
99 frame
->frame_me_lambda
*= encoder
->magic_me_lambda_scale
;
103 case SCHRO_ENCODER_RATE_CONTROL_LOSSLESS
:
104 frame
->frame_me_lambda
= 10;
107 /* others don't use lambda */
108 frame
->frame_lambda
= 1.0;
109 frame
->frame_me_lambda
= 0.1;
112 if ((frame
->num_refs
!= 0)) {
113 if (schro_encoder_frame_is_B_frame (frame
)) {
114 frame
->frame_lambda
*= frame
->encoder
->magic_B_lambda_scale
;
116 frame
->frame_lambda
*= frame
->encoder
->magic_P_lambda_scale
;
119 if (frame
->encoder
->rate_control
==
120 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
) {
121 if (frame
->encoder
->intra_cbr_lambda
!= -1) {
122 frame
->frame_lambda
= sqrt (frame
->frame_lambda
*
123 frame
->encoder
->intra_cbr_lambda
);
125 frame
->encoder
->intra_cbr_lambda
= frame
->frame_lambda
;
126 SCHRO_DEBUG ("Using filtered CBR value for intra lambda %g (picture %d)",
127 frame
->frame_lambda
, frame
->frame_number
);
129 frame
->frame_lambda
*= frame
->encoder
->magic_I_lambda_scale
;
135 * Return 1 if the last subgroup has been coded, 0 otherwise
140 schro_encoder_last_subgroup_coded (SchroEncoder
* encoder
, int picnum
)
142 int subgroup_coded
= 1;
146 (picnum
+ encoder
->subgroup_length
- 1) / encoder
->subgroup_length
;
147 last_P_frame
*= encoder
->subgroup_length
;
148 last_P_frame
-= encoder
->subgroup_length
;
150 if (picnum
> encoder
->subgroup_length
) {
151 for (pos
= 0; pos
< encoder
->frame_queue
->n
; ++pos
) {
152 SchroEncoderFrame
*frame
= encoder
->frame_queue
->elements
[pos
].data
;
153 fnum
= frame
->frame_number
;
154 if (fnum
<= last_P_frame
155 && fnum
>= last_P_frame
- encoder
->subgroup_length
+ 1) {
156 if (!(frame
->state
& SCHRO_ENCODER_FRAME_STATE_ENCODING
))
161 for (pos
= 0; pos
< encoder
->frame_queue
->n
; ++pos
) {
162 SchroEncoderFrame
*frame
= encoder
->frame_queue
->elements
[pos
].data
;
163 fnum
= frame
->frame_number
;
164 if (fnum
== 0 && !(frame
->state
& SCHRO_ENCODER_FRAME_STATE_ENCODING
))
169 SCHRO_DEBUG ("Last subgroup coded value %d at %d", subgroup_coded
, picnum
);
171 return subgroup_coded
;
177 * schro_encoder_init_rc_buffer
179 * Initialises the buffer model for rate control
183 schro_encoder_init_rc_buffer (SchroEncoder
* encoder
)
187 SCHRO_ASSERT (encoder
);
189 gop_length
= encoder
->au_distance
;
190 if (encoder
->buffer_size
== 0) {
191 encoder
->buffer_size
= 3 * encoder
->bitrate
;
193 // Set initial level at 100%
194 if (encoder
->buffer_level
== 0) {
195 encoder
->buffer_level
= encoder
->buffer_size
;
197 encoder
->bits_per_picture
= muldiv64 (encoder
->bitrate
,
198 encoder
->video_format
.frame_rate_denominator
,
199 encoder
->video_format
.frame_rate_numerator
);
200 encoder
->gop_target
= muldiv64 (encoder
->bitrate
* gop_length
,
201 encoder
->video_format
.frame_rate_denominator
,
202 encoder
->video_format
.frame_rate_numerator
);
204 if (encoder
->video_format
.interlaced_coding
) {
205 encoder
->bits_per_picture
/= 2;
208 encoder
->B_complexity_sum
= 0;
210 // Set up an initial allocation
211 if (encoder
->gop_structure
== SCHRO_ENCODER_GOP_INTRA_ONLY
) {
212 encoder
->I_frame_alloc
= encoder
->bits_per_picture
;
213 encoder
->P_frame_alloc
= 0;
214 encoder
->B_frame_alloc
= 0;
217 encoder
->au_distance
/ encoder
->magic_subgroup_length
- 1;
218 int num_B_frames
= gop_length
- num_P_frames
- 1;
220 encoder
->I_frame_alloc
= 2 ^ 24;
221 encoder
->P_frame_alloc
= encoder
->I_frame_alloc
/ 3;
222 encoder
->B_frame_alloc
= encoder
->P_frame_alloc
/ 3;
223 total
= encoder
->I_frame_alloc
+ num_P_frames
* encoder
->P_frame_alloc
+
224 num_B_frames
* encoder
->B_frame_alloc
;
225 encoder
->I_frame_alloc
=
226 (encoder
->I_frame_alloc
* encoder
->gop_target
) / total
;
227 encoder
->P_frame_alloc
=
228 (encoder
->P_frame_alloc
* encoder
->gop_target
) / total
;
229 encoder
->B_frame_alloc
=
230 (encoder
->B_frame_alloc
* encoder
->gop_target
) / total
;
232 encoder
->I_complexity
= encoder
->I_frame_alloc
;
233 encoder
->P_complexity
= encoder
->P_frame_alloc
;
234 encoder
->B_complexity
= encoder
->B_frame_alloc
;
236 SCHRO_DEBUG ("Initialising buffer with allocations (I, B, P) %d, %d, %d",
237 encoder
->I_frame_alloc
, encoder
->P_frame_alloc
, encoder
->B_frame_alloc
);
239 encoder
->subgroup_position
= 1;
243 * schro_encoder_projected_subgroup_bits
245 * Returns the total number of bits expected for the next subgroup
249 schro_encoder_projected_subgroup_bits (SchroEncoder
* encoder
)
251 // FIXME: take account of subgroups with an I instead of a P??
252 int bits
= encoder
->P_complexity
+
253 (encoder
->magic_subgroup_length
- 1) * encoder
->B_complexity
;
258 * schro_encoder_target_subgroup_bits
260 * Returns the target for the next subgroup
264 schro_encoder_target_subgroup_bits (SchroEncoder
* encoder
)
266 // FIXME: take account of subgroups with an I instead of a P??
267 int bits
= encoder
->P_frame_alloc
+
268 (encoder
->magic_subgroup_length
- 1) * encoder
->B_frame_alloc
;
273 * schro_encoder_cbr_allocate:
275 * TM5-style bit allocation routine
279 schro_encoder_cbr_allocate (SchroEncoder
* encoder
, int fnum
)
293 SCHRO_ASSERT (encoder
);
295 gop_length
= encoder
->au_distance
;
296 Icty
= encoder
->I_complexity
;
297 Pcty
= encoder
->P_complexity
;
298 Bcty
= encoder
->B_complexity
;
300 num_P_frames
= encoder
->au_distance
/ encoder
->magic_subgroup_length
- 1;
301 num_B_frames
= gop_length
- num_I_frames
- num_P_frames
;
302 total_gop_bits
= muldiv64 (encoder
->bitrate
* gop_length
,
303 encoder
->video_format
.frame_rate_denominator
,
304 encoder
->video_format
.frame_rate_numerator
);
305 sg_len
= encoder
->magic_subgroup_length
;
307 ((double) encoder
->buffer_level
) / ((double) encoder
->buffer_size
);
309 if (encoder
->gop_structure
!= SCHRO_ENCODER_GOP_INTRA_ONLY
) {
311 if (buffer_occ
< 0.9 && ((fnum
+ 1) % 4 * sg_len
) == 0) {
312 // If we're undershooting buffer target, correct slowly
313 correction
= MIN (0.25, 0.25 * (0.9 - buffer_occ
) / 0.9);
314 encoder
->gop_target
=
315 (long int) ((double) (total_gop_bits
) * (1.0 - correction
));
316 } else if (buffer_occ
> 0.9 && ((fnum
+ 1) % sg_len
) == 0) {
317 // If we're overshooting buffer target, correct quickly
318 correction
= MIN (0.5, 0.5 * (buffer_occ
- 0.9) / 0.9);
319 encoder
->gop_target
=
320 (long int) ((double) (total_gop_bits
) * (1.0 + correction
));
324 min_bits
= total_gop_bits
/ (100 * gop_length
);
326 encoder
->I_frame_alloc
= (long int) (encoder
->gop_target
328 + (double) (num_P_frames
* Pcty
) / Icty
329 + (double) (num_B_frames
* Bcty
) / Icty
));
331 encoder
->I_frame_alloc
= MAX (min_bits
, encoder
->I_frame_alloc
);
333 encoder
->P_frame_alloc
= (long int) (encoder
->gop_target
335 + (double) (num_I_frames
* Icty
) / Pcty
336 + (double) (num_B_frames
* Bcty
) / Pcty
));
338 encoder
->P_frame_alloc
= MAX (min_bits
, encoder
->P_frame_alloc
);
340 encoder
->B_frame_alloc
= (long int) (encoder
->gop_target
342 + (double) (num_I_frames
* Icty
) / Bcty
343 + (double) (num_P_frames
* Pcty
) / Bcty
));
345 encoder
->B_frame_alloc
= MAX (min_bits
, encoder
->B_frame_alloc
);
350 * schro_encoder_cbr_update
352 * Sets the qf (and hence lambdas) for the next subgroup to be coded
356 schro_encoder_cbr_update (SchroEncoderFrame
* frame
, int num_bits
)
358 SchroEncoder
*encoder
= frame
->encoder
;
364 int emergency_realloc
;
368 // The target buffer occupancy
370 actual_ratio
= (double) (encoder
->buffer_level
) /
371 (double) (encoder
->buffer_size
);
372 P_separation
= encoder
->magic_subgroup_length
;
374 // 1 is coding frames, 2 if coding fields
376 if (encoder
->video_format
.interlaced_coding
)
379 emergency_realloc
= 0;
381 // Decrement the subgroup frame counter. This is zero just after the last
382 // B frame before the next P frame i.e. before the start of a subgroup
383 encoder
->subgroup_position
--;
385 /* Determine the filter tap for adjusting lambda */
386 if ((frame
->frame_number
/ field_factor
) <= 3 * P_separation
) {
387 // Adjust immediately at the beginning of the sequence
390 if (actual_ratio
> target_ratio
)
391 filter_tap
= (actual_ratio
- target_ratio
) / (1.0 - target_ratio
);
393 filter_tap
= (target_ratio
- actual_ratio
) / target_ratio
;
395 filter_tap
= CLAMP (filter_tap
, 0.25, 1.0);
398 // Now for the actual update
399 if (encoder
->gop_structure
!= SCHRO_ENCODER_GOP_INTRA_ONLY
) {
402 if (frame
->num_refs
== 0) {
403 encoder
->I_complexity
= num_bits
;
404 target
= encoder
->I_frame_alloc
;
406 if (num_bits
< target
/ 2 || num_bits
> 3 * target
)
407 emergency_realloc
= 1;
409 if ((frame
->frame_number
/ field_factor
) == 0) { //FIXME: needed?
410 // We've just coded the very first frame, which is a special
411 // case as the B frames which normally follow are missing
412 encoder
->subgroup_position
= P_separation
;
416 if (((frame
->frame_number
) / field_factor
) % P_separation
!= 0) {
417 // Scheduled B picture
418 encoder
->B_complexity_sum
+= num_bits
;
419 target
= encoder
->B_frame_alloc
;
421 if (num_bits
< target
/ 2 || num_bits
> 3 * target
) {
422 emergency_realloc
= 1;
425 } else if (frame
->num_refs
!= 0) {
426 // Scheduled P picture (if inserted I picture, don't change the complexity)
427 encoder
->P_complexity
= num_bits
;
428 target
= encoder
->P_frame_alloc
;
430 if (num_bits
< target
/ 2 || num_bits
> 3 * target
) {
431 emergency_realloc
= 1;
436 if (encoder
->subgroup_position
== 0 || emergency_realloc
== 1) {
440 if (emergency_realloc
== 1)
441 SCHRO_DEBUG ("Major undershoot of frame bit rate: Reallocating");
443 // We recompute allocations for the next subgroup
444 if (P_separation
> 1 && encoder
->subgroup_position
< P_separation
- 1) {
445 encoder
->B_complexity
=
446 encoder
->B_complexity_sum
/ (P_separation
- 1 -
447 encoder
->subgroup_position
);
449 schro_encoder_cbr_allocate (encoder
, frame
->frame_number
/ field_factor
);
451 // We work out what this means for the quality factor and set it
453 tbits
= (double) (schro_encoder_target_subgroup_bits (encoder
));
454 pbits
= (double) (schro_encoder_projected_subgroup_bits (encoder
));
456 SCHRO_DEBUG ("Reallocating: target bits = %g, projected bits = %g", tbits
,
459 // Determine K value in model
460 K
= pow (pbits
, 2) * pow (10.0,
461 ((double) 2 / 5 * (12 - encoder
->qf
))) / 16;
463 // Determine a new qf from K
464 new_qf
= 12 - (double) 5 / 2 * log10 (16 * K
/ pow (tbits
, 2));
466 if ((abs (encoder
->qf
- new_qf
) >= 0.25 || new_qf
<= 4.0)
468 new_qf
= filter_tap
* new_qf
+ (1.0 - filter_tap
) * encoder
->qf
;
471 if (pbits
< 2 * tbits
) {
472 new_qf
= MAX (new_qf
, encoder
->qf
- 1.0);
474 new_qf
= MAX (new_qf
, encoder
->qf
- 2.0);
479 5 + 10 * ((double) encoder
->buffer_level
) / encoder
->buffer_size
);
481 encoder
->qf
= new_qf
;
482 SCHRO_DEBUG ("Setting qf for next subgroup to %g, bits %d", encoder
->qf
,
483 encoder
->buffer_level
);
485 // Reset the frame counter
486 if (encoder
->subgroup_position
== 0) {
487 encoder
->subgroup_position
= encoder
->magic_subgroup_length
;
488 encoder
->B_complexity_sum
= 0;
493 // We're doing intraonly coding
495 double tbits
= (double) encoder
->bits_per_picture
;
496 double pbits
= (double) num_bits
;
500 pow (pbits
, 2) * pow (10.0, ((double) 2 / 5 * (12 - encoder
->qf
))) / 16;
502 // Determine a new QF
503 double new_qf
= 12 - (double) 5 / 2 * log10 (16 * K
/ pow (tbits
, 2));
505 // Adjust the QF to meet the target
506 double abs_delta
= abs (new_qf
- encoder
->qf
);
507 if (abs_delta
> 0.01) {
508 // Rate of convergence to new QF
511 // Use an Sshaped curve to compute r
512 // Where the qf difference is less than 1/2, r decreases to zero
513 // exponentially, so for small differences in QF we jump straight
514 // to the target value. For large differences in QF, r converges
515 // exponentially to 0.75, so we converge to the target value at
518 // Overall behaviour is to converge steadily for 2 or 3 frames until
519 // close and then lock to the correct value. This avoids very rapid
520 // changes in quality.
522 // Actual parameters may be adjusted later. Some applications may
523 // require instant lock.
525 double lg_diff
= log (abs_delta
/ 2.0);
527 r
= 0.5 * exp (-lg_diff
* lg_diff
/ 2.0);
529 r
= 1.0 - 0.5 * exp (-lg_diff
* lg_diff
/ 2.0);
532 encoder
->qf
= r
* encoder
->qf
+ (1.0 - r
) * new_qf
;
533 SCHRO_DEBUG ("Setting qf for next subgroup to %g", encoder
->qf
);
542 * Create a new encoder object.
544 * Returns: a new encoder object
547 schro_encoder_new (void)
549 SchroEncoder
*encoder
;
552 encoder
= schro_malloc0 (sizeof (SchroEncoder
));
554 encoder
->version_major
= 2;
555 encoder
->version_minor
= 2;
557 encoder
->au_frame
= -1;
559 encoder
->last_ref
= -1;
562 schro_encoder_setting_set_defaults (encoder
);
564 schro_video_format_set_std_video_format (&encoder
->video_format
,
565 SCHRO_VIDEO_FORMAT_CUSTOM
);
567 encoder
->inserted_buffers
=
568 schro_list_new_full ((SchroListFreeFunc
) schro_buffer_unref
, NULL
);
570 for (c
= 0; c
< 3; ++c
) {
571 for (b
= 0; b
< SCHRO_LIMIT_SUBBANDS
; ++b
) {
572 encoder
->average_arith_context_ratios_intra
[c
][b
] = 1.0;
573 encoder
->average_arith_context_ratios_inter
[c
][b
] = 1.0;
581 handle_gop_enum (SchroEncoder
* encoder
)
583 switch (encoder
->gop_structure
) {
584 case SCHRO_ENCODER_GOP_BACKREF
:
585 case SCHRO_ENCODER_GOP_CHAINED_BACKREF
:
586 SCHRO_DEBUG ("Setting backref\n");
587 encoder
->init_frame
= schro_encoder_init_frame
;
588 encoder
->handle_gop
= schro_encoder_handle_gop_backref
;
589 encoder
->handle_quants
= schro_encoder_handle_quants
;
590 encoder
->setup_frame
= schro_encoder_setup_frame_backref
;
592 case SCHRO_ENCODER_GOP_INTRA_ONLY
:
593 SCHRO_DEBUG ("Setting intra only\n");
594 encoder
->init_frame
= schro_encoder_init_frame
;
595 encoder
->handle_gop
= schro_encoder_handle_gop_intra_only
;
596 encoder
->handle_quants
= schro_encoder_handle_quants
;
597 encoder
->setup_frame
= schro_encoder_setup_frame_intra_only
;
599 case SCHRO_ENCODER_GOP_ADAPTIVE
:
600 case SCHRO_ENCODER_GOP_BIREF
:
601 case SCHRO_ENCODER_GOP_CHAINED_BIREF
:
602 SCHRO_DEBUG ("Setting tworef engine\n");
603 encoder
->init_frame
= schro_encoder_init_frame
;
604 encoder
->handle_gop
= schro_encoder_handle_gop_tworef
;
605 encoder
->handle_quants
= schro_encoder_handle_quants
;
606 encoder
->setup_frame
= schro_encoder_setup_frame_tworef
;
615 * schro_encoder_start:
616 * @encoder: an encoder object
618 * Locks in encoder configuration and causes the encoder to start
619 * encoding pictures. At this point, the encoder will start worker
620 * threads to do the actual encoding.
623 schro_encoder_start (SchroEncoder
* encoder
)
625 encoder
->frame_queue
= schro_queue_new (encoder
->queue_depth
,
626 (SchroQueueFreeFunc
) schro_encoder_frame_unref
);
627 SCHRO_DEBUG ("queue depth %d", encoder
->queue_depth
);
629 encoder
->engine_init
= 1;
630 encoder
->force_sequence_header
= TRUE
;
632 /* add check on 'enable' switches */
633 if (encoder
->enable_scene_change_detection
) {
634 encoder
->magic_scene_change_threshold
= 3.25;
636 encoder
->magic_scene_change_threshold
= 0.2;
638 if (encoder
->enable_bigblock_estimation
&& encoder
->enable_deep_estimation
) {
639 /* only one can be set at any one time */
640 encoder
->enable_bigblock_estimation
= FALSE
;
641 } else if (!encoder
->enable_bigblock_estimation
642 && !encoder
->enable_deep_estimation
) {
643 SCHRO_ERROR ("no motion estimation selected!");
646 if (!encoder
->enable_deep_estimation
&& encoder
->enable_chroma_me
) {
647 encoder
->enable_chroma_me
= FALSE
;
649 /* Global motion is broken */
650 encoder
->enable_global_motion
= FALSE
;
652 encoder
->bit_depth
= schro_video_format_get_bit_depth (&encoder
->video_format
);
653 if (encoder
->video_format
.luma_excursion
>= 256 ||
654 encoder
->video_format
.chroma_excursion
>= 256) {
655 encoder
->input_frame_depth
= 16;
656 encoder
->intermediate_frame_depth
= 32;
658 encoder
->input_frame_depth
= 8;
659 //encoder->intermediate_frame_depth = 32;
660 encoder
->intermediate_frame_depth
= 16;
663 encoder
->video_format
.interlaced_coding
= encoder
->interlaced_coding
;
665 schro_encoder_encode_codec_comment (encoder
);
667 schro_tables_init ();
668 schro_encoder_init_perceptual_weighting (encoder
);
669 /* special value indicating invalid */
670 encoder
->intra_cbr_lambda
= -1;
672 schro_encoder_init_error_tables (encoder
);
674 encoder
->async
= schro_async_new (0,
675 (SchroAsyncScheduleFunc
) schro_encoder_async_schedule
,
676 (SchroAsyncCompleteFunc
) schro_encoder_frame_complete
, encoder
);
678 if (encoder
->force_profile
== SCHRO_ENCODER_PROFILE_AUTO
) {
679 if (encoder
->rate_control
== SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY
) {
680 encoder
->force_profile
= SCHRO_ENCODER_PROFILE_VC2_LOW_DELAY
;
681 } else if (encoder
->enable_noarith
) {
682 encoder
->force_profile
= SCHRO_ENCODER_PROFILE_VC2_SIMPLE
;
683 } else if (encoder
->gop_structure
== SCHRO_ENCODER_GOP_INTRA_ONLY
) {
684 encoder
->force_profile
= SCHRO_ENCODER_PROFILE_VC2_MAIN
;
686 encoder
->force_profile
= SCHRO_ENCODER_PROFILE_MAIN
;
690 if (encoder
->bitrate
== 0) {
691 encoder
->bitrate
= encoder
->video_format
.width
*
692 encoder
->video_format
.height
*
693 encoder
->video_format
.frame_rate_numerator
/
694 encoder
->video_format
.frame_rate_denominator
;
695 if (encoder
->force_profile
== SCHRO_ENCODER_PROFILE_MAIN
) {
696 encoder
->bitrate
/= 10;
698 encoder
->bitrate
*= 2;
702 switch (encoder
->force_profile
) {
703 case SCHRO_ENCODER_PROFILE_VC2_LOW_DELAY
:
704 encoder
->profile
= SCHRO_PROFILE_LOW_DELAY
;
705 encoder
->rate_control
= SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY
;
706 encoder
->gop_structure
= SCHRO_ENCODER_GOP_INTRA_ONLY
;
707 encoder
->au_distance
= 1;
709 case SCHRO_ENCODER_PROFILE_VC2_SIMPLE
:
710 encoder
->profile
= SCHRO_PROFILE_SIMPLE
;
711 encoder
->enable_noarith
= TRUE
;
712 encoder
->gop_structure
= SCHRO_ENCODER_GOP_INTRA_ONLY
;
713 encoder
->au_distance
= 1;
715 case SCHRO_ENCODER_PROFILE_VC2_MAIN
:
716 encoder
->profile
= SCHRO_PROFILE_MAIN_INTRA
;
717 encoder
->enable_noarith
= FALSE
;
718 encoder
->gop_structure
= SCHRO_ENCODER_GOP_INTRA_ONLY
;
719 encoder
->au_distance
= 1;
721 case SCHRO_ENCODER_PROFILE_MAIN
:
722 encoder
->profile
= SCHRO_PROFILE_MAIN
;
723 encoder
->enable_noarith
= FALSE
;
725 case SCHRO_ENCODER_PROFILE_AUTO
:
730 switch (encoder
->rate_control
) {
731 case SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD
:
732 handle_gop_enum (encoder
);
733 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_SIMPLE
;
735 case SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
:
736 handle_gop_enum (encoder
);
737 if (encoder
->enable_rdo_cbr
) {
738 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_CBR
;
740 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_RDO_BIT_ALLOCATION
;
742 schro_encoder_init_rc_buffer (encoder
);
744 schro_encoder_encode_bitrate_comment (encoder
, encoder
->bitrate
);
746 case SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY
:
747 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_LOWDELAY
;
749 encoder
->init_frame
= schro_encoder_init_frame
;
750 encoder
->handle_gop
= schro_encoder_handle_gop_lowdelay
;
751 encoder
->handle_quants
= schro_encoder_handle_quants
;
752 encoder
->setup_frame
= schro_encoder_setup_frame_lowdelay
;
754 schro_encoder_encode_bitrate_comment (encoder
, encoder
->bitrate
);
756 case SCHRO_ENCODER_RATE_CONTROL_LOSSLESS
:
757 if (encoder
->force_profile
== SCHRO_ENCODER_PROFILE_MAIN
) {
758 encoder
->handle_gop
= schro_encoder_handle_gop_lossless
;
760 encoder
->handle_gop
= schro_encoder_handle_gop_intra_only
;
762 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_LOSSLESS
;
763 encoder
->init_frame
= schro_encoder_init_frame
;
764 encoder
->handle_quants
= schro_encoder_handle_quants
;
765 encoder
->setup_frame
= schro_encoder_setup_frame_tworef
;
767 case SCHRO_ENCODER_RATE_CONTROL_CONSTANT_LAMBDA
:
768 handle_gop_enum (encoder
);
769 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_RDO_LAMBDA
;
771 case SCHRO_ENCODER_RATE_CONTROL_CONSTANT_ERROR
:
772 handle_gop_enum (encoder
);
773 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_CONSTANT_ERROR
;
775 case SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY
:
776 handle_gop_enum (encoder
);
777 encoder
->quantiser_engine
= SCHRO_QUANTISER_ENGINE_RDO_LAMBDA
;
784 encoder
->video_format
.index
=
785 schro_video_format_get_std_video_format (&encoder
->video_format
);
786 switch (encoder
->profile
) {
787 case SCHRO_PROFILE_LOW_DELAY
:
788 case SCHRO_PROFILE_SIMPLE
:
789 case SCHRO_PROFILE_MAIN_INTRA
:
790 if (schro_video_format_check_VC2_DL (&encoder
->video_format
)) {
794 case SCHRO_PROFILE_MAIN
:
795 if (schro_video_format_check_MP_DL (&encoder
->video_format
)) {
796 encoder
->level
= 128;
804 /* 1, 3, 4, 6 have overflows at transform-depth>=5
805 * 5 has errors at transform-depth=any */
806 const int max_transform_depths
[] = { 5, 4, 5, 4, 4, 3, 4 };
808 encoder
->transform_depth
= MIN (encoder
->transform_depth
,
809 max_transform_depths
[encoder
->intra_wavelet
]);
810 if (encoder
->gop_structure
!= SCHRO_ENCODER_GOP_INTRA_ONLY
) {
811 encoder
->transform_depth
= MIN (encoder
->transform_depth
,
812 max_transform_depths
[encoder
->inter_wavelet
]);
816 encoder
->start_time
= schro_utils_get_time ();
820 * schro_encoder_free:
821 * @encoder: an encoder object
823 * Frees an encoder object and all its resources.
826 schro_encoder_free (SchroEncoder
* encoder
)
830 if (encoder
->async
) {
831 schro_async_free (encoder
->async
);
834 if (encoder
->last_frame
) {
835 schro_encoder_frame_unref (encoder
->last_frame
);
836 encoder
->last_frame
= NULL
;
839 for (i
= 0; i
< SCHRO_LIMIT_REFERENCE_FRAMES
; i
++) {
840 if (encoder
->reference_pictures
[i
]) {
841 schro_encoder_frame_unref (encoder
->reference_pictures
[i
]);
842 encoder
->reference_pictures
[i
] = NULL
;
845 if (encoder
->frame_queue
) {
846 schro_queue_free (encoder
->frame_queue
);
849 if (encoder
->inserted_buffers
) {
850 schro_list_free (encoder
->inserted_buffers
);
853 schro_free (encoder
);
857 schro_encoder_init_perceptual_weighting (SchroEncoder
* encoder
)
859 encoder
->cycles_per_degree_vert
= 0.5 * encoder
->video_format
.height
/
860 (2.0 * atan (0.5 / encoder
->perceptual_distance
) * 180 / M_PI
);
861 encoder
->cycles_per_degree_horiz
= encoder
->cycles_per_degree_vert
*
862 encoder
->video_format
.aspect_ratio_denominator
/
863 encoder
->video_format
.aspect_ratio_numerator
;
865 if (encoder
->video_format
.interlaced_coding
) {
866 encoder
->cycles_per_degree_vert
*= 0.5;
869 SCHRO_DEBUG ("cycles per degree horiz=%g vert=%g",
870 encoder
->cycles_per_degree_horiz
, encoder
->cycles_per_degree_vert
);
872 switch (encoder
->perceptual_weighting
) {
874 case SCHRO_ENCODER_PERCEPTUAL_CONSTANT
:
875 schro_encoder_calculate_subband_weights (encoder
,
876 schro_encoder_perceptual_weight_constant
);
878 case SCHRO_ENCODER_PERCEPTUAL_CCIR959
:
879 schro_encoder_calculate_subband_weights (encoder
,
880 schro_encoder_perceptual_weight_ccir959
);
882 case SCHRO_ENCODER_PERCEPTUAL_MOO
:
883 schro_encoder_calculate_subband_weights (encoder
,
884 schro_encoder_perceptual_weight_moo
);
886 case SCHRO_ENCODER_PERCEPTUAL_MANOS_SAKRISON
:
887 schro_encoder_calculate_subband_weights (encoder
,
888 schro_encoder_perceptual_weight_manos_sakrison
);
894 * schro_encoder_get_video_format:
895 * @encoder: an encoder object
897 * Creates a new SchroVideoFormat structure and copies the
898 * video format information of @decoder into it.
900 * When no longer needed, the returned pointer should be
901 * freed using free().
903 * Returns: a pointer to a SchroVideoFormat structure
906 schro_encoder_get_video_format (SchroEncoder
* encoder
)
908 SchroVideoFormat
*format
;
910 format
= malloc (sizeof (SchroVideoFormat
));
911 memcpy (format
, &encoder
->video_format
, sizeof (SchroVideoFormat
));
917 * schro_encoder_set_video_format:
918 * @encoder: an encoder object
919 * @format: the video format to use
921 * Sets the video format used by @encoder to the values specified
922 * in @format. This function may only be called before schro_encoder_start()
923 * is called on the encoder.
926 schro_encoder_set_video_format (SchroEncoder
* encoder
,
927 SchroVideoFormat
* format
)
929 /* FIXME check that we're in the right state to do this */
931 memcpy (&encoder
->video_format
, format
, sizeof (SchroVideoFormat
));
933 schro_video_format_validate (&encoder
->video_format
);
937 * schro_encoder_set_packet_assembly:
938 * @encoder: an encoder object
941 * If @value is TRUE, all subsequent calls to schro_encoder_pull()
942 * will return a buffer that contains all Dirac packets related to
943 * a frame. If @value is FALSE, each buffer will be one Dirac packet.
945 * It is recommended that users always call this function with TRUE
946 * immediately after creating an encoder object.
949 schro_encoder_set_packet_assembly (SchroEncoder
* encoder
, int value
)
951 encoder
->assemble_packets
= value
;
955 schro_encoder_push_is_ready_locked (SchroEncoder
* encoder
)
959 if (encoder
->end_of_stream
) {
963 n
= schro_queue_slots_available (encoder
->frame_queue
);
965 if (encoder
->video_format
.interlaced_coding
) {
973 * schro_encoder_push_ready:
974 * @encoder: an encoder object
976 * Returns true if the encoder has available space for additional
979 * Returns: TRUE if the encoder is ready for another video frame to
983 schro_encoder_push_ready (SchroEncoder
* encoder
)
987 schro_async_lock (encoder
->async
);
988 ret
= schro_encoder_push_is_ready_locked (encoder
);
989 schro_async_unlock (encoder
->async
);
995 * schro_encoder_force_sequence_header:
996 * @encoder: an encoder object
998 * Indicates to the encoder that the next frame pushed should be
999 * encoded with a sequence header.
1002 schro_encoder_force_sequence_header (SchroEncoder
* encoder
)
1004 encoder
->force_sequence_header
= TRUE
;
1008 * schro_encoder_push_frame:
1009 * @encoder: an encoder object
1010 * @frame: a frame to encode
1012 * Provides a frame to the encoder to encode.
1015 schro_encoder_push_frame (SchroEncoder
* encoder
, SchroFrame
* frame
)
1017 schro_encoder_push_frame_full (encoder
, frame
, NULL
);
1021 * schro_encoder_push_frame_full:
1022 * @encoder: an encoder object
1023 * @frame: a frame to encode
1024 * @priv: a private tag
1026 * Provides a frame to the encoder to encode. The value of @priv is
1027 * returned when schro_encoder_pull_full() is called for the encoded
1031 schro_encoder_push_frame_full (SchroEncoder
* encoder
, SchroFrame
* frame
,
1034 schro_async_lock (encoder
->async
);
1035 if (encoder
->video_format
.interlaced_coding
== 0) {
1036 SchroEncoderFrame
*encoder_frame
;
1037 SchroFrameFormat format
;
1039 encoder_frame
= schro_encoder_frame_new (encoder
);
1040 encoder_frame
->encoder
= encoder
;
1042 encoder_frame
->priv
= priv
;
1044 encoder_frame
->previous_frame
= encoder
->last_frame
;
1045 schro_encoder_frame_ref (encoder_frame
);
1046 encoder
->last_frame
= encoder_frame
;
1049 schro_params_get_frame_format (encoder
->input_frame_depth
, encoder
->video_format
.chroma_format
);
1050 if (format
== frame
->format
) {
1051 encoder_frame
->original_frame
= frame
;
1053 encoder_frame
->original_frame
= schro_frame_new_and_alloc (NULL
, format
,
1054 encoder
->video_format
.width
, encoder
->video_format
.height
);
1055 schro_frame_convert (encoder_frame
->original_frame
, frame
);
1056 schro_frame_unref (frame
);
1060 encoder_frame
->frame_number
= encoder
->next_frame_number
++;
1062 if (schro_queue_is_full (encoder
->frame_queue
)) {
1063 SCHRO_ERROR ("push when queue full");
1066 schro_queue_add (encoder
->frame_queue
, encoder_frame
,
1067 encoder_frame
->frame_number
);
1068 schro_async_signal_scheduler (encoder
->async
);
1069 schro_async_unlock (encoder
->async
);
1071 SchroEncoderFrame
*encoder_frame1
;
1072 SchroEncoderFrame
*encoder_frame2
;
1073 SchroFrameFormat format
;
1076 encoder_frame1
= schro_encoder_frame_new (encoder
);
1077 encoder_frame1
->encoder
= encoder
;
1078 encoder_frame1
->priv
= priv
;
1079 encoder_frame2
= schro_encoder_frame_new (encoder
);
1080 encoder_frame2
->encoder
= encoder
;
1082 encoder_frame1
->previous_frame
= encoder
->last_frame
;
1083 schro_encoder_frame_ref (encoder_frame1
);
1084 encoder_frame2
->previous_frame
= encoder_frame1
;
1085 schro_encoder_frame_ref (encoder_frame2
);
1086 encoder
->last_frame
= encoder_frame2
;
1088 schro_video_format_get_picture_luma_size (&encoder
->video_format
,
1090 format
= schro_params_get_frame_format (encoder
->input_frame_depth
,
1091 encoder
->video_format
.chroma_format
);
1093 encoder_frame1
->original_frame
= schro_frame_new_and_alloc (NULL
, format
,
1095 encoder_frame2
->original_frame
= schro_frame_new_and_alloc (NULL
, format
,
1097 schro_frame_split_fields (encoder_frame1
->original_frame
,
1098 encoder_frame2
->original_frame
, frame
);
1099 schro_frame_unref (frame
);
1102 encoder_frame1
->frame_number
= encoder
->next_frame_number
++;
1103 encoder_frame2
->frame_number
= encoder
->next_frame_number
++;
1105 if (schro_queue_slots_available (encoder
->frame_queue
) < 2) {
1106 SCHRO_ERROR ("push when queue full");
1109 schro_queue_add (encoder
->frame_queue
, encoder_frame1
,
1110 encoder_frame1
->frame_number
);
1111 schro_queue_add (encoder
->frame_queue
, encoder_frame2
,
1112 encoder_frame2
->frame_number
);
1113 schro_async_signal_scheduler (encoder
->async
);
1114 schro_async_unlock (encoder
->async
);
1119 schro_encoder_pull_is_ready_locked (SchroEncoder
* encoder
)
1123 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
1124 SchroEncoderFrame
*frame
;
1125 frame
= encoder
->frame_queue
->elements
[i
].data
;
1126 if (frame
->slot
== encoder
->output_slot
&&
1127 (frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_DONE
].is_done
)) {
1132 if (schro_queue_is_empty (encoder
->frame_queue
) && encoder
->end_of_stream
1133 && !encoder
->end_of_stream_pulled
) {
1141 schro_encoder_shift_frame_queue (SchroEncoder
* encoder
)
1143 SchroEncoderFrame
*frame
;
1145 while (!schro_queue_is_empty (encoder
->frame_queue
)) {
1146 frame
= encoder
->frame_queue
->elements
[0].data
;
1147 if (!(frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_FREE
].is_done
)) {
1151 schro_queue_pop (encoder
->frame_queue
);
1156 * schro_encoder_pull:
1157 * @encoder: an encoder object
1158 * @presentation_frame: (output) latest decodable frame
1160 * Pulls a buffer of compressed video from the encoder. If
1161 * @presentation_frame is not NULL, the frame number of the
1162 * latest decodable frame is returned.
1164 * Returns: a buffer containing compressed video
1167 schro_encoder_pull (SchroEncoder
* encoder
, int *presentation_frame
)
1169 return schro_encoder_pull_full (encoder
, presentation_frame
, NULL
);
1173 schro_encoder_frame_get_encoded_size (SchroEncoderFrame
* frame
)
1175 SchroBuffer
*buffer
;
1179 if (frame
->sequence_header_buffer
) {
1180 size
+= frame
->sequence_header_buffer
->length
;
1183 for (i
= 0; i
< schro_list_get_size (frame
->inserted_buffers
); i
++) {
1184 buffer
= schro_list_get (frame
->inserted_buffers
, i
);
1185 size
+= buffer
->length
;
1187 for (i
= 0; i
< schro_list_get_size (frame
->encoder
->inserted_buffers
); i
++) {
1188 buffer
= schro_list_get (frame
->encoder
->inserted_buffers
, i
);
1189 size
+= buffer
->length
;
1192 size
+= frame
->output_buffer
->length
;
1198 schro_encoder_frame_assemble_buffer (SchroEncoderFrame
* frame
,
1199 SchroBuffer
* buffer
)
1205 if (frame
->sequence_header_buffer
) {
1206 buf
= frame
->sequence_header_buffer
;
1207 schro_encoder_fixup_offsets (frame
->encoder
, buf
, FALSE
);
1208 orc_memcpy (buffer
->data
+ offset
, buf
->data
, buf
->length
);
1209 offset
+= frame
->sequence_header_buffer
->length
;
1212 for (i
= 0; i
< schro_list_get_size (frame
->inserted_buffers
); i
++) {
1213 buf
= schro_list_get (frame
->inserted_buffers
, i
);
1214 schro_encoder_fixup_offsets (frame
->encoder
, buf
, FALSE
);
1215 orc_memcpy (buffer
->data
+ offset
, buf
->data
, buf
->length
);
1216 offset
+= buf
->length
;
1218 while (schro_list_get_size (frame
->encoder
->inserted_buffers
) > 0) {
1219 buf
= schro_list_remove (frame
->encoder
->inserted_buffers
, 0);
1220 schro_encoder_fixup_offsets (frame
->encoder
, buf
, FALSE
);
1221 orc_memcpy (buffer
->data
+ offset
, buf
->data
, buf
->length
);
1222 offset
+= buf
->length
;
1223 schro_buffer_unref (buf
);
1227 buf
= frame
->output_buffer
;
1228 schro_encoder_fixup_offsets (frame
->encoder
, buf
, FALSE
);
1229 orc_memcpy (buffer
->data
+ offset
, buf
->data
, buf
->length
);
1230 offset
+= buf
->length
;
1234 schro_encoder_get_frame_stats_size (SchroEncoder
* encoder
)
1240 schro_encoder_get_frame_stats (SchroEncoder
* encoder
, double *dest
, int n
)
1242 memcpy (dest
, encoder
->frame_stats
, sizeof (double) * MIN (n
, 21));
1246 * schro_encoder_pull_full:
1247 * @encoder: an encoder object
1248 * @presentation_frame: (output) latest decodable frame
1251 * Pulls a buffer of compressed video from the encoder. If
1252 * @presentation_frame is not NULL, the frame number of the
1253 * latest decodable frame is returned. If @priv is not NULL,
1254 * the private tag attached to the pushed uncompressed frame
1257 * Returns: a buffer containing compressed video
1260 schro_encoder_pull_full (SchroEncoder
* encoder
, int *presentation_frame
,
1263 SchroBuffer
*buffer
;
1267 SCHRO_DEBUG ("pulling slot %d", encoder
->output_slot
);
1269 schro_async_lock (encoder
->async
);
1270 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
1271 SchroEncoderFrame
*frame
;
1272 frame
= encoder
->frame_queue
->elements
[i
].data
;
1273 if (frame
->slot
== encoder
->output_slot
&&
1274 (frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_DONE
].is_done
)) {
1275 int is_picture
= FALSE
;
1277 if (presentation_frame
) {
1278 *presentation_frame
= frame
->presentation_frame
;
1281 if (encoder
->assemble_packets
) {
1284 size
= schro_encoder_frame_get_encoded_size (frame
);
1285 buffer
= schro_buffer_new_and_alloc (size
);
1286 schro_encoder_frame_assemble_buffer (frame
, buffer
);
1289 *priv
= frame
->priv
;
1293 if (frame
->sequence_header_buffer
) {
1294 buffer
= frame
->sequence_header_buffer
;
1295 frame
->sequence_header_buffer
= NULL
;
1296 } else if (schro_list_get_size (frame
->inserted_buffers
) > 0) {
1297 buffer
= schro_list_remove (frame
->inserted_buffers
, 0);
1298 } else if (schro_list_get_size (encoder
->inserted_buffers
) > 0) {
1299 buffer
= schro_list_remove (encoder
->inserted_buffers
, 0);
1302 *priv
= frame
->priv
;
1304 buffer
= frame
->output_buffer
;
1305 frame
->output_buffer
= NULL
;
1311 double elapsed_time
;
1314 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_FREE
].is_done
= TRUE
;
1315 encoder
->output_slot
++;
1317 elapsed_time
= schro_utils_get_time () - encoder
->start_time
;
1319 if (frame
->num_refs
== 0) {
1320 frame
->badblock_ratio
= 0;
1321 frame
->dcblock_ratio
= 0;
1322 frame
->mc_error
= 0;
1325 schro_dump (SCHRO_DUMP_PICTURE
, "%d %d %d %d %d %g %d %d %d %d %g %d %g %g %g %g %g %g %g %g\n", frame
->frame_number
, /* 0 */
1326 frame
->num_refs
, frame
->is_ref
, frame
->allocated_mc_bits
, frame
->allocated_residual_bits
, frame
->picture_weight
, /* 5 */
1327 frame
->estimated_mc_bits
, frame
->estimated_residual_bits
, frame
->actual_mc_bits
, frame
->actual_residual_bits
, frame
->scene_change_score
, /* 10 */
1328 encoder
->buffer_level
, frame
->frame_lambda
, frame
->mc_error
, frame
->mean_squared_error_luma
, frame
->mean_squared_error_chroma
, /* 15 */
1330 frame
->badblock_ratio
, frame
->dcblock_ratio
, frame
->hist_slope
);
1332 encoder
->frame_stats
[0] = frame
->frame_number
;
1333 encoder
->frame_stats
[1] = frame
->num_refs
;
1334 encoder
->frame_stats
[2] = frame
->is_ref
;
1335 encoder
->frame_stats
[3] = frame
->allocated_mc_bits
;
1336 encoder
->frame_stats
[4] = frame
->allocated_residual_bits
;
1337 encoder
->frame_stats
[5] = frame
->picture_weight
;
1338 encoder
->frame_stats
[6] = frame
->estimated_mc_bits
;
1339 encoder
->frame_stats
[7] = frame
->estimated_residual_bits
;
1340 encoder
->frame_stats
[8] = frame
->actual_mc_bits
;
1341 encoder
->frame_stats
[8] = frame
->actual_residual_bits
;
1342 encoder
->frame_stats
[10] = frame
->scene_change_score
;
1343 encoder
->frame_stats
[11] = encoder
->buffer_level
;
1344 encoder
->frame_stats
[12] = frame
->frame_lambda
;
1345 encoder
->frame_stats
[13] = frame
->mc_error
;
1346 encoder
->frame_stats
[14] = frame
->mean_squared_error_luma
;
1347 encoder
->frame_stats
[15] = frame
->mean_squared_error_chroma
;
1348 encoder
->frame_stats
[16] = elapsed_time
;
1349 encoder
->frame_stats
[17] = frame
->badblock_ratio
;
1350 encoder
->frame_stats
[18] = frame
->dcblock_ratio
;
1351 encoder
->frame_stats
[19] = frame
->hist_slope
;
1352 encoder
->frame_stats
[20] = frame
->mssim
;
1354 schro_encoder_shift_frame_queue (encoder
);
1357 if (encoder
->rate_control
== SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
) {
1358 encoder
->buffer_level
-= buffer
->length
* 8;
1360 if (encoder
->buffer_level
< 0) {
1361 SCHRO_WARNING ("buffer underrun by %d bits",
1362 -encoder
->buffer_level
);
1363 encoder
->buffer_level
= 0;
1365 encoder
->buffer_level
+= encoder
->bits_per_picture
;
1366 if (encoder
->buffer_level
> encoder
->buffer_size
) {
1369 n
= (encoder
->buffer_level
- encoder
->buffer_size
+ 7) / 8;
1370 SCHRO_DEBUG ("buffer overrun, adding padding of %d bytes", n
);
1371 n
= schro_encoder_encode_padding (encoder
, n
);
1372 encoder
->buffer_level
-= n
* 8;
1374 SCHRO_DEBUG ("buffer level %d of %d bits", encoder
->buffer_level
,
1375 encoder
->buffer_size
);
1379 if (!encoder
->assemble_packets
) {
1380 schro_encoder_fixup_offsets (encoder
, buffer
, FALSE
);
1383 SCHRO_DEBUG ("got buffer length=%d", buffer
->length
);
1384 schro_async_unlock (encoder
->async
);
1389 if (schro_queue_is_empty (encoder
->frame_queue
) && encoder
->end_of_stream
) {
1390 buffer
= schro_encoder_encode_end_of_stream (encoder
);
1391 schro_encoder_fixup_offsets (encoder
, buffer
, TRUE
);
1392 encoder
->end_of_stream_pulled
= TRUE
;
1394 schro_async_unlock (encoder
->async
);
1397 schro_async_unlock (encoder
->async
);
1399 SCHRO_DEBUG ("got nothing");
1404 * schro_encoder_end_of_stream:
1405 * @encoder: an encoder object
1407 * Tells the encoder that the end of the stream has been reached, and
1408 * no more frames are available to encode. The encoder will then
1412 schro_encoder_end_of_stream (SchroEncoder
* encoder
)
1414 encoder
->end_of_stream
= TRUE
;
1415 schro_async_lock (encoder
->async
);
1416 if (encoder
->frame_queue
->n
> 0) {
1417 SchroEncoderFrame
*encoder_frame
;
1420 encoder
->frame_queue
->elements
[encoder
->frame_queue
->n
- 1].data
;
1421 encoder_frame
->last_frame
= TRUE
;
1423 schro_async_unlock (encoder
->async
);
1427 schro_encoder_fixup_offsets (SchroEncoder
* encoder
, SchroBuffer
* buffer
,
1430 uint8_t *data
= buffer
->data
;
1431 unsigned int next_offset
;
1433 if (buffer
->length
< 13) {
1434 SCHRO_ERROR ("packet too short (%d < 13)", buffer
->length
);
1440 next_offset
= buffer
->length
;
1443 data
[5] = (next_offset
>> 24) & 0xff;
1444 data
[6] = (next_offset
>> 16) & 0xff;
1445 data
[7] = (next_offset
>> 8) & 0xff;
1446 data
[8] = (next_offset
>> 0) & 0xff;
1447 data
[9] = (encoder
->prev_offset
>> 24) & 0xff;
1448 data
[10] = (encoder
->prev_offset
>> 16) & 0xff;
1449 data
[11] = (encoder
->prev_offset
>> 8) & 0xff;
1450 data
[12] = (encoder
->prev_offset
>> 0) & 0xff;
1452 encoder
->prev_offset
= next_offset
;
1456 schro_encoder_encode_padding (SchroEncoder
* encoder
, int n
)
1458 SchroBuffer
*buffer
;
1461 if (n
< SCHRO_PARSE_HEADER_SIZE
)
1462 n
= SCHRO_PARSE_HEADER_SIZE
;
1464 buffer
= schro_buffer_new_and_alloc (n
);
1466 pack
= schro_pack_new ();
1467 schro_pack_encode_init (pack
, buffer
);
1469 schro_encoder_encode_parse_info (pack
, SCHRO_PARSE_CODE_PADDING
);
1471 schro_pack_append_zero (pack
, n
- SCHRO_PARSE_HEADER_SIZE
);
1473 schro_pack_free (pack
);
1475 schro_encoder_insert_buffer (encoder
, buffer
);
1481 schro_encoder_encode_codec_comment (SchroEncoder
* encoder
)
1483 const char *s
= "Schroedinger " VERSION
;
1484 SchroBuffer
*buffer
;
1486 buffer
= schro_encoder_encode_auxiliary_data (encoder
,
1487 SCHRO_AUX_DATA_ENCODER_STRING
, s
, strlen (s
));
1489 schro_encoder_insert_buffer (encoder
, buffer
);
1493 schro_encoder_encode_bitrate_comment (SchroEncoder
* encoder
,
1494 unsigned int bitrate
)
1497 SchroBuffer
*buffer
;
1499 s
[0] = (bitrate
>> 24) & 0xff;
1500 s
[1] = (bitrate
>> 16) & 0xff;
1501 s
[2] = (bitrate
>> 8) & 0xff;
1502 s
[3] = (bitrate
>> 0) & 0xff;
1503 buffer
= schro_encoder_encode_auxiliary_data (encoder
,
1504 SCHRO_AUX_DATA_BITRATE
, s
, 4);
1506 schro_encoder_insert_buffer (encoder
, buffer
);
1510 schro_encoder_encode_md5_checksum (SchroEncoderFrame
* encoder_frame
)
1512 SchroBuffer
*buffer
;
1513 uint32_t checksum
[4];
1516 frame
= schro_upsampled_frame_get_frame (encoder_frame
->reconstructed_frame
, 0);
1518 schro_frame_md5 (frame
, checksum
);
1519 buffer
= schro_encoder_encode_auxiliary_data (encoder_frame
->encoder
,
1520 SCHRO_AUX_DATA_MD5_CHECKSUM
, checksum
, 16);
1522 schro_encoder_frame_insert_buffer (encoder_frame
, buffer
);
1526 * schro_encoder_insert_buffer:
1527 * @encoder: an encoder object
1530 * Inserts an application-provided buffer into the encoded video stream
1531 * with the next frame that is pushed.
1534 schro_encoder_insert_buffer (SchroEncoder
* encoder
, SchroBuffer
* buffer
)
1536 schro_list_append (encoder
->inserted_buffers
, buffer
);
1540 * schro_encoder_frame_insert_buffer:
1541 * @frame: an encoder frame
1544 * Inserts a buffer into an encoder frame.
1547 schro_encoder_frame_insert_buffer (SchroEncoderFrame
* frame
,
1548 SchroBuffer
* buffer
)
1550 schro_list_append (frame
->inserted_buffers
, buffer
);
1554 * schro_encoder_encode_auxiliary_data:
1560 * Packs data into a Dirac auxiliary data packet.
1565 schro_encoder_encode_auxiliary_data (SchroEncoder
* encoder
,
1566 SchroAuxiliaryDataID id
, const void *data
, int size
)
1569 SchroBuffer
*buffer
;
1571 buffer
= schro_buffer_new_and_alloc (size
+ SCHRO_PARSE_HEADER_SIZE
+ 1);
1573 pack
= schro_pack_new ();
1574 schro_pack_encode_init (pack
, buffer
);
1576 schro_encoder_encode_parse_info (pack
, SCHRO_PARSE_CODE_AUXILIARY_DATA
);
1577 schro_pack_encode_bits (pack
, 8, id
);
1578 schro_pack_append (pack
, data
, size
);
1580 schro_pack_free (pack
);
1586 * schro_encoder_encode_sequence_header:
1587 * @encoder: an encoder object
1589 * Creates a buffer containing a sequence header.
1594 schro_encoder_encode_sequence_header (SchroEncoder
* encoder
)
1597 SchroBuffer
*buffer
;
1598 SchroBuffer
*subbuffer
;
1601 buffer
= schro_buffer_new_and_alloc (0x100);
1603 pack
= schro_pack_new ();
1604 schro_pack_encode_init (pack
, buffer
);
1606 schro_encoder_encode_sequence_header_header (encoder
, pack
);
1608 schro_pack_flush (pack
);
1610 next_offset
= schro_pack_get_offset (pack
);
1611 buffer
->data
[5] = (next_offset
>> 24) & 0xff;
1612 buffer
->data
[6] = (next_offset
>> 16) & 0xff;
1613 buffer
->data
[7] = (next_offset
>> 8) & 0xff;
1614 buffer
->data
[8] = (next_offset
>> 0) & 0xff;
1616 subbuffer
= schro_buffer_new_subbuffer (buffer
, 0,
1617 schro_pack_get_offset (pack
));
1618 schro_pack_free (pack
);
1619 schro_buffer_unref (buffer
);
1626 * schro_encoder_encode_end_of_stream:
1629 * Creates an end-of-stream packet.
1634 schro_encoder_encode_end_of_stream (SchroEncoder
* encoder
)
1637 SchroBuffer
*buffer
;
1639 buffer
= schro_buffer_new_and_alloc (SCHRO_PARSE_HEADER_SIZE
);
1641 pack
= schro_pack_new ();
1642 schro_pack_encode_init (pack
, buffer
);
1644 schro_encoder_encode_parse_info (pack
, SCHRO_PARSE_CODE_END_OF_SEQUENCE
);
1646 schro_pack_free (pack
);
1652 * schro_encoder_wait:
1653 * @encoder: an encoder object
1655 * Checks the state of the encoder. If the encoder requires the
1656 * application to do something, an appropriate state code is returned.
1657 * Otherwise, this function waits until the encoder requires the
1658 * application to do something.
1660 * Returns: a state code
1663 schro_encoder_wait (SchroEncoder
* encoder
)
1665 SchroStateEnum ret
= SCHRO_STATE_AGAIN
;
1667 schro_async_lock (encoder
->async
);
1669 if (schro_encoder_pull_is_ready_locked (encoder
)) {
1670 SCHRO_DEBUG ("have buffer");
1671 ret
= SCHRO_STATE_HAVE_BUFFER
;
1674 if (schro_encoder_push_is_ready_locked (encoder
)) {
1675 SCHRO_DEBUG ("need frame");
1676 ret
= SCHRO_STATE_NEED_FRAME
;
1679 if (schro_queue_is_empty (encoder
->frame_queue
)
1680 && encoder
->end_of_stream_pulled
) {
1681 ret
= SCHRO_STATE_END_OF_STREAM
;
1685 SCHRO_DEBUG ("encoder waiting");
1686 ret
= schro_async_wait_locked (encoder
->async
);
1690 SCHRO_WARNING ("deadlock? kicking scheduler");
1691 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
1692 SchroEncoderFrame
*frame
= encoder
->frame_queue
->elements
[i
].data
;
1693 SCHRO_WARNING ("%d: %d %d %d %d %04x", i
, frame
->frame_number
,
1694 frame
->picture_number_ref
[0], frame
->picture_number_ref
[1],
1695 frame
->busy
, 0 /*frame->state */ );
1697 for (i
= 0; i
< SCHRO_LIMIT_REFERENCE_FRAMES
; i
++) {
1698 SchroEncoderFrame
*frame
= encoder
->reference_pictures
[i
];
1700 SCHRO_WARNING ("ref %d: %d %d %04x", i
, frame
->frame_number
,
1701 frame
->busy
, 0 /*frame->state */ );
1703 SCHRO_WARNING ("ref %d: NULL", i
);
1707 schro_async_signal_scheduler (encoder
->async
);
1708 ret
= SCHRO_STATE_AGAIN
;
1712 schro_async_unlock (encoder
->async
);
1718 schro_encoder_frame_is_B_frame (SchroEncoderFrame
* frame
)
1722 if (frame
->num_refs
== 2 &&
1723 ((frame
->picture_number_ref
[0] < frame
->frame_number
1724 && frame
->picture_number_ref
[1] > frame
->frame_number
)
1725 || (frame
->picture_number_ref
[1] < frame
->frame_number
1726 && frame
->picture_number_ref
[0] > frame
->frame_number
))) {
1736 schro_encoder_frame_complete (SchroAsyncStage
* stage
)
1738 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
1741 SCHRO_INFO ("completing task, picture %d working %02x in state %02x",
1742 frame
->frame_number
, frame
->working
, 0 /*frame->state */ );
1744 SCHRO_ASSERT (frame
->busy
== TRUE
);
1746 frame
->busy
= FALSE
;
1747 stage
->is_done
= TRUE
;
1750 if (stage
== frame
->stages
+ SCHRO_ENCODER_FRAME_STAGE_POSTANALYSE
) {
1751 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_DONE
].is_done
= TRUE
;
1753 SCHRO_ASSERT (frame
->output_buffer_size
> 0);
1755 if (frame
->previous_frame
) {
1756 schro_encoder_frame_unref (frame
->previous_frame
);
1757 frame
->previous_frame
= NULL
;
1759 if (frame
->motion
) {
1760 schro_motion_free (frame
->motion
);
1761 frame
->motion
= NULL
;
1764 schro_motionest_free (frame
->me
);
1767 if (frame
->ref_frame
[0]) {
1768 schro_encoder_frame_unref (frame
->ref_frame
[0]);
1769 frame
->ref_frame
[0] = NULL
;
1771 if (frame
->ref_frame
[1]) {
1772 schro_encoder_frame_unref (frame
->ref_frame
[1]);
1773 frame
->ref_frame
[1] = NULL
;
1776 if (frame
->deep_me
) {
1777 schro_me_free (frame
->deep_me
);
1778 frame
->deep_me
= NULL
;
1781 for (i
= 0; 2 > i
; ++i
) {
1782 if (frame
->hier_bm
[i
]) {
1783 schro_hbm_unref (frame
->hier_bm
[i
]);
1784 frame
->hier_bm
[i
] = NULL
;
1788 if (!frame
->is_ref
) {
1789 for (i
= 0; i
< 5; i
++) {
1790 if (frame
->downsampled_frames
[i
]) {
1791 schro_frame_unref (frame
->downsampled_frames
[i
]);
1792 frame
->downsampled_frames
[i
] = NULL
;
1797 if (frame
->start_sequence_header
) {
1798 frame
->sequence_header_buffer
=
1799 schro_encoder_encode_sequence_header (frame
->encoder
);
1801 if (frame
->last_frame
) {
1802 frame
->encoder
->completed_eos
= TRUE
;
1808 schro_encoder_sc_detect_1 (SchroAsyncStage
* stage
)
1810 SchroEncoderFrame
*frame
= stage
->priv
;
1811 SchroFrameData
*comp1
;
1812 SchroFrameData
*comp2
;
1814 SCHRO_ASSERT (frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_ANALYSE
].is_done
1815 && frame
->previous_frame
1816 && frame
->previous_frame
->
1817 stages
[SCHRO_ENCODER_FRAME_STAGE_ANALYSE
].is_done
);
1819 comp1
= &frame
->downsampled_frames
[0]->components
[0];
1820 comp2
= &frame
->previous_frame
->downsampled_frames
[0]->components
[0];
1823 schro_metric_absdiff_u8 (comp1
->data
, comp1
->stride
, comp2
->data
,
1824 comp2
->stride
, comp1
->width
, comp1
->height
);
1825 frame
->sc_mad
/= (comp1
->width
* comp1
->height
);
1826 frame
->have_mad
= 1;
1835 * Runs a stage in the encoding process.
1838 run_stage (SchroEncoderFrame
* frame
, int stage
)
1842 SCHRO_ASSERT (frame
->stages
[stage
].is_done
== FALSE
);
1845 frame
->working
= stage
;
1847 case SCHRO_ENCODER_FRAME_STAGE_ANALYSE
:
1848 func
= schro_encoder_analyse_picture
;
1850 case SCHRO_ENCODER_FRAME_STAGE_PREDICT_ROUGH
:
1851 func
= schro_encoder_predict_rough_picture
;
1853 case SCHRO_ENCODER_FRAME_STAGE_PREDICT_PEL
:
1854 func
= schro_encoder_predict_pel_picture
;
1856 case SCHRO_ENCODER_FRAME_STAGE_PREDICT_SUBPEL
:
1857 func
= schro_encoder_predict_subpel_picture
;
1859 case SCHRO_ENCODER_FRAME_STAGE_MODE_DECISION
:
1860 func
= schro_encoder_mode_decision
;
1862 case SCHRO_ENCODER_FRAME_STAGE_ENCODING
:
1863 func
= schro_encoder_encode_picture
;
1865 case SCHRO_ENCODER_FRAME_STAGE_RECONSTRUCT
:
1866 func
= schro_encoder_reconstruct_picture
;
1868 case SCHRO_ENCODER_FRAME_STAGE_POSTANALYSE
:
1869 func
= schro_encoder_postanalyse_picture
;
1871 case SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_1
:
1872 func
= schro_encoder_sc_detect_1
;
1878 frame
->stages
[stage
].task_func
= func
;
1879 frame
->stages
[stage
].priv
= frame
;
1880 schro_async_run_stage_locked (frame
->encoder
->async
, frame
->stages
+ stage
);
1885 * @frame: encoder frame
1887 * Checks whether reference pictures are available to be used for motion
1891 check_refs (SchroEncoderFrame
* frame
)
1893 if (frame
->num_refs
== 0)
1896 if (frame
->num_refs
> 0 &&
1897 !(frame
->ref_frame
[0]->stages
[SCHRO_ENCODER_FRAME_STAGE_DONE
].is_done
)) {
1900 if (frame
->num_refs
> 1 &&
1901 !(frame
->ref_frame
[1]->stages
[SCHRO_ENCODER_FRAME_STAGE_DONE
].is_done
)) {
1909 calculate_sc_score (SchroEncoder
* encoder
)
1911 #define SC_THRESHOLD_RANGE 2
1912 #define MAD_ARRAY_LEN SC_THRESHOLD_RANGE * 2 + 1
1913 #define BIG_INT 0xffffffff
1915 int mad_array
[MAD_ARRAY_LEN
];
1916 SchroEncoderFrame
*frame
, *f
, *f2
;
1918 /* calculate threshold first */
1919 for (i
= SC_THRESHOLD_RANGE
; encoder
->frame_queue
->n
- SC_THRESHOLD_RANGE
> i
;
1921 int mad_max
= 0, mad_min
= BIG_INT
;
1923 frame
= encoder
->frame_queue
->elements
[i
].data
;
1924 if (!frame
->need_mad
)
1926 if (frame
->have_scene_change_score
)
1928 if (0 > frame
->sc_threshold
) {
1929 memset (mad_array
, -1, sizeof (mad_array
));
1930 mad_array
[SC_THRESHOLD_RANGE
] = frame
->sc_mad
;
1931 for (j
= 0; SC_THRESHOLD_RANGE
> j
; ++j
) {
1932 f
= encoder
->frame_queue
->elements
[i
- j
- 1].data
;
1933 mad_array
[SC_THRESHOLD_RANGE
- j
- 1] = f
->sc_mad
;
1934 f
= encoder
->frame_queue
->elements
[i
+ j
+ 1].data
;
1935 mad_array
[SC_THRESHOLD_RANGE
+ j
+ 1] = f
->sc_mad
;
1937 for (j
= 0; MAD_ARRAY_LEN
> j
; ++j
) {
1938 if (mad_array
[j
] > mad_max
)
1939 mad_max
= mad_array
[j
];
1940 if (mad_array
[j
] < mad_min
)
1941 mad_min
= mad_array
[j
];
1943 frame
->sc_threshold
= mad_max
+ 0.8 * (mad_max
- mad_min
);
1946 /* and now calculate the score */
1947 for (i
= SC_THRESHOLD_RANGE
+ 3;
1948 encoder
->frame_queue
->n
- SC_THRESHOLD_RANGE
- 3 > i
; ++i
) {
1949 frame
= encoder
->frame_queue
->elements
[i
].data
;
1950 if (!frame
->need_mad
)
1952 if (frame
->have_scene_change_score
)
1954 f
= encoder
->frame_queue
->elements
[i
- 3].data
;
1955 f2
= encoder
->frame_queue
->elements
[i
+ 3].data
;
1956 frame
->scene_change_score
=
1957 frame
->sc_mad
- (f
->sc_threshold
+ f2
->sc_threshold
) / 2;
1958 frame
->have_scene_change_score
= 1;
1960 /* finally we update the state of the processed items */
1961 if (encoder
->queue_depth
> encoder
->frame_queue
->n
&& encoder
->end_of_stream
) {
1962 /* end of sequence */
1963 end
= encoder
->frame_queue
->n
;
1965 end
= encoder
->frame_queue
->n
- SC_THRESHOLD_RANGE
- 3;
1967 for (i
= 0; end
> i
; ++i
) {
1968 f
= encoder
->frame_queue
->elements
[i
].data
;
1969 f
->stages
[SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_2
].is_done
= TRUE
;
1970 SCHRO_DEBUG ("calculate sc score for i=%d picture=%d state=%d", i
,
1971 f
->frame_number
, 0 /* f->state */ );
1979 schro_encoder_async_schedule (SchroEncoder
* encoder
,
1980 SchroExecDomain exec_domain
)
1982 SchroEncoderFrame
*frame
;
1986 SCHRO_INFO ("iterate %d", encoder
->completed_eos
);
1988 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
1989 frame
= encoder
->frame_queue
->elements
[i
].data
;
1990 SCHRO_DEBUG ("analyse i=%d picture=%d state=%d busy=%d", i
,
1991 frame
->frame_number
, 0 /*frame->state */ , frame
->busy
);
1996 #define TODO(stage) (frame->stages[(stage)].is_needed && !frame->stages[(stage)].is_done)
1998 if (TODO (SCHRO_ENCODER_FRAME_STAGE_ANALYSE
)) {
1999 encoder
->init_frame (frame
);
2000 init_params (frame
);
2001 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_ANALYSE
);
2006 if (encoder
->enable_scene_change_detection
) {
2007 /* calculate MAD for entire queue of frames */
2008 for (i
= 0; encoder
->frame_queue
->n
> i
; ++i
) {
2009 SchroEncoderFrame
*prev_frame
;
2011 frame
= encoder
->frame_queue
->elements
[i
].data
;
2013 ("phase I of shot change detection for i=%d picture=%d state=%d", i
,
2014 frame
->frame_number
, 0 /* frame->state */ );
2019 if (0 == i
|| FALSE
== frame
->need_mad
) {
2020 /* we can't calculate a MAD for first frame in the queue */
2021 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_1
].is_done
= TRUE
;
2024 prev_frame
= frame
->previous_frame
;
2026 if (!frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_1
].is_done
2028 && prev_frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_ANALYSE
].is_done
2029 && frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_ANALYSE
].is_done
) {
2030 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_1
);
2035 /* calculate new scene change score */
2036 for (i
= 0; encoder
->frame_queue
->n
> i
; ++i
) {
2037 frame
= encoder
->frame_queue
->elements
[i
].data
;
2038 if (!frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_1
].is_done
) {
2042 calculate_sc_score (encoder
);
2044 for (i
= 0; encoder
->frame_queue
->n
> i
; ++i
) {
2045 frame
= encoder
->frame_queue
->elements
[i
].data
;
2046 if (frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_ANALYSE
].is_done
) {
2047 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_1
].is_done
= TRUE
;
2048 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_SC_DETECT_2
].is_done
= TRUE
;
2053 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
2054 frame
= encoder
->frame_queue
->elements
[i
].data
;
2055 if (frame
->frame_number
== encoder
->gop_picture
) {
2056 encoder
->handle_gop (encoder
, i
);
2061 /* Reference pictures are higher priority, so we pass over the list
2062 * first for reference pictures, then for non-ref. */
2063 for (ref
= 1; ref
>= 0; ref
--) {
2064 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
2065 frame
= encoder
->frame_queue
->elements
[i
].data
;
2066 SCHRO_DEBUG ("backref i=%d picture=%d state=%d busy=%d", i
,
2067 frame
->frame_number
, 0 /*frame->state */ , frame
->busy
);
2072 if (frame
->is_ref
!= ref
)
2075 if (TODO (SCHRO_ENCODER_FRAME_STAGE_HAVE_PARAMS
) &&
2076 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_HAVE_GOP
].is_done
) {
2078 if (encoder
->setup_frame (frame
)) {
2079 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_HAVE_PARAMS
].is_done
= TRUE
;
2083 if (TODO (SCHRO_ENCODER_FRAME_STAGE_PREDICT_ROUGH
) &&
2084 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_HAVE_PARAMS
].is_done
) {
2085 if (!check_refs (frame
))
2087 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_PREDICT_ROUGH
);
2091 if (TODO (SCHRO_ENCODER_FRAME_STAGE_PREDICT_PEL
) &&
2092 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_PREDICT_ROUGH
].is_done
) {
2093 schro_frame_set_wavelet_params (frame
);
2094 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_PREDICT_PEL
);
2101 for (ref
= 1; ref
>= 0; ref
--) {
2102 for (i
= 0; encoder
->frame_queue
->n
> i
; ++i
) {
2103 frame
= encoder
->frame_queue
->elements
[i
].data
;
2104 SCHRO_DEBUG ("serialised stage i=%d picture=%d state=%d slot=%d quant_slot=%d", i
, frame
->frame_number
, 0 /* frame>state */
2105 , frame
->slot
, encoder
->quant_slot
);
2110 if (encoder
->enable_deep_estimation
) {
2111 if (frame
->slot
!= encoder
->quant_slot
) {
2114 } else if (encoder
->enable_bigblock_estimation
) {
2115 if (frame
->is_ref
!= ref
)
2119 if (TODO (SCHRO_ENCODER_FRAME_STAGE_PREDICT_SUBPEL
) &&
2120 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_PREDICT_PEL
].is_done
) {
2121 schro_encoder_set_frame_lambda (frame
);
2122 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_PREDICT_SUBPEL
);
2126 if (TODO (SCHRO_ENCODER_FRAME_STAGE_MODE_DECISION
) &&
2127 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_PREDICT_SUBPEL
].is_done
) {
2128 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_MODE_DECISION
);
2134 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
2135 frame
= encoder
->frame_queue
->elements
[i
].data
;
2136 if (frame
->slot
== encoder
->quant_slot
) {
2138 ret
= encoder
->handle_quants (encoder
, i
);
2144 for (ref
= 1; ref
>= 0; ref
--) {
2145 for (i
= 0; encoder
->frame_queue
->n
> i
; ++i
) {
2146 frame
= encoder
->frame_queue
->elements
[i
].data
;
2151 if (encoder
->enable_deep_estimation
) {
2152 if (frame
->slot
!= encoder
->quant_slot
)
2154 } else if (encoder
->enable_bigblock_estimation
) {
2155 if (frame
->is_ref
!= ref
)
2159 if (TODO (SCHRO_ENCODER_FRAME_STAGE_ENCODING
) &&
2160 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_HAVE_QUANTS
].is_done
) {
2161 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_ENCODING
);
2167 for (ref
= 1; ref
>= 0; ref
--) {
2168 for (i
= 0; i
< encoder
->frame_queue
->n
; i
++) {
2169 frame
= encoder
->frame_queue
->elements
[i
].data
;
2170 SCHRO_DEBUG ("backref i=%d picture=%d state=%d busy=%d", i
,
2171 frame
->frame_number
, 0 /*frame->state */ , frame
->busy
);
2175 if (frame
->is_ref
!= ref
)
2178 if (TODO (SCHRO_ENCODER_FRAME_STAGE_RECONSTRUCT
) &&
2179 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_ENCODING
].is_done
) {
2180 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_RECONSTRUCT
);
2183 if (TODO (SCHRO_ENCODER_FRAME_STAGE_POSTANALYSE
) &&
2184 frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_RECONSTRUCT
].is_done
) {
2185 run_stage (frame
, SCHRO_ENCODER_FRAME_STAGE_POSTANALYSE
);
2196 schro_encoder_analyse_picture (SchroAsyncStage
* stage
)
2198 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
2200 if (frame
->encoder
->filtering
!= 0 || frame
->need_extension
) {
2201 if (frame
->encoder
->enable_deep_estimation
) {
2202 frame
->filtered_frame
=
2203 schro_frame_dup_full (frame
->original_frame
,
2204 MAX (frame
->params
.xbsep_luma
* 4, frame
->params
.ybsep_luma
* 4),
2206 } else if (frame
->encoder
->enable_bigblock_estimation
) {
2207 frame
->filtered_frame
= schro_frame_dup_full (frame
->original_frame
,
2211 switch (frame
->encoder
->filtering
) {
2213 schro_frame_filter_cwmN (frame
->filtered_frame
,
2214 frame
->encoder
->filter_value
);
2217 schro_frame_filter_lowpass2 (frame
->filtered_frame
,
2218 frame
->encoder
->filter_value
);
2221 schro_frame_filter_addnoise (frame
->filtered_frame
,
2222 frame
->encoder
->filter_value
);
2225 schro_frame_filter_adaptive_lowpass (frame
->filtered_frame
);
2228 schro_frame_filter_lowpass (frame
->filtered_frame
,
2229 frame
->encoder
->filter_value
);
2235 schro_frame_mc_edgeextend (frame
->filtered_frame
);
2237 frame
->filtered_frame
= schro_frame_dup_full (frame
->original_frame
,
2241 if (frame
->need_downsampling
) {
2242 schro_encoder_frame_downsample (frame
);
2243 frame
->have_downsampling
= TRUE
;
2245 schro_frame_ref (frame
->filtered_frame
);
2246 frame
->upsampled_original_frame
= frame
->filtered_frame
;
2247 if (frame
->need_upsampling
) {
2248 schro_upsampled_frame_upsample (frame
->upsampled_original_frame
);
2249 frame
->have_upsampling
= TRUE
;
2252 if (frame
->need_average_luma
) {
2253 if (frame
->have_downsampling
) {
2254 frame
->average_luma
=
2255 schro_frame_calculate_average_luma (frame
->
2256 downsampled_frames
[frame
->encoder
->downsample_levels
- 1]);
2258 frame
->average_luma
=
2259 schro_frame_calculate_average_luma (frame
->filtered_frame
);
2261 frame
->have_average_luma
= TRUE
;
2266 schro_encoder_predict_rough_picture (SchroAsyncStage
* stage
)
2268 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
2270 SCHRO_INFO ("predict picture %d", frame
->frame_number
);
2272 if (frame
->params
.num_refs
> 0) {
2273 schro_encoder_motion_predict_rough (frame
);
2277 /* should perform fullpel ME without "rendering",
2278 * ie without mode decision and motion compensation and DWT */
2280 schro_encoder_predict_pel_picture (SchroAsyncStage
* stage
)
2282 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
2285 && frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_PREDICT_ROUGH
].is_done
);
2286 SCHRO_INFO ("fullpel predict picture %d", frame
->frame_number
);
2288 if (frame
->params
.num_refs
> 0) {
2289 schro_encoder_motion_predict_pel (frame
);
2294 schro_encoder_predict_subpel_picture (SchroAsyncStage
* stage
)
2296 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
2298 if (frame
->encoder
->enable_bigblock_estimation
) {
2299 if (frame
->params
.num_refs
> 0 && frame
->params
.mv_precision
> 0) {
2300 schro_encoder_motion_predict_subpel (frame
);
2302 } else if (frame
->encoder
->enable_deep_estimation
) {
2303 int ref
, xnum_blocks
, ynum_blocks
;
2304 SchroMotionField
*mf_dest
, *mf_src
;
2306 if (frame
->params
.num_refs
> 0) {
2307 xnum_blocks
= frame
->params
.x_num_blocks
;
2308 ynum_blocks
= frame
->params
.y_num_blocks
;
2309 for (ref
= 0; frame
->params
.num_refs
> ref
; ++ref
) {
2310 mf_dest
= schro_motion_field_new (xnum_blocks
, ynum_blocks
);
2311 mf_src
= schro_hbm_motion_field (frame
->hier_bm
[ref
], 0);
2312 memcpy (mf_dest
->motion_vectors
, mf_src
->motion_vectors
,
2313 xnum_blocks
* ynum_blocks
* sizeof (SchroMotionVector
));
2314 schro_me_set_subpel_mf (frame
->deep_me
, mf_dest
, ref
);
2317 if (frame
->params
.num_refs
> 0 && frame
->params
.mv_precision
> 0) {
2318 schro_me_set_lambda (frame
->deep_me
, frame
->frame_me_lambda
);
2319 schro_encoder_motion_predict_subpel_deep (frame
->deep_me
);
2324 /* performs mode decision and superblock splitting
2325 * finally it does DWT */
2327 schro_encoder_mode_decision (SchroAsyncStage
* stage
)
2329 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
2331 SCHRO_ASSERT (frame
->stages
[SCHRO_ENCODER_FRAME_STAGE_PREDICT_PEL
].is_done
);
2332 SCHRO_INFO ("mode decision and superblock splitting picture %d",
2333 frame
->frame_number
);
2335 if (frame
->encoder
->enable_deep_estimation
) {
2336 SchroMotionField
*mf
, *mf_src
;
2337 int xnum_blocks
, ynum_blocks
, ref
;
2338 if (frame
->params
.num_refs
> 0) {
2339 xnum_blocks
= frame
->params
.x_num_blocks
;
2340 ynum_blocks
= frame
->params
.y_num_blocks
;
2341 for (ref
= 0; frame
->params
.num_refs
> ref
; ++ref
) {
2342 mf
= schro_motion_field_new (xnum_blocks
, ynum_blocks
);
2343 schro_motion_field_set (mf
, 2, ref
+ 1);
2344 /* copy split2 from subpel */
2345 mf_src
= schro_me_subpel_mf (frame
->deep_me
, ref
);
2346 SCHRO_ASSERT (mf_src
);
2347 memcpy (mf
->motion_vectors
, mf_src
->motion_vectors
,
2348 xnum_blocks
* ynum_blocks
* sizeof (SchroMotionVector
));
2349 schro_me_set_split2_mf (frame
->deep_me
, mf
, ref
);
2351 mf
= schro_motion_field_new (xnum_blocks
, ynum_blocks
);
2352 schro_motion_field_set (mf
, 1, ref
+ 1);
2353 schro_me_set_split1_mf (frame
->deep_me
, mf
, ref
);
2355 mf
= schro_motion_field_new (xnum_blocks
, ynum_blocks
);
2356 schro_motion_field_set (mf
, 0, ref
+ 1);
2357 schro_me_set_split0_mf (frame
->deep_me
, mf
, ref
);
2359 SCHRO_INFO ("mode decision and superblock splitting picture %d",
2360 frame
->frame_number
);
2361 schro_me_set_motion (frame
->deep_me
, frame
->motion
);
2362 schro_me_set_lambda (frame
->deep_me
, frame
->frame_me_lambda
);
2363 schro_mode_decision (frame
->deep_me
);
2364 schro_motion_calculate_stats (frame
->motion
, frame
);
2365 frame
->estimated_mc_bits
= schro_motion_estimate_entropy (frame
->motion
);
2366 frame
->badblock_ratio
= schro_me_badblocks_ratio (frame
->deep_me
);
2367 frame
->dcblock_ratio
= schro_me_dcblock_ratio (frame
->deep_me
);
2368 frame
->mc_error
= schro_me_mc_error (frame
->deep_me
);
2370 SCHRO_DEBUG ("DC block ratio for frame %d s %g", frame
->frame_number
,
2371 frame
->dcblock_ratio
);
2373 if (frame
->dcblock_ratio
> frame
->encoder
->magic_me_bailout_limit
) {
2374 if (frame
->deep_me
) {
2375 /* Free the motion estimation info if we are inserting an I frame */
2376 schro_me_free (frame
->deep_me
);
2377 frame
->deep_me
= NULL
;
2379 frame
->params
.num_refs
= 0;
2380 frame
->num_refs
= 0;
2382 ("DC block ratio too high for frame %d, inserting an intra picture",
2383 frame
->frame_number
);
2388 schro_encoder_render_picture (frame
);
2392 schro_encoder_iwt_transform (SchroFrame
* frame
, SchroParams
* params
)
2400 tmp
= schro_malloc (sizeof (int32_t) * (params
->iwt_luma_width
+ 8) * 2);
2402 for (component
= 0; component
< 3; component
++) {
2403 SchroFrameData
*comp
= &frame
->components
[component
];
2405 if (component
== 0) {
2406 width
= params
->iwt_luma_width
;
2407 height
= params
->iwt_luma_height
;
2409 width
= params
->iwt_chroma_width
;
2410 height
= params
->iwt_chroma_height
;
2413 for (level
= 0; level
< params
->transform_depth
; level
++) {
2416 fd
.format
= frame
->format
;
2417 fd
.data
= comp
->data
;
2418 fd
.width
= width
>> level
;
2419 fd
.height
= height
>> level
;
2420 fd
.stride
= comp
->stride
<< level
;
2422 schro_wavelet_transform_2d (&fd
, params
->wavelet_filter_index
, tmp
);
2430 schro_encoder_render_picture (SchroEncoderFrame
* frame
)
2432 SCHRO_INFO ("render picture %d", frame
->frame_number
);
2434 if (frame
->params
.num_refs
> 0) {
2435 frame
->motion
->src1
= frame
->ref_frame
[0]->reconstructed_frame
;
2436 if (frame
->params
.num_refs
> 1) {
2437 frame
->motion
->src2
= frame
->ref_frame
[1]->reconstructed_frame
;
2440 SCHRO_ASSERT (schro_motion_verify (frame
->motion
));
2444 if (frame
->params
.num_refs
> 0) {
2445 schro_frame_convert (frame
->iwt_frame
, frame
->filtered_frame
);
2447 schro_motion_render (frame
->motion
, frame
->prediction_frame
,
2448 frame
->iwt_frame
, FALSE
, NULL
);
2450 schro_frame_zero_extend (frame
->iwt_frame
,
2451 frame
->params
.video_format
->width
,
2452 schro_video_format_get_picture_height (frame
->params
.video_format
));
2454 schro_frame_convert (frame
->iwt_frame
, frame
->filtered_frame
);
2457 schro_encoder_iwt_transform (frame
->iwt_frame
, &frame
->params
);
2459 schro_encoder_clean_up_transform (frame
);
2463 schro_encoder_encode_picture (SchroAsyncStage
* stage
)
2465 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
2466 SchroBuffer
*subbuffer
;
2467 int picture_chroma_width
, picture_chroma_height
;
2469 int total_frame_bits
;
2471 double est_subband_bits
;
2473 SCHRO_INFO ("encode picture %d", frame
->frame_number
);
2475 frame
->output_buffer
= schro_buffer_new_and_alloc (frame
->output_buffer_size
);
2477 schro_video_format_get_iwt_alloc_size (&frame
->encoder
->video_format
,
2478 &width
, &height
, frame
->encoder
->transform_depth
);
2479 frame
->subband_size
= width
* height
/ 4;
2480 frame
->subband_buffer
=
2481 schro_buffer_new_and_alloc (frame
->subband_size
* sizeof (int16_t));
2483 schro_video_format_get_picture_chroma_size (&frame
->encoder
->video_format
,
2484 &picture_chroma_width
, &picture_chroma_height
);
2486 /* FIXME this is only used for lowdelay, and is way too big */
2487 frame
->quant_data
= schro_malloc (sizeof (int16_t) * frame
->subband_size
);
2489 frame
->pack
= schro_pack_new ();
2490 schro_pack_encode_init (frame
->pack
, frame
->output_buffer
);
2492 total_frame_bits
= -schro_pack_get_offset (frame
->pack
) * 8;
2495 schro_encoder_encode_parse_info (frame
->pack
,
2496 SCHRO_PARSE_CODE_PICTURE (frame
->is_ref
, frame
->params
.num_refs
,
2497 frame
->params
.is_lowdelay
, frame
->params
.is_noarith
));
2498 schro_encoder_encode_picture_header (frame
);
2500 if (frame
->params
.num_refs
> 0) {
2501 schro_pack_sync (frame
->pack
);
2502 schro_encoder_encode_picture_prediction_parameters (frame
);
2503 schro_pack_sync (frame
->pack
);
2504 frame
->actual_mc_bits
= -schro_pack_get_offset (frame
->pack
) * 8;
2505 schro_encoder_encode_superblock_split (frame
);
2506 schro_encoder_encode_prediction_modes (frame
);
2507 schro_encoder_encode_vector_data (frame
, 0, 0);
2508 schro_encoder_encode_vector_data (frame
, 0, 1);
2509 if (frame
->params
.num_refs
> 1) {
2510 schro_encoder_encode_vector_data (frame
, 1, 0);
2511 schro_encoder_encode_vector_data (frame
, 1, 1);
2513 schro_encoder_encode_dc_data (frame
, 0);
2514 schro_encoder_encode_dc_data (frame
, 1);
2515 schro_encoder_encode_dc_data (frame
, 2);
2516 frame
->actual_mc_bits
+= schro_pack_get_offset (frame
->pack
) * 8;
2519 if (schro_pack_get_offset (frame
->pack
) * 8 > frame
->hard_limit_bits
) {
2520 SCHRO_ERROR ("over hard_limit_bits after MC (%d>%d)",
2521 schro_pack_get_offset (frame
->pack
) * 8, frame
->hard_limit_bits
);
2524 schro_pack_sync (frame
->pack
);
2525 schro_encoder_encode_transform_parameters (frame
);
2527 if (schro_pack_get_offset (frame
->pack
) * 8 > frame
->hard_limit_bits
) {
2528 SCHRO_ERROR ("over hard_limit_bits after transform params (%d>%d)",
2529 schro_pack_get_offset (frame
->pack
) * 8, frame
->hard_limit_bits
);
2532 frame
->actual_residual_bits
= -schro_pack_get_offset (frame
->pack
) * 8;
2534 schro_pack_sync (frame
->pack
);
2535 if (frame
->params
.is_lowdelay
) {
2536 schro_encoder_encode_lowdelay_transform_data (frame
);
2538 schro_encoder_encode_transform_data (frame
);
2541 schro_pack_flush (frame
->pack
);
2542 frame
->actual_residual_bits
+= schro_pack_get_offset (frame
->pack
) * 8;
2543 total_frame_bits
+= schro_pack_get_offset (frame
->pack
) * 8;
2545 SCHRO_DEBUG ("Actual frame %d residual bits : %d", frame
->frame_number
,
2546 frame
->actual_residual_bits
);
2547 // Update the fiddle factors for estimating entropy
2548 if (frame
->num_refs
== 0) {
2550 for (component
= 0; component
< 3; ++component
) {
2551 for (b
= 0; b
< 1 + 3 * frame
->params
.transform_depth
; ++b
) {
2554 est_entropy
[component
][b
][frame
->quant_indices
[component
][b
][0]];
2555 SCHRO_DEBUG ("Actual versus estimated band bits : %d %d %g %g",
2556 component
, b
, frame
->actual_subband_bits
[component
][b
],
2558 if (est_subband_bits
> 200.0) {
2560 factor
= frame
->actual_subband_bits
[component
][b
] / est_subband_bits
;
2562 frame
->encoder
->average_arith_context_ratios_intra
[component
][b
];
2563 ratio
= ratio
* 0.9 + factor
* 0.1;
2564 frame
->encoder
->average_arith_context_ratios_intra
[component
][b
] =
2571 for (component
= 0; component
< 3; ++component
) {
2572 for (b
= 0; b
< 1 + 3 * frame
->params
.transform_depth
; ++b
) {
2575 est_entropy
[component
][b
][frame
->quant_indices
[component
][b
][0]];
2576 SCHRO_DEBUG ("Actual versus estimated band bits : %d %d %g %g",
2577 component
, b
, frame
->actual_subband_bits
[component
][b
],
2579 if (est_subband_bits
> 200.0) {
2581 factor
= frame
->actual_subband_bits
[component
][b
] / est_subband_bits
;
2583 frame
->encoder
->average_arith_context_ratios_inter
[component
][b
];
2584 ratio
= ratio
* 0.9 + factor
* 0.1;
2585 frame
->encoder
->average_arith_context_ratios_inter
[component
][b
] =
2592 // Update the buffer model
2593 if (frame
->encoder
->rate_control
==
2594 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE
) {
2595 SchroEncoder
*encoder
= frame
->encoder
;
2596 encoder
->buffer_level
-= total_frame_bits
;
2597 encoder
->buffer_level
+= encoder
->bits_per_picture
;
2599 if (encoder
->buffer_level
< 0) {
2600 SCHRO_DEBUG ("buffer underrun by %d bytes", -encoder
->buffer_level
);
2601 encoder
->buffer_level
= 0;
2604 if (encoder
->buffer_level
> encoder
->buffer_size
) {
2607 n
= (encoder
->buffer_level
- encoder
->buffer_size
+ 7) / 8;
2608 SCHRO_DEBUG ("buffer overrun, adding padding of %d bytes", n
);
2609 n
= schro_encoder_encode_padding (encoder
, n
);
2610 encoder
->buffer_level
-= n
* 8;
2612 SCHRO_DEBUG ("At frame %d, buffer level %d of %d bits", frame
->frame_number
,
2613 encoder
->buffer_level
, encoder
->buffer_size
);
2615 schro_encoder_cbr_update (frame
, total_frame_bits
);
2618 if (schro_pack_get_offset (frame
->pack
) * 8 > frame
->hard_limit_bits
) {
2619 SCHRO_ERROR ("over hard_limit_bits after residual (%d>%d)",
2620 schro_pack_get_offset (frame
->pack
) * 8, frame
->hard_limit_bits
);
2623 subbuffer
= schro_buffer_new_subbuffer (frame
->output_buffer
, 0,
2624 schro_pack_get_offset (frame
->pack
));
2625 schro_buffer_unref (frame
->output_buffer
);
2626 frame
->output_buffer
= subbuffer
;
2628 if (frame
->subband_buffer
) {
2629 schro_buffer_unref (frame
->subband_buffer
);
2631 if (frame
->quant_data
) {
2632 free (frame
->quant_data
);
2634 if (frame
->quant_frame
) {
2635 schro_frame_unref (frame
->quant_frame
);
2638 schro_pack_free (frame
->pack
);
2642 frame
->encoder
->quant_slot
++;
2646 schro_encoder_inverse_iwt_transform (SchroFrame
* frame
, SchroParams
* params
)
2654 tmp
= schro_malloc (sizeof (int32_t) * (params
->iwt_luma_width
+ 16));
2656 for (component
= 0; component
< 3; component
++) {
2657 SchroFrameData
*comp
= &frame
->components
[component
];
2659 if (component
== 0) {
2660 width
= params
->iwt_luma_width
;
2661 height
= params
->iwt_luma_height
;
2663 width
= params
->iwt_chroma_width
;
2664 height
= params
->iwt_chroma_height
;
2667 for (level
= params
->transform_depth
- 1; level
>= 0; level
--) {
2668 SchroFrameData fd_dest
;
2669 SchroFrameData fd_src
;
2671 fd_src
.format
= frame
->format
;
2672 fd_src
.data
= comp
->data
;
2673 fd_src
.width
= width
>> level
;
2674 fd_src
.height
= height
>> level
;
2675 fd_src
.stride
= comp
->stride
<< level
;
2677 fd_dest
.format
= frame
->format
;
2678 fd_dest
.data
= comp
->data
;
2679 fd_dest
.width
= width
>> level
;
2680 fd_dest
.height
= height
>> level
;
2681 fd_dest
.stride
= comp
->stride
<< level
;
2683 schro_wavelet_inverse_transform_2d (&fd_dest
, &fd_src
,
2684 params
->wavelet_filter_index
, tmp
);
2692 schro_encoder_reconstruct_picture (SchroAsyncStage
* stage
)
2694 SchroEncoderFrame
*encoder_frame
= (SchroEncoderFrame
*) stage
->priv
;
2696 schro_encoder_inverse_iwt_transform (encoder_frame
->iwt_frame
,
2697 &encoder_frame
->params
);
2699 if (encoder_frame
->params
.num_refs
> 0) {
2700 schro_frame_add (encoder_frame
->iwt_frame
, encoder_frame
->prediction_frame
);
2703 if (encoder_frame
->encoder
->enable_md5
|| (encoder_frame
->is_ref
&&
2704 encoder_frame
->encoder
->gop_structure
!= SCHRO_ENCODER_GOP_INTRA_ONLY
)) {
2705 SchroFrameFormat frame_format
;
2708 frame_format
= schro_params_get_frame_format (encoder_frame
->encoder
->input_frame_depth
,
2709 encoder_frame
->encoder
->video_format
.chroma_format
);
2710 frame
= schro_frame_new_and_alloc_full (NULL
, frame_format
,
2711 encoder_frame
->encoder
->video_format
.width
,
2712 schro_video_format_get_picture_height (&encoder_frame
->
2713 encoder
->video_format
), 32, TRUE
);
2714 schro_frame_convert (frame
, encoder_frame
->iwt_frame
);
2715 schro_frame_mc_edgeextend (frame
);
2716 encoder_frame
->reconstructed_frame
= frame
;
2719 if (encoder_frame
->encoder
->enable_md5
) {
2720 schro_encoder_encode_md5_checksum (encoder_frame
);
2723 if (encoder_frame
->is_ref
&& encoder_frame
->encoder
->mv_precision
> 0) {
2724 schro_upsampled_frame_upsample (encoder_frame
->reconstructed_frame
);
2729 schro_encoder_postanalyse_picture (SchroAsyncStage
* stage
)
2731 SchroEncoderFrame
*frame
= (SchroEncoderFrame
*) stage
->priv
;
2732 SchroVideoFormat
*video_format
= frame
->params
.video_format
;
2734 if (frame
->encoder
->enable_psnr
) {
2737 schro_frame_mean_squared_error (frame
->filtered_frame
,
2738 schro_upsampled_frame_get_frame (frame
->reconstructed_frame
, 0), mse
);
2740 frame
->mean_squared_error_luma
= mse
[0] /
2741 (video_format
->luma_excursion
* video_format
->luma_excursion
);
2742 frame
->mean_squared_error_chroma
= 0.5 * (mse
[1] + mse
[2]) /
2743 (video_format
->chroma_excursion
* video_format
->chroma_excursion
);
2746 if (frame
->encoder
->enable_ssim
) {
2747 frame
->mssim
= schro_frame_ssim (frame
->original_frame
,
2748 schro_upsampled_frame_get_frame (frame
->reconstructed_frame
, 0));
2749 schro_dump (SCHRO_DUMP_SSIM
, "%d %g\n", frame
->frame_number
, frame
->mssim
);
2754 schro_encoder_encode_picture_prediction_parameters (SchroEncoderFrame
* frame
)
2756 SchroParams
*params
= &frame
->params
;
2759 /* block parameters */
2760 index
= schro_params_get_block_params (params
);
2761 schro_pack_encode_uint (frame
->pack
, index
);
2763 schro_pack_encode_uint (frame
->pack
, params
->xblen_luma
);
2764 schro_pack_encode_uint (frame
->pack
, params
->yblen_luma
);
2765 schro_pack_encode_uint (frame
->pack
, params
->xbsep_luma
);
2766 schro_pack_encode_uint (frame
->pack
, params
->xbsep_luma
);
2769 MARKER (frame
->pack
);
2772 schro_pack_encode_uint (frame
->pack
, params
->mv_precision
);
2774 MARKER (frame
->pack
);
2776 /* global motion flag */
2777 schro_pack_encode_bit (frame
->pack
, params
->have_global_motion
);
2778 if (params
->have_global_motion
) {
2780 for (i
= 0; i
< params
->num_refs
; i
++) {
2781 SchroGlobalMotion
*gm
= params
->global_motion
+ i
;
2784 if (gm
->b0
== 0 && gm
->b1
== 0) {
2785 schro_pack_encode_bit (frame
->pack
, 0);
2787 schro_pack_encode_bit (frame
->pack
, 1);
2788 schro_pack_encode_sint (frame
->pack
, gm
->b0
);
2789 schro_pack_encode_sint (frame
->pack
, gm
->b1
);
2792 /* zoom rotate shear */
2793 if (gm
->a_exp
== 0 && gm
->a00
== 1 && gm
->a01
== 0 && gm
->a10
== 0 &&
2795 schro_pack_encode_bit (frame
->pack
, 0);
2797 schro_pack_encode_bit (frame
->pack
, 1);
2798 schro_pack_encode_uint (frame
->pack
, gm
->a_exp
);
2799 schro_pack_encode_sint (frame
->pack
, gm
->a00
);
2800 schro_pack_encode_sint (frame
->pack
, gm
->a01
);
2801 schro_pack_encode_sint (frame
->pack
, gm
->a10
);
2802 schro_pack_encode_sint (frame
->pack
, gm
->a11
);
2806 if (gm
->c_exp
== 0 && gm
->c0
== 0 && gm
->c1
== 0) {
2807 schro_pack_encode_bit (frame
->pack
, 0);
2809 schro_pack_encode_bit (frame
->pack
, 1);
2810 schro_pack_encode_uint (frame
->pack
, gm
->c_exp
);
2811 schro_pack_encode_sint (frame
->pack
, gm
->c0
);
2812 schro_pack_encode_sint (frame
->pack
, gm
->c1
);
2817 MARKER (frame
->pack
);
2819 /* picture prediction mode flag */
2820 schro_pack_encode_uint (frame
->pack
, params
->picture_pred_mode
);
2822 /* non-default weights flag */
2823 SCHRO_ASSERT (params
->picture_weight_2
== 1 || params
->num_refs
== 2);
2824 if (params
->picture_weight_bits
== 1 &&
2825 params
->picture_weight_1
== 1 && params
->picture_weight_2
== 1) {
2826 schro_pack_encode_bit (frame
->pack
, FALSE
);
2828 schro_pack_encode_bit (frame
->pack
, TRUE
);
2829 schro_pack_encode_uint (frame
->pack
, params
->picture_weight_bits
);
2830 schro_pack_encode_sint (frame
->pack
, params
->picture_weight_1
);
2831 if (params
->num_refs
> 1) {
2832 schro_pack_encode_sint (frame
->pack
, params
->picture_weight_2
);
2836 MARKER (frame
->pack
);
2841 schro_encoder_encode_superblock_split (SchroEncoderFrame
* frame
)
2843 SchroParams
*params
= &frame
->params
;
2845 SchroArith
*arith
= NULL
;
2846 SchroPack b
, *pack
= &b
;
2848 if (params
->is_noarith
) {
2849 schro_pack_encode_init (pack
, frame
->subband_buffer
);
2851 arith
= schro_arith_new ();
2852 schro_arith_encode_init (arith
, frame
->subband_buffer
);
2855 for (j
= 0; j
< params
->y_num_blocks
; j
+= 4) {
2856 for (i
= 0; i
< params
->x_num_blocks
; i
+= 4) {
2857 int split_prediction
;
2859 SchroMotionVector
*mv
=
2860 &frame
->motion
->motion_vectors
[j
* params
->x_num_blocks
+ i
];
2862 SCHRO_ASSERT (mv
->split
< 3);
2864 split_prediction
= schro_motion_split_prediction (frame
->motion
, i
, j
);
2865 split_residual
= (mv
->split
- split_prediction
+ 3) % 3;
2866 if (params
->is_noarith
) {
2867 schro_pack_encode_uint (pack
, split_residual
);
2869 _schro_arith_encode_uint (arith
, SCHRO_CTX_SB_F1
,
2870 SCHRO_CTX_SB_DATA
, split_residual
);
2875 schro_pack_sync (frame
->pack
);
2876 if (params
->is_noarith
) {
2877 schro_pack_flush (pack
);
2878 schro_pack_encode_uint (frame
->pack
, schro_pack_get_offset (pack
));
2879 schro_pack_sync (frame
->pack
);
2880 schro_pack_append (frame
->pack
, pack
->buffer
->data
,
2881 schro_pack_get_offset (pack
));
2883 schro_arith_flush (arith
);
2884 schro_pack_encode_uint (frame
->pack
, arith
->offset
);
2885 schro_pack_sync (frame
->pack
);
2886 schro_pack_append (frame
->pack
, arith
->buffer
->data
, arith
->offset
);
2887 schro_arith_free (arith
);
2892 schro_encoder_encode_prediction_modes (SchroEncoderFrame
* frame
)
2894 SchroParams
*params
= &frame
->params
;
2896 SchroArith
*arith
= NULL
;
2897 SchroPack b
, *pack
= &b
;
2899 if (params
->is_noarith
) {
2900 schro_pack_encode_init (pack
, frame
->subband_buffer
);
2902 arith
= schro_arith_new ();
2903 schro_arith_encode_init (arith
, frame
->subband_buffer
);
2906 for (j
= 0; j
< params
->y_num_blocks
; j
+= 4) {
2907 for (i
= 0; i
< params
->x_num_blocks
; i
+= 4) {
2909 SchroMotionVector
*mv1
=
2910 &frame
->motion
->motion_vectors
[j
* params
->x_num_blocks
+ i
];
2912 for (l
= 0; l
< 4; l
+= (4 >> mv1
->split
)) {
2913 for (k
= 0; k
< 4; k
+= (4 >> mv1
->split
)) {
2914 SchroMotionVector
*mv
=
2915 &frame
->motion
->motion_vectors
[(j
+ l
) * params
->x_num_blocks
+
2919 pred_mode
= mv
->pred_mode
^
2920 schro_motion_get_mode_prediction (frame
->motion
, i
+ k
, j
+ l
);
2922 if (params
->is_noarith
) {
2923 schro_pack_encode_bit (pack
, pred_mode
& 1);
2925 _schro_arith_encode_bit (arith
, SCHRO_CTX_BLOCK_MODE_REF1
,
2928 if (params
->num_refs
> 1) {
2929 if (params
->is_noarith
) {
2930 schro_pack_encode_bit (pack
, pred_mode
>> 1);
2932 _schro_arith_encode_bit (arith
, SCHRO_CTX_BLOCK_MODE_REF2
,
2936 if (mv
->pred_mode
!= 0) {
2937 if (params
->have_global_motion
) {
2939 pred
= schro_motion_get_global_prediction (frame
->motion
,
2941 if (params
->is_noarith
) {
2942 schro_pack_encode_bit (pack
, mv
->using_global
^ pred
);
2944 _schro_arith_encode_bit (arith
, SCHRO_CTX_GLOBAL_BLOCK
,
2945 mv
->using_global
^ pred
);
2948 SCHRO_ASSERT (mv
->using_global
== FALSE
);
2956 schro_pack_sync (frame
->pack
);
2957 if (params
->is_noarith
) {
2958 schro_pack_flush (pack
);
2959 schro_pack_encode_uint (frame
->pack
, schro_pack_get_offset (pack
));
2960 schro_pack_sync (frame
->pack
);
2961 schro_pack_append (frame
->pack
, pack
->buffer
->data
,
2962 schro_pack_get_offset (pack
));
2964 schro_arith_flush (arith
);
2965 schro_pack_encode_uint (frame
->pack
, arith
->offset
);
2966 schro_pack_sync (frame
->pack
);
2967 schro_pack_append (frame
->pack
, arith
->buffer
->data
, arith
->offset
);
2968 schro_arith_free (arith
);
2973 schro_encoder_encode_vector_data (SchroEncoderFrame
* frame
, int ref
, int xy
)
2975 SchroParams
*params
= &frame
->params
;
2977 SchroArith
*arith
= NULL
;
2978 int cont
, value
, sign
;
2979 SchroPack b
, *pack
= &b
;
2981 if (params
->is_noarith
) {
2982 schro_pack_encode_init (pack
, frame
->subband_buffer
);
2984 arith
= schro_arith_new ();
2985 schro_arith_encode_init (arith
, frame
->subband_buffer
);
2990 cont
= SCHRO_CTX_MV_REF1_H_CONT_BIN1
;
2991 value
= SCHRO_CTX_MV_REF1_H_VALUE
;
2992 sign
= SCHRO_CTX_MV_REF1_H_SIGN
;
2994 cont
= SCHRO_CTX_MV_REF2_H_CONT_BIN1
;
2995 value
= SCHRO_CTX_MV_REF2_H_VALUE
;
2996 sign
= SCHRO_CTX_MV_REF2_H_SIGN
;
3000 cont
= SCHRO_CTX_MV_REF1_V_CONT_BIN1
;
3001 value
= SCHRO_CTX_MV_REF1_V_VALUE
;
3002 sign
= SCHRO_CTX_MV_REF1_V_SIGN
;
3004 cont
= SCHRO_CTX_MV_REF2_V_CONT_BIN1
;
3005 value
= SCHRO_CTX_MV_REF2_V_VALUE
;
3006 sign
= SCHRO_CTX_MV_REF2_V_SIGN
;
3010 for (j
= 0; j
< params
->y_num_blocks
; j
+= 4) {
3011 for (i
= 0; i
< params
->x_num_blocks
; i
+= 4) {
3013 SchroMotionVector
*mv1
=
3014 &frame
->motion
->motion_vectors
[j
* params
->x_num_blocks
+ i
];
3016 for (l
= 0; l
< 4; l
+= (4 >> mv1
->split
)) {
3017 for (k
= 0; k
< 4; k
+= (4 >> mv1
->split
)) {
3020 SchroMotionVector
*mv
=
3021 &frame
->motion
->motion_vectors
[(j
+ l
) * params
->x_num_blocks
+
3024 if ((mv
->pred_mode
& (1 << ref
)) && !mv
->using_global
) {
3025 schro_motion_vector_prediction (frame
->motion
,
3026 i
+ k
, j
+ l
, &pred_x
, &pred_y
, 1 << ref
);
3027 x
= mv
->u
.vec
.dx
[ref
];
3028 y
= mv
->u
.vec
.dy
[ref
];
3030 if (!params
->is_noarith
) {
3032 _schro_arith_encode_sint (arith
, cont
, value
, sign
, x
- pred_x
);
3034 _schro_arith_encode_sint (arith
, cont
, value
, sign
, y
- pred_y
);
3038 schro_pack_encode_sint (pack
, x
- pred_x
);
3040 schro_pack_encode_sint (pack
, y
- pred_y
);
3049 schro_pack_sync (frame
->pack
);
3050 if (params
->is_noarith
) {
3051 schro_pack_flush (pack
);
3052 schro_pack_encode_uint (frame
->pack
, schro_pack_get_offset (pack
));
3053 schro_pack_sync (frame
->pack
);
3054 schro_pack_append (frame
->pack
, pack
->buffer
->data
,
3055 schro_pack_get_offset (pack
));
3057 schro_arith_flush (arith
);
3058 schro_pack_encode_uint (frame
->pack
, arith
->offset
);
3059 schro_pack_sync (frame
->pack
);
3060 schro_pack_append (frame
->pack
, arith
->buffer
->data
, arith
->offset
);
3061 schro_arith_free (arith
);
3066 schro_encoder_encode_dc_data (SchroEncoderFrame
* frame
, int comp
)
3068 SchroParams
*params
= &frame
->params
;
3070 SchroArith
*arith
= NULL
;
3071 SchroPack b
, *pack
= &b
;
3073 if (params
->is_noarith
) {
3074 schro_pack_encode_init (pack
, frame
->subband_buffer
);
3076 arith
= schro_arith_new ();
3077 schro_arith_encode_init (arith
, frame
->subband_buffer
);
3080 for (j
= 0; j
< params
->y_num_blocks
; j
+= 4) {
3081 for (i
= 0; i
< params
->x_num_blocks
; i
+= 4) {
3083 SchroMotionVector
*mv1
=
3084 &frame
->motion
->motion_vectors
[j
* params
->x_num_blocks
+ i
];
3086 for (l
= 0; l
< 4; l
+= (4 >> mv1
->split
)) {
3087 for (k
= 0; k
< 4; k
+= (4 >> mv1
->split
)) {
3088 SchroMotionVector
*mv
=
3089 &frame
->motion
->motion_vectors
[(j
+ l
) * params
->x_num_blocks
+
3092 if (mv
->pred_mode
== 0) {
3095 schro_motion_dc_prediction (frame
->motion
, i
+ k
, j
+ l
, pred
);
3097 if (!params
->is_noarith
) {
3100 _schro_arith_encode_sint (arith
,
3101 SCHRO_CTX_LUMA_DC_CONT_BIN1
, SCHRO_CTX_LUMA_DC_VALUE
,
3102 SCHRO_CTX_LUMA_DC_SIGN
, mv
->u
.dc
.dc
[0] - pred
[0]);
3105 _schro_arith_encode_sint (arith
,
3106 SCHRO_CTX_CHROMA1_DC_CONT_BIN1
,
3107 SCHRO_CTX_CHROMA1_DC_VALUE
, SCHRO_CTX_CHROMA1_DC_SIGN
,
3108 mv
->u
.dc
.dc
[1] - pred
[1]);
3111 _schro_arith_encode_sint (arith
,
3112 SCHRO_CTX_CHROMA2_DC_CONT_BIN1
,
3113 SCHRO_CTX_CHROMA2_DC_VALUE
, SCHRO_CTX_CHROMA2_DC_SIGN
,
3114 mv
->u
.dc
.dc
[2] - pred
[2]);
3120 schro_pack_encode_sint (pack
, mv
->u
.dc
.dc
[comp
] - pred
[comp
]);
3128 schro_pack_sync (frame
->pack
);
3129 if (params
->is_noarith
) {
3130 schro_pack_flush (pack
);
3131 schro_pack_encode_uint (frame
->pack
, schro_pack_get_offset (pack
));
3132 schro_pack_sync (frame
->pack
);
3133 schro_pack_append (frame
->pack
, pack
->buffer
->data
,
3134 schro_pack_get_offset (pack
));
3136 schro_arith_flush (arith
);
3137 schro_pack_encode_uint (frame
->pack
, arith
->offset
);
3138 schro_pack_sync (frame
->pack
);
3139 schro_pack_append (frame
->pack
, arith
->buffer
->data
, arith
->offset
);
3140 schro_arith_free (arith
);
3146 schro_encoder_encode_sequence_header_header (SchroEncoder
* encoder
,
3149 SchroVideoFormat
*format
= &encoder
->video_format
;
3150 SchroVideoFormat _std_format
;
3151 SchroVideoFormat
*std_format
= &_std_format
;
3154 schro_encoder_encode_parse_info (pack
, SCHRO_PARSE_CODE_SEQUENCE_HEADER
);
3156 /* parse parameters */
3157 schro_pack_encode_uint (pack
, encoder
->version_major
);
3158 schro_pack_encode_uint (pack
, encoder
->version_minor
);
3159 schro_pack_encode_uint (pack
, encoder
->profile
);
3160 schro_pack_encode_uint (pack
, encoder
->level
);
3162 /* sequence parameters */
3163 schro_pack_encode_uint (pack
, encoder
->video_format
.index
);
3164 schro_video_format_set_std_video_format (std_format
,
3165 encoder
->video_format
.index
);
3167 if (std_format
->width
== format
->width
&&
3168 std_format
->height
== format
->height
) {
3169 schro_pack_encode_bit (pack
, FALSE
);
3171 schro_pack_encode_bit (pack
, TRUE
);
3172 schro_pack_encode_uint (pack
, format
->width
);
3173 schro_pack_encode_uint (pack
, format
->height
);
3176 if (std_format
->chroma_format
== format
->chroma_format
) {
3177 schro_pack_encode_bit (pack
, FALSE
);
3179 schro_pack_encode_bit (pack
, TRUE
);
3180 schro_pack_encode_uint (pack
, format
->chroma_format
);
3184 if (std_format
->interlaced
== format
->interlaced
) {
3185 schro_pack_encode_bit (pack
, FALSE
);
3187 schro_pack_encode_bit (pack
, TRUE
);
3188 schro_pack_encode_uint (pack
, format
->interlaced
);
3194 if (std_format
->frame_rate_numerator
== format
->frame_rate_numerator
&&
3195 std_format
->frame_rate_denominator
== format
->frame_rate_denominator
) {
3196 schro_pack_encode_bit (pack
, FALSE
);
3198 schro_pack_encode_bit (pack
, TRUE
);
3199 i
= schro_video_format_get_std_frame_rate (format
);
3200 schro_pack_encode_uint (pack
, i
);
3202 schro_pack_encode_uint (pack
, format
->frame_rate_numerator
);
3203 schro_pack_encode_uint (pack
, format
->frame_rate_denominator
);
3209 /* pixel aspect ratio */
3210 if (std_format
->aspect_ratio_numerator
== format
->aspect_ratio_numerator
&&
3211 std_format
->aspect_ratio_denominator
==
3212 format
->aspect_ratio_denominator
) {
3213 schro_pack_encode_bit (pack
, FALSE
);
3215 schro_pack_encode_bit (pack
, TRUE
);
3216 i
= schro_video_format_get_std_aspect_ratio (format
);
3217 schro_pack_encode_uint (pack
, i
);
3219 schro_pack_encode_uint (pack
, format
->aspect_ratio_numerator
);
3220 schro_pack_encode_uint (pack
, format
->aspect_ratio_denominator
);
3227 if (std_format
->clean_width
== format
->clean_width
&&
3228 std_format
->clean_height
== format
->clean_height
&&
3229 std_format
->left_offset
== format
->left_offset
&&
3230 std_format
->top_offset
== format
->top_offset
) {
3231 schro_pack_encode_bit (pack
, FALSE
);
3233 schro_pack_encode_bit (pack
, TRUE
);
3234 schro_pack_encode_uint (pack
, format
->clean_width
);
3235 schro_pack_encode_uint (pack
, format
->clean_height
);
3236 schro_pack_encode_uint (pack
, format
->left_offset
);
3237 schro_pack_encode_uint (pack
, format
->top_offset
);
3243 if (std_format
->luma_offset
== format
->luma_offset
&&
3244 std_format
->luma_excursion
== format
->luma_excursion
&&
3245 std_format
->chroma_offset
== format
->chroma_offset
&&
3246 std_format
->chroma_excursion
== format
->chroma_excursion
) {
3247 schro_pack_encode_bit (pack
, FALSE
);
3249 schro_pack_encode_bit (pack
, TRUE
);
3250 i
= schro_video_format_get_std_signal_range (format
);
3251 schro_pack_encode_uint (pack
, i
);
3253 schro_pack_encode_uint (pack
, format
->luma_offset
);
3254 schro_pack_encode_uint (pack
, format
->luma_excursion
);
3255 schro_pack_encode_uint (pack
, format
->chroma_offset
);
3256 schro_pack_encode_uint (pack
, format
->chroma_excursion
);
3263 if (std_format
->colour_primaries
== format
->colour_primaries
&&
3264 std_format
->colour_matrix
== format
->colour_matrix
&&
3265 std_format
->transfer_function
== format
->transfer_function
) {
3266 schro_pack_encode_bit (pack
, FALSE
);
3268 schro_pack_encode_bit (pack
, TRUE
);
3269 i
= schro_video_format_get_std_colour_spec (format
);
3270 schro_pack_encode_uint (pack
, i
);
3272 schro_pack_encode_bit (pack
, TRUE
);
3273 schro_pack_encode_uint (pack
, format
->colour_primaries
);
3274 schro_pack_encode_bit (pack
, TRUE
);
3275 schro_pack_encode_uint (pack
, format
->colour_matrix
);
3276 schro_pack_encode_bit (pack
, TRUE
);
3277 schro_pack_encode_uint (pack
, format
->transfer_function
);
3281 /* interlaced coding */
3282 schro_pack_encode_uint (pack
, format
->interlaced_coding
);
3286 schro_pack_sync (pack
);
3290 schro_encoder_encode_parse_info (SchroPack
* pack
, int parse_code
)
3292 /* parse parameters */
3293 schro_pack_encode_bits (pack
, 8, 'B');
3294 schro_pack_encode_bits (pack
, 8, 'B');
3295 schro_pack_encode_bits (pack
, 8, 'C');
3296 schro_pack_encode_bits (pack
, 8, 'D');
3297 schro_pack_encode_bits (pack
, 8, parse_code
);
3300 schro_pack_encode_bits (pack
, 32, 0);
3301 schro_pack_encode_bits (pack
, 32, 0);
3305 schro_encoder_encode_picture_header (SchroEncoderFrame
* frame
)
3307 schro_pack_sync (frame
->pack
);
3308 schro_pack_encode_bits (frame
->pack
, 32, frame
->frame_number
);
3310 SCHRO_DEBUG ("refs %d ref0 %d ref1 %d", frame
->params
.num_refs
,
3311 frame
->picture_number_ref
[0], frame
->picture_number_ref
[1]);
3313 if (frame
->params
.num_refs
> 0) {
3314 schro_pack_encode_sint (frame
->pack
,
3315 (int32_t) (frame
->picture_number_ref
[0] - frame
->frame_number
));
3316 if (frame
->params
.num_refs
> 1) {
3317 schro_pack_encode_sint (frame
->pack
,
3318 (int32_t) (frame
->picture_number_ref
[1] - frame
->frame_number
));
3322 if (frame
->is_ref
) {
3323 if (frame
->retired_picture_number
!= -1) {
3324 schro_pack_encode_sint (frame
->pack
,
3325 (int32_t) (frame
->retired_picture_number
- frame
->frame_number
));
3327 schro_pack_encode_sint (frame
->pack
, 0);
3334 schro_encoder_encode_transform_parameters (SchroEncoderFrame
* frame
)
3336 SchroParams
*params
= &frame
->params
;
3337 SchroPack
*pack
= frame
->pack
;
3339 if (params
->num_refs
> 0) {
3341 schro_pack_encode_bit (pack
, FALSE
);
3345 schro_pack_encode_uint (pack
, params
->wavelet_filter_index
);
3347 /* transform depth */
3348 schro_pack_encode_uint (pack
, params
->transform_depth
);
3350 /* spatial partitioning */
3351 if (!params
->is_lowdelay
) {
3352 if (schro_params_is_default_codeblock (params
)) {
3353 schro_pack_encode_bit (pack
, FALSE
);
3357 schro_pack_encode_bit (pack
, TRUE
);
3358 for (i
= 0; i
< params
->transform_depth
+ 1; i
++) {
3359 schro_pack_encode_uint (pack
, params
->horiz_codeblocks
[i
]);
3360 schro_pack_encode_uint (pack
, params
->vert_codeblocks
[i
]);
3362 schro_pack_encode_uint (pack
, params
->codeblock_mode_index
);
3365 schro_pack_encode_uint (pack
, params
->n_horiz_slices
);
3366 schro_pack_encode_uint (pack
, params
->n_vert_slices
);
3367 schro_pack_encode_uint (pack
, params
->slice_bytes_num
);
3368 schro_pack_encode_uint (pack
, params
->slice_bytes_denom
);
3370 if (schro_params_is_default_quant_matrix (params
)) {
3371 schro_pack_encode_bit (pack
, FALSE
);
3374 schro_pack_encode_bit (pack
, TRUE
);
3375 schro_pack_encode_uint (pack
, params
->quant_matrix
[0]);
3376 for (i
= 0; i
< params
->transform_depth
; i
++) {
3377 schro_pack_encode_uint (pack
, params
->quant_matrix
[1 + 3 * i
]);
3378 schro_pack_encode_uint (pack
, params
->quant_matrix
[2 + 3 * i
]);
3379 schro_pack_encode_uint (pack
, params
->quant_matrix
[3 + 3 * i
]);
3388 schro_encoder_clean_up_transform (SchroEncoderFrame
* frame
)
3392 SchroParams
*params
= &frame
->params
;
3394 for (component
= 0; component
< 3; component
++) {
3395 for (i
= 0; i
< 1 + 3 * params
->transform_depth
; i
++) {
3396 schro_encoder_clean_up_transform_subband (frame
, component
, i
);
3402 schro_encoder_clean_up_transform_subband (SchroEncoderFrame
* frame
,
3403 int component
, int index
)
3405 static const int wavelet_extent
[SCHRO_N_WAVELETS
] = { 2, 1, 2, 0, 0, 4, 2 };
3406 SchroParams
*params
= &frame
->params
;
3413 position
= schro_subband_get_position (index
);
3414 schro_subband_get_frame_data (&fd
, frame
->iwt_frame
, component
,
3417 if (component
== 0) {
3418 schro_video_format_get_picture_luma_size (params
->video_format
, &w
, &h
);
3420 schro_video_format_get_picture_chroma_size (params
->video_format
, &w
, &h
);
3423 h
= MIN (h
+ wavelet_extent
[params
->wavelet_filter_index
], fd
.height
);
3424 w
= MIN (w
+ wavelet_extent
[params
->wavelet_filter_index
], fd
.width
);
3426 if ((SCHRO_FRAME_FORMAT_DEPTH (fd
.format
) ==
3427 SCHRO_FRAME_FORMAT_DEPTH_S16
)) {
3430 for (j
= 0; j
< h
; j
++) {
3431 line
= SCHRO_FRAME_DATA_GET_LINE (&fd
, j
);
3432 for (i
= w
; i
< fd
.width
; i
++) {
3437 for (j
= h
; j
< fd
.height
; j
++) {
3438 line
= SCHRO_FRAME_DATA_GET_LINE (&fd
, j
);
3439 for (i
= 0; i
< fd
.width
; i
++) {
3446 for (j
= 0; j
< h
; j
++) {
3447 line
= SCHRO_FRAME_DATA_GET_LINE (&fd
, j
);
3448 for (i
= w
; i
< fd
.width
; i
++) {
3453 for (j
= h
; j
< fd
.height
; j
++) {
3454 line
= SCHRO_FRAME_DATA_GET_LINE (&fd
, j
);
3455 for (i
= 0; i
< fd
.width
; i
++) {
3463 schro_encoder_encode_transform_data (SchroEncoderFrame
* frame
)
3467 SchroParams
*params
= &frame
->params
;
3469 for (component
= 0; component
< 3; component
++) {
3470 for (i
= 0; i
< 1 + 3 * params
->transform_depth
; i
++) {
3471 schro_pack_sync (frame
->pack
);
3472 frame
->actual_subband_bits
[component
][i
] =
3473 -schro_pack_get_offset (frame
->pack
) * 8;
3474 if (params
->is_noarith
) {
3475 schro_encoder_encode_subband_noarith (frame
, component
, i
);
3477 schro_encoder_encode_subband (frame
, component
, i
);
3479 frame
->actual_subband_bits
[component
][i
] +=
3480 schro_pack_get_offset (frame
->pack
) * 8;
3486 schro_frame_data_quantise (SchroFrameData
* quant_fd
,
3487 SchroFrameData
* fd
, int quant_index
, schro_bool is_intra
)
3491 int16_t *quant_line
;
3492 int quant_factor
= schro_table_quant
[quant_index
];
3494 int quant_shift
= (quant_index
>> 2) + 2;
3495 int real_quant_offset
;
3496 int inv_quant
= schro_table_inverse_quant
[quant_index
];
3499 quant_offset
= schro_table_offset_1_2
[quant_index
];
3501 quant_offset
= schro_table_offset_3_8
[quant_index
];
3503 real_quant_offset
= quant_offset
;
3504 quant_offset
-= quant_factor
>> 1;
3506 if (SCHRO_FRAME_FORMAT_DEPTH (fd
->format
) ==
3507 SCHRO_FRAME_FORMAT_DEPTH_S32
) {
3508 for (j
= 0; j
< fd
->height
; j
++) {
3509 int32_t *line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3510 int32_t *quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3512 schro_quantise_s32 (quant_line
, line
, quant_factor
, real_quant_offset
,
3518 if (quant_index
== 0) {
3519 for (j
= 0; j
< fd
->height
; j
++) {
3520 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3521 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3523 orc_memcpy (quant_line
, line
, fd
->width
* sizeof (int16_t));
3525 } else if ((quant_index
& 3) == 0) {
3526 for (j
= 0; j
< fd
->height
; j
++) {
3527 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3528 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3530 orc_quantdequant2_s16 (quant_line
, line
, quant_shift
,
3531 quant_offset
, quant_factor
, real_quant_offset
+ 2, fd
->width
);
3533 } else if (quant_index
== 3) {
3534 for (j
= 0; j
< fd
->height
; j
++) {
3535 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3536 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3538 orc_quantdequant3_s16 (quant_line
, line
, inv_quant
, quant_offset
,
3539 quant_shift
+ 16, quant_factor
, real_quant_offset
+ 2, 32768,
3543 if (quant_index
> 8)
3545 for (j
= 0; j
< fd
->height
; j
++) {
3546 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3547 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3549 orc_quantdequant1_s16 (quant_line
, line
, inv_quant
, quant_offset
,
3550 quant_shift
, quant_factor
, real_quant_offset
+ 2, fd
->width
);
3557 schro_frame_data_dequantise (SchroFrameData
* fd
,
3558 SchroFrameData
* quant_fd
, int quant_index
, schro_bool is_intra
)
3562 int16_t *quant_line
;
3563 int quant_factor
= schro_table_quant
[quant_index
];
3567 quant_offset
= schro_table_offset_1_2
[quant_index
];
3569 quant_offset
= schro_table_offset_3_8
[quant_index
];
3572 if (quant_index
== 0) {
3573 for (j
= 0; j
< fd
->height
; j
++) {
3574 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3575 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3577 orc_memcpy (line
, quant_line
, fd
->width
* sizeof (int16_t));
3580 for (j
= 0; j
< fd
->height
; j
++) {
3581 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3582 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3584 orc_dequantise_s16 (line
, quant_line
, quant_factor
, quant_offset
+ 2,
3592 schro_frame_data_quantise_dc_predict (SchroFrameData
* quant_fd
,
3593 SchroFrameData
* fd
, int quant_factor
, int quant_offset
, int x
, int y
)
3595 if (SCHRO_FRAME_FORMAT_DEPTH (fd
->format
) ==
3596 SCHRO_FRAME_FORMAT_DEPTH_S16
) {
3600 int16_t *quant_line
;
3602 for (j
= 0; j
< fd
->height
; j
++) {
3603 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3604 prev_line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
- 1);
3605 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3607 for (i
= 0; i
< fd
->width
; i
++) {
3613 pred_value
= schro_divide3 (line
[i
- 1] +
3614 prev_line
[i
] + prev_line
[i
- 1] + 1);
3616 pred_value
= prev_line
[i
];
3620 pred_value
= line
[i
- 1];
3626 q
= schro_quantise (line
[i
] - pred_value
, quant_factor
, quant_offset
);
3627 line
[i
] = schro_dequantise (q
, quant_factor
, quant_offset
) + pred_value
;
3635 int32_t *quant_line
;
3637 for (j
= 0; j
< fd
->height
; j
++) {
3638 line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
3639 prev_line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
- 1);
3640 quant_line
= SCHRO_FRAME_DATA_GET_LINE (quant_fd
, j
);
3642 for (i
= 0; i
< fd
->width
; i
++) {
3648 pred_value
= schro_divide (line
[i
- 1] +
3649 prev_line
[i
] + prev_line
[i
- 1] + 1, 3);
3651 pred_value
= prev_line
[i
];
3655 pred_value
= line
[i
- 1];
3661 q
= schro_quantise (line
[i
] - pred_value
, quant_factor
, quant_offset
);
3662 line
[i
] = schro_dequantise (q
, quant_factor
, quant_offset
) + pred_value
;
3670 schro_encoder_frame_get_quant_index (SchroEncoderFrame
* frame
, int component
,
3671 int index
, int x
, int y
)
3673 SchroParams
*params
= &frame
->params
;
3675 int horiz_codeblocks
;
3676 int *codeblock_quants
;
3678 position
= schro_subband_get_position (index
);
3680 params
->horiz_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
3682 codeblock_quants
= frame
->quant_indices
[component
][index
];
3684 SCHRO_ASSERT (codeblock_quants
);
3686 return codeblock_quants
[y
* horiz_codeblocks
+ x
];
3690 schro_encoder_frame_set_quant_index (SchroEncoderFrame
* frame
, int component
,
3691 int index
, int x
, int y
, int quant_index
)
3693 SchroParams
*params
= &frame
->params
;
3694 int *codeblock_quants
;
3696 int horiz_codeblocks
;
3697 int vert_codeblocks
;
3701 position
= schro_subband_get_position (index
);
3703 params
->horiz_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
3704 vert_codeblocks
= params
->vert_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
3706 SCHRO_ASSERT (horiz_codeblocks
> 0);
3707 SCHRO_ASSERT (vert_codeblocks
> 0);
3708 SCHRO_ASSERT (x
< horiz_codeblocks
);
3709 SCHRO_ASSERT (y
< vert_codeblocks
);
3711 if (frame
->quant_indices
[component
][index
] == NULL
) {
3712 frame
->quant_indices
[component
][index
] =
3713 schro_malloc (horiz_codeblocks
* vert_codeblocks
* sizeof (int));
3718 codeblock_quants
= frame
->quant_indices
[component
][index
];
3720 if (x
< 0 || y
< 0) {
3721 for (i
= 0; i
< horiz_codeblocks
* vert_codeblocks
; i
++) {
3722 codeblock_quants
[i
] = quant_index
;
3725 codeblock_quants
[x
+ y
* horiz_codeblocks
] = quant_index
;
3730 schro_encoder_quantise_subband (SchroEncoderFrame
* frame
, int component
,
3739 SchroParams
*params
= &frame
->params
;
3740 int horiz_codeblocks
;
3741 int vert_codeblocks
;
3744 position
= schro_subband_get_position (index
);
3745 schro_subband_get_frame_data (&fd
, frame
->iwt_frame
, component
,
3747 schro_subband_get_frame_data (&qd
, frame
->quant_frame
, component
,
3751 params
->horiz_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
3752 vert_codeblocks
= params
->vert_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
3754 for (y
= 0; y
< vert_codeblocks
; y
++) {
3756 for (x
= 0; x
< horiz_codeblocks
; x
++) {
3757 SchroFrameData quant_cb
;
3760 quant_index
= schro_encoder_frame_get_quant_index (frame
, component
,
3762 quant_factor
= schro_table_quant
[quant_index
];
3763 if (params
->num_refs
> 0) {
3764 quant_offset
= schro_table_offset_3_8
[quant_index
];
3766 quant_offset
= schro_table_offset_1_2
[quant_index
];
3769 schro_frame_data_get_codeblock (&cb
, &fd
, x
, y
, horiz_codeblocks
,
3771 schro_frame_data_get_codeblock (&quant_cb
, &qd
, x
, y
, horiz_codeblocks
,
3774 if (params
->num_refs
== 0 && index
== 0) {
3775 schro_frame_data_quantise_dc_predict (&quant_cb
, &cb
, quant_factor
,
3776 quant_offset
, x
, y
);
3778 schro_frame_data_quantise (&quant_cb
, &cb
, quant_index
,
3779 (params
->num_refs
== 0));
3784 return schro_frame_data_is_zero (&qd
);
3788 schro_frame_data_clear (SchroFrameData
* fd
)
3792 for (i
= 0; i
< fd
->height
; i
++) {
3793 orc_splat_s16_ns (SCHRO_FRAME_DATA_GET_LINE (fd
, i
), 0, fd
->width
);
3798 schro_encoder_encode_subband (SchroEncoderFrame
* frame
, int component
,
3801 SchroParams
*params
= &frame
->params
;
3804 int subband_zero_flag
;
3806 int horiz_codeblocks
;
3807 int vert_codeblocks
;
3808 int have_zero_flags
;
3809 int have_quant_offset
;
3813 SchroFrameData parent_fd
;
3815 int n_subbands_left
;
3816 int first_quant_index
;
3818 position
= schro_subband_get_position (index
);
3819 schro_subband_get_frame_data (&fd
, frame
->iwt_frame
, component
,
3821 schro_subband_get_frame_data (&qd
, frame
->quant_frame
, component
,
3824 if (position
>= 4) {
3825 schro_subband_get_frame_data (&parent_fd
, frame
->iwt_frame
, component
,
3826 position
- 4, params
);
3828 parent_fd
.data
= NULL
;
3829 parent_fd
.stride
= 0;
3832 arith
= schro_arith_new ();
3833 schro_arith_encode_init (arith
, frame
->subband_buffer
);
3835 subband_zero_flag
= schro_encoder_quantise_subband (frame
, component
, index
);
3837 if (subband_zero_flag
) {
3838 SCHRO_DEBUG ("subband is zero");
3839 schro_pack_encode_uint (frame
->pack
, 0);
3840 schro_arith_free (arith
);
3845 horiz_codeblocks
= params
->horiz_codeblocks
[0];
3846 vert_codeblocks
= params
->vert_codeblocks
[0];
3849 params
->horiz_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
3851 params
->vert_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
3853 if (horiz_codeblocks
> 1 || vert_codeblocks
> 1) {
3854 have_zero_flags
= TRUE
;
3856 have_zero_flags
= FALSE
;
3858 if (params
->codeblock_mode_index
== 1) {
3859 have_quant_offset
= TRUE
;
3861 have_quant_offset
= FALSE
;
3864 first_quant_index
= -1;
3866 for (y
= 0; y
< vert_codeblocks
; y
++) {
3867 int ymin
= (fd
.height
* y
) / vert_codeblocks
;
3868 int ymax
= (fd
.height
* (y
+ 1)) / vert_codeblocks
;
3870 for (x
= 0; x
< horiz_codeblocks
; x
++) {
3871 int xmin
= (fd
.width
* x
) / horiz_codeblocks
;
3872 int xmax
= (fd
.width
* (x
+ 1)) / horiz_codeblocks
;
3873 SchroFrameData quant_cb
;
3876 schro_frame_data_get_codeblock (&cb
, &fd
, x
, y
, horiz_codeblocks
,
3878 schro_frame_data_get_codeblock (&quant_cb
, &qd
, x
, y
, horiz_codeblocks
,
3881 if (have_zero_flags
) {
3882 int zero_codeblock
= schro_frame_data_is_zero (&quant_cb
);
3884 _schro_arith_encode_bit (arith
, SCHRO_CTX_ZERO_CODEBLOCK
,
3886 if (zero_codeblock
) {
3891 if (have_quant_offset
) {
3892 int new_quant_index
;
3894 new_quant_index
= schro_encoder_frame_get_quant_index (frame
,
3895 component
, index
, x
, y
);
3896 if (first_quant_index
== -1) {
3897 quant_index
= new_quant_index
;
3898 first_quant_index
= new_quant_index
;
3901 _schro_arith_encode_sint (arith
,
3902 SCHRO_CTX_QUANTISER_CONT
, SCHRO_CTX_QUANTISER_VALUE
,
3903 SCHRO_CTX_QUANTISER_SIGN
, new_quant_index
- quant_index
);
3905 quant_index
= new_quant_index
;
3908 for (j
= ymin
; j
< ymax
; j
++) {
3909 int16_t *prev_quant_line
= SCHRO_FRAME_DATA_GET_LINE (&qd
, j
- 1);
3910 int16_t *quant_line
= SCHRO_FRAME_DATA_GET_LINE (&qd
, j
);
3911 int16_t *parent_line
= SCHRO_FRAME_DATA_GET_LINE (&parent_fd
, (j
>> 1));
3913 #define STUFF(have_parent,is_horiz,is_vert) \
3914 for(i=xmin;i<xmax;i++){ \
3917 int value_context; \
3919 int previous_value; \
3922 if (have_parent) { \
3923 parent = parent_line[(i>>1)]; \
3930 nhood_or |= prev_quant_line[i]; \
3933 nhood_or |= quant_line[i - 1]; \
3936 nhood_or |= prev_quant_line[i - 1]; \
3939 previous_value = 0; \
3942 previous_value = quant_line[i - 1]; \
3944 } else if (is_vert) { \
3946 previous_value = prev_quant_line[i]; \
3950 if (previous_value < 0) { \
3951 sign_context = SCHRO_CTX_SIGN_NEG; \
3953 if (previous_value > 0) { \
3954 sign_context = SCHRO_CTX_SIGN_POS; \
3956 sign_context = SCHRO_CTX_SIGN_ZERO; \
3960 if (parent == 0) { \
3961 if (nhood_or == 0) { \
3962 cont_context = SCHRO_CTX_ZPZN_F1; \
3964 cont_context = SCHRO_CTX_ZPNN_F1; \
3967 if (nhood_or == 0) { \
3968 cont_context = SCHRO_CTX_NPZN_F1; \
3970 cont_context = SCHRO_CTX_NPNN_F1; \
3974 value_context = SCHRO_CTX_COEFF_DATA; \
3976 _schro_arith_encode_sint (arith, cont_context, value_context, \
3977 sign_context, quant_line[i]); \
3980 if (position
>= 4) {
3981 if (SCHRO_SUBBAND_IS_HORIZONTALLY_ORIENTED (position
)) {
3982 STUFF (TRUE
, TRUE
, FALSE
);
3983 } else if (SCHRO_SUBBAND_IS_VERTICALLY_ORIENTED (position
)) {
3984 STUFF (TRUE
, FALSE
, TRUE
);
3986 STUFF (TRUE
, FALSE
, FALSE
);
3989 if (SCHRO_SUBBAND_IS_HORIZONTALLY_ORIENTED (position
)) {
3990 STUFF (FALSE
, TRUE
, FALSE
);
3991 } else if (SCHRO_SUBBAND_IS_VERTICALLY_ORIENTED (position
)) {
3992 STUFF (FALSE
, FALSE
, TRUE
);
3994 STUFF (FALSE
, FALSE
, FALSE
);
4001 schro_arith_flush (arith
);
4003 SCHRO_ASSERT (arith
->offset
< frame
->subband_buffer
->length
);
4005 schro_dump (SCHRO_DUMP_SUBBAND_EST
, "%d %d %d %g %d\n",
4006 frame
->frame_number
, component
, index
,
4008 est_entropy
[component
][index
][frame
->quant_indices
[component
][index
][0]],
4011 n_subbands_left
= (3 - component
) * (1 + 3 * params
->transform_depth
) - index
;
4012 if ((schro_pack_get_offset (frame
->pack
) + arith
->offset
+
4013 n_subbands_left
+ 4) * 8 > frame
->hard_limit_bits
) {
4014 SCHRO_ERROR ("skipping comp=%d subband=%d, too big (%d+%d+%d+32 > %d)",
4016 schro_pack_get_offset (frame
->pack
) * 8, arith
->offset
* 8,
4017 n_subbands_left
* 8, frame
->hard_limit_bits
);
4019 schro_pack_encode_uint (frame
->pack
, 0);
4021 schro_frame_data_clear (&fd
);
4023 SCHRO_DEBUG ("appending comp=%d subband=%d, (%d+%d+%d+32 <= %d)",
4025 schro_pack_get_offset (frame
->pack
) * 8, arith
->offset
* 8,
4026 n_subbands_left
* 8, frame
->hard_limit_bits
);
4027 schro_pack_encode_uint (frame
->pack
, arith
->offset
);
4028 if (first_quant_index
== -1) {
4030 schro_encoder_frame_get_quant_index (frame
, component
, index
, 0, 0);
4032 if (arith
->offset
> 0) {
4033 schro_pack_encode_uint (frame
->pack
, first_quant_index
);
4035 schro_pack_sync (frame
->pack
);
4037 schro_pack_append (frame
->pack
, arith
->buffer
->data
, arith
->offset
);
4040 schro_arith_free (arith
);
4044 schro_frame_data_is_zero (SchroFrameData
* fd
)
4049 if (SCHRO_FRAME_FORMAT_DEPTH (fd
->format
) ==
4050 SCHRO_FRAME_FORMAT_DEPTH_S32
) {
4051 for (j
= 0; j
< fd
->height
; j
++) {
4052 int32_t *line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
4054 for (i
=0; i
< fd
->width
; i
++) {
4055 if (line
[i
] != 0) return FALSE
;
4059 for (j
= 0; j
< fd
->height
; j
++) {
4060 int16_t *line
= SCHRO_FRAME_DATA_GET_LINE (fd
, j
);
4061 /* FIXME this could theoretically cause false positives. Fix when
4062 * Orc gets an accorw opcode. */
4063 orc_accw (&acc
, line
, fd
->width
);
4073 schro_encoder_encode_subband_noarith (SchroEncoderFrame
* frame
,
4074 int component
, int index
)
4076 SchroParams
*params
= &frame
->params
;
4078 SchroPack
*pack
= &b
;
4080 int subband_zero_flag
;
4082 int horiz_codeblocks
;
4083 int vert_codeblocks
;
4084 int have_zero_flags
;
4085 int have_quant_offset
;
4091 position
= schro_subband_get_position (index
);
4092 schro_subband_get_frame_data (&fd
, frame
->iwt_frame
, component
,
4094 schro_subband_get_frame_data (&qd
, frame
->quant_frame
, component
,
4097 deep
= (SCHRO_FRAME_FORMAT_DEPTH (fd
.format
) ==
4098 SCHRO_FRAME_FORMAT_DEPTH_S32
);
4101 subband_zero_flag
= schro_encoder_quantise_subband (frame
, component
, index
);
4103 subband_zero_flag
= schro_encoder_quantise_subband (frame
, component
, index
);
4106 if (subband_zero_flag
) {
4107 SCHRO_DEBUG ("subband is zero");
4108 schro_pack_encode_uint (frame
->pack
, 0);
4112 schro_pack_encode_init (pack
, frame
->subband_buffer
);
4115 horiz_codeblocks
= params
->horiz_codeblocks
[0];
4116 vert_codeblocks
= params
->vert_codeblocks
[0];
4119 params
->horiz_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
4121 params
->vert_codeblocks
[SCHRO_SUBBAND_SHIFT (position
) + 1];
4123 if ((horiz_codeblocks
> 1 || vert_codeblocks
> 1) && index
> 0) {
4124 have_zero_flags
= TRUE
;
4126 have_zero_flags
= FALSE
;
4128 if (horiz_codeblocks
> 1 || vert_codeblocks
> 1) {
4129 if (params
->codeblock_mode_index
== 1) {
4130 have_quant_offset
= TRUE
;
4132 have_quant_offset
= FALSE
;
4135 have_quant_offset
= FALSE
;
4138 for (y
= 0; y
< vert_codeblocks
; y
++) {
4139 for (x
= 0; x
< horiz_codeblocks
; x
++) {
4142 schro_frame_data_get_codeblock (&cb
, &qd
, x
, y
, horiz_codeblocks
,
4145 if (have_zero_flags
) {
4146 int zero_codeblock
= schro_frame_data_is_zero (&cb
);
4147 schro_pack_encode_bit (pack
, zero_codeblock
);
4148 if (zero_codeblock
) {
4153 if (have_quant_offset
) {
4154 schro_pack_encode_sint (pack
, 0);
4158 for (j
= 0; j
< cb
.height
; j
++) {
4159 int32_t *quant_line
= SCHRO_FRAME_DATA_GET_LINE (&cb
, j
);
4160 for (i
= 0; i
< cb
.width
; i
++) {
4161 schro_pack_encode_sint (pack
, quant_line
[i
]);
4165 for (j
= 0; j
< cb
.height
; j
++) {
4166 int16_t *quant_line
= SCHRO_FRAME_DATA_GET_LINE (&cb
, j
);
4167 for (i
= 0; i
< cb
.width
; i
++) {
4168 schro_pack_encode_sint (pack
, quant_line
[i
]);
4174 schro_pack_flush (pack
);
4176 SCHRO_ASSERT (schro_pack_get_offset (pack
) < frame
->subband_buffer
->length
);
4178 schro_dump (SCHRO_DUMP_SUBBAND_EST
, "%d %d %d %d %d\n",
4179 frame
->frame_number
, component
, index
,
4180 frame
->estimated_residual_bits
, schro_pack_get_offset (pack
) * 8);
4182 schro_pack_encode_uint (frame
->pack
, schro_pack_get_offset (pack
));
4183 if (schro_pack_get_offset (pack
) > 0) {
4184 schro_pack_encode_uint (frame
->pack
,
4185 schro_encoder_frame_get_quant_index (frame
, component
, index
, 0, 0));
4187 schro_pack_sync (frame
->pack
);
4188 schro_pack_append (frame
->pack
, pack
->buffer
->data
,
4189 schro_pack_get_offset (pack
));
4197 schro_encoder_frame_new (SchroEncoder
* encoder
)
4199 SchroEncoderFrame
*encoder_frame
;
4200 SchroFrameFormat frame_format
;
4201 int iwt_width
, iwt_height
;
4206 encoder_frame
= schro_malloc0 (sizeof (SchroEncoderFrame
));
4207 for (i
= 0; i
< SCHRO_ENCODER_FRAME_STAGE_LAST
; i
++) {
4208 encoder_frame
->stages
[i
].is_needed
= TRUE
;
4210 encoder_frame
->refcount
= 1;
4212 encoder_frame
->sc_mad
= -1;
4213 encoder_frame
->sc_threshold
= -1.;
4214 encoder_frame
->scene_change_score
= -1.;
4216 frame_format
= schro_params_get_frame_format (encoder
->intermediate_frame_depth
,
4217 encoder
->video_format
.chroma_format
);
4219 schro_video_format_get_iwt_alloc_size (&encoder
->video_format
,
4220 &iwt_width
, &iwt_height
, encoder
->transform_depth
);
4221 encoder_frame
->iwt_frame
= schro_frame_new_and_alloc (NULL
, frame_format
,
4222 iwt_width
, iwt_height
);
4223 encoder_frame
->quant_frame
= schro_frame_new_and_alloc (NULL
, frame_format
,
4224 iwt_width
, iwt_height
);
4226 schro_video_format_get_picture_luma_size (&encoder
->video_format
,
4227 &picture_width
, &picture_height
);
4228 encoder_frame
->prediction_frame
=
4229 schro_frame_new_and_alloc (NULL
, frame_format
, picture_width
,
4232 encoder_frame
->inserted_buffers
=
4233 schro_list_new_full ((SchroListFreeFunc
) schro_buffer_unref
, NULL
);
4235 encoder_frame
->retired_picture_number
= -1;
4237 return encoder_frame
;
4241 schro_encoder_frame_ref (SchroEncoderFrame
* frame
)
4243 SCHRO_ASSERT (frame
&& frame
->refcount
> 0);
4248 schro_encoder_frame_unref (SchroEncoderFrame
* frame
)
4253 if (frame
->refcount
== 0) {
4254 if (frame
->previous_frame
) {
4255 schro_encoder_frame_unref (frame
->previous_frame
);
4257 if (frame
->original_frame
) {
4258 schro_frame_unref (frame
->original_frame
);
4260 if (frame
->filtered_frame
) {
4261 schro_frame_unref (frame
->filtered_frame
);
4263 if (frame
->reconstructed_frame
) {
4264 schro_frame_unref (frame
->reconstructed_frame
);
4267 if (frame
->upsampled_original_frame
) {
4268 schro_frame_unref (frame
->upsampled_original_frame
);
4271 for (i
= 0; i
< 2; i
++) {
4273 schro_motion_field_free (frame
->mf
[i
]);
4278 for (i
= 0; i
< frame
->encoder
->downsample_levels
; i
++) {
4279 if (frame
->downsampled_frames
[i
]) {
4280 schro_frame_unref (frame
->downsampled_frames
[i
]);
4283 if (frame
->iwt_frame
) {
4284 schro_frame_unref (frame
->iwt_frame
);
4286 if (frame
->prediction_frame
) {
4287 schro_frame_unref (frame
->prediction_frame
);
4289 if (frame
->motion
) {
4290 schro_motion_free (frame
->motion
);
4293 schro_list_free (frame
->inserted_buffers
);
4294 if (frame
->output_buffer
) {
4295 schro_buffer_unref (frame
->output_buffer
);
4297 if (frame
->sequence_header_buffer
) {
4298 schro_buffer_unref (frame
->sequence_header_buffer
);
4302 schro_motionest_free (frame
->me
);
4305 schro_rough_me_free (frame
->rme
[0]);
4307 schro_rough_me_free (frame
->rme
[1]);
4308 if (frame
->hier_bm
[0])
4309 schro_hbm_unref (frame
->hier_bm
[0]);
4310 frame
->hier_bm
[0] = NULL
;
4311 if (frame
->hier_bm
[1])
4312 schro_hbm_unref (frame
->hier_bm
[1]);
4313 frame
->hier_bm
[1] = NULL
;
4315 schro_me_free (frame
->deep_me
);
4316 frame
->deep_me
= NULL
;
4317 if (frame
->phasecorr
[0])
4318 schro_phasecorr_free (frame
->phasecorr
[0]);
4319 if (frame
->phasecorr
[1])
4320 schro_phasecorr_free (frame
->phasecorr
[1]);
4322 for (i
= 0; i
< SCHRO_LIMIT_SUBBANDS
; i
++) {
4323 if (frame
->quant_indices
[0][i
])
4324 schro_free (frame
->quant_indices
[0][i
]);
4325 if (frame
->quant_indices
[1][i
])
4326 schro_free (frame
->quant_indices
[1][i
]);
4327 if (frame
->quant_indices
[2][i
])
4328 schro_free (frame
->quant_indices
[2][i
]);
4337 /* reference pool */
4340 schro_encoder_reference_add (SchroEncoder
* encoder
, SchroEncoderFrame
* frame
)
4342 if (schro_queue_is_full (encoder
->reference_queue
)) {
4343 schro_queue_pop (encoder
->reference_queue
);
4346 SCHRO_DEBUG ("adding reference %p %d", frame
, frame
->frame_number
);
4348 schro_encoder_frame_ref (frame
);
4349 schro_queue_add (encoder
->reference_queue
, frame
, frame
->frame_number
);
4354 schro_encoder_reference_get (SchroEncoder
* encoder
,
4355 SchroPictureNumber frame_number
)
4358 for (i
= 0; i
< SCHRO_LIMIT_REFERENCE_FRAMES
; i
++) {
4359 if (encoder
->reference_pictures
[i
] &&
4360 encoder
->reference_pictures
[i
]->frame_number
== frame_number
) {
4361 return encoder
->reference_pictures
[i
];
4369 #define ENUM(name,list,def) \
4370 {{#name, SCHRO_ENCODER_SETTING_TYPE_ENUM, 0, ARRAY_SIZE(list)-1, def, list}, offsetof(SchroEncoder, name)}
4371 #define INT(name,min,max,def) \
4372 {{#name, SCHRO_ENCODER_SETTING_TYPE_INT, min, max, def}, offsetof(SchroEncoder, name)}
4373 #define BOOL(name,def) \
4374 {{#name, SCHRO_ENCODER_SETTING_TYPE_BOOLEAN, 0, 1, def}, offsetof(SchroEncoder, name)}
4375 #define DOUB(name,min,max,def) \
4376 {{#name, SCHRO_ENCODER_SETTING_TYPE_DOUBLE, min, max, def}, offsetof(SchroEncoder, name)}
4378 static const char *rate_control_list
[] = {
4379 "constant_noise_threshold",
4388 static const char *profile_list
[] = {
4396 static const char *gop_structure_list
[] = {
4405 static const char *perceptual_weighting_list
[] = {
4412 static const char *filtering_list
[] = {
4414 "center_weighted_median",
4417 "adaptive_gaussian",
4421 static const char *wavelet_list
[] = {
4431 static const char *block_size_list
[] = {
4438 static const char *codeblock_size_list
[] = {
4446 static const char *block_overlap_list
[] = {
4454 #define INT_MAX 2147483647
4457 struct SchroEncoderSettings
4459 SchroEncoderSetting s
;
4461 } static const encoder_settings
[] = {
4462 ENUM (rate_control
, rate_control_list
, 6),
4463 INT (bitrate
, 0, INT_MAX
, 0),
4464 INT (max_bitrate
, 0, INT_MAX
, 13824000),
4465 INT (min_bitrate
, 0, INT_MAX
, 13824000),
4466 INT (buffer_size
, 0, INT_MAX
, 0),
4467 INT (buffer_level
, 0, INT_MAX
, 0),
4468 DOUB (quality
, 0, 10.0, 5.0),
4469 DOUB (noise_threshold
, 0, 100.0, 25.0),
4470 ENUM (gop_structure
, gop_structure_list
, 0),
4471 INT (queue_depth
, 1, SCHRO_LIMIT_FRAME_QUEUE_LENGTH
, 20),
4472 ENUM (perceptual_weighting
, perceptual_weighting_list
, 1),
4473 DOUB (perceptual_distance
, 0, 100.0, 4.0),
4474 ENUM (filtering
, filtering_list
, 0),
4475 DOUB (filter_value
, 0, 100.0, 5.0),
4476 INT (profile
, 0, 0, 0),
4477 ENUM (force_profile
, profile_list
, 0),
4478 INT (level
, 0, 0, 0),
4479 INT (max_refs
, 1, 4, 3),
4480 BOOL (open_gop
, TRUE
),
4481 INT (au_distance
, 1, INT_MAX
, 120),
4482 BOOL (enable_psnr
, FALSE
),
4483 BOOL (enable_ssim
, FALSE
),
4485 INT (transform_depth
, 0, SCHRO_LIMIT_ENCODER_TRANSFORM_DEPTH
, 3),
4486 ENUM (intra_wavelet
, wavelet_list
, SCHRO_WAVELET_DESLAURIERS_DUBUC_9_7
),
4487 ENUM (inter_wavelet
, wavelet_list
, SCHRO_WAVELET_DESLAURIERS_DUBUC_9_7
),
4488 INT (mv_precision
, 0, 3, 0),
4489 INT (downsample_levels
, 2, 8, 5),
4490 ENUM (motion_block_size
, block_size_list
, 0),
4491 ENUM (motion_block_overlap
, block_overlap_list
, 0),
4492 BOOL (interlaced_coding
, FALSE
),
4493 BOOL (enable_internal_testing
, FALSE
),
4494 BOOL (enable_noarith
, FALSE
),
4495 BOOL (enable_md5
, FALSE
),
4496 BOOL (enable_fullscan_estimation
, FALSE
),
4497 BOOL (enable_hierarchical_estimation
, TRUE
),
4498 BOOL (enable_zero_estimation
, FALSE
),
4499 BOOL (enable_phasecorr_estimation
, FALSE
),
4500 BOOL (enable_bigblock_estimation
, TRUE
),
4501 BOOL (enable_multiquant
, FALSE
),
4502 BOOL (enable_dc_multiquant
, FALSE
),
4503 BOOL (enable_global_motion
, FALSE
),
4504 BOOL (enable_scene_change_detection
, TRUE
),
4505 BOOL (enable_deep_estimation
, TRUE
),
4506 BOOL (enable_rdo_cbr
, TRUE
),
4507 BOOL (enable_chroma_me
, FALSE
),
4508 INT (horiz_slices
, 0, INT_MAX
, 0),
4509 INT (vert_slices
, 0, INT_MAX
, 0),
4510 ENUM (codeblock_size
, codeblock_size_list
, 0),
4512 DOUB (magic_dc_metric_offset
, 0.0, 1000.0, 1.0),
4513 DOUB (magic_subband0_lambda_scale
, 0.0, 1000.0, 10.0),
4514 DOUB (magic_chroma_lambda_scale
, 0.0, 1000.0, 0.1),
4515 DOUB (magic_nonref_lambda_scale
, 0.0, 1000.0, 0.01),
4516 DOUB (magic_me_lambda_scale
, 0.0, 100.0, 1.0),
4517 DOUB (magic_I_lambda_scale
, 0.0, 100.0, 1.0),
4518 DOUB (magic_P_lambda_scale
, 0.0, 10.0, 0.25),
4519 DOUB (magic_B_lambda_scale
, 0.0, 10.0, 0.01),
4520 DOUB (magic_allocation_scale
, 0.0, 1000.0, 1.1),
4521 DOUB (magic_inter_cpd_scale
, 0.0, 1.0, 1.0),
4522 DOUB (magic_keyframe_weight
, 0.0, 1000.0, 7.5),
4523 DOUB (magic_scene_change_threshold
, 0.0, 1000.0, 3.0),
4524 DOUB (magic_inter_p_weight
, 0.0, 1000.0, 1.5),
4525 DOUB (magic_inter_b_weight
, 0.0, 1000.0, 0.2),
4526 DOUB (magic_me_bailout_limit
, 0.0, 1000.0, 0.33),
4527 DOUB (magic_bailout_weight
, 0.0, 1000.0, 4.0),
4528 DOUB (magic_error_power
, 0.0, 1000.0, 4.0),
4529 DOUB (magic_subgroup_length
, 1.0, 10.0, 4.0),
4530 DOUB (magic_badblock_multiplier_nonref
, 0.0, 1000.0, 4.0),
4531 DOUB (magic_badblock_multiplier_ref
, 0.0, 1000.0, 8.0),
4532 DOUB (magic_block_search_threshold
, 0.0, 1000.0, 15.0),
4533 DOUB (magic_scan_distance
, 0.0, 1000.0, 4.0),
4534 DOUB (magic_diagonal_lambda_scale
, 0.0, 1000.0, 1.0),
4538 schro_encoder_get_n_settings (void)
4540 return ARRAY_SIZE (encoder_settings
);
4543 const SchroEncoderSetting
*
4544 schro_encoder_get_setting_info (int i
)
4546 if (i
>= 0 && i
< ARRAY_SIZE (encoder_settings
)) {
4547 return &encoder_settings
[i
].s
;
4553 * schro_encoder_setting_set_defaults:
4554 * @encoder: an encoder structure
4556 * set the encoder options to the defaults advertised through
4557 * schro_encoder_get_setting_info. old settings are lost.
4560 schro_encoder_setting_set_defaults (SchroEncoder
* encoder
)
4563 for (i
= 0; i
< ARRAY_SIZE (encoder_settings
); i
++) {
4564 switch (encoder_settings
[i
].s
.type
) {
4565 case SCHRO_ENCODER_SETTING_TYPE_BOOLEAN
:
4566 case SCHRO_ENCODER_SETTING_TYPE_INT
:
4567 case SCHRO_ENCODER_SETTING_TYPE_ENUM
:
4568 *(int *) SCHRO_OFFSET (encoder
, encoder_settings
[i
].offset
) =
4569 encoder_settings
[i
].s
.default_value
;
4571 case SCHRO_ENCODER_SETTING_TYPE_DOUBLE
:
4572 *(double *) SCHRO_OFFSET (encoder
, encoder_settings
[i
].offset
) =
4573 encoder_settings
[i
].s
.default_value
;
4582 * schro_encoder_setting_set_double:
4583 * @encoder: an encoder object
4585 * set the encoder option given by @name to @value.
4588 schro_encoder_setting_set_double (SchroEncoder
* encoder
, const char *name
,
4592 for (i
= 0; i
< ARRAY_SIZE (encoder_settings
); i
++) {
4593 if (strcmp (name
, encoder_settings
[i
].s
.name
)) {
4596 switch (encoder_settings
[i
].s
.type
) {
4597 case SCHRO_ENCODER_SETTING_TYPE_BOOLEAN
:
4598 case SCHRO_ENCODER_SETTING_TYPE_INT
:
4599 case SCHRO_ENCODER_SETTING_TYPE_ENUM
:
4600 *(int *) SCHRO_OFFSET (encoder
, encoder_settings
[i
].offset
) = value
;
4602 case SCHRO_ENCODER_SETTING_TYPE_DOUBLE
:
4603 *(double *) SCHRO_OFFSET (encoder
, encoder_settings
[i
].offset
) = value
;
4612 * schro_encoder_setting_get_double:
4613 * @encoder: an encoder object
4615 * Returns: the current value of an encoder option given by @name
4618 schro_encoder_setting_get_double (SchroEncoder
* encoder
, const char *name
)
4621 for (i
= 0; i
< ARRAY_SIZE (encoder_settings
); i
++) {
4622 if (strcmp (name
, encoder_settings
[i
].s
.name
)) {
4625 switch (encoder_settings
[i
].s
.type
) {
4626 case SCHRO_ENCODER_SETTING_TYPE_BOOLEAN
:
4627 case SCHRO_ENCODER_SETTING_TYPE_INT
:
4628 case SCHRO_ENCODER_SETTING_TYPE_ENUM
:
4629 return *(int *) SCHRO_OFFSET (encoder
, encoder_settings
[i
].offset
);
4630 case SCHRO_ENCODER_SETTING_TYPE_DOUBLE
:
4631 return *(double *) SCHRO_OFFSET (encoder
, encoder_settings
[i
].offset
);