schroedinger.c 55.9 KB
Newer Older
1
2
3
4
5
/*****************************************************************************
 * schroedinger.c: Dirac decoder module making use of libschroedinger.
 *          (http://www.bbc.co.uk/rd/projects/dirac/index.shtml)
 *          (http://diracvideo.org)
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
6
 * Copyright (C) 2008-2011 VLC authors and VideoLAN
7
8
9
 *
 * Authors: Jonathan Rosser <jonathan.rosser@gmail.com>
 *          David Flynn <davidf at rd dot bbc.co.uk>
10
 *          Anuradha Suraparaju <asuraparaju at gmail dot com>
11
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
12
13
14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15
16
17
18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
19
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
22
23
24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
26
27
28
29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30

31
32
33
34
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

35
36
#include <assert.h>

37
38
#include <vlc_common.h>
#include <vlc_plugin.h>
39
#include <vlc_codec.h>                              /* decoder_DeletePicture */
40
41
42

#include <schroedinger/schro.h>

43
44
45
46
47
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int        OpenDecoder  ( vlc_object_t * );
static void       CloseDecoder ( vlc_object_t * );
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
static int        OpenEncoder  ( vlc_object_t * );
static void       CloseEncoder ( vlc_object_t * );

#define ENC_CFG_PREFIX "sout-schro-"

#define ENC_CHROMAFMT "chroma-fmt"
#define ENC_CHROMAFMT_TEXT N_("Chroma format")
#define ENC_CHROMAFMT_LONGTEXT N_("Picking chroma format will force a " \
                                  "conversion of the video into that format")
static const char *const enc_chromafmt_list[] =
  { "420", "422", "444" };
static const char *const enc_chromafmt_list_text[] =
  { N_("4:2:0"), N_("4:2:2"), N_("4:4:4") };

#define ENC_RATE_CONTROL "rate-control"
#define ENC_RATE_CONTROL_TEXT N_("Rate control method")
#define ENC_RATE_CONTROL_LONGTEXT N_("Method used to encode the video sequence")

static const char *enc_rate_control_list[] = {
  "constant_noise_threshold",
  "constant_bitrate",
  "low_delay",
  "lossless",
  "constant_lambda",
  "constant_error",
  "constant_quality"
};

static const char *enc_rate_control_list_text[] = {
  N_("Constant noise threshold mode"),
  N_("Constant bitrate mode (CBR)"),
  N_("Low Delay mode"),
  N_("Lossless mode"),
  N_("Constant lambda mode"),
  N_("Constant error mode"),
  N_("Constant quality mode")
};

#define ENC_GOP_STRUCTURE "gop-structure"
#define ENC_GOP_STRUCTURE_TEXT N_("GOP structure")
#define ENC_GOP_STRUCTURE_LONGTEXT N_("GOP structure used to encode the video sequence")

static const char *enc_gop_structure_list[] = {
  "adaptive",
  "intra_only",
  "backref",
  "chained_backref",
  "biref",
  "chained_biref"
};

static const char *enc_gop_structure_list_text[] = {
  N_("No fixed gop structure. A picture can be intra or inter and refer to previous or future pictures."),
  N_("I-frame only sequence"),
  N_("Inter pictures refere to previous pictures only"),
  N_("Inter pictures refere to previous pictures only"),
  N_("Inter pictures can refer to previous or future pictures"),
  N_("Inter pictures can refer to previous or future pictures")
};

#define ENC_QUALITY "quality"
#define ENC_QUALITY_TEXT N_("Constant quality factor")
#define ENC_QUALITY_LONGTEXT N_("Quality factor to use in constant quality mode")

#define ENC_NOISE_THRESHOLD "noise-threshold"
#define ENC_NOISE_THRESHOLD_TEXT N_("Noise Threshold")
#define ENC_NOISE_THRESHOLD_LONGTEXT N_("Noise threshold to use in constant noise threshold mode")

#define ENC_BITRATE "bitrate"
#define ENC_BITRATE_TEXT N_("CBR bitrate (kbps)")
#define ENC_BITRATE_LONGTEXT N_("Target bitrate in kbps when encoding in constant bitrate mode")

#define ENC_MAX_BITRATE "max-bitrate"
#define ENC_MAX_BITRATE_TEXT N_("Maximum bitrate (kbps)")
#define ENC_MAX_BITRATE_LONGTEXT N_("Maximum bitrate in kbps when encoding in constant bitrate mode")

#define ENC_MIN_BITRATE "min-bitrate"
#define ENC_MIN_BITRATE_TEXT N_("Minimum bitrate (kbps)")
#define ENC_MIN_BITRATE_LONGTEXT N_("Minimum bitrate in kbps when encoding in constant bitrate mode")

#define ENC_AU_DISTANCE "gop-length"
#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")
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151


#define ENC_PREFILTER "filtering"
#define ENC_PREFILTER_TEXT N_("Prefilter")
#define ENC_PREFILTER_LONGTEXT N_("Enable adaptive prefiltering")

static const char *enc_filtering_list[] = {
  "none",
  "center_weighted_median",
  "gaussian",
  "add_noise",
  "adaptive_gaussian",
  "lowpass"
};

static const char *enc_filtering_list_text[] = {
  N_("No pre-filtering"),
  N_("Centre Weighted Median"),
  N_("Gaussian Low Pass Filter"),
  N_("Add Noise"),
  N_("Gaussian Adaptive Low Pass Filter"),
152
  N_("Low Pass Filter"),
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
};

#define ENC_PREFILTER_STRENGTH "filter-value"
#define ENC_PREFILTER_STRENGTH_TEXT N_("Amount of prefiltering")
#define ENC_PREFILTER_STRENGTH_LONGTEXT N_("Higher value implies more prefiltering")

#define ENC_CODINGMODE "coding-mode"
#define ENC_CODINGMODE_TEXT N_("Picture coding mode")
#define ENC_CODINGMODE_LONGTEXT N_("Field coding is where interlaced fields are coded" \
                                   " separately as opposed to a pseudo-progressive frame")
static const char *const enc_codingmode_list[] =
  { "auto", "progressive", "field" };
static const char *const enc_codingmode_list_text[] =
  { N_("auto - let encoder decide based upon input (Best)"),
    N_("force coding frame as single picture"),
    N_("force coding frame as separate interlaced fields"),
  };

/* advanced option only */
#define ENC_MCBLK_SIZE "motion-block-size"
#define ENC_MCBLK_SIZE_TEXT N_("Size of motion compensation blocks")

static const char *enc_block_size_list[] = {
  "automatic",
  "small",
  "medium",
  "large"
};
static const char *const enc_block_size_list_text[] =
  { N_("automatic - let encoder decide based upon input (Best)"),
    N_("small - use small motion compensation blocks"),
    N_("medium - use medium motion compensation blocks"),
    N_("large - use large motion compensation blocks"),
  };

/* advanced option only */
#define ENC_MCBLK_OVERLAP "motion-block-overlap"
#define ENC_MCBLK_OVERLAP_TEXT N_("Overlap of motion compensation blocks")

static const char *enc_block_overlap_list[] = {
  "automatic",
  "none",
  "partial",
  "full"
};
static const char *const enc_block_overlap_list_text[] =
  { N_("automatic - let encoder decide based upon input (Best)"),
    N_("none - Motion compensation blocks do not overlap"),
    N_("partial - Motion compensation blocks only partially overlap"),
    N_("full - Motion compensation blocks fully overlap"),
  };


#define ENC_MVPREC "mv-precision"
#define ENC_MVPREC_TEXT N_("Motion Vector precision")
#define ENC_MVPREC_LONGTEXT N_("Motion Vector precision in pels")
static const char *const enc_mvprec_list[] =
  { "1", "1/2", "1/4", "1/8" };

/* advanced option only */
#define ENC_ME_COMBINED "me-combined"
#define ENC_ME_COMBINED_TEXT N_("Three component motion estimation")
#define ENC_ME_COMBINED_LONGTEXT N_("Use chroma as part of the motion estimation process")

