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