3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002-2003 Peter Ross <pross@xvid.org> |
8 |
* to use this software module in hardware or software products are |
* 2002 Daniel Smith <danielsmith@astroboymail.com> |
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
9 |
* |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
21 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
|
****************************************************************************/ |
|
|
|
|
|
/***************************************************************************** |
|
|
* |
|
|
* History |
|
|
* |
|
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
|
|
* MinChen <chenm001@163.com> |
|
|
* 20.06.2002 bframe patch |
|
|
* 08.05.2002 fix some problem in DEBUG mode; |
|
|
* MinChen <chenm001@163.com> |
|
|
* 14.04.2002 added FrameCodeB() |
|
|
* |
|
24 |
* $Id$ |
* $Id$ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
|
|
|
28 |
#include <stdlib.h> |
#include <stdlib.h> |
29 |
#include <stdio.h> |
#include <stdio.h> |
30 |
#include <math.h> |
#include <math.h> |
35 |
#include "global.h" |
#include "global.h" |
36 |
#include "utils/timer.h" |
#include "utils/timer.h" |
37 |
#include "image/image.h" |
#include "image/image.h" |
|
#ifdef BFRAMES |
|
38 |
#include "image/font.h" |
#include "image/font.h" |
39 |
#include "motion/sad.h" |
#include "motion/sad.h" |
|
#endif |
|
40 |
#include "motion/motion.h" |
#include "motion/motion.h" |
41 |
|
#include "motion/gmc.h" |
42 |
|
|
43 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
44 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
45 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
46 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
|
#include "utils/ratecontrol.h" |
|
47 |
#include "utils/emms.h" |
#include "utils/emms.h" |
48 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
|
#include "quant/adapt_quant.h" |
|
49 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
50 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
51 |
|
|
52 |
/***************************************************************************** |
/***************************************************************************** |
|
* Local macros |
|
|
****************************************************************************/ |
|
|
|
|
|
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
|
|
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
|
|
|
|
|
/***************************************************************************** |
|
53 |
* Local function prototypes |
* Local function prototypes |
54 |
****************************************************************************/ |
****************************************************************************/ |
55 |
|
|
56 |
static int FrameCodeI(Encoder * pEnc, |
static int FrameCodeI(Encoder * pEnc, |
57 |
Bitstream * bs, |
Bitstream * bs); |
|
uint32_t * pBits); |
|
58 |
|
|
59 |
static int FrameCodeP(Encoder * pEnc, |
static int FrameCodeP(Encoder * pEnc, |
60 |
Bitstream * bs, |
Bitstream * bs); |
|
uint32_t * pBits, |
|
|
bool force_inter, |
|
|
bool vol_header); |
|
61 |
|
|
62 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
63 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
64 |
Bitstream * bs, |
Bitstream * bs); |
|
uint32_t * pBits); |
|
|
|
|
|
/***************************************************************************** |
|
|
* Local data |
|
|
****************************************************************************/ |
|
|
|
|
|
static int DQtab[4] = { |
|
|
-1, -2, 1, 2 |
|
|
}; |
|
|
|
|
|
static int iDQtab[5] = { |
|
|
1, 0, NO_CHANGE, 2, 3 |
|
|
}; |
|
|
|
|
|
|
|
|
static void __inline |
|
|
image_null(IMAGE * image) |
|
|
{ |
|
|
image->y = image->u = image->v = NULL; |
|
|
} |
|
65 |
|
|
66 |
|
|
67 |
/***************************************************************************** |
/***************************************************************************** |
75 |
* and cleaning code. |
* and cleaning code. |
76 |
* |
* |
77 |
* Returned values : |
* Returned values : |
78 |
* - XVID_ERR_OK - no errors |
* - 0 - no errors |
79 |
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
80 |
* cleans the structure before exiting. |
* cleans the structure before exiting. |
81 |
* pParam->handle is also set to NULL. |
* pParam->handle is also set to NULL. |
82 |
* |
* |
83 |
****************************************************************************/ |
****************************************************************************/ |
84 |
|
|
|
int |
|
|
encoder_create(XVID_ENC_PARAM * pParam) |
|
|
{ |
|
|
Encoder *pEnc; |
|
|
int i; |
|
|
|
|
|
pParam->handle = NULL; |
|
|
|
|
|
ENC_CHECK(pParam); |
|
|
|
|
|
ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
|
|
ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); |
|
|
ENC_CHECK(!(pParam->width % 2)); |
|
|
ENC_CHECK(!(pParam->height % 2)); |
|
|
|
|
|
/* Fps */ |
|
|
|
|
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
|
|
pParam->fincr = 1; |
|
|
pParam->fbase = 25; |
|
|
} |
|
|
|
|
85 |
/* |
/* |
86 |
* Simplify the "fincr/fbase" fraction |
* Simplify the "fincr/fbase" fraction |
|
* (neccessary, since windows supplies us with huge numbers) |
|
87 |
*/ |
*/ |
88 |
|
static int |
89 |
|
gcd(int a, int b) |
90 |
|
{ |
91 |
|
int r ; |
92 |
|
|
93 |
i = pParam->fincr; |
if (b > a) { |
94 |
while (i > 1) { |
r = a; |
95 |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
a = b; |
96 |
pParam->fincr /= i; |
b = r; |
|
pParam->fbase /= i; |
|
|
i = pParam->fincr; |
|
|
continue; |
|
97 |
} |
} |
|
i--; |
|
|
} |
|
|
|
|
|
if (pParam->fbase > 65535) { |
|
|
float div = (float) pParam->fbase / 65535; |
|
98 |
|
|
99 |
pParam->fbase = (int) (pParam->fbase / div); |
while ((r = a % b)) { |
100 |
pParam->fincr = (int) (pParam->fincr / div); |
a = b; |
101 |
|
b = r; |
102 |
|
} |
103 |
|
return b; |
104 |
} |
} |
105 |
|
|
106 |
/* Bitrate allocator defaults */ |
static void |
107 |
|
simplify_time(int *inc, int *base) |
108 |
if (pParam->rc_bitrate <= 0) |
{ |
109 |
pParam->rc_bitrate = 900000; |
/* common factor */ |
110 |
|
const int s = gcd(*inc, *base); |
111 |
|
*inc /= s; |
112 |
|
*base /= s; |
113 |
|
|
114 |
if (pParam->rc_reaction_delay_factor <= 0) |
if (*base > 65535 || *inc > 65535) { |
115 |
pParam->rc_reaction_delay_factor = 16; |
int *biggest; |
116 |
|
int *other; |
117 |
|
float div; |
118 |
|
|
119 |
if (pParam->rc_averaging_period <= 0) |
if (*base > *inc) { |
120 |
pParam->rc_averaging_period = 100; |
biggest = base; |
121 |
|
other = inc; |
122 |
|
} else { |
123 |
|
biggest = inc; |
124 |
|
other = base; |
125 |
|
} |
126 |
|
|
127 |
if (pParam->rc_buffer <= 0) |
div = ((float)*biggest)/((float)65535); |
128 |
pParam->rc_buffer = 100; |
*biggest = (unsigned int)(((float)*biggest)/div); |
129 |
|
*other = (unsigned int)(((float)*other)/div); |
130 |
|
} |
131 |
|
} |
132 |
|
|
|
/* Max and min quantizers */ |
|
133 |
|
|
134 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
int |
135 |
pParam->min_quantizer = 1; |
enc_create(xvid_enc_create_t * create) |
136 |
|
{ |
137 |
|
Encoder *pEnc; |
138 |
|
int n; |
139 |
|
|
140 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */ |
141 |
pParam->max_quantizer = 31; |
return XVID_ERR_VERSION; |
142 |
|
|
143 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (create->width%2 || create->height%2) |
144 |
pParam->max_quantizer = pParam->min_quantizer; |
return XVID_ERR_FAIL; |
145 |
|
|
146 |
/* 1 keyframe each 10 seconds */ |
if (create->width<=0 || create->height<=0) |
147 |
|
return XVID_ERR_FAIL; |
148 |
|
|
149 |
if (pParam->max_key_interval <= 0) |
/* allocate encoder struct */ |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
150 |
|
|
151 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
152 |
if (pEnc == NULL) |
if (pEnc == NULL) |
153 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
|
|
|
|
/* Zero the Encoder Structure */ |
|
|
|
|
154 |
memset(pEnc, 0, sizeof(Encoder)); |
memset(pEnc, 0, sizeof(Encoder)); |
155 |
|
|
156 |
/* Fill members of Encoder structure */ |
pEnc->mbParam.profile = create->profile; |
|
|
|
|
pEnc->mbParam.width = pParam->width; |
|
|
pEnc->mbParam.height = pParam->height; |
|
157 |
|
|
158 |
|
/* global flags */ |
159 |
|
pEnc->mbParam.global_flags = create->global; |
160 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED)) |
161 |
|
pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA; |
162 |
|
|
163 |
|
/* width, height */ |
164 |
|
pEnc->mbParam.width = create->width; |
165 |
|
pEnc->mbParam.height = create->height; |
166 |
pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; |
pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; |
167 |
pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; |
pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; |
|
|
|
168 |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
169 |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
170 |
|
|
171 |
pEnc->mbParam.fbase = pParam->fbase; |
/* framerate */ |
172 |
pEnc->mbParam.fincr = pParam->fincr; |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
173 |
|
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
174 |
|
if (pEnc->mbParam.fincr>0) |
175 |
|
simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase); |
176 |
|
|
177 |
|
/* zones */ |
178 |
|
if(create->num_zones > 0) { |
179 |
|
pEnc->num_zones = create->num_zones; |
180 |
|
pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE); |
181 |
|
if (pEnc->zones == NULL) |
182 |
|
goto xvid_err_memory0; |
183 |
|
memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones); |
184 |
|
} else { |
185 |
|
pEnc->num_zones = 0; |
186 |
|
pEnc->zones = NULL; |
187 |
|
} |
188 |
|
|
189 |
|
/* plugins */ |
190 |
|
if(create->num_plugins > 0) { |
191 |
|
pEnc->num_plugins = create->num_plugins; |
192 |
|
pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); |
193 |
|
if (pEnc->plugins == NULL) |
194 |
|
goto xvid_err_memory0; |
195 |
|
} else { |
196 |
|
pEnc->num_plugins = 0; |
197 |
|
pEnc->plugins = NULL; |
198 |
|
} |
199 |
|
|
200 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
for (n=0; n<pEnc->num_plugins;n++) { |
201 |
|
xvid_plg_create_t pcreate; |
202 |
|
xvid_plg_info_t pinfo; |
203 |
|
|
204 |
|
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
205 |
|
pinfo.version = XVID_VERSION; |
206 |
|
if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) { |
207 |
|
pEnc->mbParam.plugin_flags |= pinfo.flags; |
208 |
|
} |
209 |
|
|
210 |
pEnc->sStat.fMvPrevSigma = -1; |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
211 |
|
pcreate.version = XVID_VERSION; |
212 |
|
pcreate.num_zones = pEnc->num_zones; |
213 |
|
pcreate.zones = pEnc->zones; |
214 |
|
pcreate.width = pEnc->mbParam.width; |
215 |
|
pcreate.height = pEnc->mbParam.height; |
216 |
|
pcreate.mb_width = pEnc->mbParam.mb_width; |
217 |
|
pcreate.mb_height = pEnc->mbParam.mb_height; |
218 |
|
pcreate.fincr = pEnc->mbParam.fincr; |
219 |
|
pcreate.fbase = pEnc->mbParam.fbase; |
220 |
|
pcreate.param = create->plugins[n].param; |
221 |
|
|
222 |
/* Fill rate control parameters */ |
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
223 |
|
if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
224 |
|
pEnc->plugins[n].func = create->plugins[n].func; |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
pEnc->bitrate = pParam->rc_bitrate; |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) || |
229 |
|
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
230 |
|
pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */ |
231 |
|
} |
232 |
|
|
233 |
pEnc->iFrameNum = 0; |
/* temp dquants */ |
234 |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
235 |
|
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
236 |
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
237 |
|
if (pEnc->temp_dquants==NULL) |
238 |
|
goto xvid_err_memory1a; |
239 |
|
} |
240 |
|
|
241 |
|
/* temp lambdas */ |
242 |
|
if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
243 |
|
pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width * |
244 |
|
pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE); |
245 |
|
if (pEnc->temp_lambda == NULL) |
246 |
|
goto xvid_err_memory1a; |
247 |
|
} |
248 |
|
|
249 |
|
/* bframes */ |
250 |
|
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
251 |
|
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
252 |
|
pEnc->mbParam.bquant_offset = create->bquant_offset; |
253 |
|
|
254 |
|
/* min/max quant */ |
255 |
|
for (n=0; n<3; n++) { |
256 |
|
pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2; |
257 |
|
pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31; |
258 |
|
} |
259 |
|
|
260 |
|
/* frame drop ratio */ |
261 |
|
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
262 |
|
|
263 |
|
/* max keyframe interval */ |
264 |
|
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval; |
265 |
|
|
266 |
/* try to allocate frame memory */ |
/* allocate working frame-image memory */ |
267 |
|
|
268 |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
269 |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
271 |
if (pEnc->current == NULL || pEnc->reference == NULL) |
if (pEnc->current == NULL || pEnc->reference == NULL) |
272 |
goto xvid_err_memory1; |
goto xvid_err_memory1; |
273 |
|
|
274 |
/* try to allocate mb memory */ |
/* allocate macroblock memory */ |
275 |
|
|
276 |
pEnc->current->mbs = |
pEnc->current->mbs = |
277 |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
283 |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
284 |
goto xvid_err_memory2; |
goto xvid_err_memory2; |
285 |
|
|
286 |
/* try to allocate image memory */ |
/* allocate quant matrix memory */ |
287 |
|
|
288 |
#ifdef _DEBUG_PSNR |
pEnc->mbParam.mpeg_quant_matrices = |
289 |
|
xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
290 |
|
|
291 |
|
if (pEnc->mbParam.mpeg_quant_matrices == NULL) |
292 |
|
goto xvid_err_memory2a; |
293 |
|
|
294 |
|
/* allocate interpolation image memory */ |
295 |
|
|
296 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
297 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
298 |
#endif |
image_null(&pEnc->sOriginal2); |
299 |
|
} |
300 |
|
|
301 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
302 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
306 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
307 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
308 |
image_null(&pEnc->vInterV); |
image_null(&pEnc->vInterV); |
|
image_null(&pEnc->vInterVf); |
|
309 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
|
image_null(&pEnc->vInterHVf); |
|
310 |
|
|
311 |
#ifdef _DEBUG_PSNR |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
312 |
if (image_create |
if (image_create |
313 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
314 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
315 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
316 |
#endif |
|
317 |
|
if (image_create |
318 |
|
(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
319 |
|
pEnc->mbParam.edged_height) < 0) |
320 |
|
goto xvid_err_memory3; |
321 |
|
} |
322 |
|
|
323 |
if (image_create |
if (image_create |
324 |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
350 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
351 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
352 |
if (image_create |
if (image_create |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
|
if (image_create |
|
353 |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
354 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
355 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
356 |
|
|
357 |
|
/* Create full bitplane for GMC, this might be wasteful */ |
358 |
if (image_create |
if (image_create |
359 |
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
(&pEnc->vGMC, pEnc->mbParam.edged_width, |
360 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
361 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
362 |
|
|
363 |
|
/* init bframe image buffers */ |
364 |
|
|
365 |
|
pEnc->bframenum_head = 0; |
366 |
|
pEnc->bframenum_tail = 0; |
367 |
|
pEnc->flush_bframes = 0; |
368 |
|
pEnc->closed_bframenum = -1; |
369 |
|
|
370 |
/* B Frames specific init */ |
/* B Frames specific init */ |
|
|
|
|
pEnc->global = pParam->global; |
|
|
pEnc->mbParam.max_bframes = pParam->max_bframes; |
|
|
pEnc->bquant_ratio = pParam->bquant_ratio; |
|
|
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
|
371 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
372 |
|
|
373 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
|
int n; |
|
374 |
|
|
375 |
pEnc->bframes = |
pEnc->bframes = |
376 |
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
406 |
} |
} |
407 |
} |
} |
408 |
|
|
409 |
pEnc->bframenum_head = 0; |
/* init incoming frame queue */ |
410 |
pEnc->bframenum_tail = 0; |
pEnc->queue_head = 0; |
411 |
pEnc->flush_bframes = 0; |
pEnc->queue_tail = 0; |
412 |
pEnc->bframenum_dx50bvop = -1; |
pEnc->queue_size = 0; |
|
|
|
|
pEnc->queue = NULL; |
|
|
|
|
|
|
|
|
if (pEnc->mbParam.max_bframes > 0) { |
|
|
int n; |
|
413 |
|
|
414 |
pEnc->queue = |
pEnc->queue = |
415 |
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO), |
416 |
CACHE_LINE); |
CACHE_LINE); |
417 |
|
|
418 |
if (pEnc->queue == NULL) |
if (pEnc->queue == NULL) |
419 |
goto xvid_err_memory4; |
goto xvid_err_memory4; |
420 |
|
|
421 |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
422 |
image_null(&pEnc->queue[n]); |
image_null(&pEnc->queue[n].image); |
423 |
|
|
424 |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
|
425 |
|
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
426 |
if (image_create |
if (image_create |
427 |
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
428 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
429 |
goto xvid_err_memory5; |
goto xvid_err_memory5; |
|
|
|
|
} |
|
430 |
} |
} |
431 |
|
|
432 |
pEnc->queue_head = 0; |
/* timestamp stuff */ |
|
pEnc->queue_tail = 0; |
|
|
pEnc->queue_size = 0; |
|
433 |
|
|
434 |
pEnc->mbParam.m_stamp = 0; |
pEnc->mbParam.m_stamp = 0; |
|
|
|
435 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
436 |
pEnc->current->stamp = 0; |
pEnc->current->stamp = 0; |
437 |
pEnc->reference->stamp = 0; |
pEnc->reference->stamp = 0; |
438 |
|
|
439 |
pParam->handle = (void *) pEnc; |
/* other stuff */ |
440 |
|
|
441 |
if (pParam->rc_bitrate) { |
pEnc->iFrameNum = 0; |
442 |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
pEnc->fMvPrevSigma = -1; |
443 |
pParam->rc_reaction_delay_factor, |
|
444 |
pParam->rc_averaging_period, pParam->rc_buffer, |
create->handle = (void *) pEnc; |
|
pParam->fbase * 1000 / pParam->fincr, |
|
|
pParam->max_quantizer, pParam->min_quantizer); |
|
|
} |
|
445 |
|
|
446 |
init_timer(); |
init_timer(); |
447 |
|
init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices); |
448 |
|
|
449 |
return XVID_ERR_OK; |
return 0; /* ok */ |
450 |
|
|
451 |
/* |
/* |
452 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
454 |
|
|
455 |
xvid_err_memory5: |
xvid_err_memory5: |
456 |
|
|
457 |
|
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
458 |
if (pEnc->mbParam.max_bframes > 0) { |
image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
|
|
|
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
|
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
|
459 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
460 |
} |
} |
461 |
|
|
462 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
|
} |
|
463 |
|
|
464 |
xvid_err_memory4: |
xvid_err_memory4: |
465 |
|
|
466 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
467 |
|
int i; |
468 |
|
|
469 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
470 |
|
|
473 |
|
|
474 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
475 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
476 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
477 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
478 |
} |
} |
479 |
|
|
480 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
481 |
} |
} |
482 |
|
|
483 |
xvid_err_memory3: |
xvid_err_memory3: |
484 |
#ifdef _DEBUG_PSNR |
|
485 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
486 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
487 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
488 |
#endif |
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
489 |
|
pEnc->mbParam.edged_height); |
490 |
|
} |
491 |
|
|
492 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
493 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
504 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
505 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
506 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
507 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
508 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
509 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
510 |
|
/* destroy GMC image */ |
511 |
|
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
512 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
513 |
|
|
514 |
|
xvid_err_memory2a: |
515 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
516 |
|
|
517 |
xvid_err_memory2: |
xvid_err_memory2: |
518 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
519 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
521 |
xvid_err_memory1: |
xvid_err_memory1: |
522 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
523 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
524 |
|
|
525 |
|
xvid_err_memory1a: |
526 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
527 |
|
xvid_free(pEnc->temp_dquants); |
528 |
|
} |
529 |
|
|
530 |
|
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
531 |
|
xvid_free(pEnc->temp_lambda); |
532 |
|
} |
533 |
|
|
534 |
|
xvid_err_memory0: |
535 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
536 |
|
if (pEnc->plugins[n].func) { |
537 |
|
pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL); |
538 |
|
} |
539 |
|
} |
540 |
|
xvid_free(pEnc->plugins); |
541 |
|
|
542 |
|
xvid_free(pEnc->zones); |
543 |
|
|
544 |
xvid_free(pEnc); |
xvid_free(pEnc); |
545 |
|
|
546 |
pParam->handle = NULL; |
create->handle = NULL; |
547 |
|
|
548 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
549 |
} |
} |
552 |
* Encoder destruction |
* Encoder destruction |
553 |
* |
* |
554 |
* This function destroy the entire encoder structure created by a previous |
* This function destroy the entire encoder structure created by a previous |
555 |
* successful encoder_create call. |
* successful enc_create call. |
556 |
* |
* |
557 |
* Returned values (for now only one returned value) : |
* Returned values (for now only one returned value) : |
558 |
* - XVID_ERR_OK - no errors |
* - 0 - no errors |
559 |
* |
* |
560 |
****************************************************************************/ |
****************************************************************************/ |
561 |
|
|
562 |
int |
int |
563 |
encoder_destroy(Encoder * pEnc) |
enc_destroy(Encoder * pEnc) |
564 |
{ |
{ |
565 |
int i; |
int i; |
566 |
|
|
|
ENC_CHECK(pEnc); |
|
|
|
|
567 |
/* B Frames specific */ |
/* B Frames specific */ |
568 |
if (pEnc->mbParam.max_bframes > 0) { |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
569 |
|
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
|
|
|
|
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
|
570 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
571 |
} |
} |
|
xvid_free(pEnc->queue); |
|
|
} |
|
572 |
|
|
573 |
|
xvid_free(pEnc->queue); |
574 |
|
|
575 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
576 |
|
|
581 |
|
|
582 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
583 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
584 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
585 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
586 |
} |
} |
587 |
|
|
599 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
600 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
601 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
602 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
603 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
|
|
|
604 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
605 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
606 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
607 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
608 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
609 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
610 |
|
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
611 |
|
pEnc->mbParam.edged_height); |
612 |
|
|
613 |
#ifdef _DEBUG_PSNR |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
614 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
615 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
616 |
#endif |
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
617 |
|
pEnc->mbParam.edged_height); |
618 |
|
} |
619 |
|
|
620 |
/* Encoder structure */ |
/* Encoder structure */ |
621 |
|
|
625 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
626 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
627 |
|
|
628 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
629 |
|
xvid_free(pEnc->temp_dquants); |
630 |
|
} |
631 |
|
|
632 |
|
|
633 |
|
if (pEnc->num_plugins>0) { |
634 |
|
xvid_plg_destroy_t pdestroy; |
635 |
|
memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t)); |
636 |
|
|
637 |
|
pdestroy.version = XVID_VERSION; |
638 |
|
pdestroy.num_frames = pEnc->m_framenum; |
639 |
|
|
640 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
641 |
|
if (pEnc->plugins[i].func) { |
642 |
|
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL); |
643 |
|
} |
644 |
|
} |
645 |
|
xvid_free(pEnc->plugins); |
646 |
|
} |
647 |
|
|
648 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
649 |
|
|
650 |
|
if (pEnc->num_plugins>0) |
651 |
|
xvid_free(pEnc->zones); |
652 |
|
|
653 |
xvid_free(pEnc); |
xvid_free(pEnc); |
654 |
|
|
655 |
return XVID_ERR_OK; |
return 0; /* ok */ |
656 |
|
} |
657 |
|
|
658 |
|
|
659 |
|
/* |
660 |
|
call the plugins |
661 |
|
*/ |
662 |
|
|
663 |
|
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
664 |
|
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
665 |
|
{ |
666 |
|
unsigned int i, j, k; |
667 |
|
xvid_plg_data_t data; |
668 |
|
|
669 |
|
/* set data struct */ |
670 |
|
|
671 |
|
memset(&data, 0, sizeof(xvid_plg_data_t)); |
672 |
|
data.version = XVID_VERSION; |
673 |
|
|
674 |
|
/* find zone */ |
675 |
|
for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ; |
676 |
|
data.zone = i>0 ? &pEnc->zones[i-1] : NULL; |
677 |
|
|
678 |
|
data.width = pEnc->mbParam.width; |
679 |
|
data.height = pEnc->mbParam.height; |
680 |
|
data.mb_width = pEnc->mbParam.mb_width; |
681 |
|
data.mb_height = pEnc->mbParam.mb_height; |
682 |
|
data.fincr = frame->fincr; |
683 |
|
data.fbase = pEnc->mbParam.fbase; |
684 |
|
data.bquant_ratio = pEnc->mbParam.bquant_ratio; |
685 |
|
data.bquant_offset = pEnc->mbParam.bquant_offset; |
686 |
|
|
687 |
|
for (i=0; i<3; i++) { |
688 |
|
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
689 |
|
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
690 |
|
} |
691 |
|
|
692 |
|
data.reference.csp = XVID_CSP_PLANAR; |
693 |
|
data.reference.plane[0] = pEnc->reference->image.y; |
694 |
|
data.reference.plane[1] = pEnc->reference->image.u; |
695 |
|
data.reference.plane[2] = pEnc->reference->image.v; |
696 |
|
data.reference.stride[0] = pEnc->mbParam.edged_width; |
697 |
|
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
698 |
|
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
699 |
|
|
700 |
|
data.current.csp = XVID_CSP_PLANAR; |
701 |
|
data.current.plane[0] = frame->image.y; |
702 |
|
data.current.plane[1] = frame->image.u; |
703 |
|
data.current.plane[2] = frame->image.v; |
704 |
|
data.current.stride[0] = pEnc->mbParam.edged_width; |
705 |
|
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
706 |
|
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
707 |
|
|
708 |
|
data.frame_num = frame->frame_num; |
709 |
|
|
710 |
|
if (opt == XVID_PLG_BEFORE) { |
711 |
|
data.type = *type; |
712 |
|
data.quant = *quant; |
713 |
|
|
714 |
|
data.vol_flags = frame->vol_flags; |
715 |
|
data.vop_flags = frame->vop_flags; |
716 |
|
data.motion_flags = frame->motion_flags; |
717 |
|
|
718 |
|
} else if (opt == XVID_PLG_FRAME) { |
719 |
|
data.type = coding2type(frame->coding_type); |
720 |
|
data.quant = frame->quant; |
721 |
|
|
722 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
723 |
|
data.dquant = pEnc->temp_dquants; |
724 |
|
data.dquant_stride = pEnc->mbParam.mb_width; |
725 |
|
memset(data.dquant, 0, data.mb_width*data.mb_height); |
726 |
|
} |
727 |
|
|
728 |
|
if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
729 |
|
int block = 0; |
730 |
|
data.lambda = pEnc->temp_lambda; |
731 |
|
for(i = 0;i < pEnc->mbParam.mb_height; i++) |
732 |
|
for(j = 0;j < pEnc->mbParam.mb_width; j++) |
733 |
|
for (k = 0; k < 6; k++) |
734 |
|
data.lambda[block++] = 1.0f; |
735 |
|
} |
736 |
|
|
737 |
|
} else { /* XVID_PLG_AFTER */ |
738 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
739 |
|
data.original.csp = XVID_CSP_PLANAR; |
740 |
|
data.original.plane[0] = original->y; |
741 |
|
data.original.plane[1] = original->u; |
742 |
|
data.original.plane[2] = original->v; |
743 |
|
data.original.stride[0] = pEnc->mbParam.edged_width; |
744 |
|
data.original.stride[1] = pEnc->mbParam.edged_width/2; |
745 |
|
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
746 |
|
} |
747 |
|
|
748 |
|
if ((frame->vol_flags & XVID_VOL_EXTRASTATS) || |
749 |
|
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
750 |
|
|
751 |
|
data.sse_y = |
752 |
|
plane_sse( original->y, frame->image.y, |
753 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
754 |
|
pEnc->mbParam.height); |
755 |
|
|
756 |
|
data.sse_u = |
757 |
|
plane_sse( original->u, frame->image.u, |
758 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
759 |
|
pEnc->mbParam.height/2); |
760 |
|
|
761 |
|
data.sse_v = |
762 |
|
plane_sse( original->v, frame->image.v, |
763 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
764 |
|
pEnc->mbParam.height/2); |
765 |
|
} |
766 |
|
|
767 |
|
data.type = coding2type(frame->coding_type); |
768 |
|
data.quant = frame->quant; |
769 |
|
|
770 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
771 |
|
data.dquant = pEnc->temp_dquants; |
772 |
|
data.dquant_stride = pEnc->mbParam.mb_width; |
773 |
|
|
774 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
775 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
776 |
|
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant; |
777 |
|
} |
778 |
|
} |
779 |
|
|
780 |
|
data.vol_flags = frame->vol_flags; |
781 |
|
data.vop_flags = frame->vop_flags; |
782 |
|
data.motion_flags = frame->motion_flags; |
783 |
|
|
784 |
|
data.length = frame->length; |
785 |
|
data.kblks = frame->sStat.kblks; |
786 |
|
data.mblks = frame->sStat.mblks; |
787 |
|
data.ublks = frame->sStat.ublks; |
788 |
|
|
789 |
|
/* New code */ |
790 |
|
data.stats.type = coding2type(frame->coding_type); |
791 |
|
data.stats.quant = frame->quant; |
792 |
|
data.stats.vol_flags = frame->vol_flags; |
793 |
|
data.stats.vop_flags = frame->vop_flags; |
794 |
|
data.stats.length = frame->length; |
795 |
|
data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8); |
796 |
|
data.stats.kblks = frame->sStat.kblks; |
797 |
|
data.stats.mblks = frame->sStat.mblks; |
798 |
|
data.stats.ublks = frame->sStat.ublks; |
799 |
|
data.stats.sse_y = data.sse_y; |
800 |
|
data.stats.sse_u = data.sse_u; |
801 |
|
data.stats.sse_v = data.sse_v; |
802 |
|
|
803 |
|
if (stats) |
804 |
|
*stats = data.stats; |
805 |
|
} |
806 |
|
|
807 |
|
/* call plugins */ |
808 |
|
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
809 |
|
emms(); |
810 |
|
if (pEnc->plugins[i].func) { |
811 |
|
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) { |
812 |
|
continue; |
813 |
|
} |
814 |
|
} |
815 |
|
} |
816 |
|
emms(); |
817 |
|
|
818 |
|
/* copy modified values back into frame*/ |
819 |
|
if (opt == XVID_PLG_BEFORE) { |
820 |
|
*type = data.type; |
821 |
|
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
822 |
|
|
823 |
|
frame->vol_flags = data.vol_flags; |
824 |
|
frame->vop_flags = data.vop_flags; |
825 |
|
frame->motion_flags = data.motion_flags; |
826 |
|
|
827 |
|
} else if (opt == XVID_PLG_FRAME) { |
828 |
|
|
829 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
830 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
831 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
832 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i]; |
833 |
|
} |
834 |
|
} else { |
835 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
836 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
837 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
838 |
|
} |
839 |
|
} |
840 |
|
|
841 |
|
if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
842 |
|
for (j = 0; j < pEnc->mbParam.mb_height; j++) |
843 |
|
for (i = 0; i < pEnc->mbParam.mb_width; i++) |
844 |
|
for (k = 0; k < 6; k++) { |
845 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = |
846 |
|
(int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]); |
847 |
|
} |
848 |
|
} else { |
849 |
|
for (j = 0; j<pEnc->mbParam.mb_height; j++) |
850 |
|
for (i = 0; i<pEnc->mbParam.mb_width; i++) |
851 |
|
for (k = 0; k < 6; k++) { |
852 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP; |
853 |
|
} |
854 |
|
} |
855 |
|
|
856 |
|
|
857 |
|
frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
858 |
|
} |
859 |
|
|
860 |
|
|
861 |
} |
} |
862 |
|
|
863 |
|
|
864 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
865 |
{ |
{ |
866 |
pEnc->current->stamp = pEnc->mbParam.m_stamp; // first frame is zero |
pEnc->current->frame_num = pEnc->m_framenum; |
867 |
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
868 |
|
|
869 |
|
pEnc->mbParam.m_stamp += pEnc->current->fincr; |
870 |
|
pEnc->m_framenum++; /* debug ticker */ |
871 |
} |
} |
872 |
|
|
873 |
|
static __inline void dec_frame_num(Encoder * pEnc) |
874 |
|
{ |
875 |
|
pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr; |
876 |
|
pEnc->m_framenum--; /* debug ticker */ |
877 |
|
} |
878 |
|
|
879 |
static __inline void |
static __inline void |
880 |
queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam) |
881 |
{ |
{ |
882 |
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
if (pMB->cbp == 0) { |
883 |
{ |
/* we want to code dquant but the quantizer value will not be used yet |
884 |
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
let's find out if we can postpone dquant to next MB |
885 |
|
*/ |
886 |
|
if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) { |
887 |
|
pMB->dquant = 0; /* it's the last MB of all, the easiest case */ |
888 |
|
return; |
889 |
|
} else { |
890 |
|
MACROBLOCK * next = pMB + 1; |
891 |
|
const MACROBLOCK * prev = pMB - 1; |
892 |
|
if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED) |
893 |
|
/* mode allows dquant change in the future */ |
894 |
|
if (abs(next->quant - prev->quant) <= 2) { |
895 |
|
/* quant change is not out of range */ |
896 |
|
pMB->quant = prev->quant; |
897 |
|
pMB->dquant = 0; |
898 |
|
next->dquant = next->quant - prev->quant; |
899 |
return; |
return; |
900 |
} |
} |
901 |
|
} |
902 |
|
} |
903 |
|
/* couldn't skip this dquant */ |
904 |
|
pMB->mode = MODE_INTER_Q; |
905 |
|
} |
906 |
|
|
|
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
|
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
|
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
|
|
|
|
|
|
|
start_timer(); |
|
|
if (image_input |
|
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
|
|
return; |
|
|
stop_conv_timer(); |
|
907 |
|
|
|
pEnc->queue_size++; |
|
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
|
|
} |
|
908 |
|
|
909 |
static __inline void |
static __inline void |
910 |
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
913 |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
914 |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
915 |
|
|
916 |
//HEAVY DEBUG OUTPUT remove when timecodes prove to be stable |
#if 0 /* HEAVY DEBUG OUTPUT */ |
917 |
|
fprintf(stderr,"WriteVop: %d - %d \n", |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
|
918 |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
919 |
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
920 |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
921 |
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
922 |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
923 |
|
#endif |
|
*/ |
|
924 |
} |
} |
925 |
|
|
926 |
|
static void |
927 |
|
simplify_par(int *par_width, int *par_height) |
928 |
|
{ |
929 |
|
|
930 |
|
int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width; |
931 |
|
int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height; |
932 |
|
int divisor = gcd(_par_width, _par_height); |
933 |
|
|
934 |
|
_par_width /= divisor; |
935 |
|
_par_height /= divisor; |
936 |
|
|
937 |
|
/* 2^8 precision maximum */ |
938 |
|
if (_par_width>255 || _par_height>255) { |
939 |
|
float div; |
940 |
|
emms(); |
941 |
|
if (_par_width>_par_height) |
942 |
|
div = (float)_par_width/255; |
943 |
|
else |
944 |
|
div = (float)_par_height/255; |
945 |
|
|
946 |
|
_par_width = (int)((float)_par_width/div); |
947 |
|
_par_height = (int)((float)_par_height/div); |
948 |
|
} |
949 |
|
|
950 |
|
*par_width = _par_width; |
951 |
|
*par_height = _par_height; |
952 |
|
|
953 |
|
return; |
954 |
|
} |
955 |
|
|
956 |
|
|
957 |
/***************************************************************************** |
/***************************************************************************** |
958 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
959 |
* |
* |
960 |
* Returned values : |
* Returned values : |
961 |
* - XVID_ERR_OK - no errors |
* - >0 - output bytes |
962 |
|
* - 0 - no output |
963 |
|
* - XVID_ERR_VERSION - wrong version passed to core |
964 |
|
* - XVID_ERR_END - End of stream reached before end of coding |
965 |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
966 |
* format |
* format |
967 |
****************************************************************************/ |
****************************************************************************/ |
968 |
|
|
969 |
|
|
970 |
int |
int |
971 |
encoder_encode_bframes(Encoder * pEnc, |
enc_encode(Encoder * pEnc, |
972 |
XVID_ENC_FRAME * pFrame, |
xvid_enc_frame_t * xFrame, |
973 |
XVID_ENC_STATS * pResult) |
xvid_enc_stats_t * stats) |
974 |
{ |
{ |
975 |
uint16_t x, y; |
xvid_enc_frame_t * frame; |
976 |
|
int type; |
977 |
Bitstream bs; |
Bitstream bs; |
|
uint32_t bits, mode; |
|
978 |
|
|
979 |
int input_valid = 1; |
if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */ |
980 |
|
return XVID_ERR_VERSION; |
981 |
|
|
982 |
#ifdef _DEBUG_PSNR |
xFrame->out_flags = 0; |
|
float psnr; |
|
|
char temp[128]; |
|
|
#endif |
|
|
|
|
|
ENC_CHECK(pEnc); |
|
|
ENC_CHECK(pFrame); |
|
|
ENC_CHECK(pFrame->image); |
|
983 |
|
|
984 |
start_global_timer(); |
start_global_timer(); |
985 |
|
BitstreamInit(&bs, xFrame->bitstream, 0); |
986 |
|
|
|
BitstreamInit(&bs, pFrame->bitstream, 0); |
|
987 |
|
|
988 |
ipvop_loop: |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
989 |
|
* enqueue image to the encoding-queue |
990 |
/* |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
* bframe "flush" code |
|
|
*/ |
|
991 |
|
|
992 |
if ((pFrame->image == NULL || pEnc->flush_bframes) |
if (xFrame->input.csp != XVID_CSP_NULL) |
993 |
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
{ |
994 |
|
QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail]; |
995 |
|
|
996 |
if (pEnc->flush_bframes == 0) { |
start_timer(); |
997 |
/* |
if (image_input |
998 |
* we have reached the end of stream without getting |
(&q->image, pEnc->mbParam.width, pEnc->mbParam.height, |
999 |
* a future reference frame... so encode last final |
pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, |
1000 |
* frame as a pframe |
xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING)) |
1001 |
*/ |
{ |
1002 |
|
emms(); |
1003 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
return XVID_ERR_FORMAT; |
1004 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
} |
1005 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
stop_conv_timer(); |
1006 |
|
|
1007 |
pEnc->bframenum_tail--; |
if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
1008 |
SWAP(pEnc->current, pEnc->reference); |
image_chroma_optimize(&q->image, |
1009 |
|
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1010 |
|
} |
1011 |
|
|
1012 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
q->frame = *xFrame; |
1013 |
|
|
1014 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
if (xFrame->quant_intra_matrix) |
1015 |
|
{ |
1016 |
|
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char)); |
1017 |
|
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
1018 |
|
} |
1019 |
|
|
1020 |
BitstreamPad(&bs); |
if (xFrame->quant_inter_matrix) |
1021 |
pFrame->length = BitstreamLength(&bs); |
{ |
1022 |
pFrame->intra = 0; |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char)); |
1023 |
|
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
1024 |
|
} |
1025 |
|
|
1026 |
return XVID_ERR_OK; |
pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1); |
1027 |
|
pEnc->queue_size++; |
1028 |
} |
} |
1029 |
|
|
1030 |
|
|
1031 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1032 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
* bframe flush code |
1033 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1034 |
|
|
1035 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
repeat: |
|
pEnc->bframenum_head++; |
|
1036 |
|
|
1037 |
BitstreamPad(&bs); |
if (pEnc->flush_bframes) |
1038 |
pFrame->length = BitstreamLength(&bs); |
{ |
1039 |
pFrame->intra = 0; |
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
1040 |
|
|
1041 |
if (input_valid) |
DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1042 |
queue_image(pEnc, pFrame); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1043 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1044 |
|
|
1045 |
return XVID_ERR_OK; |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1046 |
|
image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image, |
1047 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1048 |
} |
} |
1049 |
|
|
1050 |
if (pEnc->bframenum_head > 0) { |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
1051 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats); |
1052 |
|
pEnc->bframenum_head++; |
1053 |
|
|
1054 |
|
goto done; |
1055 |
|
} |
1056 |
|
|
1057 |
/* write an empty marker to the bitstream. |
/* write an empty marker to the bitstream. |
1058 |
|
|
1061 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
1062 |
*/ |
*/ |
1063 |
|
|
1064 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) { |
1065 |
int tmp; |
int tmp; |
1066 |
|
int bits; |
1067 |
|
|
1068 |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1069 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1070 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1071 |
|
|
1072 |
BitstreamPad(&bs); |
bits = BitstreamPos(&bs); |
1073 |
|
|
1074 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
1075 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
|
|
pEnc->current->seconds = tmp; |
|
|
|
|
|
pFrame->length = BitstreamLength(&bs); |
|
|
pFrame->intra = 0; |
|
|
|
|
|
if (input_valid) |
|
|
queue_image(pEnc, pFrame); |
|
1076 |
|
|
1077 |
return XVID_ERR_OK; |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant); |
1078 |
} |
BitstreamPad(&bs); |
1079 |
} |
pEnc->current->seconds = tmp; |
|
|
|
|
|
|
|
bvop_loop: |
|
1080 |
|
|
1081 |
if (pEnc->bframenum_dx50bvop != -1) |
/* add the not-coded length to the reference frame size */ |
1082 |
{ |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
1083 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1084 |
|
|
1085 |
SWAP(pEnc->current, pEnc->reference); |
/* flush complete: reset counters */ |
1086 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
pEnc->flush_bframes = 0; |
1087 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
1088 |
|
goto done; |
1089 |
|
|
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
|
1090 |
} |
} |
1091 |
|
|
1092 |
if (input_valid) |
/* flush complete: reset counters */ |
1093 |
{ |
pEnc->flush_bframes = 0; |
1094 |
queue_image(pEnc, pFrame); |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
|
input_valid = 0; |
|
1095 |
} |
} |
1096 |
|
|
1097 |
} else if (input_valid) { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1098 |
|
* dequeue frame from the encoding queue |
1099 |
SWAP(pEnc->current, pEnc->reference); |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
|
|
|
start_timer(); |
|
|
if (image_input |
|
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
|
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
1100 |
|
|
1101 |
// queue input frame, and dequue next image |
if (pEnc->queue_size == 0) /* empty */ |
|
if (pEnc->queue_size > 0) |
|
1102 |
{ |
{ |
1103 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
|
if (pEnc->queue_head != pEnc->queue_tail) |
|
1104 |
{ |
{ |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
|
|
} |
|
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
|
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
|
|
} |
|
1105 |
|
|
1106 |
} else if (pEnc->queue_size > 0) { |
DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
|
|
|
|
SWAP(pEnc->current, pEnc->reference); |
|
|
|
|
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
|
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
|
|
pEnc->queue_size--; |
|
|
|
|
|
} else { |
|
|
|
|
|
/* if nothing was encoded, write an 'ignore this frame' flag |
|
|
to the bitstream */ |
|
|
|
|
|
if (BitstreamPos(&bs) == 0) { |
|
|
|
|
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
|
1107 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1108 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1109 |
|
|
1110 |
BitstreamPutBits(&bs, 0x7f, 8); |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
1111 |
pFrame->intra = 0; |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1112 |
} |
} |
1113 |
|
|
1114 |
pFrame->length = BitstreamLength(&bs); |
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
1115 |
return XVID_ERR_OK; |
if (pEnc->bframenum_tail > 0) { |
|
} |
|
|
|
|
|
pEnc->flush_bframes = 0; |
|
|
|
|
|
emms(); |
|
|
|
|
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
|
|
if (pEnc->bframenum_dx50bvop < 0 ) |
|
|
{ |
|
|
if (pFrame->quant == 0) |
|
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
|
|
else |
|
|
pEnc->current->quant = pFrame->quant; |
|
|
|
|
|
/* if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
|
|
|
|
if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
*/ |
|
|
pEnc->current->global_flags = pFrame->general; |
|
|
pEnc->current->motion_flags = pFrame->motion; |
|
1116 |
|
|
1117 |
/* ToDo : dynamic fcode (in both directions) */ |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1118 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->bframenum_tail--; |
1119 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1120 |
|
|
1121 |
inc_frame_num(pEnc); |
/* convert B-VOP to P-VOP */ |
1122 |
|
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
1123 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1124 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1125 |
|
|
1126 |
#ifdef _DEBUG_PSNR |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1127 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1128 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
#endif |
|
|
|
|
|
emms(); |
|
|
|
|
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
|
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
|
1129 |
} |
} |
1130 |
|
|
1131 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1132 |
* Luminance masking |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1133 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1134 |
|
pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
|
|
int *temp_dquants = |
|
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
|
|
pEnc->mbParam.mb_height * sizeof(int), |
|
|
CACHE_LINE); |
|
|
|
|
|
pEnc->current->quant = |
|
|
adaptive_quantization(pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, temp_dquants, |
|
|
pEnc->current->quant, pEnc->current->quant, |
|
|
2 * pEnc->current->quant, |
|
|
pEnc->mbParam.mb_width, |
|
|
pEnc->mbParam.mb_height); |
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
1135 |
|
|
1136 |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
FrameCodeP(pEnc, &bs); |
1137 |
|
|
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
|
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
|
1138 |
|
|
1139 |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
1140 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1141 |
|
}else{ |
1142 |
|
pEnc->flush_bframes = 1; |
1143 |
|
goto done; |
1144 |
} |
} |
|
|
|
|
#undef OFFSET |
|
1145 |
} |
} |
1146 |
|
DPRINTF(XVID_DEBUG_DEBUG, "*** END\n"); |
1147 |
|
|
1148 |
xvid_free(temp_dquants); |
emms(); |
1149 |
|
return XVID_ERR_END; /* end of stream reached */ |
1150 |
} |
} |
1151 |
|
goto done; /* nothing to encode yet; encoder lag */ |
1152 |
} |
} |
1153 |
|
|
1154 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* the current FRAME becomes the reference */ |
1155 |
* ivop/pvop/bvop selection |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
|
|
|
|
|
|
|
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
|
|
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
|
|
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
|
|
|| /*image_mad(&pEnc->reference->image, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height) > 30) {*/ |
|
|
2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, |
|
|
&pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { |
|
|
|
|
|
/* |
|
|
* This will be coded as an Intra Frame |
|
|
*/ |
|
1156 |
|
|
1157 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
/* remove frame from encoding-queue (head), and move it into the current */ |
1158 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1159 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
frame = &pEnc->queue[pEnc->queue_head].frame; |
1160 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1); |
1161 |
|
pEnc->queue_size--; |
1162 |
|
|
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
|
|
} |
|
1163 |
|
|
1164 |
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1165 |
|
* init pEnc->current fields |
1166 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1167 |
|
|
1168 |
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1169 |
|
inc_frame_num(pEnc); |
1170 |
|
pEnc->current->vol_flags = frame->vol_flags; |
1171 |
|
pEnc->current->vop_flags = frame->vop_flags; |
1172 |
|
pEnc->current->motion_flags = frame->motion; |
1173 |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1174 |
|
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
1175 |
|
|
|
pEnc->bframenum_tail--; |
|
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
|
1176 |
|
|
1177 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
1178 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
image_chroma_optimize(&pEnc->current->image, |
1179 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1180 |
} |
} |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
|
1181 |
|
|
1182 |
pFrame->intra = 0; |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1183 |
|
* frame type & quant selection |
1184 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1185 |
|
|
1186 |
} else { |
type = frame->type; |
1187 |
|
pEnc->current->quant = frame->quant; |
1188 |
|
|
1189 |
FrameCodeI(pEnc, &bs, &bits); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats); |
|
pFrame->intra = 1; |
|
1190 |
|
|
1191 |
pEnc->bframenum_dx50bvop = -1; |
if (type > 0){ /* XVID_TYPE_?VOP */ |
1192 |
|
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1193 |
|
} else{ /* XVID_TYPE_AUTO */ |
1194 |
|
if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){ |
1195 |
|
pEnc->iFrameNum = 0; |
1196 |
|
type = I_VOP; |
1197 |
|
}else{ |
1198 |
|
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1199 |
|
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1200 |
|
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold, |
1201 |
|
(pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL); |
1202 |
} |
} |
|
|
|
|
pEnc->flush_bframes = 1; |
|
|
|
|
|
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
|
|
BitstreamPad(&bs); |
|
|
input_valid = 0; |
|
|
goto ipvop_loop; |
|
1203 |
} |
} |
1204 |
|
|
1205 |
/* |
if (type != I_VOP) |
1206 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */ |
|
* go screwy with divx 5.00 |
|
|
*/ |
|
|
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
|
|
/* |
|
|
* This will be coded as a Predicted Frame |
|
|
*/ |
|
|
|
|
|
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
|
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
|
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
1207 |
|
|
1208 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
/* bframes buffer overflow check */ |
1209 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1210 |
|
type = P_VOP; |
1211 |
} |
} |
1212 |
|
|
1213 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
pEnc->iFrameNum++; |
|
pFrame->intra = 0; |
|
|
pEnc->flush_bframes = 1; |
|
1214 |
|
|
1215 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1216 |
BitstreamPad(&bs); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
1217 |
input_valid = 0; |
"%d st:%lld if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
|
goto ipvop_loop; |
|
1218 |
} |
} |
1219 |
|
|
1220 |
} else { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1221 |
/* |
* encode this frame as a b-vop |
1222 |
* This will be coded as a Bidirectional Frame |
* (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) |
1223 |
*/ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1224 |
|
if (type == B_VOP) { |
1225 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1226 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1227 |
} |
} |
1228 |
|
|
1229 |
if (pFrame->bquant < 1) { |
if (frame->quant < 1) { |
1230 |
pEnc->current->quant = |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1231 |
((pEnc->reference->quant + |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1232 |
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
|
1233 |
} else { |
} else { |
1234 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = frame->quant; |
1235 |
} |
} |
1236 |
|
|
1237 |
if (pEnc->current->quant < 1) |
if (pEnc->current->quant < 1) |
1238 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
1239 |
|
else if (pEnc->current->quant > 31) |
|
if (pEnc->current->quant > 31) |
|
1240 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
1241 |
|
|
1242 |
|
DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
|
1243 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1244 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1245 |
|
|
|
|
|
|
|
|
1246 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1247 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1248 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1249 |
|
|
1250 |
pEnc->bframenum_tail++; |
pEnc->bframenum_tail++; |
1251 |
|
|
1252 |
pFrame->intra = 0; |
goto repeat; |
|
pFrame->length = 0; |
|
|
|
|
|
input_valid = 0; |
|
|
goto bvop_loop; |
|
1253 |
} |
} |
1254 |
|
|
|
pEnc->iFrameNum++; |
|
1255 |
|
|
1256 |
BitstreamPad(&bs); |
DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1257 |
pFrame->length = BitstreamLength(&bs); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1258 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1259 |
|
|
1260 |
if (pResult) { |
/* for unpacked bframes, output the stats for the last encoded frame */ |
1261 |
pResult->quant = pEnc->current->quant; |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) |
1262 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
{ |
1263 |
pResult->kblks = pEnc->sStat.kblks; |
if (pEnc->current->stamp > 0) { |
1264 |
pResult->mblks = pEnc->sStat.mblks; |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1265 |
pResult->ublks = pEnc->sStat.ublks; |
} |
1266 |
|
else |
1267 |
|
stats->type = XVID_TYPE_NOTHING; |
1268 |
} |
} |
1269 |
|
|
1270 |
emms(); |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1271 |
|
* closed-gop |
1272 |
#ifdef _DEBUG_PSNR |
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
1273 |
psnr = |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height); |
|
|
|
|
|
snprintf(temp, 127, "PSNR: %f\n", psnr); |
|
|
DEBUG(temp); |
|
|
#endif |
|
1274 |
|
|
1275 |
if (pFrame->quant == 0) { |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
|
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
|
|
pFrame->length, pFrame->intra); |
|
|
} |
|
1276 |
|
|
1277 |
stop_global_timer(); |
/* place this frame back on the encoding-queue (head) */ |
1278 |
write_timer(); |
/* we will deal with it next time */ |
1279 |
|
dec_frame_num(pEnc); |
1280 |
|
pEnc->iFrameNum--; |
1281 |
|
|
1282 |
return XVID_ERR_OK; |
pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1); |
1283 |
} |
pEnc->queue_size++; |
1284 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1285 |
|
|
1286 |
|
/* grab the last frame from the bframe-queue */ |
1287 |
|
|
1288 |
|
pEnc->bframenum_tail--; |
1289 |
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1290 |
|
|
1291 |
/***************************************************************************** |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1292 |
* "original" IP frame encoder entry point |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP"); |
1293 |
* |
} |
|
* Returned values : |
|
|
* - XVID_ERR_OK - no errors |
|
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
|
|
* format |
|
|
****************************************************************************/ |
|
1294 |
|
|
1295 |
int |
/* convert B-VOP quant to P-VOP */ |
1296 |
encoder_encode(Encoder * pEnc, |
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
1297 |
XVID_ENC_FRAME * pFrame, |
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1298 |
XVID_ENC_STATS * pResult) |
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1299 |
{ |
type = P_VOP; |
1300 |
uint16_t x, y; |
} |
|
Bitstream bs; |
|
|
uint32_t bits; |
|
|
uint16_t write_vol_header = 0; |
|
1301 |
|
|
|
#ifdef _DEBUG_PSNR |
|
|
float psnr; |
|
|
uint8_t temp[128]; |
|
|
#endif |
|
1302 |
|
|
1303 |
start_global_timer(); |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1304 |
|
* encode this frame as an i-vop |
1305 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1306 |
|
|
1307 |
ENC_CHECK(pEnc); |
if (type == I_VOP) { |
|
ENC_CHECK(pFrame); |
|
|
ENC_CHECK(pFrame->bitstream); |
|
|
ENC_CHECK(pFrame->image); |
|
|
|
|
|
SWAP(pEnc->current, pEnc->reference); |
|
|
|
|
|
pEnc->current->global_flags = pFrame->general; |
|
|
pEnc->current->motion_flags = pFrame->motion; |
|
|
pEnc->mbParam.hint = &pFrame->hint; |
|
1308 |
|
|
1309 |
inc_frame_num(pEnc); |
DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1310 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1311 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1312 |
|
|
1313 |
/* disable alternate scan flag if interlacing is not enabled */ |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1314 |
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
|
|
{ |
|
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
|
1315 |
} |
} |
1316 |
|
|
1317 |
start_timer(); |
pEnc->iFrameNum = 1; |
|
if (image_input |
|
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
|
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
1318 |
|
|
1319 |
#ifdef _DEBUG_PSNR |
/* ---- update vol flags at IVOP ----------- */ |
1320 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
pEnc->mbParam.vol_flags = pEnc->current->vol_flags; |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
|
#endif |
|
1321 |
|
|
1322 |
emms(); |
/* Aspect ratio */ |
1323 |
|
switch(frame->par) { |
1324 |
|
case XVID_PAR_11_VGA: |
1325 |
|
case XVID_PAR_43_PAL: |
1326 |
|
case XVID_PAR_43_NTSC: |
1327 |
|
case XVID_PAR_169_PAL: |
1328 |
|
case XVID_PAR_169_NTSC: |
1329 |
|
case XVID_PAR_EXT: |
1330 |
|
pEnc->mbParam.par = frame->par; |
1331 |
|
break; |
1332 |
|
default: |
1333 |
|
pEnc->mbParam.par = XVID_PAR_11_VGA; |
1334 |
|
break; |
1335 |
|
} |
1336 |
|
|
1337 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
/* For extended PAR only, we try to sanityse/simplify par values */ |
1338 |
|
if (pEnc->mbParam.par == XVID_PAR_EXT) { |
1339 |
|
pEnc->mbParam.par_width = frame->par_width; |
1340 |
|
pEnc->mbParam.par_height = frame->par_height; |
1341 |
|
simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height); |
1342 |
|
} |
1343 |
|
|
1344 |
if (pFrame->quant == 0) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1345 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
if (frame->quant_intra_matrix != NULL) |
1346 |
} else { |
set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix); |
1347 |
pEnc->current->quant = pFrame->quant; |
if (frame->quant_inter_matrix != NULL) |
1348 |
|
set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix); |
1349 |
} |
} |
1350 |
|
|
1351 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
/* prevent vol/vop misuse */ |
|
int *temp_dquants = |
|
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
|
|
pEnc->mbParam.mb_height * sizeof(int), |
|
|
CACHE_LINE); |
|
1352 |
|
|
1353 |
pEnc->current->quant = |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
1354 |
adaptive_quantization(pEnc->current->image.y, |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
|
pEnc->mbParam.edged_width, temp_dquants, |
|
|
pEnc->current->quant, pEnc->current->quant, |
|
|
2 * pEnc->current->quant, |
|
|
pEnc->mbParam.mb_width, |
|
|
pEnc->mbParam.mb_height); |
|
1355 |
|
|
1356 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
/* ^^^------------------------ */ |
1357 |
|
|
1358 |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1359 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1360 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1361 |
|
} |
1362 |
|
|
1363 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
FrameCodeI(pEnc, &bs); |
1364 |
|
xFrame->out_flags |= XVID_KEYFRAME; |
1365 |
|
|
1366 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1367 |
|
* encode this frame as an p-vop |
1368 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1369 |
|
|
1370 |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
} else { /* (type == P_VOP || type == S_VOP) */ |
1371 |
|
|
1372 |
|
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1373 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1374 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1375 |
|
|
1376 |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1377 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
1378 |
} |
} |
1379 |
|
|
1380 |
#undef OFFSET |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1381 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1382 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1383 |
} |
} |
1384 |
|
|
1385 |
xvid_free(temp_dquants); |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1386 |
|
/* N-VOP, we mustn't code b-frames yet */ |
1387 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1388 |
|
goto done; |
1389 |
|
} |
1390 |
} |
} |
1391 |
|
|
|
if (pEnc->current->global_flags & XVID_H263QUANT) { |
|
|
if (pEnc->mbParam.m_quant_type != H263_QUANT) |
|
|
write_vol_header = 1; |
|
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
|
|
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
|
|
int matrix1_changed, matrix2_changed; |
|
1392 |
|
|
1393 |
matrix1_changed = matrix2_changed = 0; |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1394 |
|
* on next enc_encode call we must flush bframes |
1395 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1396 |
|
|
1397 |
if (pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
/*done_flush:*/ |
|
write_vol_header = 1; |
|
1398 |
|
|
1399 |
pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
pEnc->flush_bframes = 1; |
1400 |
|
|
1401 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
/* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */ |
1402 |
if (pFrame->quant_intra_matrix != NULL) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
1403 |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
goto repeat; |
|
if (pFrame->quant_inter_matrix != NULL) |
|
|
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
|
|
} else { |
|
|
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
|
|
matrix2_changed = set_inter_matrix(get_default_inter_matrix()); |
|
|
} |
|
|
if (write_vol_header == 0) |
|
|
write_vol_header = matrix1_changed | matrix2_changed; |
|
1404 |
} |
} |
1405 |
|
|
1406 |
if (pFrame->intra < 0) { |
/* packed or no-bframes or no-bframes-queued: output stats */ |
1407 |
if ((pEnc->iFrameNum == 0) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) { |
1408 |
|| ((pEnc->iMaxKeyInterval > 0) |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
|
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
|
|
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
|
|
} else { |
|
|
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
|
|
} |
|
|
} else { |
|
|
if (pFrame->intra == 1) { |
|
|
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
|
|
} else { |
|
|
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
|
1409 |
} |
} |
1410 |
|
|
1411 |
} |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1412 |
|
* done; return number of bytes consumed |
1413 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1414 |
|
|
1415 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
done: |
|
BitstreamPutBits(&bs, 0xFFFF, 16); |
|
|
BitstreamPad(&bs); |
|
|
pFrame->length = BitstreamLength(&bs); |
|
1416 |
|
|
1417 |
if (pResult) { |
stop_global_timer(); |
1418 |
pResult->quant = pEnc->current->quant; |
write_timer(); |
|
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
|
|
pResult->kblks = pEnc->sStat.kblks; |
|
|
pResult->mblks = pEnc->sStat.mblks; |
|
|
pResult->ublks = pEnc->sStat.ublks; |
|
|
} |
|
1419 |
|
|
1420 |
emms(); |
emms(); |
1421 |
|
return BitstreamLength(&bs); |
1422 |
|
} |
1423 |
|
|
|
if (pFrame->quant == 0) { |
|
|
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
|
|
pFrame->length, pFrame->intra); |
|
|
} |
|
|
#ifdef _DEBUG_PSNR |
|
|
psnr = |
|
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height); |
|
|
|
|
|
snprintf(temp, 127, "PSNR: %f\n", psnr); |
|
|
DEBUG(temp); |
|
|
#endif |
|
|
|
|
|
pEnc->iFrameNum++; |
|
|
|
|
|
stop_global_timer(); |
|
|
write_timer(); |
|
1424 |
|
|
1425 |
return XVID_ERR_OK; |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1426 |
|
{ |
1427 |
|
unsigned int i; |
1428 |
|
MACROBLOCK * pMB = frame->mbs; |
1429 |
|
int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */ |
1430 |
|
if (quant > 31) |
1431 |
|
frame->quant = quant = 31; |
1432 |
|
else if (quant < 1) |
1433 |
|
frame->quant = quant = 1; |
1434 |
|
|
1435 |
|
for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) { |
1436 |
|
quant += pMB->dquant; |
1437 |
|
if (quant > 31) |
1438 |
|
quant = 31; |
1439 |
|
else if (quant < 1) |
1440 |
|
quant = 1; |
1441 |
|
pMB->quant = quant; |
1442 |
|
pMB++; |
1443 |
|
} |
1444 |
} |
} |
1445 |
|
|
1446 |
|
|
1457 |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
1458 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1459 |
|
|
1460 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1461 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
|
pEnc->current->quant += DQtab[pMB->dquant]; |
|
|
|
|
|
if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
pMB->quant = pEnc->current->quant; |
|
|
} |
|
|
|
|
|
|
|
|
#define FCODEBITS 3 |
|
|
#define MODEBITS 5 |
|
|
|
|
|
void |
|
|
HintedMESet(Encoder * pEnc, |
|
|
int *intra) |
|
|
{ |
|
|
HINTINFO *hint; |
|
|
Bitstream bs; |
|
|
int length, high; |
|
|
uint32_t x, y; |
|
|
|
|
|
hint = pEnc->mbParam.hint; |
|
|
|
|
|
if (hint->rawhints) { |
|
|
*intra = hint->mvhint.intra; |
|
|
} else { |
|
|
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
|
|
*intra = BitstreamGetBit(&bs); |
|
|
} |
|
|
|
|
|
if (*intra) { |
|
|
return; |
|
|
} |
|
|
|
|
|
pEnc->current->fcode = |
|
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
|
|
FCODEBITS); |
|
|
|
|
|
length = pEnc->current->fcode + 5; |
|
|
high = 1 << (length - 1); |
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
|
|
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
|
|
MACROBLOCK *pMB = |
|
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MVBLOCKHINT *bhint = |
|
|
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
|
|
VECTOR pred; |
|
|
VECTOR tmp; |
|
|
int vec; |
|
|
|
|
|
pMB->mode = |
|
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
|
|
MODEBITS); |
|
|
|
|
|
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
|
|
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
|
|
|
|
|
if (pMB->mode == MODE_INTER) { |
|
|
tmp.x = |
|
|
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
|
|
length); |
|
|
tmp.y = |
|
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
|
|
length); |
|
|
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
|
|
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
|
|
|
|
|
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
|
|
|
|
|
for (vec = 0; vec < 4; ++vec) { |
|
|
pMB->mvs[vec].x = tmp.x; |
|
|
pMB->mvs[vec].y = tmp.y; |
|
|
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
|
|
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
|
|
} |
|
|
} else if (pMB->mode == MODE_INTER4V) { |
|
|
for (vec = 0; vec < 4; ++vec) { |
|
|
tmp.x = |
|
|
(hint->rawhints) ? bhint->mvs[vec]. |
|
|
x : BitstreamGetBits(&bs, length); |
|
|
tmp.y = |
|
|
(hint->rawhints) ? bhint->mvs[vec]. |
|
|
y : BitstreamGetBits(&bs, length); |
|
|
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
|
|
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
|
|
|
|
|
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
|
|
|
|
|
pMB->mvs[vec].x = tmp.x; |
|
|
pMB->mvs[vec].y = tmp.y; |
|
|
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
|
|
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
|
|
} |
|
|
} else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec = 0; vec < 4; ++vec) { |
|
|
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
if (pMB->mode == MODE_INTER4V && |
|
|
(pEnc->current->global_flags & XVID_LUMIMASKING) |
|
|
&& pMB->dquant != NO_CHANGE) { |
|
|
pMB->mode = MODE_INTRA; |
|
|
|
|
|
for (vec = 0; vec < 4; ++vec) { |
|
|
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
|
|
} |
|
|
} |
|
|
} |
|
1462 |
} |
} |
1463 |
} |
} |
1464 |
|
|
1465 |
|
|
|
void |
|
|
HintedMEGet(Encoder * pEnc, |
|
|
int intra) |
|
|
{ |
|
|
HINTINFO *hint; |
|
|
Bitstream bs; |
|
|
uint32_t x, y; |
|
|
int length, high; |
|
|
|
|
|
hint = pEnc->mbParam.hint; |
|
|
|
|
|
if (hint->rawhints) { |
|
|
hint->mvhint.intra = intra; |
|
|
} else { |
|
|
BitstreamInit(&bs, hint->hintstream, 0); |
|
|
BitstreamPutBit(&bs, intra); |
|
|
} |
|
|
|
|
|
if (intra) { |
|
|
if (!hint->rawhints) { |
|
|
BitstreamPad(&bs); |
|
|
hint->hintlength = BitstreamLength(&bs); |
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
length = pEnc->current->fcode + 5; |
|
|
high = 1 << (length - 1); |
|
|
|
|
|
if (hint->rawhints) { |
|
|
hint->mvhint.fcode = pEnc->current->fcode; |
|
|
} else { |
|
|
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
|
|
} |
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
|
|
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
|
|
MACROBLOCK *pMB = |
|
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MVBLOCKHINT *bhint = |
|
|
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
|
|
VECTOR tmp; |
|
|
|
|
|
if (hint->rawhints) { |
|
|
bhint->mode = pMB->mode; |
|
|
} else { |
|
|
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
|
|
} |
|
|
|
|
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
|
tmp.x = pMB->mvs[0].x; |
|
|
tmp.y = pMB->mvs[0].y; |
|
|
tmp.x += (tmp.x < 0) ? high * 2 : 0; |
|
|
tmp.y += (tmp.y < 0) ? high * 2 : 0; |
|
|
|
|
|
if (hint->rawhints) { |
|
|
bhint->mvs[0].x = tmp.x; |
|
|
bhint->mvs[0].y = tmp.y; |
|
|
} else { |
|
|
BitstreamPutBits(&bs, tmp.x, length); |
|
|
BitstreamPutBits(&bs, tmp.y, length); |
|
|
} |
|
|
} else if (pMB->mode == MODE_INTER4V) { |
|
|
int vec; |
|
|
|
|
|
for (vec = 0; vec < 4; ++vec) { |
|
|
tmp.x = pMB->mvs[vec].x; |
|
|
tmp.y = pMB->mvs[vec].y; |
|
|
tmp.x += (tmp.x < 0) ? high * 2 : 0; |
|
|
tmp.y += (tmp.y < 0) ? high * 2 : 0; |
|
|
|
|
|
if (hint->rawhints) { |
|
|
bhint->mvs[vec].x = tmp.x; |
|
|
bhint->mvs[vec].y = tmp.y; |
|
|
} else { |
|
|
BitstreamPutBits(&bs, tmp.x, length); |
|
|
BitstreamPutBits(&bs, tmp.y, length); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (!hint->rawhints) { |
|
|
BitstreamPad(&bs); |
|
|
hint->hintlength = BitstreamLength(&bs); |
|
|
} |
|
|
} |
|
|
|
|
1466 |
|
|
1467 |
static int |
static int |
1468 |
FrameCodeI(Encoder * pEnc, |
FrameCodeI(Encoder * pEnc, |
1469 |
Bitstream * bs, |
Bitstream * bs) |
|
uint32_t * pBits) |
|
1470 |
{ |
{ |
1471 |
|
int bits = BitstreamPos(bs); |
1472 |
|
int mb_width = pEnc->mbParam.mb_width; |
1473 |
|
int mb_height = pEnc->mbParam.mb_height; |
1474 |
|
|
1475 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1476 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1477 |
|
|
1478 |
uint16_t x, y; |
uint16_t x, y; |
1479 |
|
|
|
pEnc->iFrameNum = 0; |
|
1480 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1481 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1482 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1483 |
|
|
1484 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1485 |
|
|
1486 |
#define DIVX501B481P "DivX501b481p" |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1487 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
|
1488 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
|
} |
|
1489 |
|
|
1490 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
|
1491 |
|
|
1492 |
*pBits = BitstreamPos(bs); |
BitstreamPad(bs); |
1493 |
|
|
1494 |
pEnc->sStat.iTextBits = 0; |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
|
pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
|
|
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
|
1495 |
|
|
1496 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
pEnc->current->sStat.iTextBits = 0; |
1497 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1498 |
|
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1499 |
|
|
1500 |
|
for (y = 0; y < mb_height; y++) |
1501 |
|
for (x = 0; x < mb_width; x++) { |
1502 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1503 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1504 |
|
|
1512 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1513 |
|
|
1514 |
start_timer(); |
start_timer(); |
1515 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
|
1516 |
stop_coding_timer(); |
stop_coding_timer(); |
1517 |
} |
} |
1518 |
|
|
1519 |
emms(); |
emms(); |
1520 |
|
|
1521 |
*pBits = BitstreamPos(bs) - *pBits; |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1522 |
pEnc->sStat.fMvPrevSigma = -1; |
|
1523 |
pEnc->sStat.iMvSum = 0; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1524 |
pEnc->sStat.iMvCount = 0; |
|
1525 |
|
pEnc->fMvPrevSigma = -1; |
1526 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1527 |
|
|
1528 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
pEnc->current->is_edged = 0; /* not edged */ |
1529 |
HintedMEGet(pEnc, 1); |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
|
} |
|
1530 |
|
|
1531 |
return 1; // intra |
return 1; /* intra */ |
1532 |
} |
} |
1533 |
|
|
1534 |
|
static __inline void |
1535 |
|
updateFcode(Statistics * sStat, Encoder * pEnc) |
1536 |
|
{ |
1537 |
|
float fSigma; |
1538 |
|
int iSearchRange; |
1539 |
|
|
1540 |
|
if (sStat->iMvCount == 0) |
1541 |
|
sStat->iMvCount = 1; |
1542 |
|
|
1543 |
|
fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); |
1544 |
|
|
1545 |
|
iSearchRange = 16 << pEnc->mbParam.m_fcode; |
1546 |
|
|
1547 |
|
if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) |
1548 |
|
pEnc->mbParam.m_fcode++; |
1549 |
|
|
1550 |
|
else if ((5.0 * fSigma < iSearchRange) |
1551 |
|
&& (4.0 * pEnc->fMvPrevSigma < iSearchRange) |
1552 |
|
&& (pEnc->mbParam.m_fcode >= 2) ) |
1553 |
|
pEnc->mbParam.m_fcode--; |
1554 |
|
|
1555 |
|
pEnc->fMvPrevSigma = fSigma; |
1556 |
|
} |
1557 |
|
|
|
#define INTRA_THRESHOLD 0.5 |
|
1558 |
#define BFRAME_SKIP_THRESHHOLD 30 |
#define BFRAME_SKIP_THRESHHOLD 30 |
1559 |
|
|
1560 |
|
/* FrameCodeP also handles S(GMC)-VOPs */ |
1561 |
static int |
static int |
1562 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1563 |
Bitstream * bs, |
Bitstream * bs) |
|
uint32_t * pBits, |
|
|
bool force_inter, |
|
|
bool vol_header) |
|
1564 |
{ |
{ |
1565 |
float fSigma; |
int bits = BitstreamPos(bs); |
1566 |
|
|
1567 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1568 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1569 |
|
|
|
int iLimit; |
|
1570 |
int x, y, k; |
int x, y, k; |
1571 |
int iSearchRange; |
FRAMEINFO *const current = pEnc->current; |
1572 |
int bIntra; |
FRAMEINFO *const reference = pEnc->reference; |
1573 |
|
MBParam * const pParam = &pEnc->mbParam; |
1574 |
|
int mb_width = pParam->mb_width; |
1575 |
|
int mb_height = pParam->mb_height; |
1576 |
|
int coded = 1; |
1577 |
|
|
1578 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
IMAGE *pRef = &reference->image; |
|
IMAGE *pRef = &pEnc->reference->image; |
|
1579 |
|
|
1580 |
|
if (!reference->is_edged) { |
1581 |
start_timer(); |
start_timer(); |
1582 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1583 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pParam->width, pParam->height, 0); |
1584 |
stop_edges_timer(); |
stop_edges_timer(); |
1585 |
|
reference->is_edged = 1; |
1586 |
|
} |
1587 |
|
|
1588 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
1589 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1590 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
current->fcode = pParam->m_fcode; |
1591 |
|
|
1592 |
if (!force_inter) |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1593 |
iLimit = |
if (reference->is_interpolated != current->rounding_type) { |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
|
|
INTRA_THRESHOLD); |
|
|
else |
|
|
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
|
|
|
|
|
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
|
1594 |
start_timer(); |
start_timer(); |
1595 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1596 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pParam->edged_width, |
1597 |
pEnc->mbParam.edged_height, |
pParam->edged_height, |
1598 |
pEnc->current->rounding_type); |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1599 |
|
current->rounding_type); |
1600 |
stop_inter_timer(); |
stop_inter_timer(); |
1601 |
|
reference->is_interpolated = current->rounding_type; |
1602 |
|
} |
1603 |
} |
} |
1604 |
|
|
1605 |
|
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1606 |
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1607 |
|
|
1608 |
|
current->coding_type = P_VOP; |
1609 |
|
|
1610 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1611 |
|
|
1612 |
|
SetMacroblockQuants(&pEnc->mbParam, current); |
1613 |
|
|
1614 |
start_timer(); |
start_timer(); |
1615 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
1616 |
HintedMESet(pEnc, &bIntra); |
{ int gmcval; |
1617 |
if (bIntra == 0) { |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
|
pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current); |
|
|
MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
1618 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
|
} |
|
1619 |
|
|
1620 |
|
if (current->motion_flags & XVID_ME_GME_REFINE) { |
1621 |
|
gmcval = GlobalMotionEstRefine(¤t->warp, |
1622 |
|
current->mbs, pParam, |
1623 |
|
current, reference, |
1624 |
|
¤t->image, |
1625 |
|
&reference->image, |
1626 |
|
&pEnc->vInterH, |
1627 |
|
&pEnc->vInterV, |
1628 |
|
&pEnc->vInterHV); |
1629 |
} else { |
} else { |
1630 |
|
gmcval = globalSAD(¤t->warp, pParam, current->mbs, |
1631 |
|
current, |
1632 |
|
&reference->image, |
1633 |
|
¤t->image, |
1634 |
|
pEnc->vGMC.y); |
1635 |
|
} |
1636 |
|
|
1637 |
|
gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height); |
1638 |
|
|
1639 |
|
/* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */ |
1640 |
|
generate_GMCparameters( 3, 3, ¤t->warp, |
1641 |
|
pParam->width, pParam->height, |
1642 |
|
¤t->new_gmc_data); |
1643 |
|
|
1644 |
|
if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) || |
1645 |
|
(current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) ) |
1646 |
|
{ |
1647 |
|
current->coding_type = S_VOP; |
1648 |
|
|
1649 |
|
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1650 |
|
pParam->mb_width, pParam->mb_height, |
1651 |
|
pParam->edged_width, pParam->edged_width/2, |
1652 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1653 |
|
current->rounding_type, current->mbs, &pEnc->vGMC); |
1654 |
|
|
1655 |
bIntra = |
} else { |
|
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
|
iLimit); |
|
1656 |
|
|
1657 |
|
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1658 |
|
pParam->mb_width, pParam->mb_height, |
1659 |
|
pParam->edged_width, pParam->edged_width/2, |
1660 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1661 |
|
current->rounding_type, current->mbs, NULL); /* no warping, just AMV */ |
1662 |
|
} |
1663 |
} |
} |
|
stop_motion_timer(); |
|
1664 |
|
|
1665 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
MotionEstimation(&pEnc->mbParam, current, reference, |
1666 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1667 |
|
&pEnc->vGMC, 256*4096); |
1668 |
|
|
|
pEnc->current->coding_type = P_VOP; |
|
1669 |
|
|
1670 |
if (vol_header) |
stop_motion_timer(); |
|
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
|
1671 |
|
|
1672 |
|
set_timecodes(current,reference,pParam->fbase); |
1673 |
|
|
1674 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
|
1675 |
|
|
1676 |
*pBits = BitstreamPos(bs); |
for (y = 0; y < mb_height; y++) { |
1677 |
|
for (x = 0; x < mb_width; x++) { |
1678 |
|
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
1679 |
|
int skip_possible; |
1680 |
|
|
1681 |
pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1682 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
CodeIntraMB(pEnc, pMB); |
1683 |
|
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1684 |
|
dct_codes, qcoeff); |
1685 |
|
|
1686 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
start_timer(); |
1687 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
MBPrediction(current, x, y, pParam->mb_width, qcoeff); |
1688 |
MACROBLOCK *pMB = |
stop_prediction_timer(); |
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1689 |
|
|
1690 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
current->sStat.kblks++; |
1691 |
|
|
1692 |
|
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1693 |
|
stop_coding_timer(); |
1694 |
|
continue; |
1695 |
|
} |
1696 |
|
|
|
if (!bIntra) { |
|
1697 |
start_timer(); |
start_timer(); |
1698 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &reference->image, |
1699 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1700 |
&pEnc->vInterHV, &pEnc->current->image, |
&pEnc->vInterHV, &pEnc->vGMC, |
1701 |
dct_codes, pEnc->mbParam.width, |
¤t->image, |
1702 |
pEnc->mbParam.height, |
dct_codes, pParam->width, |
1703 |
pEnc->mbParam.edged_width, |
pParam->height, |
1704 |
pEnc->current->rounding_type); |
pParam->edged_width, |
1705 |
stop_comp_timer(); |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
1706 |
|
current->rounding_type); |
1707 |
|
|
1708 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
stop_comp_timer(); |
|
if (pMB->dquant != NO_CHANGE) { |
|
|
pMB->mode = MODE_INTER_Q; |
|
|
pEnc->current->quant += DQtab[pMB->dquant]; |
|
|
if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
else if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
|
} |
|
|
} |
|
|
pMB->quant = pEnc->current->quant; |
|
1709 |
|
|
1710 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1711 |
|
|
1712 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->cbp != 0) { |
1713 |
pMB->cbp = |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
|
dct_codes, qcoeff); |
|
|
} else { |
|
|
CodeIntraMB(pEnc, pMB); |
|
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
1714 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1715 |
} |
} |
1716 |
|
|
1717 |
start_timer(); |
if (pMB->dquant != 0) |
1718 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBSetDquant(pMB, x, y, &pEnc->mbParam); |
|
stop_prediction_timer(); |
|
1719 |
|
|
1720 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
1721 |
pEnc->sStat.kblks++; |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
|
1722 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1723 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1724 |
pEnc->sStat.mblks++; |
current->sStat.mblks++; |
1725 |
} else { |
} else { |
1726 |
pEnc->sStat.ublks++; |
current->sStat.ublks++; |
1727 |
} |
} |
1728 |
|
|
1729 |
start_timer(); |
start_timer(); |
1730 |
|
|
1731 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1732 |
|
|
1733 |
if ((pMB->mode == MODE_NOT_CODED) || |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER); |
|
(pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
|
|
pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) { |
|
1734 |
|
|
1735 |
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
if (current->coding_type == S_VOP) |
1736 |
|
skip_possible &= (pMB->mcsel == 1); |
1737 |
|
else { /* PVOP */ |
1738 |
|
const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ? |
1739 |
|
pMB->qmvs : pMB->mvs; |
1740 |
|
skip_possible &= ((mv->x|mv->y) == 0); |
1741 |
|
} |
1742 |
|
|
1743 |
|
if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1744 |
|
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1745 |
int bSkip = 1; |
int bSkip = 1; |
|
pMB->mode = MODE_NOT_CODED; |
|
1746 |
|
|
1747 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */ |
1748 |
{ |
|
1749 |
|
for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) { |
1750 |
int iSAD; |
int iSAD; |
1751 |
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
1752 |
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
1753 |
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant); |
1754 |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) { |
1755 |
{ bSkip = 0; |
bSkip = 0; /* could not SKIP */ |
1756 |
break; |
if (pParam->vol_flags & XVID_VOL_QUARTERPEL) { |
1757 |
} |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1758 |
|
pMB->pmvs[0].x = - predMV.x; |
1759 |
|
pMB->pmvs[0].y = - predMV.y; |
1760 |
|
} else { |
1761 |
|
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1762 |
|
pMB->pmvs[0].x = - predMV.x; |
1763 |
|
pMB->pmvs[0].y = - predMV.y; |
1764 |
} |
} |
|
if (!bSkip) |
|
|
{ |
|
|
VECTOR predMV; |
|
|
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
|
|
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
|
1765 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1766 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1767 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
break; |
1768 |
|
} |
1769 |
|
} |
1770 |
} |
} |
|
else MBSkip(bs); |
|
1771 |
|
|
1772 |
} else { |
if (bSkip) { |
1773 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
/* do SKIP */ |
1774 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
pMB->mode = MODE_NOT_CODED; |
1775 |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
MBSkip(bs); |
1776 |
qcoeff[5*64+0]=0; |
stop_coding_timer(); |
1777 |
|
continue; /* next MB */ |
1778 |
} |
} |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
|
1779 |
} |
} |
1780 |
|
|
1781 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1782 |
|
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1783 |
stop_coding_timer(); |
stop_coding_timer(); |
1784 |
} |
} |
1785 |
} |
} |
1786 |
|
|
1787 |
emms(); |
emms(); |
1788 |
|
updateFcode(¤t->sStat, pEnc); |
1789 |
|
|
1790 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
/* frame drop code */ |
1791 |
HintedMEGet(pEnc, 0); |
#if 0 |
1792 |
} |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
1793 |
|
#endif |
1794 |
if (pEnc->sStat.iMvCount == 0) |
if (current->sStat.kblks + current->sStat.mblks <= |
1795 |
pEnc->sStat.iMvCount = 1; |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1796 |
|
( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) |
1797 |
|
{ |
1798 |
|
current->sStat.kblks = current->sStat.mblks = 0; |
1799 |
|
current->sStat.ublks = mb_width * mb_height; |
1800 |
|
|
1801 |
fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
BitstreamReset(bs); |
1802 |
|
|
1803 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
set_timecodes(current,reference,pParam->fbase); |
1804 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant); |
1805 |
|
|
1806 |
if ((fSigma > iSearchRange / 3) |
/* copy reference frame details into the current frame */ |
1807 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
current->quant = reference->quant; |
1808 |
{ |
current->motion_flags = reference->motion_flags; |
1809 |
pEnc->mbParam.m_fcode++; |
current->rounding_type = reference->rounding_type; |
1810 |
iSearchRange *= 2; |
current->fcode = reference->fcode; |
1811 |
} else if ((fSigma < iSearchRange / 6) |
current->bcode = reference->bcode; |
1812 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
current->stamp = reference->stamp; |
1813 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1814 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1815 |
{ |
coded = 0; |
|
pEnc->mbParam.m_fcode--; |
|
|
iSearchRange /= 2; |
|
|
} |
|
1816 |
|
|
1817 |
pEnc->sStat.fMvPrevSigma = fSigma; |
} else { |
1818 |
|
|
1819 |
#ifdef FRAMEDROP |
pEnc->current->is_edged = 0; /* not edged */ |
1820 |
/* frame drop code */ |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
|
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
|
|
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
|
|
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
|
|
{ |
|
|
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
|
|
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
|
1821 |
|
|
1822 |
BitstreamReset(bs); |
/* what was this frame's interpolated reference will become |
1823 |
|
forward (past) reference in b-frame coding */ |
1824 |
|
|
1825 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1826 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1827 |
|
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1828 |
|
} |
1829 |
|
|
1830 |
// copy reference frame details into the current frame |
/* XXX: debug |
1831 |
pEnc->current->quant = pEnc->reference->quant; |
{ |
1832 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
char s[100]; |
1833 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
1834 |
pEnc->current->fcode = pEnc->reference->fcode; |
image_dump_yuvpgm(¤t->image, |
1835 |
pEnc->current->bcode = pEnc->reference->bcode; |
pParam->edged_width, |
1836 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pParam->width, pParam->height, s); |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
|
1837 |
|
|
1838 |
|
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
1839 |
|
image_dump_yuvpgm(&reference->image, |
1840 |
|
pParam->edged_width, |
1841 |
|
pParam->width, pParam->height, s); |
1842 |
} |
} |
1843 |
#endif |
*/ |
1844 |
|
|
1845 |
*pBits = BitstreamPos(bs) - *pBits; |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1846 |
|
|
1847 |
return 0; // inter |
current->length = (BitstreamPos(bs) - bits) / 8; |
1848 |
|
|
1849 |
|
return coded; |
1850 |
} |
} |
1851 |
|
|
1852 |
|
|
1853 |
static __inline void |
static void |
1854 |
FrameCodeB(Encoder * pEnc, |
FrameCodeB(Encoder * pEnc, |
1855 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
1856 |
Bitstream * bs, |
Bitstream * bs) |
|
uint32_t * pBits) |
|
1857 |
{ |
{ |
1858 |
int16_t dct_codes[6 * 64]; |
int bits = BitstreamPos(bs); |
1859 |
int16_t qcoeff[6 * 64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1860 |
|
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1861 |
uint32_t x, y; |
uint32_t x, y; |
1862 |
|
|
1863 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1875 |
} |
} |
1876 |
#endif |
#endif |
1877 |
|
|
1878 |
// forward |
/* forward */ |
1879 |
|
if (!pEnc->reference->is_edged) { |
1880 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1881 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1882 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1883 |
|
pEnc->current->is_edged = 1; |
1884 |
|
} |
1885 |
|
|
1886 |
|
if (pEnc->reference->is_interpolated != 0) { |
1887 |
start_timer(); |
start_timer(); |
1888 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1889 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1890 |
0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1891 |
stop_inter_timer(); |
stop_inter_timer(); |
1892 |
|
pEnc->reference->is_interpolated = 0; |
1893 |
|
} |
1894 |
|
|
1895 |
// backward |
/* backward */ |
1896 |
|
if (!pEnc->current->is_edged) { |
1897 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1898 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1899 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1900 |
|
pEnc->current->is_edged = 1; |
1901 |
|
} |
1902 |
|
|
1903 |
|
if (pEnc->current->is_interpolated != 0) { |
1904 |
start_timer(); |
start_timer(); |
1905 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1906 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1907 |
0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1908 |
stop_inter_timer(); |
stop_inter_timer(); |
1909 |
|
pEnc->current->is_interpolated = 0; |
1910 |
|
} |
1911 |
|
|
1912 |
start_timer(); |
frame->coding_type = B_VOP; |
1913 |
|
call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1914 |
|
|
1915 |
|
start_timer(); |
1916 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1917 |
((int32_t)(pEnc->current->stamp - frame->stamp)), // time_bp |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
1918 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
1919 |
pEnc->reference->mbs, f_ref, |
pEnc->reference->mbs, f_ref, |
1920 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1921 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
1922 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
|
|
|
|
|
|
1923 |
stop_motion_timer(); |
stop_motion_timer(); |
1924 |
|
|
|
/*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
|
|
{ |
|
|
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
|
|
} */ |
|
|
|
|
|
frame->coding_type = B_VOP; |
|
|
|
|
1925 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1926 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
|
|
|
|
*pBits = BitstreamPos(bs); |
|
|
|
|
|
pEnc->sStat.iTextBits = 0; |
|
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
|
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
|
1927 |
|
|
1928 |
|
frame->sStat.iTextBits = 0; |
1929 |
|
frame->sStat.iMvSum = 0; |
1930 |
|
frame->sStat.iMvCount = 0; |
1931 |
|
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
1932 |
|
frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1933 |
|
frame->sStat.kblks = frame->sStat.ublks = 0; |
1934 |
|
|
1935 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1936 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1937 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
|
1938 |
|
|
1939 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
1940 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1941 |
//mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1942 |
|
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
1943 |
|
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
1944 |
|
} |
1945 |
continue; |
continue; |
1946 |
} |
} |
1947 |
|
|
1948 |
if (mb->mode != MODE_DIRECT_NONE_MV) { |
mb->quant = frame->quant; |
1949 |
|
|
1950 |
|
if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1951 |
|
/* we have to motion-compensate, transfer etc, |
1952 |
|
because there might be blocks to code */ |
1953 |
|
|
1954 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1955 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1956 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1957 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1958 |
dct_codes); |
dct_codes); |
1959 |
|
|
1960 |
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1961 |
mb->quant = frame->quant; |
} |
1962 |
|
|
1963 |
mb->cbp = |
if (mb->mode == MODE_DIRECT_NO4V) |
1964 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
mb->mode = MODE_DIRECT; |
1965 |
|
|
1966 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) |
1967 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
1968 |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
else |
1969 |
} |
if (frame->vop_flags & XVID_VOP_GREYSCALE) |
1970 |
} |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */ |
1971 |
|
mb->cbp &= 0x3C; |
1972 |
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
BFRAME_DEBUG |
|
|
#endif |
|
1973 |
start_timer(); |
start_timer(); |
1974 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, |
1975 |
&pEnc->sStat, direction); |
&frame->sStat); |
1976 |
stop_coding_timer(); |
stop_coding_timer(); |
1977 |
} |
} |
1978 |
} |
} |
1979 |
|
|
1980 |
emms(); |
emms(); |
1981 |
|
|
1982 |
// TODO: dynamic fcode/bcode ??? |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1983 |
|
frame->length = (BitstreamPos(bs) - bits) / 8; |
|
*pBits = BitstreamPos(bs) - *pBits; |
|
1984 |
|
|
1985 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1986 |
if (!first){ |
if (!first){ |
1990 |
} |
} |
1991 |
#endif |
#endif |
1992 |
} |
} |
|
|
|
|
|
|
|
/* in case internal output is needed somewhere... */ |
|
|
/* { |
|
|
FILE *filehandle; |
|
|
filehandle=fopen("last-b.pgm","wb"); |
|
|
if (filehandle) |
|
|
{ |
|
|
fprintf(filehandle,"P5\n\n"); // |
|
|
fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height); |
|
|
fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle); |
|
|
fclose(filehandle); |
|
|
} |
|
|
} |
|
|
*/ |
|