#define ENC_DWTINTRA "intra-wavelet"
#define ENC_DWTINTRA_TEXT N_("Intra picture DWT filter")

#define ENC_DWTINTER "inter-wavelet"
#define ENC_DWTINTER_TEXT N_("Inter picture DWT filter")

static const char *enc_wavelet_list[] = {
  "desl_dubuc_9_7",
  "le_gall_5_3",
  "desl_dubuc_13_7",
  "haar_0",
  "haar_1",
  "fidelity",
  "daub_9_7"
};

static const char *enc_wavelet_list_text[] = {
  "Deslauriers-Dubuc (9,7)",
  "LeGall (5,3)",
  "Deslauriers-Dubuc (13,7)",
  "Haar with no shift",
  "Haar with single shift per level",
  "Fidelity filter",
  "Daubechies (9,7) integer approximation"
};

#define ENC_DWTDEPTH "transform-depth"
#define ENC_DWTDEPTH_TEXT N_("Number of DWT iterations")
#define ENC_DWTDEPTH_LONGTEXT N_("Also known as DWT levels")


/* advanced option only */
#define ENC_MULTIQUANT "enable-multiquant"
#define ENC_MULTIQUANT_TEXT N_("Enable multiple quantizers")
#define ENC_MULTIQUANT_LONGTEXT N_("Enable multiple quantizers per subband (one per codeblock)")

/* advanced option only */
#define ENC_NOAC "enable-noarith"
#define ENC_NOAC_TEXT N_("Disable arithmetic coding")
#define ENC_NOAC_LONGTEXT N_("Use variable length codes instead, useful for very high bitrates")

/* visual modelling */
/* advanced option only */
#define ENC_PWT "perceptual-weighting"
#define ENC_PWT_TEXT N_("perceptual weighting method")

static const char *enc_perceptual_weighting_list[] = {
  "none",
  "ccir959",
  "moo",
  "manos_sakrison"
};

/* advanced option only */
#define ENC_PDIST "perceptual-distance"
#define ENC_PDIST_TEXT N_("perceptual distance")
#define ENC_PDIST_LONGTEXT N_("perceptual distance to calculate perceptual weight")

/* advanced option only */
#define ENC_HSLICES "horiz-slices"
#define ENC_HSLICES_TEXT N_("Horizontal slices per frame")
#define ENC_HSLICES_LONGTEXT N_("Number of horizontal slices per frame in low delay mode")

/* advanced option only */
#define ENC_VSLICES "vert-slices"
#define ENC_VSLICES_TEXT N_("Vertical slices per frame")
#define ENC_VSLICES_LONGTEXT N_("Number of vertical slices per frame in low delay mode")

/* advanced option only */
#define ENC_SCBLK_SIZE "codeblock-size"
#define ENC_SCBLK_SIZE_TEXT N_("Size of code blocks in each subband")

static const char *enc_codeblock_size_list[] = {
  "automatic",
  "small",
  "medium",
  "large",
  "full"
};
static const char *const enc_codeblock_size_list_text[] =
  { N_("automatic - let encoder decide based upon input (Best)"),
    N_("small - use small code blocks"),
    N_("medium - use medium sized code blocks"),
    N_("large - use large code blocks"),
    N_("full - One code block per subband"),
  };

/* advanced option only */
#define ENC_ME_HIERARCHICAL "enable-hierarchical-me"
#define ENC_ME_HIERARCHICAL_TEXT N_("Enable hierarchical Motion Estimation")

/* advanced option only */
#define ENC_ME_DOWNSAMPLE_LEVELS "downsample-levels"
#define ENC_ME_DOWNSAMPLE_LEVELS_TEXT N_("Number of levels of downsampling")
#define ENC_ME_DOWNSAMPLE_LEVELS_LONGTEXT N_("Number of levels of downsampling in hierarchical motion estimation mode")

/* advanced option only */
#define ENC_ME_GLOBAL_MOTION "enable-global-me"
#define ENC_ME_GLOBAL_MOTION_TEXT N_("Enable Global Motion Estimation")

/* advanced option only */
#define ENC_ME_PHASECORR "enable-phasecorr-me"
#define ENC_ME_PHASECORR_TEXT N_("Enable Phase Correlation Estimation")

/* advanced option only */
#define ENC_SCD "enable-scd"
#define ENC_SCD_TEXT N_("Enable Scene Change Detection")

/* advanced option only */
#define ENC_FORCE_PROFILE "force-profile"
#define ENC_FORCE_PROFILE_TEXT N_("Force Profile")

static const char *enc_profile_list[] = {
  "auto",
  "vc2_low_delay",
  "vc2_simple",
  "vc2_main",
  "main"
};

static const char *const enc_profile_list_text[] =
  { N_("automatic - let encoder decide based upon input (Best)"),
    N_("VC2 Low Delay Profile"),
    N_("VC2 Simple Profile"),
    N_("VC2 Main Profile"),
    N_("Main Profile"),
  };

345
static const char *const ppsz_enc_options[] = {
346
347
348
349
350
351
352
353
354
355
356
357
    ENC_RATE_CONTROL, ENC_GOP_STRUCTURE, ENC_QUALITY, ENC_NOISE_THRESHOLD, ENC_BITRATE,
    ENC_MIN_BITRATE, ENC_MAX_BITRATE, ENC_AU_DISTANCE, ENC_CHROMAFMT,
    ENC_PREFILTER, ENC_PREFILTER_STRENGTH, ENC_CODINGMODE, ENC_MCBLK_SIZE,
    ENC_MCBLK_OVERLAP, ENC_MVPREC, ENC_ME_COMBINED, ENC_DWTINTRA, ENC_DWTINTER,
    ENC_DWTDEPTH, ENC_MULTIQUANT, ENC_NOAC, ENC_PWT, ENC_PDIST, ENC_HSLICES,
    ENC_VSLICES, ENC_SCBLK_SIZE, ENC_ME_HIERARCHICAL, ENC_ME_DOWNSAMPLE_LEVELS,
    ENC_ME_GLOBAL_MOTION, ENC_ME_PHASECORR, ENC_SCD, ENC_FORCE_PROFILE,
    NULL
};


/* Module declaration */
358

359
360
361
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
362
363
    set_shortname( "Schroedinger" )
    set_description( N_("Dirac video decoder using libschroedinger") )
364
365
366
    set_capability( "decoder", 200 )
    set_callbacks( OpenDecoder, CloseDecoder )
    add_shortcut( "schroedinger" )
367
368
369
370
371
372
373
374
375
376
377

    /* encoder */
    add_submodule()
    set_section( N_("Encoding") , NULL )
    set_description( N_("Dirac video encoder using libschroedinger") )
    set_capability( "encoder", 110 )
    set_callbacks( OpenEncoder, CloseEncoder )
    add_shortcut( "schroedinger", "schro" )

    add_string( ENC_CFG_PREFIX ENC_RATE_CONTROL, NULL,
                 ENC_RATE_CONTROL_TEXT, ENC_RATE_CONTROL_LONGTEXT, false )
378
    change_string_list( enc_rate_control_list, enc_rate_control_list_text )
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401

    add_float( ENC_CFG_PREFIX ENC_QUALITY, -1.,
               ENC_QUALITY_TEXT, ENC_QUALITY_LONGTEXT, false )
    change_float_range(-1., 10.);

    add_float( ENC_CFG_PREFIX ENC_NOISE_THRESHOLD, -1.,
               ENC_NOISE_THRESHOLD_TEXT, ENC_NOISE_THRESHOLD_LONGTEXT, false )
    change_float_range(-1., 100.);

    add_integer( ENC_CFG_PREFIX ENC_BITRATE, -1,
                 ENC_BITRATE_TEXT, ENC_BITRATE_LONGTEXT, false )
    change_integer_range(-1, INT_MAX);

    add_integer( ENC_CFG_PREFIX ENC_MAX_BITRATE, -1,
                 ENC_MAX_BITRATE_TEXT, ENC_MAX_BITRATE_LONGTEXT, false )
    change_integer_range(-1, INT_MAX);

    add_integer( ENC_CFG_PREFIX ENC_MIN_BITRATE, -1,
                 ENC_MIN_BITRATE_TEXT, ENC_MIN_BITRATE_LONGTEXT, false )
    change_integer_range(-1, INT_MAX);

    add_string( ENC_CFG_PREFIX ENC_GOP_STRUCTURE, NULL,
                 ENC_GOP_STRUCTURE_TEXT, ENC_GOP_STRUCTURE_LONGTEXT, false )
