* all: add wmv1/2 codec support.
[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.5 2002/10/20 17:28:01 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 } bitmapinfoheader_t;
42
43
44 #if 0
45 typedef struct statistic_s
46 {
47     mtime_t i_frame_time[3];  /* total time to decode frame */
48     int     i_frame_count[3]; /* number of frame to calculate frame_time */
49     
50     int     i_frame_late[3];  /* number of frame consecutively late */
51     int     i_frame_skip[3];  /* number of frame skip */
52
53 } statistic_t;
54 #endif
55
56 typedef struct videodec_thread_s
57 {
58     decoder_fifo_t      *p_fifo;    
59
60     bitmapinfoheader_t  format;
61
62     AVCodecContext      *p_context;
63     AVCodec             *p_codec;
64     vout_thread_t       *p_vout; 
65
66     /* for post processing */
67     u32                 i_pp_mode; /* valid only with I420 and YV12 */
68     postprocessing_t    *p_pp;
69
70     char *psz_namecodec;
71
72     /* for frame skipping algo */
73 //    statistic_s statistic;
74
75     int b_hurry_up;
76     int i_frame_error;
77     int i_frame_skip;
78     int i_frame_late;  /* how may frame decoded are in late */
79
80      /* private */
81     mtime_t i_pts;
82     int     i_framesize;
83     u8      *p_framedata;
84   
85     u8      *p_buffer;     /* buffer for gather pes */
86     int     i_buffer_size; /* size of allocated p_framedata */
87             
88 } videodec_thread_t;
89
90 /* MPEG4 video */
91 #define FOURCC_DIVX         VLC_FOURCC('D','I','V','X')
92 #define FOURCC_divx         VLC_FOURCC('d','i','v','x')
93 #define FOURCC_DIV1         VLC_FOURCC('D','I','V','1')
94 #define FOURCC_div1         VLC_FOURCC('d','i','v','1')
95 #define FOURCC_MP4S         VLC_FOURCC('M','P','4','S')
96 #define FOURCC_mp4s         VLC_FOURCC('m','p','4','s')
97 #define FOURCC_M4S2         VLC_FOURCC('M','4','S','2')
98 #define FOURCC_m4s2         VLC_FOURCC('m','4','s','2')
99 #define FOURCC_xvid         VLC_FOURCC('x','v','i','d')
100 #define FOURCC_XVID         VLC_FOURCC('X','V','I','D')
101 #define FOURCC_XviD         VLC_FOURCC('X','v','i','D')
102 #define FOURCC_DX50         VLC_FOURCC('D','X','5','0')
103 #define FOURCC_mp4v         VLC_FOURCC('m','p','4','v')
104 #define FOURCC_4            VLC_FOURCC( 4,  0,  0,  0 )
105
106 /* MSMPEG4 v2 */
107 #define FOURCC_MPG4         VLC_FOURCC('M','P','G','4')
108 #define FOURCC_mpg4         VLC_FOURCC('m','p','g','4')
109 #define FOURCC_DIV2         VLC_FOURCC('D','I','V','2')
110 #define FOURCC_div2         VLC_FOURCC('d','i','v','2')
111 #define FOURCC_MP42         VLC_FOURCC('M','P','4','2')
112 #define FOURCC_mp42         VLC_FOURCC('m','p','4','2')
113
114 /* MSMPEG4 v3 / M$ mpeg4 v3 */
115 #define FOURCC_MPG3         VLC_FOURCC('M','P','G','3')
116 #define FOURCC_mpg3         VLC_FOURCC('m','p','g','3')
117 #define FOURCC_div3         VLC_FOURCC('d','i','v','3')
118 #define FOURCC_MP43         VLC_FOURCC('M','P','4','3')
119 #define FOURCC_mp43         VLC_FOURCC('m','p','4','3')
120
121 /* DivX 3.20 */
122 #define FOURCC_DIV3         VLC_FOURCC('D','I','V','3')
123 #define FOURCC_DIV4         VLC_FOURCC('D','I','V','4')
124 #define FOURCC_div4         VLC_FOURCC('d','i','v','4')
125 #define FOURCC_DIV5         VLC_FOURCC('D','I','V','5')
126 #define FOURCC_div5         VLC_FOURCC('d','i','v','5')
127 #define FOURCC_DIV6         VLC_FOURCC('D','I','V','6')
128 #define FOURCC_div6         VLC_FOURCC('d','i','v','6')
129
130 /* AngelPotion stuff */
131 #define FOURCC_AP41         VLC_FOURCC('A','P','4','1')
132
133 /* ?? */
134 #define FOURCC_3IV1         VLC_FOURCC('3','I','V','1')
135 #define FOURCC_3IV2         VLC_FOURCC('3','I','V','2')
136
137 /* H263 and H263i */        
138 #define FOURCC_H263         VLC_FOURCC('H','2','6','3')
139 #define FOURCC_h263         VLC_FOURCC('h','2','6','3')
140 #define FOURCC_U263         VLC_FOURCC('U','2','6','3')
141 #define FOURCC_I263         VLC_FOURCC('I','2','6','3')
142 #define FOURCC_i263         VLC_FOURCC('i','2','6','3')
143
144 /* Sorenson v1 */
145 #define FOURCC_SVQ1 VLC_FOURCC( 'S', 'V', 'Q', '1' )
146
147 /* wmv */
148 #define FOURCC_WMV1         VLC_FOURCC('W','M','V','1')
149 #define FOURCC_WMV2         VLC_FOURCC('W','M','V','2')
150
151 static int ffmpeg_GetFfmpegCodec( vlc_fourcc_t i_fourcc,
152                                   int *pi_ffmpeg_codec,
153                                   char **ppsz_name )
154 {
155     int i_codec = 0;
156     char *psz_name = NULL;
157
158     switch( i_fourcc )
159     {
160 #if LIBAVCODEC_BUILD >= 4608 
161         case FOURCC_DIV1:
162         case FOURCC_div1:
163         case FOURCC_MPG4:
164         case FOURCC_mpg4:
165             i_codec = CODEC_ID_MSMPEG4V1;
166             psz_name = "MS MPEG-4 v1";
167             break;
168
169         case FOURCC_DIV2:
170         case FOURCC_div2:
171         case FOURCC_MP42:
172         case FOURCC_mp42:
173             i_codec = CODEC_ID_MSMPEG4V2;
174             psz_name = "MS MPEG-4 v2";
175             break;
176 #endif
177
178         case FOURCC_MPG3:
179         case FOURCC_mpg3:
180         case FOURCC_div3:
181         case FOURCC_MP43:
182         case FOURCC_mp43:
183         case FOURCC_DIV3:
184         case FOURCC_DIV4:
185         case FOURCC_div4:
186         case FOURCC_DIV5:
187         case FOURCC_div5:
188         case FOURCC_DIV6:
189         case FOURCC_div6:
190         case FOURCC_AP41:
191         case FOURCC_3IV1:
192 #if LIBAVCODEC_BUILD >= 4608 
193             i_codec = CODEC_ID_MSMPEG4V3;
194 #else
195             i_codec = CODEC_ID_MSMPEG4;
196 #endif
197             psz_name = "MS MPEG-4 v3";
198             break;
199
200 #if LIBAVCODEC_BUILD >= 4615
201         case FOURCC_SVQ1:
202             i_codec = CODEC_ID_SVQ1;
203             psz_name = "SVQ-1 (Sorenson Video v1)";
204             break;
205 #endif
206
207         case FOURCC_DIVX:
208         case FOURCC_divx:
209         case FOURCC_MP4S:
210         case FOURCC_mp4s:
211         case FOURCC_M4S2:
212         case FOURCC_m4s2:
213         case FOURCC_xvid:
214         case FOURCC_XVID:
215         case FOURCC_XviD:
216         case FOURCC_DX50:
217         case FOURCC_mp4v:
218         case FOURCC_4:
219         case FOURCC_3IV2:
220
221             i_codec = CODEC_ID_MPEG4;
222             psz_name = "MPEG-4";
223             break;
224 /* FIXME FOURCC_H263P exist but what fourcc ? */
225         case FOURCC_H263:
226         case FOURCC_h263:
227         case FOURCC_U263:
228             i_codec = CODEC_ID_H263;
229             psz_name = "H263";
230             break;
231
232         case FOURCC_I263:
233         case FOURCC_i263:
234             i_codec = CODEC_ID_H263I;
235             psz_name = "I263.I";
236             break;
237         case FOURCC_WMV1:
238             i_codec = CODEC_ID_WMV1;
239             psz_name ="Windows Media Video 1";
240             break;
241         case FOURCC_WMV2:
242             i_codec = CODEC_ID_WMV2;
243             psz_name ="Windows Media Video 2";
244             break;
245
246     }
247
248     if( i_codec )
249     {
250         if( pi_ffmpeg_codec ) *pi_ffmpeg_codec = i_codec;
251         if( ppsz_name ) *ppsz_name = psz_name;
252         return VLC_TRUE;
253     }
254
255     return VLC_FALSE;
256 }
257
258 /* FIXME FIXME some of them are wrong */
259 static int i_ffmpeg_PixFmtToChroma[] =
260 {
261     /* PIX_FMT_ANY = -1, PIX_FMT_YUV420P, 
262        PIX_FMT_YUV422,   PIX_FMT_RGB24,   
263        PIX_FMT_BGR24,    PIX_FMT_YUV422P, 
264        PIX_FMT_YUV444P,  PIX_FMT_YUV410P 
265      */
266     0,                           VLC_FOURCC('I','4','2','0'),
267     VLC_FOURCC('I','4','2','0'), VLC_FOURCC('R','V','2','4'),
268     0,                           VLC_FOURCC('Y','4','2','2'),
269     VLC_FOURCC('I','4','4','4'), 0
270 };
271
272 static inline u32 ffmpeg_PixFmtToChroma( int i_ffmpegchroma )
273 {
274     if( ++i_ffmpegchroma > 7 )
275     {
276         return( 0 );
277     }
278     else
279     {
280         return( i_ffmpeg_PixFmtToChroma[i_ffmpegchroma] );
281     }
282 }
283
284 static inline int ffmpeg_FfAspect( int i_width, int i_height, int i_ffaspect )
285 {
286     switch( i_ffaspect )
287     {
288         case( FF_ASPECT_4_3_625 ):
289         case( FF_ASPECT_4_3_525 ):
290             return( VOUT_ASPECT_FACTOR * 4 / 3);
291         case( FF_ASPECT_16_9_625 ):
292         case( FF_ASPECT_16_9_525 ):
293             return( VOUT_ASPECT_FACTOR * 16 / 9 );
294         case( FF_ASPECT_SQUARE ):
295         default:
296             return( VOUT_ASPECT_FACTOR * i_width / i_height );
297     }
298 }
299