]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Cosmetics.
[vlc] / src / input / decoder.c
1 /*****************************************************************************
2  * decoder.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42
43 #include <vlc_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
47 #include "input_clock.h"
48 #include "input_decoder.h"
49
50 #include "../video_output/vout_internal.h"
51
52 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
53 static void       DeleteDecoder( decoder_t * );
54
55 static void      *DecoderThread( vlc_object_t * );
56 static int        DecoderProcess( decoder_t *, block_t * );
57 static void       DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
58 static void       DecoderFlush( decoder_t * );
59
60 static void       DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
61
62 /* Buffers allocation callbacks for the decoders */
63 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
64 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
65
66 static picture_t *vout_new_buffer( decoder_t * );
67 static void vout_del_buffer( decoder_t *, picture_t * );
68 static void vout_link_picture( decoder_t *, picture_t * );
69 static void vout_unlink_picture( decoder_t *, picture_t * );
70
71 static subpicture_t *spu_new_buffer( decoder_t * );
72 static void spu_del_buffer( decoder_t *, subpicture_t * );
73
74 static es_format_t null_es_format;
75
76 struct decoder_owner_sys_t
77 {
78     bool      b_own_thread;
79
80     int64_t         i_preroll_end;
81
82     input_thread_t  *p_input;
83     input_clock_t   *p_clock;
84     int             i_last_rate;
85
86     vout_thread_t   *p_spu_vout;
87     int              i_spu_channel;
88     int64_t          i_spu_order;
89
90     sout_instance_t         *p_sout;
91     sout_packetizer_input_t *p_sout_input;
92
93     /* Some decoders require already packetized data (ie. not truncated) */
94     decoder_t *p_packetizer;
95
96     /* Current format in use by the output */
97     video_format_t video;
98     audio_format_t audio;
99     es_format_t    sout;
100
101     /* fifo */
102     block_fifo_t *p_fifo;
103
104     /* Lock for communication with decoder thread */
105     vlc_mutex_t lock;
106     vlc_cond_t  wait;
107
108     /* -- These variables need locking on write(only) -- */
109     aout_instance_t *p_aout;
110     aout_input_t    *p_aout_input;
111
112     vout_thread_t   *p_vout;
113
114     /* -- Theses variables need locking on read *and* write -- */
115     /* */
116     /* Pause */
117     bool b_paused;
118     mtime_t i_pause_date;
119
120     /* Flushing */
121     bool b_flushing;
122
123     /* CC */
124     struct
125     {
126         bool b_supported;
127         bool pb_present[4];
128         decoder_t *pp_decoder[4];
129     } cc;
130
131     /* Delay */
132     mtime_t i_ts_delay;
133 };
134
135 /*****************************************************************************
136  * Public functions
137  *****************************************************************************/
138
139 /* decoder_GetInputAttachment:
140  */
141 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
142                                                 const char *psz_name )
143 {
144     input_attachment_t *p_attachment;
145     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
146         return NULL;
147     return p_attachment;
148 }
149 /* decoder_GetInputAttachments:
150  */
151 int decoder_GetInputAttachments( decoder_t *p_dec,
152                                  input_attachment_t ***ppp_attachment,
153                                  int *pi_attachment )
154 {
155     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
156                           ppp_attachment, pi_attachment );
157 }
158 /* decoder_GetDisplayDate:
159  */
160 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
161 {
162     decoder_owner_sys_t *p_owner = p_dec->p_owner;
163
164     if( !p_owner->p_clock )
165         return i_ts;
166     return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
167 }
168 /* decoder_GetDisplayRate:
169  */
170 int decoder_GetDisplayRate( decoder_t *p_dec )
171 {
172     decoder_owner_sys_t *p_owner = p_dec->p_owner;
173
174     if( !p_owner->p_clock )
175         return INPUT_RATE_DEFAULT;
176     return input_clock_GetRate( p_owner->p_clock );
177 }
178
179 /**
180  * Spawns a new decoder thread
181  *
182  * \param p_input the input thread
183  * \param p_es the es descriptor
184  * \return the spawned decoder object
185  */
186 decoder_t *input_DecoderNew( input_thread_t *p_input,
187                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
188 {
189     decoder_t   *p_dec = NULL;
190     vlc_value_t val;
191
192 #ifndef ENABLE_SOUT
193     (void)b_force_decoder;
194 #else
195     /* If we are in sout mode, search for packetizer module */
196     if( p_sout )
197     {
198         /* Create the decoder configuration structure */
199         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
200         if( p_dec == NULL )
201         {
202             msg_Err( p_input, "could not create packetizer" );
203             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
204                             _("VLC could not open the packetizer module.") );
205             return NULL;
206         }
207     }
208     else
209 #endif
210     {
211         /* Create the decoder configuration structure */
212         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
213         if( p_dec == NULL )
214         {
215             msg_Err( p_input, "could not create decoder" );
216             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
217                             _("VLC could not open the decoder module.") );
218             return NULL;
219         }
220     }
221
222     if( !p_dec->p_module )
223     {
224         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
225
226         DeleteDecoder( p_dec );
227         vlc_object_release( p_dec );
228         return NULL;
229     }
230
231     p_dec->p_owner->p_clock = p_clock;
232
233     if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
234     {
235         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
236         p_dec->p_owner->b_own_thread = false;
237     }
238     else
239     {
240         var_Get( p_input, "minimize-threads", &val );
241         p_dec->p_owner->b_own_thread = !val.b_bool;
242     }
243
244     if( p_dec->p_owner->b_own_thread )
245     {
246         int i_priority;
247         if( fmt->i_cat == AUDIO_ES )
248             i_priority = VLC_THREAD_PRIORITY_AUDIO;
249         else
250             i_priority = VLC_THREAD_PRIORITY_VIDEO;
251
252         /* Spawn the decoder thread */
253         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
254                                i_priority, false ) )
255         {
256             msg_Err( p_dec, "cannot spawn decoder thread" );
257             module_unneed( p_dec, p_dec->p_module );
258             DeleteDecoder( p_dec );
259             vlc_object_release( p_dec );
260             return NULL;
261         }
262     }
263
264     return p_dec;
265 }
266
267 /**
268  * Kills a decoder thread and waits until it's finished
269  *
270  * \param p_input the input thread
271  * \param p_es the es descriptor
272  * \return nothing
273  */
274 void input_DecoderDelete( decoder_t *p_dec )
275 {
276     decoder_owner_sys_t *p_owner = p_dec->p_owner;
277
278     vlc_object_kill( p_dec );
279
280     if( p_owner->b_own_thread )
281     {
282         /* Make sure we aren't paused anymore */
283         vlc_mutex_lock( &p_owner->lock );
284         if( p_owner->b_paused )
285         {
286             p_owner->b_paused = false;
287             vlc_cond_signal( &p_owner->wait );
288         }
289         vlc_mutex_unlock( &p_owner->lock );
290
291         /* Make sure the thread leaves the function */
292         block_FifoWake( p_owner->p_fifo );
293
294         vlc_thread_join( p_dec );
295
296         /* Don't module_unneed() here because of the dll loader that wants
297          * close() in the same thread than open()/decode() */
298     }
299     else
300     {
301         /* Flush */
302         input_DecoderDecode( p_dec, NULL );
303
304         module_unneed( p_dec, p_dec->p_module );
305     }
306
307     /* */
308     if( p_dec->p_owner->cc.b_supported )
309     {
310         int i;
311         for( i = 0; i < 4; i++ )
312             input_DecoderSetCcState( p_dec, false, i );
313     }
314
315     /* Delete decoder configuration */
316     DeleteDecoder( p_dec );
317
318     /* Delete the decoder */
319     vlc_object_release( p_dec );
320 }
321
322 /**
323  * Put a block_t in the decoder's fifo.
324  *
325  * \param p_dec the decoder object
326  * \param p_block the data block
327  */
328 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
329 {
330     decoder_owner_sys_t *p_owner = p_dec->p_owner;
331
332     if( p_owner->b_own_thread )
333     {
334         if( p_owner->p_input->p->b_out_pace_control )
335         {
336             /* FIXME !!!!! */
337             while( vlc_object_alive( p_dec ) && !p_dec->b_error &&
338                    block_FifoCount( p_owner->p_fifo ) > 10 )
339             {
340                 msleep( 1000 );
341             }
342         }
343         else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
344         {
345             /* FIXME: ideally we would check the time amount of data
346              * in the fifo instead of its size. */
347             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
348                       "consumed quickly enough), resetting fifo!" );
349             block_FifoEmpty( p_owner->p_fifo );
350         }
351
352         block_FifoPut( p_owner->p_fifo, p_block );
353     }
354     else
355     {
356         if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
357         {
358             if( p_block )
359                 block_Release( p_block );
360         }
361         else
362         {
363             DecoderProcess( p_dec, p_block );
364         }
365     }
366 }
367
368 bool input_DecoderIsEmpty( decoder_t * p_dec )
369 {
370     if( p_dec->p_owner->b_own_thread &&
371         block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
372     {
373         return false;
374     }
375     return true;
376 }
377
378 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
379 {
380     decoder_owner_sys_t *p_owner = p_dec->p_owner;
381     int i;
382
383     vlc_mutex_lock( &p_owner->lock );
384     for( i = 0; i < 4; i++ )
385         pb_present[i] =  p_owner->cc.pb_present[i];
386     vlc_mutex_unlock( &p_owner->lock );
387 }
388 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
389 {
390     decoder_owner_sys_t *p_owner = p_dec->p_owner;
391
392     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
393
394     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
395         return VLC_EGENERIC;
396
397     if( b_decode )
398     {
399         static const vlc_fourcc_t fcc[4] = {
400             VLC_FOURCC('c', 'c', '1', ' '),
401             VLC_FOURCC('c', 'c', '2', ' '),
402             VLC_FOURCC('c', 'c', '3', ' '),
403             VLC_FOURCC('c', 'c', '4', ' '),
404         };
405         decoder_t *p_cc;
406         es_format_t fmt;
407
408         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
409         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
410         if( !p_cc )
411         {
412             msg_Err( p_dec, "could not create decoder" );
413             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
414                             _("VLC could not open the decoder module.") );
415             return VLC_EGENERIC;
416         }
417         else if( !p_cc->p_module )
418         {
419             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
420             DeleteDecoder( p_cc );
421             vlc_object_release( p_cc );
422             return VLC_EGENERIC;
423         }
424         p_cc->p_owner->p_clock = p_owner->p_clock;
425
426         vlc_mutex_lock( &p_owner->lock );
427         p_owner->cc.pp_decoder[i_channel] = p_cc;
428         vlc_mutex_unlock( &p_owner->lock );
429     }
430     else
431     {
432         decoder_t *p_cc;
433
434         vlc_mutex_lock( &p_owner->lock );
435         p_cc = p_owner->cc.pp_decoder[i_channel];
436         p_owner->cc.pp_decoder[i_channel] = NULL;
437         vlc_mutex_unlock( &p_owner->lock );
438
439         if( p_cc )
440         {
441             vlc_object_kill( p_cc );
442             module_unneed( p_cc, p_cc->p_module );
443             DeleteDecoder( p_cc );
444             vlc_object_release( p_cc );
445         }
446     }
447     return VLC_SUCCESS;
448 }
449 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
450 {
451     decoder_owner_sys_t *p_owner = p_dec->p_owner;
452
453     *pb_decode = false;
454     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
455         return VLC_EGENERIC;
456
457     vlc_mutex_lock( &p_owner->lock );
458     *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
459     vlc_mutex_unlock( &p_owner->lock );
460     return VLC_EGENERIC;
461 }
462
463 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
464 {
465     decoder_owner_sys_t *p_owner = p_dec->p_owner;
466
467     vlc_mutex_lock( &p_owner->lock );
468
469     assert( !p_owner->b_paused || !b_paused );
470     p_owner->b_paused = b_paused;
471     p_owner->i_pause_date = i_date;
472     if( p_owner->b_own_thread )
473         vlc_cond_signal( &p_owner->wait );
474
475     DecoderOutputChangePause( p_dec, b_paused, i_date );
476     vlc_mutex_unlock( &p_owner->lock );
477 }
478
479 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
480 {
481     decoder_owner_sys_t *p_owner = p_dec->p_owner;
482
483     vlc_mutex_lock( &p_owner->lock );
484     p_owner->i_ts_delay = i_delay;
485     vlc_mutex_unlock( &p_owner->lock );
486 }
487
488 void input_DecoderStartBuffering( decoder_t *p_dec )
489 {
490     decoder_owner_sys_t *p_owner = p_dec->p_owner;
491
492     vlc_mutex_lock( &p_owner->lock );
493
494     DecoderFlush( p_dec );
495
496     vlc_mutex_unlock( &p_owner->lock );
497 }
498
499 void input_DecoderStopBuffering( decoder_t *p_dec )
500 {
501 }
502
503 /*****************************************************************************
504  * Internal functions
505  *****************************************************************************/
506
507 /* */
508 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
509 {
510     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
511              "VLC probably does not support this sound or video format.",
512              (char*)&codec );
513     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
514                     _("VLC does not support the audio or video format \"%4.4s\". "
515                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
516 }
517
518
519 /**
520  * Create a decoder object
521  *
522  * \param p_input the input thread
523  * \param p_es the es descriptor
524  * \param i_object_type Object type as define in include/vlc_objects.h
525  * \return the decoder object
526  */
527 static decoder_t * CreateDecoder( input_thread_t *p_input,
528                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
529 {
530     decoder_t *p_dec;
531     decoder_owner_sys_t *p_owner;
532     int i;
533
534     p_dec = vlc_object_create( p_input, i_object_type );
535     if( p_dec == NULL )
536         return NULL;
537
538     p_dec->pf_decode_audio = NULL;
539     p_dec->pf_decode_video = NULL;
540     p_dec->pf_decode_sub = NULL;
541     p_dec->pf_get_cc = NULL;
542     p_dec->pf_packetize = NULL;
543
544     /* Initialize the decoder fifo */
545     p_dec->p_module = NULL;
546
547     memset( &null_es_format, 0, sizeof(es_format_t) );
548     es_format_Copy( &p_dec->fmt_in, fmt );
549     es_format_Copy( &p_dec->fmt_out, &null_es_format );
550
551     /* Allocate our private structure for the decoder */
552     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
553     if( p_dec->p_owner == NULL )
554     {
555         vlc_object_release( p_dec );
556         return NULL;
557     }
558     p_dec->p_owner->b_own_thread = true;
559     p_dec->p_owner->i_preroll_end = -1;
560     p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
561     p_dec->p_owner->p_input = p_input;
562     p_dec->p_owner->p_aout = NULL;
563     p_dec->p_owner->p_aout_input = NULL;
564     p_dec->p_owner->p_vout = NULL;
565     p_dec->p_owner->p_spu_vout = NULL;
566     p_dec->p_owner->i_spu_channel = 0;
567     p_dec->p_owner->i_spu_order = 0;
568     p_dec->p_owner->p_sout = p_sout;
569     p_dec->p_owner->p_sout_input = NULL;
570     p_dec->p_owner->p_packetizer = NULL;
571
572     /* decoder fifo */
573     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
574     {
575         free( p_dec->p_owner );
576         vlc_object_release( p_dec );
577         return NULL;
578     }
579
580     /* Set buffers allocation callbacks for the decoders */
581     p_dec->pf_aout_buffer_new = aout_new_buffer;
582     p_dec->pf_aout_buffer_del = aout_del_buffer;
583     p_dec->pf_vout_buffer_new = vout_new_buffer;
584     p_dec->pf_vout_buffer_del = vout_del_buffer;
585     p_dec->pf_picture_link    = vout_link_picture;
586     p_dec->pf_picture_unlink  = vout_unlink_picture;
587     p_dec->pf_spu_buffer_new  = spu_new_buffer;
588     p_dec->pf_spu_buffer_del  = spu_del_buffer;
589
590     vlc_object_attach( p_dec, p_input );
591
592     /* Find a suitable decoder/packetizer module */
593     if( i_object_type == VLC_OBJECT_DECODER )
594         p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
595     else
596         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
597
598     /* Check if decoder requires already packetized data */
599     if( i_object_type == VLC_OBJECT_DECODER &&
600         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
601     {
602         p_dec->p_owner->p_packetizer =
603             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
604         if( p_dec->p_owner->p_packetizer )
605         {
606             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
607                             &p_dec->fmt_in );
608
609             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
610                             &null_es_format );
611
612             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
613
614             p_dec->p_owner->p_packetizer->p_module =
615                 module_need( p_dec->p_owner->p_packetizer,
616                              "packetizer", "$packetizer", 0 );
617
618             if( !p_dec->p_owner->p_packetizer->p_module )
619             {
620                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
621                 vlc_object_detach( p_dec->p_owner->p_packetizer );
622                 vlc_object_release( p_dec->p_owner->p_packetizer );
623             }
624         }
625     }
626
627     /* Copy ourself the input replay gain */
628     if( fmt->i_cat == AUDIO_ES )
629     {
630         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
631         {
632             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
633             {
634                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
635                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
636             }
637             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
638             {
639                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
640                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
641             }
642         }
643     }
644     /* */
645     p_owner->cc.b_supported = false;
646     if( i_object_type == VLC_OBJECT_DECODER )
647     {
648         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
649             p_owner->cc.b_supported = true;
650         if( p_dec->pf_get_cc )
651             p_owner->cc.b_supported = true;
652     }
653
654     vlc_mutex_init( &p_owner->lock );
655     vlc_cond_init( &p_owner->wait );
656     p_owner->b_paused = false;
657     p_owner->i_pause_date = 0;
658     p_owner->b_flushing = false;
659     for( i = 0; i < 4; i++ )
660     {
661         p_owner->cc.pb_present[i] = false;
662         p_owner->cc.pp_decoder[i] = NULL;
663     }
664     p_owner->i_ts_delay = 0;
665     return p_dec;
666 }
667
668 /**
669  * The decoding main loop
670  *
671  * \param p_dec the decoder
672  */
673 static void *DecoderThread( vlc_object_t *p_this )
674 {
675     decoder_t *p_dec = (decoder_t *)p_this;
676     decoder_owner_sys_t *p_owner = p_dec->p_owner;
677
678     block_t *p_block;
679     int canc = vlc_savecancel();
680
681     /* The decoder's main loop */
682     while( vlc_object_alive( p_dec ) && !p_dec->b_error )
683     {
684         if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
685             continue;
686
687         if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
688             break;
689     }
690
691     while( vlc_object_alive( p_dec ) )
692     {
693         /* Trash all received PES packets */
694         p_block = block_FifoGet( p_owner->p_fifo );
695         if( p_block )
696             block_Release( p_block );
697     }
698
699     /* We do it here because of the dll loader that wants close() in the
700      * same thread than open()/decode() */
701     module_unneed( p_dec, p_dec->p_module );
702     vlc_restorecancel( canc );
703     return NULL;
704 }
705
706 static void DecoderFlush( decoder_t *p_dec )
707 {
708     decoder_owner_sys_t *p_owner = p_dec->p_owner;
709     block_t *p_null;
710
711     if( p_owner->b_own_thread )
712     {
713          vlc_assert_locked( &p_owner->lock );
714
715         /* Empty the fifo */
716         block_FifoEmpty( p_owner->p_fifo );
717
718         /* Monitor for flush end */
719         p_owner->b_flushing = true;
720         vlc_cond_signal( &p_owner->wait );
721     }
722
723     /* Send a special block */
724     p_null = block_New( p_dec, 128 );
725     if( !p_null )
726         return;
727     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
728     p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
729     if( !p_dec->fmt_in.b_packetized )
730         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
731     memset( p_null->p_buffer, 0, p_null->i_buffer );
732
733     input_DecoderDecode( p_dec, p_null );
734
735     /* */
736     if( p_owner->b_own_thread )
737     {
738         while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
739             vlc_cond_wait( &p_owner->wait, &p_owner->lock );
740     }
741 }
742
743 static void DecoderWaitUnpause( decoder_t *p_dec, bool *pb_reject )
744 {
745     decoder_owner_sys_t *p_owner = p_dec->p_owner;
746
747     vlc_mutex_lock( &p_owner->lock );
748
749     while( p_owner->b_paused && !p_owner->b_flushing )
750         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
751
752     if( pb_reject )
753         *pb_reject = p_owner->b_flushing;
754
755     vlc_mutex_unlock( &p_owner->lock );
756 }
757
758 static void DecoderSignalFlushed( decoder_t *p_dec )
759 {
760     decoder_owner_sys_t *p_owner = p_dec->p_owner;
761
762     vlc_mutex_lock( &p_owner->lock );
763
764     if( p_owner->b_flushing )
765     {
766         p_owner->b_flushing = false;
767         vlc_cond_signal( &p_owner->wait );
768     }
769
770     vlc_mutex_unlock( &p_owner->lock );
771 }
772
773 static bool DecoderIsFlushing( decoder_t *p_dec )
774 {
775     decoder_owner_sys_t *p_owner = p_dec->p_owner;
776     bool b_flushing;
777
778     vlc_mutex_lock( &p_owner->lock );
779
780     b_flushing = p_owner->b_flushing;
781
782     vlc_mutex_unlock( &p_owner->lock );
783
784     return b_flushing;
785 }
786
787 static void DecoderGetDelays( decoder_t *p_dec, mtime_t *pi_ts_delay, mtime_t *pi_es_delay )
788 {
789     decoder_owner_sys_t *p_owner = p_dec->p_owner;
790
791     *pi_ts_delay = p_owner->p_input->i_pts_delay;
792
793     vlc_mutex_lock( &p_owner->lock );
794
795     *pi_es_delay = p_owner->i_ts_delay;
796
797     vlc_mutex_unlock( &p_owner->lock );
798 }
799
800 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
801 {
802     decoder_owner_sys_t *p_owner = p_dec->p_owner;
803
804     vlc_assert_locked( &p_owner->lock );
805
806     /* XXX only audio and video output have to be paused.
807      * - for sout it is useless
808      * - for subs, it is done by the vout
809      */
810     if( p_dec->fmt_in.i_cat == AUDIO_ES )
811     {
812         if( p_owner->p_aout && p_owner->p_aout_input )
813             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
814                                  b_paused, i_date );
815     }
816     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
817     {
818         if( p_owner->p_vout )
819             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
820     }
821 }
822 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
823 {
824     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
825         *pi_preroll = INT64_MAX;
826     else if( p->i_pts > 0 )
827         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
828     else if( p->i_dts > 0 )
829         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
830 }
831
832 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
833 {
834     mtime_t current_date = mdate();
835
836     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
837     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
838     {
839         /* ETSI EN 300 472 Annex A : do not take into account the PTS
840          * for teletext streams. */
841         return current_date + 400000 + i_ts_delay;
842     }
843     return i_ts;
844 }
845
846 static void DecoderSoutBufferFixTs( block_t *p_block,
847                                     input_clock_t *p_clock,
848                                     mtime_t i_ts_delay, mtime_t i_es_delay,
849                                     bool b_teletext )
850 {
851     assert( p_clock );
852
853     if( !p_block->i_dts && !p_block->i_pts )
854         p_block->i_rate = input_clock_GetRate( p_clock );
855
856     if( p_block->i_dts > 0 )
857         p_block->i_dts = input_clock_GetTS( p_clock, &p_block->i_rate,
858                                             i_ts_delay, p_block->i_dts + i_es_delay);
859
860     if( p_block->i_pts > 0 )
861         p_block->i_pts = input_clock_GetTS( p_clock, &p_block->i_rate,
862                                             i_ts_delay, p_block->i_pts + i_es_delay );
863
864     if( p_block->i_length > 0 )
865         p_block->i_length = ( p_block->i_length * p_block->i_rate +
866                                 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
867
868     if( b_teletext )
869         p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
870 }
871 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer, int *pi_rate,
872                                     input_clock_t *p_clock,
873                                     mtime_t i_ts_delay, mtime_t i_es_delay )
874 {
875     /* sout display module does not set clock */
876     if( !p_clock )
877         return;
878
879     if( !p_buffer->start_date && !p_buffer->end_date )
880         *pi_rate = input_clock_GetRate( p_clock );
881
882     if( p_buffer->start_date > 0 )
883         p_buffer->start_date =
884             input_clock_GetTS( p_clock, pi_rate,
885                                i_ts_delay, p_buffer->start_date + i_es_delay );
886
887     if( p_buffer->end_date > 0 )
888         p_buffer->end_date =
889             input_clock_GetTS( p_clock, pi_rate,
890                                i_ts_delay, p_buffer->end_date + i_es_delay );
891 }
892 static void DecoderVoutBufferFixTs( picture_t *p_picture, int *pi_rate,
893                                     input_clock_t *p_clock,
894                                     mtime_t i_ts_delay, mtime_t i_es_delay )
895 {
896     /* sout display module does not set clock */
897     if( !p_clock )
898         return;
899
900     if( p_picture->date > 0 )
901         p_picture->date =
902             input_clock_GetTS( p_clock, pi_rate,
903                                i_ts_delay, p_picture->date + i_es_delay );
904 }
905 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
906                                    input_clock_t *p_clock,
907                                    mtime_t i_ts_delay, mtime_t i_es_delay,
908                                    bool b_teletext )
909 {
910     bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
911
912     /* sout display module does not set clock */
913     if( !p_clock )
914         return;
915
916     if( p_subpic->i_start > 0 )
917         p_subpic->i_start =
918             input_clock_GetTS( p_clock, NULL,
919                                i_ts_delay, p_subpic->i_start + i_es_delay );
920
921     if( p_subpic->i_stop > 0 )
922         p_subpic->i_stop =
923             input_clock_GetTS( p_clock, NULL,
924                                i_ts_delay, p_subpic->i_stop + i_es_delay );
925
926     /* Do not create ephemere picture because of rounding errors */
927     if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
928         p_subpic->i_stop++;
929
930     if( b_teletext )
931         p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
932 }
933
934 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
935 {
936     decoder_owner_sys_t *p_owner = p_dec->p_owner;
937     input_thread_t  *p_input = p_owner->p_input;
938     input_clock_t   *p_clock = p_owner->p_clock;
939     aout_buffer_t   *p_aout_buf;
940     int i_decoded = 0;
941     int i_lost = 0;
942     int i_played = 0;
943
944     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
945     {
946         aout_instance_t *p_aout = p_owner->p_aout;
947         aout_input_t    *p_aout_input = p_owner->p_aout_input;
948         int i_lost = 0;
949         int i_played;
950
951         if( p_dec->b_die )
952         {
953             /* It prevent freezing VLC in case of broken decoder */
954             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
955             if( p_block )
956                 block_Release( p_block );
957             break;
958         }
959         i_decoded++;
960
961         if( p_aout_buf->start_date < p_owner->i_preroll_end )
962         {
963             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
964             continue;
965         }
966
967         if( p_owner->i_preroll_end > 0 )
968         {
969             msg_Dbg( p_dec, "End of audio preroll" );
970             if( p_owner->p_aout && p_owner->p_aout_input )
971                 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
972             /* */
973             p_owner->i_preroll_end = -1;
974         }
975
976         bool b_reject;
977         DecoderWaitUnpause( p_dec, &b_reject );
978
979         int i_rate = INPUT_RATE_DEFAULT;
980         mtime_t i_ts_delay;
981         mtime_t i_es_delay;
982         DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
983
984         DecoderAoutBufferFixTs( p_aout_buf, &i_rate, p_clock, i_ts_delay, i_es_delay );
985
986         if( !p_clock && p_block && p_block->i_rate > 0 )
987             i_rate = p_block->i_rate;
988
989         /* FIXME TODO take care of audio-delay for mdate check */
990         const mtime_t i_max_date = mdate() + i_ts_delay +
991             i_es_delay * i_rate / INPUT_RATE_DEFAULT + AOUT_MAX_ADVANCE_TIME;
992
993         if( !b_reject &&
994             p_aout_buf->start_date > 0 &&
995             p_aout_buf->start_date <= i_max_date &&
996             i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
997             i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
998         {
999             /* Wait if we are too early
1000              * FIXME that's plain ugly to do it here */
1001             mwait( p_aout_buf->start_date - AOUT_MAX_PREPARE_TIME );
1002
1003             if( !aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate ) )
1004                 i_played++;
1005             i_lost += aout_DecGetResetLost( p_aout, p_aout_input );
1006         }
1007         else
1008         {
1009             if( p_aout_buf->start_date <= 0 )
1010             {
1011                 msg_Warn( p_dec, "non-dated audio buffer received" );
1012             }
1013             else if( p_aout_buf->start_date > i_max_date )
1014             {
1015                 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1016                           p_aout_buf->start_date - mdate() );
1017             }
1018             i_lost++;
1019             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1020         }
1021
1022     }
1023
1024     /* Update ugly stat */
1025     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1026     {
1027         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1028
1029         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1030                              i_lost, NULL );
1031         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1032                              i_played, NULL );
1033         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1034                              i_decoded, NULL );
1035
1036         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1037     }
1038 }
1039 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1040 {
1041     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1042     block_t *p_cc;
1043     bool pb_present[4];
1044     int i;
1045     int i_cc_decoder;
1046
1047     assert( p_dec_cc->pf_get_cc != NULL );
1048
1049     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1050     if( !p_owner->cc.b_supported )
1051         return;
1052
1053     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1054     if( !p_cc )
1055         return;
1056
1057     vlc_mutex_lock( &p_owner->lock );
1058     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1059     {
1060         p_owner->cc.pb_present[i] |= pb_present[i];
1061         if( p_owner->cc.pp_decoder[i] )
1062             i_cc_decoder++;
1063     }
1064
1065     for( i = 0; i < 4; i++ )
1066     {
1067         if( !p_owner->cc.pp_decoder[i] )
1068             continue;
1069
1070         if( i_cc_decoder > 1 )
1071             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1072         else
1073             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1074         i_cc_decoder--;
1075     }
1076     vlc_mutex_unlock( &p_owner->lock );
1077 }
1078 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
1079 {
1080     vlc_mutex_lock( &p_vout->picture_lock );
1081
1082     if( p_pic->i_status == READY_PICTURE )
1083     {
1084         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
1085         p_pic->date = 1;
1086     }
1087     else if( p_pic->i_refcount > 0 )
1088     {
1089         p_pic->i_status = DISPLAYED_PICTURE;
1090     }
1091     else
1092     {
1093         p_pic->i_status = DESTROYED_PICTURE;
1094         picture_CleanupQuant( p_pic );
1095         p_vout->i_heap_size--;
1096     }
1097
1098     vlc_mutex_unlock( &p_vout->picture_lock );
1099 }
1100 static void VoutFlushPicture( vout_thread_t *p_vout, mtime_t i_max_date )
1101 {
1102     int i;
1103     vlc_mutex_lock( &p_vout->picture_lock );
1104     for( i = 0; i < p_vout->render.i_pictures; i++ )
1105     {
1106         picture_t *p_pic = p_vout->render.pp_picture[i];
1107
1108         if( p_pic->i_status == READY_PICTURE ||
1109             p_pic->i_status == DISPLAYED_PICTURE )
1110         {
1111             /* We cannot change picture status if it is in READY_PICTURE state,
1112              * Just make sure they won't be displayed */
1113             if( p_pic->date > i_max_date )
1114                 p_pic->date = i_max_date;
1115         }
1116     }
1117     vlc_mutex_unlock( &p_vout->picture_lock );
1118 }
1119
1120 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1121 {
1122     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1123     input_thread_t *p_input = p_owner->p_input;
1124     picture_t      *p_pic;
1125     int i_lost = 0;
1126     int i_decoded = 0;
1127     int i_displayed = 0;
1128
1129     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1130     {
1131         vout_thread_t  *p_vout = p_owner->p_vout;
1132         if( p_dec->b_die )
1133         {
1134             /* It prevent freezing VLC in case of broken decoder */
1135             VoutDisplayedPicture( p_vout, p_pic );
1136             if( p_block )
1137                 block_Release( p_block );
1138             break;
1139         }
1140
1141         i_decoded++;
1142
1143         if( p_pic->date < p_owner->i_preroll_end )
1144         {
1145             VoutDisplayedPicture( p_vout, p_pic );
1146             continue;
1147         }
1148
1149         if( p_owner->i_preroll_end > 0 )
1150         {
1151             msg_Dbg( p_dec, "End of video preroll" );
1152             if( p_vout )
1153                 VoutFlushPicture( p_vout, 1 );
1154             /* */
1155             p_owner->i_preroll_end = -1;
1156         }
1157
1158         if( p_dec->pf_get_cc &&
1159             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1160             DecoderGetCc( p_dec, p_dec );
1161
1162         bool b_reject;
1163         DecoderWaitUnpause( p_dec, &b_reject );
1164
1165         mtime_t i_ts_delay;
1166         mtime_t i_es_delay;
1167         DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1168         int i_rate = INPUT_RATE_DEFAULT;
1169
1170         DecoderVoutBufferFixTs( p_pic, &i_rate, p_owner->p_clock, i_ts_delay, i_es_delay );
1171
1172         /* */
1173         const mtime_t i_max_date = mdate() + i_ts_delay +
1174             i_es_delay * i_rate / INPUT_RATE_DEFAULT + VOUT_BOGUS_DELAY;
1175
1176         if( !b_reject &&
1177             p_pic->date > 0 && p_pic->date < i_max_date )
1178         {
1179             if( i_rate != p_owner->i_last_rate  )
1180             {
1181                 /* Be sure to not display old picture after our own */
1182                 VoutFlushPicture( p_vout, p_pic->date );
1183                 p_owner->i_last_rate = i_rate;
1184             }
1185
1186             vout_DatePicture( p_vout, p_pic, p_pic->date );
1187
1188             vout_DisplayPicture( p_vout, p_pic );
1189         }
1190         else
1191         {
1192             if( p_pic->date <= 0 )
1193             {
1194                 msg_Warn( p_vout, "non-dated video buffer received" );
1195             }
1196             else
1197             {
1198                 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1199                           p_pic->date - mdate() );
1200             }
1201             i_lost++;
1202             VoutDisplayedPicture( p_vout, p_pic );
1203         }
1204         int i_tmp_display;
1205         int i_tmp_lost;
1206         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1207
1208         i_displayed += i_tmp_display;
1209         i_lost += i_tmp_lost;
1210     }
1211     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1212     {
1213         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1214
1215         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1216                              i_decoded, NULL );
1217         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1218                              i_lost , NULL);
1219
1220         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1221                              i_displayed, NULL);
1222
1223         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1224     }
1225 }
1226
1227 /* This function process a block for sout
1228  */
1229 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1230 {
1231     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1232     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1233     block_t *p_sout_block;
1234
1235     while( ( p_sout_block =
1236                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1237     {
1238         if( !p_owner->p_sout_input )
1239         {
1240             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1241
1242             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1243             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1244             if( p_dec->fmt_in.psz_language )
1245             {
1246                 if( p_owner->sout.psz_language )
1247                     free( p_owner->sout.psz_language );
1248                 p_owner->sout.psz_language =
1249                     strdup( p_dec->fmt_in.psz_language );
1250             }
1251
1252             p_owner->p_sout_input =
1253                 sout_InputNew( p_owner->p_sout,
1254                                &p_owner->sout );
1255
1256             if( p_owner->p_sout_input == NULL )
1257             {
1258                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1259                          (char *)&p_owner->sout.i_codec );
1260                 p_dec->b_error = true;
1261
1262                 while( p_sout_block )
1263                 {
1264                     block_t *p_next = p_sout_block->p_next;
1265                     block_Release( p_sout_block );
1266                     p_sout_block = p_next;
1267                 }
1268                 break;
1269             }
1270         }
1271
1272         while( p_sout_block )
1273         {
1274             block_t *p_next = p_sout_block->p_next;
1275
1276             p_sout_block->p_next = NULL;
1277
1278             DecoderWaitUnpause( p_dec, NULL );
1279
1280             mtime_t i_ts_delay;
1281             mtime_t i_es_delay;
1282             DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1283
1284             DecoderSoutBufferFixTs( p_sout_block, p_owner->p_clock,
1285                                     i_ts_delay, i_es_delay, b_telx );
1286
1287             sout_InputSendBuffer( p_owner->p_sout_input,
1288                                   p_sout_block );
1289
1290             p_sout_block = p_next;
1291         }
1292
1293         /* For now it's enough, as only sout impact on this flag */
1294         if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1295             p_owner->p_input->p->b_out_pace_control )
1296         {
1297             msg_Dbg( p_dec, "switching to sync mode" );
1298             p_owner->p_input->p->b_out_pace_control = false;
1299         }
1300         else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1301                  !p_owner->p_input->p->b_out_pace_control )
1302         {
1303             msg_Dbg( p_dec, "switching to async mode" );
1304             p_owner->p_input->p->b_out_pace_control = true;
1305         }
1306     }
1307 }
1308
1309 /* This function process a video block
1310  */
1311 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1312 {
1313     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1314
1315     if( p_owner->p_packetizer )
1316     {
1317         block_t *p_packetized_block;
1318         decoder_t *p_packetizer = p_owner->p_packetizer;
1319
1320         while( (p_packetized_block =
1321                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1322         {
1323             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1324             {
1325                 es_format_Clean( &p_dec->fmt_in );
1326                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1327             }
1328             if( p_packetizer->pf_get_cc )
1329                 DecoderGetCc( p_dec, p_packetizer );
1330
1331             while( p_packetized_block )
1332             {
1333                 block_t *p_next = p_packetized_block->p_next;
1334                 p_packetized_block->p_next = NULL;
1335
1336                 DecoderDecodeVideo( p_dec, p_packetized_block );
1337
1338                 p_packetized_block = p_next;
1339             }
1340         }
1341     }
1342     else if( p_block )
1343     {
1344         DecoderDecodeVideo( p_dec, p_block );
1345     }
1346
1347     if( b_flush && p_owner->p_vout )
1348         VoutFlushPicture( p_owner->p_vout, 1 );
1349 }
1350
1351 /* This function process a audio block
1352  */
1353 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1354 {
1355     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1356
1357     if( p_owner->p_packetizer )
1358     {
1359         block_t *p_packetized_block;
1360         decoder_t *p_packetizer = p_owner->p_packetizer;
1361
1362         while( (p_packetized_block =
1363                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1364         {
1365             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1366             {
1367                 es_format_Clean( &p_dec->fmt_in );
1368                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1369             }
1370
1371             while( p_packetized_block )
1372             {
1373                 block_t *p_next = p_packetized_block->p_next;
1374                 p_packetized_block->p_next = NULL;
1375
1376                 DecoderDecodeAudio( p_dec, p_packetized_block );
1377
1378                 p_packetized_block = p_next;
1379             }
1380         }
1381     }
1382     else if( p_block )
1383     {
1384         DecoderDecodeAudio( p_dec, p_block );
1385     }
1386
1387     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1388         aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1389 }
1390
1391 /* This function process a subtitle block
1392  */
1393 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1394 {
1395     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1396     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1397
1398     input_thread_t *p_input = p_owner->p_input;
1399     vout_thread_t *p_vout;
1400     subpicture_t *p_spu;
1401
1402     if( b_flush && p_owner->p_spu_vout )
1403     {
1404         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1405
1406         if( p_vout && p_owner->p_spu_vout == p_vout )
1407             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1408                          p_owner->i_spu_channel );
1409
1410         if( p_vout )
1411             vlc_object_release( p_vout );
1412     }
1413
1414     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1415     {
1416         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1417         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1418         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1419
1420         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1421         if( p_vout && p_owner->p_spu_vout == p_vout )
1422         {
1423             /* Preroll does not work very well with subtitle */
1424             if( p_spu->i_start > 0 &&
1425                 p_spu->i_start < p_owner->i_preroll_end &&
1426                 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1427             {
1428                 subpicture_Delete( p_spu );
1429             }
1430             else
1431             {
1432                 bool b_reject;
1433                 DecoderWaitUnpause( p_dec, &b_reject );
1434
1435                 mtime_t i_ts_delay;
1436                 mtime_t i_es_delay;
1437                 DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1438
1439                 DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, i_ts_delay, i_es_delay, b_telx );
1440                 if( !b_reject )
1441                     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1442                 else
1443                     subpicture_Delete( p_spu );
1444             }
1445         }
1446         else
1447         {
1448             subpicture_Delete( p_spu );
1449         }
1450         if( p_vout )
1451             vlc_object_release( p_vout );
1452     }
1453 }
1454
1455 /**
1456  * Decode a block
1457  *
1458  * \param p_dec the decoder object
1459  * \param p_block the block to decode
1460  * \return VLC_SUCCESS or an error code
1461  */
1462 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1463 {
1464     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1465     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1466
1467     if( p_block && p_block->i_buffer <= 0 )
1468     {
1469         assert( !b_flush_request );
1470         block_Release( p_block );
1471         return VLC_SUCCESS;
1472     }
1473
1474 #ifdef ENABLE_SOUT
1475     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1476     {
1477         if( p_block )
1478             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1479
1480         DecoderProcessSout( p_dec, p_block );
1481     }
1482     else
1483 #endif
1484     {
1485         bool b_flush = false;
1486
1487         if( p_block )
1488         {
1489             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1490             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1491
1492             b_flush = !b_flushing && b_flush_request;
1493
1494             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1495         }
1496
1497         if( p_dec->fmt_in.i_cat == AUDIO_ES )
1498         {
1499             DecoderProcessAudio( p_dec, p_block, b_flush );
1500         }
1501         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1502         {
1503             DecoderProcessVideo( p_dec, p_block, b_flush );
1504         }
1505         else if( p_dec->fmt_in.i_cat == SPU_ES )
1506         {
1507             DecoderProcessSpu( p_dec, p_block, b_flush );
1508         }
1509         else
1510         {
1511             msg_Err( p_dec, "unknown ES format" );
1512             p_dec->b_error = true;
1513         }
1514     }
1515
1516     /* */
1517     if( b_flush_request )
1518         DecoderSignalFlushed( p_dec );
1519
1520     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1521 }
1522
1523 /**
1524  * Destroys a decoder object
1525  *
1526  * \param p_dec the decoder object
1527  * \return nothing
1528  */
1529 static void DeleteDecoder( decoder_t * p_dec )
1530 {
1531     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1532
1533     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1534              (char*)&p_dec->fmt_in.i_codec,
1535              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1536
1537     /* Free all packets still in the decoder fifo. */
1538     block_FifoEmpty( p_owner->p_fifo );
1539     block_FifoRelease( p_owner->p_fifo );
1540
1541     /* Cleanup */
1542     if( p_owner->p_aout_input )
1543         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1544     if( p_owner->p_aout )
1545     {
1546         vlc_object_release( p_owner->p_aout );
1547         p_owner->p_aout = NULL;
1548     }
1549     if( p_owner->p_vout )
1550     {
1551         int i_pic;
1552
1553 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1554         /* Hack to make sure all the the pictures are freed by the decoder */
1555         for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1556              i_pic++ )
1557         {
1558             if( p_pic->i_status == RESERVED_PICTURE )
1559                 vout_DestroyPicture( p_owner->p_vout, p_pic );
1560             if( p_pic->i_refcount > 0 )
1561                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1562         }
1563 #undef p_pic
1564
1565         /* We are about to die. Reattach video output to p_vlc. */
1566         vout_Request( p_dec, p_owner->p_vout, NULL );
1567         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1568     }
1569
1570 #ifdef ENABLE_SOUT
1571     if( p_owner->p_sout_input )
1572     {
1573         sout_InputDelete( p_owner->p_sout_input );
1574         es_format_Clean( &p_owner->sout );
1575     }
1576 #endif
1577
1578     if( p_dec->fmt_in.i_cat == SPU_ES )
1579     {
1580         vout_thread_t *p_vout;
1581
1582         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1583         if( p_vout && p_owner->p_spu_vout == p_vout )
1584         {
1585             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1586                          p_owner->i_spu_channel );
1587             vlc_object_release( p_vout );
1588         }
1589     }
1590
1591     es_format_Clean( &p_dec->fmt_in );
1592     es_format_Clean( &p_dec->fmt_out );
1593
1594     if( p_owner->p_packetizer )
1595     {
1596         module_unneed( p_owner->p_packetizer,
1597                        p_owner->p_packetizer->p_module );
1598         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1599         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1600         vlc_object_detach( p_owner->p_packetizer );
1601         vlc_object_release( p_owner->p_packetizer );
1602     }
1603
1604     vlc_cond_destroy( &p_owner->wait );
1605     vlc_mutex_destroy( &p_owner->lock );
1606
1607     vlc_object_detach( p_dec );
1608
1609     free( p_owner );
1610 }
1611
1612 /*****************************************************************************
1613  * Buffers allocation callbacks for the decoders
1614  *****************************************************************************/
1615 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1616 {
1617     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1618     aout_buffer_t *p_buffer;
1619
1620     if( p_owner->p_aout_input != NULL &&
1621         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1622           p_dec->fmt_out.audio.i_original_channels !=
1623               p_owner->audio.i_original_channels ||
1624           p_dec->fmt_out.audio.i_bytes_per_frame !=
1625               p_owner->audio.i_bytes_per_frame ) )
1626     {
1627         aout_input_t *p_aout_input = p_owner->p_aout_input;
1628
1629         /* Parameters changed, restart the aout */
1630         vlc_mutex_lock( &p_owner->lock );
1631
1632         p_owner->p_aout_input = NULL;
1633         aout_DecDelete( p_owner->p_aout, p_aout_input );
1634
1635         vlc_mutex_unlock( &p_owner->lock );
1636     }
1637
1638     if( p_owner->p_aout_input == NULL )
1639     {
1640         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1641         audio_sample_format_t format;
1642         aout_input_t *p_aout_input;
1643         aout_instance_t *p_aout;
1644
1645         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1646         p_owner->audio = p_dec->fmt_out.audio;
1647
1648         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1649         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1650                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1651         {
1652             if ( i_force_dolby == 1 )
1653             {
1654                 format.i_original_channels = format.i_original_channels |
1655                                              AOUT_CHAN_DOLBYSTEREO;
1656             }
1657             else /* i_force_dolby == 2 */
1658             {
1659                 format.i_original_channels = format.i_original_channels &
1660                                              ~AOUT_CHAN_DOLBYSTEREO;
1661             }
1662         }
1663
1664         p_aout = p_owner->p_aout;
1665         p_aout_input = aout_DecNew( p_dec, &p_aout,
1666                                     &format, &p_dec->fmt_out.audio_replay_gain );
1667
1668         vlc_mutex_lock( &p_owner->lock );
1669         p_owner->p_aout = p_aout;
1670         p_owner->p_aout_input = p_aout_input;
1671         vlc_mutex_unlock( &p_owner->lock );
1672
1673         if( p_owner->p_aout_input == NULL )
1674         {
1675             msg_Err( p_dec, "failed to create audio output" );
1676             p_dec->b_error = true;
1677             return NULL;
1678         }
1679         p_dec->fmt_out.audio.i_bytes_per_frame =
1680             p_owner->audio.i_bytes_per_frame;
1681     }
1682
1683     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1684
1685     return p_buffer;
1686 }
1687
1688 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1689 {
1690     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1691
1692     aout_DecDeleteBuffer( p_owner->p_aout,
1693                           p_owner->p_aout_input, p_buffer );
1694 }
1695
1696
1697 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1698
1699 static picture_t *vout_new_buffer( decoder_t *p_dec )
1700 {
1701     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1702
1703     if( p_owner->p_vout == NULL ||
1704         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1705         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1706         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1707         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1708     {
1709         vout_thread_t *p_vout;
1710
1711         if( !p_dec->fmt_out.video.i_width ||
1712             !p_dec->fmt_out.video.i_height )
1713         {
1714             /* Can't create a new vout without display size */
1715             return NULL;
1716         }
1717
1718         if( !p_dec->fmt_out.video.i_visible_width ||
1719             !p_dec->fmt_out.video.i_visible_height )
1720         {
1721             if( p_dec->fmt_in.video.i_visible_width &&
1722                 p_dec->fmt_in.video.i_visible_height )
1723             {
1724                 p_dec->fmt_out.video.i_visible_width =
1725                     p_dec->fmt_in.video.i_visible_width;
1726                 p_dec->fmt_out.video.i_visible_height =
1727                     p_dec->fmt_in.video.i_visible_height;
1728             }
1729             else
1730             {
1731                 p_dec->fmt_out.video.i_visible_width =
1732                     p_dec->fmt_out.video.i_width;
1733                 p_dec->fmt_out.video.i_visible_height =
1734                     p_dec->fmt_out.video.i_height;
1735             }
1736         }
1737
1738         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1739             var_CreateGetBool( p_dec, "hdtv-fix" ) )
1740         {
1741             p_dec->fmt_out.video.i_visible_height = 1080;
1742             p_dec->fmt_out.video.i_sar_num *= 135;
1743             p_dec->fmt_out.video.i_sar_den *= 136;
1744             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1745         }
1746
1747         if( !p_dec->fmt_out.video.i_sar_num ||
1748             !p_dec->fmt_out.video.i_sar_den )
1749         {
1750             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1751               p_dec->fmt_out.video.i_visible_height;
1752
1753             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1754               p_dec->fmt_out.video.i_visible_width;
1755         }
1756
1757         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1758                      &p_dec->fmt_out.video.i_sar_den,
1759                      p_dec->fmt_out.video.i_sar_num,
1760                      p_dec->fmt_out.video.i_sar_den, 50000 );
1761
1762         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1763         p_owner->video = p_dec->fmt_out.video;
1764
1765         vlc_mutex_lock( &p_owner->lock );
1766         p_vout = p_owner->p_vout;
1767         p_owner->p_vout = NULL;
1768         vlc_mutex_unlock( &p_owner->lock );
1769
1770         p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
1771
1772         vlc_mutex_lock( &p_owner->lock );
1773         p_owner->p_vout = p_vout;
1774         vlc_mutex_unlock( &p_owner->lock );
1775
1776         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1777         if( p_vout == NULL )
1778         {
1779             msg_Err( p_dec, "failed to create video output" );
1780             p_dec->b_error = true;
1781             return NULL;
1782         }
1783
1784         if( p_owner->video.i_rmask )
1785             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1786         if( p_owner->video.i_gmask )
1787             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1788         if( p_owner->video.i_bmask )
1789             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1790     }
1791
1792     /* Get a new picture
1793      */
1794     for( ;; )
1795     {
1796         picture_t *p_picture;
1797         int i_pic, i_ready_pic;
1798
1799         if( p_dec->b_die || p_dec->b_error )
1800             return NULL;
1801
1802         /* The video filter chain required that there is always 1 free buffer
1803          * that it will use as temporary one. It will release the temporary
1804          * buffer once its work is done, so this check is safe even if we don't
1805          * lock around both count() and create().
1806          */
1807         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1808         {
1809             p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1810             if( p_picture )
1811                 return p_picture;
1812         }
1813
1814         if( DecoderIsFlushing( p_dec ) )
1815             return NULL;
1816
1817         /* Check the decoder doesn't leak pictures */
1818         for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1819         {
1820             const picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
1821
1822             if( p_pic->i_status == READY_PICTURE )
1823             {
1824                 i_ready_pic++;
1825                 /* If we have at least 2 ready pictures, wait for the vout thread to
1826                  * process one */
1827                 if( i_ready_pic >= 2 )
1828                     break;
1829
1830                 continue;
1831             }
1832
1833             if( p_pic->i_status == DISPLAYED_PICTURE )
1834             {
1835                 /* If at least one displayed picture is not referenced
1836                  * let vout free it */
1837                 if( p_pic->i_refcount == 0 )
1838                     break;
1839             }
1840         }
1841
1842         if( i_pic == p_owner->p_vout->render.i_pictures )
1843         {
1844             /* Too many pictures are still referenced, there is probably a bug
1845              * with the decoder */
1846             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1847
1848             /* Just free all the pictures */
1849             for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1850             {
1851                 picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
1852
1853                 if( p_pic->i_status == RESERVED_PICTURE )
1854                     vout_DestroyPicture( p_owner->p_vout, p_pic );
1855                 if( p_pic->i_refcount > 0 )
1856                     vout_UnlinkPicture( p_owner->p_vout, p_pic );
1857             }
1858         }
1859
1860         msleep( VOUT_OUTMEM_SLEEP );
1861     }
1862 }
1863
1864 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1865 {
1866     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1867 }
1868
1869 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1870 {
1871     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1872 }
1873
1874 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1875 {
1876     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1877 }
1878
1879 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1880 {
1881     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1882     vout_thread_t *p_vout = NULL;
1883     subpicture_t *p_subpic;
1884     int i_attempts = 30;
1885
1886     while( i_attempts-- )
1887     {
1888         if( p_dec->b_die || p_dec->b_error )
1889             break;
1890
1891         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1892         if( p_vout )
1893             break;
1894
1895         msleep( VOUT_DISPLAY_DELAY );
1896     }
1897
1898     if( !p_vout )
1899     {
1900         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1901         return NULL;
1902     }
1903
1904     if( p_owner->p_spu_vout != p_vout )
1905     {
1906         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1907                      &p_owner->i_spu_channel );
1908         p_owner->i_spu_order = 0;
1909         p_owner->p_spu_vout = p_vout;
1910     }
1911
1912     p_subpic = subpicture_New();
1913     if( p_subpic )
1914     {
1915         p_subpic->i_channel = p_owner->i_spu_channel;
1916         p_subpic->i_order = p_owner->i_spu_order++;
1917         p_subpic->b_subtitle = true;
1918     }
1919
1920     vlc_object_release( p_vout );
1921
1922     return p_subpic;
1923 }
1924
1925 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1926 {
1927     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1928     vout_thread_t *p_vout = NULL;
1929
1930     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1931     if( !p_vout || p_owner->p_spu_vout != p_vout )
1932     {
1933         if( p_vout )
1934             vlc_object_release( p_vout );
1935         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1936         return;
1937     }
1938
1939     subpicture_Delete( p_subpic );
1940
1941     vlc_object_release( p_vout );
1942 }
1943