402
    change_string_list( enc_gop_structure_list, enc_gop_structure_list_text )
403
404
405
406
407
408
409

    add_integer( ENC_CFG_PREFIX ENC_AU_DISTANCE, -1,
                 ENC_AU_DISTANCE_TEXT, ENC_AU_DISTANCE_LONGTEXT, false )
    change_integer_range(-1, INT_MAX);

    add_string( ENC_CFG_PREFIX ENC_CHROMAFMT, "420",
                ENC_CHROMAFMT_TEXT, ENC_CHROMAFMT_LONGTEXT, false )
410
    change_string_list( enc_chromafmt_list, enc_chromafmt_list_text )
411
412
413

    add_string( ENC_CFG_PREFIX ENC_CODINGMODE, "auto",
                ENC_CODINGMODE_TEXT, ENC_CODINGMODE_LONGTEXT, false )
414
    change_string_list( enc_codingmode_list, enc_codingmode_list_text )
415
416
417

    add_string( ENC_CFG_PREFIX ENC_MVPREC, NULL,
                ENC_MVPREC_TEXT, ENC_MVPREC_LONGTEXT, false )
418
    change_string_list( enc_mvprec_list, enc_mvprec_list )
419
420
421
422

    /* advanced option only */
    add_string( ENC_CFG_PREFIX ENC_MCBLK_SIZE, NULL,
                ENC_MCBLK_SIZE_TEXT, ENC_MCBLK_SIZE_TEXT, true )
423
    change_string_list( enc_block_size_list, enc_block_size_list_text )
424
425
426
427
428


    /* advanced option only */
    add_string( ENC_CFG_PREFIX ENC_MCBLK_OVERLAP, NULL,
                ENC_MCBLK_OVERLAP_TEXT, ENC_MCBLK_OVERLAP_TEXT, true )
429
    change_string_list( enc_block_overlap_list, enc_block_overlap_list_text )
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_ME_COMBINED, -1,
              ENC_ME_COMBINED_TEXT, ENC_ME_COMBINED_LONGTEXT, true )
    change_integer_range(-1, 1 );

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_ME_HIERARCHICAL, -1,
                 ENC_ME_HIERARCHICAL_TEXT, ENC_ME_HIERARCHICAL_TEXT, true )
    change_integer_range(-1, 1 );

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_ME_DOWNSAMPLE_LEVELS, -1,
                 ENC_ME_DOWNSAMPLE_LEVELS_TEXT, ENC_ME_DOWNSAMPLE_LEVELS_LONGTEXT, true )
    change_integer_range(-1, 8 );

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_ME_GLOBAL_MOTION, -1,
                 ENC_ME_GLOBAL_MOTION_TEXT, ENC_ME_GLOBAL_MOTION_TEXT, true )
    change_integer_range(-1, 1 );

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_ME_PHASECORR, -1,
                 ENC_ME_PHASECORR_TEXT, ENC_ME_PHASECORR_TEXT, true )
    change_integer_range(-1, 1 );

    add_string( ENC_CFG_PREFIX ENC_DWTINTRA, NULL,
                ENC_DWTINTRA_TEXT, ENC_DWTINTRA_TEXT, false )
458
    change_string_list( enc_wavelet_list, enc_wavelet_list_text )
459
460
461

    add_string( ENC_CFG_PREFIX ENC_DWTINTER, NULL,
                ENC_DWTINTER_TEXT, ENC_DWTINTER_TEXT, false )
462
    change_string_list( enc_wavelet_list, enc_wavelet_list_text )
463
464
465
466
467
468
469
470
471
472
473
474
475

    add_integer( ENC_CFG_PREFIX ENC_DWTDEPTH, -1,
                 ENC_DWTDEPTH_TEXT, ENC_DWTDEPTH_LONGTEXT, false )
    change_integer_range(-1, SCHRO_LIMIT_ENCODER_TRANSFORM_DEPTH );

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_MULTIQUANT, -1,
                 ENC_MULTIQUANT_TEXT, ENC_MULTIQUANT_LONGTEXT, true )
    change_integer_range(-1, 1 );

    /* advanced option only */
    add_string( ENC_CFG_PREFIX ENC_SCBLK_SIZE, NULL,
                ENC_SCBLK_SIZE_TEXT, ENC_SCBLK_SIZE_TEXT, true )
476
    change_string_list( enc_codeblock_size_list, enc_codeblock_size_list_text )
477
478
479

    add_string( ENC_CFG_PREFIX ENC_PREFILTER, NULL,
                ENC_PREFILTER_TEXT, ENC_PREFILTER_LONGTEXT, false )
480
    change_string_list( enc_filtering_list, enc_filtering_list_text )
481
482
483
484
485
486
487
488
489
490
491
492
493

    add_float( ENC_CFG_PREFIX ENC_PREFILTER_STRENGTH, -1.,
                 ENC_PREFILTER_STRENGTH_TEXT, ENC_PREFILTER_STRENGTH_LONGTEXT, false )
    change_float_range(-1., 100.0);

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_SCD, -1,
                 ENC_SCD_TEXT, ENC_SCD_TEXT, true )
    change_integer_range(-1, 1 );

    /* advanced option only */
    add_string( ENC_CFG_PREFIX ENC_PWT, NULL,
                ENC_PWT_TEXT, ENC_PWT_TEXT, true )
494
    change_string_list( enc_perceptual_weighting_list, enc_perceptual_weighting_list )
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518

    /* advanced option only */
    add_float( ENC_CFG_PREFIX ENC_PDIST, -1,
               ENC_PDIST_TEXT, ENC_PDIST_LONGTEXT, true )
    change_float_range(-1., 100.);

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_NOAC, -1,
              ENC_NOAC_TEXT, ENC_NOAC_LONGTEXT, true )
    change_integer_range(-1, 1 );

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_HSLICES, -1,
                 ENC_HSLICES_TEXT, ENC_HSLICES_LONGTEXT, true )
    change_integer_range(-1, INT_MAX );

    /* advanced option only */
    add_integer( ENC_CFG_PREFIX ENC_VSLICES, -1,
                 ENC_VSLICES_TEXT, ENC_VSLICES_LONGTEXT, true )
    change_integer_range(-1, INT_MAX );

    /* advanced option only */
    add_string( ENC_CFG_PREFIX ENC_FORCE_PROFILE, NULL,
                ENC_FORCE_PROFILE_TEXT, ENC_FORCE_PROFILE_TEXT, true )
519
    change_string_list( enc_profile_list, enc_profile_list_text )
520

521
vlc_module_end ()
522

523

524
525
526
527
528
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static picture_t *DecodeBlock  ( decoder_t *p_dec, block_t **pp_block );

529
530
531
532
533
534
struct picture_free_t
{
   picture_t *p_pic;
   decoder_t *p_dec;
};

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*****************************************************************************
 * decoder_sys_t : Schroedinger decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
    /*
     * Dirac properties
     */
    mtime_t i_lastpts;
    mtime_t i_frame_pts_delta;
    SchroDecoder *p_schro;
    SchroVideoFormat *p_format;
};

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
    SchroDecoder *p_schro;

558
    if( p_dec->fmt_in.i_codec != VLC_CODEC_DIRAC )
