[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.12 - (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.10 * $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 :     /* context struct */
38 :     typedef struct
39 :     {
40 :     FILE * stat_file;
41 : suxen_drol 1.1.2.3
42 :     double fq_error;
43 : suxen_drol 1.1.2.1 } rc_2pass1_t;
44 :    
45 :    
46 :    
47 :     static int rc_2pass1_create(xvid_plg_create_t * create, rc_2pass1_t ** handle)
48 :     {
49 :     xvid_plugin_2pass1_t * param = (xvid_plugin_2pass1_t *)create->param;
50 :     rc_2pass1_t * rc;
51 :    
52 :     /* check filename */
53 : edgomez 1.1.2.6 if ((param->filename == NULL) ||
54 :     (param->filename != NULL && param->filename[0] == '\0'))
55 : suxen_drol 1.1.2.1 return XVID_ERR_FAIL;
56 :    
57 :     /* allocate context struct */
58 :     if((rc = malloc(sizeof(rc_2pass1_t))) == NULL)
59 :     return(XVID_ERR_MEMORY);
60 :    
61 : edgomez 1.1.2.7 /* Initialize safe defaults for 2pass 1 */
62 : suxen_drol 1.1.2.1 rc->stat_file = NULL;
63 :    
64 :     /* Open the 1st pass file */
65 : edgomez 1.1.2.8 if((rc->stat_file = fopen(param->filename, "w+b")) == NULL)
66 : suxen_drol 1.1.2.1 return(XVID_ERR_FAIL);
67 : suxen_drol 1.1.2.3
68 : edgomez 1.1.2.12 /* 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 : suxen_drol 1.1.2.1 /*
96 :     * The File Header
97 :     */
98 : edgomez 1.1.2.8 fprintf(rc->stat_file, "# XviD 2pass stat file (core version %d.%d.%d)\n",
99 :     XVID_VERSION_MAJOR(XVID_VERSION),
100 :     XVID_VERSION_MINOR(XVID_VERSION),
101 :     XVID_VERSION_PATCH(XVID_VERSION));
102 :     fprintf(rc->stat_file, "# Please do not modify this file\n\n");
103 : edgomez 1.1.2.7
104 : suxen_drol 1.1.2.3 rc->fq_error = 0;
105 : suxen_drol 1.1.2.1
106 :     *handle = rc;
107 :     return(0);
108 :     }
109 :    
110 :    
111 :     static int rc_2pass1_destroy(rc_2pass1_t * rc, xvid_plg_destroy_t * destroy)
112 :     {
113 : edgomez 1.1.2.12 if (rc->stat_file) {
114 :     if (fclose(rc->stat_file) == EOF) {
115 :     DPRINTF(XVID_DEBUG_RC, "Error closing stats file (%s)", strerror(errno));
116 :     }
117 :     }
118 :     rc->stat_file = NULL; /* Just a paranoid reset */
119 :     free(rc); /* as the container structure is freed anyway */
120 : suxen_drol 1.1.2.1 return(0);
121 :     }
122 :    
123 :    
124 :     static int rc_2pass1_before(rc_2pass1_t * rc, xvid_plg_data_t * data)
125 :     {
126 : edgomez 1.1.2.10 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;
133 :     }
134 :     }
135 :     return(0);
136 : suxen_drol 1.1.2.1 }
137 :    
138 :    
139 :     static int rc_2pass1_after(rc_2pass1_t * rc, xvid_plg_data_t * data)
140 :     {
141 :     char type;
142 : edgomez 1.1.2.11 xvid_enc_stats_t *stats = &data->stats;
143 : suxen_drol 1.1.2.1
144 :     /* Frame type in ascii I/P/B */
145 : edgomez 1.1.2.11 switch(stats->type) {
146 : suxen_drol 1.1.2.1 case XVID_TYPE_IVOP:
147 :     type = 'i';
148 :     break;
149 :     case XVID_TYPE_PVOP:
150 :     type = 'p';
151 :     break;
152 :     case XVID_TYPE_BVOP:
153 :     type = 'b';
154 :     break;
155 :     case XVID_TYPE_SVOP:
156 :     type = 's';
157 :     break;
158 :     default: /* Should not go here */
159 :     return(XVID_ERR_FAIL);
160 :     }
161 :    
162 :     /* write the resulting statistics */
163 :    
164 : edgomez 1.1.2.11 fprintf(rc->stat_file, "%c %d %d %d %d %d %d\n",
165 :     type,
166 :     stats->quant,
167 :     stats->kblks,
168 :     stats->mblks,
169 :     stats->ublks,
170 :     stats->length,
171 :     stats->hlength);
172 : suxen_drol 1.1.2.1
173 :     return(0);
174 :     }
175 :    
176 :    
177 :    
178 :     int xvid_plugin_2pass1(void * handle, int opt, void * param1, void * param2)
179 :     {
180 :     switch(opt)
181 :     {
182 :     case XVID_PLG_INFO :
183 : syskin 1.1.2.9 case XVID_PLG_FRAME :
184 : suxen_drol 1.1.2.1 return 0;
185 :    
186 :     case XVID_PLG_CREATE :
187 :     return rc_2pass1_create((xvid_plg_create_t*)param1, param2);
188 :    
189 :     case XVID_PLG_DESTROY :
190 :     return rc_2pass1_destroy((rc_2pass1_t*)handle, (xvid_plg_destroy_t*)param1);
191 :    
192 :     case XVID_PLG_BEFORE :
193 :     return rc_2pass1_before((rc_2pass1_t*)handle, (xvid_plg_data_t*)param1);
194 :    
195 :     case XVID_PLG_AFTER :
196 :     return rc_2pass1_after((rc_2pass1_t*)handle, (xvid_plg_data_t*)param1);
197 :     }
198 :    
199 :     return XVID_ERR_FAIL;
200 :     }

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