Parent Directory | Revision Log
Revision 229 -
(view)
(download)
Original Path: trunk/xvidcore/src/encoder.c
1 : | edgomez | 194 | /***************************************************************************** |
2 : | edgomez | 145 | * |
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | * - Encoder main module - | ||
5 : | * | ||
6 : | * This program is an implementation of a part of one or more MPEG-4 | ||
7 : | * Video tools as specified in ISO/IEC 14496-2 standard. Those intending | ||
8 : | * to use this software module in hardware or software products are | ||
9 : | * advised that its use may infringe existing patents or copyrights, and | ||
10 : | * any such use would be at such party's own risk. The original | ||
11 : | * developer of this software module and his/her company, and subsequent | ||
12 : | * editors and their companies, will have no liability for use of this | ||
13 : | * software or modifications or derivatives thereof. | ||
14 : | * | ||
15 : | * This program is free software; you can redistribute it and/or modify | ||
16 : | * it under the terms of the GNU General Public License as published by | ||
17 : | * the Free Software Foundation; either version 2 of the License, or | ||
18 : | * (at your option) any later version. | ||
19 : | * | ||
20 : | * This program is distributed in the hope that it will be useful, | ||
21 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
22 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
23 : | * GNU General Public License for more details. | ||
24 : | * | ||
25 : | * You should have received a copy of the GNU General Public License | ||
26 : | * along with this program; if not, write to the Free Software | ||
27 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
28 : | * | ||
29 : | edgomez | 194 | ****************************************************************************/ |
30 : | suxen_drol | 118 | |
31 : | edgomez | 194 | /***************************************************************************** |
32 : | edgomez | 145 | * |
33 : | * History | ||
34 : | * | ||
35 : | suxen_drol | 229 | * 20.06.2002 bframe patch |
36 : | chenm001 | 168 | * 08.05.2002 fix some problem in DEBUG mode; |
37 : | * MinChen <chenm001@163.com> | ||
38 : | edgomez | 145 | * 14.04.2002 added FrameCodeB() |
39 : | * | ||
40 : | suxen_drol | 229 | * $Id: encoder.c,v 1.44 2002-06-20 14:05:57 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 : | pEnc->mbParam.m_seconds++; | ||
613 : | pEnc->mbParam.m_ticks = 0; | ||
614 : | } | ||
615 : | |||
616 : | } | ||
617 : | #endif | ||
618 : | |||
619 : | |||
620 : | #ifdef BFRAMES | ||
621 : | void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) | ||
622 : | { | ||
623 : | if (pEnc->queue_size >= pEnc->mbParam.max_bframes) | ||
624 : | { | ||
625 : | DPRINTF("FATAL: QUEUE FULL"); | ||
626 : | return; | ||
627 : | } | ||
628 : | |||
629 : | DPRINTF("*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
630 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
631 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
632 : | |||
633 : | |||
634 : | start_timer(); | ||
635 : | if (image_input | ||
636 : | (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, | ||
637 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) | ||
638 : | return; | ||
639 : | stop_conv_timer(); | ||
640 : | |||
641 : | pEnc->queue_size++; | ||
642 : | pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; | ||
643 : | } | ||
644 : | #endif | ||
645 : | |||
646 : | |||
647 : | #ifdef BFRAMES | ||
648 : | edgomez | 194 | /***************************************************************************** |
649 : | suxen_drol | 229 | * IPB frame encoder entry point |
650 : | edgomez | 194 | * |
651 : | * Returned values : | ||
652 : | * - XVID_ERR_OK - no errors | ||
653 : | * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong | ||
654 : | * format | ||
655 : | ****************************************************************************/ | ||
656 : | suxen_drol | 152 | |
657 : | edgomez | 194 | int |
658 : | suxen_drol | 229 | encoder_encode_bframes(Encoder * pEnc, |
659 : | edgomez | 195 | XVID_ENC_FRAME * pFrame, |
660 : | XVID_ENC_STATS * pResult) | ||
661 : | Isibaar | 3 | { |
662 : | edgomez | 13 | uint16_t x, y; |
663 : | Bitstream bs; | ||
664 : | uint32_t bits; | ||
665 : | suxen_drol | 152 | |
666 : | suxen_drol | 229 | int input_valid = 1; |
667 : | |||
668 : | #ifdef _DEBUG_PSNR | ||
669 : | chenm001 | 168 | float psnr; |
670 : | char temp[128]; | ||
671 : | #endif | ||
672 : | |||
673 : | suxen_drol | 152 | ENC_CHECK(pEnc); |
674 : | ENC_CHECK(pFrame); | ||
675 : | suxen_drol | 229 | ENC_CHECK(pFrame->image); |
676 : | suxen_drol | 152 | |
677 : | start_global_timer(); | ||
678 : | edgomez | 195 | |
679 : | suxen_drol | 152 | BitstreamInit(&bs, pFrame->bitstream, 0); |
680 : | |||
681 : | suxen_drol | 229 | ipvop_loop: |
682 : | |||
683 : | edgomez | 194 | /* |
684 : | * bframe "flush" code | ||
685 : | */ | ||
686 : | suxen_drol | 152 | |
687 : | edgomez | 195 | if ((pFrame->image == NULL || pEnc->flush_bframes) |
688 : | && (pEnc->bframenum_head < pEnc->bframenum_tail)) { | ||
689 : | |||
690 : | if (pEnc->flush_bframes == 0) { | ||
691 : | edgomez | 194 | /* |
692 : | * we have reached the end of stream without getting | ||
693 : | * a future reference frame... so encode last final | ||
694 : | * frame as a pframe | ||
695 : | */ | ||
696 : | |||
697 : | suxen_drol | 229 | DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
698 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
699 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
700 : | |||
701 : | suxen_drol | 152 | pEnc->bframenum_tail--; |
702 : | SWAP(pEnc->current, pEnc->reference); | ||
703 : | |||
704 : | edgomez | 195 | SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
705 : | suxen_drol | 152 | |
706 : | FrameCodeP(pEnc, &bs, &bits, 1, 0); | ||
707 : | |||
708 : | BitstreamPad(&bs); | ||
709 : | pFrame->length = BitstreamLength(&bs); | ||
710 : | pFrame->intra = 0; | ||
711 : | edgomez | 194 | |
712 : | suxen_drol | 152 | return XVID_ERR_OK; |
713 : | } | ||
714 : | |||
715 : | suxen_drol | 229 | |
716 : | DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
717 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
718 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
719 : | |||
720 : | edgomez | 195 | FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
721 : | suxen_drol | 152 | pEnc->bframenum_head++; |
722 : | |||
723 : | BitstreamPad(&bs); | ||
724 : | pFrame->length = BitstreamLength(&bs); | ||
725 : | pFrame->intra = 0; | ||
726 : | edgomez | 194 | |
727 : | suxen_drol | 229 | if (input_valid) |
728 : | queue_image(pEnc, pFrame); | ||
729 : | |||
730 : | suxen_drol | 152 | return XVID_ERR_OK; |
731 : | } | ||
732 : | |||
733 : | suxen_drol | 229 | if (pEnc->bframenum_head > 0) { |
734 : | pEnc->bframenum_head = pEnc->bframenum_tail = 0; | ||
735 : | |||
736 : | if (pEnc->packed) { | ||
737 : | |||
738 : | DPRINTF("*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
739 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
740 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
741 : | |||
742 : | |||
743 : | BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); | ||
744 : | BitstreamPad(&bs); | ||
745 : | BitstreamPutBits(&bs, 0x7f, 8); | ||
746 : | |||
747 : | pFrame->length = BitstreamLength(&bs); | ||
748 : | pFrame->intra = 0; | ||
749 : | |||
750 : | if (input_valid) | ||
751 : | queue_image(pEnc, pFrame); | ||
752 : | |||
753 : | return XVID_ERR_OK; | ||
754 : | } | ||
755 : | } | ||
756 : | |||
757 : | |||
758 : | bvop_loop: | ||
759 : | |||
760 : | if (input_valid) { | ||
761 : | |||
762 : | SWAP(pEnc->current, pEnc->reference); | ||
763 : | |||
764 : | start_timer(); | ||
765 : | if (image_input | ||
766 : | (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
767 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) | ||
768 : | return XVID_ERR_FORMAT; | ||
769 : | stop_conv_timer(); | ||
770 : | |||
771 : | // queue input frame, and dequue next image | ||
772 : | if (pEnc->queue_size > 0) | ||
773 : | { | ||
774 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); | ||
775 : | if (pEnc->queue_head != pEnc->queue_tail) | ||
776 : | { | ||
777 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); | ||
778 : | } | ||
779 : | pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; | ||
780 : | pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; | ||
781 : | } | ||
782 : | |||
783 : | } else if (pEnc->queue_size > 0) { | ||
784 : | |||
785 : | SWAP(pEnc->current, pEnc->reference); | ||
786 : | |||
787 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); | ||
788 : | pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; | ||
789 : | pEnc->queue_size--; | ||
790 : | |||
791 : | } else if (BitstreamPos(&bs) == 0) { | ||
792 : | |||
793 : | DPRINTF("*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", | ||
794 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
795 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
796 : | |||
797 : | |||
798 : | suxen_drol | 152 | pFrame->intra = 0; |
799 : | edgomez | 194 | |
800 : | suxen_drol | 229 | BitstreamPutBits(&bs, 0x7f, 8); |
801 : | BitstreamPad(&bs); | ||
802 : | pFrame->length = BitstreamLength(&bs); | ||
803 : | |||
804 : | suxen_drol | 152 | return XVID_ERR_OK; |
805 : | |||
806 : | suxen_drol | 229 | } else { |
807 : | |||
808 : | pFrame->length = BitstreamLength(&bs); | ||
809 : | return XVID_ERR_OK; | ||
810 : | suxen_drol | 152 | } |
811 : | |||
812 : | pEnc->flush_bframes = 0; | ||
813 : | |||
814 : | edgomez | 194 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
815 : | * Well there was a separation here so i put it in ANSI C | ||
816 : | * comment style :-) | ||
817 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
818 : | suxen_drol | 152 | |
819 : | suxen_drol | 229 | //$$ SWAP(pEnc->current, pEnc->reference); |
820 : | suxen_drol | 152 | |
821 : | edgomez | 198 | emms(); |
822 : | edgomez | 194 | |
823 : | if (pFrame->quant == 0) | ||
824 : | pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); | ||
825 : | else | ||
826 : | pEnc->current->quant = pFrame->quant; | ||
827 : | |||
828 : | edgomez | 195 | if (pEnc->current->quant < 1) |
829 : | Isibaar | 157 | pEnc->current->quant = 1; |
830 : | edgomez | 195 | |
831 : | if (pEnc->current->quant > 31) | ||
832 : | Isibaar | 157 | pEnc->current->quant = 31; |
833 : | |||
834 : | suxen_drol | 152 | pEnc->current->global_flags = pFrame->general; |
835 : | pEnc->current->motion_flags = pFrame->motion; | ||
836 : | pEnc->current->seconds = pEnc->mbParam.m_seconds; | ||
837 : | pEnc->current->ticks = pEnc->mbParam.m_ticks; | ||
838 : | edgomez | 194 | /* ToDo : dynamic fcode (in both directions) */ |
839 : | suxen_drol | 152 | pEnc->current->fcode = pEnc->mbParam.m_fcode; |
840 : | edgomez | 195 | pEnc->current->bcode = pEnc->mbParam.m_fcode; |
841 : | suxen_drol | 152 | |
842 : | suxen_drol | 229 | //$$$ start_timer(); |
843 : | //$$$ if (image_input | ||
844 : | //$$$ (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
845 : | //$$$ pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) | ||
846 : | //$$$ return XVID_ERR_FORMAT; | ||
847 : | //$$$ stop_conv_timer(); | ||
848 : | suxen_drol | 152 | |
849 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
850 : | edgomez | 195 | image_copy(&pEnc->sOriginal, &pEnc->current->image, |
851 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
852 : | suxen_drol | 152 | #endif |
853 : | |||
854 : | edgomez | 198 | emms(); |
855 : | suxen_drol | 152 | |
856 : | edgomez | 194 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
857 : | * Luminance masking | ||
858 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
859 : | suxen_drol | 152 | |
860 : | edgomez | 195 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
861 : | int *temp_dquants = | ||
862 : | (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
863 : | pEnc->mbParam.mb_height * sizeof(int), | ||
864 : | CACHE_LINE); | ||
865 : | |||
866 : | edgomez | 194 | pEnc->current->quant = |
867 : | adaptive_quantization(pEnc->current->image.y, | ||
868 : | edgomez | 195 | pEnc->mbParam.edged_width, temp_dquants, |
869 : | pEnc->current->quant, pEnc->current->quant, | ||
870 : | 2 * pEnc->current->quant, | ||
871 : | pEnc->mbParam.mb_width, | ||
872 : | pEnc->mbParam.mb_height); | ||
873 : | edgomez | 194 | |
874 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
875 : | edgomez | 194 | |
876 : | edgomez | 195 | #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
877 : | |||
878 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) { | ||
879 : | MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; | ||
880 : | |||
881 : | pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; | ||
882 : | suxen_drol | 152 | } |
883 : | edgomez | 194 | |
884 : | edgomez | 195 | #undef OFFSET |
885 : | edgomez | 194 | |
886 : | } | ||
887 : | |||
888 : | suxen_drol | 152 | xvid_free(temp_dquants); |
889 : | } | ||
890 : | |||
891 : | edgomez | 194 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
892 : | * ivop/pvop/bvop selection | ||
893 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
894 : | suxen_drol | 152 | |
895 : | |||
896 : | edgomez | 195 | if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
897 : | (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && | ||
898 : | pEnc->iFrameNum >= pEnc->iMaxKeyInterval) | ||
899 : | || image_mad(&pEnc->reference->image, &pEnc->current->image, | ||
900 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
901 : | pEnc->mbParam.height) > 30) { | ||
902 : | edgomez | 194 | /* |
903 : | * This will be coded as an Intra Frame | ||
904 : | */ | ||
905 : | suxen_drol | 152 | |
906 : | suxen_drol | 229 | DPRINTF("*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
907 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
908 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
909 : | edgomez | 194 | |
910 : | suxen_drol | 152 | FrameCodeI(pEnc, &bs, &bits); |
911 : | |||
912 : | pFrame->intra = 1; | ||
913 : | pEnc->flush_bframes = 1; | ||
914 : | |||
915 : | suxen_drol | 229 | inc_frame_num(pEnc); |
916 : | |||
917 : | if (pEnc->packed) { | ||
918 : | BitstreamPad(&bs); | ||
919 : | input_valid = 0; | ||
920 : | goto ipvop_loop; | ||
921 : | } | ||
922 : | |||
923 : | edgomez | 194 | /* |
924 : | * NB : sequences like "IIBB" decode fine with msfdam but, | ||
925 : | * go screwy with divx 5.00 | ||
926 : | */ | ||
927 : | edgomez | 195 | } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
928 : | edgomez | 194 | /* |
929 : | * This will be coded as a Predicted Frame | ||
930 : | */ | ||
931 : | suxen_drol | 152 | |
932 : | suxen_drol | 229 | DPRINTF("*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
933 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
934 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
935 : | edgomez | 194 | |
936 : | suxen_drol | 152 | FrameCodeP(pEnc, &bs, &bits, 1, 0); |
937 : | pFrame->intra = 0; | ||
938 : | pEnc->flush_bframes = 1; | ||
939 : | suxen_drol | 229 | |
940 : | inc_frame_num(pEnc); | ||
941 : | |||
942 : | if (pEnc->packed) { | ||
943 : | BitstreamPad(&bs); | ||
944 : | input_valid = 0; | ||
945 : | goto ipvop_loop; | ||
946 : | } | ||
947 : | |||
948 : | edgomez | 195 | } else { |
949 : | edgomez | 194 | /* |
950 : | * This will be coded as a Bidirectional Frame | ||
951 : | */ | ||
952 : | suxen_drol | 152 | |
953 : | suxen_drol | 229 | DPRINTF("*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
954 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
955 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
956 : | edgomez | 194 | |
957 : | edgomez | 195 | if (pFrame->bquant < 1) { |
958 : | edgomez | 194 | pEnc->current->quant = |
959 : | edgomez | 195 | ((pEnc->reference->quant + |
960 : | pEnc->current->quant) * pEnc->bquant_ratio) / 200; | ||
961 : | } else { | ||
962 : | suxen_drol | 152 | pEnc->current->quant = pFrame->bquant; |
963 : | } | ||
964 : | edgomez | 195 | |
965 : | edgomez | 194 | /* store frame into bframe buffer & swap ref back to current */ |
966 : | suxen_drol | 152 | SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
967 : | SWAP(pEnc->current, pEnc->reference); | ||
968 : | |||
969 : | pEnc->bframenum_tail++; | ||
970 : | |||
971 : | pFrame->intra = 0; | ||
972 : | pFrame->length = 0; | ||
973 : | |||
974 : | suxen_drol | 229 | inc_frame_num(pEnc); |
975 : | edgomez | 194 | |
976 : | suxen_drol | 229 | input_valid = 0; |
977 : | goto bvop_loop; | ||
978 : | suxen_drol | 152 | } |
979 : | |||
980 : | BitstreamPad(&bs); | ||
981 : | pFrame->length = BitstreamLength(&bs); | ||
982 : | |||
983 : | edgomez | 195 | if (pResult) { |
984 : | suxen_drol | 152 | pResult->quant = pEnc->current->quant; |
985 : | edgomez | 195 | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
986 : | suxen_drol | 152 | pResult->kblks = pEnc->sStat.kblks; |
987 : | pResult->mblks = pEnc->sStat.mblks; | ||
988 : | pResult->ublks = pEnc->sStat.ublks; | ||
989 : | } | ||
990 : | |||
991 : | edgomez | 198 | emms(); |
992 : | edgomez | 194 | |
993 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
994 : | edgomez | 195 | psnr = |
995 : | image_psnr(&pEnc->sOriginal, &pEnc->current->image, | ||
996 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
997 : | pEnc->mbParam.height); | ||
998 : | suxen_drol | 152 | |
999 : | edgomez | 194 | snprintf(temp, 127, "PSNR: %f\n", psnr); |
1000 : | suxen_drol | 152 | DEBUG(temp); |
1001 : | #endif | ||
1002 : | |||
1003 : | edgomez | 195 | if (pFrame->quant == 0) { |
1004 : | RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, | ||
1005 : | pFrame->length, pFrame->intra); | ||
1006 : | suxen_drol | 152 | } |
1007 : | |||
1008 : | edgomez | 195 | |
1009 : | suxen_drol | 152 | stop_global_timer(); |
1010 : | write_timer(); | ||
1011 : | |||
1012 : | return XVID_ERR_OK; | ||
1013 : | } | ||
1014 : | suxen_drol | 229 | |
1015 : | #endif | ||
1016 : | |||
1017 : | |||
1018 : | |||
1019 : | edgomez | 194 | /***************************************************************************** |
1020 : | suxen_drol | 229 | * "original" IP frame encoder entry point |
1021 : | * | ||
1022 : | * Returned values : | ||
1023 : | * - XVID_ERR_OK - no errors | ||
1024 : | * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong | ||
1025 : | * format | ||
1026 : | edgomez | 194 | ****************************************************************************/ |
1027 : | suxen_drol | 229 | |
1028 : | edgomez | 194 | int |
1029 : | encoder_encode(Encoder * pEnc, | ||
1030 : | edgomez | 195 | XVID_ENC_FRAME * pFrame, |
1031 : | XVID_ENC_STATS * pResult) | ||
1032 : | suxen_drol | 152 | { |
1033 : | uint16_t x, y; | ||
1034 : | Bitstream bs; | ||
1035 : | uint32_t bits; | ||
1036 : | Isibaar | 4 | uint16_t write_vol_header = 0; |
1037 : | edgomez | 195 | |
1038 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
1039 : | Isibaar | 113 | float psnr; |
1040 : | edgomez | 194 | uint8_t temp[128]; |
1041 : | Isibaar | 113 | #endif |
1042 : | Isibaar | 3 | |
1043 : | start_global_timer(); | ||
1044 : | |||
1045 : | edgomez | 13 | ENC_CHECK(pEnc); |
1046 : | ENC_CHECK(pFrame); | ||
1047 : | ENC_CHECK(pFrame->bitstream); | ||
1048 : | ENC_CHECK(pFrame->image); | ||
1049 : | Isibaar | 3 | |
1050 : | suxen_drol | 136 | SWAP(pEnc->current, pEnc->reference); |
1051 : | |||
1052 : | pEnc->current->global_flags = pFrame->general; | ||
1053 : | pEnc->current->motion_flags = pFrame->motion; | ||
1054 : | suxen_drol | 229 | #ifdef BFRAMES |
1055 : | pEnc->current->seconds = pEnc->mbParam.m_seconds; | ||
1056 : | pEnc->current->ticks = pEnc->mbParam.m_ticks; | ||
1057 : | #endif | ||
1058 : | h | 101 | pEnc->mbParam.hint = &pFrame->hint; |
1059 : | Isibaar | 3 | |
1060 : | start_timer(); | ||
1061 : | edgomez | 195 | if (image_input |
1062 : | (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
1063 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) | ||
1064 : | Isibaar | 3 | return XVID_ERR_FORMAT; |
1065 : | stop_conv_timer(); | ||
1066 : | |||
1067 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
1068 : | edgomez | 195 | image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1069 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
1070 : | Isibaar | 113 | #endif |
1071 : | |||
1072 : | edgomez | 198 | emms(); |
1073 : | suxen_drol | 136 | |
1074 : | edgomez | 13 | BitstreamInit(&bs, pFrame->bitstream, 0); |
1075 : | Isibaar | 3 | |
1076 : | edgomez | 195 | if (pFrame->quant == 0) { |
1077 : | pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); | ||
1078 : | } else { | ||
1079 : | suxen_drol | 136 | pEnc->current->quant = pFrame->quant; |
1080 : | Isibaar | 3 | } |
1081 : | |||
1082 : | edgomez | 195 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1083 : | int *temp_dquants = | ||
1084 : | (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
1085 : | pEnc->mbParam.mb_height * sizeof(int), | ||
1086 : | CACHE_LINE); | ||
1087 : | |||
1088 : | edgomez | 194 | pEnc->current->quant = |
1089 : | adaptive_quantization(pEnc->current->image.y, | ||
1090 : | edgomez | 195 | pEnc->mbParam.edged_width, temp_dquants, |
1091 : | pEnc->current->quant, pEnc->current->quant, | ||
1092 : | 2 * pEnc->current->quant, | ||
1093 : | pEnc->mbParam.mb_width, | ||
1094 : | pEnc->mbParam.mb_height); | ||
1095 : | edgomez | 194 | |
1096 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1097 : | edgomez | 194 | |
1098 : | edgomez | 195 | #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
1099 : | edgomez | 194 | |
1100 : | edgomez | 195 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1101 : | edgomez | 194 | |
1102 : | edgomez | 195 | |
1103 : | MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; | ||
1104 : | |||
1105 : | pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; | ||
1106 : | Isibaar | 3 | } |
1107 : | edgomez | 194 | |
1108 : | edgomez | 195 | #undef OFFSET |
1109 : | edgomez | 194 | } |
1110 : | |||
1111 : | Isibaar | 41 | xvid_free(temp_dquants); |
1112 : | Isibaar | 3 | } |
1113 : | |||
1114 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_H263QUANT) { |
1115 : | if (pEnc->mbParam.m_quant_type != H263_QUANT) | ||
1116 : | Isibaar | 20 | write_vol_header = 1; |
1117 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = H263_QUANT; |
1118 : | edgomez | 195 | } else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
1119 : | edgomez | 194 | int matrix1_changed, matrix2_changed; |
1120 : | Isibaar | 3 | |
1121 : | edgomez | 194 | matrix1_changed = matrix2_changed = 0; |
1122 : | edgomez | 78 | |
1123 : | edgomez | 195 | if (pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
1124 : | Isibaar | 20 | write_vol_header = 1; |
1125 : | edgomez | 195 | |
1126 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
1127 : | edgomez | 195 | |
1128 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1129 : | edgomez | 195 | if (pFrame->quant_intra_matrix != NULL) |
1130 : | matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); | ||
1131 : | if (pFrame->quant_inter_matrix != NULL) | ||
1132 : | matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); | ||
1133 : | } else { | ||
1134 : | edgomez | 194 | matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
1135 : | matrix2_changed = set_inter_matrix(get_default_inter_matrix()); | ||
1136 : | Isibaar | 20 | } |
1137 : | edgomez | 195 | if (write_vol_header == 0) |
1138 : | edgomez | 194 | write_vol_header = matrix1_changed | matrix2_changed; |
1139 : | Isibaar | 4 | } |
1140 : | Isibaar | 3 | |
1141 : | edgomez | 195 | if (pFrame->intra < 0) { |
1142 : | if ((pEnc->iFrameNum == 0) | ||
1143 : | || ((pEnc->iMaxKeyInterval > 0) | ||
1144 : | && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { | ||
1145 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
1146 : | } else { | ||
1147 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); | ||
1148 : | edgomez | 194 | } |
1149 : | edgomez | 195 | } else { |
1150 : | if (pFrame->intra == 1) { | ||
1151 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
1152 : | } else { | ||
1153 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); | ||
1154 : | edgomez | 194 | } |
1155 : | |||
1156 : | edgomez | 13 | } |
1157 : | Isibaar | 3 | |
1158 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
1159 : | edgomez | 13 | BitstreamPutBits(&bs, 0xFFFF, 16); |
1160 : | BitstreamPad(&bs); | ||
1161 : | pFrame->length = BitstreamLength(&bs); | ||
1162 : | h | 29 | |
1163 : | edgomez | 195 | if (pResult) { |
1164 : | suxen_drol | 136 | pResult->quant = pEnc->current->quant; |
1165 : | Isibaar | 3 | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
1166 : | pResult->kblks = pEnc->sStat.kblks; | ||
1167 : | pResult->mblks = pEnc->sStat.mblks; | ||
1168 : | pResult->ublks = pEnc->sStat.ublks; | ||
1169 : | edgomez | 13 | } |
1170 : | edgomez | 195 | |
1171 : | edgomez | 198 | emms(); |
1172 : | Isibaar | 41 | |
1173 : | edgomez | 195 | if (pFrame->quant == 0) { |
1174 : | RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, | ||
1175 : | pFrame->length, pFrame->intra); | ||
1176 : | Isibaar | 3 | } |
1177 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
1178 : | edgomez | 195 | psnr = |
1179 : | image_psnr(&pEnc->sOriginal, &pEnc->current->image, | ||
1180 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
1181 : | pEnc->mbParam.height); | ||
1182 : | Isibaar | 113 | |
1183 : | edgomez | 194 | snprintf(temp, 127, "PSNR: %f\n", psnr); |
1184 : | Isibaar | 113 | DEBUG(temp); |
1185 : | #endif | ||
1186 : | |||
1187 : | suxen_drol | 229 | #ifdef BFRAMES |
1188 : | inc_frame_num(pEnc); | ||
1189 : | #else | ||
1190 : | Isibaar | 3 | pEnc->iFrameNum++; |
1191 : | suxen_drol | 229 | #endif |
1192 : | edgomez | 195 | |
1193 : | suxen_drol | 229 | |
1194 : | Isibaar | 3 | stop_global_timer(); |
1195 : | write_timer(); | ||
1196 : | |||
1197 : | return XVID_ERR_OK; | ||
1198 : | } | ||
1199 : | |||
1200 : | |||
1201 : | edgomez | 195 | static __inline void |
1202 : | CodeIntraMB(Encoder * pEnc, | ||
1203 : | MACROBLOCK * pMB) | ||
1204 : | { | ||
1205 : | Isibaar | 3 | |
1206 : | pMB->mode = MODE_INTRA; | ||
1207 : | |||
1208 : | suxen_drol | 136 | /* zero mv statistics */ |
1209 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
1210 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
1211 : | pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
1212 : | pMB->sad16 = 0; | ||
1213 : | |||
1214 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
1215 : | edgomez | 195 | if (pMB->dquant != NO_CHANGE) { |
1216 : | Isibaar | 3 | pMB->mode = MODE_INTRA_Q; |
1217 : | suxen_drol | 136 | pEnc->current->quant += DQtab[pMB->dquant]; |
1218 : | edgomez | 195 | |
1219 : | if (pEnc->current->quant > 31) | ||
1220 : | pEnc->current->quant = 31; | ||
1221 : | if (pEnc->current->quant < 1) | ||
1222 : | pEnc->current->quant = 1; | ||
1223 : | Isibaar | 3 | } |
1224 : | } | ||
1225 : | |||
1226 : | suxen_drol | 136 | pMB->quant = pEnc->current->quant; |
1227 : | Isibaar | 3 | } |
1228 : | |||
1229 : | |||
1230 : | h | 101 | #define FCODEBITS 3 |
1231 : | #define MODEBITS 5 | ||
1232 : | |||
1233 : | edgomez | 195 | void |
1234 : | HintedMESet(Encoder * pEnc, | ||
1235 : | int *intra) | ||
1236 : | h | 101 | { |
1237 : | edgomez | 195 | HINTINFO *hint; |
1238 : | h | 101 | Bitstream bs; |
1239 : | int length, high; | ||
1240 : | uint32_t x, y; | ||
1241 : | |||
1242 : | hint = pEnc->mbParam.hint; | ||
1243 : | |||
1244 : | edgomez | 195 | if (hint->rawhints) { |
1245 : | h | 101 | *intra = hint->mvhint.intra; |
1246 : | edgomez | 195 | } else { |
1247 : | h | 101 | BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1248 : | *intra = BitstreamGetBit(&bs); | ||
1249 : | } | ||
1250 : | |||
1251 : | edgomez | 195 | if (*intra) { |
1252 : | h | 101 | return; |
1253 : | } | ||
1254 : | |||
1255 : | edgomez | 195 | pEnc->current->fcode = |
1256 : | (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, | ||
1257 : | FCODEBITS); | ||
1258 : | h | 101 | |
1259 : | edgomez | 195 | length = pEnc->current->fcode + 5; |
1260 : | high = 1 << (length - 1); | ||
1261 : | h | 101 | |
1262 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1263 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
1264 : | MACROBLOCK *pMB = | ||
1265 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1266 : | MVBLOCKHINT *bhint = | ||
1267 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
1268 : | h | 101 | VECTOR pred[4]; |
1269 : | VECTOR tmp; | ||
1270 : | edgomez | 147 | int32_t dummy[4]; |
1271 : | h | 101 | int vec; |
1272 : | |||
1273 : | edgomez | 195 | pMB->mode = |
1274 : | (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, | ||
1275 : | MODEBITS); | ||
1276 : | h | 101 | |
1277 : | h | 128 | pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1278 : | pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; | ||
1279 : | |||
1280 : | edgomez | 195 | if (pMB->mode == MODE_INTER) { |
1281 : | tmp.x = | ||
1282 : | (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, | ||
1283 : | length); | ||
1284 : | tmp.y = | ||
1285 : | (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, | ||
1286 : | length); | ||
1287 : | tmp.x -= (tmp.x >= high) ? high * 2 : 0; | ||
1288 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
1289 : | h | 101 | |
1290 : | edgomez | 195 | get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, |
1291 : | 0, pred, dummy); | ||
1292 : | h | 101 | |
1293 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1294 : | pMB->mvs[vec].x = tmp.x; | ||
1295 : | pMB->mvs[vec].y = tmp.y; | ||
1296 : | h | 101 | pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
1297 : | pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; | ||
1298 : | } | ||
1299 : | edgomez | 195 | } else if (pMB->mode == MODE_INTER4V) { |
1300 : | for (vec = 0; vec < 4; ++vec) { | ||
1301 : | tmp.x = | ||
1302 : | (hint->rawhints) ? bhint->mvs[vec]. | ||
1303 : | x : BitstreamGetBits(&bs, length); | ||
1304 : | tmp.y = | ||
1305 : | (hint->rawhints) ? bhint->mvs[vec]. | ||
1306 : | y : BitstreamGetBits(&bs, length); | ||
1307 : | tmp.x -= (tmp.x >= high) ? high * 2 : 0; | ||
1308 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
1309 : | h | 101 | |
1310 : | edgomez | 195 | get_pmvdata(pEnc->current->mbs, x, y, |
1311 : | pEnc->mbParam.mb_width, vec, pred, dummy); | ||
1312 : | h | 101 | |
1313 : | edgomez | 195 | pMB->mvs[vec].x = tmp.x; |
1314 : | pMB->mvs[vec].y = tmp.y; | ||
1315 : | h | 101 | pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
1316 : | pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; | ||
1317 : | } | ||
1318 : | edgomez | 195 | } else // intra / stuffing / not_coded |
1319 : | h | 101 | { |
1320 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1321 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
1322 : | h | 101 | } |
1323 : | } | ||
1324 : | h | 128 | |
1325 : | suxen_drol | 136 | if (pMB->mode == MODE_INTER4V && |
1326 : | edgomez | 195 | (pEnc->current->global_flags & XVID_LUMIMASKING) |
1327 : | && pMB->dquant != NO_CHANGE) { | ||
1328 : | h | 128 | pMB->mode = MODE_INTRA; |
1329 : | |||
1330 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1331 : | h | 128 | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1332 : | } | ||
1333 : | } | ||
1334 : | h | 101 | } |
1335 : | } | ||
1336 : | } | ||
1337 : | |||
1338 : | |||
1339 : | edgomez | 195 | void |
1340 : | HintedMEGet(Encoder * pEnc, | ||
1341 : | int intra) | ||
1342 : | h | 101 | { |
1343 : | edgomez | 195 | HINTINFO *hint; |
1344 : | h | 101 | Bitstream bs; |
1345 : | uint32_t x, y; | ||
1346 : | int length, high; | ||
1347 : | |||
1348 : | hint = pEnc->mbParam.hint; | ||
1349 : | |||
1350 : | edgomez | 195 | if (hint->rawhints) { |
1351 : | h | 101 | hint->mvhint.intra = intra; |
1352 : | edgomez | 195 | } else { |
1353 : | h | 101 | BitstreamInit(&bs, hint->hintstream, 0); |
1354 : | BitstreamPutBit(&bs, intra); | ||
1355 : | } | ||
1356 : | |||
1357 : | edgomez | 195 | if (intra) { |
1358 : | if (!hint->rawhints) { | ||
1359 : | h | 101 | BitstreamPad(&bs); |
1360 : | hint->hintlength = BitstreamLength(&bs); | ||
1361 : | } | ||
1362 : | return; | ||
1363 : | } | ||
1364 : | |||
1365 : | edgomez | 195 | length = pEnc->current->fcode + 5; |
1366 : | high = 1 << (length - 1); | ||
1367 : | h | 101 | |
1368 : | edgomez | 195 | if (hint->rawhints) { |
1369 : | suxen_drol | 136 | hint->mvhint.fcode = pEnc->current->fcode; |
1370 : | edgomez | 195 | } else { |
1371 : | suxen_drol | 136 | BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1372 : | h | 101 | } |
1373 : | |||
1374 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1375 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
1376 : | MACROBLOCK *pMB = | ||
1377 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1378 : | MVBLOCKHINT *bhint = | ||
1379 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
1380 : | h | 101 | VECTOR tmp; |
1381 : | |||
1382 : | edgomez | 195 | if (hint->rawhints) { |
1383 : | h | 101 | bhint->mode = pMB->mode; |
1384 : | edgomez | 195 | } else { |
1385 : | h | 101 | BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1386 : | } | ||
1387 : | |||
1388 : | edgomez | 195 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
1389 : | tmp.x = pMB->mvs[0].x; | ||
1390 : | tmp.y = pMB->mvs[0].y; | ||
1391 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
1392 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
1393 : | h | 101 | |
1394 : | edgomez | 195 | if (hint->rawhints) { |
1395 : | h | 101 | bhint->mvs[0].x = tmp.x; |
1396 : | bhint->mvs[0].y = tmp.y; | ||
1397 : | edgomez | 195 | } else { |
1398 : | h | 101 | BitstreamPutBits(&bs, tmp.x, length); |
1399 : | BitstreamPutBits(&bs, tmp.y, length); | ||
1400 : | } | ||
1401 : | edgomez | 195 | } else if (pMB->mode == MODE_INTER4V) { |
1402 : | h | 101 | int vec; |
1403 : | |||
1404 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
1405 : | tmp.x = pMB->mvs[vec].x; | ||
1406 : | tmp.y = pMB->mvs[vec].y; | ||
1407 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
1408 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
1409 : | h | 101 | |
1410 : | edgomez | 195 | if (hint->rawhints) { |
1411 : | h | 101 | bhint->mvs[vec].x = tmp.x; |
1412 : | bhint->mvs[vec].y = tmp.y; | ||
1413 : | edgomez | 195 | } else { |
1414 : | h | 101 | BitstreamPutBits(&bs, tmp.x, length); |
1415 : | BitstreamPutBits(&bs, tmp.y, length); | ||
1416 : | } | ||
1417 : | } | ||
1418 : | } | ||
1419 : | } | ||
1420 : | } | ||
1421 : | |||
1422 : | edgomez | 195 | if (!hint->rawhints) { |
1423 : | h | 101 | BitstreamPad(&bs); |
1424 : | hint->hintlength = BitstreamLength(&bs); | ||
1425 : | } | ||
1426 : | } | ||
1427 : | |||
1428 : | |||
1429 : | edgomez | 195 | static int |
1430 : | FrameCodeI(Encoder * pEnc, | ||
1431 : | Bitstream * bs, | ||
1432 : | uint32_t * pBits) | ||
1433 : | h | 104 | { |
1434 : | |||
1435 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
1436 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1437 : | h | 104 | |
1438 : | uint16_t x, y; | ||
1439 : | |||
1440 : | pEnc->iFrameNum = 0; | ||
1441 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1; |
1442 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
1443 : | pEnc->current->coding_type = I_VOP; | ||
1444 : | h | 104 | |
1445 : | suxen_drol | 136 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1446 : | suxen_drol | 229 | #ifdef BFRAMES |
1447 : | #define DIVX501B481P "DivX501b481p" | ||
1448 : | if (pEnc->packed) { | ||
1449 : | BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); | ||
1450 : | } | ||
1451 : | #endif | ||
1452 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); | ||
1453 : | h | 104 | |
1454 : | *pBits = BitstreamPos(bs); | ||
1455 : | |||
1456 : | pEnc->sStat.iTextBits = 0; | ||
1457 : | pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; | ||
1458 : | pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
1459 : | |||
1460 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
1461 : | edgomez | 195 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1462 : | MACROBLOCK *pMB = | ||
1463 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1464 : | h | 104 | |
1465 : | CodeIntraMB(pEnc, pMB); | ||
1466 : | |||
1467 : | edgomez | 195 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1468 : | dct_codes, qcoeff); | ||
1469 : | h | 104 | |
1470 : | start_timer(); | ||
1471 : | suxen_drol | 136 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1472 : | h | 104 | stop_prediction_timer(); |
1473 : | |||
1474 : | start_timer(); | ||
1475 : | suxen_drol | 136 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1476 : | h | 104 | stop_coding_timer(); |
1477 : | } | ||
1478 : | |||
1479 : | emms(); | ||
1480 : | |||
1481 : | *pBits = BitstreamPos(bs) - *pBits; | ||
1482 : | pEnc->sStat.fMvPrevSigma = -1; | ||
1483 : | pEnc->sStat.iMvSum = 0; | ||
1484 : | pEnc->sStat.iMvCount = 0; | ||
1485 : | suxen_drol | 136 | pEnc->mbParam.m_fcode = 2; |
1486 : | h | 104 | |
1487 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1488 : | h | 104 | HintedMEGet(pEnc, 1); |
1489 : | } | ||
1490 : | |||
1491 : | edgomez | 195 | return 1; // intra |
1492 : | h | 104 | } |
1493 : | |||
1494 : | |||
1495 : | Isibaar | 3 | #define INTRA_THRESHOLD 0.5 |
1496 : | |||
1497 : | edgomez | 195 | static int |
1498 : | FrameCodeP(Encoder * pEnc, | ||
1499 : | Bitstream * bs, | ||
1500 : | uint32_t * pBits, | ||
1501 : | bool force_inter, | ||
1502 : | bool vol_header) | ||
1503 : | Isibaar | 3 | { |
1504 : | edgomez | 13 | float fSigma; |
1505 : | Isibaar | 42 | |
1506 : | edgomez | 78 | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1507 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1508 : | edgomez | 78 | |
1509 : | Isibaar | 3 | int iLimit; |
1510 : | edgomez | 13 | uint32_t x, y; |
1511 : | int iSearchRange; | ||
1512 : | edgomez | 145 | int bIntra; |
1513 : | Isibaar | 3 | |
1514 : | edgomez | 145 | /* IMAGE *pCurrent = &pEnc->current->image; */ |
1515 : | suxen_drol | 136 | IMAGE *pRef = &pEnc->reference->image; |
1516 : | Isibaar | 3 | |
1517 : | h | 69 | start_timer(); |
1518 : | edgomez | 195 | image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1519 : | pEnc->mbParam.width, pEnc->mbParam.height, | ||
1520 : | pEnc->current->global_flags & XVID_INTERLACING); | ||
1521 : | h | 69 | stop_edges_timer(); |
1522 : | Isibaar | 3 | |
1523 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1524 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
1525 : | pEnc->current->fcode = pEnc->mbParam.m_fcode; | ||
1526 : | Isibaar | 3 | |
1527 : | if (!force_inter) | ||
1528 : | edgomez | 195 | iLimit = |
1529 : | (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * | ||
1530 : | INTRA_THRESHOLD); | ||
1531 : | edgomez | 13 | else |
1532 : | Isibaar | 3 | iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
1533 : | |||
1534 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1535 : | Isibaar | 3 | start_timer(); |
1536 : | edgomez | 195 | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1537 : | &pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
1538 : | pEnc->mbParam.edged_height, | ||
1539 : | pEnc->current->rounding_type); | ||
1540 : | Isibaar | 3 | stop_inter_timer(); |
1541 : | } | ||
1542 : | |||
1543 : | start_timer(); | ||
1544 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1545 : | h | 101 | HintedMESet(pEnc, &bIntra); |
1546 : | edgomez | 195 | } else { |
1547 : | bIntra = | ||
1548 : | MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, | ||
1549 : | &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
1550 : | iLimit); | ||
1551 : | h | 101 | } |
1552 : | Isibaar | 3 | stop_motion_timer(); |
1553 : | |||
1554 : | edgomez | 195 | if (bIntra == 1) { |
1555 : | Isibaar | 3 | return FrameCodeI(pEnc, bs, pBits); |
1556 : | h | 101 | } |
1557 : | Isibaar | 3 | |
1558 : | suxen_drol | 136 | pEnc->current->coding_type = P_VOP; |
1559 : | Isibaar | 3 | |
1560 : | edgomez | 195 | if (vol_header) |
1561 : | suxen_drol | 136 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1562 : | Isibaar | 3 | |
1563 : | suxen_drol | 229 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1564 : | Isibaar | 3 | |
1565 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
1566 : | Isibaar | 3 | |
1567 : | edgomez | 13 | pEnc->sStat.iTextBits = 0; |
1568 : | pEnc->sStat.iMvSum = 0; | ||
1569 : | pEnc->sStat.iMvCount = 0; | ||
1570 : | Isibaar | 3 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1571 : | |||
1572 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1573 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) { | ||
1574 : | MACROBLOCK *pMB = | ||
1575 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1576 : | Isibaar | 3 | |
1577 : | edgomez | 13 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1578 : | Isibaar | 3 | |
1579 : | edgomez | 195 | if (!bIntra) { |
1580 : | Isibaar | 3 | start_timer(); |
1581 : | edgomez | 195 | MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1582 : | &pEnc->vInterH, &pEnc->vInterV, | ||
1583 : | &pEnc->vInterHV, &pEnc->current->image, | ||
1584 : | dct_codes, pEnc->mbParam.width, | ||
1585 : | pEnc->mbParam.height, | ||
1586 : | pEnc->mbParam.edged_width, | ||
1587 : | pEnc->current->rounding_type); | ||
1588 : | Isibaar | 3 | stop_comp_timer(); |
1589 : | |||
1590 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1591 : | edgomez | 195 | if (pMB->dquant != NO_CHANGE) { |
1592 : | Isibaar | 3 | pMB->mode = MODE_INTER_Q; |
1593 : | suxen_drol | 136 | pEnc->current->quant += DQtab[pMB->dquant]; |
1594 : | edgomez | 195 | if (pEnc->current->quant > 31) |
1595 : | pEnc->current->quant = 31; | ||
1596 : | else if (pEnc->current->quant < 1) | ||
1597 : | pEnc->current->quant = 1; | ||
1598 : | Isibaar | 3 | } |
1599 : | } | ||
1600 : | suxen_drol | 136 | pMB->quant = pEnc->current->quant; |
1601 : | Isibaar | 3 | |
1602 : | h | 69 | pMB->field_pred = 0; |
1603 : | |||
1604 : | edgomez | 195 | pMB->cbp = |
1605 : | MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, | ||
1606 : | dct_codes, qcoeff); | ||
1607 : | } else { | ||
1608 : | Isibaar | 3 | CodeIntraMB(pEnc, pMB); |
1609 : | edgomez | 195 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1610 : | dct_codes, qcoeff); | ||
1611 : | Isibaar | 3 | } |
1612 : | |||
1613 : | edgomez | 13 | start_timer(); |
1614 : | edgomez | 195 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1615 : | Isibaar | 3 | stop_prediction_timer(); |
1616 : | |||
1617 : | edgomez | 195 | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1618 : | Isibaar | 3 | pEnc->sStat.kblks++; |
1619 : | edgomez | 195 | } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1620 : | pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || | ||
1621 : | pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { | ||
1622 : | Isibaar | 3 | pEnc->sStat.mblks++; |
1623 : | edgomez | 195 | } else { |
1624 : | Isibaar | 3 | pEnc->sStat.ublks++; |
1625 : | } | ||
1626 : | |||
1627 : | start_timer(); | ||
1628 : | suxen_drol | 136 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1629 : | Isibaar | 3 | stop_coding_timer(); |
1630 : | } | ||
1631 : | } | ||
1632 : | |||
1633 : | emms(); | ||
1634 : | |||
1635 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1636 : | h | 101 | HintedMEGet(pEnc, 0); |
1637 : | } | ||
1638 : | |||
1639 : | Isibaar | 3 | if (pEnc->sStat.iMvCount == 0) |
1640 : | pEnc->sStat.iMvCount = 1; | ||
1641 : | |||
1642 : | edgomez | 195 | fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
1643 : | Isibaar | 3 | |
1644 : | suxen_drol | 136 | iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1645 : | Isibaar | 3 | |
1646 : | edgomez | 195 | if ((fSigma > iSearchRange / 3) |
1647 : | && (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 | ||
1648 : | edgomez | 13 | { |
1649 : | suxen_drol | 136 | pEnc->mbParam.m_fcode++; |
1650 : | Isibaar | 3 | iSearchRange *= 2; |
1651 : | edgomez | 195 | } else if ((fSigma < iSearchRange / 6) |
1652 : | && (pEnc->sStat.fMvPrevSigma >= 0) | ||
1653 : | && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) | ||
1654 : | && (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 | ||
1655 : | edgomez | 13 | { |
1656 : | suxen_drol | 136 | pEnc->mbParam.m_fcode--; |
1657 : | Isibaar | 3 | iSearchRange /= 2; |
1658 : | edgomez | 13 | } |
1659 : | Isibaar | 3 | |
1660 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = fSigma; |
1661 : | edgomez | 195 | |
1662 : | Isibaar | 3 | *pBits = BitstreamPos(bs) - *pBits; |
1663 : | |||
1664 : | edgomez | 195 | return 0; // inter |
1665 : | Isibaar | 3 | } |
1666 : | suxen_drol | 118 | |
1667 : | |||
1668 : | suxen_drol | 152 | #ifdef BFRAMES |
1669 : | edgomez | 195 | static void |
1670 : | FrameCodeB(Encoder * pEnc, | ||
1671 : | FRAMEINFO * frame, | ||
1672 : | Bitstream * bs, | ||
1673 : | uint32_t * pBits) | ||
1674 : | suxen_drol | 118 | { |
1675 : | edgomez | 195 | int16_t dct_codes[6 * 64]; |
1676 : | int16_t qcoeff[6 * 64]; | ||
1677 : | uint32_t x, y; | ||
1678 : | suxen_drol | 118 | VECTOR forward; |
1679 : | VECTOR backward; | ||
1680 : | |||
1681 : | edgomez | 195 | IMAGE *f_ref = &pEnc->reference->image; |
1682 : | suxen_drol | 118 | IMAGE *b_ref = &pEnc->current->image; |
1683 : | |||
1684 : | suxen_drol | 152 | // forward |
1685 : | edgomez | 195 | image_setedges(f_ref, pEnc->mbParam.edged_width, |
1686 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
1687 : | pEnc->mbParam.height, | ||
1688 : | frame->global_flags & XVID_INTERLACING); | ||
1689 : | suxen_drol | 118 | start_timer(); |
1690 : | image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
1691 : | edgomez | 195 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1692 : | 0); | ||
1693 : | suxen_drol | 118 | stop_inter_timer(); |
1694 : | |||
1695 : | suxen_drol | 152 | // backward |
1696 : | edgomez | 195 | image_setedges(b_ref, pEnc->mbParam.edged_width, |
1697 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
1698 : | pEnc->mbParam.height, | ||
1699 : | frame->global_flags & XVID_INTERLACING); | ||
1700 : | start_timer(); | ||
1701 : | suxen_drol | 118 | image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1702 : | edgomez | 195 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1703 : | 0); | ||
1704 : | suxen_drol | 118 | stop_inter_timer(); |
1705 : | |||
1706 : | start_timer(); | ||
1707 : | edgomez | 195 | MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
1708 : | &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
1709 : | pEnc->current->mbs, b_ref, &pEnc->vInterH, | ||
1710 : | &pEnc->vInterV, &pEnc->vInterHV); | ||
1711 : | suxen_drol | 118 | |
1712 : | edgomez | 195 | |
1713 : | suxen_drol | 118 | stop_motion_timer(); |
1714 : | edgomez | 195 | |
1715 : | suxen_drol | 152 | /*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
1716 : | edgomez | 195 | { |
1717 : | BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); | ||
1718 : | } */ | ||
1719 : | suxen_drol | 118 | |
1720 : | edgomez | 195 | frame->coding_type = B_VOP; |
1721 : | suxen_drol | 229 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1722 : | suxen_drol | 118 | |
1723 : | edgomez | 195 | *pBits = BitstreamPos(bs); |
1724 : | suxen_drol | 118 | |
1725 : | edgomez | 195 | pEnc->sStat.iTextBits = 0; |
1726 : | pEnc->sStat.iMvSum = 0; | ||
1727 : | pEnc->sStat.iMvCount = 0; | ||
1728 : | suxen_drol | 118 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1729 : | |||
1730 : | |||
1731 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1732 : | suxen_drol | 118 | // reset prediction |
1733 : | |||
1734 : | forward.x = 0; | ||
1735 : | forward.y = 0; | ||
1736 : | backward.x = 0; | ||
1737 : | backward.y = 0; | ||
1738 : | |||
1739 : | edgomez | 195 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1740 : | MACROBLOCK *f_mb = | ||
1741 : | &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1742 : | MACROBLOCK *b_mb = | ||
1743 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1744 : | MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1745 : | suxen_drol | 118 | |
1746 : | // decoder ignores mb when refence block is INTER(0,0), CBP=0 | ||
1747 : | edgomez | 195 | if (mb->mode == MODE_NOT_CODED) { |
1748 : | suxen_drol | 118 | mb->mvs[0].x = 0; |
1749 : | mb->mvs[0].y = 0; | ||
1750 : | continue; | ||
1751 : | } | ||
1752 : | |||
1753 : | MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, | ||
1754 : | edgomez | 195 | f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1755 : | &pEnc->f_refhv, b_ref, &pEnc->vInterH, | ||
1756 : | &pEnc->vInterV, &pEnc->vInterHV, | ||
1757 : | dct_codes); | ||
1758 : | |||
1759 : | suxen_drol | 118 | mb->quant = frame->quant; |
1760 : | edgomez | 195 | mb->cbp = |
1761 : | MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, | ||
1762 : | qcoeff); | ||
1763 : | suxen_drol | 118 | //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
1764 : | |||
1765 : | |||
1766 : | edgomez | 195 | if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1767 : | && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { | ||
1768 : | mb->mode = 5; // skipped | ||
1769 : | suxen_drol | 118 | } |
1770 : | |||
1771 : | edgomez | 195 | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
1772 : | suxen_drol | 118 | mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1773 : | mb->pmvs[0].y = mb->mvs[0].y - forward.y; | ||
1774 : | forward.x = mb->mvs[0].x; | ||
1775 : | forward.y = mb->mvs[0].y; | ||
1776 : | } | ||
1777 : | edgomez | 195 | |
1778 : | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { | ||
1779 : | suxen_drol | 118 | mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
1780 : | mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; | ||
1781 : | backward.x = mb->b_mvs[0].x; | ||
1782 : | backward.y = mb->b_mvs[0].y; | ||
1783 : | } | ||
1784 : | 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); |
1785 : | suxen_drol | 118 | |
1786 : | edgomez | 195 | start_timer(); |
1787 : | MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, | ||
1788 : | &pEnc->sStat); | ||
1789 : | suxen_drol | 118 | stop_coding_timer(); |
1790 : | } | ||
1791 : | } | ||
1792 : | |||
1793 : | emms(); | ||
1794 : | |||
1795 : | // TODO: dynamic fcode/bcode ??? | ||
1796 : | |||
1797 : | *pBits = BitstreamPos(bs) - *pBits; | ||
1798 : | } | ||
1799 : | edgomez | 188 | #endif |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |