1
/***************************************************************************
2
adm_encxvid.cpp - description
4
Encoder for Xvid 1.0x (dev-api4)
5
begin : Sun Jul 14 2002
6
copyright : (C) 2002/2003 by mean
7
email : fixounet@free.fr
8
***************************************************************************/
10
/***************************************************************************
12
* This program is free software; you can redistribute it and/or modify *
13
* it under the terms of the GNU General Public License as published by *
14
* the Free Software Foundation; either version 2 of the License, or *
15
* (at your option) any later version. *
17
***************************************************************************/
20
#define __STDC_LIMIT_MACROS
28
#include "ADM_default.h"
30
#include "ADM_encoder/ADM_vidEncode.hxx"
32
#include "ADM_videoFilter.h"
33
#include "ADM_codecs/ADM_xvid4.h"
34
#include "ADM_encoder/adm_encoder.h"
35
#include "ADM_encoder/adm_encXvid4.h"
38
#define aprintf printf
42
/*_________________________________________________*/
43
EncoderXvid4::EncoderXvid4 (COMPRES_PARAMS * codecconfig)
47
strcpy (_logname, "");
51
memcpy (&_param, codecconfig, sizeof (_param));
52
ADM_assert (codecconfig->extraSettingsLen == sizeof (encparam));
53
memcpy (&encparam, codecconfig->extraSettings, sizeof (encparam));
57
EncoderXvid4::~EncoderXvid4 ()
63
//--------------------------------
65
EncoderXvid4::configure (AVDMGenericVideoStream * instream, int useExistingLogFile)
67
ADM_assert (instream);
70
//uint32_t flag1,flag2,flag3;
71
if(encparam.par_as_input)
73
encparam.par_width=instream->getPARWidth();
74
encparam.par_height=instream->getPARHeight();
77
printf("[xvid] Using %u x %u aspect ratio\n",encparam.par_width,encparam.par_height);
81
info = instream->getInfo ();
84
// _vbuffer = new uint8_t[_w * _h * 2];
85
_vbuffer = new ADMImage (_w, _h);
86
ADM_assert (_vbuffer);
88
_fps1000 = info->fps1000;
92
printf ("[xvid] Follow quant mode\n");
94
_codec = new xvid4EncoderVBRExternal (_w, _h);
95
if (!_codec->init (2, info->fps1000, &encparam))
97
printf ("[xvid] Error init Follow mode\n");
102
printf ("[xvid] CQ mode: %ld\n", _param.qz);
104
_codec = new xvid4EncoderCQ (_w, _h);
106
if (!_codec->init (_param.qz, info->fps1000, &encparam))
108
printf ("[xvid] Error init CQ mode\n");
113
printf ("[xvid] CBR mode: %lu\n", _param.bitrate);
116
_codec = new xvid4EncoderCBR (_w, _h);
117
if (!_codec->init (_param.bitrate, info->fps1000, &encparam))
119
printf ("[xvid] Error init CBR mode\n");
124
case COMPRESS_2PASS_BITRATE:
125
if(_param.mode==COMPRESS_2PASS)
126
printf ("[xvid] Dual size: %lu (%s)\n", _param.finalsize, _logname);
128
printf ("[xvid] Dual avg br: %u kb/s (%s)\n", _param.avg_bitrate, _logname);
130
_codec = new xvid4EncoderPass1 (_w, _h);
131
strcpy (encparam.logName, _logname);
132
printf ("[xvid] Using %s as stat file\n", encparam.logName);
138
printf ("[xvid] Encoder ready, w: %lu h: %lu mode: %d\n", _w, _h, _state);
145
uint8_t EncoderXvid4::startPass1 (void)
149
ADM_assert (_state == enc_Pass1);
150
info = _in->getInfo ();
151
if (!_codec->init (_param.bitrate, info->fps1000, &encparam))
153
printf ("[xvid] Error init pass 1 mode\n");
159
int EncoderXvid4::getRequirements (void) { return ADM_ENC_REQ_NULL_FLUSH; }
161
uint8_t EncoderXvid4::isDualPass (void)
163
if ((_state == enc_Pass1) || (_state == enc_Pass2))
171
uint8_t EncoderXvid4::setLogFile (const char *lofile, uint32_t nbframe)
173
strcpy (_logname, lofile);
174
_frametogo = nbframe;
175
_totalframe = nbframe;
180
//______________________________
182
EncoderXvid4::encode (uint32_t frame, ADMBitstream *out)
189
if (frame != UINT32_MAX)
191
if (!_in->getFrameNumberNoAlloc (frame, &l, _vbuffer, &f))
193
printf ("[xvid] Error: Cannot read incoming frame!\n");
202
if (frame != UINT32_MAX)
206
if (frame < (encparam.bframes + 1))
208
out->flags = AVI_KEY_FRAME;
209
printf ("[xvid] Forcing keyframe for B frame\n");
212
int q = _vbuffer->_Qp;
216
printf ("[xvid] Out of bound incoming q: %d\n", q);
224
out->in_quantizer = q;
231
return _codec->encode (frame == UINT32_MAX ? NULL : _vbuffer, out);
241
//_______________________________
242
uint8_t EncoderXvid4::stop (void)
253
extern uint32_t ADM_computeBitrate(uint32_t fps1000, uint32_t nbFrame, uint32_t sizeInMB);
254
uint8_t EncoderXvid4::startPass2 (void)
259
ADM_assert (_state == enc_Pass1);
260
printf ("[xvid] Starting pass 2 (%d x %d)\n", _w, _h);
262
if(_param.mode==COMPRESS_2PASS)
265
br=ADM_computeBitrate( _fps1000,_totalframe,_param.finalsize);
266
printf("[xvid] Final Size: %u MB, avg bitrate %u kb/s\n",_param.finalsize,br/1000);
267
}else if(_param.mode==COMPRESS_2PASS_BITRATE)
269
br=_param.avg_bitrate*1000;
270
printf("[xvid] 2 pass avg bitrate %u kb/s\n",br/1000);
274
// Delete codec and start new one
282
_codec = new xvid4EncoderPass2 (_w, _h);
283
strcpy (encparam.logName, _logname);
284
printf ("[xvid] Using %s as stat file, average bitrate %d kbps\n", _logname, br / 1000);
285
if (!_codec->init (br, _fps1000, &encparam))
287
printf ("[xvid] Error initializing pass1 mode\n");