Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

mpeg4video.c

00001 /*****************************************************************************
00002  * mpeg4video.c: mpeg 4 video packetizer
00003  *****************************************************************************
00004  * Copyright (C) 2001, 2002 the VideoLAN team
00005  * $Id: mpeg4video.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *          Eric Petit <[email protected]>
00009  *          Gildas Bazin <[email protected]>
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027  * Preamble
00028  *****************************************************************************/
00029 #include <stdlib.h>                                      /* malloc(), free() */
00030 
00031 #include <vlc/vlc.h>
00032 #include <vlc/decoder.h>
00033 #include <vlc/sout.h>
00034 #include <vlc/input.h>                  /* hmmm, just for INPUT_RATE_DEFAULT */
00035 
00036 #include "vlc_bits.h"
00037 
00038 /*****************************************************************************
00039  * Module descriptor
00040  *****************************************************************************/
00041 static int  Open ( vlc_object_t * );
00042 static void Close( vlc_object_t * );
00043 
00044 vlc_module_begin();
00045     set_category( CAT_SOUT );
00046     set_subcategory( SUBCAT_SOUT_PACKETIZER );
00047     set_description( _("MPEG4 video packetizer") );
00048     set_capability( "packetizer", 50 );
00049     set_callbacks( Open, Close );
00050 vlc_module_end();
00051 
00052 
00053 /****************************************************************************
00054  * Local prototypes
00055  ****************************************************************************/
00056 static block_t *Packetize( decoder_t *, block_t ** );
00057 
00058 struct decoder_sys_t
00059 {
00060     /*
00061      * Common properties
00062      */
00063     mtime_t i_interpolated_pts;
00064     mtime_t i_interpolated_dts;
00065     mtime_t i_last_ref_pts;
00066     mtime_t i_last_time_ref;
00067     mtime_t i_time_ref;
00068     mtime_t i_last_time;
00069     mtime_t i_last_timeincr;
00070 
00071     vlc_bool_t  b_vop;
00072     int         i_buffer;
00073     int         i_buffer_size;
00074     uint8_t     *p_buffer;
00075     unsigned int i_flags;
00076 
00077     int         i_fps_num;
00078     int         i_fps_den;
00079     int         i_last_incr;
00080     int         i_last_incr_diff;
00081 
00082     vlc_bool_t  b_frame;
00083 };
00084 
00085 static int m4v_FindStartCode( uint8_t **, uint8_t * );
00086 static int m4v_VOLParse( decoder_t *, es_format_t *, uint8_t *, int );
00087 static int vlc_log2( unsigned int );
00088 
00089 #define VIDEO_OBJECT_MASK                       0x01f
00090 #define VIDEO_OBJECT_LAYER_MASK                 0x00f
00091 
00092 #define VIDEO_OBJECT_START_CODE                 0x100
00093 #define VIDEO_OBJECT_LAYER_START_CODE           0x120
00094 #define VISUAL_OBJECT_SEQUENCE_START_CODE       0x1b0
00095 #define VISUAL_OBJECT_SEQUENCE_END_CODE         0x1b1
00096 #define USER_DATA_START_CODE                    0x1b2
00097 #define GROUP_OF_VOP_START_CODE                 0x1b3
00098 #define VIDEO_SESSION_ERROR_CODE                0x1b4
00099 #define VISUAL_OBJECT_START_CODE                0x1b5
00100 #define VOP_START_CODE                          0x1b6
00101 #define FACE_OBJECT_START_CODE                  0x1ba
00102 #define FACE_OBJECT_PLANE_START_CODE            0x1bb
00103 #define MESH_OBJECT_START_CODE                  0x1bc
00104 #define MESH_OBJECT_PLANE_START_CODE            0x1bd
00105 #define STILL_TEXTURE_OBJECT_START_CODE         0x1be
00106 #define TEXTURE_SPATIAL_LAYER_START_CODE        0x1bf
00107 #define TEXTURE_SNR_LAYER_START_CODE            0x1c0
00108 
00109 /*****************************************************************************
00110  * Open: probe the packetizer and return score
00111  *****************************************************************************/
00112 static int Open( vlc_object_t *p_this )
00113 {
00114     decoder_t     *p_dec = (decoder_t*)p_this;
00115     decoder_sys_t *p_sys;
00116 
00117     switch( p_dec->fmt_in.i_codec )
00118     {
00119         case VLC_FOURCC( 'm', '4', 's', '2'):
00120         case VLC_FOURCC( 'M', '4', 'S', '2'):
00121         case VLC_FOURCC( 'm', 'p', '4', 's'):
00122         case VLC_FOURCC( 'M', 'P', '4', 'S'):
00123         case VLC_FOURCC( 'm', 'p', '4', 'v'):
00124         case VLC_FOURCC( 'M', 'P', '4', 'V'):
00125         case VLC_FOURCC( 'D', 'I', 'V', 'X'):
00126         case VLC_FOURCC( 'd', 'i', 'v', 'x'):
00127         case VLC_FOURCC( 'X', 'V', 'I', 'D'):
00128         case VLC_FOURCC( 'X', 'v', 'i', 'D'):
00129         case VLC_FOURCC( 'x', 'v', 'i', 'd'):
00130         case VLC_FOURCC( 'D', 'X', '5', '0'):
00131         case VLC_FOURCC( 'd', 'x', '5', '0'):
00132         case VLC_FOURCC( 0x04, 0,   0,   0):
00133         case VLC_FOURCC( '3', 'I', 'V', '2'):
00134         case VLC_FOURCC( 'm', '4', 'c', 'c'):
00135         case VLC_FOURCC( 'M', '4', 'C', 'C'):
00136             break;
00137 
00138         default:
00139             return VLC_EGENERIC;
00140     }
00141 
00142     /* Allocate the memory needed to store the decoder's structure */
00143     if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
00144     {
00145         msg_Err( p_dec, "out of memory" );
00146         return VLC_EGENERIC;
00147     }
00148     memset( p_sys, 0, sizeof(decoder_sys_t) );
00149 
00150     /* Setup properties */
00151     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
00152     p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
00153 
00154     if( p_dec->fmt_in.i_extra )
00155     {
00156         /* We have a vol */
00157         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
00158         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
00159         memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
00160                 p_dec->fmt_in.i_extra );
00161 
00162         msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
00163         m4v_VOLParse( p_dec, &p_dec->fmt_out,
00164                       p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
00165     }
00166     else
00167     {
00168         /* No vol, we'll have to look for one later on */
00169         p_dec->fmt_out.i_extra = 0;
00170         p_dec->fmt_out.p_extra = 0;
00171     }
00172 
00173     /* Set callback */
00174     p_dec->pf_packetize = Packetize;
00175 
00176     return VLC_SUCCESS;
00177 }
00178 
00179 /*****************************************************************************
00180  * Close: clean up the packetizer
00181  *****************************************************************************/
00182 static void Close( vlc_object_t *p_this )
00183 {
00184     decoder_t *p_dec = (decoder_t*)p_this;
00185 
00186     if( p_dec->p_sys->p_buffer ) free( p_dec->p_sys->p_buffer );
00187     free( p_dec->p_sys );
00188 }
00189 
00190 /****************************************************************************
00191  * Packetize: the whole thing
00192  ****************************************************************************/
00193 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
00194 {
00195     decoder_sys_t *p_sys = p_dec->p_sys;
00196 
00197     block_t *p_chain_out = NULL;
00198     block_t *p_block;
00199     uint8_t *p_vol = NULL;
00200     uint8_t *p_start;
00201 
00202     if( !pp_block || !*pp_block ) return NULL;
00203 
00204     p_block = *pp_block;
00205 
00206     /* Append data */
00207     if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
00208     {
00209         p_sys->i_buffer_size += p_block->i_buffer + 1024;
00210         p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
00211     }
00212     memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
00213             p_block->i_buffer );
00214     p_sys->i_buffer += p_block->i_buffer;
00215 
00216     if( p_sys->i_buffer > 10*1000000 )
00217     {
00218         msg_Warn( p_dec, "reseting context" );
00219         p_sys->i_buffer = 0;
00220     }
00221 
00222     /* Search vop */
00223     p_start = &p_sys->p_buffer[p_sys->i_buffer - p_block->i_buffer - 4];
00224     if( p_start < p_sys->p_buffer )
00225     {
00226         p_start = p_sys->p_buffer;
00227     }
00228     for( ;; )
00229     {
00230         if( m4v_FindStartCode( &p_start, &p_sys->p_buffer[p_sys->i_buffer] ) )
00231         {
00232             block_Release( p_block );
00233             *pp_block = NULL;
00234             return p_chain_out;
00235         }
00236         /* fprintf( stderr, "start code=0x1%2.2x\n", p_start[3] ); */
00237 
00238         if( p_vol )
00239         {
00240             if( !p_dec->fmt_out.i_extra )
00241             {
00242                 /* Copy the complete VOL */
00243                 p_dec->fmt_out.i_extra = p_start - p_vol;
00244                 p_dec->fmt_out.p_extra =
00245                     realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
00246                 memcpy( p_dec->fmt_out.p_extra, p_vol,
00247                         p_dec->fmt_out.i_extra );
00248                 m4v_VOLParse( p_dec, &p_dec->fmt_out,
00249                               p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
00250             }
00251 
00252             /* Remove VOL from the original stream */
00253             memmove( p_vol, p_start,
00254                      p_sys->i_buffer - (p_start - p_sys->p_buffer) );
00255             p_sys->i_buffer -= p_dec->fmt_out.i_extra;
00256             p_start = p_vol;
00257 
00258             p_vol = NULL;
00259         }
00260         if( p_sys->b_vop )
00261         {
00262             /* Output the complete VOP we have */
00263             int     i_out = p_start - p_sys->p_buffer;
00264             block_t *p_out = block_New( p_dec, i_out );
00265 
00266             /* extract data */
00267             memcpy( p_out->p_buffer, p_sys->p_buffer, i_out );
00268             if( i_out < p_sys->i_buffer )
00269             {
00270                 memmove( p_sys->p_buffer, &p_sys->p_buffer[i_out],
00271                          p_sys->i_buffer - i_out );
00272             }
00273             p_sys->i_buffer -= i_out;
00274             p_start -= i_out;
00275 
00276             p_out->i_flags = p_sys->i_flags;
00277             p_out->i_pts = p_sys->i_interpolated_pts;
00278             p_out->i_dts = p_sys->i_interpolated_dts;
00279 
00280             /* FIXME doesn't work when there is multiple VOP in one block */
00281             if( p_block->i_dts > p_sys->i_interpolated_dts )
00282             {
00283                 p_out->i_length = p_block->i_dts - p_sys->i_interpolated_dts;
00284             }
00285 
00286             if( p_dec->fmt_out.i_extra > 0 )
00287             {
00288                 block_ChainAppend( &p_chain_out, p_out );
00289             }
00290             else
00291             {
00292                 msg_Warn( p_dec, "waiting for VOL" );
00293                 block_Release( p_out );
00294             }
00295 
00296             p_sys->b_vop = VLC_FALSE;
00297         }
00298 
00299         if( p_start[3] >= 0x20 && p_start[3] <= 0x2f )
00300         {
00301             /* Start of the VOL */
00302             p_vol = p_start;
00303         }
00304         else if( p_start[3] == 0xb3 )
00305         {
00306             /* GOP header */
00307         }
00308         else if( p_start[3] == 0xb6 )
00309         {
00310             /* Parse the VOP */
00311             bs_t s;
00312             int i_modulo_time_base = 0;
00313             int i_time_increment_bits;
00314             int64_t i_time_increment, i_time_ref;
00315 
00316             /* FIXME: we don't actually check we received enough data to read
00317              * the VOP time increment. */
00318             bs_init( &s, &p_start[4],
00319                      p_sys->i_buffer - (p_start - p_sys->p_buffer) - 4 );
00320 
00321             switch( bs_read( &s, 2 ) )
00322             {
00323                 case 0:
00324                     p_sys->i_flags = BLOCK_FLAG_TYPE_I;
00325                     break;
00326                 case 1:
00327                     p_sys->i_flags = BLOCK_FLAG_TYPE_P;
00328                     break;
00329                 case 2:
00330                     p_sys->i_flags = BLOCK_FLAG_TYPE_B;
00331                     p_sys->b_frame = VLC_TRUE;
00332                     break;
00333                 case 3: /* gni ? */
00334                     p_sys->i_flags = BLOCK_FLAG_TYPE_PB;
00335                     break;
00336             }
00337 
00338             while( bs_read( &s, 1 ) ) i_modulo_time_base++;
00339             if( !bs_read1( &s ) ) continue; /* Marker */
00340 
00341             /* VOP time increment */
00342             i_time_increment_bits = vlc_log2(p_dec->p_sys->i_fps_num - 1) + 1;
00343             if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
00344             i_time_increment = bs_read( &s, i_time_increment_bits );
00345 
00346             /* Interpolate PTS/DTS */
00347             if( !(p_sys->i_flags & BLOCK_FLAG_TYPE_B) )
00348             {
00349                 p_sys->i_last_time_ref = p_sys->i_time_ref;
00350                 p_sys->i_time_ref +=
00351                     (i_modulo_time_base * p_dec->p_sys->i_fps_num);
00352                 i_time_ref = p_sys->i_time_ref;
00353             }
00354             else
00355             {
00356                 i_time_ref = p_sys->i_last_time_ref +
00357                     (i_modulo_time_base * p_dec->p_sys->i_fps_num);
00358             }
00359 
00360             if( p_dec->p_sys->i_fps_num < 5 && /* Work-around buggy streams */
00361                 p_dec->fmt_in.video.i_frame_rate > 0 &&
00362                 p_dec->fmt_in.video.i_frame_rate_base > 0 )
00363             {
00364                 p_sys->i_interpolated_pts += I64C(1000000) *
00365                     p_dec->fmt_in.video.i_frame_rate_base *
00366                     p_block->i_rate / INPUT_RATE_DEFAULT /
00367                     p_dec->fmt_in.video.i_frame_rate;
00368             }
00369             else if( p_dec->p_sys->i_fps_num )
00370                 p_sys->i_interpolated_pts +=
00371                     ( I64C(1000000) * (i_time_ref + i_time_increment -
00372                        p_sys->i_last_time - p_sys->i_last_timeincr) *
00373                       p_block->i_rate / INPUT_RATE_DEFAULT /
00374                       p_dec->p_sys->i_fps_num );
00375 
00376             p_sys->i_last_time = i_time_ref;
00377             p_sys->i_last_timeincr = i_time_increment;
00378 
00379             /* Correct interpolated dts when we receive a new pts/dts */
00380             if( p_block->i_pts > 0 )
00381                 p_sys->i_interpolated_pts = p_block->i_pts;
00382             if( p_block->i_dts > 0 )
00383                 p_sys->i_interpolated_dts = p_block->i_dts;
00384 
00385             if( (p_sys->i_flags & BLOCK_FLAG_TYPE_B) || !p_sys->b_frame )
00386             {
00387                 /* Trivial case (DTS == PTS) */
00388 
00389                 p_sys->i_interpolated_dts = p_sys->i_interpolated_pts;
00390 
00391                 if( p_block->i_pts > 0 )
00392                     p_sys->i_interpolated_dts = p_block->i_pts;
00393                 if( p_block->i_dts > 0 )
00394                     p_sys->i_interpolated_dts = p_block->i_dts;
00395 
00396                 p_sys->i_interpolated_pts = p_sys->i_interpolated_dts;
00397             }
00398             else
00399             {
00400                 if( p_sys->i_last_ref_pts > 0 )
00401                     p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
00402 
00403                 p_sys->i_last_ref_pts = p_sys->i_interpolated_pts;
00404             }
00405 
00406             p_sys->b_vop = VLC_TRUE;
00407 
00408             /* Don't re-use the same PTS/DTS twice */
00409             p_block->i_pts = p_block->i_dts = 0;
00410         }
00411 
00412         p_start += 4; /* Next */
00413     }
00414 }
00415 
00416 /****************************************************************************
00417  * m4v_FindStartCode
00418  ****************************************************************************/
00419 static int m4v_FindStartCode( uint8_t **pp_start, uint8_t *p_end )
00420 {
00421     uint8_t *p = *pp_start;
00422 
00423     /* We wait for 4+1 bytes */
00424     for( p = *pp_start; p < p_end - 5; p++ )
00425     {
00426         if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
00427         {
00428             *pp_start = p;
00429             return VLC_SUCCESS;
00430         }
00431     }
00432 
00433     *pp_start = p_end;
00434     return VLC_EGENERIC;
00435 }
00436 
00437 
00438 /* look at ffmpeg av_log2 ;) */
00439 static int vlc_log2( unsigned int v )
00440 {
00441     int n = 0;
00442     static const int vlc_log2_table[16] =
00443     {
00444         0,0,1,1,2,2,2,2, 3,3,3,3,3,3,3,3
00445     };
00446 
00447     if( v&0xffff0000 )
00448     {
00449         v >>= 16;
00450         n += 16;
00451     }
00452     if( v&0xff00 )
00453     {
00454         v >>= 8;
00455         n += 8;
00456     }
00457     if( v&0xf0 )
00458     {
00459         v >>= 4;
00460         n += 4;
00461     }
00462     n += vlc_log2_table[v];
00463 
00464     return n;
00465 }
00466 
00467 /* m4v_VOLParse:
00468  *  TODO:
00469  *      - support aspect ratio
00470  */
00471 static int m4v_VOLParse( decoder_t *p_dec, es_format_t *fmt,
00472                          uint8_t *p_vol, int i_vol )
00473 {
00474     bs_t s;
00475     int i_vo_type;
00476     int i_vo_ver_id;
00477     int i_ar;
00478     int i_shape;
00479 
00480     for( ;; )
00481     {
00482         if( p_vol[0] == 0x00 && p_vol[1] == 0x00 &&
00483             p_vol[2] == 0x01 &&
00484             p_vol[3] >= 0x20 && p_vol[3] <= 0x2f )
00485         {
00486             break;
00487         }
00488         p_vol++;
00489         i_vol--;
00490         if( i_vol <= 4 )
00491         {
00492             return VLC_EGENERIC;
00493         }
00494     }
00495 
00496     /* parse the vol */
00497     bs_init( &s, &p_vol[4], i_vol - 4 );
00498 
00499     bs_skip( &s, 1 );   /* random access */
00500     i_vo_type = bs_read( &s, 8 );
00501     if( bs_read1( &s ) )
00502     {
00503         i_vo_ver_id = bs_read( &s, 4 );
00504         bs_skip( &s, 3 );
00505     }
00506     else
00507     {
00508         i_vo_ver_id = 1;
00509     }
00510     i_ar = bs_read( &s, 4 );
00511     if( i_ar == 0xf )
00512     {
00513         int i_ar_width, i_ar_height;
00514 
00515         i_ar_width = bs_read( &s, 8 );
00516         i_ar_height= bs_read( &s, 8 );
00517     }
00518     if( bs_read1( &s ) )
00519     {
00520         int i_chroma_format;
00521         int i_low_delay;
00522 
00523         /* vol control parameter */
00524         i_chroma_format = bs_read( &s, 2 );
00525         i_low_delay = bs_read1( &s );
00526 
00527         if( bs_read1( &s ) )
00528         {
00529             bs_skip( &s, 16 );
00530             bs_skip( &s, 16 );
00531             bs_skip( &s, 16 );
00532             bs_skip( &s, 3 );
00533             bs_skip( &s, 11 );
00534             bs_skip( &s, 1 );
00535             bs_skip( &s, 16 );
00536         }
00537     }
00538     /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
00539     i_shape = bs_read( &s, 2 );
00540     if( i_shape == 3 && i_vo_ver_id != 1 )
00541     {
00542         bs_skip( &s, 4 );
00543     }
00544 
00545     if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
00546 
00547     p_dec->p_sys->i_fps_num = bs_read( &s, 16 ); /* Time increment resolution*/
00548     if( !p_dec->p_sys->i_fps_num ) p_dec->p_sys->i_fps_num = 1;
00549 
00550     if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
00551 
00552     if( bs_read1( &s ) )
00553     {
00554         int i_time_increment_bits =
00555             vlc_log2( p_dec->p_sys->i_fps_num - 1 ) + 1;
00556 
00557         if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
00558 
00559         p_dec->p_sys->i_fps_den = bs_read( &s, i_time_increment_bits );
00560     }
00561     if( i_shape == 0 )
00562     {
00563         bs_skip( &s, 1 );
00564         fmt->video.i_width = bs_read( &s, 13 );
00565         bs_skip( &s, 1 );
00566         fmt->video.i_height= bs_read( &s, 13 );
00567         bs_skip( &s, 1 );
00568     }
00569     return VLC_SUCCESS;
00570 }

Generated on Tue Dec 20 10:14:49 2005 for vlc-0.8.4a by  doxygen 1.4.2