1 /*****************************************************************************
2 * video_parser.c : video parser thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: video_parser.c,v 1.7 2001/12/10 04:53:11 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 #define MODULE_NAME mpeg_vdec
26 #include "modules_inner.h"
28 /*****************************************************************************
30 *****************************************************************************/
33 #include <stdlib.h> /* malloc(), free() */
36 #include <unistd.h> /* getpid() */
42 #ifdef HAVE_SYS_TIMES_H
43 # include <sys/times.h>
52 #include "video_output.h"
54 #include "stream_control.h"
55 #include "input_ext-dec.h"
57 #include "vdec_ext-plugins.h"
58 #include "vpar_pool.h"
59 #include "video_parser.h"
62 #include "modules_export.h"
67 static int mpeg_vdec_Probe ( probedata_t * );
68 static int mpeg_vdec_Run ( decoder_config_t * );
69 static int mpeg_vdec_Init ( vpar_thread_t * );
70 static void mpeg_vdec_ErrorThread ( vpar_thread_t * );
71 static void mpeg_vdec_EndThread ( vpar_thread_t * );
72 static void BitstreamCallback ( bit_stream_t *, boolean_t );
74 /*****************************************************************************
76 *****************************************************************************/
77 void _M( vdec_getfunctions )( function_list_t * p_function_list )
79 p_function_list->pf_probe = mpeg_vdec_Probe;
80 p_function_list->functions.dec.pf_run = mpeg_vdec_Run;
83 /*****************************************************************************
84 * Build configuration tree.
85 *****************************************************************************/
87 ADD_WINDOW( "Configuration for MPEG video decoder module" )
88 ADD_COMMENT( "Nothing to configure" )
92 p_module->i_capabilities = MODULE_CAPABILITY_DEC;
93 p_module->psz_longname = "MPEG I/II video decoder module";
97 _M( vdec_getfunctions )( &p_module->p_functions->dec );
100 MODULE_DEACTIVATE_START
101 MODULE_DEACTIVATE_STOP
104 /*****************************************************************************
105 * mpeg_vdec_Probe: probe the decoder and return score
106 *****************************************************************************
107 * Tries to launch a decoder and return score so that the interface is able
109 *****************************************************************************/
110 static int mpeg_vdec_Probe( probedata_t *p_data )
112 if( p_data->i_type == MPEG1_VIDEO_ES || p_data->i_type == MPEG2_VIDEO_ES )
118 /*****************************************************************************
119 * mpeg_vdec_Run: this function is called just after the thread is created
120 *****************************************************************************/
121 static int mpeg_vdec_Run ( decoder_config_t * p_config )
123 vpar_thread_t * p_vpar;
126 intf_DbgMsg( "vpar debug: video parser thread created. Initializing..." );
128 /* Allocate the memory needed to store the thread's structure */
129 if ( (p_vpar = (vpar_thread_t *)malloc( sizeof(vpar_thread_t) )) == NULL )
131 intf_ErrMsg( "vpar error: not enough memory "
132 "for vpar_CreateThread() to create the new thread");
137 * Initialize the thread properties
139 p_vpar->p_fifo = p_config->p_decoder_fifo;
140 p_vpar->p_config = p_config;
141 p_vpar->p_vout = NULL;
146 p_vpar->p_fifo->b_error = mpeg_vdec_Init( p_vpar );
149 * Main loop - it is not executed if an error occured during
152 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
154 /* Find the next sequence header in the stream */
155 p_vpar->p_fifo->b_error = vpar_NextSequenceHeader( p_vpar );
157 while( (!p_vpar->p_fifo->b_die) && (!p_vpar->p_fifo->b_error) )
161 /* Parse the next sequence, group or picture header */
162 if( vpar_ParseHeader( p_vpar ) )
164 /* End of sequence */
173 if( ( b_error = p_vpar->p_fifo->b_error ) )
175 mpeg_vdec_ErrorThread( p_vpar );
179 mpeg_vdec_EndThread( p_vpar );
190 /*****************************************************************************
191 * mpeg_vdec_Init: initialize vpar output thread
192 *****************************************************************************
193 * This function is called from mpeg_vdec_Run and performs the second step
194 * of the initialization. It returns 0 on success. Note that the thread's
195 * flag are not modified inside this function.
196 *****************************************************************************/
197 static int mpeg_vdec_Init( vpar_thread_t *p_vpar )
200 * Choose the best motion compensation module
202 p_vpar->p_motion_module = module_Need( MODULE_CAPABILITY_MOTION, NULL );
204 if( p_vpar->p_motion_module == NULL )
206 intf_ErrMsg( "vpar error: no suitable motion compensation module" );
211 #define f ( p_vpar->p_motion_module->p_functions->motion.functions.motion )
212 memcpy( p_vpar->pool.ppppf_motion, f.ppppf_motion, sizeof(void *) * 16 );
216 * Choose the best IDCT module
218 p_vpar->p_idct_module = module_Need( MODULE_CAPABILITY_IDCT, NULL );
220 if( p_vpar->p_idct_module == NULL )
222 intf_ErrMsg( "vpar error: no suitable IDCT module" );
223 module_Unneed( p_vpar->p_motion_module );
228 #define f p_vpar->p_idct_module->p_functions->idct.functions.idct
229 p_vpar->pool.pf_idct_init = f.pf_idct_init;
230 p_vpar->pf_sparse_idct_add = f.pf_sparse_idct_add;
231 p_vpar->pf_idct_add = f.pf_idct_add;
232 p_vpar->pf_sparse_idct_copy = f.pf_sparse_idct_copy;
233 p_vpar->pf_idct_copy = f.pf_idct_copy;
234 p_vpar->pf_norm_scan = f.pf_norm_scan;
237 /* Initialize input bitstream */
238 p_vpar->p_config->pf_init_bit_stream( &p_vpar->bit_stream,
239 p_vpar->p_config->p_decoder_fifo, BitstreamCallback,
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 */
280 intf_DbgMsg("vpar debug: mpeg_vdec_Init(%p) succeeded", p_vpar);
284 /*****************************************************************************
285 * mpeg_vdec_ErrorThread: RunThread() error loop
286 *****************************************************************************
287 * This function is called when an error occured during thread main's loop. The
288 * thread can still receive feed, but must be ready to terminate as soon as
290 *****************************************************************************/
291 static void mpeg_vdec_ErrorThread( vpar_thread_t *p_vpar )
293 /* We take the lock, because we are going to read/write the start/end
294 * indexes of the decoder fifo */
295 vlc_mutex_lock( &p_vpar->p_fifo->data_lock );
297 /* Wait until a `die' order is sent */
298 while( !p_vpar->p_fifo->b_die )
300 /* Trash all received PES packets */
301 while( !DECODER_FIFO_ISEMPTY(*p_vpar->p_fifo) )
303 p_vpar->p_fifo->pf_delete_pes( p_vpar->p_fifo->p_packets_mgt,
304 DECODER_FIFO_START(*p_vpar->p_fifo) );
305 DECODER_FIFO_INCSTART( *p_vpar->p_fifo );
308 /* Waiting for the input thread to put new PES packets in the fifo */
309 vlc_cond_wait( &p_vpar->p_fifo->data_wait, &p_vpar->p_fifo->data_lock );
312 /* We can release the lock before leaving */
313 vlc_mutex_unlock( &p_vpar->p_fifo->data_lock );
316 /*****************************************************************************
317 * mpeg_vdec_EndThread: thread destruction
318 *****************************************************************************
319 * This function is called when the thread ends after a sucessful
321 *****************************************************************************/
322 static void mpeg_vdec_EndThread( vpar_thread_t *p_vpar )
324 intf_DbgMsg("vpar debug: destroying video parser thread %p", p_vpar);
326 /* Release used video buffers. */
327 if( p_vpar->sequence.p_forward != NULL )
329 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
331 if( p_vpar->sequence.p_backward != NULL )
333 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
334 vpar_SynchroDate( p_vpar ) );
335 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
337 if( p_vpar->picture.p_picture != NULL )
339 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
342 if( p_main->b_stats )
344 #ifdef HAVE_SYS_TIMES_H
345 struct tms cpu_usage;
349 intf_StatMsg( "vpar stats: %d loops among %d sequence(s)",
350 p_vpar->c_loops, p_vpar->c_sequences );
352 #ifdef HAVE_SYS_TIMES_H
353 intf_StatMsg( "vpar stats: cpu usage (user: %d, system: %d)",
354 cpu_usage.tms_utime, cpu_usage.tms_stime );
357 intf_StatMsg( "vpar stats: Read %d frames/fields (I %d/P %d/B %d)",
358 p_vpar->pc_pictures[I_CODING_TYPE]
359 + p_vpar->pc_pictures[P_CODING_TYPE]
360 + p_vpar->pc_pictures[B_CODING_TYPE],
361 p_vpar->pc_pictures[I_CODING_TYPE],
362 p_vpar->pc_pictures[P_CODING_TYPE],
363 p_vpar->pc_pictures[B_CODING_TYPE] );
364 intf_StatMsg( "vpar stats: Decoded %d frames/fields (I %d/P %d/B %d)",
365 p_vpar->pc_decoded_pictures[I_CODING_TYPE]
366 + p_vpar->pc_decoded_pictures[P_CODING_TYPE]
367 + p_vpar->pc_decoded_pictures[B_CODING_TYPE],
368 p_vpar->pc_decoded_pictures[I_CODING_TYPE],
369 p_vpar->pc_decoded_pictures[P_CODING_TYPE],
370 p_vpar->pc_decoded_pictures[B_CODING_TYPE] );
371 intf_StatMsg( "vpar stats: Read %d malformed frames/fields (I %d/P %d/B %d)",
372 p_vpar->pc_malformed_pictures[I_CODING_TYPE]
373 + p_vpar->pc_malformed_pictures[P_CODING_TYPE]
374 + p_vpar->pc_malformed_pictures[B_CODING_TYPE],
375 p_vpar->pc_malformed_pictures[I_CODING_TYPE],
376 p_vpar->pc_malformed_pictures[P_CODING_TYPE],
377 p_vpar->pc_malformed_pictures[B_CODING_TYPE] );
378 #define S p_vpar->sequence
379 intf_StatMsg( "vpar info: %s stream (%dx%d), %d.%d pi/s",
380 S.b_mpeg2 ? "MPEG-2" : "MPEG-1",
381 S.i_width, S.i_height, S.i_frame_rate/1001,
382 S.i_frame_rate % 1001 );
383 intf_StatMsg( "vpar info: %s, %s, matrix_coeff: %d",
384 S.b_progressive ? "Progressive" : "Non-progressive",
385 S.i_scalable_mode ? "scalable" : "non-scalable",
386 S.i_matrix_coefficients );
390 /* Dispose of matrices if they have been allocated. */
391 if( p_vpar->sequence.intra_quant.b_allocated )
393 free( p_vpar->sequence.intra_quant.pi_matrix );
395 if( p_vpar->sequence.nonintra_quant.b_allocated )
397 free( p_vpar->sequence.nonintra_quant.pi_matrix) ;
399 if( p_vpar->sequence.chroma_intra_quant.b_allocated )
401 free( p_vpar->sequence.chroma_intra_quant.pi_matrix );
403 if( p_vpar->sequence.chroma_nonintra_quant.b_allocated )
405 free( p_vpar->sequence.chroma_nonintra_quant.pi_matrix );
408 vpar_EndPool( p_vpar );
410 module_Unneed( p_vpar->p_idct_module );
411 module_Unneed( p_vpar->p_motion_module );
415 intf_DbgMsg("vpar debug: EndThread(%p)", p_vpar);
418 /*****************************************************************************
419 * BitstreamCallback: Import parameters from the new data/PES packet
420 *****************************************************************************
421 * This function is called by input's NextDataPacket.
422 *****************************************************************************/
423 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
424 boolean_t b_new_pes )
426 vpar_thread_t * p_vpar = (vpar_thread_t *)p_bit_stream->p_callback_arg;
430 p_vpar->sequence.next_pts =
431 DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->i_pts;
432 p_vpar->sequence.next_dts =
433 DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->i_dts;
434 p_vpar->sequence.i_current_rate =
435 DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->i_rate;
437 if( DECODER_FIFO_START( *p_bit_stream->p_decoder_fifo )->b_discontinuity )
440 intf_DbgMsg( "Discontinuity in BitstreamCallback" );
442 /* Escape the current picture and reset the picture predictors. */
443 p_vpar->sequence.b_expect_discontinuity = 1;
444 p_vpar->picture.b_error = 1;
448 if( p_bit_stream->p_data->b_discard_payload )
451 intf_DbgMsg( "Discard payload in BitstreamCallback" );
453 /* 1 packet messed up, trash the slice. */
454 p_vpar->picture.b_error = 1;