]> git.sesse.net Git - vlc/blobdiff - modules/demux/pva.c
MKV: the Block duration is for the whole Block, not each frame
[vlc] / modules / demux / pva.c
index d5ceafb6b9e1cb1e787231aace003026993154ef..4c9db0e480be679232ceced478b164c390525336 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * pva.c: PVA demuxer
  *****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -84,7 +84,7 @@ static int Open( vlc_object_t *p_this )
     es_format_t  fmt;
     const uint8_t *p_peek;
 
-    if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
+    if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
     {
         /* In case we had forced this demuxer we try to resynch */
@@ -92,16 +92,22 @@ static int Open( vlc_object_t *p_this )
             return VLC_EGENERIC;
     }
 
+    p_sys = malloc( sizeof( demux_sys_t ) );
+    if( unlikely(p_sys == NULL) )
+        return VLC_ENOMEM;
+
     /* Fill p_demux field */
     p_demux->pf_demux = Demux;
     p_demux->pf_control = Control;
-    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
+    p_demux->p_sys = p_sys;
 
     /* Register one audio and one video stream */
     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_MPGA );
+    fmt.b_packetized = false;
     p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
 
     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_MPGV );
+    fmt.b_packetized = false;
     p_sys->p_video = es_out_Add( p_demux->out, &fmt );
 
     p_sys->i_vc    = -1;
@@ -131,6 +137,8 @@ static void Close( vlc_object_t *p_this )
 
 /*****************************************************************************
  * Demux:
+ *****************************************************************************
+ * See http://multimedia.cx/mirror/av_format_v1.pdf
  *****************************************************************************/
 static int Demux( demux_t *p_demux )
 {
@@ -207,7 +215,7 @@ static int Demux( demux_t *p_demux )
                 if( ( p_frame = p_sys->p_es ) )
                 {
 
-                    if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
+                    if( p_frame->i_pts > VLC_TS_INVALID && !p_sys->b_pcr_audio )
                     {
                         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
                     }
@@ -221,7 +229,8 @@ static int Demux( demux_t *p_demux )
             {
                 p_frame->p_buffer += i_skip;
                 p_frame->i_buffer -= i_skip;
-                if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
+                if( i_pts >= 0 )
+                    p_frame->i_pts = VLC_TS_0 + i_pts * 100 / 9;
                 block_ChainAppend( &p_sys->p_es, p_frame );
             }
             break;
@@ -251,7 +260,7 @@ static int Demux( demux_t *p_demux )
                 p_frame->p_buffer += 8;
                 p_frame->i_buffer -= 8;
                 /* XXX this a hack, some streams aren't compliant and
-                 * doesn't set pes_start flag */
+                 * don't set pes_start flag */
                 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
                     p_frame->p_buffer[0] == 0x00 &&
                     p_frame->p_buffer[1] == 0x00 &&
@@ -285,7 +294,8 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
             {
                 pf = (double*) va_arg( args, double* );
-                *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
+                double current = stream_Tell( p_demux->s );
+                *pf = current / (double)i64;
                 return VLC_SUCCESS;
             }
             return VLC_EGENERIC;
@@ -339,33 +349,30 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
  *****************************************************************************/
 static int ReSynch( demux_t *p_demux )
 {
-    const uint8_t *p_peek;
-    int      i_skip;
-    int      i_peek;
-
-    while( vlc_object_alive (p_demux) )
+    for( ;; )
     {
-        if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
-        {
-            return VLC_EGENERIC;
-        }
-        i_skip = 0;
+        const uint8_t *p_peek;
+        int i_peek = stream_Peek( p_demux->s, &p_peek, 1024 );
+        if( i_peek < 8 )
+            break;
+
+        int i_skip = 0;
 
         while( i_skip < i_peek - 5 )
         {
             if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
             {
-                if( i_skip > 0 )
-                {
-                    stream_Read( p_demux->s, NULL, i_skip );
-                }
+                if( i_skip > 0
+                 && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
+                    return VLC_EGENERIC;
                 return VLC_SUCCESS;
             }
             p_peek++;
             i_skip++;
         }
 
-        stream_Read( p_demux->s, NULL, i_skip );
+        if( stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
+            break;
     }
 
     return VLC_EGENERIC;
@@ -376,9 +383,8 @@ static void ParsePES( demux_t *p_demux )
     demux_sys_t *p_sys = p_demux->p_sys;
     block_t     *p_pes = p_sys->p_pes;
     uint8_t     hdr[30];
-    int         i_pes_size;
 
-    int         i_skip;
+    unsigned    i_skip;
     mtime_t     i_dts = -1;
     mtime_t     i_pts = -1;
 
@@ -387,6 +393,7 @@ static void ParsePES( demux_t *p_demux )
     /* FIXME find real max size */
     block_ChainExtract( p_pes, hdr, 30 );
 
+    /* See ยง2.4.3.6 of ISO 13818-1 */
     if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
     {
         msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
@@ -394,7 +401,8 @@ static void ParsePES( demux_t *p_demux )
         block_ChainRelease( p_pes );
         return;
     }
-    i_pes_size = GetWBE( &hdr[4] );
+    // hdr[4] i_pes_size, 2 bytes
+    // hdr[6] Marker -> original_or_copy
 
     /* we assume mpeg2 PES */
     i_skip = hdr[8] + 9;
@@ -426,8 +434,10 @@ static void ParsePES( demux_t *p_demux )
     p_pes->i_buffer -= i_skip;
     p_pes->p_buffer += i_skip;
 
-    if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
-    if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
+    if( i_dts >= 0 )
+        p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
+    if( i_pts >= 0 )
+        p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
 
     /* Set PCR */
     if( p_pes->i_pts > 0 )