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