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