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