1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
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 |
***************************************************************************/ |
****************************************************************************/ |
30 |
|
|
31 |
/**************************************************************************** |
/***************************************************************************** |
32 |
* |
* |
33 |
* History |
* History |
34 |
* |
* |
35 |
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 20.06.2002 bframe patch |
38 |
|
* 08.05.2002 fix some problem in DEBUG mode; |
39 |
|
* MinChen <chenm001@163.com> |
40 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
41 |
* |
* |
42 |
* $Id: encoder.c,v 1.34 2002-05-03 15:26:30 edgomez Exp $ |
* $Id: encoder.c,v 1.76.2.3 2002-09-27 17:25:17 h Exp $ |
43 |
* |
* |
44 |
***************************************************************************/ |
****************************************************************************/ |
45 |
|
|
46 |
|
|
47 |
#include <stdlib.h> |
#include <stdlib.h> |
48 |
#include <stdio.h> |
#include <stdio.h> |
49 |
#include <math.h> |
#include <math.h> |
50 |
|
#include <string.h> |
51 |
|
|
52 |
#include "encoder.h" |
#include "encoder.h" |
53 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
54 |
#include "global.h" |
#include "global.h" |
55 |
#include "utils/timer.h" |
#include "utils/timer.h" |
56 |
#include "image/image.h" |
#include "image/image.h" |
57 |
|
#ifdef BFRAMES |
58 |
|
#include "image/font.h" |
59 |
|
#include "motion/sad.h" |
60 |
|
#endif |
61 |
#include "motion/motion.h" |
#include "motion/motion.h" |
62 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
63 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
70 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
71 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
72 |
|
|
73 |
|
/***************************************************************************** |
74 |
|
* Local macros |
75 |
|
****************************************************************************/ |
76 |
|
|
77 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
78 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
79 |
|
|
80 |
|
/***************************************************************************** |
81 |
|
* Local function prototypes |
82 |
|
****************************************************************************/ |
83 |
|
|
84 |
|
static int FrameCodeI(Encoder * pEnc, |
85 |
|
Bitstream * bs, |
86 |
|
uint32_t * pBits); |
87 |
|
|
88 |
|
static int FrameCodeP(Encoder * pEnc, |
89 |
|
Bitstream * bs, |
90 |
|
uint32_t * pBits, |
91 |
|
bool force_inter, |
92 |
|
bool vol_header); |
93 |
|
|
94 |
|
static void FrameCodeB(Encoder * pEnc, |
95 |
|
FRAMEINFO * frame, |
96 |
|
Bitstream * bs, |
97 |
|
uint32_t * pBits); |
98 |
|
|
99 |
|
/***************************************************************************** |
100 |
|
* Local data |
101 |
|
****************************************************************************/ |
102 |
|
|
103 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
static int DQtab[4] = { |
|
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); |
|
|
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits); |
|
|
|
|
|
static int DQtab[4] = |
|
|
{ |
|
104 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
105 |
}; |
}; |
106 |
|
|
107 |
static int iDQtab[5] = |
static int iDQtab[5] = { |
|
{ |
|
108 |
1, 0, NO_CHANGE, 2, 3 |
1, 0, NO_CHANGE, 2, 3 |
109 |
}; |
}; |
110 |
|
|
111 |
|
|
112 |
void static image_null(IMAGE * image) |
static void __inline |
113 |
|
image_null(IMAGE * image) |
114 |
{ |
{ |
115 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
116 |
} |
} |
117 |
|
|
118 |
|
|
119 |
int encoder_create(XVID_ENC_PARAM * pParam) |
/***************************************************************************** |
120 |
|
* Encoder creation |
121 |
|
* |
122 |
|
* This function creates an Encoder instance, it allocates all necessary |
123 |
|
* image buffers (reference, current and bframes) and initialize the internal |
124 |
|
* xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. |
125 |
|
* |
126 |
|
* The code seems to be very long but is very basic, mainly memory allocation |
127 |
|
* and cleaning code. |
128 |
|
* |
129 |
|
* Returned values : |
130 |
|
* - XVID_ERR_OK - no errors |
131 |
|
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
132 |
|
* cleans the structure before exiting. |
133 |
|
* pParam->handle is also set to NULL. |
134 |
|
* |
135 |
|
****************************************************************************/ |
136 |
|
|
137 |
|
int |
138 |
|
encoder_create(XVID_ENC_PARAM * pParam) |
139 |
{ |
{ |
140 |
Encoder *pEnc; |
Encoder *pEnc; |
141 |
uint32_t i; |
int i; |
142 |
|
|
143 |
pParam->handle = NULL; |
pParam->handle = NULL; |
144 |
|
|
149 |
ENC_CHECK(!(pParam->width % 2)); |
ENC_CHECK(!(pParam->width % 2)); |
150 |
ENC_CHECK(!(pParam->height % 2)); |
ENC_CHECK(!(pParam->height % 2)); |
151 |
|
|
152 |
if (pParam->fincr <= 0 || pParam->fbase <= 0) |
/* Fps */ |
153 |
{ |
|
154 |
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
155 |
pParam->fincr = 1; |
pParam->fincr = 1; |
156 |
pParam->fbase = 25; |
pParam->fbase = 25; |
157 |
} |
} |
158 |
|
|
159 |
// simplify the "fincr/fbase" fraction |
/* |
160 |
// (neccessary, since windows supplies us with huge numbers) |
* Simplify the "fincr/fbase" fraction |
161 |
|
* (neccessary, since windows supplies us with huge numbers) |
162 |
|
*/ |
163 |
|
|
164 |
i = pParam->fincr; |
i = pParam->fincr; |
165 |
while (i > 1) |
while (i > 1) { |
166 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
167 |
pParam->fincr /= i; |
pParam->fincr /= i; |
168 |
pParam->fbase /= i; |
pParam->fbase /= i; |
169 |
i = pParam->fincr; |
i = pParam->fincr; |
172 |
i--; |
i--; |
173 |
} |
} |
174 |
|
|
175 |
if (pParam->fbase > 65535) |
if (pParam->fbase > 65535) { |
|
{ |
|
176 |
float div = (float)pParam->fbase / 65535; |
float div = (float)pParam->fbase / 65535; |
177 |
|
|
178 |
pParam->fbase = (int)(pParam->fbase / div); |
pParam->fbase = (int)(pParam->fbase / div); |
179 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
180 |
} |
} |
181 |
|
|
182 |
|
/* Bitrate allocator defaults */ |
183 |
|
|
184 |
if (pParam->rc_bitrate <= 0) |
if (pParam->rc_bitrate <= 0) |
185 |
pParam->rc_bitrate = 900000; |
pParam->rc_bitrate = 900000; |
186 |
|
|
193 |
if (pParam->rc_buffer <= 0) |
if (pParam->rc_buffer <= 0) |
194 |
pParam->rc_buffer = 100; |
pParam->rc_buffer = 100; |
195 |
|
|
196 |
|
/* Max and min quantizers */ |
197 |
|
|
198 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
199 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
200 |
|
|
201 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
202 |
pParam->max_quantizer = 31; |
pParam->max_quantizer = 31; |
203 |
|
|
|
if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
|
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
|
|
|
204 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
205 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
206 |
|
|
207 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
/* 1 keyframe each 10 seconds */ |
208 |
|
|
209 |
|
if (pParam->max_key_interval <= 0) |
210 |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
211 |
|
|
212 |
|
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
213 |
|
if (pEnc == NULL) |
214 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
215 |
|
|
216 |
|
/* Zero the Encoder Structure */ |
217 |
|
|
218 |
|
memset(pEnc, 0, sizeof(Encoder)); |
219 |
|
|
220 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
221 |
|
|
222 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
231 |
pEnc->mbParam.fbase = pParam->fbase; |
pEnc->mbParam.fbase = pParam->fbase; |
232 |
pEnc->mbParam.fincr = pParam->fincr; |
pEnc->mbParam.fincr = pParam->fincr; |
233 |
|
|
234 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
235 |
|
|
236 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
237 |
|
|
238 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
244 |
|
|
245 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
246 |
|
|
247 |
pEnc->current = NULL; |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
248 |
pEnc->reference = NULL; |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
249 |
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
|
250 |
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
if (pEnc->current == NULL || pEnc->reference == NULL) |
251 |
{ |
goto xvid_err_memory1; |
|
if (pEnc->current) xvid_free(pEnc->current); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
252 |
|
|
253 |
/* try to allocate mb memory */ |
/* try to allocate mb memory */ |
254 |
|
|
255 |
pEnc->current->mbs = NULL; |
pEnc->current->mbs = |
256 |
pEnc->reference->mbs = NULL; |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
257 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
258 |
|
pEnc->reference->mbs = |
259 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
260 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
261 |
|
|
262 |
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
263 |
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
goto xvid_err_memory2; |
|
{ |
|
|
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
} |
|
264 |
|
|
265 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
266 |
|
|
267 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
268 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
269 |
#endif |
#endif |
270 |
#ifdef BFRAMES |
|
271 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
272 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
273 |
image_null(&pEnc->f_refhv); |
image_null(&pEnc->f_refhv); |
274 |
#endif |
|
275 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
276 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
277 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
280 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
281 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
282 |
|
|
283 |
if ( |
#ifdef _DEBUG_PSNR |
284 |
#ifdef _DEBUG |
if (image_create |
285 |
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
286 |
|
pEnc->mbParam.edged_height) < 0) |
287 |
|
goto xvid_err_memory3; |
288 |
#endif |
#endif |
|
#ifdef BFRAMES |
|
|
image_create(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
#endif |
|
|
image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
#ifdef _DEBUG |
|
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
#endif |
|
|
#ifdef BFRAMES |
|
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
#endif |
|
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
289 |
|
|
290 |
xvid_free(pEnc->current); |
if (image_create |
291 |
xvid_free(pEnc->reference); |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
292 |
xvid_free(pEnc); |
pEnc->mbParam.edged_height) < 0) |
293 |
return XVID_ERR_MEMORY; |
goto xvid_err_memory3; |
294 |
} |
if (image_create |
295 |
|
(&pEnc->f_refv, pEnc->mbParam.edged_width, |
296 |
|
pEnc->mbParam.edged_height) < 0) |
297 |
|
goto xvid_err_memory3; |
298 |
|
if (image_create |
299 |
|
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
300 |
|
pEnc->mbParam.edged_height) < 0) |
301 |
|
goto xvid_err_memory3; |
302 |
|
|
303 |
|
if (image_create |
304 |
|
(&pEnc->current->image, pEnc->mbParam.edged_width, |
305 |
|
pEnc->mbParam.edged_height) < 0) |
306 |
|
goto xvid_err_memory3; |
307 |
|
if (image_create |
308 |
|
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
309 |
|
pEnc->mbParam.edged_height) < 0) |
310 |
|
goto xvid_err_memory3; |
311 |
|
if (image_create |
312 |
|
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
313 |
|
pEnc->mbParam.edged_height) < 0) |
314 |
|
goto xvid_err_memory3; |
315 |
|
if (image_create |
316 |
|
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
317 |
|
pEnc->mbParam.edged_height) < 0) |
318 |
|
goto xvid_err_memory3; |
319 |
|
if (image_create |
320 |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
321 |
|
pEnc->mbParam.edged_height) < 0) |
322 |
|
goto xvid_err_memory3; |
323 |
|
if (image_create |
324 |
|
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
325 |
|
pEnc->mbParam.edged_height) < 0) |
326 |
|
goto xvid_err_memory3; |
327 |
|
if (image_create |
328 |
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
329 |
|
pEnc->mbParam.edged_height) < 0) |
330 |
|
goto xvid_err_memory3; |
331 |
|
|
|
// ============================================================================== |
|
|
#ifdef BFRAMES |
|
332 |
|
|
333 |
// TODO: handle malloc() == NULL |
|
334 |
pEnc->max_bframes = pParam->max_bframes; |
/* B Frames specific init */ |
335 |
|
|
336 |
|
pEnc->global = pParam->global; |
337 |
|
pEnc->mbParam.max_bframes = pParam->max_bframes; |
338 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
339 |
if (pEnc->max_bframes > 0) |
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
340 |
{ |
pEnc->bframes = NULL; |
341 |
|
|
342 |
|
if (pEnc->mbParam.max_bframes > 0) { |
343 |
int n; |
int n; |
344 |
|
|
345 |
pEnc->bframes = malloc(pEnc->max_bframes * sizeof(FRAMEINFO *)); |
pEnc->bframes = |
346 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
347 |
|
CACHE_LINE); |
348 |
|
|
349 |
for (n = 0; n < pEnc->max_bframes; n++) |
if (pEnc->bframes == NULL) |
350 |
{ |
goto xvid_err_memory3; |
351 |
pEnc->bframes[n] = malloc(sizeof(FRAMEINFO)); |
|
352 |
pEnc->bframes[n]->mbs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
353 |
|
pEnc->bframes[n] = NULL; |
354 |
|
|
355 |
|
|
356 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
357 |
|
pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
358 |
|
|
359 |
|
if (pEnc->bframes[n] == NULL) |
360 |
|
goto xvid_err_memory4; |
361 |
|
|
362 |
|
pEnc->bframes[n]->mbs = |
363 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
364 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
365 |
|
|
366 |
|
if (pEnc->bframes[n]->mbs == NULL) |
367 |
|
goto xvid_err_memory4; |
368 |
|
|
369 |
|
image_null(&pEnc->bframes[n]->image); |
370 |
|
|
371 |
|
if (image_create |
372 |
|
(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, |
373 |
|
pEnc->mbParam.edged_height) < 0) |
374 |
|
goto xvid_err_memory4; |
375 |
|
|
|
if (image_create(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
376 |
} |
} |
377 |
} |
} |
378 |
|
|
379 |
pEnc->bframenum_head = 0; |
pEnc->bframenum_head = 0; |
380 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
381 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
382 |
|
pEnc->bframenum_dx50bvop = -1; |
383 |
|
|
384 |
pEnc->mbParam.m_seconds = 0; |
pEnc->queue = NULL; |
385 |
pEnc->mbParam.m_ticks = 0; |
|
386 |
#endif |
|
387 |
|
if (pEnc->mbParam.max_bframes > 0) { |
388 |
|
int n; |
389 |
|
|
390 |
|
pEnc->queue = |
391 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
392 |
|
CACHE_LINE); |
393 |
|
|
394 |
// ============================================================================== |
if (pEnc->queue == NULL) |
395 |
|
goto xvid_err_memory4; |
396 |
|
|
397 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
398 |
|
image_null(&pEnc->queue[n]); |
399 |
|
|
400 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
401 |
|
if (image_create |
402 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
403 |
|
pEnc->mbParam.edged_height) < 0) |
404 |
|
goto xvid_err_memory5; |
405 |
|
|
406 |
|
} |
407 |
|
} |
408 |
|
|
409 |
|
pEnc->queue_head = 0; |
410 |
|
pEnc->queue_tail = 0; |
411 |
|
pEnc->queue_size = 0; |
412 |
|
|
413 |
|
|
414 |
|
pEnc->mbParam.m_seconds = 0; |
415 |
|
pEnc->mbParam.m_ticks = 0; |
416 |
|
pEnc->m_framenum = 0; |
417 |
|
pEnc->last_pframe = 0; |
418 |
|
pEnc->last_sync = 0; |
419 |
|
|
420 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
421 |
|
|
422 |
if (pParam->rc_bitrate) |
if (pParam->rc_bitrate) { |
423 |
{ |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
424 |
RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
pParam->rc_reaction_delay_factor, |
425 |
pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, |
pParam->rc_averaging_period, pParam->rc_buffer, |
426 |
|
pParam->fbase * 1000 / pParam->fincr, |
427 |
pParam->max_quantizer, pParam->min_quantizer); |
pParam->max_quantizer, pParam->min_quantizer); |
428 |
} |
} |
429 |
|
|
430 |
init_timer(); |
init_timer(); |
431 |
|
|
432 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
433 |
|
|
434 |
|
/* |
435 |
|
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
436 |
|
*/ |
437 |
|
|
438 |
|
xvid_err_memory5: |
439 |
|
|
440 |
|
|
441 |
|
if (pEnc->mbParam.max_bframes > 0) { |
442 |
|
|
443 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
444 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
445 |
|
pEnc->mbParam.edged_height); |
446 |
|
} |
447 |
|
xvid_free(pEnc->queue); |
448 |
|
} |
449 |
|
|
450 |
|
xvid_err_memory4: |
451 |
|
|
452 |
|
if (pEnc->mbParam.max_bframes > 0) { |
453 |
|
|
454 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
455 |
|
|
456 |
|
if (pEnc->bframes[i] == NULL) |
457 |
|
continue; |
458 |
|
|
459 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
460 |
|
pEnc->mbParam.edged_height); |
461 |
|
|
462 |
|
xvid_free(pEnc->bframes[i]->mbs); |
463 |
|
|
464 |
|
xvid_free(pEnc->bframes[i]); |
465 |
|
|
466 |
|
} |
467 |
|
|
468 |
|
xvid_free(pEnc->bframes); |
469 |
|
} |
470 |
|
|
471 |
|
xvid_err_memory3: |
472 |
|
#ifdef _DEBUG_PSNR |
473 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
474 |
|
pEnc->mbParam.edged_height); |
475 |
|
#endif |
476 |
|
|
477 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
478 |
|
pEnc->mbParam.edged_height); |
479 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
480 |
|
pEnc->mbParam.edged_height); |
481 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
482 |
|
pEnc->mbParam.edged_height); |
483 |
|
|
484 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
485 |
|
pEnc->mbParam.edged_height); |
486 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
487 |
|
pEnc->mbParam.edged_height); |
488 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
489 |
|
pEnc->mbParam.edged_height); |
490 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
491 |
|
pEnc->mbParam.edged_height); |
492 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
493 |
|
pEnc->mbParam.edged_height); |
494 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
495 |
|
pEnc->mbParam.edged_height); |
496 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
497 |
|
pEnc->mbParam.edged_height); |
498 |
|
|
499 |
|
xvid_err_memory2: |
500 |
|
xvid_free(pEnc->current->mbs); |
501 |
|
xvid_free(pEnc->reference->mbs); |
502 |
|
|
503 |
|
xvid_err_memory1: |
504 |
|
xvid_free(pEnc->current); |
505 |
|
xvid_free(pEnc->reference); |
506 |
|
xvid_free(pEnc); |
507 |
|
|
508 |
|
pParam->handle = NULL; |
509 |
|
|
510 |
|
return XVID_ERR_MEMORY; |
511 |
} |
} |
512 |
|
|
513 |
|
/***************************************************************************** |
514 |
|
* Encoder destruction |
515 |
|
* |
516 |
|
* This function destroy the entire encoder structure created by a previous |
517 |
|
* successful encoder_create call. |
518 |
|
* |
519 |
|
* Returned values (for now only one returned value) : |
520 |
|
* - XVID_ERR_OK - no errors |
521 |
|
* |
522 |
|
****************************************************************************/ |
523 |
|
|
524 |
int encoder_destroy(Encoder * pEnc) |
int |
525 |
|
encoder_destroy(Encoder * pEnc) |
526 |
{ |
{ |
527 |
|
int i; |
528 |
|
|
529 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
530 |
|
|
531 |
// ================================================================= |
/* B Frames specific */ |
532 |
#ifdef BFRAMES |
if (pEnc->mbParam.max_bframes > 0) { |
533 |
if (pEnc->max_bframes > 0) |
|
534 |
{ |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
535 |
int n; |
|
536 |
for (n = 0; n < pEnc->max_bframes; n++) |
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
537 |
{ |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
free(pEnc->bframes[n]->mbs); |
|
|
free(pEnc->bframes[n]); |
|
538 |
} |
} |
539 |
free(pEnc->bframes); |
xvid_free(pEnc->queue); |
540 |
} |
} |
|
#endif |
|
|
//==================================================================== |
|
541 |
|
|
542 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
543 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (pEnc->mbParam.max_bframes > 0) { |
544 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
545 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
546 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
547 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (pEnc->bframes[i] == NULL) |
548 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
continue; |
549 |
#ifdef BFRAMES |
|
550 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
551 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
552 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
553 |
#endif |
xvid_free(pEnc->bframes[i]->mbs); |
554 |
#ifdef _DEBUG |
|
555 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
xvid_free(pEnc->bframes[i]); |
556 |
|
} |
557 |
|
|
558 |
|
xvid_free(pEnc->bframes); |
559 |
|
|
560 |
|
} |
561 |
|
|
562 |
|
/* All images, reference, current etc ... */ |
563 |
|
|
564 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
565 |
|
pEnc->mbParam.edged_height); |
566 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
567 |
|
pEnc->mbParam.edged_height); |
568 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
569 |
|
pEnc->mbParam.edged_height); |
570 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
571 |
|
pEnc->mbParam.edged_height); |
572 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
573 |
|
pEnc->mbParam.edged_height); |
574 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
575 |
|
pEnc->mbParam.edged_height); |
576 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
577 |
|
pEnc->mbParam.edged_height); |
578 |
|
|
579 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
580 |
|
pEnc->mbParam.edged_height); |
581 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
582 |
|
pEnc->mbParam.edged_height); |
583 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
584 |
|
pEnc->mbParam.edged_height); |
585 |
|
|
586 |
|
#ifdef _DEBUG_PSNR |
587 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
588 |
|
pEnc->mbParam.edged_height); |
589 |
#endif |
#endif |
590 |
|
|
591 |
|
/* Encoder structure */ |
592 |
|
|
593 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
594 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
595 |
|
|
597 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
598 |
|
|
599 |
xvid_free(pEnc); |
xvid_free(pEnc); |
600 |
|
|
601 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
602 |
} |
} |
603 |
|
|
604 |
|
|
605 |
|
static __inline void inc_frame_num(Encoder * pEnc) |
606 |
|
{ |
607 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
608 |
|
|
609 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
610 |
|
if (pEnc->mbParam.m_ticks < pEnc->last_sync) |
611 |
|
pEnc->mbParam.m_seconds = 1; |
612 |
|
// more than 1 second since last I or P is not supported. |
613 |
|
else |
614 |
|
pEnc->mbParam.m_seconds = 0; |
615 |
|
|
616 |
|
} |
617 |
|
|
618 |
|
|
619 |
|
static __inline void |
620 |
|
queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
621 |
|
{ |
622 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
623 |
|
{ |
624 |
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
625 |
|
return; |
626 |
|
} |
627 |
|
|
628 |
// ================================================================== |
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
629 |
#ifdef BFRAMES |
pEnc->bframenum_head, pEnc->bframenum_tail, |
630 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
631 |
|
|
632 |
|
|
633 |
|
start_timer(); |
634 |
|
if (image_input |
635 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
636 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
637 |
|
return; |
638 |
|
stop_conv_timer(); |
639 |
|
|
640 |
|
pEnc->queue_size++; |
641 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
642 |
|
} |
643 |
|
|
644 |
|
|
645 |
|
|
646 |
|
/***************************************************************************** |
647 |
|
* IPB frame encoder entry point |
648 |
|
* |
649 |
|
* Returned values : |
650 |
|
* - XVID_ERR_OK - no errors |
651 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
652 |
|
* format |
653 |
|
****************************************************************************/ |
654 |
|
|
655 |
|
int |
656 |
|
encoder_encode_bframes(Encoder * pEnc, |
657 |
|
XVID_ENC_FRAME * pFrame, |
658 |
|
XVID_ENC_STATS * pResult) |
659 |
{ |
{ |
660 |
uint16_t x, y; |
uint16_t x, y; |
661 |
Bitstream bs; |
Bitstream bs; |
662 |
uint32_t bits; |
uint32_t bits, mode; |
663 |
|
|
664 |
|
int input_valid = 1; |
665 |
|
|
666 |
|
#ifdef _DEBUG_PSNR |
667 |
|
float psnr; |
668 |
|
char temp[128]; |
669 |
|
#endif |
670 |
|
|
671 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
672 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
673 |
|
ENC_CHECK(pFrame->image); |
674 |
|
|
675 |
start_global_timer(); |
start_global_timer(); |
676 |
|
|
677 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
678 |
|
|
679 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
ipvop_loop: |
|
// bframe "flush" code |
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
680 |
|
|
681 |
if ((pFrame->image == NULL || pEnc->flush_bframes) && |
/* |
682 |
pEnc->bframenum_head < pEnc->bframenum_tail) |
* bframe "flush" code |
683 |
{ |
*/ |
684 |
|
|
685 |
|
if ((pFrame->image == NULL || pEnc->flush_bframes) |
686 |
|
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
687 |
|
|
688 |
|
if (pEnc->flush_bframes == 0) { |
689 |
|
/* |
690 |
|
* we have reached the end of stream without getting |
691 |
|
* a future reference frame... so encode last final |
692 |
|
* frame as a pframe |
693 |
|
*/ |
694 |
|
|
695 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
696 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
697 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
698 |
|
|
|
if (pEnc->flush_bframes == 0) |
|
|
{ |
|
|
// we have reached end of stream without getting a future reference |
|
|
// .. so encode last final frame a pframe |
|
|
dprintf("--- PFRAME (final frame correction) --- "); |
|
699 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
700 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
701 |
|
|
705 |
|
|
706 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
707 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
708 |
pFrame->intra = 0; |
pFrame->intra = 0; |
709 |
|
|
710 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
711 |
} |
} |
712 |
|
|
|
dprintf("--- BFRAME (flush) --- "); |
|
|
FrameCodeB(pEnc, |
|
|
pEnc->bframes[pEnc->bframenum_head], |
|
|
&bs, &bits); |
|
|
pEnc->bframenum_head++; |
|
713 |
|
|
714 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
715 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
716 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
717 |
|
|
718 |
|
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
719 |
|
pEnc->bframenum_head++; |
720 |
|
|
721 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
722 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
723 |
pFrame->intra = 0; |
pFrame->intra = 0; |
724 |
|
|
725 |
|
if (input_valid) |
726 |
|
queue_image(pEnc, pFrame); |
727 |
|
|
728 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
729 |
} |
} |
730 |
|
|
731 |
if (pFrame->image == NULL) |
if (pEnc->bframenum_head > 0) { |
732 |
{ |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
733 |
pFrame->length = 0; |
|
734 |
pFrame->input_consumed = 1; |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
735 |
|
|
736 |
|
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
737 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
738 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
739 |
|
|
740 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
741 |
|
BitstreamPad(&bs); |
742 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
743 |
|
|
744 |
|
pFrame->length = BitstreamLength(&bs); |
745 |
pFrame->intra = 0; |
pFrame->intra = 0; |
746 |
|
|
747 |
|
if (input_valid) |
748 |
|
queue_image(pEnc, pFrame); |
749 |
|
|
750 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
751 |
} |
} |
752 |
|
} |
753 |
|
|
754 |
|
|
755 |
|
bvop_loop: |
756 |
|
|
757 |
|
if (pEnc->bframenum_dx50bvop != -1) |
758 |
|
{ |
759 |
|
|
760 |
|
SWAP(pEnc->current, pEnc->reference); |
761 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
762 |
|
|
763 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
764 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
765 |
|
} |
766 |
|
|
767 |
if (pEnc->bframenum_head > 0) |
if (input_valid) |
768 |
{ |
{ |
769 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
queue_image(pEnc, pFrame); |
770 |
|
input_valid = 0; |
771 |
} |
} |
772 |
|
|
773 |
pEnc->flush_bframes = 0; |
} else if (input_valid) { |
774 |
|
|
775 |
|
SWAP(pEnc->current, pEnc->reference); |
776 |
|
|
777 |
|
start_timer(); |
778 |
|
if (image_input |
779 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
780 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
781 |
|
return XVID_ERR_FORMAT; |
782 |
|
stop_conv_timer(); |
783 |
|
|
784 |
|
// queue input frame, and dequue next image |
785 |
|
if (pEnc->queue_size > 0) |
786 |
|
{ |
787 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
788 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
789 |
|
{ |
790 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
791 |
|
} |
792 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
793 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
794 |
|
} |
795 |
|
|
796 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
} else if (pEnc->queue_size > 0) { |
797 |
|
|
798 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
799 |
|
|
800 |
pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(0) : pFrame->quant; |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
801 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
802 |
|
pEnc->queue_size--; |
803 |
|
|
804 |
|
} else if (BitstreamPos(&bs) == 0) { |
805 |
|
|
806 |
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
807 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
808 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
809 |
|
|
810 |
if(pEnc->current->quant < 1) |
pFrame->intra = 0; |
811 |
|
|
812 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP |
813 |
|
BitstreamPad(&bs); |
814 |
|
pFrame->length = BitstreamLength(&bs); |
815 |
|
|
816 |
|
return XVID_ERR_OK; |
817 |
|
|
818 |
|
} else { |
819 |
|
|
820 |
|
pFrame->length = BitstreamLength(&bs); |
821 |
|
return XVID_ERR_OK; |
822 |
|
} |
823 |
|
|
824 |
|
pEnc->flush_bframes = 0; |
825 |
|
|
826 |
|
emms(); |
827 |
|
|
828 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
829 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
830 |
|
{ |
831 |
|
if (pFrame->quant == 0) |
832 |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
833 |
|
else |
834 |
|
pEnc->current->quant = pFrame->quant; |
835 |
|
|
836 |
|
/* if (pEnc->current->quant < 1) |
837 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
838 |
|
|
839 |
if(pEnc->current->quant > 31) |
if(pEnc->current->quant > 31) |
840 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
841 |
|
*/ |
842 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
843 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
844 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
845 |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
/* ToDo : dynamic fcode (in both directions) */ |
|
//@@@ TODO: dyanmic fcode (in both directions) |
|
846 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
847 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
848 |
|
|
849 |
start_timer(); |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
850 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
851 |
pFrame->image, pFrame->colorspace)) |
|
852 |
{ |
inc_frame_num(pEnc); |
853 |
return XVID_ERR_FORMAT; |
|
854 |
|
#ifdef _DEBUG_PSNR |
855 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
856 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
857 |
|
#endif |
858 |
|
|
859 |
|
emms(); |
860 |
|
|
861 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
862 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
863 |
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
864 |
|
} |
865 |
|
|
866 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
867 |
|
* Luminance masking |
868 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
869 |
|
|
870 |
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
871 |
|
int *temp_dquants = |
872 |
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
873 |
|
pEnc->mbParam.mb_height * sizeof(int), |
874 |
|
CACHE_LINE); |
875 |
|
|
876 |
|
pEnc->current->quant = |
877 |
|
adaptive_quantization(pEnc->current->image.y, |
878 |
|
pEnc->mbParam.edged_width, temp_dquants, |
879 |
|
pEnc->current->quant, pEnc->current->quant, |
880 |
|
2 * pEnc->current->quant, |
881 |
|
pEnc->mbParam.mb_width, |
882 |
|
pEnc->mbParam.mb_height); |
883 |
|
|
884 |
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
885 |
|
|
886 |
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
887 |
|
|
888 |
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
889 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
890 |
|
|
891 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
892 |
|
} |
893 |
|
|
894 |
|
#undef OFFSET |
895 |
|
} |
896 |
|
|
897 |
|
xvid_free(temp_dquants); |
898 |
|
} |
899 |
|
|
900 |
} |
} |
|
stop_conv_timer(); |
|
901 |
|
|
902 |
#ifdef _DEBUG |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
903 |
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
* ivop/pvop/bvop selection |
904 |
#endif |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
905 |
|
|
906 |
|
|
907 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
908 |
// lumi masking |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
909 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
910 |
|
|| /*image_mad(&pEnc->reference->image, &pEnc->current->image, |
911 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
912 |
{ |
pEnc->mbParam.height) > 30) {*/ |
913 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, |
914 |
|
&pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { |
|
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, // stride |
|
|
temp_dquants, |
|
|
pEnc->current->quant, |
|
|
pEnc->current->quant, // min_quant |
|
|
2*pEnc->current->quant, // max_quant |
|
|
pEnc->mbParam.mb_width, |
|
|
pEnc->mbParam.mb_height); |
|
915 |
|
|
916 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
/* |
917 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
* This will be coded as an Intra Frame |
918 |
{ |
*/ |
919 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
920 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
921 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
922 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
923 |
|
|
924 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
925 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
926 |
} |
} |
927 |
xvid_free(temp_dquants); |
|
928 |
|
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
929 |
|
|
930 |
|
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
931 |
|
|
932 |
|
pEnc->bframenum_tail--; |
933 |
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
934 |
|
|
935 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
936 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
937 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
938 |
} |
} |
939 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
940 |
|
|
941 |
|
pFrame->intra = 0; |
942 |
|
|
943 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
} else { |
|
// ivop/pvop/bvop selection |
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
|
|
|
if (pEnc->iFrameNum == 0 || |
|
|
pFrame->intra == 1 || |
|
|
(pFrame->intra < 0 && (pEnc->iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->iMaxKeyInterval)) || |
|
|
image_mad(&pEnc->reference->image, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.width, pEnc->mbParam.height) > 30) |
|
|
{ |
|
|
dprintf("--- IFRAME ---"); |
|
944 |
|
|
945 |
FrameCodeI(pEnc, &bs, &bits); |
FrameCodeI(pEnc, &bs, &bits); |
|
|
|
946 |
pFrame->intra = 1; |
pFrame->intra = 1; |
947 |
|
|
948 |
|
pEnc->bframenum_dx50bvop = -1; |
949 |
|
} |
950 |
|
|
951 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
952 |
|
|
953 |
/* note: sequences like "IIBB" decode fine with msfdam but, |
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
954 |
go screwy with divx5.00 */ |
BitstreamPad(&bs); |
955 |
|
input_valid = 0; |
956 |
|
goto ipvop_loop; |
957 |
|
} |
958 |
|
|
959 |
|
/* |
960 |
|
* NB : sequences like "IIBB" decode fine with msfdam but, |
961 |
|
* go screwy with divx 5.00 |
962 |
|
*/ |
963 |
|
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
964 |
|
/* |
965 |
|
* This will be coded as a Predicted Frame |
966 |
|
*/ |
967 |
|
|
968 |
|
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
969 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
970 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
971 |
|
|
972 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
973 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
974 |
} |
} |
|
else if (pEnc->bframenum_tail >= pEnc->max_bframes) |
|
|
{ |
|
|
dprintf("--- PFRAME ---"); |
|
975 |
|
|
976 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
977 |
pFrame->intra = 0; |
pFrame->intra = 0; |
978 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
979 |
|
|
980 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
981 |
|
BitstreamPad(&bs); |
982 |
|
input_valid = 0; |
983 |
|
goto ipvop_loop; |
984 |
} |
} |
|
else |
|
|
{ |
|
|
dprintf("--- BFRAME (store) --- head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail); |
|
985 |
|
|
986 |
if (pFrame->bquant < 1) |
} else { |
987 |
{ |
/* |
988 |
pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
* This will be coded as a Bidirectional Frame |
989 |
|
*/ |
990 |
|
|
991 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
992 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
993 |
} |
} |
994 |
else |
|
995 |
{ |
if (pFrame->bquant < 1) { |
996 |
|
pEnc->current->quant = |
997 |
|
((pEnc->reference->quant + |
998 |
|
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
999 |
|
} else { |
1000 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1001 |
} |
} |
1002 |
|
if (pEnc->current->quant < 1) |
1003 |
|
pEnc->current->quant = 1; |
1004 |
|
|
1005 |
|
if (pEnc->current->quant > 31) |
1006 |
|
pEnc->current->quant = 31; |
1007 |
|
|
1008 |
|
|
1009 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1010 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1011 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1012 |
|
|
1013 |
|
|
1014 |
// store frame into bframe buffer & swap ref back to current |
|
1015 |
|
/* store frame into bframe buffer & swap ref back to current */ |
1016 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1017 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
1018 |
|
|
1020 |
|
|
1021 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1022 |
pFrame->length = 0; |
pFrame->length = 0; |
|
pFrame->input_consumed = 1; |
|
1023 |
|
|
1024 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
input_valid = 0; |
1025 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
goto bvop_loop; |
|
{ |
|
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
|
return XVID_ERR_OK; |
|
1026 |
} |
} |
1027 |
|
|
1028 |
|
pEnc->iFrameNum++; |
1029 |
|
|
1030 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1031 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1032 |
|
|
1033 |
if (pResult) |
if (pResult) { |
|
{ |
|
1034 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1035 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
1036 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
1038 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
1039 |
} |
} |
1040 |
|
|
1041 |
#ifdef _DEBUG |
emms(); |
1042 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
|
1043 |
pEnc->mbParam.width, pEnc->mbParam.height); |
#ifdef _DEBUG_PSNR |
1044 |
|
psnr = |
1045 |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1046 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1047 |
|
pEnc->mbParam.height); |
1048 |
|
|
1049 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1050 |
DEBUG(temp); |
DEBUG(temp); |
1051 |
#endif |
#endif |
1052 |
|
|
1053 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
1054 |
{ |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1055 |
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1056 |
} |
} |
1057 |
|
|
|
pEnc->iFrameNum++; |
|
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
|
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
|
|
{ |
|
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
|
pFrame->input_consumed = 1; |
|
1058 |
|
|
1059 |
stop_global_timer(); |
stop_global_timer(); |
1060 |
write_timer(); |
write_timer(); |
1061 |
|
|
1062 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1063 |
} |
} |
1064 |
// ================================================================== |
|
1065 |
#else |
|
1066 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
|
1067 |
|
/***************************************************************************** |
1068 |
|
* "original" IP frame encoder entry point |
1069 |
|
* |
1070 |
|
* Returned values : |
1071 |
|
* - XVID_ERR_OK - no errors |
1072 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1073 |
|
* format |
1074 |
|
****************************************************************************/ |
1075 |
|
|
1076 |
|
int |
1077 |
|
encoder_encode(Encoder * pEnc, |
1078 |
|
XVID_ENC_FRAME * pFrame, |
1079 |
|
XVID_ENC_STATS * pResult) |
1080 |
{ |
{ |
1081 |
uint16_t x, y; |
uint16_t x, y; |
1082 |
Bitstream bs; |
Bitstream bs; |
1083 |
uint32_t bits; |
uint32_t bits; |
1084 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1085 |
#ifdef _DEBUG |
|
1086 |
|
#ifdef _DEBUG_PSNR |
1087 |
float psnr; |
float psnr; |
1088 |
uint8_t temp[100]; |
uint8_t temp[128]; |
1089 |
#endif |
#endif |
1090 |
|
|
1091 |
start_global_timer(); |
start_global_timer(); |
1099 |
|
|
1100 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1101 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1102 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1103 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1104 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1105 |
|
|
1106 |
start_timer(); |
/* disable alternate scan flag if interlacing is not enabled */ |
1107 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
1108 |
pFrame->image, pFrame->colorspace)) |
!(pEnc->current->global_flags & XVID_INTERLACING)) |
1109 |
{ |
{ |
1110 |
return XVID_ERR_FORMAT; |
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
1111 |
} |
} |
1112 |
|
|
1113 |
|
start_timer(); |
1114 |
|
if (image_input |
1115 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1116 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
1117 |
|
return XVID_ERR_FORMAT; |
1118 |
stop_conv_timer(); |
stop_conv_timer(); |
1119 |
|
|
1120 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1121 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1122 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1123 |
#endif |
#endif |
1124 |
|
|
1125 |
EMMS(); |
emms(); |
1126 |
|
|
1127 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
1128 |
|
|
1129 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
1130 |
{ |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
1131 |
pEnc->current->quant = RateControlGetQ(0); |
} else { |
|
} |
|
|
else |
|
|
{ |
|
1132 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
1133 |
} |
} |
1134 |
|
|
1135 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1136 |
{ |
int *temp_dquants = |
1137 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
1138 |
|
pEnc->mbParam.mb_height * sizeof(int), |
1139 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
CACHE_LINE); |
1140 |
pEnc->mbParam.edged_width, // stride |
|
1141 |
temp_dquants, |
pEnc->current->quant = |
1142 |
pEnc->current->quant, |
adaptive_quantization(pEnc->current->image.y, |
1143 |
pEnc->current->quant, // min_quant |
pEnc->mbParam.edged_width, temp_dquants, |
1144 |
2*pEnc->current->quant, // max_quant |
pEnc->current->quant, pEnc->current->quant, |
1145 |
|
2 * pEnc->current->quant, |
1146 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
1147 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
1148 |
|
|
1149 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1150 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
1151 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
1152 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1153 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1154 |
|
|
1155 |
|
|
1156 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
1157 |
|
|
1158 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
1159 |
|
} |
1160 |
|
|
1161 |
|
#undef OFFSET |
1162 |
} |
} |
1163 |
|
|
1164 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
1165 |
} |
} |
1166 |
|
|
1168 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
1169 |
write_vol_header = 1; |
write_vol_header = 1; |
1170 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
1171 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
1172 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
int matrix1_changed, matrix2_changed; |
|
int ret1, ret2; |
|
1173 |
|
|
1174 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
1175 |
|
|
1176 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
1177 |
write_vol_header = 1; |
write_vol_header = 1; |
1180 |
|
|
1181 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1182 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
1183 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
1184 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
1185 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
1186 |
} |
} else { |
1187 |
else { |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
1188 |
ret1 = set_intra_matrix(get_default_intra_matrix()); |
matrix2_changed = set_inter_matrix(get_default_inter_matrix()); |
|
ret2 = set_inter_matrix(get_default_inter_matrix()); |
|
1189 |
} |
} |
1190 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
1191 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
1192 |
} |
} |
1193 |
|
|
1194 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
1195 |
{ |
if ((pEnc->iFrameNum == 0) |
1196 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->iMaxKeyInterval > 0) |
1197 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
|
|
|
1198 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1199 |
else |
} else { |
1200 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
1201 |
} |
} |
1202 |
else |
} else { |
1203 |
{ |
if (pFrame->intra == 1) { |
|
if (pFrame->intra == 1) |
|
1204 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1205 |
else |
} else { |
1206 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
1207 |
} |
} |
1208 |
|
|
1209 |
|
} |
1210 |
|
|
1211 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
1212 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
1213 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1214 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1215 |
|
|
1216 |
if (pResult) |
if (pResult) { |
|
{ |
|
1217 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1218 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
1219 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
1221 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
1222 |
} |
} |
1223 |
|
|
1224 |
EMMS(); |
emms(); |
|
|
|
|
if (pFrame->quant == 0) |
|
|
{ |
|
|
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
|
|
} |
|
1225 |
|
|
1226 |
#ifdef _DEBUG |
if (pFrame->quant == 0) { |
1227 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1228 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pFrame->length, pFrame->intra); |
1229 |
|
} |
1230 |
|
#ifdef _DEBUG_PSNR |
1231 |
|
psnr = |
1232 |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1233 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1234 |
|
pEnc->mbParam.height); |
1235 |
|
|
1236 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1237 |
DEBUG(temp); |
DEBUG(temp); |
1238 |
#endif |
#endif |
1239 |
|
|
1240 |
|
inc_frame_num(pEnc); |
1241 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1242 |
|
|
1243 |
stop_global_timer(); |
stop_global_timer(); |
1245 |
|
|
1246 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1247 |
} |
} |
|
#endif |
|
1248 |
|
|
1249 |
|
|
1250 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
1251 |
|
CodeIntraMB(Encoder * pEnc, |
1252 |
|
MACROBLOCK * pMB) |
1253 |
|
{ |
1254 |
|
|
1255 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1256 |
|
|
1261 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1262 |
|
|
1263 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1264 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
1265 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
1266 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1267 |
|
|
1268 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1269 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1270 |
|
if (pEnc->current->quant < 1) |
1271 |
|
pEnc->current->quant = 1; |
1272 |
} |
} |
1273 |
} |
} |
1274 |
|
|
1279 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
1280 |
#define MODEBITS 5 |
#define MODEBITS 5 |
1281 |
|
|
1282 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
1283 |
|
HintedMESet(Encoder * pEnc, |
1284 |
|
int *intra) |
1285 |
{ |
{ |
1286 |
HINTINFO * hint; |
HINTINFO * hint; |
1287 |
Bitstream bs; |
Bitstream bs; |
1290 |
|
|
1291 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1292 |
|
|
1293 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1294 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
1295 |
} |
} else { |
|
else |
|
|
{ |
|
1296 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1297 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
1298 |
} |
} |
1299 |
|
|
1300 |
if (*intra) |
if (*intra) { |
|
{ |
|
1301 |
return; |
return; |
1302 |
} |
} |
1303 |
|
|
1304 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = |
1305 |
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
1306 |
|
FCODEBITS); |
1307 |
|
|
1308 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1309 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1310 |
|
|
1311 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1312 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1313 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1314 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1315 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1316 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1317 |
VECTOR pred[4]; |
VECTOR pred; |
1318 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
1319 |
int vec; |
int vec; |
1320 |
|
|
1321 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = |
1322 |
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
1323 |
|
MODEBITS); |
1324 |
|
|
1325 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1326 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1327 |
|
|
1328 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
1329 |
{ |
tmp.x = |
1330 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
1331 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
length); |
1332 |
|
tmp.y = |
1333 |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
1334 |
|
length); |
1335 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1336 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1337 |
|
|
1338 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
1339 |
|
|
1340 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1341 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1342 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1343 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
1344 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
1345 |
} |
} |
1346 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
1347 |
else if (pMB->mode == MODE_INTER4V) |
for (vec = 0; vec < 4; ++vec) { |
1348 |
{ |
tmp.x = |
1349 |
for (vec=0 ; vec<4 ; ++vec) |
(hint->rawhints) ? bhint->mvs[vec]. |
1350 |
{ |
x : BitstreamGetBits(&bs, length); |
1351 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
tmp.y = |
1352 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[vec]. |
1353 |
|
y : BitstreamGetBits(&bs, length); |
1354 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1355 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1356 |
|
|
1357 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
1358 |
|
|
1359 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1360 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1361 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1362 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
|
} |
|
1363 |
} |
} |
1364 |
else // intra / stuffing / not_coded |
} else // intra / stuffing / not_coded |
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
1365 |
{ |
{ |
1366 |
|
for (vec = 0; vec < 4; ++vec) { |
1367 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1368 |
} |
} |
1369 |
} |
} |
1370 |
|
|
1371 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
1372 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
1373 |
{ |
&& pMB->dquant != NO_CHANGE) { |
1374 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1375 |
|
|
1376 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1377 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1378 |
} |
} |
1379 |
} |
} |
1382 |
} |
} |
1383 |
|
|
1384 |
|
|
1385 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
1386 |
|
HintedMEGet(Encoder * pEnc, |
1387 |
|
int intra) |
1388 |
{ |
{ |
1389 |
HINTINFO * hint; |
HINTINFO * hint; |
1390 |
Bitstream bs; |
Bitstream bs; |
1393 |
|
|
1394 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1395 |
|
|
1396 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1397 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
1398 |
} |
} else { |
|
else |
|
|
{ |
|
1399 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
1400 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
1401 |
} |
} |
1402 |
|
|
1403 |
if (intra) |
if (intra) { |
1404 |
{ |
if (!hint->rawhints) { |
|
if (!hint->rawhints) |
|
|
{ |
|
1405 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1406 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1407 |
} |
} |
1411 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1412 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1413 |
|
|
1414 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1415 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
1416 |
} |
} else { |
|
else |
|
|
{ |
|
1417 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1418 |
} |
} |
1419 |
|
|
1420 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1421 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1422 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1423 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1424 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1425 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1426 |
VECTOR tmp; |
VECTOR tmp; |
1427 |
|
|
1428 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1429 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
1430 |
} |
} else { |
|
else |
|
|
{ |
|
1431 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1432 |
} |
} |
1433 |
|
|
1434 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
1435 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
1436 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
1437 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1438 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1439 |
|
|
1440 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1441 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
1442 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
1443 |
} |
} else { |
|
else |
|
|
{ |
|
1444 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1445 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1446 |
} |
} |
1447 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
1448 |
int vec; |
int vec; |
1449 |
|
|
1450 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1451 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
1452 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
1453 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1454 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1455 |
|
|
1456 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1457 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
1458 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
1459 |
} |
} else { |
|
else |
|
|
{ |
|
1460 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1461 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1462 |
} |
} |
1465 |
} |
} |
1466 |
} |
} |
1467 |
|
|
1468 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
1469 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1470 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1471 |
} |
} |
1472 |
} |
} |
1473 |
|
|
1474 |
|
|
1475 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
1476 |
|
FrameCodeI(Encoder * pEnc, |
1477 |
|
Bitstream * bs, |
1478 |
|
uint32_t * pBits) |
1479 |
{ |
{ |
1480 |
|
|
1481 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1489 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1490 |
|
|
1491 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1492 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
|
1493 |
|
#define DIVX501B481P "DivX501b481p" |
1494 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1495 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1496 |
|
} |
1497 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1498 |
|
|
1499 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1500 |
|
|
1503 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1504 |
|
|
1505 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
1506 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1507 |
{ |
MACROBLOCK *pMB = |
1508 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1509 |
|
|
1510 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1511 |
|
|
1512 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1513 |
|
dct_codes, qcoeff); |
1514 |
|
|
1515 |
start_timer(); |
start_timer(); |
1516 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1517 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1518 |
|
|
1519 |
start_timer(); |
start_timer(); |
1520 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1521 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1522 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1523 |
|
qcoeff[5*64+0]=0; |
1524 |
|
} |
1525 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1526 |
stop_coding_timer(); |
stop_coding_timer(); |
1527 |
} |
} |
1534 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1535 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1536 |
|
|
1537 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
pEnc->last_pframe = pEnc->current->ticks; |
1538 |
{ |
pEnc->last_sync = pEnc->current->ticks; |
1539 |
|
|
1540 |
|
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1541 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1542 |
} |
} |
1543 |
|
|
1546 |
|
|
1547 |
|
|
1548 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1549 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1550 |
|
|
1551 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int |
1552 |
|
FrameCodeP(Encoder * pEnc, |
1553 |
|
Bitstream * bs, |
1554 |
|
uint32_t * pBits, |
1555 |
|
bool force_inter, |
1556 |
|
bool vol_header) |
1557 |
{ |
{ |
1558 |
float fSigma; |
float fSigma; |
1559 |
|
|
1561 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1562 |
|
|
1563 |
int iLimit; |
int iLimit; |
1564 |
uint32_t x, y; |
int x, y, k; |
1565 |
int iSearchRange; |
int iSearchRange; |
1566 |
int bIntra; |
int bIntra; |
1567 |
|
|
1569 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1570 |
|
|
1571 |
start_timer(); |
start_timer(); |
1572 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1573 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height); |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
|
pEnc->current->global_flags & XVID_INTERLACING); |
|
1574 |
stop_edges_timer(); |
stop_edges_timer(); |
1575 |
|
|
1576 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1578 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1579 |
|
|
1580 |
if (!force_inter) |
if (!force_inter) |
1581 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = |
1582 |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
1583 |
|
INTRA_THRESHOLD); |
1584 |
else |
else |
1585 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
1586 |
|
|
1587 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1588 |
start_timer(); |
start_timer(); |
1589 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1590 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1591 |
|
pEnc->mbParam.edged_height, |
1592 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1593 |
stop_inter_timer(); |
stop_inter_timer(); |
1594 |
} |
} |
1595 |
|
|
1596 |
start_timer(); |
start_timer(); |
1597 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
|
{ |
|
1598 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1599 |
} |
if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1600 |
else |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1601 |
{ |
|
1602 |
bIntra = MotionEstimation( |
} else { |
1603 |
&pEnc->mbParam, |
|
1604 |
pEnc->current, |
bIntra = |
1605 |
pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1606 |
&pEnc->vInterH, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
1607 |
iLimit); |
iLimit); |
1608 |
|
|
1609 |
} |
} |
1610 |
stop_motion_timer(); |
stop_motion_timer(); |
1611 |
|
|
1612 |
if (bIntra == 1) |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
|
{ |
|
|
return FrameCodeI(pEnc, bs, pBits); |
|
|
} |
|
1613 |
|
|
1614 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1615 |
|
|
1616 |
if(vol_header) |
if(vol_header) |
1617 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1618 |
|
|
1619 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1620 |
|
|
1621 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1622 |
|
|
1623 |
pEnc->sStat.iTextBits = 0; |
pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = |
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
1624 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1625 |
|
|
1626 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1627 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1628 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
MACROBLOCK *pMB = |
1629 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1630 |
|
|
1631 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1632 |
|
|
1633 |
if (!bIntra) |
if (!bIntra) { |
|
{ |
|
1634 |
start_timer(); |
start_timer(); |
1635 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1636 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
1637 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->current->image, |
1638 |
&pEnc->vInterH, |
dct_codes, pEnc->mbParam.width, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
|
&pEnc->current->image, |
|
|
dct_codes, |
|
|
pEnc->mbParam.width, |
|
1639 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1640 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1641 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1645 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
1646 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
1647 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1648 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1649 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1650 |
|
else if (pEnc->current->quant < 1) |
1651 |
|
pEnc->current->quant = 1; |
1652 |
} |
} |
1653 |
} |
} |
1654 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
1655 |
|
|
1656 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1657 |
|
|
1658 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
if (pMB->mode != MODE_NOT_CODED) |
1659 |
} |
pMB->cbp = |
1660 |
else |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1661 |
{ |
dct_codes, qcoeff); |
1662 |
|
} else { |
1663 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1664 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1665 |
|
dct_codes, qcoeff); |
1666 |
} |
} |
1667 |
|
|
1668 |
start_timer(); |
start_timer(); |
1669 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1670 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1671 |
|
|
1672 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
{ |
|
1673 |
pEnc->sStat.kblks++; |
pEnc->sStat.kblks++; |
1674 |
|
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1675 |
|
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1676 |
|
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1677 |
|
pEnc->sStat.mblks++; |
1678 |
|
} else { |
1679 |
|
pEnc->sStat.ublks++; |
1680 |
} |
} |
1681 |
else if (pMB->cbp || |
|
1682 |
pMB->mvs[0].x || pMB->mvs[0].y || |
start_timer(); |
1683 |
pMB->mvs[1].x || pMB->mvs[1].y || |
|
1684 |
pMB->mvs[2].x || pMB->mvs[2].y || |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1685 |
pMB->mvs[3].x || pMB->mvs[3].y) |
|
1686 |
|
if ((pMB->mode == MODE_NOT_CODED) || |
1687 |
|
(pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
1688 |
|
pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) { |
1689 |
|
|
1690 |
|
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
1691 |
|
|
1692 |
|
int bSkip = 1; |
1693 |
|
pMB->mode = MODE_NOT_CODED; |
1694 |
|
|
1695 |
|
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1696 |
{ |
{ |
1697 |
pEnc->sStat.mblks++; |
int iSAD; |
1698 |
|
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1699 |
|
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1700 |
|
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
1701 |
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
1702 |
|
{ bSkip = 0; |
1703 |
|
break; |
1704 |
} |
} |
1705 |
else |
} |
1706 |
|
if (!bSkip) |
1707 |
{ |
{ |
1708 |
pEnc->sStat.ublks++; |
VECTOR predMV; |
1709 |
|
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1710 |
|
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
1711 |
|
pMB->mode = MODE_INTER; |
1712 |
|
pMB->cbp = 0; |
1713 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1714 |
} |
} |
1715 |
|
else MBSkip(bs); |
1716 |
|
|
1717 |
start_timer(); |
} else { |
1718 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1719 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1720 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1721 |
|
qcoeff[5*64+0]=0; |
1722 |
|
} |
1723 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1724 |
|
} |
1725 |
|
|
1726 |
stop_coding_timer(); |
stop_coding_timer(); |
1727 |
} |
} |
1728 |
} |
} |
1729 |
|
|
1730 |
emms(); |
emms(); |
1731 |
|
|
1732 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1733 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1734 |
} |
} |
1735 |
|
|
1745 |
{ |
{ |
1746 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1747 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1748 |
} |
} else if ((fSigma < iSearchRange / 6) |
|
else if ((fSigma < iSearchRange / 6) |
|
1749 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1750 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1751 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
1756 |
|
|
1757 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1758 |
|
|
1759 |
|
#ifdef BFRAMES |
1760 |
|
/* frame drop code */ |
1761 |
|
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
1762 |
|
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1763 |
|
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
1764 |
|
{ |
1765 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1766 |
|
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1767 |
|
|
1768 |
|
BitstreamReset(bs); |
1769 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1770 |
|
|
1771 |
|
// copy reference frame details into the current frame |
1772 |
|
pEnc->current->quant = pEnc->reference->quant; |
1773 |
|
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1774 |
|
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1775 |
|
pEnc->current->fcode = pEnc->reference->fcode; |
1776 |
|
pEnc->current->bcode = pEnc->reference->bcode; |
1777 |
|
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1778 |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
1779 |
|
|
1780 |
|
} |
1781 |
|
#endif |
1782 |
|
|
1783 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1784 |
|
|
1785 |
|
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) % |
1786 |
|
(int32_t)pEnc->mbParam.fbase; |
1787 |
|
pEnc->last_pframe = pEnc->current->ticks; |
1788 |
|
|
1789 |
return 0; // inter |
return 0; // inter |
1790 |
} |
} |
1791 |
|
|
1792 |
|
|
1793 |
#ifdef BFRAMES |
static __inline void |
1794 |
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
FrameCodeB(Encoder * pEnc, |
1795 |
|
FRAMEINFO * frame, |
1796 |
|
Bitstream * bs, |
1797 |
|
uint32_t * pBits) |
1798 |
{ |
{ |
1799 |
int16_t dct_codes[6*64]; |
int16_t dct_codes[6*64]; |
1800 |
int16_t qcoeff[6*64]; |
int16_t qcoeff[6*64]; |
1801 |
uint32_t x, y; |
uint32_t x, y; |
|
VECTOR forward; |
|
|
VECTOR backward; |
|
1802 |
|
|
1803 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1804 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1805 |
|
|
1806 |
|
#ifdef BFRAMES_DEC_DEBUG |
1807 |
|
FILE *fp; |
1808 |
|
static char first=0; |
1809 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
1810 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1811 |
|
} |
1812 |
|
|
1813 |
|
if (!first){ |
1814 |
|
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1815 |
|
} |
1816 |
|
#endif |
1817 |
|
|
1818 |
// forward |
// forward |
1819 |
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1820 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1821 |
|
pEnc->mbParam.height); |
1822 |
start_timer(); |
start_timer(); |
1823 |
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, |
1824 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1825 |
|
0); |
1826 |
stop_inter_timer(); |
stop_inter_timer(); |
1827 |
|
|
1828 |
// backward |
// backward |
1829 |
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1830 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1831 |
|
pEnc->mbParam.height); |
1832 |
start_timer(); |
start_timer(); |
1833 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1834 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1835 |
|
0); |
1836 |
stop_inter_timer(); |
stop_inter_timer(); |
1837 |
|
|
1838 |
start_timer(); |
start_timer(); |
1839 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1840 |
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase, |
1841 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
pEnc->time_pp, |
1842 |
|
pEnc->reference->mbs, f_ref, |
1843 |
|
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1844 |
|
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1845 |
|
&pEnc->vInterV, &pEnc->vInterHV); |
1846 |
|
|
1847 |
|
|
1848 |
stop_motion_timer(); |
stop_motion_timer(); |
1853 |
}*/ |
}*/ |
1854 |
|
|
1855 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1856 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1857 |
|
|
1858 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1859 |
|
|
1863 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1864 |
|
|
1865 |
|
|
1866 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1867 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1868 |
// reset prediction |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1869 |
|
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
|
forward.x = 0; |
|
|
forward.y = 0; |
|
|
backward.x = 0; |
|
|
backward.y = 0; |
|
|
|
|
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
|
{ |
|
|
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1870 |
|
|
1871 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1872 |
if (mb->mode == MODE_NOT_CODED) |
if (mb->mode == MODE_NOT_CODED) { |
1873 |
{ |
//mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; |
|
mb->mvs[0].x = 0; |
|
|
mb->mvs[0].y = 0; |
|
1874 |
continue; |
continue; |
1875 |
} |
} |
1876 |
|
|
1877 |
|
if (mb->mode != MODE_DIRECT_NONE_MV) { |
1878 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1879 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1880 |
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1881 |
|
&pEnc->vInterV, &pEnc->vInterHV, |
1882 |
dct_codes); |
dct_codes); |
1883 |
|
|
1884 |
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
1885 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
|
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
|
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
|
1886 |
|
|
1887 |
|
mb->cbp = |
1888 |
|
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
1889 |
|
|
1890 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1891 |
mb->cbp == 0 && |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
1892 |
mb->mvs[0].x == 0 && |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
|
mb->mvs[0].y == 0) |
|
|
{ |
|
|
mb->mode = 5; // skipped |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
|
|
{ |
|
|
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
|
|
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
|
|
forward.x = mb->mvs[0].x; |
|
|
forward.y = mb->mvs[0].y; |
|
1893 |
} |
} |
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
|
|
{ |
|
|
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
|
|
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
|
|
backward.x = mb->b_mvs[0].x; |
|
|
backward.y = mb->b_mvs[0].y; |
|
1894 |
} |
} |
1895 |
|
|
1896 |
// printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y); |
#ifdef BFRAMES_DEC_DEBUG |
1897 |
|
BFRAME_DEBUG |
1898 |
|
#endif |
1899 |
start_timer(); |
start_timer(); |
1900 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat); |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1901 |
|
&pEnc->sStat, direction); |
1902 |
stop_coding_timer(); |
stop_coding_timer(); |
1903 |
} |
} |
1904 |
} |
} |
1908 |
// TODO: dynamic fcode/bcode ??? |
// TODO: dynamic fcode/bcode ??? |
1909 |
|
|
1910 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1911 |
|
|
1912 |
|
#ifdef BFRAMES_DEC_DEBUG |
1913 |
|
if (!first){ |
1914 |
|
first=1; |
1915 |
|
if (fp) |
1916 |
|
fclose(fp); |
1917 |
} |
} |
1918 |
#endif |
#endif |
1919 |
|
} |
1920 |
|
|
1921 |
|
|
1922 |
|
/* in case internal output is needed somewhere... */ |
1923 |
|
/* { |
1924 |
|
FILE *filehandle; |
1925 |
|
filehandle=fopen("last-b.pgm","wb"); |
1926 |
|
if (filehandle) |
1927 |
|
{ |
1928 |
|
fprintf(filehandle,"P5\n\n"); // |
1929 |
|
fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height); |
1930 |
|
fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle); |
1931 |
|
fclose(filehandle); |
1932 |
|
} |
1933 |
|
} |
1934 |
|
*/ |