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