~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/ADM_codecs/ADM_xvid4.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-12-15 17:13:20 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215171320-w79pvpehxx2fr217
Tags: 1:2.3.0-0.0ubuntu1
* Merge from debian-multimedia.org, remaining Ubuntu change:
  - desktop file,
  - no support for ccache and make -j.
* Closes Ubuntu: #69614.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include "ADM_codecs/ADM_xvid4.h"
33
33
#include "xvid.h"
34
34
#include <ADM_assert.h>
35
 
 
36
 
 
37
 
static const int motion_presets[] = {   
38
 
                        0,
39
 
                        0,
40
 
                        0,
41
 
                        0,
42
 
                        XVID_ME_HALFPELREFINE16,
43
 
                        XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND16,
44
 
                        XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 |
45
 
                        XVID_ME_HALFPELREFINE8  | XVID_ME_USESQUARES16
 
35
#include "ADM_toolkit/ADM_cpuCap.h"
 
36
 
 
37
#include "ADM_editor/ADM_edit.hxx"
 
38
extern ADM_Composer *video_body;
 
39
 
 
40
static const int motion_presets[] = {
 
41
  0,
 
42
  0,
 
43
  0,
 
44
  0,
 
45
  XVID_ME_HALFPELREFINE16,
 
46
  XVID_ME_HALFPELREFINE16 | XVID_ME_ADVANCEDDIAMOND16,
 
47
  XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 |
 
48
    XVID_ME_HALFPELREFINE8 | XVID_ME_USESQUARES16
46
49
};
47
50
 
48
51
#define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0]))
50
53
 
51
54
#define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0]))
52
55
 
53
 
   
54
56
 
55
 
static myENC_RESULT xvid_res;
56
 
static  xvid_gbl_init_t xvid_gbl_init2;
57
 
static  xvid_enc_create_t xvid_enc_create;
58
 
static  xvid_plugin_single_t single;
59
 
static  xvid_plugin_2pass1_t pass1;
60
 
static  xvid_plugin_2pass2_t pass2;
61
 
static  xvid_enc_plugin_t plugins[7];
62
 
static  xvid_enc_frame_t xvid_enc_frame;
63
 
static  xvid_enc_stats_t xvid_enc_stats;
64
 
static  xvid_gbl_info_t xvid_gbl_info;
 
57
static xvid_gbl_init_t xvid_gbl_init2;
 
58
static xvid_enc_create_t xvid_enc_create;
 
59
static xvid_plugin_single_t single;
 
60
static xvid_plugin_2pass1_t pass1;
 
61
static xvid_plugin_2pass2_t pass2;
 
62
static xvid_enc_plugin_t plugins[7];
 
63
static xvid_enc_frame_t xvid_enc_frame;
 
64
static xvid_enc_stats_t xvid_enc_stats;
 
65
static xvid_gbl_info_t xvid_gbl_info;
 
66
static uint32_t xvid_framenum;
65
67
#define MMSET(x) memset(&x,0,sizeof(x))
66
68
 
67
 
void xvid4_init(void);
 
69
/******/
 
70
static int
 
71
adm_hook (void *handle, int opt, void *param1, void *param2)
 
72
{
 
73
  xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
 
74
  // printf("Pass %d value %d\n",opt,data->frame_num);
 
75
  if (opt == XVID_PLG_FRAME)
 
76
    {
 
77
 
 
78
      xvid_framenum = data->frame_num;
 
79
      //printf("Before called with %u\n",xvid_framenum);
 
80
    }
 
81
 
 
82
 
 
83
  return 0;
 
84
}
 
85
uint8_t xvid4Encoder::encode(ADMImage * in,ADMBitstream * out)
 
86
{
 
87
    int  ret=0;
 
88
    /* Encode the frame */
 
89
    xvid_enc_frame.bitstream = out->data;
 
90
    preAmble (in->data);
 
91
    ret = xvid_encore (_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
 
92
                       &xvid_enc_stats);
 
93
    if (ret < 0)
 
94
    {
 
95
        printf ("Error calling xvid4 %d\n", ret);
 
96
        return 0;
 
97
 
 
98
    }
 
99
    postAmble (out);
 
100
 
 
101
    out->len = ret;
 
102
    if (!ret)
 
103
        printf ("Skipped\n");
 
104
    return 1;
 
105
 
 
106
}
 
107
 
 
108
 
 
109
void xvid4_init (void);
68
110
/*
69
111
        System wide init, do it once for all 
70
112
*/
71
 
void xvid4_init(void)
 
113
void
 
114
xvid4_init (void)
72
115
{
73
 
        MMSET(xvid_gbl_init2);
74
 
        MMSET(xvid_gbl_info);
75
 
        
76
 
        printf("Initializing global xvid 4\n");
77
 
        xvid_gbl_init2.version = XVID_VERSION;
78
 
        xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init2, NULL);
79
 
        xvid_gbl_info.version = XVID_VERSION;
80
 
        xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL);
81
 
        if(xvid_gbl_info.build)
82
 
                {
83
 
                        printf("\txvid build:%s\n",xvid_gbl_info.build);
84
 
                }
85
 
        printf("\txvid thread:%d\n",xvid_gbl_info.num_threads);
86
 
        printf("\txvid SIMD supported:(%x)\n",xvid_gbl_info.cpu_flags);
87
 
        #define CPUF(x) if(xvid_gbl_info.cpu_flags  & XVID_CPU_##x) printf("\t\t"#x"\n");       
 
116
  MMSET (xvid_gbl_init2);
 
117
  MMSET (xvid_gbl_info);
 
118
 
 
119
  printf ("Initializing global xvid 4\n");
 
120
  xvid_gbl_init2.version = XVID_VERSION;
 
121
  xvid_global (NULL, XVID_GBL_INIT, &xvid_gbl_init2, NULL);
 
122
  xvid_gbl_info.version = XVID_VERSION;
 
123
  xvid_global (NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL);
 
124
  if (xvid_gbl_info.build)
 
125
    {
 
126
      printf ("\txvid build:%s\n", xvid_gbl_info.build);
 
127
    }
 
128
  printf ("\txvid thread:%d\n", xvid_gbl_info.num_threads);
 
129
  printf ("\txvid SIMD supported:(%x)\n", xvid_gbl_info.cpu_flags);
 
130
#define CPUF(x) if(xvid_gbl_info.cpu_flags  & XVID_CPU_##x) printf("\t\t"#x"\n");
88
131
#if defined( ARCH_X86)  || defined(ARCH_X86_64)
89
 
        CPUF(MMX);
90
 
        CPUF(MMXEXT);
91
 
        CPUF(SSE);
92
 
        CPUF(SSE2);
93
 
        CPUF(3DNOW);
94
 
        CPUF(3DNOWEXT);
95
 
#endif  
96
 
#ifdef USE_ALTIVEC      
97
 
        CPUF(ALTIVEC);
98
 
#endif  
99
 
        
 
132
  CPUF (MMX);
 
133
  CPUF (MMXEXT);
 
134
  CPUF (SSE);
 
135
  CPUF (SSE2);
 
136
  CPUF (3DNOW);
 
137
  CPUF (3DNOWEXT);
 
138
#endif
 
139
#ifdef USE_ALTIVEC
 
140
  CPUF (ALTIVEC);
 
141
#endif
 
142
 
100
143
}
101
 
xvid4Encoder::~xvid4Encoder()
 
