]> git.sesse.net Git - vlc/blob - plugins/mpeg_vdec/video_parser.c
Some heavy changes today:
[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.10 2001/12/30 07:09:56 sam 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     p_vpar->p_config->pf_init_bit_stream( &p_vpar->bit_stream,
228         p_vpar->p_config->p_decoder_fifo, BitstreamCallback,
229         (void *)p_vpar );
230
231     /* Initialize parsing data */
232     p_vpar->sequence.p_forward = NULL;
233     p_vpar->sequence.p_backward = NULL;
234     p_vpar->sequence.intra_quant.b_allocated = 0;
235     p_vpar->sequence.nonintra_quant.b_allocated = 0;
236     p_vpar->sequence.chroma_intra_quant.b_allocated = 0;
237     p_vpar->sequence.chroma_nonintra_quant.b_allocated = 0;
238     p_vpar->sequence.i_matrix_coefficients = 1;
239     p_vpar->sequence.next_pts = p_vpar->sequence.next_dts = 0;
240     p_vpar->sequence.b_expect_discontinuity = 0;
241
242     /* Initialize copyright information */
243     p_vpar->sequence.b_copyright_flag = 0;
244     p_vpar->sequence.b_original = 0;
245     p_vpar->sequence.i_copyright_id = 0;
246     p_vpar->sequence.i_copyright_nb = 0;
247
248     p_vpar->picture.p_picture = NULL;
249     p_vpar->picture.i_current_structure = 0;
250
251     /* Initialize other properties */
252     p_vpar->c_loops = 0;
253     p_vpar->c_sequences = 0;
254     memset(p_vpar->pc_pictures, 0, sizeof(p_vpar->pc_pictures));
255     memset(p_vpar->pc_decoded_pictures, 0, sizeof(p_vpar->pc_decoded_pictures));
256     memset(p_vpar->pc_malformed_pictures, 0,
257            sizeof(p_vpar->pc_malformed_pictures));
258     vpar_InitScanTable( p_vpar );
259
260     /*
261      * Initialize the synchro properties
262      */
263     vpar_SynchroInit( p_vpar );
264
265     /* Spawn optional video decoder threads */
266     vpar_InitPool( p_vpar );
267
268     /* Mark thread as running and return */
269     intf_DbgMsg("vpar debug: InitThread(%p) succeeded", p_vpar);
270     return( 0 );
271 }
272
273 /*****************************************************************************
274  * EndThread: thread destruction
275  *****************************************************************************
276  * This function is called when the thread ends after a sucessful
277  * initialization.
278  *****************************************************************************/
279 static void EndThread( vpar_thread_t *p_vpar )
280 {
281     intf_DbgMsg("vpar debug: destroying video parser thread %p", p_vpar);
282
283     /* Release used video buffers. */
284     if( p_vpar->sequence.p_forward != NULL )
285     {
286         vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
287     }
288     if( p_vpar->sequence.p_backward != NULL )
289     {
290         vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
291                           vpar_SynchroDate( p_vpar ) );
292         vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
293     }
294     if( p_vpar->picture.p_picture != NULL )
295     {
296         vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
297     }
298
299     if( p_main->b_stats )
300     {
301 #ifdef HAVE_SYS_TIMES_H
302         struct tms cpu_usage;
303         times( &cpu_usage );
304 #endif
305
306         intf_StatMsg( "vpar stats: %d loops among %d sequence(s)",
307                       p_vpar->c_loops, p_vpar->c_sequences );
308
309 #ifdef HAVE_SYS_TIMES_H
310         intf_StatMsg( "vpar stats: cpu usage (user: %d, system: %d)",
311                       cpu_usage.tms_utime, cpu_usage.tms_stime );
312 #endif
313
314         intf_StatMsg( "vpar stats: Read %d frames/fields (I %d/P %d/B %d)",
315                       p_vpar->pc_pictures[I_CODING_TYPE]
316                       + p_vpar->pc_pictures[P_CODING_TYPE]
317                       + p_vpar->pc_pictures[B_CODING_TYPE],
318                       p_vpar->pc_pictures[I_CODING_TYPE],
319                       p_vpar->pc_pictures[P_CODING_TYPE],
320                       p_vpar->pc_pictures[B_CODING_TYPE] );
321         intf_StatMsg( "vpar stats: Decoded %d frames/fields (I %d/P %d/B %d)",
322                       p_vpar->pc_decoded_pictures[I_CODING_TYPE]
323                       + p_vpar->pc_decoded_pictures[P_CODING_TYPE]
324                       + p_vpar->pc_decoded_pictures[B_CODING_TYPE],
325                       p_vpar->pc_decoded_pictures[I_CODING_TYPE],
326                       p_vpar->pc_decoded_pictures[P_CODING_TYPE],
327                       p_vpar->pc_decoded_pictures[B_CODING_TYPE] );
328         intf_StatMsg( "vpar stats: Read %d malformed frames/fields (I %d/P %d/B %d)",
329                       p_vpar->pc_malformed_pictures[I_CODING_TYPE]
330                       + p_vpar->pc_malformed_pictures[P_CODING_TYPE]
331                       + p_vpar->pc_malformed_pictures[B_CODING_TYPE],
332                       p_vpar->pc_malformed_pictures[I_CODING_TYPE],
333                       p_vpar->pc_malformed_pictures[P_CODING_TYPE],
334                       p_vpar->pc_malformed_pictures[B_CODING_TYPE] );
335 #define S   p_vpar->sequence
336         intf_StatMsg( "vpar info: %s stream (%dx%d), %d.%d pi/s",
337                       S.b_mpeg2 ? "MPEG-2" : "MPEG-1",
338                       S.i_width, S.i_height, S.i_frame_rate/1001,
339                       S.i_frame_rate % 1001 );
340         intf_StatMsg( "vpar info: %s, %s, matrix_coeff: %d",
341                       S.b_progressive ? "Progressive" : "Non-progressive",
342                       S.i_scalable_mode ? "scalable" : "non-scalable",
343                       S.i_matrix_coefficients );
344 #undef S
345     }
346
347     /* Dispose of matrices if they have been allocated. */
348     if( p_vpar->sequence.intra_quant.b_allocated )
349     {
350         free( p_vpar->sequence.intra_quant.pi_matrix );
351     }
352     if( p_vpar->sequence.nonintra_quant.b_allocated )
353     {
354         free( p_vpar->sequence.nonintra_quant.pi_matrix) ;
355     }
356     if( p_vpar->sequence.chroma_intra_quant.b_allocated )
357     {
358         free( p_vpar->sequence.chroma_intra_quant.pi_matrix );
359     }
360     if( p_vpar->sequence.chroma_nonintra_quant.b_allocated )
361     {
362         free( p_vpar->sequence.chroma_nonintra_quant.pi_matrix );
363     }
364
365     vpar_EndPool( p_vpar );
366
367     module_Unneed( p_vpar->p_idct_module );
368     module_Unneed( p_vpar->p_motion_module );
369
370     free( p_vpar );
371
372     intf_DbgMsg("vpar debug: EndThread(%p)", p_vpar);
373 }
374
375 /*****************************************************************************
376  * BitstreamCallback: Import parameters from the new data/PES packet
377  *****************************************************************************
378  * This function is called by input's NextDataPacket.
379  *****************************************************************************/
380 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
381                                 boolean_t b_new_pes )
382 {
383     vpar_thread_t * p_vpar = (vpar_thread_t *)p_bit_stream->p_callback_arg;
384
385     if( b_new_pes )
386     {
387         p_vpar->sequence.next_pts =
388             p_bit_stream->p_decoder_fifo->p_first->i_pts;
389         p_vpar->sequence.next_dts =
390             p_bit_stream->p_decoder_fifo->p_first->i_dts;
391         p_vpar->sequence.i_current_rate =
392             p_bit_stream->p_decoder_fifo->p_first->i_rate;
393
394         if( p_bit_stream->p_decoder_fifo->p_first->b_discontinuity )
395         {
396 #ifdef TRACE_VPAR
397             intf_DbgMsg( "Discontinuity in BitstreamCallback" );
398 #endif
399             /* Escape the current picture and reset the picture predictors. */
400             p_vpar->sequence.b_expect_discontinuity = 1;
401             p_vpar->picture.b_error = 1;
402         }
403     }
404
405     if( p_bit_stream->p_data->b_discard_payload )
406     {
407 #ifdef TRACE_VPAR
408         intf_DbgMsg( "Discard payload in BitstreamCallback" );
409 #endif
410         /* 1 packet messed up, trash the slice. */
411         p_vpar->picture.b_error = 1;
412     }
413 }