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