Mercurial > mplayer.hg
annotate libmpcodecs/ve_xvid4.c @ 15036:5f36a0580b75
sync 1.923
author | wight |
---|---|
date | Fri, 01 Apr 2005 07:59:15 +0000 |
parents | 5723c4b2a2ea |
children | bde4030f9134 |
rev | line source |
---|---|
11437 | 1 /***************************************************************************** |
2 * | |
13610 | 3 * - XviD 1.x export module for mplayer/mencoder - |
11437 | 4 * |
13610 | 5 * Copyright(C) 2003 Marco Belli <elcabesa@inwind.it> |
6 * 2003-2004 Edouard Gomez <ed.gomez@free.fr> | |
11437 | 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 |
13610 | 66 typedef struct xvid_rational_s{ |
11912
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; |
13610 | 163 static int xvidenc_bvhq = 0; |
11437 | 164 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
|
165 static int xvidenc_turbo = 0; |
11437 | 166 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
|
167 static int xvidenc_max_key_interval = 0; /* Let xvidcore set a 10s interval by default */ |
11437 | 168 static int xvidenc_frame_drop_ratio = 0; |
169 static int xvidenc_greyscale = 0; | |
11920 | 170 static int xvidenc_debug = 0; |
171 static int xvidenc_psnr = 0; | |
11437 | 172 |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
173 static int xvidenc_max_bframes = 2; |
11437 | 174 static int xvidenc_bquant_ratio = 150; |
175 static int xvidenc_bquant_offset = 100; | |
176 static int xvidenc_bframe_threshold = 0; | |
177 | |
178 static int xvidenc_min_quant[3] = {2, 2, 2}; | |
179 static int xvidenc_max_quant[3] = {31, 31, 31}; | |
180 static char *xvidenc_intra_matrix_file = NULL; | |
181 static char *xvidenc_inter_matrix_file = NULL; | |
182 static char *xvidenc_quant_method = NULL; | |
183 | |
184 static int xvidenc_cbr_reaction_delay_factor = 0; | |
185 static int xvidenc_cbr_averaging_period = 0; | |
186 static int xvidenc_cbr_buffer = 0; | |
187 | |
188 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
|
189 static int xvidenc_vbr_overflow_control_strength = 5; |
11437 | 190 static int xvidenc_vbr_curve_compression_high = 0; |
191 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
|
192 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
|
193 static int xvidenc_vbr_max_overflow_degradation = 5; |
11437 | 194 static int xvidenc_vbr_kfreduction = 0; |
11586 | 195 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
|
196 static int xvidenc_vbr_container_frame_overhead = 24; /* mencoder uses AVI container */ |
11437 | 197 |
198 static char *xvidenc_par = NULL; | |
199 static int xvidenc_par_width = 0; | |
200 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
|
201 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
|
202 static int xvidenc_autoaspect = 0; |
11437 | 203 |
204 m_option_t xvidencopts_conf[] = | |
205 { | |
206 /* Standard things mencoder should be able to treat directly */ | |
207 {"bitrate", &xvidenc_bitrate, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
208 {"pass", &xvidenc_pass, CONF_TYPE_INT, CONF_RANGE, 1, 2, NULL}, | |
209 {"fixed_quant", &xvidenc_quantizer, CONF_TYPE_FLOAT, CONF_RANGE, 1, 31, NULL}, | |
210 | |
211 /* Features */ | |
212 {"quant_type", &xvidenc_quant_method, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
213 {"me_quality", &xvidenc_motion, CONF_TYPE_INT, CONF_RANGE, 0, 6, NULL}, | |
214 {"chroma_me", &xvidenc_chromame, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
215 {"nochroma_me", &xvidenc_chromame, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
216 {"chroma_opt", &xvidenc_chroma_opt, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
217 {"nochroma_opt", &xvidenc_chroma_opt, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 218 {"vhq", &xvidenc_vhq, CONF_TYPE_INT, CONF_RANGE, 0, 4, NULL}, |
13610 | 219 {"bvhq", &xvidenc_bvhq, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL}, |
11437 | 220 {"max_bframes", &xvidenc_max_bframes, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL}, |
221 {"bquant_ratio", &xvidenc_bquant_ratio, CONF_TYPE_INT, CONF_RANGE, 0, 200, NULL}, | |
222 {"bquant_offset", &xvidenc_bquant_offset, CONF_TYPE_INT, CONF_RANGE, 0, 200, NULL}, | |
223 {"bf_threshold", &xvidenc_bframe_threshold, CONF_TYPE_INT, CONF_RANGE, -255, 255, NULL}, | |
224 {"qpel", &xvidenc_quarterpel, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
225 {"noqpel", &xvidenc_quarterpel, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 226 {"gmc", &xvidenc_gmc, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
227 {"nogmc", &xvidenc_gmc, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 228 {"trellis", &xvidenc_trellis, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
229 {"notrellis", &xvidenc_trellis, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 230 {"packed", &xvidenc_packed, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
231 {"nopacked", &xvidenc_packed, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 232 {"closed_gop", &xvidenc_closed_gop, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
233 {"noclosed_gop", &xvidenc_closed_gop, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11491 | 234 {"interlacing", &xvidenc_interlaced, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
235 {"nointerlacing", &xvidenc_interlaced, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 236 {"cartoon", &xvidenc_cartoon, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
237 {"nocartoon", &xvidenc_cartoon, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 238 {"hq_ac", &xvidenc_hqacpred, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
239 {"nohq_ac", &xvidenc_hqacpred, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 240 {"frame_drop_ratio", &xvidenc_frame_drop_ratio, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, |
241 {"max_key_interval", &xvidenc_max_key_interval, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, | |
242 {"greyscale", &xvidenc_greyscale, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
243 {"nogreyscale", &xvidenc_greyscale, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
244 {"turbo", &xvidenc_turbo, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11920 | 245 {"debug", &xvidenc_debug, CONF_TYPE_INT , 0 ,0,-1,NULL}, |
11437 | 246 {"stats", &xvidenc_stats, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11920 | 247 {"psnr", &xvidenc_psnr , CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
11437 | 248 |
249 | |
250 /* section [quantizer] */ | |
251 {"min_iquant", &xvidenc_min_quant[0], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
252 {"max_iquant", &xvidenc_max_quant[0], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
253 {"min_pquant", &xvidenc_min_quant[1], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
254 {"max_pquant", &xvidenc_max_quant[1], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
255 {"min_bquant", &xvidenc_min_quant[2], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
256 {"max_bquant", &xvidenc_max_quant[2], CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
257 {"quant_intra_matrix", &xvidenc_intra_matrix_file, CONF_TYPE_STRING, 0, 0, 100, NULL}, | |
258 {"quant_inter_matrix", &xvidenc_inter_matrix_file, CONF_TYPE_STRING, 0, 0, 100, NULL}, | |
259 | |
260 /* section [cbr] */ | |
11491 | 261 {"rc_reaction_delay_factor", &xvidenc_cbr_reaction_delay_factor, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, |
262 {"rc_averaging_period", &xvidenc_cbr_averaging_period, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, | |
263 {"rc_buffer", &xvidenc_cbr_buffer, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, | |
11437 | 264 |
265 /* section [vbr] */ | |
266 {"keyframe_boost", &xvidenc_vbr_keyframe_boost, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
267 {"curve_compression_high", &xvidenc_vbr_curve_compression_high, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
268 {"curve_compression_low", &xvidenc_vbr_curve_compression_low, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
269 {"overflow_control_strength", &xvidenc_vbr_overflow_control_strength, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
270 {"max_overflow_improvement", &xvidenc_vbr_max_overflow_improvement, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
271 {"max_overflow_degradation", &xvidenc_vbr_max_overflow_degradation, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
272 {"kfreduction", &xvidenc_vbr_kfreduction, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, | |
11586 | 273 {"kfthreshold", &xvidenc_vbr_kfthreshold, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, |
11437 | 274 {"container_frame_overhead", &xvidenc_vbr_container_frame_overhead, CONF_TYPE_INT, CONF_MIN, 0, 0, NULL}, |
275 | |
276 /* Section Aspect Ratio */ | |
277 {"par", &xvidenc_par, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
278 {"par_width", &xvidenc_par_width, CONF_TYPE_INT, CONF_RANGE, 0, 255, NULL}, | |
279 {"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
|
280 {"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
|
281 {"autoaspect", &xvidenc_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
13853
5a786c7e4df4
have each XviD's option flag have its (no)counterpart
gpoirier
parents:
13675
diff
changeset
|
282 {"noautoaspect", &xvidenc_autoaspect, CONF_TYPE_FLAG, 0, 1, 0, NULL}, |
11437 | 283 |
284 /* End of the config array */ | |
285 {NULL, 0, 0, 0, 0, 0, NULL} | |
286 }; | |
287 | |
288 /***************************************************************************** | |
289 * Module private data | |
290 ****************************************************************************/ | |
291 | |
292 typedef struct _xvid_mplayer_module_t | |
293 { | |
294 /* Instance related global vars */ | |
295 void *instance; | |
296 xvid_gbl_init_t init; | |
297 xvid_enc_create_t create; | |
298 xvid_enc_frame_t frame; | |
299 xvid_plugin_single_t onepass; | |
300 xvid_plugin_2pass1_t pass1; | |
301 xvid_plugin_2pass2_t pass2; | |
302 | |
303 /* This data must survive local block scope, so here it is */ | |
304 xvid_enc_plugin_t plugins[7]; | |
305 xvid_enc_zone_t zones[1]; | |
306 | |
307 /* MPEG4 stream buffer */ | |
308 muxer_stream_t *mux; | |
309 | |
310 /* Stats accumulators */ | |
311 int frames; | |
312 long long sse_y; | |
313 long long sse_u; | |
314 long long sse_v; | |
315 | |
316 /* Min & Max PSNR */ | |
317 int min_sse_y; | |
318 int min_sse_u; | |
319 int min_sse_v; | |
11920 | 320 int min_framenum; |
11437 | 321 int max_sse_y; |
322 int max_sse_u; | |
323 int max_sse_v; | |
11920 | 324 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
|
325 |
11920 | 326 int pixels; |
13610 | 327 |
328 /* DAR/PAR and all that thingies */ | |
329 int d_width; | |
330 int d_height; | |
331 FILE *fvstats; | |
11437 | 332 } xvid_mplayer_module_t; |
333 | |
334 static void dispatch_settings(xvid_mplayer_module_t *mod); | |
335 static int set_create_struct(xvid_mplayer_module_t *mod); | |
336 static int set_frame_struct(xvid_mplayer_module_t *mod, mp_image_t *mpi); | |
13610 | 337 static void update_stats(xvid_mplayer_module_t *mod, xvid_enc_stats_t *stats); |
338 static void print_stats(xvid_mplayer_module_t *mod); | |
339 static void flush_internal_buffers(xvid_mplayer_module_t *mod); | |
340 static const char *par_string(int parcode); | |
11437 | 341 static const char *errorstring(int err); |
342 | |
343 /***************************************************************************** | |
344 * Video Filter API function definitions | |
345 ****************************************************************************/ | |
346 | |
347 /*============================================================================ | |
348 * config | |
349 *==========================================================================*/ | |
350 | |
351 static int | |
352 config(struct vf_instance_s* vf, | |
353 int width, int height, int d_width, int d_height, | |
354 unsigned int flags, unsigned int outfmt) | |
355 { | |
356 int err; | |
357 xvid_mplayer_module_t *mod = (xvid_mplayer_module_t *)vf->priv; | |
358 | |
359 /* Complete the muxer initialization */ | |
360 mod->mux->bih->biWidth = width; | |
361 mod->mux->bih->biHeight = height; | |
362 mod->mux->bih->biSizeImage = | |
13610 | 363 mod->mux->bih->biWidth * mod->mux->bih->biHeight * 3 / 2; |
12061 | 364 mod->mux->aspect = (float)d_width/d_height; |
11437 | 365 |
366 /* Message the FourCC type */ | |
367 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
368 "videocodec: XviD (%dx%d fourcc=%x [%.4s])\n", | |
369 width, height, mod->mux->bih->biCompression, | |
370 (char *)&mod->mux->bih->biCompression); | |
371 | |
13610 | 372 /* Total number of pixels per frame required for PSNR */ |
373 mod->pixels = mod->mux->bih->biWidth*mod->mux->bih->biHeight; | |
374 | |
11437 | 375 /*-------------------------------------------------------------------- |
376 * Dispatch all module settings to XviD structures | |
377 *------------------------------------------------------------------*/ | |
378 | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
379 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
|
380 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
|
381 |
11437 | 382 dispatch_settings(mod); |
383 | |
384 /*-------------------------------------------------------------------- | |
385 * Set remaining information in the xvid_enc_create_t structure | |
386 *------------------------------------------------------------------*/ | |
387 | |
388 if(set_create_struct(mod) == BAD) | |
389 return(BAD); | |
390 | |
391 /*-------------------------------------------------------------------- | |
392 * Encoder instance creation | |
393 *------------------------------------------------------------------*/ | |
394 | |
395 err = xvid_encore(NULL, XVID_ENC_CREATE, &mod->create, NULL); | |
396 | |
397 if(err<0) { | |
398 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
399 "xvid: xvidcore returned a '%s' error\n", errorstring(err)); | |
400 return(BAD); | |
401 } | |
402 | |
403 /* Store the encoder instance into the private data */ | |
404 mod->instance = mod->create.handle; | |
405 | |
406 return(FINE); | |
407 } | |
408 | |
409 /*============================================================================ | |
410 * uninit | |
411 *==========================================================================*/ | |
412 | |
413 static void | |
414 uninit(struct vf_instance_s* vf) | |
415 { | |
416 | |
417 xvid_mplayer_module_t *mod = (xvid_mplayer_module_t *)vf->priv; | |
418 | |
419 /* Destroy xvid instance */ | |
420 xvid_encore(mod->instance, XVID_ENC_DESTROY, NULL, NULL); | |
421 | |
13610 | 422 /* Display stats (if any) */ |
423 print_stats(mod); | |
11437 | 424 |
13610 | 425 /* Close PSNR file if ever opened */ |
426 if (mod->fvstats) { | |
427 fclose(mod->fvstats); | |
428 mod->fvstats = NULL; | |
11437 | 429 } |
430 | |
13610 | 431 /* Free allocated memory */ |
432 if(mod->frame.quant_intra_matrix) | |
433 free(mod->frame.quant_intra_matrix); | |
434 | |
435 if(mod->frame.quant_inter_matrix) | |
436 free(mod->frame.quant_inter_matrix); | |
437 | |
438 if(mod->mux->bih) | |
439 free(mod->mux->bih); | |
440 | |
441 free(vf->priv); | |
442 vf->priv=NULL; | |
11437 | 443 |
444 return; | |
445 } | |
446 | |
447 /*============================================================================ | |
448 * control | |
449 *==========================================================================*/ | |
450 | |
451 static int | |
452 control(struct vf_instance_s* vf, int request, void* data) | |
453 { | |
13610 | 454 xvid_mplayer_module_t *mod = (xvid_mplayer_module_t *)vf->priv; |
455 | |
456 switch(request){ | |
457 case VFCTRL_FLUSH_FRAMES: | |
458 if(mod)/*paranoid*/ | |
459 flush_internal_buffers(mod); | |
460 break; | |
461 } | |
11437 | 462 return(CONTROL_UNKNOWN); |
463 } | |
464 | |
465 /*============================================================================ | |
466 * query_format | |
467 *==========================================================================*/ | |
468 | |
469 static int | |
470 query_format(struct vf_instance_s* vf, unsigned int fmt) | |
471 { | |
472 switch(fmt){ | |
473 case IMGFMT_YV12: | |
474 case IMGFMT_IYUV: | |
475 case IMGFMT_I420: | |
476 return(VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW); | |
477 case IMGFMT_YUY2: | |
478 case IMGFMT_UYVY: | |
479 return(VFCAP_CSP_SUPPORTED); | |
480 } | |
481 return(BAD); | |
482 } | |
483 | |
484 /*============================================================================ | |
485 * put_image | |
486 *==========================================================================*/ | |
487 | |
488 static int | |
489 put_image(struct vf_instance_s* vf, mp_image_t *mpi) | |
490 { | |
491 int size; | |
492 xvid_enc_stats_t stats; | |
493 xvid_mplayer_module_t *mod = (xvid_mplayer_module_t *)vf->priv; | |
494 | |
495 /* Prepare the stats */ | |
496 memset(&stats,0,sizeof( xvid_enc_stats_t)); | |
497 stats.version = XVID_VERSION; | |
498 | |
499 /* ------------------------------------------------------------------- | |
500 * Set remaining information in the xvid_enc_frame_t structure | |
501 * NB: all the other struct members were initialized by | |
502 * dispatch_settings | |
503 * -----------------------------------------------------------------*/ | |
504 | |
505 if(set_frame_struct(mod, mpi) == BAD) | |
506 return(BAD); | |
507 | |
508 /* ------------------------------------------------------------------- | |
509 * Encode the frame | |
510 * ---------------------------------------------------------------- */ | |
511 | |
512 size = xvid_encore(mod->instance, XVID_ENC_ENCODE, &mod->frame, &stats); | |
513 | |
514 /* Analyse the returned value */ | |
515 if(size<0) { | |
516 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
517 "xvid: xvidcore returned a '%s' error\n", errorstring(size)); | |
518 return(BAD); | |
519 } | |
520 | |
521 /* If size is == 0, we're done with that frame */ | |
522 if(size == 0) return(FINE); | |
523 | |
13610 | 524 /* xvidcore returns stats about encoded frame in an asynchronous way |
525 * accumulate these stats */ | |
526 update_stats(mod, &stats); | |
11437 | 527 |
528 /* xvidcore outputed bitstream -- mux it */ | |
529 muxer_write_chunk(mod->mux, | |
530 size, | |
531 (mod->frame.out_flags & XVID_KEYFRAME)?0x10:0); | |
532 | |
533 return(FINE); | |
534 } | |
535 | |
536 /*============================================================================ | |
537 * vf_open | |
538 *==========================================================================*/ | |
539 | |
540 static int | |
541 vf_open(vf_instance_t *vf, char* args) | |
542 { | |
543 xvid_mplayer_module_t *mod; | |
544 xvid_gbl_init_t xvid_gbl_init; | |
545 xvid_gbl_info_t xvid_gbl_info; | |
546 | |
547 /* Setting libmpcodec module API pointers */ | |
548 vf->config = config; | |
14878 | 549 vf->default_caps = VFCAP_CONSTANT; |
11437 | 550 vf->control = control; |
551 vf->uninit = uninit; | |
552 vf->query_format = query_format; | |
553 vf->put_image = put_image; | |
554 | |
555 /* Allocate the private part of the codec module */ | |
556 vf->priv = malloc(sizeof(xvid_mplayer_module_t)); | |
557 mod = (xvid_mplayer_module_t*)vf->priv; | |
558 | |
559 if(mod == NULL) { | |
560 mp_msg(MSGT_MENCODER,MSGL_ERR, | |
561 "xvid: memory allocation failure (private data)\n"); | |
562 return(BAD); | |
563 } | |
564 | |
565 /* Initialize the module to zeros */ | |
566 memset(mod, 0, sizeof(xvid_mplayer_module_t)); | |
567 mod->min_sse_y = mod->min_sse_u = mod->min_sse_v = INT_MAX; | |
568 mod->max_sse_y = mod->max_sse_u = mod->max_sse_v = INT_MIN; | |
569 | |
570 /* Bind the Muxer */ | |
571 mod->mux = (muxer_stream_t*)args; | |
572 | |
573 /* Initialize muxer BITMAP header */ | |
14549
acf3241be19b
Initialized BITMAPINFOHEADER to 0 to avoid problems, esp. windows has problems
reimar
parents:
13853
diff
changeset
|
574 mod->mux->bih = calloc(1, sizeof(BITMAPINFOHEADER)); |
11437 | 575 |
576 if(mod->mux->bih == NULL) { | |
577 mp_msg(MSGT_MENCODER,MSGL_ERR, | |
578 "xvid: memory allocation failure (BITMAP header)\n"); | |
579 return(BAD); | |
580 } | |
581 | |
582 mod->mux->bih->biSize = sizeof(BITMAPINFOHEADER); | |
583 mod->mux->bih->biWidth = 0; | |
584 mod->mux->bih->biHeight = 0; | |
585 mod->mux->bih->biPlanes = 1; | |
13610 | 586 mod->mux->bih->biBitCount = 12; |
11437 | 587 mod->mux->bih->biCompression = mmioFOURCC('X','V','I','D'); |
588 | |
589 /* Retrieve information about the host XviD library */ | |
590 memset(&xvid_gbl_info, 0, sizeof(xvid_gbl_info_t)); | |
591 xvid_gbl_info.version = XVID_VERSION; | |
592 | |
593 if (xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL) < 0) { | |
13610 | 594 mp_msg(MSGT_MENCODER,MSGL_WARN, "xvid: could not get information about the library\n"); |
11437 | 595 } else { |
596 mp_msg(MSGT_MENCODER,MSGL_INFO, "xvid: using library version %d.%d.%d (build %s)\n", | |
597 XVID_VERSION_MAJOR(xvid_gbl_info.actual_version), | |
598 XVID_VERSION_MINOR(xvid_gbl_info.actual_version), | |
599 XVID_VERSION_PATCH(xvid_gbl_info.actual_version), | |
600 xvid_gbl_info.build); | |
601 } | |
602 | |
603 /* Initialize the xvid_gbl_init structure */ | |
604 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t)); | |
605 xvid_gbl_init.version = XVID_VERSION; | |
11920 | 606 xvid_gbl_init.debug = xvidenc_debug; |
11437 | 607 |
608 /* Initialize the xvidcore library */ | |
609 if (xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL) < 0) { | |
610 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: initialisation failure\n"); | |
611 return(BAD); | |
612 } | |
613 | |
614 return(FINE); | |
615 } | |
616 | |
617 /***************************************************************************** | |
618 * Helper functions | |
619 ****************************************************************************/ | |
620 | |
621 static void *read_matrix(unsigned char *filename); | |
622 | |
623 static void dispatch_settings(xvid_mplayer_module_t *mod) | |
624 { | |
625 xvid_enc_create_t *create = &mod->create; | |
626 xvid_enc_frame_t *frame = &mod->frame; | |
627 xvid_plugin_single_t *onepass = &mod->onepass; | |
628 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
|
629 XVIDRational ar; |
11437 | 630 |
631 const int motion_presets[7] = | |
632 { | |
633 0, | |
634 0, | |
635 0, | |
636 0, | |
637 XVID_ME_HALFPELREFINE16, | |
638 XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND16, | |
639 XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | | |
640 XVID_ME_HALFPELREFINE8 | XVID_ME_USESQUARES16 | |
641 }; | |
642 | |
643 | |
644 /* ------------------------------------------------------------------- | |
645 * Dispatch all settings having an impact on the "create" structure | |
646 * This includes plugins as they are passed to encore through the | |
647 * create structure | |
648 * -----------------------------------------------------------------*/ | |
649 | |
650 /* ------------------------------------------------------------------- | |
651 * The create structure | |
652 * ---------------------------------------------------------------- */ | |
653 | |
654 create->global = 0; | |
655 | |
656 if(xvidenc_packed) | |
657 create->global |= XVID_GLOBAL_PACKED; | |
658 | |
659 if(xvidenc_closed_gop) | |
660 create->global |= XVID_GLOBAL_CLOSED_GOP; | |
661 | |
11920 | 662 if(xvidenc_psnr) |
663 xvidenc_stats = 1; | |
664 | |
11437 | 665 if(xvidenc_stats) |
666 create->global |= XVID_GLOBAL_EXTRASTATS_ENABLE; | |
667 | |
668 create->num_zones = 0; | |
669 create->zones = NULL; | |
670 create->num_plugins = 0; | |
671 create->plugins = NULL; | |
672 create->num_threads = 0; | |
673 create->max_bframes = xvidenc_max_bframes; | |
674 create->bquant_ratio = xvidenc_bquant_ratio; | |
675 create->bquant_offset = xvidenc_bquant_offset; | |
676 create->max_key_interval = xvidenc_max_key_interval; | |
677 create->frame_drop_ratio = xvidenc_frame_drop_ratio; | |
678 create->min_quant[0] = xvidenc_min_quant[0]; | |
679 create->min_quant[1] = xvidenc_min_quant[1]; | |
680 create->min_quant[2] = xvidenc_min_quant[2]; | |
681 create->max_quant[0] = xvidenc_max_quant[0]; | |
682 create->max_quant[1] = xvidenc_max_quant[1]; | |
683 create->max_quant[2] = xvidenc_max_quant[2]; | |
684 | |
685 | |
686 /* ------------------------------------------------------------------- | |
687 * The single pass plugin | |
688 * ---------------------------------------------------------------- */ | |
689 | |
13675
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
690 if (xvidenc_bitrate > 16000) onepass->bitrate = xvidenc_bitrate; |
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
691 else onepass->bitrate = xvidenc_bitrate*1000; |
11437 | 692 onepass->reaction_delay_factor = xvidenc_cbr_reaction_delay_factor; |
693 onepass->averaging_period = xvidenc_cbr_averaging_period; | |
694 onepass->buffer = xvidenc_cbr_buffer; | |
695 | |
696 /* ------------------------------------------------------------------- | |
697 * The pass2 plugin | |
698 * ---------------------------------------------------------------- */ | |
699 | |
700 pass2->keyframe_boost = xvidenc_vbr_keyframe_boost; | |
701 pass2->overflow_control_strength = xvidenc_vbr_overflow_control_strength; | |
702 pass2->curve_compression_high = xvidenc_vbr_curve_compression_high; | |
703 pass2->curve_compression_low = xvidenc_vbr_curve_compression_low; | |
704 pass2->max_overflow_improvement = xvidenc_vbr_max_overflow_improvement; | |
705 pass2->max_overflow_degradation = xvidenc_vbr_max_overflow_degradation; | |
706 pass2->kfreduction = xvidenc_vbr_kfreduction; | |
11586 | 707 pass2->kfthreshold = xvidenc_vbr_kfthreshold; |
11437 | 708 pass2->container_frame_overhead = xvidenc_vbr_container_frame_overhead; |
709 | |
710 /* ------------------------------------------------------------------- | |
711 * The frame structure | |
712 * ---------------------------------------------------------------- */ | |
713 frame->vol_flags = 0; | |
714 frame->vop_flags = 0; | |
715 frame->motion = 0; | |
716 | |
717 frame->vop_flags |= XVID_VOP_HALFPEL; | |
718 frame->motion |= motion_presets[xvidenc_motion]; | |
719 | |
720 if(xvidenc_stats) | |
721 frame->vol_flags |= XVID_VOL_EXTRASTATS; | |
722 | |
723 if(xvidenc_greyscale) | |
724 frame->vop_flags |= XVID_VOP_GREYSCALE; | |
725 | |
726 if(xvidenc_cartoon) { | |
727 frame->vop_flags |= XVID_VOP_CARTOON; | |
728 frame->motion |= XVID_ME_DETECT_STATIC_MOTION; | |
729 } | |
730 | |
731 if(xvidenc_intra_matrix_file != NULL) { | |
732 frame->quant_intra_matrix = (unsigned char*)read_matrix(xvidenc_intra_matrix_file); | |
733 if(frame->quant_intra_matrix != NULL) { | |
13610 | 734 mp_msg(MSGT_MENCODER, MSGL_INFO, "xvid: Loaded Intra matrix (switching to mpeg quantization type)\n"); |
11437 | 735 if(xvidenc_quant_method) free(xvidenc_quant_method); |
736 xvidenc_quant_method = strdup("mpeg"); | |
737 } | |
738 } | |
739 if(xvidenc_inter_matrix_file != NULL) { | |
740 frame->quant_inter_matrix = read_matrix(xvidenc_inter_matrix_file); | |
741 if(frame->quant_inter_matrix) { | |
13610 | 742 mp_msg(MSGT_MENCODER, MSGL_INFO, "\nxvid: Loaded Inter matrix (switching to mpeg quantization type)\n"); |
11437 | 743 if(xvidenc_quant_method) free(xvidenc_quant_method); |
744 xvidenc_quant_method = strdup("mpeg"); | |
745 } | |
746 } | |
747 if(xvidenc_quant_method != NULL && !strcasecmp(xvidenc_quant_method, "mpeg")) { | |
748 frame->vol_flags |= XVID_VOL_MPEGQUANT; | |
749 } | |
750 if(xvidenc_quarterpel) { | |
751 frame->vol_flags |= XVID_VOL_QUARTERPEL; | |
752 frame->motion |= XVID_ME_QUARTERPELREFINE16; | |
753 frame->motion |= XVID_ME_QUARTERPELREFINE8; | |
754 } | |
755 if(xvidenc_gmc) { | |
756 frame->vol_flags |= XVID_VOL_GMC; | |
757 frame->motion |= XVID_ME_GME_REFINE; | |
758 } | |
759 if(xvidenc_interlaced) { | |
760 frame->vol_flags |= XVID_VOL_INTERLACING; | |
761 } | |
762 if(xvidenc_trellis) { | |
763 frame->vop_flags |= XVID_VOP_TRELLISQUANT; | |
764 } | |
765 if(xvidenc_hqacpred) { | |
766 frame->vop_flags |= XVID_VOP_HQACPRED; | |
767 } | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
768 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
|
769 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
|
770 } |
11437 | 771 if(xvidenc_motion > 4) { |
772 frame->vop_flags |= XVID_VOP_INTER4V; | |
773 } | |
774 if(xvidenc_chromame) { | |
775 frame->motion |= XVID_ME_CHROMA_PVOP; | |
776 frame->motion |= XVID_ME_CHROMA_BVOP; | |
777 } | |
778 if(xvidenc_vhq >= 1) { | |
779 frame->vop_flags |= XVID_VOP_MODEDECISION_RD; | |
780 } | |
781 if(xvidenc_vhq >= 2) { | |
782 frame->motion |= XVID_ME_HALFPELREFINE16_RD; | |
783 frame->motion |= XVID_ME_QUARTERPELREFINE16_RD; | |
784 } | |
785 if(xvidenc_vhq >= 3) { | |
786 frame->motion |= XVID_ME_HALFPELREFINE8_RD; | |
787 frame->motion |= XVID_ME_QUARTERPELREFINE8_RD; | |
788 frame->motion |= XVID_ME_CHECKPREDICTION_RD; | |
789 } | |
790 if(xvidenc_vhq >= 4) { | |
791 frame->motion |= XVID_ME_EXTSEARCH_RD; | |
792 } | |
13610 | 793 if(xvidenc_bvhq >= 1) { |
794 #if XVID_API >= XVID_MAKE_API(4,1) | |
795 frame->vop_flags |= XVID_VOP_RD_BVOP; | |
796 #endif | |
797 } | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
798 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
|
799 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
|
800 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
|
801 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
|
802 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
|
803 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
|
804 } |
11437 | 805 |
806 /* motion level == 0 means no motion search which is equivalent to | |
807 * intra coding only */ | |
808 if(xvidenc_motion == 0) { | |
809 frame->type = XVID_TYPE_IVOP; | |
810 } else { | |
811 frame->type = XVID_TYPE_AUTO; | |
812 } | |
813 | |
814 frame->bframe_threshold = xvidenc_bframe_threshold; | |
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 /* 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
|
817 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
|
818 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
819 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
|
820 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
|
821 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
|
822 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
|
823 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
|
824 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
825 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
|
826 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
|
827 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
|
828 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
|
829 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
|
830 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
|
831 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
|
832 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
|
833 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
|
834 else |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
835 { |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
836 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
|
837 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
|
838 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
|
839 } |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
840 |
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
841 } else if(xvidenc_par != NULL) { |
11437 | 842 if(strcasecmp(xvidenc_par, "pal43") == 0) |
843 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
|
844 else if(strcasecmp(xvidenc_par, "pal169") == 0) |
11437 | 845 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
|
846 else if(strcasecmp(xvidenc_par, "ntsc43") == 0) |
11437 | 847 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
|
848 else if(strcasecmp(xvidenc_par, "ntsc169") == 0) |
11437 | 849 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
|
850 else if(strcasecmp(xvidenc_par, "ext") == 0) |
11437 | 851 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
|
852 |
11437 | 853 if(frame->par == XVID_PAR_EXT) { |
854 if(xvidenc_par_width) | |
855 frame->par_width = xvidenc_par_width; | |
856 else | |
857 frame->par_width = 1; | |
858 | |
859 if(xvidenc_par_height) | |
860 frame->par_height = xvidenc_par_height; | |
861 else | |
862 frame->par_height = 1; | |
863 } | |
11912
8eb96d751dcd
new options - aspect,autoaspect,turbo,chroma_opt. patch by Nico <nsabbi@tiscali.it> and Edouard Gomez
iive
parents:
11586
diff
changeset
|
864 } |
13610 | 865 |
866 /* Display par information */ | |
867 mp_msg(MSGT_MENCODER, MSGL_INFO, "xvid: par=%d/%d (%s), displayed=%dx%d, sampled=%dx%d\n", | |
868 ar.num, ar.den, par_string(frame->par), | |
869 mod->d_width, mod->d_height, mod->mux->bih->biWidth, mod->mux->bih->biHeight); | |
11437 | 870 return; |
871 } | |
872 | |
873 static int set_create_struct(xvid_mplayer_module_t *mod) | |
874 { | |
875 int pass; | |
876 xvid_enc_create_t *create = &mod->create; | |
877 | |
878 /* Most of the structure is initialized by dispatch settings, only a | |
879 * few things are missing */ | |
880 create->version = XVID_VERSION; | |
881 | |
882 /* Width and Height */ | |
883 create->width = mod->mux->bih->biWidth; | |
884 create->height = mod->mux->bih->biHeight; | |
885 | |
886 /* FPS */ | |
887 create->fincr = mod->mux->h.dwScale; | |
888 create->fbase = mod->mux->h.dwRate; | |
889 | |
890 /* Encodings zones */ | |
891 memset(mod->zones, 0, sizeof(mod->zones)); | |
892 create->zones = mod->zones; | |
893 create->num_zones = 0; | |
894 | |
895 /* Plugins */ | |
896 memset(mod->plugins, 0, sizeof(mod->plugins)); | |
897 create->plugins = mod->plugins; | |
898 create->num_plugins = 0; | |
899 | |
900 /* ------------------------------------------------------------------- | |
901 * Initialize and bind the right rate controller plugin | |
902 * ---------------------------------------------------------------- */ | |
903 | |
904 /* First we try to sort out configuration conflicts */ | |
905 if(xvidenc_quantizer != 0 && (xvidenc_bitrate || xvidenc_pass)) { | |
906 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
907 "xvid: you can't mix Fixed Quantizer Rate Control" | |
908 " with other Rate Control mechanisms\n"); | |
909 return(BAD); | |
910 } | |
911 | |
912 if(xvidenc_bitrate != 0 && xvidenc_pass == 1) { | |
13610 | 913 mp_msg(MSGT_MENCODER, MSGL_WARN, |
11437 | 914 "xvid: bitrate setting is ignored during first pass\n"); |
915 } | |
916 | |
917 /* Sort out which sort of pass we are supposed to do | |
918 * pass == 1<<0 CBR | |
919 * pass == 1<<1 Two pass first pass | |
920 * pass == 1<<2 Two pass second pass | |
921 * pass == 1<<3 Constant quantizer | |
922 */ | |
923 #define MODE_CBR (1<<0) | |
924 #define MODE_2PASS1 (1<<1) | |
925 #define MODE_2PASS2 (1<<2) | |
926 #define MODE_QUANT (1<<3) | |
927 | |
928 pass = 0; | |
929 | |
930 if(xvidenc_bitrate != 0 && xvidenc_pass == 0) | |
931 pass |= MODE_CBR; | |
932 | |
933 if(xvidenc_pass == 1) | |
934 pass |= MODE_2PASS1; | |
935 | |
936 if(xvidenc_bitrate != 0 && xvidenc_pass == 2) | |
937 pass |= MODE_2PASS2; | |
938 | |
939 if(xvidenc_quantizer != 0 && xvidenc_pass == 0) | |
940 pass |= MODE_QUANT; | |
941 | |
942 /* We must be in at least one RC mode */ | |
943 if(pass == 0) { | |
944 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
945 "xvid: you must specify one or a valid combination of " | |
946 "'bitrate', 'pass', 'quantizer' settings\n"); | |
947 return(BAD); | |
948 } | |
949 | |
950 /* Sanity checking */ | |
951 if(pass != MODE_CBR && pass != MODE_QUANT && | |
952 pass != MODE_2PASS1 && pass != MODE_2PASS2) { | |
953 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
954 "xvid: this code should not be reached - fill a bug " | |
955 "report\n"); | |
956 return(BAD); | |
957 } | |
958 | |
959 /* This is a single pass encoding: either a CBR pass or a constant | |
960 * quantizer pass */ | |
961 if(pass == MODE_CBR || pass == MODE_QUANT) { | |
962 xvid_plugin_single_t *onepass = &mod->onepass; | |
963 | |
964 /* There is not much left to initialize after dispatch settings */ | |
965 onepass->version = XVID_VERSION; | |
13675
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
966 if (xvidenc_bitrate > 16000) onepass->bitrate = xvidenc_bitrate; |
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
967 else onepass->bitrate = xvidenc_bitrate*1000; |
11437 | 968 |
969 /* Quantizer mode uses the same plugin, we have only to define | |
970 * a constant quantizer zone beginning at frame 0 */ | |
971 if(pass == MODE_QUANT) { | |
11929 | 972 XVIDRational squant; |
973 squant = xvid_d2q(xvidenc_quantizer,128); | |
11437 | 974 |
975 create->zones[create->num_zones].mode = XVID_ZONE_QUANT; | |
976 create->zones[create->num_zones].frame = 0; | |
12806 | 977 create->zones[create->num_zones].increment = squant.num; |
978 create->zones[create->num_zones].base = squant.den; | |
11437 | 979 create->num_zones++; |
980 | |
981 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
982 "xvid: Fixed Quant Rate Control -- quantizer=%d/%d=%2.2f\n", | |
11929 | 983 squant.num, |
984 squant.den, | |
985 (float)(squant.num)/(float)(squant.den)); | |
11437 | 986 |
987 } else { | |
988 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
989 "xvid: CBR Rate Control -- bitrate=%dkbit/s\n", | |
13675
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
990 xvidenc_bitrate>16000?xvidenc_bitrate/1000:xvidenc_bitrate); |
11437 | 991 } |
992 | |
993 create->plugins[create->num_plugins].func = xvid_plugin_single; | |
994 create->plugins[create->num_plugins].param = onepass; | |
995 create->num_plugins++; | |
996 } | |
997 | |
998 /* This is the first pass of a Two pass process */ | |
999 if(pass == MODE_2PASS1) { | |
1000 xvid_plugin_2pass1_t *pass1 = &mod->pass1; | |
1001 | |
1002 /* There is not much to initialize for this plugin */ | |
1003 pass1->version = XVID_VERSION; | |
1004 pass1->filename = XVID_FIRST_PASS_FILENAME; | |
1005 | |
1006 create->plugins[create->num_plugins].func = xvid_plugin_2pass1; | |
1007 create->plugins[create->num_plugins].param = pass1; | |
1008 create->num_plugins++; | |
1009 | |
1010 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1011 "xvid: 2Pass Rate Control -- 1st pass\n"); | |
1012 } | |
1013 | |
1014 /* This is the second pass of a Two pass process */ | |
1015 if(pass == MODE_2PASS2) { | |
1016 xvid_plugin_2pass2_t *pass2 = &mod->pass2; | |
1017 | |
1018 /* There is not much left to initialize after dispatch settings */ | |
1019 pass2->version = XVID_VERSION; | |
1020 pass2->filename = XVID_FIRST_PASS_FILENAME; | |
1021 | |
1022 /* Positive bitrate values are bitrates as usual but if the | |
1023 * value is negative it is considered as being a total size | |
1024 * to reach (in kilobytes) */ | |
1025 if(xvidenc_bitrate > 0) { | |
13675
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
1026 if(xvidenc_bitrate > 16000) pass2->bitrate = xvidenc_bitrate; |
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
1027 else pass2->bitrate = xvidenc_bitrate*1000; |
11437 | 1028 mp_msg(MSGT_MENCODER, MSGL_INFO, |
1029 "xvid: 2Pass Rate Control -- 2nd pass -- bitrate=%dkbit/s\n", | |
13675
d4cba4c4c54c
Bitrate setting option in ve_xvid4.c doesn't follow the rules described
rathann
parents:
13610
diff
changeset
|
1030 xvidenc_bitrate>16000?xvidenc_bitrate/1000:xvidenc_bitrate); |
11437 | 1031 } else { |
1032 pass2->bitrate = xvidenc_bitrate; | |
1033 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1034 "xvid: 2Pass Rate Control -- 2nd pass -- total size=%dkB\n", | |
1035 -xvidenc_bitrate); | |
1036 } | |
1037 | |
1038 create->plugins[create->num_plugins].func = xvid_plugin_2pass2; | |
1039 create->plugins[create->num_plugins].param = pass2; | |
1040 create->num_plugins++; | |
1041 } | |
1042 | |
1043 return(FINE); | |
1044 } | |
1045 | |
1046 static int set_frame_struct(xvid_mplayer_module_t *mod, mp_image_t *mpi) | |
1047 { | |
1048 xvid_enc_frame_t *frame = &mod->frame; | |
1049 | |
1050 /* Most of the initialization is done during dispatch_settings */ | |
1051 frame->version = XVID_VERSION; | |
1052 | |
1053 /* Bind output buffer */ | |
1054 frame->bitstream = mod->mux->buffer; | |
1055 frame->length = -1; | |
1056 | |
1057 /* Frame format */ | |
1058 switch(mpi->imgfmt) { | |
1059 case IMGFMT_YV12: | |
1060 case IMGFMT_IYUV: | |
1061 case IMGFMT_I420: | |
1062 frame->input.csp = XVID_CSP_USER; | |
1063 break; | |
1064 case IMGFMT_YUY2: | |
1065 frame->input.csp = XVID_CSP_YUY2; | |
1066 break; | |
1067 case IMGFMT_UYVY: | |
1068 frame->input.csp = XVID_CSP_UYVY; | |
1069 break; | |
1070 default: | |
1071 mp_msg(MSGT_MENCODER, MSGL_ERR, | |
1072 "xvid: unsupported picture format (%s)!\n", | |
1073 vo_format_name(mpi->imgfmt)); | |
1074 return(BAD); | |
1075 } | |
1076 | |
1077 /* Bind source frame */ | |
1078 frame->input.plane[0] = mpi->planes[0]; | |
1079 frame->input.plane[1] = mpi->planes[1]; | |
1080 frame->input.plane[2] = mpi->planes[2]; | |
1081 frame->input.stride[0] = mpi->stride[0]; | |
1082 frame->input.stride[1] = mpi->stride[1]; | |
1083 frame->input.stride[2] = mpi->stride[2]; | |
1084 | |
1085 /* Force the right quantizer -- It is internally managed by RC | |
1086 * plugins */ | |
1087 frame->quant = 0; | |
1088 | |
1089 return(FINE); | |
1090 } | |
1091 | |
13610 | 1092 static void |
1093 flush_internal_buffers(xvid_mplayer_module_t *mod) | |
1094 { | |
1095 int size; | |
1096 xvid_enc_frame_t *frame = &mod->frame; | |
1097 | |
1098 if (mod->instance == NULL) | |
1099 return;/*encoder not inited*/ | |
1100 | |
1101 /* Init a fake frame to force flushing */ | |
1102 frame->version = XVID_VERSION; | |
1103 frame->bitstream = mod->mux->buffer; | |
1104 frame->length = -1; | |
1105 frame->input.csp = XVID_CSP_NULL; | |
1106 frame->input.plane[0] = NULL; | |
1107 frame->input.plane[1] = NULL; | |
1108 frame->input.plane[2] = NULL; | |
1109 frame->input.stride[0] = 0; | |
1110 frame->input.stride[1] = 0; | |
1111 frame->input.stride[2] = 0; | |
1112 frame->quant = 0; | |
1113 | |
1114 /* Flush encoder buffers caused by bframes usage */ | |
1115 do { | |
1116 xvid_enc_stats_t stats; | |
1117 memset(&stats, 0, sizeof(xvid_enc_stats_t)); | |
1118 stats.version = XVID_VERSION; | |
1119 | |
1120 /* Encode internal buffer */ | |
1121 size = xvid_encore(mod->instance, XVID_ENC_ENCODE, &mod->frame, &stats); | |
1122 | |
1123 if (size>0) { | |
1124 /* Update stats */ | |
1125 update_stats(mod, &stats); | |
1126 | |
1127 /* xvidcore outputed bitstream -- mux it */ | |
1128 muxer_write_chunk(mod->mux, size, | |
1129 (mod->frame.out_flags & XVID_KEYFRAME)?0x10:0); | |
1130 } | |
1131 } while (size>0); | |
1132 } | |
1133 | |
1134 #define SSE2PSNR(sse, nbpixels) \ | |
1135 ((!(sse)) ? 99.99f : 48.131f - 10*(double)log10((double)(sse)/(double)((nbpixels)))) | |
1136 static void | |
1137 update_stats(xvid_mplayer_module_t *mod, xvid_enc_stats_t *stats) | |
1138 { | |
1139 if(xvidenc_stats && stats->type > 0) { | |
1140 mod->sse_y += stats->sse_y; | |
1141 mod->sse_u += stats->sse_u; | |
1142 mod->sse_v += stats->sse_v; | |
1143 | |
1144 if(mod->min_sse_y > stats->sse_y) { | |
1145 mod->min_sse_y = stats->sse_y; | |
1146 mod->min_sse_u = stats->sse_u; | |
1147 mod->min_sse_v = stats->sse_v; | |
1148 mod->min_framenum = mod->frames; | |
1149 } | |
1150 | |
1151 if(mod->max_sse_y < stats->sse_y) { | |
1152 mod->max_sse_y = stats->sse_y; | |
1153 mod->max_sse_u = stats->sse_u; | |
1154 mod->max_sse_v = stats->sse_v; | |
1155 mod->max_framenum = mod->frames; | |
1156 } | |
1157 | |
1158 if (xvidenc_psnr) { | |
1159 if (!mod->fvstats) { | |
1160 char filename[20]; | |
1161 time_t today2; | |
1162 struct tm *today; | |
1163 today2 = time (NULL); | |
1164 today = localtime (&today2); | |
1165 sprintf (filename, "psnr_%02d%02d%02d.log", today->tm_hour, today->tm_min, today->tm_sec); | |
1166 mod->fvstats = fopen (filename,"w"); | |
1167 if (!mod->fvstats) { | |
1168 perror ("fopen"); | |
1169 /* Disable PSNR file output so we don't get here again */ | |
1170 xvidenc_psnr = 0; | |
1171 } | |
1172 } | |
1173 fprintf (mod->fvstats, "%6d, %2d, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n", | |
1174 mod->frames, | |
1175 stats->quant, | |
1176 stats->length, | |
1177 SSE2PSNR (stats->sse_y, mod->pixels), | |
1178 SSE2PSNR (stats->sse_u, mod->pixels / 4), | |
1179 SSE2PSNR (stats->sse_v, mod->pixels / 4), | |
1180 SSE2PSNR (stats->sse_y + stats->sse_u + stats->sse_v,(double)mod->pixels * 1.5), | |
1181 stats->type==1?'I':stats->type==2?'P':stats->type==3?'B':stats->type?'S':'?' | |
1182 ); | |
1183 } | |
1184 mod->frames++; | |
1185 } | |
1186 } | |
1187 | |
1188 static void | |
1189 print_stats(xvid_mplayer_module_t *mod) | |
1190 { | |
1191 if (mod->frames) { | |
1192 mod->sse_y /= mod->frames; | |
1193 mod->sse_u /= mod->frames; | |
1194 mod->sse_v /= mod->frames; | |
1195 | |
1196 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1197 "The value 99.99dB is a special value and represents " | |
1198 "the upper range limit\n"); | |
1199 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1200 "xvid: Min PSNR y : %.2f dB, u : %.2f dB, v : %.2f dB, in frame %d\n", | |
1201 SSE2PSNR(mod->max_sse_y, mod->pixels), | |
1202 SSE2PSNR(mod->max_sse_u, mod->pixels/4), | |
1203 SSE2PSNR(mod->max_sse_v, mod->pixels/4), | |
1204 mod->max_framenum); | |
1205 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1206 "xvid: Average PSNR y : %.2f dB, u : %.2f dB, v : %.2f dB, for %d frames\n", | |
1207 SSE2PSNR(mod->sse_y, mod->pixels), | |
1208 SSE2PSNR(mod->sse_u, mod->pixels/4), | |
1209 SSE2PSNR(mod->sse_v, mod->pixels/4), | |
1210 mod->frames); | |
1211 mp_msg(MSGT_MENCODER, MSGL_INFO, | |
1212 "xvid: Max PSNR y : %.2f dB, u : %.2f dB, v : %.2f dB, in frame %d\n", | |
1213 SSE2PSNR(mod->min_sse_y, mod->pixels), | |
1214 SSE2PSNR(mod->min_sse_u, mod->pixels/4), | |
1215 SSE2PSNR(mod->min_sse_v, mod->pixels/4), | |
1216 mod->min_framenum); | |
1217 } | |
1218 } | |
1219 #undef SSE2PSNR | |
1220 | |
11437 | 1221 static void *read_matrix(unsigned char *filename) |
1222 { | |
1223 int i; | |
1224 unsigned char *matrix; | |
1225 FILE *input; | |
1226 | |
1227 /* Allocate matrix space */ | |
1228 if((matrix = malloc(64*sizeof(unsigned char))) == NULL) | |
1229 return(NULL); | |
1230 | |
1231 /* Open the matrix file */ | |
1232 if((input = fopen(filename, "rb")) == NULL) { | |
13610 | 1233 mp_msg(MSGT_MENCODER, MSGL_ERR, |
11437 | 1234 "xvid: Error opening the matrix file %s\n", |
1235 filename); | |
1236 free(matrix); | |
1237 return(NULL); | |
1238 } | |
1239 | |
1240 /* Read the matrix */ | |
1241 for(i=0; i<64; i++) { | |
1242 | |
1243 int value; | |
1244 | |
1245 /* If fscanf fails then get out of the loop */ | |
1246 if(fscanf(input, "%d", &value) != 1) { | |
13610 | 1247 mp_msg(MSGT_MENCODER, MSGL_ERR, |
11437 | 1248 "xvid: Error reading the matrix file %s\n", |
1249 filename); | |
1250 free(matrix); | |
1251 fclose(input); | |
1252 return(NULL); | |
1253 } | |
1254 | |
1255 /* Clamp the value to safe range */ | |
1256 value = (value< 1)?1 :value; | |
1257 value = (value>255)?255:value; | |
1258 matrix[i] = value; | |
1259 } | |
1260 | |
1261 /* Fills the rest with 1 */ | |
1262 while(i<64) matrix[i++] = 1; | |
1263 | |
1264 /* We're done */ | |
1265 fclose(input); | |
1266 | |
1267 return(matrix); | |
1268 | |
1269 } | |
1270 | |
13610 | 1271 |
1272 static const char * | |
1273 par_string(int parcode) | |
1274 { | |
1275 const char *par_string; | |
1276 switch (parcode) { | |
1277 case XVID_PAR_11_VGA: | |
1278 par_string = "vga11"; | |
1279 break; | |
1280 case XVID_PAR_43_PAL: | |
1281 par_string = "pal43"; | |
1282 break; | |
1283 case XVID_PAR_43_NTSC: | |
1284 par_string = "ntsc43"; | |
1285 break; | |
1286 case XVID_PAR_169_PAL: | |
1287 par_string = "pal169"; | |
1288 break; | |
1289 case XVID_PAR_169_NTSC: | |
1290 par_string = "ntsc69"; | |
1291 break; | |
1292 case XVID_PAR_EXT: | |
1293 par_string = "ext"; | |
1294 break; | |
1295 default: | |
1296 par_string = "unknown"; | |
1297 break; | |
1298 } | |
1299 return (par_string); | |
1300 } | |
1301 | |
11437 | 1302 static const char *errorstring(int err) |
1303 { | |
13610 | 1304 const char *error; |
11437 | 1305 switch(err) { |
1306 case XVID_ERR_FAIL: | |
1307 error = "General fault"; | |
1308 break; | |
1309 case XVID_ERR_MEMORY: | |
1310 error = "Memory allocation error"; | |
1311 break; | |
1312 case XVID_ERR_FORMAT: | |
1313 error = "File format error"; | |
1314 break; | |
1315 case XVID_ERR_VERSION: | |
1316 error = "Structure version not supported"; | |
1317 break; | |
1318 case XVID_ERR_END: | |
1319 error = "End of stream reached"; | |
1320 break; | |
1321 default: | |
1322 error = "Unknown"; | |
1323 } | |
1324 | |
13610 | 1325 return(error); |
11437 | 1326 } |
1327 | |
1328 /***************************************************************************** | |
1329 * Module structure definition | |
1330 ****************************************************************************/ | |
1331 | |
1332 vf_info_t ve_info_xvid = { | |
1333 "XviD 1.0 encoder", | |
1334 "xvid", | |
1335 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>", | |
1336 "No comment", | |
1337 vf_open | |
1338 }; | |
1339 | |
1340 | |
1341 #endif /* HAVE_XVID4 */ | |
1342 | |
1343 /* Please do not change that tag comment. | |
1344 * arch-tag: 42ccc257-0548-4a3e-9617-2876c4e8ac88 mplayer xvid encoder module */ |