]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4video.c
* modules/codec/ffmpeg/*: fixed small memleak in chroma plugin.
[vlc] / modules / packetizer / mpeg4video.c
1 /*****************************************************************************
2  * mpeg4video.c: mpeg 4 video packetizer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: mpeg4video.c,v 1.17 2003/11/26 08:18:09 gbazin Exp $
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 };
69
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 );
72
73 #define VIDEO_OBJECT_MASK                       0x01f
74 #define VIDEO_OBJECT_LAYER_MASK                 0x00f
75
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
92
93 /*****************************************************************************
94  * Open: probe the packetizer and return score
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     decoder_t     *p_dec = (decoder_t*)p_this;
99     decoder_sys_t *p_sys;
100
101     switch( p_dec->fmt_in.i_codec )
102     {
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( 0x04, 0,   0,   0):
115         case VLC_FOURCC( '3', 'I', 'V', '2'):
116             break;
117
118         default:
119             return VLC_EGENERIC;
120     }
121
122     /* Allocate the memory needed to store the decoder's structure */
123     if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
124     {
125         msg_Err( p_dec, "out of memory" );
126         return VLC_EGENERIC;
127     }
128     p_sys->i_pts = 0;
129     p_sys->b_vop = VLC_FALSE;
130     p_sys->i_buffer = 0;
131     p_sys->i_buffer_size = 0;
132     p_sys->p_buffer = 0;
133
134     /* Setup properties */
135     p_dec->fmt_out = p_dec->fmt_in;
136     p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
137
138     if( p_dec->fmt_in.i_extra )
139     {
140         /* We have a vol */
141         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
142         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
143         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
144                 p_dec->fmt_in.i_extra );
145
146         msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
147         m4v_VOLParse( &p_dec->fmt_out,
148                       p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
149     }
150     else
151     {
152         /* No vol, we'll have to look for one later on */
153         p_dec->fmt_out.i_extra = 0;
154         p_dec->fmt_out.p_extra = 0;
155     }
156
157     /* Set callback */
158     p_dec->pf_packetize = Packetize;
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * Close: clean up the packetizer
165  *****************************************************************************/
166 static void Close( vlc_object_t *p_this )
167 {
168     decoder_t *p_dec = (decoder_t*)p_this;
169
170     if( p_dec->p_sys->p_buffer ) free( p_dec->p_sys->p_buffer );
171     free( p_dec->p_sys );
172 }
173
174 /****************************************************************************
175  * Packetize: the whole thing
176  ****************************************************************************/
177 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
178 {
179     decoder_sys_t *p_sys = p_dec->p_sys;
180
181     block_t *p_chain_out = NULL;
182     block_t *p_block;
183     uint8_t *p_vol = NULL;
184     uint8_t *p_start;
185
186     if( !pp_block || !*pp_block ) return NULL;
187
188     p_block = *pp_block;
189
190     /* Append data */
191     if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
192     {
193         p_sys->i_buffer_size += p_block->i_buffer + 1024;
194         p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
195     }
196     memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
197             p_block->i_buffer );
198     p_sys->i_buffer += p_block->i_buffer;
199
200     if( p_sys->i_buffer > 10*1000000 )
201     {
202         msg_Err( p_dec, "mmh reseting context" );
203         p_sys->i_buffer = 0;
204     }
205
206     /* Search vop */
207     p_start = &p_sys->p_buffer[p_sys->i_buffer - p_block->i_buffer - 4];
208     if( p_start < p_sys->p_buffer )
209     {
210         p_start = p_sys->p_buffer;
211     }
212     for( ;; )
213     {
214         if( m4v_FindStartCode( &p_start, &p_sys->p_buffer[p_sys->i_buffer] ) )
215         {
216             block_Release( p_block );
217             *pp_block = NULL;
218             return p_chain_out;
219         }
220         /* fprintf( stderr, "start code=0x1%2.2x\n", p_start[3] ); */
221
222         if( p_vol )
223         {
224             /* Copy the complete VOL */
225             p_dec->fmt_out.i_extra = p_start - p_vol;
226             p_dec->fmt_out.p_extra = malloc( p_dec->fmt_out.i_extra );
227             memcpy( p_dec->fmt_out.p_extra, p_vol, p_dec->fmt_out.i_extra );
228             m4v_VOLParse( &p_dec->fmt_out,
229                           p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
230
231             p_vol = NULL;
232         }
233         if( p_sys->b_vop )
234         {
235             /* Output the complete VOP we have */
236             int     i_out = p_start - p_sys->p_buffer;
237             block_t *p_out = block_New( p_dec, i_out );
238
239             /* extract data */
240             memcpy( p_out->p_buffer, p_sys->p_buffer, i_out );
241             if( i_out < p_sys->i_buffer )
242             {
243                 memmove( p_sys->p_buffer, &p_sys->p_buffer[i_out],
244                          p_sys->i_buffer - i_out );
245             }
246             p_sys->i_buffer -= i_out;
247             p_start -= i_out;
248
249             /* FIXME do proper dts/pts */
250             p_out->i_pts = p_sys->i_pts;
251             p_out->i_dts = p_sys->i_dts;
252             /* FIXME doesn't work when there is multiple VOP in one block */
253             if( p_block->i_dts > p_sys->i_dts )
254             {
255                 p_out->i_length = p_block->i_dts - p_sys->i_dts;
256             }
257
258             if( p_dec->fmt_out.i_extra > 0 )
259             {
260                 block_ChainAppend( &p_chain_out, p_out );
261             }
262             else
263             {
264                 msg_Warn( p_dec, "waiting for VOL" );
265                 block_Release( p_out );
266             }
267
268 #if 0
269             fprintf( stderr, "pts=%lld dts=%lld length=%lldms\n",
270                      p_out->i_pts, p_out->i_dts,
271                      p_out->i_length / 1000 );
272 #endif
273             p_sys->b_vop = VLC_FALSE;
274         }
275
276         if( p_start[3] >= 0x20 && p_start[3] <= 0x2f )
277         {
278             /* Start of the VOL */
279             p_vol = p_start;
280         }
281         else if( p_start[3] == 0xb6 )
282         {
283             p_sys->b_vop = VLC_TRUE;
284             p_sys->i_pts = p_block->i_pts;
285             p_sys->i_dts = p_block->i_dts;
286         }
287         p_start += 4; /* Next */
288     }
289 }
290
291 /****************************************************************************
292  * m4v_FindStartCode
293  ****************************************************************************/
294 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end )
295 {
296     uint8_t *p = *pp_start;
297
298     for( p = *pp_start; p < p_end - 4; p++ )
299     {
300         if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
301         {
302             *pp_start = p;
303             return VLC_SUCCESS;
304         }
305     }
306
307     *pp_start = p_end;
308     return VLC_EGENERIC;
309 }
310
311
312 /* look at ffmpeg av_log2 ;) */
313 static int vlc_log2( unsigned int v )
314 {
315     int n = 0;
316     static const int vlc_log2_table[16] =
317     {
318         0,0,1,1,2,2,2,2, 3,3,3,3,3,3,3,3
319     };
320
321     if( v&0xffff0000 )
322     {
323         v >>= 16;
324         n += 16;
325     }
326     if( v&0xff00 )
327     {
328         v >>= 8;
329         n += 8;
330     }
331     if( v&0xf0 )
332     {
333         v >>= 4;
334         n += 4;
335     }
336     n += vlc_log2_table[v];
337
338     return n;
339 }
340
341 /* m4v_VOLParse:
342  *  TODO:
343  *      - support aspect ratio
344  */
345 static int m4v_VOLParse( es_format_t *fmt, uint8_t *p_vol, int i_vol )
346 {
347     bs_t s;
348     int i_vo_type;
349     int i_vo_ver_id;
350     int i_ar;
351     int i_shape;
352     int i_time_increment_resolution;
353
354     for( ;; )
355     {
356         if( p_vol[0] == 0x00 && p_vol[1] == 0x00 &&
357             p_vol[2] == 0x01 &&
358             p_vol[3] >= 0x20 && p_vol[3] <= 0x2f )
359         {
360             break;
361         }
362         p_vol++;
363         i_vol--;
364         if( i_vol <= 4 )
365         {
366             return VLC_EGENERIC;
367         }
368     }
369
370     /* parse the vol */
371     bs_init( &s, &p_vol[4], i_vol - 4 );
372
373     bs_skip( &s, 1 );   /* random access */
374     i_vo_type = bs_read( &s, 8 );
375     if( bs_read1( &s ) )
376     {
377         i_vo_ver_id = bs_read( &s, 4 );
378         bs_skip( &s, 3 );
379     }
380     else
381     {
382         i_vo_ver_id = 1;
383     }
384     i_ar = bs_read( &s, 4 );
385     if( i_ar == 0xf )
386     {
387         int i_ar_width = bs_read( &s, 8 );
388         int i_ar_height= bs_read( &s, 8 );
389     }
390     if( bs_read1( &s ) )
391     {
392         /* vol control parameter */
393         int i_chroma_format = bs_read( &s, 2 );
394         int i_low_delay = bs_read1( &s );
395
396         if( bs_read1( &s ) )
397         {
398             bs_skip( &s, 16 );
399             bs_skip( &s, 16 );
400             bs_skip( &s, 16 );
401             bs_skip( &s, 3 );
402             bs_skip( &s, 11 );
403             bs_skip( &s, 1 );
404             bs_skip( &s, 16 );
405         }
406     }
407     /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
408     i_shape = bs_read( &s, 2 );
409     if( i_shape == 3 && i_vo_ver_id != 1 )
410     {
411         bs_skip( &s, 4 );
412     }
413
414     if( !bs_read1( &s ) )
415     {
416         /* marker */
417         return VLC_EGENERIC;
418     }
419     i_time_increment_resolution = bs_read( &s, 16 );
420     if( !bs_read1( &s ) )
421     {
422         /* marker */
423         return VLC_EGENERIC;
424     }
425
426     if( bs_read1( &s ) )
427     {
428         int i_time_increment_bits = vlc_log2( i_time_increment_resolution - 1 ) + 1;
429         if( i_time_increment_bits < 1 )
430         {
431             i_time_increment_bits = 1;
432         }
433         bs_skip( &s, i_time_increment_bits );
434     }
435     if( i_shape == 0 )
436     {
437         bs_skip( &s, 1 );
438         fmt->video.i_width = bs_read( &s, 13 );
439         bs_skip( &s, 1 );
440         fmt->video.i_height= bs_read( &s, 13 );
441         bs_skip( &s, 1 );
442     }
443     return VLC_SUCCESS;
444 }
445
446
447