* add post processing modules.( As defined in MPEG4 ISO) There are C,
[vlc.git] / modules / codec / ffmpeg / ffmpeg.h
1 /*****************************************************************************
2  * ffmpeg_vdec.h: video decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: ffmpeg.h,v 1.2 2002/08/04 22:13:05 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /* Pour un flux video */
25 typedef struct bitmapinfoheader_s
26 {
27     u32 i_size; /* size of header */
28     u32 i_width;
29     u32 i_height;
30     u16 i_planes;
31     u16 i_bitcount;
32     u32 i_compression;
33     u32 i_sizeimage;
34     u32 i_xpelspermeter;
35     u32 i_ypelspermeter;
36     u32 i_clrused;
37     u32 i_clrimportant;
38
39     int i_data;
40     u8  *p_data;
41
42 } bitmapinfoheader_t;
43
44 typedef struct videodec_thread_s
45 {
46     decoder_fifo_t      *p_fifo;    
47
48     bitmapinfoheader_t  format;
49
50     AVCodecContext      context, *p_context;
51     AVCodec             *p_codec;
52     vout_thread_t       *p_vout; 
53
54     /* for post processing */
55     u32                 i_pp_mode; /* valid only with I420 and YV12 */
56     postprocessing_t    *p_pp;
57
58     char *psz_namecodec;
59
60     /* for frame skipping algo */
61     int b_hurry_up;
62     int i_frame_error;
63     int i_frame_skip;
64     int i_frame_late;  /* how may frame decoded are in late */
65
66      /* private */
67     mtime_t i_pts;
68     int     i_framesize;
69     u8      *p_framedata;
70   
71     u8      *p_buffer;     /* buffer for gather pes */
72     int     i_buffer_size; /* size of allocated p_framedata */
73             
74 } videodec_thread_t;
75
76 /* MPEG4 video */
77 #define FOURCC_DIVX         VLC_FOURCC('D','I','V','X')
78 #define FOURCC_divx         VLC_FOURCC('d','i','v','x')
79 #define FOURCC_DIV1         VLC_FOURCC('D','I','V','1')
80 #define FOURCC_div1         VLC_FOURCC('d','i','v','1')
81 #define FOURCC_MP4S         VLC_FOURCC('M','P','4','S')
82 #define FOURCC_mp4s         VLC_FOURCC('m','p','4','s')
83 #define FOURCC_M4S2         VLC_FOURCC('M','4','S','2')
84 #define FOURCC_m4s2         VLC_FOURCC('m','4','s','2')
85 #define FOURCC_xvid         VLC_FOURCC('x','v','i','d')
86 #define FOURCC_XVID         VLC_FOURCC('X','V','I','D')
87 #define FOURCC_XviD         VLC_FOURCC('X','v','i','D')
88 #define FOURCC_DX50         VLC_FOURCC('D','X','5','0')
89 #define FOURCC_mp4v         VLC_FOURCC('m','p','4','v')
90 #define FOURCC_4            VLC_FOURCC( 4,  0,  0,  0 )
91
92 /* MSMPEG4 v2 */
93 #define FOURCC_MPG4         VLC_FOURCC('M','P','G','4')
94 #define FOURCC_mpg4         VLC_FOURCC('m','p','g','4')
95 #define FOURCC_DIV2         VLC_FOURCC('D','I','V','2')
96 #define FOURCC_div2         VLC_FOURCC('d','i','v','2')
97 #define FOURCC_MP42         VLC_FOURCC('M','P','4','2')
98 #define FOURCC_mp42         VLC_FOURCC('m','p','4','2')
99
100 /* MSMPEG4 v3 / M$ mpeg4 v3 */
101 #define FOURCC_MPG3         VLC_FOURCC('M','P','G','3')
102 #define FOURCC_mpg3         VLC_FOURCC('m','p','g','3')
103 #define FOURCC_div3         VLC_FOURCC('d','i','v','3')
104 #define FOURCC_MP43         VLC_FOURCC('M','P','4','3')
105 #define FOURCC_mp43         VLC_FOURCC('m','p','4','3')
106
107 /* DivX 3.20 */
108 #define FOURCC_DIV3         VLC_FOURCC('D','I','V','3')
109 #define FOURCC_DIV4         VLC_FOURCC('D','I','V','4')
110 #define FOURCC_div4         VLC_FOURCC('d','i','v','4')
111 #define FOURCC_DIV5         VLC_FOURCC('D','I','V','5')
112 #define FOURCC_div5         VLC_FOURCC('d','i','v','5')
113 #define FOURCC_DIV6         VLC_FOURCC('D','I','V','6')
114 #define FOURCC_div6         VLC_FOURCC('d','i','v','6')
115
116 /* AngelPotion stuff */
117 #define FOURCC_AP41         VLC_FOURCC('A','P','4','1')
118
119 /* ?? */
120 #define FOURCC_3IV1         VLC_FOURCC('3','I','V','1')
121 #define FOURCC_3IV2         VLC_FOURCC('3','I','V','2')
122
123 /* H263 and H263i */        
124 #define FOURCC_H263         VLC_FOURCC('H','2','6','3')
125 #define FOURCC_h263         VLC_FOURCC('h','2','6','3')
126 #define FOURCC_U263         VLC_FOURCC('U','2','6','3')
127 #define FOURCC_I263         VLC_FOURCC('I','2','6','3')
128 #define FOURCC_i263         VLC_FOURCC('i','2','6','3')
129
130 /* Sorenson v1 */
131 #define FOURCC_SVQ1 VLC_FOURCC( 'S', 'V', 'Q', '1' )
132
133 static int ffmpeg_GetFfmpegCodec( vlc_fourcc_t i_fourcc,
134                                   int *pi_ffmpeg_codec,
135                                   char **ppsz_name )
136 {
137     int i_codec = 0;
138     char *psz_name = NULL;
139
140     switch( i_fourcc )
141     {
142 #if LIBAVCODEC_BUILD >= 4608 
143         case FOURCC_DIV1:
144         case FOURCC_div1:
145         case FOURCC_MPG4:
146         case FOURCC_mpg4:
147             i_codec = CODEC_ID_MSMPEG4V1;
148             psz_name = "MS MPEG-4 v1";
149             break;
150
151         case FOURCC_DIV2:
152         case FOURCC_div2:
153         case FOURCC_MP42:
154         case FOURCC_mp42:
155             i_codec = CODEC_ID_MSMPEG4V2;
156             psz_name = "MS MPEG-4 v2";
157             break;
158 #endif
159
160         case FOURCC_MPG3:
161         case FOURCC_mpg3:
162         case FOURCC_div3:
163         case FOURCC_MP43:
164         case FOURCC_mp43:
165         case FOURCC_DIV3:
166         case FOURCC_DIV4:
167         case FOURCC_div4:
168         case FOURCC_DIV5:
169         case FOURCC_div5:
170         case FOURCC_DIV6:
171         case FOURCC_div6:
172         case FOURCC_AP41:
173         case FOURCC_3IV1:
174 #if LIBAVCODEC_BUILD >= 4608 
175             i_codec = CODEC_ID_MSMPEG4V3;
176 #else
177             i_codec = CODEC_ID_MSMPEG4;
178 #endif
179             psz_name = "MS MPEG-4 v3";
180             break;
181
182 #if LIBAVCODEC_BUILD >= 4615
183         case FOURCC_SVQ1:
184             i_codec = CODEC_ID_SVQ1;
185             psz_name = "SVQ-1 (Sorenson Video v1)";
186             break;
187 #endif
188
189         case FOURCC_DIVX:
190         case FOURCC_divx:
191         case FOURCC_MP4S:
192         case FOURCC_mp4s:
193         case FOURCC_M4S2:
194         case FOURCC_m4s2:
195         case FOURCC_xvid:
196         case FOURCC_XVID:
197         case FOURCC_XviD:
198         case FOURCC_DX50:
199         case FOURCC_mp4v:
200         case FOURCC_4:
201         case FOURCC_3IV2:
202
203             i_codec = CODEC_ID_MPEG4;
204             psz_name = "MPEG-4";
205             break;
206 /* FIXME FOURCC_H263P exist but what fourcc ? */
207         case FOURCC_H263:
208         case FOURCC_h263:
209         case FOURCC_U263:
210             i_codec = CODEC_ID_H263;
211             psz_name = "H263";
212             break;
213
214         case FOURCC_I263:
215         case FOURCC_i263:
216             i_codec = CODEC_ID_H263I;
217             psz_name = "I263.I";
218             break;
219     }
220
221     if( i_codec )
222     {
223         if( pi_ffmpeg_codec ) *pi_ffmpeg_codec = i_codec;
224         if( ppsz_name ) *ppsz_name = psz_name;
225         return VLC_TRUE;
226     }
227
228     return VLC_FALSE;
229 }
230
231 /* FIXME FIXME some of them are wrong */
232 static int i_ffmpeg_PixFmtToChroma[] =
233 {
234     /* PIX_FMT_ANY = -1, PIX_FMT_YUV420P, 
235        PIX_FMT_YUV422,   PIX_FMT_RGB24,   
236        PIX_FMT_BGR24,    PIX_FMT_YUV422P, 
237        PIX_FMT_YUV444P,  PIX_FMT_YUV410P 
238      */
239     0,                           VLC_FOURCC('I','4','2','0'),
240     VLC_FOURCC('I','4','2','0'), VLC_FOURCC('R','V','2','4'),
241     0,                           VLC_FOURCC('Y','4','2','2'),
242     VLC_FOURCC('I','4','4','4'), 0
243 };
244
245 static inline u32 ffmpeg_PixFmtToChroma( int i_ffmpegchroma )
246 {
247     if( ++i_ffmpegchroma > 7 )
248     {
249         return( 0 );
250     }
251     else
252     {
253         return( i_ffmpeg_PixFmtToChroma[i_ffmpegchroma] );
254     }
255 }
256
257 static inline int ffmpeg_FfAspect( int i_width, int i_height, int i_ffaspect )
258 {
259     switch( i_ffaspect )
260     {
261         case( FF_ASPECT_4_3_625 ):
262         case( FF_ASPECT_4_3_525 ):
263             return( VOUT_ASPECT_FACTOR * 4 / 3);
264         case( FF_ASPECT_16_9_625 ):
265         case( FF_ASPECT_16_9_525 ):
266             return( VOUT_ASPECT_FACTOR * 16 / 9 );
267         case( FF_ASPECT_SQUARE ):
268         default:
269             return( VOUT_ASPECT_FACTOR * i_width / i_height );
270     }
271 }
272