27 |
*****************************************************************************/ |
*****************************************************************************/ |
28 |
|
|
29 |
#include <stdio.h> |
#include <stdio.h> |
30 |
|
#include <errno.h> /* errno var (or function with recent libc) */ |
31 |
|
#include <string.h> /* strerror() */ |
32 |
|
|
33 |
#include "../xvid.h" |
#include "../xvid.h" |
34 |
#include "../image/image.h" |
#include "../image/image.h" |
35 |
|
|
36 |
|
|
37 |
|
/* 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 |
|
#define FAST1PASS |
41 |
|
#define FAST1PASS_QPEL_TOO |
42 |
|
|
43 |
|
|
44 |
/* context struct */ |
/* context struct */ |
45 |
typedef struct |
typedef struct |
46 |
{ |
{ |
69 |
rc->stat_file = NULL; |
rc->stat_file = NULL; |
70 |
|
|
71 |
/* Open the 1st pass file */ |
/* Open the 1st pass file */ |
72 |
if((rc->stat_file = fopen(param->filename, "w+")) == NULL) |
if((rc->stat_file = fopen(param->filename, "w+b")) == NULL) |
73 |
return(XVID_ERR_FAIL); |
return(XVID_ERR_FAIL); |
74 |
|
|
75 |
|
/* I swear xvidcore isn't buggy, but when using mencoder+xvid4 i observe |
76 |
|
* this weird bug. |
77 |
|
* |
78 |
|
* Symptoms: The stats file grows until it's fclosed, but at this moment |
79 |
|
* a large part of the file is filled by 0x00 bytes w/o any |
80 |
|
* reasonable cause. The stats file is then completly unusable |
81 |
|
* |
82 |
|
* So far, i think i found "the why": |
83 |
|
* - take a MPEG stream containing 2 sequences (concatenate 2 MPEG files |
84 |
|
* together) |
85 |
|
* - Encode this MPEG file |
86 |
|
* |
87 |
|
* It should trigger the bug |
88 |
|
* |
89 |
|
* I think this is caused by some kind of race condition on mencoder module |
90 |
|
* start/stop. |
91 |
|
* - mencoder encodes the first sequence |
92 |
|
* + xvid4 module opens xvid-twopass.stats and writes stats in it. |
93 |
|
* - mencoder detects the second sequence and initialize a second |
94 |
|
* module and stops the old encoder |
95 |
|
* + new xvid4 module opens a new xvid-twopass.stats, old xvid4 |
96 |
|
* module closes it |
97 |
|
* |
98 |
|
* This is IT, got a racing condition. |
99 |
|
* Unbuffered IO, may help ... */ |
100 |
|
setbuf(rc->stat_file, NULL); |
101 |
|
|
102 |
/* |
/* |
103 |
* The File Header |
* The File Header |
104 |
*/ |
*/ |
105 |
#if 0 |
fprintf(rc->stat_file, "# XviD 2pass stat file (core version %d.%d.%d)\n", |
106 |
fprintf(rc->stat_file, "# XviD 2pass stat file\n"); |
XVID_VERSION_MAJOR(XVID_VERSION), |
107 |
fprintf(rc->stat_file, "version %i.%i.%i\n",XVID_MAJOR(XVID_VERSION), XVID_MINOR(XVID_VERSION), XVID_PATCH(XVID_VERSION)); |
XVID_VERSION_MINOR(XVID_VERSION), |
108 |
fprintf(rc->stat_file, "start\n"); |
XVID_VERSION_PATCH(XVID_VERSION)); |
109 |
fprintf(rc->stat_file, "type quantizer length kblocks mblocks ublocks\n"); |
fprintf(rc->stat_file, "# Please do not modify this file\n\n"); |
|
#endif |
|
110 |
|
|
111 |
rc->fq_error = 0; |
rc->fq_error = 0; |
112 |
|
|
117 |
|
|
118 |
static int rc_2pass1_destroy(rc_2pass1_t * rc, xvid_plg_destroy_t * destroy) |
static int rc_2pass1_destroy(rc_2pass1_t * rc, xvid_plg_destroy_t * destroy) |
119 |
{ |
{ |
120 |
fclose(rc->stat_file); |
if (rc->stat_file) { |
121 |
|
if (fclose(rc->stat_file) == EOF) { |
122 |
free(rc); |
DPRINTF(XVID_DEBUG_RC, "Error closing stats file (%s)", strerror(errno)); |
123 |
|
} |
124 |
|
} |
125 |
|
rc->stat_file = NULL; /* Just a paranoid reset */ |
126 |
|
free(rc); /* as the container structure is freed anyway */ |
127 |
return(0); |
return(0); |
128 |
} |
} |
129 |
|
|
132 |
{ |
{ |
133 |
if (data->quant <= 0) { |
if (data->quant <= 0) { |
134 |
if (data->zone && data->zone->mode == XVID_ZONE_QUANT) { |
if (data->zone && data->zone->mode == XVID_ZONE_QUANT) { |
135 |
|
/* We disable no options in quant zones, as their implementation is |
136 |
|
* based on the fact we do first pass exactly the same way as the |
137 |
|
* second one to have exact zone size */ |
138 |
rc->fq_error += (double)data->zone->increment / (double)data->zone->base; |
rc->fq_error += (double)data->zone->increment / (double)data->zone->base; |
139 |
data->quant = (int)rc->fq_error; |
data->quant = (int)rc->fq_error; |
140 |
rc->fq_error -= data->quant; |
rc->fq_error -= data->quant; |
|
|
|
141 |
}else { |
}else { |
142 |
data->quant = 2; |
data->quant = 2; |
143 |
|
|
144 |
|
#ifdef FAST1PASS |
145 |
|
/* Given the fact our 2pass algorithm is based on very simple |
146 |
|
* rules, we can disable some options that are too CPU intensive |
147 |
|
* and do not provide the 2nd pass any benefit */ |
148 |
|
|
149 |
|
/* First disable some motion flags */ |
150 |
|
data->motion_flags &= ~XVID_ME_CHROMA_PVOP; |
151 |
|
data->motion_flags &= ~XVID_ME_CHROMA_BVOP; |
152 |
|
data->motion_flags &= ~XVID_ME_USESQUARES16; |
153 |
|
data->motion_flags &= ~XVID_ME_ADVANCEDDIAMOND16; |
154 |
|
data->motion_flags &= ~XVID_ME_EXTSEARCH16; |
155 |
|
|
156 |
|
/* And enable fast replacements */ |
157 |
|
data->motion_flags |= XVID_ME_FAST_MODEINTERPOLATE; |
158 |
|
data->motion_flags |= XVID_ME_SKIP_DELTASEARCH; |
159 |
|
data->motion_flags |= XVID_ME_FASTREFINE16; |
160 |
|
data->motion_flags |= XVID_ME_BFRAME_EARLYSTOP; |
161 |
|
|
162 |
|
/* Now VOP flags (no fast replacements) */ |
163 |
|
data->vop_flags &= ~XVID_VOP_MODEDECISION_RD; |
164 |
|
data->vop_flags &= ~XVID_VOP_FAST_MODEDECISION_RD; |
165 |
|
data->vop_flags &= ~XVID_VOP_TRELLISQUANT; |
166 |
|
data->vop_flags &= ~XVID_VOP_INTER4V; |
167 |
|
data->vop_flags &= ~XVID_VOP_HQACPRED; |
168 |
|
|
169 |
|
/* Finnaly VOL flags |
170 |
|
* |
171 |
|
* NB: Qpel cannot be disable because this option really changes |
172 |
|
* too much the texture data compressibility, and thus the |
173 |
|
* second pass gets confused by too much impredictability |
174 |
|
* of frame sizes, and actually hurts quality */ |
175 |
|
#ifdef FAST1PASS_QPEL_TOO |
176 |
|
/* or maybe we can disable it after all? */ |
177 |
|
data->vol_flags &= ~XVID_VOL_QUARTERPEL; |
178 |
|
#endif |
179 |
|
data->vol_flags &= ~XVID_VOL_GMC; |
180 |
|
#endif |
181 |
} |
} |
182 |
} |
} |
183 |
return 0; |
return(0); |
184 |
} |
} |
185 |
|
|
186 |
|
|
187 |
static int rc_2pass1_after(rc_2pass1_t * rc, xvid_plg_data_t * data) |
static int rc_2pass1_after(rc_2pass1_t * rc, xvid_plg_data_t * data) |
188 |
{ |
{ |
189 |
char type; |
char type; |
190 |
|
xvid_enc_stats_t *stats = &data->stats; |
191 |
|
|
192 |
/* Frame type in ascii I/P/B */ |
/* Frame type in ascii I/P/B */ |
193 |
switch(data->type) { |
switch(stats->type) { |
194 |
case XVID_TYPE_IVOP: |
case XVID_TYPE_IVOP: |
195 |
type = 'i'; |
type = 'i'; |
196 |
break; |
break; |
209 |
|
|
210 |
/* write the resulting statistics */ |
/* write the resulting statistics */ |
211 |
|
|
212 |
fprintf(rc->stat_file, "%c %d %d %d %d %d\n", |
fprintf(rc->stat_file, "%c %d %d %d %d %d %d\n", |
213 |
type, |
type, |
214 |
data->quant, |
stats->quant, |
215 |
data->kblks, |
stats->kblks, |
216 |
data->mblks, |
stats->mblks, |
217 |
data->ublks, |
stats->ublks, |
218 |
data->length); |
stats->length, |
219 |
|
stats->hlength); |
220 |
|
|
221 |
return(0); |
return(0); |
222 |
} |
} |
228 |
switch(opt) |
switch(opt) |
229 |
{ |
{ |
230 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
231 |
|
case XVID_PLG_FRAME : |
232 |
return 0; |
return 0; |
233 |
|
|
234 |
case XVID_PLG_CREATE : |
case XVID_PLG_CREATE : |