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