1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mpegvideo.c,v 1.13 2003/04/16 00:12:36 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
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 *****************************************************************************/
29 #include <vlc/decoder.h>
30 #include <vlc/input.h>
32 #include "codecs.h" /* WAVEFORMATEX BITMAPINFOHEADER */
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* strdup() */
37 /*****************************************************************************
39 *****************************************************************************/
40 typedef struct packetizer_s
42 /* Input properties */
43 decoder_fifo_t *p_fifo;
44 bit_stream_t bit_stream;
46 /* Output properties */
47 sout_packetizer_input_t *p_sout_input;
48 sout_format_t output_format;
51 mtime_t i_last_ref_pts;
53 int i_progressive_sequence;
54 uint8_t p_sequence_header[150];
55 int i_sequence_header_length;
56 int i_last_sequence_header;
58 int i_last_picture_structure;
61 static int Open ( vlc_object_t * );
62 static int Run ( decoder_fifo_t * );
64 static int InitThread ( packetizer_t * );
65 static void PacketizeThread ( packetizer_t * );
66 static void EndThread ( packetizer_t * );
68 /*****************************************************************************
70 *****************************************************************************/
73 set_description( _("MPEG-I/II video packetizer") );
74 set_capability( "packetizer", 50 );
75 set_callbacks( Open, NULL );
78 /*****************************************************************************
79 * OpenDecoder: probe the packetizer and return score
80 *****************************************************************************
81 * Tries to launch a decoder and return score so that the interface is able
83 *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
86 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
88 if( p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'v') &&
89 p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', '1') &&
90 p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', '2') )
99 /*****************************************************************************
100 * RunDecoder: this function is called just after the thread is created
101 *****************************************************************************/
102 static int Run( decoder_fifo_t *p_fifo )
104 packetizer_t *p_pack;
107 msg_Info( p_fifo, "Running mpegvideo packetizer" );
108 if( !( p_pack = malloc( sizeof( packetizer_t ) ) ) )
110 msg_Err( p_fifo, "out of memory" );
111 DecoderError( p_fifo );
114 memset( p_pack, 0, sizeof( packetizer_t ) );
116 p_pack->p_fifo = p_fifo;
118 if( InitThread( p_pack ) != 0 )
120 DecoderError( p_fifo );
124 while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
126 PacketizeThread( p_pack );
130 if( ( b_error = p_pack->p_fifo->b_error ) )
132 DecoderError( p_pack->p_fifo );
151 /*****************************************************************************
152 * InitThread: initialize data before entering main loop
153 *****************************************************************************/
155 static int InitThread( packetizer_t *p_pack )
158 p_pack->output_format.i_cat = VIDEO_ES;
159 p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'v');
160 p_pack->output_format.i_width = 0;
161 p_pack->output_format.i_height = 0;
162 p_pack->output_format.i_bitrate= 0;
163 p_pack->output_format.i_extra_data = 0;
164 p_pack->output_format.p_extra_data = NULL;
166 p_pack->p_sout_input = NULL;
168 if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
169 NULL, NULL ) != VLC_SUCCESS )
171 msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
175 p_pack->i_last_picture_structure = 0x03; /* frame picture */
179 /* from ISO 13818-2 */
180 /* converting frame_rate_code to frame_rate */
181 static const double pd_frame_rates[16] =
183 0, 24000/1001, 24, 25, 30000/1001, 30, 50, 60000/1001, 60,
187 static int CopyUntilNextStartCode( packetizer_t *p_pack,
188 sout_buffer_t *p_sout_buffer,
189 unsigned int *pi_pos )
195 p_sout_buffer->p_buffer[(*pi_pos)++] =
196 GetBits( &p_pack->bit_stream, 8 );
199 if( *pi_pos + 2048 > p_sout_buffer->i_buffer_size )
201 sout_BufferRealloc( p_pack->p_sout_input->p_sout,
203 p_sout_buffer->i_buffer_size + 50 * 1024);
206 } while( ShowBits( &p_pack->bit_stream, 24 ) != 0x01 &&
207 !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error );
212 /*****************************************************************************
213 * PacketizeThread: packetize an unit (here copy a complete pes)
214 *****************************************************************************/
215 static void PacketizeThread( packetizer_t *p_pack )
217 sout_buffer_t *p_sout_buffer = NULL;
218 vlc_bool_t b_seen_slice = VLC_FALSE;
221 mtime_t i_duration; /* of the parsed picture */
226 /* needed to calculate pts/dts */
227 int i_temporal_ref = 0;
228 int i_picture_structure = 0x03; /* frame picture */
229 int i_top_field_first = 0;
230 int i_repeat_first_field = 0;
232 if( !p_pack->p_sout_input )
234 byte_t p_temp[512];/* 150 bytes is the maximal size
235 of a sequence_header + sequence_extension */
236 int i_frame_rate_code;
239 /* skip data until we find a sequence_header_code */
240 /* TODO: store skipped somewhere so can send it to the mux
241 * after the input is created */
243 while( ShowBits( &p_pack->bit_stream, 32 ) != 0x1B3 &&
244 !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error )
246 RemoveBits( &p_pack->bit_stream, 8 );
249 msg_Warn( p_pack->p_fifo, "sequence_header_code found (%d skipped)",
252 /* copy the start_code */
254 GetChunk( &p_pack->bit_stream, p_temp, 4 ); i_pos += 4;
256 /* horizontal_size_value */
257 p_pack->output_format.i_width = ShowBits( &p_pack->bit_stream, 12 );
258 /* vertical_size_value */
259 p_pack->output_format.i_height= ShowBits( &p_pack->bit_stream, 24 ) & 0xFFF;
260 /* frame_rate_code */
261 i_frame_rate_code = ShowBits( &p_pack->bit_stream, 32 ) & 0xF;
264 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 7 ); i_pos += 7;
266 /* intra_quantiser_matrix [non_intra_quantiser_matrix] */
267 if( ShowBits( &p_pack->bit_stream, 7 ) & 0x1 )
270 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 64 ); i_pos += 64;
272 if( ShowBits( &p_pack->bit_stream, 8 ) & 0x1 )
274 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 65); i_pos += 65;
277 /* non_intra_quantiser_matrix */
278 else if( ShowBits( &p_pack->bit_stream, 8 ) & 0x1 )
280 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 65); i_pos += 65;
285 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 1 ); i_pos += 1;
288 /* sequence_extension (10 bytes) */
289 if( ShowBits( &p_pack->bit_stream, 32 ) != 0x1B5 )
291 msg_Dbg( p_pack->p_fifo, "ARRGG no extension_start_code" );
292 p_pack->i_progressive_sequence = 1;
296 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 10 );
297 p_pack->i_progressive_sequence = ( p_temp[i_pos+5]&0x08 ) ? 1 : 0;
302 /* remember sequence_header and sequence_extention */
303 memcpy( p_pack->p_sequence_header, p_temp, i_pos );
304 p_pack->i_sequence_header_length = i_pos;
306 p_pack->d_frame_rate = pd_frame_rates[i_frame_rate_code];
308 msg_Warn( p_pack->p_fifo,
309 "creating input (image size %dx%d, frame rate %.2f)",
310 p_pack->output_format.i_width,
311 p_pack->output_format.i_height,
312 p_pack->d_frame_rate );
314 /* now we have informations to create the input */
315 p_pack->p_sout_input = sout_InputNew( p_pack->p_fifo,
316 &p_pack->output_format );
318 if( !p_pack->p_sout_input )
320 msg_Err( p_pack->p_fifo, "cannot add a new stream" );
321 p_pack->p_fifo->b_error = VLC_TRUE;
325 p_sout_buffer = sout_BufferNew( p_pack->p_sout_input->p_sout,
327 p_sout_buffer->i_size = i_pos;
328 memcpy( p_sout_buffer->p_buffer, p_temp, i_pos );
334 sout_BufferNew( p_pack->p_sout_input->p_sout, 100 * 1024 );
338 p_pack->i_last_sequence_header++;
343 if( p_pack->p_fifo->b_die || p_pack->p_fifo->b_error )
348 i_code = ShowBits( &p_pack->bit_stream, 32 );
350 if( b_seen_slice && ( i_code < 0x101 || i_code > 0x1af ) )
355 if( i_code == 0x1B8 ) /* GOP */
357 /* usefull for bad MPEG-1 : repeat the sequence_header
359 if( p_pack->i_last_sequence_header > (int) p_pack->d_frame_rate )
361 memcpy( p_sout_buffer->p_buffer + i_pos,
362 p_pack->p_sequence_header,
363 p_pack->i_sequence_header_length );
364 i_pos += p_pack->i_sequence_header_length;
365 p_pack->i_last_sequence_header = 0;
368 p_pack->i_last_ref_pts =
370 (mtime_t)( 1000000 / p_pack->d_frame_rate); /* FIXME */
372 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
374 else if( i_code == 0x100 ) /* Picture */
376 /* picture_start_code */
377 GetChunk( &p_pack->bit_stream,
378 p_sout_buffer->p_buffer + i_pos, 4 ); i_pos += 4;
380 NextPTS( &p_pack->bit_stream, &i_pts, &i_dts );
381 i_temporal_ref = ShowBits( &p_pack->bit_stream, 10 );
383 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
385 else if( i_code == 0x1b5 )
387 /* extention start code 32 */
388 GetChunk( &p_pack->bit_stream,
389 p_sout_buffer->p_buffer + i_pos, 4 ); i_pos += 4;
391 if( ShowBits( &p_pack->bit_stream, 4 ) == 0x08 )
393 /* picture coding extention */
395 /* extention start code identifier(b1000) 4 */
396 /* f_code[2][2] 16 */
397 /* intra_dc_precision 2 */
398 /* picture_structure 2 */
399 /* top_field_first 1 */
400 i_picture_structure =
401 ShowBits( &p_pack->bit_stream, 24 ) & 0x03;
403 ShowBits( &p_pack->bit_stream, 25 ) & 0x01;
404 i_repeat_first_field =
405 ShowBits( &p_pack->bit_stream, 31 ) & 0x01;
407 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
411 if( i_code >= 0x101 && i_code <= 0x1af )
413 b_seen_slice = VLC_TRUE;
416 if( i_code == 0x1B3 )
418 p_pack->i_last_sequence_header = 0;
420 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
424 sout_BufferRealloc( p_pack->p_sout_input->p_sout,
425 p_sout_buffer, i_pos );
426 p_sout_buffer->i_size = i_pos;
428 /* calculate dts/pts */
429 if( p_pack->i_progressive_sequence || i_picture_structure == 0x03)
431 i_duration = (mtime_t)( 1000000 / p_pack->d_frame_rate );
435 i_duration = (mtime_t)( 1000000 / p_pack->d_frame_rate / 2);
438 /* fix i_last_dts and i_last_ref_pts with i_dts and i_pts from stream */
439 if( i_dts <= 0 && p_pack->i_last_dts <= 0 )
441 msg_Dbg( p_pack->p_fifo, "need a starting pts" );
442 sout_BufferDelete( p_pack->p_sout_input->p_sout,
450 //if( i_dts - p_pack->i_last_dts > 200000 ||
451 // i_dts - p_pack->i_last_dts < 200000 )
453 p_pack->i_last_dts = i_dts;
456 p_pack->i_last_ref_pts = i_pts -
457 i_temporal_ref * (mtime_t)( 1000000 / p_pack->d_frame_rate );
463 p_sout_buffer->i_dts = p_pack->i_last_dts;
464 p_sout_buffer->i_pts = p_pack->i_last_ref_pts +
465 i_temporal_ref * (mtime_t)( 1000000 / p_pack->d_frame_rate );
466 p_sout_buffer->i_length = i_duration;
467 p_sout_buffer->i_bitrate = (int)( 8 * i_pos * p_pack->d_frame_rate );
468 sout_InputSendBuffer( p_pack->p_sout_input, p_sout_buffer );
470 if( p_pack->i_progressive_sequence )
472 if( i_top_field_first == 0 && i_repeat_first_field == 0 )
474 p_pack->i_last_dts += i_duration;
476 else if( i_top_field_first == 0 && i_repeat_first_field == 1 )
478 p_pack->i_last_dts += 2 * i_duration;
480 else if( i_top_field_first == 1 && i_repeat_first_field == 1 )
482 p_pack->i_last_dts += 3 * i_duration;
487 if( i_picture_structure == 0x03 )
489 p_pack->i_last_dts += i_duration;
491 else if( i_picture_structure == p_pack->i_last_picture_structure )
493 p_pack->i_last_dts += 2 * i_duration;
495 else if( ( !i_top_field_first && i_picture_structure == 0x01 ) ||
496 ( i_top_field_first && i_picture_structure == 0x02 ) )
498 p_pack->i_last_dts += 2 * i_duration;
501 p_pack->i_last_picture_structure = i_picture_structure;
505 /*****************************************************************************
506 * EndThread : packetizer thread destruction
507 *****************************************************************************/
508 static void EndThread ( packetizer_t *p_pack)
510 if( p_pack->p_sout_input )
512 sout_InputDelete( p_pack->p_sout_input );