1 |
|
/****************************************************************************** |
2 |
|
* |
3 |
|
* XviD Bit Rate Controller Library |
4 |
|
* - VBR 2 pass bitrate controler implementation - |
5 |
|
* |
6 |
|
* Copyright (C) 2002-2003 Edouard Gomez <ed.gomez@free.fr> |
7 |
|
* |
8 |
|
* 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 |
|
* |
11 |
|
* 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 |
|
* |
16 |
|
* 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 |
|
* |
21 |
|
* 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 |
|
* |
25 |
|
* $Id$ |
26 |
|
* |
27 |
|
*****************************************************************************/ |
28 |
|
|
29 |
|
#include <stdio.h> |
30 |
|
|
31 |
|
#include "../xvid.h" |
32 |
|
#include "../image/image.h" |
33 |
|
|
34 |
|
|
35 |
|
/* context struct */ |
36 |
|
typedef struct |
37 |
|
{ |
38 |
|
FILE * stat_file; |
39 |
|
|
40 |
|
double fq_error; |
41 |
|
} rc_2pass1_t; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
static int rc_2pass1_create(xvid_plg_create_t * create, rc_2pass1_t ** handle) |
46 |
|
{ |
47 |
|
xvid_plugin_2pass1_t * param = (xvid_plugin_2pass1_t *)create->param; |
48 |
|
rc_2pass1_t * rc; |
49 |
|
|
50 |
|
/* check filename */ |
51 |
|
if (param->filename == NULL || param->filename[0] == '\0') |
52 |
|
return XVID_ERR_FAIL; |
53 |
|
|
54 |
|
/* allocate context struct */ |
55 |
|
if((rc = malloc(sizeof(rc_2pass1_t))) == NULL) |
56 |
|
return(XVID_ERR_MEMORY); |
57 |
|
|
58 |
|
/* Initialize safe defaults for 2pass 1 */ |
59 |
|
rc->stat_file = NULL; |
60 |
|
|
61 |
|
/* Open the 1st pass file */ |
62 |
|
if((rc->stat_file = fopen(param->filename, "w+")) == NULL) |
63 |
|
return(XVID_ERR_FAIL); |
64 |
|
|
65 |
|
/* |
66 |
|
* The File Header |
67 |
|
*/ |
68 |
|
#if 0 |
69 |
|
fprintf(rc->stat_file, "# XviD 2pass stat file\n"); |
70 |
|
fprintf(rc->stat_file, "version %i.%i.%i\n",XVID_MAJOR(XVID_VERSION), XVID_MINOR(XVID_VERSION), XVID_PATCH(XVID_VERSION)); |
71 |
|
fprintf(rc->stat_file, "start\n"); |
72 |
|
fprintf(rc->stat_file, "type quantizer length kblocks mblocks ublocks\n"); |
73 |
|
#endif |
74 |
|
|
75 |
|
rc->fq_error = 0; |
76 |
|
|
77 |
|
*handle = rc; |
78 |
|
return(0); |
79 |
|
} |
80 |
|
|
81 |
|
|
82 |
|
static int rc_2pass1_destroy(rc_2pass1_t * rc, xvid_plg_destroy_t * destroy) |
83 |
|
{ |
84 |
|
fclose(rc->stat_file); |
85 |
|
|
86 |
|
free(rc); |
87 |
|
return(0); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
static int rc_2pass1_before(rc_2pass1_t * rc, xvid_plg_data_t * data) |
92 |
|
{ |
93 |
|
if (data->quant <= 0) { |
94 |
|
if (data->zone && data->zone->mode == XVID_ZONE_QUANT) { |
95 |
|
rc->fq_error += (double)data->zone->increment / (double)data->zone->base; |
96 |
|
data->quant = (int)rc->fq_error; |
97 |
|
rc->fq_error -= data->quant; |
98 |
|
|
99 |
|
}else { |
100 |
|
data->quant = 2; |
101 |
|
} |
102 |
|
} |
103 |
|
return 0; |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
static int rc_2pass1_after(rc_2pass1_t * rc, xvid_plg_data_t * data) |
108 |
|
{ |
109 |
|
char type; |
110 |
|
|
111 |
|
/* Frame type in ascii I/P/B */ |
112 |
|
switch(data->type) { |
113 |
|
case XVID_TYPE_IVOP: |
114 |
|
type = 'i'; |
115 |
|
break; |
116 |
|
case XVID_TYPE_PVOP: |
117 |
|
type = 'p'; |
118 |
|
break; |
119 |
|
case XVID_TYPE_BVOP: |
120 |
|
type = 'b'; |
121 |
|
break; |
122 |
|
case XVID_TYPE_SVOP: |
123 |
|
type = 's'; |
124 |
|
break; |
125 |
|
default: /* Should not go here */ |
126 |
|
return(XVID_ERR_FAIL); |
127 |
|
} |
128 |
|
|
129 |
|
/* write the resulting statistics */ |
130 |
|
|
131 |
|
fprintf(rc->stat_file, "%c %d %d %d %d %d\n", |
132 |
|
type, |
133 |
|
data->quant, |
134 |
|
data->kblks, |
135 |
|
data->mblks, |
136 |
|
data->ublks, |
137 |
|
data->length); |
138 |
|
|
139 |
|
return(0); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
int xvid_plugin_2pass1(void * handle, int opt, void * param1, void * param2) |
145 |
|
{ |
146 |
|
switch(opt) |
147 |
|
{ |
148 |
|
case XVID_PLG_INFO : |
149 |
|
return 0; |
150 |
|
|
151 |
|
case XVID_PLG_CREATE : |
152 |
|
return rc_2pass1_create((xvid_plg_create_t*)param1, param2); |
153 |
|
|
154 |
|
case XVID_PLG_DESTROY : |
155 |
|
return rc_2pass1_destroy((rc_2pass1_t*)handle, (xvid_plg_destroy_t*)param1); |
156 |
|
|
157 |
|
case XVID_PLG_BEFORE : |
158 |
|
return rc_2pass1_before((rc_2pass1_t*)handle, (xvid_plg_data_t*)param1); |
159 |
|
|
160 |
|
case XVID_PLG_AFTER : |
161 |
|
return rc_2pass1_after((rc_2pass1_t*)handle, (xvid_plg_data_t*)param1); |
162 |
|
} |
163 |
|
|
164 |
|
return XVID_ERR_FAIL; |
165 |
|
} |