2
Strongly derivated from mpeg2enc, mean
3
Modifications and enhancements (C) 2000/2001 Andrew Stevens
6
* These modifications are free software; you can redistribute it
7
* and/or modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2 of
9
* the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33
#define GLOBAL /* used by global.h */
35
#include "motionsearch.h"
36
#include "predict_ref.h"
37
#include "transfrm_ref.h"
38
#include "quantize_ref.h"
39
#include "format_codes.h"
40
#include "mpegconsts.h"
41
#include "fastintfns.h"
42
#if defined( HAVE_ALTIVEC) && defined(USE_ALTIVEC)
43
/* needed for ALTIVEC_BENCHMARK and print_benchmark_statistics() */
44
//#include "../utils/altivec/altivec_conf.h"
46
#include "mpeg2parm.h"
47
#include "ADM_codecs/ADM_codec.h"
49
#define aprintf printf
50
/* private prototypes */
51
extern void set_format_presets(mpeg2parm *param,Mpeg2Settings *opt);
52
extern int infer_default_params(mpeg2parm *param,Mpeg2Settings *opt);
53
extern int check_param_constraints(mpeg2parm *param);
54
extern void init_mpeg_parms(mpeg2parm *param,Mpeg2Settings *opt);
55
extern int infer_mpeg1_aspect_code( mpeg2parm *param );
56
extern void init_encoder(void);
57
extern void init_quantmat(mpeg2parm *param,Mpeg2Settings *opt);
58
extern void border_mark( uint8_t *frame,
59
int w1, int h1, int w2, int h2);
60
extern void init_encoder(mpeg2parm *param,Mpeg2Settings *opt);
61
extern void DisplayFrameRates(void);
62
extern void DisplayAspectRatios(void);
63
extern int parse_custom_matrixfile(char *fname, int dbug);
64
extern void Usage(void);
65
extern void parse_custom_option(char *arg,mpeg2parm *param);
67
extern int push_init( void );
68
extern int push_cleanup( void );
69
extern void mpeg_freebuffers(void);
71
mpeg2parm myParam,*param;
73
int rateCtlDisablePadding;
74
#define MAX(a,b) ( (a)>(b) ? (a) : (b) )
75
#define MIN(a,b) ( (a)<(b) ? (a) : (b) )
81
static int mpeg_cleanup( void );
83
/* Command Parameter values. These are checked and processed for
84
defaults etc after parsing. The resulting values then set the opt->
85
variables that control the actual encoder.
89
/* Input Stream parameter values that have to be further processed to
90
set encoding options */
93
static unsigned int strm_frame_rate_code;
97
uint16_t custom_intra_quantizer_matrix[64];
98
uint16_t custom_nonintra_quantizer_matrix[64];
102
void feedOneFrame(char *y, char *u,char *v);
104
static int fedPictures=0;
106
unsigned char *mpeg2enc_buffer=NULL;
107
int mpegenc_init(mpeg2parm *incoming,int width, int height, int fps1000)
110
Reset a whole bunch of vars
112
istrm_nframes=lum_buffer_size=chrom_buffer_size=block_count=mb_width=mb_height=mb_height2=0;
113
qsubsample_offset=fsubsample_offset=0;
116
/*-----------------------------------------*/
118
if(incoming->noPadding)
120
rateCtlDisablePadding=1;
121
printf("Padding disabled\n");
123
else rateCtlDisablePadding=0;
125
memset(&myControl,0,sizeof(myControl));
127
// 1 we memset the opt
128
memset(opt,0,sizeof(*opt));
130
memcpy(param,incoming, sizeof(*param));
135
if(fps1000 > 25500 || fps1000 < 24000)
141
param->frame_rate=MPG_FILM;
142
printf("Detecting FILM format\n");
143
if(param->format!=1) // not VCD -> SVCD or DVD
145
param->_32_pulldown=1;
146
printf("****Activating pulldown\n");
152
param->frame_rate=MPG_NTSC;
154
printf("Detecting NTSC format\n");
160
param->frame_rate=MPG_PAL;
161
printf("Detecting PAL format\n");
166
opt->horizontal_size=width;
167
opt->vertical_size=height;
168
// param->fieldenc=0; // progressive
170
strm_frame_rate_code=2;
171
opt->frame_rate_code=1;
175
set_format_presets(param,opt);
177
infer_default_params(param,opt);
179
check_param_constraints(param);
183
aprintf("[mpeg2enc]Encoding MPEG-%d video \n",param->mpeg);
184
aprintf("[mpeg2enc]Horizontal size: %d pe \nl",opt->horizontal_size);
185
aprintf("[mpeg2enc]Vertical size: %d pel \n",opt->vertical_size);
186
aprintf("[mpeg2enc]Aspect ratio code: %d = %s \n",
188
mpeg_aspect_code_definition(param->mpeg,param->aspect_ratio));
189
aprintf("[mpeg2enc]Frame rate code: %d = %s \n",
191
mpeg_framerate_code_definition(param->frame_rate));
194
aprintf("[mpeg2enc]Bitrate: %d KBit/s \n",param->bitrate/1000);
196
aprintf( "[mpeg2enc]Bitrate: VCD \n");
198
aprintf("[mpeg2enc]Quality factor: %d (Quantisation = %d) (1=best, 31=worst) \n",
200
(int)(inv_scale_quant( param->mpeg == 1 ? 0 : 1,
204
aprintf("[mpeg2enc]Field order for input: %s \n",
205
mpeg_interlace_code_definition(param->input_interlacing) );
207
if( param->seq_length_limit )
209
aprintf( "[mpeg2enc]New Sequence every %d Mbytes \n", param->seq_length_limit );
210
aprintf( "[mpeg2enc]Assuming non-video stream of %d Kbps \n", param->nonvid_bitrate );
213
aprintf( "[mpeg2enc]Sequence unlimited length \n" );
215
aprintf("[mpeg2enc]Search radius: %d \n",param->searchrad);
218
init_mpeg_parms(param,opt);
220
/* read quantization matrices */
221
init_quantmat(param,opt);
223
init_encoder(param,opt);
231
aprintf("opt->enc_height2 :%d opt->enc_width: %d opt->enc_height2:%d \n",
232
opt->enc_height2,opt->enc_width,opt->enc_height);
235
int mpegenc_encode( char *in, char *out, int *size,int *flags,int *quant)
239
mpeg2enc_buffer=(unsigned char *)out;
244
in+((opt->horizontal_size*opt->vertical_size*5)>>2),
245
in+(opt->horizontal_size*opt->vertical_size)
251
if(fedPictures<PREFILL)
253
*size=mpeg2enc_buffer-(unsigned char *)out;
257
putseq_next(&type,quant);
258
#warning : Approximate..
259
*quant=map_non_linear_mquant[*quant];
260
*size=mpeg2enc_buffer-(unsigned char *)out;
264
*flags=AVI_KEY_FRAME;
276
int mpegenc_end(void)
278
uint8_t out[20]; // Temporary buffer to store SEQ_END_CODE
279
mpeg2enc_buffer=(unsigned char *)out;
286
Release (most) memory consumed by mpeg2enc
289
int mpeg_cleanup( void )
294
printf("Trying to clean already cleaned frame_buffers!!!\n");
297
// frame buffers is cleaned by mpeg_free
302
delete [] opt->motion_data;
303
opt->motion_data=NULL;
306
if(lum_mean) delete [] lum_mean;
308
printf("frame_buffers cleaned up\n");
311
int mpegenc_setQuantizer(int newQz)
314
if(newQz>31) newQz=31;
317
ctl->quant_floor = inv_scale_quant( param->mpeg == 1 ? 0 : 1,