1 /*****************************************************************************
2 * video_parser.c : video parser thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video_parser.c,v 1.20 2002/05/19 12:57:32 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 #define VDEC_IDCT_TEXT N_("IDCT module")
75 #define VDEC_IDCT_LONGTEXT N_( \
76 "This option allows you to select the IDCT module used by this video " \
78 "Note that the default behavior is to automatically select the best " \
81 #define VDEC_MOTION_TEXT N_("motion compensation module")
82 #define VDEC_MOTION_LONGTEXT N_( \
83 "This option allows you to select the motion compensation module used by "\
84 "this video decoder.\n" \
85 "Note that the default behavior is to automatically select the best " \
88 #define VDEC_SMP_TEXT N_("use additional processors")
89 #define VDEC_SMP_LONGTEXT N_( \
90 "This video decoder can benefit from a multiprocessor computer. If you " \
91 "have one, you can specify the number of processors here.")
93 #define VPAR_SYNCHRO_TEXT N_("force synchro algorithm {I|I+|IP|IP+|IPB}")
94 #define VPAR_SYNCHRO_LONGTEXT N_( \
95 "This allows you to force the synchro algorithm, by directly selecting " \
96 "the types of picture you want to decode. Please bear in mind that if " \
97 "you select more pictures than what your CPU is capable to decode, " \
98 "you won't get anything.")
101 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL)
102 ADD_MODULE ( "mpeg-idct", MODULE_CAPABILITY_IDCT, NULL, NULL,
103 VDEC_IDCT_TEXT, VDEC_IDCT_LONGTEXT )
104 ADD_MODULE ( "mpeg-motion", MODULE_CAPABILITY_MOTION, NULL, NULL,
105 VDEC_MOTION_TEXT, VDEC_IDCT_LONGTEXT )
106 ADD_INTEGER ( "vdec-smp", 0, NULL, VDEC_SMP_TEXT, VDEC_SMP_LONGTEXT )
107 ADD_STRING ( "vpar-synchro", NULL, NULL, VPAR_SYNCHRO_TEXT,
108 VPAR_SYNCHRO_LONGTEXT )
112 SET_DESCRIPTION( _("MPEG I/II video decoder module") )
113 ADD_CAPABILITY( DECODER, 50 )
116 MODULE_ACTIVATE_START
117 _M( vdec_getfunctions )( &p_module->p_functions->dec );
120 MODULE_DEACTIVATE_START
121 MODULE_DEACTIVATE_STOP
124 /*****************************************************************************
125 * decoder_Probe: probe the decoder and return score
126 *****************************************************************************
127 * Tries to launch a decoder and return score so that the interface is able
129 *****************************************************************************/
130 static int decoder_Probe( u8 *pi_type )
132 return( ( *pi_type == MPEG1_VIDEO_ES
133 || *pi_type == MPEG2_VIDEO_ES ) ? 0 : -1 );
136 /*****************************************************************************
137 * decoder_Run: this function is called just after the thread is created
138 *****************************************************************************/
139 static int decoder_Run ( decoder_config_t * p_config )
141 vpar_thread_t * p_vpar;
144 /* Allocate the memory needed to store the thread's structure */
145 if ( (p_vpar = (vpar_thread_t *)malloc( sizeof(vpar_thread_t) )) == NULL )
147 intf_ErrMsg( "vpar error: not enough memory "
148 "for vpar_CreateThread() to create the new thread");
149 DecoderError( p_config->p_decoder_fifo );
154 * Initialize the thread properties
156 p_vpar->p_fifo = p_config->p_decoder_fifo;
157 p_vpar->p_config = p_config;
158 p_vpar->p_vout = NULL;
163 p_vpar->p_fifo->b_error = InitThread( p_vpar );
166 * Main loop - it is not executed if an error occured during
169 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
171 /* Find the next sequence header in the stream */
172 p_vpar->p_fifo->b_error = vpar_NextSequenceHeader( p_vpar );
174 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
178 /* Parse the next sequence, group or picture header */
179 if( vpar_ParseHeader( p_vpar ) )
181 /* End of sequence */
190 if( ( b_error = p_vpar->p_fifo->b_error ) )
192 DecoderError( p_vpar->p_fifo );
207 /*****************************************************************************
208 * InitThread: initialize vpar output thread
209 *****************************************************************************
210 * This function is called from decoder_Run and performs the second step
211 * of the initialization. It returns 0 on success. Note that the thread's
212 * flag are not modified inside this function.
213 *****************************************************************************/
214 static int InitThread( vpar_thread_t *p_vpar )
219 * Choose the best motion compensation module
221 psz_name = config_GetPszVariable( "mpeg-motion" );
222 p_vpar->p_motion_module = module_Need( MODULE_CAPABILITY_MOTION, psz_name,
224 if( psz_name ) free( psz_name );
226 if( p_vpar->p_motion_module == NULL )
228 intf_ErrMsg( "vpar error: no suitable motion compensation module" );
233 #define f ( p_vpar->p_motion_module->p_functions->motion.functions.motion )
234 memcpy( p_vpar->pool.ppppf_motion, f.ppppf_motion, sizeof(void *) * 16 );
238 * Choose the best IDCT module
240 psz_name = config_GetPszVariable( "mpeg-idct" );
241 p_vpar->p_idct_module = module_Need( MODULE_CAPABILITY_IDCT, psz_name,
243 if( psz_name ) free( psz_name );
245 if( p_vpar->p_idct_module == NULL )
247 intf_ErrMsg( "vpar error: no suitable IDCT module" );
248 module_Unneed( p_vpar->p_motion_module );
253 #define f p_vpar->p_idct_module->p_functions->idct.functions.idct
254 p_vpar->pool.pf_idct_init = f.pf_idct_init;
255 p_vpar->pf_sparse_idct_add = f.pf_sparse_idct_add;
256 p_vpar->pf_idct_add = f.pf_idct_add;
257 p_vpar->pf_sparse_idct_copy = f.pf_sparse_idct_copy;
258 p_vpar->pf_idct_copy = f.pf_idct_copy;
259 p_vpar->pf_norm_scan = f.pf_norm_scan;
262 /* Initialize input bitstream */
263 InitBitstream( &p_vpar->bit_stream, p_vpar->p_config->p_decoder_fifo,
264 BitstreamCallback, (void *)p_vpar );
266 /* Initialize parsing data */
267 p_vpar->sequence.p_forward = NULL;
268 p_vpar->sequence.p_backward = NULL;
269 p_vpar->sequence.intra_quant.b_allocated = 0;
270 p_vpar->sequence.nonintra_quant.b_allocated = 0;
271 p_vpar->sequence.chroma_intra_quant.b_allocated = 0;
272 p_vpar->sequence.chroma_nonintra_quant.b_allocated = 0;
273 p_vpar->sequence.i_matrix_coefficients = 1;
274 p_vpar->sequence.next_pts = p_vpar->sequence.next_dts = 0;
275 p_vpar->sequence.b_expect_discontinuity = 0;
277 /* Initialize copyright information */
278 p_vpar->sequence.b_copyright_flag = 0;
279 p_vpar->sequence.b_original = 0;
280 p_vpar->sequence.i_copyright_id = 0;
281 p_vpar->sequence.i_copyright_nb = 0;
283 p_vpar->picture.p_picture = NULL;
284 p_vpar->picture.i_current_structure = 0;
286 /* Initialize other properties */
288 p_vpar->c_sequences = 0;
289 memset(p_vpar->pc_pictures, 0, sizeof(p_vpar->pc_pictures));
290 memset(p_vpar->pc_decoded_pictures, 0, sizeof(p_vpar->pc_decoded_pictures));
291 memset(p_vpar->pc_malformed_pictures, 0,
292 sizeof(p_vpar->pc_malformed_pictures));
293 vpar_InitScanTable( p_vpar );
296 * Initialize the synchro properties
298 vpar_SynchroInit( p_vpar );
300 /* Spawn optional video decoder threads */
301 vpar_InitPool( p_vpar );
303 /* Mark thread as running and return */
307 /*****************************************************************************
308 * EndThread: thread destruction
309 *****************************************************************************
310 * This function is called when the thread ends after a sucessful
312 *****************************************************************************/
313 static void EndThread( vpar_thread_t *p_vpar )
315 /* Release used video buffers. */
316 if( p_vpar->sequence.p_forward != NULL )
318 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
320 if( p_vpar->sequence.p_backward != NULL )
322 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
323 vpar_SynchroDate( p_vpar ) );
324 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
326 if( p_vpar->picture.p_picture != NULL )
328 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
331 if( p_main->b_stats )
333 #ifdef HAVE_SYS_TIMES_H
334 struct tms cpu_usage;
338 intf_StatMsg( "vpar stats: %d loops among %d sequence(s)",
339 p_vpar->c_loops, p_vpar->c_sequences );
341 #ifdef HAVE_SYS_TIMES_H
342 intf_StatMsg( "vpar stats: cpu usage (user: %d, system: %d)",
343 cpu_usage.tms_utime, cpu_usage.tms_stime );
346 intf_StatMsg( "vpar stats: Read %d frames/fields (I %d/P %d/B %d)",
347 p_vpar->pc_pictures[I_CODING_TYPE]
348 + p_vpar->pc_pictures[P_CODING_TYPE]
349 + p_vpar->pc_pictures[B_CODING_TYPE],
350 p_vpar->pc_pictures[I_CODING_TYPE],
351 p_vpar->pc_pictures[P_CODING_TYPE],
352 p_vpar->pc_pictures[B_CODING_TYPE] );
353 intf_StatMsg( "vpar stats: Decoded %d frames/fields (I %d/P %d/B %d)",
354 p_vpar->pc_decoded_pictures[I_CODING_TYPE]
355 + p_vpar->pc_decoded_pictures[P_CODING_TYPE]
356 + p_vpar->pc_decoded_pictures[B_CODING_TYPE],
357 p_vpar->pc_decoded_pictures[I_CODING_TYPE],
358 p_vpar->pc_decoded_pictures[P_CODING_TYPE],
359 p_vpar->pc_decoded_pictures[B_CODING_TYPE] );
360 intf_StatMsg( "vpar stats: Read %d malformed frames/fields (I %d/P %d/B %d)",
361 p_vpar->pc_malformed_pictures[I_CODING_TYPE]
362 + p_vpar->pc_malformed_pictures[P_CODING_TYPE]
363 + p_vpar->pc_malformed_pictures[B_CODING_TYPE],
364 p_vpar->pc_malformed_pictures[I_CODING_TYPE],
365 p_vpar->pc_malformed_pictures[P_CODING_TYPE],
366 p_vpar->pc_malformed_pictures[B_CODING_TYPE] );
367 #define S p_vpar->sequence
368 intf_StatMsg( "vpar info: %s stream (%dx%d), %d.%d pi/s",
369 S.b_mpeg2 ? "MPEG-2" : "MPEG-1",
370 S.i_width, S.i_height, S.i_frame_rate/1001,
371 S.i_frame_rate % 1001 );
372 intf_StatMsg( "vpar info: %s, %s, matrix_coeff: %d",
373 S.b_progressive ? "Progressive" : "Non-progressive",
374 S.i_scalable_mode ? "scalable" : "non-scalable",
375 S.i_matrix_coefficients );
379 /* Dispose of matrices if they have been allocated. */
380 if( p_vpar->sequence.intra_quant.b_allocated )
382 free( p_vpar->sequence.intra_quant.pi_matrix );
384 if( p_vpar->sequence.nonintra_quant.b_allocated )
386 free( p_vpar->sequence.nonintra_quant.pi_matrix) ;
388 if( p_vpar->sequence.chroma_intra_quant.b_allocated )
390 free( p_vpar->sequence.chroma_intra_quant.pi_matrix );
392 if( p_vpar->sequence.chroma_nonintra_quant.b_allocated )
394 free( p_vpar->sequence.chroma_nonintra_quant.pi_matrix );
397 vpar_EndPool( p_vpar );
399 module_Unneed( p_vpar->p_idct_module );
400 module_Unneed( p_vpar->p_motion_module );
405 /*****************************************************************************
406 * BitstreamCallback: Import parameters from the new data/PES packet
407 *****************************************************************************
408 * This function is called by input's NextDataPacket.
409 *****************************************************************************/
410 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
411 boolean_t b_new_pes )
413 vpar_thread_t * p_vpar = (vpar_thread_t *)p_bit_stream->p_callback_arg;
417 p_vpar->sequence.i_current_rate =
418 p_bit_stream->p_decoder_fifo->p_first->i_rate;
420 if( p_bit_stream->p_decoder_fifo->p_first->b_discontinuity )
422 /* Escape the current picture and reset the picture predictors. */
423 p_vpar->sequence.b_expect_discontinuity = 1;
424 p_vpar->picture.b_error = 1;
428 if( p_bit_stream->p_data->b_discard_payload )
430 /* 1 packet messed up, trash the slice. */
431 p_vpar->picture.b_error = 1;