ps.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * ps.h: Program Stream demuxer helper
00003  *****************************************************************************
00004  * Copyright (C) 2004-2009 the VideoLAN team
00005  * $Id: ae778d6914c26e15352cedea03a71a271a4a33a7 $
00006  *
00007  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #include <assert.h>
00025 #include <vlc_demux.h>
00026 #include <vlc_memory.h>
00027 
00028 /* 256-0xC0 for normal stream, 256 for 0xbd stream, 256 for 0xfd stream, 8 for 0xa0 AOB stream */
00029 #define PS_TK_COUNT (256+256+256+8 - 0xc0)
00030 #if 0
00031 #define PS_ID_TO_TK( id ) ((id) <= 0xff ? (id) - 0xc0 : \
00032             ((id)&0xff) + (((id)&0xff00) == 0xbd00 ? 256-0xC0 : 512-0xc0) )
00033 #else
00034 static inline int ps_id_to_tk( unsigned i_id )
00035 {
00036     if( i_id <= 0xff )
00037         return i_id - 0xc0;
00038     else if( (i_id & 0xff00) == 0xbd00 )
00039         return 256-0xC0 + (i_id & 0xff);
00040     else if( (i_id & 0xff00) == 0xfd00 )
00041         return 512-0xc0 + (i_id & 0xff);
00042     else
00043         return 768-0xc0 + (i_id & 0x07);
00044 }
00045 #define PS_ID_TO_TK( id ) ps_id_to_tk( id )
00046 #endif
00047 
00048 typedef struct ps_psm_t ps_psm_t;
00049 static inline int ps_id_to_type( const ps_psm_t *, int );
00050 static inline const uint8_t *ps_id_to_lang( const ps_psm_t *, int );
00051 
00052 typedef struct
00053 {
00054     bool  b_seen;
00055     int         i_skip;
00056     int         i_id;
00057     es_out_id_t *es;
00058     es_format_t fmt;
00059     mtime_t     i_first_pts;
00060     mtime_t     i_last_pts;
00061 
00062 } ps_track_t;
00063 
00064 /* Init a set of track */
00065 static inline void ps_track_init( ps_track_t tk[PS_TK_COUNT] )
00066 {
00067     int i;
00068     for( i = 0; i < PS_TK_COUNT; i++ )
00069     {
00070         tk[i].b_seen = false;
00071         tk[i].i_skip = 0;
00072         tk[i].i_id   = 0;
00073         tk[i].es     = NULL;
00074         tk[i].i_first_pts = -1;
00075         tk[i].i_last_pts = -1;
00076         es_format_Init( &tk[i].fmt, UNKNOWN_ES, 0 );
00077     }
00078 }
00079 
00080 /* From id fill i_skip and es_format_t */
00081 static inline int ps_track_fill( ps_track_t *tk, ps_psm_t *p_psm, int i_id )
00082 {
00083     tk->i_skip = 0;
00084     tk->i_id = i_id;
00085     if( ( i_id&0xff00 ) == 0xbd00 )
00086     {
00087         if( ( i_id&0xf8 ) == 0x88 || (i_id&0xf8) == 0x98 )
00088         {
00089             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_DTS );
00090             tk->i_skip = 4;
00091         }
00092         else if( ( i_id&0xf0 ) == 0x80
00093                ||  (i_id&0xf0) == 0xc0 ) /* AC-3, Can also be used for DD+/E-AC-3 */
00094         {
00095             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_A52 );
00096             tk->i_skip = 4;
00097         }
00098         else if( (i_id&0xf0) == 0xb0 )
00099         {
00100             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_MLP );
00101             /* FIXME / untested ... no known decoder (at least not in VLC/ffmpeg) */
00102         }
00103         else if( ( i_id&0xe0 ) == 0x20 )
00104         {
00105             es_format_Init( &tk->fmt, SPU_ES, VLC_CODEC_SPU );
00106             tk->i_skip = 1;
00107         }
00108         else if( ( i_id&0xf0 ) == 0xa0 )
00109         {
00110             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
00111             tk->i_skip = 1;
00112         }
00113         else if( ( i_id&0xff ) == 0x70 )
00114         {
00115             es_format_Init( &tk->fmt, SPU_ES, VLC_FOURCC('o','g','t',' ') );
00116         }
00117         else if( ( i_id&0xfc ) == 0x00 )
00118         {
00119             es_format_Init( &tk->fmt, SPU_ES, VLC_FOURCC('c','v','d',' ') );
00120         }
00121         else if( ( i_id&0xff ) == 0x10 )
00122         {
00123             es_format_Init( &tk->fmt, SPU_ES, VLC_CODEC_TELETEXT );
00124         }
00125         else
00126         {
00127             es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
00128             return VLC_EGENERIC;
00129         }
00130     }
00131     else if( (i_id&0xff00) == 0xfd00 )
00132     {
00133         uint8_t i_sub_id = i_id & 0xff;
00134         if( i_sub_id >= 0x55 && i_sub_id <= 0x5f )
00135         {
00136             es_format_Init( &tk->fmt, VIDEO_ES, VLC_CODEC_VC1 );
00137         }
00138         else
00139         {
00140             es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
00141             return VLC_EGENERIC;
00142         }
00143     }
00144     else if( (i_id&0xff00) == 0xa000 )
00145     {
00146         uint8_t i_sub_id = i_id & 0x07;
00147         if( i_sub_id == 0 )
00148         {
00149             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_DVDA_LPCM );
00150             tk->i_skip = 1;
00151         }
00152         else if( i_sub_id == 1 )
00153         {
00154             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_MLP );
00155             tk->i_skip = -1; /* It's a hack for variable skip value */
00156         }
00157         else
00158         {
00159             es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
00160             return VLC_EGENERIC;
00161         }
00162     }
00163     else
00164     {
00165         int i_type = ps_id_to_type( p_psm , i_id );
00166 
00167         es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
00168 
00169         if( (i_id&0xf0) == 0xe0 && i_type == 0x1b )
00170         {
00171             es_format_Init( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 );
00172         }
00173         else if( (i_id&0xf0) == 0xe0 && i_type == 0x10 )
00174         {
00175             es_format_Init( &tk->fmt, VIDEO_ES, VLC_CODEC_MP4V );
00176         }
00177         else if( (i_id&0xf0) == 0xe0 && i_type == 0x02 )
00178         {
00179             es_format_Init( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
00180         }
00181         else if( ( i_id&0xe0 ) == 0xc0 && i_type == 0x0f )
00182         {
00183             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A );
00184         }
00185         else if( ( i_id&0xe0 ) == 0xc0 && i_type == 0x11 )
00186         {
00187             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A );
00188         }
00189         else if( ( i_id&0xe0 ) == 0xc0 && i_type == 0x03 )
00190         {
00191             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA );
00192         }
00193 
00194         if( tk->fmt.i_cat == UNKNOWN_ES && ( i_id&0xf0 ) == 0xe0 )
00195         {
00196             es_format_Init( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
00197         }
00198         else if( tk->fmt.i_cat == UNKNOWN_ES && ( i_id&0xe0 ) == 0xc0 )
00199         {
00200             es_format_Init( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA );
00201         }
00202         else if( tk->fmt.i_cat == UNKNOWN_ES ) return VLC_EGENERIC;
00203     }
00204 
00205     /* PES packets usually contain truncated frames */
00206     tk->fmt.b_packetized = false;
00207 
00208     if( ps_id_to_lang( p_psm, i_id ) )
00209     {
00210         tk->fmt.psz_language = malloc( 4 );
00211         if( tk->fmt.psz_language )
00212         {
00213             memcpy( tk->fmt.psz_language, ps_id_to_lang( p_psm , i_id ), 3 );
00214             tk->fmt.psz_language[3] = 0;
00215         }
00216     }
00217 
00218     return VLC_SUCCESS;
00219 }
00220 
00221 /* return the id of a PES (should be valid) */
00222 static inline int ps_pkt_id( block_t *p_pkt )
00223 {
00224     if( p_pkt->p_buffer[3] == 0xbd &&
00225         p_pkt->i_buffer >= 9 &&
00226         p_pkt->i_buffer >= 9 + (size_t)p_pkt->p_buffer[8] )
00227     {
00228         const unsigned i_start = 9 + p_pkt->p_buffer[8];
00229         const uint8_t i_sub_id = p_pkt->p_buffer[i_start];
00230 
00231         if( (i_sub_id & 0xfe) == 0xa0 &&
00232             p_pkt->i_buffer >= i_start + 7 &&
00233             ( p_pkt->p_buffer[i_start + 5] >=  0xc0 ||
00234               p_pkt->p_buffer[i_start + 6] != 0x80 ) )
00235         {
00236             /* AOB LPCM/MLP extension
00237              * XXX for MLP I think that the !=0x80 test is not good and
00238              * will fail for some valid files */
00239             return 0xa000 | (i_sub_id & 0x01);
00240         }
00241 
00242         /* VOB extension */
00243         return 0xbd00 | i_sub_id;
00244     }
00245     else if( p_pkt->p_buffer[3] == 0xfd &&
00246              p_pkt->i_buffer >= 9 &&
00247              (p_pkt->p_buffer[6]&0xC0) == 0x80 &&   /* mpeg2 */
00248              (p_pkt->p_buffer[7]&0x01) == 0x01 )    /* extension_flag */
00249     {
00250         /* ISO 13818 amendment 2 and SMPTE RP 227 */
00251         const uint8_t i_flags = p_pkt->p_buffer[7];
00252         unsigned int i_skip = 9;
00253 
00254         /* Find PES extension */
00255         if( (i_flags & 0x80 ) )
00256         {
00257             i_skip += 5;        /* pts */
00258             if( (i_flags & 0x40) )
00259                 i_skip += 5;    /* dts */
00260         }
00261         if( (i_flags & 0x20 ) )
00262             i_skip += 6;
00263         if( (i_flags & 0x10 ) )
00264             i_skip += 3;
00265         if( (i_flags & 0x08 ) )
00266             i_skip += 1;
00267         if( (i_flags & 0x04 ) )
00268             i_skip += 1;
00269         if( (i_flags & 0x02 ) )
00270             i_skip += 2;
00271 
00272         if( i_skip < p_pkt->i_buffer && (p_pkt->p_buffer[i_skip]&0x01) )
00273         {
00274             const uint8_t i_flags2 = p_pkt->p_buffer[i_skip];
00275 
00276             /* Find PES extension 2 */
00277             i_skip += 1;
00278             if( i_flags2 & 0x80 )
00279                 i_skip += 16;
00280             if( (i_flags2 & 0x40) && i_skip < p_pkt->i_buffer )
00281                 i_skip += 1 + p_pkt->p_buffer[i_skip];
00282             if( i_flags2 & 0x20 )
00283                 i_skip += 2;
00284             if( i_flags2 & 0x10 )
00285                 i_skip += 2;
00286 
00287             if( i_skip + 1 < p_pkt->i_buffer )
00288             {
00289                 const int i_extension_field_length = p_pkt->p_buffer[i_skip]&0x7f;
00290                 if( i_extension_field_length >=1 )
00291                 {
00292                     int i_stream_id_extension_flag = (p_pkt->p_buffer[i_skip+1] >> 7)&0x1;
00293                     if( i_stream_id_extension_flag == 0 )
00294                         return 0xfd00 | (p_pkt->p_buffer[i_skip+1]&0x7f);
00295                 }
00296             }
00297         }
00298     }
00299     return p_pkt->p_buffer[3];
00300 }
00301 
00302 /* return the size of the next packet
00303  * You need to give him at least 14 bytes (and it need to start as a
00304  * valid packet) It does not handle less than 6 bytes */
00305 static inline int ps_pkt_size( const uint8_t *p, int i_peek )
00306 {
00307     assert( i_peek >= 6 );
00308     if( p[3] == 0xb9 && i_peek >= 4 )
00309     {
00310         return 4;
00311     }
00312     else if( p[3] == 0xba )
00313     {
00314         if( (p[4] >> 6) == 0x01 && i_peek >= 14 )
00315         {
00316             return 14 + (p[13]&0x07);
00317         }
00318         else if( (p[4] >> 4) == 0x02 && i_peek >= 12 )
00319         {
00320             return 12;
00321         }
00322         return -1;
00323     }
00324     else if( i_peek >= 6 )
00325     {
00326         return 6 + ((p[4]<<8) | p[5] );
00327     }
00328     return -1;
00329 }
00330 
00331 /* parse a PACK PES */
00332 static inline int ps_pkt_parse_pack( block_t *p_pkt, int64_t *pi_scr,
00333                                      int *pi_mux_rate )
00334 {
00335     uint8_t *p = p_pkt->p_buffer;
00336     if( p_pkt->i_buffer >= 14 && (p[4] >> 6) == 0x01 )
00337     {
00338         *pi_scr =((((int64_t)p[4]&0x38) << 27 )|
00339                   (((int64_t)p[4]&0x03) << 28 )|
00340                    ((int64_t)p[5] << 20 )|
00341                   (((int64_t)p[6]&0xf8) << 12 )|
00342                   (((int64_t)p[6]&0x03) << 13 )|
00343                    ((int64_t)p[7] << 5 )|
00344                    ((int64_t)p[8] >> 3 )) * 100 / 9;
00345 
00346         *pi_mux_rate = ( p[10] << 14 )|( p[11] << 6 )|( p[12] >> 2);
00347     }
00348     else if( p_pkt->i_buffer >= 12 && (p[4] >> 4) == 0x02 )
00349     {
00350         *pi_scr =((((int64_t)p[4]&0x0e) << 29 )|
00351                    ((int64_t)p[5] << 22 )|
00352                   (((int64_t)p[6]&0xfe) << 14 )|
00353                    ((int64_t)p[7] <<  7 )|
00354                    ((int64_t)p[8] >> 1 )) * 100 / 9;
00355 
00356         *pi_mux_rate = ( ( p[9]&0x7f )<< 15 )|( p[10] << 7 )|( p[11] >> 1);
00357     }
00358     else
00359     {
00360         return VLC_EGENERIC;
00361     }
00362     return VLC_SUCCESS;
00363 }
00364 
00365 /* Parse a SYSTEM PES */
00366 static inline int ps_pkt_parse_system( block_t *p_pkt, ps_psm_t *p_psm,
00367                                        ps_track_t tk[PS_TK_COUNT] )
00368 {
00369     uint8_t *p = &p_pkt->p_buffer[6 + 3 + 1 + 1 + 1];
00370 
00371     /* System header is not useable if it references private streams (0xBD)
00372      * or 'all audio streams' (0xB8) or 'all video streams' (0xB9) */
00373     while( p < &p_pkt->p_buffer[p_pkt->i_buffer] )
00374     {
00375         int i_id = p[0];
00376 
00377         /* fprintf( stderr, "   SYSTEM_START_CODEEE: id=0x%x\n", p[0] ); */
00378         if( p[0] >= 0xBC || p[0] == 0xB8 || p[0] == 0xB9 ) p += 2;
00379         p++;
00380 
00381         if( i_id >= 0xc0 )
00382         {
00383             int i_tk = PS_ID_TO_TK( i_id );
00384 
00385             if( !tk[i_tk].b_seen )
00386             {
00387                 if( !ps_track_fill( &tk[i_tk], p_psm, i_id ) )
00388                 {
00389                     tk[i_tk].b_seen = true;
00390                 }
00391             }
00392         }
00393     }
00394     return VLC_SUCCESS;
00395 }
00396 
00397 /* Parse a PES (and skip i_skip_extra in the payload) */
00398 static inline int ps_pkt_parse_pes( block_t *p_pes, int i_skip_extra )
00399 {
00400     uint8_t header[34];
00401     unsigned int i_skip  = 0;
00402     int64_t i_pts = -1;
00403     int64_t i_dts = -1;
00404 
00405     memcpy( header, p_pes->p_buffer, __MIN( p_pes->i_buffer, 34 ) );
00406 
00407     switch( header[3] )
00408     {
00409         case 0xBC:  /* Program stream map */
00410         case 0xBE:  /* Padding */
00411         case 0xBF:  /* Private stream 2 */
00412         case 0xB0:  /* ECM */
00413         case 0xB1:  /* EMM */
00414         case 0xFF:  /* Program stream directory */
00415         case 0xF2:  /* DSMCC stream */
00416         case 0xF8:  /* ITU-T H.222.1 type E stream */
00417             i_skip = 6;
00418             break;
00419 
00420         default:
00421             if( ( header[6]&0xC0 ) == 0x80 )
00422             {
00423                 /* mpeg2 PES */
00424                 i_skip = header[8] + 9;
00425 
00426                 if( header[7]&0x80 )    /* has pts */
00427                 {
00428                     i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
00429                              (mtime_t)(header[10] << 22)|
00430                             ((mtime_t)(header[11]&0xfe) << 14)|
00431                              (mtime_t)(header[12] << 7)|
00432                              (mtime_t)(header[13] >> 1);
00433 
00434                     if( header[7]&0x40 )    /* has dts */
00435                     {
00436                          i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
00437                                   (mtime_t)(header[15] << 22)|
00438                                  ((mtime_t)(header[16]&0xfe) << 14)|
00439                                   (mtime_t)(header[17] << 7)|
00440                                   (mtime_t)(header[18] >> 1);
00441                     }
00442                 }
00443             }
00444             else
00445             {
00446                 i_skip = 6;
00447                 while( i_skip < 23 && header[i_skip] == 0xff )
00448                 {
00449                     i_skip++;
00450                 }
00451                 if( i_skip == 23 )
00452                 {
00453                     /* msg_Err( p_demux, "too much MPEG-1 stuffing" ); */
00454                     return VLC_EGENERIC;
00455                 }
00456                 if( ( header[i_skip] & 0xC0 ) == 0x40 )
00457                 {
00458                     i_skip += 2;
00459                 }
00460 
00461                 if(  header[i_skip]&0x20 )
00462                 {
00463                      i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
00464                               (mtime_t)(header[i_skip+1] << 22)|
00465                              ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
00466                               (mtime_t)(header[i_skip+3] << 7)|
00467                               (mtime_t)(header[i_skip+4] >> 1);
00468 
00469                     if( header[i_skip]&0x10 )    /* has dts */
00470                     {
00471                          i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
00472                                   (mtime_t)(header[i_skip+6] << 22)|
00473                                  ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
00474                                   (mtime_t)(header[i_skip+8] << 7)|
00475                                   (mtime_t)(header[i_skip+9] >> 1);
00476                          i_skip += 10;
00477                     }
00478                     else
00479                     {
00480                         i_skip += 5;
00481                     }
00482                 }
00483                 else
00484                 {
00485                     i_skip += 1;
00486                 }
00487             }
00488     }
00489 
00490     if( i_skip_extra >= 0 )
00491         i_skip += i_skip_extra;
00492     else if( p_pes->i_buffer > i_skip + 3 &&
00493              ( ps_pkt_id( p_pes ) == 0xa001 || ps_pkt_id( p_pes ) == 0xbda1 ) )
00494         i_skip += 4 + p_pes->p_buffer[i_skip+3];
00495 
00496     if( p_pes->i_buffer <= i_skip )
00497     {
00498         return VLC_EGENERIC;
00499     }
00500 
00501     p_pes->p_buffer += i_skip;
00502     p_pes->i_buffer -= i_skip;
00503 
00504     if( i_dts >= 0 )
00505         p_pes->i_dts = VLC_TS_0 + 100 * i_dts / 9;
00506     if( i_pts >= 0 )
00507         p_pes->i_pts = VLC_TS_0 + 100 * i_pts / 9;
00508 
00509     return VLC_SUCCESS;
00510 }
00511 
00512 /* Program stream map handling */
00513 typedef struct ps_es_t
00514 {
00515     int i_type;
00516     int i_id;
00517 
00518     int i_descriptor;
00519     uint8_t *p_descriptor;
00520 
00521     /* Language is iso639-2T */
00522     uint8_t lang[3];
00523 
00524 } ps_es_t;
00525 
00526 struct ps_psm_t
00527 {
00528     int i_version;
00529 
00530     int     i_es;
00531     ps_es_t **es;
00532 };
00533 
00534 static inline int ps_id_to_type( const ps_psm_t *p_psm, int i_id )
00535 {
00536     int i;
00537     for( i = 0; p_psm && i < p_psm->i_es; i++ )
00538     {
00539         if( p_psm->es[i]->i_id == i_id ) return p_psm->es[i]->i_type;
00540     }
00541     return 0;
00542 }
00543 
00544 static inline const uint8_t *ps_id_to_lang( const ps_psm_t *p_psm, int i_id )
00545 {
00546     int i;
00547     for( i = 0; p_psm && i < p_psm->i_es; i++ )
00548     {
00549         if( p_psm->es[i]->i_id == i_id ) return p_psm->es[i]->lang;
00550     }
00551     return 0;
00552 }
00553 
00554 static inline void ps_psm_init( ps_psm_t *p_psm )
00555 {
00556     p_psm->i_version = 0xFFFF;
00557     p_psm->i_es = 0;
00558     p_psm->es = 0;
00559 }
00560 
00561 static inline void ps_psm_destroy( ps_psm_t *p_psm )
00562 {
00563     while( p_psm->i_es-- )
00564     {
00565         free( p_psm->es[p_psm->i_es]->p_descriptor );
00566         free( p_psm->es[p_psm->i_es] );
00567     }
00568     free( p_psm->es );
00569 
00570     p_psm->es = 0;
00571     p_psm->i_es = 0;
00572 }
00573 
00574 static inline int ps_psm_fill( ps_psm_t *p_psm, block_t *p_pkt,
00575                                ps_track_t tk[PS_TK_COUNT], es_out_t *out )
00576 {
00577     int i_buffer = p_pkt->i_buffer;
00578     uint8_t *p_buffer = p_pkt->p_buffer;
00579     int i_length, i_version, i_info_length, i_esm_length, i_es_base;
00580 
00581     if( !p_psm || p_buffer[3] != 0xbc ) return VLC_EGENERIC;
00582 
00583     i_length = (uint16_t)(p_buffer[4] << 8) + p_buffer[5] + 6;
00584     if( i_length > i_buffer ) return VLC_EGENERIC;
00585 
00586     //i_current_next_indicator = (p_buffer[6] && 0x01);
00587     i_version = (p_buffer[6] && 0xf8);
00588 
00589     if( p_psm->i_version == i_version ) return VLC_EGENERIC;
00590 
00591     ps_psm_destroy( p_psm );
00592 
00593     i_info_length = (uint16_t)(p_buffer[8] << 8) + p_buffer[9];
00594     if( i_info_length + 10 > i_length ) return VLC_EGENERIC;
00595 
00596     /* Elementary stream map */
00597     i_esm_length = (uint16_t)(p_buffer[ 10 + i_info_length ] << 8) +
00598         p_buffer[ 11 + i_info_length];
00599     i_es_base = 12 + i_info_length;
00600 
00601     while( i_es_base + 4 < i_length )
00602     {
00603         ps_es_t **tmp_es;
00604         ps_es_t es;
00605         es.lang[0] = es.lang[1] = es.lang[2] = 0;
00606 
00607         es.i_type = p_buffer[ i_es_base  ];
00608         es.i_id = p_buffer[ i_es_base + 1 ];
00609         i_info_length = (uint16_t)(p_buffer[ i_es_base + 2 ] << 8) +
00610             p_buffer[ i_es_base + 3 ];
00611 
00612         if( i_es_base + 4 + i_info_length > i_length ) break;
00613 
00614         /* TODO Add support for VC-1 stream:
00615          *      stream_type=0xea, stream_id=0xfd AND registration
00616          *      descriptor 0x5 with format_identifier == 0x56432D31 (VC-1)
00617          *      (I need a sample that use PSM with VC-1) */
00618 
00619         es.p_descriptor = 0;
00620         es.i_descriptor = i_info_length;
00621         if( i_info_length > 0 )
00622         {
00623             int i = 0;
00624 
00625             es.p_descriptor = malloc( i_info_length );
00626             if( es.p_descriptor )
00627             {
00628                 memcpy( es.p_descriptor, p_buffer + i_es_base + 4, i_info_length);
00629 
00630                 while( i <= es.i_descriptor - 2 )
00631                 {
00632                     /* Look for the ISO639 language descriptor */
00633                     if( es.p_descriptor[i] != 0x0a )
00634                     {
00635                         i += es.p_descriptor[i+1] + 2;
00636                         continue;
00637                     }
00638 
00639                     if( i <= es.i_descriptor - 6 )
00640                     {
00641                         es.lang[0] = es.p_descriptor[i+2];
00642                         es.lang[1] = es.p_descriptor[i+3];
00643                         es.lang[2] = es.p_descriptor[i+4];
00644                     }
00645                     break;
00646                 }
00647             }
00648         }
00649 
00650         tmp_es = realloc_or_free( p_psm->es, sizeof(ps_es_t *) * (p_psm->i_es+1) );
00651         if( tmp_es )
00652         {
00653             p_psm->es = tmp_es;
00654             p_psm->es[p_psm->i_es] = malloc( sizeof(ps_es_t) );
00655             if( p_psm->es[p_psm->i_es] )
00656             {
00657                 *p_psm->es[p_psm->i_es++] = es;
00658                 i_es_base += 4 + i_info_length;
00659             }
00660         }
00661     }
00662 
00663     /* TODO: CRC */
00664 
00665     p_psm->i_version = i_version;
00666 
00667     /* Check/Modify our existing tracks */
00668     for( int i = 0; i < PS_TK_COUNT; i++ )
00669     {
00670         ps_track_t tk_tmp;
00671 
00672         if( !tk[i].b_seen || !tk[i].es ) continue;
00673 
00674         if( ps_track_fill( &tk_tmp, p_psm, tk[i].i_id ) != VLC_SUCCESS )
00675             continue;
00676 
00677         if( tk_tmp.fmt.i_codec == tk[i].fmt.i_codec )
00678         {
00679             es_format_Clean( &tk_tmp.fmt );
00680             continue;
00681         }
00682 
00683         es_out_Del( out, tk[i].es );
00684         es_format_Clean( &tk[i].fmt );
00685 
00686         tk[i] = tk_tmp;
00687         tk[i].b_seen = true;
00688         tk[i].es = es_out_Add( out, &tk[i].fmt );
00689     }
00690 
00691     return VLC_SUCCESS;
00692 }

Generated on Tue May 25 08:04:56 2010 for VLC by  doxygen 1.5.6