]> git.sesse.net Git - vlc/blobdiff - src/video_parser/vpar_headers.c
The input-II. (more info by mail in about an hour)
[vlc] / src / video_parser / vpar_headers.c
index dec30d21dc32e536186c8d03f49d6d4d3f4f6a0e..4cf469ad5389445ab2f57331912d453d414d45fa 100644 (file)
@@ -1,29 +1,46 @@
 /*****************************************************************************
  * vpar_headers.c : headers parsing
- * (c)1999 VideoLAN
+ *****************************************************************************
+ * Copyright (C) 1999, 2000 VideoLAN
+ *
+ * Authors: Christophe Massiot <massiot@via.ecp.fr>
+ *          Stéphane Borel <stef@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
+ * (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.
+ *
+ * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <string.h>
-#include <sys/uio.h>
+#include "defs.h"
+
+#include <stdlib.h>                                                /* free() */
+#include <sys/types.h>                        /* on BSD, uio.h needs types.h */
+#include <sys/uio.h>                                            /* "input.h" */
 
 #include "config.h"
 #include "common.h"
+#include "threads.h"
 #include "mtime.h"
-#include "vlc_thread.h"
+#include "plugins.h"
 
 #include "intf_msg.h"
-#include "debug.h"                    /* ?? temporaire, requis par netlist.h */
 
-#include "input.h"
-#include "input_netlist.h"
-#include "decoder_fifo.h"
+#include "stream_control.h"
+#include "input_ext-dec.h"
+
 #include "video.h"
 #include "video_output.h"
 
 #include "video_parser.h"
 #include "video_fifo.h"
 
-/*
- * Function pointer
- */
-typedef void    (*f_picture_data_t)( vpar_thread_t*, int );
-
 /*
  * Local prototypes
  */
-static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
+static __inline__ void NextStartCode( bit_stream_t * );
 static void SequenceHeader( vpar_thread_t * p_vpar );
 static void GroupHeader( vpar_thread_t * p_vpar );
 static void PictureHeader( vpar_thread_t * p_vpar );
@@ -76,7 +88,7 @@ int pi_default_intra_quant[] =
     26, 27, 29, 32, 35, 40, 48, 58,
     26, 27, 29, 34, 38, 46, 56, 69,
     27, 29, 35, 38, 46, 56, 69, 83
-};     
+};
 #else
 int pi_default_intra_quant[] =
 {
@@ -87,7 +99,7 @@ int pi_default_intra_quant[] =
     5632,   9232,   9031,   8730,   8192,   7040,   5542,   3390,
     5230,   7533,   7621,   7568,   7040,   6321,   5225,   3219,
     3602,   5189,   5250,   5539,   5265,   5007,   4199,   2638,
-    1907,   2841,   3230,   3156,   3249,   3108,   2638,   1617       
+    1907,   2841,   3230,   3156,   3249,   3108,   2638,   1617
 };
 #endif
 
@@ -116,7 +128,7 @@ int pi_default_nonintra_quanit[] =
     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
     3216,   4464,   4208,   3792,   3216,   2528,   1744,   880,
     2224,   3072,   2896,   2608,   2224,   1744,   1200,   608,
-    1136,   1568,   1472,   1328,   1136,   880,    608,    304        
+    1136,   1568,   1472,   1328,   1136,   880,    608,    304
 };
 #endif
 
@@ -153,34 +165,25 @@ static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
     if( i_coding_type != B_CODING_TYPE )
     {
         if( p_vpar->sequence.p_forward != NULL )
+        {
             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
+        }
         if( p_vpar->sequence.p_backward != NULL )
         {
-#ifdef SAM_SYNCHRO
             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
                               vpar_SynchroDate( p_vpar ) );
-#else
-            mtime_t     date;
-            date = vpar_SynchroDate( p_vpar );
-            vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
-                              date );
-            if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
-                vpar_SynchroKludge( p_vpar, date );
-#endif
         }
         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
         p_vpar->sequence.p_backward = p_newref;
         if( p_newref != NULL )
+        {
             vout_LinkPicture( p_vpar->p_vout, p_newref );
-#ifndef SAM_SYNCHRO
-        p_vpar->synchro.i_coding_type = i_coding_type;
-#endif
+        }
     }
     else if( p_newref != NULL )
     {
         /* Put date immediately. */
-        vout_DatePicture( p_vpar->p_vout, p_newref,
-                          vpar_SynchroDate( p_vpar ) );
+        vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
     }
 }
 
