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 $
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 ( 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 );
62 /*****************************************************************************
64 *****************************************************************************/
65 void _M( vdec_getfunctions )( function_list_t * p_function_list )
67 p_function_list->pf_probe = decoder_Probe;
68 p_function_list->functions.dec.pf_run = decoder_Run;
71 /*****************************************************************************
72 * Build configuration tree.
73 *****************************************************************************/
78 SET_DESCRIPTION( "MPEG I/II video decoder module" )
79 ADD_CAPABILITY( DECODER, 50 )
83 _M( vdec_getfunctions )( &p_module->p_functions->dec );
86 MODULE_DEACTIVATE_START
87 MODULE_DEACTIVATE_STOP
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
95 *****************************************************************************/
96 static int decoder_Probe( probedata_t *p_data )
98 return( ( p_data->i_type == MPEG1_VIDEO_ES
99 || p_data->i_type == MPEG2_VIDEO_ES ) ? 50 : 0 );
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 )
107 vpar_thread_t * p_vpar;
110 intf_DbgMsg( "vpar debug: video parser thread created. Initializing..." );
112 /* Allocate the memory needed to store the thread's structure */
113 if ( (p_vpar = (vpar_thread_t *)malloc( sizeof(vpar_thread_t) )) == NULL )
115 intf_ErrMsg( "vpar error: not enough memory "
116 "for vpar_CreateThread() to create the new thread");
117 DecoderError( p_config->p_decoder_fifo );
122 * Initialize the thread properties
124 p_vpar->p_fifo = p_config->p_decoder_fifo;
125 p_vpar->p_config = p_config;
126 p_vpar->p_vout = NULL;
131 p_vpar->p_fifo->b_error = InitThread( p_vpar );
134 * Main loop - it is not executed if an error occured during
137 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
139 /* Find the next sequence header in the stream */
140 p_vpar->p_fifo->b_error = vpar_NextSequenceHeader( p_vpar );
142 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
146 /* Parse the next sequence, group or picture header */
147 if( vpar_ParseHeader( p_vpar ) )
149 /* End of sequence */
158 if( ( b_error = p_vpar->p_fifo->b_error ) )
160 DecoderError( p_vpar->p_fifo );
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 )
185 * Choose the best motion compensation module
187 p_vpar->p_motion_module = module_Need( MODULE_CAPABILITY_MOTION,
188 main_GetPszVariable( MOTION_METHOD_VAR, NULL ),
191 if( p_vpar->p_motion_module == NULL )
193 intf_ErrMsg( "vpar error: no suitable motion compensation module" );
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 );
203 * Choose the best IDCT module
205 p_vpar->p_idct_module = module_Need( MODULE_CAPABILITY_IDCT,
206 main_GetPszVariable( IDCT_METHOD_VAR, NULL ),
209 if( p_vpar->p_idct_module == NULL )
211 intf_ErrMsg( "vpar error: no suitable IDCT module" );
212 module_Unneed( p_vpar->p_motion_module );
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;
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,
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;
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;
248 p_vpar->picture.p_picture = NULL;
249 p_vpar->picture.i_current_structure = 0;
251 /* Initialize other properties */
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 );
261 * Initialize the synchro properties
263 vpar_SynchroInit( p_vpar );
265 /* Spawn optional video decoder threads */
266 vpar_InitPool( p_vpar );
268 /* Mark thread as running and return */
269 intf_DbgMsg("vpar debug: InitThread(%p) succeeded", p_vpar);
273 /*****************************************************************************
274 * EndThread: thread destruction
275 *****************************************************************************
276 * This function is called when the thread ends after a sucessful
278 *****************************************************************************/
279 static void EndThread( vpar_thread_t *p_vpar )
281 intf_DbgMsg("vpar debug: destroying video parser thread %p", p_vpar);
283 /* Release used video buffers. */
284 if( p_vpar->sequence.p_forward != NULL )
286 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
288 if( p_vpar->sequence.p_backward != NULL )
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 );
294 if( p_vpar->picture.p_picture != NULL )
296 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
299 if( p_main->b_stats )
301 #ifdef HAVE_SYS_TIMES_H
302 struct tms cpu_usage;
306 intf_StatMsg( "vpar stats: %d loops among %d sequence(s)",
307 p_vpar->c_loops, p_vpar->c_sequences );
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 );
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 );
347 /* Dispose of matrices if they have been allocated. */
348 if( p_vpar->sequence.intra_quant.b_allocated )
350 free( p_vpar->sequence.intra_quant.pi_matrix );
352 if( p_vpar->sequence.nonintra_quant.b_allocated )
354 free( p_vpar->sequence.nonintra_quant.pi_matrix) ;
356 if( p_vpar->sequence.chroma_intra_quant.b_allocated )
358 free( p_vpar->sequence.chroma_intra_quant.pi_matrix );
360 if( p_vpar->sequence.chroma_nonintra_quant.b_allocated )
362 free( p_vpar->sequence.chroma_nonintra_quant.pi_matrix );
365 vpar_EndPool( p_vpar );
367 module_Unneed( p_vpar->p_idct_module );
368 module_Unneed( p_vpar->p_motion_module );
372 intf_DbgMsg("vpar debug: EndThread(%p)", p_vpar);
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 )
383 vpar_thread_t * p_vpar = (vpar_thread_t *)p_bit_stream->p_callback_arg;
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;
394 if( p_bit_stream->p_decoder_fifo->p_first->b_discontinuity )
397 intf_DbgMsg( "Discontinuity in BitstreamCallback" );
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;
405 if( p_bit_stream->p_data->b_discard_payload )
408 intf_DbgMsg( "Discard payload in BitstreamCallback" );
410 /* 1 packet messed up, trash the slice. */
411 p_vpar->picture.b_error = 1;