7456
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4
|
|
5 #include "../config.h"
|
|
6 #include "../mp_msg.h"
|
|
7
|
|
8 #ifdef HAVE_XVID
|
|
9
|
|
10 #include "codec-cfg.h"
|
|
11 #include "stream.h"
|
|
12 #include "demuxer.h"
|
|
13 #include "stheader.h"
|
|
14
|
|
15 #include "aviwrite.h"
|
|
16
|
|
17 #include "img_format.h"
|
|
18 #include "mp_image.h"
|
|
19 #include "vf.h"
|
|
20
|
|
21 #include <xvid.h>
|
|
22 #include "xvid_vbr.h"
|
|
23
|
|
24 #include "cfgparser.h"
|
|
25
|
|
26 /**********************************************************************/
|
|
27 /* Divx4 quality to XviD encoder motion flag presets */
|
|
28 static int const divx4_motion_presets[7] = {
|
|
29 0,
|
|
30 PMV_EARLYSTOP16,
|
|
31 PMV_EARLYSTOP16 | PMV_ADVANCEDDIAMOND16,
|
|
32 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,
|
|
33 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8,
|
|
34 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8,
|
|
35 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8
|
|
36 };
|
|
37
|
|
38 /* Divx4 quality to general encoder flag presets */
|
|
39 static int const divx4_general_presets[7] = {
|
|
40 0,
|
|
41 XVID_H263QUANT,
|
|
42 XVID_H263QUANT,
|
|
43 XVID_H263QUANT | XVID_HALFPEL,
|
|
44 XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,
|
|
45 XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,
|
|
46 XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL
|
|
47 };
|
|
48
|
|
49 struct {
|
|
50 int quality;
|
|
51 int bitrate;
|
|
52 int rc_reaction_delay_factor;
|
|
53 int rc_averaging_period;
|
|
54 int rc_buffer;
|
|
55 int max_quantizer;
|
|
56 int min_quantizer;
|
|
57 int max_key_interval;
|
|
58 enum {
|
|
59 XVID_MODE_CBR = 0, XVID_MODE_2PASS_1, XVID_MODE_2PASS_2, XVID_MODE_FIXED_QUANT,
|
|
60 XVID_MODE_UNSPEC = -1
|
|
61 } mode;
|
|
62 int debug;
|
|
63 char *stats_file;;
|
|
64 int keyframe_boost;
|
|
65 int kfthreshold;
|
|
66 int kfreduction;
|
|
67 int min_key_interval;
|
|
68 int fixed_quant;
|
|
69 } xvidenc_param = {
|
|
70 sizeof(divx4_motion_presets) / sizeof(divx4_motion_presets[0]) - 1, /* quality */
|
|
71 0, 0, 0, 0, 0, 0, 0,
|
|
72 XVID_MODE_CBR,
|
|
73 1, /* debug */
|
|
74 NULL, /* stats_file */
|
|
75 -1, -1, -1, /* keyframe_boost, kfthreshold, kfreduction */
|
|
76 -1, /* min_key_interval */
|
|
77 -1, /* fixed_quant */
|
|
78 };
|
|
79
|
7458
|
80 static struct config mode_conf[] = {
|
|
81 /* cbr, vbrqual, vbrquant, 2pass-1, 2pass-2-int, 2pass-2-ext */
|
|
82 { "cbr", &xvidenc_param.mode, CONF_TYPE_FLAG, 0, 0, XVID_MODE_CBR, NULL},
|
|
83 { "fixedquant", &xvidenc_param.mode, CONF_TYPE_FLAG, 0, 0, XVID_MODE_FIXED_QUANT, NULL},
|
|
84 { "2pass-1", &xvidenc_param.mode, CONF_TYPE_FLAG, 0, 0, XVID_MODE_2PASS_1, NULL},
|
|
85 { "2pass-2", &xvidenc_param.mode, CONF_TYPE_FLAG, 0, 0, XVID_MODE_2PASS_2, NULL},
|
|
86 { "help", "\nAvailable modes: \n"
|
|
87 " cbr - Constant Bit Rate\n"
|
|
88 " 2pass-1 - First pass of two pass mode\n"
|
|
89 " 2pass-2 - Second pass of two pass mode\n"
|
|
90 " fixedquant - Fixed quantizer mode\n"
|
|
91 "\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL},
|
|
92 { NULL, NULL, 0, 0, 0, 0, NULL}
|
|
93 };
|
|
94
|
7456
|
95 struct config xvidencopts_conf[] = {
|
7458
|
96 { "mode", mode_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
|
7456
|
97 { "quality", &xvidenc_param.quality, CONF_TYPE_INT, CONF_RANGE, 0,
|
|
98 sizeof(divx4_motion_presets) / sizeof(divx4_motion_presets[0]) - 1, NULL},
|
|
99 { "br", &xvidenc_param.bitrate, CONF_TYPE_INT, 0, 0, 0, NULL},
|
|
100 { "rc_reaction_delay_factor", &xvidenc_param.rc_reaction_delay_factor, CONF_TYPE_INT, 0, 0, NULL},
|
|
101 { "rc_averaging_period", &xvidenc_param.rc_averaging_period, CONF_TYPE_INT, 0, 0, NULL},
|
|
102 { "rc_buffer", &xvidenc_param.rc_buffer, CONF_TYPE_INT, 0, 0, NULL},
|
|
103 { "max_quantizer", &xvidenc_param.max_quantizer, CONF_TYPE_INT, 0, 0, NULL},
|
|
104 { "min_quantizer", &xvidenc_param.max_quantizer, CONF_TYPE_INT, 0, 0, NULL},
|
|
105 { "max_key_interval", &xvidenc_param.max_key_interval, CONF_TYPE_INT, 0, 0, NULL},
|
|
106 { "nodebug", &xvidenc_param.debug, CONF_TYPE_FLAG, 0, 0, 0, NULL},
|
|
107 { "debug", &xvidenc_param.debug, CONF_TYPE_FLAG, 0, 0, 1, NULL},
|
|
108 { "statsfile", &xvidenc_param.stats_file, CONF_TYPE_STRING, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_1/22 */
|
|
109 { "keyframe_boost", &xvidenc_param.keyframe_boost, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */
|
|
110 { "kfthreshold", &xvidenc_param.kfthreshold, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */
|
|
111 { "kfreduction", &xvidenc_param.kfreduction, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */
|
|
112 { "min_key_interval", &xvidenc_param.max_key_interval, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */
|
|
113 { "fixed_quant", &xvidenc_param.fixed_quant, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, /* for XVID_MODE_FIXED_QUANT */
|
|
114 { NULL, NULL, 0, 0, 0, 0, NULL}
|
|
115 };
|
|
116
|
|
117 struct vf_priv_s {
|
|
118 aviwrite_stream_t* mux;
|
|
119 XVID_ENC_FRAME enc_frame;
|
|
120 void* enc_handle;
|
|
121 vbr_control_t vbr_state;
|
|
122 FILE *hintfile;
|
|
123 };
|
|
124
|
|
125 static FILE *
|
|
126 get_hint_file(struct vf_instance_s* vf, unsigned char *mode)
|
|
127 {
|
|
128 if (vf->priv->hintfile == NULL) {
|
|
129 vf->priv->hintfile = fopen("xvid_hint_me.dat", mode);
|
|
130 if (vf->priv->hintfile == NULL)
|
|
131 perror("xvid: could not open xvid_hint_me.dat");
|
|
132 }
|
|
133 return vf->priv->hintfile;
|
|
134 }
|
|
135
|
|
136 static int
|
|
137 config(struct vf_instance_s* vf,
|
|
138 int width, int height, int d_width, int d_height,
|
|
139 unsigned int flags, unsigned int outfmt)
|
|
140 {
|
|
141 XVID_ENC_PARAM enc_param;
|
|
142
|
|
143 vf->priv->mux->bih->biWidth = width;
|
|
144 vf->priv->mux->bih->biHeight = height;
|
|
145 vf->priv->mux->bih->biSizeImage = vf->priv->mux->bih->biWidth * vf->priv->mux->bih->biHeight * 3;
|
|
146
|
|
147 memset(&enc_param, 0, sizeof(enc_param));
|
|
148 enc_param.width = width;
|
|
149 enc_param.height = height;
|
|
150 enc_param.fincr = vf->priv->mux->h.dwScale;
|
|
151 enc_param.fbase = vf->priv->mux->h.dwRate;
|
|
152 enc_param.rc_bitrate = xvidenc_param.bitrate;
|
|
153 enc_param.rc_reaction_delay_factor = xvidenc_param.rc_reaction_delay_factor;
|
|
154 enc_param.rc_averaging_period = xvidenc_param.rc_averaging_period;
|
|
155 enc_param.rc_buffer = xvidenc_param.rc_buffer;
|
|
156 enc_param.max_quantizer = xvidenc_param.max_quantizer;
|
|
157 enc_param.min_quantizer = xvidenc_param.min_quantizer;
|
|
158 enc_param.max_key_interval = xvidenc_param.max_key_interval;
|
|
159 switch (xvid_encore(NULL, XVID_ENC_CREATE, &enc_param, NULL)) {
|
|
160 case XVID_ERR_FAIL:
|
|
161 fprintf(stderr, "xvid: encoder creation failed\n");
|
|
162 return 0;
|
|
163 case XVID_ERR_MEMORY:
|
|
164 fprintf(stderr, "xvid: encoder creation failed, out of memory\n");
|
|
165 return 0;
|
|
166 case XVID_ERR_FORMAT:
|
|
167 fprintf(stderr, "xvid: encoder creation failed, bad format\n");
|
|
168 return 0;
|
|
169 }
|
|
170 vf->priv->enc_handle = enc_param.handle;
|
|
171
|
|
172 vf->priv->enc_frame.general = divx4_general_presets[xvidenc_param.quality];
|
|
173 vf->priv->enc_frame.motion = divx4_motion_presets[xvidenc_param.quality];
|
|
174 switch (outfmt) {
|
|
175 case IMGFMT_YV12:
|
|
176 vf->priv->enc_frame.colorspace = XVID_CSP_YV12;
|
|
177 break;
|
|
178 case IMGFMT_IYUV: case IMGFMT_I420:
|
|
179 vf->priv->enc_frame.colorspace = XVID_CSP_I420;
|
|
180 break;
|
|
181 case IMGFMT_YUY2:
|
|
182 vf->priv->enc_frame.colorspace = XVID_CSP_YUY2;
|
|
183 break;
|
|
184 case IMGFMT_UYVY:
|
|
185 vf->priv->enc_frame.colorspace = XVID_CSP_UYVY;
|
|
186 break;
|
|
187 case IMGFMT_RGB24: case IMGFMT_BGR24:
|
|
188 vf->priv->enc_frame.colorspace = XVID_CSP_RGB24;
|
|
189 break;
|
|
190 default:
|
|
191 mp_msg(MSGT_MENCODER,MSGL_ERR,"xvid: unsupported picture format (%s)!\n",
|
|
192 vo_format_name(outfmt));
|
|
193 return 0;
|
|
194 }
|
|
195 vf->priv->enc_frame.quant_intra_matrix = 0;
|
|
196 vf->priv->enc_frame.quant_inter_matrix = 0;
|
|
197
|
|
198 vf->priv->vbr_state.debug = xvidenc_param.debug;
|
|
199 vbrSetDefaults(&vf->priv->vbr_state);
|
|
200 vf->priv->vbr_state.fps = (double)enc_param.fbase / enc_param.fincr;
|
|
201 if (xvidenc_param.stats_file)
|
|
202 vf->priv->vbr_state.filename = xvidenc_param.stats_file;
|
|
203 if (xvidenc_param.bitrate)
|
|
204 vf->priv->vbr_state.desired_bitrate = xvidenc_param.bitrate;
|
|
205 if (xvidenc_param.keyframe_boost)
|
|
206 vf->priv->vbr_state.keyframe_boost = xvidenc_param.keyframe_boost;
|
|
207 if (xvidenc_param.kfthreshold)
|
|
208 vf->priv->vbr_state.kftreshold = xvidenc_param.kfthreshold;
|
|
209 if (xvidenc_param.kfreduction)
|
|
210 vf->priv->vbr_state.kfreduction = xvidenc_param.kfreduction;
|
|
211 if (xvidenc_param.min_key_interval)
|
|
212 vf->priv->vbr_state.min_key_interval = xvidenc_param.min_key_interval;
|
|
213 if (xvidenc_param.max_key_interval)
|
|
214 vf->priv->vbr_state.max_key_interval = xvidenc_param.max_key_interval;
|
|
215 if (xvidenc_param.fixed_quant)
|
|
216 vf->priv->vbr_state.fixed_quant = xvidenc_param.fixed_quant;
|
|
217 switch (xvidenc_param.mode) {
|
|
218 case XVID_MODE_CBR:
|
|
219 vf->priv->vbr_state.mode = VBR_MODE_1PASS;
|
|
220 break;
|
|
221 case XVID_MODE_FIXED_QUANT:
|
|
222 vf->priv->vbr_state.mode = VBR_MODE_FIXED_QUANT;
|
|
223 break;
|
|
224 case XVID_MODE_2PASS_1:
|
|
225 vf->priv->vbr_state.mode = VBR_MODE_2PASS_1;
|
|
226 break;
|
|
227 case XVID_MODE_2PASS_2:
|
|
228 vf->priv->vbr_state.mode = VBR_MODE_2PASS_2;
|
|
229 break;
|
|
230 default:
|
|
231 abort();
|
|
232 }
|
|
233 vbrInit(&vf->priv->vbr_state);
|
|
234 return 1;
|
|
235 }
|
|
236
|
|
237 static void
|
|
238 uninit(struct vf_instance_s* vf)
|
|
239 {
|
|
240 if (vf->priv->hintfile)
|
|
241 fclose(vf->priv->hintfile);
|
|
242 vbrFinish(&vf->priv->vbr_state);
|
|
243 }
|
|
244
|
|
245 static int
|
|
246 control(struct vf_instance_s* vf, int request, void* data)
|
|
247 {
|
|
248 return CONTROL_UNKNOWN;
|
|
249 }
|
|
250
|
|
251 static int
|
|
252 query_format(struct vf_instance_s* vf, unsigned int fmt)
|
|
253 {
|
|
254 switch(fmt){
|
|
255 case IMGFMT_YV12: case IMGFMT_IYUV: case IMGFMT_I420:
|
|
256 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
|
|
257 case IMGFMT_YUY2: case IMGFMT_UYVY:
|
|
258 return VFCAP_CSP_SUPPORTED;
|
|
259 case IMGFMT_RGB24: case IMGFMT_BGR24:
|
|
260 return VFCAP_CSP_SUPPORTED | VFCAP_FLIPPED;
|
|
261 }
|
|
262 return 0;
|
|
263 }
|
|
264
|
|
265 static int
|
|
266 put_image(struct vf_instance_s* vf, mp_image_t *mpi)
|
|
267 {
|
|
268 XVID_ENC_STATS enc_stats;
|
|
269
|
|
270 vf->priv->enc_frame.bitstream = vf->priv->mux->buffer;
|
|
271 vf->priv->enc_frame.length = -1 /* vf->priv->mux->buffer_size */;
|
|
272 vf->priv->enc_frame.image = mpi->planes[0];
|
|
273 vf->priv->enc_frame.quant = vbrGetQuant(&vf->priv->vbr_state);
|
|
274 vf->priv->enc_frame.intra = vbrGetIntra(&vf->priv->vbr_state);
|
|
275 #if 0
|
|
276 if (xvidenc_param.mode == XVID_MODE_2PASS_1) {
|
|
277 vf->priv->enc_frame.hint.hintstream = hintstream;
|
|
278 vf->priv->enc_frame.hint.rawhints = 0;
|
|
279 vf->priv->enc_frame.general |= XVID_HINTEDME_GET;
|
|
280 }
|
|
281 else if (xvidenc_param.mode == XVID_MODE_2PASS_2) {
|
|
282 FILE *f = get_hint_file(vf, "r");
|
|
283 vf->priv->enc_frame.general &= ~XVID_HINTEDME_SET;
|
|
284 if (f) {
|
|
285 int blocksize;
|
|
286 int read;
|
|
287 read = fread(&blocksize, sizeof(blocksize), 1, f);
|
|
288 if (read == 1) {
|
|
289 read = fread(hintstream, blocksize, 1, f);
|
|
290 if (read == blocksize) {
|
|
291 vf->priv->enc_frame.hint.hintstream = hintstream;
|
|
292 vf->priv->enc_frame.hint.rawhints = 0;
|
|
293 vf->priv->enc_frame.general |= XVID_HINTEDME_SET;
|
|
294 }
|
|
295 else
|
|
296 perror("xvid: hint file read block failure");
|
|
297 }
|
|
298 else
|
|
299 perror("xvid: hint file read failure");
|
|
300 }
|
|
301 }
|
|
302 #endif
|
|
303 switch (xvid_encore(vf->priv->enc_handle, XVID_ENC_ENCODE, &vf->priv->enc_frame, &enc_stats)) {
|
|
304 case XVID_ERR_OK:
|
|
305 break;
|
|
306 case XVID_ERR_MEMORY:
|
|
307 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: out of memory\n");
|
|
308 break;
|
|
309 case XVID_ERR_FORMAT:
|
|
310 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: bad format\n");
|
|
311 break;
|
|
312 default:
|
|
313 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: failure\n");
|
|
314 break;
|
|
315 }
|
|
316 mencoder_write_chunk(vf->priv->mux, vf->priv->enc_frame.length, vf->priv->enc_frame.intra ? 0x10 : 0);
|
|
317 vbrUpdate(&vf->priv->vbr_state, enc_stats.quant, vf->priv->enc_frame.intra,
|
|
318 enc_stats.hlength, vf->priv->enc_frame.length, enc_stats.kblks, enc_stats.mblks, enc_stats.ublks);
|
|
319 #if 1
|
|
320 if (vf->priv->enc_frame.general & XVID_HINTEDME_GET) {
|
|
321 FILE *f = get_hint_file(vf, "w");
|
|
322 if (f) {
|
|
323 unsigned int wrote;
|
|
324 wrote = fwrite(&vf->priv->enc_frame.hint.hintlength, sizeof(vf->priv->enc_frame.hint.hintlength), 1, f);
|
|
325 if (wrote == 1) {
|
|
326 wrote = fwrite(&vf->priv->enc_frame.hint.hintstream, vf->priv->enc_frame.hint.hintlength, 1, f);
|
|
327 if (wrote != 1)
|
|
328 perror("xvid: hint write block failure");
|
|
329 }
|
|
330 else
|
|
331 perror("xvid: hint write failure");
|
|
332 }
|
|
333 }
|
|
334 #endif
|
|
335 return 1;
|
|
336 }
|
|
337
|
|
338 //===========================================================================//
|
|
339
|
|
340 static int
|
|
341 vf_open(vf_instance_t *vf, char* args)
|
|
342 {
|
|
343 XVID_INIT_PARAM params = { 0, 0, 0};
|
|
344 vf->config = config;
|
|
345 vf->control = control;
|
|
346 vf->uninit = uninit;
|
|
347 vf->query_format = query_format;
|
|
348 vf->put_image = put_image;
|
|
349 vf->priv = malloc(sizeof(struct vf_priv_s));
|
|
350 memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
|
351 vf->priv->mux = (aviwrite_stream_t*)args;
|
|
352
|
|
353 vf->priv->mux->bih = malloc(sizeof(BITMAPINFOHEADER));
|
|
354 vf->priv->mux->bih->biSize = sizeof(BITMAPINFOHEADER);
|
|
355 vf->priv->mux->bih->biWidth = 0;
|
|
356 vf->priv->mux->bih->biHeight = 0;
|
|
357 vf->priv->mux->bih->biPlanes = 1;
|
|
358 vf->priv->mux->bih->biBitCount = 24;
|
|
359 vf->priv->mux->bih->biCompression = mmioFOURCC('X','V','I','D');
|
|
360
|
|
361 if (xvid_init(NULL, 0, ¶ms, NULL) != XVID_ERR_OK) {
|
|
362 fprintf(stderr, "xvid: initialisation failure\n");
|
|
363 abort();
|
|
364 }
|
|
365 if (params.api_version != API_VERSION) {
|
|
366 fprintf(stderr, "xvid: XviD library API version mismatch\n"
|
|
367 "\texpected %d.%d, got %d.%d, you should recompile MPlayer.\n",
|
|
368 API_VERSION >> 16, API_VERSION & 0xff,
|
|
369 params.api_version >> 16, params.api_version & 0xff);
|
|
370 abort();
|
|
371 }
|
|
372
|
|
373 return 1;
|
|
374 }
|
|
375
|
|
376 vf_info_t ve_info_xvid = {
|
|
377 "XviD encoder",
|
|
378 "xvid",
|
|
379 "Kim Minh Kaplan",
|
|
380 "for internal use by mencoder",
|
|
381 vf_open
|
|
382 };
|
|
383
|
|
384 //===========================================================================//
|
|
385 #endif
|