1 /*****************************************************************************
2 * schroedinger.c: Dirac decoder module making use of libschroedinger.
3 * (http://www.bbc.co.uk/rd/projects/dirac/index.shtml)
4 * (http://diracvideo.org)
5 *****************************************************************************
6 * Copyright (C) 2008-2011 the VideoLAN team
8 * Authors: Jonathan Rosser <jonathan.rosser@gmail.com>
9 * David Flynn <davidf at rd dot bbc.co.uk>
10 * Anuradha Suraparaju <asuraparaju at gmail dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_codec.h> /* decoder_DeletePicture */
41 #include <schroedinger/schro.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenDecoder ( vlc_object_t * );
47 static void CloseDecoder ( vlc_object_t * );
48 static int OpenEncoder ( vlc_object_t * );
49 static void CloseEncoder ( vlc_object_t * );
51 #define ENC_CFG_PREFIX "sout-schro-"
53 #define ENC_CHROMAFMT "chroma-fmt"
54 #define ENC_CHROMAFMT_TEXT N_("Chroma format")
55 #define ENC_CHROMAFMT_LONGTEXT N_("Picking chroma format will force a " \
56 "conversion of the video into that format")
57 static const char *const enc_chromafmt_list[] =
58 { "420", "422", "444" };
59 static const char *const enc_chromafmt_list_text[] =
60 { N_("4:2:0"), N_("4:2:2"), N_("4:4:4") };
62 #define ENC_RATE_CONTROL "rate-control"
63 #define ENC_RATE_CONTROL_TEXT N_("Rate control method")
64 #define ENC_RATE_CONTROL_LONGTEXT N_("Method used to encode the video sequence")
66 static const char *enc_rate_control_list[] = {
67 "constant_noise_threshold",
76 static const char *enc_rate_control_list_text[] = {
77 N_("Constant noise threshold mode"),
78 N_("Constant bitrate mode (CBR)"),
81 N_("Constant lambda mode"),
82 N_("Constant error mode"),
83 N_("Constant quality mode")
86 #define ENC_GOP_STRUCTURE "gop-structure"
87 #define ENC_GOP_STRUCTURE_TEXT N_("GOP structure")
88 #define ENC_GOP_STRUCTURE_LONGTEXT N_("GOP structure used to encode the video sequence")
90 static const char *enc_gop_structure_list[] = {
99 static const char *enc_gop_structure_list_text[] = {
100 N_("No fixed gop structure. A picture can be intra or inter and refer to previous or future pictures."),
101 N_("I-frame only sequence"),
102 N_("Inter pictures refere to previous pictures only"),
103 N_("Inter pictures refere to previous pictures only"),
104 N_("Inter pictures can refer to previous or future pictures"),
105 N_("Inter pictures can refer to previous or future pictures")
108 #define ENC_QUALITY "quality"
109 #define ENC_QUALITY_TEXT N_("Constant quality factor")
110 #define ENC_QUALITY_LONGTEXT N_("Quality factor to use in constant quality mode")
112 #define ENC_NOISE_THRESHOLD "noise-threshold"
113 #define ENC_NOISE_THRESHOLD_TEXT N_("Noise Threshold")
114 #define ENC_NOISE_THRESHOLD_LONGTEXT N_("Noise threshold to use in constant noise threshold mode")
116 #define ENC_BITRATE "bitrate"
117 #define ENC_BITRATE_TEXT N_("CBR bitrate (kbps)")
118 #define ENC_BITRATE_LONGTEXT N_("Target bitrate in kbps when encoding in constant bitrate mode")
120 #define ENC_MAX_BITRATE "max-bitrate"
121 #define ENC_MAX_BITRATE_TEXT N_("Maximum bitrate (kbps)")
122 #define ENC_MAX_BITRATE_LONGTEXT N_("Maximum bitrate in kbps when encoding in constant bitrate mode")
124 #define ENC_MIN_BITRATE "min-bitrate"
125 #define ENC_MIN_BITRATE_TEXT N_("Minimum bitrate (kbps)")
126 #define ENC_MIN_BITRATE_LONGTEXT N_("Minimum bitrate in kbps when encoding in constant bitrate mode")
128 #define ENC_AU_DISTANCE "gop-length"
129 #define ENC_AU_DISTANCE_TEXT N_("GOP length")
130 #define ENC_AU_DISTANCE_LONGTEXT N_("Number of pictures between successive sequence headers i.e. length of the group of pictures")
133 #define ENC_PREFILTER "filtering"
134 #define ENC_PREFILTER_TEXT N_("Prefilter")
135 #define ENC_PREFILTER_LONGTEXT N_("Enable adaptive prefiltering")
137 static const char *enc_filtering_list[] = {
139 "center_weighted_median",
146 static const char *enc_filtering_list_text[] = {
147 N_("No pre-filtering"),
148 N_("Centre Weighted Median"),
149 N_("Gaussian Low Pass Filter"),
151 N_("Gaussian Adaptive Low Pass Filter"),
152 N_("Low Pass Filter"),
155 #define ENC_PREFILTER_STRENGTH "filter-value"
156 #define ENC_PREFILTER_STRENGTH_TEXT N_("Amount of prefiltering")
157 #define ENC_PREFILTER_STRENGTH_LONGTEXT N_("Higher value implies more prefiltering")
159 #define ENC_CODINGMODE "coding-mode"
160 #define ENC_CODINGMODE_TEXT N_("Picture coding mode")
161 #define ENC_CODINGMODE_LONGTEXT N_("Field coding is where interlaced fields are coded" \
162 " separately as opposed to a pseudo-progressive frame")
163 static const char *const enc_codingmode_list[] =
164 { "auto", "progressive", "field" };
165 static const char *const enc_codingmode_list_text[] =
166 { N_("auto - let encoder decide based upon input (Best)"),
167 N_("force coding frame as single picture"),
168 N_("force coding frame as separate interlaced fields"),
171 /* advanced option only */
172 #define ENC_MCBLK_SIZE "motion-block-size"
173 #define ENC_MCBLK_SIZE_TEXT N_("Size of motion compensation blocks")
175 static const char *enc_block_size_list[] = {
181 static const char *const enc_block_size_list_text[] =
182 { N_("automatic - let encoder decide based upon input (Best)"),
183 N_("small - use small motion compensation blocks"),
184 N_("medium - use medium motion compensation blocks"),
185 N_("large - use large motion compensation blocks"),
188 /* advanced option only */
189 #define ENC_MCBLK_OVERLAP "motion-block-overlap"
190 #define ENC_MCBLK_OVERLAP_TEXT N_("Overlap of motion compensation blocks")
192 static const char *enc_block_overlap_list[] = {
198 static const char *const enc_block_overlap_list_text[] =
199 { N_("automatic - let encoder decide based upon input (Best)"),
200 N_("none - Motion compensation blocks do not overlap"),
201 N_("partial - Motion compensation blocks only partially overlap"),
202 N_("full - Motion compensation blocks fully overlap"),
206 #define ENC_MVPREC "mv-precision"
207 #define ENC_MVPREC_TEXT N_("Motion Vector precision")
208 #define ENC_MVPREC_LONGTEXT N_("Motion Vector precision in pels")
209 static const char *const enc_mvprec_list[] =
210 { "1", "1/2", "1/4", "1/8" };
212 /* advanced option only */
213 #define ENC_ME_COMBINED "me-combined"
214 #define ENC_ME_COMBINED_TEXT N_("Three component motion estimation")
215 #define ENC_ME_COMBINED_LONGTEXT N_("Use chroma as part of the motion estimation process")
217 #define ENC_DWTINTRA "intra-wavelet"
218 #define ENC_DWTINTRA_TEXT N_("Intra picture DWT filter")
220 #define ENC_DWTINTER "inter-wavelet"
221 #define ENC_DWTINTER_TEXT N_("Inter picture DWT filter")
223 static const char *enc_wavelet_list[] = {
233 static const char *enc_wavelet_list_text[] = {
234 "Deslauriers-Dubuc (9,7)",
236 "Deslauriers-Dubuc (13,7)",
237 "Haar with no shift",
238 "Haar with single shift per level",
240 "Daubechies (9,7) integer approximation"
243 #define ENC_DWTDEPTH "transform-depth"
244 #define ENC_DWTDEPTH_TEXT N_("Number of DWT iterations")
245 #define ENC_DWTDEPTH_LONGTEXT N_("Also known as DWT levels")
248 /* advanced option only */
249 #define ENC_MULTIQUANT "enable-multiquant"
250 #define ENC_MULTIQUANT_TEXT N_("Enable multiple quantizers")
251 #define ENC_MULTIQUANT_LONGTEXT N_("Enable multiple quantizers per subband (one per codeblock)")
253 /* advanced option only */
254 #define ENC_NOAC "enable-noarith"
255 #define ENC_NOAC_TEXT N_("Disable arithmetic coding")
256 #define ENC_NOAC_LONGTEXT N_("Use variable length codes instead, useful for very high bitrates")
258 /* visual modelling */
259 /* advanced option only */
260 #define ENC_PWT "perceptual-weighting"
261 #define ENC_PWT_TEXT N_("perceptual weighting method")
263 static const char *enc_perceptual_weighting_list[] = {
270 /* advanced option only */
271 #define ENC_PDIST "perceptual-distance"
272 #define ENC_PDIST_TEXT N_("perceptual distance")
273 #define ENC_PDIST_LONGTEXT N_("perceptual distance to calculate perceptual weight")
275 /* advanced option only */
276 #define ENC_HSLICES "horiz-slices"
277 #define ENC_HSLICES_TEXT N_("Horizontal slices per frame")
278 #define ENC_HSLICES_LONGTEXT N_("Number of horizontal slices per frame in low delay mode")
280 /* advanced option only */
281 #define ENC_VSLICES "vert-slices"
282 #define ENC_VSLICES_TEXT N_("Vertical slices per frame")
283 #define ENC_VSLICES_LONGTEXT N_("Number of vertical slices per frame in low delay mode")
285 /* advanced option only */
286 #define ENC_SCBLK_SIZE "codeblock-size"
287 #define ENC_SCBLK_SIZE_TEXT N_("Size of code blocks in each subband")
289 static const char *enc_codeblock_size_list[] = {
296 static const char *const enc_codeblock_size_list_text[] =
297 { N_("automatic - let encoder decide based upon input (Best)"),
298 N_("small - use small code blocks"),
299 N_("medium - use medium sized code blocks"),
300 N_("large - use large code blocks"),
301 N_("full - One code block per subband"),
304 /* advanced option only */
305 #define ENC_ME_HIERARCHICAL "enable-hierarchical-me"
306 #define ENC_ME_HIERARCHICAL_TEXT N_("Enable hierarchical Motion Estimation")
308 /* advanced option only */
309 #define ENC_ME_DOWNSAMPLE_LEVELS "downsample-levels"
310 #define ENC_ME_DOWNSAMPLE_LEVELS_TEXT N_("Number of levels of downsampling")
311 #define ENC_ME_DOWNSAMPLE_LEVELS_LONGTEXT N_("Number of levels of downsampling in hierarchical motion estimation mode")
313 /* advanced option only */
314 #define ENC_ME_GLOBAL_MOTION "enable-global-me"
315 #define ENC_ME_GLOBAL_MOTION_TEXT N_("Enable Global Motion Estimation")
317 /* advanced option only */
318 #define ENC_ME_PHASECORR "enable-phasecorr-me"
319 #define ENC_ME_PHASECORR_TEXT N_("Enable Phase Correlation Estimation")
321 /* advanced option only */
322 #define ENC_SCD "enable-scd"
323 #define ENC_SCD_TEXT N_("Enable Scene Change Detection")
325 /* advanced option only */
326 #define ENC_FORCE_PROFILE "force-profile"
327 #define ENC_FORCE_PROFILE_TEXT N_("Force Profile")
329 static const char *enc_profile_list[] = {
337 static const char *const enc_profile_list_text[] =
338 { N_("automatic - let encoder decide based upon input (Best)"),
339 N_("VC2 Low Delay Profile"),
340 N_("VC2 Simple Profile"),
341 N_("VC2 Main Profile"),
345 static const char *const const ppsz_enc_options[] = {
346 ENC_RATE_CONTROL, ENC_GOP_STRUCTURE, ENC_QUALITY, ENC_NOISE_THRESHOLD, ENC_BITRATE,
347 ENC_MIN_BITRATE, ENC_MAX_BITRATE, ENC_AU_DISTANCE, ENC_CHROMAFMT,
348 ENC_PREFILTER, ENC_PREFILTER_STRENGTH, ENC_CODINGMODE, ENC_MCBLK_SIZE,
349 ENC_MCBLK_OVERLAP, ENC_MVPREC, ENC_ME_COMBINED, ENC_DWTINTRA, ENC_DWTINTER,
350 ENC_DWTDEPTH, ENC_MULTIQUANT, ENC_NOAC, ENC_PWT, ENC_PDIST, ENC_HSLICES,
351 ENC_VSLICES, ENC_SCBLK_SIZE, ENC_ME_HIERARCHICAL, ENC_ME_DOWNSAMPLE_LEVELS,
352 ENC_ME_GLOBAL_MOTION, ENC_ME_PHASECORR, ENC_SCD, ENC_FORCE_PROFILE,
357 /* Module declaration */
360 set_category( CAT_INPUT )
361 set_subcategory( SUBCAT_INPUT_VCODEC )
362 set_shortname( "Schroedinger" )
363 set_description( N_("Dirac video decoder using libschroedinger") )
364 set_capability( "decoder", 200 )
365 set_callbacks( OpenDecoder, CloseDecoder )
366 add_shortcut( "schroedinger" )
370 set_section( N_("Encoding") , NULL )
371 set_description( N_("Dirac video encoder using libschroedinger") )
372 set_capability( "encoder", 110 )
373 set_callbacks( OpenEncoder, CloseEncoder )
374 add_shortcut( "schroedinger", "schro" )
376 add_string( ENC_CFG_PREFIX ENC_RATE_CONTROL, NULL,
377 ENC_RATE_CONTROL_TEXT, ENC_RATE_CONTROL_LONGTEXT, false )
378 change_string_list( enc_rate_control_list, enc_rate_control_list_text, 0 );
380 add_float( ENC_CFG_PREFIX ENC_QUALITY, -1.,
381 ENC_QUALITY_TEXT, ENC_QUALITY_LONGTEXT, false )
382 change_float_range(-1., 10.);
384 add_float( ENC_CFG_PREFIX ENC_NOISE_THRESHOLD, -1.,
385 ENC_NOISE_THRESHOLD_TEXT, ENC_NOISE_THRESHOLD_LONGTEXT, false )
386 change_float_range(-1., 100.);
388 add_integer( ENC_CFG_PREFIX ENC_BITRATE, -1,
389 ENC_BITRATE_TEXT, ENC_BITRATE_LONGTEXT, false )
390 change_integer_range(-1, INT_MAX);
392 add_integer( ENC_CFG_PREFIX ENC_MAX_BITRATE, -1,
393 ENC_MAX_BITRATE_TEXT, ENC_MAX_BITRATE_LONGTEXT, false )
394 change_integer_range(-1, INT_MAX);
396 add_integer( ENC_CFG_PREFIX ENC_MIN_BITRATE, -1,
397 ENC_MIN_BITRATE_TEXT, ENC_MIN_BITRATE_LONGTEXT, false )
398 change_integer_range(-1, INT_MAX);
400 add_string( ENC_CFG_PREFIX ENC_GOP_STRUCTURE, NULL,
401 ENC_GOP_STRUCTURE_TEXT, ENC_GOP_STRUCTURE_LONGTEXT, false )
402 change_string_list( enc_gop_structure_list, enc_gop_structure_list_text, 0 );
404 add_integer( ENC_CFG_PREFIX ENC_AU_DISTANCE, -1,
405 ENC_AU_DISTANCE_TEXT, ENC_AU_DISTANCE_LONGTEXT, false )
406 change_integer_range(-1, INT_MAX);
408 add_string( ENC_CFG_PREFIX ENC_CHROMAFMT, "420",
409 ENC_CHROMAFMT_TEXT, ENC_CHROMAFMT_LONGTEXT, false )
410 change_string_list( enc_chromafmt_list, enc_chromafmt_list_text, 0 );
412 add_string( ENC_CFG_PREFIX ENC_CODINGMODE, "auto",
413 ENC_CODINGMODE_TEXT, ENC_CODINGMODE_LONGTEXT, false )
414 change_string_list( enc_codingmode_list, enc_codingmode_list_text, 0 );
416 add_string( ENC_CFG_PREFIX ENC_MVPREC, NULL,
417 ENC_MVPREC_TEXT, ENC_MVPREC_LONGTEXT, false )
418 change_string_list( enc_mvprec_list, enc_mvprec_list, 0 );
420 /* advanced option only */
421 add_string( ENC_CFG_PREFIX ENC_MCBLK_SIZE, NULL,
422 ENC_MCBLK_SIZE_TEXT, ENC_MCBLK_SIZE_TEXT, true )
423 change_string_list( enc_block_size_list, enc_block_size_list_text, 0 );
426 /* advanced option only */
427 add_string( ENC_CFG_PREFIX ENC_MCBLK_OVERLAP, NULL,
428 ENC_MCBLK_OVERLAP_TEXT, ENC_MCBLK_OVERLAP_TEXT, true )
429 change_string_list( enc_block_overlap_list, enc_block_overlap_list_text, 0 );
431 /* advanced option only */
432 add_integer( ENC_CFG_PREFIX ENC_ME_COMBINED, -1,
433 ENC_ME_COMBINED_TEXT, ENC_ME_COMBINED_LONGTEXT, true )
434 change_integer_range(-1, 1 );
436 /* advanced option only */
437 add_integer( ENC_CFG_PREFIX ENC_ME_HIERARCHICAL, -1,
438 ENC_ME_HIERARCHICAL_TEXT, ENC_ME_HIERARCHICAL_TEXT, true )
439 change_integer_range(-1, 1 );
441 /* advanced option only */
442 add_integer( ENC_CFG_PREFIX ENC_ME_DOWNSAMPLE_LEVELS, -1,
443 ENC_ME_DOWNSAMPLE_LEVELS_TEXT, ENC_ME_DOWNSAMPLE_LEVELS_LONGTEXT, true )
444 change_integer_range(-1, 8 );
446 /* advanced option only */
447 add_integer( ENC_CFG_PREFIX ENC_ME_GLOBAL_MOTION, -1,
448 ENC_ME_GLOBAL_MOTION_TEXT, ENC_ME_GLOBAL_MOTION_TEXT, true )
449 change_integer_range(-1, 1 );
451 /* advanced option only */
452 add_integer( ENC_CFG_PREFIX ENC_ME_PHASECORR, -1,
453 ENC_ME_PHASECORR_TEXT, ENC_ME_PHASECORR_TEXT, true )
454 change_integer_range(-1, 1 );
456 add_string( ENC_CFG_PREFIX ENC_DWTINTRA, NULL,
457 ENC_DWTINTRA_TEXT, ENC_DWTINTRA_TEXT, false )
458 change_string_list( enc_wavelet_list, enc_wavelet_list_text, 0 );
460 add_string( ENC_CFG_PREFIX ENC_DWTINTER, NULL,
461 ENC_DWTINTER_TEXT, ENC_DWTINTER_TEXT, false )
462 change_string_list( enc_wavelet_list, enc_wavelet_list_text, 0 );
464 add_integer( ENC_CFG_PREFIX ENC_DWTDEPTH, -1,
465 ENC_DWTDEPTH_TEXT, ENC_DWTDEPTH_LONGTEXT, false )
466 change_integer_range(-1, SCHRO_LIMIT_ENCODER_TRANSFORM_DEPTH );
468 /* advanced option only */
469 add_integer( ENC_CFG_PREFIX ENC_MULTIQUANT, -1,
470 ENC_MULTIQUANT_TEXT, ENC_MULTIQUANT_LONGTEXT, true )
471 change_integer_range(-1, 1 );
473 /* advanced option only */
474 add_string( ENC_CFG_PREFIX ENC_SCBLK_SIZE, NULL,
475 ENC_SCBLK_SIZE_TEXT, ENC_SCBLK_SIZE_TEXT, true )
476 change_string_list( enc_codeblock_size_list, enc_codeblock_size_list_text, 0 );
478 add_string( ENC_CFG_PREFIX ENC_PREFILTER, NULL,
479 ENC_PREFILTER_TEXT, ENC_PREFILTER_LONGTEXT, false )
480 change_string_list( enc_filtering_list, enc_filtering_list_text, 0 );
482 add_float( ENC_CFG_PREFIX ENC_PREFILTER_STRENGTH, -1.,
483 ENC_PREFILTER_STRENGTH_TEXT, ENC_PREFILTER_STRENGTH_LONGTEXT, false )
484 change_float_range(-1., 100.0);
486 /* advanced option only */
487 add_integer( ENC_CFG_PREFIX ENC_SCD, -1,
488 ENC_SCD_TEXT, ENC_SCD_TEXT, true )
489 change_integer_range(-1, 1 );
491 /* advanced option only */
492 add_string( ENC_CFG_PREFIX ENC_PWT, NULL,
493 ENC_PWT_TEXT, ENC_PWT_TEXT, true )
494 change_string_list( enc_perceptual_weighting_list, enc_perceptual_weighting_list, 0 );
496 /* advanced option only */
497 add_float( ENC_CFG_PREFIX ENC_PDIST, -1,
498 ENC_PDIST_TEXT, ENC_PDIST_LONGTEXT, true )
499 change_float_range(-1., 100.);
501 /* advanced option only */
502 add_integer( ENC_CFG_PREFIX ENC_NOAC, -1,
503 ENC_NOAC_TEXT, ENC_NOAC_LONGTEXT, true )
504 change_integer_range(-1, 1 );
506 /* advanced option only */
507 add_integer( ENC_CFG_PREFIX ENC_HSLICES, -1,
508 ENC_HSLICES_TEXT, ENC_HSLICES_LONGTEXT, true )
509 change_integer_range(-1, INT_MAX );
511 /* advanced option only */
512 add_integer( ENC_CFG_PREFIX ENC_VSLICES, -1,
513 ENC_VSLICES_TEXT, ENC_VSLICES_LONGTEXT, true )
514 change_integer_range(-1, INT_MAX );
516 /* advanced option only */
517 add_string( ENC_CFG_PREFIX ENC_FORCE_PROFILE, NULL,
518 ENC_FORCE_PROFILE_TEXT, ENC_FORCE_PROFILE_TEXT, true )
519 change_string_list( enc_profile_list, enc_profile_list_text, 0 );
524 /*****************************************************************************
526 *****************************************************************************/
527 static picture_t *DecodeBlock ( decoder_t *p_dec, block_t **pp_block );
529 struct picture_free_t
535 /*****************************************************************************
536 * decoder_sys_t : Schroedinger decoder descriptor
537 *****************************************************************************/
544 mtime_t i_frame_pts_delta;
545 SchroDecoder *p_schro;
546 SchroVideoFormat *p_format;
549 /*****************************************************************************
550 * OpenDecoder: probe the decoder and return score
551 *****************************************************************************/
552 static int OpenDecoder( vlc_object_t *p_this )
554 decoder_t *p_dec = (decoder_t*)p_this;
555 decoder_sys_t *p_sys;
556 SchroDecoder *p_schro;
558 if( p_dec->fmt_in.i_codec != VLC_CODEC_DIRAC )
563 /* Allocate the memory needed to store the decoder's structure */
564 p_sys = malloc(sizeof(decoder_sys_t));
568 /* Initialise the schroedinger (and hence liboil libraries */
569 /* This does no allocation and is safe to call */
572 /* Initialise the schroedinger decoder */
573 if( !(p_schro = schro_decoder_new()) )
579 p_dec->p_sys = p_sys;
580 p_sys->p_schro = p_schro;
581 p_sys->p_format = NULL;
582 p_sys->i_lastpts = VLC_TS_INVALID;
583 p_sys->i_frame_pts_delta = 0;
585 /* Set output properties */
586 p_dec->fmt_out.i_cat = VIDEO_ES;
587 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
590 p_dec->pf_decode_video = DecodeBlock;
595 /*****************************************************************************
596 * SetPictureFormat: Set the decoded picture params to the ones from the stream
597 *****************************************************************************/
598 static void SetVideoFormat( decoder_t *p_dec )
600 decoder_sys_t *p_sys = p_dec->p_sys;
602 p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
603 if( p_sys->p_format == NULL ) return;
605 p_sys->i_frame_pts_delta = INT64_C(1000000)
606 * p_sys->p_format->frame_rate_denominator
607 / p_sys->p_format->frame_rate_numerator;
609 switch( p_sys->p_format->chroma_format )
611 case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_CODEC_I420; break;
612 case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_CODEC_I422; break;
613 case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_CODEC_I444; break;
615 p_dec->fmt_out.i_codec = 0;
619 p_dec->fmt_out.video.i_visible_width = p_sys->p_format->clean_width;
620 p_dec->fmt_out.video.i_x_offset = p_sys->p_format->left_offset;
621 p_dec->fmt_out.video.i_width = p_sys->p_format->width;
623 p_dec->fmt_out.video.i_visible_height = p_sys->p_format->clean_height;
624 p_dec->fmt_out.video.i_y_offset = p_sys->p_format->top_offset;
625 p_dec->fmt_out.video.i_height = p_sys->p_format->height;
627 /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
628 p_dec->fmt_out.video.i_sar_num = p_sys->p_format->aspect_ratio_numerator;
629 p_dec->fmt_out.video.i_sar_den = p_sys->p_format->aspect_ratio_denominator;
631 p_dec->fmt_out.video.i_frame_rate =
632 p_sys->p_format->frame_rate_numerator;
633 p_dec->fmt_out.video.i_frame_rate_base =
634 p_sys->p_format->frame_rate_denominator;
637 /*****************************************************************************
638 * SchroFrameFree: schro_frame callback to release the associated picture_t
639 * When schro_decoder_reset() is called there will be pictures in the
640 * decoding pipeline that need to be released rather than displayed.
641 *****************************************************************************/
642 static void SchroFrameFree( SchroFrame *frame, void *priv)
644 struct picture_free_t *p_free = priv;
649 decoder_DeletePicture( p_free->p_dec, p_free->p_pic );
654 /*****************************************************************************
655 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
656 *****************************************************************************/
657 static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
659 decoder_sys_t *p_sys = p_dec->p_sys;
660 SchroFrame *p_schroframe = schro_frame_new();
661 picture_t *p_pic = NULL;
662 struct picture_free_t *p_free;
667 p_pic = decoder_NewPicture( p_dec );
672 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
673 if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
675 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
677 else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
679 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
682 p_schroframe->width = p_sys->p_format->width;
683 p_schroframe->height = p_sys->p_format->height;
685 p_free = malloc( sizeof( *p_free ) );
686 p_free->p_pic = p_pic;
687 p_free->p_dec = p_dec;
688 schro_frame_set_free_callback( p_schroframe, SchroFrameFree, p_free );
690 for( int i=0; i<3; i++ )
692 p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
693 p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
694 p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
695 p_schroframe->components[i].length =
696 p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
697 p_schroframe->components[i].data = p_pic->p[i].p_pixels;
701 p_schroframe->components[i].v_shift =
702 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
703 p_schroframe->components[i].h_shift =
704 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
708 p_pic->b_progressive = !p_sys->p_format->interlaced;
709 p_pic->b_top_field_first = p_sys->p_format->top_field_first;
710 p_pic->i_nb_fields = 2;
715 /*****************************************************************************
716 * SchroBufferFree: schro_buffer callback to release the associated block_t
717 *****************************************************************************/
718 static void SchroBufferFree( SchroBuffer *buf, void *priv )
720 block_t *p_block = priv;
725 block_Release( p_block );
729 /*****************************************************************************
730 * CloseDecoder: decoder destruction
731 *****************************************************************************/
732 static void CloseDecoder( vlc_object_t *p_this )
734 decoder_t *p_dec = (decoder_t *)p_this;
735 decoder_sys_t *p_sys = p_dec->p_sys;
737 schro_decoder_free( p_sys->p_schro );
741 /****************************************************************************
742 * DecodeBlock: the whole thing
743 ****************************************************************************
744 * Blocks need not be Dirac dataunit aligned.
745 * If a block has a PTS signaled, it applies to the first picture at or after p_block
747 * If this function returns a picture (!NULL), it is called again and the
748 * same block is resubmitted. To avoid this, set *pp_block to NULL;
749 * If this function returns NULL, the *pp_block is lost (and leaked).
750 * This function must free all blocks when finished with them.
751 ****************************************************************************/
752 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
754 decoder_sys_t *p_sys = p_dec->p_sys;
756 if( !pp_block ) return NULL;
759 block_t *p_block = *pp_block;
761 /* reset the decoder when seeking as the decode in progress is invalid */
762 /* discard the block as it is just a null magic block */
763 if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) {
764 schro_decoder_reset( p_sys->p_schro );
766 p_sys->i_lastpts = VLC_TS_INVALID;
767 block_Release( p_block );
772 SchroBuffer *p_schrobuffer;
773 p_schrobuffer = schro_buffer_new_with_data( p_block->p_buffer, p_block->i_buffer );
774 p_schrobuffer->free = SchroBufferFree;
775 p_schrobuffer->priv = p_block;
776 if( p_block->i_pts > VLC_TS_INVALID ) {
777 mtime_t *p_pts = malloc( sizeof(*p_pts) );
779 *p_pts = p_block->i_pts;
780 /* if this call fails, p_pts is freed automatically */
781 p_schrobuffer->tag = schro_tag_new( p_pts, free );
785 /* this stops the same block being fed back into this function if
786 * we were on the next iteration of this loop to output a picture */
788 schro_decoder_autoparse_push( p_sys->p_schro, p_schrobuffer );
789 /* DO NOT refer to p_block after this point, it may have been freed */
794 SchroFrame *p_schroframe;
796 int state = schro_decoder_autoparse_wait( p_sys->p_schro );
800 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
801 SetVideoFormat( p_dec );
804 case SCHRO_DECODER_NEED_BITS:
807 case SCHRO_DECODER_NEED_FRAME:
808 p_schroframe = CreateSchroFrameFromPic( p_dec );
812 msg_Err( p_dec, "Could not allocate picture for decoder");
816 schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
819 case SCHRO_DECODER_OK: {
820 SchroTag *p_tag = schro_decoder_get_picture_tag( p_sys->p_schro );
821 p_schroframe = schro_decoder_pull( p_sys->p_schro );
822 if( !p_schroframe || !p_schroframe->priv )
824 /* frame can't be one that was allocated by us
825 * -- no private data: discard */
826 if( p_tag ) schro_tag_free( p_tag );
827 if( p_schroframe ) schro_frame_unref( p_schroframe );
830 p_pic = ((struct picture_free_t*) p_schroframe->priv)->p_pic;
831 p_schroframe->priv = NULL;
835 /* free is handled by schro_frame_unref */
836 p_pic->date = *(mtime_t*) p_tag->value;
837 schro_tag_free( p_tag );
839 else if( p_sys->i_lastpts > VLC_TS_INVALID )
841 /* NB, this shouldn't happen since the packetizer does a
842 * very thorough job of inventing timestamps. The
843 * following is just a very rough fall back incase packetizer
845 /* maybe it would be better to set p_pic->b_force ? */
846 p_pic->date = p_sys->i_lastpts + p_sys->i_frame_pts_delta;
848 p_sys->i_lastpts = p_pic->date;
850 schro_frame_unref( p_schroframe );
853 case SCHRO_DECODER_EOS:
854 /* NB, the new api will not emit _EOS, it handles the reset internally */
857 case SCHRO_DECODER_ERROR:
858 msg_Err( p_dec, "SCHRO_DECODER_ERROR");
864 /*****************************************************************************
866 *****************************************************************************/
867 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
870 /*****************************************************************************
871 * picture_pts_t : store pts alongside picture number, not carried through
873 *****************************************************************************/
876 mtime_t i_pts; /* associated pts */
877 uint32_t u_pnum; /* dirac picture number */
878 bool b_empty; /* entry is invalid */
881 /*****************************************************************************
882 * encoder_sys_t : Schroedinger encoder descriptor
883 *****************************************************************************/
884 #define SCHRO_PTS_TLB_SIZE 256
890 SchroEncoder *p_schro;
891 SchroVideoFormat *p_format;
893 bool b_auto_field_coding;
895 uint32_t i_input_picnum;
896 block_fifo_t *p_dts_fifo;
900 struct picture_pts_t pts_tlb[SCHRO_PTS_TLB_SIZE];
901 mtime_t i_pts_offset;
902 mtime_t i_field_time;
904 bool b_eos_signalled;
910 unsigned int i_height;
912 SchroVideoFormatEnum i_vf;
913 } schro_format_guess[] = {
914 /* Important: Keep this list ordered in ascending picture height */
915 {1, 0, SCHRO_VIDEO_FORMAT_CUSTOM},
916 {120, 15, SCHRO_VIDEO_FORMAT_QSIF},
917 {144, 12, SCHRO_VIDEO_FORMAT_QCIF},
918 {240, 15, SCHRO_VIDEO_FORMAT_SIF},
919 {288, 12, SCHRO_VIDEO_FORMAT_CIF},
920 {480, 30, SCHRO_VIDEO_FORMAT_SD480I_60},
921 {480, 15, SCHRO_VIDEO_FORMAT_4SIF},
922 {576, 12, SCHRO_VIDEO_FORMAT_4CIF},
923 {576, 25, SCHRO_VIDEO_FORMAT_SD576I_50},
924 {720, 50, SCHRO_VIDEO_FORMAT_HD720P_50},
925 {720, 60, SCHRO_VIDEO_FORMAT_HD720P_60},
926 {1080, 24, SCHRO_VIDEO_FORMAT_DC2K_24},
927 {1080, 25, SCHRO_VIDEO_FORMAT_HD1080I_50},
928 {1080, 30, SCHRO_VIDEO_FORMAT_HD1080I_60},
929 {1080, 50, SCHRO_VIDEO_FORMAT_HD1080P_50},
930 {1080, 60, SCHRO_VIDEO_FORMAT_HD1080P_60},
931 {2160, 24, SCHRO_VIDEO_FORMAT_DC4K_24},
935 /*****************************************************************************
936 * ResetPTStlb: Purge all entries in @p_enc@'s PTS-tlb
937 *****************************************************************************/
938 static void ResetPTStlb( encoder_t *p_enc )
940 encoder_sys_t *p_sys = p_enc->p_sys;
941 for( int i = 0; i < SCHRO_PTS_TLB_SIZE; i++ )
943 p_sys->pts_tlb[i].b_empty = true;
948 /*****************************************************************************
949 * StorePicturePTS: Store the PTS value for a particular picture number
950 *****************************************************************************/
951 static void StorePicturePTS( encoder_t *p_enc, uint32_t u_pnum, mtime_t i_pts )
953 encoder_sys_t *p_sys = p_enc->p_sys;
955 for( int i = 0; i<SCHRO_PTS_TLB_SIZE; i++ )
957 if( p_sys->pts_tlb[i].b_empty )
959 p_sys->pts_tlb[i].u_pnum = u_pnum;
960 p_sys->pts_tlb[i].i_pts = i_pts;
961 p_sys->pts_tlb[i].b_empty = false;
967 msg_Err( p_enc, "Could not store PTS %"PRId64" for frame %u", i_pts, u_pnum );
970 /*****************************************************************************
971 * GetPicturePTS: Retrieve the PTS value for a particular picture number
972 *****************************************************************************/
973 static mtime_t GetPicturePTS( encoder_t *p_enc, uint32_t u_pnum )
975 encoder_sys_t *p_sys = p_enc->p_sys;
977 for( int i = 0; i < SCHRO_PTS_TLB_SIZE; i++ )
979 if( !p_sys->pts_tlb[i].b_empty &&
980 p_sys->pts_tlb[i].u_pnum == u_pnum )
982 p_sys->pts_tlb[i].b_empty = true;
983 return p_sys->pts_tlb[i].i_pts;
987 msg_Err( p_enc, "Could not retrieve PTS for picture %u", u_pnum );
991 static inline bool SchroSetEnum( const encoder_t *p_enc, int i_list_size, const char *list[],
992 const char *psz_name, const char *psz_name_text, const char *psz_value)
994 encoder_sys_t *p_sys = p_enc->p_sys;
995 if( list && psz_name_text && psz_name && psz_value ) {
996 for( int i = 0; i < i_list_size; ++i ) {
997 if( strcmp( list[i], psz_value ) )
999 schro_encoder_setting_set_double( p_sys->p_schro, psz_name, i );
1002 msg_Err( p_enc, "Invalid %s: %s", psz_name_text, psz_value );
1007 static bool SetEncChromaFormat( encoder_t *p_enc, uint32_t i_codec )
1009 encoder_sys_t *p_sys = p_enc->p_sys;
1012 case VLC_CODEC_I420:
1013 p_enc->fmt_in.i_codec = i_codec;
1014 p_enc->fmt_in.video.i_bits_per_pixel = 12;
1015 p_sys->p_format->chroma_format = SCHRO_CHROMA_420;
1017 case VLC_CODEC_I422:
1018 p_enc->fmt_in.i_codec = i_codec;
1019 p_enc->fmt_in.video.i_bits_per_pixel = 16;
1020 p_sys->p_format->chroma_format = SCHRO_CHROMA_422;
1022 case VLC_CODEC_I444:
1023 p_enc->fmt_in.i_codec = i_codec;
1024 p_enc->fmt_in.video.i_bits_per_pixel = 24;
1025 p_sys->p_format->chroma_format = SCHRO_CHROMA_444;
1034 #define SCHRO_SET_FLOAT(psz_name, pschro_name) \
1035 f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX psz_name ); \
1036 if( f_tmp >= 0.0 ) \
1037 schro_encoder_setting_set_double( p_sys->p_schro, pschro_name, f_tmp );
1039 #define SCHRO_SET_INTEGER(psz_name, pschro_name, ignore_val) \
1040 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX psz_name ); \
1041 if( i_tmp > ignore_val ) \
1042 schro_encoder_setting_set_double( p_sys->p_schro, pschro_name, i_tmp );
1044 #define SCHRO_SET_ENUM(list, psz_name, psz_name_text, pschro_name) \
1045 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX psz_name ); \
1048 else if ( *psz_tmp != '\0' ) { \
1049 int i_list_size = ARRAY_SIZE(list); \
1050 if( !SchroSetEnum( p_enc, i_list_size, list, pschro_name, psz_name_text, psz_tmp ) ) { \
1057 /*****************************************************************************
1058 * OpenEncoder: probe the encoder and return score
1059 *****************************************************************************/
1060 static int OpenEncoder( vlc_object_t *p_this )
1062 encoder_t *p_enc = (encoder_t *)p_this;
1063 encoder_sys_t *p_sys = p_enc->p_sys;
1068 if( p_enc->fmt_out.i_codec != VLC_CODEC_DIRAC &&
1071 return VLC_EGENERIC;
1074 if( !p_enc->fmt_in.video.i_frame_rate || !p_enc->fmt_in.video.i_frame_rate_base ||
1075 !p_enc->fmt_in.video.i_height || !p_enc->fmt_in.video.i_width )
1077 msg_Err( p_enc, "Framerate and picture dimensions must be non-zero" );
1078 return VLC_EGENERIC;
1081 /* Allocate the memory needed to store the decoder's structure */
1082 if( ( p_sys = calloc( 1, sizeof( *p_sys ) ) ) == NULL )
1085 p_enc->p_sys = p_sys;
1086 p_enc->pf_encode_video = Encode;
1087 p_enc->fmt_out.i_codec = VLC_CODEC_DIRAC;
1088 p_enc->fmt_out.i_cat = VIDEO_ES;
1090 if( ( p_sys->p_dts_fifo = block_FifoNew() ) == NULL )
1092 CloseEncoder( p_this );
1096 ResetPTStlb( p_enc );
1098 /* guess the video format based upon number of lines and picture height */
1100 SchroVideoFormatEnum guessed_video_fmt = SCHRO_VIDEO_FORMAT_CUSTOM;
1101 /* Pick the dirac_video_format in this order of preference:
1102 * 1. an exact match in frame height and an approximate fps match
1103 * 2. the previous preset with a smaller number of lines.
1107 if( schro_format_guess[i].i_height > p_enc->fmt_in.video.i_height )
1109 guessed_video_fmt = schro_format_guess[i-1].i_vf;
1112 if( schro_format_guess[i].i_height != p_enc->fmt_in.video.i_height )
1114 int src_fps = p_enc->fmt_in.video.i_frame_rate / p_enc->fmt_in.video.i_frame_rate_base;
1115 int delta_fps = abs( schro_format_guess[i].i_approx_fps - src_fps );
1119 guessed_video_fmt = schro_format_guess[i].i_vf;
1121 } while( schro_format_guess[++i].i_height );
1124 p_sys->p_schro = schro_encoder_new();
1125 if( !p_sys->p_schro ) {
1126 msg_Err( p_enc, "Failed to initialize libschroedinger encoder" );
1127 return VLC_EGENERIC;
1129 schro_encoder_set_packet_assembly( p_sys->p_schro, true );
1131 if( !( p_sys->p_format = schro_encoder_get_video_format( p_sys->p_schro ) ) ) {
1132 msg_Err( p_enc, "Failed to get Schroedigner video format" );
1133 schro_encoder_free( p_sys->p_schro );
1134 return VLC_EGENERIC;
1137 /* initialise the video format parameters to the guessed format */
1138 schro_video_format_set_std_video_format( p_sys->p_format, guessed_video_fmt );
1140 /* constants set from the input video format */
1141 p_sys->p_format->width = p_enc->fmt_in.video.i_width;
1142 p_sys->p_format->height = p_enc->fmt_in.video.i_height;
1143 p_sys->p_format->frame_rate_numerator = p_enc->fmt_in.video.i_frame_rate;
1144 p_sys->p_format->frame_rate_denominator = p_enc->fmt_in.video.i_frame_rate_base;
1145 unsigned u_asr_num, u_asr_den;
1146 vlc_ureduce( &u_asr_num, &u_asr_den,
1147 p_enc->fmt_in.video.i_sar_num,
1148 p_enc->fmt_in.video.i_sar_den, 0 );
1149 p_sys->p_format->aspect_ratio_numerator = u_asr_num;
1150 p_sys->p_format->aspect_ratio_denominator = u_asr_den;
1152 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1154 SCHRO_SET_ENUM(enc_rate_control_list, ENC_RATE_CONTROL, ENC_RATE_CONTROL_TEXT, "rate_control")
1156 SCHRO_SET_ENUM(enc_gop_structure_list, ENC_GOP_STRUCTURE, ENC_GOP_STRUCTURE_TEXT, "gop_structure")
1158 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_CHROMAFMT );
1163 if( !strcmp( psz_tmp, "420" ) ) {
1164 i_codec = VLC_CODEC_I420;
1166 else if( !strcmp( psz_tmp, "422" ) ) {
1167 i_codec = VLC_CODEC_I422;
1169 else if( !strcmp( psz_tmp, "444" ) ) {
1170 i_codec = VLC_CODEC_I444;
1173 msg_Err( p_enc, "Invalid chroma format: %s", psz_tmp );
1177 SetEncChromaFormat( p_enc, i_codec );
1181 SCHRO_SET_FLOAT(ENC_QUALITY, "quality")
1183 SCHRO_SET_FLOAT(ENC_NOISE_THRESHOLD, "noise_threshold")
1185 /* use bitrate from sout-transcode-vb in kbps */
1186 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_BITRATE );
1188 schro_encoder_setting_set_double( p_sys->p_schro, "bitrate", i_tmp * 1000 );
1190 schro_encoder_setting_set_double( p_sys->p_schro, "bitrate", p_enc->fmt_out.i_bitrate );
1192 p_enc->fmt_out.i_bitrate = schro_encoder_setting_get_double( p_sys->p_schro, "bitrate" );
1194 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_MIN_BITRATE );
1196 schro_encoder_setting_set_double( p_sys->p_schro, "min_bitrate", i_tmp * 1000 );
1198 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_MAX_BITRATE );
1200 schro_encoder_setting_set_double( p_sys->p_schro, "max_bitrate", i_tmp * 1000 );
1202 SCHRO_SET_INTEGER(ENC_AU_DISTANCE, "au_distance", -1)
1204 SCHRO_SET_ENUM(enc_filtering_list, ENC_PREFILTER, ENC_PREFILTER_TEXT, "filtering")
1206 SCHRO_SET_FLOAT(ENC_PREFILTER_STRENGTH, "filter_value")
1209 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_CODINGMODE );
1212 else if( !strcmp( psz_tmp, "auto" ) ) {
1213 p_sys->b_auto_field_coding = true;
1215 else if( !strcmp( psz_tmp, "progressive" ) ) {
1216 p_sys->b_auto_field_coding = false;
1217 schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", false);
1219 else if( !strcmp( psz_tmp, "field" ) ) {
1220 p_sys->b_auto_field_coding = false;
1221 schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", true);
1224 msg_Err( p_enc, "Invalid codingmode: %s", psz_tmp );
1230 SCHRO_SET_ENUM(enc_block_size_list, ENC_MCBLK_SIZE, ENC_MCBLK_SIZE_TEXT, "motion_block_size")
1232 SCHRO_SET_ENUM(enc_block_overlap_list, ENC_MCBLK_OVERLAP, ENC_MCBLK_OVERLAP_TEXT, "motion_block_overlap")
1234 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_MVPREC );
1237 else if( *psz_tmp != '\0') {
1238 if( !strcmp( psz_tmp, "1" ) ) {
1239 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 0 );
1241 else if( !strcmp( psz_tmp, "1/2" ) ) {
1242 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 1 );
1244 else if( !strcmp( psz_tmp, "1/4" ) ) {
1245 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 2 );
1247 else if( !strcmp( psz_tmp, "1/8" ) ) {
1248 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 3 );
1251 msg_Err( p_enc, "Invalid mv_precision: %s", psz_tmp );
1258 SCHRO_SET_INTEGER(ENC_ME_COMBINED, "enable_chroma_me", -1)
1260 SCHRO_SET_ENUM(enc_wavelet_list, ENC_DWTINTRA, ENC_DWTINTRA_TEXT, "intra_wavelet")
1262 SCHRO_SET_ENUM(enc_wavelet_list, ENC_DWTINTER, ENC_DWTINTER_TEXT, "inter_wavelet")
1264 SCHRO_SET_INTEGER(ENC_DWTDEPTH, "transform_depth", -1)
1266 SCHRO_SET_INTEGER(ENC_MULTIQUANT, "enable_multiquant", -1)
1268 SCHRO_SET_INTEGER(ENC_NOAC, "enable_noarith", -1)
1270 SCHRO_SET_ENUM(enc_perceptual_weighting_list, ENC_PWT, ENC_PWT_TEXT, "perceptual_weighting")
1272 SCHRO_SET_FLOAT(ENC_PDIST, "perceptual_distance")
1274 SCHRO_SET_INTEGER(ENC_HSLICES, "horiz_slices", -1)
1276 SCHRO_SET_INTEGER(ENC_VSLICES, "vert_slices", -1)
1278 SCHRO_SET_ENUM(enc_codeblock_size_list, ENC_SCBLK_SIZE, ENC_SCBLK_SIZE_TEXT, "codeblock_size")
1280 SCHRO_SET_INTEGER(ENC_ME_HIERARCHICAL, "enable_hierarchical_estimation", -1)
1282 SCHRO_SET_INTEGER(ENC_ME_DOWNSAMPLE_LEVELS, "downsample_levels", 1)
1284 SCHRO_SET_INTEGER(ENC_ME_GLOBAL_MOTION, "enable_global_motion", -1)
1286 SCHRO_SET_INTEGER(ENC_ME_PHASECORR, "enable_phasecorr_estimation", -1)
1288 SCHRO_SET_INTEGER(ENC_SCD, "enable_scene_change_detection", -1)
1290 SCHRO_SET_ENUM(enc_profile_list, ENC_FORCE_PROFILE, ENC_FORCE_PROFILE_TEXT, "force_profile")
1296 CloseEncoder( p_this );
1297 return VLC_EGENERIC;
1301 struct enc_picture_free_t
1307 /*****************************************************************************
1308 * EncSchroFrameFree: schro_frame callback to release the associated picture_t
1309 * When schro_encoder_reset() is called there will be pictures in the
1310 * encoding pipeline that need to be released rather than displayed.
1311 *****************************************************************************/
1312 static void EncSchroFrameFree( SchroFrame *frame, void *priv )
1314 struct enc_picture_free_t *p_free = priv;
1319 picture_Release( p_free->p_pic );
1324 /*****************************************************************************
1325 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
1326 *****************************************************************************/
1327 static SchroFrame *CreateSchroFrameFromInputPic( encoder_t *p_enc, picture_t *p_pic )
1329 encoder_sys_t *p_sys = p_enc->p_sys;
1330 SchroFrame *p_schroframe = schro_frame_new();
1331 struct enc_picture_free_t *p_free;
1339 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
1340 if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
1342 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
1344 else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
1346 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
1349 p_schroframe->width = p_sys->p_format->width;
1350 p_schroframe->height = p_sys->p_format->height;
1352 p_free = malloc( sizeof( *p_free ) );
1353 if( unlikely( p_free == NULL ) ) {
1354 schro_frame_unref( p_schroframe );
1358 p_free->p_pic = p_pic;
1359 p_free->p_enc = p_enc;
1360 schro_frame_set_free_callback( p_schroframe, EncSchroFrameFree, p_free );
1362 for( int i=0; i<3; i++ )
1364 p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
1365 p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
1366 p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
1367 p_schroframe->components[i].length =
1368 p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
1369 p_schroframe->components[i].data = p_pic->p[i].p_pixels;
1373 p_schroframe->components[i].v_shift =
1374 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
1375 p_schroframe->components[i].h_shift =
1376 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
1380 return p_schroframe;
1383 /* Attempt to find dirac picture number in an encapsulation unit */
1384 static int ReadDiracPictureNumber( uint32_t *p_picnum, block_t *p_block )
1387 /* protect against falling off the edge */
1388 while( u_pos + 13 < p_block->i_buffer )
1390 /* find the picture startcode */
1391 if( p_block->p_buffer[u_pos] & 0x08 )
1393 *p_picnum = GetDWBE( p_block->p_buffer + u_pos + 9 );
1396 /* skip to the next dirac data unit */
1397 uint32_t u_npo = GetDWBE( p_block->p_buffer + u_pos + 1 );
1398 assert( u_npo <= UINT32_MAX - u_pos );
1407 static block_t *Encode( encoder_t *p_enc, picture_t *p_pic )
1409 encoder_sys_t *p_sys = p_enc->p_sys;
1410 block_t *p_block, *p_output_chain = NULL;
1411 SchroFrame *p_frame;
1415 if( !p_sys->started || p_sys->b_eos_pulled )
1418 if( !p_sys->b_eos_signalled ) {
1419 p_sys->b_eos_signalled = 1;
1420 schro_encoder_end_of_stream( p_sys->p_schro );
1423 /* we only know if the sequence is interlaced when the first
1424 * picture arrives, so final setup is done here */
1425 /* XXX todo, detect change of interlace */
1426 p_sys->p_format->interlaced = !p_pic->b_progressive;
1427 p_sys->p_format->top_field_first = p_pic->b_top_field_first;
1429 if( p_sys->b_auto_field_coding )
1430 schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", !p_pic->b_progressive );
1433 if( !p_sys->started ) {
1436 if( p_pic->format.i_chroma != p_enc->fmt_in.i_codec ) {
1437 char chroma_in[5], chroma_out[5];
1438 vlc_fourcc_to_char( p_pic->format.i_chroma, chroma_in );
1439 chroma_in[4] = '\0';
1440 chroma_out[4] = '\0';
1441 vlc_fourcc_to_char( p_enc->fmt_in.i_codec, chroma_out );
1442 msg_Warn( p_enc, "Resetting chroma from %s to %s", chroma_out, chroma_in );
1443 if( !SetEncChromaFormat( p_enc, p_pic->format.i_chroma ) ) {
1444 msg_Err( p_enc, "Could not reset chroma format to %s", chroma_in );
1449 date_Init( &date, p_enc->fmt_in.video.i_frame_rate, p_enc->fmt_in.video.i_frame_rate_base );
1450 /* FIXME - Unlike dirac-research codec Schro doesn't have a function that returns the delay in pics yet.
1451 * Use a default of 1
1453 date_Increment( &date, 1 );
1454 p_sys->i_pts_offset = date_Get( &date );
1455 if( schro_encoder_setting_get_double( p_sys->p_schro, "interlaced_coding" ) > 0.0 ) {
1456 date_Set( &date, 0 );
1457 date_Increment( &date, 1);
1458 p_sys->i_field_time = date_Get( &date ) / 2;
1461 schro_video_format_set_std_signal_range( p_sys->p_format, SCHRO_SIGNAL_RANGE_8BIT_VIDEO );
1462 schro_encoder_set_video_format( p_sys->p_schro, p_sys->p_format );
1463 schro_encoder_start( p_sys->p_schro );
1467 if( !p_sys->b_eos_signalled ) {
1468 /* create a schro frame from the input pic and load */
1469 /* Increase ref count by 1 so that the picture is not freed until
1470 Schro finishes with it */
1471 picture_Hold( p_pic );
1473 p_frame = CreateSchroFrameFromInputPic( p_enc, p_pic );
1476 schro_encoder_push_frame( p_sys->p_schro, p_frame );
1479 /* store pts in a lookaside buffer, so that the same pts may
1480 * be used for the picture in coded order */
1481 StorePicturePTS( p_enc, p_sys->i_input_picnum, p_pic->date );
1482 p_sys->i_input_picnum++;
1484 /* store dts in a queue, so that they appear in order in
1486 p_block = block_New( p_enc, 1 );
1489 p_block->i_dts = p_pic->date - p_sys->i_pts_offset;
1490 block_FifoPut( p_sys->p_dts_fifo, p_block );
1493 /* for field coding mode, insert an extra value into both the
1494 * pts lookaside buffer and dts queue, offset to correspond
1495 * to a one field delay. */
1496 if( schro_encoder_setting_get_double( p_sys->p_schro, "interlaced_coding" ) > 0.0 ) {
1497 StorePicturePTS( p_enc, p_sys->i_input_picnum, p_pic->date + p_sys->i_field_time );
1498 p_sys->i_input_picnum++;
1500 p_block = block_New( p_enc, 1 );
1503 p_block->i_dts = p_pic->date - p_sys->i_pts_offset + p_sys->i_field_time;
1504 block_FifoPut( p_sys->p_dts_fifo, p_block );
1511 SchroStateEnum state;
1512 state = schro_encoder_wait( p_sys->p_schro );
1515 case SCHRO_STATE_NEED_FRAME:
1518 case SCHRO_STATE_AGAIN:
1520 case SCHRO_STATE_END_OF_STREAM:
1521 p_sys->b_eos_pulled = 1;
1524 case SCHRO_STATE_HAVE_BUFFER:
1526 SchroBuffer *p_enc_buf;
1528 int i_presentation_frame;
1529 p_enc_buf = schro_encoder_pull( p_sys->p_schro, &i_presentation_frame );
1530 p_block = block_New( p_enc, p_enc_buf->length );
1534 memcpy( p_block->p_buffer, p_enc_buf->data, p_enc_buf->length );
1535 schro_buffer_unref( p_enc_buf );
1537 /* Presence of a Sequence header indicates a seek point */
1538 if( 0 == p_block->p_buffer[4] )
1540 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1542 if( !p_enc->fmt_out.p_extra ) {
1543 const uint8_t eos[] = { 'B','B','C','D',0x10,0,0,0,13,0,0,0,0 };
1544 uint32_t len = GetDWBE( p_block->p_buffer + 5 );
1545 /* if it hasn't been done so far, stash a copy of the
1546 * sequence header for muxers such as ogg */
1547 /* The OggDirac spec advises that a Dirac EOS DataUnit
1548 * is appended to the sequence header to allow guard
1549 * against poor streaming servers */
1550 /* XXX, should this be done using the packetizer ? */
1551 p_enc->fmt_out.p_extra = malloc( len + sizeof( eos ) );
1552 if( !p_enc->fmt_out.p_extra )
1554 memcpy( p_enc->fmt_out.p_extra, p_block->p_buffer, len );
1555 memcpy( (uint8_t*)p_enc->fmt_out.p_extra + len, eos, sizeof( eos ) );
1556 SetDWBE( (uint8_t*)p_enc->fmt_out.p_extra + len + 10, len );
1557 p_enc->fmt_out.i_extra = len + sizeof( eos );
1561 if( ReadDiracPictureNumber( &u_pic_num, p_block ) ) {
1562 block_t *p_dts_block = block_FifoGet( p_sys->p_dts_fifo );
1563 p_block->i_dts = p_dts_block->i_dts;
1564 p_block->i_pts = GetPicturePTS( p_enc, u_pic_num );
1565 block_Release( p_dts_block );
1566 block_ChainAppend( &p_output_chain, p_block );
1568 /* End of sequence */
1569 block_ChainAppend( &p_output_chain, p_block );
1578 return p_output_chain;
1581 /*****************************************************************************
1582 * CloseEncoder: Schro encoder destruction
1583 *****************************************************************************/
1584 static void CloseEncoder( vlc_object_t *p_this )
1586 encoder_t *p_enc = (encoder_t *)p_this;
1587 encoder_sys_t *p_sys = p_enc->p_sys;
1589 /* Free the encoder resources */
1590 if( p_sys->p_schro )
1591 schro_encoder_free( p_sys->p_schro );
1593 free( p_sys->p_format );
1595 if( p_sys->p_dts_fifo )
1596 block_FifoRelease( p_sys->p_dts_fifo );
1598 block_ChainRelease( p_sys->p_chain );