1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mpegvideo.c,v 1.15 2003/06/10 22:42:59 gbazin 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 /*****************************************************************************
26 * Problem with this implementation:
28 * Although we should time-stamp each picture with a PTS, this isn't possible
29 * with the current implementation.
30 * The problem comes from the fact that for non-low-delay streams we can't
31 * calculate the PTS of pictures used as backward reference. Even the temporal
32 * reference number doesn't help here because all the pictures don't
33 * necessarily have the same duration (eg. 3:2 pulldown).
35 * However this doesn't really matter as far as the MPEG muxers are concerned
36 * because they allow having empty PTS fields. --gibalou
37 *****************************************************************************/
39 /*****************************************************************************
41 *****************************************************************************/
43 #include <vlc/decoder.h>
44 #include <vlc/input.h>
46 #include "codecs.h" /* WAVEFORMATEX BITMAPINFOHEADER */
48 #include <stdlib.h> /* malloc(), free() */
49 #include <string.h> /* strdup() */
51 /*****************************************************************************
53 *****************************************************************************/
54 typedef struct packetizer_s
56 /* Input properties */
57 decoder_fifo_t *p_fifo;
58 bit_stream_t bit_stream;
60 /* Output properties */
61 sout_packetizer_input_t *p_sout_input;
62 sout_format_t output_format;
64 mtime_t i_interpolated_dts;
65 mtime_t i_old_duration;
66 mtime_t i_last_ref_pts;
68 int i_progressive_sequence;
70 uint8_t p_sequence_header[150];
71 int i_sequence_header_length;
72 int i_last_sequence_header;
76 static int Open ( vlc_object_t * );
77 static int Run ( decoder_fifo_t * );
79 static int InitThread ( packetizer_t * );
80 static void PacketizeThread ( packetizer_t * );
81 static void EndThread ( packetizer_t * );
83 /*****************************************************************************
85 *****************************************************************************/
88 set_description( _("MPEG-I/II video packetizer") );
89 set_capability( "packetizer", 50 );
90 set_callbacks( Open, NULL );
93 /*****************************************************************************
94 * OpenDecoder: probe the packetizer and return score
95 *****************************************************************************
96 * Tries to launch a decoder and return score so that the interface is able
98 *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
103 if( p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'v') &&
104 p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', '1') &&
105 p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', '2') )
110 p_fifo->pf_run = Run;
114 /*****************************************************************************
115 * RunDecoder: this function is called just after the thread is created
116 *****************************************************************************/
117 static int Run( decoder_fifo_t *p_fifo )
119 packetizer_t *p_pack;
122 msg_Info( p_fifo, "Running mpegvideo packetizer" );
123 if( !( p_pack = malloc( sizeof( packetizer_t ) ) ) )
125 msg_Err( p_fifo, "out of memory" );
126 DecoderError( p_fifo );
129 memset( p_pack, 0, sizeof( packetizer_t ) );
131 p_pack->p_fifo = p_fifo;
133 if( InitThread( p_pack ) != 0 )
135 DecoderError( p_fifo );
139 while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
141 PacketizeThread( p_pack );
145 if( ( b_error = p_pack->p_fifo->b_error ) )
147 DecoderError( p_pack->p_fifo );
166 /*****************************************************************************
167 * InitThread: initialize data before entering main loop
168 *****************************************************************************/
170 static int InitThread( packetizer_t *p_pack )
173 p_pack->output_format.i_cat = VIDEO_ES;
174 p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', 'g', 'v');
175 p_pack->output_format.i_width = 0;
176 p_pack->output_format.i_height = 0;
177 p_pack->output_format.i_bitrate= 0;
178 p_pack->output_format.i_extra_data = 0;
179 p_pack->output_format.p_extra_data = NULL;
181 p_pack->p_sout_input = NULL;
183 if( InitBitstream( &p_pack->bit_stream, p_pack->p_fifo,
184 NULL, NULL ) != VLC_SUCCESS )
186 msg_Err( p_pack->p_fifo, "cannot initialize bitstream" );
193 /* from ISO 13818-2 */
194 /* converting frame_rate_code to frame_rate */
195 static const double pd_frame_rates[16] =
197 0, 24000.0/1001, 24, 25, 30000.0/1001, 30, 50, 60000.0/1001, 60,
201 static int CopyUntilNextStartCode( packetizer_t *p_pack,
202 sout_buffer_t *p_sout_buffer,
203 unsigned int *pi_pos )
209 p_sout_buffer->p_buffer[(*pi_pos)++] =
210 GetBits( &p_pack->bit_stream, 8 );
213 if( *pi_pos + 2048 > p_sout_buffer->i_buffer_size )
215 sout_BufferRealloc( p_pack->p_sout_input->p_sout,
217 p_sout_buffer->i_buffer_size + 50 * 1024);
220 } while( ShowBits( &p_pack->bit_stream, 24 ) != 0x01 &&
221 !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error );
226 /*****************************************************************************
227 * PacketizeThread: packetize an unit (here copy a complete pes)
228 *****************************************************************************/
229 static void PacketizeThread( packetizer_t *p_pack )
231 sout_buffer_t *p_sout_buffer = NULL;
232 vlc_bool_t b_seen_slice = VLC_FALSE;
235 mtime_t i_duration; /* of the parsed picture */
240 /* needed to calculate pts/dts */
241 int i_temporal_ref = 0;
242 int i_picture_coding_type = 0;
243 int i_picture_structure = 0x03; /* frame picture */
244 int i_top_field_first = 0;
245 int i_repeat_first_field = 0;
246 int i_progressive_frame = 0;
248 if( !p_pack->p_sout_input )
250 byte_t p_temp[512];/* 150 bytes is the maximal size
251 of a sequence_header + sequence_extension */
252 int i_frame_rate_code;
255 /* skip data until we find a sequence_header_code */
256 /* TODO: store skipped somewhere so can send it to the mux
257 * after the input is created */
259 while( ShowBits( &p_pack->bit_stream, 32 ) != 0x1B3 &&
260 !p_pack->p_fifo->b_die && !p_pack->p_fifo->b_error )
262 RemoveBits( &p_pack->bit_stream, 8 );
265 msg_Warn( p_pack->p_fifo, "sequence_header_code found (%d skipped)",
268 /* copy the start_code */
270 GetChunk( &p_pack->bit_stream, p_temp, 4 ); i_pos += 4;
272 /* horizontal_size_value */
273 p_pack->output_format.i_width = ShowBits( &p_pack->bit_stream, 12 );
274 /* vertical_size_value */
275 p_pack->output_format.i_height= ShowBits( &p_pack->bit_stream, 24 ) & 0xFFF;
276 /* frame_rate_code */
277 i_frame_rate_code = ShowBits( &p_pack->bit_stream, 32 ) & 0xF;
280 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 7 ); i_pos += 7;
282 /* intra_quantiser_matrix [non_intra_quantiser_matrix] */
283 if( ShowBits( &p_pack->bit_stream, 7 ) & 0x1 )
286 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 64 ); i_pos += 64;
288 if( ShowBits( &p_pack->bit_stream, 8 ) & 0x1 )
290 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 65); i_pos += 65;
293 /* non_intra_quantiser_matrix */
294 else if( ShowBits( &p_pack->bit_stream, 8 ) & 0x1 )
296 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 65); i_pos += 65;
301 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 1 ); i_pos += 1;
304 /* sequence_extension (10 bytes) */
305 if( ShowBits( &p_pack->bit_stream, 32 ) != 0x1B5 )
307 msg_Dbg( p_pack->p_fifo, "ARRGG no extension_start_code" );
308 p_pack->i_progressive_sequence = 1;
309 p_pack->i_low_delay = 1;
313 GetChunk( &p_pack->bit_stream, p_temp + i_pos, 10 );
314 p_pack->i_progressive_sequence = ( p_temp[i_pos+5]&0x08 ) ? 1 : 0;
315 p_pack->i_low_delay = ( p_temp[i_pos+9]&0x80 ) ? 1 : 0;
319 /* remember sequence_header and sequence_extention */
320 memcpy( p_pack->p_sequence_header, p_temp, i_pos );
321 p_pack->i_sequence_header_length = i_pos;
323 p_pack->d_frame_rate = pd_frame_rates[i_frame_rate_code];
325 msg_Warn( p_pack->p_fifo,
326 "creating input (image size %dx%d, frame rate %.2f)",
327 p_pack->output_format.i_width,
328 p_pack->output_format.i_height,
329 p_pack->d_frame_rate );
331 /* now we have informations to create the input */
332 p_pack->p_sout_input = sout_InputNew( p_pack->p_fifo,
333 &p_pack->output_format );
335 if( !p_pack->p_sout_input )
337 msg_Err( p_pack->p_fifo, "cannot add a new stream" );
338 p_pack->p_fifo->b_error = VLC_TRUE;
342 p_sout_buffer = sout_BufferNew( p_pack->p_sout_input->p_sout,
344 p_sout_buffer->i_size = i_pos;
345 memcpy( p_sout_buffer->p_buffer, p_temp, i_pos );
351 sout_BufferNew( p_pack->p_sout_input->p_sout, 100 * 1024 );
355 p_pack->i_last_sequence_header++;
360 if( p_pack->p_fifo->b_die || p_pack->p_fifo->b_error )
365 i_code = ShowBits( &p_pack->bit_stream, 32 );
367 if( b_seen_slice && ( i_code < 0x101 || i_code > 0x1af ) )
372 if( i_code == 0x1B8 ) /* GOP */
374 /* usefull for bad MPEG-1 : repeat the sequence_header
376 if( p_pack->i_last_sequence_header > (int) p_pack->d_frame_rate )
378 memcpy( p_sout_buffer->p_buffer + i_pos,
379 p_pack->p_sequence_header,
380 p_pack->i_sequence_header_length );
381 i_pos += p_pack->i_sequence_header_length;
382 p_pack->i_last_sequence_header = 0;
384 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
386 else if( i_code == 0x100 ) /* Picture */
388 /* picture_start_code */
389 GetChunk( &p_pack->bit_stream,
390 p_sout_buffer->p_buffer + i_pos, 4 ); i_pos += 4;
392 NextPTS( &p_pack->bit_stream, &i_pts, &i_dts );
393 i_temporal_ref = ShowBits( &p_pack->bit_stream, 10 );
394 i_picture_coding_type = ShowBits( &p_pack->bit_stream, 13 ) & 0x3;
396 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
398 else if( i_code == 0x1b5 )
400 /* extention start code 32 */
401 GetChunk( &p_pack->bit_stream,
402 p_sout_buffer->p_buffer + i_pos, 4 ); i_pos += 4;
404 if( ShowBits( &p_pack->bit_stream, 4 ) == 0x08 )
406 /* picture coding extention */
408 /* extention start code identifier(b1000) 4 */
409 /* f_code[2][2] 16 */
410 /* intra_dc_precision 2 */
411 /* picture_structure 2 */
412 /* top_field_first 1 */
413 i_picture_structure =
414 ShowBits( &p_pack->bit_stream, 24 ) & 0x03;
416 ShowBits( &p_pack->bit_stream, 25 ) & 0x01;
417 i_repeat_first_field =
418 ShowBits( &p_pack->bit_stream, 31 ) & 0x01;
420 GetChunk( &p_pack->bit_stream,
421 p_sout_buffer->p_buffer + i_pos, 4 ); i_pos += 4;
423 i_progressive_frame =
424 ShowBits( &p_pack->bit_stream, 1 ) & 0x01;
426 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
430 if( i_code >= 0x101 && i_code <= 0x1af )
432 b_seen_slice = VLC_TRUE;
435 if( i_code == 0x1B3 )
437 p_pack->i_last_sequence_header = 0;
439 CopyUntilNextStartCode( p_pack, p_sout_buffer, &i_pos );
443 if( i_pts <= 0 && i_dts <= 0 && p_pack->i_interpolated_dts <= 0 )
445 msg_Dbg( p_pack->p_fifo, "need a starting pts/dts" );
446 sout_BufferDelete( p_pack->p_sout_input->p_sout, p_sout_buffer );
450 sout_BufferRealloc( p_pack->p_sout_input->p_sout,
451 p_sout_buffer, i_pos );
452 p_sout_buffer->i_size = i_pos;
454 /* calculate frame duration */
455 if( p_pack->i_progressive_sequence || i_picture_structure == 0x03)
457 i_duration = (mtime_t)( 1000000 / p_pack->d_frame_rate );
461 i_duration = (mtime_t)( 1000000 / p_pack->d_frame_rate / 2);
464 if( p_pack->i_progressive_sequence )
466 if( i_top_field_first == 0 && i_repeat_first_field == 1 )
468 i_duration = 2 * i_duration;
470 else if( i_top_field_first == 1 && i_repeat_first_field == 1 )
472 i_duration = 3 * i_duration;
477 if( i_picture_structure == 0x03 )
479 if( i_progressive_frame && i_repeat_first_field )
481 i_duration += i_duration / 2;
486 if( p_pack->i_low_delay || i_picture_coding_type == 0x03 )
488 /* Trivial case (DTS == PTS) */
489 /* Correct interpolated dts when we receive a new pts/dts */
490 if( i_pts > 0 ) p_pack->i_interpolated_dts = i_pts;
491 if( i_dts > 0 ) p_pack->i_interpolated_dts = i_dts;
495 /* Correct interpolated dts when we receive a new pts/dts */
496 if( p_pack->i_last_ref_pts )
497 p_pack->i_interpolated_dts = p_pack->i_last_ref_pts;
498 if( i_dts > 0 ) p_pack->i_interpolated_dts = i_dts;
500 p_pack->i_last_ref_pts = i_pts;
503 /* Don't even try to calculate the PTS unless it is given in the
505 p_sout_buffer->i_pts = i_pts ? i_pts : -1;
507 p_sout_buffer->i_dts = p_pack->i_interpolated_dts;
509 if( p_pack->i_low_delay || i_picture_coding_type == 0x03 )
511 /* Trivial case (DTS == PTS) */
512 p_pack->i_interpolated_dts += i_duration;
516 p_pack->i_interpolated_dts += p_pack->i_old_duration;
517 p_pack->i_old_duration = i_duration;
520 p_sout_buffer->i_bitrate = (int)( 8 * i_pos * p_pack->d_frame_rate );
521 p_sout_buffer->i_length = i_duration;
523 sout_InputSendBuffer( p_pack->p_sout_input, p_sout_buffer );
527 /*****************************************************************************
528 * EndThread : packetizer thread destruction
529 *****************************************************************************/
530 static void EndThread ( packetizer_t *p_pack)
532 if( p_pack->p_sout_input )
534 sout_InputDelete( p_pack->p_sout_input );