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