]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4video.c
* modules/packetizer/mpeg4video.c: fixed memory leak (patch by Sau).
[vlc] / modules / packetizer / mpeg4video.c
1 /*****************************************************************************
2  * mpeg4video.c: mpeg 4 video packetizer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/decoder.h>
33 #include <vlc/sout.h>
34
35 #include "vlc_bits.h"
36
37 /*****************************************************************************
38  * Module descriptor
39  *****************************************************************************/
40 static int  Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
42
43 vlc_module_begin();
44     set_description( _("MPEG4 video packetizer") );
45     set_capability( "packetizer", 50 );
46     set_callbacks( Open, Close );
47 vlc_module_end();
48
49
50 /****************************************************************************
51  * Local prototypes
52  ****************************************************************************/
53 static block_t *Packetize( decoder_t *, block_t ** );
54
55 struct decoder_sys_t
56 {
57     /*
58      * Common properties
59      */
60     mtime_t i_pts;
61     mtime_t i_dts;
62
63
64     vlc_bool_t  b_vop;
65     int         i_buffer;
66     int         i_buffer_size;
67     uint8_t     *p_buffer;
68     unsigned int i_flags;
69
70     vlc_bool_t  b_frame;
71 };
72
73 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end );
74 static int m4v_VOLParse( es_format_t *fmt, uint8_t *p_vol, int i_vol );
75
76 #define VIDEO_OBJECT_MASK                       0x01f
77 #define VIDEO_OBJECT_LAYER_MASK                 0x00f
78
79 #define VIDEO_OBJECT_START_CODE                 0x100
80 #define VIDEO_OBJECT_LAYER_START_CODE           0x120
81 #define VISUAL_OBJECT_SEQUENCE_START_CODE       0x1b0
82 #define VISUAL_OBJECT_SEQUENCE_END_CODE         0x1b1
83 #define USER_DATA_START_CODE                    0x1b2
84 #define GROUP_OF_VOP_START_CODE                 0x1b3
85 #define VIDEO_SESSION_ERROR_CODE                0x1b4
86 #define VISUAL_OBJECT_START_CODE                0x1b5
87 #define VOP_START_CODE                          0x1b6
88 #define FACE_OBJECT_START_CODE                  0x1ba
89 #define FACE_OBJECT_PLANE_START_CODE            0x1bb
90 #define MESH_OBJECT_START_CODE                  0x1bc
91 #define MESH_OBJECT_PLANE_START_CODE            0x1bd
92 #define STILL_TEXTURE_OBJECT_START_CODE         0x1be
93 #define TEXTURE_SPATIAL_LAYER_START_CODE        0x1bf
94 #define TEXTURE_SNR_LAYER_START_CODE            0x1c0
95
96 /*****************************************************************************
97  * Open: probe the packetizer and return score
98  *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {
101     decoder_t     *p_dec = (decoder_t*)p_this;
102     decoder_sys_t *p_sys;
103
104     switch( p_dec->fmt_in.i_codec )
105     {
106         case VLC_FOURCC( 'm', '4', 's', '2'):
107         case VLC_FOURCC( 'M', '4', 'S', '2'):
108         case VLC_FOURCC( 'm', 'p', '4', 's'):
109         case VLC_FOURCC( 'M', 'P', '4', 'S'):
110         case VLC_FOURCC( 'm', 'p', '4', 'v'):
111         case VLC_FOURCC( 'D', 'I', 'V', 'X'):
112         case VLC_FOURCC( 'd', 'i', 'v', 'x'):
113         case VLC_FOURCC( 'X', 'V', 'I', 'D'):
114         case VLC_FOURCC( 'X', 'v', 'i', 'D'):
115         case VLC_FOURCC( 'x', 'v', 'i', 'd'):
116         case VLC_FOURCC( 'D', 'X', '5', '0'):
117         case VLC_FOURCC( 'd', 'x', '5', '0'):
118         case VLC_FOURCC( 0x04, 0,   0,   0):
119         case VLC_FOURCC( '3', 'I', 'V', '2'):
120         case VLC_FOURCC( 'm', '4', 'c', 'c'):
121         case VLC_FOURCC( 'M', '4', 'C', 'C'):
122             break;
123
124         default:
125             return VLC_EGENERIC;
126     }
127
128     /* Allocate the memory needed to store the decoder's structure */
129     if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
130     {
131         msg_Err( p_dec, "out of memory" );
132         return VLC_EGENERIC;
133     }
134     p_sys->i_pts = 0;
135     p_sys->i_dts = 0;
136     p_sys->b_vop = VLC_FALSE;
137     p_sys->i_buffer = 0;
138     p_sys->i_buffer_size = 0;
139     p_sys->p_buffer = 0;
140     p_sys->i_flags = 0;
141     p_sys->b_frame = VLC_FALSE;
142
143     /* Setup properties */
144     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
145     p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
146
147     if( p_dec->fmt_in.i_extra )
148     {
149         /* We have a vol */
150         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
151         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
152         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
153                 p_dec->fmt_in.i_extra );
154
155         msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
156         m4v_VOLParse( &p_dec->fmt_out,
157                       p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
158     }
159     else
160     {
161         /* No vol, we'll have to look for one later on */
162         p_dec->fmt_out.i_extra = 0;
163         p_dec->fmt_out.p_extra = 0;
164     }
165
166     /* Set callback */
167     p_dec->pf_packetize = Packetize;
168
169     return VLC_SUCCESS;
170 }
171
172 /*****************************************************************************
173  * Close: clean up the packetizer
174  *****************************************************************************/
175 static void Close( vlc_object_t *p_this )
176 {
177     decoder_t *p_dec = (decoder_t*)p_this;
178
179     if( p_dec->p_sys->p_buffer ) free( p_dec->p_sys->p_buffer );
180     free( p_dec->p_sys );
181 }
182
183 /****************************************************************************
184  * Packetize: the whole thing
185  ****************************************************************************/
186 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
187 {
188     decoder_sys_t *p_sys = p_dec->p_sys;
189
190     block_t *p_chain_out = NULL;
191     block_t *p_block;
192     uint8_t *p_vol = NULL;
193     uint8_t *p_start;
194
195     if( !pp_block || !*pp_block ) return NULL;
196
197     p_block = *pp_block;
198
199     /* Append data */
200     if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
201     {
202         p_sys->i_buffer_size += p_block->i_buffer + 1024;
203         p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
204     }
205     memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
206             p_block->i_buffer );
207     p_sys->i_buffer += p_block->i_buffer;
208
209     if( p_sys->i_buffer > 10*1000000 )
210     {
211         msg_Err( p_dec, "mmh reseting context" );
212         p_sys->i_buffer = 0;
213     }
214
215     /* Search vop */
216     p_start = &p_sys->p_buffer[p_sys->i_buffer - p_block->i_buffer - 4];
217     if( p_start < p_sys->p_buffer )
218     {
219         p_start = p_sys->p_buffer;
220     }
221     for( ;; )
222     {
223         if( m4v_FindStartCode( &p_start, &p_sys->p_buffer[p_sys->i_buffer] ) )
224         {
225             block_Release( p_block );
226             *pp_block = NULL;
227             return p_chain_out;
228         }
229         /* fprintf( stderr, "start code=0x1%2.2x\n", p_start[3] ); */
230
231         if( p_vol )
232         {
233             /* Copy the complete VOL */
234             p_dec->fmt_out.i_extra = p_start - p_vol;
235             p_dec->fmt_out.p_extra =
236                 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
237             memcpy( p_dec->fmt_out.p_extra, p_vol, p_dec->fmt_out.i_extra );
238             m4v_VOLParse( &p_dec->fmt_out,
239                           p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
240
241             p_vol = NULL;
242         }
243         if( p_sys->b_vop )
244         {
245             /* Output the complete VOP we have */
246             int     i_out = p_start - p_sys->p_buffer;
247             block_t *p_out = block_New( p_dec, i_out );
248
249             /* extract data */
250             memcpy( p_out->p_buffer, p_sys->p_buffer, i_out );
251             if( i_out < p_sys->i_buffer )
252             {
253                 memmove( p_sys->p_buffer, &p_sys->p_buffer[i_out],
254                          p_sys->i_buffer - i_out );
255             }
256             p_sys->i_buffer -= i_out;
257             p_start -= i_out;
258
259             p_out->i_flags = p_sys->i_flags;
260
261             /* FIXME do proper dts/pts */
262             p_out->i_pts = p_sys->i_pts;
263             p_out->i_dts = p_sys->i_dts;
264             /* FIXME doesn't work when there is multiple VOP in one block */
265             if( p_block->i_dts > p_sys->i_dts )
266             {
267                 p_out->i_length = p_block->i_dts - p_sys->i_dts;
268             }
269
270             if( p_dec->fmt_out.i_extra > 0 )
271             {
272                 block_ChainAppend( &p_chain_out, p_out );
273             }
274             else
275             {
276                 msg_Warn( p_dec, "waiting for VOL" );
277                 block_Release( p_out );
278             }
279
280 #if 0
281             fprintf( stderr, "pts=%lld dts=%lld length=%lldms\n",
282                      p_out->i_pts, p_out->i_dts,
283                      p_out->i_length / 1000 );
284 #endif
285             p_sys->b_vop = VLC_FALSE;
286         }
287
288         if( p_start[3] >= 0x20 && p_start[3] <= 0x2f )
289         {
290             /* Start of the VOL */
291             p_vol = p_start;
292         }
293         else if( p_start[3] == 0xb6 )
294         {
295             p_sys->b_vop = VLC_TRUE;
296             switch( p_start[4] >> 6 )
297             {
298                 case 0:
299                     p_sys->i_flags = BLOCK_FLAG_TYPE_I;
300                     break;
301                 case 1:
302                     p_sys->i_flags = BLOCK_FLAG_TYPE_P;
303                     break;
304                 case 2:
305                     p_sys->i_flags = BLOCK_FLAG_TYPE_B;
306                     p_sys->b_frame = VLC_TRUE;
307                     break;
308                 case 3: /* gni ? */
309                     p_sys->i_flags = BLOCK_FLAG_TYPE_PB;
310                     break;
311             }
312
313             /* The pts information is not available in all the containers.
314              * FIXME: calculate the pts correctly */
315             if( p_block->i_pts > 0 )
316             {
317                 p_sys->i_pts = p_block->i_pts;
318             }
319             else if( (p_sys->i_flags&BLOCK_FLAG_TYPE_B) || !p_sys->b_frame )
320             {
321                 p_sys->i_pts = p_block->i_dts;
322             }
323             else
324             {
325                 p_sys->i_pts = 0;
326             }
327             if( p_block->i_dts > 0 )
328             {
329                 p_sys->i_dts = p_block->i_dts;
330             }
331             else if( p_sys->i_dts > 0 )
332             {
333                 /* XXX KLUDGE immonde, else transcode won't work */
334                 p_sys->i_dts += 1000;
335             }
336         }
337         p_start += 4; /* Next */
338     }
339 }
340
341 /****************************************************************************
342  * m4v_FindStartCode
343  ****************************************************************************/
344 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end )
345 {
346     uint8_t *p = *pp_start;
347
348     /* We wait for 4+1 bytes */
349     for( p = *pp_start; p < p_end - 5; p++ )
350     {
351         if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
352         {
353             *pp_start = p;
354             return VLC_SUCCESS;
355         }
356     }
357
358     *pp_start = p_end;
359     return VLC_EGENERIC;
360 }
361
362
363 /* look at ffmpeg av_log2 ;) */
364 static int vlc_log2( unsigned int v )
365 {
366     int n = 0;
367     static const int vlc_log2_table[16] =
368     {
369         0,0,1,1,2,2,2,2, 3,3,3,3,3,3,3,3
370     };
371
372     if( v&0xffff0000 )
373     {
374         v >>= 16;
375         n += 16;
376     }
377     if( v&0xff00 )
378     {
379         v >>= 8;
380         n += 8;
381     }
382     if( v&0xf0 )
383     {
384         v >>= 4;
385         n += 4;
386     }
387     n += vlc_log2_table[v];
388
389     return n;
390 }
391
392 /* m4v_VOLParse:
393  *  TODO:
394  *      - support aspect ratio
395  */
396 static int m4v_VOLParse( es_format_t *fmt, uint8_t *p_vol, int i_vol )
397 {
398     bs_t s;
399     int i_vo_type;
400     int i_vo_ver_id;
401     int i_ar;
402     int i_shape;
403     int i_time_increment_resolution;
404
405     for( ;; )
406     {
407         if( p_vol[0] == 0x00 && p_vol[1] == 0x00 &&
408             p_vol[2] == 0x01 &&
409             p_vol[3] >= 0x20 && p_vol[3] <= 0x2f )
410         {
411             break;
412         }
413         p_vol++;
414         i_vol--;
415         if( i_vol <= 4 )
416         {
417             return VLC_EGENERIC;
418         }
419     }
420
421     /* parse the vol */
422     bs_init( &s, &p_vol[4], i_vol - 4 );
423
424     bs_skip( &s, 1 );   /* random access */
425     i_vo_type = bs_read( &s, 8 );
426     if( bs_read1( &s ) )
427     {
428         i_vo_ver_id = bs_read( &s, 4 );
429         bs_skip( &s, 3 );
430     }
431     else
432     {
433         i_vo_ver_id = 1;
434     }
435     i_ar = bs_read( &s, 4 );
436     if( i_ar == 0xf )
437     {
438         int i_ar_width, i_ar_height;
439
440         i_ar_width = bs_read( &s, 8 );
441         i_ar_height= bs_read( &s, 8 );
442     }
443     if( bs_read1( &s ) )
444     {
445         int i_chroma_format;
446         int i_low_delay;
447
448         /* vol control parameter */
449         i_chroma_format = bs_read( &s, 2 );
450         i_low_delay = bs_read1( &s );
451
452         if( bs_read1( &s ) )
453         {
454             bs_skip( &s, 16 );
455             bs_skip( &s, 16 );
456             bs_skip( &s, 16 );
457             bs_skip( &s, 3 );
458             bs_skip( &s, 11 );
459             bs_skip( &s, 1 );
460             bs_skip( &s, 16 );
461         }
462     }
463     /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
464     i_shape = bs_read( &s, 2 );
465     if( i_shape == 3 && i_vo_ver_id != 1 )
466     {
467         bs_skip( &s, 4 );
468     }
469
470     if( !bs_read1( &s ) )
471     {
472         /* marker */
473         return VLC_EGENERIC;
474     }
475     i_time_increment_resolution = bs_read( &s, 16 );
476     if( !bs_read1( &s ) )
477     {
478         /* marker */
479         return VLC_EGENERIC;
480     }
481
482     if( bs_read1( &s ) )
483     {
484         int i_time_increment_bits = vlc_log2( i_time_increment_resolution - 1 ) + 1;
485         if( i_time_increment_bits < 1 )
486         {
487             i_time_increment_bits = 1;
488         }
489         bs_skip( &s, i_time_increment_bits );
490     }
491     if( i_shape == 0 )
492     {
493         bs_skip( &s, 1 );
494         fmt->video.i_width = bs_read( &s, 13 );
495         bs_skip( &s, 1 );
496         fmt->video.i_height= bs_read( &s, 13 );
497         bs_skip( &s, 1 );
498     }
499     return VLC_SUCCESS;
500 }