144
xvid4Encoder::~xvid4Encoder ()
102
145
{
103
 
        stopEncoder();
 
146
  stopEncoder ();
104
147
}
105
148
//--------------------------------------------------
106
149
 
107
 
uint8_t xvid4Encoder::init(uint32_t q,uint32_t fps1000,xvid4EncParam *param)
108
 
{
109
 
        return 0;
110
 
}
111
 
 
112
 
 
113
 
uint8_t         xvid4Encoder::getResult( void *ress)
114
 
{
115
 
                myENC_RESULT *res;
116
 
 
117
 
                res=(myENC_RESULT *)ress;
118
 
                memcpy(res,&xvid_res,sizeof(myENC_RESULT));
119
 
                return 1;
120
 
 
121
 
}
122
 
void     xvid4Encoder::createUpdate(void )
123
 
{
124
 
                xvid_enc_create.max_bframes=_param.bframes;
125
 
                xvid_enc_create.max_key_interval=_param.max_key_interval;               
126
 
                xvid_enc_create.bquant_ratio =  _param.bquant_ratio;
127
 
                xvid_enc_create.bquant_offset =         _param.bquant_offset;
128
 
                
129
 
                if (_param.packed)
130
 
                        xvid_enc_create.global |= XVID_GLOBAL_PACKED;
131
 
                
132
 
                if (_param.closed_gop)
133
 
                        xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
134
 
                
135
 
                for(uint32_t i=0;i<3;i++)
136
 
                {
137
 
                        xvid_enc_create.min_quant[i]=_param.qmin[i];
138
 
                        xvid_enc_create.max_quant[i]=_param.qmax[i];
139
 
                }
140
 
 
141
 
                preAmble(NULL);
142
 
#define C_VOP(x) if(xvid_enc_frame.vop_flags& XVID_VOP_##x) printf("Vop :"#x" is set\n");               
 
150
uint8_t
 
151
  xvid4Encoder::init (uint32_t q, uint32_t fps1000, xvid4EncParam * param)
 
152
{
 
153
  return 0;
 
154
}
 
155
 
 
156
 
 
157
void
 
158
xvid4Encoder::createUpdate (void)
 
159
{
 
160
  uint32_t nbThread = 0;
 
161
  xvid_enc_create.num_threads = 0;
 
162
  if (nbThread = ADM_useNbThreads ())
 
163
    {
 
164
      printf ("Xvid4 : using %u threads..\n", nbThread);
 
165
      xvid_enc_create.num_threads = nbThread;
 
166
    }
 
167
  xvid_enc_create.max_bframes = _param.bframes;
 
168
  xvid_enc_create.max_key_interval = _param.max_key_interval;
 
169
  xvid_enc_create.bquant_ratio = _param.bquant_ratio;
 
170
  xvid_enc_create.bquant_offset = _param.bquant_offset;
 
171
 
 
172
  if (_param.packed)
 
173
    xvid_enc_create.global |=XVID_GLOBAL_PACKED;
 
174
 
 
175
  if (_param.closed_gop)
 
176
    xvid_enc_create.global |=XVID_GLOBAL_CLOSED_GOP;
 
177
 
 
178
  for (uint32_t i = 0; i < 3; i++)
 
179
    {
 
180
      xvid_enc_create.min_quant[i] = _param.qmin[i];
 
181
      xvid_enc_create.max_quant[i] = _param.qmax[i];
 
182
    }
 
183
 
 
184
  preAmble (NULL);
 
185
#define C_VOP(x) if(xvid_enc_frame.vop_flags& XVID_VOP_##x) printf("Vop :"#x" is set\n");
143
186
#define C_VOL(x) if(xvid_enc_frame.vol_flags& XVID_VOL_##x) printf("Vol :"#x" is set\n");
144
187
#define C_ME(x) if(xvid_enc_frame.motion& XVID_ME_##x) printf("ME :"#x" is set\n");
145
188
 
146
 
        C_VOP(DEBUG);
147
 
        C_VOP(CHROMAOPT);
148
 
        C_VOP(HALFPEL);
149
 
        C_VOP(INTER4V);
150
 
        C_VOP(TRELLISQUANT);
151
 
        C_VOP(CHROMAOPT);
152
 
        C_VOP(CARTOON);
153
 
        C_VOP(GREYSCALE);
154
 
        C_VOP(HQACPRED);
155
 
        C_VOP(MODEDECISION_RD);
156
 
        
157
 
        C_VOL(MPEGQUANT);
158
 
        C_VOL(EXTRASTATS);
159
 
        C_VOL(GMC);
160
 
        C_VOL(QUARTERPEL);
161
 
        C_VOL(INTERLACING);
162
 
        
163
 
        C_ME(ADVANCEDDIAMOND16);
164
 
        C_ME(ADVANCEDDIAMOND8);
165
 
        
166
 
        C_ME(USESQUARES16);
167
 
        C_ME(USESQUARES8);
168
 
        
169
 
        C_ME(HALFPELREFINE16);
170
 
        C_ME(HALFPELREFINE8);
171
 
        
172
 
        // Turbo Mode
173
 
        C_ME(FASTREFINE16);
174
 
        C_ME(FASTREFINE8);
175
 
        C_ME(SKIP_DELTASEARCH);
176
 
        C_ME(FAST_MODEINTERPOLATE);
177
 
        C_ME(BFRAME_EARLYSTOP);
178
 
        
179
 
        C_ME(GME_REFINE);
180
 
        C_ME(QUARTERPELREFINE8);
181
 
        C_ME(QUARTERPELREFINE16);
182
 
        C_ME(EXTSEARCH16);
183
 
        C_ME(EXTSEARCH8);
184
 
        C_ME(CHROMA_BVOP);
185
 
        C_ME(CHROMA_PVOP);
186
 
        
187
 
        
188
 
                
189
 
}
190
 
void     xvid4Encoder::encUpdate(void )
191
 
{
192
 
                
193
 
 
194
 
}
195
 
 
196
 
void xvid4Encoder::dump(void)
197
 
{
198
 
        printf("Xvid4 codec configuration:\n");
199
 
        if(_param.mpegQuantizer)
200
 
                printf("\tMPEGQUANT\n");
201
 
        if(_param.interlaced)
202
 
                printf("\tINTERLACED\n");
203
 
                
 
189
  C_VOP (DEBUG);
 
190
  C_VOP (CHROMAOPT);
 
191
  C_VOP (HALFPEL);
 
192
  C_VOP (INTER4V);
 
193
  C_VOP (TRELLISQUANT);
 
194
  C_VOP (CHROMAOPT);
 
195
  C_VOP (CARTOON);
 
196
  C_VOP (GREYSCALE);
 
197
  C_VOP (HQACPRED);
 
198
  C_VOP (MODEDECISION_RD);
 
199
 
 
200
  C_VOL (MPEGQUANT);
 
201
  C_VOL (EXTRASTATS);
 
202
  C_VOL (GMC);
 
203
  C_VOL (QUARTERPEL);
 
204
  C_VOL (INTERLACING);
 
205
 
 
206
  C_ME (ADVANCEDDIAMOND16);
 
207
  C_ME (ADVANCEDDIAMOND8);
 
208
 
 
209
  C_ME (USESQUARES16);
 
210
  C_ME (USESQUARES8);
 
211
 
 
212
  C_ME (HALFPELREFINE16);
 
213
  C_ME (HALFPELREFINE8);
 
214
 
 
215
  // Turbo Mode
 
216
  C_ME (FASTREFINE16);
 
217
  C_ME (FASTREFINE8);
 
218
  C_ME (SKIP_DELTASEARCH);
 
219
  C_ME (FAST_MODEINTERPOLATE);
 
220
  C_ME (BFRAME_EARLYSTOP);
 
221
 
 
222
  C_ME (GME_REFINE);
 
223
  C_ME (QUARTERPELREFINE8);
 
224
  C_ME (QUARTERPELREFINE16);
 
225
  C_ME (EXTSEARCH16);
 
226
  C_ME (EXTSEARCH8);
 
227
  C_ME (CHROMA_BVOP);
 
228
  C_ME (CHROMA_PVOP);
 
229
  MMSET (xvid_enc_frame);
 
230
 
 
231
 
 
232
}
 
233
 
 
234
void
 
235
xvid4Encoder::dump (void)
 
236
{
 
237
  printf ("Xvid4 codec configuration:\n");
 
238
  if (_param.mpegQuantizer)
 
239
    printf ("\tMPEGQUANT\n");
 
240
  if (_param.interlaced)
 
241
    printf ("\tINTERLACED\n");
 
242
 
204
243
 
205
244
#define ST(x,y) \
206
 
        if(_param.x) printf("\t"#y"\n");                                
207
 
        
208
 
        
209
 
        ST(  inter4mv,  INTER4V);
210
 
        ST(  trellis,   TRELLISQUANT);
211
 
        ST(  cartoon,   CARTOON);
212
 
        ST(  greyscale, GREYSCALE);
 
245
        if(_param.x) printf("\t"#y"\n");
 
246
 
 
247
 
 
248
  ST (inter4mv, INTER4V);
 
249
  ST (trellis, TRELLISQUANT);
 
250
  ST (cartoon, CARTOON);
 
251
  ST (greyscale, GREYSCALE);
213
252
#undef ST
214
253
 
215
254
}
217
256
//
218
257
//  Initialize the compressor
219
258
//
220
 
uint8_t     xvid4Encoder::stopEncoder(void )
 
259
uint8_t
 
260
xvid4Encoder::stopEncoder (void)
221
261
{
222
 
        if(_handle)
223
 
        {
224
 
                xvid_encore(_handle, XVID_ENC_DESTROY, NULL, NULL);
225
 
                printf("Destroying xvid codec\n");
226
 
                _handle=NULL;
227
 
        }
228
 
    return 1;
 
262
  if (_handle)
 
263
    {
 
264
      xvid_encore (_handle, XVID_ENC_DESTROY, NULL, NULL);
 
265
      printf ("Destroying xvid codec\n");
 
266
      _handle = NULL;
 
267
    }
 
268
  return 1;
229
269
}
230
 
uint8_t     xvid4Encoder::preAmble(uint8_t *in )
 
270
uint8_t
 
271
xvid4Encoder::preAmble (uint8_t * in)
231
272
{
232
273
/* Version for the frame and the stats */
233
 
        MMSET(xvid_enc_frame);
234
 
        MMSET(xvid_enc_stats);
235
 
        
236
 
        xvid_enc_frame.version = XVID_VERSION;  
237
 
        xvid_enc_stats.version = XVID_VERSION;
238
 
 
239
 
        /* Bind output buffer */
240
 
 
241
 
        xvid_enc_frame.length = 0;
242
 
        if(_param.mpegQuantizer)
243
 
                xvid_enc_frame.vol_flags|=XVID_VOL_MPEGQUANT;
244
 
        if(_param.interlaced)
245
 
                xvid_enc_frame.vol_flags|=XVID_VOL_INTERLACING;
246
 
                
247
 
        
 
274
  //MMSET (xvid_enc_frame);
 
275
  MMSET (xvid_enc_stats);
 
276
 
 
277
  xvid_enc_frame.version = XVID_VERSION;
 
278
  xvid_enc_stats.version = XVID_VERSION;
 
279
 
 
280
  /* Bind output buffer */
 
281
 
 
282
  xvid_enc_frame.length = 0;
 
283
  if (_param.mpegQuantizer)
 
284
    xvid_enc_frame.vol_flags |= XVID_VOL_MPEGQUANT;
 
285
  if (_param.interlaced)
 
286
    xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING;
 
287
 
 
288
 
248
289
#define SVOP(x,y) if(_param.x) xvid_enc_frame.vop_flags|=XVID_VOP_##y
249
 
                                        
250
 
        
251
 
        xvid_enc_frame.motion = motion_presets[_param.guiLevel];
252
 
        
253
 
        SVOP(  inter4mv ,INTER4V);
254
 
        SVOP(  trellis  ,TRELLISQUANT);
255
 
        SVOP(  hqac     ,HQACPRED);
256
 
        SVOP(  greyscale,GREYSCALE);
257
 
        SVOP(  cartoon  ,CARTOON);
258
 
        SVOP(  chroma_opt,CHROMAOPT);
259
 
        //SVOP(  1      ,HALFPEL);
260
 
        xvid_enc_frame.vop_flags|=XVID_VOP_HALFPEL;     
261
 
        // ME 
262
 
        if(_param.chroma_me)
263
 
                {
264
 
                        xvid_enc_frame.motion|=XVID_ME_CHROMA_BVOP;     
265
 
                        xvid_enc_frame.motion|=XVID_ME_CHROMA_PVOP;
266
 
                }
267
 
        if(_param.cartoon) 
268
 
        {
269
 
                        xvid_enc_frame.motion|=XVID_ME_DETECT_STATIC_MOTION;
270
 
        }
271
 
        //printf("Vhq mode : %d\n",_param.vhqmode);
 
290
 
 
291
 
 
292
  xvid_enc_frame.motion = motion_presets[_param.guiLevel];
 
293
 
 
294
  SVOP (inter4mv, INTER4V);
 
295
  SVOP (trellis, TRELLISQUANT);
 
296
  SVOP (hqac, HQACPRED);
 
297
#if  XVID_API==XVID_MAKE_API(4, 1)
 
298
  SVOP (bvhq, RD_BVOP);
 
299
#endif
 
300
 
 
301
  SVOP (greyscale, GREYSCALE);
 
302
  SVOP (cartoon, CARTOON);
 
303
  SVOP (chroma_opt, CHROMAOPT);
 
304
  //SVOP(  1      ,HALFPEL);
 
305
  xvid_enc_frame.vop_flags |= XVID_VOP_HALFPEL;
 
306
  // ME 
 
307
  if (_param.chroma_me)
 
308
    {
 
309
      xvid_enc_frame.motion |= XVID_ME_CHROMA_BVOP;
 
310
      xvid_enc_frame.motion |= XVID_ME_CHROMA_PVOP;
 
311
    }
 
312
  if (_param.cartoon)
 
313
    {
 
314
      xvid_enc_frame.motion |= XVID_ME_DETECT_STATIC_MOTION;
 
315
    }
 
316
  //printf("Vhq mode : %d\n",_param.vhqmode);
272
317
#define VHME(x) xvid_enc_frame.motion|= XVID_ME_##x
273
 
        switch(_param.vhqmode)
274
 
        {
275
 
                case 4: VHME(EXTSEARCH_RD);
276
 
                case 3: VHME(HALFPELREFINE8_RD);
277
 
                        VHME(QUARTERPELREFINE8_RD);
278
 
                        VHME(CHECKPREDICTION_RD);
279
 
                
280
 
                case 2:
281
 
                        VHME(HALFPELREFINE16_RD);
282
 
                        VHME(QUARTERPELREFINE16_RD);
283
 
                case 1:
284
 
                        SVOP(vhqmode,MODEDECISION_RD);                  
285
 
                case 0:
286
 
                        break;
287
 
                default:
288
 
                        ADM_assert(0);
289
 
        }       
290
 
        if(_param.qpel)
291
 
                {
292
 
                        xvid_enc_frame.vol_flags|=XVID_VOL_QUARTERPEL;                  
293
 
                        
294
 
                        xvid_enc_frame.motion    |= XVID_ME_QUARTERPELREFINE16;
295
 
                        xvid_enc_frame.motion    |= XVID_ME_QUARTERPELREFINE8;  
296
 
                }
297
 
                        
298
 
        if (_param.gmc)
299
 
                {
300
 
                        xvid_enc_frame.vol_flags |= XVID_VOL_GMC;
301
 
                        xvid_enc_frame.motion    |= XVID_ME_GME_REFINE;
302
 
                }
303
 
                                                                        
304
 
        if (_param.turbo)
305
 
                {
306
 
                        xvid_enc_frame.motion    |= XVID_ME_FASTREFINE16;
307
 
                        xvid_enc_frame.motion    |= XVID_ME_FASTREFINE8;
308
 
                        xvid_enc_frame.motion    |= XVID_ME_SKIP_DELTASEARCH;
309
 
                        xvid_enc_frame.motion    |= XVID_ME_FAST_MODEINTERPOLATE;
310
 
                        xvid_enc_frame.motion    |= XVID_ME_BFRAME_EARLYSTOP;
311
 
                }
312
 
                
313
 
        xvid_enc_frame.bframe_threshold =       _param.bframe_threshold;
314
 
                
315
 
        xvid_enc_frame.input.csp = XVID_CSP_YV12;
316
 
        xvid_enc_frame.input.stride[0] = _w;
317
 
        xvid_enc_frame.input.stride[1] = _w>>1;
318
 
        xvid_enc_frame.input.stride[2] = _w>>1;
319
 
        xvid_enc_frame.type = XVID_TYPE_AUTO;
320
 
        
321
 
        
322
 
        /* Set up motion estimation flags */
323
 
        xvid_enc_frame.input.plane[0] = in;
324
 
        xvid_enc_frame.input.plane[1] = in+(_w*_h);
325
 
        xvid_enc_frame.input.plane[2] = in+((_w*_h*5)>>2);
326
 
        return 1;
327
 
}       
328
 
 
329
 
uint8_t     xvid4Encoder::postAmble(uint32_t *flags )
330
 
{
331
 
        xvid_res.is_key_frame=*flags=0;
332
 
        
333
 
        if(xvid_enc_frame.out_flags & XVID_KEYFRAME) 
334
 
        {
335
 
                *flags=AVI_KEY_FRAME;
336
 
                xvid_res.is_key_frame=1;
337
 
        }
338
 
        else
339
 
        if(xvid_enc_stats.type == XVID_TYPE_BVOP)
340
 
        {
341
 
                *flags=AVI_B_FRAME;
342
 
        
343
 
        }
344
 
        xvid_res.out_quantizer=xvid_enc_stats.quant;
345
 
        return 1;
346
 
}
347
 
 
348
 
// *************************************************
349
 
// *************************************************
350
 
//                                                                      CQ
351
 
// *************************************************
352
 
// *************************************************
353
 
uint8_t xvid4EncoderCQ::init(uint32_t q,uint32_t fps1000, xvid4EncParam *param)
354
 
{
355
 
    int xerr;
356
 
        printf("Using Xvid 4 codec with CQ mode = %lu (%dx%d)\n",q,_w,_h);
357
 
        _q=q;
358
 
        _fps1000=fps1000;       
359
 
        memcpy(&_param,param,sizeof(_param));
360
 
        dump();
361
 
        // Here we go...
362
 
        MMSET(xvid_enc_create);
363
 
        xvid_enc_create.version = XVID_VERSION;
364
 
        xvid_enc_create.width = _w;
365
 
        xvid_enc_create.height = _h;
366
 
        MMSET(single);
367
 
        
368
 
 
369
 
        plugins[0].func = xvid_plugin_single;
370
 
        plugins[0].param = &single;
371
 
        
372
 
        single.version = XVID_VERSION;
373
 
        single.bitrate = 1500;
374
 
 
375
 
        xvid_enc_create.plugins = plugins;
376
 
        xvid_enc_create.num_plugins = 1;
377
 
        
378
 
        //Framerate
379
 
        xvid_enc_create.fincr = 1000;
380
 
        xvid_enc_create.fbase = _fps1000;
381
 
        //
382
 
        createUpdate();
383
 
        xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
384
 
        if(xerr<0)
385
 
        {
386
 
                printf("Xvid-4 init error :%d\n",xerr);
387
 
                return 0;
388
 
        
389
 
        }
390
 
        
391
 
        _handle = xvid_enc_create.handle;
392
 
 
393
 
        printf("Xvid-4 CQ init Ok\n");
394
 
 
395
 
        return 1;
396
 
}
397
 
 
398
 
 
399
 
uint8_t xvid4EncoderCQ::encode(
400
 
                        ADMImage * in,
401
 
                        uint8_t * out,
402
 
                        uint32_t * len,
403
 
                        uint32_t * flags)
404
 
{
405
 
int ret;
406
 
 
407
 
        
408
 
        
409
 
        preAmble(in->data);
410
 
        
411
 
        xvid_enc_frame.quant = _q;
412
 
        xvid_enc_frame.bitstream = out; 
413
 
        
414
 
        /* Encode the frame */
415
 
        ret = xvid_encore(_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
416
 
                                          &xvid_enc_stats);
417
 
        if(ret<0)
418
 
                {
419
 
                        printf("Error calling xvid4 %d\n",ret);
420
 
                        return 0;
421
 
                        
422
 
                }
423
 
        postAmble(flags);
424
 
        
425
 
        *len=xvid_res.total_bits=ret;
426
 
        xvid_res.total_bits*=8;
427
 
        
428
 
        return 1;
429
 
 
430
 
}
431
 
// *************************************************
432
 
// *************************************************
433
 
//              CQ but Q is controlled externally
434
 
// *************************************************
435
 
// *************************************************
436
 
 
437
 
uint8_t xvid4EncoderVBRExternal::init(uint32_t q,uint32_t fps1000, xvid4EncParam *param)
438
 
{
439
 
    int xerr;
440
 
        printf("Using Xvid 4 codec with CQ mode = %lu (%dx%d)\n",q,_w,_h);
441
 
        _q=q;
442
 
        _fps1000=fps1000;       
443
 
        memcpy(&_param,param,sizeof(_param));
444
 
        dump();
445
 
        // Here we go...
446
 
        MMSET(xvid_enc_create);
447
 
        xvid_enc_create.version = XVID_VERSION;
448
 
        xvid_enc_create.width = _w;
449
 
        xvid_enc_create.height = _h;
450
 
        MMSET(single);
451
 
        
452
 
 
453
 
        plugins[0].func = xvid_plugin_single;
454
 
        plugins[0].param = &single;
455
 
        
456
 
        single.version = XVID_VERSION;
457
 
        single.bitrate = 1500;
458
 
 
459
 
        xvid_enc_create.plugins = plugins;
460
 
        xvid_enc_create.num_plugins = 1;
461
 
        
462
 
        //Framerate
463
 
        xvid_enc_create.fincr = 1000;
464
 
        xvid_enc_create.fbase = _fps1000;
465
 
        //
466
 
        createUpdate();
467
 
        xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
468
 
        if(xerr<0)
469
 
        {
470
 
                printf("Xvid-4 init error :%d\n",xerr);
471
 
                return 0;
472
 
        
473
 
        }
474
 
        
475
 
        _handle = xvid_enc_create.handle;
476
 
 
477
 
        printf("Xvid-4 CQ init Ok\n");
478
 
 
479
 
        return 1;
480
 
}
481
 
 
482
 
 
483
 
uint8_t xvid4EncoderVBRExternal::encode(
484
 
                        ADMImage * in,
485
 
                        uint8_t * out,
486
 
                        uint32_t * len,
487
 
                        uint32_t * flags)
488
 
{
489
 
int q;
490
 
        q=(*flags)>>16;
491
 
        *flags&=0xFFFF;
492
 
        return encodeVBR(in,out,len,q,flags);
493
 
}
494
 
 
495
 
uint8_t xvid4EncoderVBRExternal::encodeVBR(
496
 
                        ADMImage * in,
497
 
                        uint8_t * out,
498
 
                        uint32_t * len,
499
 
                        uint32_t q,
500
 
                        uint32_t * flags)
501
 
{       
502
 
int  ret;
503
 
        preAmble(in->data);
504
 
        
505
 
        xvid_enc_frame.quant = q;
506
 
        xvid_enc_frame.bitstream = out; 
507
 
        if(*flags & AVI_KEY_FRAME)
508
 
        {
509
 
                xvid_enc_frame.type=XVID_TYPE_IVOP;
510
 
        }
511
 
        else if(*flags & AVI_B_FRAME)
512
 
        {
513
 
                xvid_enc_frame.type=XVID_TYPE_BVOP;
514
 
        }
515
 
        else
516
 
        {       //P
517
 
                        xvid_enc_frame.type=XVID_TYPE_PVOP;
518
 
        }
519
 
        
520
 
        /* Encode the frame */
521
 
        ret = xvid_encore(_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
522
 
                                          &xvid_enc_stats);
523
 
        if(ret<0)
524
 
                {
525
 
                        printf("Error calling xvid4 %d\n",ret);
526
 
                        return 0;
527
 
                        
528
 
                }
529
 
        postAmble(flags);
530
 
        
531
 
        *len=xvid_res.total_bits=ret;
532
 
        xvid_res.total_bits*=8;
533
 
        
534
 
        return 1;
535
 
 
536
 
}
537
 
// *************************************************
538
 
// *************************************************
539
 
//              CBR
540
 
// *************************************************
541
 
// *************************************************
542
 
uint8_t xvid4EncoderCBR::init(uint32_t br,uint32_t fps1000, xvid4EncParam *param)
543
 
{
544
 
    int xerr;
545
 
        printf("Using Xvid 4 codec with CBR mode = %lu kbps\n",br);
546
 
        _bitrate=br;
547
 
        _fps1000=fps1000;
548
 
        memcpy(&_param,param,sizeof(_param));
549
 
        dump();
550
 
        // Here we go...
551
 
        MMSET(single);
552
 
        MMSET(xvid_enc_create);
553
 
        
554
 
        xvid_enc_create.version = XVID_VERSION;
555
 
        xvid_enc_create.width = _w;
556
 
        xvid_enc_create.height = _h;
557
 
        
558
 
 
559
 
        plugins[0].func = xvid_plugin_single;
560
 
        plugins[0].param = &single;
561
 
        
562
 
        single.version = XVID_VERSION;
563
 
        single.bitrate = _bitrate*1000; // Kb->bit
564
 
 
565
 
        xvid_enc_create.plugins = plugins;
566
 
        xvid_enc_create.num_plugins = 1;
567
 
        
568
 
        //Framerate
569
 
        xvid_enc_create.fincr = 1000;
570
 
        xvid_enc_create.fbase = _fps1000;
571
 
        //
572
 
        createUpdate();
573
 
        xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
574
 
        if(xerr<0)
575
 
        {
576
 
                printf("Xvid-4 init error :%d\n",xerr);
577
 
                return 0;
578
 
        
579
 
        }
580
 
        
581
 
        _handle = xvid_enc_create.handle;
582
 
 
583
 
        printf("Xvid-4 CBR init Ok\n");
584
 
 
585
 
        return 1;
586
 
}
587
 
 
588
 
 
589
 
uint8_t xvid4EncoderCBR::encode(
590
 
                        ADMImage * in,
591
 
                        uint8_t * out,
592
 
                        uint32_t * len,
593
 
                        uint32_t * flags)
594
 
{
595
 
int ret;
596
 
 
597
 
        
598
 
        
599
 
        preAmble(in->data);
600
 
        
601
 
        xvid_enc_frame.quant = 0;
602
 
        xvid_enc_frame.bitstream = out;
603
 
        
604
 
        //--
605
 
        
606
 
        
607
 
        //--
608
 
 
609
 
        
610
 
        /* Encode the frame */
611
 
        ret = xvid_encore(_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
612
 
                                          &xvid_enc_stats);
613
 
        if(ret<0)
614
 
                {
615
 
                        printf("Error calling xvid4 %d\n",ret);
616
 
                        return 0;
617
 
                        
618
 
                }
619
 
        postAmble(flags);
620
 
        
621
 
        *len=xvid_res.total_bits=ret;
622
 
        xvid_res.total_bits*=8;
623
 
        
624
 
        return 1;
625
 
 
626
 
}
627
 
// *************************************************
628
 
// *************************************************
629
 
//              Pass1
630
 
// *************************************************
631
 
// *************************************************
632
 
uint8_t xvid4EncoderPass1::init(uint32_t br,uint32_t fps1000, xvid4EncParam *param)
633
 
{
634
 
    int xerr;
635
 
        printf("Using Xvid 4 codec pass 1 of 2 (log: %s)\n",(char *)param->logName);
636
 
        
637
 
        _fps1000=fps1000;
638
 
        memcpy(&_param,param,sizeof(_param));
639
 
        dump();
640
 
        // Here we go...
641
 
        MMSET(xvid_enc_create);
642
 
        MMSET(pass1);
643
 
        
644
 
        xvid_enc_create.version = XVID_VERSION;
645
 
        xvid_enc_create.width = _w;
646
 
        xvid_enc_create.height = _h;
647
 
        
648
 
        
649
 
 
650
 
        plugins[0].func = xvid_plugin_2pass1;
651
 
        plugins[0].param = &pass1;
652
 
        
653
 
        pass1.version = XVID_VERSION;
654
 
        pass1.filename = (char *)_param.logName;
655
 
 
656
 
        xvid_enc_create.plugins = plugins;
657
 
        xvid_enc_create.num_plugins = 1;
658
 
        
659
 
        
660
 
        //Framerate
661
 
        xvid_enc_create.fincr = 1000;
662
 
        xvid_enc_create.fbase = _fps1000;
663
 
        //
664
 
        createUpdate();
665
 
        // Force Qz=2 !
666
 
        xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
667
 
        if(xerr<0)
668
 
        {
669
 
                printf("Xvid-4 init error :%d\n",xerr);
670
 
                return 0;
671
 
        
672
 
        }
673
 
        
674
 
        _handle = xvid_enc_create.handle;
675
 
 
676
 
        printf("Xvid-4 Pass1 init Ok\n");
677
 
 
678
 
        return 1;
679
 
}
680
 
 
681
 
 
682
 
uint8_t xvid4EncoderPass1::encode(
683
 
                        ADMImage * in,
684
 
                        uint8_t * out,
685
 
                        uint32_t * len,
686
 
                        uint32_t * flags)
687
 
{
688
 
int ret;
689
 
 
690
 
        
691
 
        
692
 
        preAmble(in->data);
693
 
        
694
 
        //xvid_enc_frame.quant = 2;
695
 
        xvid_enc_frame.bitstream = out;
696
 
        xvid_enc_frame.input.plane[0] = YPLANE(in);
697
 
        
698
 
 
699
 
        
700
 
        /* Encode the frame */
701
 
        ret = xvid_encore(_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
702
 
                                          &xvid_enc_stats);
703
 
        if(ret<0)
704
 
                {
705
 
                        printf("Error calling xvid4 %d\n",ret);
706
 
                        return 0;
707
 
                        
708
 
                }
709
 
        postAmble(flags);
710
 
        
711
 
        *len=xvid_res.total_bits=ret;
712
 
        xvid_res.total_bits*=8;
713
 
        
714
 
        return 1;
715
 
 
716
 
}
717
 
// *************************************************
718
 
// *************************************************
719
 
//              Pass2
720
 
// *************************************************
721
 
// *************************************************
722
 
uint8_t xvid4EncoderPass2::init(uint32_t br,uint32_t fps1000, xvid4EncParam *param)
723
 
{
724
 
    int xerr;
725
 
        printf("Using Xvid 4 codec with 2pass  mode pass 2 of 2, average bitrate = %lu\n",br);
726
 
        _bitrate=br;
727
 
        _fps1000=fps1000;
728
 
        memcpy(&_param,param,sizeof(_param));
729
 
        dump();
730
 
        // Here we go...
731
 
        MMSET(xvid_enc_create);
732
 
        MMSET(pass2);
733
 
        
734
 
        xvid_enc_create.version = XVID_VERSION;
735
 
        xvid_enc_create.width = _w;
736
 
        xvid_enc_create.height = _h;
737
 
        
738
 
        
739
 
 
740
 
        plugins[0].func = xvid_plugin_2pass2;
741
 
        plugins[0].param = &pass2;
742
 
        
743
 
        pass2.version = XVID_VERSION;
744
 
        pass2.filename = (char *)_param.logName;
745
 
 
746
 
        pass2.bitrate=br;       // Average bitrate
747
 
        #define CPY(x) pass2.x=_param.x;printf(#x"=%d\n",pass2.x);
748
 
        
749
 
        CPY( keyframe_boost);
750
 
        CPY( curve_compression_high);
751
 
        CPY( curve_compression_low);
752
 
        CPY( overflow_control_strength);
753
 
        CPY( max_overflow_improvement);
754
 
        CPY( max_overflow_degradation);
755
 
        CPY( kfreduction);
756
 
        CPY( kfthreshold);
757
 
        
758
 
        CPY( container_frame_overhead);
759
 
        
760
 
        
761
 
        
762
 
        xvid_enc_create.plugins = plugins;
763
 
        xvid_enc_create.num_plugins = 1;
764
 
        
765
 
        //Framerate
766
 
        xvid_enc_create.fincr = 1000;
767
 
        xvid_enc_create.fbase = _fps1000;
768
 
        //
769
 
        createUpdate();
770
 
        xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
771
 
        if(xerr<0)
772
 
        {
773
 
                printf("Xvid-4 init error :%d\n",xerr);
774
 
                return 0;
775
 
        
776
 
        }
777
 
        
778
 
        _handle = xvid_enc_create.handle;
779
 
 
780
 
        printf("Xvid-4 init Ok\n");
781
 
 
782
 
        return 1;
783
 
}
784
 
 
785
 
 
786
 
uint8_t xvid4EncoderPass2::encode(
787
 
                        ADMImage * in,
788
 
                        uint8_t * out,
789
 
                        uint32_t * len,
790
 
                        uint32_t * flags)
791
 
{
792
 
int ret;
793
 
 
794
 
        
795
 
        
796
 
        preAmble(in->data);
797
 
                
798
 
        xvid_enc_frame.bitstream = out;
799
 
        xvid_enc_frame.input.plane[0] = YPLANE(in);
800
 
        
801
 
 
802
 
        
803
 
        /* Encode the frame */
804
 
        ret = xvid_encore(_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
805
 
                                          &xvid_enc_stats);
806
 
        if(ret<0)
807
 
                {
808
 
                        printf("Error calling xvid4 %d\n",ret);
809
 
                        return 0;
810
 
                        
811
 
                }
812
 
        postAmble(flags);
813
 
        
814
 
        *len=xvid_res.total_bits=ret;
815
 
        xvid_res.total_bits*=8;
816
 
        
817
 
        return 1;
818
 
 
819
 
}
 
318
  switch (_param.vhqmode)
 
319
    {
 
320
    case 4:
 
321
      VHME (EXTSEARCH_RD);
 
322
    case 3:
 
323
      VHME (HALFPELREFINE8_RD);
 
324
      VHME (QUARTERPELREFINE8_RD);
 
325
      VHME (CHECKPREDICTION_RD);
 
326
 
 
327
    case 2:
 
328
      VHME (HALFPELREFINE16_RD);
 
329
      VHME (QUARTERPELREFINE16_RD);
 
330
    case 1:
 
331
      SVOP (vhqmode, MODEDECISION_RD);
 
332
    case 0:
 
333
      break;
 
334
    default:
 
335
      ADM_assert (0);
 
336
    }
 
337
  if (_param.qpel)
 
338
    {
 
339
      xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL;
 
340
 
 
341
      xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16;
 
342
      xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8;
 
343
    }
 
344
 
 
345
  if (_param.gmc)
 
346
    {
 
347
      xvid_enc_frame.vol_flags |= XVID_VOL_GMC;
 
348
      xvid_enc_frame.motion |= XVID_ME_GME_REFINE;
 
349
    }
 
350
 
 
351
  if (_param.turbo)
 
352
    {
 
353
      xvid_enc_frame.motion |= XVID_ME_FASTREFINE16;
 
354
      xvid_enc_frame.motion |= XVID_ME_FASTREFINE8;
 
355
      xvid_enc_frame.motion |= XVID_ME_SKIP_DELTASEARCH;
 
356
      xvid_enc_frame.motion |= XVID_ME_FAST_MODEINTERPOLATE;
 
357
      xvid_enc_frame.motion |= XVID_ME_BFRAME_EARLYSTOP;
 
358
    }
 
359
 
 
360
  xvid_enc_frame.bframe_threshold = _param.bframe_threshold;
 
361
 
 
362
  xvid_enc_frame.input.csp = XVID_CSP_YV12;
 
363
  xvid_enc_frame.input.stride[0] = _w;
 
364
  xvid_enc_frame.input.stride[1] = _w >> 1;
 
365
  xvid_enc_frame.input.stride[2] = _w >> 1;
 
366
  xvid_enc_frame.type = XVID_TYPE_AUTO;
 
367
 
 
368
 
 
369
  /* Set up motion estimation flags */
 
370
  xvid_enc_frame.input.plane[0] = in;
 
371
  xvid_enc_frame.input.plane[1] = in + (_w * _h);
 
372
  xvid_enc_frame.input.plane[2] = in + ((_w * _h * 5) >> 2);
 
373
 
 
374
  xvid_enc_frame.par_width = _param.par_width;
 
375
  xvid_enc_frame.par_height = _param.par_height;
 
376
  //printf("Using AR : %u x %u\n",xvid_enc_frame.par_width,xvid_enc_frame.par_height );
 
377
  if (xvid_enc_frame.par_width != xvid_enc_frame.par_height)
 
378
      xvid_enc_frame.par = XVID_PAR_EXT;
 
379
  else
 
380
      xvid_enc_frame.par = XVID_PAR_11_VGA;
 
381
 
 
382
  /* Custome matrices */
 
383
  if(_param.useCustomIntra) 
 
384
  {
 
385
  if(!xvid_enc_frame.quant_intra_matrix)  
 
386
      printf("[Xvid4]Using custom intra matrix\n");
 
387
      xvid_enc_frame.quant_intra_matrix=_param.intraMatrix;
 
388
  }
 
389
  if(_param.useCustomInter)
 
390
  {
 
391
    if(!xvid_enc_frame.quant_inter_matrix)
 
392
      printf("[Xvid4]Using custom inter matrix\n");
 
393
     xvid_enc_frame.quant_inter_matrix=_param.interMatrix;
 
394
  }
 
395
  return 1;
 
396
}
 
397
 
 
398
uint8_t
 
399
xvid4Encoder::postAmble (ADMBitstream * out)
 
400
{
 
401
  out->flags = 0;
 
402
  if (xvid_enc_frame.out_flags & XVID_KEYFRAME)
 
403
    {
 
404
      out->flags = AVI_KEY_FRAME;
 
405
    }
 
406
  else if (xvid_enc_stats.type == XVID_TYPE_BVOP)
 
407
    {
 
408
      out->flags = AVI_B_FRAME;
 
409
 
 
410
    }
 
411
  out->out_quantizer = xvid_enc_stats.quant;
 
412
  out->ptsFrame= xvid_framenum; 
 
413
  return 1;
 
414
}
 
415
 
 
416
// *************************************************
 
417
// *************************************************
 
418
//                                                                      CQ
 
419
// *************************************************
 
420
// *************************************************
 
421
uint8_t
 
422
  xvid4EncoderCQ::init (uint32_t q, uint32_t fps1000, xvid4EncParam * param)
 
423
{
 
424
  int xerr;
 
425
  printf ("Using Xvid 4 codec with CQ mode = %lu (%dx%d)\n", q, _w, _h);
 
426
  _q = q;
 
427
  _fps1000 = fps1000;
 
428
  memcpy (&_param, param, sizeof (_param));
 
429
  dump ();
 
430
  // Here we go...
 
431
  MMSET (xvid_enc_create);
 
432
  xvid_enc_create.version = XVID_VERSION;
 
433
  xvid_enc_create.width = _w;
 
434
  xvid_enc_create.height = _h;
 
435
  MMSET (single);
 
436
 
 
437
 
 
438
  plugins[0].func = xvid_plugin_single;
 
439
  plugins[0].param = &single;
 
440
 
 
441
  plugins[1].func = adm_hook;
 
442
  plugins[1].param = NULL;
 
443
 
 
444
 
 
445
  single.version = XVID_VERSION;
 
446
  single.bitrate = 1500;
 
447
 
 
448
  xvid_enc_create.plugins = plugins;
 
449
  xvid_enc_create.num_plugins = 2;
 
450
 
 
451
  //Framerate
 
452
  xvid_enc_create.fincr = 1000;
 
453
  xvid_enc_create.fbase = _fps1000;
 
454
  //
 
455
  createUpdate ();
 
456
  xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
 
457
  if (xerr < 0)
 
458
    {
 
459
      printf ("Xvid-4 init error :%d\n", xerr);
 
460
      return 0;
 
461
 
 
462
    }
 
463
 
 
464
  _handle = xvid_enc_create.handle;
 
465
 
 
466
  printf ("Xvid-4 CQ init Ok\n");
 
467
 
 
468
  return 1;
 
469
}
 
470
 
 
471
 
 
472
uint8_t
 
473
xvid4EncoderCQ::encode (ADMImage * in, ADMBitstream * out)
 
474
{
 
475
  int ret;
 
476
 
 
477
  xvid_enc_frame.quant = _q;
 
478
  return xvid4Encoder::encode(in,out);
 
479
 
 
480
}
 
481
// *************************************************
 
482
// *************************************************
 
483
//              CQ but Q is controlled externally
 
484
// *************************************************
 
485
// *************************************************
 
486
 
 
487
uint8_t
 
488
  xvid4EncoderVBRExternal::init (uint32_t q, uint32_t fps1000,
 
489
                                 xvid4EncParam * param)
 
490
{
 
491
  int xerr;
 
492
  printf ("Using Xvid 4 codec with CQ mode = %lu (%dx%d)\n", q, _w, _h);
 
493
  _q = q;
 
494
  _fps1000 = fps1000;
 
495
  memcpy (&_param, param, sizeof (_param));
 
496
  dump ();
 
497
  // Here we go...
 
498
  MMSET (xvid_enc_create);
 
499
  xvid_enc_create.version = XVID_VERSION;
 
500
  xvid_enc_create.width = _w;
 
501
  xvid_enc_create.height = _h;
 
502
  MMSET (single);
 
503
 
 
504
 
 
505
  plugins[0].func = xvid_plugin_single;
 
506
  plugins[0].param = &single;
 
507
 
 
508
  plugins[1].func = adm_hook;
 
509
  plugins[1].param = NULL;
 
510
 
 
511
  single.version = XVID_VERSION;
 
512
  single.bitrate = 1500;
 
513
 
 
514
  xvid_enc_create.plugins = plugins;
 
515
  xvid_enc_create.num_plugins = 2;
 
516
 
 
517
  //Framerate
 
518
  xvid_enc_create.fincr = 1000;
 
519
  xvid_enc_create.fbase = _fps1000;
 
520
  //
 
521
  createUpdate ();
 
522
  xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
 
523
  if (xerr < 0)
 
524
    {
 
525
      printf ("Xvid-4 init error :%d\n", xerr);
 
526
      return 0;
 
527
 
 
528
    }
 
529
 
 
530
  _handle = xvid_enc_create.handle;
 
531
 
 
532
  printf ("Xvid-4 CQ init Ok\n");
 
533
 
 
534
  return 1;
 
535
}
 
536
 
 
537
 
 
538
 
 
539
 
 
540
uint8_t
 
541
  xvid4EncoderVBRExternal::encode (ADMImage * in,
 
542
                                      ADMBitstream * out)
 
543
{
 
544
  xvid_enc_frame.quant = out->in_quantizer;
 
545
  if (out->flags & AVI_KEY_FRAME)
 
546
    {
 
547
      xvid_enc_frame.type = XVID_TYPE_IVOP;
 
548
    }
 
549
  else if (out->flags & AVI_B_FRAME)
 
550
    {
 
551
      xvid_enc_frame.type = XVID_TYPE_BVOP;
 
552
    }
 
553
  else
 
554
    {                           //P
 
555
      xvid_enc_frame.type = XVID_TYPE_PVOP;
 
556
    }
 
557
    return xvid4Encoder::encode(in,out);
 
558
}
 
559
// *************************************************
 
560
// *************************************************
 
561
//              CBR
 
562
// *************************************************
 
563
// *************************************************
 
564
uint8_t
 
565
  xvid4EncoderCBR::init (uint32_t br, uint32_t fps1000, xvid4EncParam * param)
 
566
{
 
567
  int xerr;
 
568
  printf ("Using Xvid 4 codec with CBR mode = %lu kbps\n", br);
 
569
  _bitrate = br;
 
570
  _fps1000 = fps1000;
 
571
  memcpy (&_param, param, sizeof (_param));
 
572
  dump ();
 
573
  // Here we go...
 
574
  MMSET (single);
 
575
  MMSET (xvid_enc_create);
 
576
 
 
577
  xvid_enc_create.version = XVID_VERSION;
 
578
  xvid_enc_create.width = _w;
 
579
  xvid_enc_create.height = _h;
 
580
 
 
581
 
 
582
  plugins[0].func = xvid_plugin_single;
 
583
  plugins[0].param = &single;
 
584
 
 
585
  plugins[1].func = adm_hook;
 
586
  plugins[1].param = NULL;
 
587
 
 
588
  single.version = XVID_VERSION;
 
589
  single.bitrate = _bitrate * 1000;     // Kb->bit
 
590
 
 
591
  xvid_enc_create.plugins = plugins;
 
592
  xvid_enc_create.num_plugins = 2;
 
593
 
 
594
  //Framerate
 
595
  xvid_enc_create.fincr = 1000;
 
596
  xvid_enc_create.fbase = _fps1000;
 
597
  //
 
598
  createUpdate ();
 
599
  xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
 
600
  if (xerr < 0)
 
601
    {
 
602
      printf ("Xvid-4 init error :%d\n", xerr);
 
603
      return 0;
 
604
 
 
605
    }
 
606
 
 
607
  _handle = xvid_enc_create.handle;
 
608
 
 
609
  printf ("Xvid-4 CBR init Ok\n");
 
610
 
 
611
  return 1;
 
612
}
 
613
 
 
614
 
 
615
uint8_t
 
616
xvid4EncoderCBR::encode (ADMImage * in, ADMBitstream * out)
 
617
{
 
618
  xvid_enc_frame.quant = 0;
 
619
  return xvid4Encoder::encode(in,out);
 
620
}
 
621
// *************************************************
 
622
// *************************************************
 
623
//              Pass1
 
624
// *************************************************
 
625
// *************************************************
 
626
uint8_t
 
627
  xvid4EncoderPass1::init (uint32_t br, uint32_t fps1000,
 
628
                           xvid4EncParam * param)
 
629
{
 
630
  int xerr;
 
631
  printf ("Using Xvid 4 codec pass 1 of 2 (log: %s)\n",
 
632
          (char *) param->logName);
 
633
 
 
634
  _fps1000 = fps1000;
 
635
  memcpy (&_param, param, sizeof (_param));
 
636
  dump ();
 
637
  // Here we go...
 
638
  MMSET (xvid_enc_create);
 
639
  MMSET (pass1);
 
640
 
 
641
  xvid_enc_create.version = XVID_VERSION;
 
642
  xvid_enc_create.width = _w;
 
643
  xvid_enc_create.height = _h;
 
644
 
 
645
 
 
646
 
 
647
  plugins[0].func = xvid_plugin_2pass1;
 
648
  plugins[0].param = &pass1;
 
649
 
 
650
 
 
651
  pass1.version = XVID_VERSION;
 
652
  pass1.filename = (char *) _param.logName;
 
653
 
 
654
  xvid_enc_create.plugins = plugins;
 
655
  xvid_enc_create.num_plugins = 1;
 
656
 
 
657
 
 
658
  //Framerate
 
659
  xvid_enc_create.fincr = 1000;
 
660
  xvid_enc_create.fbase = _fps1000;
 
661
  //
 
662
  createUpdate ();
 
663
  // Force Qz=2 !
 
664
  xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
 
665
  if (xerr < 0)
 
666
    {
 
667
      printf ("Xvid-4 init error :%d\n", xerr);
 
668
      return 0;
 
669
 
 
670
    }
 
671
 
 
672
  _handle = xvid_enc_create.handle;
 
673
 
 
674
  printf ("Xvid-4 Pass1 init Ok\n");
 
675
 
 
676
  return 1;
 
677
}
 
678
 
 
679
 
 
680
 
 
681
// *************************************************
 
682
// *************************************************
 
683
//              Pass2
 
684
// *************************************************
 
685
// *************************************************
 
686
uint8_t
 
687
  xvid4EncoderPass2::init (uint32_t br, uint32_t fps1000,
 
688
                           xvid4EncParam * param)
 
689
{
 
690
  int xerr;
 
691
  printf
 
692
    ("Using Xvid 4 codec with 2pass  mode pass 2 of 2, average bitrate = %lu\n",
 
693
     br);
 
694
  _bitrate = br;
 
695
  _fps1000 = fps1000;
 
696
  memcpy (&_param, param, sizeof (_param));
 
697
  dump ();
 
698
  // Here we go...
 
699
  MMSET (xvid_enc_create);
 
700
  MMSET (pass2);
 
701
 
 
702
  xvid_enc_create.version = XVID_VERSION;
 
703
  xvid_enc_create.width = _w;
 
704
  xvid_enc_create.height = _h;
 
705
 
 
706
 
 
707
 
 
708
  plugins[0].func = xvid_plugin_2pass2;
 
709
  plugins[0].param = &pass2;
 
710
  plugins[1].func = adm_hook;
 
711
  plugins[1].param = NULL;
 
712
 
 
713
  pass2.version = XVID_VERSION;
 
714
  pass2.filename = (char *) _param.logName;
 
715
 
 
716
  pass2.bitrate = br;           // Average bitrate
 
717
#define CPY(x) pass2.x=_param.x;printf(#x"=%d\n",pass2.x);
 
718
 
 
719
  CPY (keyframe_boost);
 
720
  CPY (curve_compression_high);
 
721
  CPY (curve_compression_low);
 
722
  CPY (overflow_control_strength);
 
723
  CPY (max_overflow_improvement);
 
724
  CPY (max_overflow_degradation);
 
725
  CPY (kfreduction);
 
726
  CPY (kfthreshold);
 
727
 
 
728
  CPY (container_frame_overhead);
 
729
 
 
730
 
 
731
 
 
732
  xvid_enc_create.plugins = plugins;
 
733
  xvid_enc_create.num_plugins = 2;
 
734
 
 
735
  //Framerate
 
736
  xvid_enc_create.fincr = 1000;
 
737
  xvid_enc_create.fbase = _fps1000;
 
738
  //
 
739
  createUpdate ();
 
740
  xerr = xvid_encore (NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
 
741
  if (xerr < 0)
 
742
    {
 
743
      printf ("Xvid-4 init error :%d\n", xerr);
 
744
      return 0;
 
745
 
 
746
    }
 
747
 
 
748
  _handle = xvid_enc_create.handle;
 
749
 
 
750
  printf ("Xvid-4 init Ok\n");
 
751
 
 
752
  return 1;
 
753
}
 
754
 
 
755
 
820
756
#endif