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