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