]> git.sesse.net Git - vlc/blob - src/input/input_dec.c
0df4b453d601934596b8e090c2498dce240aedbe
[vlc] / src / input / input_dec.c
1 /*****************************************************************************
2  * input_dec.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <string.h>                                    /* memcpy(), memset() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/decoder.h>
33 #include <vlc/vout.h>
34
35 #include "stream_output.h"
36
37 #include "input_ext-intf.h"
38 #include "input_ext-plugins.h"
39
40 #include "codecs.h"
41
42 static void input_NullPacket( input_thread_t *, es_descriptor_t * );
43
44 static decoder_t * CreateDecoder( input_thread_t *, es_descriptor_t *, int );
45 static int         DecoderThread( decoder_t * );
46 static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
47 static void        DeleteDecoder( decoder_t * );
48
49 /* Buffers allocation callbacks for the decoders */
50 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
51 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
52
53 static picture_t *vout_new_buffer( decoder_t * );
54 static void vout_del_buffer( decoder_t *, picture_t * );
55 static void vout_link_picture( decoder_t *, picture_t * );
56 static void vout_unlink_picture( decoder_t *, picture_t * );
57
58 static es_format_t null_es_format = {0};
59
60 struct decoder_owner_sys_t
61 {
62     vlc_bool_t      b_own_thread;
63
64     input_thread_t  *p_input;
65
66     aout_instance_t *p_aout;
67     aout_input_t    *p_aout_input;
68
69     vout_thread_t   *p_vout;
70
71     sout_instance_t         *p_sout;
72     sout_packetizer_input_t *p_sout_input;
73
74     /* Some decoders require already packetized data (ie. not truncated) */
75     decoder_t *p_packetizer;
76
77     /* Current format in use by the output */
78     video_format_t video;
79     audio_format_t audio;
80     es_format_t    sout;
81
82     /* fifo */
83     block_fifo_t *p_fifo;
84
85     /* */
86     input_buffers_t *p_method_data;
87     es_descriptor_t *p_es_descriptor;
88 };
89
90
91 /**
92  * Spawns a new decoder thread
93  *
94  * \param p_input the input thread
95  * \param p_es the es descriptor
96  * \return the spawned decoder object
97  */
98 decoder_t * input_RunDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
99 {
100     decoder_t   *p_dec = NULL;
101     vlc_value_t val;
102
103     /* If we are in sout mode, search for packetizer module */
104     if( !p_es->b_force_decoder && p_input->stream.p_sout )
105     {
106         /* Create the decoder configuration structure */
107         p_dec = CreateDecoder( p_input, p_es, VLC_OBJECT_PACKETIZER );
108         if( p_dec == NULL )
109         {
110             msg_Err( p_input, "could not create packetizer" );
111             return NULL;
112         }
113     }
114     else
115     {
116         /* Create the decoder configuration structure */
117         p_dec = CreateDecoder( p_input, p_es, VLC_OBJECT_DECODER );
118         if( p_dec == NULL )
119         {
120             msg_Err( p_input, "could not create decoder" );
121             return NULL;
122         }
123     }
124
125     if( !p_dec->p_module )
126     {
127         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
128                  "VLC probably does not support this sound or video format.",
129                  (char*)&p_dec->fmt_in.i_codec );
130
131         DeleteDecoder( p_dec );
132         vlc_object_destroy( p_dec );
133         return NULL;
134     }
135
136     var_Get( p_input, "minimize-threads", &val );
137     p_dec->p_owner->b_own_thread = val.b_bool ? VLC_FALSE : VLC_TRUE;
138
139     if( p_dec->p_owner->b_own_thread )
140     {
141         int i_priority;
142         if ( p_es->i_cat == AUDIO_ES )
143         {
144             i_priority = VLC_THREAD_PRIORITY_AUDIO;
145         }
146         else
147         {
148             i_priority = VLC_THREAD_PRIORITY_VIDEO;
149         }
150
151         /* Spawn the decoder thread */
152         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
153                                i_priority, VLC_FALSE ) )
154         {
155             msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
156                              p_dec->p_module->psz_object_name );
157             module_Unneed( p_dec, p_dec->p_module );
158             DeleteDecoder( p_dec );
159             vlc_object_destroy( p_dec );
160             return NULL;
161         }
162     }
163
164     /* Select a new ES */
165     INSERT_ELEM( p_input->stream.pp_selected_es,
166                  p_input->stream.i_selected_es_number,
167                  p_input->stream.i_selected_es_number,
168                  p_es );
169
170     p_input->stream.b_changed = 1;
171
172     return p_dec;
173 }
174
175 /**
176  * Kills a decoder thread and waits until it's finished
177  *
178  * \param p_input the input thread
179  * \param p_es the es descriptor
180  * \return nothing
181  */
182 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
183 {
184     decoder_t *p_dec = p_es->p_dec;
185     int i_dummy;
186
187     p_dec->b_die = VLC_TRUE;
188
189     if( p_dec->p_owner->b_own_thread )
190     {
191         /* Make sure the thread leaves the NextDataPacket() function by
192          * sending it a few null packets. */
193         for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
194         {
195             input_NullPacket( p_input, p_es );
196         }
197
198         if( p_es->p_pes != NULL )
199         {
200             input_DecodePES( p_es->p_dec, p_es->p_pes );
201         }
202
203         /* Waiting for the thread to exit */
204         /* I thought that unlocking was better since thread join can be long
205          * but it actually creates late pictures and freezes --stef */
206         /* vlc_mutex_unlock( &p_input->stream.stream_lock ); */
207         vlc_thread_join( p_dec );
208         /* vlc_mutex_lock( &p_input->stream.stream_lock ); */
209 #if 0
210         /* XXX We don't do it here because of dll loader that want close in the
211          * same thread than open/decode */
212         /* Unneed module */
213         module_Unneed( p_dec, p_dec->p_module );
214 #endif
215     }
216     else
217     {
218         module_Unneed( p_dec, p_dec->p_module );
219     }
220
221     /* Delete decoder configuration */
222     DeleteDecoder( p_dec );
223
224     /* Delete the decoder */
225     vlc_object_destroy( p_dec );
226
227     /* Tell the input there is no more decoder */
228     p_es->p_dec = NULL;
229
230     p_input->stream.b_changed = 1;
231 }
232
233 /**
234  * Put a PES in the decoder's fifo.
235  *
236  * \param p_dec the decoder object
237  * \param p_pes the pes packet
238  * \return nothing
239  */
240 void input_DecodePES( decoder_t * p_dec, pes_packet_t * p_pes )
241 {
242     data_packet_t *p_data;
243     int     i_size = 0;
244
245     for( p_data = p_pes->p_first; p_data != NULL; p_data = p_data->p_next )
246     {
247         i_size += p_data->p_payload_end - p_data->p_payload_start;
248     }
249     if( i_size > 0 )
250     {
251         block_t *p_block = block_New( p_dec, i_size );
252         if( p_block )
253         {
254             uint8_t *p_buffer = p_block->p_buffer;
255
256             for( p_data = p_pes->p_first; p_data != NULL; p_data = p_data->p_next )
257             {
258                 int i_copy = p_data->p_payload_end - p_data->p_payload_start;
259
260                 memcpy( p_buffer, p_data->p_payload_start, i_copy );
261
262                 p_buffer += i_copy;
263             }
264             p_block->i_pts = p_pes->i_pts;
265             p_block->i_dts = p_pes->i_dts;
266             if( p_pes->b_discontinuity )
267                 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
268             p_block->i_rate = p_pes->i_rate;
269
270             input_DecodeBlock( p_dec, p_block );
271         }
272     }
273
274     input_DeletePES( p_dec->p_owner->p_method_data, p_pes );
275 }
276
277 /**
278  * Put a block_t in the decoder's fifo.
279  *
280  * \param p_dec the decoder object
281  * \param p_block the data block
282  */
283 void input_DecodeBlock( decoder_t * p_dec, block_t *p_block )
284 {
285     if( p_dec->p_owner->b_own_thread )
286     {
287         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
288
289         if( p_dec->p_owner->p_input->b_out_pace_control )
290         {
291             /* FIXME !!!!! */
292             while( !p_dec->b_die && !p_dec->b_error &&
293                    p_dec->p_owner->p_fifo->i_depth > 10 )
294             {
295                 msleep( 1000 );
296             }
297         }
298     }
299     else
300     {
301         if( p_dec->b_error || p_block->i_buffer <= 0 )
302         {
303             block_Release( p_block );
304         }
305         else
306         {
307             DecoderDecode( p_dec, p_block );
308         }
309     }
310 }
311
312 /**
313  * Create a NULL packet for padding in case of a data loss
314  *
315  * \param p_input the input thread
316  * \param p_es es descriptor
317  * \return nothing
318  */
319 static void input_NullPacket( input_thread_t * p_input,
320                               es_descriptor_t * p_es )
321 {
322     block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
323     if( p_block )
324     {
325         memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
326         p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
327
328         block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
329     }
330 }
331
332 /**
333  * Send a NULL packet to the decoders
334  *
335  * \param p_input the input thread
336  * \return nothing
337  */
338 void input_EscapeDiscontinuity( input_thread_t * p_input )
339 {
340     unsigned int i_es, i;
341
342     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
343     {
344         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
345
346         if( p_es->p_dec != NULL )
347         {
348             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
349             {
350                 input_NullPacket( p_input, p_es );
351             }
352         }
353     }
354 }
355
356 /**
357  * Send a NULL packet to the audio decoders
358  *
359  * \param p_input the input thread
360  * \return nothing
361  */
362 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
363 {
364     unsigned int i_es, i;
365
366     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
367     {
368         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
369
370         if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
371         {
372             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
373             {
374                 input_NullPacket( p_input, p_es );
375             }
376         }
377     }
378 }
379
380 /**
381  * Create a decoder object
382  *
383  * \param p_input the input thread
384  * \param p_es the es descriptor
385  * \param i_object_type Object type as define in include/vlc_objects.h
386  * \return the decoder object
387  */
388 static decoder_t * CreateDecoder( input_thread_t * p_input,
389                                   es_descriptor_t * p_es, int i_object_type )
390 {
391     decoder_t *p_dec;
392
393     p_dec = vlc_object_create( p_input, i_object_type );
394     if( p_dec == NULL )
395     {
396         msg_Err( p_input, "out of memory" );
397         return NULL;
398     }
399
400     p_dec->pf_decode_audio = 0;
401     p_dec->pf_decode_video = 0;
402     p_dec->pf_decode_sub = 0;
403     p_dec->pf_packetize = 0;
404
405     /* Initialize the decoder fifo */
406     p_dec->p_module = NULL;
407
408     es_format_Copy( &p_dec->fmt_in, &p_es->fmt );
409
410     if( p_es->p_waveformatex )
411     {
412 #define p_wf ((WAVEFORMATEX *)p_es->p_waveformatex)
413         p_dec->fmt_in.audio.i_channels = p_wf->nChannels;
414         p_dec->fmt_in.audio.i_rate = p_wf->nSamplesPerSec;
415         p_dec->fmt_in.i_bitrate = p_wf->nAvgBytesPerSec * 8;
416         p_dec->fmt_in.audio.i_blockalign = p_wf->nBlockAlign;
417         p_dec->fmt_in.audio.i_bitspersample = p_wf->wBitsPerSample;
418         p_dec->fmt_in.i_extra = p_wf->cbSize;
419         p_dec->fmt_in.p_extra = NULL;
420         if( p_wf->cbSize )
421         {
422             p_dec->fmt_in.p_extra = malloc( p_wf->cbSize );
423             memcpy( p_dec->fmt_in.p_extra, &p_wf[1], p_wf->cbSize );
424         }
425     }
426
427     if( p_es->p_bitmapinfoheader )
428     {
429 #define p_bih ((BITMAPINFOHEADER *) p_es->p_bitmapinfoheader)
430         p_dec->fmt_in.i_extra = p_bih->biSize - sizeof(BITMAPINFOHEADER);
431         p_dec->fmt_in.p_extra = NULL;
432         if( p_dec->fmt_in.i_extra )
433         {
434             p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
435             memcpy( p_dec->fmt_in.p_extra, &p_bih[1], p_dec->fmt_in.i_extra );
436         }
437
438         p_dec->fmt_in.video.i_width = p_bih->biWidth;
439         p_dec->fmt_in.video.i_height = p_bih->biHeight;
440     }
441
442     /* FIXME
443      *  - 1: beurk
444      *  - 2: I'm not sure there isn't any endian problem here (spu)... */
445     if( p_es->i_cat == SPU_ES && p_es->p_demux_data )
446     {
447         if( ( p_es->i_fourcc == VLC_FOURCC( 's', 'p', 'u', ' ' ) ||
448               p_es->i_fourcc == VLC_FOURCC( 's', 'p', 'u', 'b' ) ) &&
449             *((uint32_t*)p_es->p_demux_data) == 0xBeef )
450         {
451             memcpy( p_dec->fmt_in.subs.spu.palette,
452                     p_es->p_demux_data, 17 * 4 );
453         }
454         else if( p_es->i_fourcc == VLC_FOURCC( 'd', 'v', 'b', 's' ) )
455         {
456             dvb_spuinfo_t *p_dvbs = (dvb_spuinfo_t*)p_es->p_demux_data;
457
458             p_dec->fmt_in.subs.dvb.i_id = p_dvbs->i_id;
459         }
460     }
461
462     p_dec->fmt_in.i_cat = p_es->i_cat;
463     p_dec->fmt_in.i_codec = p_es->i_fourcc;
464
465     p_dec->fmt_out = null_es_format;
466
467     /* Allocate our private structure for the decoder */
468     p_dec->p_owner = (decoder_owner_sys_t*)malloc(sizeof(decoder_owner_sys_t));
469     if( p_dec->p_owner == NULL )
470     {
471         msg_Err( p_dec, "out of memory" );
472         return NULL;
473     }
474     p_dec->p_owner->b_own_thread = VLC_TRUE;
475     p_dec->p_owner->p_input = p_input;
476     p_dec->p_owner->p_aout = NULL;
477     p_dec->p_owner->p_aout_input = NULL;
478     p_dec->p_owner->p_vout = NULL;
479     p_dec->p_owner->p_sout = p_input->stream.p_sout;
480     p_dec->p_owner->p_sout_input = NULL;
481     p_dec->p_owner->p_packetizer = NULL;
482     p_dec->p_owner->p_es_descriptor = p_es;
483
484
485     /* decoder fifo */
486     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
487     {
488         msg_Err( p_dec, "out of memory" );
489         return NULL;
490     }
491     p_dec->p_owner->p_method_data = p_input->p_method_data;
492
493     /* Set buffers allocation callbacks for the decoders */
494     p_dec->pf_aout_buffer_new = aout_new_buffer;
495     p_dec->pf_aout_buffer_del = aout_del_buffer;
496     p_dec->pf_vout_buffer_new = vout_new_buffer;
497     p_dec->pf_vout_buffer_del = vout_del_buffer;
498     p_dec->pf_picture_link    = vout_link_picture;
499     p_dec->pf_picture_unlink  = vout_unlink_picture;
500
501     vlc_object_attach( p_dec, p_input );
502
503     /* Find a suitable decoder/packetizer module */
504     if( i_object_type == VLC_OBJECT_DECODER )
505         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
506     else
507         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
508
509     /* Check if decoder requires already packetized data */
510     if( i_object_type == VLC_OBJECT_DECODER &&
511         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
512     {
513         p_dec->p_owner->p_packetizer =
514             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
515         if( p_dec->p_owner->p_packetizer )
516         {
517             p_dec->p_owner->p_packetizer->fmt_in = null_es_format;
518             p_dec->p_owner->p_packetizer->fmt_out = null_es_format;
519             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
520                             &p_dec->fmt_in );
521
522             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
523
524             p_dec->p_owner->p_packetizer->p_module =
525                 module_Need( p_dec->p_owner->p_packetizer,
526                              "packetizer", "$packetizer", 0 );
527
528             if( !p_dec->p_owner->p_packetizer->p_module )
529             {
530                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
531                 vlc_object_detach( p_dec->p_owner->p_packetizer );
532                 vlc_object_destroy( p_dec->p_owner->p_packetizer );
533             }
534         }
535     }
536
537     return p_dec;
538 }
539
540 /**
541  * The decoding main loop
542  *
543  * \param p_dec the decoder
544  * \return 0
545  */
546 static int DecoderThread( decoder_t * p_dec )
547 {
548     block_t       *p_block;
549
550     /* The decoder's main loop */
551     while( !p_dec->b_die && !p_dec->b_error )
552     {
553         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
554         {
555             p_dec->b_error = 1;
556             break;
557         }
558         if( p_block->i_buffer <= 0 )
559         {
560             block_Release( p_block );
561             continue;
562         }
563         if( DecoderDecode( p_dec, p_block ) )
564         {
565             break;
566         }
567     }
568
569     while( !p_dec->b_die )
570     {
571         /* Trash all received PES packets */
572         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
573         if( p_block )
574         {
575             block_Release( p_block );
576         }
577     }
578
579     /* XXX We do it here because of dll loader that want close in the
580      * same thread than open/decode */
581     /* Unneed module */
582     module_Unneed( p_dec, p_dec->p_module );
583
584     return 0;
585 }
586
587 /**
588  * Decode a block
589  *
590  * \param p_dec the decoder object
591  * \param p_block the block to decode
592  * \return VLC_SUCCESS or an error code
593  */
594 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
595 {
596     if( p_block->i_buffer <= 0 )
597     {
598         block_Release( p_block );
599         return VLC_SUCCESS;
600     }
601
602     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
603     {
604         block_t *p_sout_block;
605
606         while( (p_sout_block = p_dec->pf_packetize( p_dec, &p_block )) )
607         {
608             if( !p_dec->p_owner->p_sout_input )
609             {
610                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
611                 if( p_dec->p_owner->p_es_descriptor->p_pgrm )
612                 {
613                     p_dec->p_owner->sout.i_group =
614                         p_dec->p_owner->p_es_descriptor->p_pgrm->i_number;
615                 }
616                 p_dec->p_owner->sout.i_id =
617                     p_dec->p_owner->p_es_descriptor->i_id - 1;
618                 if( p_dec->fmt_in.psz_language )
619                 {
620                     p_dec->p_owner->sout.psz_language =
621                         strdup( p_dec->fmt_in.psz_language );
622                 }
623
624                 p_dec->p_owner->p_sout_input =
625                     sout_InputNew( p_dec->p_owner->p_sout,
626                                    &p_dec->p_owner->sout );
627
628                 if( p_dec->p_owner->p_sout_input == NULL )
629                 {
630                     msg_Err( p_dec, "cannot create packetizer output" );
631                     p_dec->b_error = VLC_TRUE;
632
633                     while( p_sout_block )
634                     {
635                         block_t *p_next = p_sout_block->p_next;
636                         block_Release( p_sout_block );
637                         p_sout_block = p_next;
638                     }
639                     break;
640                 }
641             }
642
643             while( p_sout_block )
644             {
645                 block_t       *p_next = p_sout_block->p_next;
646                 sout_buffer_t *p_sout_buffer;
647
648                 p_sout_buffer =
649                     sout_BufferNew( p_dec->p_owner->p_sout_input->p_sout,
650                                     p_sout_block->i_buffer );
651                 if( p_sout_buffer == NULL )
652                 {
653                     msg_Err( p_dec, "cannot get sout buffer" );
654                     break;
655                 }
656
657                 memcpy( p_sout_buffer->p_buffer, p_sout_block->p_buffer,
658                         p_sout_block->i_buffer );
659
660                 p_sout_buffer->i_pts = p_sout_block->i_pts;
661                 p_sout_buffer->i_dts = p_sout_block->i_dts;
662                 p_sout_buffer->i_length = p_sout_block->i_length;
663                 p_sout_buffer->i_flags =
664                     ( p_sout_block->i_flags << SOUT_BUFFER_FLAGS_BLOCK_SHIFT )
665                     & SOUT_BUFFER_FLAGS_BLOCK_MASK;
666
667                 block_Release( p_sout_block );
668
669                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
670                                       p_sout_buffer );
671
672                 p_sout_block = p_next;
673             }
674
675             /* For now it's enough, as only sout inpact on this flag */
676             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
677                 p_dec->p_owner->p_input->b_out_pace_control )
678             {
679                 msg_Dbg( p_dec, "switching to synch mode" );
680                 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
681             }
682             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
683                      !p_dec->p_owner->p_input->b_out_pace_control )
684             {
685                 msg_Dbg( p_dec, "switching to asynch mode" );
686                 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
687             }
688         }
689     }
690     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
691     {
692         aout_buffer_t *p_aout_buf;
693
694         if( p_dec->p_owner->p_packetizer )
695         {
696             block_t *p_packetized_block;
697             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
698
699             while( (p_packetized_block =
700                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
701             {
702                 while( (p_aout_buf =
703                         p_dec->pf_decode_audio( p_dec, &p_packetized_block )) )
704                 {
705                     aout_DecPlay( p_dec->p_owner->p_aout,
706                                   p_dec->p_owner->p_aout_input, p_aout_buf );
707                 }
708             }
709         }
710         else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
711         {
712             aout_DecPlay( p_dec->p_owner->p_aout,
713                           p_dec->p_owner->p_aout_input, p_aout_buf );
714         }
715     }
716     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
717     {
718         picture_t *p_pic;
719
720         if( p_dec->p_owner->p_packetizer )
721         {
722             block_t *p_packetized_block;
723             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
724
725             while( (p_packetized_block =
726                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
727             {
728                 while( (p_pic =
729                         p_dec->pf_decode_video( p_dec, &p_packetized_block )) )
730                 {
731                     vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
732                                       p_pic->date );
733                     vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
734                 }
735             }
736         }
737         else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
738         {
739             vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
740             vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
741         }
742     }
743     else if( p_dec->fmt_in.i_cat == SPU_ES )
744     {
745         p_dec->pf_decode_sub( p_dec, &p_block );
746     }
747     else
748     {
749         msg_Err( p_dec, "unknown ES format" );
750         p_dec->b_error = 1;
751     }
752
753     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
754 }
755
756 /**
757  * Destroys a decoder object
758  *
759  * \param p_dec the decoder object
760  * \return nothing
761  */
762 static void DeleteDecoder( decoder_t * p_dec )
763 {
764     vlc_object_detach( p_dec );
765
766     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
767              (char*)&p_dec->fmt_in.i_codec,
768              p_dec->p_owner->p_fifo->i_depth );
769
770     /* Free all packets still in the decoder fifo. */
771     block_FifoEmpty( p_dec->p_owner->p_fifo );
772     block_FifoRelease( p_dec->p_owner->p_fifo );
773
774    /* Cleanup */
775     if( p_dec->p_owner->p_aout_input )
776         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
777
778     if( p_dec->p_owner->p_vout )
779     {
780         int i_pic;
781
782 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
783         /* Hack to make sure all the the pictures are freed by the decoder */
784         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
785              i_pic++ )
786         {
787             if( p_pic->i_status == RESERVED_PICTURE )
788                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
789             if( p_pic->i_refcount > 0 )
790                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
791         }
792 #undef p_pic
793
794         /* We are about to die. Reattach video output to p_vlc. */
795         vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
796     }
797
798     if( p_dec->p_owner->p_sout_input )
799     {
800         sout_InputDelete( p_dec->p_owner->p_sout_input );
801         es_format_Clean( &p_dec->p_owner->sout );
802     }
803
804     es_format_Clean( &p_dec->fmt_in );
805     es_format_Clean( &p_dec->fmt_out );
806
807     if( p_dec->p_owner->p_packetizer )
808     {
809         module_Unneed( p_dec->p_owner->p_packetizer,
810                        p_dec->p_owner->p_packetizer->p_module );
811         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
812         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
813         vlc_object_detach( p_dec->p_owner->p_packetizer );
814         vlc_object_destroy( p_dec->p_owner->p_packetizer );
815     }
816
817     free( p_dec->p_owner );
818 }
819
820 /*****************************************************************************
821  * Buffers allocation callbacks for the decoders
822  *****************************************************************************/
823 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
824 {
825     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
826     aout_buffer_t *p_buffer;
827
828     if( p_sys->p_aout_input != NULL &&
829         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
830           p_dec->fmt_out.audio.i_original_channels !=
831               p_sys->audio.i_original_channels ||
832           p_dec->fmt_out.audio.i_bytes_per_frame !=
833               p_sys->audio.i_bytes_per_frame ) )
834     {
835         /* Parameters changed, restart the aout */
836         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
837         p_sys->p_aout_input = NULL;
838     }
839
840     if( p_sys->p_aout_input == NULL )
841     {
842         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
843         p_sys->audio = p_dec->fmt_out.audio;
844         p_sys->p_aout_input =
845             aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
846         if( p_sys->p_aout_input == NULL )
847         {
848             msg_Err( p_dec, "failed to create audio output" );
849             p_dec->b_error = VLC_TRUE;
850             return NULL;
851         }
852         p_dec->fmt_out.audio.i_bytes_per_frame =
853             p_sys->audio.i_bytes_per_frame;
854     }
855
856     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
857                                   i_samples );
858
859     return p_buffer;
860 }
861
862 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
863 {
864     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
865                           p_dec->p_owner->p_aout_input, p_buffer );
866 }
867
868 static picture_t *vout_new_buffer( decoder_t *p_dec )
869 {
870     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
871     picture_t *p_pic;
872
873     if( p_sys->p_vout == NULL ||
874         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
875         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
876         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
877         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
878     {
879         if( !p_dec->fmt_out.video.i_width ||
880             !p_dec->fmt_out.video.i_height )
881         {
882             /* Can't create a new vout without display size */
883             return NULL;
884         }
885
886         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
887         p_sys->video = p_dec->fmt_out.video;
888
889         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
890                                       p_sys->video.i_width,
891                                       p_sys->video.i_height,
892                                       p_sys->video.i_chroma,
893                                       p_sys->video.i_aspect );
894
895         if( p_sys->p_vout == NULL )
896         {
897             msg_Err( p_dec, "failed to create video output" );
898             p_dec->b_error = VLC_TRUE;
899             return NULL;
900         }
901     }
902
903     /* Get a new picture */
904     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
905     {
906         int i_pic, i_ready_pic = 0;
907
908         if( p_dec->b_die || p_dec->b_error )
909         {
910             return NULL;
911         }
912
913 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
914         /* Check the decoder doesn't leak pictures */
915         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
916              i_pic++ )
917         {
918             if( p_pic->i_status == READY_PICTURE && i_ready_pic++ > 0 ) break;
919
920             if( p_pic->i_status != DISPLAYED_PICTURE &&
921                 p_pic->i_status != RESERVED_PICTURE &&
922                 p_pic->i_status != READY_PICTURE ) break;
923
924             if( !p_pic->i_refcount ) break;
925         }
926         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
927         {
928             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
929
930             /* Just free all the pictures */
931             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
932                  i_pic++ )
933             {
934                 if( p_pic->i_status == RESERVED_PICTURE )
935                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
936                 if( p_pic->i_refcount > 0 )
937                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
938             }
939         }
940 #undef p_pic
941
942         msleep( VOUT_OUTMEM_SLEEP );
943     }
944
945     return p_pic;
946 }
947
948 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
949 {
950     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
951 }
952
953 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
954 {
955     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
956 }
957
958 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
959 {
960     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
961 }