]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Copy complete es format descriptor when extra data is added.
[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 #include <stdlib.h>
30 #include <vlc/vlc.h>
31
32 #include <vlc_block.h>
33 #include <vlc_vout.h>
34 #include <vlc_aout.h>
35 #include <vlc_sout.h>
36 #include <vlc_codec.h>
37 #include <vlc_osd.h>
38
39 #include <vlc_interface.h>
40 #include "audio_output/aout_internal.h"
41 #include "stream_output/stream_output.h"
42 #include "input_internal.h"
43
44 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
45 static void        DeleteDecoder( decoder_t * );
46
47 static int         DecoderThread( decoder_t * );
48 static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
49
50 /* Buffers allocation callbacks for the decoders */
51 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
52 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
53
54 static picture_t *vout_new_buffer( decoder_t * );
55 static void vout_del_buffer( decoder_t *, picture_t * );
56 static void vout_link_picture( decoder_t *, picture_t * );
57 static void vout_unlink_picture( decoder_t *, picture_t * );
58
59 static subpicture_t *spu_new_buffer( decoder_t * );
60 static void spu_del_buffer( decoder_t *, subpicture_t * );
61
62 static es_format_t null_es_format;
63
64 struct decoder_owner_sys_t
65 {
66     vlc_bool_t      b_own_thread;
67
68     int64_t         i_preroll_end;
69
70     input_thread_t  *p_input;
71
72     aout_instance_t *p_aout;
73     aout_input_t    *p_aout_input;
74
75     vout_thread_t   *p_vout;
76
77     vout_thread_t   *p_spu_vout;
78     int              i_spu_channel;
79
80     sout_instance_t         *p_sout;
81     sout_packetizer_input_t *p_sout_input;
82
83     /* Some decoders require already packetized data (ie. not truncated) */
84     decoder_t *p_packetizer;
85
86     /* Current format in use by the output */
87     video_format_t video;
88     audio_format_t audio;
89     es_format_t    sout;
90
91     /* fifo */
92     block_fifo_t *p_fifo;
93 };
94
95
96 /**
97  * Spawns a new decoder thread
98  *
99  * \param p_input the input thread
100  * \param p_es the es descriptor
101  * \return the spawned decoder object
102  */
103 decoder_t *input_DecoderNew( input_thread_t *p_input,
104                              es_format_t *fmt, vlc_bool_t b_force_decoder )
105 {
106     decoder_t   *p_dec = NULL;
107     vlc_value_t val;
108
109     /* If we are in sout mode, search for packetizer module */
110     if( p_input->p->p_sout && !b_force_decoder )
111     {
112         /* Create the decoder configuration structure */
113         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
114         if( p_dec == NULL )
115         {
116             msg_Err( p_input, "could not create packetizer" );
117             intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"), 
118                             _("VLC could not open the packetizer module.") );
119             return NULL;
120         }
121     }
122     else
123     {
124         /* Create the decoder configuration structure */
125         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
126         if( p_dec == NULL )
127         {
128             msg_Err( p_input, "could not create decoder" );
129             intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"), 
130                             _("VLC could not open the decoder module.") );
131             return NULL;
132         }
133     }
134
135     if( !p_dec->p_module )
136     {
137         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
138                  "VLC probably does not support this sound or video format.",
139                  (char*)&p_dec->fmt_in.i_codec );
140         intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
141             "for format"), _("VLC probably does not support the \"%4.4s\" "
142             "audio or video format. Unfortunately there is no way for you "
143             "to fix this."), (char*)&p_dec->fmt_in.i_codec );
144
145         DeleteDecoder( p_dec );
146         vlc_object_destroy( p_dec );
147         return NULL;
148     }
149
150     if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
151         !b_force_decoder )
152     {
153         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
154         p_dec->p_owner->b_own_thread = VLC_FALSE;
155     }
156     else
157     {
158         var_Get( p_input, "minimize-threads", &val );
159         p_dec->p_owner->b_own_thread = !val.b_bool;
160     }
161
162     if( p_dec->p_owner->b_own_thread )
163     {
164         int i_priority;
165         if( fmt->i_cat == AUDIO_ES )
166             i_priority = VLC_THREAD_PRIORITY_AUDIO;
167         else
168             i_priority = VLC_THREAD_PRIORITY_VIDEO;
169
170         /* Spawn the decoder thread */
171         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
172                                i_priority, VLC_FALSE ) )
173         {
174             msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
175                              p_dec->p_module->psz_object_name );
176             module_Unneed( p_dec, p_dec->p_module );
177             DeleteDecoder( p_dec );
178             vlc_object_destroy( p_dec );
179             return NULL;
180         }
181     }
182
183     return p_dec;
184 }
185
186 /**
187  * Kills a decoder thread and waits until it's finished
188  *
189  * \param p_input the input thread
190  * \param p_es the es descriptor
191  * \return nothing
192  */
193 void input_DecoderDelete( decoder_t *p_dec )
194 {
195     p_dec->b_die = VLC_TRUE;
196
197     if( p_dec->p_owner->b_own_thread )
198     {
199         /* Make sure the thread leaves the function by
200          * sending it an empty block. */
201         block_t *p_block = block_New( p_dec, 0 );
202         input_DecoderDecode( p_dec, p_block );
203
204         vlc_thread_join( p_dec );
205
206         /* Don't module_Unneed() here because of the dll loader that wants
207          * close() in the same thread than open()/decode() */
208     }
209     else
210     {
211         /* Flush */
212         input_DecoderDecode( p_dec, NULL );
213
214         module_Unneed( p_dec, p_dec->p_module );
215     }
216
217     /* Delete decoder configuration */
218     DeleteDecoder( p_dec );
219
220     /* Delete the decoder */
221     vlc_object_destroy( p_dec );
222 }
223
224 /**
225  * Put a block_t in the decoder's fifo.
226  *
227  * \param p_dec the decoder object
228  * \param p_block the data block
229  */
230 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
231 {
232     if( p_dec->p_owner->b_own_thread )
233     {
234         if( p_dec->p_owner->p_input->p->b_out_pace_control )
235         {
236             /* FIXME !!!!! */
237             while( !p_dec->b_die && !p_dec->b_error &&
238                    p_dec->p_owner->p_fifo->i_depth > 10 )
239             {
240                 msleep( 1000 );
241             }
242         }
243         else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
244         {
245             /* FIXME: ideally we would check the time amount of data
246              * in the fifo instead of its size. */
247             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
248                       "consumed quickly enough), resetting fifo!" );
249             block_FifoEmpty( p_dec->p_owner->p_fifo );
250         }
251
252         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
253     }
254     else
255     {
256         if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
257         {
258             if( p_block ) block_Release( p_block );
259         }
260         else
261         {
262             DecoderDecode( p_dec, p_block );
263         }
264     }
265 }
266
267 void input_DecoderDiscontinuity( decoder_t * p_dec )
268 {
269     block_t *p_null;
270
271     /* Empty the fifo */
272     if( p_dec->p_owner->b_own_thread )
273     {
274         block_FifoEmpty( p_dec->p_owner->p_fifo );
275     }
276
277     /* Send a special block */
278     p_null = block_New( p_dec, 128 );
279     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
280     memset( p_null->p_buffer, 0, p_null->i_buffer );
281
282     input_DecoderDecode( p_dec, p_null );
283 }
284
285 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
286 {
287     if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
288     {
289         return VLC_FALSE;
290     }
291     return VLC_TRUE;
292 }
293
294 void input_DecoderPreroll( decoder_t *p_dec, int64_t i_preroll_end )
295 {
296     p_dec->p_owner->i_preroll_end = i_preroll_end;
297 }
298
299 #if 0
300 /**
301  * Create a NULL packet for padding in case of a data loss
302  *
303  * \param p_input the input thread
304  * \param p_es es descriptor
305  * \return nothing
306  */
307 static void input_NullPacket( input_thread_t * p_input,
308                               es_descriptor_t * p_es )
309 {
310 #if 0
311     block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
312     if( p_block )
313     {
314         memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
315         p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
316
317         block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
318     }
319 #endif
320 }
321
322 /**
323  * Send a NULL packet to the decoders
324  *
325  * \param p_input the input thread
326  * \return nothing
327  */
328 void input_EscapeDiscontinuity( input_thread_t * p_input )
329 {
330 #if 0
331     unsigned int i_es, i;
332
333     for( i_es = 0; i_es < p_input->p->stream.i_selected_es_number; i_es++ )
334     {
335         es_descriptor_t * p_es = p_input->p->stream.pp_selected_es[i_es];
336
337         if( p_es->p_dec != NULL )
338         {
339             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
340             {
341                 input_NullPacket( p_input, p_es );
342             }
343         }
344     }
345 #endif
346 }
347
348 /**
349  * Send a NULL packet to the audio decoders
350  *
351  * \param p_input the input thread
352  * \return nothing
353  */
354 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
355 {
356 #if 0
357     unsigned int i_es, i;
358
359     for( i_es = 0; i_es < p_input->p->stream.i_selected_es_number; i_es++ )
360     {
361         es_descriptor_t * p_es = p_input->p->stream.pp_selected_es[i_es];
362
363         if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
364         {
365             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
366             {
367                 input_NullPacket( p_input, p_es );
368             }
369         }
370     }
371 #endif
372 }
373 #endif
374
375 /**
376  * Create a decoder object
377  *
378  * \param p_input the input thread
379  * \param p_es the es descriptor
380  * \param i_object_type Object type as define in include/vlc_objects.h
381  * \return the decoder object
382  */
383 static decoder_t * CreateDecoder( input_thread_t *p_input,
384                                   es_format_t *fmt, int i_object_type )
385 {
386     decoder_t *p_dec;
387
388     p_dec = vlc_object_create( p_input, i_object_type );
389     if( p_dec == NULL )
390     {
391         msg_Err( p_input, "out of memory" );
392         return NULL;
393     }
394
395     p_dec->pf_decode_audio = 0;
396     p_dec->pf_decode_video = 0;
397     p_dec->pf_decode_sub = 0;
398     p_dec->pf_packetize = 0;
399
400    /* Initialize the decoder fifo */
401     p_dec->p_module = NULL;
402
403     memset( &null_es_format, 0, sizeof(es_format_t) );
404     es_format_Copy( &p_dec->fmt_in, fmt );
405     es_format_Copy( &p_dec->fmt_out, &null_es_format );
406
407     /* Allocate our private structure for the decoder */
408     p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
409     if( p_dec->p_owner == NULL )
410     {
411         msg_Err( p_dec, "out of memory" );
412         return NULL;
413     }
414     p_dec->p_owner->b_own_thread = VLC_TRUE;
415     p_dec->p_owner->i_preroll_end = -1;
416     p_dec->p_owner->p_input = p_input;
417     p_dec->p_owner->p_aout = NULL;
418     p_dec->p_owner->p_aout_input = NULL;
419     p_dec->p_owner->p_vout = NULL;
420     p_dec->p_owner->p_spu_vout = NULL;
421     p_dec->p_owner->i_spu_channel = 0;
422     p_dec->p_owner->p_sout = p_input->p->p_sout;
423     p_dec->p_owner->p_sout_input = NULL;
424     p_dec->p_owner->p_packetizer = NULL;
425
426     /* decoder fifo */
427     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
428     {
429         msg_Err( p_dec, "out of memory" );
430         return NULL;
431     }
432
433     /* Set buffers allocation callbacks for the decoders */
434     p_dec->pf_aout_buffer_new = aout_new_buffer;
435     p_dec->pf_aout_buffer_del = aout_del_buffer;
436     p_dec->pf_vout_buffer_new = vout_new_buffer;
437     p_dec->pf_vout_buffer_del = vout_del_buffer;
438     p_dec->pf_picture_link    = vout_link_picture;
439     p_dec->pf_picture_unlink  = vout_unlink_picture;
440     p_dec->pf_spu_buffer_new  = spu_new_buffer;
441     p_dec->pf_spu_buffer_del  = spu_del_buffer;
442
443     vlc_object_attach( p_dec, p_input );
444
445     /* Find a suitable decoder/packetizer module */
446     if( i_object_type == VLC_OBJECT_DECODER )
447         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
448     else
449         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
450
451     /* Check if decoder requires already packetized data */
452     if( i_object_type == VLC_OBJECT_DECODER &&
453         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
454     {
455         p_dec->p_owner->p_packetizer =
456             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
457         if( p_dec->p_owner->p_packetizer )
458         {
459             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
460                             &p_dec->fmt_in );
461
462             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
463                             &null_es_format );
464
465             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
466
467             p_dec->p_owner->p_packetizer->p_module =
468                 module_Need( p_dec->p_owner->p_packetizer,
469                              "packetizer", "$packetizer", 0 );
470
471             if( !p_dec->p_owner->p_packetizer->p_module )
472             {
473                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
474                 vlc_object_detach( p_dec->p_owner->p_packetizer );
475                 vlc_object_destroy( p_dec->p_owner->p_packetizer );
476             }
477         }
478     }
479
480     return p_dec;
481 }
482
483 /**
484  * The decoding main loop
485  *
486  * \param p_dec the decoder
487  * \return 0
488  */
489 static int DecoderThread( decoder_t * p_dec )
490 {
491     block_t *p_block;
492
493     /* The decoder's main loop */
494     while( !p_dec->b_die && !p_dec->b_error )
495     {
496         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
497         {
498             p_dec->b_error = 1;
499             break;
500         }
501         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
502         {
503             break;
504         }
505     }
506
507     while( !p_dec->b_die )
508     {
509         /* Trash all received PES packets */
510         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
511         if( p_block ) block_Release( p_block );
512     }
513
514     /* We do it here because of the dll loader that wants close() in the
515      * same thread than open()/decode() */
516     module_Unneed( p_dec, p_dec->p_module );
517
518     return 0;
519 }
520
521 /**
522  * Decode a block
523  *
524  * \param p_dec the decoder object
525  * \param p_block the block to decode
526  * \return VLC_SUCCESS or an error code
527  */
528 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
529 {
530     int i_rate = p_block ? p_block->i_rate : 1000;
531
532     if( p_block && p_block->i_buffer <= 0 )
533     {
534         block_Release( p_block );
535         return VLC_SUCCESS;
536     }
537
538     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
539     {
540         block_t *p_sout_block;
541
542         while( ( p_sout_block =
543                      p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
544         {
545             if( !p_dec->p_owner->p_sout_input )
546             {
547                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
548
549                 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
550                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
551                 if( p_dec->fmt_in.psz_language )
552                 {
553                     if( p_dec->p_owner->sout.psz_language )
554                         free( p_dec->p_owner->sout.psz_language );
555                     p_dec->p_owner->sout.psz_language =
556                         strdup( p_dec->fmt_in.psz_language );
557                 }
558
559                 p_dec->p_owner->p_sout_input =
560                     sout_InputNew( p_dec->p_owner->p_sout,
561                                    &p_dec->p_owner->sout );
562
563                 if( p_dec->p_owner->p_sout_input == NULL )
564                 {
565                     msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
566                              (char *)&p_dec->p_owner->sout.i_codec );
567                     p_dec->b_error = VLC_TRUE;
568
569                     while( p_sout_block )
570                     {
571                         block_t *p_next = p_sout_block->p_next;
572                         block_Release( p_sout_block );
573                         p_sout_block = p_next;
574                     }
575                     break;
576                 }
577             }
578
579             while( p_sout_block )
580             {
581                 block_t *p_next = p_sout_block->p_next;
582
583                 p_sout_block->p_next = NULL;
584                 p_sout_block->i_rate = i_rate;
585
586                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
587                                       p_sout_block );
588
589                 p_sout_block = p_next;
590             }
591
592             /* For now it's enough, as only sout inpact on this flag */
593             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
594                 p_dec->p_owner->p_input->p->b_out_pace_control )
595             {
596                 msg_Dbg( p_dec, "switching to sync mode" );
597                 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
598             }
599             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
600                      !p_dec->p_owner->p_input->p->b_out_pace_control )
601             {
602                 msg_Dbg( p_dec, "switching to async mode" );
603                 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
604             }
605         }
606     }
607     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
608     {
609         aout_buffer_t *p_aout_buf;
610
611         if( p_dec->p_owner->p_packetizer )
612         {
613             block_t *p_packetized_block;
614             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
615
616             while( (p_packetized_block =
617                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
618             {
619                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
620                 {
621                     es_format_Clean( &p_dec->fmt_in );
622                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
623                 }
624
625                 while( p_packetized_block )
626                 {
627                     block_t *p_next = p_packetized_block->p_next;
628                     p_packetized_block->p_next = NULL;
629                     p_packetized_block->i_rate = i_rate;
630
631                     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
632                                                        &p_packetized_block )) )
633                     {
634                         input_thread_t *p_i =(input_thread_t*)(p_dec->p_parent);
635                         vlc_mutex_lock( &p_i->p->counters.counters_lock );
636                         stats_UpdateInteger( p_dec,
637                                p_i->p->counters.p_decoded_audio, 1, NULL );
638                         vlc_mutex_unlock( &p_i->p->counters.counters_lock );
639
640                         /* FIXME the best would be to handle the case
641                          * start_date < preroll < end_date
642                          * but that's not easy with non raw audio stream */
643                         if( p_dec->p_owner->i_preroll_end > 0 &&
644                             p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
645                         {
646                             aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
647                                                   p_dec->p_owner->p_aout_input, p_aout_buf );
648                         }
649                         else
650                         {
651                             p_dec->p_owner->i_preroll_end = -1;
652                             aout_DecPlay( p_dec->p_owner->p_aout,
653                                           p_dec->p_owner->p_aout_input,
654                                           p_aout_buf );
655                         }
656                     }
657
658                     p_packetized_block = p_next;
659                 }
660             }
661         }
662         else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
663         {
664             input_thread_t *p_i = (input_thread_t*)(p_dec->p_parent);
665             vlc_mutex_lock( &p_i->p->counters.counters_lock );
666             stats_UpdateInteger( p_dec,
667                                p_i->p->counters.p_decoded_audio, 1, NULL );
668             vlc_mutex_unlock( &p_i->p->counters.counters_lock );
669
670             if( p_dec->p_owner->i_preroll_end > 0 &&
671                 p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
672             {
673                 aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
674                                       p_dec->p_owner->p_aout_input, p_aout_buf );
675             }
676             else
677             {
678                 p_dec->p_owner->i_preroll_end = -1;
679                 aout_DecPlay( p_dec->p_owner->p_aout,
680                               p_dec->p_owner->p_aout_input,
681                               p_aout_buf );
682             }
683         }
684     }
685     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
686     {
687         picture_t *p_pic;
688
689         if( p_dec->p_owner->p_packetizer )
690         {
691             block_t *p_packetized_block;
692             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
693
694             while( (p_packetized_block =
695                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
696             {
697                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
698                 {
699                     es_format_Clean( &p_dec->fmt_in );
700                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
701                 }
702
703                 while( p_packetized_block )
704                 {
705                     block_t *p_next = p_packetized_block->p_next;
706                     p_packetized_block->p_next = NULL;
707                     p_packetized_block->i_rate = i_rate;
708
709                     while( (p_pic = p_dec->pf_decode_video( p_dec,
710                                                        &p_packetized_block )) )
711                     {
712                         input_thread_t *p_i =(input_thread_t*)(p_dec->p_parent);
713                         vlc_mutex_lock( &p_i->p->counters.counters_lock );
714                         stats_UpdateInteger( p_dec,
715                                p_i->p->counters.p_decoded_video, 1, NULL );
716                         vlc_mutex_unlock( &p_i->p->counters.counters_lock );
717
718                         if( p_dec->p_owner->i_preroll_end > 0 &&
719                             p_pic->date < p_dec->p_owner->i_preroll_end )
720                         {
721                             vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
722                         }
723                         else
724                         {
725                             p_dec->p_owner->i_preroll_end = -1;
726                             vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
727                                               p_pic->date );
728                             vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
729                         }
730                     }
731
732                     p_packetized_block = p_next;
733                 }
734             }
735         }
736         else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
737         {
738             input_thread_t *p_i = (input_thread_t*)(p_dec->p_parent);
739             vlc_mutex_lock( &p_i->p->counters.counters_lock );
740             stats_UpdateInteger( p_dec,
741                                p_i->p->counters.p_decoded_video, 1, NULL );
742             vlc_mutex_unlock( &p_i->p->counters.counters_lock );
743
744             if( p_dec->p_owner->i_preroll_end > 0 &&
745                 p_pic->date < p_dec->p_owner->i_preroll_end )
746             {
747                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
748             }
749             else
750             {
751                 p_dec->p_owner->i_preroll_end = -1;
752                 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
753                 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
754             }
755         }
756     }
757     else if( p_dec->fmt_in.i_cat == SPU_ES )
758     {
759         vout_thread_t *p_vout;
760         subpicture_t *p_spu;
761         while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
762         {
763             input_thread_t *p_i = (input_thread_t*)(p_dec->p_parent);
764             vlc_mutex_lock( &p_i->p->counters.counters_lock );
765             stats_UpdateInteger( p_dec,
766                                p_i->p->counters.p_decoded_sub, 1, NULL );
767             vlc_mutex_unlock( &p_i->p->counters.counters_lock );
768
769             if( p_dec->p_owner->i_preroll_end > 0 &&
770                 p_spu->i_start < p_dec->p_owner->i_preroll_end &&
771                 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
772             {
773                 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
774                 continue;
775             }
776
777             p_dec->p_owner->i_preroll_end = -1;
778             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
779             if( p_vout )
780             {
781                 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
782                 vlc_object_release( p_vout );
783             }
784         }
785     }
786     else
787     {
788         msg_Err( p_dec, "unknown ES format" );
789         p_dec->b_error = 1;
790     }
791
792     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
793 }
794
795 /**
796  * Destroys a decoder object
797  *
798  * \param p_dec the decoder object
799  * \return nothing
800  */
801 static void DeleteDecoder( decoder_t * p_dec )
802 {
803     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
804              (char*)&p_dec->fmt_in.i_codec,
805              p_dec->p_owner->p_fifo->i_depth );
806
807     /* Free all packets still in the decoder fifo. */
808     block_FifoEmpty( p_dec->p_owner->p_fifo );
809     block_FifoRelease( p_dec->p_owner->p_fifo );
810
811     /* Cleanup */
812     if( p_dec->p_owner->p_aout_input )
813         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
814
815     if( p_dec->p_owner->p_vout )
816     {
817         int i_pic;
818
819 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
820         /* Hack to make sure all the the pictures are freed by the decoder */
821         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
822              i_pic++ )
823         {
824             if( p_pic->i_status == RESERVED_PICTURE )
825                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
826             if( p_pic->i_refcount > 0 )
827                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
828         }
829 #undef p_pic
830
831         /* We are about to die. Reattach video output to p_vlc. */
832         vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
833     }
834
835     if( p_dec->p_owner->p_sout_input )
836     {
837         sout_InputDelete( p_dec->p_owner->p_sout_input );
838         es_format_Clean( &p_dec->p_owner->sout );
839     }
840
841     if( p_dec->fmt_in.i_cat == SPU_ES )
842     {
843         vout_thread_t *p_vout;
844
845         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
846         if( p_vout )
847         {
848             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
849                          p_dec->p_owner->i_spu_channel );
850             vlc_object_release( p_vout );
851         }
852     }
853
854     es_format_Clean( &p_dec->fmt_in );
855     es_format_Clean( &p_dec->fmt_out );
856
857     if( p_dec->p_owner->p_packetizer )
858     {
859         module_Unneed( p_dec->p_owner->p_packetizer,
860                        p_dec->p_owner->p_packetizer->p_module );
861         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
862         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
863         vlc_object_detach( p_dec->p_owner->p_packetizer );
864         vlc_object_destroy( p_dec->p_owner->p_packetizer );
865     }
866
867     vlc_object_detach( p_dec );
868
869     free( p_dec->p_owner );
870 }
871
872 /*****************************************************************************
873  * Buffers allocation callbacks for the decoders
874  *****************************************************************************/
875 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
876 {
877     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
878     aout_buffer_t *p_buffer;
879
880     if( p_sys->p_aout_input != NULL &&
881         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
882           p_dec->fmt_out.audio.i_original_channels !=
883               p_sys->audio.i_original_channels ||
884           p_dec->fmt_out.audio.i_bytes_per_frame !=
885               p_sys->audio.i_bytes_per_frame ) )
886     {
887         /* Parameters changed, restart the aout */
888         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
889         p_sys->p_aout_input = NULL;
890     }
891
892     if( p_sys->p_aout_input == NULL )
893     {
894         audio_sample_format_t format;
895         int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
896
897         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
898         p_sys->audio = p_dec->fmt_out.audio;
899
900         memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
901         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
902                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
903         {
904             if ( i_force_dolby == 1 )
905             {
906                 format.i_original_channels = format.i_original_channels |
907                                              AOUT_CHAN_DOLBYSTEREO;
908             }
909             else /* i_force_dolby == 2 */
910             {
911                 format.i_original_channels = format.i_original_channels &
912                                              ~AOUT_CHAN_DOLBYSTEREO;
913             }
914         }
915
916         p_sys->p_aout_input =
917             aout_DecNew( p_dec, &p_sys->p_aout, &format );
918         if( p_sys->p_aout_input == NULL )
919         {
920             msg_Err( p_dec, "failed to create audio output" );
921             p_dec->b_error = VLC_TRUE;
922             return NULL;
923         }
924         p_dec->fmt_out.audio.i_bytes_per_frame =
925             p_sys->audio.i_bytes_per_frame;
926     }
927
928     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
929                                   i_samples );
930
931     return p_buffer;
932 }
933
934 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
935 {
936     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
937                           p_dec->p_owner->p_aout_input, p_buffer );
938 }
939
940 static picture_t *vout_new_buffer( decoder_t *p_dec )
941 {
942     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
943     picture_t *p_pic;
944
945     if( p_sys->p_vout == NULL ||
946         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
947         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
948         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
949         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
950     {
951         if( !p_dec->fmt_out.video.i_width ||
952             !p_dec->fmt_out.video.i_height )
953         {
954             /* Can't create a new vout without display size */
955             return NULL;
956         }
957
958         if( !p_dec->fmt_out.video.i_visible_width ||
959             !p_dec->fmt_out.video.i_visible_height )
960         {
961             if( p_dec->fmt_in.video.i_visible_width &&
962                 p_dec->fmt_in.video.i_visible_height )
963             {
964                 p_dec->fmt_out.video.i_visible_width =
965                     p_dec->fmt_in.video.i_visible_width;
966                 p_dec->fmt_out.video.i_visible_height =
967                     p_dec->fmt_in.video.i_visible_height;
968             }
969             else
970             {
971                 p_dec->fmt_out.video.i_visible_width =
972                     p_dec->fmt_out.video.i_width;
973                 p_dec->fmt_out.video.i_visible_height =
974                     p_dec->fmt_out.video.i_height;
975             }
976         }
977
978         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
979             var_CreateGetBool( p_dec, "hdtv-fix" ) )
980         {
981             p_dec->fmt_out.video.i_visible_height = 1080;
982             p_dec->fmt_out.video.i_sar_num *= 135;
983             p_dec->fmt_out.video.i_sar_den *= 136;
984             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
985         }
986
987         if( !p_dec->fmt_out.video.i_sar_num ||
988             !p_dec->fmt_out.video.i_sar_den )
989         {
990             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
991               p_dec->fmt_out.video.i_visible_height;
992
993             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
994               p_dec->fmt_out.video.i_visible_width;
995         }
996
997         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
998                      &p_dec->fmt_out.video.i_sar_den,
999                      p_dec->fmt_out.video.i_sar_num,
1000                      p_dec->fmt_out.video.i_sar_den, 50000 );
1001
1002         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1003         p_sys->video = p_dec->fmt_out.video;
1004
1005         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1006                                       &p_dec->fmt_out.video );
1007         if( p_sys->p_vout == NULL )
1008         {
1009             msg_Err( p_dec, "failed to create video output" );
1010             p_dec->b_error = VLC_TRUE;
1011             return NULL;
1012         }
1013
1014         if( p_sys->video.i_rmask )
1015             p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1016         if( p_sys->video.i_gmask )
1017             p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1018         if( p_sys->video.i_bmask )
1019             p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1020     }
1021
1022     /* Get a new picture */
1023     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1024     {
1025         int i_pic, i_ready_pic = 0;
1026
1027         if( p_dec->b_die || p_dec->b_error )
1028         {
1029             return NULL;
1030         }
1031
1032 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1033         /* Check the decoder doesn't leak pictures */
1034         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1035              i_pic++ )
1036         {
1037             if( p_pic->i_status == READY_PICTURE )
1038             {
1039                 if( i_ready_pic++ > 0 ) break;
1040                 else continue;
1041             }
1042
1043             if( p_pic->i_status != DISPLAYED_PICTURE &&
1044                 p_pic->i_status != RESERVED_PICTURE &&
1045                 p_pic->i_status != READY_PICTURE ) break;
1046
1047             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1048                 break;
1049         }
1050         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1051         {
1052             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1053
1054             /* Just free all the pictures */
1055             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1056                  i_pic++ )
1057             {
1058                 if( p_pic->i_status == RESERVED_PICTURE )
1059                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1060                 if( p_pic->i_refcount > 0 )
1061                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1062             }
1063         }
1064 #undef p_pic
1065
1066         msleep( VOUT_OUTMEM_SLEEP );
1067     }
1068
1069     return p_pic;
1070 }
1071
1072 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1073 {
1074     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1075 }
1076
1077 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1078 {
1079     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1080 }
1081
1082 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1083 {
1084     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1085 }
1086
1087 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1088 {
1089     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1090     vout_thread_t *p_vout = NULL;
1091     subpicture_t *p_subpic;
1092     int i_attempts = 30;
1093
1094     while( i_attempts-- )
1095     {
1096         if( p_dec->b_die || p_dec->b_error ) break;
1097
1098         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1099         if( p_vout ) break;
1100
1101         msleep( VOUT_DISPLAY_DELAY );
1102     }
1103
1104     if( !p_vout )
1105     {
1106         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1107         return NULL;
1108     }
1109
1110     if( p_sys->p_spu_vout != p_vout )
1111     {
1112         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1113                      &p_sys->i_spu_channel );
1114         p_sys->p_spu_vout = p_vout;
1115     }
1116
1117     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1118     if( p_subpic )
1119     {
1120         p_subpic->i_channel = p_sys->i_spu_channel;
1121     }
1122
1123     vlc_object_release( p_vout );
1124
1125     return p_subpic;
1126 }
1127
1128 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1129 {
1130     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1131     vout_thread_t *p_vout = NULL;
1132
1133     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1134     if( !p_vout || p_sys->p_spu_vout != p_vout )
1135     {
1136         if( p_vout )
1137             vlc_object_release( p_vout );
1138         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1139         return;
1140     }
1141
1142     spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1143
1144     vlc_object_release( p_vout );
1145 }
1146