[cvs] / xvidcore / src / plugins / plugin_2pass1.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/plugins/plugin_2pass1.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.2.14 - (view) (download)

1 : suxen_drol 1.1.2.1 /******************************************************************************
2 :     *
3 : edgomez 1.1.2.5 * XviD Bit Rate Controller Library
4 :     * - VBR 2 pass bitrate controler implementation -
5 : suxen_drol 1.1.2.1 *
6 : edgomez 1.1.2.5 * Copyright (C) 2002-2003 Edouard Gomez <ed.gomez@free.fr>
7 : suxen_drol 1.1.2.1 *
8 : edgomez 1.1.2.5 * The curve treatment algorithm is the one implemented by Foxer <email?> and
9 :     * Dirk Knop <dknop@gwdg.de> for the XviD vfw dynamic library.
10 : suxen_drol 1.1.2.1 *
11 : edgomez 1.1.2.5 * This program is free software; you can redistribute it and/or modify
12 :     * it under the terms of the GNU General Public License as published by
13 :     * the Free Software Foundation; either version 2 of the License, or
14 :     * (at your option) any later version.
15 : suxen_drol 1.1.2.1 *
16 : edgomez 1.1.2.5 * This program is distributed in the hope that it will be useful,
17 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 :     * GNU General Public License for more details.
20 : suxen_drol 1.1.2.1 *
21 : edgomez 1.1.2.5 * You should have received a copy of the GNU General Public License
22 :     * along with this program; if not, write to the Free Software
23 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 : suxen_drol 1.1.2.1 *
25 : edgomez 1.1.2.14 * $Id$
26 : suxen_drol 1.1.2.1 *
27 :     *****************************************************************************/
28 :    
29 :     #include <stdio.h>
30 : edgomez 1.1.2.12 #include <errno.h> /* errno var (or function with recent libc) */
31 :     #include <string.h> /* strerror() */
32 : suxen_drol 1.1.2.1
33 :     #include "../xvid.h"
34 :     #include "../image/image.h"
35 :    
36 :    
37 : edgomez 1.1.2.14 /* This preprocessor constant controls wheteher or not, first pass is done
38 :     * using fast ME routines to speed up the 2pass process at the expense of
39 :     * less precise first pass stats */
40 : syskin 1.1.2.13 #define FAST1PASS
41 :    
42 :    
43 : suxen_drol 1.1.2.1 /* context struct */
44 :     typedef struct
45 :     {
46 :     FILE * stat_file;
47 : suxen_drol 1.1.2.3
48 :     double fq_error;
49 : suxen_drol 1.1.2.1 } rc_2pass1_t;
50 :    
51 :    
52 :    
53 :     static int rc_2pass1_create(xvid_plg_create_t * create, rc_2pass1_t ** handle)
54 :     {
55 :     xvid_plugin_2pass1_t * param = (xvid_plugin_2pass1_t *)create->param;
56 :     rc_2pass1_t * rc;
57 :    
58 :     /* check filename */
59 : edgomez 1.1.2.6 if ((param->filename == NULL) ||
60 :     (param->filename != NULL && param->filename[0] == '\0'))
61 : suxen_drol 1.1.2.1 return XVID_ERR_FAIL;
62 :    
63 :     /* allocate context struct */
64 :     if((rc = malloc(sizeof(rc_2pass1_t))) == NULL)
65 :     return(XVID_ERR_MEMORY);
66 :    
67 : edgomez 1.1.2.7 /* Initialize safe defaults for 2pass 1 */
68 : suxen_drol 1.1.2.1 rc->stat_file = NULL;
69 :    
70 :     /* Open the 1st pass file */
71 : edgomez 1.1.2.8 if((rc->stat_file = fopen(param->filename, "w+b")) == NULL)
72 : suxen_drol 1.1.2.1 return(XVID_ERR_FAIL);
73 : suxen_drol 1.1.2.3
74 : edgomez 1.1.2.12 /* I swear xvidcore isn't buggy, but when using mencoder+xvid4 i observe
75 :     * this weird bug.
76 :     *
77 :     * Symptoms: The stats file grows until it's fclosed, but at this moment
78 :     * a large part of the file is filled by 0x00 bytes w/o any
79 :     * reasonable cause. The stats file is then completly unusable
80 :     *
81 :     * So far, i think i found "the why":
82 :     * - take a MPEG stream containing 2 sequences (concatenate 2 MPEG files
83 :     * together)
84 :     * - Encode this MPEG file
85 :     *
86 :     * It should trigger the bug
87 :     *
88 :     * I think this is caused by some kind of race condition on mencoder module
89 :     * start/stop.
90 :     * - mencoder encodes the first sequence
91 :     * + xvid4 module opens xvid-twopass.stats and writes stats in it.
92 :     * - mencoder detects the second sequence and initialize a second
93 :     * module and stops the old encoder
94 :     * + new xvid4 module opens a new xvid-twopass.stats, old xvid4
95 :     * module closes it
96 :     *
97 :     * This is IT, got a racing condition.
98 :     * Unbuffered IO, may help ... */
99 :     setbuf(rc->stat_file, NULL);
100 :    
101 : suxen_drol 1.1.2.1 /*
102 :     * The File Header
103 :     */
104 : edgomez 1.1.2.8 fprintf(rc->stat_file, "# XviD 2pass stat file (core version %d.%d.%d)\n",
105 :     XVID_VERSION_MAJOR(XVID_VERSION),
106 :     XVID_VERSION_MINOR(XVID_VERSION),
107 :     XVID_VERSION_PATCH(XVID_VERSION));
108 :     fprintf(rc->stat_file, "# Please do not modify this file\n\n");
109 : edgomez 1.1.2.7
110 : suxen_drol 1.1.2.3 rc->fq_error = 0;
111 : suxen_drol 1.1.2.1
112 :     *handle = rc;
113 :     return(0);
114 :     }
115 :    
116 :    
117 :     static int rc_2pass1_destroy(rc_2pass1_t * rc, xvid_plg_destroy_t * destroy)
118 :     {
119 : edgomez 1.1.2.12 if (rc->stat_file) {
120 :     if (fclose(rc->stat_file) == EOF) {
121 :     DPRINTF(XVID_DEBUG_RC, "Error closing stats file (%s)", strerror(errno));
122 :     }
123 :     }
124 :     rc->stat_file = NULL; /* Just a paranoid reset */
125 :     free(rc); /* as the container structure is freed anyway */
126 : suxen_drol 1.1.2.1 return(0);
127 :     }
128 :    
129 :    
130 :     static int rc_2pass1_before(rc_2pass1_t * rc, xvid_plg_data_t * data)
131 :     {
132 : edgomez 1.1.2.10 if (data->quant <= 0) {
133 :     if (data->zone && data->zone->mode == XVID_ZONE_QUANT) {
134 : edgomez 1.1.2.14 /* We disable no options in quant zones, as their implementation is
135 :     * based on the fact we do first pass exactly the same way as the
136 :     * second one to have exact zone size */
137 : edgomez 1.1.2.10 rc->fq_error += (double)data->zone->increment / (double)data->zone->base;
138 :     data->quant = (int)rc->fq_error;
139 :     rc->fq_error -= data->quant;
140 :     } else {
141 :     data->quant = 2;
142 : syskin 1.1.2.13
143 :     #ifdef FAST1PASS
144 : edgomez 1.1.2.14 /* Given the fact our 2pass algorithm is based on very simple
145 :     * rules, we can disable some options that are too CPU intensive
146 :     * and do not provide the 2nd pass any benefit */
147 :    
148 :     /* First disable some motion flags */
149 :     data->motion_flags &= ~XVID_ME_CHROMA_PVOP;
150 :     data->motion_flags &= ~XVID_ME_CHROMA_BVOP;
151 :     data->motion_flags &= ~XVID_ME_USESQUARES16;
152 :     data->motion_flags &= ~XVID_ME_ADVANCEDDIAMOND16;
153 :     data->motion_flags &= ~XVID_ME_EXTSEARCH16;
154 :    
155 :     /* And enable fast replacements */
156 :     data->motion_flags |= XVID_ME_FAST_MODEINTERPOLATE;
157 :     data->motion_flags |= XVID_ME_SKIP_DELTASEARCH;
158 :     data->motion_flags |= XVID_ME_FASTREFINE16;
159 :     data->motion_flags |= XVID_ME_BFRAME_EARLYSTOP;
160 :    
161 :     /* Now VOP flags (no fast replacements) */
162 :     data->vop_flags &= ~XVID_VOP_MODEDECISION_RD;
163 :     data->vop_flags &= ~XVID_VOP_FAST_MODEDECISION_RD;
164 :     data->vop_flags &= ~XVID_VOP_TRELLISQUANT;
165 :     data->vop_flags &= ~XVID_VOP_INTER4V;
166 :     data->vop_flags &= ~XVID_VOP_HQACPRED;
167 :    
168 :     /* Finnaly VOL flags
169 :     *
170 :     * NB: Qpel cannot be disable because this option really changes
171 :     * too much the texture data compressibility, and thus the
172 :     * second pass gets confused by too much impredictability
173 :     * of frame sizes, and actually hurts quality */
174 : syskin 1.1.2.13 data->vol_flags &= ~XVID_VOL_GMC;
175 :     #endif
176 : edgomez 1.1.2.10 }
177 :     }
178 :     return(0);
179 : suxen_drol 1.1.2.1 }
180 :    
181 :    
182 :     static int rc_2pass1_after(rc_2pass1_t * rc, xvid_plg_data_t * data)
183 :     {
184 :     char type;
185 : edgomez 1.1.2.11 xvid_enc_stats_t *stats = &data->stats;
186 : suxen_drol 1.1.2.1
187 :     /* Frame type in ascii I/P/B */
188 : edgomez 1.1.2.11 switch(stats->type) {
189 : suxen_drol 1.1.2.1 case XVID_TYPE_IVOP:
190 :     type = 'i';
191 :     break;
192 :     case XVID_TYPE_PVOP:
193 :     type = 'p';
194 :     break;
195 :     case XVID_TYPE_BVOP:
196 :     type = 'b';
197 :     break;
198 :     case XVID_TYPE_SVOP:
199 :     type = 's';
200 :     break;
201 :     default: /* Should not go here */
202 :     return(XVID_ERR_FAIL);
203 :     }
204 :    
205 :     /* write the resulting statistics */
206 :    
207 : edgomez 1.1.2.11 fprintf(rc->stat_file, "%c %d %d %d %d %d %d\n",
208 :     type,
209 :     stats->quant,
210 :     stats->kblks,
211 :     stats->mblks,
212 :     stats->ublks,
213 :     stats->length,
214 :     stats->hlength);
215 : suxen_drol 1.1.2.1
216 :     return(0);
217 :     }
218 :    
219 :    
220 :    
221 :     int xvid_plugin_2pass1(void * handle, int opt, void * param1, void * param2)
222 :     {
223 :     switch(opt)
224 :     {
225 :     case XVID_PLG_INFO :
226 : syskin 1.1.2.9 case XVID_PLG_FRAME :
227 : suxen_drol 1.1.2.1 return 0;
228 :    
229 :     case XVID_PLG_CREATE :
230 :     return rc_2pass1_create((xvid_plg_create_t*)param1, param2);
231 :    
232 :     case XVID_PLG_DESTROY :
233 :     return rc_2pass1_destroy((rc_2pass1_t*)handle, (xvid_plg_destroy_t*)param1);
234 :    
235 :     case XVID_PLG_BEFORE :
236 :     return rc_2pass1_before((rc_2pass1_t*)handle, (xvid_plg_data_t*)param1);
237 :    
238 :     case XVID_PLG_AFTER :
239 :     return rc_2pass1_after((rc_2pass1_t*)handle, (xvid_plg_data_t*)param1);
240 :     }
241 :    
242 :     return XVID_ERR_FAIL;
243 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4