Mercurial > mplayer.hg
annotate libmpcodecs/ve_xvid4.c @ 12052:69cf59bb739d
-aviaspect option moved to its alphabetical position, new sentences should
start on a new line, spelling fixes.
author | diego |
---|---|
date | Sun, 21 Mar 2004 22:50:49 +0000 |
parents | d870d266634e |
children | 656a1b45b309 |
rev | line source |
---|---|
11437 | 1 /***************************************************************************** |
2 * | |
3 * - XviD 1.0 export module for mplayer/mencoder - | |
4 * | |
5 * Copyright(C) 2003 Marco Belli <elcabesa@inwind.it> | |
6 * 2003 Edouard Gomez <ed.gomez@free.fr> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 * | |
22 ****************************************************************************/ | |
23 | |
24 /***************************************************************************** | |
25 * Includes | |
26 ****************************************************************************/ | |
27 | |
28 #include <stdio.h> | |
29 #include <stdlib.h> | |
30 #include <string.h> | |
31 #include <errno.h> | |
32 #include <math.h> | |
33 #include <limits.h> | |
11920 | 34 #include <time.h> |
11437 | 35 |
36 #include "../config.h" | |
37 #include "../mp_msg.h" | |
38 | |
39 #ifdef HAVE_XVID4 | |
40 | |
41 #include "codec-cfg.h" | |
42 #include "stream.h" | |
43 #include "demuxer.h" | |
44 #include "stheader.h" | |
45 | |
46 #include "muxer.h" | |
47 | |
48 #include "img_format.h" | |
49 #include "mp_image.h" | |
50 #include "vf.h" | |
51 | |
52 #include <xvid.h> | |
53 #include <stdio.h> | |
54 #include <stdarg.h> | |
55 #include <limits.h> | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
56 #include <assert.h> |
11437 | 57 |
58 #include "m_option.h" | |
59 | |
60 #define XVID_FIRST_PASS_FILENAME "xvid-twopass.stats" | |
61 #define FINE (!0) | |
62 #define BAD (!FINE) | |
63 | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
64 // Code taken from Libavcodec and ve_lavc.c to handle Aspect Ratio calculation |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
65 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
66 typedef struct XVIDRational{ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
67 int num; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
68 int den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
69 } XVIDRational; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
70 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
71 #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
72 #define ABS(a) ((a) >= 0 ? (a) : (-(a))) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
73 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
74 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
75 static int64_t xvid_gcd(int64_t a, int64_t b){ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
76 if(b) return xvid_gcd(b, a%b); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
77 else return a; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
78 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
79 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
80 static int xvid_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
81 int exact=1, sign=0; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
82 int64_t gcd; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
83 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
84 assert(den != 0); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
85 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
86 if(den < 0){ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
87 den= -den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
88 nom= -nom; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
89 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
90 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
91 if(nom < 0){ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
92 nom= -nom; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
93 sign= 1; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
94 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
95 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
96 gcd = xvid_gcd(nom, den); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
97 nom /= gcd; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
98 den /= gcd; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
99 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
100 if(nom > max || den > max){ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
101 XVIDRational a0={0,1}, a1={1,0}; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
102 exact=0; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
103 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
104 for(;;){ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
105 int64_t x= nom / den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
106 int64_t a2n= x*a1.num + a0.num; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
107 int64_t a2d= x*a1.den + a0.den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
108 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
109 if(a2n > max || a2d > max) break; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
110 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
111 nom %= den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
112 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
113 a0= a1; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
114 a1= (XVIDRational){a2n, a2d}; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
115 if(nom==0) break; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
116 x= nom; nom=den; den=x; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
117 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
118 nom= a1.num; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
119 den= a1.den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
120 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
121 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
122 assert(xvid_gcd(nom, den) == 1); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
123 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
124 if(sign) nom= -nom; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
125 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
126 *dst_nom = nom; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
127 *dst_den = den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
128 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
129 return exact; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
130 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
131 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
132 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
133 static XVIDRational xvid_d2q(double d, int max){ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
134 XVIDRational a; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
135 int exponent= MAX( (int)(log(ABS(d) + 1e-20)/log(2)), 0); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
136 int64_t den= 1LL << (61 - exponent); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
137 xvid_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
138 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
139 return a; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
140 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
141 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
142 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
143 |
11437 | 144 /***************************************************************************** |
145 * Configuration options | |
146 ****************************************************************************/ | |
147 | |
148 static int xvidenc_bitrate = 0; | |
149 static int xvidenc_pass = 0; | |
150 static float xvidenc_quantizer = 0; | |
151 | |
152 static int xvidenc_packed = 0; | |
153 static int xvidenc_closed_gop = 1; | |
154 static int xvidenc_interlaced = 0; | |
155 static int xvidenc_quarterpel = 0; | |
156 static int xvidenc_gmc = 0; | |
157 static int xvidenc_trellis = 0; | |
158 static int xvidenc_cartoon = 0; | |
159 static int xvidenc_hqacpred = 1; | |
160 static int xvidenc_chromame = 0; | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
161 static int xvidenc_chroma_opt = 0; |
11437 | 162 static int xvidenc_vhq = 0; |
163 static int xvidenc_motion = 6; | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
164 static int xvidenc_turbo = 0; |
11437 | 165 static int xvidenc_stats = 0; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
166 static int xvidenc_max_key_interval = 0; /* Let xvidcore set a 10s interval by default */ |
11437 | 167 static int xvidenc_frame_drop_ratio = 0; |
168 static int xvidenc_greyscale = 0; | |
11920 | 169 static int xvidenc_debug = 0; |
170 static int xvidenc_psnr = 0; | |
11437 | 171 |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
172 static int xvidenc_max_bframes = 2; |
11437 | 173 static int xvidenc_bquant_ratio = 150; |
174 static int xvidenc_bquant_offset = 100; | |
175 static int xvidenc_bframe_threshold = 0; | |
176 | |
177 static int xvidenc_min_quant[3] = {2, 2, 2}; | |
178 static int xvidenc_max_quant[3] = {31, 31, 31}; | |
179 static char *xvidenc_intra_matrix_file = NULL; | |
180 static char *xvidenc_inter_matrix_file = NULL; | |
181 static char *xvidenc_quant_method = NULL; | |
182 | |
183 static int xvidenc_cbr_reaction_delay_factor = 0; | |
184 static int xvidenc_cbr_averaging_period = 0; | |
185 static int xvidenc_cbr_buffer = 0; | |
186 | |
187 static int xvidenc_vbr_keyframe_boost = 0; | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
188 static int xvidenc_vbr_overflow_control_strength = 5; |
11437 | 189 static int xvidenc_vbr_curve_compression_high = 0; |
190 static int xvidenc_vbr_curve_compression_low = 0; | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
191 static int xvidenc_vbr_max_overflow_improvement = 5; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
192 static int xvidenc_vbr_max_overflow_degradation = 5; |
11437 | 193 static int xvidenc_vbr_kfreduction = 0; |
11586 | 194 static int xvidenc_vbr_kfthreshold = 0; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
195 static int xvidenc_vbr_container_frame_overhead = 24; /* mencoder uses AVI container */ |
11437 | 196 |
197 static char *xvidenc_par = NULL; | |
198 static int xvidenc_par_width = 0; | |
199 static int xvidenc_par_height = 0; | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
200 static float xvidenc_dar_aspect = 0.0f; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
201 static int xvidenc_autoaspect = 0; |
11437 | 202 |
203 m_option_t xvidencopts_conf[] = | |
204 { | |
205 /* Standard things mencoder should be able to treat directly */ | |
206 {"bitrate", &xvidenc_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
207 {"pass", &xvidenc_pass, CONF_TYPE_INT, CONF_RANGE, 1, 2, NULL}, | |
208 {"fixed_quant", &xvidenc_quantizer, CONF_TYPE_FLOAT, CONF_RANGE, 1, 31, NULL}, | |
209 | |
210 /* Features */ | |
211 {"quant_type", &xvidenc_quant_method, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
212 {"me_quality", &xvidenc_motion, CONF_TYPE_INT, CONF_RANGE, 0, 6, NULL}, | |
213 {"chroma_me", &xvidenc_chromame, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
214 {"chroma_opt", &xvidenc_chroma_opt, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11437 | 215 {"vhq", &xvidenc_vhq, CONF_TYPE_INT, CONF_RANGE, 0, 4, NULL}, |
216 {"max_bframes", &xvidenc_max_bframes, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL}, | |
217 {"bquant_ratio", &xvidenc_bquant_ratio, CONF_TYPE_INT, CONF_RANGE, 0, 200, NULL}, | |
218 {"bquant_offset", &xvidenc_bquant_offset, CONF_TYPE_INT, CONF_RANGE, 0, 200, NULL}, | |
219 {"bf_threshold", &xvidenc_bframe_threshold, CONF_TYPE_INT, CONF_RANGE, -255, 255, NULL}, | |
220 {"qpel", &xvidenc_quarterpel, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
221 {"gmc", &xvidenc_gmc, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
222 {"trellis", &xvidenc_trellis, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
223 {"packed", &xvidenc_packed, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
224 {"closed_gop", &xvidenc_closed_gop, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
11491 | 225 {"interlacing", &xvidenc_interlaced, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11437 | 226 {"cartoon", &xvidenc_cartoon, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
227 {"hq_ac", &xvidenc_hqacpred, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
228 {"frame_drop_ratio", &xvidenc_frame_drop_ratio, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
229 {"max_key_interval", &xvidenc_max_key_interval, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, | |
230 {"greyscale", &xvidenc_greyscale, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
231 {"turbo", &xvidenc_turbo, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11920 | 232 {"debug", &xvidenc_debug, CONF_TYPE_INT , 0 ,0,-1,NULL}, |
11437 | 233 {"stats", &xvidenc_stats, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11920 | 234 {"psnr", &xvidenc_psnr , CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11437 | 235 |
236 | |
237 /* section [quantizer] */ | |
238 {"min_iquant", &xvidenc_min_quant[0], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
239 {"max_iquant", &xvidenc_max_quant[0], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
240 {"min_pquant", &xvidenc_min_quant[1], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
241 {"max_pquant", &xvidenc_max_quant[1], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
242 {"min_bquant", &xvidenc_min_quant[2], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
243 {"max_bquant", &xvidenc_max_quant[2], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
244 {"quant_intra_matrix", &xvidenc_intra_matrix_file, CONF_TYPE_STRING, 0, 0, 100, NULL}, | |
245 {"quant_inter_matrix", &xvidenc_inter_matrix_file, CONF_TYPE_STRING, 0, 0, 100, NULL}, | |
246 | |
247 /* section [cbr] */ | |
11491 | 248 {"rc_reaction_delay_factor", &xvidenc_cbr_reaction_delay_factor, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, |
249 {"rc_averaging_period", &xvidenc_cbr_averaging_period, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, | |
250 {"rc_buffer", &xvidenc_cbr_buffer, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, | |
11437 | 251 |
252 /* section [vbr] */ | |
253 {"keyframe_boost", &xvidenc_vbr_keyframe_boost, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
254 {"curve_compression_high", &xvidenc_vbr_curve_compression_high, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
255 {"curve_compression_low", &xvidenc_vbr_curve_compression_low, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
256 {"overflow_control_strength", &xvidenc_vbr_overflow_control_strength, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
257 {"max_overflow_improvement", &xvidenc_vbr_max_overflow_improvement, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
258 {"max_overflow_degradation", &xvidenc_vbr_max_overflow_degradation, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
259 {"kfreduction", &xvidenc_vbr_kfreduction, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
11586 | 260 {"kfthreshold", &xvidenc_vbr_kfthreshold, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, |
11437 | 261 {"container_frame_overhead", &xvidenc_vbr_container_frame_overhead, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, |
262 | |
263 /* Section Aspect Ratio */ | |
264 {"par", &xvidenc_par, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
265 {"par_width", &xvidenc_par_width, CONF_TYPE_INT, CONF_RANGE, 0, 255, NULL}, | |
266 {"par_height", &xvidenc_par_height, CONF_TYPE_INT, CONF_RANGE, 0, 255, NULL}, | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
267 {"aspect", &xvidenc_dar_aspect, CONF_TYPE_FLOAT, CONF_RANGE, 0.1, 9.99, NULL}, |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
268 {"autoaspect", &xvidenc_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11437 | 269 |
270 /* End of the config array */ | |
271 {NULL, 0, 0, 0, 0, 0, NULL} | |
272 }; | |
273 | |
274 /***************************************************************************** | |
275 * Module private data | |
276 ****************************************************************************/ | |
277 | |
278 typedef struct _xvid_mplayer_module_t | |
279 { | |
280 /* Instance related global vars */ | |
281 void *instance; | |
282 xvid_gbl_init_t init; | |
283 xvid_enc_create_t create; | |
284 xvid_enc_frame_t frame; | |
285 xvid_plugin_single_t onepass; | |
286 xvid_plugin_2pass1_t pass1; | |
287 xvid_plugin_2pass2_t pass2; | |
288 | |
289 /* This data must survive local block scope, so here it is */ | |
290 xvid_enc_plugin_t plugins[7]; | |
291 xvid_enc_zone_t zones[1]; | |
292 | |
293 /* MPEG4 stream buffer */ | |
294 muxer_stream_t *mux; | |
295 | |
296 /* Stats accumulators */ | |
297 int frames; | |
298 long long sse_y; | |
299 long long sse_u; | |
300 long long sse_v; | |
301 | |
302 /* Min & Max PSNR */ | |
303 int min_sse_y; | |
304 int min_sse_u; | |
305 int min_sse_v; | |
11920 | 306 int min_framenum; |
11437 | 307 int max_sse_y; |
308 int max_sse_u; | |
309 int max_sse_v; | |
11920 | 310 int max_framenum; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
311 |
11920 | 312 int pixels; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
313 int d_width, d_height; |
11437 | 314 } xvid_mplayer_module_t; |
315 | |
316 static void dispatch_settings(xvid_mplayer_module_t *mod); | |
317 static int set_create_struct(xvid_mplayer_module_t *mod); | |
318 static int set_frame_struct(xvid_mplayer_module_t *mod, mp_image_t *mpi); | |
319 static const char *errorstring(int err); | |
320 | |
321 /***************************************************************************** | |
322 * Video Filter API function definitions | |
323 ****************************************************************************/ | |
324 | |
325 /*============================================================================ | |
326 * config | |
327 *==========================================================================*/ | |
328 | |
329 static int | |
330 config(struct vf_instance_s* vf, | |
331 int width, int height, int d_width, int d_height, | |
332 unsigned int flags, unsigned int outfmt) | |
333 { | |
334 int err; | |
335 xvid_mplayer_module_t *mod = (xvid_mplayer_module_t *)vf->priv; | |
336 | |
337 /* Complete the muxer initialization */ | |
338 mod->mux->bih->biWidth = width; | |
339 mod->mux->bih->biHeight = height; | |
340 mod->mux->bih->biSizeImage = | |
341 mod->mux->bih->biWidth * mod->mux->bih->biHeight * 3; | |
342 | |
343 /* Message the FourCC type */ | |
344 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
345 "videocodec: XviD (%dx%d fourcc=%x [%.4s])\n", | |
346 width, height, mod->mux->bih->biCompression, | |
347 (char *)&mod->mux->bih->biCompression); | |
348 | |
349 /*-------------------------------------------------------------------- | |
350 * Dispatch all module settings to XviD structures | |
351 *------------------------------------------------------------------*/ | |
352 | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
353 mod->d_width = d_width; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
354 mod->d_height = d_height; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
355 |
11437 | 356 dispatch_settings(mod); |
357 | |
358 /*-------------------------------------------------------------------- | |
359 * Set remaining information in the xvid_enc_create_t structure | |
360 *------------------------------------------------------------------*/ | |
361 | |
362 if(set_create_struct(mod) == BAD) | |
363 return(BAD); | |
364 | |
365 /*-------------------------------------------------------------------- | |
366 * Encoder instance creation | |
367 *------------------------------------------------------------------*/ | |
368 | |
369 err = xvid_encore(NULL, XVID_ENC_CREATE, &mod->create, NULL); | |
370 | |
371 if(err<0) { | |
372 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
373 "xvid: xvidcore returned a '%s' error\n", errorstring(err)); | |
374 return(BAD); | |
375 } | |
376 | |
377 /* Store the encoder instance into the private data */ | |
378 mod->instance = mod->create.handle; | |
379 | |
380 return(FINE); | |
381 } | |
382 | |
383 /*============================================================================ | |
384 * uninit | |
385 *==========================================================================*/ | |
386 | |
387 static void | |
388 uninit(struct vf_instance_s* vf) | |
389 { | |
390 | |
391 xvid_mplayer_module_t *mod = (xvid_mplayer_module_t *)vf->priv; | |
392 | |
393 /* Destroy xvid instance */ | |
394 xvid_encore(mod->instance, XVID_ENC_DESTROY, NULL, NULL); | |
395 | |
396 /* Display stats */ | |
397 if(mod->frames) { | |
398 mod->sse_y /= mod->frames; | |
399 mod->sse_u /= mod->frames; | |
400 mod->sse_v /= mod->frames; | |
401 | |
402 #define SSE2PSNR(sse, nbpixels) \ | |
403 ((!(sse)) ? 99.99f : 48.131f - 10*(double)log10((double)(sse)/(double)((nbpixels)))) | |
404 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
405 "The value 99.99dB is a special value and represents " | |
406 "the upper range limit\n"); | |
407 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
11920 | 408 "xvid: Min PSNR y : %.2f dB, u : %.2f dB, v : %.2f dB, in frame %d\n", |
409 SSE2PSNR(mod->max_sse_y, mod->pixels), | |
410 SSE2PSNR(mod->max_sse_u, mod->pixels/4), | |
411 SSE2PSNR(mod->max_sse_v, mod->pixels/4), | |
412 mod->max_framenum); | |
11437 | 413 mp_msg(MSGT_MENCODER, MSGL_INFO, |
11920 | 414 "xvid: Average PSNR y : %.2f dB, u : %.2f dB, v : %.2f dB, for %d frames\n", |
415 SSE2PSNR(mod->sse_y, mod->pixels), | |
416 SSE2PSNR(mod->sse_u, mod->pixels/4), | |
417 SSE2PSNR(mod->sse_v, mod->pixels/4), | |
418 mod->frames); | |
11437 | 419 mp_msg(MSGT_MENCODER, MSGL_INFO, |
11920 | 420 "xvid: Max PSNR y : %.2f dB, u : %.2f dB, v : %.2f dB, in frame %d\n", |
421 SSE2PSNR(mod->min_sse_y, mod->pixels), | |
422 SSE2PSNR(mod->min_sse_u, mod->pixels/4), | |
423 SSE2PSNR(mod->min_sse_v, mod->pixels/4), | |
424 mod->min_framenum); | |
11437 | 425 } |
426 | |
427 /* ToDo: free matrices, and some string settings (quant method, matrix | |
428 * filenames...) */ | |
429 | |
430 return; | |
431 } | |
432 | |
433 /*============================================================================ | |
434 * control | |
435 *==========================================================================*/ | |
436 | |
437 static int | |
438 control(struct vf_instance_s* vf, int request, void* data) | |
439 { | |
440 return(CONTROL_UNKNOWN); | |
441 } | |
442 | |
443 /*============================================================================ | |
444 * query_format | |
445 *==========================================================================*/ | |
446 | |
447 static int | |
448 query_format(struct vf_instance_s* vf, unsigned int fmt) | |
449 { | |
450 switch(fmt){ | |
451 case IMGFMT_YV12: | |
452 case IMGFMT_IYUV: | |
453 case IMGFMT_I420: | |
454 return(VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW); | |
455 case IMGFMT_YUY2: | |
456 case IMGFMT_UYVY: | |
457 return(VFCAP_CSP_SUPPORTED); | |
458 } | |
459 return(BAD); | |
460 } | |
461 | |
462 /*============================================================================ | |
463 * put_image | |
464 *==========================================================================*/ | |
465 | |
466 static int | |
467 put_image(struct vf_instance_s* vf, mp_image_t *mpi) | |
468 { | |
469 int size; | |
470 xvid_enc_stats_t stats; | |
471 xvid_mplayer_module_t *mod = (xvid_mplayer_module_t *)vf->priv; | |
472 | |
473 /* Prepare the stats */ | |
474 memset(&stats,0,sizeof( xvid_enc_stats_t)); | |
475 stats.version = XVID_VERSION; | |
476 | |
477 /* ------------------------------------------------------------------- | |
478 * Set remaining information in the xvid_enc_frame_t structure | |
479 * NB: all the other struct members were initialized by | |
480 * dispatch_settings | |
481 * -----------------------------------------------------------------*/ | |
482 | |
483 if(set_frame_struct(mod, mpi) == BAD) | |
484 return(BAD); | |
485 | |
486 /* ------------------------------------------------------------------- | |
487 * Encode the frame | |
488 * ---------------------------------------------------------------- */ | |
489 | |
490 size = xvid_encore(mod->instance, XVID_ENC_ENCODE, &mod->frame, &stats); | |
491 | |
492 /* Analyse the returned value */ | |
493 if(size<0) { | |
494 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
495 "xvid: xvidcore returned a '%s' error\n", errorstring(size)); | |
496 return(BAD); | |
497 } | |
498 | |
499 /* If size is == 0, we're done with that frame */ | |
500 if(size == 0) return(FINE); | |
501 | |
11586 | 502 /* Did xvidcore returned stats about an encoded frame ? (asynchronous) */ |
11437 | 503 if(xvidenc_stats && stats.type > 0) { |
504 mod->sse_y += stats.sse_y; | |
505 mod->sse_u += stats.sse_u; | |
506 mod->sse_v += stats.sse_v; | |
507 | |
508 if(mod->min_sse_y > stats.sse_y) { | |
509 mod->min_sse_y = stats.sse_y; | |
510 mod->min_sse_u = stats.sse_u; | |
511 mod->min_sse_v = stats.sse_v; | |
11920 | 512 mod->min_framenum = mod->frames; |
11437 | 513 } |
514 | |
515 if(mod->max_sse_y < stats.sse_y) { | |
516 mod->max_sse_y = stats.sse_y; | |
517 mod->max_sse_u = stats.sse_u; | |
518 mod->max_sse_v = stats.sse_v; | |
11920 | 519 mod->max_framenum = mod->frames; |
11437 | 520 } |
11920 | 521 if (xvidenc_psnr) { |
522 static FILE *fvstats = NULL; | |
523 char filename[20]; | |
524 | |
525 if (!fvstats) { | |
526 time_t today2; | |
527 struct tm *today; | |
528 today2 = time (NULL); | |
529 today = localtime (&today2); | |
530 sprintf (filename, "psnr_%02d%02d%02d.log", today->tm_hour, today->tm_min, today->tm_sec); | |
531 fvstats = fopen (filename,"w"); | |
532 if (!fvstats) { | |
533 perror ("fopen"); | |
534 xvidenc_psnr = 0; // disable block | |
535 } | |
536 } | |
537 fprintf (fvstats, "%6d, %2d, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n", | |
538 mod->frames, | |
539 stats.quant, | |
540 stats.length, | |
541 SSE2PSNR (stats.sse_y, mod->pixels), | |
542 SSE2PSNR (stats.sse_u, mod->pixels / 4), | |
543 SSE2PSNR (stats.sse_v, mod->pixels / 4), | |
544 SSE2PSNR (stats.sse_y + stats.sse_u + stats.sse_v,(double)mod->pixels * 1.5), | |
545 stats.type==1?'I':stats.type==2?'P':stats.type==3?'B':stats.type?'S':'?' | |
546 ); | |
547 } | |
548 mod->frames++; | |
549 | |
11437 | 550 } |
11920 | 551 #undef SSE2PSNR |
11437 | 552 |
553 /* xvidcore outputed bitstream -- mux it */ | |
554 muxer_write_chunk(mod->mux, | |
555 size, | |
556 (mod->frame.out_flags & XVID_KEYFRAME)?0x10:0); | |
557 | |
558 return(FINE); | |
559 } | |
560 | |
561 /*============================================================================ | |
562 * vf_open | |
563 *==========================================================================*/ | |
564 | |
565 static int | |
566 vf_open(vf_instance_t *vf, char* args) | |
567 { | |
568 xvid_mplayer_module_t *mod; | |
569 xvid_gbl_init_t xvid_gbl_init; | |
570 xvid_gbl_info_t xvid_gbl_info; | |
571 | |
572 /* Setting libmpcodec module API pointers */ | |
573 vf->config = config; | |
574 vf->control = control; | |
575 vf->uninit = uninit; | |
576 vf->query_format = query_format; | |
577 vf->put_image = put_image; | |
578 | |
579 /* Allocate the private part of the codec module */ | |
580 vf->priv = malloc(sizeof(xvid_mplayer_module_t)); | |
581 mod = (xvid_mplayer_module_t*)vf->priv; | |
582 | |
583 if(mod == NULL) { | |
584 mp_msg(MSGT_MENCODER,MSGL_ERR, | |
585 "xvid: memory allocation failure (private data)\n"); | |
586 return(BAD); | |
587 } | |
588 | |
589 /* Initialize the module to zeros */ | |
590 memset(mod, 0, sizeof(xvid_mplayer_module_t)); | |
591 mod->min_sse_y = mod->min_sse_u = mod->min_sse_v = INT_MAX; | |
592 mod->max_sse_y = mod->max_sse_u = mod->max_sse_v = INT_MIN; | |
593 | |
594 /* Bind the Muxer */ | |
595 mod->mux = (muxer_stream_t*)args; | |
596 | |
597 /* Initialize muxer BITMAP header */ | |
598 mod->mux->bih = malloc(sizeof(BITMAPINFOHEADER)); | |
599 | |
600 if(mod->mux->bih == NULL) { | |
601 mp_msg(MSGT_MENCODER,MSGL_ERR, | |
602 "xvid: memory allocation failure (BITMAP header)\n"); | |
603 return(BAD); | |
604 } | |
605 | |
606 mod->mux->bih->biSize = sizeof(BITMAPINFOHEADER); | |
607 mod->mux->bih->biWidth = 0; | |
608 mod->mux->bih->biHeight = 0; | |
609 mod->mux->bih->biPlanes = 1; | |
610 mod->mux->bih->biBitCount = 24; | |
611 mod->mux->bih->biCompression = mmioFOURCC('X','V','I','D'); | |
612 | |
613 /* Retrieve information about the host XviD library */ | |
614 memset(&xvid_gbl_info, 0, sizeof(xvid_gbl_info_t)); | |
615 xvid_gbl_info.version = XVID_VERSION; | |
616 | |
617 if (xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL) < 0) { | |
618 mp_msg(MSGT_MENCODER,MSGL_INFO, "xvid: could not get information about the library\n"); | |
619 } else { | |
620 mp_msg(MSGT_MENCODER,MSGL_INFO, "xvid: using library version %d.%d.%d (build %s)\n", | |
621 XVID_VERSION_MAJOR(xvid_gbl_info.actual_version), | |
622 XVID_VERSION_MINOR(xvid_gbl_info.actual_version), | |
623 XVID_VERSION_PATCH(xvid_gbl_info.actual_version), | |
624 xvid_gbl_info.build); | |
625 } | |
626 | |
627 /* Initialize the xvid_gbl_init structure */ | |
628 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t)); | |
629 xvid_gbl_init.version = XVID_VERSION; | |
11920 | 630 xvid_gbl_init.debug = xvidenc_debug; |
11437 | 631 |
632 /* Initialize the xvidcore library */ | |
633 if (xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL) < 0) { | |
634 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: initialisation failure\n"); | |
635 return(BAD); | |
636 } | |
637 | |
638 return(FINE); | |
639 } | |
640 | |
641 /***************************************************************************** | |
642 * Helper functions | |
643 ****************************************************************************/ | |
644 | |
645 static void *read_matrix(unsigned char *filename); | |
646 | |
647 static void dispatch_settings(xvid_mplayer_module_t *mod) | |
648 { | |
649 xvid_enc_create_t *create = &mod->create; | |
650 xvid_enc_frame_t *frame = &mod->frame; | |
651 xvid_plugin_single_t *onepass = &mod->onepass; | |
652 xvid_plugin_2pass2_t *pass2 = &mod->pass2; | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
653 XVIDRational ar; |
11437 | 654 |
655 const int motion_presets[7] = | |
656 { | |
657 0, | |
658 0, | |
659 0, | |
660 0, | |
661 XVID_ME_HALFPELREFINE16, | |
662 XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND16, | |
663 XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | | |
664 XVID_ME_HALFPELREFINE8 | XVID_ME_USESQUARES16 | |
665 }; | |
666 | |
667 | |
668 /* ------------------------------------------------------------------- | |
669 * Dispatch all settings having an impact on the "create" structure | |
670 * This includes plugins as they are passed to encore through the | |
671 * create structure | |
672 * -----------------------------------------------------------------*/ | |
673 | |
674 /* ------------------------------------------------------------------- | |
675 * The create structure | |
676 * ---------------------------------------------------------------- */ | |
677 | |
678 create->global = 0; | |
679 | |
680 if(xvidenc_packed) | |
681 create->global |= XVID_GLOBAL_PACKED; | |
682 | |
683 if(xvidenc_closed_gop) | |
684 create->global |= XVID_GLOBAL_CLOSED_GOP; | |
685 | |
11920 | 686 if(xvidenc_psnr) |
687 xvidenc_stats = 1; | |
688 | |
11437 | 689 if(xvidenc_stats) |
690 create->global |= XVID_GLOBAL_EXTRASTATS_ENABLE; | |
691 | |
692 create->num_zones = 0; | |
693 create->zones = NULL; | |
694 create->num_plugins = 0; | |
695 create->plugins = NULL; | |
696 create->num_threads = 0; | |
697 create->max_bframes = xvidenc_max_bframes; | |
698 create->bquant_ratio = xvidenc_bquant_ratio; | |
699 create->bquant_offset = xvidenc_bquant_offset; | |
700 create->max_key_interval = xvidenc_max_key_interval; | |
701 create->frame_drop_ratio = xvidenc_frame_drop_ratio; | |
702 create->min_quant[0] = xvidenc_min_quant[0]; | |
703 create->min_quant[1] = xvidenc_min_quant[1]; | |
704 create->min_quant[2] = xvidenc_min_quant[2]; | |
705 create->max_quant[0] = xvidenc_max_quant[0]; | |
706 create->max_quant[1] = xvidenc_max_quant[1]; | |
707 create->max_quant[2] = xvidenc_max_quant[2]; | |
708 | |
709 | |
710 /* ------------------------------------------------------------------- | |
711 * The single pass plugin | |
712 * ---------------------------------------------------------------- */ | |
713 | |
714 onepass->bitrate = xvidenc_bitrate; | |
715 onepass->reaction_delay_factor = xvidenc_cbr_reaction_delay_factor; | |
716 onepass->averaging_period = xvidenc_cbr_averaging_period; | |
717 onepass->buffer = xvidenc_cbr_buffer; | |
718 | |
719 /* ------------------------------------------------------------------- | |
720 * The pass2 plugin | |
721 * ---------------------------------------------------------------- */ | |
722 | |
723 pass2->keyframe_boost = xvidenc_vbr_keyframe_boost; | |
724 pass2->overflow_control_strength = xvidenc_vbr_overflow_control_strength; | |
725 pass2->curve_compression_high = xvidenc_vbr_curve_compression_high; | |
726 pass2->curve_compression_low = xvidenc_vbr_curve_compression_low; | |
727 pass2->max_overflow_improvement = xvidenc_vbr_max_overflow_improvement; | |
728 pass2->max_overflow_degradation = xvidenc_vbr_max_overflow_degradation; | |
729 pass2->kfreduction = xvidenc_vbr_kfreduction; | |
11586 | 730 pass2->kfthreshold = xvidenc_vbr_kfthreshold; |
11437 | 731 pass2->container_frame_overhead = xvidenc_vbr_container_frame_overhead; |
732 | |
733 /* ------------------------------------------------------------------- | |
734 * The frame structure | |
735 * ---------------------------------------------------------------- */ | |
736 frame->vol_flags = 0; | |
737 frame->vop_flags = 0; | |
738 frame->motion = 0; | |
739 | |
740 frame->vop_flags |= XVID_VOP_HALFPEL; | |
741 frame->motion |= motion_presets[xvidenc_motion]; | |
742 | |
743 if(xvidenc_stats) | |
744 frame->vol_flags |= XVID_VOL_EXTRASTATS; | |
745 | |
746 if(xvidenc_greyscale) | |
747 frame->vop_flags |= XVID_VOP_GREYSCALE; | |
748 | |
749 if(xvidenc_cartoon) { | |
750 frame->vop_flags |= XVID_VOP_CARTOON; | |
751 frame->motion |= XVID_ME_DETECT_STATIC_MOTION; | |
752 } | |
753 | |
754 if(xvidenc_intra_matrix_file != NULL) { | |
755 frame->quant_intra_matrix = (unsigned char*)read_matrix(xvidenc_intra_matrix_file); | |
756 if(frame->quant_intra_matrix != NULL) { | |
757 fprintf(stderr, "xvid: Loaded Intra matrix (switching to mpeg quantization type)\n"); | |
758 if(xvidenc_quant_method) free(xvidenc_quant_method); | |
759 xvidenc_quant_method = strdup("mpeg"); | |
760 } | |
761 } | |
762 if(xvidenc_inter_matrix_file != NULL) { | |
763 frame->quant_inter_matrix = read_matrix(xvidenc_inter_matrix_file); | |
764 if(frame->quant_inter_matrix) { | |
765 fprintf(stderr, "\nxvid: Loaded Inter matrix (switching to mpeg quantization type)\n"); | |
766 if(xvidenc_quant_method) free(xvidenc_quant_method); | |
767 xvidenc_quant_method = strdup("mpeg"); | |
768 } | |
769 } | |
770 if(xvidenc_quant_method != NULL && !strcasecmp(xvidenc_quant_method, "mpeg")) { | |
771 frame->vol_flags |= XVID_VOL_MPEGQUANT; | |
772 } | |
773 if(xvidenc_quarterpel) { | |
774 frame->vol_flags |= XVID_VOL_QUARTERPEL; | |
775 frame->motion |= XVID_ME_QUARTERPELREFINE16; | |
776 frame->motion |= XVID_ME_QUARTERPELREFINE8; | |
777 } | |
778 if(xvidenc_gmc) { | |
779 frame->vol_flags |= XVID_VOL_GMC; | |
780 frame->motion |= XVID_ME_GME_REFINE; | |
781 } | |
782 if(xvidenc_interlaced) { | |
783 frame->vol_flags |= XVID_VOL_INTERLACING; | |
784 } | |
785 if(xvidenc_trellis) { | |
786 frame->vop_flags |= XVID_VOP_TRELLISQUANT; | |
787 } | |
788 if(xvidenc_hqacpred) { | |
789 frame->vop_flags |= XVID_VOP_HQACPRED; | |
790 } | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
791 if(xvidenc_chroma_opt) { |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
792 frame->vop_flags |= XVID_VOP_CHROMAOPT; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
793 } |
11437 | 794 if(xvidenc_motion > 4) { |
795 frame->vop_flags |= XVID_VOP_INTER4V; | |
796 } | |
797 if(xvidenc_chromame) { | |
798 frame->motion |= XVID_ME_CHROMA_PVOP; | |
799 frame->motion |= XVID_ME_CHROMA_BVOP; | |
800 } | |
801 if(xvidenc_vhq >= 1) { | |
802 frame->vop_flags |= XVID_VOP_MODEDECISION_RD; | |
803 } | |
804 if(xvidenc_vhq >= 2) { | |
805 frame->motion |= XVID_ME_HALFPELREFINE16_RD; | |
806 frame->motion |= XVID_ME_QUARTERPELREFINE16_RD; | |
807 } | |
808 if(xvidenc_vhq >= 3) { | |
809 frame->motion |= XVID_ME_HALFPELREFINE8_RD; | |
810 frame->motion |= XVID_ME_QUARTERPELREFINE8_RD; | |
811 frame->motion |= XVID_ME_CHECKPREDICTION_RD; | |
812 } | |
813 if(xvidenc_vhq >= 4) { | |
814 frame->motion |= XVID_ME_EXTSEARCH_RD; | |
815 } | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
816 if(xvidenc_turbo) { |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
817 frame->motion |= XVID_ME_FASTREFINE16; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
818 frame->motion |= XVID_ME_FASTREFINE8; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
819 frame->motion |= XVID_ME_SKIP_DELTASEARCH; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
820 frame->motion |= XVID_ME_FAST_MODEINTERPOLATE; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
821 frame->motion |= XVID_ME_BFRAME_EARLYSTOP; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
822 } |
11437 | 823 |
824 /* motion level == 0 means no motion search which is equivalent to | |
825 * intra coding only */ | |
826 if(xvidenc_motion == 0) { | |
827 frame->type = XVID_TYPE_IVOP; | |
828 } else { | |
829 frame->type = XVID_TYPE_AUTO; | |
830 } | |
831 | |
832 frame->bframe_threshold = xvidenc_bframe_threshold; | |
833 | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
834 /* PAR related initialization */ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
835 frame->par = XVID_PAR_11_VGA; /* Default */ |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
836 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
837 if(xvidenc_dar_aspect > 0) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
838 ar = xvid_d2q(xvidenc_dar_aspect * mod->mux->bih->biHeight / mod->mux->bih->biWidth, 255); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
839 else if(xvidenc_autoaspect) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
840 ar = xvid_d2q((float)mod->d_width / mod->d_height * mod->mux->bih->biHeight / mod->mux->bih->biWidth, 255); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
841 else ar.num = ar.den = 0; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
842 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
843 if(ar.den != 0) { |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
844 if(ar.num == 12 && ar.den == 11) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
845 frame->par = XVID_PAR_43_PAL; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
846 else if(ar.num == 10 && ar.den == 11) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
847 frame->par = XVID_PAR_43_NTSC; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
848 else if(ar.num == 16 && ar.den == 11) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
849 frame->par = XVID_PAR_169_PAL; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
850 else if(ar.num == 40 && ar.den == 33) |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
851 frame->par = XVID_PAR_169_NTSC; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
852 else |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
853 { |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
854 frame->par = XVID_PAR_EXT; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
855 frame->par_width = ar.num; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
856 frame->par_height= ar.den; |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
857 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
858 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
859 mp_msg(MSGT_MENCODER, MSGL_INFO, "XVID4_DAR: %d/%d code %d, Display frame: (%d, %d), original frame: (%d, %d)\n", |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
860 ar.num, ar.den, frame->par, |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
861 mod->d_width, mod->d_height, mod->mux->bih->biWidth, mod->mux->bih->biHeight); |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
862 } else if(xvidenc_par != NULL) { |
11437 | 863 if(strcasecmp(xvidenc_par, "pal43") == 0) |
864 frame->par = XVID_PAR_43_PAL; | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
865 else if(strcasecmp(xvidenc_par, "pal169") == 0) |
11437 | 866 frame->par = XVID_PAR_169_PAL; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
867 else if(strcasecmp(xvidenc_par, "ntsc43") == 0) |
11437 | 868 frame->par = XVID_PAR_43_NTSC; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
869 else if(strcasecmp(xvidenc_par, "ntsc169") == 0) |
11437 | 870 frame->par = XVID_PAR_169_NTSC; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
871 else if(strcasecmp(xvidenc_par, "ext") == 0) |
11437 | 872 frame->par = XVID_PAR_EXT; |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
873 |
11437 | 874 if(frame->par == XVID_PAR_EXT) { |
875 if(xvidenc_par_width) | |
876 frame->par_width = xvidenc_par_width; | |
877 else | |
878 frame->par_width = 1; | |
879 | |
880 if(xvidenc_par_height) | |
881 frame->par_height = xvidenc_par_height; | |
882 else | |
883 frame->par_height = 1; | |
884 } | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
885 } |
11437 | 886 return; |
887 } | |
888 | |
889 static int set_create_struct(xvid_mplayer_module_t *mod) | |
890 { | |
891 int pass; | |
892 xvid_enc_create_t *create = &mod->create; | |
893 | |
894 /* Most of the structure is initialized by dispatch settings, only a | |
895 * few things are missing */ | |
896 create->version = XVID_VERSION; | |
897 | |
898 /* Width and Height */ | |
899 create->width = mod->mux->bih->biWidth; | |
900 create->height = mod->mux->bih->biHeight; | |
901 | |
11920 | 902 /* Pixels are needed for PSNR calculations */ |
903 mod->pixels = create->width * create->height; | |
904 | |
11437 | 905 /* FPS */ |
906 create->fincr = mod->mux->h.dwScale; | |
907 create->fbase = mod->mux->h.dwRate; | |
908 | |
909 /* Encodings zones */ | |
910 memset(mod->zones, 0, sizeof(mod->zones)); | |
911 create->zones = mod->zones; | |
912 create->num_zones = 0; | |
913 | |
914 /* Plugins */ | |
915 memset(mod->plugins, 0, sizeof(mod->plugins)); | |
916 create->plugins = mod->plugins; | |
917 create->num_plugins = 0; | |
918 | |
919 /* ------------------------------------------------------------------- | |
920 * Initialize and bind the right rate controller plugin | |
921 * ---------------------------------------------------------------- */ | |
922 | |
923 /* First we try to sort out configuration conflicts */ | |
924 if(xvidenc_quantizer != 0 && (xvidenc_bitrate || xvidenc_pass)) { | |
925 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
926 "xvid: you can't mix Fixed Quantizer Rate Control" | |
927 " with other Rate Control mechanisms\n"); | |
928 return(BAD); | |
929 } | |
930 | |
931 if(xvidenc_bitrate != 0 && xvidenc_pass == 1) { | |
932 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
933 "xvid: bitrate setting is ignored during first pass\n"); | |
934 } | |
935 | |
936 /* Sort out which sort of pass we are supposed to do | |
937 * pass == 1<<0 CBR | |
938 * pass == 1<<1 Two pass first pass | |
939 * pass == 1<<2 Two pass second pass | |
940 * pass == 1<<3 Constant quantizer | |
941 */ | |
942 #define MODE_CBR (1<<0) | |
943 #define MODE_2PASS1 (1<<1) | |
944 #define MODE_2PASS2 (1<<2) | |
945 #define MODE_QUANT (1<<3) | |
946 | |
947 pass = 0; | |
948 | |
949 if(xvidenc_bitrate != 0 && xvidenc_pass == 0) | |
950 pass |= MODE_CBR; | |
951 | |
952 if(xvidenc_pass == 1) | |
953 pass |= MODE_2PASS1; | |
954 | |
955 if(xvidenc_bitrate != 0 && xvidenc_pass == 2) | |
956 pass |= MODE_2PASS2; | |
957 | |
958 if(xvidenc_quantizer != 0 && xvidenc_pass == 0) | |
959 pass |= MODE_QUANT; | |
960 | |
961 /* We must be in at least one RC mode */ | |
962 if(pass == 0) { | |
963 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
964 "xvid: you must specify one or a valid combination of " | |
965 "'bitrate', 'pass', 'quantizer' settings\n"); | |
966 return(BAD); | |
967 } | |
968 | |
969 /* Sanity checking */ | |
970 if(pass != MODE_CBR && pass != MODE_QUANT && | |
971 pass != MODE_2PASS1 && pass != MODE_2PASS2) { | |
972 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
973 "xvid: this code should not be reached - fill a bug " | |
974 "report\n"); | |
975 return(BAD); | |
976 } | |
977 | |
978 /* This is a single pass encoding: either a CBR pass or a constant | |
979 * quantizer pass */ | |
980 if(pass == MODE_CBR || pass == MODE_QUANT) { | |
981 xvid_plugin_single_t *onepass = &mod->onepass; | |
982 | |
983 /* There is not much left to initialize after dispatch settings */ | |
984 onepass->version = XVID_VERSION; | |
985 onepass->bitrate = xvidenc_bitrate*1000; | |
986 | |
987 /* Quantizer mode uses the same plugin, we have only to define | |
988 * a constant quantizer zone beginning at frame 0 */ | |
989 if(pass == MODE_QUANT) { | |
11929 | 990 XVIDRational squant; |
991 squant = xvid_d2q(xvidenc_quantizer,128); | |
11437 | 992 |
993 create->zones[create->num_zones].mode = XVID_ZONE_QUANT; | |
994 create->zones[create->num_zones].frame = 0; | |
11929 | 995 create->zones[create->num_zones].base = squant.num; |
996 create->zones[create->num_zones].increment = squant.den; | |
11437 | 997 create->num_zones++; |
998 | |
999 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1000 "xvid: Fixed Quant Rate Control -- quantizer=%d/%d=%2.2f\n", | |
11929 | 1001 squant.num, |
1002 squant.den, | |
1003 (float)(squant.num)/(float)(squant.den)); | |
11437 | 1004 |
1005 } else { | |
1006 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1007 "xvid: CBR Rate Control -- bitrate=%dkbit/s\n", | |
1008 xvidenc_bitrate); | |
1009 } | |
1010 | |
1011 create->plugins[create->num_plugins].func = xvid_plugin_single; | |
1012 create->plugins[create->num_plugins].param = onepass; | |
1013 create->num_plugins++; | |
1014 } | |
1015 | |
1016 /* This is the first pass of a Two pass process */ | |
1017 if(pass == MODE_2PASS1) { | |
1018 xvid_plugin_2pass1_t *pass1 = &mod->pass1; | |
1019 | |
1020 /* There is not much to initialize for this plugin */ | |
1021 pass1->version = XVID_VERSION; | |
1022 pass1->filename = XVID_FIRST_PASS_FILENAME; | |
1023 | |
1024 create->plugins[create->num_plugins].func = xvid_plugin_2pass1; | |
1025 create->plugins[create->num_plugins].param = pass1; | |
1026 create->num_plugins++; | |
1027 | |
1028 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1029 "xvid: 2Pass Rate Control -- 1st pass\n"); | |
1030 } | |
1031 | |
1032 /* This is the second pass of a Two pass process */ | |
1033 if(pass == MODE_2PASS2) { | |
1034 xvid_plugin_2pass2_t *pass2 = &mod->pass2; | |
1035 | |
1036 /* There is not much left to initialize after dispatch settings */ | |
1037 pass2->version = XVID_VERSION; | |
1038 pass2->filename = XVID_FIRST_PASS_FILENAME; | |
1039 | |
1040 /* Positive bitrate values are bitrates as usual but if the | |
1041 * value is negative it is considered as being a total size | |
1042 * to reach (in kilobytes) */ | |
1043 if(xvidenc_bitrate > 0) { | |
1044 pass2->bitrate = xvidenc_bitrate*1000; | |
1045 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1046 "xvid: 2Pass Rate Control -- 2nd pass -- bitrate=%dkbit/s\n", | |
1047 xvidenc_bitrate); | |
1048 } else { | |
1049 pass2->bitrate = xvidenc_bitrate; | |
1050 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1051 "xvid: 2Pass Rate Control -- 2nd pass -- total size=%dkB\n", | |
1052 -xvidenc_bitrate); | |
1053 } | |
1054 | |
1055 create->plugins[create->num_plugins].func = xvid_plugin_2pass2; | |
1056 create->plugins[create->num_plugins].param = pass2; | |
1057 create->num_plugins++; | |
1058 } | |
1059 | |
1060 return(FINE); | |
1061 } | |
1062 | |
1063 static int set_frame_struct(xvid_mplayer_module_t *mod, mp_image_t *mpi) | |
1064 { | |
1065 xvid_enc_frame_t *frame = &mod->frame; | |
1066 | |
1067 /* Most of the initialization is done during dispatch_settings */ | |
1068 frame->version = XVID_VERSION; | |
1069 | |
1070 /* Bind output buffer */ | |
1071 frame->bitstream = mod->mux->buffer; | |
1072 frame->length = -1; | |
1073 | |
1074 /* Frame format */ | |
1075 switch(mpi->imgfmt) { | |
1076 case IMGFMT_YV12: | |
1077 case IMGFMT_IYUV: | |
1078 case IMGFMT_I420: | |
1079 frame->input.csp = XVID_CSP_USER; | |
1080 break; | |
1081 case IMGFMT_YUY2: | |
1082 frame->input.csp = XVID_CSP_YUY2; | |
1083 break; | |
1084 case IMGFMT_UYVY: | |
1085 frame->input.csp = XVID_CSP_UYVY; | |
1086 break; | |
1087 default: | |
1088 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
1089 "xvid: unsupported picture format (%s)!\n", | |
1090 vo_format_name(mpi->imgfmt)); | |
1091 return(BAD); | |
1092 } | |
1093 | |
1094 /* Bind source frame */ | |
1095 frame->input.plane[0] = mpi->planes[0]; | |
1096 frame->input.plane[1] = mpi->planes[1]; | |
1097 frame->input.plane[2] = mpi->planes[2]; | |
1098 frame->input.stride[0] = mpi->stride[0]; | |
1099 frame->input.stride[1] = mpi->stride[1]; | |
1100 frame->input.stride[2] = mpi->stride[2]; | |
1101 | |
1102 /* Force the right quantizer -- It is internally managed by RC | |
1103 * plugins */ | |
1104 frame->quant = 0; | |
1105 | |
1106 return(FINE); | |
1107 } | |
1108 | |
1109 static void *read_matrix(unsigned char *filename) | |
1110 { | |
1111 int i; | |
1112 unsigned char *matrix; | |
1113 FILE *input; | |
1114 | |
1115 /* Allocate matrix space */ | |
1116 if((matrix = malloc(64*sizeof(unsigned char))) == NULL) | |
1117 return(NULL); | |
1118 | |
1119 /* Open the matrix file */ | |
1120 if((input = fopen(filename, "rb")) == NULL) { | |
1121 fprintf(stderr, | |
1122 "xvid: Error opening the matrix file %s\n", | |
1123 filename); | |
1124 free(matrix); | |
1125 return(NULL); | |
1126 } | |
1127 | |
1128 /* Read the matrix */ | |
1129 for(i=0; i<64; i++) { | |
1130 | |
1131 int value; | |
1132 | |
1133 /* If fscanf fails then get out of the loop */ | |
1134 if(fscanf(input, "%d", &value) != 1) { | |
1135 fprintf(stderr, | |
1136 "xvid: Error reading the matrix file %s\n", | |
1137 filename); | |
1138 free(matrix); | |
1139 fclose(input); | |
1140 return(NULL); | |
1141 } | |
1142 | |
1143 /* Clamp the value to safe range */ | |
1144 value = (value< 1)?1 :value; | |
1145 value = (value>255)?255:value; | |
1146 matrix[i] = value; | |
1147 } | |
1148 | |
1149 /* Fills the rest with 1 */ | |
1150 while(i<64) matrix[i++] = 1; | |
1151 | |
1152 /* We're done */ | |
1153 fclose(input); | |
1154 | |
1155 return(matrix); | |
1156 | |
1157 } | |
1158 | |
1159 static const char *errorstring(int err) | |
1160 { | |
1161 char *error; | |
1162 switch(err) { | |
1163 case XVID_ERR_FAIL: | |
1164 error = "General fault"; | |
1165 break; | |
1166 case XVID_ERR_MEMORY: | |
1167 error = "Memory allocation error"; | |
1168 break; | |
1169 case XVID_ERR_FORMAT: | |
1170 error = "File format error"; | |
1171 break; | |
1172 case XVID_ERR_VERSION: | |
1173 error = "Structure version not supported"; | |
1174 break; | |
1175 case XVID_ERR_END: | |
1176 error = "End of stream reached"; | |
1177 break; | |
1178 default: | |
1179 error = "Unknown"; | |
1180 } | |
1181 | |
1182 return((const char *)error); | |
1183 } | |
1184 | |
1185 /***************************************************************************** | |
1186 * Module structure definition | |
1187 ****************************************************************************/ | |
1188 | |
1189 vf_info_t ve_info_xvid = { | |
1190 "XviD 1.0 encoder", | |
1191 "xvid", | |
1192 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>", | |
1193 "No comment", | |
1194 vf_open | |
1195 }; | |
1196 | |
1197 | |
1198 #endif /* HAVE_XVID4 */ | |
1199 | |
1200 /* Please do not change that tag comment. | |
1201 * arch-tag: 42ccc257-0548-4a3e-9617-2876c4e8ac88 mplayer xvid encoder module */ |