1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
|
* Copyright(C) 2002 Michael Militzer |
7 |
|
* |
8 |
* This program is an implementation of a part of one or more MPEG-4 |
* This program is an implementation of a part of one or more MPEG-4 |
9 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
* to use this software module in hardware or software products are |
* to use this software module in hardware or software products are |
28 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
29 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
30 |
* |
* |
31 |
***************************************************************************/ |
* $Id: encoder.c,v 1.81 2002-09-05 21:02:19 chl Exp $ |
|
|
|
|
/**************************************************************************** |
|
|
* |
|
|
* History |
|
|
* |
|
|
* 14.04.2002 added FrameCodeB() |
|
32 |
* |
* |
33 |
* $Id: encoder.c,v 1.32 2002-05-01 13:00:01 suxen_drol Exp $ |
****************************************************************************/ |
|
* |
|
|
***************************************************************************/ |
|
34 |
|
|
35 |
#include <stdlib.h> |
#include <stdlib.h> |
36 |
#include <stdio.h> |
#include <stdio.h> |
37 |
#include <math.h> |
#include <math.h> |
38 |
|
#include <string.h> |
39 |
|
|
40 |
#include "encoder.h" |
#include "encoder.h" |
41 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
54 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
55 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
56 |
|
|
57 |
|
#ifdef _SMP |
58 |
|
#include "motion/smp_motion_est.h" |
59 |
|
#endif |
60 |
|
/***************************************************************************** |
61 |
|
* Local macros |
62 |
|
****************************************************************************/ |
63 |
|
|
64 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
65 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
66 |
|
|
67 |
|
/***************************************************************************** |
68 |
|
* Local function prototypes |
69 |
|
****************************************************************************/ |
70 |
|
|
71 |
|
static int FrameCodeI(Encoder * pEnc, |
72 |
|
Bitstream * bs, |
73 |
|
uint32_t * pBits); |
74 |
|
|
75 |
|
static int FrameCodeP(Encoder * pEnc, |
76 |
|
Bitstream * bs, |
77 |
|
uint32_t * pBits, |
78 |
|
bool force_inter, |
79 |
|
bool vol_header); |
80 |
|
|
81 |
|
/***************************************************************************** |
82 |
|
* Local data |
83 |
|
****************************************************************************/ |
84 |
|
|
85 |
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] = |
|
|
{ |
|
86 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
87 |
}; |
}; |
88 |
|
|
89 |
static int iDQtab[5] = |
static int iDQtab[5] = { |
|
{ |
|
90 |
1, 0, NO_CHANGE, 2, 3 |
1, 0, NO_CHANGE, 2, 3 |
91 |
}; |
}; |
92 |
|
|
93 |
|
|
94 |
void static image_null(IMAGE * image) |
static void __inline |
95 |
|
image_null(IMAGE * image) |
96 |
{ |
{ |
97 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
98 |
} |
} |
99 |
|
|
100 |
|
|
101 |
int encoder_create(XVID_ENC_PARAM * pParam) |
/***************************************************************************** |
102 |
|
* Encoder creation |
103 |
|
* |
104 |
|
* This function creates an Encoder instance, it allocates all necessary |
105 |
|
* image buffers (reference, current) and initialize the internal xvid |
106 |
|
* encoder paremeters according to the XVID_ENC_PARAM input parameter. |
107 |
|
* |
108 |
|
* The code seems to be very long but is very basic, mainly memory allocation |
109 |
|
* and cleaning code. |
110 |
|
* |
111 |
|
* Returned values : |
112 |
|
* - XVID_ERR_OK - no errors |
113 |
|
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
114 |
|
* cleans the structure before exiting. |
115 |
|
* pParam->handle is also set to NULL. |
116 |
|
* |
117 |
|
****************************************************************************/ |
118 |
|
|
119 |
|
int |
120 |
|
encoder_create(XVID_ENC_PARAM * pParam) |
121 |
{ |
{ |
122 |
Encoder *pEnc; |
Encoder *pEnc; |
123 |
uint32_t i; |
int i; |
124 |
|
|
125 |
pParam->handle = NULL; |
pParam->handle = NULL; |
126 |
|
|
131 |
ENC_CHECK(!(pParam->width % 2)); |
ENC_CHECK(!(pParam->width % 2)); |
132 |
ENC_CHECK(!(pParam->height % 2)); |
ENC_CHECK(!(pParam->height % 2)); |
133 |
|
|
134 |
if (pParam->fincr <= 0 || pParam->fbase <= 0) |
/* Fps */ |
135 |
{ |
|
136 |
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
137 |
pParam->fincr = 1; |
pParam->fincr = 1; |
138 |
pParam->fbase = 25; |
pParam->fbase = 25; |
139 |
} |
} |
140 |
|
|
141 |
// simplify the "fincr/fbase" fraction |
/* |
142 |
// (neccessary, since windows supplies us with huge numbers) |
* Simplify the "fincr/fbase" fraction |
143 |
|
* (neccessary, since windows supplies us with huge numbers) |
144 |
|
*/ |
145 |
|
|
146 |
i = pParam->fincr; |
i = pParam->fincr; |
147 |
while (i > 1) |
while (i > 1) { |
148 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
149 |
pParam->fincr /= i; |
pParam->fincr /= i; |
150 |
pParam->fbase /= i; |
pParam->fbase /= i; |
151 |
i = pParam->fincr; |
i = pParam->fincr; |
154 |
i--; |
i--; |
155 |
} |
} |
156 |
|
|
157 |
if (pParam->fbase > 65535) |
if (pParam->fbase > 65535) { |
|
{ |
|
158 |
float div = (float)pParam->fbase / 65535; |
float div = (float)pParam->fbase / 65535; |
159 |
|
|
160 |
pParam->fbase = (int)(pParam->fbase / div); |
pParam->fbase = (int)(pParam->fbase / div); |
161 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
162 |
} |
} |
163 |
|
|
164 |
|
/* Bitrate allocator defaults */ |
165 |
|
|
166 |
if (pParam->rc_bitrate <= 0) |
if (pParam->rc_bitrate <= 0) |
167 |
pParam->rc_bitrate = 900000; |
pParam->rc_bitrate = 900000; |
168 |
|
|
175 |
if (pParam->rc_buffer <= 0) |
if (pParam->rc_buffer <= 0) |
176 |
pParam->rc_buffer = 100; |
pParam->rc_buffer = 100; |
177 |
|
|
178 |
|
/* Max and min quantizers */ |
179 |
|
|
180 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
181 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
182 |
|
|
183 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
184 |
pParam->max_quantizer = 31; |
pParam->max_quantizer = 31; |
185 |
|
|
|
if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
|
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
|
|
|
186 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
187 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
188 |
|
|
189 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
/* 1 keyframe each 10 seconds */ |
190 |
|
|
191 |
|
if (pParam->max_key_interval <= 0) |
192 |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
193 |
|
|
194 |
|
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
195 |
|
if (pEnc == NULL) |
196 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
197 |
|
|
198 |
|
/* Zero the Encoder Structure */ |
199 |
|
|
200 |
|
memset(pEnc, 0, sizeof(Encoder)); |
201 |
|
|
202 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
203 |
|
|
204 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
213 |
pEnc->mbParam.fbase = pParam->fbase; |
pEnc->mbParam.fbase = pParam->fbase; |
214 |
pEnc->mbParam.fincr = pParam->fincr; |
pEnc->mbParam.fincr = pParam->fincr; |
215 |
|
|
216 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
217 |
|
|
218 |
|
#ifdef _SMP |
219 |
|
pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS); |
220 |
|
#endif |
221 |
|
|
222 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
223 |
|
|
224 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
230 |
|
|
231 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
232 |
|
|
233 |
pEnc->current = NULL; |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
234 |
pEnc->reference = NULL; |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
235 |
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
|
236 |
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
if (pEnc->current == NULL || pEnc->reference == NULL) |
237 |
{ |
goto xvid_err_memory1; |
|
if (pEnc->current) xvid_free(pEnc->current); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
238 |
|
|
239 |
/* try to allocate mb memory */ |
/* try to allocate mb memory */ |
240 |
|
|
241 |
pEnc->current->mbs = NULL; |
pEnc->current->mbs = |
242 |
pEnc->reference->mbs = NULL; |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
243 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
244 |
|
pEnc->reference->mbs = |
245 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
246 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
247 |
|
|
248 |
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) |
249 |
(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); |
|
|
} |
|
250 |
|
|
251 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
252 |
|
|
253 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
254 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
255 |
#endif |
#endif |
|
#ifdef BFRAMES |
|
|
image_null(&pEnc->f_refh); |
|
|
image_null(&pEnc->f_refv); |
|
|
image_null(&pEnc->f_refhv); |
|
|
#endif |
|
256 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
257 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
258 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
261 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
262 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
263 |
|
|
264 |
if ( |
#ifdef _DEBUG_PSNR |
265 |
#ifdef _DEBUG |
if (image_create |
266 |
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
267 |
#endif |
pEnc->mbParam.edged_height) < 0) |
268 |
#ifdef BFRAMES |
goto xvid_err_memory3; |
|
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); |
|
|
|
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
// ============================================================================== |
|
|
#ifdef BFRAMES |
|
|
|
|
|
// TODO: handle malloc() == NULL |
|
|
pEnc->max_bframes = pParam->max_bframes; |
|
|
pEnc->bquant_ratio = pParam->bquant_ratio; |
|
|
if (pEnc->max_bframes > 0) |
|
|
{ |
|
|
int n; |
|
|
|
|
|
pEnc->bframes = malloc(pEnc->max_bframes * sizeof(FRAMEINFO *)); |
|
|
|
|
|
for (n = 0; n < pEnc->max_bframes; n++) |
|
|
{ |
|
|
pEnc->bframes[n] = malloc(sizeof(FRAMEINFO)); |
|
|
pEnc->bframes[n]->mbs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
|
|
|
|
|
if (image_create(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
} |
|
|
} |
|
|
pEnc->bframenum_head = 0; |
|
|
pEnc->bframenum_tail = 0; |
|
|
pEnc->flush_bframes = 0; |
|
|
|
|
|
pEnc->mbParam.m_seconds = 0; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
269 |
#endif |
#endif |
270 |
|
if (image_create |
271 |
|
(&pEnc->current->image, pEnc->mbParam.edged_width, |
272 |
// ============================================================================== |
pEnc->mbParam.edged_height) < 0) |
273 |
|
goto xvid_err_memory3; |
274 |
|
if (image_create |
275 |
|
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
276 |
|
pEnc->mbParam.edged_height) < 0) |
277 |
|
goto xvid_err_memory3; |
278 |
|
if (image_create |
279 |
|
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
280 |
|
pEnc->mbParam.edged_height) < 0) |
281 |
|
goto xvid_err_memory3; |
282 |
|
if (image_create |
283 |
|
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
284 |
|
pEnc->mbParam.edged_height) < 0) |
285 |
|
goto xvid_err_memory3; |
286 |
|
if (image_create |
287 |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
288 |
|
pEnc->mbParam.edged_height) < 0) |
289 |
|
goto xvid_err_memory3; |
290 |
|
if (image_create |
291 |
|
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
292 |
|
pEnc->mbParam.edged_height) < 0) |
293 |
|
goto xvid_err_memory3; |
294 |
|
if (image_create |
295 |
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
296 |
|
pEnc->mbParam.edged_height) < 0) |
297 |
|
goto xvid_err_memory3; |
298 |
|
|
299 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
300 |
|
|
301 |
if (pParam->rc_bitrate) |
if (pParam->rc_bitrate) { |
302 |
{ |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
303 |
RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
pParam->rc_reaction_delay_factor, |
304 |
pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, |
pParam->rc_averaging_period, pParam->rc_buffer, |
305 |
|
pParam->fbase * 1000 / pParam->fincr, |
306 |
pParam->max_quantizer, pParam->min_quantizer); |
pParam->max_quantizer, pParam->min_quantizer); |
307 |
} |
} |
308 |
|
|
309 |
init_timer(); |
init_timer(); |
310 |
|
|
311 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
|
} |
|
312 |
|
|
313 |
|
/* |
314 |
|
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
315 |
|
*/ |
316 |
|
|
317 |
|
xvid_err_memory3: |
318 |
|
#ifdef _DEBUG_PSNR |
319 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
320 |
|
pEnc->mbParam.edged_height); |
321 |
|
#endif |
322 |
|
|
323 |
int encoder_destroy(Encoder * pEnc) |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
324 |
{ |
pEnc->mbParam.edged_height); |
325 |
ENC_CHECK(pEnc); |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
326 |
|
pEnc->mbParam.edged_height); |
327 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
328 |
|
pEnc->mbParam.edged_height); |
329 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
330 |
|
pEnc->mbParam.edged_height); |
331 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
332 |
|
pEnc->mbParam.edged_height); |
333 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
334 |
|
pEnc->mbParam.edged_height); |
335 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
336 |
|
pEnc->mbParam.edged_height); |
337 |
|
|
338 |
// ================================================================= |
xvid_err_memory2: |
|
#ifdef BFRAMES |
|
|
if (pEnc->max_bframes > 0) |
|
|
{ |
|
|
int n; |
|
|
for (n = 0; n < pEnc->max_bframes; n++) |
|
|
{ |
|
|
image_destroy(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
free(pEnc->bframes[n]->mbs); |
|
|
free(pEnc->bframes[n]); |
|
|
} |
|
|
free(pEnc->bframes); |
|
|
} |
|
|
#endif BFRAMES |
|
|
//==================================================================== |
|
|
|
|
|
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); |
|
|
#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 |
|
|
#ifdef _DEBUG |
|
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
#endif |
|
339 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
|
xvid_free(pEnc->current); |
|
|
|
|
340 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
|
xvid_free(pEnc->reference); |
|
341 |
|
|
342 |
|
xvid_err_memory1: |
343 |
|
xvid_free(pEnc->current); |
344 |
|
xvid_free(pEnc->reference); |
345 |
xvid_free(pEnc); |
xvid_free(pEnc); |
|
return XVID_ERR_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ================================================================== |
|
|
#ifdef BFRAMES |
|
|
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
|
|
{ |
|
|
uint16_t x, y; |
|
|
Bitstream bs; |
|
|
uint32_t bits; |
|
|
|
|
|
ENC_CHECK(pEnc); |
|
|
ENC_CHECK(pFrame); |
|
|
|
|
|
start_global_timer(); |
|
|
|
|
|
BitstreamInit(&bs, pFrame->bitstream, 0); |
|
|
|
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
// bframe "flush" code |
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
|
|
|
if ((pFrame->image == NULL || pEnc->flush_bframes) && |
|
|
pEnc->bframenum_head < pEnc->bframenum_tail) |
|
|
{ |
|
|
|
|
|
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) --- "); |
|
|
pEnc->bframenum_tail--; |
|
|
SWAP(pEnc->current, pEnc->reference); |
|
|
|
|
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
|
|
|
|
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
|
|
|
|
|
BitstreamPad(&bs); |
|
|
pFrame->length = BitstreamLength(&bs); |
|
|
pFrame->input_consumed = 0; |
|
|
pFrame->intra = 0; |
|
|
return XVID_ERR_OK; |
|
|
} |
|
|
|
|
|
dprintf("--- BFRAME (flush) --- "); |
|
|
FrameCodeB(pEnc, |
|
|
pEnc->bframes[pEnc->bframenum_head], |
|
|
&bs, &bits); |
|
|
pEnc->bframenum_head++; |
|
346 |
|
|
347 |
|
pParam->handle = NULL; |
348 |
|
|
349 |
BitstreamPad(&bs); |
return XVID_ERR_MEMORY; |
|
pFrame->length = BitstreamLength(&bs); |
|
|
pFrame->input_consumed = 0; |
|
|
pFrame->intra = 0; |
|
|
return XVID_ERR_OK; |
|
350 |
} |
} |
351 |
|
|
352 |
if (pFrame->image == NULL) |
/***************************************************************************** |
353 |
{ |
* Encoder destruction |
354 |
pFrame->length = 0; |
* |
355 |
pFrame->input_consumed = 1; |
* This function destroy the entire encoder structure created by a previous |
356 |
pFrame->intra = 0; |
* successful encoder_create call. |
357 |
return XVID_ERR_OK; |
* |
358 |
} |
* Returned values (for now only one returned value) : |
359 |
|
* - XVID_ERR_OK - no errors |
360 |
|
* |
361 |
|
****************************************************************************/ |
362 |
|
|
363 |
if (pEnc->bframenum_head > 0) |
int |
364 |
|
encoder_destroy(Encoder * pEnc) |
365 |
{ |
{ |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
|
|
} |
|
|
|
|
|
pEnc->flush_bframes = 0; |
|
|
|
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
|
|
|
SWAP(pEnc->current, pEnc->reference); |
|
|
|
|
|
pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(0) : pFrame->quant; |
|
|
pEnc->current->global_flags = pFrame->general; |
|
|
pEnc->current->motion_flags = pFrame->motion; |
|
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
|
//@@@ TODO: dyanmic fcode (in both directions) |
|
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
|
|
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
|
366 |
|
|
367 |
start_timer(); |
ENC_CHECK(pEnc); |
|
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
|
|
pFrame->image, pFrame->colorspace)) |
|
|
{ |
|
|
return XVID_ERR_FORMAT; |
|
|
} |
|
|
stop_conv_timer(); |
|
368 |
|
|
369 |
#ifdef _DEBUG |
/* All images, reference, current etc ... */ |
370 |
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
371 |
|
pEnc->mbParam.edged_height); |
372 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
373 |
|
pEnc->mbParam.edged_height); |
374 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
375 |
|
pEnc->mbParam.edged_height); |
376 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
377 |
|
pEnc->mbParam.edged_height); |
378 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
379 |
|
pEnc->mbParam.edged_height); |
380 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
381 |
|
pEnc->mbParam.edged_height); |
382 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
383 |
|
pEnc->mbParam.edged_height); |
384 |
|
|
385 |
|
#ifdef _DEBUG_PSNR |
386 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
387 |
|
pEnc->mbParam.edged_height); |
388 |
#endif |
#endif |
389 |
|
|
390 |
|
/* Encoder structure */ |
391 |
|
xvid_free(pEnc->current->mbs); |
392 |
|
xvid_free(pEnc->current); |
393 |
|
|
394 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
xvid_free(pEnc->reference->mbs); |
395 |
// lumi masking |
xvid_free(pEnc->reference); |
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
|
|
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
|
|
{ |
|
|
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
|
|
|
|
|
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, // 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); |
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
|
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
|
{ |
|
|
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
|
|
} |
|
|
xvid_free(temp_dquants); |
|
|
} |
|
|
|
|
|
|
|
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
// 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 ---"); |
|
|
|
|
|
FrameCodeI(pEnc, &bs, &bits); |
|
|
|
|
|
pFrame->intra = 1; |
|
|
pEnc->flush_bframes = 1; |
|
|
|
|
|
/* note: sequences like "IIBB" decode fine with msfdam but, |
|
|
go screwy with divx5.00 */ |
|
|
} |
|
|
else if (pEnc->bframenum_tail >= pEnc->max_bframes) |
|
|
{ |
|
|
dprintf("--- PFRAME ---"); |
|
|
|
|
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
|
|
pFrame->intra = 0; |
|
|
pEnc->flush_bframes = 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
dprintf("--- BFRAME (store) --- head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail); |
|
|
|
|
|
if (pFrame->bquant < 1) |
|
|
{ |
|
|
pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
|
|
} |
|
|
else |
|
|
{ |
|
|
pEnc->current->quant = pFrame->bquant; |
|
|
} |
|
|
|
|
|
// store frame into bframe buffer & swap ref back to current |
|
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
|
|
SWAP(pEnc->current, pEnc->reference); |
|
|
|
|
|
pEnc->bframenum_tail++; |
|
396 |
|
|
397 |
pFrame->intra = 0; |
xvid_free(pEnc); |
|
pFrame->length = 0; |
|
|
pFrame->input_consumed = 1; |
|
398 |
|
|
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
|
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
|
|
{ |
|
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
399 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
400 |
} |
} |
401 |
|
|
|
BitstreamPad(&bs); |
|
|
pFrame->length = BitstreamLength(&bs); |
|
402 |
|
|
403 |
if (pResult) |
void inc_frame_num(Encoder * pEnc) |
404 |
{ |
{ |
|
pResult->quant = pEnc->current->quant; |
|
|
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
|
|
pResult->kblks = pEnc->sStat.kblks; |
|
|
pResult->mblks = pEnc->sStat.mblks; |
|
|
pResult->ublks = pEnc->sStat.ublks; |
|
|
} |
|
|
|
|
|
#ifdef _DEBUG |
|
|
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.width, pEnc->mbParam.height); |
|
|
|
|
|
sprintf(temp, "PSNR: %f\n", psnr); |
|
|
DEBUG(temp); |
|
|
#endif |
|
|
|
|
|
if (pFrame->quant == 0) |
|
|
{ |
|
|
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
|
|
} |
|
|
|
|
|
pEnc->iFrameNum++; |
|
405 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
406 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
407 |
{ |
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
408 |
} |
} |
|
pFrame->input_consumed = 1; |
|
409 |
|
|
410 |
stop_global_timer(); |
/***************************************************************************** |
411 |
write_timer(); |
* "original" IP frame encoder entry point |
412 |
|
* |
413 |
return XVID_ERR_OK; |
* Returned values : |
414 |
} |
* - XVID_ERR_OK - no errors |
415 |
// ================================================================== |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
416 |
#else |
* format |
417 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
****************************************************************************/ |
418 |
|
|
419 |
|
int |
420 |
|
encoder_encode(Encoder * pEnc, |
421 |
|
XVID_ENC_FRAME * pFrame, |
422 |
|
XVID_ENC_STATS * pResult) |
423 |
{ |
{ |
424 |
uint16_t x, y; |
uint16_t x, y; |
425 |
Bitstream bs; |
Bitstream bs; |
426 |
uint32_t bits; |
uint32_t bits; |
427 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
428 |
#ifdef _DEBUG |
|
429 |
|
#ifdef _DEBUG_PSNR |
430 |
float psnr; |
float psnr; |
431 |
uint8_t temp[100]; |
uint8_t temp[128]; |
432 |
#endif |
#endif |
433 |
|
|
434 |
start_global_timer(); |
start_global_timer(); |
442 |
|
|
443 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
444 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
445 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
446 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
447 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
448 |
|
|
449 |
start_timer(); |
start_timer(); |
450 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input |
451 |
pFrame->image, pFrame->colorspace)) |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
452 |
{ |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
453 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
454 |
stop_conv_timer(); |
stop_conv_timer(); |
455 |
|
|
456 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
457 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
458 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
459 |
#endif |
#endif |
460 |
|
|
461 |
EMMS(); |
emms(); |
462 |
|
|
463 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
464 |
|
|
465 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
466 |
{ |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
467 |
pEnc->current->quant = RateControlGetQ(0); |
} else { |
|
} |
|
|
else |
|
|
{ |
|
468 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
469 |
} |
} |
470 |
|
|
471 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
472 |
{ |
int *temp_dquants = |
473 |
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 * |
474 |
|
pEnc->mbParam.mb_height * sizeof(int), |
475 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
CACHE_LINE); |
476 |
pEnc->mbParam.edged_width, // stride |
|
477 |
temp_dquants, |
pEnc->current->quant = |
478 |
pEnc->current->quant, |
adaptive_quantization(pEnc->current->image.y, |
479 |
pEnc->current->quant, // min_quant |
pEnc->mbParam.edged_width, temp_dquants, |
480 |
2*pEnc->current->quant, // max_quant |
pEnc->current->quant, pEnc->current->quant, |
481 |
|
2 * pEnc->current->quant, |
482 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
483 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
484 |
|
|
485 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
486 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
487 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
488 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
489 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
490 |
|
|
491 |
|
|
492 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
493 |
|
|
494 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
495 |
} |
} |
496 |
|
|
497 |
|
#undef OFFSET |
498 |
|
} |
499 |
|
|
500 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
501 |
} |
} |
502 |
|
|
504 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
505 |
write_vol_header = 1; |
write_vol_header = 1; |
506 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
507 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
508 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
int matrix1_changed, matrix2_changed; |
|
int ret1, ret2; |
|
509 |
|
|
510 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
511 |
|
|
512 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
513 |
write_vol_header = 1; |
write_vol_header = 1; |
516 |
|
|
517 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
518 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
519 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
520 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
521 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
522 |
} |
} else { |
523 |
else { |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
524 |
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()); |
|
525 |
} |
} |
526 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
527 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
528 |
} |
} |
529 |
|
|
530 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
531 |
{ |
if ((pEnc->iFrameNum == 0) |
532 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->iMaxKeyInterval > 0) |
533 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
|
|
|
534 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
535 |
else |
} else { |
536 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
537 |
} |
} |
538 |
else |
} else { |
539 |
{ |
if (pFrame->intra == 1) { |
|
if (pFrame->intra == 1) |
|
540 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
541 |
else |
} else { |
542 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
543 |
} |
} |
544 |
|
|
545 |
|
} |
546 |
|
|
547 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
548 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
549 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
550 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
551 |
|
|
552 |
if (pResult) |
if (pResult) { |
|
{ |
|
553 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
554 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
555 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
557 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
558 |
} |
} |
559 |
|
|
560 |
EMMS(); |
emms(); |
|
|
|
|
if (pFrame->quant == 0) |
|
|
{ |
|
|
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
|
|
} |
|
561 |
|
|
562 |
#ifdef _DEBUG |
if (pFrame->quant == 0) { |
563 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
564 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pFrame->length, pFrame->intra); |
565 |
|
} |
566 |
|
#ifdef _DEBUG_PSNR |
567 |
|
psnr = |
568 |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
569 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
570 |
|
pEnc->mbParam.height); |
571 |
|
|
572 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
573 |
DEBUG(temp); |
DEBUG(temp); |
574 |
#endif |
#endif |
575 |
|
|
576 |
|
inc_frame_num(pEnc); |
577 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
578 |
|
|
579 |
stop_global_timer(); |
stop_global_timer(); |
581 |
|
|
582 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
583 |
} |
} |
|
#endif |
|
584 |
|
|
585 |
|
|
586 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
587 |
|
CodeIntraMB(Encoder * pEnc, |
588 |
|
MACROBLOCK * pMB) |
589 |
|
{ |
590 |
|
|
591 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
592 |
|
|
597 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
598 |
|
|
599 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
600 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
601 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
602 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
603 |
|
|
604 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
605 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
606 |
|
if (pEnc->current->quant < 1) |
607 |
|
pEnc->current->quant = 1; |
608 |
} |
} |
609 |
} |
} |
610 |
|
|
615 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
616 |
#define MODEBITS 5 |
#define MODEBITS 5 |
617 |
|
|
618 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
619 |
|
HintedMESet(Encoder * pEnc, |
620 |
|
int *intra) |
621 |
{ |
{ |
622 |
HINTINFO * hint; |
HINTINFO * hint; |
623 |
Bitstream bs; |
Bitstream bs; |
626 |
|
|
627 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
628 |
|
|
629 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
630 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
631 |
} |
} else { |
|
else |
|
|
{ |
|
632 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
633 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
634 |
} |
} |
635 |
|
|
636 |
if (*intra) |
if (*intra) { |
|
{ |
|
637 |
return; |
return; |
638 |
} |
} |
639 |
|
|
640 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = |
641 |
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
642 |
|
FCODEBITS); |
643 |
|
|
644 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
645 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
646 |
|
|
647 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
648 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
649 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
650 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
651 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
652 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
653 |
VECTOR pred[4]; |
VECTOR pred; |
654 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
655 |
int vec; |
int vec; |
656 |
|
|
657 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = |
658 |
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
659 |
|
MODEBITS); |
660 |
|
|
661 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
662 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
663 |
|
|
664 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
665 |
{ |
tmp.x = |
666 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
667 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
length); |
668 |
|
tmp.y = |
669 |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
670 |
|
length); |
671 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
672 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
673 |
|
|
674 |
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); |
675 |
|
|
676 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
677 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
678 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
679 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
680 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
681 |
} |
} |
682 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
683 |
else if (pMB->mode == MODE_INTER4V) |
for (vec = 0; vec < 4; ++vec) { |
684 |
{ |
tmp.x = |
685 |
for (vec=0 ; vec<4 ; ++vec) |
(hint->rawhints) ? bhint->mvs[vec]. |
686 |
{ |
x : BitstreamGetBits(&bs, length); |
687 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
tmp.y = |
688 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[vec]. |
689 |
|
y : BitstreamGetBits(&bs, length); |
690 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
691 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
692 |
|
|
693 |
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); |
694 |
|
|
695 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
696 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
697 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
698 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
699 |
} |
} |
700 |
} |
} else // intra / stuffing / not_coded |
|
else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
701 |
{ |
{ |
702 |
|
for (vec = 0; vec < 4; ++vec) { |
703 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
704 |
} |
} |
705 |
} |
} |
706 |
|
|
707 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
708 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
709 |
{ |
&& pMB->dquant != NO_CHANGE) { |
710 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
711 |
|
|
712 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
713 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
714 |
} |
} |
715 |
} |
} |
718 |
} |
} |
719 |
|
|
720 |
|
|
721 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
722 |
|
HintedMEGet(Encoder * pEnc, |
723 |
|
int intra) |
724 |
{ |
{ |
725 |
HINTINFO * hint; |
HINTINFO * hint; |
726 |
Bitstream bs; |
Bitstream bs; |
729 |
|
|
730 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
731 |
|
|
732 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
733 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
734 |
} |
} else { |
|
else |
|
|
{ |
|
735 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
736 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
737 |
} |
} |
738 |
|
|
739 |
if (intra) |
if (intra) { |
740 |
{ |
if (!hint->rawhints) { |
|
if (!hint->rawhints) |
|
|
{ |
|
741 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
742 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
743 |
} |
} |
747 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
748 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
749 |
|
|
750 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
751 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
752 |
} |
} else { |
|
else |
|
|
{ |
|
753 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
754 |
} |
} |
755 |
|
|
756 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
757 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
758 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
759 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
760 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
761 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
762 |
VECTOR tmp; |
VECTOR tmp; |
763 |
|
|
764 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
765 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
766 |
} |
} else { |
|
else |
|
|
{ |
|
767 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
768 |
} |
} |
769 |
|
|
770 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
771 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
772 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
773 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
774 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
775 |
|
|
776 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
777 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
778 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
779 |
} |
} else { |
|
else |
|
|
{ |
|
780 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
781 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
782 |
} |
} |
783 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
784 |
int vec; |
int vec; |
785 |
|
|
786 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
787 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
788 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
789 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
790 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
791 |
|
|
792 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
793 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
794 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
795 |
} |
} else { |
|
else |
|
|
{ |
|
796 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
797 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
798 |
} |
} |
801 |
} |
} |
802 |
} |
} |
803 |
|
|
804 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
805 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
806 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
807 |
} |
} |
808 |
} |
} |
809 |
|
|
810 |
|
|
811 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
812 |
|
FrameCodeI(Encoder * pEnc, |
813 |
|
Bitstream * bs, |
814 |
|
uint32_t * pBits) |
815 |
{ |
{ |
816 |
|
|
817 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
825 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
826 |
|
|
827 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
828 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
|
829 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
830 |
|
|
831 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
832 |
|
|
835 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
836 |
|
|
837 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
838 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
839 |
{ |
MACROBLOCK *pMB = |
840 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
841 |
|
|
842 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
843 |
|
|
844 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
845 |
|
dct_codes, qcoeff); |
846 |
|
|
847 |
start_timer(); |
start_timer(); |
848 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
849 |
stop_prediction_timer(); |
stop_prediction_timer(); |
850 |
|
|
851 |
start_timer(); |
start_timer(); |
852 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
853 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
854 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
855 |
|
qcoeff[5*64+0]=0; |
856 |
|
} |
857 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
858 |
stop_coding_timer(); |
stop_coding_timer(); |
859 |
} |
} |
866 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
867 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
868 |
|
|
869 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
870 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
871 |
} |
} |
872 |
|
|
876 |
|
|
877 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
878 |
|
|
879 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int |
880 |
|
FrameCodeP(Encoder * pEnc, |
881 |
|
Bitstream * bs, |
882 |
|
uint32_t * pBits, |
883 |
|
bool force_inter, |
884 |
|
bool vol_header) |
885 |
{ |
{ |
886 |
float fSigma; |
float fSigma; |
887 |
|
|
889 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
890 |
|
|
891 |
int iLimit; |
int iLimit; |
892 |
uint32_t x, y; |
int x, y, k; |
893 |
int iSearchRange; |
int iSearchRange; |
894 |
int bIntra; |
int bIntra; |
895 |
|
|
897 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
898 |
|
|
899 |
start_timer(); |
start_timer(); |
900 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
901 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height, |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
902 |
pEnc->current->global_flags & XVID_INTERLACING); |
pEnc->current->global_flags & XVID_INTERLACING); |
903 |
stop_edges_timer(); |
stop_edges_timer(); |
904 |
|
|
907 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
908 |
|
|
909 |
if (!force_inter) |
if (!force_inter) |
910 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = |
911 |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
912 |
|
INTRA_THRESHOLD); |
913 |
else |
else |
914 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
915 |
|
|
916 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
917 |
start_timer(); |
start_timer(); |
918 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
919 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
920 |
|
pEnc->mbParam.edged_height, |
921 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
922 |
stop_inter_timer(); |
stop_inter_timer(); |
923 |
} |
} |
924 |
|
|
925 |
start_timer(); |
start_timer(); |
926 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
|
{ |
|
927 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
928 |
} |
} else { |
929 |
|
|
930 |
|
#ifdef _SMP |
931 |
|
if (pEnc->mbParam.num_threads > 1) |
932 |
|
bIntra = |
933 |
|
SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
934 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
935 |
|
iLimit); |
936 |
else |
else |
937 |
{ |
#endif |
938 |
bIntra = MotionEstimation( |
bIntra = |
939 |
&pEnc->mbParam, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
940 |
pEnc->current, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
pEnc->reference, |
|
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
941 |
iLimit); |
iLimit); |
942 |
|
|
943 |
} |
} |
944 |
stop_motion_timer(); |
stop_motion_timer(); |
945 |
|
|
946 |
if (bIntra == 1) |
if (bIntra == 1) { |
|
{ |
|
947 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
948 |
} |
} |
949 |
|
|
952 |
if(vol_header) |
if(vol_header) |
953 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
954 |
|
|
955 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
956 |
|
|
957 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
958 |
|
|
961 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
962 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
963 |
|
|
964 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
965 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
966 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
MACROBLOCK *pMB = |
967 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
968 |
|
|
969 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
970 |
|
|
971 |
if (!bIntra) |
if (!bIntra) { |
|
{ |
|
972 |
start_timer(); |
start_timer(); |
973 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
974 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
975 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->current->image, |
976 |
&pEnc->vInterH, |
dct_codes, pEnc->mbParam.width, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
|
&pEnc->current->image, |
|
|
dct_codes, |
|
|
pEnc->mbParam.width, |
|
977 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
978 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
979 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
983 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
984 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
985 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
986 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
987 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
988 |
|
else if (pEnc->current->quant < 1) |
989 |
|
pEnc->current->quant = 1; |
990 |
} |
} |
991 |
} |
} |
992 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
993 |
|
|
994 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
995 |
|
|
996 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
pMB->cbp = |
997 |
} |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
998 |
else |
dct_codes, qcoeff); |
999 |
{ |
} else { |
1000 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1001 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1002 |
} |
dct_codes, qcoeff); |
1003 |
|
|
1004 |
start_timer(); |
start_timer(); |
1005 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1006 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1007 |
|
} |
1008 |
|
|
1009 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
{ |
|
1010 |
pEnc->sStat.kblks++; |
pEnc->sStat.kblks++; |
1011 |
} |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1012 |
else if (pMB->cbp || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1013 |
pMB->mvs[0].x || pMB->mvs[0].y || |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
|
pMB->mvs[1].x || pMB->mvs[1].y || |
|
|
pMB->mvs[2].x || pMB->mvs[2].y || |
|
|
pMB->mvs[3].x || pMB->mvs[3].y) |
|
|
{ |
|
1014 |
pEnc->sStat.mblks++; |
pEnc->sStat.mblks++; |
1015 |
} |
} else { |
|
else |
|
|
{ |
|
1016 |
pEnc->sStat.ublks++; |
pEnc->sStat.ublks++; |
1017 |
} |
} |
1018 |
|
|
1019 |
start_timer(); |
start_timer(); |
1020 |
|
|
1021 |
|
/* Finished processing the MB, now check if to CODE or SKIP */ |
1022 |
|
|
1023 |
|
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
1024 |
|
pMB->mvs[0].y == 0) { |
1025 |
|
|
1026 |
|
MBSkip(bs); /* without B-frames, no precautions are needed */ |
1027 |
|
|
1028 |
|
} |
1029 |
|
else { |
1030 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) { |
1031 |
|
pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1032 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1033 |
|
qcoeff[5*64+0]=0; |
1034 |
|
} |
1035 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1036 |
|
} |
1037 |
|
|
1038 |
stop_coding_timer(); |
stop_coding_timer(); |
1039 |
} |
} |
1040 |
} |
} |
1041 |
|
|
1042 |
emms(); |
emms(); |
1043 |
|
|
1044 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1045 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1046 |
} |
} |
1047 |
|
|
1057 |
{ |
{ |
1058 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1059 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1060 |
} |
} else if ((fSigma < iSearchRange / 6) |
|
else if ((fSigma < iSearchRange / 6) |
|
1061 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1062 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1063 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
1071 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1072 |
|
|
1073 |
return 0; // inter |
return 0; // inter |
|
} |
|
|
|
|
|
|
|
|
#ifdef BFRAMES |
|
|
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
|
|
{ |
|
|
int16_t dct_codes[6*64]; |
|
|
int16_t qcoeff[6*64]; |
|
|
uint32_t x, y; |
|
|
VECTOR forward; |
|
|
VECTOR backward; |
|
1074 |
|
|
|
IMAGE *f_ref = &pEnc->reference->image; |
|
|
IMAGE *b_ref = &pEnc->current->image; |
|
|
|
|
|
// forward |
|
|
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
|
|
start_timer(); |
|
|
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
|
|
stop_inter_timer(); |
|
|
|
|
|
// backward |
|
|
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
|
|
start_timer(); |
|
|
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
|
|
stop_inter_timer(); |
|
|
|
|
|
start_timer(); |
|
|
MotionEstimationBVOP(&pEnc->mbParam, frame, |
|
|
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
|
|
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
|
|
|
|
|
|
|
|
stop_motion_timer(); |
|
|
|
|
|
/*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
|
|
{ |
|
|
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
|
|
}*/ |
|
|
|
|
|
frame->coding_type = B_VOP; |
|
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
|
|
|
|
|
*pBits = BitstreamPos(bs); |
|
|
|
|
|
pEnc->sStat.iTextBits = 0; |
|
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
|
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
|
|
|
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
|
|
{ |
|
|
// reset prediction |
|
|
|
|
|
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]; |
|
|
|
|
|
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
|
|
if (mb->mode == MODE_NOT_CODED) |
|
|
{ |
|
|
mb->mvs[0].x = 0; |
|
|
mb->mvs[0].y = 0; |
|
|
continue; |
|
1075 |
} |
} |
|
|
|
|
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
|
|
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
|
|
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
|
dct_codes); |
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
|
|
mb->cbp == 0 && |
|
|
mb->mvs[0].x == 0 && |
|
|
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; |
|
|
} |
|
|
|
|
|
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; |
|
|
} |
|
|
|
|
|
// 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); |
|
|
|
|
|
start_timer(); |
|
|
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat); |
|
|
stop_coding_timer(); |
|
|
} |
|
|
} |
|
|
|
|
|
emms(); |
|
|
|
|
|
// TODO: dynamic fcode/bcode ??? |
|
|
|
|
|
*pBits = BitstreamPos(bs) - *pBits; |
|
|
} |
|
|
#endif |
|