1 /*****************************************************************************
2  *
3  *  XVID MPEG-4 VIDEO CODEC
4  *  - Encoder related header  -
5  *
6  *  Copyright(C) 2002-2010 Michael Militzer <isibaar@xvid.org>
7  *               2002-2003 Peter Ross <pross@xvid.org>
8  *
9  *  This program is free software ; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation ; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY ; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program ; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  * $Id: encoder.h 1985 2011-05-18 09:02:35Z Isibaar $
24  *
25  ****************************************************************************/
26 
27 #ifndef _ENCODER_H_
28 #define _ENCODER_H_
29 
30 #include "xvid.h"
31 #include "portab.h"
32 #include "global.h"
33 
34 /*****************************************************************************
35  * Constants
36  ****************************************************************************/
37 
38 /* lambda base exponential. 1<<LAMBDA_EXP is the neutral lambda */
39 #define LAMBDA_EXP		6
40 
41 /*****************************************************************************
42  * Types
43  ****************************************************************************/
44 
45 typedef int bool;
46 
47 /*****************************************************************************
48  * Structures
49  ****************************************************************************/
50 
51 typedef struct
52 {
53     /* --- constants --- */
54 	uint32_t width;
55 	uint32_t height;
56 
57 	uint32_t edged_width;
58 	uint32_t edged_height;
59 	uint32_t mb_width;
60 	uint32_t mb_height;
61 
62     int plugin_flags;
63 
64 	/* frame rate increment & base */
65 	int32_t fincr;
66 	uint32_t fbase;
67 
68     int profile;
69 
70 	int global_flags;
71 	int bquant_ratio;
72 	int bquant_offset;
73 	int frame_drop_ratio;
74 
75     int min_quant[3];
76     int max_quant[3];
77 
78 	int par;
79 	int par_width;
80 	int par_height;
81 
82 	int iMaxKeyInterval;
83 	int max_bframes;
84 
85 /* --- inbetween vop stuff --- */
86 	/* rounding type; alternate 0-1 after each interframe */
87 	/* 1 <= fixed_code <= 4
88 	   automatically adjusted using motion vector statistics inside
89 	 */
90 
91 	/* vars that not "quite" frame independant */
92 	uint32_t m_rounding_type;
93 	uint32_t m_fcode;
94     int vol_flags;
95 
96 	int64_t m_stamp;
97 
98 	uint16_t *mpeg_quant_matrices;
99 	uint32_t last_quant_initialized_intra; /* needed for mpeg matrices initialization */
100 } MBParam;
101 
102 typedef struct
103 {
104 	int iTextBits;
105 	int iMvSum;
106 	int iMvCount;
107 	int kblks;
108 	int mblks;
109 	int ublks;
110 	int gblks;
111 	int iMVBits;
112 } Statistics;
113 
114 /* encoding queue */
115 typedef struct
116 {
117 	xvid_enc_frame_t frame;
118 	unsigned char quant_intra_matrix[64];
119 	unsigned char quant_inter_matrix[64];
120 	IMAGE image;
121 } QUEUEINFO;
122 
123 typedef struct
124 {
125     int frame_num;
126     int fincr;
127 	int vol_flags;
128     int vop_flags;
129 	int motion_flags;
130 
131 	int coding_type;
132 	uint32_t quant;
133 	uint32_t rounding_type;
134 	uint32_t fcode;
135 	uint32_t bcode;
136 
137 	uint32_t seconds;
138 	uint32_t ticks;
139 	int64_t stamp;
140 
141 	IMAGE image;
142 
143 	MACROBLOCK *mbs;
144 
145 	WARPPOINTS warp;		/* as in bitstream */
146 	GMC_DATA gmc_data;		/* common data for all MBs */
147 	NEW_GMC_DATA new_gmc_data;		/* common data for all MBs */
148 
149     int length;         /* the encoded size of this frame */
150 
151 	Statistics sStat;
152 	int is_edged, is_interpolated;
153 } FRAMEINFO;
154 
155 #include "motion/motion_smp.h"
156 
157 typedef struct
158 {
159 	MBParam mbParam;
160 
161 	int iFrameNum;
162 	int bitrate;
163 
164     /* zones */
165     unsigned int num_zones;
166     xvid_enc_zone_t * zones;
167 
168     /* plugins */
169     int num_plugins;    /* note: we store plugin flags in MBPARAM */
170     xvid_enc_plugin_t * plugins;
171 
172     /* dquant */
173 
174     int * temp_dquants;
175 
176     /* lambda */
177 
178     float * temp_lambda;
179 
180 	/* images */
181 
182 	FRAMEINFO *current;
183 	FRAMEINFO *reference;
184 
185 	IMAGE sOriginal;    /* original image copy for i/p frames */
186     IMAGE sOriginal2;   /* original image copy for b-frames */
187 	IMAGE vInterH;
188 	IMAGE vInterV;
189 	IMAGE vInterVf;
190 	IMAGE vInterHV;
191 	IMAGE vInterHVf;
192 
193 	IMAGE vGMC;
194 
195 	/* image queue */
196 	int queue_head;
197 	int queue_tail;
198 	int queue_size;
199 	QUEUEINFO *queue;
200 
201 	/* bframe buffer */
202 	int bframenum_head;
203 	int bframenum_tail;
204 	int flush_bframes;
205 
206 	FRAMEINFO **bframes;
207 	IMAGE f_refh;
208 	IMAGE f_refv;
209 	IMAGE f_refhv;
210 
211     /* closed_gop fixup temporary storage */
212 	int closed_bframenum; /* == -1 if there is no fixup intended */
213     QUEUEINFO closed_qframe;	/* qFrame, only valid when >= 0 */
214 
215 	/* multithreaded stuff */
216 	int num_threads;			/* number of encoder threads */
217 	SMPData * smpData;			/* data structures used to pass all thread-specific data */
218 
219 	int m_framenum; /* debug frame num counter; unlike iFrameNum, does not reset at ivop */
220 
221 	float fMvPrevSigma;
222 
223 	int num_slices;			/* number of slices to code */
224 } Encoder;
225 
226 /*****************************************************************************
227  * Inline functions
228  ****************************************************************************/
229 
230 static __inline uint8_t
get_fcode(uint16_t sr)231 get_fcode(uint16_t sr)
232 {
233 	if (sr <= 16)
234 		return 1;
235 
236 	else if (sr <= 32)
237 		return 2;
238 
239 	else if (sr <= 64)
240 		return 3;
241 
242 	else if (sr <= 128)
243 		return 4;
244 
245 	else if (sr <= 256)
246 		return 5;
247 
248 	else if (sr <= 512)
249 		return 6;
250 
251 	else if (sr <= 1024)
252 		return 7;
253 
254 	else
255 		return 0;
256 }
257 
258 /*****************************************************************************
259  * Prototypes
260  ****************************************************************************/
261 
262 void init_encoder(uint32_t cpu_flags);
263 
264 int enc_create(xvid_enc_create_t * create);
265 int enc_destroy(Encoder * pEnc);
266 int enc_encode(Encoder * pEnc,
267 				   xvid_enc_frame_t * pFrame,
268 				   xvid_enc_stats_t * stats);
269 
270 #endif
271