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