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