559
560
561
562
    {
        return VLC_EGENERIC;
    }

563
564
565
566
567
    /* Allocate the memory needed to store the decoder's structure */
    p_sys = malloc(sizeof(decoder_sys_t));
    if( p_sys == NULL )
        return VLC_ENOMEM;

568
569
570
571
572
    /* Initialise the schroedinger (and hence liboil libraries */
    /* This does no allocation and is safe to call */
    schro_init();

    /* Initialise the schroedinger decoder */
573
574
575
576
577
    if( !(p_schro = schro_decoder_new()) )
    {
        free( p_sys );
        return VLC_EGENERIC;
    }
578

579
    p_dec->p_sys = p_sys;
580
581
    p_sys->p_schro = p_schro;
    p_sys->p_format = NULL;
582
    p_sys->i_lastpts = VLC_TS_INVALID;
583
584
585
586
    p_sys->i_frame_pts_delta = 0;

    /* Set output properties */
    p_dec->fmt_out.i_cat = VIDEO_ES;
587
    p_dec->fmt_out.i_codec = VLC_CODEC_I420;
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610

    /* Set callbacks */
    p_dec->pf_decode_video = DecodeBlock;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * SetPictureFormat: Set the decoded picture params to the ones from the stream
 *****************************************************************************/
static void SetVideoFormat( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    p_sys->p_format = schro_decoder_get_video_format(p_sys->p_schro);
    if( p_sys->p_format == NULL ) return;

    p_sys->i_frame_pts_delta = INT64_C(1000000)
                            * p_sys->p_format->frame_rate_denominator
                            / p_sys->p_format->frame_rate_numerator;

    switch( p_sys->p_format->chroma_format )
    {
611
612
613
    case SCHRO_CHROMA_420: p_dec->fmt_out.i_codec = VLC_CODEC_I420; break;
    case SCHRO_CHROMA_422: p_dec->fmt_out.i_codec = VLC_CODEC_I422; break;
    case SCHRO_CHROMA_444: p_dec->fmt_out.i_codec = VLC_CODEC_I444; break;
614
615
616
617
618
    default:
        p_dec->fmt_out.i_codec = 0;
        break;
    }

619
620
    p_dec->fmt_out.video.i_visible_width = p_sys->p_format->clean_width;
    p_dec->fmt_out.video.i_x_offset = p_sys->p_format->left_offset;
621
622
    p_dec->fmt_out.video.i_width = p_sys->p_format->width;

623
624
    p_dec->fmt_out.video.i_visible_height = p_sys->p_format->clean_height;
    p_dec->fmt_out.video.i_y_offset = p_sys->p_format->top_offset;
625
626
627
    p_dec->fmt_out.video.i_height = p_sys->p_format->height;

    /* aspect_ratio_[numerator|denominator] describes the pixel aspect ratio */
Laurent Aimar's avatar
Laurent Aimar committed
628
629
    p_dec->fmt_out.video.i_sar_num = p_sys->p_format->aspect_ratio_numerator;
    p_dec->fmt_out.video.i_sar_den = p_sys->p_format->aspect_ratio_denominator;
630
631
632
633
634
635
636
637
638
639
640
641
642
643

    p_dec->fmt_out.video.i_frame_rate =
        p_sys->p_format->frame_rate_numerator;
    p_dec->fmt_out.video.i_frame_rate_base =
        p_sys->p_format->frame_rate_denominator;
}

/*****************************************************************************
 * SchroFrameFree: schro_frame callback to release the associated picture_t
 * When schro_decoder_reset() is called there will be pictures in the
 * decoding pipeline that need to be released rather than displayed.
 *****************************************************************************/
static void SchroFrameFree( SchroFrame *frame, void *priv)
{
644
    struct picture_free_t *p_free = priv;
645

646
    if( !p_free )
647
648
        return;

649
    decoder_DeletePicture( p_free->p_dec, p_free->p_pic );
650
    free(p_free);
651
    (void)frame;
652
653
654
655
656
657
658
659
660
661
}

/*****************************************************************************
 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
 *****************************************************************************/
static SchroFrame *CreateSchroFrameFromPic( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    SchroFrame *p_schroframe = schro_frame_new();
    picture_t *p_pic = NULL;
662
    struct picture_free_t *p_free;
663
664
665
666

    if( !p_schroframe )
        return NULL;

667
    p_pic = decoder_NewPicture( p_dec );
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683

    if( !p_pic )
        return NULL;

    p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
    if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
    {
        p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
    }
    else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
    {
        p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
    }

    p_schroframe->width = p_sys->p_format->width;
    p_schroframe->height = p_sys->p_format->height;
684
685
686
687
688

    p_free = malloc( sizeof( *p_free ) );
    p_free->p_pic = p_pic;
    p_free->p_dec = p_dec;
    schro_frame_set_free_callback( p_schroframe, SchroFrameFree, p_free );
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725

    for( int i=0; i<3; i++ )
    {
        p_schroframe->components[i].width = p_pic->p[i].i_visible_pitch;
        p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
        p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
        p_schroframe->components[i].length =
            p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
        p_schroframe->components[i].data = p_pic->p[i].p_pixels;

        if(i!=0)
        {
            p_schroframe->components[i].v_shift =
                SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
            p_schroframe->components[i].h_shift =
                SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
        }
    }

    p_pic->b_progressive = !p_sys->p_format->interlaced;
    p_pic->b_top_field_first = p_sys->p_format->top_field_first;
    p_pic->i_nb_fields = 2;

    return p_schroframe;
}

/*****************************************************************************
 * SchroBufferFree: schro_buffer callback to release the associated block_t
 *****************************************************************************/
static void SchroBufferFree( SchroBuffer *buf, void *priv )
{
    block_t *p_block = priv;

    if( !p_block )
        return;

    block_Release( p_block );
726
    (void)buf;
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
}

/*****************************************************************************
 * CloseDecoder: decoder destruction
 *****************************************************************************/
static void CloseDecoder( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t *)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;

    schro_decoder_free( p_sys->p_schro );
    free( p_sys );
}

/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
744
745
 * Blocks need not be Dirac dataunit aligned.
 * If a block has a PTS signaled, it applies to the first picture at or after p_block
746
747
748
749
750
751
752
753
754
755
756
757
 *
 * If this function returns a picture (!NULL), it is called again and the
 * same block is resubmitted.  To avoid this, set *pp_block to NULL;
 * If this function returns NULL, the *pp_block is lost (and leaked).
 * This function must free all blocks when finished with them.
 ****************************************************************************/
static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    if( !pp_block ) return NULL;

758
759
    if ( *pp_block ) {
        block_t *p_block = *pp_block;
760
761
762

        /* reset the decoder when seeking as the decode in progress is invalid */
        /* discard the block as it is just a null magic block */
763
        if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) {
764
765
            schro_decoder_reset( p_sys->p_schro );

766
            p_sys->i_lastpts = VLC_TS_INVALID;
767
768
769
770
771
            block_Release( p_block );
            *pp_block = NULL;
            return NULL;
        }

772
773
774
775
        SchroBuffer *p_schrobuffer;
        p_schrobuffer = schro_buffer_new_with_data( p_block->p_buffer, p_block->i_buffer );
        p_schrobuffer->free = SchroBufferFree;
        p_schrobuffer->priv = p_block;
776
        if( p_block->i_pts > VLC_TS_INVALID ) {
777
778
779
780
781
782
            mtime_t *p_pts = malloc( sizeof(*p_pts) );
            if( p_pts ) {
                *p_pts = p_block->i_pts;
                /* if this call fails, p_pts is freed automatically */
                p_schrobuffer->tag = schro_tag_new( p_pts, free );
            }
783
        }
784
785
786
787
788
789
790

        /* this stops the same block being fed back into this function if
         * we were on the next iteration of this loop to output a picture */
        *pp_block = NULL;
        schro_decoder_autoparse_push( p_sys->p_schro, p_schrobuffer );
        /* DO NOT refer to p_block after this point, it may have been freed */
    }
791
792
793

    while( 1 )
    {
794
795
796
        SchroFrame *p_schroframe;
        picture_t *p_pic;
        int state = schro_decoder_autoparse_wait( p_sys->p_schro );
797
798
799

        switch( state )
        {
800
801
802
803
        case SCHRO_DECODER_FIRST_ACCESS_UNIT:
            SetVideoFormat( p_dec );
            break;

804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
        case SCHRO_DECODER_NEED_BITS:
            return NULL;

        case SCHRO_DECODER_NEED_FRAME:
            p_schroframe = CreateSchroFrameFromPic( p_dec );

            if( !p_schroframe )
            {
                msg_Err( p_dec, "Could not allocate picture for decoder");
                return NULL;
            }

            schro_decoder_add_output_picture( p_sys->p_schro, p_schroframe);
            break;

819
820
        case SCHRO_DECODER_OK: {
            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 )
823
            {
824
825
826
                /* frame can't be one that was allocated by us
                 *   -- no private data: discard */
                if( p_tag ) schro_tag_free( p_tag );
827
                if( p_schroframe ) schro_frame_unref( p_schroframe );
828
829
                break;
            }
830
            p_pic = ((struct picture_free_t*) p_schroframe->priv)->p_pic;
831
832
            p_schroframe->priv = NULL;

833
834
835
836
837
838
            if( p_tag )
            {
                /* free is handled by schro_frame_unref */
                p_pic->date = *(mtime_t*) p_tag->value;
                schro_tag_free( p_tag );
            }
839
            else if( p_sys->i_lastpts > VLC_TS_INVALID )
840
            {
841
842
843
844
845
                /* NB, this shouldn't happen since the packetizer does a
                 * very thorough job of inventing timestamps.  The
                 * following is just a very rough fall back incase packetizer
                 * is missing. */
                /* 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;
847
            }
848
849
            p_sys->i_lastpts = p_pic->date;

850
            schro_frame_unref( p_schroframe );
851
            return p_pic;
852
        }
853
        case SCHRO_DECODER_EOS:
854
            /* NB, the new api will not emit _EOS, it handles the reset internally */
855
856
857
            break;

        case SCHRO_DECODER_ERROR:
858
            msg_Err( p_dec, "SCHRO_DECODER_ERROR");
859
860
861
862
            return NULL;
        }
    }
}
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static block_t *Encode( encoder_t *p_enc, picture_t *p_pict );


/*****************************************************************************
 * picture_pts_t : store pts alongside picture number, not carried through
 * encoder
 *****************************************************************************/
struct picture_pts_t
{
   mtime_t i_pts;    /* associated pts */
   uint32_t u_pnum;  /* dirac picture number */
   bool b_empty;     /* entry is invalid */
};

/*****************************************************************************
 * encoder_sys_t : Schroedinger encoder descriptor
 *****************************************************************************/
#define SCHRO_PTS_TLB_SIZE 256
struct encoder_sys_t
{
    /*
     * Schro properties
     */
    SchroEncoder *p_schro;
    SchroVideoFormat *p_format;
    int started;
    bool b_auto_field_coding;

    uint32_t i_input_picnum;
    block_fifo_t *p_dts_fifo;

    block_t *p_chain;

    struct picture_pts_t pts_tlb[SCHRO_PTS_TLB_SIZE];
    mtime_t i_pts_offset;
    mtime_t i_field_time;

    bool b_eos_signalled;
    bool b_eos_pulled;
};

static struct
{
    unsigned int i_height;
    int i_approx_fps;
    SchroVideoFormatEnum i_vf;
} schro_format_guess[] = {
    /* Important: Keep this list ordered in ascending picture height */
    {1, 0, SCHRO_VIDEO_FORMAT_CUSTOM},
    {120, 15, SCHRO_VIDEO_FORMAT_QSIF},
    {144, 12, SCHRO_VIDEO_FORMAT_QCIF},
    {240, 15, SCHRO_VIDEO_FORMAT_SIF},
    {288, 12, SCHRO_VIDEO_FORMAT_CIF},
    {480, 30, SCHRO_VIDEO_FORMAT_SD480I_60},
    {480, 15, SCHRO_VIDEO_FORMAT_4SIF},
    {576, 12, SCHRO_VIDEO_FORMAT_4CIF},
    {576, 25, SCHRO_VIDEO_FORMAT_SD576I_50},
    {720, 50, SCHRO_VIDEO_FORMAT_HD720P_50},
    {720, 60, SCHRO_VIDEO_FORMAT_HD720P_60},
    {1080, 24, SCHRO_VIDEO_FORMAT_DC2K_24},
    {1080, 25, SCHRO_VIDEO_FORMAT_HD1080I_50},
    {1080, 30, SCHRO_VIDEO_FORMAT_HD1080I_60},
    {1080, 50, SCHRO_VIDEO_FORMAT_HD1080P_50},
    {1080, 60, SCHRO_VIDEO_FORMAT_HD1080P_60},
    {2160, 24, SCHRO_VIDEO_FORMAT_DC4K_24},
    {0, 0, 0},
};

/*****************************************************************************
 * ResetPTStlb: Purge all entries in @p_enc@'s PTS-tlb
 *****************************************************************************/
static void ResetPTStlb( encoder_t *p_enc )
{
    encoder_sys_t *p_sys = p_enc->p_sys;
    for( int i = 0; i < SCHRO_PTS_TLB_SIZE; i++ )
    {
        p_sys->pts_tlb[i].b_empty = true;
    }
}


/*****************************************************************************
 * StorePicturePTS: Store the PTS value for a particular picture number
 *****************************************************************************/
static void StorePicturePTS( encoder_t *p_enc, uint32_t u_pnum, mtime_t i_pts )
{
    encoder_sys_t *p_sys = p_enc->p_sys;

    for( int i = 0; i<SCHRO_PTS_TLB_SIZE; i++ )
    {
        if( p_sys->pts_tlb[i].b_empty )
        {
            p_sys->pts_tlb[i].u_pnum = u_pnum;
            p_sys->pts_tlb[i].i_pts = i_pts;
            p_sys->pts_tlb[i].b_empty = false;

            return;
        }
    }

    msg_Err( p_enc, "Could not store PTS %"PRId64" for frame %u", i_pts, u_pnum );
}

/*****************************************************************************
 * GetPicturePTS: Retrieve the PTS value for a particular picture number
 *****************************************************************************/
static mtime_t GetPicturePTS( encoder_t *p_enc, uint32_t u_pnum )
{
    encoder_sys_t *p_sys = p_enc->p_sys;

    for( int i = 0; i < SCHRO_PTS_TLB_SIZE; i++ )
    {
        if( !p_sys->pts_tlb[i].b_empty &&
            p_sys->pts_tlb[i].u_pnum == u_pnum )
        {
             p_sys->pts_tlb[i].b_empty = true;
             return p_sys->pts_tlb[i].i_pts;
        }
    }

    msg_Err( p_enc, "Could not retrieve PTS for picture %u", u_pnum );
    return 0;
}

static inline bool SchroSetEnum( const encoder_t *p_enc, int i_list_size, const char *list[],
                  const char *psz_name,  const char *psz_name_text,  const char *psz_value)
{
    encoder_sys_t *p_sys = p_enc->p_sys;
    if( list && psz_name_text && psz_name && psz_value ) {
        for( int i = 0; i < i_list_size; ++i ) {
            if( strcmp( list[i], psz_value ) )
                continue;
            schro_encoder_setting_set_double( p_sys->p_schro, psz_name, i );
            return true;
        }
        msg_Err( p_enc, "Invalid %s: %s", psz_name_text, psz_value );
    }
    return false;
}

static bool SetEncChromaFormat( encoder_t *p_enc, uint32_t i_codec )
{
    encoder_sys_t *p_sys = p_enc->p_sys;

    switch( i_codec ) {
    case VLC_CODEC_I420:
        p_enc->fmt_in.i_codec = i_codec;
        p_enc->fmt_in.video.i_bits_per_pixel = 12;
        p_sys->p_format->chroma_format = SCHRO_CHROMA_420;
           break;
    case VLC_CODEC_I422:
        p_enc->fmt_in.i_codec = i_codec;
        p_enc->fmt_in.video.i_bits_per_pixel = 16;
        p_sys->p_format->chroma_format = SCHRO_CHROMA_422;
        break;
    case VLC_CODEC_I444:
        p_enc->fmt_in.i_codec = i_codec;
        p_enc->fmt_in.video.i_bits_per_pixel = 24;
        p_sys->p_format->chroma_format = SCHRO_CHROMA_444;
        break;
    default:
        return false;
    }

    return true;
}

#define SCHRO_SET_FLOAT(psz_name, pschro_name) \
    f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX psz_name ); \
    if( f_tmp >= 0.0 ) \
        schro_encoder_setting_set_double( p_sys->p_schro, pschro_name, f_tmp );

#define SCHRO_SET_INTEGER(psz_name, pschro_name, ignore_val) \
    i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX psz_name ); \
    if( i_tmp > ignore_val ) \
        schro_encoder_setting_set_double( p_sys->p_schro, pschro_name, i_tmp );

#define SCHRO_SET_ENUM(list, psz_name, psz_name_text, pschro_name) \
    psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX psz_name ); \
    if( !psz_tmp ) \
        goto error; \
    else if ( *psz_tmp != '\0' ) { \
        int i_list_size = ARRAY_SIZE(list); \
        if( !SchroSetEnum( p_enc, i_list_size, list, pschro_name, psz_name_text, psz_tmp ) ) { \
            free( psz_tmp ); \
            goto error; \
        } \
    } \
    free( psz_tmp );

/*****************************************************************************
 * OpenEncoder: probe the encoder and return score
 *****************************************************************************/
static int OpenEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
ivoire's avatar
ivoire committed
1063
    encoder_sys_t *p_sys;
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
    int i_tmp;
    float f_tmp;
    char *psz_tmp;

    if( p_enc->fmt_out.i_codec != VLC_CODEC_DIRAC &&
        !p_enc->b_force )
    {
        return VLC_EGENERIC;
    }

    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_visible_height || !p_enc->fmt_in.video.i_visible_width )
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
    {
        msg_Err( p_enc, "Framerate and picture dimensions must be non-zero" );
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_sys = calloc( 1, sizeof( *p_sys ) ) ) == NULL )
        return VLC_ENOMEM;

    p_enc->p_sys = p_sys;
    p_enc->pf_encode_video = Encode;
    p_enc->fmt_out.i_codec = VLC_CODEC_DIRAC;
    p_enc->fmt_out.i_cat = VIDEO_ES;

    if( ( p_sys->p_dts_fifo = block_FifoNew() ) == NULL )
    {
        CloseEncoder( p_this );
        return VLC_ENOMEM;
    }

    ResetPTStlb( p_enc );

    /* guess the video format based upon number of lines and picture height */
    int i = 0;
    SchroVideoFormatEnum guessed_video_fmt = SCHRO_VIDEO_FORMAT_CUSTOM;
    /* Pick the dirac_video_format in this order of preference:
     *  1. an exact match in frame height and an approximate fps match
     *  2. the previous preset with a smaller number of lines.
     */
    do
    {
        if( schro_format_guess[i].i_height > p_enc->fmt_in.video.i_height )
        {
            guessed_video_fmt = schro_format_guess[i-1].i_vf;
            break;
        }
        if( schro_format_guess[i].i_height != p_enc->fmt_in.video.i_height )
            continue;
        int src_fps = p_enc->fmt_in.video.i_frame_rate / p_enc->fmt_in.video.i_frame_rate_base;
        int delta_fps = abs( schro_format_guess[i].i_approx_fps - src_fps );
        if( delta_fps > 2 )
            continue;

        guessed_video_fmt = schro_format_guess[i].i_vf;
        break;
    } while( schro_format_guess[++i].i_height );

    schro_init();
    p_sys->p_schro = schro_encoder_new();
    if( !p_sys->p_schro ) {
        msg_Err( p_enc, "Failed to initialize libschroedinger encoder" );
        return VLC_EGENERIC;
    }
    schro_encoder_set_packet_assembly( p_sys->p_schro, true );

    if( !( p_sys->p_format = schro_encoder_get_video_format( p_sys->p_schro ) ) ) {
        msg_Err( p_enc, "Failed to get Schroedigner video format" );
        schro_encoder_free( p_sys->p_schro );
        return VLC_EGENERIC;
    }

    /* initialise the video format parameters to the guessed format */
    schro_video_format_set_std_video_format( p_sys->p_format, guessed_video_fmt );

    /* constants set from the input video format */
