Parent Directory
|
Revision Log
Revision 875 -
(view)
(download)
Original Path: trunk/xvidcore/src/encoder.c
1 : | edgomez | 194 | /***************************************************************************** |
2 : | edgomez | 145 | * |
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | edgomez | 851 | * - Encoder main module - |
5 : | edgomez | 145 | * |
6 : | edgomez | 851 | * 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 : | edgomez | 402 | * |
15 : | edgomez | 851 | * 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 : | edgomez | 145 | * 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 | 875 | * $Id: encoder.c,v 1.95 2003-02-19 21:30:52 edgomez Exp $ |
30 : | edgomez | 405 | * |
31 : | edgomez | 194 | ****************************************************************************/ |
32 : | suxen_drol | 118 | |
33 : | Isibaar | 3 | #include <stdlib.h> |
34 : | #include <stdio.h> | ||
35 : | #include <math.h> | ||
36 : | edgomez | 198 | #include <string.h> |
37 : | Isibaar | 3 | |
38 : | #include "encoder.h" | ||
39 : | #include "prediction/mbprediction.h" | ||
40 : | #include "global.h" | ||
41 : | #include "utils/timer.h" | ||
42 : | #include "image/image.h" | ||
43 : | edgomez | 851 | #include "image/font.h" |
44 : | #include "motion/sad.h" | ||
45 : | suxen_drol | 152 | #include "motion/motion.h" |
46 : | Isibaar | 3 | #include "bitstream/cbp.h" |
47 : | #include "utils/mbfunctions.h" | ||
48 : | #include "bitstream/bitstream.h" | ||
49 : | #include "bitstream/mbcoding.h" | ||
50 : | #include "utils/ratecontrol.h" | ||
51 : | #include "utils/emms.h" | ||
52 : | #include "bitstream/mbcoding.h" | ||
53 : | #include "quant/adapt_quant.h" | ||
54 : | Isibaar | 4 | #include "quant/quant_matrix.h" |
55 : | Isibaar | 41 | #include "utils/mem_align.h" |
56 : | Isibaar | 3 | |
57 : | edgomez | 194 | /***************************************************************************** |
58 : | * Local macros | ||
59 : | ****************************************************************************/ | ||
60 : | |||
61 : | Isibaar | 3 | #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
62 : | edgomez | 851 | #define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
63 : | Isibaar | 3 | |
64 : | edgomez | 194 | /***************************************************************************** |
65 : | * Local function prototypes | ||
66 : | ****************************************************************************/ | ||
67 : | |||
68 : | edgomez | 192 | static int FrameCodeI(Encoder * pEnc, |
69 : | edgomez | 195 | Bitstream * bs, |
70 : | uint32_t * pBits); | ||
71 : | Isibaar | 3 | |
72 : | edgomez | 192 | static int FrameCodeP(Encoder * pEnc, |
73 : | edgomez | 195 | Bitstream * bs, |
74 : | uint32_t * pBits, | ||
75 : | bool force_inter, | ||
76 : | bool vol_header); | ||
77 : | Isibaar | 3 | |
78 : | edgomez | 851 | static void FrameCodeB(Encoder * pEnc, |
79 : | FRAMEINFO * frame, | ||
80 : | Bitstream * bs, | ||
81 : | uint32_t * pBits); | ||
82 : | |||
83 : | edgomez | 194 | /***************************************************************************** |
84 : | * Local data | ||
85 : | ****************************************************************************/ | ||
86 : | |||
87 : | edgomez | 195 | static int DQtab[4] = { |
88 : | Isibaar | 3 | -1, -2, 1, 2 |
89 : | }; | ||
90 : | |||
91 : | edgomez | 195 | static int iDQtab[5] = { |
92 : | Isibaar | 3 | 1, 0, NO_CHANGE, 2, 3 |
93 : | }; | ||
94 : | |||
95 : | |||
96 : | edgomez | 194 | /***************************************************************************** |
97 : | * Encoder creation | ||
98 : | * | ||
99 : | * This function creates an Encoder instance, it allocates all necessary | ||
100 : | edgomez | 851 | * image buffers (reference, current and bframes) and initialize the internal |
101 : | * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. | ||
102 : | edgomez | 194 | * |
103 : | * The code seems to be very long but is very basic, mainly memory allocation | ||
104 : | * and cleaning code. | ||
105 : | * | ||
106 : | * Returned values : | ||
107 : | * - XVID_ERR_OK - no errors | ||
108 : | * - XVID_ERR_MEMORY - the libc could not allocate memory, the function | ||
109 : | * cleans the structure before exiting. | ||
110 : | * pParam->handle is also set to NULL. | ||
111 : | * | ||
112 : | ****************************************************************************/ | ||
113 : | |||
114 : | int | ||
115 : | encoder_create(XVID_ENC_PARAM * pParam) | ||
116 : | Isibaar | 3 | { |
117 : | edgomez | 13 | Encoder *pEnc; |
118 : | suxen_drol | 229 | int i; |
119 : | Isibaar | 3 | |
120 : | edgomez | 13 | pParam->handle = NULL; |
121 : | Isibaar | 3 | |
122 : | edgomez | 13 | ENC_CHECK(pParam); |
123 : | Isibaar | 3 | |
124 : | edgomez | 13 | ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
125 : | ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); | ||
126 : | ENC_CHECK(!(pParam->width % 2)); | ||
127 : | ENC_CHECK(!(pParam->height % 2)); | ||
128 : | Isibaar | 3 | |
129 : | edgomez | 192 | /* Fps */ |
130 : | |||
131 : | edgomez | 195 | if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
132 : | Isibaar | 3 | pParam->fincr = 1; |
133 : | pParam->fbase = 25; | ||
134 : | } | ||
135 : | |||
136 : | edgomez | 192 | /* |
137 : | * Simplify the "fincr/fbase" fraction | ||
138 : | * (neccessary, since windows supplies us with huge numbers) | ||
139 : | */ | ||
140 : | Isibaar | 3 | |
141 : | i = pParam->fincr; | ||
142 : | edgomez | 195 | while (i > 1) { |
143 : | if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { | ||
144 : | Isibaar | 3 | pParam->fincr /= i; |
145 : | pParam->fbase /= i; | ||
146 : | i = pParam->fincr; | ||
147 : | continue; | ||
148 : | } | ||
149 : | i--; | ||
150 : | } | ||
151 : | |||
152 : | edgomez | 195 | if (pParam->fbase > 65535) { |
153 : | float div = (float) pParam->fbase / 65535; | ||
154 : | |||
155 : | pParam->fbase = (int) (pParam->fbase / div); | ||
156 : | pParam->fincr = (int) (pParam->fincr / div); | ||
157 : | Isibaar | 3 | } |
158 : | |||
159 : | edgomez | 192 | /* Bitrate allocator defaults */ |
160 : | |||
161 : | h | 121 | if (pParam->rc_bitrate <= 0) |
162 : | pParam->rc_bitrate = 900000; | ||
163 : | Isibaar | 3 | |
164 : | h | 121 | if (pParam->rc_reaction_delay_factor <= 0) |
165 : | pParam->rc_reaction_delay_factor = 16; | ||
166 : | Isibaar | 3 | |
167 : | h | 121 | if (pParam->rc_averaging_period <= 0) |
168 : | pParam->rc_averaging_period = 100; | ||
169 : | |||
170 : | if (pParam->rc_buffer <= 0) | ||
171 : | pParam->rc_buffer = 100; | ||
172 : | |||
173 : | edgomez | 192 | /* Max and min quantizers */ |
174 : | |||
175 : | edgomez | 13 | if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
176 : | Isibaar | 3 | pParam->min_quantizer = 1; |
177 : | |||
178 : | edgomez | 13 | if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
179 : | Isibaar | 3 | pParam->max_quantizer = 31; |
180 : | |||
181 : | edgomez | 13 | if (pParam->max_quantizer < pParam->min_quantizer) |
182 : | Isibaar | 3 | pParam->max_quantizer = pParam->min_quantizer; |
183 : | |||
184 : | edgomez | 195 | /* 1 keyframe each 10 seconds */ |
185 : | edgomez | 192 | |
186 : | edgomez | 325 | if (pParam->max_key_interval <= 0) |
187 : | edgomez | 192 | pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
188 : | |||
189 : | pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); | ||
190 : | if (pEnc == NULL) | ||
191 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
192 : | |||
193 : | edgomez | 198 | /* Zero the Encoder Structure */ |
194 : | |||
195 : | memset(pEnc, 0, sizeof(Encoder)); | ||
196 : | |||
197 : | Isibaar | 3 | /* Fill members of Encoder structure */ |
198 : | |||
199 : | edgomez | 13 | pEnc->mbParam.width = pParam->width; |
200 : | pEnc->mbParam.height = pParam->height; | ||
201 : | Isibaar | 3 | |
202 : | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; | ||
203 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
204 : | |||
205 : | edgomez | 195 | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
206 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
207 : | Isibaar | 3 | |
208 : | suxen_drol | 152 | pEnc->mbParam.fbase = pParam->fbase; |
209 : | pEnc->mbParam.fincr = pParam->fincr; | ||
210 : | |||
211 : | Isibaar | 208 | pEnc->mbParam.m_quant_type = H263_QUANT; |
212 : | |||
213 : | edgomez | 851 | pEnc->fMvPrevSigma = -1; |
214 : | Isibaar | 3 | |
215 : | /* Fill rate control parameters */ | ||
216 : | |||
217 : | h | 121 | pEnc->bitrate = pParam->rc_bitrate; |
218 : | Isibaar | 3 | |
219 : | edgomez | 851 | pEnc->iFrameNum = -1; |
220 : | pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; | ||
221 : | Isibaar | 3 | |
222 : | suxen_drol | 136 | /* try to allocate frame memory */ |
223 : | Isibaar | 3 | |
224 : | edgomez | 192 | pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
225 : | pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); | ||
226 : | Isibaar | 3 | |
227 : | edgomez | 195 | if (pEnc->current == NULL || pEnc->reference == NULL) |
228 : | edgomez | 192 | goto xvid_err_memory1; |
229 : | |||
230 : | suxen_drol | 136 | /* try to allocate mb memory */ |
231 : | Isibaar | 3 | |
232 : | edgomez | 195 | pEnc->current->mbs = |
233 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
234 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
235 : | pEnc->reference->mbs = | ||
236 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
237 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
238 : | suxen_drol | 136 | |
239 : | edgomez | 192 | if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
240 : | goto xvid_err_memory2; | ||
241 : | suxen_drol | 136 | |
242 : | /* try to allocate image memory */ | ||
243 : | |||
244 : | edgomez | 851 | if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
245 : | image_null(&pEnc->sOriginal); | ||
246 : | |||
247 : | image_null(&pEnc->f_refh); | ||
248 : | image_null(&pEnc->f_refv); | ||
249 : | image_null(&pEnc->f_refhv); | ||
250 : | |||
251 : | suxen_drol | 136 | image_null(&pEnc->current->image); |
252 : | image_null(&pEnc->reference->image); | ||
253 : | image_null(&pEnc->vInterH); | ||
254 : | image_null(&pEnc->vInterV); | ||
255 : | edgomez | 851 | image_null(&pEnc->vInterVf); |
256 : | suxen_drol | 136 | image_null(&pEnc->vInterHV); |
257 : | edgomez | 851 | image_null(&pEnc->vInterHVf); |
258 : | edgomez | 192 | |
259 : | edgomez | 851 | if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
260 : | { if (image_create | ||
261 : | (&pEnc->sOriginal, pEnc->mbParam.edged_width, | ||
262 : | pEnc->mbParam.edged_height) < 0) | ||
263 : | goto xvid_err_memory3; | ||
264 : | } | ||
265 : | |||
266 : | edgomez | 195 | if (image_create |
267 : | edgomez | 851 | (&pEnc->f_refh, pEnc->mbParam.edged_width, |
268 : | edgomez | 195 | pEnc->mbParam.edged_height) < 0) |
269 : | edgomez | 192 | goto xvid_err_memory3; |
270 : | edgomez | 195 | if (image_create |
271 : | edgomez | 851 | (&pEnc->f_refv, pEnc->mbParam.edged_width, |
272 : | pEnc->mbParam.edged_height) < 0) | ||
273 : | goto xvid_err_memory3; | ||
274 : | if (image_create | ||
275 : | (&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
276 : | pEnc->mbParam.edged_height) < 0) | ||
277 : | goto xvid_err_memory3; | ||
278 : | |||
279 : | if (image_create | ||
280 : | edgomez | 195 | (&pEnc->current->image, pEnc->mbParam.edged_width, |
281 : | pEnc->mbParam.edged_height) < 0) | ||
282 : | edgomez | 192 | goto xvid_err_memory3; |
283 : | edgomez | 195 | if (image_create |
284 : | (&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
285 : | pEnc->mbParam.edged_height) < 0) | ||
286 : | edgomez | 192 | goto xvid_err_memory3; |
287 : | edgomez | 195 | if (image_create |
288 : | (&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
289 : | pEnc->mbParam.edged_height) < 0) | ||
290 : | edgomez | 192 | goto xvid_err_memory3; |
291 : | edgomez | 195 | if (image_create |
292 : | (&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
293 : | pEnc->mbParam.edged_height) < 0) | ||
294 : | edgomez | 192 | goto xvid_err_memory3; |
295 : | edgomez | 195 | if (image_create |
296 : | edgomez | 851 | (&pEnc->vInterVf, pEnc->mbParam.edged_width, |
297 : | pEnc->mbParam.edged_height) < 0) | ||
298 : | goto xvid_err_memory3; | ||
299 : | if (image_create | ||
300 : | edgomez | 195 | (&pEnc->vInterHV, pEnc->mbParam.edged_width, |
301 : | pEnc->mbParam.edged_height) < 0) | ||
302 : | edgomez | 192 | goto xvid_err_memory3; |
303 : | edgomez | 851 | if (image_create |
304 : | (&pEnc->vInterHVf, pEnc->mbParam.edged_width, | ||
305 : | pEnc->mbParam.edged_height) < 0) | ||
306 : | goto xvid_err_memory3; | ||
307 : | suxen_drol | 136 | |
308 : | edgomez | 851 | /* Create full bitplane for GMC, this might be wasteful */ |
309 : | if (image_create | ||
310 : | (&pEnc->vGMC, pEnc->mbParam.edged_width, | ||
311 : | pEnc->mbParam.edged_height) < 0) | ||
312 : | goto xvid_err_memory3; | ||
313 : | |||
314 : | |||
315 : | |||
316 : | pEnc->mbParam.global = pParam->global; | ||
317 : | |||
318 : | /* B Frames specific init */ | ||
319 : | pEnc->mbParam.max_bframes = pParam->max_bframes; | ||
320 : | pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; | ||
321 : | pEnc->mbParam.bquant_offset = pParam->bquant_offset; | ||
322 : | pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio; | ||
323 : | pEnc->bframes = NULL; | ||
324 : | |||
325 : | if (pEnc->mbParam.max_bframes > 0) { | ||
326 : | int n; | ||
327 : | |||
328 : | pEnc->bframes = | ||
329 : | xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), | ||
330 : | CACHE_LINE); | ||
331 : | |||
332 : | if (pEnc->bframes == NULL) | ||
333 : | goto xvid_err_memory3; | ||
334 : | |||
335 : | for (n = 0; n < pEnc->mbParam.max_bframes; n++) | ||
336 : | pEnc->bframes[n] = NULL; | ||
337 : | |||
338 : | |||
339 : | for (n = 0; n < pEnc->mbParam.max_bframes; n++) { | ||
340 : | pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); | ||
341 : | |||
342 : | if (pEnc->bframes[n] == NULL) | ||
343 : | goto xvid_err_memory4; | ||
344 : | |||
345 : | pEnc->bframes[n]->mbs = | ||
346 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
347 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
348 : | |||
349 : | if (pEnc->bframes[n]->mbs == NULL) | ||
350 : | goto xvid_err_memory4; | ||
351 : | |||
352 : | image_null(&pEnc->bframes[n]->image); | ||
353 : | |||
354 : | if (image_create | ||
355 : | (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, | ||
356 : | pEnc->mbParam.edged_height) < 0) | ||
357 : | goto xvid_err_memory4; | ||
358 : | |||
359 : | } | ||
360 : | } | ||
361 : | |||
362 : | pEnc->bframenum_head = 0; | ||
363 : | pEnc->bframenum_tail = 0; | ||
364 : | pEnc->flush_bframes = 0; | ||
365 : | pEnc->bframenum_dx50bvop = -1; | ||
366 : | |||
367 : | pEnc->queue = NULL; | ||
368 : | |||
369 : | |||
370 : | if (pEnc->mbParam.max_bframes > 0) { | ||
371 : | int n; | ||
372 : | |||
373 : | pEnc->queue = | ||
374 : | xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), | ||
375 : | CACHE_LINE); | ||
376 : | |||
377 : | if (pEnc->queue == NULL) | ||
378 : | goto xvid_err_memory4; | ||
379 : | |||
380 : | for (n = 0; n < pEnc->mbParam.max_bframes; n++) | ||
381 : | image_null(&pEnc->queue[n]); | ||
382 : | |||
383 : | for (n = 0; n < pEnc->mbParam.max_bframes; n++) { | ||
384 : | if (image_create | ||
385 : | (&pEnc->queue[n], pEnc->mbParam.edged_width, | ||
386 : | pEnc->mbParam.edged_height) < 0) | ||
387 : | goto xvid_err_memory5; | ||
388 : | |||
389 : | } | ||
390 : | } | ||
391 : | |||
392 : | pEnc->queue_head = 0; | ||
393 : | pEnc->queue_tail = 0; | ||
394 : | pEnc->queue_size = 0; | ||
395 : | |||
396 : | pEnc->mbParam.m_stamp = 0; | ||
397 : | |||
398 : | pEnc->m_framenum = 0; | ||
399 : | pEnc->current->stamp = 0; | ||
400 : | pEnc->reference->stamp = 0; | ||
401 : | |||
402 : | edgomez | 195 | pParam->handle = (void *) pEnc; |
403 : | Isibaar | 3 | |
404 : | edgomez | 195 | if (pParam->rc_bitrate) { |
405 : | RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, | ||
406 : | pParam->rc_reaction_delay_factor, | ||
407 : | pParam->rc_averaging_period, pParam->rc_buffer, | ||
408 : | pParam->fbase * 1000 / pParam->fincr, | ||
409 : | pParam->max_quantizer, pParam->min_quantizer); | ||
410 : | Isibaar | 3 | } |
411 : | |||
412 : | Isibaar | 36 | init_timer(); |
413 : | Isibaar | 3 | |
414 : | return XVID_ERR_OK; | ||
415 : | edgomez | 192 | |
416 : | /* | ||
417 : | * We handle all XVID_ERR_MEMORY here, this makes the code lighter | ||
418 : | */ | ||
419 : | suxen_drol | 229 | |
420 : | edgomez | 851 | xvid_err_memory5: |
421 : | |||
422 : | |||
423 : | if (pEnc->mbParam.max_bframes > 0) { | ||
424 : | |||
425 : | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { | ||
426 : | image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, | ||
427 : | pEnc->mbParam.edged_height); | ||
428 : | } | ||
429 : | xvid_free(pEnc->queue); | ||
430 : | } | ||
431 : | |||
432 : | xvid_err_memory4: | ||
433 : | |||
434 : | if (pEnc->mbParam.max_bframes > 0) { | ||
435 : | |||
436 : | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { | ||
437 : | |||
438 : | if (pEnc->bframes[i] == NULL) | ||
439 : | continue; | ||
440 : | |||
441 : | image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, | ||
442 : | pEnc->mbParam.edged_height); | ||
443 : | |||
444 : | xvid_free(pEnc->bframes[i]->mbs); | ||
445 : | |||
446 : | xvid_free(pEnc->bframes[i]); | ||
447 : | |||
448 : | } | ||
449 : | |||
450 : | xvid_free(pEnc->bframes); | ||
451 : | } | ||
452 : | |||
453 : | edgomez | 195 | xvid_err_memory3: |
454 : | edgomez | 851 | |
455 : | if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) | ||
456 : | { image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, | ||
457 : | pEnc->mbParam.edged_height); | ||
458 : | } | ||
459 : | |||
460 : | image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, | ||
461 : | edgomez | 195 | pEnc->mbParam.edged_height); |
462 : | edgomez | 851 | image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
463 : | pEnc->mbParam.edged_height); | ||
464 : | image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
465 : | pEnc->mbParam.edged_height); | ||
466 : | edgomez | 192 | |
467 : | edgomez | 195 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
468 : | pEnc->mbParam.edged_height); | ||
469 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
470 : | pEnc->mbParam.edged_height); | ||
471 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
472 : | pEnc->mbParam.edged_height); | ||
473 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
474 : | pEnc->mbParam.edged_height); | ||
475 : | edgomez | 851 | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
476 : | pEnc->mbParam.edged_height); | ||
477 : | edgomez | 195 | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
478 : | pEnc->mbParam.edged_height); | ||
479 : | edgomez | 851 | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
480 : | pEnc->mbParam.edged_height); | ||
481 : | edgomez | 192 | |
482 : | edgomez | 851 | /* destroy GMC image */ |
483 : | image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, | ||
484 : | pEnc->mbParam.edged_height); | ||
485 : | |||
486 : | |||
487 : | edgomez | 195 | xvid_err_memory2: |
488 : | edgomez | 192 | xvid_free(pEnc->current->mbs); |
489 : | xvid_free(pEnc->reference->mbs); | ||
490 : | |||
491 : | edgomez | 195 | xvid_err_memory1: |
492 : | edgomez | 192 | xvid_free(pEnc->current); |
493 : | xvid_free(pEnc->reference); | ||
494 : | xvid_free(pEnc); | ||
495 : | |||
496 : | edgomez | 194 | pParam->handle = NULL; |
497 : | |||
498 : | edgomez | 192 | return XVID_ERR_MEMORY; |
499 : | Isibaar | 3 | } |
500 : | |||
501 : | edgomez | 194 | /***************************************************************************** |
502 : | * Encoder destruction | ||
503 : | * | ||
504 : | * This function destroy the entire encoder structure created by a previous | ||
505 : | * successful encoder_create call. | ||
506 : | * | ||
507 : | * Returned values (for now only one returned value) : | ||
508 : | * - XVID_ERR_OK - no errors | ||
509 : | * | ||
510 : | ****************************************************************************/ | ||
511 : | |||
512 : | int | ||
513 : | encoder_destroy(Encoder * pEnc) | ||
514 : | Isibaar | 3 | { |
515 : | edgomez | 851 | int i; |
516 : | suxen_drol | 229 | |
517 : | edgomez | 13 | ENC_CHECK(pEnc); |
518 : | Isibaar | 3 | |
519 : | edgomez | 851 | /* B Frames specific */ |
520 : | if (pEnc->mbParam.max_bframes > 0) { | ||
521 : | |||
522 : | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { | ||
523 : | |||
524 : | image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, | ||
525 : | pEnc->mbParam.edged_height); | ||
526 : | } | ||
527 : | xvid_free(pEnc->queue); | ||
528 : | } | ||
529 : | |||
530 : | |||
531 : | if (pEnc->mbParam.max_bframes > 0) { | ||
532 : | |||
533 : | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { | ||
534 : | |||
535 : | if (pEnc->bframes[i] == NULL) | ||
536 : | continue; | ||
537 : | |||
538 : | image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, | ||
539 : | pEnc->mbParam.edged_height); | ||
540 : | |||
541 : | xvid_free(pEnc->bframes[i]->mbs); | ||
542 : | |||
543 : | xvid_free(pEnc->bframes[i]); | ||
544 : | } | ||
545 : | |||
546 : | xvid_free(pEnc->bframes); | ||
547 : | |||
548 : | } | ||
549 : | |||
550 : | edgomez | 192 | /* All images, reference, current etc ... */ |
551 : | edgomez | 851 | |
552 : | edgomez | 195 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
553 : | pEnc->mbParam.edged_height); | ||
554 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
555 : | pEnc->mbParam.edged_height); | ||
556 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
557 : | pEnc->mbParam.edged_height); | ||
558 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
559 : | pEnc->mbParam.edged_height); | ||
560 : | edgomez | 851 | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
561 : | pEnc->mbParam.edged_height); | ||
562 : | edgomez | 195 | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
563 : | pEnc->mbParam.edged_height); | ||
564 : | edgomez | 851 | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
565 : | pEnc->mbParam.edged_height); | ||
566 : | edgomez | 403 | |
567 : | edgomez | 851 | image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
568 : | edgomez | 195 | pEnc->mbParam.edged_height); |
569 : | edgomez | 851 | image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
570 : | pEnc->mbParam.edged_height); | ||
571 : | image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
572 : | pEnc->mbParam.edged_height); | ||
573 : | edgomez | 192 | |
574 : | edgomez | 851 | if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
575 : | { image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, | ||
576 : | pEnc->mbParam.edged_height); | ||
577 : | } | ||
578 : | |||
579 : | edgomez | 192 | /* Encoder structure */ |
580 : | edgomez | 851 | |
581 : | suxen_drol | 136 | xvid_free(pEnc->current->mbs); |
582 : | xvid_free(pEnc->current); | ||
583 : | |||
584 : | xvid_free(pEnc->reference->mbs); | ||
585 : | xvid_free(pEnc->reference); | ||
586 : | |||
587 : | Isibaar | 41 | xvid_free(pEnc); |
588 : | edgomez | 192 | |
589 : | edgomez | 13 | return XVID_ERR_OK; |
590 : | Isibaar | 3 | } |
591 : | |||
592 : | suxen_drol | 229 | |
593 : | edgomez | 851 | static __inline void inc_frame_num(Encoder * pEnc) |
594 : | suxen_drol | 229 | { |
595 : | edgomez | 867 | pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
596 : | edgomez | 851 | pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
597 : | chl | 387 | } |
598 : | suxen_drol | 229 | |
599 : | edgomez | 851 | |
600 : | static __inline void | ||
601 : | queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) | ||
602 : | { | ||
603 : | if (pEnc->queue_size >= pEnc->mbParam.max_bframes) | ||
604 : | { | ||
605 : | DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); | ||
606 : | return; | ||
607 : | } | ||
608 : | |||
609 : | DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
610 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
611 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
612 : | |||
613 : | |||
614 : | start_timer(); | ||
615 : | if (image_input | ||
616 : | (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, | ||
617 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) | ||
618 : | return; | ||
619 : | stop_conv_timer(); | ||
620 : | |||
621 : | edgomez | 853 | if ((pFrame->general & XVID_CHROMAOPT)) { |
622 : | image_chroma_optimize(&pEnc->queue[pEnc->queue_tail], | ||
623 : | pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); | ||
624 : | } | ||
625 : | |||
626 : | edgomez | 851 | pEnc->queue_size++; |
627 : | pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; | ||
628 : | } | ||
629 : | |||
630 : | static __inline void | ||
631 : | set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) | ||
632 : | { | ||
633 : | |||
634 : | pCur->ticks = (int32_t)pCur->stamp % time_base; | ||
635 : | pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; | ||
636 : | |||
637 : | edgomez | 867 | /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ |
638 : | edgomez | 851 | |
639 : | /* fprintf(stderr,"WriteVop: %d - %d \n", | ||
640 : | ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); | ||
641 : | fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", | ||
642 : | pCur->coding_type, pCur->stamp, pRef->stamp, time_base); | ||
643 : | fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", | ||
644 : | pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); | ||
645 : | |||
646 : | */ | ||
647 : | } | ||
648 : | |||
649 : | |||
650 : | |||
651 : | /* convert pFrame->intra to coding_type */ | ||
652 : | static int intra2coding_type(int intra) | ||
653 : | { | ||
654 : | if (intra < 0) return -1; | ||
655 : | if (intra == 1) return I_VOP; | ||
656 : | if (intra == 2) return B_VOP; | ||
657 : | |||
658 : | return P_VOP; | ||
659 : | } | ||
660 : | |||
661 : | |||
662 : | |||
663 : | edgomez | 194 | /***************************************************************************** |
664 : | edgomez | 851 | * IPB frame encoder entry point |
665 : | * | ||
666 : | * Returned values : | ||
667 : | * - XVID_ERR_OK - no errors | ||
668 : | * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong | ||
669 : | * format | ||
670 : | ****************************************************************************/ | ||
671 : | |||
672 : | int | ||
673 : | encoder_encode_bframes(Encoder * pEnc, | ||
674 : | XVID_ENC_FRAME * pFrame, | ||
675 : | XVID_ENC_STATS * pResult) | ||
676 : | { | ||
677 : | uint16_t x, y; | ||
678 : | Bitstream bs; | ||
679 : | uint32_t bits; | ||
680 : | edgomez | 875 | int mode = -1; /* Just to shut up compiler warning */ |
681 : | edgomez | 851 | |
682 : | int input_valid = 1; | ||
683 : | int bframes_count = 0; | ||
684 : | |||
685 : | ENC_CHECK(pEnc); | ||
686 : | ENC_CHECK(pFrame); | ||
687 : | ENC_CHECK(pFrame->image); | ||
688 : | |||
689 : | start_global_timer(); | ||
690 : | |||
691 : | BitstreamInit(&bs, pFrame->bitstream, 0); | ||
692 : | |||
693 : | ipvop_loop: | ||
694 : | |||
695 : | /* | ||
696 : | * bframe "flush" code | ||
697 : | */ | ||
698 : | |||
699 : | if ((pFrame->image == NULL || pEnc->flush_bframes) | ||
700 : | && (pEnc->bframenum_head < pEnc->bframenum_tail)) { | ||
701 : | |||
702 : | if (pEnc->flush_bframes == 0) { | ||
703 : | /* | ||
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 : | DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
710 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
711 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
712 : | |||
713 : | pEnc->bframenum_tail--; | ||
714 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); | ||
715 : | |||
716 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); | ||
717 : | |||
718 : | FrameCodeP(pEnc, &bs, &bits, 1, 0); | ||
719 : | bframes_count = 0; | ||
720 : | |||
721 : | BitstreamPadAlways(&bs); | ||
722 : | pFrame->length = BitstreamLength(&bs); | ||
723 : | pFrame->intra = 0; | ||
724 : | |||
725 : | |||
726 : | emms(); | ||
727 : | |||
728 : | if (pResult) { | ||
729 : | pResult->quant = pEnc->current->quant; | ||
730 : | pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); | ||
731 : | pResult->kblks = pEnc->current->sStat.kblks; | ||
732 : | pResult->mblks = pEnc->current->sStat.mblks; | ||
733 : | pResult->ublks = pEnc->current->sStat.ublks; | ||
734 : | } | ||
735 : | |||
736 : | return XVID_ERR_OK; | ||
737 : | } | ||
738 : | |||
739 : | |||
740 : | DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
741 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
742 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
743 : | |||
744 : | FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); | ||
745 : | pEnc->bframenum_head++; | ||
746 : | |||
747 : | BitstreamPadAlways(&bs); | ||
748 : | pFrame->length = BitstreamLength(&bs); | ||
749 : | pFrame->intra = 2; | ||
750 : | |||
751 : | if (pResult) { | ||
752 : | pResult->quant = pEnc->current->quant; | ||
753 : | pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); | ||
754 : | pResult->kblks = pEnc->current->sStat.kblks; | ||
755 : | pResult->mblks = pEnc->current->sStat.mblks; | ||
756 : | pResult->ublks = pEnc->current->sStat.ublks; | ||
757 : | } | ||
758 : | |||
759 : | if (input_valid) | ||
760 : | queue_image(pEnc, pFrame); | ||
761 : | |||
762 : | emms(); | ||
763 : | |||
764 : | return XVID_ERR_OK; | ||
765 : | } | ||
766 : | |||
767 : | if (pEnc->bframenum_head > 0) { | ||
768 : | pEnc->bframenum_head = pEnc->bframenum_tail = 0; | ||
769 : | |||
770 : | /* write an empty marker to the bitstream. | ||
771 : | |||
772 : | for divx5 decoder compatibility, this marker must consist | ||
773 : | of a not-coded p-vop, with a time_base of zero, and time_increment | ||
774 : | indentical to the future-referece frame. | ||
775 : | */ | ||
776 : | |||
777 : | if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) { | ||
778 : | int tmp; | ||
779 : | |||
780 : | DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
781 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
782 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
783 : | |||
784 : | |||
785 : | tmp = pEnc->current->seconds; | ||
786 : | pEnc->current->seconds = 0; /* force time_base = 0 */ | ||
787 : | |||
788 : | BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); | ||
789 : | pEnc->current->seconds = tmp; | ||
790 : | |||
791 : | BitstreamPadAlways(&bs); | ||
792 : | pFrame->length = BitstreamLength(&bs); | ||
793 : | pFrame->intra = 4; | ||
794 : | |||
795 : | if (pResult) { | ||
796 : | pResult->quant = pEnc->current->quant; | ||
797 : | pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); | ||
798 : | pResult->kblks = pEnc->current->sStat.kblks; | ||
799 : | pResult->mblks = pEnc->current->sStat.mblks; | ||
800 : | pResult->ublks = pEnc->current->sStat.ublks; | ||
801 : | } | ||
802 : | |||
803 : | if (input_valid) | ||
804 : | queue_image(pEnc, pFrame); | ||
805 : | |||
806 : | emms(); | ||
807 : | |||
808 : | return XVID_ERR_OK; | ||
809 : | } | ||
810 : | } | ||
811 : | |||
812 : | |||
813 : | bvop_loop: | ||
814 : | |||
815 : | if (pEnc->bframenum_dx50bvop != -1) | ||
816 : | { | ||
817 : | |||
818 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); | ||
819 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); | ||
820 : | |||
821 : | if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { | ||
822 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); | ||
823 : | } | ||
824 : | |||
825 : | if (input_valid) | ||
826 : | { | ||
827 : | queue_image(pEnc, pFrame); | ||
828 : | input_valid = 0; | ||
829 : | } | ||
830 : | |||
831 : | } else if (input_valid) { | ||
832 : | |||
833 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); | ||
834 : | |||
835 : | start_timer(); | ||
836 : | if (image_input | ||
837 : | (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
838 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) | ||
839 : | { | ||
840 : | emms(); | ||
841 : | return XVID_ERR_FORMAT; | ||
842 : | } | ||
843 : | stop_conv_timer(); | ||
844 : | |||
845 : | edgomez | 853 | if ((pFrame->general & XVID_CHROMAOPT)) { |
846 : | image_chroma_optimize(&pEnc->current->image, | ||
847 : | pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); | ||
848 : | } | ||
849 : | |||
850 : | edgomez | 867 | /* queue input frame, and dequue next image */ |
851 : | edgomez | 851 | if (pEnc->queue_size > 0) |
852 : | { | ||
853 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); | ||
854 : | if (pEnc->queue_head != pEnc->queue_tail) | ||
855 : | { | ||
856 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); | ||
857 : | } | ||
858 : | pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; | ||
859 : | pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; | ||
860 : | } | ||
861 : | |||
862 : | } else if (pEnc->queue_size > 0) { | ||
863 : | |||
864 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); | ||
865 : | |||
866 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); | ||
867 : | pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; | ||
868 : | pEnc->queue_size--; | ||
869 : | |||
870 : | } else { | ||
871 : | |||
872 : | /* if nothing was encoded, write an 'ignore this frame' flag | ||
873 : | to the bitstream */ | ||
874 : | |||
875 : | if (BitstreamPos(&bs) == 0) { | ||
876 : | |||
877 : | DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
878 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
879 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
880 : | |||
881 : | edgomez | 867 | /* That disabled line of code was supposed to inform VirtualDub |
882 : | * that the frame was a dummy delay frame - now disabled (thx god :-) | ||
883 : | */ | ||
884 : | /* BitstreamPutBits(&bs, 0x7f, 8); */ | ||
885 : | edgomez | 851 | pFrame->intra = 5; |
886 : | |||
887 : | if (pResult) { | ||
888 : | /* | ||
889 : | * We must decide what to put there because i know some apps | ||
890 : | * are storing statistics about quantizers and just do | ||
891 : | * stats[quant]++ or stats[quant-1]++ | ||
892 : | * transcode is one of these app with its 2pass module | ||
893 : | */ | ||
894 : | |||
895 : | /* | ||
896 : | * For now i prefer 31 than 0 that could lead to a segfault | ||
897 : | * in transcode | ||
898 : | */ | ||
899 : | pResult->quant = 31; | ||
900 : | |||
901 : | pResult->hlength = 0; | ||
902 : | pResult->kblks = 0; | ||
903 : | pResult->mblks = 0; | ||
904 : | pResult->ublks = 0; | ||
905 : | } | ||
906 : | |||
907 : | } else { | ||
908 : | |||
909 : | if (pResult) { | ||
910 : | pResult->quant = pEnc->current->quant; | ||
911 : | pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); | ||
912 : | pResult->kblks = pEnc->current->sStat.kblks; | ||
913 : | pResult->mblks = pEnc->current->sStat.mblks; | ||
914 : | pResult->ublks = pEnc->current->sStat.ublks; | ||
915 : | } | ||
916 : | |||
917 : | } | ||
918 : | |||
919 : | pFrame->length = BitstreamLength(&bs); | ||
920 : | |||
921 : | emms(); | ||
922 : | |||
923 : | return XVID_ERR_OK; | ||
924 : | } | ||
925 : | |||
926 : | pEnc->flush_bframes = 0; | ||
927 : | |||
928 : | emms(); | ||
929 : | |||
930 : | edgomez | 867 | /* only inc frame num, adapt quant, etc. if we havent seen it before */ |
931 : | edgomez | 851 | if (pEnc->bframenum_dx50bvop < 0 ) |
932 : | { | ||
933 : | mode = intra2coding_type(pFrame->intra); | ||
934 : | if (pFrame->quant == 0) | ||
935 : | pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); | ||
936 : | else | ||
937 : | pEnc->current->quant = pFrame->quant; | ||
938 : | |||
939 : | /* if (pEnc->current->quant < 1) | ||
940 : | pEnc->current->quant = 1; | ||
941 : | |||
942 : | if (pEnc->current->quant > 31) | ||
943 : | pEnc->current->quant = 31; | ||
944 : | */ | ||
945 : | pEnc->current->global_flags = pFrame->general; | ||
946 : | pEnc->current->motion_flags = pFrame->motion; | ||
947 : | |||
948 : | /* ToDo : dynamic fcode (in both directions) */ | ||
949 : | pEnc->current->fcode = pEnc->mbParam.m_fcode; | ||
950 : | pEnc->current->bcode = pEnc->mbParam.m_fcode; | ||
951 : | |||
952 : | inc_frame_num(pEnc); | ||
953 : | |||
954 : | if (pFrame->general & XVID_EXTRASTATS) | ||
955 : | { image_copy(&pEnc->sOriginal, &pEnc->current->image, | ||
956 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
957 : | } | ||
958 : | |||
959 : | emms(); | ||
960 : | |||
961 : | if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { | ||
962 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, | ||
963 : | "%i if:%i st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp); | ||
964 : | } | ||
965 : | |||
966 : | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
967 : | * Luminance masking | ||
968 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
969 : | |||
970 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
971 : | int *temp_dquants = | ||
972 : | (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
973 : | pEnc->mbParam.mb_height * sizeof(int), | ||
974 : | CACHE_LINE); | ||
975 : | |||
976 : | pEnc->current->quant = | ||
977 : | adaptive_quantization(pEnc->current->image.y, | ||
978 : | pEnc->mbParam.edged_width, temp_dquants, | ||
979 : | pEnc->current->quant, pEnc->current->quant, | ||
980 : | 2 * pEnc->current->quant, | ||
981 : | pEnc->mbParam.mb_width, | ||
982 : | pEnc->mbParam.mb_height); | ||
983 : | |||
984 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) { | ||
985 : | |||
986 : | #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) | ||
987 : | |||
988 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) { | ||
989 : | MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; | ||
990 : | |||
991 : | pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; | ||
992 : | } | ||
993 : | |||
994 : | #undef OFFSET | ||
995 : | } | ||
996 : | |||
997 : | xvid_free(temp_dquants); | ||
998 : | } | ||
999 : | |||
1000 : | } | ||
1001 : | |||
1002 : | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
1003 : | * ivop/pvop/bvop selection | ||
1004 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1005 : | pEnc->iFrameNum++; | ||
1006 : | |||
1007 : | if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || | ||
1008 : | (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && | ||
1009 : | pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) | ||
1010 : | { | ||
1011 : | mode = I_VOP; | ||
1012 : | }else{ | ||
1013 : | mode = MEanalysis(&pEnc->reference->image, pEnc->current, | ||
1014 : | &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, | ||
1015 : | (mode < 0) ? pEnc->iFrameNum : 0, | ||
1016 : | bframes_count++); | ||
1017 : | } | ||
1018 : | |||
1019 : | if (mode == I_VOP) { | ||
1020 : | /* | ||
1021 : | * This will be coded as an Intra Frame | ||
1022 : | */ | ||
1023 : | if ((pEnc->current->global_flags & XVID_QUARTERPEL)) | ||
1024 : | pEnc->mbParam.m_quarterpel = 1; | ||
1025 : | else | ||
1026 : | pEnc->mbParam.m_quarterpel = 0; | ||
1027 : | |||
1028 : | if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT; | ||
1029 : | |||
1030 : | if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { | ||
1031 : | if (pFrame->quant_intra_matrix != NULL) | ||
1032 : | set_intra_matrix(pFrame->quant_intra_matrix); | ||
1033 : | if (pFrame->quant_inter_matrix != NULL) | ||
1034 : | set_inter_matrix(pFrame->quant_inter_matrix); | ||
1035 : | } | ||
1036 : | |||
1037 : | |||
1038 : | DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
1039 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1040 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1041 : | |||
1042 : | if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { | ||
1043 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); | ||
1044 : | } | ||
1045 : | |||
1046 : | edgomez | 867 | /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */ |
1047 : | edgomez | 851 | |
1048 : | if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { | ||
1049 : | |||
1050 : | pEnc->bframenum_tail--; | ||
1051 : | pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; | ||
1052 : | |||
1053 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); | ||
1054 : | if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { | ||
1055 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); | ||
1056 : | } | ||
1057 : | FrameCodeP(pEnc, &bs, &bits, 1, 0); | ||
1058 : | bframes_count = 0; | ||
1059 : | pFrame->intra = 0; | ||
1060 : | |||
1061 : | } else { | ||
1062 : | |||
1063 : | FrameCodeI(pEnc, &bs, &bits); | ||
1064 : | bframes_count = 0; | ||
1065 : | pFrame->intra = 1; | ||
1066 : | |||
1067 : | pEnc->bframenum_dx50bvop = -1; | ||
1068 : | } | ||
1069 : | |||
1070 : | pEnc->flush_bframes = 1; | ||
1071 : | |||
1072 : | if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { | ||
1073 : | BitstreamPadAlways(&bs); | ||
1074 : | input_valid = 0; | ||
1075 : | goto ipvop_loop; | ||
1076 : | } | ||
1077 : | |||
1078 : | /* | ||
1079 : | * NB : sequences like "IIBB" decode fine with msfdam but, | ||
1080 : | * go screwy with divx 5.00 | ||
1081 : | */ | ||
1082 : | } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { | ||
1083 : | /* | ||
1084 : | * This will be coded as a Predicted Frame | ||
1085 : | */ | ||
1086 : | |||
1087 : | DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
1088 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1089 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1090 : | |||
1091 : | if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { | ||
1092 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); | ||
1093 : | } | ||
1094 : | |||
1095 : | FrameCodeP(pEnc, &bs, &bits, 1, 0); | ||
1096 : | bframes_count = 0; | ||
1097 : | pFrame->intra = 0; | ||
1098 : | pEnc->flush_bframes = 1; | ||
1099 : | |||
1100 : | if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { | ||
1101 : | BitstreamPadAlways(&bs); | ||
1102 : | input_valid = 0; | ||
1103 : | goto ipvop_loop; | ||
1104 : | } | ||
1105 : | |||
1106 : | } else { /* mode == B_VOP */ | ||
1107 : | /* | ||
1108 : | * This will be coded as a Bidirectional Frame | ||
1109 : | */ | ||
1110 : | |||
1111 : | if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { | ||
1112 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); | ||
1113 : | } | ||
1114 : | |||
1115 : | if (pFrame->bquant < 1) { | ||
1116 : | pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * | ||
1117 : | pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; | ||
1118 : | |||
1119 : | } else { | ||
1120 : | pEnc->current->quant = pFrame->bquant; | ||
1121 : | } | ||
1122 : | |||
1123 : | if (pEnc->current->quant < 1) | ||
1124 : | pEnc->current->quant = 1; | ||
1125 : | else if (pEnc->current->quant > 31) | ||
1126 : | pEnc->current->quant = 31; | ||
1127 : | |||
1128 : | DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", | ||
1129 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1130 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); | ||
1131 : | |||
1132 : | /* store frame into bframe buffer & swap ref back to current */ | ||
1133 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); | ||
1134 : | SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); | ||
1135 : | |||
1136 : | pEnc->bframenum_tail++; | ||
1137 : | |||
1138 : | edgomez | 867 | /* bframe report by koepi */ |
1139 : | edgomez | 851 | pFrame->intra = 2; |
1140 : | pFrame->length = 0; | ||
1141 : | |||
1142 : | input_valid = 0; | ||
1143 : | goto bvop_loop; | ||
1144 : | } | ||
1145 : | |||
1146 : | BitstreamPadAlways(&bs); | ||
1147 : | pFrame->length = BitstreamLength(&bs); | ||
1148 : | |||
1149 : | if (pResult) { | ||
1150 : | pResult->quant = pEnc->current->quant; | ||
1151 : | pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); | ||
1152 : | pResult->kblks = pEnc->current->sStat.kblks; | ||
1153 : | pResult->mblks = pEnc->current->sStat.mblks; | ||
1154 : | pResult->ublks = pEnc->current->sStat.ublks; | ||
1155 : | |||
1156 : | if (pFrame->general & XVID_EXTRASTATS) | ||
1157 : | { pResult->sse_y = | ||
1158 : | plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, | ||
1159 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
1160 : | pEnc->mbParam.height); | ||
1161 : | |||
1162 : | pResult->sse_u = | ||
1163 : | plane_sse( pEnc->sOriginal.u, pEnc->current->image.u, | ||
1164 : | pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, | ||
1165 : | pEnc->mbParam.height/2); | ||
1166 : | |||
1167 : | pResult->sse_v = | ||
1168 : | plane_sse( pEnc->sOriginal.v, pEnc->current->image.v, | ||
1169 : | pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, | ||
1170 : | pEnc->mbParam.height/2); | ||
1171 : | } | ||
1172 : | } | ||
1173 : | |||
1174 : | emms(); | ||
1175 : | |||
1176 : | if (pFrame->quant == 0) { | ||
1177 : | RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, | ||
1178 : | pFrame->length, pFrame->intra); | ||
1179 : | } | ||
1180 : | |||
1181 : | stop_global_timer(); | ||
1182 : | write_timer(); | ||
1183 : | |||
1184 : | emms(); | ||
1185 : | return XVID_ERR_OK; | ||
1186 : | } | ||
1187 : | |||
1188 : | |||
1189 : | |||
1190 : | /***************************************************************************** | ||
1191 : | suxen_drol | 229 | * "original" IP frame encoder entry point |
1192 : | * | ||
1193 : | * Returned values : | ||
1194 : | * - XVID_ERR_OK - no errors | ||
1195 : | * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong | ||
1196 : | * format | ||
1197 : | edgomez | 194 | ****************************************************************************/ |
1198 : | suxen_drol | 229 | |
1199 : | edgomez | 194 | int |
1200 : | encoder_encode(Encoder * pEnc, | ||
1201 : | edgomez | 195 | XVID_ENC_FRAME * pFrame, |
1202 : | XVID_ENC_STATS * pResult) | ||
1203 : | suxen_drol | 152 | { |
1204 : | uint16_t x, y; | ||
1205 : | Bitstream bs; | ||
1206 : | uint32_t bits; | ||
1207 : | Isibaar | 4 | uint16_t write_vol_header = 0; |
1208 : | edgomez | 195 | |
1209 : | Isibaar | 113 | float psnr; |
1210 : | chl | 870 | char temp[128]; |
1211 : | Isibaar | 3 | |
1212 : | start_global_timer(); | ||
1213 : | |||
1214 : | edgomez | 13 | ENC_CHECK(pEnc); |
1215 : | ENC_CHECK(pFrame); | ||
1216 : | ENC_CHECK(pFrame->bitstream); | ||
1217 : | ENC_CHECK(pFrame->image); | ||
1218 : | Isibaar | 3 | |
1219 : | edgomez | 851 | SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1220 : | suxen_drol | 136 | |
1221 : | pEnc->current->global_flags = pFrame->general; | ||
1222 : | pEnc->current->motion_flags = pFrame->motion; | ||
1223 : | h | 101 | pEnc->mbParam.hint = &pFrame->hint; |
1224 : | Isibaar | 3 | |
1225 : | edgomez | 851 | inc_frame_num(pEnc); |
1226 : | |||
1227 : | /* disable alternate scan flag if interlacing is not enabled */ | ||
1228 : | if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && | ||
1229 : | !(pEnc->current->global_flags & XVID_INTERLACING)) | ||
1230 : | { | ||
1231 : | pEnc->current->global_flags -= XVID_ALTERNATESCAN; | ||
1232 : | } | ||
1233 : | |||
1234 : | Isibaar | 3 | start_timer(); |
1235 : | edgomez | 195 | if (image_input |
1236 : | (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
1237 : | edgomez | 851 | pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0) |
1238 : | Isibaar | 3 | return XVID_ERR_FORMAT; |
1239 : | stop_conv_timer(); | ||
1240 : | |||
1241 : | edgomez | 853 | if ((pFrame->general & XVID_CHROMAOPT)) { |
1242 : | image_chroma_optimize(&pEnc->current->image, | ||
1243 : | pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); | ||
1244 : | } | ||
1245 : | |||
1246 : | edgomez | 851 | if (pFrame->general & XVID_EXTRASTATS) |
1247 : | { image_copy(&pEnc->sOriginal, &pEnc->current->image, | ||
1248 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
1249 : | } | ||
1250 : | Isibaar | 113 | |
1251 : | edgomez | 198 | emms(); |
1252 : | suxen_drol | 136 | |
1253 : | edgomez | 13 | BitstreamInit(&bs, pFrame->bitstream, 0); |
1254 : | Isibaar | 3 | |
1255 : | edgomez | 195 | if (pFrame->quant == 0) { |
1256 : | pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); | ||
1257 : | } else { | ||
1258 : | suxen_drol | 136 | pEnc->current->quant = pFrame->quant; |
1259 : | Isibaar | 3 | } |
1260 : | |||
1261 : | edgomez | 851 | if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1262 : | pEnc->mbParam.m_quarterpel = 1; | ||
1263 : | else | ||
1264 : | pEnc->mbParam.m_quarterpel = 0; | ||
1265 : | |||
1266 : | edgomez | 195 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1267 : | int *temp_dquants = | ||
1268 : | (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
1269 : | pEnc->mbParam.mb_height * sizeof(int), | ||
1270 : | CACHE_LINE); | ||
1271 : | |||
1272 : | edgomez | 194 | pEnc->current->quant = |
1273 : | adaptive_quantization(pEnc->current->image.y, | ||
1274 : | edgomez | 195 | pEnc->mbParam.edged_width, temp_dquants, |
1275 : | pEnc->current->quant, pEnc->current->quant, | ||
1276 : | 2 * pEnc->current->quant, | ||
1277 : | pEnc->mbParam.mb_width, | ||
1278 : | pEnc->mbParam.mb_height); | ||
1279 : | edgomez | 194 | |
1280 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1281 : | edgomez | 194 | |
1282 : | edgomez | 195 | #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
1283 : | edgomez | 194 | |
1284 : | edgomez | 195 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1285 : | edgomez | 194 | |
1286 : | edgomez | 195 | |
1287 : | MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; | ||
1288 : | |||
1289 : | pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; | ||
1290 : | Isibaar | 3 | } |
1291 : | edgomez | 194 | |
1292 : | edgomez | 195 | #undef OFFSET |
1293 : | edgomez | 194 | } |
1294 : | |||
1295 : | Isibaar | 41 | xvid_free(temp_dquants); |
1296 : | Isibaar | 3 | } |
1297 : | |||
1298 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_H263QUANT) { |
1299 : | if (pEnc->mbParam.m_quant_type != H263_QUANT) | ||
1300 : | Isibaar | 20 | write_vol_header = 1; |
1301 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = H263_QUANT; |
1302 : | edgomez | 195 | } else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
1303 : | edgomez | 194 | int matrix1_changed, matrix2_changed; |
1304 : | Isibaar | 3 | |
1305 : | edgomez | 194 | matrix1_changed = matrix2_changed = 0; |
1306 : | edgomez | 78 | |
1307 : | edgomez | 195 | if (pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
1308 : | Isibaar | 20 | write_vol_header = 1; |
1309 : | edgomez | 195 | |
1310 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
1311 : | edgomez | 195 | |
1312 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1313 : | edgomez | 195 | if (pFrame->quant_intra_matrix != NULL) |
1314 : | matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); | ||
1315 : | if (pFrame->quant_inter_matrix != NULL) | ||
1316 : | matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); | ||
1317 : | } else { | ||
1318 : | edgomez | 194 | matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
1319 : | matrix2_changed = set_inter_matrix(get_default_inter_matrix()); | ||
1320 : | Isibaar | 20 | } |
1321 : | edgomez | 195 | if (write_vol_header == 0) |
1322 : | edgomez | 194 | write_vol_header = matrix1_changed | matrix2_changed; |
1323 : | Isibaar | 4 | } |
1324 : | Isibaar | 3 | |
1325 : | edgomez | 195 | if (pFrame->intra < 0) { |
1326 : | edgomez | 851 | if ((pEnc->iFrameNum == -1) |
1327 : | || ((pEnc->mbParam.iMaxKeyInterval > 0) | ||
1328 : | && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) { | ||
1329 : | edgomez | 195 | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1330 : | } else { | ||
1331 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); | ||
1332 : | edgomez | 194 | } |
1333 : | edgomez | 195 | } else { |
1334 : | if (pFrame->intra == 1) { | ||
1335 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
1336 : | } else { | ||
1337 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); | ||
1338 : | edgomez | 194 | } |
1339 : | |||
1340 : | edgomez | 13 | } |
1341 : | Isibaar | 3 | |
1342 : | edgomez | 867 | /* Relic from OpenDivX - now disabled |
1343 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
1344 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
1345 : | */ | ||
1346 : | |||
1347 : | edgomez | 851 | BitstreamPadAlways(&bs); |
1348 : | edgomez | 13 | pFrame->length = BitstreamLength(&bs); |
1349 : | h | 29 | |
1350 : | edgomez | 195 | if (pResult) { |
1351 : | suxen_drol | 136 | pResult->quant = pEnc->current->quant; |
1352 : | edgomez | 851 | pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
1353 : | pResult->kblks = pEnc->current->sStat.kblks; | ||
1354 : | pResult->mblks = pEnc->current->sStat.mblks; | ||
1355 : | pResult->ublks = pEnc->current->sStat.ublks; | ||
1356 : | edgomez | 13 | } |
1357 : | edgomez | 195 | |
1358 : | edgomez | 198 | emms(); |
1359 : | Isibaar | 41 | |
1360 : | edgomez | 195 | if (pFrame->quant == 0) { |
1361 : | edgomez | 851 | RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1362 : | edgomez | 195 | pFrame->length, pFrame->intra); |
1363 : | Isibaar | 3 | } |
1364 : | edgomez | 851 | if (pFrame->general & XVID_EXTRASTATS) |
1365 : | { | ||
1366 : | psnr = | ||
1367 : | image_psnr(&pEnc->sOriginal, &pEnc->current->image, | ||
1368 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
1369 : | pEnc->mbParam.height); | ||
1370 : | |||
1371 : | snprintf(temp, 127, "PSNR: %f\n", psnr); | ||
1372 : | } | ||
1373 : | Isibaar | 113 | |
1374 : | Isibaar | 3 | pEnc->iFrameNum++; |
1375 : | edgomez | 195 | |
1376 : | Isibaar | 3 | stop_global_timer(); |
1377 : | write_timer(); | ||
1378 : | |||
1379 : | return XVID_ERR_OK; | ||
1380 : | } | ||
1381 : | |||
1382 : | |||
1383 : | edgomez | 195 | static __inline void |
1384 : | CodeIntraMB(Encoder * pEnc, | ||
1385 : | MACROBLOCK * pMB) | ||
1386 : | { | ||
1387 : | Isibaar | 3 | |
1388 : | pMB->mode = MODE_INTRA; | ||
1389 : | |||
1390 : | suxen_drol | 136 | /* zero mv statistics */ |
1391 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
1392 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
1393 : | pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
1394 : | pMB->sad16 = 0; | ||
1395 : | |||
1396 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
1397 : | edgomez | 195 | if (pMB->dquant != NO_CHANGE) { |
1398 : | Isibaar | 3 | pMB->mode = MODE_INTRA_Q; |
1399 : | suxen_drol | 136 | pEnc->current->quant += DQtab[pMB->dquant]; |
1400 : | edgomez | 195 | |
1401 : | if (pEnc->current->quant > 31) | ||
1402 : | pEnc->current->quant = 31; | ||
1403 : | if (pEnc->current->quant < 1) | ||
1404 : | pEnc->current->quant = 1; | ||
1405 : | Isibaar | 3 | } |
1406 : | } | ||
1407 : | |||
1408 : | suxen_drol | 136 | pMB->quant = pEnc->current->quant; |
1409 : | Isibaar | 3 | } |
1410 : | |||
1411 : | |||
1412 : | h | 101 | #define FCODEBITS 3 |
1413 : | #define MODEBITS 5 | ||
1414 : | |||
1415 : | edgomez | 195 | void |
1416 : | HintedMESet(Encoder * pEnc, | ||
1417 : | int *intra) | ||
1418 : | h | 101 | { |
1419 : | edgomez | 195 | HINTINFO *hint; |
1420 : | h | 101 | Bitstream bs; |
1421 : | int length, high; | ||
1422 : | uint32_t x, y; | ||
1423 : | |||
1424 : | hint = pEnc->mbParam.hint; | ||
1425 : | |||
1426 : | edgomez | 195 | if (hint->rawhints) { |
1427 : | h | 101 | *intra = hint->mvhint.intra; |
1428 : | edgomez | 195 | } else { |
1429 : | h | 101 | BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1430 : | *intra = BitstreamGetBit(&bs); | ||
1431 : | } | ||
1432 : | |||
1433 : | edgomez | 195 | if (*intra) { |
1434 : | h | 101 | return; |
1435 : | } | ||
1436 : | |||
1437 : | edgomez | 875 | pEnc->current->fcode = (hint->rawhints) ? |
1438 : | (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); | ||
1439 : | h | 101 | |
1440 : | edgomez | 195 | length = pEnc->current->fcode + 5; |
1441 : | high = 1 << (length - 1); | ||
1442 : | h | 101 | |
1443 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1444 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
1445 : | MACROBLOCK *pMB = | ||
1446 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1447 : | MVBLOCKHINT *bhint = | ||
1448 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
1449 : | chl | 284 | VECTOR pred; |
1450 : | h | 101 | VECTOR tmp; |
1451 : | int vec; | ||
1452 : | |||
1453 : | edgomez | 875 | pMB->mode = (hint->rawhints) ? |
1454 : | (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS); | ||
1455 : | h | 101 | |
1456 : | h | 128 | pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1457 : | pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; | ||
1458 : | |||
1459 : | edgomez | 195 | if (pMB->mode == MODE_INTER) { |
1460 : | edgomez | 875 | tmp.x = (hint->rawhints) ? |
1461 : | bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length); | ||
1462 : | tmp.y = (hint->rawhints) ? | ||
1463 : | bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length); | ||
1464 : | edgomez | 195 | tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1465 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
1466 : | h | 101 | |
1467 : | chl | 284 | pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
1468 : | h | 101 | |
1469 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1470 : | pMB->mvs[vec].x = tmp.x; | ||
1471 : | pMB->mvs[vec].y = tmp.y; | ||
1472 : | chl | 284 | pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
1473 : | pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; | ||
1474 : | h | 101 | } |
1475 : | edgomez | 195 | } else if (pMB->mode == MODE_INTER4V) { |
1476 : | for (vec = 0; vec < 4; ++vec) { | ||
1477 : | edgomez | 875 | tmp.x = (hint->rawhints) ? |
1478 : | bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length); | ||
1479 : | tmp.y = (hint->rawhints) ? | ||
1480 : | bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length); | ||
1481 : | edgomez | 195 | tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1482 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
1483 : | h | 101 | |
1484 : | chl | 284 | pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
1485 : | h | 101 | |
1486 : | edgomez | 195 | pMB->mvs[vec].x = tmp.x; |
1487 : | pMB->mvs[vec].y = tmp.y; | ||
1488 : | chl | 284 | pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1489 : | pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; | ||
1490 : | h | 101 | } |
1491 : | edgomez | 867 | } else /* intra / stuffing / not_coded */ |
1492 : | h | 101 | { |
1493 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1494 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
1495 : | h | 101 | } |
1496 : | } | ||
1497 : | h | 128 | |
1498 : | suxen_drol | 136 | if (pMB->mode == MODE_INTER4V && |
1499 : | edgomez | 195 | (pEnc->current->global_flags & XVID_LUMIMASKING) |
1500 : | && pMB->dquant != NO_CHANGE) { | ||
1501 : | h | 128 | pMB->mode = MODE_INTRA; |
1502 : | |||
1503 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1504 : | h | 128 | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1505 : | } | ||
1506 : | } | ||
1507 : | h | 101 | } |
1508 : | } | ||
1509 : | } | ||
1510 : | |||
1511 : | |||
1512 : | edgomez | 195 | void |
1513 : | HintedMEGet(Encoder * pEnc, | ||
1514 : | int intra) | ||
1515 : | h | 101 | { |
1516 : | edgomez | 195 | HINTINFO *hint; |
1517 : | h | 101 | Bitstream bs; |
1518 : | uint32_t x, y; | ||
1519 : | int length, high; | ||
1520 : | |||
1521 : | hint = pEnc->mbParam.hint; | ||
1522 : | |||
1523 : | edgomez | 195 | if (hint->rawhints) { |
1524 : | h | 101 | hint->mvhint.intra = intra; |
1525 : | edgomez | 195 | } else { |
1526 : | h | 101 | BitstreamInit(&bs, hint->hintstream, 0); |
1527 : | BitstreamPutBit(&bs, intra); | ||
1528 : | } | ||
1529 : | |||
1530 : | edgomez | 195 | if (intra) { |
1531 : | if (!hint->rawhints) { | ||
1532 : | edgomez | 851 | BitstreamPadAlways(&bs); |
1533 : | h | 101 | hint->hintlength = BitstreamLength(&bs); |
1534 : | } | ||
1535 : | return; | ||
1536 : | } | ||
1537 : | |||
1538 : | edgomez | 195 | length = pEnc->current->fcode + 5; |
1539 : | high = 1 << (length - 1); | ||
1540 : | h | 101 | |
1541 : | edgomez | 195 | if (hint->rawhints) { |
1542 : | suxen_drol | 136 | hint->mvhint.fcode = pEnc->current->fcode; |
1543 : | edgomez | 195 | } else { |
1544 : | suxen_drol | 136 | BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1545 : | h | 101 | } |
1546 : | |||
1547 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1548 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
1549 : | MACROBLOCK *pMB = | ||
1550 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1551 : | MVBLOCKHINT *bhint = | ||
1552 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
1553 : | h | 101 | VECTOR tmp; |
1554 : | |||
1555 : | edgomez | 195 | if (hint->rawhints) { |
1556 : | h | 101 | bhint->mode = pMB->mode; |
1557 : | edgomez | 195 | } else { |
1558 : | h | 101 | BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1559 : | } | ||
1560 : | |||
1561 : | edgomez | 195 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
1562 : | tmp.x = pMB->mvs[0].x; | ||
1563 : | tmp.y = pMB->mvs[0].y; | ||
1564 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
1565 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
1566 : | h | 101 | |
1567 : | edgomez | 195 | if (hint->rawhints) { |
1568 : | h | 101 | bhint->mvs[0].x = tmp.x; |
1569 : | bhint->mvs[0].y = tmp.y; | ||
1570 : | edgomez | 195 | } else { |
1571 : | h | 101 | BitstreamPutBits(&bs, tmp.x, length); |
1572 : | BitstreamPutBits(&bs, tmp.y, length); | ||
1573 : | } | ||
1574 : | edgomez | 195 | } else if (pMB->mode == MODE_INTER4V) { |
1575 : | h | 101 | int vec; |
1576 : | |||
1577 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1578 : | tmp.x = pMB->mvs[vec].x; | ||
1579 : | tmp.y = pMB->mvs[vec].y; | ||
1580 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
1581 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
1582 : | h | 101 | |
1583 : | edgomez | 195 | if (hint->rawhints) { |
1584 : | h | 101 | bhint->mvs[vec].x = tmp.x; |
1585 : | bhint->mvs[vec].y = tmp.y; | ||
1586 : | edgomez | 195 | } else { |
1587 : | h | 101 | BitstreamPutBits(&bs, tmp.x, length); |
1588 : | BitstreamPutBits(&bs, tmp.y, length); | ||
1589 : | } | ||
1590 : | } | ||
1591 : | } | ||
1592 : | } | ||
1593 : | } | ||
1594 : | |||
1595 : | edgomez | 195 | if (!hint->rawhints) { |
1596 : | h | 101 | BitstreamPad(&bs); |
1597 : | hint->hintlength = BitstreamLength(&bs); | ||
1598 : | } | ||
1599 : | } | ||
1600 : | |||
1601 : | |||
1602 : | edgomez | 195 | static int |
1603 : | FrameCodeI(Encoder * pEnc, | ||
1604 : | Bitstream * bs, | ||
1605 : | uint32_t * pBits) | ||
1606 : | h | 104 | { |
1607 : | edgomez | 851 | int mb_width = pEnc->mbParam.mb_width; |
1608 : | int mb_height = pEnc->mbParam.mb_height; | ||
1609 : | h | 104 | |
1610 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
1611 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1612 : | h | 104 | |
1613 : | uint16_t x, y; | ||
1614 : | |||
1615 : | edgomez | 851 | if ((pEnc->current->global_flags & XVID_REDUCED)) |
1616 : | { | ||
1617 : | mb_width = (pEnc->mbParam.width + 31) / 32; | ||
1618 : | mb_height = (pEnc->mbParam.height + 31) / 32; | ||
1619 : | |||
1620 : | /* 16x16->8x8 downsample requires 1 additional edge pixel*/ | ||
1621 : | /* XXX: setedges is overkill */ | ||
1622 : | start_timer(); | ||
1623 : | image_setedges(&pEnc->current->image, | ||
1624 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, | ||
1625 : | pEnc->mbParam.width, pEnc->mbParam.height); | ||
1626 : | stop_edges_timer(); | ||
1627 : | } | ||
1628 : | |||
1629 : | h | 104 | pEnc->iFrameNum = 0; |
1630 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1; |
1631 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
1632 : | edgomez | 851 | pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1633 : | suxen_drol | 136 | pEnc->current->coding_type = I_VOP; |
1634 : | h | 104 | |
1635 : | suxen_drol | 136 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1636 : | edgomez | 403 | |
1637 : | edgomez | 851 | set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1638 : | |||
1639 : | BitstreamPadAlways(bs); | ||
1640 : | suxen_drol | 229 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1641 : | h | 104 | |
1642 : | *pBits = BitstreamPos(bs); | ||
1643 : | |||
1644 : | edgomez | 851 | pEnc->current->sStat.iTextBits = 0; |
1645 : | pEnc->current->sStat.kblks = mb_width * mb_height; | ||
1646 : | pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; | ||
1647 : | h | 104 | |
1648 : | edgomez | 851 | for (y = 0; y < mb_height; y++) |
1649 : | for (x = 0; x < mb_width; x++) { | ||
1650 : | edgomez | 195 | MACROBLOCK *pMB = |
1651 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1652 : | h | 104 | |
1653 : | CodeIntraMB(pEnc, pMB); | ||
1654 : | |||
1655 : | edgomez | 195 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1656 : | dct_codes, qcoeff); | ||
1657 : | h | 104 | |
1658 : | start_timer(); | ||
1659 : | suxen_drol | 136 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1660 : | h | 104 | stop_prediction_timer(); |
1661 : | |||
1662 : | start_timer(); | ||
1663 : | chl | 353 | if (pEnc->current->global_flags & XVID_GREYSCALE) |
1664 : | { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ | ||
1665 : | qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ | ||
1666 : | qcoeff[5*64+0]=0; | ||
1667 : | } | ||
1668 : | edgomez | 851 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1669 : | h | 104 | stop_coding_timer(); |
1670 : | } | ||
1671 : | |||
1672 : | edgomez | 851 | if ((pEnc->current->global_flags & XVID_REDUCED)) |
1673 : | { | ||
1674 : | image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, | ||
1675 : | pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, | ||
1676 : | 16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); | ||
1677 : | } | ||
1678 : | h | 104 | emms(); |
1679 : | |||
1680 : | *pBits = BitstreamPos(bs) - *pBits; | ||
1681 : | edgomez | 851 | pEnc->fMvPrevSigma = -1; |
1682 : | suxen_drol | 136 | pEnc->mbParam.m_fcode = 2; |
1683 : | h | 104 | |
1684 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1685 : | h | 104 | HintedMEGet(pEnc, 1); |
1686 : | } | ||
1687 : | |||
1688 : | edgomez | 867 | return 1; /* intra */ |
1689 : | h | 104 | } |
1690 : | |||
1691 : | |||
1692 : | Isibaar | 3 | #define INTRA_THRESHOLD 0.5 |
1693 : | edgomez | 851 | #define BFRAME_SKIP_THRESHHOLD 30 |
1694 : | Isibaar | 3 | |
1695 : | edgomez | 851 | |
1696 : | /* FrameCodeP also handles S(GMC)-VOPs */ | ||
1697 : | edgomez | 195 | static int |
1698 : | FrameCodeP(Encoder * pEnc, | ||
1699 : | Bitstream * bs, | ||
1700 : | uint32_t * pBits, | ||
1701 : | bool force_inter, | ||
1702 : | bool vol_header) | ||
1703 : | Isibaar | 3 | { |
1704 : | edgomez | 13 | float fSigma; |
1705 : | Isibaar | 42 | |
1706 : | edgomez | 78 | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1707 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1708 : | edgomez | 78 | |
1709 : | edgomez | 851 | int mb_width = pEnc->mbParam.mb_width; |
1710 : | int mb_height = pEnc->mbParam.mb_height; | ||
1711 : | |||
1712 : | Isibaar | 3 | int iLimit; |
1713 : | edgomez | 851 | int x, y, k; |
1714 : | edgomez | 13 | int iSearchRange; |
1715 : | edgomez | 851 | int bIntra, skip_possible; |
1716 : | suxen_drol | 240 | |
1717 : | edgomez | 145 | /* IMAGE *pCurrent = &pEnc->current->image; */ |
1718 : | suxen_drol | 136 | IMAGE *pRef = &pEnc->reference->image; |
1719 : | Isibaar | 3 | |
1720 : | edgomez | 851 | if ((pEnc->current->global_flags & XVID_REDUCED)) |
1721 : | { | ||
1722 : | mb_width = (pEnc->mbParam.width + 31) / 32; | ||
1723 : | mb_height = (pEnc->mbParam.height + 31) / 32; | ||
1724 : | } | ||
1725 : | |||
1726 : | |||
1727 : | h | 69 | start_timer(); |
1728 : | edgomez | 195 | image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1729 : | h | 541 | pEnc->mbParam.width, pEnc->mbParam.height); |
1730 : | h | 69 | stop_edges_timer(); |
1731 : | Isibaar | 3 | |
1732 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1733 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
1734 : | edgomez | 851 | pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1735 : | suxen_drol | 136 | pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1736 : | Isibaar | 3 | |
1737 : | if (!force_inter) | ||
1738 : | edgomez | 851 | iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
1739 : | edgomez | 13 | else |
1740 : | edgomez | 851 | iLimit = mb_width * mb_height + 1; |
1741 : | Isibaar | 3 | |
1742 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1743 : | Isibaar | 3 | start_timer(); |
1744 : | edgomez | 195 | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1745 : | &pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
1746 : | pEnc->mbParam.edged_height, | ||
1747 : | edgomez | 851 | pEnc->mbParam.m_quarterpel, |
1748 : | edgomez | 195 | pEnc->current->rounding_type); |
1749 : | Isibaar | 3 | stop_inter_timer(); |
1750 : | } | ||
1751 : | |||
1752 : | edgomez | 851 | pEnc->current->coding_type = P_VOP; |
1753 : | |||
1754 : | Isibaar | 3 | start_timer(); |
1755 : | edgomez | 851 | if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1756 : | h | 101 | HintedMESet(pEnc, &bIntra); |
1757 : | edgomez | 851 | else |
1758 : | suxen_drol | 295 | bIntra = |
1759 : | MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, | ||
1760 : | &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
1761 : | iLimit); | ||
1762 : | edgomez | 851 | |
1763 : | Isibaar | 3 | stop_motion_timer(); |
1764 : | |||
1765 : | edgomez | 851 | if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1766 : | |||
1767 : | if ( ( pEnc->current->global_flags & XVID_GMC ) | ||
1768 : | && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) | ||
1769 : | { | ||
1770 : | pEnc->current->coding_type = S_VOP; | ||
1771 : | |||
1772 : | generate_GMCparameters( 2, 16, &pEnc->current->warp, | ||
1773 : | pEnc->mbParam.width, pEnc->mbParam.height, | ||
1774 : | &pEnc->current->gmc_data); | ||
1775 : | |||
1776 : | generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, | ||
1777 : | pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, | ||
1778 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, | ||
1779 : | pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0, | ||
1780 : | pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); | ||
1781 : | |||
1782 : | h | 101 | } |
1783 : | Isibaar | 3 | |
1784 : | edgomez | 851 | set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1785 : | edgomez | 195 | if (vol_header) |
1786 : | edgomez | 851 | { BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1787 : | BitstreamPadAlways(bs); | ||
1788 : | } | ||
1789 : | Isibaar | 3 | |
1790 : | suxen_drol | 229 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1791 : | Isibaar | 3 | |
1792 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
1793 : | Isibaar | 3 | |
1794 : | edgomez | 851 | pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1795 : | pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; | ||
1796 : | Isibaar | 3 | |
1797 : | edgomez | 851 | |
1798 : | for (y = 0; y < mb_height; y++) { | ||
1799 : | for (x = 0; x < mb_width; x++) { | ||
1800 : | edgomez | 195 | MACROBLOCK *pMB = |
1801 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1802 : | Isibaar | 3 | |
1803 : | edgomez | 851 | /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
1804 : | /* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ | ||
1805 : | |||
1806 : | edgomez | 13 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1807 : | Isibaar | 3 | |
1808 : | edgomez | 851 | if (bIntra) { |
1809 : | Isibaar | 3 | CodeIntraMB(pEnc, pMB); |
1810 : | edgomez | 195 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1811 : | dct_codes, qcoeff); | ||
1812 : | chl | 419 | |
1813 : | start_timer(); | ||
1814 : | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); | ||
1815 : | stop_prediction_timer(); | ||
1816 : | edgomez | 851 | |
1817 : | pEnc->current->sStat.kblks++; | ||
1818 : | |||
1819 : | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); | ||
1820 : | stop_coding_timer(); | ||
1821 : | continue; | ||
1822 : | Isibaar | 3 | } |
1823 : | edgomez | 851 | |
1824 : | if (pEnc->current->coding_type == S_VOP) { | ||
1825 : | Isibaar | 3 | |
1826 : | edgomez | 851 | int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1827 : | pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, | ||
1828 : | pEnc->mbParam.edged_width, 65536); | ||
1829 : | |||
1830 : | if (pEnc->current->motion_flags & PMV_CHROMA16) { | ||
1831 : | iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, | ||
1832 : | pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); | ||
1833 : | Isibaar | 3 | |
1834 : | edgomez | 851 | iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1835 : | pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); | ||
1836 : | } | ||
1837 : | |||
1838 : | if (iSAD <= pMB->sad16) { /* mode decision GMC */ | ||
1839 : | |||
1840 : | if (pEnc->mbParam.m_quarterpel) | ||
1841 : | pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; | ||
1842 : | else | ||
1843 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; | ||
1844 : | |||
1845 : | pMB->mode = MODE_INTER; | ||
1846 : | pMB->mcsel = 1; | ||
1847 : | pMB->sad16 = iSAD; | ||
1848 : | } else { | ||
1849 : | pMB->mcsel = 0; | ||
1850 : | } | ||
1851 : | } else { | ||
1852 : | pMB->mcsel = 0; /* just a precaution */ | ||
1853 : | } | ||
1854 : | |||
1855 : | Isibaar | 3 | start_timer(); |
1856 : | edgomez | 851 | MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1857 : | &pEnc->vInterH, &pEnc->vInterV, | ||
1858 : | &pEnc->vInterHV, &pEnc->vGMC, | ||
1859 : | &pEnc->current->image, | ||
1860 : | dct_codes, pEnc->mbParam.width, | ||
1861 : | pEnc->mbParam.height, | ||
1862 : | pEnc->mbParam.edged_width, | ||
1863 : | pEnc->mbParam.m_quarterpel, | ||
1864 : | (pEnc->current->global_flags & XVID_REDUCED), | ||
1865 : | pEnc->current->rounding_type); | ||
1866 : | chl | 347 | |
1867 : | edgomez | 851 | stop_comp_timer(); |
1868 : | |||
1869 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
1870 : | if (pMB->dquant != NO_CHANGE) { | ||
1871 : | pMB->mode = MODE_INTER_Q; | ||
1872 : | pEnc->current->quant += DQtab[pMB->dquant]; | ||
1873 : | if (pEnc->current->quant > 31) | ||
1874 : | pEnc->current->quant = 31; | ||
1875 : | else if (pEnc->current->quant < 1) | ||
1876 : | pEnc->current->quant = 1; | ||
1877 : | } | ||
1878 : | } | ||
1879 : | pMB->quant = pEnc->current->quant; | ||
1880 : | |||
1881 : | pMB->field_pred = 0; | ||
1882 : | |||
1883 : | if (pMB->mode != MODE_NOT_CODED) | ||
1884 : | { pMB->cbp = | ||
1885 : | MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, | ||
1886 : | dct_codes, qcoeff); | ||
1887 : | } | ||
1888 : | |||
1889 : | if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || | ||
1890 : | pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || | ||
1891 : | pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { | ||
1892 : | pEnc->current->sStat.mblks++; | ||
1893 : | } else { | ||
1894 : | pEnc->current->sStat.ublks++; | ||
1895 : | } | ||
1896 : | |||
1897 : | start_timer(); | ||
1898 : | |||
1899 : | chl | 347 | /* Finished processing the MB, now check if to CODE or SKIP */ |
1900 : | |||
1901 : | edgomez | 851 | skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1902 : | (pMB->dquant == NO_CHANGE); | ||
1903 : | |||
1904 : | if (pEnc->current->coding_type == S_VOP) | ||
1905 : | skip_possible &= (pMB->mcsel == 1); | ||
1906 : | else if (pEnc->current->coding_type == P_VOP) { | ||
1907 : | if (pEnc->mbParam.m_quarterpel) | ||
1908 : | skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); | ||
1909 : | else | ||
1910 : | skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); | ||
1911 : | } | ||
1912 : | chl | 347 | |
1913 : | edgomez | 851 | if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1914 : | chl | 347 | |
1915 : | edgomez | 851 | /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1916 : | |||
1917 : | if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ | ||
1918 : | { | ||
1919 : | int bSkip = 1; | ||
1920 : | |||
1921 : | for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) | ||
1922 : | { | ||
1923 : | int iSAD; | ||
1924 : | iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, | ||
1925 : | pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, | ||
1926 : | pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); | ||
1927 : | if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) | ||
1928 : | { bSkip = 0; | ||
1929 : | break; | ||
1930 : | } | ||
1931 : | } | ||
1932 : | |||
1933 : | if (!bSkip) { /* no SKIP, but trivial block */ | ||
1934 : | if(pEnc->mbParam.m_quarterpel) { | ||
1935 : | VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); | ||
1936 : | pMB->pmvs[0].x = - predMV.x; | ||
1937 : | pMB->pmvs[0].y = - predMV.y; | ||
1938 : | } | ||
1939 : | else { | ||
1940 : | VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); | ||
1941 : | pMB->pmvs[0].x = - predMV.x; | ||
1942 : | pMB->pmvs[0].y = - predMV.y; | ||
1943 : | } | ||
1944 : | pMB->mode = MODE_INTER; | ||
1945 : | pMB->cbp = 0; | ||
1946 : | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); | ||
1947 : | stop_coding_timer(); | ||
1948 : | |||
1949 : | continue; /* next MB */ | ||
1950 : | } | ||
1951 : | } | ||
1952 : | /* do SKIP */ | ||
1953 : | |||
1954 : | pMB->mode = MODE_NOT_CODED; | ||
1955 : | MBSkip(bs); | ||
1956 : | stop_coding_timer(); | ||
1957 : | continue; /* next MB */ | ||
1958 : | edgomez | 403 | } |
1959 : | edgomez | 851 | /* ordinary case: normal coded INTER/INTER4V block */ |
1960 : | |||
1961 : | if (pEnc->current->global_flags & XVID_GREYSCALE) | ||
1962 : | { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ | ||
1963 : | qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ | ||
1964 : | qcoeff[5*64+0]=0; | ||
1965 : | } | ||
1966 : | |||
1967 : | if(pEnc->mbParam.m_quarterpel) { | ||
1968 : | VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); | ||
1969 : | pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; | ||
1970 : | pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; | ||
1971 : | DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); | ||
1972 : | } else { | ||
1973 : | VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); | ||
1974 : | pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; | ||
1975 : | pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; | ||
1976 : | DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); | ||
1977 : | } | ||
1978 : | |||
1979 : | |||
1980 : | if (pMB->mode == MODE_INTER4V) | ||
1981 : | { int k; | ||
1982 : | for (k=1;k<4;k++) | ||
1983 : | { | ||
1984 : | if(pEnc->mbParam.m_quarterpel) { | ||
1985 : | VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); | ||
1986 : | pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; | ||
1987 : | pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; | ||
1988 : | DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); | ||
1989 : | } else { | ||
1990 : | VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); | ||
1991 : | pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; | ||
1992 : | pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; | ||
1993 : | DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); | ||
1994 : | } | ||
1995 : | |||
1996 : | chl | 353 | } |
1997 : | chl | 347 | } |
1998 : | edgomez | 851 | |
1999 : | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); | ||
2000 : | stop_coding_timer(); | ||
2001 : | chl | 347 | |
2002 : | Isibaar | 3 | } |
2003 : | } | ||
2004 : | |||
2005 : | edgomez | 851 | if ((pEnc->current->global_flags & XVID_REDUCED)) |
2006 : | { | ||
2007 : | image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, | ||
2008 : | pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, | ||
2009 : | 16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); | ||
2010 : | } | ||
2011 : | |||
2012 : | Isibaar | 3 | emms(); |
2013 : | |||
2014 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
2015 : | h | 101 | HintedMEGet(pEnc, 0); |
2016 : | } | ||
2017 : | |||
2018 : | edgomez | 851 | if (pEnc->current->sStat.iMvCount == 0) |
2019 : | pEnc->current->sStat.iMvCount = 1; | ||
2020 : | Isibaar | 3 | |
2021 : | edgomez | 851 | fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); |
2022 : | Isibaar | 3 | |
2023 : | suxen_drol | 136 | iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
2024 : | Isibaar | 3 | |
2025 : | edgomez | 195 | if ((fSigma > iSearchRange / 3) |
2026 : | edgomez | 867 | && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */ |
2027 : | edgomez | 13 | { |
2028 : | suxen_drol | 136 | pEnc->mbParam.m_fcode++; |
2029 : | Isibaar | 3 | iSearchRange *= 2; |
2030 : | edgomez | 195 | } else if ((fSigma < iSearchRange / 6) |
2031 : | edgomez | 851 | && (pEnc->fMvPrevSigma >= 0) |
2032 : | && (pEnc->fMvPrevSigma < iSearchRange / 6) | ||
2033 : | edgomez | 867 | && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) /* minimum search range 16 */ |
2034 : | edgomez | 13 | { |
2035 : | suxen_drol | 136 | pEnc->mbParam.m_fcode--; |
2036 : | Isibaar | 3 | iSearchRange /= 2; |
2037 : | edgomez | 13 | } |
2038 : | Isibaar | 3 | |
2039 : | edgomez | 851 | pEnc->fMvPrevSigma = fSigma; |
2040 : | edgomez | 195 | |
2041 : | edgomez | 851 | /* frame drop code */ |
2042 : | DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); | ||
2043 : | if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < | ||
2044 : | (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) | ||
2045 : | { | ||
2046 : | pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; | ||
2047 : | pEnc->current->sStat.ublks = mb_width * mb_height; | ||
2048 : | |||
2049 : | BitstreamReset(bs); | ||
2050 : | |||
2051 : | set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); | ||
2052 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); | ||
2053 : | |||
2054 : | edgomez | 867 | /* copy reference frame details into the current frame */ |
2055 : | edgomez | 851 | pEnc->current->quant = pEnc->reference->quant; |
2056 : | pEnc->current->motion_flags = pEnc->reference->motion_flags; | ||
2057 : | pEnc->current->rounding_type = pEnc->reference->rounding_type; | ||
2058 : | pEnc->current->quarterpel = pEnc->reference->quarterpel; | ||
2059 : | pEnc->current->fcode = pEnc->reference->fcode; | ||
2060 : | pEnc->current->bcode = pEnc->reference->bcode; | ||
2061 : | image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
2062 : | memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); | ||
2063 : | } | ||
2064 : | |||
2065 : | /* XXX: debug | ||
2066 : | { | ||
2067 : | char s[100]; | ||
2068 : | sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); | ||
2069 : | image_dump_yuvpgm(&pEnc->current->image, | ||
2070 : | pEnc->mbParam.edged_width, | ||
2071 : | pEnc->mbParam.width, pEnc->mbParam.height, s); | ||
2072 : | |||
2073 : | sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); | ||
2074 : | image_dump_yuvpgm(&pEnc->reference->image, | ||
2075 : | pEnc->mbParam.edged_width, | ||
2076 : | pEnc->mbParam.width, pEnc->mbParam.height, s); | ||
2077 : | } | ||
2078 : | */ | ||
2079 : | |||
2080 : | |||
2081 : | Isibaar | 3 | *pBits = BitstreamPos(bs) - *pBits; |
2082 : | |||
2083 : | edgomez | 867 | return 0; /* inter */ |
2084 : | edgomez | 851 | } |
2085 : | suxen_drol | 118 | |
2086 : | edgomez | 851 | |
2087 : | static void | ||
2088 : | FrameCodeB(Encoder * pEnc, | ||
2089 : | FRAMEINFO * frame, | ||
2090 : | Bitstream * bs, | ||
2091 : | uint32_t * pBits) | ||
2092 : | { | ||
2093 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
2094 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
2095 : | uint32_t x, y; | ||
2096 : | |||
2097 : | IMAGE *f_ref = &pEnc->reference->image; | ||
2098 : | IMAGE *b_ref = &pEnc->current->image; | ||
2099 : | |||
2100 : | #ifdef BFRAMES_DEC_DEBUG | ||
2101 : | FILE *fp; | ||
2102 : | static char first=0; | ||
2103 : | #define BFRAME_DEBUG if (!first && fp){ \ | ||
2104 : | fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ | ||
2105 : | } | ||
2106 : | |||
2107 : | pEnc->current->global_flags &= ~XVID_REDUCED; /* reduced resoltion not yet supported */ | ||
2108 : | |||
2109 : | if (!first){ | ||
2110 : | fp=fopen("C:\\XVIDDBGE.TXT","w"); | ||
2111 : | } | ||
2112 : | #endif | ||
2113 : | |||
2114 : | frame->quarterpel = pEnc->mbParam.m_quarterpel; | ||
2115 : | |||
2116 : | edgomez | 867 | /* forward */ |
2117 : | edgomez | 851 | image_setedges(f_ref, pEnc->mbParam.edged_width, |
2118 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
2119 : | pEnc->mbParam.height); | ||
2120 : | start_timer(); | ||
2121 : | image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
2122 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, | ||
2123 : | pEnc->mbParam.m_quarterpel, 0); | ||
2124 : | stop_inter_timer(); | ||
2125 : | |||
2126 : | edgomez | 867 | /* backward */ |
2127 : | edgomez | 851 | image_setedges(b_ref, pEnc->mbParam.edged_width, |
2128 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
2129 : | pEnc->mbParam.height); | ||
2130 : | start_timer(); | ||
2131 : | image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
2132 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, | ||
2133 : | pEnc->mbParam.m_quarterpel, 0); | ||
2134 : | stop_inter_timer(); | ||
2135 : | |||
2136 : | start_timer(); | ||
2137 : | |||
2138 : | MotionEstimationBVOP(&pEnc->mbParam, frame, | ||
2139 : | edgomez | 867 | ((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
2140 : | ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ | ||
2141 : | pEnc->reference->mbs, f_ref, | ||
2142 : | edgomez | 851 | &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2143 : | pEnc->current, b_ref, &pEnc->vInterH, | ||
2144 : | &pEnc->vInterV, &pEnc->vInterHV); | ||
2145 : | |||
2146 : | |||
2147 : | stop_motion_timer(); | ||
2148 : | |||
2149 : | edgomez | 867 | /* |
2150 : | if (test_quant_type(&pEnc->mbParam, pEnc->current)) { | ||
2151 : | BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); | ||
2152 : | } | ||
2153 : | */ | ||
2154 : | edgomez | 851 | |
2155 : | frame->coding_type = B_VOP; | ||
2156 : | |||
2157 : | set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); | ||
2158 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); | ||
2159 : | |||
2160 : | *pBits = BitstreamPos(bs); | ||
2161 : | |||
2162 : | frame->sStat.iTextBits = 0; | ||
2163 : | frame->sStat.iMvSum = 0; | ||
2164 : | frame->sStat.iMvCount = 0; | ||
2165 : | frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; | ||
2166 : | |||
2167 : | |||
2168 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) { | ||
2169 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) { | ||
2170 : | MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; | ||
2171 : | int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0; | ||
2172 : | |||
2173 : | edgomez | 867 | /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
2174 : | edgomez | 851 | if (mb->mode == MODE_NOT_CODED) { |
2175 : | edgomez | 867 | /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */ |
2176 : | edgomez | 851 | continue; |
2177 : | } | ||
2178 : | |||
2179 : | if (mb->mode != MODE_DIRECT_NONE_MV) { | ||
2180 : | MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, | ||
2181 : | f_ref, &pEnc->f_refh, &pEnc->f_refv, | ||
2182 : | &pEnc->f_refhv, b_ref, &pEnc->vInterH, | ||
2183 : | &pEnc->vInterV, &pEnc->vInterHV, | ||
2184 : | dct_codes); | ||
2185 : | |||
2186 : | if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; | ||
2187 : | mb->quant = frame->quant; | ||
2188 : | |||
2189 : | mb->cbp = | ||
2190 : | MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); | ||
2191 : | |||
2192 : | if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) | ||
2193 : | && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { | ||
2194 : | edgomez | 867 | mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
2195 : | edgomez | 851 | } |
2196 : | } | ||
2197 : | |||
2198 : | #ifdef BFRAMES_DEC_DEBUG | ||
2199 : | BFRAME_DEBUG | ||
2200 : | #endif | ||
2201 : | start_timer(); | ||
2202 : | MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, | ||
2203 : | &frame->sStat, direction); | ||
2204 : | stop_coding_timer(); | ||
2205 : | } | ||
2206 : | } | ||
2207 : | |||
2208 : | emms(); | ||
2209 : | |||
2210 : | edgomez | 867 | /* TODO: dynamic fcode/bcode ??? */ |
2211 : | edgomez | 851 | |
2212 : | *pBits = BitstreamPos(bs) - *pBits; | ||
2213 : | |||
2214 : | #ifdef BFRAMES_DEC_DEBUG | ||
2215 : | if (!first){ | ||
2216 : | first=1; | ||
2217 : | if (fp) | ||
2218 : | fclose(fp); | ||
2219 : | } | ||
2220 : | #endif | ||
2221 : | suxen_drol | 118 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |