vpx: default fmt_out aspect ratio to 1:1 if unset
[vlc.git] / modules / codec / schroedinger.c
blob44afbf042db04100668cbdf5879346f10af95481
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 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <assert.h>
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_codec.h>
41 #include <schroedinger/schro.h>
43 /*****************************************************************************
44 * Module descriptor
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",
68 "constant_bitrate",
69 "low_delay",
70 "lossless",
71 "constant_lambda",
72 "constant_error",
73 "constant_quality"
76 static const char *enc_rate_control_list_text[] = {
77 N_("Constant noise threshold mode"),
78 N_("Constant bitrate mode (CBR)"),
79 N_("Low Delay mode"),
80 N_("Lossless mode"),
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[] = {
91 "adaptive",
92 "intra_only",
93 "backref",
94 "chained_backref",
95 "biref",
96 "chained_biref"
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[] = {
138 "none",
139 "center_weighted_median",
140 "gaussian",
141 "add_noise",
142 "adaptive_gaussian",
143 "lowpass"
146 static const char *enc_filtering_list_text[] = {
147 N_("No pre-filtering"),
148 N_("Centre Weighted Median"),
149 N_("Gaussian Low Pass Filter"),
150 N_("Add Noise"),
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[] = {
176 "automatic",
177 "small",
178 "medium",
179 "large"
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[] = {
193 "automatic",
194 "none",
195 "partial",
196 "full"
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[] = {
224 "desl_dubuc_9_7",
225 "le_gall_5_3",
226 "desl_dubuc_13_7",
227 "haar_0",
228 "haar_1",
229 "fidelity",
230 "daub_9_7"
233 static const char *enc_wavelet_list_text[] = {
234 "Deslauriers-Dubuc (9,7)",
235 "LeGall (5,3)",
236 "Deslauriers-Dubuc (13,7)",
237 "Haar with no shift",
238 "Haar with single shift per level",
239 "Fidelity filter",
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[] = {
264 "none",
265 "ccir959",
266 "moo",
267 "manos_sakrison"
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[] = {
290 "automatic",
291 "small",
292 "medium",
293 "large",
294 "full"
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[] = {
330 "auto",
331 "vc2_low_delay",
332 "vc2_simple",
333 "vc2_main",
334 "main"
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"),
342 N_("Main Profile"),
345 static const char *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,
353 NULL
357 /* Module declaration */
359 vlc_module_begin ()
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" )
368 /* encoder */
369 add_submodule()
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 )
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 )
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 )
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 )
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 )
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 )
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 )
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 )
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 )
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 )
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 )
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 )
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 )
521 vlc_module_end ()
524 /*****************************************************************************
525 * Local prototypes
526 *****************************************************************************/
527 static picture_t *DecodeBlock ( decoder_t *p_dec, block_t **pp_block );
528 static void Flush( decoder_t * );
530 struct picture_free_t
532 picture_t *p_pic;
533 decoder_t *p_dec;
536 /*****************************************************************************
537 * decoder_sys_t : Schroedinger decoder descriptor
538 *****************************************************************************/
539 struct decoder_sys_t
542 * Dirac properties
544 mtime_t i_lastpts;
545 mtime_t i_frame_pts_delta;
546 SchroDecoder *p_schro;
547 SchroVideoFormat *p_format;
550 /*****************************************************************************
551 * OpenDecoder: probe the decoder and return score
552 *****************************************************************************/
553 static int OpenDecoder( vlc_object_t *p_this )
555 decoder_t *p_dec = (decoder_t*)p_this;
556 decoder_sys_t *p_sys;
557 SchroDecoder *p_schro;
559 if( p_dec->fmt_in.i_codec != VLC_CODEC_DIRAC )
561 return VLC_EGENERIC;
564 /* Allocate the memory needed to store the decoder's structure */
565 p_sys = malloc(sizeof(decoder_sys_t));
566 if( p_sys == NULL )
567 return VLC_ENOMEM;
569 /* Initialise the schroedinger (and hence liboil libraries */
570 /* This does no allocation and is safe to call */
571 schro_init();
573 /* Initialise the schroedinger decoder */
574 if( !(p_schro = schro_decoder_new()) )
576 free( p_sys );
577 return VLC_EGENERIC;
580 p_dec->p_sys = p_sys;
581 p_sys->p_schro = p_schro;
582 p_sys->p_format = NULL;
583 p_sys->i_lastpts = VLC_TS_INVALID;
584 p_sys->i_frame_pts_delta = 0;
586 /* Set output properties */
587 p_dec->fmt_out.i_cat = VIDEO_ES;
588 p_dec->fmt_out.i_codec = VLC_CODEC_I420;
590 /* Set callbacks */
591 p_dec->pf_decode_video = DecodeBlock;
592 p_dec->pf_flush = Flush;
594 return VLC_SUCCESS;
597 /*****************************************************************************
598 * SetPictureFormat: Set the decoded picture params to the ones from the stream
599 *****************************************************************************/
600 static void SetVideoFormat( decoder_t *p_dec )
602 decoder_sys_t *p_sys = p_dec->p_sys;
604 p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
605 if( p_sys->p_format == NULL ) return;
607 p_sys->i_frame_pts_delta = CLOCK_FREQ
608 * p_sys->p_format->frame_rate_denominator
609 / p_sys->p_format->frame_rate_numerator;
611 switch( p_sys->p_format->chroma_format )
613 case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_CODEC_I420; break;
614 case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_CODEC_I422; break;
615 case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_CODEC_I444; break;
616 default:
617 p_dec->fmt_out.i_codec = 0;
618 break;
621 p_dec->fmt_out.video.i_visible_width = p_sys->p_format->clean_width;
622 p_dec->fmt_out.video.i_x_offset = p_sys->p_format->left_offset;
623 p_dec->fmt_out.video.i_width = p_sys->p_format->width;
625 p_dec->fmt_out.video.i_visible_height = p_sys->p_format->clean_height;
626 p_dec->fmt_out.video.i_y_offset = p_sys->p_format->top_offset;
627 p_dec->fmt_out.video.i_height = p_sys->p_format->height;
629 /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
630 p_dec->fmt_out.video.i_sar_num = p_sys->p_format->aspect_ratio_numerator;
631 p_dec->fmt_out.video.i_sar_den = p_sys->p_format->aspect_ratio_denominator;
633 p_dec->fmt_out.video.i_frame_rate =
634 p_sys->p_format->frame_rate_numerator;
635 p_dec->fmt_out.video.i_frame_rate_base =
636 p_sys->p_format->frame_rate_denominator;
639 /*****************************************************************************
640 * SchroFrameFree: schro_frame callback to release the associated picture_t
641 * When schro_decoder_reset() is called there will be pictures in the
642 * decoding pipeline that need to be released rather than displayed.
643 *****************************************************************************/
644 static void SchroFrameFree( SchroFrame *frame, void *priv)
646 struct picture_free_t *p_free = priv;
648 if( !p_free )
649 return;
651 picture_Release( p_free->p_pic );
652 free(p_free);
653 (void)frame;
656 /*****************************************************************************
657 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
658 *****************************************************************************/
659 static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
661 decoder_sys_t *p_sys = p_dec->p_sys;
662 SchroFrame *p_schroframe = schro_frame_new();
663 picture_t *p_pic = NULL;
664 struct picture_free_t *p_free;
666 if( !p_schroframe )
667 return NULL;
669 p_pic = decoder_NewPicture( p_dec );
671 if( !p_pic )
672 return NULL;
674 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
675 if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
677 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
679 else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
681 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
684 p_schroframe->width = p_sys->p_format->width;
685 p_schroframe->height = p_sys->p_format->height;
687 p_free = malloc( sizeof( *p_free ) );
688 p_free->p_pic = p_pic;
689 p_free->p_dec = p_dec;
690 schro_frame_set_free_callback( p_schroframe, SchroFrameFree, p_free );
692 for( int i=0; i<3; i++ )
694 p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
695 p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
696 p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
697 p_schroframe->components[i].length =
698 p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
699 p_schroframe->components[i].data = p_pic->p[i].p_pixels;
701 if(i!=0)
703 p_schroframe->components[i].v_shift =
704 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
705 p_schroframe->components[i].h_shift =
706 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
710 p_pic->b_progressive = !p_sys->p_format->interlaced;
711 p_pic->b_top_field_first = p_sys->p_format->top_field_first;
712 p_pic->i_nb_fields = 2;
714 return p_schroframe;
717 /*****************************************************************************
718 * SchroBufferFree: schro_buffer callback to release the associated block_t
719 *****************************************************************************/
720 static void SchroBufferFree( SchroBuffer *buf, void *priv )
722 block_t *p_block = priv;
724 if( !p_block )
725 return;
727 block_Release( p_block );
728 (void)buf;
731 /*****************************************************************************
732 * CloseDecoder: decoder destruction
733 *****************************************************************************/
734 static void CloseDecoder( vlc_object_t *p_this )
736 decoder_t *p_dec = (decoder_t *)p_this;
737 decoder_sys_t *p_sys = p_dec->p_sys;
739 schro_decoder_free( p_sys->p_schro );
740 free( p_sys );
743 /*****************************************************************************
744 * Flush:
745 *****************************************************************************/
746 static void Flush( decoder_t *p_dec )
748 decoder_sys_t *p_sys = p_dec->p_sys;
750 schro_decoder_reset( p_sys->p_schro );
751 p_sys->i_lastpts = VLC_TS_INVALID;
754 /****************************************************************************
755 * DecodeBlock: the whole thing
756 ****************************************************************************
757 * Blocks need not be Dirac dataunit aligned.
758 * If a block has a PTS signaled, it applies to the first picture at or after p_block
760 * If this function returns a picture (!NULL), it is called again and the
761 * same block is resubmitted. To avoid this, set *pp_block to NULL;
762 * If this function returns NULL, the *pp_block is lost (and leaked).
763 * This function must free all blocks when finished with them.
764 ****************************************************************************/
765 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
767 decoder_sys_t *p_sys = p_dec->p_sys;
769 if( !pp_block ) return NULL;
771 if ( *pp_block ) {
772 block_t *p_block = *pp_block;
774 /* reset the decoder when seeking as the decode in progress is invalid */
775 /* discard the block as it is just a null magic block */
776 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) ) {
777 Flush( p_dec );
779 block_Release( p_block );
780 *pp_block = NULL;
781 return NULL;
784 SchroBuffer *p_schrobuffer;
785 p_schrobuffer = schro_buffer_new_with_data( p_block->p_buffer, p_block->i_buffer );
786 p_schrobuffer->free = SchroBufferFree;
787 p_schrobuffer->priv = p_block;
788 if( p_block->i_pts > VLC_TS_INVALID ) {
789 mtime_t *p_pts = malloc( sizeof(*p_pts) );
790 if( p_pts ) {
791 *p_pts = p_block->i_pts;
792 /* if this call fails, p_pts is freed automatically */
793 p_schrobuffer->tag = schro_tag_new( p_pts, free );
797 /* this stops the same block being fed back into this function if
798 * we were on the next iteration of this loop to output a picture */
799 *pp_block = NULL;
800 schro_decoder_autoparse_push( p_sys->p_schro, p_schrobuffer );
801 /* DO NOT refer to p_block after this point, it may have been freed */
804 while( 1 )
806 SchroFrame *p_schroframe;
807 picture_t *p_pic;
808 int state = schro_decoder_autoparse_wait( p_sys->p_schro );
810 switch( state )
812 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
813 SetVideoFormat( p_dec );
814 break;
816 case SCHRO_DECODER_NEED_BITS:
817 return NULL;
819 case SCHRO_DECODER_NEED_FRAME:
820 p_schroframe = CreateSchroFrameFromPic( p_dec );
822 if( !p_schroframe )
824 msg_Err( p_dec, "Could not allocate picture for decoder");
825 return NULL;
828 schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
829 break;
831 case SCHRO_DECODER_OK: {
832 SchroTag *p_tag = schro_decoder_get_picture_tag( p_sys->p_schro );
833 p_schroframe = schro_decoder_pull( p_sys->p_schro );
834 if( !p_schroframe || !p_schroframe->priv )
836 /* frame can't be one that was allocated by us
837 * -- no private data: discard */
838 if( p_tag ) schro_tag_free( p_tag );
839 if( p_schroframe ) schro_frame_unref( p_schroframe );
840 break;
842 p_pic = ((struct picture_free_t*) p_schroframe->priv)->p_pic;
843 p_schroframe->priv = NULL;
845 if( p_tag )
847 /* free is handled by schro_frame_unref */
848 p_pic->date = *(mtime_t*) p_tag->value;
849 schro_tag_free( p_tag );
851 else if( p_sys->i_lastpts > VLC_TS_INVALID )
853 /* NB, this shouldn't happen since the packetizer does a
854 * very thorough job of inventing timestamps. The
855 * following is just a very rough fall back incase packetizer
856 * is missing. */
857 /* maybe it would be better to set p_pic->b_force ? */
858 p_pic->date = p_sys->i_lastpts + p_sys->i_frame_pts_delta;
860 p_sys->i_lastpts = p_pic->date;
862 schro_frame_unref( p_schroframe );
863 return p_pic;
865 case SCHRO_DECODER_EOS:
866 /* NB, the new api will not emit _EOS, it handles the reset internally */
867 break;
869 case SCHRO_DECODER_ERROR:
870 msg_Err( p_dec, "SCHRO_DECODER_ERROR");
871 return NULL;
876 /*****************************************************************************
877 * Local prototypes
878 *****************************************************************************/
879 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );
882 /*****************************************************************************
883 * picture_pts_t : store pts alongside picture number, not carried through
884 * encoder
885 *****************************************************************************/
886 struct picture_pts_t
888 mtime_t i_pts; /* associated pts */
889 uint32_t u_pnum; /* dirac picture number */
890 bool b_empty; /* entry is invalid */
893 /*****************************************************************************
894 * encoder_sys_t : Schroedinger encoder descriptor
895 *****************************************************************************/
896 #define SCHRO_PTS_TLB_SIZE 256
897 struct encoder_sys_t
900 * Schro properties
902 SchroEncoder *p_schro;
903 SchroVideoFormat *p_format;
904 int started;
905 bool b_auto_field_coding;
907 uint32_t i_input_picnum;
908 block_fifo_t *p_dts_fifo;
910 block_t *p_chain;
912 struct picture_pts_t pts_tlb[SCHRO_PTS_TLB_SIZE];
913 mtime_t i_pts_offset;
914 mtime_t i_field_time;
916 bool b_eos_signalled;
917 bool b_eos_pulled;
920 static struct
922 unsigned int i_height;
923 int i_approx_fps;
924 SchroVideoFormatEnum i_vf;
925 } schro_format_guess[] = {
926 /* Important: Keep this list ordered in ascending picture height */
927 {1, 0, SCHRO_VIDEO_FORMAT_CUSTOM},
928 {120, 15, SCHRO_VIDEO_FORMAT_QSIF},
929 {144, 12, SCHRO_VIDEO_FORMAT_QCIF},
930 {240, 15, SCHRO_VIDEO_FORMAT_SIF},
931 {288, 12, SCHRO_VIDEO_FORMAT_CIF},
932 {480, 30, SCHRO_VIDEO_FORMAT_SD480I_60},
933 {480, 15, SCHRO_VIDEO_FORMAT_4SIF},
934 {576, 12, SCHRO_VIDEO_FORMAT_4CIF},
935 {576, 25, SCHRO_VIDEO_FORMAT_SD576I_50},
936 {720, 50, SCHRO_VIDEO_FORMAT_HD720P_50},
937 {720, 60, SCHRO_VIDEO_FORMAT_HD720P_60},
938 {1080, 24, SCHRO_VIDEO_FORMAT_DC2K_24},
939 {1080, 25, SCHRO_VIDEO_FORMAT_HD1080I_50},
940 {1080, 30, SCHRO_VIDEO_FORMAT_HD1080I_60},
941 {1080, 50, SCHRO_VIDEO_FORMAT_HD1080P_50},
942 {1080, 60, SCHRO_VIDEO_FORMAT_HD1080P_60},
943 {2160, 24, SCHRO_VIDEO_FORMAT_DC4K_24},
944 {0, 0, 0},
947 /*****************************************************************************
948 * ResetPTStlb: Purge all entries in @p_enc@'s PTS-tlb
949 *****************************************************************************/
950 static void ResetPTStlb( encoder_t *p_enc )
952 encoder_sys_t *p_sys = p_enc->p_sys;
953 for( int i = 0; i < SCHRO_PTS_TLB_SIZE; i++ )
955 p_sys->pts_tlb[i].b_empty = true;
960 /*****************************************************************************
961 * StorePicturePTS: Store the PTS value for a particular picture number
962 *****************************************************************************/
963 static void StorePicturePTS( encoder_t *p_enc, uint32_t u_pnum, mtime_t i_pts )
965 encoder_sys_t *p_sys = p_enc->p_sys;
967 for( int i = 0; i<SCHRO_PTS_TLB_SIZE; i++ )
969 if( p_sys->pts_tlb[i].b_empty )
971 p_sys->pts_tlb[i].u_pnum = u_pnum;
972 p_sys->pts_tlb[i].i_pts = i_pts;
973 p_sys->pts_tlb[i].b_empty = false;
975 return;
979 msg_Err( p_enc, "Could not store PTS %"PRId64" for frame %u", i_pts, u_pnum );
982 /*****************************************************************************
983 * GetPicturePTS: Retrieve the PTS value for a particular picture number
984 *****************************************************************************/
985 static mtime_t GetPicturePTS( encoder_t *p_enc, uint32_t u_pnum )
987 encoder_sys_t *p_sys = p_enc->p_sys;
989 for( int i = 0; i < SCHRO_PTS_TLB_SIZE; i++ )
991 if( !p_sys->pts_tlb[i].b_empty &&
992 p_sys->pts_tlb[i].u_pnum == u_pnum )
994 p_sys->pts_tlb[i].b_empty = true;
995 return p_sys->pts_tlb[i].i_pts;
999 msg_Err( p_enc, "Could not retrieve PTS for picture %u", u_pnum );
1000 return 0;
1003 static inline bool SchroSetEnum( const encoder_t *p_enc, int i_list_size, const char *list[],
1004 const char *psz_name, const char *psz_name_text, const char *psz_value)
1006 encoder_sys_t *p_sys = p_enc->p_sys;
1007 if( list && psz_name_text && psz_name && psz_value ) {
1008 for( int i = 0; i < i_list_size; ++i ) {
1009 if( strcmp( list[i], psz_value ) )
1010 continue;
1011 schro_encoder_setting_set_double( p_sys->p_schro, psz_name, i );
1012 return true;
1014 msg_Err( p_enc, "Invalid %s: %s", psz_name_text, psz_value );
1016 return false;
1019 static bool SetEncChromaFormat( encoder_t *p_enc, uint32_t i_codec )
1021 encoder_sys_t *p_sys = p_enc->p_sys;
1023 switch( i_codec ) {
1024 case VLC_CODEC_I420:
1025 p_enc->fmt_in.i_codec = i_codec;
1026 p_enc->fmt_in.video.i_bits_per_pixel = 12;
1027 p_sys->p_format->chroma_format = SCHRO_CHROMA_420;
1028 break;
1029 case VLC_CODEC_I422:
1030 p_enc->fmt_in.i_codec = i_codec;
1031 p_enc->fmt_in.video.i_bits_per_pixel = 16;
1032 p_sys->p_format->chroma_format = SCHRO_CHROMA_422;
1033 break;
1034 case VLC_CODEC_I444:
1035 p_enc->fmt_in.i_codec = i_codec;
1036 p_enc->fmt_in.video.i_bits_per_pixel = 24;
1037 p_sys->p_format->chroma_format = SCHRO_CHROMA_444;
1038 break;
1039 default:
1040 return false;
1043 return true;
1046 #define SCHRO_SET_FLOAT(psz_name, pschro_name) \
1047 f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX psz_name ); \
1048 if( f_tmp >= 0.0 ) \
1049 schro_encoder_setting_set_double( p_sys->p_schro, pschro_name, f_tmp );
1051 #define SCHRO_SET_INTEGER(psz_name, pschro_name, ignore_val) \
1052 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX psz_name ); \
1053 if( i_tmp > ignore_val ) \
1054 schro_encoder_setting_set_double( p_sys->p_schro, pschro_name, i_tmp );
1056 #define SCHRO_SET_ENUM(list, psz_name, psz_name_text, pschro_name) \
1057 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX psz_name ); \
1058 if( !psz_tmp ) \
1059 goto error; \
1060 else if ( *psz_tmp != '\0' ) { \
1061 int i_list_size = ARRAY_SIZE(list); \
1062 if( !SchroSetEnum( p_enc, i_list_size, list, pschro_name, psz_name_text, psz_tmp ) ) { \
1063 free( psz_tmp ); \
1064 goto error; \
1067 free( psz_tmp );
1069 /*****************************************************************************
1070 * OpenEncoder: probe the encoder and return score
1071 *****************************************************************************/
1072 static int OpenEncoder( vlc_object_t *p_this )
1074 encoder_t *p_enc = (encoder_t *)p_this;
1075 encoder_sys_t *p_sys;
1076 int i_tmp;
1077 float f_tmp;
1078 char *psz_tmp;
1080 if( p_enc->fmt_out.i_codec != VLC_CODEC_DIRAC &&
1081 !p_enc->b_force )
1083 return VLC_EGENERIC;
1086 if( !p_enc->fmt_in.video.i_frame_rate || !p_enc->fmt_in.video.i_frame_rate_base ||
1087 !p_enc->fmt_in.video.i_visible_height || !p_enc->fmt_in.video.i_visible_width )
1089 msg_Err( p_enc, "Framerate and picture dimensions must be non-zero" );
1090 return VLC_EGENERIC;
1093 /* Allocate the memory needed to store the decoder's structure */
1094 if( ( p_sys = calloc( 1, sizeof( *p_sys ) ) ) == NULL )
1095 return VLC_ENOMEM;
1097 p_enc->p_sys = p_sys;
1098 p_enc->pf_encode_video = Encode;
1099 p_enc->fmt_out.i_codec = VLC_CODEC_DIRAC;
1100 p_enc->fmt_out.i_cat = VIDEO_ES;
1102 if( ( p_sys->p_dts_fifo = block_FifoNew() ) == NULL )
1104 CloseEncoder( p_this );
1105 return VLC_ENOMEM;
1108 ResetPTStlb( p_enc );
1110 /* guess the video format based upon number of lines and picture height */
1111 int i = 0;
1112 SchroVideoFormatEnum guessed_video_fmt = SCHRO_VIDEO_FORMAT_CUSTOM;
1113 /* Pick the dirac_video_format in this order of preference:
1114 * 1. an exact match in frame height and an approximate fps match
1115 * 2. the previous preset with a smaller number of lines.
1119 if( schro_format_guess[i].i_height > p_enc->fmt_in.video.i_height )
1121 guessed_video_fmt = schro_format_guess[i-1].i_vf;
1122 break;
1124 if( schro_format_guess[i].i_height != p_enc->fmt_in.video.i_height )
1125 continue;
1126 int src_fps = p_enc->fmt_in.video.i_frame_rate / p_enc->fmt_in.video.i_frame_rate_base;
1127 int delta_fps = abs( schro_format_guess[i].i_approx_fps - src_fps );
1128 if( delta_fps > 2 )
1129 continue;
1131 guessed_video_fmt = schro_format_guess[i].i_vf;
1132 break;
1133 } while( schro_format_guess[++i].i_height );
1135 schro_init();
1136 p_sys->p_schro = schro_encoder_new();
1137 if( !p_sys->p_schro ) {
1138 msg_Err( p_enc, "Failed to initialize libschroedinger encoder" );
1139 return VLC_EGENERIC;
1141 schro_encoder_set_packet_assembly( p_sys->p_schro, true );
1143 if( !( p_sys->p_format = schro_encoder_get_video_format( p_sys->p_schro ) ) ) {
1144 msg_Err( p_enc, "Failed to get Schroedigner video format" );
1145 schro_encoder_free( p_sys->p_schro );
1146 return VLC_EGENERIC;
1149 /* initialise the video format parameters to the guessed format */
1150 schro_video_format_set_std_video_format( p_sys->p_format, guessed_video_fmt );
1152 /* constants set from the input video format */
1153 p_sys->p_format->width = p_enc->fmt_in.video.i_visible_width;
1154 p_sys->p_format->height = p_enc->fmt_in.video.i_visible_height;
1155 p_sys->p_format->frame_rate_numerator = p_enc->fmt_in.video.i_frame_rate;
1156 p_sys->p_format->frame_rate_denominator = p_enc->fmt_in.video.i_frame_rate_base;
1157 unsigned u_asr_num, u_asr_den;
1158 vlc_ureduce( &u_asr_num, &u_asr_den,
1159 p_enc->fmt_in.video.i_sar_num,
1160 p_enc->fmt_in.video.i_sar_den, 0 );
1161 p_sys->p_format->aspect_ratio_numerator = u_asr_num;
1162 p_sys->p_format->aspect_ratio_denominator = u_asr_den;
1164 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1166 SCHRO_SET_ENUM(enc_rate_control_list, ENC_RATE_CONTROL, ENC_RATE_CONTROL_TEXT, "rate_control")
1168 SCHRO_SET_ENUM(enc_gop_structure_list, ENC_GOP_STRUCTURE, ENC_GOP_STRUCTURE_TEXT, "gop_structure")
1170 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_CHROMAFMT );
1171 if( !psz_tmp )
1172 goto error;
1173 else {
1174 uint32_t i_codec;
1175 if( !strcmp( psz_tmp, "420" ) ) {
1176 i_codec = VLC_CODEC_I420;
1178 else if( !strcmp( psz_tmp, "422" ) ) {
1179 i_codec = VLC_CODEC_I422;
1181 else if( !strcmp( psz_tmp, "444" ) ) {
1182 i_codec = VLC_CODEC_I444;
1184 else {
1185 msg_Err( p_enc, "Invalid chroma format: %s", psz_tmp );
1186 free( psz_tmp );
1187 goto error;
1189 SetEncChromaFormat( p_enc, i_codec );
1191 free( psz_tmp );
1193 SCHRO_SET_FLOAT(ENC_QUALITY, "quality")
1195 SCHRO_SET_FLOAT(ENC_NOISE_THRESHOLD, "noise_threshold")
1197 /* use bitrate from sout-transcode-vb in kbps */
1198 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_BITRATE );
1199 if( i_tmp > -1 )
1200 schro_encoder_setting_set_double( p_sys->p_schro, "bitrate", i_tmp * 1000 );
1201 else
1202 schro_encoder_setting_set_double( p_sys->p_schro, "bitrate", p_enc->fmt_out.i_bitrate );
1204 p_enc->fmt_out.i_bitrate = schro_encoder_setting_get_double( p_sys->p_schro, "bitrate" );
1206 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_MIN_BITRATE );
1207 if( i_tmp > -1 )
1208 schro_encoder_setting_set_double( p_sys->p_schro, "min_bitrate", i_tmp * 1000 );
1210 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_MAX_BITRATE );
1211 if( i_tmp > -1 )
1212 schro_encoder_setting_set_double( p_sys->p_schro, "max_bitrate", i_tmp * 1000 );
1214 SCHRO_SET_INTEGER(ENC_AU_DISTANCE, "au_distance", -1)
1216 SCHRO_SET_ENUM(enc_filtering_list, ENC_PREFILTER, ENC_PREFILTER_TEXT, "filtering")
1218 SCHRO_SET_FLOAT(ENC_PREFILTER_STRENGTH, "filter_value")
1221 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_CODINGMODE );
1222 if( !psz_tmp )
1223 goto error;
1224 else if( !strcmp( psz_tmp, "auto" ) ) {
1225 p_sys->b_auto_field_coding = true;
1227 else if( !strcmp( psz_tmp, "progressive" ) ) {
1228 p_sys->b_auto_field_coding = false;
1229 schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", false);
1231 else if( !strcmp( psz_tmp, "field" ) ) {
1232 p_sys->b_auto_field_coding = false;
1233 schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", true);
1235 else {
1236 msg_Err( p_enc, "Invalid codingmode: %s", psz_tmp );
1237 free( psz_tmp );
1238 goto error;
1240 free( psz_tmp );
1242 SCHRO_SET_ENUM(enc_block_size_list, ENC_MCBLK_SIZE, ENC_MCBLK_SIZE_TEXT, "motion_block_size")
1244 SCHRO_SET_ENUM(enc_block_overlap_list, ENC_MCBLK_OVERLAP, ENC_MCBLK_OVERLAP_TEXT, "motion_block_overlap")
1246 psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_MVPREC );
1247 if( !psz_tmp )
1248 goto error;
1249 else if( *psz_tmp != '\0') {
1250 if( !strcmp( psz_tmp, "1" ) ) {
1251 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 0 );
1253 else if( !strcmp( psz_tmp, "1/2" ) ) {
1254 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 1 );
1256 else if( !strcmp( psz_tmp, "1/4" ) ) {
1257 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 2 );
1259 else if( !strcmp( psz_tmp, "1/8" ) ) {
1260 schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 3 );
1262 else {
1263 msg_Err( p_enc, "Invalid mv_precision: %s", psz_tmp );
1264 free( psz_tmp );
1265 goto error;
1268 free( psz_tmp );
1270 SCHRO_SET_INTEGER(ENC_ME_COMBINED, "enable_chroma_me", -1)
1272 SCHRO_SET_ENUM(enc_wavelet_list, ENC_DWTINTRA, ENC_DWTINTRA_TEXT, "intra_wavelet")
1274 SCHRO_SET_ENUM(enc_wavelet_list, ENC_DWTINTER, ENC_DWTINTER_TEXT, "inter_wavelet")
1276 SCHRO_SET_INTEGER(ENC_DWTDEPTH, "transform_depth", -1)
1278 SCHRO_SET_INTEGER(ENC_MULTIQUANT, "enable_multiquant", -1)
1280 SCHRO_SET_INTEGER(ENC_NOAC, "enable_noarith", -1)
1282 SCHRO_SET_ENUM(enc_perceptual_weighting_list, ENC_PWT, ENC_PWT_TEXT, "perceptual_weighting")
1284 SCHRO_SET_FLOAT(ENC_PDIST, "perceptual_distance")
1286 SCHRO_SET_INTEGER(ENC_HSLICES, "horiz_slices", -1)
1288 SCHRO_SET_INTEGER(ENC_VSLICES, "vert_slices", -1)
1290 SCHRO_SET_ENUM(enc_codeblock_size_list, ENC_SCBLK_SIZE, ENC_SCBLK_SIZE_TEXT, "codeblock_size")
1292 SCHRO_SET_INTEGER(ENC_ME_HIERARCHICAL, "enable_hierarchical_estimation", -1)
1294 SCHRO_SET_INTEGER(ENC_ME_DOWNSAMPLE_LEVELS, "downsample_levels", 1)
1296 SCHRO_SET_INTEGER(ENC_ME_GLOBAL_MOTION, "enable_global_motion", -1)
1298 SCHRO_SET_INTEGER(ENC_ME_PHASECORR, "enable_phasecorr_estimation", -1)
1300 SCHRO_SET_INTEGER(ENC_SCD, "enable_scene_change_detection", -1)
1302 SCHRO_SET_ENUM(enc_profile_list, ENC_FORCE_PROFILE, ENC_FORCE_PROFILE_TEXT, "force_profile")
1304 p_sys->started = 0;
1306 return VLC_SUCCESS;
1307 error:
1308 CloseEncoder( p_this );
1309 return VLC_EGENERIC;
1313 struct enc_picture_free_t
1315 picture_t *p_pic;
1316 encoder_t *p_enc;
1319 /*****************************************************************************
1320 * EncSchroFrameFree: schro_frame callback to release the associated picture_t
1321 * When schro_encoder_reset() is called there will be pictures in the
1322 * encoding pipeline that need to be released rather than displayed.
1323 *****************************************************************************/
1324 static void EncSchroFrameFree( SchroFrame *frame, void *priv )
1326 struct enc_picture_free_t *p_free = priv;
1328 if( !p_free )
1329 return;
1331 picture_Release( p_free->p_pic );
1332 free( p_free );
1333 (void)frame;
1336 /*****************************************************************************
1337 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
1338 *****************************************************************************/
1339 static SchroFrame *CreateSchroFrameFromInputPic( encoder_t *p_enc, picture_t *p_pic )
1341 encoder_sys_t *p_sys = p_enc->p_sys;
1342 SchroFrame *p_schroframe = schro_frame_new();
1343 struct enc_picture_free_t *p_free;
1345 if( !p_schroframe )
1346 return NULL;
1348 if( !p_pic )
1349 return NULL;
1351 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
1352 if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
1354 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
1356 else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
1358 p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
1361 p_schroframe->width = p_sys->p_format->width;
1362 p_schroframe->height = p_sys->p_format->height;
1364 p_free = malloc( sizeof( *p_free ) );
1365 if( unlikely( p_free == NULL ) ) {
1366 schro_frame_unref( p_schroframe );
1367 return NULL;
1370 p_free->p_pic = p_pic;
1371 p_free->p_enc = p_enc;
1372 schro_frame_set_free_callback( p_schroframe, EncSchroFrameFree, p_free );
1374 for( int i=0; i<3; i++ )
1376 p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
1377 p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
1378 p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
1379 p_schroframe->components[i].length =
1380 p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
1381 p_schroframe->components[i].data = p_pic->p[i].p_pixels;
1383 if( i!=0 )
1385 p_schroframe->components[i].v_shift =
1386 SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
1387 p_schroframe->components[i].h_shift =
1388 SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
1392 return p_schroframe;
1395 /* Attempt to find dirac picture number in an encapsulation unit */
1396 static int ReadDiracPictureNumber( uint32_t *p_picnum, block_t *p_block )
1398 uint32_t u_pos = 4;
1399 /* protect against falling off the edge */
1400 while( u_pos + 13 < p_block->i_buffer )
1402 /* find the picture startcode */
1403 if( p_block->p_buffer[u_pos] & 0x08 )
1405 *p_picnum = GetDWBE( p_block->p_buffer + u_pos + 9 );
1406 return 1;
1408 /* skip to the next dirac data unit */
1409 uint32_t u_npo = GetDWBE( p_block->p_buffer + u_pos + 1 );
1410 assert( u_npo <= UINT32_MAX - u_pos );
1411 if( u_npo == 0 )
1412 u_npo = 13;
1413 u_pos += u_npo;
1415 return 0;
1419 static block_t *Encode( encoder_t *p_enc, picture_t *p_pic )
1421 encoder_sys_t *p_sys = p_enc->p_sys;
1422 block_t *p_block, *p_output_chain = NULL;
1423 SchroFrame *p_frame;
1424 bool b_go = true;
1426 if( !p_pic ) {
1427 if( !p_sys->started || p_sys->b_eos_pulled )
1428 return NULL;
1430 if( !p_sys->b_eos_signalled ) {
1431 p_sys->b_eos_signalled = 1;
1432 schro_encoder_end_of_stream( p_sys->p_schro );
1434 } else {
1435 /* we only know if the sequence is interlaced when the first
1436 * picture arrives, so final setup is done here */
1437 /* XXX todo, detect change of interlace */
1438 p_sys->p_format->interlaced = !p_pic->b_progressive;
1439 p_sys->p_format->top_field_first = p_pic->b_top_field_first;
1441 if( p_sys->b_auto_field_coding )
1442 schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", !p_pic->b_progressive );
1445 if( !p_sys->started ) {
1446 date_t date;
1448 if( p_pic->format.i_chroma != p_enc->fmt_in.i_codec ) {
1449 char chroma_in[5], chroma_out[5];
1450 vlc_fourcc_to_char( p_pic->format.i_chroma, chroma_in );
1451 chroma_in[4] = '\0';
1452 chroma_out[4] = '\0';
1453 vlc_fourcc_to_char( p_enc->fmt_in.i_codec, chroma_out );
1454 msg_Warn( p_enc, "Resetting chroma from %s to %s", chroma_out, chroma_in );
1455 if( !SetEncChromaFormat( p_enc, p_pic->format.i_chroma ) ) {
1456 msg_Err( p_enc, "Could not reset chroma format to %s", chroma_in );
1457 return NULL;
1461 date_Init( &date, p_enc->fmt_in.video.i_frame_rate, p_enc->fmt_in.video.i_frame_rate_base );
1462 /* FIXME - Unlike dirac-research codec Schro doesn't have a function that returns the delay in pics yet.
1463 * Use a default of 1
1465 date_Increment( &date, 1 );
1466 p_sys->i_pts_offset = date_Get( &date );
1467 if( schro_encoder_setting_get_double( p_sys->p_schro, "interlaced_coding" ) > 0.0 ) {
1468 date_Set( &date, 0 );
1469 date_Increment( &date, 1);
1470 p_sys->i_field_time = date_Get( &date ) / 2;
1473 schro_video_format_set_std_signal_range( p_sys->p_format, SCHRO_SIGNAL_RANGE_8BIT_VIDEO );
1474 schro_encoder_set_video_format( p_sys->p_schro, p_sys->p_format );
1475 schro_encoder_start( p_sys->p_schro );
1476 p_sys->started = 1;
1479 if( !p_sys->b_eos_signalled ) {
1480 /* create a schro frame from the input pic and load */
1481 /* Increase ref count by 1 so that the picture is not freed until
1482 Schro finishes with it */
1483 picture_Hold( p_pic );
1485 p_frame = CreateSchroFrameFromInputPic( p_enc, p_pic );
1486 if( !p_frame )
1487 return NULL;
1488 schro_encoder_push_frame( p_sys->p_schro, p_frame );
1491 /* store pts in a lookaside buffer, so that the same pts may
1492 * be used for the picture in coded order */
1493 StorePicturePTS( p_enc, p_sys->i_input_picnum, p_pic->date );
1494 p_sys->i_input_picnum++;
1496 /* store dts in a queue, so that they appear in order in
1497 * coded order */
1498 p_block = block_Alloc( 1 );
1499 if( !p_block )
1500 return NULL;
1501 p_block->i_dts = p_pic->date - p_sys->i_pts_offset;
1502 block_FifoPut( p_sys->p_dts_fifo, p_block );
1503 p_block = NULL;
1505 /* for field coding mode, insert an extra value into both the
1506 * pts lookaside buffer and dts queue, offset to correspond
1507 * to a one field delay. */
1508 if( schro_encoder_setting_get_double( p_sys->p_schro, "interlaced_coding" ) > 0.0 ) {
1509 StorePicturePTS( p_enc, p_sys->i_input_picnum, p_pic->date + p_sys->i_field_time );
1510 p_sys->i_input_picnum++;
1512 p_block = block_Alloc( 1 );
1513 if( !p_block )
1514 return NULL;
1515 p_block->i_dts = p_pic->date - p_sys->i_pts_offset + p_sys->i_field_time;
1516 block_FifoPut( p_sys->p_dts_fifo, p_block );
1517 p_block = NULL;
1523 SchroStateEnum state;
1524 state = schro_encoder_wait( p_sys->p_schro );
1525 switch( state )
1527 case SCHRO_STATE_NEED_FRAME:
1528 b_go = false;
1529 break;
1530 case SCHRO_STATE_AGAIN:
1531 break;
1532 case SCHRO_STATE_END_OF_STREAM:
1533 p_sys->b_eos_pulled = 1;
1534 b_go = false;
1535 break;
1536 case SCHRO_STATE_HAVE_BUFFER:
1538 SchroBuffer *p_enc_buf;
1539 uint32_t u_pic_num;
1540 int i_presentation_frame;
1541 p_enc_buf = schro_encoder_pull( p_sys->p_schro, &i_presentation_frame );
1542 p_block = block_Alloc( p_enc_buf->length );
1543 if( !p_block )
1544 return NULL;
1546 memcpy( p_block->p_buffer, p_enc_buf->data, p_enc_buf->length );
1547 schro_buffer_unref( p_enc_buf );
1549 /* Presence of a Sequence header indicates a seek point */
1550 if( 0 == p_block->p_buffer[4] )
1552 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1554 if( !p_enc->fmt_out.p_extra ) {
1555 const uint8_t eos[] = { 'B','B','C','D',0x10,0,0,0,13,0,0,0,0 };
1556 uint32_t len = GetDWBE( p_block->p_buffer + 5 );
1557 /* if it hasn't been done so far, stash a copy of the
1558 * sequence header for muxers such as ogg */
1559 /* The OggDirac spec advises that a Dirac EOS DataUnit
1560 * is appended to the sequence header to allow guard
1561 * against poor streaming servers */
1562 /* XXX, should this be done using the packetizer ? */
1564 if( len > UINT32_MAX - sizeof( eos ) )
1565 return NULL;
1567 p_enc->fmt_out.p_extra = malloc( len + sizeof( eos ) );
1568 if( !p_enc->fmt_out.p_extra )
1569 return NULL;
1570 memcpy( p_enc->fmt_out.p_extra, p_block->p_buffer, len );
1571 memcpy( (uint8_t*)p_enc->fmt_out.p_extra + len, eos, sizeof( eos ) );
1572 SetDWBE( (uint8_t*)p_enc->fmt_out.p_extra + len + sizeof(eos) - 4, len );
1573 p_enc->fmt_out.i_extra = len + sizeof( eos );
1577 if( ReadDiracPictureNumber( &u_pic_num, p_block ) ) {
1578 block_t *p_dts_block = block_FifoGet( p_sys->p_dts_fifo );
1579 p_block->i_dts = p_dts_block->i_dts;
1580 p_block->i_pts = GetPicturePTS( p_enc, u_pic_num );
1581 block_Release( p_dts_block );
1582 block_ChainAppend( &p_output_chain, p_block );
1583 } else {
1584 /* End of sequence */
1585 block_ChainAppend( &p_output_chain, p_block );
1587 break;
1589 default:
1590 break;
1592 } while( b_go );
1594 return p_output_chain;
1597 /*****************************************************************************
1598 * CloseEncoder: Schro encoder destruction
1599 *****************************************************************************/
1600 static void CloseEncoder( vlc_object_t *p_this )
1602 encoder_t *p_enc = (encoder_t *)p_this;
1603 encoder_sys_t *p_sys = p_enc->p_sys;
1605 /* Free the encoder resources */
1606 if( p_sys->p_schro )
1607 schro_encoder_free( p_sys->p_schro );
1609 free( p_sys->p_format );
1611 if( p_sys->p_dts_fifo )
1612 block_FifoRelease( p_sys->p_dts_fifo );
1614 block_ChainRelease( p_sys->p_chain );
1616 free( p_sys );