3 |
* XviD Bit Rate Controller Library |
* XviD Bit Rate Controller Library |
4 |
* - VBR 2 pass bitrate controler implementation - |
* - VBR 2 pass bitrate controler implementation - |
5 |
* |
* |
6 |
* Copyright (C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr> |
* Copyright (C) 2002-2003 Edouard Gomez <ed.gomez@free.fr> |
7 |
* |
* |
8 |
* The curve treatment algorithm is the one implemented by Foxer <email?> and |
* The curve treatment algorithm is the one implemented by Foxer <email?> and |
9 |
* Dirk Knop <dknop@gwdg.de> for the XviD vfw dynamic library. |
* Dirk Knop <dknop@gwdg.de> for the XviD vfw dynamic library. |
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" |
38 |
typedef struct |
typedef struct |
39 |
{ |
{ |
40 |
FILE * stat_file; |
FILE * stat_file; |
41 |
|
|
42 |
|
double fq_error; |
43 |
} rc_2pass1_t; |
} rc_2pass1_t; |
44 |
|
|
45 |
|
|
50 |
rc_2pass1_t * rc; |
rc_2pass1_t * rc; |
51 |
|
|
52 |
/* check filename */ |
/* check filename */ |
53 |
if (param->filename == NULL || param->filename[0] == '\0') |
if ((param->filename == NULL) || |
54 |
|
(param->filename != NULL && param->filename[0] == '\0')) |
55 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
56 |
|
|
57 |
/* allocate context struct */ |
/* allocate context struct */ |
62 |
rc->stat_file = NULL; |
rc->stat_file = NULL; |
63 |
|
|
64 |
/* Open the 1st pass file */ |
/* Open the 1st pass file */ |
65 |
if((rc->stat_file = fopen(param->filename, "w+")) == NULL) |
if((rc->stat_file = fopen(param->filename, "w+b")) == NULL) |
66 |
return(XVID_ERR_FAIL); |
return(XVID_ERR_FAIL); |
67 |
|
|
68 |
|
/* I swear xvidcore isn't buggy, but when using mencoder+xvid4 i observe |
69 |
|
* this weird bug. |
70 |
|
* |
71 |
|
* Symptoms: The stats file grows until it's fclosed, but at this moment |
72 |
|
* a large part of the file is filled by 0x00 bytes w/o any |
73 |
|
* reasonable cause. The stats file is then completly unusable |
74 |
|
* |
75 |
|
* So far, i think i found "the why": |
76 |
|
* - take a MPEG stream containing 2 sequences (concatenate 2 MPEG files |
77 |
|
* together) |
78 |
|
* - Encode this MPEG file |
79 |
|
* |
80 |
|
* It should trigger the bug |
81 |
|
* |
82 |
|
* I think this is caused by some kind of race condition on mencoder module |
83 |
|
* start/stop. |
84 |
|
* - mencoder encodes the first sequence |
85 |
|
* + xvid4 module opens xvid-twopass.stats and writes stats in it. |
86 |
|
* - mencoder detects the second sequence and initialize a second |
87 |
|
* module and stops the old encoder |
88 |
|
* + new xvid4 module opens a new xvid-twopass.stats, old xvid4 |
89 |
|
* module closes it |
90 |
|
* |
91 |
|
* This is IT, got a racing condition. |
92 |
|
* Unbuffered IO, may help ... */ |
93 |
|
setbuf(rc->stat_file, NULL); |
94 |
|
|
95 |
/* |
/* |
96 |
* The File Header |
* The File Header |
97 |
*/ |
*/ |
98 |
/* fprintf(rc->stat_file, "# XviD 2pass stat file\n"); |
fprintf(rc->stat_file, "# XviD 2pass stat file (core version %d.%d.%d)\n", |
99 |
fprintf(rc->stat_file, "version %i.%i.%i\n",XVID_MAJOR(XVID_VERSION), XVID_MINOR(XVID_VERSION), XVID_PATCH(XVID_VERSION)); |
XVID_VERSION_MAJOR(XVID_VERSION), |
100 |
fprintf(rc->stat_file, "start\n"); |
XVID_VERSION_MINOR(XVID_VERSION), |
101 |
fprintf(rc->stat_file, "type quantizer length kblocks mblocks ublocks\n"); */ |
XVID_VERSION_PATCH(XVID_VERSION)); |
102 |
|
fprintf(rc->stat_file, "# Please do not modify this file\n\n"); |
103 |
|
|
104 |
|
rc->fq_error = 0; |
105 |
|
|
106 |
*handle = rc; |
*handle = rc; |
107 |
return(0); |
return(0); |
110 |
|
|
111 |
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) |
112 |
{ |
{ |
113 |
//fprintf(rc->stat_file, "stop\n"); |
if (rc->stat_file) { |
114 |
fclose(rc->stat_file); |
if (fclose(rc->stat_file) == EOF) { |
115 |
|
DPRINTF(XVID_DEBUG_RC, "Error closing stats file (%s)", strerror(errno)); |
116 |
free(rc); |
} |
117 |
|
} |
118 |
|
rc->stat_file = NULL; /* Just a paranoid reset */ |
119 |
|
free(rc); /* as the container structure is freed anyway */ |
120 |
return(0); |
return(0); |
121 |
} |
} |
122 |
|
|
123 |
|
|
124 |
static int rc_2pass1_before(rc_2pass1_t * rc, xvid_plg_data_t * data) |
static int rc_2pass1_before(rc_2pass1_t * rc, xvid_plg_data_t * data) |
125 |
{ |
{ |
126 |
|
if (data->quant <= 0) { |
127 |
|
if (data->zone && data->zone->mode == XVID_ZONE_QUANT) { |
128 |
|
rc->fq_error += (double)data->zone->increment / (double)data->zone->base; |
129 |
|
data->quant = (int)rc->fq_error; |
130 |
|
rc->fq_error -= data->quant; |
131 |
|
} else { |
132 |
data->quant = 2; |
data->quant = 2; |
133 |
data->type = XVID_TYPE_AUTO; |
} |
134 |
return 0; |
} |
135 |
|
return(0); |
136 |
} |
} |
137 |
|
|
138 |
|
|
139 |
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) |
140 |
{ |
{ |
141 |
char type; |
char type; |
142 |
|
xvid_enc_stats_t *stats = &data->stats; |
143 |
|
|
144 |
/* Frame type in ascii I/P/B */ |
/* Frame type in ascii I/P/B */ |
145 |
switch(data->type) { |
switch(stats->type) { |
146 |
case XVID_TYPE_IVOP: |
case XVID_TYPE_IVOP: |
147 |
type = 'i'; |
type = 'i'; |
148 |
break; |
break; |
161 |
|
|
162 |
/* write the resulting statistics */ |
/* write the resulting statistics */ |
163 |
|
|
164 |
fprintf(rc->stat_file, "%c %d %d %d %d %d\n", |
fprintf(rc->stat_file, "%c %d %d %d %d %d %d\n", |
165 |
type, |
type, |
166 |
data->quant, |
stats->quant, |
167 |
data->length, |
stats->kblks, |
168 |
data->kblks, |
stats->mblks, |
169 |
data->mblks, |
stats->ublks, |
170 |
data->ublks); |
stats->length, |
171 |
|
stats->hlength); |
172 |
|
|
173 |
return(0); |
return(0); |
174 |
} |
} |
180 |
switch(opt) |
switch(opt) |
181 |
{ |
{ |
182 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
183 |
|
case XVID_PLG_FRAME : |
184 |
return 0; |
return 0; |
185 |
|
|
186 |
case XVID_PLG_CREATE : |
case XVID_PLG_CREATE : |