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 $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Samuel Hocevar <sam@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
30 #include <videolan/vlc.h>
33 #include <unistd.h> /* getpid() */
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
44 #include "video_output.h"
46 #include "stream_control.h"
47 #include "input_ext-dec.h"
49 #include "vdec_ext-plugins.h"
50 #include "vpar_pool.h"
51 #include "video_parser.h"
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 );
62 /*****************************************************************************
64 *****************************************************************************/
65 void _M( vdec_getfunctions )( function_list_t * p_function_list )
67 p_function_list->functions.dec.pf_probe = decoder_Probe;
68 p_function_list->functions.dec.pf_run = decoder_Run;
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"
80 ADD_CATEGORY_HINT( "Misc Options", NULL)
81 ADD_PLUGIN( IDCT_METHOD_VAR, MODULE_CAPABILITY_IDCT, NULL, NULL,
83 ADD_PLUGIN( MOTION_METHOD_VAR, MODULE_CAPABILITY_MOTION, NULL, NULL,
84 "motion compensation method", NULL )
88 SET_DESCRIPTION( "MPEG I/II video decoder module" )
89 ADD_CAPABILITY( DECODER, 50 )
93 _M( vdec_getfunctions )( &p_module->p_functions->dec );
96 MODULE_DEACTIVATE_START
97 MODULE_DEACTIVATE_STOP
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
105 *****************************************************************************/
106 static int decoder_Probe( u8 *pi_type )
108 return( ( *pi_type == MPEG1_VIDEO_ES
109 || *pi_type == MPEG2_VIDEO_ES ) ? 0 : -1 );
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 )
117 vpar_thread_t * p_vpar;
120 /* Allocate the memory needed to store the thread's structure */
121 if ( (p_vpar = (vpar_thread_t *)malloc( sizeof(vpar_thread_t) )) == NULL )
123 intf_ErrMsg( "vpar error: not enough memory "
124 "for vpar_CreateThread() to create the new thread");
125 DecoderError( p_config->p_decoder_fifo );
130 * Initialize the thread properties
132 p_vpar->p_fifo = p_config->p_decoder_fifo;
133 p_vpar->p_config = p_config;
134 p_vpar->p_vout = NULL;
139 p_vpar->p_fifo->b_error = InitThread( p_vpar );
142 * Main loop - it is not executed if an error occured during
145 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
147 /* Find the next sequence header in the stream */
148 p_vpar->p_fifo->b_error = vpar_NextSequenceHeader( p_vpar );
150 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
154 /* Parse the next sequence, group or picture header */
155 if( vpar_ParseHeader( p_vpar ) )
157 /* End of sequence */
166 if( ( b_error = p_vpar->p_fifo->b_error ) )
168 DecoderError( p_vpar->p_fifo );
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 )
195 * Choose the best motion compensation module
197 psz_name = config_GetPszVariable( MOTION_METHOD_VAR );
198 p_vpar->p_motion_module = module_Need( MODULE_CAPABILITY_MOTION, psz_name,
200 if( psz_name ) free( psz_name );
202 if( p_vpar->p_motion_module == NULL )
204 intf_ErrMsg( "vpar error: no suitable motion compensation module" );
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 );
214 * Choose the best IDCT module
216 psz_name = config_GetPszVariable( IDCT_METHOD_VAR );
217 p_vpar->p_idct_module = module_Need( MODULE_CAPABILITY_IDCT, psz_name,
219 if( psz_name ) free( psz_name );
221 if( p_vpar->p_idct_module == NULL )
223 intf_ErrMsg( "vpar error: no suitable IDCT module" );
224 module_Unneed( p_vpar->p_motion_module );
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;
238 /* Initialize input bitstream */
239 InitBitstream( &p_vpar->bit_stream, p_vpar->p_config->p_decoder_fifo,
240 BitstreamCallback, (void *)p_vpar );
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;
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;
259 p_vpar->picture.p_picture = NULL;
260 p_vpar->picture.i_current_structure = 0;
262 /* Initialize other properties */
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 );
272 * Initialize the synchro properties
274 vpar_SynchroInit( p_vpar );
276 /* Spawn optional video decoder threads */
277 vpar_InitPool( p_vpar );
279 /* Mark thread as running and return */
283 /*****************************************************************************
284 * EndThread: thread destruction
285 *****************************************************************************
286 * This function is called when the thread ends after a sucessful
288 *****************************************************************************/
289 static void EndThread( vpar_thread_t *p_vpar )
291 /* Release used video buffers. */
292 if( p_vpar->sequence.p_forward != NULL )
294 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
296 if( p_vpar->sequence.p_backward != NULL )
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 );
302 if( p_vpar->picture.p_picture != NULL )
304 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
307 if( p_main->b_stats )
309 #ifdef HAVE_SYS_TIMES_H
310 struct tms cpu_usage;
314 intf_StatMsg( "vpar stats: %d loops among %d sequence(s)",
315 p_vpar->c_loops, p_vpar->c_sequences );
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 );
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 );
355 /* Dispose of matrices if they have been allocated. */
356 if( p_vpar->sequence.intra_quant.b_allocated )
358 free( p_vpar->sequence.intra_quant.pi_matrix );
360 if( p_vpar->sequence.nonintra_quant.b_allocated )
362 free( p_vpar->sequence.nonintra_quant.pi_matrix) ;
364 if( p_vpar->sequence.chroma_intra_quant.b_allocated )
366 free( p_vpar->sequence.chroma_intra_quant.pi_matrix );
368 if( p_vpar->sequence.chroma_nonintra_quant.b_allocated )
370 free( p_vpar->sequence.chroma_nonintra_quant.pi_matrix );
373 vpar_EndPool( p_vpar );
375 module_Unneed( p_vpar->p_idct_module );
376 module_Unneed( p_vpar->p_motion_module );
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 )
389 vpar_thread_t * p_vpar = (vpar_thread_t *)p_bit_stream->p_callback_arg;
393 p_vpar->sequence.i_current_rate =
394 p_bit_stream->p_decoder_fifo->p_first->i_rate;
396 if( p_bit_stream->p_decoder_fifo->p_first->b_discontinuity )
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;
404 if( p_bit_stream->p_data->b_discard_payload )
406 /* 1 packet messed up, trash the slice. */
407 p_vpar->picture.b_error = 1;