1 /*****************************************************************************
2 * mpeg4video.c: mpeg 4 video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mpeg4video.c,v 1.22 2003/12/22 02:24:53 sam Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
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>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_description( _("MPEG4 video packetizer") );
45 set_capability( "packetizer", 50 );
46 set_callbacks( Open, Close );
50 /****************************************************************************
52 ****************************************************************************/
53 static block_t *Packetize( decoder_t *, block_t ** );
70 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end );
71 static int m4v_VOLParse( es_format_t *fmt, uint8_t *p_vol, int i_vol );
73 #define VIDEO_OBJECT_MASK 0x01f
74 #define VIDEO_OBJECT_LAYER_MASK 0x00f
76 #define VIDEO_OBJECT_START_CODE 0x100
77 #define VIDEO_OBJECT_LAYER_START_CODE 0x120
78 #define VISUAL_OBJECT_SEQUENCE_START_CODE 0x1b0
79 #define VISUAL_OBJECT_SEQUENCE_END_CODE 0x1b1
80 #define USER_DATA_START_CODE 0x1b2
81 #define GROUP_OF_VOP_START_CODE 0x1b3
82 #define VIDEO_SESSION_ERROR_CODE 0x1b4
83 #define VISUAL_OBJECT_START_CODE 0x1b5
84 #define VOP_START_CODE 0x1b6
85 #define FACE_OBJECT_START_CODE 0x1ba
86 #define FACE_OBJECT_PLANE_START_CODE 0x1bb
87 #define MESH_OBJECT_START_CODE 0x1bc
88 #define MESH_OBJECT_PLANE_START_CODE 0x1bd
89 #define STILL_TEXTURE_OBJECT_START_CODE 0x1be
90 #define TEXTURE_SPATIAL_LAYER_START_CODE 0x1bf
91 #define TEXTURE_SNR_LAYER_START_CODE 0x1c0
93 /*****************************************************************************
94 * Open: probe the packetizer and return score
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
98 decoder_t *p_dec = (decoder_t*)p_this;
101 switch( p_dec->fmt_in.i_codec )
103 case VLC_FOURCC( 'm', '4', 's', '2'):
104 case VLC_FOURCC( 'M', '4', 'S', '2'):
105 case VLC_FOURCC( 'm', 'p', '4', 's'):
106 case VLC_FOURCC( 'M', 'P', '4', 'S'):
107 case VLC_FOURCC( 'm', 'p', '4', 'v'):
108 case VLC_FOURCC( 'D', 'I', 'V', 'X'):
109 case VLC_FOURCC( 'd', 'i', 'v', 'x'):
110 case VLC_FOURCC( 'X', 'V', 'I', 'D'):
111 case VLC_FOURCC( 'X', 'v', 'i', 'D'):
112 case VLC_FOURCC( 'x', 'v', 'i', 'd'):
113 case VLC_FOURCC( 'D', 'X', '5', '0'):
114 case VLC_FOURCC( 'd', 'x', '5', '0'):
115 case VLC_FOURCC( 0x04, 0, 0, 0):
116 case VLC_FOURCC( '3', 'I', 'V', '2'):
123 /* Allocate the memory needed to store the decoder's structure */
124 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
126 msg_Err( p_dec, "out of memory" );
130 p_sys->b_vop = VLC_FALSE;
132 p_sys->i_buffer_size = 0;
135 /* Setup properties */
136 p_dec->fmt_out = p_dec->fmt_in;
137 p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
139 if( p_dec->fmt_in.i_extra )
142 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
143 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
144 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
145 p_dec->fmt_in.i_extra );
147 msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
148 m4v_VOLParse( &p_dec->fmt_out,
149 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
153 /* No vol, we'll have to look for one later on */
154 p_dec->fmt_out.i_extra = 0;
155 p_dec->fmt_out.p_extra = 0;
159 p_dec->pf_packetize = Packetize;
164 /*****************************************************************************
165 * Close: clean up the packetizer
166 *****************************************************************************/
167 static void Close( vlc_object_t *p_this )
169 decoder_t *p_dec = (decoder_t*)p_this;
171 if( p_dec->p_sys->p_buffer ) free( p_dec->p_sys->p_buffer );
172 free( p_dec->p_sys );
175 /****************************************************************************
176 * Packetize: the whole thing
177 ****************************************************************************/
178 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
180 decoder_sys_t *p_sys = p_dec->p_sys;
182 block_t *p_chain_out = NULL;
184 uint8_t *p_vol = NULL;
187 if( !pp_block || !*pp_block ) return NULL;
192 if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
194 p_sys->i_buffer_size += p_block->i_buffer + 1024;
195 p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
197 memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
199 p_sys->i_buffer += p_block->i_buffer;
201 if( p_sys->i_buffer > 10*1000000 )
203 msg_Err( p_dec, "mmh reseting context" );
208 p_start = &p_sys->p_buffer[p_sys->i_buffer - p_block->i_buffer - 4];
209 if( p_start < p_sys->p_buffer )
211 p_start = p_sys->p_buffer;
215 if( m4v_FindStartCode( &p_start, &p_sys->p_buffer[p_sys->i_buffer] ) )
217 block_Release( p_block );
221 /* fprintf( stderr, "start code=0x1%2.2x\n", p_start[3] ); */
225 /* Copy the complete VOL */
226 p_dec->fmt_out.i_extra = p_start - p_vol;
227 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_out.i_extra );
228 memcpy( p_dec->fmt_out.p_extra, p_vol, p_dec->fmt_out.i_extra );
229 m4v_VOLParse( &p_dec->fmt_out,
230 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
236 /* Output the complete VOP we have */
237 int i_out = p_start - p_sys->p_buffer;
238 block_t *p_out = block_New( p_dec, i_out );
241 memcpy( p_out->p_buffer, p_sys->p_buffer, i_out );
242 if( i_out < p_sys->i_buffer )
244 memmove( p_sys->p_buffer, &p_sys->p_buffer[i_out],
245 p_sys->i_buffer - i_out );
247 p_sys->i_buffer -= i_out;
250 /* FIXME do proper dts/pts */
251 p_out->i_pts = p_sys->i_pts;
252 p_out->i_dts = p_sys->i_dts;
253 /* FIXME doesn't work when there is multiple VOP in one block */
254 if( p_block->i_dts > p_sys->i_dts )
256 p_out->i_length = p_block->i_dts - p_sys->i_dts;
259 if( p_dec->fmt_out.i_extra > 0 )
261 block_ChainAppend( &p_chain_out, p_out );
265 msg_Warn( p_dec, "waiting for VOL" );
266 block_Release( p_out );
270 fprintf( stderr, "pts=%lld dts=%lld length=%lldms\n",
271 p_out->i_pts, p_out->i_dts,
272 p_out->i_length / 1000 );
274 p_sys->b_vop = VLC_FALSE;
277 if( p_start[3] >= 0x20 && p_start[3] <= 0x2f )
279 /* Start of the VOL */
282 else if( p_start[3] == 0xb6 )
284 p_sys->b_vop = VLC_TRUE;
286 /* The pts information is not available in all the containers.
287 * FIXME: calculate the pts correctly */
288 if( p_block->i_pts > 0 )
290 p_sys->i_pts = p_block->i_pts;
294 p_sys->i_pts = p_block->i_dts;
296 p_sys->i_dts = p_block->i_dts;
298 p_start += 4; /* Next */
302 /****************************************************************************
304 ****************************************************************************/
305 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end )
307 uint8_t *p = *pp_start;
309 for( p = *pp_start; p < p_end - 4; p++ )
311 if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
323 /* look at ffmpeg av_log2 ;) */
324 static int vlc_log2( unsigned int v )
327 static const int vlc_log2_table[16] =
329 0,0,1,1,2,2,2,2, 3,3,3,3,3,3,3,3
347 n += vlc_log2_table[v];
354 * - support aspect ratio
356 static int m4v_VOLParse( es_format_t *fmt, uint8_t *p_vol, int i_vol )
363 int i_time_increment_resolution;
367 if( p_vol[0] == 0x00 && p_vol[1] == 0x00 &&
369 p_vol[3] >= 0x20 && p_vol[3] <= 0x2f )
382 bs_init( &s, &p_vol[4], i_vol - 4 );
384 bs_skip( &s, 1 ); /* random access */
385 i_vo_type = bs_read( &s, 8 );
388 i_vo_ver_id = bs_read( &s, 4 );
395 i_ar = bs_read( &s, 4 );
398 int i_ar_width, i_ar_height;
400 i_ar_width = bs_read( &s, 8 );
401 i_ar_height= bs_read( &s, 8 );
408 /* vol control parameter */
409 i_chroma_format = bs_read( &s, 2 );
410 i_low_delay = bs_read1( &s );
423 /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
424 i_shape = bs_read( &s, 2 );
425 if( i_shape == 3 && i_vo_ver_id != 1 )
430 if( !bs_read1( &s ) )
435 i_time_increment_resolution = bs_read( &s, 16 );
436 if( !bs_read1( &s ) )
444 int i_time_increment_bits = vlc_log2( i_time_increment_resolution - 1 ) + 1;
445 if( i_time_increment_bits < 1 )
447 i_time_increment_bits = 1;
449 bs_skip( &s, i_time_increment_bits );
454 fmt->video.i_width = bs_read( &s, 13 );
456 fmt->video.i_height= bs_read( &s, 13 );