1141
1142
    p_sys->p_format->width                  = p_enc->fmt_in.video.i_visible_width;
    p_sys->p_format->height                 = p_enc->fmt_in.video.i_visible_height;
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
    p_sys->p_format->frame_rate_numerator   = p_enc->fmt_in.video.i_frame_rate;
    p_sys->p_format->frame_rate_denominator = p_enc->fmt_in.video.i_frame_rate_base;
    unsigned u_asr_num, u_asr_den;
    vlc_ureduce( &u_asr_num, &u_asr_den,
                 p_enc->fmt_in.video.i_sar_num,
                 p_enc->fmt_in.video.i_sar_den, 0 );
    p_sys->p_format->aspect_ratio_numerator   = u_asr_num;
    p_sys->p_format->aspect_ratio_denominator = u_asr_den;

    config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );

    SCHRO_SET_ENUM(enc_rate_control_list, ENC_RATE_CONTROL, ENC_RATE_CONTROL_TEXT, "rate_control")

    SCHRO_SET_ENUM(enc_gop_structure_list, ENC_GOP_STRUCTURE, ENC_GOP_STRUCTURE_TEXT, "gop_structure")

    psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_CHROMAFMT );
    if( !psz_tmp )
        goto error;
    else {
        uint32_t i_codec;
        if( !strcmp( psz_tmp, "420" ) ) {
            i_codec = VLC_CODEC_I420;
        }
        else if( !strcmp( psz_tmp, "422" ) ) {
            i_codec = VLC_CODEC_I422;
        }
        else if( !strcmp( psz_tmp, "444" ) ) {
            i_codec = VLC_CODEC_I444;
        }
        else {
            msg_Err( p_enc, "Invalid chroma format: %s", psz_tmp );
            free( psz_tmp );
            goto error;
        }
        SetEncChromaFormat( p_enc, i_codec );
    }
    free( psz_tmp );

    SCHRO_SET_FLOAT(ENC_QUALITY, "quality")

    SCHRO_SET_FLOAT(ENC_NOISE_THRESHOLD, "noise_threshold")

    /* use bitrate from sout-transcode-vb in kbps */
    i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_BITRATE );
    if( i_tmp > -1 )
        schro_encoder_setting_set_double( p_sys->p_schro, "bitrate", i_tmp * 1000 );
    else
        schro_encoder_setting_set_double( p_sys->p_schro, "bitrate", p_enc->fmt_out.i_bitrate );

       p_enc->fmt_out.i_bitrate = schro_encoder_setting_get_double( p_sys->p_schro, "bitrate" );

    i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_MIN_BITRATE );
    if( i_tmp > -1 )
        schro_encoder_setting_set_double( p_sys->p_schro, "min_bitrate", i_tmp * 1000 );

    i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX ENC_MAX_BITRATE );
    if( i_tmp > -1 )
        schro_encoder_setting_set_double( p_sys->p_schro, "max_bitrate", i_tmp * 1000 );

    SCHRO_SET_INTEGER(ENC_AU_DISTANCE, "au_distance", -1)

    SCHRO_SET_ENUM(enc_filtering_list, ENC_PREFILTER, ENC_PREFILTER_TEXT, "filtering")

    SCHRO_SET_FLOAT(ENC_PREFILTER_STRENGTH, "filter_value")


    psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_CODINGMODE );
    if( !psz_tmp )
        goto error;
    else if( !strcmp( psz_tmp, "auto" ) ) {
        p_sys->b_auto_field_coding = true;
    }
    else if( !strcmp( psz_tmp, "progressive" ) ) {
        p_sys->b_auto_field_coding = false;
        schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", false);
    }
    else if( !strcmp( psz_tmp, "field" ) ) {
        p_sys->b_auto_field_coding = false;
        schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", true);
    }
    else {
        msg_Err( p_enc, "Invalid codingmode: %s", psz_tmp );
        free( psz_tmp );
        goto error;
    }
    free( psz_tmp );

    SCHRO_SET_ENUM(enc_block_size_list, ENC_MCBLK_SIZE, ENC_MCBLK_SIZE_TEXT, "motion_block_size")

    SCHRO_SET_ENUM(enc_block_overlap_list, ENC_MCBLK_OVERLAP, ENC_MCBLK_OVERLAP_TEXT, "motion_block_overlap")

    psz_tmp = var_GetString( p_enc, ENC_CFG_PREFIX ENC_MVPREC );
    if( !psz_tmp )
        goto error;
    else if( *psz_tmp != '\0') {
        if( !strcmp( psz_tmp, "1" ) ) {
            schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 0 );
        }
        else if( !strcmp( psz_tmp, "1/2" ) ) {
            schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 1 );
        }
        else if( !strcmp( psz_tmp, "1/4" ) ) {
            schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 2 );
        }
        else if( !strcmp( psz_tmp, "1/8" ) ) {
            schro_encoder_setting_set_double( p_sys->p_schro, "mv_precision", 3 );
        }
        else {
            msg_Err( p_enc, "Invalid mv_precision: %s", psz_tmp );
            free( psz_tmp );
            goto error;
        }
    }
    free( psz_tmp );

    SCHRO_SET_INTEGER(ENC_ME_COMBINED, "enable_chroma_me", -1)

    SCHRO_SET_ENUM(enc_wavelet_list, ENC_DWTINTRA, ENC_DWTINTRA_TEXT, "intra_wavelet")

    SCHRO_SET_ENUM(enc_wavelet_list, ENC_DWTINTER, ENC_DWTINTER_TEXT, "inter_wavelet")

    SCHRO_SET_INTEGER(ENC_DWTDEPTH, "transform_depth", -1)

    SCHRO_SET_INTEGER(ENC_MULTIQUANT, "enable_multiquant", -1)

    SCHRO_SET_INTEGER(ENC_NOAC, "enable_noarith", -1)

    SCHRO_SET_ENUM(enc_perceptual_weighting_list, ENC_PWT, ENC_PWT_TEXT, "perceptual_weighting")

    SCHRO_SET_FLOAT(ENC_PDIST, "perceptual_distance")

    SCHRO_SET_INTEGER(ENC_HSLICES, "horiz_slices", -1)

    SCHRO_SET_INTEGER(ENC_VSLICES, "vert_slices", -1)

    SCHRO_SET_ENUM(enc_codeblock_size_list, ENC_SCBLK_SIZE, ENC_SCBLK_SIZE_TEXT, "codeblock_size")

    SCHRO_SET_INTEGER(ENC_ME_HIERARCHICAL, "enable_hierarchical_estimation", -1)

    SCHRO_SET_INTEGER(ENC_ME_DOWNSAMPLE_LEVELS, "downsample_levels", 1)

    SCHRO_SET_INTEGER(ENC_ME_GLOBAL_MOTION, "enable_global_motion", -1)

    SCHRO_SET_INTEGER(ENC_ME_PHASECORR, "enable_phasecorr_estimation", -1)

    SCHRO_SET_INTEGER(ENC_SCD, "enable_scene_change_detection", -1)

    SCHRO_SET_ENUM(enc_profile_list, ENC_FORCE_PROFILE, ENC_FORCE_PROFILE_TEXT, "force_profile")

    p_sys->started = 0;

    return VLC_SUCCESS;
error:
    CloseEncoder( p_this );
    return VLC_EGENERIC;
}


struct enc_picture_free_t
{
   picture_t *p_pic;
   encoder_t *p_enc;
};

/*****************************************************************************
 * EncSchroFrameFree: schro_frame callback to release the associated picture_t
 * When schro_encoder_reset() is called there will be pictures in the
 * encoding pipeline that need to be released rather than displayed.
 *****************************************************************************/
static void EncSchroFrameFree( SchroFrame *frame, void *priv )
{
    struct enc_picture_free_t *p_free = priv;

    if( !p_free )
        return;

    picture_Release( p_free->p_pic );
    free( p_free );
    (void)frame;
}

/*****************************************************************************
 * CreateSchroFrameFromPic: wrap a picture_t in a SchroFrame
 *****************************************************************************/
static SchroFrame *CreateSchroFrameFromInputPic( encoder_t *p_enc,  picture_t *p_pic )
{
    encoder_sys_t *p_sys = p_enc->p_sys;
    SchroFrame *p_schroframe = schro_frame_new();
    struct enc_picture_free_t *p_free;

    if( !p_schroframe )
        return NULL;

    if( !p_pic )
        return NULL;

    p_schroframe->format = SCHRO_FRAME_FORMAT_U8_420;
    if( p_sys->p_format->chroma_format == SCHRO_CHROMA_422 )
    {
        p_schroframe->format = SCHRO_FRAME_FORMAT_U8_422;
    }
    else if( p_sys->p_format->chroma_format == SCHRO_CHROMA_444 )
    {
        p_schroframe->format = SCHRO_FRAME_FORMAT_U8_444;
    }

    p_schroframe->width  = p_sys->p_format->width;
    p_schroframe->height = p_sys->p_format->height;

    p_free = malloc( sizeof( *p_free ) );
    if( unlikely( p_free == NULL ) ) {
        schro_frame_unref( p_schroframe );
        return NULL;
    }

    p_free->p_pic = p_pic;
    p_free->p_enc = p_enc;
    schro_frame_set_free_callback( p_schroframe, EncSchroFrameFree, p_free );

    for( int i=0; i<3; i++ )
    {
        p_schroframe->components[i].width  = p_pic->p[i].i_visible_pitch;
        p_schroframe->components[i].stride = p_pic->p[i].i_pitch;
        p_schroframe->components[i].height = p_pic->p[i].i_visible_lines;
        p_schroframe->components[i].length =
            p_pic->p[i].i_pitch * p_pic->p[i].i_lines;
        p_schroframe->components[i].data   = p_pic->p[i].p_pixels;

        if( i!=0 )
        {
            p_schroframe->components[i].v_shift =
                SCHRO_FRAME_FORMAT_V_SHIFT( p_schroframe->format );
            p_schroframe->components[i].h_shift =
                SCHRO_FRAME_FORMAT_H_SHIFT( p_schroframe->format );
        }
    }

    return p_schroframe;
}

/* Attempt to find dirac picture number in an encapsulation unit */
static int ReadDiracPictureNumber( uint32_t *p_picnum, block_t *p_block )
{
    uint32_t u_pos = 4;
    /* protect against falling off the edge */
    while( u_pos + 13 < p_block->i_buffer )
    {
        /* find the picture startcode */
        if( p_block->p_buffer[u_pos] & 0x08 )
        {
            *p_picnum = GetDWBE( p_block->p_buffer + u_pos + 9 );
            return 1;
        }
        /* skip to the next dirac data unit */
        uint32_t u_npo = GetDWBE( p_block->p_buffer + u_pos + 1 );
        assert( u_npo <= UINT32_MAX - u_pos );
        if( u_npo == 0 )
            u_npo = 13;
        u_pos += u_npo;
    }
    return 0;
}


