1 /*****************************************************************************
2 * mpeg4video.c: mpeg 4 video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/decoder.h>
34 #include <vlc/input.h> /* hmmm, just for INPUT_RATE_DEFAULT */
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
45 set_category( CAT_SOUT );
46 set_subcategory( SUBCAT_SOUT_PACKETIZER );
47 set_description( _("MPEG4 video packetizer") );
48 set_capability( "packetizer", 50 );
49 set_callbacks( Open, Close );
53 /****************************************************************************
55 ****************************************************************************/
56 static block_t *Packetize( decoder_t *, block_t ** );
63 mtime_t i_interpolated_pts;
64 mtime_t i_interpolated_dts;
65 mtime_t i_last_ref_pts;
66 mtime_t i_last_time_ref;
69 mtime_t i_last_timeincr;
85 static int m4v_FindStartCode( uint8_t **, uint8_t * );
86 static int m4v_VOLParse( decoder_t *, es_format_t *, uint8_t *, int );
87 static int vlc_log2( unsigned int );
89 #define VIDEO_OBJECT_MASK 0x01f
90 #define VIDEO_OBJECT_LAYER_MASK 0x00f
92 #define VIDEO_OBJECT_START_CODE 0x100
93 #define VIDEO_OBJECT_LAYER_START_CODE 0x120
94 #define VISUAL_OBJECT_SEQUENCE_START_CODE 0x1b0
95 #define VISUAL_OBJECT_SEQUENCE_END_CODE 0x1b1
96 #define USER_DATA_START_CODE 0x1b2
97 #define GROUP_OF_VOP_START_CODE 0x1b3
98 #define VIDEO_SESSION_ERROR_CODE 0x1b4
99 #define VISUAL_OBJECT_START_CODE 0x1b5
100 #define VOP_START_CODE 0x1b6
101 #define FACE_OBJECT_START_CODE 0x1ba
102 #define FACE_OBJECT_PLANE_START_CODE 0x1bb
103 #define MESH_OBJECT_START_CODE 0x1bc
104 #define MESH_OBJECT_PLANE_START_CODE 0x1bd
105 #define STILL_TEXTURE_OBJECT_START_CODE 0x1be
106 #define TEXTURE_SPATIAL_LAYER_START_CODE 0x1bf
107 #define TEXTURE_SNR_LAYER_START_CODE 0x1c0
109 /*****************************************************************************
110 * Open: probe the packetizer and return score
111 *****************************************************************************/
112 static int Open( vlc_object_t *p_this )
114 decoder_t *p_dec = (decoder_t*)p_this;
115 decoder_sys_t *p_sys;
117 switch( p_dec->fmt_in.i_codec )
119 case VLC_FOURCC( 'm', '4', 's', '2'):
120 case VLC_FOURCC( 'M', '4', 'S', '2'):
121 case VLC_FOURCC( 'm', 'p', '4', 's'):
122 case VLC_FOURCC( 'M', 'P', '4', 'S'):
123 case VLC_FOURCC( 'm', 'p', '4', 'v'):
124 case VLC_FOURCC( 'D', 'I', 'V', 'X'):
125 case VLC_FOURCC( 'd', 'i', 'v', 'x'):
126 case VLC_FOURCC( 'X', 'V', 'I', 'D'):
127 case VLC_FOURCC( 'X', 'v', 'i', 'D'):
128 case VLC_FOURCC( 'x', 'v', 'i', 'd'):
129 case VLC_FOURCC( 'D', 'X', '5', '0'):
130 case VLC_FOURCC( 'd', 'x', '5', '0'):
131 case VLC_FOURCC( 0x04, 0, 0, 0):
132 case VLC_FOURCC( '3', 'I', 'V', '2'):
133 case VLC_FOURCC( 'm', '4', 'c', 'c'):
134 case VLC_FOURCC( 'M', '4', 'C', 'C'):
141 /* Allocate the memory needed to store the decoder's structure */
142 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
144 msg_Err( p_dec, "out of memory" );
147 memset( p_sys, 0, sizeof(decoder_sys_t) );
149 /* Setup properties */
150 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
151 p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
153 if( p_dec->fmt_in.i_extra )
156 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
157 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
158 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
159 p_dec->fmt_in.i_extra );
161 msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
162 m4v_VOLParse( p_dec, &p_dec->fmt_out,
163 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
167 /* No vol, we'll have to look for one later on */
168 p_dec->fmt_out.i_extra = 0;
169 p_dec->fmt_out.p_extra = 0;
173 p_dec->pf_packetize = Packetize;
178 /*****************************************************************************
179 * Close: clean up the packetizer
180 *****************************************************************************/
181 static void Close( vlc_object_t *p_this )
183 decoder_t *p_dec = (decoder_t*)p_this;
185 if( p_dec->p_sys->p_buffer ) free( p_dec->p_sys->p_buffer );
186 free( p_dec->p_sys );
189 /****************************************************************************
190 * Packetize: the whole thing
191 ****************************************************************************/
192 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
194 decoder_sys_t *p_sys = p_dec->p_sys;
196 block_t *p_chain_out = NULL;
198 uint8_t *p_vol = NULL;
201 if( !pp_block || !*pp_block ) return NULL;
206 if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
208 p_sys->i_buffer_size += p_block->i_buffer + 1024;
209 p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
211 memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
213 p_sys->i_buffer += p_block->i_buffer;
215 if( p_sys->i_buffer > 10*1000000 )
217 msg_Warn( p_dec, "reseting context" );
222 p_start = &p_sys->p_buffer[p_sys->i_buffer - p_block->i_buffer - 4];
223 if( p_start < p_sys->p_buffer )
225 p_start = p_sys->p_buffer;
229 if( m4v_FindStartCode( &p_start, &p_sys->p_buffer[p_sys->i_buffer] ) )
231 block_Release( p_block );
235 /* fprintf( stderr, "start code=0x1%2.2x\n", p_start[3] ); */
239 /* Copy the complete VOL */
240 p_dec->fmt_out.i_extra = p_start - p_vol;
241 p_dec->fmt_out.p_extra =
242 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
243 memcpy( p_dec->fmt_out.p_extra, p_vol, p_dec->fmt_out.i_extra );
244 m4v_VOLParse( p_dec, &p_dec->fmt_out,
245 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
251 /* Output the complete VOP we have */
252 int i_out = p_start - p_sys->p_buffer;
253 block_t *p_out = block_New( p_dec, i_out );
256 memcpy( p_out->p_buffer, p_sys->p_buffer, i_out );
257 if( i_out < p_sys->i_buffer )
259 memmove( p_sys->p_buffer, &p_sys->p_buffer[i_out],
260 p_sys->i_buffer - i_out );
262 p_sys->i_buffer -= i_out;
265 p_out->i_flags = p_sys->i_flags;
266 p_out->i_pts = p_sys->i_interpolated_pts;
267 p_out->i_dts = p_sys->i_interpolated_dts;
269 /* FIXME doesn't work when there is multiple VOP in one block */
270 if( p_block->i_dts > p_sys->i_interpolated_dts )
272 p_out->i_length = p_block->i_dts - p_sys->i_interpolated_dts;
275 if( p_dec->fmt_out.i_extra > 0 )
277 block_ChainAppend( &p_chain_out, p_out );
281 msg_Warn( p_dec, "waiting for VOL" );
282 block_Release( p_out );
285 p_sys->b_vop = VLC_FALSE;
288 if( p_start[3] >= 0x20 && p_start[3] <= 0x2f )
290 /* Start of the VOL */
293 else if( p_start[3] == 0xb3 )
297 else if( p_start[3] == 0xb6 )
301 int i_modulo_time_base = 0;
302 int i_time_increment_bits;
303 int64_t i_time_increment, i_time_ref;
305 /* FIXME: we don't actually check we received enough data to read
306 * the VOP time increment. */
307 bs_init( &s, &p_start[4],
308 p_sys->i_buffer - (p_start - p_sys->p_buffer) - 4 );
310 switch( bs_read( &s, 2 ) )
313 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
316 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
319 p_sys->i_flags = BLOCK_FLAG_TYPE_B;
320 p_sys->b_frame = VLC_TRUE;
323 p_sys->i_flags = BLOCK_FLAG_TYPE_PB;
327 while( bs_read( &s, 1 ) ) i_modulo_time_base++;
328 if( !bs_read1( &s ) ) continue; /* Marker */
330 /* VOP time increment */
331 i_time_increment_bits = vlc_log2(p_dec->p_sys->i_fps_num - 1) + 1;
332 if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
333 i_time_increment = bs_read( &s, i_time_increment_bits );
335 /* Interpolate PTS/DTS */
336 if( !(p_sys->i_flags & BLOCK_FLAG_TYPE_B) )
338 p_sys->i_last_time_ref = p_sys->i_time_ref;
340 (i_modulo_time_base * p_dec->p_sys->i_fps_num);
341 i_time_ref = p_sys->i_time_ref;
345 i_time_ref = p_sys->i_last_time_ref +
346 (i_modulo_time_base * p_dec->p_sys->i_fps_num);
349 if( p_dec->p_sys->i_fps_num < 5 && /* Work-around buggy streams */
350 p_dec->fmt_in.video.i_frame_rate > 0 &&
351 p_dec->fmt_in.video.i_frame_rate_base > 0 )
353 p_sys->i_interpolated_pts += I64C(1000000) *
354 p_dec->fmt_in.video.i_frame_rate_base *
355 p_block->i_rate / INPUT_RATE_DEFAULT /
356 p_dec->fmt_in.video.i_frame_rate;
358 else if( p_dec->p_sys->i_fps_num )
359 p_sys->i_interpolated_pts +=
360 ( I64C(1000000) * (i_time_ref + i_time_increment -
361 p_sys->i_last_time - p_sys->i_last_timeincr) *
362 p_block->i_rate / INPUT_RATE_DEFAULT /
363 p_dec->p_sys->i_fps_num );
365 p_sys->i_last_time = i_time_ref;
366 p_sys->i_last_timeincr = i_time_increment;
368 /* Correct interpolated dts when we receive a new pts/dts */
369 if( p_block->i_pts > 0 )
370 p_sys->i_interpolated_pts = p_block->i_pts;
371 if( p_block->i_dts > 0 )
372 p_sys->i_interpolated_dts = p_block->i_dts;
374 if( (p_sys->i_flags & BLOCK_FLAG_TYPE_B) || !p_sys->b_frame )
376 /* Trivial case (DTS == PTS) */
378 p_sys->i_interpolated_dts = p_sys->i_interpolated_pts;
380 if( p_block->i_pts > 0 )
381 p_sys->i_interpolated_dts = p_block->i_pts;
382 if( p_block->i_dts > 0 )
383 p_sys->i_interpolated_dts = p_block->i_dts;
385 p_sys->i_interpolated_pts = p_sys->i_interpolated_dts;
389 if( p_sys->i_last_ref_pts > 0 )
390 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
392 p_sys->i_last_ref_pts = p_sys->i_interpolated_pts;
395 p_sys->b_vop = VLC_TRUE;
397 /* Don't re-use the same PTS/DTS twice */
398 p_block->i_pts = p_block->i_dts = 0;
401 p_start += 4; /* Next */
405 /****************************************************************************
407 ****************************************************************************/
408 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end )
410 uint8_t *p = *pp_start;
412 /* We wait for 4+1 bytes */
413 for( p = *pp_start; p < p_end - 5; p++ )
415 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
427 /* look at ffmpeg av_log2 ;) */
428 static int vlc_log2( unsigned int v )
431 static const int vlc_log2_table[16] =
433 0,0,1,1,2,2,2,2, 3,3,3,3,3,3,3,3
451 n += vlc_log2_table[v];
458 * - support aspect ratio
460 static int m4v_VOLParse( decoder_t *p_dec, es_format_t *fmt,
461 uint8_t *p_vol, int i_vol )
471 if( p_vol[0] == 0x00 && p_vol[1] == 0x00 &&
473 p_vol[3] >= 0x20 && p_vol[3] <= 0x2f )
486 bs_init( &s, &p_vol[4], i_vol - 4 );
488 bs_skip( &s, 1 ); /* random access */
489 i_vo_type = bs_read( &s, 8 );
492 i_vo_ver_id = bs_read( &s, 4 );
499 i_ar = bs_read( &s, 4 );
502 int i_ar_width, i_ar_height;
504 i_ar_width = bs_read( &s, 8 );
505 i_ar_height= bs_read( &s, 8 );
512 /* vol control parameter */
513 i_chroma_format = bs_read( &s, 2 );
514 i_low_delay = bs_read1( &s );
527 /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
528 i_shape = bs_read( &s, 2 );
529 if( i_shape == 3 && i_vo_ver_id != 1 )
534 if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
536 p_dec->p_sys->i_fps_num = bs_read( &s, 16 ); /* Time increment resolution*/
537 if( !p_dec->p_sys->i_fps_num ) p_dec->p_sys->i_fps_num = 1;
539 if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
543 int i_time_increment_bits =
544 vlc_log2( p_dec->p_sys->i_fps_num - 1 ) + 1;
546 if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
548 p_dec->p_sys->i_fps_den = bs_read( &s, i_time_increment_bits );
553 fmt->video.i_width = bs_read( &s, 13 );
555 fmt->video.i_height= bs_read( &s, 13 );