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