static block_t *Encode( encoder_t *p_enc, picture_t *p_pic )
{
    encoder_sys_t *p_sys = p_enc->p_sys;
    block_t *p_block, *p_output_chain = NULL;
    SchroFrame *p_frame;
    bool b_go = true;

    if( !p_pic ) {
        if( !p_sys->started || p_sys->b_eos_pulled )
            return NULL;

        if( !p_sys->b_eos_signalled ) {
            p_sys->b_eos_signalled = 1;
            schro_encoder_end_of_stream( p_sys->p_schro );
        }
    } else {
        /* we only know if the sequence is interlaced when the first
         * picture arrives, so final setup is done here */
        /* XXX todo, detect change of interlace */
        p_sys->p_format->interlaced = !p_pic->b_progressive;
        p_sys->p_format->top_field_first = p_pic->b_top_field_first;

        if( p_sys->b_auto_field_coding )
            schro_encoder_setting_set_double( p_sys->p_schro, "interlaced_coding", !p_pic->b_progressive );
    }

    if( !p_sys->started ) {
        date_t date;

        if( p_pic->format.i_chroma != p_enc->fmt_in.i_codec ) {
            char chroma_in[5], chroma_out[5];
            vlc_fourcc_to_char( p_pic->format.i_chroma, chroma_in );
            chroma_in[4]  = '\0';
            chroma_out[4] = '\0';
            vlc_fourcc_to_char( p_enc->fmt_in.i_codec, chroma_out );
            msg_Warn( p_enc, "Resetting chroma from %s to %s", chroma_out, chroma_in );
            if( !SetEncChromaFormat( p_enc, p_pic->format.i_chroma ) ) {
                msg_Err( p_enc, "Could not reset chroma format to %s", chroma_in );
                return NULL;
            }
        }

        date_Init( &date, p_enc->fmt_in.video.i_frame_rate, p_enc->fmt_in.video.i_frame_rate_base );
        /* FIXME - Unlike dirac-research codec Schro doesn't have a function that returns the delay in pics yet.
         *   Use a default of 1
         */
        date_Increment( &date, 1 );
        p_sys->i_pts_offset = date_Get( &date );
        if( schro_encoder_setting_get_double( p_sys->p_schro, "interlaced_coding" ) > 0.0 ) {
            date_Set( &date, 0 );
            date_Increment( &date, 1);
            p_sys->i_field_time = date_Get( &date ) / 2;
        }

        schro_video_format_set_std_signal_range( p_sys->p_format, SCHRO_SIGNAL_RANGE_8BIT_VIDEO );
        schro_encoder_set_video_format( p_sys->p_schro, p_sys->p_format );
        schro_encoder_start( p_sys->p_schro );
        p_sys->started = 1;
    }

    if( !p_sys->b_eos_signalled ) {
        /* create a schro frame from the input pic and load */
        /* Increase ref count by 1 so that the picture is not freed until
           Schro finishes with it */
        picture_Hold( p_pic );

        p_frame = CreateSchroFrameFromInputPic( p_enc, p_pic );
        if( !p_frame )
            return NULL;
        schro_encoder_push_frame( p_sys->p_schro, p_frame );


        /* store pts in a lookaside buffer, so that the same pts may
        * be used for the picture in coded order */
        StorePicturePTS( p_enc, p_sys->i_input_picnum, p_pic->date );
        p_sys->i_input_picnum++;

        /* store dts in a queue, so that they appear in order in
         * coded order */
1486
        p_block = block_Alloc( 1 );
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
        if( !p_block )
            return NULL;
        p_block->i_dts = p_pic->date - p_sys->i_pts_offset;
        block_FifoPut( p_sys->p_dts_fifo, p_block );
        p_block = NULL;

        /* for field coding mode, insert an extra value into both the
         * pts lookaside buffer and dts queue, offset to correspond
         * to a one field delay. */
        if( schro_encoder_setting_get_double( p_sys->p_schro, "interlaced_coding" ) > 0.0 ) {
            StorePicturePTS( p_enc, p_sys->i_input_picnum, p_pic->date + p_sys->i_field_time );
            p_sys->i_input_picnum++;

1500
            p_block = block_Alloc( 1 );
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
            if( !p_block )
                return NULL;
            p_block->i_dts = p_pic->date - p_sys->i_pts_offset + p_sys->i_field_time;
            block_FifoPut( p_sys->p_dts_fifo, p_block );
            p_block = NULL;
        }
    }

    do
    {
        SchroStateEnum state;
        state = schro_encoder_wait( p_sys->p_schro );
        switch( state )
        {
        case SCHRO_STATE_NEED_FRAME:
            b_go = false;
            break;
        case SCHRO_STATE_AGAIN:
            break;
        case SCHRO_STATE_END_OF_STREAM:
            p_sys->b_eos_pulled = 1;
            b_go = false;
            break;
        case SCHRO_STATE_HAVE_BUFFER:
        {
            SchroBuffer *p_enc_buf;
            uint32_t u_pic_num;
            int i_presentation_frame;
            p_enc_buf = schro_encoder_pull( p_sys->p_schro, &i_presentation_frame );
1530
            p_block = block_Alloc( p_enc_buf->length );
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
            if( !p_block )
                return NULL;

            memcpy( p_block->p_buffer, p_enc_buf->data, p_enc_buf->length );
            schro_buffer_unref( p_enc_buf );

            /* Presence of a Sequence header indicates a seek point */
            if( 0 == p_block->p_buffer[4] )
            {
                p_block->i_flags |= BLOCK_FLAG_TYPE_I;

                if( !p_enc->fmt_out.p_extra ) {
                    const uint8_t eos[] = { 'B','B','C','D',0x10,0,0,0,13,0,0,0,0 };
                    uint32_t len = GetDWBE( p_block->p_buffer + 5 );
                    /* if it hasn't been done so far, stash a copy of the
                     * sequence header for muxers such as ogg */
                    /* The OggDirac spec advises that a Dirac EOS DataUnit
                     * is appended to the sequence header to allow guard
                     * against poor streaming servers */
                    /* XXX, should this be done using the packetizer ? */
                    p_enc->fmt_out.p_extra = malloc( len + sizeof( eos ) );
                    if( !p_enc->fmt_out.p_extra )
                        return NULL;
                    memcpy( p_enc->fmt_out.p_extra, p_block->p_buffer, len );
                    memcpy( (uint8_t*)p_enc->fmt_out.p_extra + len, eos, sizeof( eos ) );
                    SetDWBE( (uint8_t*)p_enc->fmt_out.p_extra + len + 10, len );
                    p_enc->fmt_out.i_extra = len + sizeof( eos );
                }
            }

            if( ReadDiracPictureNumber( &u_pic_num, p_block ) ) {
                block_t *p_dts_block = block_FifoGet( p_sys->p_dts_fifo );
                p_block->i_dts = p_dts_block->i_dts;
                   p_block->i_pts = GetPicturePTS( p_enc, u_pic_num );
                block_Release( p_dts_block );
                block_ChainAppend( &p_output_chain, p_block );
            } else {
                /* End of sequence */
                block_ChainAppend( &p_output_chain, p_block );
            }
            break;
        }
        default:
            break;
        }
    } while( b_go );

    return p_output_chain;
}

/*****************************************************************************
 * CloseEncoder: Schro encoder destruction
 *****************************************************************************/
static void CloseEncoder( vlc_object_t *p_this )
{
    encoder_t *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys = p_enc->p_sys;

    /* Free the encoder resources */
    if( p_sys->p_schro )
        schro_encoder_free( p_sys->p_schro );

    free( p_sys->p_format );

    if( p_sys->p_dts_fifo )
        block_FifoRelease( p_sys->p_dts_fifo );

    block_ChainRelease( p_sys->p_chain );

    free( p_sys );
}