libmpeg2.h

00001 /*
00002  * Copyright (C) 2000-2003 Michel Lespinasse <[email protected]>
00003  * Copyright (C) 1999-2000 Aaron Holtzman <[email protected]>
00004  *
00005  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
00006  * See http://libmpeg2.sourceforge.net/ for updates.
00007  *
00008  * mpeg2dec is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * mpeg2dec is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 #pragma once
00024 
00025 typedef signed char int8_t;
00026 typedef signed short int16_t;
00027 typedef signed int int32_t;
00028 
00029 typedef unsigned char uint8_t;
00030 typedef unsigned short uint16_t;
00031 typedef unsigned int uint32_t;
00032 
00033 #define MPEG2_VERSION(a,b,c) (((a)<<16)|((b)<<8)|(c))
00034 #define MPEG2_RELEASE MPEG2_VERSION (0, 3, 2)   /* 0.3.2 */
00035 
00036 #define SEQ_FLAG_MPEG2 1
00037 #define SEQ_FLAG_CONSTRAINED_PARAMETERS 2
00038 #define SEQ_FLAG_PROGRESSIVE_SEQUENCE 4
00039 #define SEQ_FLAG_LOW_DELAY 8
00040 #define SEQ_FLAG_COLOUR_DESCRIPTION 16
00041 
00042 #define SEQ_MASK_VIDEO_FORMAT 0xe0
00043 #define SEQ_VIDEO_FORMAT_COMPONENT 0
00044 #define SEQ_VIDEO_FORMAT_PAL 0x20
00045 #define SEQ_VIDEO_FORMAT_NTSC 0x40
00046 #define SEQ_VIDEO_FORMAT_SECAM 0x60
00047 #define SEQ_VIDEO_FORMAT_MAC 0x80
00048 #define SEQ_VIDEO_FORMAT_UNSPECIFIED 0xa0
00049 
00050 typedef struct {
00051     unsigned int width, height;
00052     unsigned int chroma_width, chroma_height;
00053     unsigned int byte_rate;
00054     unsigned int vbv_buffer_size;
00055     uint32_t flags;
00056 
00057     unsigned int picture_width, picture_height;
00058     unsigned int display_width, display_height;
00059     unsigned int pixel_width, pixel_height;
00060     unsigned int frame_period;
00061 
00062     uint8_t profile_level_id;
00063     uint8_t colour_primaries;
00064     uint8_t transfer_characteristics;
00065     uint8_t matrix_coefficients;
00066 
00067         void finalize();
00068 } mpeg2_sequence_t;
00069 
00070 #define GOP_FLAG_DROP_FRAME 1
00071 #define GOP_FLAG_BROKEN_LINK 2
00072 #define GOP_FLAG_CLOSED_GOP 4
00073 
00074 typedef struct {
00075     uint8_t hours;
00076     uint8_t minutes;
00077     uint8_t seconds;
00078     uint8_t pictures;
00079     uint32_t flags;
00080 } mpeg2_gop_t;
00081 
00082 #define PIC_MASK_CODING_TYPE 7
00083 #define PIC_FLAG_CODING_TYPE_I 1
00084 #define PIC_FLAG_CODING_TYPE_P 2
00085 #define PIC_FLAG_CODING_TYPE_B 3
00086 #define PIC_FLAG_CODING_TYPE_D 4
00087 
00088 #define PIC_FLAG_TOP_FIELD_FIRST 8
00089 #define PIC_FLAG_PROGRESSIVE_FRAME 16
00090 #define PIC_FLAG_COMPOSITE_DISPLAY 32
00091 #define PIC_FLAG_SKIP 64
00092 #define PIC_FLAG_PTS 128
00093 #define PIC_FLAG_REPEAT_FIRST_FIELD 256
00094 #define PIC_MASK_COMPOSITE_DISPLAY 0xfffff000
00095 
00096 typedef struct {
00097     unsigned int temporal_reference;
00098     unsigned int nb_fields;
00099     uint32_t pts;
00100     uint32_t flags;
00101     struct {
00102         int x, y;
00103     } display_offset[3];
00104         __int64 rtStart, rtStop;
00105         bool fDiscontinuity, fDelivered;
00106 } mpeg2_picture_t;
00107 
00108 typedef struct {
00109     uint8_t* buf[3];
00110     void* id;
00111 } mpeg2_fbuf_t;
00112 
00113 typedef enum {
00114     STATE_BUFFER = 0,
00115     STATE_SEQUENCE = 1,
00116     STATE_SEQUENCE_REPEATED = 2,
00117     STATE_GOP = 3,
00118     STATE_PICTURE = 4,
00119     STATE_SLICE_1ST = 5,
00120     STATE_PICTURE_2ND = 6,
00121     STATE_SLICE = 7,
00122     STATE_END = 8,
00123     STATE_INVALID = 9,
00124     STATE_PADDING = 10
00125 } mpeg2_state_t;
00126 
00129 
00130 /* macroblock modes */
00131 #define MACROBLOCK_INTRA 1
00132 #define MACROBLOCK_PATTERN 2
00133 #define MACROBLOCK_MOTION_BACKWARD 4
00134 #define MACROBLOCK_MOTION_FORWARD 8
00135 #define MACROBLOCK_QUANT 16
00136 #define DCT_TYPE_INTERLACED 32
00137 /* motion_type */
00138 #define MOTION_TYPE_MASK (3*64)
00139 #define MOTION_TYPE_BASE 64
00140 #define MC_FIELD (1*64)
00141 #define MC_FRAME (2*64)
00142 #define MC_16X8 (2*64)
00143 #define MC_DMV (3*64)
00144 
00145 /* picture structure */
00146 #define TOP_FIELD 1
00147 #define BOTTOM_FIELD 2
00148 #define FRAME_PICTURE 3
00149 
00150 /* picture coding type */
00151 #define I_TYPE 1
00152 #define P_TYPE 2
00153 #define B_TYPE 3
00154 #define D_TYPE 4
00155 
00158 
00159 typedef void mpeg2_mc_fct(uint8_t*, const uint8_t*, int, int);
00160 typedef struct {mpeg2_mc_fct* put[8]; mpeg2_mc_fct* avg[8];} mpeg2_mc_t;
00161 
00162 class CMpeg2Decoder
00163 {
00164 public:
00165     /* Motion vectors */
00166     /* The f_ and b_ correspond to the forward and backward motion */
00167     /* predictors */
00168 
00169         struct motion_t {
00170                 uint8_t* ref[2][3];
00171                 uint8_t** ref2[2];
00172                 int pmv[2][2];
00173                 int f_code[2];
00174         } m_b_motion, m_f_motion;
00175 
00176 private:
00177         int get_macroblock_modes();
00178         int get_quantizer_scale();
00179         int get_motion_delta(const int f_code);
00180         int bound_motion_vector(const int vector, const int f_code);
00181         int get_dmv();
00182         int get_coded_block_pattern();
00183         int get_luma_dc_dct_diff();
00184         int get_chroma_dc_dct_diff();
00185         
00186         void get_intra_block_B14();
00187         void get_intra_block_B15();
00188         int get_non_intra_block();
00189         void get_mpeg1_intra_block();
00190         int get_mpeg1_non_intra_block();
00191         void slice_intra_DCT(const int cc, uint8_t* dest, int stride);
00192         void slice_non_intra_DCT(uint8_t* dest, int stride);
00193 
00194         void motion_mp1(motion_t* motion, mpeg2_mc_fct * const * const table);
00195         void motion_fr_frame(motion_t* motion, mpeg2_mc_fct * const * const table);
00196         void motion_fr_field(motion_t* motion, mpeg2_mc_fct * const * const table);
00197         void motion_fr_dmv(motion_t* motion, mpeg2_mc_fct * const * const table);
00198         void motion_reuse(motion_t* motion, mpeg2_mc_fct * const * const table);
00199         void motion_zero(motion_t* motion, mpeg2_mc_fct * const * const table);
00200         void motion_fr_conceal();
00201         void motion_fi_field(motion_t * motion, mpeg2_mc_fct * const * const table);
00202         void motion_fi_16x8(motion_t* motion, mpeg2_mc_fct * const * const table);
00203         void motion_fi_dmv(motion_t* motion, mpeg2_mc_fct * const * const table);
00204         void motion_fi_conceal();
00205 
00206         int slice_init(int code);
00207 
00208         static bool m_idct_initialized;
00209         void (*m_idct_init)();
00210         void (*m_idct_copy)(int16_t* block, uint8_t* dest, const int stride);
00211         void (*m_idct_add)(const int last, int16_t* block, uint8_t* dest, const int stride);
00212         mpeg2_mc_t* m_mc;
00213 
00214 public:
00215         CMpeg2Decoder();
00216         virtual ~CMpeg2Decoder();
00217 
00218         void mpeg2_init_fbuf(uint8_t* current_fbuf[3], uint8_t* forward_fbuf[3], uint8_t* backward_fbuf[3]);
00219         void mpeg2_slice(int code, const uint8_t* buffer);
00220 
00221         int16_t* m_DCTblock;
00222 
00223     /* bit parsing stuff */
00224     uint32_t m_bitstream_buf;           /* current 32 bit working set */
00225     int m_bitstream_bits;                       /* used bits in working set */
00226     const uint8_t* m_bitstream_ptr;     /* buffer with stream data */
00227 
00228     uint8_t* m_dest[3];
00229     uint8_t* m_picture_dest[3];
00230 
00231     int m_offset, m_stride, m_uv_stride;
00232     unsigned int m_limit_x, m_limit_y_16, m_limit_y_8, m_limit_y;
00233 
00234     /* predictor for DC coefficients in intra blocks */
00235     int16_t m_dc_dct_pred[3];
00236 
00237     int m_quantizer_scale;      /* remove */
00238     int m_dmv_offset;           /* remove */
00239     unsigned int m_v_offset;    /* remove */
00240 
00241     /* now non-slice-specific information */
00242 
00243     /* sequence header stuff */
00244     uint8_t m_intra_quantizer_matrix[64];
00245     uint8_t m_non_intra_quantizer_matrix[64];
00246 
00247     /* The width and height of the picture snapped to macroblock units */
00248     int m_width, m_height;
00249     int m_vertical_position_extension;
00250 
00251     /* picture header stuff */
00252 
00253     /* what type of picture this is (I, P, B, D) */
00254     int m_coding_type;
00255 
00256     /* picture coding extension stuff */
00257 
00258     /* quantization factor for intra dc coefficients */
00259     int m_intra_dc_precision;
00260     /* top/bottom/both fields */
00261     int m_picture_structure;
00262     /* bool to indicate all predictions are frame based */
00263     int m_frame_pred_frame_dct;
00264     /* bool to indicate whether intra blocks have motion vectors */
00265     /* (for concealment) */
00266     int m_concealment_motion_vectors;
00267     /* bit to indicate which quantization table to use */
00268     int m_q_scale_type;
00269     /* bool to use different vlc tables */
00270     int m_intra_vlc_format;
00271     /* used for DMV MC */
00272     int m_top_field_first;
00273 
00274     /* stuff derived from bitstream */
00275 
00276     /* pointer to the zigzag scan we're supposed to be using */
00277     const uint8_t* m_scan;
00278 
00279     int m_second_field;
00280 
00281     int m_mpeg1;
00282 };
00283 
00284 class CMpeg2Info
00285 {
00286 public:
00287         CMpeg2Info();
00288         virtual ~CMpeg2Info();
00289 
00290         void Reset();
00291 
00292         mpeg2_sequence_t* m_sequence;
00293     mpeg2_gop_t* m_gop;
00294     mpeg2_picture_t* m_current_picture;
00295     mpeg2_picture_t* m_current_picture_2nd;
00296     mpeg2_fbuf_t* m_current_fbuf;
00297     mpeg2_picture_t* m_display_picture;
00298     mpeg2_picture_t* m_display_picture_2nd;
00299     mpeg2_fbuf_t* m_display_fbuf;
00300     mpeg2_fbuf_t* m_discard_fbuf;
00301     const uint8_t* m_user_data;
00302     int m_user_data_len;
00303 };
00304 
00305 class CMpeg2Dec
00306 {
00307         int skip_chunk(int bytes);
00308         int copy_chunk(int bytes);
00309         mpeg2_state_t seek_chunk(), seek_header(), seek_sequence();
00310 
00311         int sequence_ext();
00312         int sequence_display_ext();
00313         int quant_matrix_ext();
00314         int copyright_ext();
00315         int picture_display_ext();
00316         int picture_coding_ext();
00317 
00318 public:
00319         CMpeg2Dec();
00320         virtual ~CMpeg2Dec();
00321 
00322         void mpeg2_init();
00323         void mpeg2_close();
00324 
00325         void mpeg2_buffer(uint8_t* start, uint8_t* end);
00326         int mpeg2_getpos();
00327         mpeg2_state_t mpeg2_parse();
00328 
00329         void mpeg2_skip(int skip);
00330         void mpeg2_slice_region(int start, int end);
00331 
00332         void mpeg2_pts(uint32_t pts);
00333 
00334         /* decode.c */
00335         mpeg2_state_t mpeg2_seek_sequence();
00336         mpeg2_state_t mpeg2_parse_header();
00337 
00338         /* header.c */
00339         void mpeg2_header_state_init();
00340         int mpeg2_header_sequence();
00341         int mpeg2_header_gop();
00342         mpeg2_state_t mpeg2_header_picture_start();
00343         int mpeg2_header_picture();
00344         int mpeg2_header_extension();
00345         int mpeg2_header_user_data();
00346         void mpeg2_header_matrix_finalize();
00347         void mpeg2_header_sequence_finalize();
00348         mpeg2_state_t mpeg2_header_slice_start();
00349         mpeg2_state_t mpeg2_header_end();
00350         void mpeg2_set_fbuf(int coding_type);
00351 
00352         enum {BUFFER_SIZE = 1194 * 1024};
00353 
00354 
00355         CMpeg2Decoder m_decoder;
00356     CMpeg2Info m_info;
00357 
00358     uint32_t m_shift;
00359     int m_is_display_initialized;
00360         mpeg2_state_t (CMpeg2Dec::* m_action)();
00361     mpeg2_state_t m_state;
00362     uint32_t m_ext_state;
00363 
00364     /* allocated in init - gcc has problems allocating such big structures */
00365     uint8_t* m_chunk_buffer;
00366     /* pointer to start of the current chunk */
00367     uint8_t* m_chunk_start;
00368     /* pointer to current position in chunk_buffer */
00369     uint8_t* m_chunk_ptr;
00370     /* last start code ? */
00371     uint8_t m_code;
00372 
00373     /* PTS */
00374     uint32_t m_pts_current, m_pts_previous;
00375     int m_num_pts;
00376     int m_bytes_since_pts;
00377 
00378     bool m_first;
00379     int m_alloc_index;
00380     uint8_t m_first_decode_slice;
00381     uint8_t m_nb_decode_slices;
00382 
00383     mpeg2_sequence_t m_new_sequence;
00384     mpeg2_sequence_t m_sequence;
00385     mpeg2_gop_t m_gop;
00386     mpeg2_picture_t m_pictures[4];
00387     mpeg2_picture_t* m_picture;
00388     /*const*/ mpeg2_fbuf_t* m_fbuf[3];  /* 0: current fbuf, 1-2: prediction fbufs */
00389 
00390         mpeg2_fbuf_t m_fbuf_alloc[3];
00391 
00392     uint8_t* m_buf_start;
00393     uint8_t* m_buf_end;
00394 
00395     int16_t m_display_offset_x, m_display_offset_y;
00396 
00397     int m_copy_matrix;
00398     uint8_t m_intra_quantizer_matrix[64];
00399     uint8_t m_non_intra_quantizer_matrix[64];
00400 };

Generated on Tue Dec 13 14:47:50 2005 for guliverkli by  doxygen 1.4.5