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