Parent Directory
|
Revision Log
Revision 1.126 - (view) (download)
1 : | edgomez | 1.40 | /***************************************************************************** |
2 : | edgomez | 1.29 | * |
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | edgomez | 1.102 | * - Encoder main module - |
5 : | edgomez | 1.29 | * |
6 : | edgomez | 1.102 | * Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 : | * 2002-2003 Peter Ross <pross@xvid.org> | ||
8 : | * 2002 Daniel Smith <danielsmith@astroboymail.com> | ||
9 : | edgomez | 1.77 | * |
10 : | edgomez | 1.102 | * This program is free software ; you can redistribute it and/or modify |
11 : | edgomez | 1.91 | * it under the terms of the GNU General Public License as published by |
12 : | edgomez | 1.102 | * the Free Software Foundation ; either version 2 of the License, or |
13 : | edgomez | 1.29 | * (at your option) any later version. |
14 : | * | ||
15 : | * This program is distributed in the hope that it will be useful, | ||
16 : | edgomez | 1.102 | * but WITHOUT ANY WARRANTY ; without even the implied warranty of |
17 : | edgomez | 1.29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 : | * GNU General Public License for more details. | ||
19 : | * | ||
20 : | * You should have received a copy of the GNU General Public License | ||
21 : | edgomez | 1.102 | * along with this program ; if not, write to the Free Software |
22 : | edgomez | 1.29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 : | edgomez | 1.79 | * |
24 : | syskin | 1.126 | * $Id: encoder.c,v 1.122 2005/12/17 12:04:52 syskin Exp $ |
25 : | edgomez | 1.29 | * |
26 : | edgomez | 1.40 | ****************************************************************************/ |
27 : | chl | 1.64 | |
28 : | Isibaar | 1.1 | #include <stdlib.h> |
29 : | #include <stdio.h> | ||
30 : | #include <math.h> | ||
31 : | edgomez | 1.42 | #include <string.h> |
32 : | Isibaar | 1.1 | |
33 : | #include "encoder.h" | ||
34 : | #include "prediction/mbprediction.h" | ||
35 : | #include "global.h" | ||
36 : | #include "utils/timer.h" | ||
37 : | #include "image/image.h" | ||
38 : | edgomez | 1.91 | #include "image/font.h" |
39 : | #include "motion/sad.h" | ||
40 : | suxen_drol | 1.32 | #include "motion/motion.h" |
41 : | edgomez | 1.102 | #include "motion/gmc.h" |
42 : | |||
43 : | Isibaar | 1.1 | #include "bitstream/cbp.h" |
44 : | #include "utils/mbfunctions.h" | ||
45 : | #include "bitstream/bitstream.h" | ||
46 : | #include "bitstream/mbcoding.h" | ||
47 : | #include "utils/emms.h" | ||
48 : | #include "bitstream/mbcoding.h" | ||
49 : | Isibaar | 1.2 | #include "quant/quant_matrix.h" |
50 : | Isibaar | 1.7 | #include "utils/mem_align.h" |
51 : | Isibaar | 1.1 | |
52 : | syskin | 1.126 | # include "motion/motion_smp.h" |
53 : | |||
54 : | |||
55 : | edgomez | 1.40 | /***************************************************************************** |
56 : | * Local function prototypes | ||
57 : | ****************************************************************************/ | ||
58 : | |||
59 : | edgomez | 1.39 | static int FrameCodeI(Encoder * pEnc, |
60 : | edgomez | 1.102 | Bitstream * bs); |
61 : | edgomez | 1.39 | |
62 : | static int FrameCodeP(Encoder * pEnc, | ||
63 : | syskin | 1.104 | Bitstream * bs); |
64 : | Isibaar | 1.1 | |
65 : | edgomez | 1.91 | static void FrameCodeB(Encoder * pEnc, |
66 : | FRAMEINFO * frame, | ||
67 : | edgomez | 1.102 | Bitstream * bs); |
68 : | Isibaar | 1.1 | |
69 : | |||
70 : | edgomez | 1.40 | /***************************************************************************** |
71 : | * Encoder creation | ||
72 : | * | ||
73 : | * This function creates an Encoder instance, it allocates all necessary | ||
74 : | edgomez | 1.91 | * image buffers (reference, current and bframes) and initialize the internal |
75 : | * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. | ||
76 : | edgomez | 1.40 | * |
77 : | * The code seems to be very long but is very basic, mainly memory allocation | ||
78 : | * and cleaning code. | ||
79 : | * | ||
80 : | * Returned values : | ||
81 : | edgomez | 1.102 | * - 0 - no errors |
82 : | syskin | 1.96 | * - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
83 : | * cleans the structure before exiting. | ||
84 : | * pParam->handle is also set to NULL. | ||
85 : | edgomez | 1.40 | * |
86 : | ****************************************************************************/ | ||
87 : | |||
88 : | edgomez | 1.102 | /* |
89 : | * Simplify the "fincr/fbase" fraction | ||
90 : | */ | ||
91 : | Skal | 1.118 | static int |
92 : | gcd(int a, int b) | ||
93 : | { | ||
94 : | int r ; | ||
95 : | |||
96 : | if (b > a) { | ||
97 : | r = a; | ||
98 : | a = b; | ||
99 : | b = r; | ||
100 : | } | ||
101 : | |||
102 : | while ((r = a % b)) { | ||
103 : | a = b; | ||
104 : | b = r; | ||
105 : | } | ||
106 : | return b; | ||
107 : | } | ||
108 : | |||
109 : | edgomez | 1.102 | static void |
110 : | simplify_time(int *inc, int *base) | ||
111 : | Isibaar | 1.1 | { |
112 : | edgomez | 1.102 | /* common factor */ |
113 : | Skal | 1.118 | const int s = gcd(*inc, *base); |
114 : | *inc /= s; | ||
115 : | *base /= s; | ||
116 : | Isibaar | 1.1 | |
117 : | edgomez | 1.106 | if (*base > 65535 || *inc > 65535) { |
118 : | int *biggest; | ||
119 : | int *other; | ||
120 : | edgomez | 1.107 | float div; |
121 : | edgomez | 1.108 | |
122 : | edgomez | 1.106 | if (*base > *inc) { |
123 : | biggest = base; | ||
124 : | other = inc; | ||
125 : | } else { | ||
126 : | biggest = inc; | ||
127 : | other = base; | ||
128 : | } | ||
129 : | |||
130 : | edgomez | 1.107 | div = ((float)*biggest)/((float)65535); |
131 : | Skal | 1.118 | *biggest = (unsigned int)(((float)*biggest)/div); |
132 : | *other = (unsigned int)(((float)*other)/div); | ||
133 : | Isibaar | 1.1 | } |
134 : | edgomez | 1.102 | } |
135 : | Isibaar | 1.1 | |
136 : | edgomez | 1.39 | |
137 : | edgomez | 1.102 | int |
138 : | enc_create(xvid_enc_create_t * create) | ||
139 : | { | ||
140 : | Encoder *pEnc; | ||
141 : | syskin | 1.126 | int n; |
142 : | edgomez | 1.39 | |
143 : | edgomez | 1.102 | if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */ |
144 : | return XVID_ERR_VERSION; | ||
145 : | Isibaar | 1.1 | |
146 : | edgomez | 1.102 | if (create->width%2 || create->height%2) |
147 : | return XVID_ERR_FAIL; | ||
148 : | Isibaar | 1.1 | |
149 : | syskin | 1.103 | if (create->width<=0 || create->height<=0) |
150 : | return XVID_ERR_FAIL; | ||
151 : | |||
152 : | edgomez | 1.102 | /* allocate encoder struct */ |
153 : | edgomez | 1.41 | |
154 : | edgomez | 1.39 | pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
155 : | if (pEnc == NULL) | ||
156 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
157 : | edgomez | 1.42 | memset(pEnc, 0, sizeof(Encoder)); |
158 : | |||
159 : | edgomez | 1.102 | pEnc->mbParam.profile = create->profile; |
160 : | Isibaar | 1.1 | |
161 : | edgomez | 1.102 | /* global flags */ |
162 : | pEnc->mbParam.global_flags = create->global; | ||
163 : | suxen_drol | 1.117 | if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED)) |
164 : | pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA; | ||
165 : | Isibaar | 1.1 | |
166 : | edgomez | 1.102 | /* width, height */ |
167 : | pEnc->mbParam.width = create->width; | ||
168 : | pEnc->mbParam.height = create->height; | ||
169 : | Isibaar | 1.1 | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; |
170 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
171 : | edgomez | 1.41 | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
172 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
173 : | Isibaar | 1.1 | |
174 : | edgomez | 1.102 | /* framerate */ |
175 : | pEnc->mbParam.fincr = MAX(create->fincr, 0); | ||
176 : | pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; | ||
177 : | if (pEnc->mbParam.fincr>0) | ||
178 : | Skal | 1.118 | simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase); |
179 : | Isibaar | 1.119 | |
180 : | edgomez | 1.102 | /* zones */ |
181 : | if(create->num_zones > 0) { | ||
182 : | pEnc->num_zones = create->num_zones; | ||
183 : | pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE); | ||
184 : | if (pEnc->zones == NULL) | ||
185 : | goto xvid_err_memory0; | ||
186 : | memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones); | ||
187 : | } else { | ||
188 : | pEnc->num_zones = 0; | ||
189 : | pEnc->zones = NULL; | ||
190 : | } | ||
191 : | |||
192 : | /* plugins */ | ||
193 : | if(create->num_plugins > 0) { | ||
194 : | pEnc->num_plugins = create->num_plugins; | ||
195 : | pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); | ||
196 : | if (pEnc->plugins == NULL) | ||
197 : | goto xvid_err_memory0; | ||
198 : | } else { | ||
199 : | pEnc->num_plugins = 0; | ||
200 : | pEnc->plugins = NULL; | ||
201 : | } | ||
202 : | |||
203 : | for (n=0; n<pEnc->num_plugins;n++) { | ||
204 : | xvid_plg_create_t pcreate; | ||
205 : | xvid_plg_info_t pinfo; | ||
206 : | |||
207 : | memset(&pinfo, 0, sizeof(xvid_plg_info_t)); | ||
208 : | pinfo.version = XVID_VERSION; | ||
209 : | suxen_drol | 1.120 | if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) { |
210 : | edgomez | 1.102 | pEnc->mbParam.plugin_flags |= pinfo.flags; |
211 : | } | ||
212 : | |||
213 : | memset(&pcreate, 0, sizeof(xvid_plg_create_t)); | ||
214 : | pcreate.version = XVID_VERSION; | ||
215 : | pcreate.num_zones = pEnc->num_zones; | ||
216 : | pcreate.zones = pEnc->zones; | ||
217 : | pcreate.width = pEnc->mbParam.width; | ||
218 : | pcreate.height = pEnc->mbParam.height; | ||
219 : | pcreate.mb_width = pEnc->mbParam.mb_width; | ||
220 : | pcreate.mb_height = pEnc->mbParam.mb_height; | ||
221 : | pcreate.fincr = pEnc->mbParam.fincr; | ||
222 : | pcreate.fbase = pEnc->mbParam.fbase; | ||
223 : | pcreate.param = create->plugins[n].param; | ||
224 : | |||
225 : | pEnc->plugins[n].func = NULL; /* disable plugins that fail */ | ||
226 : | suxen_drol | 1.120 | if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
227 : | edgomez | 1.102 | pEnc->plugins[n].func = create->plugins[n].func; |
228 : | } | ||
229 : | } | ||
230 : | |||
231 : | if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) || | ||
232 : | (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { | ||
233 : | pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */ | ||
234 : | } | ||
235 : | Isibaar | 1.43 | |
236 : | edgomez | 1.102 | /* temp dquants */ |
237 : | if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { | ||
238 : | pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
239 : | pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); | ||
240 : | if (pEnc->temp_dquants==NULL) | ||
241 : | goto xvid_err_memory1a; | ||
242 : | } | ||
243 : | suxen_drol | 1.32 | |
244 : | syskin | 1.121 | /* temp lambdas */ |
245 : | if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { | ||
246 : | pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width * | ||
247 : | pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE); | ||
248 : | if (pEnc->temp_lambda == NULL) | ||
249 : | goto xvid_err_memory1a; | ||
250 : | } | ||
251 : | |||
252 : | edgomez | 1.102 | /* bframes */ |
253 : | pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); | ||
254 : | pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); | ||
255 : | pEnc->mbParam.bquant_offset = create->bquant_offset; | ||
256 : | Isibaar | 1.1 | |
257 : | edgomez | 1.102 | /* min/max quant */ |
258 : | for (n=0; n<3; n++) { | ||
259 : | pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2; | ||
260 : | pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31; | ||
261 : | } | ||
262 : | Isibaar | 1.1 | |
263 : | edgomez | 1.102 | /* frame drop ratio */ |
264 : | pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); | ||
265 : | Isibaar | 1.1 | |
266 : | edgomez | 1.102 | /* max keyframe interval */ |
267 : | pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval; | ||
268 : | Isibaar | 1.1 | |
269 : | edgomez | 1.102 | /* allocate working frame-image memory */ |
270 : | h | 1.16 | |
271 : | edgomez | 1.39 | pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
272 : | pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); | ||
273 : | |||
274 : | edgomez | 1.41 | if (pEnc->current == NULL || pEnc->reference == NULL) |
275 : | edgomez | 1.39 | goto xvid_err_memory1; |
276 : | suxen_drol | 1.27 | |
277 : | edgomez | 1.102 | /* allocate macroblock memory */ |
278 : | h | 1.16 | |
279 : | edgomez | 1.41 | pEnc->current->mbs = |
280 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
281 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
282 : | pEnc->reference->mbs = | ||
283 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
284 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
285 : | Isibaar | 1.1 | |
286 : | edgomez | 1.39 | if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
287 : | goto xvid_err_memory2; | ||
288 : | suxen_drol | 1.27 | |
289 : | edgomez | 1.102 | /* allocate quant matrix memory */ |
290 : | |||
291 : | pEnc->mbParam.mpeg_quant_matrices = | ||
292 : | xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); | ||
293 : | |||
294 : | if (pEnc->mbParam.mpeg_quant_matrices == NULL) | ||
295 : | goto xvid_err_memory2a; | ||
296 : | |||
297 : | /* allocate interpolation image memory */ | ||
298 : | suxen_drol | 1.27 | |
299 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
300 : | edgomez | 1.91 | image_null(&pEnc->sOriginal); |
301 : | edgomez | 1.102 | image_null(&pEnc->sOriginal2); |
302 : | } | ||
303 : | edgomez | 1.91 | |
304 : | image_null(&pEnc->f_refh); | ||
305 : | image_null(&pEnc->f_refv); | ||
306 : | image_null(&pEnc->f_refhv); | ||
307 : | |||
308 : | suxen_drol | 1.27 | image_null(&pEnc->current->image); |
309 : | image_null(&pEnc->reference->image); | ||
310 : | image_null(&pEnc->vInterH); | ||
311 : | image_null(&pEnc->vInterV); | ||
312 : | image_null(&pEnc->vInterHV); | ||
313 : | edgomez | 1.39 | |
314 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
315 : | if (image_create | ||
316 : | edgomez | 1.91 | (&pEnc->sOriginal, pEnc->mbParam.edged_width, |
317 : | pEnc->mbParam.edged_height) < 0) | ||
318 : | goto xvid_err_memory3; | ||
319 : | edgomez | 1.102 | |
320 : | if (image_create | ||
321 : | (&pEnc->sOriginal2, pEnc->mbParam.edged_width, | ||
322 : | pEnc->mbParam.edged_height) < 0) | ||
323 : | goto xvid_err_memory3; | ||
324 : | edgomez | 1.91 | } |
325 : | |||
326 : | if (image_create | ||
327 : | (&pEnc->f_refh, pEnc->mbParam.edged_width, | ||
328 : | pEnc->mbParam.edged_height) < 0) | ||
329 : | goto xvid_err_memory3; | ||
330 : | if (image_create | ||
331 : | (&pEnc->f_refv, pEnc->mbParam.edged_width, | ||
332 : | pEnc->mbParam.edged_height) < 0) | ||
333 : | goto xvid_err_memory3; | ||
334 : | edgomez | 1.41 | if (image_create |
335 : | edgomez | 1.91 | (&pEnc->f_refhv, pEnc->mbParam.edged_width, |
336 : | edgomez | 1.41 | pEnc->mbParam.edged_height) < 0) |
337 : | edgomez | 1.39 | goto xvid_err_memory3; |
338 : | edgomez | 1.91 | |
339 : | edgomez | 1.41 | if (image_create |
340 : | (&pEnc->current->image, pEnc->mbParam.edged_width, | ||
341 : | pEnc->mbParam.edged_height) < 0) | ||
342 : | edgomez | 1.39 | goto xvid_err_memory3; |
343 : | edgomez | 1.41 | if (image_create |
344 : | (&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
345 : | pEnc->mbParam.edged_height) < 0) | ||
346 : | edgomez | 1.39 | goto xvid_err_memory3; |
347 : | edgomez | 1.41 | if (image_create |
348 : | (&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
349 : | pEnc->mbParam.edged_height) < 0) | ||
350 : | edgomez | 1.39 | goto xvid_err_memory3; |
351 : | edgomez | 1.41 | if (image_create |
352 : | (&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
353 : | pEnc->mbParam.edged_height) < 0) | ||
354 : | edgomez | 1.39 | goto xvid_err_memory3; |
355 : | edgomez | 1.41 | if (image_create |
356 : | (&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
357 : | pEnc->mbParam.edged_height) < 0) | ||
358 : | edgomez | 1.39 | goto xvid_err_memory3; |
359 : | edgomez | 1.91 | |
360 : | /* Create full bitplane for GMC, this might be wasteful */ | ||
361 : | if (image_create | ||
362 : | (&pEnc->vGMC, pEnc->mbParam.edged_width, | ||
363 : | pEnc->mbParam.edged_height) < 0) | ||
364 : | goto xvid_err_memory3; | ||
365 : | |||
366 : | edgomez | 1.102 | /* init bframe image buffers */ |
367 : | edgomez | 1.91 | |
368 : | edgomez | 1.102 | pEnc->bframenum_head = 0; |
369 : | pEnc->bframenum_tail = 0; | ||
370 : | pEnc->flush_bframes = 0; | ||
371 : | pEnc->closed_bframenum = -1; | ||
372 : | edgomez | 1.91 | |
373 : | /* B Frames specific init */ | ||
374 : | pEnc->bframes = NULL; | ||
375 : | |||
376 : | if (pEnc->mbParam.max_bframes > 0) { | ||
377 : | |||
378 : | pEnc->bframes = | ||
379 : | xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), | ||
380 : | CACHE_LINE); | ||
381 : | |||
382 : | if (pEnc->bframes == NULL) | ||
383 : | goto xvid_err_memory3; | ||
384 : | |||
385 : | for (n = 0; n < pEnc->mbParam.max_bframes; n++) | ||
386 : | pEnc->bframes[n] = NULL; | ||
387 : | |||
388 : | |||
389 : | for (n = 0; n < pEnc->mbParam.max_bframes; n++) { | ||
390 : | pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); | ||
391 : | |||
392 : | if (pEnc->bframes[n] == NULL) | ||
393 : | goto xvid_err_memory4; | ||
394 : | |||
395 : | pEnc->bframes[n]->mbs = | ||
396 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
397 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
398 : | |||
399 : | if (pEnc->bframes[n]->mbs == NULL) | ||
400 : | goto xvid_err_memory4; | ||
401 : | |||
402 : | image_null(&pEnc->bframes[n]->image); | ||
403 : | |||
404 : | if (image_create | ||
405 : | (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, | ||
406 : | pEnc->mbParam.edged_height) < 0) | ||
407 : | goto xvid_err_memory4; | ||
408 : | |||
409 : | } | ||
410 : | } | ||
411 : | |||
412 : | edgomez | 1.102 | /* init incoming frame queue */ |
413 : | pEnc->queue_head = 0; | ||
414 : | pEnc->queue_tail = 0; | ||
415 : | pEnc->queue_size = 0; | ||
416 : | edgomez | 1.91 | |
417 : | edgomez | 1.102 | pEnc->queue = |
418 : | xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO), | ||
419 : | CACHE_LINE); | ||
420 : | edgomez | 1.91 | |
421 : | edgomez | 1.102 | if (pEnc->queue == NULL) |
422 : | goto xvid_err_memory4; | ||
423 : | edgomez | 1.91 | |
424 : | edgomez | 1.102 | for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
425 : | image_null(&pEnc->queue[n].image); | ||
426 : | edgomez | 1.91 | |
427 : | |||
428 : | edgomez | 1.102 | for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
429 : | if (image_create | ||
430 : | (&pEnc->queue[n].image, pEnc->mbParam.edged_width, | ||
431 : | pEnc->mbParam.edged_height) < 0) | ||
432 : | goto xvid_err_memory5; | ||
433 : | edgomez | 1.91 | } |
434 : | |||
435 : | edgomez | 1.102 | /* timestamp stuff */ |
436 : | edgomez | 1.91 | |
437 : | pEnc->mbParam.m_stamp = 0; | ||
438 : | pEnc->m_framenum = 0; | ||
439 : | pEnc->current->stamp = 0; | ||
440 : | pEnc->reference->stamp = 0; | ||
441 : | edgomez | 1.39 | |
442 : | edgomez | 1.102 | /* other stuff */ |
443 : | |||
444 : | pEnc->iFrameNum = 0; | ||
445 : | pEnc->fMvPrevSigma = -1; | ||
446 : | Isibaar | 1.1 | |
447 : | syskin | 1.126 | /* multithreaded stuff */ |
448 : | if (create->num_threads > 0) { | ||
449 : | int t = create->num_threads; | ||
450 : | int rows_per_thread = (pEnc->mbParam.mb_height+t-1)/t; | ||
451 : | pEnc->num_threads = t; | ||
452 : | pEnc->motionData = xvid_malloc(t*sizeof(SMPmotionData), CACHE_LINE); | ||
453 : | if (!pEnc->motionData) | ||
454 : | goto xvid_err_nosmp; | ||
455 : | |||
456 : | for (n = 0; n < t; n++) { | ||
457 : | pEnc->motionData[n].complete_count_self = | ||
458 : | xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE); | ||
459 : | |||
460 : | if (!pEnc->motionData[n].complete_count_self) | ||
461 : | goto xvid_err_nosmp; | ||
462 : | |||
463 : | if (n != 0) | ||
464 : | pEnc->motionData[n].complete_count_above = | ||
465 : | pEnc->motionData[n-1].complete_count_self; | ||
466 : | } | ||
467 : | pEnc->motionData[0].complete_count_above = | ||
468 : | pEnc->motionData[t-1].complete_count_self - 1; | ||
469 : | |||
470 : | } else { | ||
471 : | xvid_err_nosmp: | ||
472 : | /* no SMP */ | ||
473 : | create->num_threads = 0; | ||
474 : | pEnc->motionData = NULL; | ||
475 : | } | ||
476 : | |||
477 : | edgomez | 1.102 | create->handle = (void *) pEnc; |
478 : | Isibaar | 1.1 | |
479 : | Isibaar | 1.6 | init_timer(); |
480 : | edgomez | 1.102 | init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices); |
481 : | Isibaar | 1.1 | |
482 : | edgomez | 1.102 | return 0; /* ok */ |
483 : | edgomez | 1.39 | |
484 : | /* | ||
485 : | * We handle all XVID_ERR_MEMORY here, this makes the code lighter | ||
486 : | */ | ||
487 : | |||
488 : | edgomez | 1.91 | xvid_err_memory5: |
489 : | |||
490 : | edgomez | 1.102 | for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
491 : | image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width, | ||
492 : | edgomez | 1.91 | pEnc->mbParam.edged_height); |
493 : | } | ||
494 : | edgomez | 1.102 | |
495 : | xvid_free(pEnc->queue); | ||
496 : | edgomez | 1.91 | |
497 : | xvid_err_memory4: | ||
498 : | |||
499 : | if (pEnc->mbParam.max_bframes > 0) { | ||
500 : | edgomez | 1.102 | int i; |
501 : | edgomez | 1.91 | |
502 : | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { | ||
503 : | |||
504 : | if (pEnc->bframes[i] == NULL) | ||
505 : | continue; | ||
506 : | |||
507 : | image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, | ||
508 : | pEnc->mbParam.edged_height); | ||
509 : | xvid_free(pEnc->bframes[i]->mbs); | ||
510 : | xvid_free(pEnc->bframes[i]); | ||
511 : | syskin | 1.96 | } |
512 : | edgomez | 1.91 | |
513 : | xvid_free(pEnc->bframes); | ||
514 : | } | ||
515 : | |||
516 : | edgomez | 1.41 | xvid_err_memory3: |
517 : | edgomez | 1.91 | |
518 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
519 : | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, | ||
520 : | pEnc->mbParam.edged_height); | ||
521 : | image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, | ||
522 : | edgomez | 1.91 | pEnc->mbParam.edged_height); |
523 : | } | ||
524 : | |||
525 : | image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, | ||
526 : | pEnc->mbParam.edged_height); | ||
527 : | image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, | ||
528 : | pEnc->mbParam.edged_height); | ||
529 : | image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
530 : | edgomez | 1.41 | pEnc->mbParam.edged_height); |
531 : | edgomez | 1.39 | |
532 : | edgomez | 1.41 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
533 : | pEnc->mbParam.edged_height); | ||
534 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
535 : | pEnc->mbParam.edged_height); | ||
536 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
537 : | pEnc->mbParam.edged_height); | ||
538 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
539 : | pEnc->mbParam.edged_height); | ||
540 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
541 : | pEnc->mbParam.edged_height); | ||
542 : | edgomez | 1.91 | |
543 : | /* destroy GMC image */ | ||
544 : | image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, | ||
545 : | pEnc->mbParam.edged_height); | ||
546 : | |||
547 : | edgomez | 1.102 | xvid_err_memory2a: |
548 : | xvid_free(pEnc->mbParam.mpeg_quant_matrices); | ||
549 : | edgomez | 1.39 | |
550 : | edgomez | 1.41 | xvid_err_memory2: |
551 : | edgomez | 1.39 | xvid_free(pEnc->current->mbs); |
552 : | xvid_free(pEnc->reference->mbs); | ||
553 : | |||
554 : | edgomez | 1.41 | xvid_err_memory1: |
555 : | edgomez | 1.39 | xvid_free(pEnc->current); |
556 : | xvid_free(pEnc->reference); | ||
557 : | edgomez | 1.102 | |
558 : | xvid_err_memory1a: | ||
559 : | if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { | ||
560 : | xvid_free(pEnc->temp_dquants); | ||
561 : | } | ||
562 : | |||
563 : | syskin | 1.121 | if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { |
564 : | xvid_free(pEnc->temp_lambda); | ||
565 : | } | ||
566 : | |||
567 : | edgomez | 1.102 | xvid_err_memory0: |
568 : | for (n=0; n<pEnc->num_plugins;n++) { | ||
569 : | if (pEnc->plugins[n].func) { | ||
570 : | suxen_drol | 1.120 | pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL); |
571 : | edgomez | 1.102 | } |
572 : | } | ||
573 : | xvid_free(pEnc->plugins); | ||
574 : | |||
575 : | xvid_free(pEnc->zones); | ||
576 : | |||
577 : | edgomez | 1.39 | xvid_free(pEnc); |
578 : | |||
579 : | edgomez | 1.102 | create->handle = NULL; |
580 : | edgomez | 1.40 | |
581 : | edgomez | 1.39 | return XVID_ERR_MEMORY; |
582 : | Isibaar | 1.1 | } |
583 : | |||
584 : | edgomez | 1.40 | /***************************************************************************** |
585 : | * Encoder destruction | ||
586 : | * | ||
587 : | * This function destroy the entire encoder structure created by a previous | ||
588 : | edgomez | 1.102 | * successful enc_create call. |
589 : | edgomez | 1.40 | * |
590 : | * Returned values (for now only one returned value) : | ||
591 : | edgomez | 1.102 | * - 0 - no errors |
592 : | edgomez | 1.40 | * |
593 : | ****************************************************************************/ | ||
594 : | |||
595 : | int | ||
596 : | edgomez | 1.102 | enc_destroy(Encoder * pEnc) |
597 : | Isibaar | 1.1 | { |
598 : | edgomez | 1.91 | int i; |
599 : | syskin | 1.96 | |
600 : | edgomez | 1.91 | /* B Frames specific */ |
601 : | edgomez | 1.102 | for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
602 : | image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, | ||
603 : | edgomez | 1.91 | pEnc->mbParam.edged_height); |
604 : | } | ||
605 : | |||
606 : | edgomez | 1.102 | xvid_free(pEnc->queue); |
607 : | |||
608 : | edgomez | 1.91 | if (pEnc->mbParam.max_bframes > 0) { |
609 : | |||
610 : | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { | ||
611 : | |||
612 : | if (pEnc->bframes[i] == NULL) | ||
613 : | continue; | ||
614 : | |||
615 : | image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, | ||
616 : | pEnc->mbParam.edged_height); | ||
617 : | xvid_free(pEnc->bframes[i]->mbs); | ||
618 : | xvid_free(pEnc->bframes[i]); | ||
619 : | } | ||
620 : | |||
621 : | xvid_free(pEnc->bframes); | ||
622 : | syskin | 1.96 | |
623 : | edgomez | 1.91 | } |
624 : | |||
625 : | edgomez | 1.39 | /* All images, reference, current etc ... */ |
626 : | edgomez | 1.91 | |
627 : | edgomez | 1.41 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
628 : | pEnc->mbParam.edged_height); | ||
629 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
630 : | pEnc->mbParam.edged_height); | ||
631 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
632 : | pEnc->mbParam.edged_height); | ||
633 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
634 : | pEnc->mbParam.edged_height); | ||
635 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
636 : | pEnc->mbParam.edged_height); | ||
637 : | edgomez | 1.91 | image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
638 : | edgomez | 1.41 | pEnc->mbParam.edged_height); |
639 : | edgomez | 1.91 | image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
640 : | pEnc->mbParam.edged_height); | ||
641 : | image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
642 : | pEnc->mbParam.edged_height); | ||
643 : | edgomez | 1.102 | image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
644 : | pEnc->mbParam.edged_height); | ||
645 : | edgomez | 1.91 | |
646 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
647 : | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, | ||
648 : | pEnc->mbParam.edged_height); | ||
649 : | image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, | ||
650 : | edgomez | 1.91 | pEnc->mbParam.edged_height); |
651 : | } | ||
652 : | edgomez | 1.39 | |
653 : | /* Encoder structure */ | ||
654 : | edgomez | 1.91 | |
655 : | suxen_drol | 1.27 | xvid_free(pEnc->current->mbs); |
656 : | xvid_free(pEnc->current); | ||
657 : | |||
658 : | xvid_free(pEnc->reference->mbs); | ||
659 : | xvid_free(pEnc->reference); | ||
660 : | |||
661 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
662 : | xvid_free(pEnc->temp_dquants); | ||
663 : | edgomez | 1.91 | } |
664 : | |||
665 : | |||
666 : | edgomez | 1.102 | if (pEnc->num_plugins>0) { |
667 : | xvid_plg_destroy_t pdestroy; | ||
668 : | memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t)); | ||
669 : | edgomez | 1.91 | |
670 : | edgomez | 1.102 | pdestroy.version = XVID_VERSION; |
671 : | pdestroy.num_frames = pEnc->m_framenum; | ||
672 : | edgomez | 1.91 | |
673 : | edgomez | 1.102 | for (i=0; i<pEnc->num_plugins;i++) { |
674 : | if (pEnc->plugins[i].func) { | ||
675 : | suxen_drol | 1.120 | pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL); |
676 : | edgomez | 1.102 | } |
677 : | } | ||
678 : | xvid_free(pEnc->plugins); | ||
679 : | edgomez | 1.92 | } |
680 : | |||
681 : | edgomez | 1.102 | xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
682 : | edgomez | 1.91 | |
683 : | syskin | 1.126 | if (pEnc->num_zones > 0) |
684 : | edgomez | 1.102 | xvid_free(pEnc->zones); |
685 : | edgomez | 1.91 | |
686 : | syskin | 1.126 | if (pEnc->num_threads > 0) { |
687 : | for (i = 0; i < pEnc->num_threads; i++) | ||
688 : | xvid_free(pEnc->motionData[i].complete_count_self); | ||
689 : | |||
690 : | xvid_free(pEnc->motionData); | ||
691 : | } | ||
692 : | |||
693 : | edgomez | 1.102 | xvid_free(pEnc); |
694 : | edgomez | 1.91 | |
695 : | edgomez | 1.102 | return 0; /* ok */ |
696 : | edgomez | 1.91 | } |
697 : | |||
698 : | |||
699 : | edgomez | 1.102 | /* |
700 : | call the plugins | ||
701 : | */ | ||
702 : | edgomez | 1.91 | |
703 : | edgomez | 1.102 | static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
704 : | int opt, int * type, int * quant, xvid_enc_stats_t * stats) | ||
705 : | suxen_drol | 1.44 | { |
706 : | syskin | 1.121 | unsigned int i, j, k; |
707 : | edgomez | 1.102 | xvid_plg_data_t data; |
708 : | edgomez | 1.91 | |
709 : | edgomez | 1.102 | /* set data struct */ |
710 : | suxen_drol | 1.44 | |
711 : | edgomez | 1.102 | memset(&data, 0, sizeof(xvid_plg_data_t)); |
712 : | data.version = XVID_VERSION; | ||
713 : | edgomez | 1.91 | |
714 : | edgomez | 1.102 | /* find zone */ |
715 : | for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ; | ||
716 : | data.zone = i>0 ? &pEnc->zones[i-1] : NULL; | ||
717 : | |||
718 : | data.width = pEnc->mbParam.width; | ||
719 : | data.height = pEnc->mbParam.height; | ||
720 : | data.mb_width = pEnc->mbParam.mb_width; | ||
721 : | data.mb_height = pEnc->mbParam.mb_height; | ||
722 : | data.fincr = frame->fincr; | ||
723 : | data.fbase = pEnc->mbParam.fbase; | ||
724 : | data.bquant_ratio = pEnc->mbParam.bquant_ratio; | ||
725 : | data.bquant_offset = pEnc->mbParam.bquant_offset; | ||
726 : | |||
727 : | for (i=0; i<3; i++) { | ||
728 : | data.min_quant[i] = pEnc->mbParam.min_quant[i]; | ||
729 : | data.max_quant[i] = pEnc->mbParam.max_quant[i]; | ||
730 : | } | ||
731 : | |||
732 : | data.reference.csp = XVID_CSP_PLANAR; | ||
733 : | data.reference.plane[0] = pEnc->reference->image.y; | ||
734 : | data.reference.plane[1] = pEnc->reference->image.u; | ||
735 : | data.reference.plane[2] = pEnc->reference->image.v; | ||
736 : | data.reference.stride[0] = pEnc->mbParam.edged_width; | ||
737 : | data.reference.stride[1] = pEnc->mbParam.edged_width/2; | ||
738 : | data.reference.stride[2] = pEnc->mbParam.edged_width/2; | ||
739 : | |||
740 : | data.current.csp = XVID_CSP_PLANAR; | ||
741 : | data.current.plane[0] = frame->image.y; | ||
742 : | data.current.plane[1] = frame->image.u; | ||
743 : | data.current.plane[2] = frame->image.v; | ||
744 : | data.current.stride[0] = pEnc->mbParam.edged_width; | ||
745 : | data.current.stride[1] = pEnc->mbParam.edged_width/2; | ||
746 : | data.current.stride[2] = pEnc->mbParam.edged_width/2; | ||
747 : | |||
748 : | data.frame_num = frame->frame_num; | ||
749 : | |||
750 : | if (opt == XVID_PLG_BEFORE) { | ||
751 : | data.type = *type; | ||
752 : | data.quant = *quant; | ||
753 : | |||
754 : | data.vol_flags = frame->vol_flags; | ||
755 : | data.vop_flags = frame->vop_flags; | ||
756 : | data.motion_flags = frame->motion_flags; | ||
757 : | |||
758 : | } else if (opt == XVID_PLG_FRAME) { | ||
759 : | data.type = coding2type(frame->coding_type); | ||
760 : | data.quant = frame->quant; | ||
761 : | |||
762 : | if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { | ||
763 : | data.dquant = pEnc->temp_dquants; | ||
764 : | data.dquant_stride = pEnc->mbParam.mb_width; | ||
765 : | syskin | 1.125 | memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int)); |
766 : | edgomez | 1.102 | } |
767 : | syskin | 1.121 | |
768 : | if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { | ||
769 : | int block = 0; | ||
770 : | data.lambda = pEnc->temp_lambda; | ||
771 : | for(i = 0;i < pEnc->mbParam.mb_height; i++) | ||
772 : | for(j = 0;j < pEnc->mbParam.mb_width; j++) | ||
773 : | for (k = 0; k < 6; k++) | ||
774 : | data.lambda[block++] = 1.0f; | ||
775 : | } | ||
776 : | |||
777 : | edgomez | 1.102 | } else { /* XVID_PLG_AFTER */ |
778 : | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { | ||
779 : | data.original.csp = XVID_CSP_PLANAR; | ||
780 : | data.original.plane[0] = original->y; | ||
781 : | data.original.plane[1] = original->u; | ||
782 : | data.original.plane[2] = original->v; | ||
783 : | data.original.stride[0] = pEnc->mbParam.edged_width; | ||
784 : | data.original.stride[1] = pEnc->mbParam.edged_width/2; | ||
785 : | data.original.stride[2] = pEnc->mbParam.edged_width/2; | ||
786 : | } | ||
787 : | edgomez | 1.91 | |
788 : | edgomez | 1.102 | if ((frame->vol_flags & XVID_VOL_EXTRASTATS) || |
789 : | (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { | ||
790 : | edgomez | 1.91 | |
791 : | edgomez | 1.102 | data.sse_y = |
792 : | plane_sse( original->y, frame->image.y, | ||
793 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
794 : | pEnc->mbParam.height); | ||
795 : | edgomez | 1.91 | |
796 : | edgomez | 1.102 | data.sse_u = |
797 : | plane_sse( original->u, frame->image.u, | ||
798 : | pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, | ||
799 : | pEnc->mbParam.height/2); | ||
800 : | edgomez | 1.91 | |
801 : | edgomez | 1.102 | data.sse_v = |
802 : | plane_sse( original->v, frame->image.v, | ||
803 : | pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, | ||
804 : | pEnc->mbParam.height/2); | ||
805 : | } | ||
806 : | edgomez | 1.91 | |
807 : | edgomez | 1.102 | data.type = coding2type(frame->coding_type); |
808 : | data.quant = frame->quant; | ||
809 : | edgomez | 1.91 | |
810 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
811 : | data.dquant = pEnc->temp_dquants; | ||
812 : | data.dquant_stride = pEnc->mbParam.mb_width; | ||
813 : | |||
814 : | for (j=0; j<pEnc->mbParam.mb_height; j++) | ||
815 : | for (i=0; i<pEnc->mbParam.mb_width; i++) { | ||
816 : | data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant; | ||
817 : | } | ||
818 : | } | ||
819 : | edgomez | 1.91 | |
820 : | edgomez | 1.102 | data.vol_flags = frame->vol_flags; |
821 : | data.vop_flags = frame->vop_flags; | ||
822 : | data.motion_flags = frame->motion_flags; | ||
823 : | |||
824 : | data.length = frame->length; | ||
825 : | data.kblks = frame->sStat.kblks; | ||
826 : | data.mblks = frame->sStat.mblks; | ||
827 : | data.ublks = frame->sStat.ublks; | ||
828 : | |||
829 : | /* New code */ | ||
830 : | data.stats.type = coding2type(frame->coding_type); | ||
831 : | data.stats.quant = frame->quant; | ||
832 : | data.stats.vol_flags = frame->vol_flags; | ||
833 : | data.stats.vop_flags = frame->vop_flags; | ||
834 : | data.stats.length = frame->length; | ||
835 : | data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8); | ||
836 : | data.stats.kblks = frame->sStat.kblks; | ||
837 : | data.stats.mblks = frame->sStat.mblks; | ||
838 : | data.stats.ublks = frame->sStat.ublks; | ||
839 : | data.stats.sse_y = data.sse_y; | ||
840 : | data.stats.sse_u = data.sse_u; | ||
841 : | data.stats.sse_v = data.sse_v; | ||
842 : | edgomez | 1.91 | |
843 : | edgomez | 1.102 | if (stats) |
844 : | *stats = data.stats; | ||
845 : | } | ||
846 : | edgomez | 1.91 | |
847 : | edgomez | 1.102 | /* call plugins */ |
848 : | for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { | ||
849 : | emms(); | ||
850 : | if (pEnc->plugins[i].func) { | ||
851 : | suxen_drol | 1.120 | if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) { |
852 : | edgomez | 1.102 | continue; |
853 : | } | ||
854 : | } | ||
855 : | } | ||
856 : | emms(); | ||
857 : | edgomez | 1.91 | |
858 : | edgomez | 1.102 | /* copy modified values back into frame*/ |
859 : | if (opt == XVID_PLG_BEFORE) { | ||
860 : | *type = data.type; | ||
861 : | *quant = data.quant > 0 ? data.quant : 2; /* default */ | ||
862 : | |||
863 : | frame->vol_flags = data.vol_flags; | ||
864 : | frame->vop_flags = data.vop_flags; | ||
865 : | frame->motion_flags = data.motion_flags; | ||
866 : | |||
867 : | } else if (opt == XVID_PLG_FRAME) { | ||
868 : | edgomez | 1.91 | |
869 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
870 : | for (j=0; j<pEnc->mbParam.mb_height; j++) | ||
871 : | for (i=0; i<pEnc->mbParam.mb_width; i++) { | ||
872 : | frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i]; | ||
873 : | } | ||
874 : | } else { | ||
875 : | for (j=0; j<pEnc->mbParam.mb_height; j++) | ||
876 : | for (i=0; i<pEnc->mbParam.mb_width; i++) { | ||
877 : | frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; | ||
878 : | } | ||
879 : | } | ||
880 : | syskin | 1.121 | |
881 : | if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) { | ||
882 : | for (j = 0; j < pEnc->mbParam.mb_height; j++) | ||
883 : | for (i = 0; i < pEnc->mbParam.mb_width; i++) | ||
884 : | for (k = 0; k < 6; k++) { | ||
885 : | frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = | ||
886 : | (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]); | ||
887 : | } | ||
888 : | } else { | ||
889 : | for (j = 0; j<pEnc->mbParam.mb_height; j++) | ||
890 : | for (i = 0; i<pEnc->mbParam.mb_width; i++) | ||
891 : | for (k = 0; k < 6; k++) { | ||
892 : | frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP; | ||
893 : | } | ||
894 : | } | ||
895 : | |||
896 : | |||
897 : | edgomez | 1.102 | frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
898 : | } | ||
899 : | edgomez | 1.91 | |
900 : | |||
901 : | edgomez | 1.102 | } |
902 : | edgomez | 1.91 | |
903 : | |||
904 : | edgomez | 1.102 | static __inline void inc_frame_num(Encoder * pEnc) |
905 : | { | ||
906 : | pEnc->current->frame_num = pEnc->m_framenum; | ||
907 : | pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ | ||
908 : | edgomez | 1.91 | |
909 : | edgomez | 1.102 | pEnc->mbParam.m_stamp += pEnc->current->fincr; |
910 : | pEnc->m_framenum++; /* debug ticker */ | ||
911 : | } | ||
912 : | |||
913 : | static __inline void dec_frame_num(Encoder * pEnc) | ||
914 : | { | ||
915 : | pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr; | ||
916 : | pEnc->m_framenum--; /* debug ticker */ | ||
917 : | } | ||
918 : | edgomez | 1.91 | |
919 : | edgomez | 1.102 | static __inline void |
920 : | MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam) | ||
921 : | { | ||
922 : | if (pMB->cbp == 0) { | ||
923 : | /* we want to code dquant but the quantizer value will not be used yet | ||
924 : | let's find out if we can postpone dquant to next MB | ||
925 : | */ | ||
926 : | if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) { | ||
927 : | pMB->dquant = 0; /* it's the last MB of all, the easiest case */ | ||
928 : | return; | ||
929 : | } else { | ||
930 : | MACROBLOCK * next = pMB + 1; | ||
931 : | const MACROBLOCK * prev = pMB - 1; | ||
932 : | if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED) | ||
933 : | /* mode allows dquant change in the future */ | ||
934 : | if (abs(next->quant - prev->quant) <= 2) { | ||
935 : | /* quant change is not out of range */ | ||
936 : | pMB->quant = prev->quant; | ||
937 : | pMB->dquant = 0; | ||
938 : | next->dquant = next->quant - prev->quant; | ||
939 : | return; | ||
940 : | } | ||
941 : | } | ||
942 : | } | ||
943 : | /* couldn't skip this dquant */ | ||
944 : | pMB->mode = MODE_INTER_Q; | ||
945 : | } | ||
946 : | |||
947 : | edgomez | 1.91 | |
948 : | |||
949 : | edgomez | 1.102 | static __inline void |
950 : | set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) | ||
951 : | { | ||
952 : | edgomez | 1.91 | |
953 : | edgomez | 1.102 | pCur->ticks = (int32_t)pCur->stamp % time_base; |
954 : | pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; | ||
955 : | edgomez | 1.91 | |
956 : | edgomez | 1.102 | #if 0 /* HEAVY DEBUG OUTPUT */ |
957 : | fprintf(stderr,"WriteVop: %d - %d \n", | ||
958 : | ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); | ||
959 : | fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", | ||
960 : | pCur->coding_type, pCur->stamp, pRef->stamp, time_base); | ||
961 : | fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", | ||
962 : | pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); | ||
963 : | #endif | ||
964 : | } | ||
965 : | edgomez | 1.91 | |
966 : | edgomez | 1.102 | static void |
967 : | simplify_par(int *par_width, int *par_height) | ||
968 : | { | ||
969 : | edgomez | 1.91 | |
970 : | edgomez | 1.102 | int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width; |
971 : | int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height; | ||
972 : | int divisor = gcd(_par_width, _par_height); | ||
973 : | |||
974 : | _par_width /= divisor; | ||
975 : | _par_height /= divisor; | ||
976 : | |||
977 : | /* 2^8 precision maximum */ | ||
978 : | if (_par_width>255 || _par_height>255) { | ||
979 : | float div; | ||
980 : | edgomez | 1.91 | emms(); |
981 : | edgomez | 1.102 | if (_par_width>_par_height) |
982 : | div = (float)_par_width/255; | ||
983 : | else | ||
984 : | div = (float)_par_height/255; | ||
985 : | edgomez | 1.91 | |
986 : | edgomez | 1.102 | _par_width = (int)((float)_par_width/div); |
987 : | _par_height = (int)((float)_par_height/div); | ||
988 : | edgomez | 1.91 | } |
989 : | |||
990 : | edgomez | 1.102 | *par_width = _par_width; |
991 : | *par_height = _par_height; | ||
992 : | edgomez | 1.91 | |
993 : | edgomez | 1.102 | return; |
994 : | } | ||
995 : | edgomez | 1.91 | |
996 : | syskin | 1.96 | |
997 : | edgomez | 1.102 | /***************************************************************************** |
998 : | * IPB frame encoder entry point | ||
999 : | * | ||
1000 : | * Returned values : | ||
1001 : | * - >0 - output bytes | ||
1002 : | * - 0 - no output | ||
1003 : | * - XVID_ERR_VERSION - wrong version passed to core | ||
1004 : | * - XVID_ERR_END - End of stream reached before end of coding | ||
1005 : | * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong | ||
1006 : | * format | ||
1007 : | ****************************************************************************/ | ||
1008 : | edgomez | 1.91 | |
1009 : | |||
1010 : | edgomez | 1.102 | int |
1011 : | enc_encode(Encoder * pEnc, | ||
1012 : | xvid_enc_frame_t * xFrame, | ||
1013 : | xvid_enc_stats_t * stats) | ||
1014 : | { | ||
1015 : | xvid_enc_frame_t * frame; | ||
1016 : | int type; | ||
1017 : | Bitstream bs; | ||
1018 : | Isibaar | 1.97 | |
1019 : | edgomez | 1.102 | if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */ |
1020 : | return XVID_ERR_VERSION; | ||
1021 : | Isibaar | 1.97 | |
1022 : | edgomez | 1.102 | xFrame->out_flags = 0; |
1023 : | edgomez | 1.91 | |
1024 : | edgomez | 1.102 | start_global_timer(); |
1025 : | BitstreamInit(&bs, xFrame->bitstream, 0); | ||
1026 : | edgomez | 1.91 | |
1027 : | |||
1028 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1029 : | * enqueue image to the encoding-queue | ||
1030 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1031 : | edgomez | 1.91 | |
1032 : | edgomez | 1.102 | if (xFrame->input.csp != XVID_CSP_NULL) |
1033 : | edgomez | 1.91 | { |
1034 : | edgomez | 1.102 | QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail]; |
1035 : | edgomez | 1.91 | |
1036 : | start_timer(); | ||
1037 : | if (image_input | ||
1038 : | edgomez | 1.102 | (&q->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1039 : | pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, | ||
1040 : | xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING)) | ||
1041 : | edgomez | 1.91 | { |
1042 : | emms(); | ||
1043 : | return XVID_ERR_FORMAT; | ||
1044 : | } | ||
1045 : | stop_conv_timer(); | ||
1046 : | |||
1047 : | edgomez | 1.102 | if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
1048 : | image_chroma_optimize(&q->image, | ||
1049 : | edgomez | 1.92 | pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1050 : | } | ||
1051 : | |||
1052 : | edgomez | 1.102 | q->frame = *xFrame; |
1053 : | |||
1054 : | if (xFrame->quant_intra_matrix) | ||
1055 : | edgomez | 1.91 | { |
1056 : | edgomez | 1.102 | memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char)); |
1057 : | q->frame.quant_intra_matrix = q->quant_intra_matrix; | ||
1058 : | edgomez | 1.91 | } |
1059 : | |||
1060 : | edgomez | 1.102 | if (xFrame->quant_inter_matrix) |
1061 : | { | ||
1062 : | memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char)); | ||
1063 : | q->frame.quant_inter_matrix = q->quant_inter_matrix; | ||
1064 : | edgomez | 1.91 | } |
1065 : | |||
1066 : | edgomez | 1.102 | pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1); |
1067 : | pEnc->queue_size++; | ||
1068 : | edgomez | 1.91 | } |
1069 : | |||
1070 : | |||
1071 : | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
1072 : | edgomez | 1.102 | * bframe flush code |
1073 : | edgomez | 1.91 | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1074 : | |||
1075 : | edgomez | 1.102 | repeat: |
1076 : | edgomez | 1.91 | |
1077 : | edgomez | 1.102 | if (pEnc->flush_bframes) |
1078 : | { | ||
1079 : | if (pEnc->bframenum_head < pEnc->bframenum_tail) { | ||
1080 : | edgomez | 1.91 | |
1081 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1082 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1083 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1084 : | edgomez | 1.91 | |
1085 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1086 : | image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image, | ||
1087 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
1088 : | edgomez | 1.91 | } |
1089 : | |||
1090 : | edgomez | 1.102 | FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
1091 : | suxen_drol | 1.120 | call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats); |
1092 : | edgomez | 1.102 | pEnc->bframenum_head++; |
1093 : | edgomez | 1.91 | |
1094 : | edgomez | 1.102 | goto done; |
1095 : | } | ||
1096 : | edgomez | 1.91 | |
1097 : | edgomez | 1.102 | /* write an empty marker to the bitstream. |
1098 : | edgomez | 1.91 | |
1099 : | edgomez | 1.102 | for divx5 decoder compatibility, this marker must consist |
1100 : | of a not-coded p-vop, with a time_base of zero, and time_increment | ||
1101 : | indentical to the future-referece frame. | ||
1102 : | */ | ||
1103 : | edgomez | 1.91 | |
1104 : | edgomez | 1.102 | if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) { |
1105 : | int tmp; | ||
1106 : | int bits; | ||
1107 : | edgomez | 1.91 | |
1108 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1109 : | edgomez | 1.91 | pEnc->bframenum_head, pEnc->bframenum_tail, |
1110 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1111 : | |||
1112 : | edgomez | 1.102 | bits = BitstreamPos(&bs); |
1113 : | edgomez | 1.91 | |
1114 : | edgomez | 1.102 | tmp = pEnc->current->seconds; |
1115 : | pEnc->current->seconds = 0; /* force time_base = 0 */ | ||
1116 : | edgomez | 1.91 | |
1117 : | edgomez | 1.102 | BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant); |
1118 : | BitstreamPad(&bs); | ||
1119 : | pEnc->current->seconds = tmp; | ||
1120 : | edgomez | 1.91 | |
1121 : | edgomez | 1.102 | /* add the not-coded length to the reference frame size */ |
1122 : | pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; | ||
1123 : | suxen_drol | 1.120 | call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1124 : | edgomez | 1.91 | |
1125 : | edgomez | 1.102 | /* flush complete: reset counters */ |
1126 : | pEnc->flush_bframes = 0; | ||
1127 : | pEnc->bframenum_head = pEnc->bframenum_tail = 0; | ||
1128 : | goto done; | ||
1129 : | edgomez | 1.91 | |
1130 : | } | ||
1131 : | |||
1132 : | edgomez | 1.102 | /* flush complete: reset counters */ |
1133 : | pEnc->flush_bframes = 0; | ||
1134 : | pEnc->bframenum_head = pEnc->bframenum_tail = 0; | ||
1135 : | } | ||
1136 : | edgomez | 1.91 | |
1137 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1138 : | * dequeue frame from the encoding queue | ||
1139 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1140 : | edgomez | 1.91 | |
1141 : | edgomez | 1.102 | if (pEnc->queue_size == 0) /* empty */ |
1142 : | { | ||
1143 : | if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ | ||
1144 : | { | ||
1145 : | edgomez | 1.91 | |
1146 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1147 : | edgomez | 1.91 | pEnc->bframenum_head, pEnc->bframenum_tail, |
1148 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1149 : | |||
1150 : | edgomez | 1.102 | if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
1151 : | suxen_drol | 1.120 | call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1152 : | edgomez | 1.102 | } |
1153 : | edgomez | 1.91 | |
1154 : | edgomez | 1.102 | /* if the very last frame is to be b-vop, we must change it to a p-vop */ |
1155 : | if (pEnc->bframenum_tail > 0) { | ||
1156 : | edgomez | 1.91 | |
1157 : | edgomez | 1.102 | SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1158 : | pEnc->bframenum_tail--; | ||
1159 : | SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); | ||
1160 : | edgomez | 1.91 | |
1161 : | edgomez | 1.102 | /* convert B-VOP to P-VOP */ |
1162 : | pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; | ||
1163 : | pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ | ||
1164 : | pEnc->current->quant /= pEnc->mbParam.bquant_ratio; | ||
1165 : | edgomez | 1.91 | |
1166 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1167 : | image_copy(&pEnc->sOriginal, &pEnc->current->image, | ||
1168 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
1169 : | } | ||
1170 : | syskin | 1.96 | |
1171 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1172 : | edgomez | 1.91 | pEnc->bframenum_head, pEnc->bframenum_tail, |
1173 : | edgomez | 1.102 | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1174 : | syskin | 1.103 | pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
1175 : | edgomez | 1.91 | |
1176 : | syskin | 1.104 | FrameCodeP(pEnc, &bs); |
1177 : | edgomez | 1.91 | |
1178 : | |||
1179 : | edgomez | 1.102 | if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
1180 : | suxen_drol | 1.120 | call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1181 : | edgomez | 1.102 | }else{ |
1182 : | pEnc->flush_bframes = 1; | ||
1183 : | goto done; | ||
1184 : | } | ||
1185 : | } | ||
1186 : | DPRINTF(XVID_DEBUG_DEBUG, "*** END\n"); | ||
1187 : | edgomez | 1.91 | |
1188 : | edgomez | 1.102 | emms(); |
1189 : | return XVID_ERR_END; /* end of stream reached */ | ||
1190 : | edgomez | 1.91 | } |
1191 : | edgomez | 1.102 | goto done; /* nothing to encode yet; encoder lag */ |
1192 : | edgomez | 1.91 | } |
1193 : | |||
1194 : | edgomez | 1.102 | /* the current FRAME becomes the reference */ |
1195 : | SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); | ||
1196 : | edgomez | 1.91 | |
1197 : | edgomez | 1.102 | /* remove frame from encoding-queue (head), and move it into the current */ |
1198 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); | ||
1199 : | frame = &pEnc->queue[pEnc->queue_head].frame; | ||
1200 : | pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1); | ||
1201 : | pEnc->queue_size--; | ||
1202 : | edgomez | 1.91 | |
1203 : | Isibaar | 1.1 | |
1204 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1205 : | * init pEnc->current fields | ||
1206 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1207 : | Isibaar | 1.1 | |
1208 : | edgomez | 1.102 | pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1209 : | edgomez | 1.91 | inc_frame_num(pEnc); |
1210 : | edgomez | 1.102 | pEnc->current->vol_flags = frame->vol_flags; |
1211 : | pEnc->current->vop_flags = frame->vop_flags; | ||
1212 : | pEnc->current->motion_flags = frame->motion; | ||
1213 : | pEnc->current->fcode = pEnc->mbParam.m_fcode; | ||
1214 : | pEnc->current->bcode = pEnc->mbParam.m_fcode; | ||
1215 : | edgomez | 1.91 | |
1216 : | edgomez | 1.92 | |
1217 : | edgomez | 1.102 | if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
1218 : | syskin | 1.96 | image_chroma_optimize(&pEnc->current->image, |
1219 : | edgomez | 1.92 | pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1220 : | } | ||
1221 : | Isibaar | 1.1 | |
1222 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1223 : | * frame type & quant selection | ||
1224 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1225 : | |||
1226 : | type = frame->type; | ||
1227 : | pEnc->current->quant = frame->quant; | ||
1228 : | |||
1229 : | Skal | 1.118 | call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats); |
1230 : | edgomez | 1.102 | |
1231 : | if (type > 0){ /* XVID_TYPE_?VOP */ | ||
1232 : | type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ | ||
1233 : | } else{ /* XVID_TYPE_AUTO */ | ||
1234 : | if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){ | ||
1235 : | pEnc->iFrameNum = 0; | ||
1236 : | type = I_VOP; | ||
1237 : | }else{ | ||
1238 : | type = MEanalysis(&pEnc->reference->image, pEnc->current, | ||
1239 : | &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, | ||
1240 : | pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold, | ||
1241 : | (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL); | ||
1242 : | } | ||
1243 : | edgomez | 1.91 | } |
1244 : | Isibaar | 1.23 | |
1245 : | edgomez | 1.102 | if (type != I_VOP) |
1246 : | pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */ | ||
1247 : | |||
1248 : | /* bframes buffer overflow check */ | ||
1249 : | if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { | ||
1250 : | type = P_VOP; | ||
1251 : | } | ||
1252 : | suxen_drol | 1.27 | |
1253 : | edgomez | 1.102 | pEnc->iFrameNum++; |
1254 : | Isibaar | 1.1 | |
1255 : | edgomez | 1.102 | if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1256 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, | ||
1257 : | "%d st:%lld if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); | ||
1258 : | Isibaar | 1.1 | } |
1259 : | |||
1260 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1261 : | * encode this frame as a b-vop | ||
1262 : | * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) | ||
1263 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1264 : | if (type == B_VOP) { | ||
1265 : | if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { | ||
1266 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); | ||
1267 : | } | ||
1268 : | |||
1269 : | if (frame->quant < 1) { | ||
1270 : | pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * | ||
1271 : | pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; | ||
1272 : | edgomez | 1.91 | |
1273 : | edgomez | 1.102 | } else { |
1274 : | pEnc->current->quant = frame->quant; | ||
1275 : | } | ||
1276 : | edgomez | 1.41 | |
1277 : | edgomez | 1.102 | if (pEnc->current->quant < 1) |
1278 : | pEnc->current->quant = 1; | ||
1279 : | else if (pEnc->current->quant > 31) | ||
1280 : | pEnc->current->quant = 31; | ||
1281 : | edgomez | 1.41 | |
1282 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1283 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1284 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); | ||
1285 : | edgomez | 1.40 | |
1286 : | edgomez | 1.102 | /* store frame into bframe buffer & swap ref back to current */ |
1287 : | SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); | ||
1288 : | SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); | ||
1289 : | edgomez | 1.41 | |
1290 : | edgomez | 1.102 | pEnc->bframenum_tail++; |
1291 : | edgomez | 1.40 | |
1292 : | edgomez | 1.102 | goto repeat; |
1293 : | } | ||
1294 : | edgomez | 1.40 | |
1295 : | |||
1296 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1297 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1298 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1299 : | edgomez | 1.40 | |
1300 : | edgomez | 1.102 | /* for unpacked bframes, output the stats for the last encoded frame */ |
1301 : | if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) | ||
1302 : | { | ||
1303 : | if (pEnc->current->stamp > 0) { | ||
1304 : | suxen_drol | 1.120 | call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1305 : | edgomez | 1.40 | } |
1306 : | edgomez | 1.102 | else |
1307 : | stats->type = XVID_TYPE_NOTHING; | ||
1308 : | } | ||
1309 : | edgomez | 1.40 | |
1310 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1311 : | * closed-gop | ||
1312 : | * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe | ||
1313 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1314 : | Isibaar | 1.1 | |
1315 : | edgomez | 1.102 | if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1316 : | Isibaar | 1.1 | |
1317 : | edgomez | 1.102 | /* place this frame back on the encoding-queue (head) */ |
1318 : | /* we will deal with it next time */ | ||
1319 : | dec_frame_num(pEnc); | ||
1320 : | pEnc->iFrameNum--; | ||
1321 : | edgomez | 1.13 | |
1322 : | edgomez | 1.102 | pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1); |
1323 : | pEnc->queue_size++; | ||
1324 : | image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); | ||
1325 : | edgomez | 1.41 | |
1326 : | edgomez | 1.102 | /* grab the last frame from the bframe-queue */ |
1327 : | edgomez | 1.41 | |
1328 : | edgomez | 1.102 | pEnc->bframenum_tail--; |
1329 : | SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); | ||
1330 : | Isibaar | 1.1 | |
1331 : | edgomez | 1.102 | if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1332 : | suxen_drol | 1.117 | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP"); |
1333 : | edgomez | 1.40 | } |
1334 : | |||
1335 : | edgomez | 1.102 | /* convert B-VOP quant to P-VOP */ |
1336 : | pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; | ||
1337 : | pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ | ||
1338 : | pEnc->current->quant /= pEnc->mbParam.bquant_ratio; | ||
1339 : | type = P_VOP; | ||
1340 : | edgomez | 1.3 | } |
1341 : | Isibaar | 1.1 | |
1342 : | edgomez | 1.93 | |
1343 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1344 : | * encode this frame as an i-vop | ||
1345 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1346 : | h | 1.5 | |
1347 : | edgomez | 1.102 | if (type == I_VOP) { |
1348 : | edgomez | 1.41 | |
1349 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1350 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1351 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1352 : | Isibaar | 1.7 | |
1353 : | edgomez | 1.102 | if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1354 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); | ||
1355 : | } | ||
1356 : | syskin | 1.96 | |
1357 : | edgomez | 1.102 | pEnc->iFrameNum = 1; |
1358 : | Isibaar | 1.1 | |
1359 : | edgomez | 1.102 | /* ---- update vol flags at IVOP ----------- */ |
1360 : | pEnc->mbParam.vol_flags = pEnc->current->vol_flags; | ||
1361 : | edgomez | 1.41 | |
1362 : | edgomez | 1.102 | /* Aspect ratio */ |
1363 : | switch(frame->par) { | ||
1364 : | case XVID_PAR_11_VGA: | ||
1365 : | case XVID_PAR_43_PAL: | ||
1366 : | case XVID_PAR_43_NTSC: | ||
1367 : | case XVID_PAR_169_PAL: | ||
1368 : | case XVID_PAR_169_NTSC: | ||
1369 : | case XVID_PAR_EXT: | ||
1370 : | pEnc->mbParam.par = frame->par; | ||
1371 : | break; | ||
1372 : | default: | ||
1373 : | pEnc->mbParam.par = XVID_PAR_11_VGA; | ||
1374 : | break; | ||
1375 : | } | ||
1376 : | Isibaar | 1.1 | |
1377 : | edgomez | 1.102 | /* For extended PAR only, we try to sanityse/simplify par values */ |
1378 : | if (pEnc->mbParam.par == XVID_PAR_EXT) { | ||
1379 : | pEnc->mbParam.par_width = frame->par_width; | ||
1380 : | pEnc->mbParam.par_height = frame->par_height; | ||
1381 : | simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height); | ||
1382 : | } | ||
1383 : | Isibaar | 1.1 | |
1384 : | edgomez | 1.102 | if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1385 : | if (frame->quant_intra_matrix != NULL) | ||
1386 : | set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix); | ||
1387 : | if (frame->quant_inter_matrix != NULL) | ||
1388 : | set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix); | ||
1389 : | } | ||
1390 : | Isibaar | 1.1 | |
1391 : | edgomez | 1.102 | /* prevent vol/vop misuse */ |
1392 : | Isibaar | 1.1 | |
1393 : | edgomez | 1.102 | if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
1394 : | pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); | ||
1395 : | suxen_drol | 1.27 | |
1396 : | edgomez | 1.102 | /* ^^^------------------------ */ |
1397 : | edgomez | 1.41 | |
1398 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1399 : | image_copy(&pEnc->sOriginal, &pEnc->current->image, | ||
1400 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
1401 : | Isibaar | 1.1 | } |
1402 : | |||
1403 : | edgomez | 1.102 | FrameCodeI(pEnc, &bs); |
1404 : | xFrame->out_flags |= XVID_KEYFRAME; | ||
1405 : | Isibaar | 1.1 | |
1406 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1407 : | * encode this frame as an p-vop | ||
1408 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1409 : | Isibaar | 1.1 | |
1410 : | edgomez | 1.102 | } else { /* (type == P_VOP || type == S_VOP) */ |
1411 : | h | 1.20 | |
1412 : | edgomez | 1.102 | DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1413 : | pEnc->bframenum_head, pEnc->bframenum_tail, | ||
1414 : | pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); | ||
1415 : | h | 1.20 | |
1416 : | edgomez | 1.102 | if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1417 : | image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); | ||
1418 : | } | ||
1419 : | h | 1.20 | |
1420 : | edgomez | 1.102 | if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1421 : | image_copy(&pEnc->sOriginal, &pEnc->current->image, | ||
1422 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
1423 : | } | ||
1424 : | h | 1.20 | |
1425 : | syskin | 1.104 | if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1426 : | syskin | 1.103 | /* N-VOP, we mustn't code b-frames yet */ |
1427 : | Isibaar | 1.124 | if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || |
1428 : | pEnc->mbParam.max_bframes == 0) | ||
1429 : | call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); | ||
1430 : | syskin | 1.103 | goto done; |
1431 : | } | ||
1432 : | h | 1.20 | } |
1433 : | |||
1434 : | edgomez | 1.41 | |
1435 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1436 : | * on next enc_encode call we must flush bframes | ||
1437 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1438 : | edgomez | 1.41 | |
1439 : | edgomez | 1.102 | /*done_flush:*/ |
1440 : | h | 1.26 | |
1441 : | edgomez | 1.102 | pEnc->flush_bframes = 1; |
1442 : | h | 1.26 | |
1443 : | edgomez | 1.102 | /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */ |
1444 : | if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { | ||
1445 : | goto repeat; | ||
1446 : | h | 1.20 | } |
1447 : | |||
1448 : | edgomez | 1.102 | /* packed or no-bframes or no-bframes-queued: output stats */ |
1449 : | if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) { | ||
1450 : | suxen_drol | 1.120 | call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats); |
1451 : | edgomez | 1.102 | } |
1452 : | h | 1.20 | |
1453 : | edgomez | 1.102 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1454 : | * done; return number of bytes consumed | ||
1455 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
1456 : | h | 1.20 | |
1457 : | edgomez | 1.102 | done: |
1458 : | h | 1.20 | |
1459 : | edgomez | 1.102 | stop_global_timer(); |
1460 : | write_timer(); | ||
1461 : | h | 1.20 | |
1462 : | edgomez | 1.102 | emms(); |
1463 : | return BitstreamLength(&bs); | ||
1464 : | } | ||
1465 : | h | 1.20 | |
1466 : | |||
1467 : | edgomez | 1.102 | static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1468 : | { | ||
1469 : | unsigned int i; | ||
1470 : | MACROBLOCK * pMB = frame->mbs; | ||
1471 : | int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */ | ||
1472 : | if (quant > 31) | ||
1473 : | frame->quant = quant = 31; | ||
1474 : | else if (quant < 1) | ||
1475 : | frame->quant = quant = 1; | ||
1476 : | |||
1477 : | for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) { | ||
1478 : | quant += pMB->dquant; | ||
1479 : | if (quant > 31) | ||
1480 : | quant = 31; | ||
1481 : | else if (quant < 1) | ||
1482 : | quant = 1; | ||
1483 : | pMB->quant = quant; | ||
1484 : | pMB++; | ||
1485 : | h | 1.20 | } |
1486 : | edgomez | 1.102 | } |
1487 : | h | 1.20 | |
1488 : | |||
1489 : | edgomez | 1.102 | static __inline void |
1490 : | CodeIntraMB(Encoder * pEnc, | ||
1491 : | MACROBLOCK * pMB) | ||
1492 : | { | ||
1493 : | h | 1.20 | |
1494 : | edgomez | 1.102 | pMB->mode = MODE_INTRA; |
1495 : | h | 1.20 | |
1496 : | edgomez | 1.102 | /* zero mv statistics */ |
1497 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
1498 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
1499 : | pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
1500 : | pMB->sad16 = 0; | ||
1501 : | h | 1.20 | |
1502 : | edgomez | 1.102 | if (pMB->dquant != 0) { |
1503 : | pMB->mode = MODE_INTRA_Q; | ||
1504 : | h | 1.20 | } |
1505 : | } | ||
1506 : | |||
1507 : | |||
1508 : | edgomez | 1.102 | |
1509 : | edgomez | 1.41 | static int |
1510 : | FrameCodeI(Encoder * pEnc, | ||
1511 : | edgomez | 1.102 | Bitstream * bs) |
1512 : | h | 1.21 | { |
1513 : | edgomez | 1.102 | int bits = BitstreamPos(bs); |
1514 : | edgomez | 1.91 | int mb_width = pEnc->mbParam.mb_width; |
1515 : | int mb_height = pEnc->mbParam.mb_height; | ||
1516 : | h | 1.21 | |
1517 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
1518 : | edgomez | 1.41 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1519 : | h | 1.21 | |
1520 : | uint16_t x, y; | ||
1521 : | |||
1522 : | suxen_drol | 1.27 | pEnc->mbParam.m_rounding_type = 1; |
1523 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
1524 : | pEnc->current->coding_type = I_VOP; | ||
1525 : | h | 1.21 | |
1526 : | edgomez | 1.102 | call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1527 : | |||
1528 : | SetMacroblockQuants(&pEnc->mbParam, pEnc->current); | ||
1529 : | |||
1530 : | suxen_drol | 1.27 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1531 : | edgomez | 1.78 | |
1532 : | edgomez | 1.91 | set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1533 : | |||
1534 : | edgomez | 1.101 | BitstreamPad(bs); |
1535 : | h | 1.21 | |
1536 : | edgomez | 1.102 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
1537 : | h | 1.21 | |
1538 : | edgomez | 1.91 | pEnc->current->sStat.iTextBits = 0; |
1539 : | Isibaar | 1.123 | pEnc->current->sStat.iMVBits = 0; |
1540 : | edgomez | 1.91 | pEnc->current->sStat.kblks = mb_width * mb_height; |
1541 : | pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; | ||
1542 : | h | 1.21 | |
1543 : | edgomez | 1.91 | for (y = 0; y < mb_height; y++) |
1544 : | for (x = 0; x < mb_width; x++) { | ||
1545 : | edgomez | 1.41 | MACROBLOCK *pMB = |
1546 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1547 : | h | 1.21 | |
1548 : | CodeIntraMB(pEnc, pMB); | ||
1549 : | |||
1550 : | edgomez | 1.41 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1551 : | dct_codes, qcoeff); | ||
1552 : | h | 1.21 | |
1553 : | start_timer(); | ||
1554 : | suxen_drol | 1.27 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1555 : | h | 1.21 | stop_prediction_timer(); |
1556 : | |||
1557 : | start_timer(); | ||
1558 : | edgomez | 1.91 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1559 : | h | 1.21 | stop_coding_timer(); |
1560 : | } | ||
1561 : | |||
1562 : | emms(); | ||
1563 : | |||
1564 : | edgomez | 1.102 | BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1565 : | |||
1566 : | pEnc->current->length = (BitstreamPos(bs) - bits) / 8; | ||
1567 : | |||
1568 : | edgomez | 1.91 | pEnc->fMvPrevSigma = -1; |
1569 : | suxen_drol | 1.27 | pEnc->mbParam.m_fcode = 2; |
1570 : | h | 1.21 | |
1571 : | edgomez | 1.102 | pEnc->current->is_edged = 0; /* not edged */ |
1572 : | pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ | ||
1573 : | h | 1.21 | |
1574 : | edgomez | 1.93 | return 1; /* intra */ |
1575 : | h | 1.21 | } |
1576 : | |||
1577 : | syskin | 1.114 | static __inline void |
1578 : | updateFcode(Statistics * sStat, Encoder * pEnc) | ||
1579 : | { | ||
1580 : | float fSigma; | ||
1581 : | int iSearchRange; | ||
1582 : | |||
1583 : | if (sStat->iMvCount == 0) | ||
1584 : | sStat->iMvCount = 1; | ||
1585 : | |||
1586 : | fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); | ||
1587 : | |||
1588 : | iSearchRange = 16 << pEnc->mbParam.m_fcode; | ||
1589 : | |||
1590 : | if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) | ||
1591 : | pEnc->mbParam.m_fcode++; | ||
1592 : | |||
1593 : | else if ((5.0 * fSigma < iSearchRange) | ||
1594 : | && (4.0 * pEnc->fMvPrevSigma < iSearchRange) | ||
1595 : | && (pEnc->mbParam.m_fcode >= 2) ) | ||
1596 : | pEnc->mbParam.m_fcode--; | ||
1597 : | |||
1598 : | pEnc->fMvPrevSigma = fSigma; | ||
1599 : | } | ||
1600 : | h | 1.21 | |
1601 : | edgomez | 1.91 | #define BFRAME_SKIP_THRESHHOLD 30 |
1602 : | |||
1603 : | /* FrameCodeP also handles S(GMC)-VOPs */ | ||
1604 : | edgomez | 1.41 | static int |
1605 : | FrameCodeP(Encoder * pEnc, | ||
1606 : | syskin | 1.104 | Bitstream * bs) |
1607 : | Isibaar | 1.1 | { |
1608 : | edgomez | 1.102 | int bits = BitstreamPos(bs); |
1609 : | edgomez | 1.13 | |
1610 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
1611 : | edgomez | 1.41 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1612 : | Isibaar | 1.8 | |
1613 : | edgomez | 1.91 | int x, y, k; |
1614 : | edgomez | 1.102 | FRAMEINFO *const current = pEnc->current; |
1615 : | FRAMEINFO *const reference = pEnc->reference; | ||
1616 : | MBParam * const pParam = &pEnc->mbParam; | ||
1617 : | int mb_width = pParam->mb_width; | ||
1618 : | int mb_height = pParam->mb_height; | ||
1619 : | syskin | 1.103 | int coded = 1; |
1620 : | syskin | 1.96 | |
1621 : | edgomez | 1.102 | IMAGE *pRef = &reference->image; |
1622 : | |||
1623 : | if (!reference->is_edged) { | ||
1624 : | start_timer(); | ||
1625 : | image_setedges(pRef, pParam->edged_width, pParam->edged_height, | ||
1626 : | pParam->width, pParam->height, 0); | ||
1627 : | stop_edges_timer(); | ||
1628 : | reference->is_edged = 1; | ||
1629 : | } | ||
1630 : | |||
1631 : | pParam->m_rounding_type = 1 - pParam->m_rounding_type; | ||
1632 : | current->rounding_type = pParam->m_rounding_type; | ||
1633 : | current->fcode = pParam->m_fcode; | ||
1634 : | Isibaar | 1.1 | |
1635 : | edgomez | 1.102 | if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1636 : | if (reference->is_interpolated != current->rounding_type) { | ||
1637 : | start_timer(); | ||
1638 : | syskin | 1.122 | image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y, |
1639 : | pEnc->vInterHV.y, pParam->edged_width, | ||
1640 : | edgomez | 1.102 | pParam->edged_height, |
1641 : | (pParam->vol_flags & XVID_VOL_QUARTERPEL), | ||
1642 : | current->rounding_type); | ||
1643 : | stop_inter_timer(); | ||
1644 : | reference->is_interpolated = current->rounding_type; | ||
1645 : | } | ||
1646 : | Isibaar | 1.1 | } |
1647 : | |||
1648 : | syskin | 1.111 | current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1649 : | Isibaar | 1.123 | current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = |
1650 : | current->sStat.iMVBits = 0; | ||
1651 : | syskin | 1.111 | |
1652 : | edgomez | 1.102 | current->coding_type = P_VOP; |
1653 : | |||
1654 : | call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); | ||
1655 : | |||
1656 : | SetMacroblockQuants(&pEnc->mbParam, current); | ||
1657 : | syskin | 1.96 | |
1658 : | Isibaar | 1.1 | start_timer(); |
1659 : | edgomez | 1.102 | if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
1660 : | { int gmcval; | ||
1661 : | current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, | ||
1662 : | &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); | ||
1663 : | |||
1664 : | if (current->motion_flags & XVID_ME_GME_REFINE) { | ||
1665 : | gmcval = GlobalMotionEstRefine(¤t->warp, | ||
1666 : | current->mbs, pParam, | ||
1667 : | current, reference, | ||
1668 : | ¤t->image, | ||
1669 : | &reference->image, | ||
1670 : | &pEnc->vInterH, | ||
1671 : | &pEnc->vInterV, | ||
1672 : | &pEnc->vInterHV); | ||
1673 : | } else { | ||
1674 : | gmcval = globalSAD(¤t->warp, pParam, current->mbs, | ||
1675 : | current, | ||
1676 : | &reference->image, | ||
1677 : | ¤t->image, | ||
1678 : | pEnc->vGMC.y); | ||
1679 : | } | ||
1680 : | |||
1681 : | gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height); | ||
1682 : | |||
1683 : | /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */ | ||
1684 : | generate_GMCparameters( 3, 3, ¤t->warp, | ||
1685 : | pParam->width, pParam->height, | ||
1686 : | ¤t->new_gmc_data); | ||
1687 : | |||
1688 : | if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) || | ||
1689 : | (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) ) | ||
1690 : | { | ||
1691 : | current->coding_type = S_VOP; | ||
1692 : | |||
1693 : | generate_GMCimage(¤t->new_gmc_data, &reference->image, | ||
1694 : | pParam->mb_width, pParam->mb_height, | ||
1695 : | pParam->edged_width, pParam->edged_width/2, | ||
1696 : | pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, | ||
1697 : | current->rounding_type, current->mbs, &pEnc->vGMC); | ||
1698 : | |||
1699 : | } else { | ||
1700 : | edgomez | 1.91 | |
1701 : | edgomez | 1.102 | generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1702 : | pParam->mb_width, pParam->mb_height, | ||
1703 : | pParam->edged_width, pParam->edged_width/2, | ||
1704 : | pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, | ||
1705 : | current->rounding_type, current->mbs, NULL); /* no warping, just AMV */ | ||
1706 : | } | ||
1707 : | } | ||
1708 : | Isibaar | 1.1 | |
1709 : | edgomez | 1.91 | |
1710 : | syskin | 1.126 | if (pEnc->num_threads > 0) { |
1711 : | /* multithreaded motion estimation - dispatch threads */ | ||
1712 : | int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads; | ||
1713 : | |||
1714 : | for (k = 0; k < pEnc->num_threads; k++) { | ||
1715 : | memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); | ||
1716 : | pEnc->motionData[k].pParam = &pEnc->mbParam; | ||
1717 : | pEnc->motionData[k].current = current; | ||
1718 : | pEnc->motionData[k].reference = reference; | ||
1719 : | pEnc->motionData[k].pRefH = &pEnc->vInterH; | ||
1720 : | pEnc->motionData[k].pRefV = &pEnc->vInterV; | ||
1721 : | pEnc->motionData[k].pRefHV = &pEnc->vInterHV; | ||
1722 : | pEnc->motionData[k].pGMC = &pEnc->vGMC; | ||
1723 : | pEnc->motionData[k].y_step = pEnc->num_threads; | ||
1724 : | pEnc->motionData[k].start_y = k; | ||
1725 : | /* todo: sort out temp space once and for all */ | ||
1726 : | pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width; | ||
1727 : | } | ||
1728 : | |||
1729 : | for (k = 0; k < pEnc->num_threads; k++) { | ||
1730 : | pthread_create(&pEnc->motionData[k].handle, NULL, | ||
1731 : | (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]); | ||
1732 : | } | ||
1733 : | } else { | ||
1734 : | /* regular ME */ | ||
1735 : | |||
1736 : | MotionEstimation(&pEnc->mbParam, current, reference, | ||
1737 : | &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
1738 : | &pEnc->vGMC, 256*4096); | ||
1739 : | } | ||
1740 : | edgomez | 1.91 | |
1741 : | edgomez | 1.102 | stop_motion_timer(); |
1742 : | edgomez | 1.91 | |
1743 : | edgomez | 1.102 | set_timecodes(current,reference,pParam->fbase); |
1744 : | Isibaar | 1.1 | |
1745 : | edgomez | 1.102 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1746 : | edgomez | 1.3 | |
1747 : | edgomez | 1.91 | for (y = 0; y < mb_height; y++) { |
1748 : | syskin | 1.126 | |
1749 : | if (pEnc->num_threads > 0) { | ||
1750 : | /* in multithreaded encoding, only proceed with a row of macroblocks | ||
1751 : | if ME finished with that row */ | ||
1752 : | while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != mb_width) | ||
1753 : | sched_yield(); | ||
1754 : | } | ||
1755 : | |||
1756 : | edgomez | 1.91 | for (x = 0; x < mb_width; x++) { |
1757 : | syskin | 1.116 | MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
1758 : | int skip_possible; | ||
1759 : | edgomez | 1.91 | |
1760 : | syskin | 1.115 | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1761 : | Isibaar | 1.1 | CodeIntraMB(pEnc, pMB); |
1762 : | edgomez | 1.102 | MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1763 : | edgomez | 1.41 | dct_codes, qcoeff); |
1764 : | chl | 1.81 | |
1765 : | start_timer(); | ||
1766 : | edgomez | 1.102 | MBPrediction(current, x, y, pParam->mb_width, qcoeff); |
1767 : | chl | 1.81 | stop_prediction_timer(); |
1768 : | edgomez | 1.91 | |
1769 : | edgomez | 1.102 | current->sStat.kblks++; |
1770 : | edgomez | 1.91 | |
1771 : | edgomez | 1.102 | MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1772 : | edgomez | 1.91 | stop_coding_timer(); |
1773 : | continue; | ||
1774 : | } | ||
1775 : | |||
1776 : | start_timer(); | ||
1777 : | edgomez | 1.102 | MBMotionCompensation(pMB, x, y, &reference->image, |
1778 : | edgomez | 1.91 | &pEnc->vInterH, &pEnc->vInterV, |
1779 : | syskin | 1.96 | &pEnc->vInterHV, &pEnc->vGMC, |
1780 : | edgomez | 1.102 | ¤t->image, |
1781 : | dct_codes, pParam->width, | ||
1782 : | pParam->height, | ||
1783 : | pParam->edged_width, | ||
1784 : | (current->vol_flags & XVID_VOL_QUARTERPEL), | ||
1785 : | current->rounding_type); | ||
1786 : | edgomez | 1.91 | |
1787 : | stop_comp_timer(); | ||
1788 : | |||
1789 : | pMB->field_pred = 0; | ||
1790 : | |||
1791 : | syskin | 1.112 | if (pMB->cbp != 0) { |
1792 : | syskin | 1.113 | pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
1793 : | edgomez | 1.91 | dct_codes, qcoeff); |
1794 : | } | ||
1795 : | |||
1796 : | edgomez | 1.102 | if (pMB->dquant != 0) |
1797 : | MBSetDquant(pMB, x, y, &pEnc->mbParam); | ||
1798 : | |||
1799 : | |||
1800 : | edgomez | 1.91 | if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1801 : | pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || | ||
1802 : | pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { | ||
1803 : | edgomez | 1.102 | current->sStat.mblks++; |
1804 : | chl | 1.64 | } else { |
1805 : | edgomez | 1.102 | current->sStat.ublks++; |
1806 : | edgomez | 1.91 | } |
1807 : | edgomez | 1.102 | |
1808 : | Isibaar | 1.1 | start_timer(); |
1809 : | chl | 1.65 | |
1810 : | /* Finished processing the MB, now check if to CODE or SKIP */ | ||
1811 : | |||
1812 : | syskin | 1.116 | skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER); |
1813 : | edgomez | 1.102 | |
1814 : | if (current->coding_type == S_VOP) | ||
1815 : | edgomez | 1.91 | skip_possible &= (pMB->mcsel == 1); |
1816 : | syskin | 1.116 | else { /* PVOP */ |
1817 : | const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ? | ||
1818 : | pMB->qmvs : pMB->mvs; | ||
1819 : | skip_possible &= ((mv->x|mv->y) == 0); | ||
1820 : | edgomez | 1.91 | } |
1821 : | |||
1822 : | syskin | 1.116 | if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1823 : | /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ | ||
1824 : | int bSkip = 1; | ||
1825 : | edgomez | 1.91 | |
1826 : | syskin | 1.116 | if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */ |
1827 : | edgomez | 1.91 | |
1828 : | syskin | 1.116 | for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) { |
1829 : | edgomez | 1.91 | int iSAD; |
1830 : | edgomez | 1.102 | iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
1831 : | syskin | 1.116 | pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
1832 : | pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant); | ||
1833 : | if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) { | ||
1834 : | bSkip = 0; /* could not SKIP */ | ||
1835 : | if (pParam->vol_flags & XVID_VOL_QUARTERPEL) { | ||
1836 : | VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); | ||
1837 : | pMB->pmvs[0].x = - predMV.x; | ||
1838 : | pMB->pmvs[0].y = - predMV.y; | ||
1839 : | } else { | ||
1840 : | VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); | ||
1841 : | pMB->pmvs[0].x = - predMV.x; | ||
1842 : | pMB->pmvs[0].y = - predMV.y; | ||
1843 : | } | ||
1844 : | pMB->mode = MODE_INTER; | ||
1845 : | pMB->cbp = 0; | ||
1846 : | edgomez | 1.91 | break; |
1847 : | } | ||
1848 : | } | ||
1849 : | } | ||
1850 : | |||
1851 : | syskin | 1.116 | if (bSkip) { |
1852 : | /* do SKIP */ | ||
1853 : | pMB->mode = MODE_NOT_CODED; | ||
1854 : | MBSkip(bs); | ||
1855 : | stop_coding_timer(); | ||
1856 : | continue; /* next MB */ | ||
1857 : | chl | 1.68 | } |
1858 : | chl | 1.65 | } |
1859 : | edgomez | 1.102 | |
1860 : | syskin | 1.116 | /* ordinary case: normal coded INTER/INTER4V block */ |
1861 : | edgomez | 1.102 | MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1862 : | edgomez | 1.91 | stop_coding_timer(); |
1863 : | Isibaar | 1.1 | } |
1864 : | } | ||
1865 : | |||
1866 : | syskin | 1.126 | if (pEnc->num_threads > 0) { |
1867 : | void * status; | ||
1868 : | for (k = 0; k < pEnc->num_threads; k++) { | ||
1869 : | pthread_join(pEnc->motionData[k].handle, &status); | ||
1870 : | } | ||
1871 : | |||
1872 : | for (k = 0; k < pEnc->num_threads; k++) { | ||
1873 : | current->sStat.iMvSum += pEnc->motionData[k].mvSum; | ||
1874 : | current->sStat.iMvCount += pEnc->motionData[k].mvCount; | ||
1875 : | } | ||
1876 : | } | ||
1877 : | |||
1878 : | Isibaar | 1.1 | emms(); |
1879 : | syskin | 1.114 | updateFcode(¤t->sStat, pEnc); |
1880 : | edgomez | 1.91 | |
1881 : | /* frame drop code */ | ||
1882 : | edgomez | 1.102 | #if 0 |
1883 : | DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); | ||
1884 : | #endif | ||
1885 : | syskin | 1.103 | if (current->sStat.kblks + current->sStat.mblks <= |
1886 : | syskin | 1.105 | (pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1887 : | ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) | ||
1888 : | edgomez | 1.91 | { |
1889 : | Isibaar | 1.124 | current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0; |
1890 : | edgomez | 1.102 | current->sStat.ublks = mb_width * mb_height; |
1891 : | edgomez | 1.91 | |
1892 : | BitstreamReset(bs); | ||
1893 : | |||
1894 : | edgomez | 1.102 | set_timecodes(current,reference,pParam->fbase); |
1895 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant); | ||
1896 : | edgomez | 1.91 | |
1897 : | edgomez | 1.93 | /* copy reference frame details into the current frame */ |
1898 : | edgomez | 1.102 | current->quant = reference->quant; |
1899 : | current->motion_flags = reference->motion_flags; | ||
1900 : | current->rounding_type = reference->rounding_type; | ||
1901 : | current->fcode = reference->fcode; | ||
1902 : | current->bcode = reference->bcode; | ||
1903 : | syskin | 1.103 | current->stamp = reference->stamp; |
1904 : | edgomez | 1.102 | image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1905 : | memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); | ||
1906 : | syskin | 1.103 | coded = 0; |
1907 : | |||
1908 : | } else { | ||
1909 : | edgomez | 1.102 | |
1910 : | syskin | 1.103 | pEnc->current->is_edged = 0; /* not edged */ |
1911 : | pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ | ||
1912 : | edgomez | 1.102 | |
1913 : | syskin | 1.103 | /* what was this frame's interpolated reference will become |
1914 : | forward (past) reference in b-frame coding */ | ||
1915 : | edgomez | 1.102 | |
1916 : | syskin | 1.103 | image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1917 : | image_swap(&pEnc->vInterV, &pEnc->f_refv); | ||
1918 : | image_swap(&pEnc->vInterHV, &pEnc->f_refhv); | ||
1919 : | } | ||
1920 : | edgomez | 1.91 | |
1921 : | /* XXX: debug | ||
1922 : | { | ||
1923 : | char s[100]; | ||
1924 : | sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); | ||
1925 : | edgomez | 1.102 | image_dump_yuvpgm(¤t->image, |
1926 : | pParam->edged_width, | ||
1927 : | pParam->width, pParam->height, s); | ||
1928 : | |||
1929 : | edgomez | 1.91 | sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
1930 : | edgomez | 1.102 | image_dump_yuvpgm(&reference->image, |
1931 : | pParam->edged_width, | ||
1932 : | pParam->width, pParam->height, s); | ||
1933 : | syskin | 1.96 | } |
1934 : | edgomez | 1.91 | */ |
1935 : | |||
1936 : | edgomez | 1.102 | BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1937 : | suxen_drol | 1.60 | |
1938 : | edgomez | 1.102 | current->length = (BitstreamPos(bs) - bits) / 8; |
1939 : | Isibaar | 1.1 | |
1940 : | syskin | 1.103 | return coded; |
1941 : | edgomez | 1.91 | } |
1942 : | |||
1943 : | |||
1944 : | static void | ||
1945 : | FrameCodeB(Encoder * pEnc, | ||
1946 : | FRAMEINFO * frame, | ||
1947 : | edgomez | 1.102 | Bitstream * bs) |
1948 : | edgomez | 1.91 | { |
1949 : | edgomez | 1.102 | int bits = BitstreamPos(bs); |
1950 : | edgomez | 1.91 | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1951 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
1952 : | uint32_t x, y; | ||
1953 : | |||
1954 : | IMAGE *f_ref = &pEnc->reference->image; | ||
1955 : | IMAGE *b_ref = &pEnc->current->image; | ||
1956 : | |||
1957 : | edgomez | 1.102 | #ifdef BFRAMES_DEC_DEBUG |
1958 : | edgomez | 1.91 | FILE *fp; |
1959 : | static char first=0; | ||
1960 : | #define BFRAME_DEBUG if (!first && fp){ \ | ||
1961 : | fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ | ||
1962 : | } | ||
1963 : | |||
1964 : | if (!first){ | ||
1965 : | fp=fopen("C:\\XVIDDBGE.TXT","w"); | ||
1966 : | } | ||
1967 : | #endif | ||
1968 : | |||
1969 : | edgomez | 1.102 | /* forward */ |
1970 : | if (!pEnc->reference->is_edged) { | ||
1971 : | image_setedges(f_ref, pEnc->mbParam.edged_width, | ||
1972 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
1973 : | pEnc->mbParam.height, 0); | ||
1974 : | pEnc->current->is_edged = 1; | ||
1975 : | } | ||
1976 : | syskin | 1.96 | |
1977 : | edgomez | 1.102 | if (pEnc->reference->is_interpolated != 0) { |
1978 : | start_timer(); | ||
1979 : | syskin | 1.122 | image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y, |
1980 : | edgomez | 1.102 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1981 : | (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); | ||
1982 : | stop_inter_timer(); | ||
1983 : | pEnc->reference->is_interpolated = 0; | ||
1984 : | } | ||
1985 : | edgomez | 1.91 | |
1986 : | edgomez | 1.93 | /* backward */ |
1987 : | edgomez | 1.102 | if (!pEnc->current->is_edged) { |
1988 : | image_setedges(b_ref, pEnc->mbParam.edged_width, | ||
1989 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
1990 : | pEnc->mbParam.height, 0); | ||
1991 : | pEnc->current->is_edged = 1; | ||
1992 : | } | ||
1993 : | |||
1994 : | if (pEnc->current->is_interpolated != 0) { | ||
1995 : | start_timer(); | ||
1996 : | syskin | 1.122 | image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y, |
1997 : | edgomez | 1.102 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1998 : | (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); | ||
1999 : | stop_inter_timer(); | ||
2000 : | pEnc->current->is_interpolated = 0; | ||
2001 : | } | ||
2002 : | |||
2003 : | frame->coding_type = B_VOP; | ||
2004 : | syskin | 1.121 | call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
2005 : | edgomez | 1.91 | |
2006 : | syskin | 1.126 | frame->fcode = frame->bcode = pEnc->current->fcode; |
2007 : | |||
2008 : | if (pEnc->num_threads > 0) { | ||
2009 : | int k; | ||
2010 : | /* multithreaded motion estimation - dispatch threads */ | ||
2011 : | int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads; | ||
2012 : | |||
2013 : | for (k = 0; k < pEnc->num_threads; k++) { | ||
2014 : | memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int)); | ||
2015 : | pEnc->motionData[k].pParam = &pEnc->mbParam; | ||
2016 : | pEnc->motionData[k].current = frame; | ||
2017 : | pEnc->motionData[k].reference = pEnc->current; | ||
2018 : | pEnc->motionData[k].fRef = f_ref; | ||
2019 : | pEnc->motionData[k].fRefH = &pEnc->f_refh; | ||
2020 : | pEnc->motionData[k].fRefV = &pEnc->f_refv; | ||
2021 : | pEnc->motionData[k].fRefHV = &pEnc->f_refhv; | ||
2022 : | pEnc->motionData[k].pRef = b_ref; | ||
2023 : | pEnc->motionData[k].pRefH = &pEnc->vInterH; | ||
2024 : | pEnc->motionData[k].pRefV = &pEnc->vInterV; | ||
2025 : | pEnc->motionData[k].pRefHV = &pEnc->vInterHV; | ||
2026 : | pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp); | ||
2027 : | pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp); | ||
2028 : | pEnc->motionData[k].y_step = pEnc->num_threads; | ||
2029 : | pEnc->motionData[k].start_y = k; | ||
2030 : | /* todo: sort out temp space once and for all */ | ||
2031 : | pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width; | ||
2032 : | } | ||
2033 : | |||
2034 : | for (k = 0; k < pEnc->num_threads; k++) { | ||
2035 : | pthread_create(&pEnc->motionData[k].handle, NULL, | ||
2036 : | (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]); | ||
2037 : | } | ||
2038 : | } else { | ||
2039 : | start_timer(); | ||
2040 : | MotionEstimationBVOP(&pEnc->mbParam, frame, | ||
2041 : | ((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ | ||
2042 : | ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ | ||
2043 : | pEnc->reference->mbs, f_ref, | ||
2044 : | &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
2045 : | pEnc->current, b_ref, &pEnc->vInterH, | ||
2046 : | &pEnc->vInterV, &pEnc->vInterHV); | ||
2047 : | stop_motion_timer(); | ||
2048 : | } | ||
2049 : | edgomez | 1.91 | |
2050 : | set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); | ||
2051 : | edgomez | 1.102 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
2052 : | edgomez | 1.91 | |
2053 : | frame->sStat.iTextBits = 0; | ||
2054 : | Isibaar | 1.123 | frame->sStat.iMVBits = 0; |
2055 : | edgomez | 1.91 | frame->sStat.iMvSum = 0; |
2056 : | frame->sStat.iMvCount = 0; | ||
2057 : | frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; | ||
2058 : | edgomez | 1.102 | frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
2059 : | frame->sStat.kblks = frame->sStat.ublks = 0; | ||
2060 : | edgomez | 1.91 | |
2061 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) { | ||
2062 : | syskin | 1.126 | |
2063 : | if (pEnc->num_threads > 0) { | ||
2064 : | /* in multithreaded encoding, only proceed with a row of macroblocks | ||
2065 : | if ME finished with that row */ | ||
2066 : | while (pEnc->motionData[y%pEnc->num_threads].complete_count_self[y/pEnc->num_threads] != pEnc->mbParam.mb_width) | ||
2067 : | sched_yield(); | ||
2068 : | } | ||
2069 : | |||
2070 : | edgomez | 1.91 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
2071 : | MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; | ||
2072 : | |||
2073 : | edgomez | 1.93 | /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
2074 : | edgomez | 1.91 | if (mb->mode == MODE_NOT_CODED) { |
2075 : | edgomez | 1.102 | if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
2076 : | MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, | ||
2077 : | syskin | 1.109 | NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
2078 : | edgomez | 1.102 | } |
2079 : | edgomez | 1.91 | continue; |
2080 : | } | ||
2081 : | |||
2082 : | syskin | 1.112 | mb->quant = frame->quant; |
2083 : | |||
2084 : | if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { | ||
2085 : | /* we have to motion-compensate, transfer etc, | ||
2086 : | because there might be blocks to code */ | ||
2087 : | |||
2088 : | edgomez | 1.91 | MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
2089 : | syskin | 1.112 | f_ref, &pEnc->f_refh, &pEnc->f_refv, |
2090 : | &pEnc->f_refhv, b_ref, &pEnc->vInterH, | ||
2091 : | &pEnc->vInterV, &pEnc->vInterHV, | ||
2092 : | dct_codes); | ||
2093 : | |||
2094 : | mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); | ||
2095 : | edgomez | 1.91 | } |
2096 : | syskin | 1.112 | |
2097 : | if (mb->mode == MODE_DIRECT_NO4V) | ||
2098 : | mb->mode = MODE_DIRECT; | ||
2099 : | |||
2100 : | if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) | ||
2101 : | mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ | ||
2102 : | syskin | 1.115 | else |
2103 : | if (frame->vop_flags & XVID_VOP_GREYSCALE) | ||
2104 : | /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */ | ||
2105 : | mb->cbp &= 0x3C; | ||
2106 : | edgomez | 1.102 | |
2107 : | edgomez | 1.91 | start_timer(); |
2108 : | edgomez | 1.102 | MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, |
2109 : | &frame->sStat); | ||
2110 : | edgomez | 1.91 | stop_coding_timer(); |
2111 : | } | ||
2112 : | } | ||
2113 : | |||
2114 : | syskin | 1.126 | if (pEnc->num_threads > 0) { |
2115 : | void * status; | ||
2116 : | int k; | ||
2117 : | for (k = 0; k < pEnc->num_threads; k++) { | ||
2118 : | pthread_join(pEnc->motionData[k].handle, &status); | ||
2119 : | } | ||
2120 : | } | ||
2121 : | |||
2122 : | edgomez | 1.91 | emms(); |
2123 : | |||
2124 : | edgomez | 1.102 | BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
2125 : | frame->length = (BitstreamPos(bs) - bits) / 8; | ||
2126 : | edgomez | 1.91 | |
2127 : | #ifdef BFRAMES_DEC_DEBUG | ||
2128 : | if (!first){ | ||
2129 : | first=1; | ||
2130 : | if (fp) | ||
2131 : | fclose(fp); | ||
2132 : | } | ||
2133 : | #endif | ||
2134 : | suxen_drol | 1.24 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |