comparison libmpcodecs/ve_xvid4.c @ 11437:7826e4e376c7

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