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