]> git.sesse.net Git - vlc/blob - src/video_decoder/video_parser.h
be23b6918957cc87eb0a1d47ea6ea941708155b9
[vlc] / src / video_decoder / video_parser.h
1 /*****************************************************************************
2  * video_parser.h : video parser thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: video_parser.h,v 1.12 2001/08/22 17:21:45 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Jean-Marc Dressler <polux@via.ecp.fr>
9  *          Stéphane Borel <stef@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*
27  * Block parsing structures
28  */
29
30 /*****************************************************************************
31  * macroblock_parsing_t : macroblock context & predictors
32  *****************************************************************************/
33 typedef struct motion_s
34 {
35     u8 *                pppi_ref[2][3];
36     int                 ppi_pmv[2][2];
37     int                 pi_f_code[2];
38 } motion_t;
39
40 typedef struct macroblock_parsing_s
41 {
42     int                 i_offset;
43
44     motion_t            b_motion;
45     motion_t            f_motion;
46
47     /* Predictor for DC coefficients in intra blocks */
48     u16                 pi_dc_dct_pred[3];
49     u8                  i_quantizer_scale;        /* scale of the quantization
50                                                    * matrices                */
51 } macroblock_parsing_t;
52
53 /*****************************************************************************
54  * Constants
55  *****************************************************************************/
56 extern u8       pi_default_intra_quant[64];
57 extern u8       pi_default_nonintra_quant[64];
58 extern u8       pi_scan[2][64];
59
60 /*****************************************************************************
61  * Prototypes
62  *****************************************************************************/
63 void vpar_InitScanTable( struct vpar_thread_s * p_vpar );
64
65 typedef void (*f_picture_data_t)( struct vpar_thread_s * p_vpar );
66 #define PROTO_PICD( FUNCNAME )                                              \
67 void FUNCNAME( struct vpar_thread_s * p_vpar );
68
69 PROTO_PICD( vpar_PictureDataGENERIC )
70 #if (VPAR_OPTIM_LEVEL > 0)
71 PROTO_PICD( vpar_PictureData1I )
72 PROTO_PICD( vpar_PictureData1P )
73 PROTO_PICD( vpar_PictureData1B )
74 PROTO_PICD( vpar_PictureData1D )
75 PROTO_PICD( vpar_PictureData2IF )
76 PROTO_PICD( vpar_PictureData2PF )
77 PROTO_PICD( vpar_PictureData2BF )
78 #endif
79 #if (VPAR_OPTIM_LEVEL > 1)
80 PROTO_PICD( vpar_PictureData2IT )
81 PROTO_PICD( vpar_PictureData2PT )
82 PROTO_PICD( vpar_PictureData2BT )
83 PROTO_PICD( vpar_PictureData2IB )
84 PROTO_PICD( vpar_PictureData2PB )
85 PROTO_PICD( vpar_PictureData2BB )
86 #endif
87
88
89 /*
90  * Headers parsing structures
91  */
92
93 /*****************************************************************************
94  * quant_matrix_t : Quantization Matrix
95  *****************************************************************************/
96 typedef struct quant_matrix_s
97 {
98     u8 *        pi_matrix;
99     boolean_t   b_allocated;
100                           /* Has the matrix been allocated by vpar_headers ? */
101 } quant_matrix_t;
102
103 /*****************************************************************************
104  * sequence_t : sequence descriptor
105  *****************************************************************************
106  * This structure should only be changed when reading the sequence header,
107  * or exceptionnally some extension structures (like quant_matrix).
108  *****************************************************************************/
109 typedef struct sequence_s
110 {
111     u32                 i_height, i_width;      /* height and width of the lum
112                                                  * comp of the picture       */
113     u32                 i_size;       /* total number of pel of the lum comp */
114     u32                 i_mb_height, i_mb_width, i_mb_size;
115                                             /* the same, in macroblock units */
116     unsigned int        i_aspect_ratio;        /* height/width display ratio */
117     unsigned int        i_matrix_coefficients;/* coeffs of the YUV transform */
118     int                 i_chroma_format, i_scalable_mode;
119     int                 i_frame_rate;  /* theoritical frame rate in fps*1001 */
120     boolean_t           b_mpeg2;                                    /* guess */
121     boolean_t           b_progressive;              /* progressive (ie.
122                                                      * non-interlaced) frame */
123     quant_matrix_t      intra_quant, nonintra_quant;
124     quant_matrix_t      chroma_intra_quant, chroma_nonintra_quant;
125                                             /* current quantization matrices */
126
127     /* Parser context */
128     picture_t *         p_forward;        /* current forward reference frame */
129     picture_t *         p_backward;      /* current backward reference frame */
130     mtime_t             next_pts, next_dts;
131     int                 i_current_rate;
132     boolean_t           b_expect_discontinuity; /* reset the frame predictors
133                                                  * after the current frame   */
134
135     /* Copyright extension */
136     boolean_t           b_copyright_flag;     /* Whether the following
137                                                  information is significant
138                                                  or not. */
139     u8                  i_copyright_id;
140     boolean_t           b_original;
141     u64                 i_copyright_nb;
142 } sequence_t;
143
144 /*****************************************************************************
145  * picture_parsing_t : parser context descriptor
146  *****************************************************************************
147  * This structure should only be changed when reading the picture header.
148  *****************************************************************************/
149 typedef struct picture_parsing_s
150 {
151     /* Values from the picture_coding_extension. */
152     int                 ppi_f_code[2][2];
153     int                 i_intra_dc_precision;
154     boolean_t           b_frame_pred_frame_dct, b_q_scale_type;
155     boolean_t           b_intra_vlc_format;
156     boolean_t           b_progressive;
157     u8 *                pi_scan;
158     boolean_t           b_top_field_first, b_concealment_mv;
159     boolean_t           b_repeat_first_field;
160     /* Relative to the current field */
161     int                 i_coding_type, i_structure;
162     boolean_t           b_frame_structure; /* i_structure == FRAME_STRUCTURE */
163     boolean_t           b_current_field;         /* i_structure == TOP_FIELD */
164     boolean_t           b_second_field;
165
166     picture_t *         p_picture;               /* picture buffer from vout */
167     int                 i_current_structure;   /* current parsed structure of
168                                                 * p_picture (second field ?) */
169     int                 i_field_width;
170     boolean_t           b_error;            /* parsing error, try to recover */
171 } picture_parsing_t;
172
173 /*****************************************************************************
174  * Standard codes
175  *****************************************************************************/
176 #define PICTURE_START_CODE      0x100L
177 #define SLICE_START_CODE_MIN    0x101L
178 #define SLICE_START_CODE_MAX    0x1AFL
179 #define USER_DATA_START_CODE    0x1B2L
180 #define SEQUENCE_HEADER_CODE    0x1B3L
181 #define SEQUENCE_ERROR_CODE     0x1B4L
182 #define EXTENSION_START_CODE    0x1B5L
183 #define SEQUENCE_END_CODE       0x1B7L
184 #define GROUP_START_CODE        0x1B8L
185
186 /* extension start code IDs */
187 #define SEQUENCE_EXTENSION_ID                    1
188 #define SEQUENCE_DISPLAY_EXTENSION_ID            2
189 #define QUANT_MATRIX_EXTENSION_ID                3
190 #define COPYRIGHT_EXTENSION_ID                   4
191 #define SEQUENCE_SCALABLE_EXTENSION_ID           5
192 #define PICTURE_DISPLAY_EXTENSION_ID             7
193 #define PICTURE_CODING_EXTENSION_ID              8
194 #define PICTURE_SPATIAL_SCALABLE_EXTENSION_ID    9
195 #define PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID  10
196
197 /* scalable modes */
198 #define SC_NONE     0
199 #define SC_DP       1
200 #define SC_SPAT     2
201 #define SC_SNR      3
202 #define SC_TEMP     4
203
204 /* Chroma types */
205 #define CHROMA_420 1
206 #define CHROMA_422 2
207 #define CHROMA_444 3
208
209 /* Pictures types */
210 #define I_CODING_TYPE           1
211 #define P_CODING_TYPE           2
212 #define B_CODING_TYPE           3
213 #define D_CODING_TYPE           4 /* MPEG-1 ONLY */
214 /* other values are reserved */
215
216 /* Structures */
217 #define TOP_FIELD               1
218 #define BOTTOM_FIELD            2
219 #define FRAME_STRUCTURE         3
220
221 /*****************************************************************************
222  * Prototypes
223  *****************************************************************************/
224 int vpar_NextSequenceHeader( struct vpar_thread_s * p_vpar );
225 int vpar_ParseHeader( struct vpar_thread_s * p_vpar );
226
227
228 /*
229  * Synchronization management
230  */
231
232 /*****************************************************************************
233  * video_synchro_t : timers for the video synchro
234  *****************************************************************************/
235 #define MAX_PIC_AVERAGE         8
236
237 /* Read the discussion on top of vpar_synchro.c for more information. */
238 typedef struct video_synchro_s
239 {
240     /* synchro algorithm */
241     int             i_type;
242
243     /* date of the beginning of the decoding of the current picture */
244     mtime_t         decoding_start;
245
246     /* stream properties */
247     unsigned int    i_n_p, i_n_b;
248
249     /* decoding values */
250     mtime_t         p_tau[4];                  /* average decoding durations */
251     unsigned int    pi_meaningful[4];            /* number of durations read */
252     /* and p_vout->render_time (read with p_vout->change_lock) */
253
254     /* stream context */
255     unsigned int    i_eta_p, i_eta_b;
256     boolean_t       b_dropped_last;            /* for special synchros below */
257     mtime_t         backward_pts, current_pts;
258     int             i_current_period;   /* period to add to the next picture */
259     int             i_backward_period;  /* period to add after the next
260                                          * reference picture
261                                          * (backward_period * period / 2) */
262
263 #ifdef STATS
264     unsigned int    i_trashed_pic, i_not_chosen_pic, i_pic;
265 #endif
266 } video_synchro_t;
267
268 /* Synchro algorithms */
269 #define VPAR_SYNCHRO_DEFAULT    0
270 #define VPAR_SYNCHRO_I          1
271 #define VPAR_SYNCHRO_Iplus      2
272 #define VPAR_SYNCHRO_IP         3
273 #define VPAR_SYNCHRO_IPplus     4
274 #define VPAR_SYNCHRO_IPB        5
275
276 /*****************************************************************************
277  * Prototypes
278  *****************************************************************************/
279 void vpar_SynchroInit           ( struct vpar_thread_s * p_vpar );
280 boolean_t vpar_SynchroChoose    ( struct vpar_thread_s * p_vpar,
281                                   int i_coding_type, int i_structure );
282 void vpar_SynchroTrash          ( struct vpar_thread_s * p_vpar,
283                                   int i_coding_type, int i_structure );
284 void vpar_SynchroDecode         ( struct vpar_thread_s * p_vpar,
285                                   int i_coding_type, int i_structure );
286 void vpar_SynchroEnd            ( struct vpar_thread_s * p_vpar,
287                                   int i_coding_type, int i_structure,
288                                   int i_garbage );
289 mtime_t vpar_SynchroDate        ( struct vpar_thread_s * p_vpar );
290 void vpar_SynchroNewPicture( struct vpar_thread_s * p_vpar, int i_coding_type,
291                              int i_repeat_field );
292
293
294 /*
295  * Video parser structures
296  */
297
298 /*****************************************************************************
299  * vpar_thread_t: video parser thread descriptor
300  *****************************************************************************/
301 typedef struct vpar_thread_s
302 {
303     bit_stream_t            bit_stream;
304
305     /* Thread properties and locks */
306     vlc_thread_t            thread_id;            /* id for thread functions */
307
308     /* Input properties */
309     decoder_fifo_t *        p_fifo;                        /* PES input fifo */
310     vdec_config_t *         p_config;
311
312     /* Output properties */
313     vout_thread_t *         p_vout;                   /* video output thread */
314
315     /* Decoder properties */
316     vdec_pool_t             pool;
317
318     /* Parser properties */
319     sequence_t              sequence;
320     picture_parsing_t       picture;
321     macroblock_parsing_t    mb;
322     video_synchro_t         synchro;
323
324     /* Scan table */
325     u8                      ppi_scan[2][64];
326     /* Default quantization matrices */
327     u8                      pi_default_intra_quant[64];
328     u8                      pi_default_nonintra_quant[64];
329
330     /* Motion compensation plug-in used and shortcuts */
331     struct module_s *       p_motion_module;
332
333     /* IDCT plugin used and shortcuts */
334     struct module_s *       p_idct_module;
335     void ( * pf_sparse_idct ) ( void *, dctelem_t*, int );
336     void ( * pf_idct )        ( void *, dctelem_t*, int );
337     void ( * pf_norm_scan )   ( u8 ppi_scan[2][64] );
338
339 #ifdef STATS
340     /* Statistics */
341     count_t         c_loops;                              /* number of loops */
342     count_t         c_sequences;                      /* number of sequences */
343     count_t         pc_pictures[4]; /* number of (coding_type) pictures read */
344     count_t         pc_decoded_pictures[4];       /* number of (coding_type)
345                                                    *        pictures decoded */
346     count_t         pc_malformed_pictures[4];  /* number of pictures trashed
347                                                 * during parsing             */
348 #endif
349 } vpar_thread_t;
350
351 /*****************************************************************************
352  * NextStartCode : Find the next start code
353  *****************************************************************************/
354 static __inline__ void NextStartCode( bit_stream_t * p_bit_stream )
355 {
356     /* Re-align the buffer on an 8-bit boundary */
357     RealignBits( p_bit_stream );
358
359     while( ShowBits( p_bit_stream, 24 ) != 0x01L
360             && !p_bit_stream->p_decoder_fifo->b_die )
361     {
362         RemoveBits( p_bit_stream, 8 );
363     }
364 }
365
366 /*****************************************************************************
367  * LoadQuantizerScale
368  *****************************************************************************
369  * Quantizer scale factor (ISO/IEC 13818-2 7.4.2.2)
370  *****************************************************************************/
371 static __inline__ void LoadQuantizerScale( struct vpar_thread_s * p_vpar )
372 {
373     /* Quantization coefficient table */
374     static u8   pi_non_linear_quantizer_scale[32] =
375     {
376         0, 1, 2, 3, 4, 5, 6, 7, 8, 10,12,14,16,18,20, 22,
377         24,28,32,36,40,44,48,52,56,64,72,80,88,96,104,112
378     };
379     int         i_q_scale_code = GetBits( &p_vpar->bit_stream, 5 );
380
381     if( p_vpar->picture.b_q_scale_type )
382     {
383         p_vpar->mb.i_quantizer_scale =
384                         pi_non_linear_quantizer_scale[i_q_scale_code];
385     }
386     else
387     {
388         p_vpar->mb.i_quantizer_scale = i_q_scale_code << 1;
389     }
390 }
391