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