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