1 /*****************************************************************************
2 * mpeg4video.c: mpeg 4 video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
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( 'M', 'P', '4', 'V'):
125 case VLC_FOURCC( 'D', 'I', 'V', 'X'):
126 case VLC_FOURCC( 'd', 'i', 'v', 'x'):
127 case VLC_FOURCC( 'X', 'V', 'I', 'D'):
128 case VLC_FOURCC( 'X', 'v', 'i', 'D'):
129 case VLC_FOURCC( 'x', 'v', 'i', 'd'):
130 case VLC_FOURCC( 'D', 'X', '5', '0'):
131 case VLC_FOURCC( 'd', 'x', '5', '0'):
132 case VLC_FOURCC( 0x04, 0, 0, 0):
133 case VLC_FOURCC( '3', 'I', 'V', '2'):
134 case VLC_FOURCC( 'm', '4', 'c', 'c'):
135 case VLC_FOURCC( 'M', '4', 'C', 'C'):
142 /* Allocate the memory needed to store the decoder's structure */
143 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
145 msg_Err( p_dec, "out of memory" );
148 memset( p_sys, 0, sizeof(decoder_sys_t) );
150 /* Setup properties */
151 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
152 p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
154 if( p_dec->fmt_in.i_extra )
157 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
158 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
159 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
160 p_dec->fmt_in.i_extra );
162 msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
163 m4v_VOLParse( p_dec, &p_dec->fmt_out,
164 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
168 /* No vol, we'll have to look for one later on */
169 p_dec->fmt_out.i_extra = 0;
170 p_dec->fmt_out.p_extra = 0;
174 p_dec->pf_packetize = Packetize;
179 /*****************************************************************************
180 * Close: clean up the packetizer
181 *****************************************************************************/
182 static void Close( vlc_object_t *p_this )
184 decoder_t *p_dec = (decoder_t*)p_this;
186 if( p_dec->p_sys->p_buffer ) free( p_dec->p_sys->p_buffer );
187 free( p_dec->p_sys );
190 /****************************************************************************
191 * Packetize: the whole thing
192 ****************************************************************************/
193 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
195 decoder_sys_t *p_sys = p_dec->p_sys;
197 block_t *p_chain_out = NULL;
199 uint8_t *p_vol = NULL;
202 if( !pp_block || !*pp_block ) return NULL;
207 if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
209 p_sys->i_buffer_size += p_block->i_buffer + 1024;
210 p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
212 memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
214 p_sys->i_buffer += p_block->i_buffer;
216 if( p_sys->i_buffer > 10*1000000 )
218 msg_Warn( p_dec, "reseting context" );
223 p_start = &p_sys->p_buffer[p_sys->i_buffer - p_block->i_buffer - 4];
224 if( p_start < p_sys->p_buffer )
226 p_start = p_sys->p_buffer;
230 if( m4v_FindStartCode( &p_start, &p_sys->p_buffer[p_sys->i_buffer] ) )
232 block_Release( p_block );
236 /* fprintf( stderr, "start code=0x1%2.2x\n", p_start[3] ); */
240 if( !p_dec->fmt_out.i_extra )
242 /* Copy the complete VOL */
243 p_dec->fmt_out.i_extra = p_start - p_vol;
244 p_dec->fmt_out.p_extra =
245 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
246 memcpy( p_dec->fmt_out.p_extra, p_vol,
247 p_dec->fmt_out.i_extra );
248 m4v_VOLParse( p_dec, &p_dec->fmt_out,
249 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
252 /* Remove VOL from the original stream */
253 memmove( p_vol, p_start,
254 p_sys->i_buffer - (p_start - p_sys->p_buffer) );
255 p_sys->i_buffer -= p_dec->fmt_out.i_extra;
262 /* Output the complete VOP we have */
263 int i_out = p_start - p_sys->p_buffer;
264 block_t *p_out = block_New( p_dec, i_out );
267 memcpy( p_out->p_buffer, p_sys->p_buffer, i_out );
268 if( i_out < p_sys->i_buffer )
270 memmove( p_sys->p_buffer, &p_sys->p_buffer[i_out],
271 p_sys->i_buffer - i_out );
273 p_sys->i_buffer -= i_out;
276 p_out->i_flags = p_sys->i_flags;
277 p_out->i_pts = p_sys->i_interpolated_pts;
278 p_out->i_dts = p_sys->i_interpolated_dts;
280 /* FIXME doesn't work when there is multiple VOP in one block */
281 if( p_block->i_dts > p_sys->i_interpolated_dts )
283 p_out->i_length = p_block->i_dts - p_sys->i_interpolated_dts;
286 if( p_dec->fmt_out.i_extra > 0 )
288 block_ChainAppend( &p_chain_out, p_out );
292 msg_Warn( p_dec, "waiting for VOL" );
293 block_Release( p_out );
296 p_sys->b_vop = VLC_FALSE;
299 if( p_start[3] >= 0x20 && p_start[3] <= 0x2f )
301 /* Start of the VOL */
304 else if( p_start[3] == 0xb3 )
308 else if( p_start[3] == 0xb6 )
312 int i_modulo_time_base = 0;
313 int i_time_increment_bits;
314 int64_t i_time_increment, i_time_ref;
316 /* FIXME: we don't actually check we received enough data to read
317 * the VOP time increment. */
318 bs_init( &s, &p_start[4],
319 p_sys->i_buffer - (p_start - p_sys->p_buffer) - 4 );
321 switch( bs_read( &s, 2 ) )
324 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
327 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
330 p_sys->i_flags = BLOCK_FLAG_TYPE_B;
331 p_sys->b_frame = VLC_TRUE;
334 p_sys->i_flags = BLOCK_FLAG_TYPE_PB;
338 while( bs_read( &s, 1 ) ) i_modulo_time_base++;
339 if( !bs_read1( &s ) ) continue; /* Marker */
341 /* VOP time increment */
342 i_time_increment_bits = vlc_log2(p_dec->p_sys->i_fps_num - 1) + 1;
343 if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
344 i_time_increment = bs_read( &s, i_time_increment_bits );
346 /* Interpolate PTS/DTS */
347 if( !(p_sys->i_flags & BLOCK_FLAG_TYPE_B) )
349 p_sys->i_last_time_ref = p_sys->i_time_ref;
351 (i_modulo_time_base * p_dec->p_sys->i_fps_num);
352 i_time_ref = p_sys->i_time_ref;
356 i_time_ref = p_sys->i_last_time_ref +
357 (i_modulo_time_base * p_dec->p_sys->i_fps_num);
360 if( p_dec->p_sys->i_fps_num < 5 && /* Work-around buggy streams */
361 p_dec->fmt_in.video.i_frame_rate > 0 &&
362 p_dec->fmt_in.video.i_frame_rate_base > 0 )
364 p_sys->i_interpolated_pts += I64C(1000000) *
365 p_dec->fmt_in.video.i_frame_rate_base *
366 p_block->i_rate / INPUT_RATE_DEFAULT /
367 p_dec->fmt_in.video.i_frame_rate;
369 else if( p_dec->p_sys->i_fps_num )
370 p_sys->i_interpolated_pts +=
371 ( I64C(1000000) * (i_time_ref + i_time_increment -
372 p_sys->i_last_time - p_sys->i_last_timeincr) *
373 p_block->i_rate / INPUT_RATE_DEFAULT /
374 p_dec->p_sys->i_fps_num );
376 p_sys->i_last_time = i_time_ref;
377 p_sys->i_last_timeincr = i_time_increment;
379 /* Correct interpolated dts when we receive a new pts/dts */
380 if( p_block->i_pts > 0 )
381 p_sys->i_interpolated_pts = p_block->i_pts;
382 if( p_block->i_dts > 0 )
383 p_sys->i_interpolated_dts = p_block->i_dts;
385 if( (p_sys->i_flags & BLOCK_FLAG_TYPE_B) || !p_sys->b_frame )
387 /* Trivial case (DTS == PTS) */
389 p_sys->i_interpolated_dts = p_sys->i_interpolated_pts;
391 if( p_block->i_pts > 0 )
392 p_sys->i_interpolated_dts = p_block->i_pts;
393 if( p_block->i_dts > 0 )
394 p_sys->i_interpolated_dts = p_block->i_dts;
396 p_sys->i_interpolated_pts = p_sys->i_interpolated_dts;
400 if( p_sys->i_last_ref_pts > 0 )
401 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
403 p_sys->i_last_ref_pts = p_sys->i_interpolated_pts;
406 p_sys->b_vop = VLC_TRUE;
408 /* Don't re-use the same PTS/DTS twice */
409 p_block->i_pts = p_block->i_dts = 0;
412 p_start += 4; /* Next */
416 /****************************************************************************
418 ****************************************************************************/
419 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end )
421 uint8_t *p = *pp_start;
423 /* We wait for 4+1 bytes */
424 for( p = *pp_start; p < p_end - 5; p++ )
426 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
438 /* look at ffmpeg av_log2 ;) */
439 static int vlc_log2( unsigned int v )
442 static const int vlc_log2_table[16] =
444 0,0,1,1,2,2,2,2, 3,3,3,3,3,3,3,3
462 n += vlc_log2_table[v];
469 * - support aspect ratio
471 static int m4v_VOLParse( decoder_t *p_dec, es_format_t *fmt,
472 uint8_t *p_vol, int i_vol )
482 if( p_vol[0] == 0x00 && p_vol[1] == 0x00 &&
484 p_vol[3] >= 0x20 && p_vol[3] <= 0x2f )
497 bs_init( &s, &p_vol[4], i_vol - 4 );
499 bs_skip( &s, 1 ); /* random access */
500 i_vo_type = bs_read( &s, 8 );
503 i_vo_ver_id = bs_read( &s, 4 );
510 i_ar = bs_read( &s, 4 );
513 int i_ar_width, i_ar_height;
515 i_ar_width = bs_read( &s, 8 );
516 i_ar_height= bs_read( &s, 8 );
523 /* vol control parameter */
524 i_chroma_format = bs_read( &s, 2 );
525 i_low_delay = bs_read1( &s );
538 /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
539 i_shape = bs_read( &s, 2 );
540 if( i_shape == 3 && i_vo_ver_id != 1 )
545 if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
547 p_dec->p_sys->i_fps_num = bs_read( &s, 16 ); /* Time increment resolution*/
548 if( !p_dec->p_sys->i_fps_num ) p_dec->p_sys->i_fps_num = 1;
550 if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
554 int i_time_increment_bits =
555 vlc_log2( p_dec->p_sys->i_fps_num - 1 ) + 1;
557 if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
559 p_dec->p_sys->i_fps_den = bs_read( &s, i_time_increment_bits );
564 fmt->video.i_width = bs_read( &s, 13 );
566 fmt->video.i_height= bs_read( &s, 13 );