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