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