26 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
29 |
* $Id: encoder.c,v 1.95 2003-02-19 21:30:52 edgomez Exp $ |
* $Id: encoder.c,v 1.95.2.5 2003-03-15 14:32:56 suxen_drol Exp $ |
30 |
* |
* |
31 |
****************************************************************************/ |
****************************************************************************/ |
32 |
|
|
47 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
48 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
49 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
|
#include "utils/ratecontrol.h" |
|
50 |
#include "utils/emms.h" |
#include "utils/emms.h" |
51 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
52 |
#include "quant/adapt_quant.h" |
#include "quant/adapt_quant.h" |
57 |
* Local macros |
* Local macros |
58 |
****************************************************************************/ |
****************************************************************************/ |
59 |
|
|
|
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
|
60 |
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
61 |
|
|
62 |
/***************************************************************************** |
/***************************************************************************** |
64 |
****************************************************************************/ |
****************************************************************************/ |
65 |
|
|
66 |
static int FrameCodeI(Encoder * pEnc, |
static int FrameCodeI(Encoder * pEnc, |
67 |
Bitstream * bs, |
Bitstream * bs); |
|
uint32_t * pBits); |
|
68 |
|
|
69 |
static int FrameCodeP(Encoder * pEnc, |
static int FrameCodeP(Encoder * pEnc, |
70 |
Bitstream * bs, |
Bitstream * bs, |
|
uint32_t * pBits, |
|
71 |
bool force_inter, |
bool force_inter, |
72 |
bool vol_header); |
bool vol_header); |
73 |
|
|
74 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
75 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
76 |
Bitstream * bs, |
Bitstream * bs); |
|
uint32_t * pBits); |
|
77 |
|
|
78 |
/***************************************************************************** |
/***************************************************************************** |
79 |
* Local data |
* Local data |
99 |
* and cleaning code. |
* and cleaning code. |
100 |
* |
* |
101 |
* Returned values : |
* Returned values : |
102 |
* - XVID_ERR_OK - no errors |
* - 0 - no errors |
103 |
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
104 |
* cleans the structure before exiting. |
* cleans the structure before exiting. |
105 |
* pParam->handle is also set to NULL. |
* pParam->handle is also set to NULL. |
106 |
* |
* |
107 |
****************************************************************************/ |
****************************************************************************/ |
108 |
|
|
|
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; |
|
|
} |
|
|
|
|
109 |
/* |
/* |
110 |
* Simplify the "fincr/fbase" fraction |
* Simplify the "fincr/fbase" fraction |
|
* (neccessary, since windows supplies us with huge numbers) |
|
111 |
*/ |
*/ |
112 |
|
static void |
113 |
i = pParam->fincr; |
simplify_time(int *inc, int *base) |
114 |
|
{ |
115 |
|
/* common factor */ |
116 |
|
int i = *inc; |
117 |
while (i > 1) { |
while (i > 1) { |
118 |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
if (*inc % i == 0 && *base % i == 0) { |
119 |
pParam->fincr /= i; |
*inc /= i; |
120 |
pParam->fbase /= i; |
*base /= i; |
121 |
i = pParam->fincr; |
i = *inc; |
122 |
continue; |
continue; |
123 |
} |
} |
124 |
i--; |
i--; |
125 |
} |
} |
126 |
|
|
127 |
if (pParam->fbase > 65535) { |
/* if neccessary, round to 65535 accuracy */ |
128 |
float div = (float) pParam->fbase / 65535; |
if (*base > 65535) { |
129 |
|
float div = (float) *base / 65535; |
130 |
pParam->fbase = (int) (pParam->fbase / div); |
*base = (int) (*base / div); |
131 |
pParam->fincr = (int) (pParam->fincr / div); |
*inc = (int) (*inc / div); |
132 |
|
} |
133 |
} |
} |
134 |
|
|
|
/* Bitrate allocator defaults */ |
|
135 |
|
|
136 |
if (pParam->rc_bitrate <= 0) |
int |
137 |
pParam->rc_bitrate = 900000; |
enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc) |
138 |
|
{ |
139 |
|
Encoder *pEnc; |
140 |
|
int n; |
141 |
|
|
142 |
if (pParam->rc_reaction_delay_factor <= 0) |
if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1)) /* v1.x.x */ |
143 |
pParam->rc_reaction_delay_factor = 16; |
return XVID_ERR_VERSION; |
144 |
|
|
145 |
if (pParam->rc_averaging_period <= 0) |
if (create->width%2 || create->height%2) |
146 |
pParam->rc_averaging_period = 100; |
return XVID_ERR_FAIL; |
147 |
|
|
148 |
if (pParam->rc_buffer <= 0) |
/* allocate encoder struct */ |
|
pParam->rc_buffer = 100; |
|
149 |
|
|
150 |
/* Max and min quantizers */ |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
151 |
|
if (pEnc == NULL) |
152 |
|
return XVID_ERR_MEMORY; |
153 |
|
memset(pEnc, 0, sizeof(Encoder)); |
154 |
|
|
155 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
/* global flags */ |
156 |
pParam->min_quantizer = 1; |
pEnc->mbParam.global_flags = create->global; |
157 |
|
|
158 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
/* width, height */ |
159 |
pParam->max_quantizer = 31; |
pEnc->mbParam.width = create->width; |
160 |
|
pEnc->mbParam.height = create->height; |
161 |
|
pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; |
162 |
|
pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; |
163 |
|
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
164 |
|
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
165 |
|
|
166 |
if (pParam->max_quantizer < pParam->min_quantizer) |
/* framerate */ |
167 |
pParam->max_quantizer = pParam->min_quantizer; |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
168 |
|
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
169 |
|
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
170 |
|
|
171 |
/* 1 keyframe each 10 seconds */ |
/* plugin */ |
172 |
|
pEnc->num_plugins = create->num_plugins; |
173 |
|
pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); |
174 |
|
if (pEnc->plugins == NULL) |
175 |
|
goto xvid_err_memory0; |
176 |
|
|
177 |
if (pParam->max_key_interval <= 0) |
for (n=0; n<pEnc->num_plugins;n++) { |
178 |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
xvid_plg_create_t pcreate; |
179 |
|
xvid_plg_info_t pinfo; |
180 |
|
|
181 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
182 |
if (pEnc == NULL) |
pinfo.version = XVID_VERSION; |
183 |
return XVID_ERR_MEMORY; |
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
184 |
|
pEnc->mbParam.plugin_flags |= pinfo.flags; |
185 |
/* Zero the Encoder Structure */ |
} |
186 |
|
|
187 |
memset(pEnc, 0, sizeof(Encoder)); |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
188 |
|
pcreate.version = XVID_VERSION; |
189 |
|
pcreate.width = pEnc->mbParam.width; |
190 |
|
pcreate.height = pEnc->mbParam.height; |
191 |
|
pcreate.fincr = pEnc->mbParam.fincr; |
192 |
|
pcreate.fbase = pEnc->mbParam.fbase; |
193 |
|
pcreate.param = create->plugins[n].param; |
194 |
|
|
195 |
/* Fill members of Encoder structure */ |
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
196 |
|
if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
197 |
|
pEnc->plugins[n].func = create->plugins[n].func; |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
pEnc->mbParam.width = pParam->width; |
/* temp dquants */ |
202 |
pEnc->mbParam.height = pParam->height; |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
203 |
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
204 |
|
/* XXX: error checking */ |
205 |
|
|
206 |
pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; |
/* bframes */ |
207 |
pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
208 |
|
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
209 |
|
pEnc->mbParam.bquant_offset = create->bquant_offset; |
210 |
|
|
211 |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
/* frame drop ratio */ |
212 |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
213 |
|
|
214 |
pEnc->mbParam.fbase = pParam->fbase; |
/* max keyframe interval */ |
215 |
pEnc->mbParam.fincr = pParam->fincr; |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval; |
216 |
|
/*XXX: replace 250 hard code with "10seconds * framerate" */ |
217 |
|
|
218 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
/* Bitrate allocator defaults |
219 |
|
|
220 |
pEnc->fMvPrevSigma = -1; |
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
221 |
|
create->min_quantizer = 1; |
222 |
|
|
223 |
/* Fill rate control parameters */ |
if ((create->max_quantizer <= 0) || (create->max_quantizer > 31)) |
224 |
|
create->max_quantizer = 31; |
225 |
|
|
226 |
pEnc->bitrate = pParam->rc_bitrate; |
if (create->max_quantizer < create->min_quantizer) |
227 |
|
create->max_quantizer = create->min_quantizer; */ |
228 |
|
|
|
pEnc->iFrameNum = -1; |
|
|
pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; |
|
229 |
|
|
230 |
/* try to allocate frame memory */ |
/* allocate working frame-image memory */ |
231 |
|
|
232 |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
233 |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
235 |
if (pEnc->current == NULL || pEnc->reference == NULL) |
if (pEnc->current == NULL || pEnc->reference == NULL) |
236 |
goto xvid_err_memory1; |
goto xvid_err_memory1; |
237 |
|
|
238 |
/* try to allocate mb memory */ |
/* allocate macroblock memory */ |
239 |
|
|
240 |
pEnc->current->mbs = |
pEnc->current->mbs = |
241 |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
247 |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
248 |
goto xvid_err_memory2; |
goto xvid_err_memory2; |
249 |
|
|
250 |
/* try to allocate image memory */ |
/* allocate interpolation image memory */ |
251 |
|
|
252 |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
253 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
254 |
|
image_null(&pEnc->sOriginal2); |
255 |
|
} |
256 |
|
|
257 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
258 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
266 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
267 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
268 |
|
|
269 |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
270 |
{ if (image_create |
if (image_create |
271 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
272 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
273 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
274 |
|
|
275 |
|
if (image_create |
276 |
|
(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
277 |
|
pEnc->mbParam.edged_height) < 0) |
278 |
|
goto xvid_err_memory3; |
279 |
} |
} |
280 |
|
|
281 |
if (image_create |
if (image_create |
326 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
327 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
328 |
|
|
329 |
|
/* init bframe image buffers */ |
330 |
|
|
331 |
|
pEnc->bframenum_head = 0; |
332 |
pEnc->mbParam.global = pParam->global; |
pEnc->bframenum_tail = 0; |
333 |
|
pEnc->flush_bframes = 0; |
334 |
|
pEnc->closed_bframenum = -1; |
335 |
|
|
336 |
/* B Frames specific init */ |
/* B Frames specific init */ |
|
pEnc->mbParam.max_bframes = pParam->max_bframes; |
|
|
pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; |
|
|
pEnc->mbParam.bquant_offset = pParam->bquant_offset; |
|
|
pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio; |
|
337 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
338 |
|
|
339 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
|
int n; |
|
340 |
|
|
341 |
pEnc->bframes = |
pEnc->bframes = |
342 |
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
372 |
} |
} |
373 |
} |
} |
374 |
|
|
375 |
pEnc->bframenum_head = 0; |
/* init incoming frame queue */ |
376 |
pEnc->bframenum_tail = 0; |
pEnc->queue_head = 0; |
377 |
pEnc->flush_bframes = 0; |
pEnc->queue_tail = 0; |
378 |
pEnc->bframenum_dx50bvop = -1; |
pEnc->queue_size = 0; |
|
|
|
|
pEnc->queue = NULL; |
|
|
|
|
|
|
|
|
if (pEnc->mbParam.max_bframes > 0) { |
|
|
int n; |
|
379 |
|
|
380 |
pEnc->queue = |
pEnc->queue = |
381 |
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO), |
382 |
CACHE_LINE); |
CACHE_LINE); |
383 |
|
|
384 |
if (pEnc->queue == NULL) |
if (pEnc->queue == NULL) |
385 |
goto xvid_err_memory4; |
goto xvid_err_memory4; |
386 |
|
|
387 |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
388 |
image_null(&pEnc->queue[n]); |
image_null(&pEnc->queue[n].image); |
389 |
|
|
390 |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
|
391 |
|
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
392 |
|
{ |
393 |
if (image_create |
if (image_create |
394 |
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
395 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
396 |
goto xvid_err_memory5; |
goto xvid_err_memory5; |
397 |
|
|
398 |
} |
} |
|
} |
|
399 |
|
|
|
pEnc->queue_head = 0; |
|
|
pEnc->queue_tail = 0; |
|
|
pEnc->queue_size = 0; |
|
400 |
|
|
401 |
pEnc->mbParam.m_stamp = 0; |
/* timestamp stuff */ |
402 |
|
|
403 |
|
pEnc->mbParam.m_stamp = 0; |
404 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
405 |
pEnc->current->stamp = 0; |
pEnc->current->stamp = 0; |
406 |
pEnc->reference->stamp = 0; |
pEnc->reference->stamp = 0; |
407 |
|
|
408 |
pParam->handle = (void *) pEnc; |
/* other stuff */ |
409 |
|
|
410 |
if (pParam->rc_bitrate) { |
pEnc->iFrameNum = 0; |
411 |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
pEnc->fMvPrevSigma = -1; |
412 |
pParam->rc_reaction_delay_factor, |
|
413 |
pParam->rc_averaging_period, pParam->rc_buffer, |
create->handle = (void *) pEnc; |
|
pParam->fbase * 1000 / pParam->fincr, |
|
|
pParam->max_quantizer, pParam->min_quantizer); |
|
|
} |
|
414 |
|
|
415 |
init_timer(); |
init_timer(); |
416 |
|
|
417 |
return XVID_ERR_OK; |
return 0; /* ok */ |
418 |
|
|
419 |
/* |
/* |
420 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
422 |
|
|
423 |
xvid_err_memory5: |
xvid_err_memory5: |
424 |
|
|
|
|
|
425 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
426 |
|
int i; |
427 |
|
|
428 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
429 |
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
430 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
431 |
} |
} |
432 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
435 |
xvid_err_memory4: |
xvid_err_memory4: |
436 |
|
|
437 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
438 |
|
int i; |
439 |
|
|
440 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
441 |
|
|
456 |
|
|
457 |
xvid_err_memory3: |
xvid_err_memory3: |
458 |
|
|
459 |
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
460 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
461 |
|
pEnc->mbParam.edged_height); |
462 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
463 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
464 |
} |
} |
465 |
|
|
497 |
xvid_err_memory1: |
xvid_err_memory1: |
498 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
499 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
500 |
|
|
501 |
|
xvid_free(pEnc->temp_dquants); |
502 |
|
|
503 |
|
xvid_err_memory0: |
504 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
505 |
|
if (pEnc->plugins[n].func) { |
506 |
|
pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0); |
507 |
|
} |
508 |
|
} |
509 |
|
xvid_free(pEnc->plugins); |
510 |
|
|
511 |
xvid_free(pEnc); |
xvid_free(pEnc); |
512 |
|
|
513 |
pParam->handle = NULL; |
create->handle = NULL; |
514 |
|
|
515 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
516 |
} |
} |
519 |
* Encoder destruction |
* Encoder destruction |
520 |
* |
* |
521 |
* This function destroy the entire encoder structure created by a previous |
* This function destroy the entire encoder structure created by a previous |
522 |
* successful encoder_create call. |
* successful enc_create call. |
523 |
* |
* |
524 |
* Returned values (for now only one returned value) : |
* Returned values (for now only one returned value) : |
525 |
* - XVID_ERR_OK - no errors |
* - 0 - no errors |
526 |
* |
* |
527 |
****************************************************************************/ |
****************************************************************************/ |
528 |
|
|
529 |
int |
int |
530 |
encoder_destroy(Encoder * pEnc) |
enc_destroy(Encoder * pEnc) |
531 |
{ |
{ |
532 |
int i; |
int i; |
533 |
|
|
|
ENC_CHECK(pEnc); |
|
|
|
|
534 |
/* B Frames specific */ |
/* B Frames specific */ |
535 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
536 |
|
|
537 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
538 |
|
|
539 |
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
540 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
541 |
} |
} |
542 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
586 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
587 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
588 |
|
|
589 |
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
590 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
591 |
|
pEnc->mbParam.edged_height); |
592 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
593 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
594 |
} |
} |
595 |
|
|
601 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
602 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
603 |
|
|
604 |
|
xvid_free(pEnc->temp_dquants); |
605 |
|
|
606 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
607 |
|
if (pEnc->plugins[i].func) { |
608 |
|
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0); |
609 |
|
} |
610 |
|
} |
611 |
|
xvid_free(pEnc->plugins); |
612 |
|
|
613 |
xvid_free(pEnc); |
xvid_free(pEnc); |
614 |
|
|
615 |
return XVID_ERR_OK; |
return 0; /* ok */ |
616 |
} |
} |
617 |
|
|
618 |
|
|
619 |
static __inline void inc_frame_num(Encoder * pEnc) |
/* |
620 |
|
call the plugins |
621 |
|
*/ |
622 |
|
|
623 |
|
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, int opt, int * type, int * quant) |
624 |
{ |
{ |
625 |
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
int i; |
626 |
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
xvid_plg_data_t data; |
627 |
|
|
628 |
|
memset(&data, 0, sizeof(xvid_plg_data_t)); |
629 |
|
data.version = XVID_VERSION; |
630 |
|
|
631 |
|
data.width = pEnc->mbParam.width; |
632 |
|
data.height = pEnc->mbParam.height; |
633 |
|
data.fincr = pEnc->mbParam.fincr; |
634 |
|
data.fbase = pEnc->mbParam.fbase; |
635 |
|
|
636 |
|
data.reference.csp = XVID_CSP_USER; |
637 |
|
data.reference.plane[0] = pEnc->reference->image.y; |
638 |
|
data.reference.plane[1] = pEnc->reference->image.u; |
639 |
|
data.reference.plane[2] = pEnc->reference->image.v; |
640 |
|
data.reference.stride[0] = pEnc->mbParam.edged_width; |
641 |
|
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
642 |
|
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
643 |
|
|
644 |
|
data.current.csp = XVID_CSP_USER; |
645 |
|
data.current.plane[0] = frame->image.y; |
646 |
|
data.current.plane[1] = frame->image.u; |
647 |
|
data.current.plane[2] = frame->image.v; |
648 |
|
data.current.stride[0] = pEnc->mbParam.edged_width; |
649 |
|
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
650 |
|
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
651 |
|
|
652 |
|
data.frame_num = frame->frame_num; |
653 |
|
|
654 |
|
if (opt == XVID_PLG_BEFORE) { |
655 |
|
data.type = XVID_TYPE_AUTO; |
656 |
|
data.quant = 2; |
657 |
|
//memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height); |
658 |
|
//data.qscale_stride = pEnc->mbParam.width; |
659 |
|
//data.qscale_table = pEnc->temp_dquants; |
660 |
|
/* todo: vol,vop,motion flags */ |
661 |
|
|
662 |
|
} else { // XVID_PLG_AFTER |
663 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
664 |
|
data.original.csp = XVID_CSP_USER; |
665 |
|
data.original.plane[0] = original->y; |
666 |
|
data.original.plane[1] = original->u; |
667 |
|
data.original.plane[2] = original->v; |
668 |
|
data.original.stride[0] = pEnc->mbParam.edged_width; |
669 |
|
data.original.stride[1] = pEnc->mbParam.edged_width/2; |
670 |
|
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
671 |
|
} |
672 |
|
|
673 |
|
data.type = coding2type(frame->coding_type); |
674 |
|
data.quant = frame->quant; |
675 |
|
/* todo: data.qscale */ |
676 |
|
data.vol_flags = frame->vol_flags; |
677 |
|
data.vop_flags = frame->vop_flags; |
678 |
|
data.motion_flags = frame->motion_flags; |
679 |
|
|
680 |
|
data.length = frame->length; |
681 |
|
data.kblks = frame->sStat.kblks; |
682 |
|
data.mblks = frame->sStat.mblks; |
683 |
|
data.ublks = frame->sStat.ublks; |
684 |
|
} |
685 |
|
|
686 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
687 |
|
if (pEnc->plugins[i].func) { |
688 |
|
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
689 |
|
continue; |
690 |
|
} |
691 |
|
} |
692 |
} |
} |
693 |
|
|
694 |
|
if (opt == XVID_PLG_BEFORE) { |
695 |
|
*type = data.type; |
696 |
|
*quant = data.quant; |
697 |
|
/* todo: copy modified qscale,vol,vop,motion flags into the frame*/ |
698 |
|
} |
699 |
|
|
|
static __inline void |
|
|
queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
|
|
{ |
|
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
|
|
{ |
|
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
|
|
return; |
|
700 |
} |
} |
701 |
|
|
|
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); |
|
702 |
|
|
703 |
|
|
|
start_timer(); |
|
|
if (image_input |
|
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
|
|
return; |
|
|
stop_conv_timer(); |
|
704 |
|
|
705 |
if ((pFrame->general & XVID_CHROMAOPT)) { |
static __inline void inc_frame_num(Encoder * pEnc) |
706 |
image_chroma_optimize(&pEnc->queue[pEnc->queue_tail], |
{ |
707 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->current->frame_num = pEnc->m_framenum; |
708 |
|
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
709 |
|
|
710 |
|
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
711 |
|
pEnc->m_framenum++; /* debug ticker */ |
712 |
} |
} |
713 |
|
|
714 |
pEnc->queue_size++; |
static __inline void dec_frame_num(Encoder * pEnc) |
715 |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
{ |
716 |
|
pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr; |
717 |
|
pEnc->m_framenum--; /* debug ticker */ |
718 |
} |
} |
719 |
|
|
720 |
|
|
721 |
|
|
722 |
static __inline void |
static __inline void |
723 |
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
724 |
{ |
{ |
739 |
} |
} |
740 |
|
|
741 |
|
|
742 |
|
static void |
743 |
/* convert pFrame->intra to coding_type */ |
set_stats(xvid_enc_stats_t * stats, const MBParam * pParam, const FRAMEINFO * frame) |
|
static int intra2coding_type(int intra) |
|
744 |
{ |
{ |
745 |
if (intra < 0) return -1; |
if (stats) |
746 |
if (intra == 1) return I_VOP; |
{ |
747 |
if (intra == 2) return B_VOP; |
stats->type = coding2type(frame->coding_type); |
748 |
|
stats->quant = frame->quant; |
749 |
return P_VOP; |
stats->vol_flags = frame->vol_flags; |
750 |
|
stats->vop_flags = frame->vop_flags; |
751 |
|
stats->length = frame->length; |
752 |
|
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
753 |
|
stats->kblks = frame->sStat.kblks; |
754 |
|
stats->mblks = frame->sStat.mblks; |
755 |
|
stats->ublks = frame->sStat.ublks; |
756 |
|
} |
757 |
} |
} |
758 |
|
|
759 |
|
|
762 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
763 |
* |
* |
764 |
* Returned values : |
* Returned values : |
765 |
* - XVID_ERR_OK - no errors |
* - >0 - output bytes |
766 |
|
* - 0 - no output |
767 |
|
* - XVID_ERR_VERSION - wrong version passed to core |
768 |
|
* - XVID_ERR_END - End of stream reached before end of coding |
769 |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
770 |
* format |
* format |
771 |
****************************************************************************/ |
****************************************************************************/ |
772 |
|
|
773 |
|
|
774 |
int |
int |
775 |
encoder_encode_bframes(Encoder * pEnc, |
enc_encode(Encoder * pEnc, |
776 |
XVID_ENC_FRAME * pFrame, |
xvid_enc_frame_t * xFrame, |
777 |
XVID_ENC_STATS * pResult) |
xvid_enc_stats_t * stats) |
778 |
{ |
{ |
779 |
uint16_t x, y; |
xvid_enc_frame_t * frame; |
780 |
|
int type; |
781 |
Bitstream bs; |
Bitstream bs; |
|
uint32_t bits; |
|
|
int mode = -1; /* Just to shut up compiler warning */ |
|
782 |
|
|
783 |
int input_valid = 1; |
if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
784 |
int bframes_count = 0; |
return XVID_ERR_VERSION; |
785 |
|
|
786 |
ENC_CHECK(pEnc); |
xFrame->out_flags = 0; |
|
ENC_CHECK(pFrame); |
|
|
ENC_CHECK(pFrame->image); |
|
787 |
|
|
788 |
start_global_timer(); |
start_global_timer(); |
789 |
|
BitstreamInit(&bs, xFrame->bitstream, 0); |
790 |
|
|
|
BitstreamInit(&bs, pFrame->bitstream, 0); |
|
|
|
|
|
ipvop_loop: |
|
|
|
|
|
/* |
|
|
* bframe "flush" code |
|
|
*/ |
|
791 |
|
|
792 |
if ((pFrame->image == NULL || pEnc->flush_bframes) |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
793 |
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
* enqueue image to the encoding-queue |
794 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
795 |
|
|
796 |
if (pEnc->flush_bframes == 0) { |
if (xFrame->input.csp != XVID_CSP_NULL) |
797 |
/* |
{ |
798 |
* we have reached the end of stream without getting |
QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail]; |
|
* a future reference frame... so encode last final |
|
|
* frame as a pframe |
|
|
*/ |
|
799 |
|
|
800 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
start_timer(); |
801 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
if (image_input |
802 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
(&q->image, pEnc->mbParam.width, pEnc->mbParam.height, |
803 |
|
pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, |
804 |
|
xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING)) |
805 |
|
{ |
806 |
|
emms(); |
807 |
|
return XVID_ERR_FORMAT; |
808 |
|
} |
809 |
|
stop_conv_timer(); |
810 |
|
|
811 |
pEnc->bframenum_tail--; |
if ((xFrame->vop_flags & XVID_CHROMAOPT)) { |
812 |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
image_chroma_optimize(&q->image, |
813 |
|
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
814 |
|
} |
815 |
|
|
816 |
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
q->frame = *xFrame; |
817 |
|
|
818 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
if (xFrame->quant_intra_matrix) |
819 |
bframes_count = 0; |
{ |
820 |
|
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix)); |
821 |
|
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
822 |
|
} |
823 |
|
|
824 |
BitstreamPadAlways(&bs); |
if (xFrame->quant_inter_matrix) |
825 |
pFrame->length = BitstreamLength(&bs); |
{ |
826 |
pFrame->intra = 0; |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix)); |
827 |
|
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
828 |
|
} |
829 |
|
|
830 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1); |
831 |
|
pEnc->queue_size++; |
832 |
|
} |
833 |
|
|
|
emms(); |
|
834 |
|
|
835 |
if (pResult) { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
836 |
pResult->quant = pEnc->current->quant; |
* bframe flush code |
837 |
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
pResult->kblks = pEnc->current->sStat.kblks; |
|
|
pResult->mblks = pEnc->current->sStat.mblks; |
|
|
pResult->ublks = pEnc->current->sStat.ublks; |
|
|
} |
|
838 |
|
|
839 |
return XVID_ERR_OK; |
repeat: |
|
} |
|
840 |
|
|
841 |
|
if (pEnc->flush_bframes) |
842 |
|
{ |
843 |
|
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
844 |
|
|
845 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
846 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
847 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
848 |
|
|
849 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
850 |
pEnc->bframenum_head++; |
image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image, |
851 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
BitstreamPadAlways(&bs); |
|
|
pFrame->length = BitstreamLength(&bs); |
|
|
pFrame->intra = 2; |
|
|
|
|
|
if (pResult) { |
|
|
pResult->quant = pEnc->current->quant; |
|
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
|
|
pResult->kblks = pEnc->current->sStat.kblks; |
|
|
pResult->mblks = pEnc->current->sStat.mblks; |
|
|
pResult->ublks = pEnc->current->sStat.ublks; |
|
852 |
} |
} |
853 |
|
|
854 |
if (input_valid) |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
|
queue_image(pEnc, pFrame); |
|
855 |
|
|
856 |
emms(); |
emms(); |
857 |
|
set_stats(stats, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); |
858 |
|
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0); |
859 |
|
emms(); |
860 |
|
|
861 |
return XVID_ERR_OK; |
pEnc->bframenum_head++; |
|
} |
|
862 |
|
|
863 |
if (pEnc->bframenum_head > 0) { |
goto done; |
864 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
} |
865 |
|
|
866 |
/* write an empty marker to the bitstream. |
/* write an empty marker to the bitstream. |
867 |
|
|
870 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
871 |
*/ |
*/ |
872 |
|
|
873 |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) { |
874 |
int tmp; |
int tmp; |
875 |
|
int bits; |
876 |
|
|
877 |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
878 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
879 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
880 |
|
|
881 |
|
bits = BitstreamPos(&bs); |
882 |
|
|
883 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
884 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
885 |
|
|
886 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
887 |
|
BitstreamPad(&bs); |
888 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
889 |
|
|
890 |
BitstreamPadAlways(&bs); |
/* add the not-coded length to the reference frame size */ |
891 |
pFrame->length = BitstreamLength(&bs); |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
892 |
pFrame->intra = 4; |
emms(); |
893 |
|
set_stats(stats, &pEnc->mbParam, pEnc->current); |
894 |
if (pResult) { |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
|
pResult->quant = pEnc->current->quant; |
|
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
|
|
pResult->kblks = pEnc->current->sStat.kblks; |
|
|
pResult->mblks = pEnc->current->sStat.mblks; |
|
|
pResult->ublks = pEnc->current->sStat.ublks; |
|
|
} |
|
|
|
|
|
if (input_valid) |
|
|
queue_image(pEnc, pFrame); |
|
|
|
|
895 |
emms(); |
emms(); |
896 |
|
|
897 |
return XVID_ERR_OK; |
/* flush complete: reset counters */ |
898 |
} |
pEnc->flush_bframes = 0; |
899 |
} |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
900 |
|
goto done; |
|
|
|
|
bvop_loop: |
|
|
|
|
|
if (pEnc->bframenum_dx50bvop != -1) |
|
|
{ |
|
|
|
|
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
|
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
|
901 |
|
|
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
|
902 |
} |
} |
903 |
|
|
904 |
if (input_valid) |
/* flush complete: reset counters */ |
905 |
{ |
pEnc->flush_bframes = 0; |
906 |
queue_image(pEnc, pFrame); |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
|
input_valid = 0; |
|
907 |
} |
} |
908 |
|
|
909 |
} else if (input_valid) { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
910 |
|
* dequeue frame from the encoding queue |
911 |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
912 |
|
|
913 |
start_timer(); |
if (pEnc->queue_size == 0) /* empty */ |
914 |
if (image_input |
{ |
915 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
|
916 |
{ |
{ |
|
emms(); |
|
|
return XVID_ERR_FORMAT; |
|
|
} |
|
|
stop_conv_timer(); |
|
917 |
|
|
918 |
if ((pFrame->general & XVID_CHROMAOPT)) { |
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) { |
919 |
image_chroma_optimize(&pEnc->current->image, |
emms(); |
920 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
set_stats(stats, &pEnc->mbParam, pEnc->current); |
921 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
922 |
|
emms(); |
923 |
} |
} |
924 |
|
|
925 |
/* queue input frame, and dequue next image */ |
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
926 |
if (pEnc->queue_size > 0) |
if (pEnc->bframenum_tail > 0) |
|
{ |
|
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
|
|
if (pEnc->queue_head != pEnc->queue_tail) |
|
927 |
{ |
{ |
|
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; |
|
|
} |
|
|
|
|
|
} else if (pEnc->queue_size > 0) { |
|
928 |
|
|
929 |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
930 |
|
pEnc->bframenum_tail--; |
931 |
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
932 |
|
|
933 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
/* convert B-VOP to P-VOP */ |
934 |
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
|
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", |
|
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
|
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
|
|
|
|
/* That disabled line of code was supposed to inform VirtualDub |
|
|
* that the frame was a dummy delay frame - now disabled (thx god :-) |
|
|
*/ |
|
|
/* BitstreamPutBits(&bs, 0x7f, 8); */ |
|
|
pFrame->intra = 5; |
|
|
|
|
|
if (pResult) { |
|
|
/* |
|
|
* We must decide what to put there because i know some apps |
|
|
* are storing statistics about quantizers and just do |
|
|
* stats[quant]++ or stats[quant-1]++ |
|
|
* transcode is one of these app with its 2pass module |
|
|
*/ |
|
|
|
|
|
/* |
|
|
* For now i prefer 31 than 0 that could lead to a segfault |
|
|
* in transcode |
|
|
*/ |
|
|
pResult->quant = 31; |
|
935 |
|
|
936 |
pResult->hlength = 0; |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
937 |
pResult->kblks = 0; |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
938 |
pResult->mblks = 0; |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
pResult->ublks = 0; |
|
939 |
} |
} |
940 |
|
|
941 |
} else { |
FrameCodeP(pEnc, &bs, 1, 0); |
942 |
|
|
943 |
if (pResult) { |
goto done_flush; |
|
pResult->quant = pEnc->current->quant; |
|
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
|
|
pResult->kblks = pEnc->current->sStat.kblks; |
|
|
pResult->mblks = pEnc->current->sStat.mblks; |
|
|
pResult->ublks = pEnc->current->sStat.ublks; |
|
944 |
} |
} |
945 |
|
|
|
} |
|
|
|
|
|
pFrame->length = BitstreamLength(&bs); |
|
|
|
|
946 |
emms(); |
emms(); |
947 |
|
return XVID_ERR_END; /* end of stream reached */ |
948 |
return XVID_ERR_OK; |
} |
949 |
|
goto done; /* nothing to encode yet; encoder lag */ |
950 |
} |
} |
951 |
|
|
952 |
pEnc->flush_bframes = 0; |
// the current FRAME becomes the reference |
953 |
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
|
emms(); |
|
954 |
|
|
955 |
/* only inc frame num, adapt quant, etc. if we havent seen it before */ |
// remove frame from encoding-queue (head), and move it into the current |
956 |
if (pEnc->bframenum_dx50bvop < 0 ) |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
957 |
{ |
frame = &pEnc->queue[pEnc->queue_head].frame; |
958 |
mode = intra2coding_type(pFrame->intra); |
pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1); |
959 |
if (pFrame->quant == 0) |
pEnc->queue_size--; |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
|
|
else |
|
|
pEnc->current->quant = pFrame->quant; |
|
960 |
|
|
|
/* if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
961 |
|
|
962 |
if (pEnc->current->quant > 31) |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
963 |
pEnc->current->quant = 31; |
* init pEnc->current fields |
964 |
*/ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
pEnc->current->global_flags = pFrame->general; |
|
|
pEnc->current->motion_flags = pFrame->motion; |
|
965 |
|
|
966 |
/* ToDo : dynamic fcode (in both directions) */ |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
967 |
|
pEnc->current->vop_flags = frame->vop_flags; |
968 |
|
pEnc->current->motion_flags = frame->motion; |
969 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
970 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
971 |
|
|
972 |
inc_frame_num(pEnc); |
if ((xFrame->vop_flags & XVID_CHROMAOPT)) { |
973 |
|
image_chroma_optimize(&pEnc->current->image, |
974 |
if (pFrame->general & XVID_EXTRASTATS) |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
|
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
|
} |
|
|
|
|
|
emms(); |
|
|
|
|
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
|
|
"%i if:%i st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp); |
|
975 |
} |
} |
976 |
|
|
977 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
emms(); /* float-point region */ |
|
* Luminance masking |
|
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
978 |
|
|
979 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
980 |
int *temp_dquants = |
unsigned int x, y; |
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
|
|
pEnc->mbParam.mb_height * sizeof(int), |
|
|
CACHE_LINE); |
|
981 |
|
|
982 |
pEnc->current->quant = |
pEnc->current->quant = |
983 |
adaptive_quantization(pEnc->current->image.y, |
adaptive_quantization(pEnc->current->image.y, |
984 |
pEnc->mbParam.edged_width, temp_dquants, |
pEnc->mbParam.edged_width, pEnc->temp_dquants, |
985 |
pEnc->current->quant, pEnc->current->quant, |
pEnc->current->quant, pEnc->current->quant, |
986 |
2 * pEnc->current->quant, |
2 * pEnc->current->quant, |
987 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
994 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
995 |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
996 |
|
|
997 |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2]; |
998 |
} |
} |
999 |
|
|
1000 |
#undef OFFSET |
#undef OFFSET |
1001 |
} |
} |
1002 |
|
|
|
xvid_free(temp_dquants); |
|
|
} |
|
|
|
|
1003 |
} |
} |
1004 |
|
|
1005 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1006 |
* ivop/pvop/bvop selection |
* frame type & quant selection |
1007 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
pEnc->iFrameNum++; |
|
|
|
|
|
if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || |
|
|
(mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
|
|
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) |
|
|
{ |
|
|
mode = I_VOP; |
|
|
}else{ |
|
|
mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
|
|
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
|
|
(mode < 0) ? pEnc->iFrameNum : 0, |
|
|
bframes_count++); |
|
|
} |
|
1008 |
|
|
1009 |
if (mode == I_VOP) { |
emms(); /* END floating-point region */ |
1010 |
/* |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant); |
1011 |
* This will be coded as an Intra Frame |
emms(); |
|
*/ |
|
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
|
|
pEnc->mbParam.m_quarterpel = 1; |
|
|
else |
|
|
pEnc->mbParam.m_quarterpel = 0; |
|
|
|
|
|
if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
|
1012 |
|
|
1013 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if (frame->type > 0) |
1014 |
if (pFrame->quant_intra_matrix != NULL) |
type = frame->type; |
|
set_intra_matrix(pFrame->quant_intra_matrix); |
|
|
if (pFrame->quant_inter_matrix != NULL) |
|
|
set_inter_matrix(pFrame->quant_inter_matrix); |
|
|
} |
|
1015 |
|
|
1016 |
|
if (frame->quant > 0) |
1017 |
|
pEnc->current->quant = frame->quant; |
1018 |
|
|
1019 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
if (type > 0){ /* XVID_TYPE_?VOP */ |
1020 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1021 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
} else{ /* XVID_TYPE_AUTO */ |
1022 |
|
if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){ |
1023 |
|
pEnc->iFrameNum = 0; |
1024 |
|
type = I_VOP; |
1025 |
|
}else{ |
1026 |
|
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1027 |
|
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1028 |
|
pEnc->iFrameNum, pEnc->bframenum_tail); |
1029 |
|
|
1030 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) { |
1031 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
type = P_VOP; /* disable dynamic bframes */ |
1032 |
} |
} |
|
|
|
|
/* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */ |
|
|
|
|
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
|
|
|
|
|
pEnc->bframenum_tail--; |
|
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
|
|
|
|
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
|
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
|
1033 |
} |
} |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
|
|
bframes_count = 0; |
|
|
pFrame->intra = 0; |
|
|
|
|
|
} else { |
|
|
|
|
|
FrameCodeI(pEnc, &bs, &bits); |
|
|
bframes_count = 0; |
|
|
pFrame->intra = 1; |
|
|
|
|
|
pEnc->bframenum_dx50bvop = -1; |
|
1034 |
} |
} |
1035 |
|
|
1036 |
pEnc->flush_bframes = 1; |
/* bframes buffer overflow check */ |
1037 |
|
if (type != I_VOP) { |
1038 |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1039 |
BitstreamPadAlways(&bs); |
type = P_VOP; |
1040 |
input_valid = 0; |
}else{ |
1041 |
goto ipvop_loop; |
type = B_VOP; |
1042 |
} |
} |
|
|
|
|
/* |
|
|
* NB : sequences like "IIBB" decode fine with msfdam but, |
|
|
* go screwy with divx 5.00 |
|
|
*/ |
|
|
} else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
|
|
/* |
|
|
* 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); |
|
|
|
|
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
|
1043 |
} |
} |
1044 |
|
|
1045 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
inc_frame_num(pEnc); |
1046 |
bframes_count = 0; |
pEnc->iFrameNum++; |
|
pFrame->intra = 0; |
|
|
pEnc->flush_bframes = 1; |
|
1047 |
|
|
1048 |
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1049 |
BitstreamPadAlways(&bs); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
1050 |
input_valid = 0; |
"%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
|
goto ipvop_loop; |
|
1051 |
} |
} |
1052 |
|
|
1053 |
} else { /* mode == B_VOP */ |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1054 |
/* |
* encode this frame as a b-vop |
1055 |
* 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) |
1056 |
*/ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1057 |
|
if (type == B_VOP) { |
1058 |
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1059 |
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"); |
1060 |
} |
} |
1061 |
|
|
1062 |
if (pFrame->bquant < 1) { |
if (frame->bquant < 1) { |
1063 |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1064 |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1065 |
|
|
1066 |
} else { |
} else { |
1067 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = frame->bquant; |
1068 |
} |
} |
1069 |
|
|
1070 |
if (pEnc->current->quant < 1) |
if (pEnc->current->quant < 1) |
1072 |
else if (pEnc->current->quant > 31) |
else if (pEnc->current->quant > 31) |
1073 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
1074 |
|
|
1075 |
DPRINTF(DPRINTF_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", |
1076 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1077 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1078 |
|
|
1082 |
|
|
1083 |
pEnc->bframenum_tail++; |
pEnc->bframenum_tail++; |
1084 |
|
|
1085 |
/* bframe report by koepi */ |
goto repeat; |
|
pFrame->intra = 2; |
|
|
pFrame->length = 0; |
|
|
|
|
|
input_valid = 0; |
|
|
goto bvop_loop; |
|
|
} |
|
|
|
|
|
BitstreamPadAlways(&bs); |
|
|
pFrame->length = BitstreamLength(&bs); |
|
|
|
|
|
if (pResult) { |
|
|
pResult->quant = pEnc->current->quant; |
|
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
|
|
pResult->kblks = pEnc->current->sStat.kblks; |
|
|
pResult->mblks = pEnc->current->sStat.mblks; |
|
|
pResult->ublks = pEnc->current->sStat.ublks; |
|
|
|
|
|
if (pFrame->general & XVID_EXTRASTATS) |
|
|
{ pResult->sse_y = |
|
|
plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height); |
|
|
|
|
|
pResult->sse_u = |
|
|
plane_sse( pEnc->sOriginal.u, pEnc->current->image.u, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
|
|
|
pResult->sse_v = |
|
|
plane_sse( pEnc->sOriginal.v, pEnc->current->image.v, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
} |
|
1086 |
} |
} |
1087 |
|
|
1088 |
|
/* for unpacked bframes, output the stats for the last encoded frame */ |
1089 |
|
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
1090 |
|
{ |
1091 |
|
if (pEnc->current->stamp > 0) { |
1092 |
emms(); |
emms(); |
1093 |
|
set_stats(stats, &pEnc->mbParam, pEnc->reference); |
1094 |
if (pFrame->quant == 0) { |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
|
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
|
|
pFrame->length, pFrame->intra); |
|
|
} |
|
|
|
|
|
stop_global_timer(); |
|
|
write_timer(); |
|
|
|
|
1095 |
emms(); |
emms(); |
1096 |
return XVID_ERR_OK; |
} |
1097 |
|
else |
1098 |
|
stats->type = XVID_TYPE_NOTHING; |
1099 |
} |
} |
1100 |
|
|
1101 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1102 |
|
* closed-gop |
1103 |
|
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
1104 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1105 |
|
|
1106 |
|
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1107 |
|
|
1108 |
/***************************************************************************** |
// place this frame back on the encoding-queue (head) |
1109 |
* "original" IP frame encoder entry point |
// we will deal with it next time |
1110 |
* |
pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1); |
1111 |
* Returned values : |
pEnc->queue_size++; |
1112 |
* - XVID_ERR_OK - no errors |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
|
|
* format |
|
|
****************************************************************************/ |
|
|
|
|
|
int |
|
|
encoder_encode(Encoder * pEnc, |
|
|
XVID_ENC_FRAME * pFrame, |
|
|
XVID_ENC_STATS * pResult) |
|
|
{ |
|
|
uint16_t x, y; |
|
|
Bitstream bs; |
|
|
uint32_t bits; |
|
|
uint16_t write_vol_header = 0; |
|
|
|
|
|
float psnr; |
|
|
char temp[128]; |
|
|
|
|
|
start_global_timer(); |
|
|
|
|
|
ENC_CHECK(pEnc); |
|
|
ENC_CHECK(pFrame); |
|
|
ENC_CHECK(pFrame->bitstream); |
|
|
ENC_CHECK(pFrame->image); |
|
|
|
|
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
|
|
|
|
|
pEnc->current->global_flags = pFrame->general; |
|
|
pEnc->current->motion_flags = pFrame->motion; |
|
|
pEnc->mbParam.hint = &pFrame->hint; |
|
1113 |
|
|
1114 |
inc_frame_num(pEnc); |
dec_frame_num(pEnc); |
1115 |
|
pEnc->iFrameNum--; |
1116 |
|
|
1117 |
/* disable alternate scan flag if interlacing is not enabled */ |
// grab the last frame from the bframe-queue |
|
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
|
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
|
|
{ |
|
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
|
|
} |
|
1118 |
|
|
1119 |
start_timer(); |
pEnc->bframenum_tail--; |
1120 |
if (image_input |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0) |
|
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
1121 |
|
|
1122 |
if ((pFrame->general & XVID_CHROMAOPT)) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1123 |
image_chroma_optimize(&pEnc->current->image, |
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); |
|
1124 |
} |
} |
1125 |
|
|
1126 |
if (pFrame->general & XVID_EXTRASTATS) |
/* convert B-VOP quant to P-VOP */ |
1127 |
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
1128 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
type = P_VOP; |
1129 |
} |
} |
1130 |
|
|
|
emms(); |
|
1131 |
|
|
1132 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1133 |
|
* encode this frame as an i-vop |
1134 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1135 |
|
|
1136 |
if (pFrame->quant == 0) { |
if (type == I_VOP) { |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
|
|
} else { |
|
|
pEnc->current->quant = pFrame->quant; |
|
|
} |
|
1137 |
|
|
1138 |
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1139 |
pEnc->mbParam.m_quarterpel = 1; |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1140 |
else |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
pEnc->mbParam.m_quarterpel = 0; |
|
1141 |
|
|
1142 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1143 |
int *temp_dquants = |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1144 |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
} |
|
pEnc->mbParam.mb_height * sizeof(int), |
|
|
CACHE_LINE); |
|
1145 |
|
|
|
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); |
|
1146 |
|
|
1147 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
/* ---- update vol flags at IVOP ----------- */ |
1148 |
|
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1149 |
|
|
1150 |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) { |
1151 |
|
if (frame->quant_intra_matrix != NULL) |
1152 |
|
set_intra_matrix(frame->quant_intra_matrix); |
1153 |
|
if (frame->quant_inter_matrix != NULL) |
1154 |
|
set_inter_matrix(frame->quant_inter_matrix); |
1155 |
|
} |
1156 |
|
|
1157 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
/* prevent vol/vop misuse */ |
1158 |
|
|
1159 |
|
if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) |
1160 |
|
pEnc->current->vop_flags &= ~XVID_REDUCED; |
1161 |
|
|
1162 |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
if (!(pEnc->current->vol_flags & XVID_INTERLACING)) |
1163 |
|
pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN); |
1164 |
|
|
1165 |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
/* ^^^------------------------ */ |
|
} |
|
1166 |
|
|
1167 |
#undef OFFSET |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1168 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1169 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1170 |
} |
} |
1171 |
|
|
|
xvid_free(temp_dquants); |
|
|
} |
|
1172 |
|
|
1173 |
if (pEnc->current->global_flags & XVID_H263QUANT) { |
FrameCodeI(pEnc, &bs); |
1174 |
if (pEnc->mbParam.m_quant_type != H263_QUANT) |
xFrame->out_flags |= XVID_KEYFRAME; |
|
write_vol_header = 1; |
|
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
|
|
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
|
|
int matrix1_changed, matrix2_changed; |
|
1175 |
|
|
1176 |
matrix1_changed = matrix2_changed = 0; |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1177 |
|
* encode this frame as an p-vop |
1178 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1179 |
|
|
1180 |
if (pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
} else { // (type == P_VOP || type == S_VOP) |
|
write_vol_header = 1; |
|
1181 |
|
|
1182 |
pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1183 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1184 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1185 |
|
|
1186 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1187 |
if (pFrame->quant_intra_matrix != NULL) |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
|
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
|
|
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; |
|
1188 |
} |
} |
1189 |
|
|
1190 |
if (pFrame->intra < 0) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1191 |
if ((pEnc->iFrameNum == -1) |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1192 |
|| ((pEnc->mbParam.iMaxKeyInterval > 0) |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
&& (pEnc->iFrameNum >= pEnc->mbParam.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); |
|
1193 |
} |
} |
1194 |
|
|
1195 |
|
FrameCodeP(pEnc, &bs, 1, 0); |
1196 |
} |
} |
1197 |
|
|
|
/* Relic from OpenDivX - now disabled |
|
|
BitstreamPutBits(&bs, 0xFFFF, 16); |
|
|
BitstreamPutBits(&bs, 0xFFFF, 16); |
|
|
*/ |
|
1198 |
|
|
1199 |
BitstreamPadAlways(&bs); |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1200 |
pFrame->length = BitstreamLength(&bs); |
* on next enc_encode call we must flush bframes |
1201 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1202 |
|
|
1203 |
if (pResult) { |
done_flush: |
|
pResult->quant = pEnc->current->quant; |
|
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
|
|
pResult->kblks = pEnc->current->sStat.kblks; |
|
|
pResult->mblks = pEnc->current->sStat.mblks; |
|
|
pResult->ublks = pEnc->current->sStat.ublks; |
|
|
} |
|
1204 |
|
|
1205 |
emms(); |
pEnc->flush_bframes = 1; |
1206 |
|
|
1207 |
if (pFrame->quant == 0) { |
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
1208 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
1209 |
pFrame->length, pFrame->intra); |
goto repeat; |
1210 |
} |
} |
|
if (pFrame->general & XVID_EXTRASTATS) |
|
|
{ |
|
|
psnr = |
|
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height); |
|
1211 |
|
|
1212 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
/* packed or no-bframes: output stats */ |
1213 |
|
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) |
1214 |
|
{ |
1215 |
|
emms(); |
1216 |
|
set_stats(stats, &pEnc->mbParam, pEnc->current); |
1217 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
1218 |
|
emms(); |
1219 |
} |
} |
1220 |
|
|
1221 |
pEnc->iFrameNum++; |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1222 |
|
* done; return number of bytes consumed |
1223 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1224 |
|
|
1225 |
|
done: |
1226 |
|
|
1227 |
stop_global_timer(); |
stop_global_timer(); |
1228 |
write_timer(); |
write_timer(); |
1229 |
|
|
1230 |
return XVID_ERR_OK; |
emms(); |
1231 |
|
return BitstreamLength(&bs); |
1232 |
} |
} |
1233 |
|
|
1234 |
|
|
1245 |
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; |
1246 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1247 |
|
|
1248 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
1249 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
1250 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
1251 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1261 |
} |
} |
1262 |
|
|
1263 |
|
|
|
#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) ? |
|
|
(uint32_t)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) ? |
|
|
(uint32_t)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 : (int)BitstreamGetBits(&bs, length); |
|
|
tmp.y = (hint->rawhints) ? |
|
|
bhint->mvs[0].y : (int)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 : (int)BitstreamGetBits(&bs, length); |
|
|
tmp.y = (hint->rawhints) ? |
|
|
bhint->mvs[vec].y : (int)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; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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) { |
|
|
BitstreamPadAlways(&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); |
|
|
} |
|
|
} |
|
|
|
|
1264 |
|
|
1265 |
static int |
static int |
1266 |
FrameCodeI(Encoder * pEnc, |
FrameCodeI(Encoder * pEnc, |
1267 |
Bitstream * bs, |
Bitstream * bs) |
|
uint32_t * pBits) |
|
1268 |
{ |
{ |
1269 |
|
int bits = BitstreamPos(bs); |
1270 |
int mb_width = pEnc->mbParam.mb_width; |
int mb_width = pEnc->mbParam.mb_width; |
1271 |
int mb_height = pEnc->mbParam.mb_height; |
int mb_height = pEnc->mbParam.mb_height; |
1272 |
|
|
1275 |
|
|
1276 |
uint16_t x, y; |
uint16_t x, y; |
1277 |
|
|
1278 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) |
1279 |
{ |
{ |
1280 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1281 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
1289 |
stop_edges_timer(); |
stop_edges_timer(); |
1290 |
} |
} |
1291 |
|
|
|
pEnc->iFrameNum = 0; |
|
1292 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1293 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
|
1294 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1295 |
|
|
1296 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1297 |
|
|
1298 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1299 |
|
|
1300 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1301 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1302 |
|
|
|
*pBits = BitstreamPos(bs); |
|
|
|
|
1303 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1304 |
pEnc->current->sStat.kblks = mb_width * mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1305 |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1319 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1320 |
|
|
1321 |
start_timer(); |
start_timer(); |
1322 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->vop_flags & XVID_GREYSCALE) |
1323 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1324 |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1325 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1328 |
stop_coding_timer(); |
stop_coding_timer(); |
1329 |
} |
} |
1330 |
|
|
1331 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
1332 |
{ |
{ |
1333 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1334 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1335 |
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
16, 0); |
1336 |
} |
} |
1337 |
emms(); |
emms(); |
1338 |
|
|
1339 |
*pBits = BitstreamPos(bs) - *pBits; |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
1340 |
|
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
1341 |
|
BitstreamPadAlways(bs); |
1342 |
|
else |
1343 |
|
BitstreamPad(bs); |
1344 |
|
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1345 |
|
|
1346 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1347 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1348 |
|
|
1349 |
|
/* XXX: hinted me |
1350 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1351 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1352 |
} |
}*/ |
1353 |
|
|
1354 |
return 1; /* intra */ |
return 1; /* intra */ |
1355 |
} |
} |
1363 |
static int |
static int |
1364 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1365 |
Bitstream * bs, |
Bitstream * bs, |
|
uint32_t * pBits, |
|
1366 |
bool force_inter, |
bool force_inter, |
1367 |
bool vol_header) |
bool vol_header) |
1368 |
{ |
{ |
1369 |
float fSigma; |
float fSigma; |
1370 |
|
int bits = BitstreamPos(bs); |
1371 |
|
|
1372 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1373 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1383 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1384 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1385 |
|
|
1386 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
1387 |
{ |
{ |
1388 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1389 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
1397 |
|
|
1398 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1399 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1400 |
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
//pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1401 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1402 |
|
|
1403 |
if (!force_inter) |
if (!force_inter) |
1405 |
else |
else |
1406 |
iLimit = mb_width * mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1407 |
|
|
1408 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->vop_flags & XVID_HALFPEL)) { |
1409 |
start_timer(); |
start_timer(); |
1410 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1411 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1412 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
1413 |
pEnc->mbParam.m_quarterpel, |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), |
1414 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1415 |
stop_inter_timer(); |
stop_inter_timer(); |
1416 |
} |
} |
1418 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1419 |
|
|
1420 |
start_timer(); |
start_timer(); |
1421 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1422 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1423 |
else |
else*/ |
1424 |
bIntra = |
bIntra = |
1425 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1426 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1428 |
|
|
1429 |
stop_motion_timer(); |
stop_motion_timer(); |
1430 |
|
|
1431 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
1432 |
|
|
1433 |
if ( ( pEnc->current->global_flags & XVID_GMC ) |
if ( ( pEnc->current->vol_flags & XVID_GMC ) |
1434 |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
1435 |
{ |
{ |
1436 |
pEnc->current->coding_type = S_VOP; |
pEnc->current->coding_type = S_VOP; |
1442 |
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1443 |
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1444 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1445 |
pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0, |
pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0, |
1446 |
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1447 |
|
|
1448 |
} |
} |
1449 |
|
|
1450 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1451 |
if (vol_header) |
if (vol_header) |
1452 |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1453 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1454 |
} |
} |
1455 |
|
|
1456 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1457 |
|
|
|
*pBits = BitstreamPos(bs); |
|
|
|
|
1458 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1459 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1460 |
|
|
1501 |
|
|
1502 |
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1503 |
|
|
1504 |
if (pEnc->mbParam.m_quarterpel) |
if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) |
1505 |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1506 |
else |
else |
1507 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1524 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1525 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1526 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1527 |
pEnc->mbParam.m_quarterpel, |
(pEnc->current->vol_flags & XVID_QUARTERPEL), |
1528 |
(pEnc->current->global_flags & XVID_REDUCED), |
(pEnc->current->vop_flags & XVID_REDUCED), |
1529 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1530 |
|
|
1531 |
stop_comp_timer(); |
stop_comp_timer(); |
1532 |
|
|
1533 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
1534 |
if (pMB->dquant != NO_CHANGE) { |
if (pMB->dquant != NO_CHANGE) { |
1535 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
1536 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1568 |
if (pEnc->current->coding_type == S_VOP) |
if (pEnc->current->coding_type == S_VOP) |
1569 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1570 |
else if (pEnc->current->coding_type == P_VOP) { |
else if (pEnc->current->coding_type == P_VOP) { |
1571 |
if (pEnc->mbParam.m_quarterpel) |
if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) |
1572 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
1573 |
else |
else |
1574 |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1595 |
} |
} |
1596 |
|
|
1597 |
if (!bSkip) { /* no SKIP, but trivial block */ |
if (!bSkip) { /* no SKIP, but trivial block */ |
1598 |
if(pEnc->mbParam.m_quarterpel) { |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
1599 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1600 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1601 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1622 |
} |
} |
1623 |
/* ordinary case: normal coded INTER/INTER4V block */ |
/* ordinary case: normal coded INTER/INTER4V block */ |
1624 |
|
|
1625 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if ((pEnc->current->vop_flags & XVID_GREYSCALE)) |
1626 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1627 |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1628 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1629 |
} |
} |
1630 |
|
|
1631 |
if(pEnc->mbParam.m_quarterpel) { |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
1632 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1633 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1634 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1645 |
{ int k; |
{ int k; |
1646 |
for (k=1;k<4;k++) |
for (k=1;k<4;k++) |
1647 |
{ |
{ |
1648 |
if(pEnc->mbParam.m_quarterpel) { |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
1649 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1650 |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1651 |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1666 |
} |
} |
1667 |
} |
} |
1668 |
|
|
1669 |
if ((pEnc->current->global_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
1670 |
{ |
{ |
1671 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1672 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1673 |
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
16, 0); |
1674 |
} |
} |
1675 |
|
|
1676 |
emms(); |
emms(); |
1677 |
|
|
1678 |
|
/* XXX: hinted me |
1679 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1680 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1681 |
} |
}*/ |
1682 |
|
|
1683 |
if (pEnc->current->sStat.iMvCount == 0) |
if (pEnc->current->sStat.iMvCount == 0) |
1684 |
pEnc->current->sStat.iMvCount = 1; |
pEnc->current->sStat.iMvCount = 1; |
1688 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1689 |
|
|
1690 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1691 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */ |
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
1692 |
{ |
{ |
1693 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1694 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1695 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1696 |
&& (pEnc->fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
1697 |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
1698 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) /* minimum search range 16 */ |
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
1699 |
{ |
{ |
1700 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1701 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1720 |
pEnc->current->quant = pEnc->reference->quant; |
pEnc->current->quant = pEnc->reference->quant; |
1721 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1722 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1723 |
pEnc->current->quarterpel = pEnc->reference->quarterpel; |
//pEnc->current->quarterpel = pEnc->reference->quarterpel; |
1724 |
pEnc->current->fcode = pEnc->reference->fcode; |
pEnc->current->fcode = pEnc->reference->fcode; |
1725 |
pEnc->current->bcode = pEnc->reference->bcode; |
pEnc->current->bcode = pEnc->reference->bcode; |
1726 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1742 |
} |
} |
1743 |
*/ |
*/ |
1744 |
|
|
1745 |
|
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
1746 |
|
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
1747 |
|
BitstreamPadAlways(bs); |
1748 |
|
else |
1749 |
|
BitstreamPad(bs); |
1750 |
|
|
1751 |
*pBits = BitstreamPos(bs) - *pBits; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1752 |
|
|
1753 |
return 0; /* inter */ |
return 0; /* inter */ |
1754 |
} |
} |
1757 |
static void |
static void |
1758 |
FrameCodeB(Encoder * pEnc, |
FrameCodeB(Encoder * pEnc, |
1759 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
1760 |
Bitstream * bs, |
Bitstream * bs) |
|
uint32_t * pBits) |
|
1761 |
{ |
{ |
1762 |
|
int bits = BitstreamPos(bs); |
1763 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1764 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1765 |
uint32_t x, y; |
uint32_t x, y; |
1774 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1775 |
} |
} |
1776 |
|
|
1777 |
pEnc->current->global_flags &= ~XVID_REDUCED; /* reduced resoltion not yet supported */ |
/* XXX: pEnc->current->global_flags &= ~XVID_REDUCED; reduced resoltion not yet supported */ |
1778 |
|
|
1779 |
if (!first){ |
if (!first){ |
1780 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1781 |
} |
} |
1782 |
#endif |
#endif |
1783 |
|
|
1784 |
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
//frame->quarterpel = pEnc->mbParam.m_quarterpel; |
1785 |
|
|
1786 |
/* forward */ |
/* forward */ |
1787 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1790 |
start_timer(); |
start_timer(); |
1791 |
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, |
1792 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1793 |
pEnc->mbParam.m_quarterpel, 0); |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); |
1794 |
stop_inter_timer(); |
stop_inter_timer(); |
1795 |
|
|
1796 |
/* backward */ |
/* backward */ |
1800 |
start_timer(); |
start_timer(); |
1801 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1802 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1803 |
pEnc->mbParam.m_quarterpel, 0); |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); |
1804 |
stop_inter_timer(); |
stop_inter_timer(); |
1805 |
|
|
1806 |
start_timer(); |
start_timer(); |
1827 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1828 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1829 |
|
|
|
*pBits = BitstreamPos(bs); |
|
|
|
|
1830 |
frame->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
1831 |
frame->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |
1832 |
frame->sStat.iMvCount = 0; |
frame->sStat.iMvCount = 0; |
1833 |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
1834 |
|
frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1835 |
|
frame->sStat.kblks = frame->sStat.ublks = 0; |
1836 |
|
|
1837 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1838 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1839 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1840 |
int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0; |
int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0; |
1841 |
|
|
1842 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
1843 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1856 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1857 |
|
|
1858 |
mb->cbp = |
mb->cbp = |
1859 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1860 |
|
|
1861 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1862 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
1878 |
|
|
1879 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
1880 |
|
|
1881 |
*pBits = BitstreamPos(bs) - *pBits; |
BitstreamPad(bs); |
1882 |
|
frame->length = (BitstreamPos(bs) - bits) / 8; |
1883 |
|
|
1884 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1885 |
if (!first){ |
if (!first){ |