@@ -195,10 +198,14 @@ static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
     if( i_coding_type != B_CODING_TYPE )
     {
         if( p_vpar->sequence.p_backward != NULL )
+        {
             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
+        }
         p_vpar->sequence.p_backward = p_newref;
         if( p_newref != NULL )
+        {
             vout_LinkPicture( p_vpar->p_vout, p_newref );
+        }
     }
 }
 
@@ -208,19 +215,19 @@ static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
 {
     int i_dummy;
-    
+
     if( !p_matrix->b_allocated )
     {
         /* Allocate a piece of memory to load the matrix. */
         if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
         {
-            intf_ErrMsg("vpar error: allocation error in LoadMatrix()\n");
+            intf_ErrMsg( "vpar error: allocation error in LoadMatrix()\n" );
             p_vpar->b_error = 1;
             return;
         }
         p_matrix->b_allocated = 1;
     }
-    
+
     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
     {
         p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
@@ -245,7 +252,7 @@ static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
         free( p_matrix->pi_matrix );
         p_matrix->b_allocated = 0;
     }
-    
+
     p_matrix->pi_matrix = pi_array;
 }
 
@@ -258,11 +265,13 @@ static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
  *****************************************************************************/
 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
 {
-    while( !p_vpar->b_die )
+    while( !p_vpar->p_fifo->b_die )
     {
-        NextStartCode( p_vpar );
+        NextStartCode( &p_vpar->bit_stream );
         if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
+        {
             return 0;
+        }
         RemoveBits( &p_vpar->bit_stream, 8 );
     }
     return 1;
@@ -273,12 +282,15 @@ int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
  *****************************************************************************/
 int vpar_ParseHeader( vpar_thread_t * p_vpar )
 {
-    while( !p_vpar->b_die )
+    while( !p_vpar->p_fifo->b_die )
     {
-        NextStartCode( p_vpar );
+        NextStartCode( &p_vpar->bit_stream );
         switch( GetBits32( &p_vpar->bit_stream ) )
         {
         case SEQUENCE_HEADER_CODE:
+#ifdef STATS
+            p_vpar->c_sequences++;
+#endif
             SequenceHeader( p_vpar );
             return 0;
             break;
@@ -314,37 +326,37 @@ int vpar_ParseHeader( vpar_thread_t * p_vpar )
  *****************************************************************************/
 static void SequenceHeader( vpar_thread_t * p_vpar )
 {
-#define RESERVED    -1 
-    static float r_frame_rate_table[16] =
+#define RESERVED    -1
+    static int i_frame_rate_table[16] =
     {
-        0.0,
-        ((23.0*1000.0)/1001.0),
-        24.0,
-        25.0,
-        ((30.0*1000.0)/1001.0),
-        30.0,
-        50.0,
-        ((60.0*1000.0)/1001.0),
-        60.0,
+        0,
+        23 * 1000,
+        24 * 1001,
+        25 * 1001,
+        30 * 1000,
+        30 * 1001,
+        50 * 1001,
+        60 * 1000,
+        60 * 1001,
         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
     };
 #undef RESERVED
 
     int i_height_save, i_width_save;
-    
+
     i_height_save = p_vpar->sequence.i_height;
     i_width_save = p_vpar->sequence.i_width;
 
     p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
     p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
     p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
-    p_vpar->sequence.r_frame_rate =
-            r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
+    p_vpar->sequence.i_frame_rate =
+            i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
 
     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
      * constrained_parameters_flag */
     RemoveBits( &p_vpar->bit_stream, 30 );
-    
+
     /*
      * Quantization matrices
      */
@@ -357,7 +369,7 @@ static void SequenceHeader( vpar_thread_t * p_vpar )
         /* Use default matrix. */
         LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
     }
-    
+
     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
     {
         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
@@ -367,7 +379,7 @@ static void SequenceHeader( vpar_thread_t * p_vpar )
         /* Use default matrix. */
         LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
     }
-    
+
     /* Unless later overwritten by a matrix extension, we have the same
      * matrices for luminance and chrominance. */
     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
@@ -378,14 +390,14 @@ static void SequenceHeader( vpar_thread_t * p_vpar )
     /*
      * Sequence Extension
      */
-    NextStartCode( p_vpar );
+    NextStartCode( &p_vpar->bit_stream );
     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
     {
         int                         i_dummy;
 
         /* Turn the MPEG2 flag on */
         p_vpar->sequence.b_mpeg2 = 1;
-    
+
         /* Parse sequence_extension */
         RemoveBits32( &p_vpar->bit_stream );
         /* extension_start_code_identifier, profile_and_level_indication */
@@ -399,7 +411,7 @@ static void SequenceHeader( vpar_thread_t * p_vpar )
         /* frame_rate_extension_n */
         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
         /* frame_rate_extension_d */
-        p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
+        p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
     }
     else
@@ -454,7 +466,7 @@ static void SequenceHeader( vpar_thread_t * p_vpar )
     if(    p_vpar->sequence.i_width != i_width_save
         || p_vpar->sequence.i_height != i_height_save )
     {
-         /* What do we do in case of a size change ??? */
+         /* FIXME: What do we do in case of a size change ?? */
     }
 #endif
 
@@ -477,51 +489,13 @@ static void GroupHeader( vpar_thread_t * p_vpar )
  *****************************************************************************/
 static void PictureHeader( vpar_thread_t * p_vpar )
 {
-    /* Table of optimized PictureData functions. */
-    static f_picture_data_t ppf_picture_data[4][4] =
-    {
-        {
-            NULL, NULL, NULL, NULL
-        },
-        {
-            /* TOP_FIELD */
-#if (VPAR_OPTIM_LEVEL > 1)
-            NULL, vpar_PictureData2I420TZ, vpar_PictureData2P420TZ,
-            vpar_PictureData2B420TZ
-#else
-            NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
-            vpar_PictureDataGENERIC
-#endif
-        },
-        {
-            /* BOTTOM_FIELD */
-#if (VPAR_OPTIM_LEVEL > 1)
-            NULL, vpar_PictureData2I420BZ, vpar_PictureData2P420BZ,
-            vpar_PictureData2B420BZ
-#else
-            NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
-            vpar_PictureDataGENERIC
-#endif
-        },
-        {
-            /* FRAME_PICTURE */
-#if (VPAR_OPTIM_LEVEL > 0)
-            NULL, vpar_PictureData2I420F0, vpar_PictureData2P420F0,
-            vpar_PictureData2B420F0
-#else
-            NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
-            vpar_PictureDataGENERIC
-#endif
-        }
-    };
-
     int                 i_structure;
     int                 i_mb_base;
     boolean_t           b_parsable;
 #ifdef VDEC_SMP
     int                 i_mb;
 #endif
-    
+
     RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
     RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
@@ -544,17 +518,17 @@ static void PictureHeader( vpar_thread_t * p_vpar )
         RemoveBits( &p_vpar->bit_stream, 8 );
     }
 
-    /* 
+    /*
      * Picture Coding Extension
      */
-    NextStartCode( p_vpar );
+    NextStartCode( &p_vpar->bit_stream );
     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
     {
         /* Parse picture_coding_extension */
         RemoveBits32( &p_vpar->bit_stream );
         /* extension_start_code_identifier */
         RemoveBits( &p_vpar->bit_stream, 4 );
-        
+
         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
@@ -574,7 +548,7 @@ static void PictureHeader( vpar_thread_t * p_vpar )
          * chroma_420_type (obsolete) */
         RemoveBits( &p_vpar->bit_stream, 1 );
         p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
-        
+
         /* composite_display_flag */
         if( GetBits( &p_vpar->bit_stream, 1 ) )
         {
@@ -597,6 +571,10 @@ static void PictureHeader( vpar_thread_t * p_vpar )
         p_vpar->picture.b_progressive_frame = 1;
     }
 
+#ifdef STATS
+        p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
+#endif
+
     if( p_vpar->picture.i_current_structure &&
         (i_structure == FRAME_STRUCTURE ||
          i_structure == p_vpar->picture.i_current_structure) )
@@ -617,7 +595,7 @@ static void PictureHeader( vpar_thread_t * p_vpar )
 #endif
             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
         }
-        
+
         p_vpar->picture.i_current_structure = 0;
 
         intf_DbgMsg("vpar debug: odd number of field picture.\n");
@@ -636,7 +614,7 @@ static void PictureHeader( vpar_thread_t * p_vpar )
         if( p_vpar->picture.i_current_structure )
         {
             /* Second field of a frame. We will decode it if, and only if we
-            * have decoded the first field. */
+             * have decoded the first field. */
             b_parsable = (p_vpar->picture.p_picture != NULL);
         }
         else
@@ -651,19 +629,37 @@ static void PictureHeader( vpar_thread_t * p_vpar )
     {
         /* Update the reference pointers. */
         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
-        
-        /* Warn Synchro we have trashed a picture. */
-        vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
 
         /* Update context. */
         if( i_structure != FRAME_STRUCTURE )
-            p_vpar->picture.i_current_structure = i_structure;
+        {
+            if( (p_vpar->picture.i_current_structure | i_structure)
+                    == FRAME_STRUCTURE )
+            {
+                p_vpar->picture.i_current_structure = 0;
+            }
+            else
+            {
+                /* The frame is complete. */
+                p_vpar->picture.i_current_structure = i_structure;
+                vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
+            }
+        }
+        else
+        {
+            /* Warn Synchro we have trashed a picture. */
+            vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
+        }
         p_vpar->picture.p_picture = NULL;
 
         return;
     }
 
     /* OK, now we are sure we will decode the picture. */
+#ifdef STATS
+    p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
+#endif
+
 #define P_picture p_vpar->picture.p_picture
     p_vpar->picture.b_error = 0;
     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
@@ -672,13 +668,13 @@ static void PictureHeader( vpar_thread_t * p_vpar )
     {
         /* This is a new frame. Get a structure from the video_output. */
         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
-                                        99+p_vpar->sequence.i_chroma_format, /*???*/
+                                        99+p_vpar->sequence.i_chroma_format, /*XXX??*/
                                         p_vpar->sequence.i_width,
                                         p_vpar->sequence.i_height ) )
              == NULL )
         {
             intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
-            if( p_vpar->b_die || p_vpar->b_error )
+            if( p_vpar->p_fifo->b_die || p_vpar->b_error )
             {
                 return;
             }
@@ -699,14 +695,14 @@ static void PictureHeader( vpar_thread_t * p_vpar )
 #ifdef VDEC_SMP
         memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
 #endif
-/* FIXME ! remove asap */
+/* FIXME ! remove asap ?? */
 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
 
         /* Update the reference pointers. */
         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
 
 #ifdef VDEC_SMP
-        /* Link referenced pictures for the decoder 
+        /* Link referenced pictures for the decoder
          * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
         if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
             p_vpar->picture.i_coding_type == B_CODING_TYPE )
@@ -716,7 +712,7 @@ static void PictureHeader( vpar_thread_t * p_vpar )
         if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
         {
             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
-        } 
+        }
 #endif
     }
     p_vpar->picture.i_current_structure |= i_structure;
@@ -739,22 +735,9 @@ static void PictureHeader( vpar_thread_t * p_vpar )
     /* Extension and User data. */
     ExtensionAndUserData( p_vpar );
 
-    /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
-    if( p_vpar->sequence.i_chroma_format != CHROMA_420
-        || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
-        || p_vpar->sequence.i_scalable_mode == SC_DP )
-    {
-        /* Weird stream. Use the slower generic function. */
-        vpar_PictureDataGENERIC( p_vpar, i_mb_base );
-    }
-    else
-    {
-        /* Try to find an optimized function. */
-        ppf_picture_data[p_vpar->picture.i_structure]
-                        [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
-    }
+    vpar_PictureData( p_vpar, i_mb_base );
 
-    if( p_vpar->b_die || p_vpar->b_error )
+    if( p_vpar->p_fifo->b_die || p_vpar->b_error )
     {
         return;
     }
@@ -770,8 +753,13 @@ static void PictureHeader( vpar_thread_t * p_vpar )
         }
 #endif
 
+#ifdef STATS
+        p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
+#endif
+
         if( P_picture->i_deccount != 1 )
         {
+            vpar_SynchroEnd( p_vpar, 1 );
             vout_DestroyPicture( p_vpar->p_vout, P_picture );
         }
 
@@ -810,9 +798,9 @@ static void PictureHeader( vpar_thread_t * p_vpar )
  *****************************************************************************/
 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
 {
-    while( !p_vpar->b_die )
+    while( !p_vpar->p_fifo->b_die )
     {
-        NextStartCode( p_vpar );
+        NextStartCode( &p_vpar->bit_stream );
         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
         {
         case EXTENSION_START_CODE: