]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_video/parser.h
* ./modules/*: moved plugins to the new tree. Yet untested builds include
[vlc] / modules / codec / mpeg_video / parser.h
1 /*****************************************************************************
2  * video_parser.h : video parser thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: parser.h,v 1.1 2002/08/04 17:23:42 sam 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_PictureData2IF )
72 PROTO_PICD( vpar_PictureData2PF )
73 PROTO_PICD( vpar_PictureData2BF )
74 #endif
75 #if (VPAR_OPTIM_LEVEL > 1)
76 PROTO_PICD( vpar_PictureData2IT )
77 PROTO_PICD( vpar_PictureData2PT )
78 PROTO_PICD( vpar_PictureData2BT )
79 PROTO_PICD( vpar_PictureData2IB )
80 PROTO_PICD( vpar_PictureData2PB )
81 PROTO_PICD( vpar_PictureData2BB )
82 PROTO_PICD( vpar_PictureData1I )
83 PROTO_PICD( vpar_PictureData1P )
84 PROTO_PICD( vpar_PictureData1B )
85 PROTO_PICD( vpar_PictureData1D )
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     vlc_bool_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;              /* 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_chroma_nb_blocks;
120     vlc_bool_t          b_chroma_h_subsampled, b_chroma_v_subsampled;
121     int                 i_frame_rate;  /* theoritical frame rate in fps*1001 */
122     vlc_bool_t          b_mpeg2;                                    /* guess */
123     vlc_bool_t          b_progressive;              /* progressive (ie.
124                                                      * non-interlaced) frame */
125     quant_matrix_t      intra_quant, nonintra_quant;
126     quant_matrix_t      chroma_intra_quant, chroma_nonintra_quant;
127                                             /* current quantization matrices */
128
129     /* Parser context */
130     picture_t *         p_forward;        /* current forward reference frame */
131     picture_t *         p_backward;      /* current backward reference frame */
132     mtime_t             next_pts, next_dts;
133     int                 i_current_rate;
134     vlc_bool_t          b_expect_discontinuity; /* reset the frame predictors
135                                                  * after the current frame   */
136
137     /* Copyright extension */
138     vlc_bool_t          b_copyright_flag;     /* Whether the following
139                                                  information is significant
140                                                  or not. */
141     u8                  i_copyright_id;
142     vlc_bool_t          b_original;
143     u64                 i_copyright_nb;
144 } sequence_t;
145
146 /*****************************************************************************
147  * picture_parsing_t : parser context descriptor
148  *****************************************************************************
149  * This structure should only be changed when reading the picture header.
150  *****************************************************************************/
151 typedef struct picture_parsing_s
152 {
153     /* Values from the picture_coding_extension. */
154     int                 ppi_f_code[2][2];
155     int                 i_intra_dc_precision;
156     vlc_bool_t          b_frame_pred_frame_dct, b_q_scale_type;
157     vlc_bool_t          b_intra_vlc_format;
158     vlc_bool_t          b_progressive;
159     u8 *                pi_scan;
160     vlc_bool_t          b_top_field_first, b_concealment_mv;
161     vlc_bool_t          b_repeat_first_field;
162     /* Relative to the current field */
163     int                 i_coding_type, i_structure, i_field_width;
164     vlc_bool_t          b_frame_structure; /* i_structure == FRAME_STRUCTURE */
165     vlc_bool_t          b_current_field;         /* i_structure == TOP_FIELD */
166     vlc_bool_t          b_second_field;
167
168     picture_t *         p_picture;               /* picture buffer from vout */
169     int                 i_current_structure;   /* current parsed structure of
170                                                 * p_picture (second field ?) */
171     vlc_bool_t          b_error;            /* parsing error, try to recover */
172
173     /* Given by the video output */
174     int                 i_lum_stride, i_chrom_stride;
175 } picture_parsing_t;
176
177 /*****************************************************************************
178  * Standard codes
179  *****************************************************************************/
180 #define PICTURE_START_CODE      0x100L
181 #define SLICE_START_CODE_MIN    0x101L
182 #define SLICE_START_CODE_MAX    0x1AFL
183 #define USER_DATA_START_CODE    0x1B2L
184 #define SEQUENCE_HEADER_CODE    0x1B3L
185 #define SEQUENCE_ERROR_CODE     0x1B4L
186 #define EXTENSION_START_CODE    0x1B5L
187 #define SEQUENCE_END_CODE       0x1B7L
188 #define GROUP_START_CODE        0x1B8L
189
190 /* extension start code IDs */
191 #define SEQUENCE_EXTENSION_ID                    1
192 #define SEQUENCE_DISPLAY_EXTENSION_ID            2
193 #define QUANT_MATRIX_EXTENSION_ID                3
194 #define COPYRIGHT_EXTENSION_ID                   4
195 #define SEQUENCE_SCALABLE_EXTENSION_ID           5
196 #define PICTURE_DISPLAY_EXTENSION_ID             7
197 #define PICTURE_CODING_EXTENSION_ID              8
198 #define PICTURE_SPATIAL_SCALABLE_EXTENSION_ID    9
199 #define PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID  10
200
201 /* scalable modes */
202 #define SC_NONE     0
203 #define SC_DP       1
204 #define SC_SPAT     2
205 #define SC_SNR      3
206 #define SC_TEMP     4
207
208 /* Chroma types */
209 #define CHROMA_NONE 0
210 #define CHROMA_420 1
211 #define CHROMA_422 2
212 #define CHROMA_444 3
213
214 /* Pictures types */
215 #define I_CODING_TYPE           1
216 #define P_CODING_TYPE           2
217 #define B_CODING_TYPE           3
218 #define D_CODING_TYPE           4 /* MPEG-1 ONLY */
219 /* other values are reserved */
220
221 /* Structures */
222 #define TOP_FIELD               1
223 #define BOTTOM_FIELD            2
224 #define FRAME_STRUCTURE         3
225
226 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
227 #define AR_SQUARE_PICTURE       1                           /* square pixels */
228 #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
229 #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
230 #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
231
232 /*****************************************************************************
233  * Prototypes
234  *****************************************************************************/
235 int vpar_NextSequenceHeader( struct vpar_thread_s * p_vpar );
236 int vpar_ParseHeader( struct vpar_thread_s * p_vpar );
237
238
239 /*
240  * Synchronization management
241  */
242
243 /*****************************************************************************
244  * video_synchro_t : timers for the video synchro
245  *****************************************************************************/
246 #define MAX_PIC_AVERAGE         8
247
248 /* Read the discussion on top of vpar_synchro.c for more information. */
249 typedef struct video_synchro_s
250 {
251     /* synchro algorithm */
252     int             i_type;
253
254     /* date of the beginning of the decoding of the current picture */
255     mtime_t         decoding_start;
256
257     /* stream properties */
258     unsigned int    i_n_p, i_n_b;
259
260     /* decoding values */
261     mtime_t         p_tau[4];                  /* average decoding durations */
262     unsigned int    pi_meaningful[4];            /* number of durations read */
263     /* and p_vout->render_time (read with p_vout->change_lock) */
264
265     /* stream context */
266     unsigned int    i_eta_p, i_eta_b;
267     vlc_bool_t      b_dropped_last;            /* for special synchros below */
268     mtime_t         backward_pts, current_pts;
269     int             i_current_period;   /* period to add to the next picture */
270     int             i_backward_period;  /* period to add after the next
271                                          * reference picture
272                                          * (backward_period * period / 2) */
273
274     /* statistics */
275     unsigned int    i_trashed_pic, i_not_chosen_pic, i_pic;
276 } video_synchro_t;
277
278 /* Synchro algorithms */
279 #define VPAR_SYNCHRO_DEFAULT    0
280 #define VPAR_SYNCHRO_I          1
281 #define VPAR_SYNCHRO_Iplus      2
282 #define VPAR_SYNCHRO_IP         3
283 #define VPAR_SYNCHRO_IPplus     4
284 #define VPAR_SYNCHRO_IPB        5
285
286 /*****************************************************************************
287  * Prototypes
288  *****************************************************************************/
289 void vpar_SynchroInit           ( struct vpar_thread_s * p_vpar );
290 vlc_bool_t vpar_SynchroChoose   ( struct vpar_thread_s * p_vpar,
291                                   int i_coding_type, int i_structure );
292 void vpar_SynchroTrash          ( struct vpar_thread_s * p_vpar,
293                                   int i_coding_type, int i_structure );
294 void vpar_SynchroDecode         ( struct vpar_thread_s * p_vpar,
295                                   int i_coding_type, int i_structure );
296 void vpar_SynchroEnd            ( struct vpar_thread_s * p_vpar,
297                                   int i_coding_type, int i_structure,
298                                   int i_garbage );
299 mtime_t vpar_SynchroDate        ( struct vpar_thread_s * p_vpar );
300 void vpar_SynchroNewPicture( struct vpar_thread_s * p_vpar, int i_coding_type,
301                              int i_repeat_field );
302
303
304 /*
305  * Video parser structures
306  */
307
308 /*****************************************************************************
309  * vpar_thread_t: video parser thread descriptor
310  *****************************************************************************/
311 typedef struct vpar_thread_s
312 {
313     bit_stream_t            bit_stream;
314
315     /* Thread properties and locks */
316     vlc_thread_t            thread_id;            /* id for thread functions */
317
318     /* Input properties */
319     decoder_fifo_t *        p_fifo;                        /* PES input fifo */
320
321     /* Output properties */
322     vout_thread_t *         p_vout;                   /* video output thread */
323
324     /* Decoder properties */
325     vdec_pool_t             pool;
326
327     /* Parser properties */
328     sequence_t              sequence;
329     picture_parsing_t       picture;
330     macroblock_parsing_t    mb;
331     video_synchro_t         synchro;
332
333     /* Scan table */
334     u8                      ppi_scan[2][64];
335     /* Default quantization matrices */
336     u8                      pi_default_intra_quant[64];
337     u8                      pi_default_nonintra_quant[64];
338
339     /* Motion compensation plug-in used and shortcuts */
340     module_t *       p_motion;
341
342     /* IDCT plug-in used and shortcuts */
343     module_t *       p_idct;
344     void ( * pf_sparse_idct_add )( dctelem_t *, yuv_data_t *, int,
345                                  void *, int );
346     void ( * pf_idct_add )     ( dctelem_t *, yuv_data_t *, int,
347                                  void *, int );
348     void ( * pf_sparse_idct_copy )( dctelem_t *, yuv_data_t *, int,
349                                   void *, int );
350     void ( * pf_idct_copy )    ( dctelem_t *, yuv_data_t *, int,
351                                  void *, int );
352
353     void ( * pf_norm_scan ) ( u8 ppi_scan[2][64] );
354
355     /* Statistics */
356     count_t         c_loops;                              /* number of loops */
357     count_t         c_sequences;                      /* number of sequences */
358     count_t         pc_pictures[4]; /* number of (coding_type) pictures read */
359     count_t         pc_decoded_pictures[4];       /* number of (coding_type)
360                                                    *        pictures decoded */
361     count_t         pc_malformed_pictures[4];  /* number of pictures trashed
362                                                 * during parsing             */
363 } vpar_thread_t;
364
365 /*****************************************************************************
366  * NextStartCode : Find the next start code
367  *****************************************************************************/
368 static inline void NextStartCode( bit_stream_t * p_bit_stream )
369 {
370     /* Re-align the buffer on an 8-bit boundary */
371     RealignBits( p_bit_stream );
372
373     while( ShowBits( p_bit_stream, 24 ) != 0x01L
374             && !p_bit_stream->p_decoder_fifo->b_die )
375     {
376         RemoveBits( p_bit_stream, 8 );
377     }
378 }
379
380 /*****************************************************************************
381  * LoadQuantizerScale
382  *****************************************************************************
383  * Quantizer scale factor (ISO/IEC 13818-2 7.4.2.2)
384  *****************************************************************************/
385 static inline void LoadQuantizerScale( struct vpar_thread_s * p_vpar )
386 {
387     /* Quantization coefficient table */
388     static u8   pi_non_linear_quantizer_scale[32] =
389     {
390         0, 1, 2, 3, 4, 5, 6, 7, 8, 10,12,14,16,18,20, 22,
391         24,28,32,36,40,44,48,52,56,64,72,80,88,96,104,112
392     };
393     int         i_q_scale_code = GetBits( &p_vpar->bit_stream, 5 );
394
395     if( p_vpar->picture.b_q_scale_type )
396     {
397         p_vpar->mb.i_quantizer_scale =
398                         pi_non_linear_quantizer_scale[i_q_scale_code];
399     }
400     else
401     {
402         p_vpar->mb.i_quantizer_scale = i_q_scale_code << 1;
403     }
404 }
405