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