]> git.sesse.net Git - vlc/blob - plugins/mpeg_vdec/video_parser.c
* Decoders do not necessarily use bit stream (see mad plug-in)
[vlc] / plugins / mpeg_vdec / video_parser.c
1 /*****************************************************************************
2  * video_parser.c : video parser thread
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: video_parser.c,v 1.12 2002/01/21 23:57:46 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Samuel Hocevar <sam@via.ecp.fr>
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>                                      /* malloc(), free() */
29
30 #include <videolan/vlc.h>
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>                                              /* getpid() */
34 #endif
35
36 #include <errno.h>
37 #include <string.h>
38
39 #ifdef HAVE_SYS_TIMES_H
40 #   include <sys/times.h>
41 #endif
42
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "stream_control.h"
47 #include "input_ext-dec.h"
48
49 #include "vdec_ext-plugins.h"
50 #include "vpar_pool.h"
51 #include "video_parser.h"
52
53 /*
54  * Local prototypes
55  */
56 static int      decoder_Probe     ( probedata_t * );
57 static int      decoder_Run       ( decoder_config_t * );
58 static int      InitThread        ( vpar_thread_t * );
59 static void     EndThread         ( vpar_thread_t * );
60 static void     BitstreamCallback ( bit_stream_t *, boolean_t );
61
62 /*****************************************************************************
63  * Capabilities
64  *****************************************************************************/
65 void _M( vdec_getfunctions )( function_list_t * p_function_list )
66 {
67     p_function_list->pf_probe = decoder_Probe;
68     p_function_list->functions.dec.pf_run = decoder_Run;
69 }
70
71 /*****************************************************************************
72  * Build configuration tree.
73  *****************************************************************************/
74 MODULE_CONFIG_START
75 MODULE_CONFIG_STOP
76
77 MODULE_INIT_START
78     SET_DESCRIPTION( "MPEG I/II video decoder module" )
79     ADD_CAPABILITY( DECODER, 50 )
80 MODULE_INIT_STOP
81
82 MODULE_ACTIVATE_START
83     _M( vdec_getfunctions )( &p_module->p_functions->dec );
84 MODULE_ACTIVATE_STOP
85
86 MODULE_DEACTIVATE_START
87 MODULE_DEACTIVATE_STOP
88
89
90 /*****************************************************************************
91  * decoder_Probe: probe the decoder and return score
92  *****************************************************************************
93  * Tries to launch a decoder and return score so that the interface is able 
94  * to chose.
95  *****************************************************************************/
96 static int decoder_Probe( probedata_t *p_data )
97 {
98     return( ( p_data->i_type == MPEG1_VIDEO_ES
99                || p_data->i_type == MPEG2_VIDEO_ES ) ? 50 : 0 );
100 }
101
102 /*****************************************************************************
103  * decoder_Run: this function is called just after the thread is created
104  *****************************************************************************/
105 static int decoder_Run ( decoder_config_t * p_config )
106 {
107     vpar_thread_t *     p_vpar;
108     boolean_t           b_error;
109
110     intf_DbgMsg( "vpar debug: video parser thread created. Initializing..." );
111
112     /* Allocate the memory needed to store the thread's structure */
113     if ( (p_vpar = (vpar_thread_t *)malloc( sizeof(vpar_thread_t) )) == NULL )
114     {
115         intf_ErrMsg( "vpar error: not enough memory "
116                      "for vpar_CreateThread() to create the new thread");
117         DecoderError( p_config->p_decoder_fifo );
118         return( -1 );
119     }
120
121     /*
122      * Initialize the thread properties
123      */
124     p_vpar->p_fifo = p_config->p_decoder_fifo;
125     p_vpar->p_config = p_config;
126     p_vpar->p_vout = NULL;
127
128     /*
129      * Initialize thread
130      */
131     p_vpar->p_fifo->b_error = InitThread( p_vpar );
132      
133     /*
134      * Main loop - it is not executed if an error occured during
135      * initialization
136      */
137     while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
138     {
139         /* Find the next sequence header in the stream */
140         p_vpar->p_fifo->b_error = vpar_NextSequenceHeader( p_vpar );
141
142         while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
143         {
144             p_vpar->c_loops++;
145
146             /* Parse the next sequence, group or picture header */
147             if( vpar_ParseHeader( p_vpar ) )
148             {
149                 /* End of sequence */
150                 break;
151             }
152         }
153     }
154
155     /*
156      * Error loop
157      */
158     if( ( b_error = p_vpar->p_fifo->b_error ) )
159     {
160         DecoderError( p_vpar->p_fifo );
161     }
162
163     /* End of thread */
164     EndThread( p_vpar );
165
166     if( b_error )
167     {
168         return( -1 );
169     }
170    
171     return( 0 );
172     
173
174
175 /*****************************************************************************
176  * InitThread: initialize vpar output thread
177  *****************************************************************************
178  * This function is called from decoder_Run and performs the second step 
179  * of the initialization. It returns 0 on success. Note that the thread's 
180  * flag are not modified inside this function.
181  *****************************************************************************/
182 static int InitThread( vpar_thread_t *p_vpar )
183 {
184     /*
185      * Choose the best motion compensation module
186      */
187     p_vpar->p_motion_module = module_Need( MODULE_CAPABILITY_MOTION,
188                                 main_GetPszVariable( MOTION_METHOD_VAR, NULL ),
189                                 NULL );
190
191     if( p_vpar->p_motion_module == NULL )
192     {
193         intf_ErrMsg( "vpar error: no suitable motion compensation module" );
194         free( p_vpar );
195         return( -1 );
196     }
197
198 #define f ( p_vpar->p_motion_module->p_functions->motion.functions.motion )
199     memcpy( p_vpar->pool.ppppf_motion, f.ppppf_motion, sizeof(void *) * 16 );
200 #undef f
201
202     /*
203      * Choose the best IDCT module
204      */
205     p_vpar->p_idct_module = module_Need( MODULE_CAPABILITY_IDCT,
206                                   main_GetPszVariable( IDCT_METHOD_VAR, NULL ),
207                                   NULL );
208
209     if( p_vpar->p_idct_module == NULL )
210     {
211         intf_ErrMsg( "vpar error: no suitable IDCT module" );
212         module_Unneed( p_vpar->p_motion_module );
213         free( p_vpar );
214         return( -1 );
215     }
216
217 #define f p_vpar->p_idct_module->p_functions->idct.functions.idct
218     p_vpar->pool.pf_idct_init   = f.pf_idct_init;
219     p_vpar->pf_sparse_idct_add  = f.pf_sparse_idct_add;
220     p_vpar->pf_idct_add         = f.pf_idct_add;
221     p_vpar->pf_sparse_idct_copy = f.pf_sparse_idct_copy;
222     p_vpar->pf_idct_copy        = f.pf_idct_copy;
223     p_vpar->pf_norm_scan        = f.pf_norm_scan;
224 #undef f
225
226     /* Initialize input bitstream */
227     InitBitstream( &p_vpar->bit_stream, p_vpar->p_config->p_decoder_fifo,
228                    BitstreamCallback, (void *)p_vpar );
229
230     /* Initialize parsing data */
231     p_vpar->sequence.p_forward = NULL;
232     p_vpar->sequence.p_backward = NULL;
233     p_vpar->sequence.intra_quant.b_allocated = 0;
234     p_vpar->sequence.nonintra_quant.b_allocated = 0;
235     p_vpar->sequence.chroma_intra_quant.b_allocated = 0;
236     p_vpar->sequence.chroma_nonintra_quant.b_allocated = 0;
237     p_vpar->sequence.i_matrix_coefficients = 1;
238     p_vpar->sequence.next_pts = p_vpar->sequence.next_dts = 0;
239     p_vpar->sequence.b_expect_discontinuity = 0;
240
241     /* Initialize copyright information */
242     p_vpar->sequence.b_copyright_flag = 0;
243     p_vpar->sequence.b_original = 0;
244     p_vpar->sequence.i_copyright_id = 0;
245     p_vpar->sequence.i_copyright_nb = 0;
246
247     p_vpar->picture.p_picture = NULL;
248     p_vpar->picture.i_current_structure = 0;
249
250     /* Initialize other properties */
251     p_vpar->c_loops = 0;
252     p_vpar->c_sequences = 0;
253     memset(p_vpar->pc_pictures, 0, sizeof(p_vpar->pc_pictures));
254     memset(p_vpar->pc_decoded_pictures, 0, sizeof(p_vpar->pc_decoded_pictures));
255     memset(p_vpar->pc_malformed_pictures, 0,
256            sizeof(p_vpar->pc_malformed_pictures));
257     vpar_InitScanTable( p_vpar );
258
259     /*
260      * Initialize the synchro properties
261      */
262     vpar_SynchroInit( p_vpar );
263
264     /* Spawn optional video decoder threads */
265     vpar_InitPool( p_vpar );
266
267     /* Mark thread as running and return */
268     intf_DbgMsg("vpar debug: InitThread(%p) succeeded", p_vpar);
269     return( 0 );
270 }
271
272 /*****************************************************************************
273  * EndThread: thread destruction
274  *****************************************************************************
275  * This function is called when the thread ends after a sucessful
276  * initialization.
277  *****************************************************************************/
278 static void EndThread( vpar_thread_t *p_vpar )
279 {
280     intf_DbgMsg("vpar debug: destroying video parser thread %p", p_vpar);
281
282     /* Release used video buffers. */
283     if( p_vpar->sequence.p_forward != NULL )
284     {
285         vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
286     }
287     if( p_vpar->sequence.p_backward != NULL )
288     {
289         vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
290                           vpar_SynchroDate( p_vpar ) );
291         vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
292     }
293     if( p_vpar->picture.p_picture != NULL )
294     {
295         vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
296     }
297
298     if( p_main->b_stats )
299     {
300 #ifdef HAVE_SYS_TIMES_H
301         struct tms cpu_usage;
302         times( &cpu_usage );
303 #endif
304
305         intf_StatMsg( "vpar stats: %d loops among %d sequence(s)",
306                       p_vpar->c_loops, p_vpar->c_sequences );
307
308 #ifdef HAVE_SYS_TIMES_H
309         intf_StatMsg( "vpar stats: cpu usage (user: %d, system: %d)",
310                       cpu_usage.tms_utime, cpu_usage.tms_stime );
311 #endif
312
313         intf_StatMsg( "vpar stats: Read %d frames/fields (I %d/P %d/B %d)",
314                       p_vpar->pc_pictures[I_CODING_TYPE]
315                       + p_vpar->pc_pictures[P_CODING_TYPE]
316                       + p_vpar->pc_pictures[B_CODING_TYPE],
317                       p_vpar->pc_pictures[I_CODING_TYPE],
318                       p_vpar->pc_pictures[P_CODING_TYPE],
319                       p_vpar->pc_pictures[B_CODING_TYPE] );
320         intf_StatMsg( "vpar stats: Decoded %d frames/fields (I %d/P %d/B %d)",
321                       p_vpar->pc_decoded_pictures[I_CODING_TYPE]
322                       + p_vpar->pc_decoded_pictures[P_CODING_TYPE]
323                       + p_vpar->pc_decoded_pictures[B_CODING_TYPE],
324                       p_vpar->pc_decoded_pictures[I_CODING_TYPE],
325                       p_vpar->pc_decoded_pictures[P_CODING_TYPE],
326                       p_vpar->pc_decoded_pictures[B_CODING_TYPE] );
327         intf_StatMsg( "vpar stats: Read %d malformed frames/fields (I %d/P %d/B %d)",
328                       p_vpar->pc_malformed_pictures[I_CODING_TYPE]
329                       + p_vpar->pc_malformed_pictures[P_CODING_TYPE]
330                       + p_vpar->pc_malformed_pictures[B_CODING_TYPE],
331                       p_vpar->pc_malformed_pictures[I_CODING_TYPE],
332                       p_vpar->pc_malformed_pictures[P_CODING_TYPE],
333                       p_vpar->pc_malformed_pictures[B_CODING_TYPE] );
334 #define S   p_vpar->sequence
335         intf_StatMsg( "vpar info: %s stream (%dx%d), %d.%d pi/s",
336                       S.b_mpeg2 ? "MPEG-2" : "MPEG-1",
337                       S.i_width, S.i_height, S.i_frame_rate/1001,
338                       S.i_frame_rate % 1001 );
339         intf_StatMsg( "vpar info: %s, %s, matrix_coeff: %d",
340                       S.b_progressive ? "Progressive" : "Non-progressive",
341                       S.i_scalable_mode ? "scalable" : "non-scalable",
342                       S.i_matrix_coefficients );
343 #undef S
344     }
345
346     /* Dispose of matrices if they have been allocated. */
347     if( p_vpar->sequence.intra_quant.b_allocated )
348     {
349         free( p_vpar->sequence.intra_quant.pi_matrix );
350     }
351     if( p_vpar->sequence.nonintra_quant.b_allocated )
352     {
353         free( p_vpar->sequence.nonintra_quant.pi_matrix) ;
354     }
355     if( p_vpar->sequence.chroma_intra_quant.b_allocated )
356     {
357         free( p_vpar->sequence.chroma_intra_quant.pi_matrix );
358     }
359     if( p_vpar->sequence.chroma_nonintra_quant.b_allocated )
360     {
361         free( p_vpar->sequence.chroma_nonintra_quant.pi_matrix );
362     }
363
364     vpar_EndPool( p_vpar );
365
366     module_Unneed( p_vpar->p_idct_module );
367     module_Unneed( p_vpar->p_motion_module );
368
369     free( p_vpar );
370
371     intf_DbgMsg("vpar debug: EndThread(%p)", p_vpar);
372 }
373
374 /*****************************************************************************
375  * BitstreamCallback: Import parameters from the new data/PES packet
376  *****************************************************************************
377  * This function is called by input's NextDataPacket.
378  *****************************************************************************/
379 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
380                                 boolean_t b_new_pes )
381 {
382     vpar_thread_t * p_vpar = (vpar_thread_t *)p_bit_stream->p_callback_arg;
383
384     if( b_new_pes )
385     {
386         p_vpar->sequence.i_current_rate =
387             p_bit_stream->p_decoder_fifo->p_first->i_rate;
388
389         if( p_bit_stream->p_decoder_fifo->p_first->b_discontinuity )
390         {
391 #ifdef TRACE_VPAR
392             intf_DbgMsg( "Discontinuity in BitstreamCallback" );
393 #endif
394             /* Escape the current picture and reset the picture predictors. */
395             p_vpar->sequence.b_expect_discontinuity = 1;
396             p_vpar->picture.b_error = 1;
397         }
398     }
399
400     if( p_bit_stream->p_data->b_discard_payload )
401     {
402 #ifdef TRACE_VPAR
403         intf_DbgMsg( "Discard payload in BitstreamCallback" );
404 #endif
405         /* 1 packet messed up, trash the slice. */
406         p_vpar->picture.b_error = 1;
407     }
408 }