video_parser.h 7.91 KB
Newer Older
1
/*****************************************************************************
2
 * video_parser.h : video parser thread
3
 *****************************************************************************
4
 * Copyright (C) 1999, 2000 VideoLAN
5
 * $Id: video_parser.h,v 1.8 2001/01/24 19:05:55 massiot Exp $
6
 *
7
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 9 10 11 12
 *
 * 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.
13
 * 
14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
18
 *
19 20 21
 * 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.
22 23 24
 *****************************************************************************/

/*****************************************************************************
25 26 27 28
 * Requires:
 *  "config.h"
 *  "common.h"
 *  "mtime.h"
29
 *  "threads.h"
30 31 32
 *  "input.h"
 *  "video.h"
 *  "video_output.h"
33 34
 *  "decoder_fifo.h"
 *  "video_fifo.h"
35 36
 *  "vpar_headers.h"
 *****************************************************************************/
37

38 39 40 41 42 43 44 45 46
/*****************************************************************************
 * video_fifo_t
 *****************************************************************************
 * This rotative FIFO contains undecoded macroblocks that are to be decoded
 *****************************************************************************/
struct vpar_thread_s;

typedef struct video_fifo_s
{
47
#ifdef VDEC_SMP
48 49 50 51
    vlc_mutex_t         lock;                              /* fifo data lock */
    vlc_cond_t          wait;              /* fifo data conditional variable */

    /* buffer is an array of undec_picture_t pointers */
52 53 54 55 56 57
    macroblock_t *      buffer[VFIFO_SIZE + 1];
    int                 i_start;
    int                 i_end;
#else
    macroblock_t        buffer;
#endif
58 59 60 61 62 63 64 65 66 67

    struct vpar_thread_s *      p_vpar;
} video_fifo_t;

/*****************************************************************************
 * video_buffer_t
 *****************************************************************************
 * This structure enables the parser to maintain a list of free
 * macroblock_t structures
 *****************************************************************************/
68
#ifdef VDEC_SMP
69 70 71 72 73 74 75 76
typedef struct video_buffer_s
{
    vlc_mutex_t         lock;                            /* buffer data lock */

    macroblock_t        p_macroblocks[VFIFO_SIZE + 1];
    macroblock_t *      pp_mb_free[VFIFO_SIZE+1];          /* this is a LIFO */
    int                 i_index;
} video_buffer_t;
77
#endif
78

79
/*****************************************************************************
80
 * vpar_thread_t: video parser thread descriptor
81
 *****************************************************************************
82
 * XXX??
83
 *****************************************************************************/
84
typedef struct vpar_thread_s
85
{
86 87
    bit_stream_t        bit_stream;

88
    /* Thread properties and locks */
89
    vlc_thread_t        thread_id;                /* id for thread functions */
90 91

    /* Thread configuration */
92
    /* XXX?? */
93
//    int *pi_status;
94

95 96

    /* Input properties */
97 98
    decoder_fifo_t *    p_fifo;                            /* PES input fifo */
    vdec_config_t *     p_config;
99 100

    /* Output properties */
101
    vout_thread_t *     p_vout;                       /* video output thread */
102

103
    /* Decoder properties */
104
    struct vdec_thread_s *      pp_vdec[NB_VDEC];
105
    video_fifo_t                vfifo;
106
#ifdef VDEC_SMP
107
    video_buffer_t              vbuffer;
108
#endif
109 110

    /* Parser properties */
111 112 113
    sequence_t              sequence;
    picture_parsing_t       picture;
    macroblock_parsing_t    mb;
114
    video_synchro_t         synchro;
115

116
    /* Lookup tables */
117 118
    lookup_t                pl_mb_addr_inc[2048];    /* for macroblock
                                                        address increment */
119
    /* tables for macroblock types 0=P 1=B */
120
    lookup_t                ppl_mb_type[2][64];
121
    /* table for coded_block_pattern */
122
    lookup_t *              pl_coded_pattern;
123
    /* variable length codes for the structure dct_dc_size for intra blocks */
124
    lookup_t *              pppl_dct_dc_size[2][2];
125
    /* Structure to store the tables B14 & B15 (ISO/IEC 13818-2 B.4) */
126
    dct_lookup_t            ppl_dct_coef[2][16384];
127 128 129 130 131
    /* Scan table */
    u8                      ppi_scan[2][64];
    /* Default quantization matrices */
    u8                      pi_default_intra_quant[64];
    u8                      pi_default_nonintra_quant[64];
132

133 134 135 136 137
    /* Motion compensation plugin used and shortcuts */
    struct module_s *       p_motion_module;
    void ( * pppf_motion[4][2][4] )     ( struct macroblock_s * );
    void ( * ppf_motion_skipped[4][4] ) ( struct macroblock_s * );

138
    /* IDCT plugin used and shortcuts to access its capabilities */
139
    struct module_s *       p_idct_module;
140 141 142
    idct_init_t             pf_init;
    f_idct_t                pf_sparse_idct;
    f_idct_t                pf_idct;
143
    norm_scan_t             pf_norm_scan;
144

145 146
#ifdef STATS
    /* Statistics */
147 148
    count_t         c_loops;                              /* number of loops */
    count_t         c_sequences;                      /* number of sequences */
Christophe Massiot's avatar
Christophe Massiot committed
149 150 151 152 153
    count_t         pc_pictures[4]; /* number of (coding_type) pictures read */
    count_t         pc_decoded_pictures[4];       /* number of (coding_type)
                                                   *        pictures decoded */
    count_t         pc_malformed_pictures[4];  /* number of pictures trashed
                                                * during parsing             */
154
#endif
155
} vpar_thread_t;
156

157
/*****************************************************************************
158
 * Prototypes
159
 *****************************************************************************/
160 161

/* Thread management functions */
162
vlc_thread_t vpar_CreateThread       ( vdec_config_t * );
163 164 165 166

/*****************************************************************************
 * NextStartCode : Find the next start code
 *****************************************************************************/
167
static __inline__ void NextStartCode( bit_stream_t * p_bit_stream )
168 169
{
    /* Re-align the buffer on an 8-bit boundary */
170
    RealignBits( p_bit_stream );
171

172 173
    while( ShowBits( p_bit_stream, 24 ) != 0x01L
            && !p_bit_stream->p_decoder_fifo->b_die )
174
    {
175
        RemoveBits( p_bit_stream, 8 );
176 177 178
    }
}

179 180 181 182 183 184 185 186
/*****************************************************************************
 * LoadQuantizerScale
 *****************************************************************************
 * Quantizer scale factor (ISO/IEC 13818-2 7.4.2.2)
 *****************************************************************************/
static __inline__ void LoadQuantizerScale( struct vpar_thread_s * p_vpar )
{
    /* Quantization coefficient table */
187
    static u8   ppi_quantizer_scale[3][32] =
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    {
        /* MPEG-2 */
        {
            /* q_scale_type */
            /* linear */
            0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,
            32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62
        },
        {
            /* non-linear */
            0, 1, 2, 3, 4, 5, 6, 7, 8, 10,12,14,16,18,20, 22,
            24,28,32,36,40,44,48,52,56,64,72,80,88,96,104,112
        },
        /* MPEG-1 */
        {
            0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
            16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
        }
    };

208
    p_vpar->mb.i_quantizer_scale = ppi_quantizer_scale
209 210 211 212
           [(!p_vpar->sequence.b_mpeg2 << 1) | p_vpar->picture.b_q_scale_type]
           [GetBits( &p_vpar->bit_stream, 5 )];
}