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