Mercurial > libavcodec.hg
annotate mpegvideo.c @ 13:174ef88f619a libavcodec
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
author | glantau |
---|---|
date | Mon, 30 Jul 2001 23:26:26 +0000 |
parents | 1b4461b5a7fb |
children | b69fe46fd708 |
rev | line source |
---|---|
0 | 1 /* |
2 * The simplest mpeg encoder (well, it was the simplest!) | |
3 * Copyright (c) 2000,2001 Gerard Lantau. | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 */ | |
19 #include <stdlib.h> | |
20 #include <stdio.h> | |
21 #include <math.h> | |
22 #include <string.h> | |
23 #include "avcodec.h" | |
24 #include "dsputil.h" | |
25 #include "mpegvideo.h" | |
26 | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
27 static void encode_picture(MpegEncContext *s, int picture_number); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
28 static void rate_control_init(MpegEncContext *s); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
29 static int rate_estimate_qscale(MpegEncContext *s); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
30 static void dct_unquantize_mpeg1_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
31 DCTELEM *block, int n, int qscale); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
32 static void dct_unquantize_h263_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
33 DCTELEM *block, int n, int qscale); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
34 static int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
35 static int dct_quantize_mmx(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
36 DCTELEM *block, int n, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
37 int qscale); |
0 | 38 #define EDGE_WIDTH 16 |
39 | |
40 /* enable all paranoid tests for rounding, overflows, etc... */ | |
41 //#define PARANOID | |
42 | |
43 //#define DEBUG | |
44 | |
45 /* for jpeg fast DCT */ | |
46 #define CONST_BITS 14 | |
47 | |
48 static const unsigned short aanscales[64] = { | |
49 /* precomputed values scaled up by 14 bits */ | |
50 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
51 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
52 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
53 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
54 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
55 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
56 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
57 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
58 }; | |
59 | |
60 static UINT8 h263_chroma_roundtab[16] = { | |
61 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | |
62 }; | |
63 | |
64 /* default motion estimation */ | |
65 int motion_estimation_method = ME_LOG; | |
66 | |
67 /* XXX: should use variable shift ? */ | |
68 #define QMAT_SHIFT_MMX 19 | |
69 #define QMAT_SHIFT 25 | |
70 | |
71 static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale) | |
72 { | |
73 int i; | |
74 | |
75 if (av_fdct == jpeg_fdct_ifast) { | |
76 for(i=0;i<64;i++) { | |
77 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
78 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ | |
79 | |
80 qmat[i] = (int)((1ULL << (QMAT_SHIFT + 11)) / (aanscales[i] * qscale * quant_matrix[i])); | |
81 } | |
82 } else { | |
83 for(i=0;i<64;i++) { | |
84 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
85 So 16 <= qscale * quant_matrix[i] <= 7905 | |
86 so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905 | |
87 */ | |
88 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); | |
89 } | |
90 } | |
91 } | |
92 | |
93 /* init common structure for both encoder and decoder */ | |
94 int MPV_common_init(MpegEncContext *s) | |
95 { | |
96 int c_size, i; | |
97 UINT8 *pict; | |
98 | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
99 if (s->out_format == FMT_H263) |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
100 s->dct_unquantize = dct_unquantize_h263_c; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
101 else |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
102 s->dct_unquantize = dct_unquantize_mpeg1_c; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
103 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
104 #ifdef HAVE_MMX |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
105 MPV_common_init_mmx(s); |
8 | 106 #endif |
0 | 107 s->mb_width = (s->width + 15) / 16; |
108 s->mb_height = (s->height + 15) / 16; | |
109 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH; | |
110 | |
111 for(i=0;i<3;i++) { | |
112 int w, h, shift, pict_start; | |
113 | |
114 w = s->linesize; | |
115 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | |
116 shift = (i == 0) ? 0 : 1; | |
117 c_size = (w >> shift) * (h >> shift); | |
118 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | |
119 | |
120 pict = av_mallocz(c_size); | |
121 if (pict == NULL) | |
122 goto fail; | |
123 s->last_picture_base[i] = pict; | |
124 s->last_picture[i] = pict + pict_start; | |
125 | |
126 pict = av_mallocz(c_size); | |
127 if (pict == NULL) | |
128 goto fail; | |
129 s->next_picture_base[i] = pict; | |
130 s->next_picture[i] = pict + pict_start; | |
131 | |
132 if (s->has_b_frames) { | |
133 pict = av_mallocz(c_size); | |
134 if (pict == NULL) | |
135 goto fail; | |
136 s->aux_picture_base[i] = pict; | |
137 s->aux_picture[i] = pict + pict_start; | |
138 } | |
139 } | |
140 | |
141 if (s->out_format == FMT_H263) { | |
142 int size; | |
143 /* MV prediction */ | |
144 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
145 s->motion_val = malloc(size * 2 * sizeof(INT16)); | |
146 if (s->motion_val == NULL) | |
147 goto fail; | |
148 memset(s->motion_val, 0, size * 2 * sizeof(INT16)); | |
149 } | |
150 | |
151 if (s->h263_pred) { | |
152 int y_size, c_size, i, size; | |
153 | |
154 /* dc values */ | |
155 | |
156 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
157 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
158 size = y_size + 2 * c_size; | |
159 s->dc_val[0] = malloc(size * sizeof(INT16)); | |
160 if (s->dc_val[0] == NULL) | |
161 goto fail; | |
162 s->dc_val[1] = s->dc_val[0] + y_size; | |
163 s->dc_val[2] = s->dc_val[1] + c_size; | |
164 for(i=0;i<size;i++) | |
165 s->dc_val[0][i] = 1024; | |
166 | |
167 /* ac values */ | |
168 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16); | |
169 if (s->ac_val[0] == NULL) | |
170 goto fail; | |
171 s->ac_val[1] = s->ac_val[0] + y_size; | |
172 s->ac_val[2] = s->ac_val[1] + c_size; | |
173 | |
174 /* cbp values */ | |
175 s->coded_block = av_mallocz(y_size); | |
176 if (!s->coded_block) | |
177 goto fail; | |
178 } | |
179 /* default structure is frame */ | |
180 s->picture_structure = PICT_FRAME; | |
181 | |
182 /* init default q matrix (only for mpeg and mjpeg) */ | |
183 for(i=0;i<64;i++) { | |
184 s->intra_matrix[i] = default_intra_matrix[i]; | |
185 s->chroma_intra_matrix[i] = default_intra_matrix[i]; | |
186 s->non_intra_matrix[i] = default_non_intra_matrix[i]; | |
187 s->chroma_non_intra_matrix[i] = default_non_intra_matrix[i]; | |
188 } | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
189 /* init macroblock skip table */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
190 if (!s->encoding) { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
191 s->mbskip_table = av_mallocz(s->mb_width * s->mb_height); |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
192 if (!s->mbskip_table) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
193 goto fail; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
194 } |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
195 |
0 | 196 s->context_initialized = 1; |
197 return 0; | |
198 fail: | |
199 if (s->motion_val) | |
200 free(s->motion_val); | |
201 if (s->dc_val[0]) | |
202 free(s->dc_val[0]); | |
203 if (s->ac_val[0]) | |
204 free(s->ac_val[0]); | |
205 if (s->coded_block) | |
206 free(s->coded_block); | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
207 if (s->mbskip_table) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
208 free(s->mbskip_table); |
0 | 209 for(i=0;i<3;i++) { |
210 if (s->last_picture_base[i]) | |
211 free(s->last_picture_base[i]); | |
212 if (s->next_picture_base[i]) | |
213 free(s->next_picture_base[i]); | |
214 if (s->aux_picture_base[i]) | |
215 free(s->aux_picture_base[i]); | |
216 } | |
217 return -1; | |
218 } | |
219 | |
220 /* init common structure for both encoder and decoder */ | |
221 void MPV_common_end(MpegEncContext *s) | |
222 { | |
223 int i; | |
224 | |
225 if (s->motion_val) | |
226 free(s->motion_val); | |
227 if (s->h263_pred) { | |
228 free(s->dc_val[0]); | |
229 free(s->ac_val[0]); | |
230 free(s->coded_block); | |
231 } | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
232 if (s->mbskip_table) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
233 free(s->mbskip_table); |
0 | 234 for(i=0;i<3;i++) { |
235 free(s->last_picture_base[i]); | |
236 free(s->next_picture_base[i]); | |
237 if (s->has_b_frames) | |
238 free(s->aux_picture_base[i]); | |
239 } | |
240 s->context_initialized = 0; | |
241 } | |
242 | |
243 /* init video encoder */ | |
244 int MPV_encode_init(AVCodecContext *avctx) | |
245 { | |
246 MpegEncContext *s = avctx->priv_data; | |
247 | |
248 s->bit_rate = avctx->bit_rate; | |
249 s->frame_rate = avctx->frame_rate; | |
250 s->width = avctx->width; | |
251 s->height = avctx->height; | |
252 s->gop_size = avctx->gop_size; | |
253 if (s->gop_size <= 1) { | |
254 s->intra_only = 1; | |
255 s->gop_size = 12; | |
256 } else { | |
257 s->intra_only = 0; | |
258 } | |
259 s->full_search = motion_estimation_method; | |
260 | |
261 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE); | |
262 | |
263 switch(avctx->codec->id) { | |
264 case CODEC_ID_MPEG1VIDEO: | |
265 s->out_format = FMT_MPEG1; | |
266 break; | |
267 case CODEC_ID_MJPEG: | |
268 s->out_format = FMT_MJPEG; | |
269 s->intra_only = 1; /* force intra only for jpeg */ | |
270 if (mjpeg_init(s) < 0) | |
271 return -1; | |
272 break; | |
273 case CODEC_ID_H263: | |
274 if (h263_get_picture_format(s->width, s->height) == 7) | |
275 return -1; | |
276 s->out_format = FMT_H263; | |
277 break; | |
278 case CODEC_ID_H263P: | |
279 s->out_format = FMT_H263; | |
280 s->h263_plus = 1; | |
281 /* XXX: not unrectricted mv yet */ | |
282 break; | |
283 case CODEC_ID_RV10: | |
284 s->out_format = FMT_H263; | |
285 s->h263_rv10 = 1; | |
286 break; | |
287 case CODEC_ID_OPENDIVX: | |
288 s->out_format = FMT_H263; | |
289 s->h263_pred = 1; | |
290 s->unrestricted_mv = 1; | |
291 break; | |
292 case CODEC_ID_MSMPEG4: | |
293 s->out_format = FMT_H263; | |
294 s->h263_msmpeg4 = 1; | |
295 s->h263_pred = 1; | |
296 s->unrestricted_mv = 1; | |
297 break; | |
298 default: | |
299 return -1; | |
300 } | |
301 | |
302 if (s->out_format == FMT_H263) | |
303 h263_encode_init_vlc(s); | |
304 | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
305 s->encoding = 1; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
306 |
0 | 307 /* init */ |
308 if (MPV_common_init(s) < 0) | |
309 return -1; | |
310 | |
311 /* rate control init */ | |
312 rate_control_init(s); | |
313 | |
314 s->picture_number = 0; | |
315 s->fake_picture_number = 0; | |
316 /* motion detector init */ | |
317 s->f_code = 1; | |
318 | |
319 return 0; | |
320 } | |
321 | |
322 int MPV_encode_end(AVCodecContext *avctx) | |
323 { | |
324 MpegEncContext *s = avctx->priv_data; | |
325 | |
326 #ifdef STATS | |
327 print_stats(); | |
328 #endif | |
329 MPV_common_end(s); | |
330 if (s->out_format == FMT_MJPEG) | |
331 mjpeg_close(s); | |
332 return 0; | |
333 } | |
334 | |
335 /* draw the edges of width 'w' of an image of size width, height */ | |
336 static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w) | |
337 { | |
338 UINT8 *ptr, *last_line; | |
339 int i; | |
340 | |
341 last_line = buf + (height - 1) * wrap; | |
342 for(i=0;i<w;i++) { | |
343 /* top and bottom */ | |
344 memcpy(buf - (i + 1) * wrap, buf, width); | |
345 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
346 } | |
347 /* left and right */ | |
348 ptr = buf; | |
349 for(i=0;i<height;i++) { | |
350 memset(ptr - w, ptr[0], w); | |
351 memset(ptr + width, ptr[width-1], w); | |
352 ptr += wrap; | |
353 } | |
354 /* corners */ | |
355 for(i=0;i<w;i++) { | |
356 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
357 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
358 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
359 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
360 } | |
361 } | |
362 | |
363 /* generic function for encode/decode called before a frame is coded/decoded */ | |
364 void MPV_frame_start(MpegEncContext *s) | |
365 { | |
366 int i; | |
367 UINT8 *tmp; | |
368 | |
369 if (s->pict_type == B_TYPE) { | |
370 for(i=0;i<3;i++) { | |
371 s->current_picture[i] = s->aux_picture[i]; | |
372 } | |
373 } else { | |
374 for(i=0;i<3;i++) { | |
375 /* swap next and last */ | |
376 tmp = s->last_picture[i]; | |
377 s->last_picture[i] = s->next_picture[i]; | |
378 s->next_picture[i] = tmp; | |
379 s->current_picture[i] = tmp; | |
380 } | |
381 } | |
382 } | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
383 |
0 | 384 /* generic function for encode/decode called after a frame has been coded/decoded */ |
385 void MPV_frame_end(MpegEncContext *s) | |
386 { | |
387 /* draw edge for correct motion prediction if outside */ | |
388 if (s->pict_type != B_TYPE) { | |
389 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH); | |
390 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
391 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
392 } | |
393 } | |
394 | |
395 int MPV_encode_picture(AVCodecContext *avctx, | |
396 unsigned char *buf, int buf_size, void *data) | |
397 { | |
398 MpegEncContext *s = avctx->priv_data; | |
399 AVPicture *pict = data; | |
400 int i, j; | |
401 | |
402 if (s->fixed_qscale) | |
403 s->qscale = avctx->quality; | |
404 | |
405 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
406 | |
407 if (!s->intra_only) { | |
408 /* first picture of GOP is intra */ | |
409 if ((s->picture_number % s->gop_size) == 0) | |
410 s->pict_type = I_TYPE; | |
411 else | |
412 s->pict_type = P_TYPE; | |
413 } else { | |
414 s->pict_type = I_TYPE; | |
415 } | |
416 avctx->key_frame = (s->pict_type == I_TYPE); | |
417 | |
418 MPV_frame_start(s); | |
419 | |
420 for(i=0;i<3;i++) { | |
421 UINT8 *src = pict->data[i]; | |
422 UINT8 *dest = s->current_picture[i]; | |
423 int src_wrap = pict->linesize[i]; | |
424 int dest_wrap = s->linesize; | |
425 int w = s->width; | |
426 int h = s->height; | |
427 | |
428 if (i >= 1) { | |
429 dest_wrap >>= 1; | |
430 w >>= 1; | |
431 h >>= 1; | |
432 } | |
433 | |
434 for(j=0;j<h;j++) { | |
435 memcpy(dest, src, w); | |
436 dest += dest_wrap; | |
437 src += src_wrap; | |
438 } | |
439 s->new_picture[i] = s->current_picture[i]; | |
440 } | |
441 | |
442 encode_picture(s, s->picture_number); | |
443 | |
444 MPV_frame_end(s); | |
445 s->picture_number++; | |
446 | |
447 if (s->out_format == FMT_MJPEG) | |
448 mjpeg_picture_trailer(s); | |
449 | |
450 flush_put_bits(&s->pb); | |
451 s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8; | |
452 avctx->quality = s->qscale; | |
453 return s->pb.buf_ptr - s->pb.buf; | |
454 } | |
455 | |
456 static inline int clip(int a, int amin, int amax) | |
457 { | |
458 if (a < amin) | |
459 return amin; | |
460 else if (a > amax) | |
461 return amax; | |
462 else | |
463 return a; | |
464 } | |
465 | |
466 /* apply one mpeg motion vector to the three components */ | |
467 static inline void mpeg_motion(MpegEncContext *s, | |
468 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
469 int dest_offset, | |
470 UINT8 **ref_picture, int src_offset, | |
471 int field_based, op_pixels_func *pix_op, | |
472 int motion_x, int motion_y, int h) | |
473 { | |
474 UINT8 *ptr; | |
475 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
476 | |
477 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
478 src_x = s->mb_x * 16 + (motion_x >> 1); | |
479 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
480 | |
481 /* WARNING: do no forget half pels */ | |
482 height = s->height >> field_based; | |
483 src_x = clip(src_x, -16, s->width); | |
484 if (src_x == s->width) | |
485 dxy &= ~1; | |
486 src_y = clip(src_y, -16, height); | |
487 if (src_y == height) | |
488 dxy &= ~2; | |
489 linesize = s->linesize << field_based; | |
490 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; | |
491 dest_y += dest_offset; | |
492 pix_op[dxy](dest_y, ptr, linesize, h); | |
493 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h); | |
494 | |
495 if (s->out_format == FMT_H263) { | |
496 dxy = 0; | |
497 if ((motion_x & 3) != 0) | |
498 dxy |= 1; | |
499 if ((motion_y & 3) != 0) | |
500 dxy |= 2; | |
501 mx = motion_x >> 2; | |
502 my = motion_y >> 2; | |
503 } else { | |
504 mx = motion_x / 2; | |
505 my = motion_y / 2; | |
506 dxy = ((my & 1) << 1) | (mx & 1); | |
507 mx >>= 1; | |
508 my >>= 1; | |
509 } | |
510 | |
511 src_x = s->mb_x * 8 + mx; | |
512 src_y = s->mb_y * (8 >> field_based) + my; | |
513 src_x = clip(src_x, -8, s->width >> 1); | |
514 if (src_x == (s->width >> 1)) | |
515 dxy &= ~1; | |
516 src_y = clip(src_y, -8, height >> 1); | |
517 if (src_y == (height >> 1)) | |
518 dxy &= ~2; | |
519 | |
520 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
521 ptr = ref_picture[1] + offset; | |
522 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
523 ptr = ref_picture[2] + offset; | |
524 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
525 } | |
526 | |
527 static inline void MPV_motion(MpegEncContext *s, | |
528 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
529 int dir, UINT8 **ref_picture, | |
530 op_pixels_func *pix_op) | |
531 { | |
532 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
533 int mb_x, mb_y, i; | |
534 UINT8 *ptr, *dest; | |
535 | |
536 mb_x = s->mb_x; | |
537 mb_y = s->mb_y; | |
538 | |
539 switch(s->mv_type) { | |
540 case MV_TYPE_16X16: | |
541 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
542 ref_picture, 0, | |
543 0, pix_op, | |
544 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
545 break; | |
546 case MV_TYPE_8X8: | |
547 for(i=0;i<4;i++) { | |
548 motion_x = s->mv[dir][i][0]; | |
549 motion_y = s->mv[dir][i][1]; | |
550 | |
551 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
552 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
553 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8; | |
554 | |
555 /* WARNING: do no forget half pels */ | |
556 src_x = clip(src_x, -16, s->width); | |
557 if (src_x == s->width) | |
558 dxy &= ~1; | |
559 src_y = clip(src_y, -16, s->height); | |
560 if (src_y == s->height) | |
561 dxy &= ~2; | |
562 | |
563 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
564 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
565 pix_op[dxy](dest, ptr, s->linesize, 8); | |
566 } | |
567 /* In case of 8X8, we construct a single chroma motion vector | |
568 with a special rounding */ | |
569 mx = 0; | |
570 my = 0; | |
571 for(i=0;i<4;i++) { | |
572 mx += s->mv[dir][i][0]; | |
573 my += s->mv[dir][i][1]; | |
574 } | |
575 if (mx >= 0) | |
576 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
577 else { | |
578 mx = -mx; | |
579 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
580 } | |
581 if (my >= 0) | |
582 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
583 else { | |
584 my = -my; | |
585 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
586 } | |
587 dxy = ((my & 1) << 1) | (mx & 1); | |
588 mx >>= 1; | |
589 my >>= 1; | |
590 | |
591 src_x = mb_x * 8 + mx; | |
592 src_y = mb_y * 8 + my; | |
593 src_x = clip(src_x, -8, s->width/2); | |
594 if (src_x == s->width/2) | |
595 dxy &= ~1; | |
596 src_y = clip(src_y, -8, s->height/2); | |
597 if (src_y == s->height/2) | |
598 dxy &= ~2; | |
599 | |
600 offset = (src_y * (s->linesize >> 1)) + src_x; | |
601 ptr = ref_picture[1] + offset; | |
602 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8); | |
603 ptr = ref_picture[2] + offset; | |
604 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8); | |
605 break; | |
606 case MV_TYPE_FIELD: | |
607 if (s->picture_structure == PICT_FRAME) { | |
608 /* top field */ | |
609 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
610 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
611 1, pix_op, | |
612 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
613 /* bottom field */ | |
614 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
615 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
616 1, pix_op, | |
617 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
618 } else { | |
619 | |
620 | |
621 } | |
622 break; | |
623 } | |
624 } | |
625 | |
626 | |
627 /* put block[] to dest[] */ | |
628 static inline void put_dct(MpegEncContext *s, | |
629 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
630 { | |
631 if (!s->mpeg2) | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
632 s->dct_unquantize(s, block, i, s->qscale); |
0 | 633 j_rev_dct (block); |
634 put_pixels_clamped(block, dest, line_size); | |
635 } | |
636 | |
637 /* add block[] to dest[] */ | |
638 static inline void add_dct(MpegEncContext *s, | |
639 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
640 { | |
641 if (s->block_last_index[i] >= 0) { | |
642 if (!s->mpeg2) | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
643 s->dct_unquantize(s, block, i, s->qscale); |
0 | 644 j_rev_dct (block); |
645 add_pixels_clamped(block, dest, line_size); | |
646 } | |
647 } | |
648 | |
649 /* generic function called after a macroblock has been parsed by the | |
650 decoder or after it has been encoded by the encoder. | |
651 | |
652 Important variables used: | |
653 s->mb_intra : true if intra macroblock | |
654 s->mv_dir : motion vector direction | |
655 s->mv_type : motion vector type | |
656 s->mv : motion vector | |
657 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
658 */ | |
659 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
660 { | |
661 int mb_x, mb_y, motion_x, motion_y; | |
662 int dct_linesize, dct_offset; | |
663 op_pixels_func *op_pix; | |
664 | |
665 mb_x = s->mb_x; | |
666 mb_y = s->mb_y; | |
667 | |
668 /* update DC predictors for P macroblocks */ | |
669 if (!s->mb_intra) { | |
670 if (s->h263_pred) { | |
671 int wrap, x, y, v; | |
672 wrap = 2 * s->mb_width + 2; | |
673 v = 1024; | |
674 x = 2 * mb_x + 1; | |
675 y = 2 * mb_y + 1; | |
676 s->dc_val[0][(x) + (y) * wrap] = v; | |
677 s->dc_val[0][(x + 1) + (y) * wrap] = v; | |
678 s->dc_val[0][(x) + (y + 1) * wrap] = v; | |
679 s->dc_val[0][(x + 1) + (y + 1) * wrap] = v; | |
680 /* ac pred */ | |
681 memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
682 memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
683 memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16)); | |
684 memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16)); | |
685 if (s->h263_msmpeg4) { | |
686 s->coded_block[(x) + (y) * wrap] = 0; | |
687 s->coded_block[(x + 1) + (y) * wrap] = 0; | |
688 s->coded_block[(x) + (y + 1) * wrap] = 0; | |
689 s->coded_block[(x + 1) + (y + 1) * wrap] = 0; | |
690 } | |
691 /* chroma */ | |
692 wrap = s->mb_width + 2; | |
693 x = mb_x + 1; | |
694 y = mb_y + 1; | |
695 s->dc_val[1][(x) + (y) * wrap] = v; | |
696 s->dc_val[2][(x) + (y) * wrap] = v; | |
697 /* ac pred */ | |
698 memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
699 memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
700 } else { | |
701 s->last_dc[0] = 128 << s->intra_dc_precision; | |
702 s->last_dc[1] = 128 << s->intra_dc_precision; | |
703 s->last_dc[2] = 128 << s->intra_dc_precision; | |
704 } | |
705 } | |
706 | |
707 /* update motion predictor */ | |
708 if (s->out_format == FMT_H263) { | |
709 int x, y, wrap; | |
710 | |
711 x = 2 * mb_x + 1; | |
712 y = 2 * mb_y + 1; | |
713 wrap = 2 * s->mb_width + 2; | |
714 if (s->mb_intra) { | |
715 motion_x = 0; | |
716 motion_y = 0; | |
717 goto motion_init; | |
718 } else if (s->mv_type == MV_TYPE_16X16) { | |
719 motion_x = s->mv[0][0][0]; | |
720 motion_y = s->mv[0][0][1]; | |
721 motion_init: | |
722 /* no update if 8X8 because it has been done during parsing */ | |
723 s->motion_val[(x) + (y) * wrap][0] = motion_x; | |
724 s->motion_val[(x) + (y) * wrap][1] = motion_y; | |
725 s->motion_val[(x + 1) + (y) * wrap][0] = motion_x; | |
726 s->motion_val[(x + 1) + (y) * wrap][1] = motion_y; | |
727 s->motion_val[(x) + (y + 1) * wrap][0] = motion_x; | |
728 s->motion_val[(x) + (y + 1) * wrap][1] = motion_y; | |
729 s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x; | |
730 s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y; | |
731 } | |
732 } | |
733 | |
734 if (!s->intra_only) { | |
735 UINT8 *dest_y, *dest_cb, *dest_cr; | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
736 UINT8 *mbskip_ptr; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
737 |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
738 /* avoid copy if macroblock skipped in last frame too */ |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
739 if (!s->encoding && s->pict_type != B_TYPE) { |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
740 mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
741 if (s->mb_skiped) { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
742 s->mb_skiped = 0; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
743 /* if previous was skipped too, then nothing to do ! */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
744 if (*mbskip_ptr != 0) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
745 goto the_end; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
746 *mbskip_ptr = 1; /* indicate that this time we skiped it */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
747 } else { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
748 *mbskip_ptr = 0; /* not skipped */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
749 } |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
750 } |
0 | 751 |
752 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16; | |
753 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
754 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
755 | |
756 if (s->interlaced_dct) { | |
757 dct_linesize = s->linesize * 2; | |
758 dct_offset = s->linesize; | |
759 } else { | |
760 dct_linesize = s->linesize; | |
761 dct_offset = s->linesize * 8; | |
762 } | |
763 | |
764 if (!s->mb_intra) { | |
765 /* motion handling */ | |
766 if (!s->no_rounding) | |
767 op_pix = put_pixels_tab; | |
768 else | |
769 op_pix = put_no_rnd_pixels_tab; | |
770 | |
771 if (s->mv_dir & MV_DIR_FORWARD) { | |
772 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix); | |
773 if (!s->no_rounding) | |
774 op_pix = avg_pixels_tab; | |
775 else | |
776 op_pix = avg_no_rnd_pixels_tab; | |
777 } | |
778 if (s->mv_dir & MV_DIR_BACKWARD) { | |
779 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix); | |
780 } | |
781 | |
782 /* add dct residue */ | |
783 add_dct(s, block[0], 0, dest_y, dct_linesize); | |
784 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
785 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
786 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
787 | |
788 add_dct(s, block[4], 4, dest_cb, dct_linesize >> 1); | |
789 add_dct(s, block[5], 5, dest_cr, dct_linesize >> 1); | |
790 } else { | |
791 /* dct only in intra block */ | |
792 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
793 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
794 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
795 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
796 | |
797 put_dct(s, block[4], 4, dest_cb, dct_linesize >> 1); | |
798 put_dct(s, block[5], 5, dest_cr, dct_linesize >> 1); | |
799 } | |
800 } | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
801 the_end: |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
802 emms_c(); |
0 | 803 } |
804 | |
805 static void encode_picture(MpegEncContext *s, int picture_number) | |
806 { | |
807 int mb_x, mb_y, wrap; | |
808 UINT8 *ptr; | |
809 int i, motion_x, motion_y; | |
810 | |
811 s->picture_number = picture_number; | |
812 if (!s->fixed_qscale) | |
813 s->qscale = rate_estimate_qscale(s); | |
814 | |
815 /* precompute matrix */ | |
816 if (s->out_format == FMT_MJPEG) { | |
817 /* for mjpeg, we do include qscale in the matrix */ | |
818 s->intra_matrix[0] = default_intra_matrix[0]; | |
819 for(i=1;i<64;i++) | |
820 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3; | |
821 convert_matrix(s->q_intra_matrix, s->intra_matrix, 8); | |
822 } else { | |
823 convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale); | |
824 convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale); | |
825 } | |
826 | |
827 switch(s->out_format) { | |
828 case FMT_MJPEG: | |
829 mjpeg_picture_header(s); | |
830 break; | |
831 case FMT_H263: | |
832 if (s->h263_msmpeg4) | |
833 msmpeg4_encode_picture_header(s, picture_number); | |
834 else if (s->h263_pred) | |
835 mpeg4_encode_picture_header(s, picture_number); | |
836 else if (s->h263_rv10) | |
837 rv10_encode_picture_header(s, picture_number); | |
838 else | |
839 h263_encode_picture_header(s, picture_number); | |
840 break; | |
841 case FMT_MPEG1: | |
842 mpeg1_encode_picture_header(s, picture_number); | |
843 break; | |
844 } | |
845 | |
846 /* init last dc values */ | |
847 /* note: quant matrix value (8) is implied here */ | |
848 s->last_dc[0] = 128; | |
849 s->last_dc[1] = 128; | |
850 s->last_dc[2] = 128; | |
851 s->mb_incr = 1; | |
852 s->last_mv[0][0][0] = 0; | |
853 s->last_mv[0][0][1] = 0; | |
854 s->mv_type = MV_TYPE_16X16; | |
855 s->mv_dir = MV_DIR_FORWARD; | |
856 | |
857 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | |
858 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
859 | |
860 s->mb_x = mb_x; | |
861 s->mb_y = mb_y; | |
862 | |
863 /* compute motion vector and macro block type (intra or non intra) */ | |
864 motion_x = 0; | |
865 motion_y = 0; | |
866 if (s->pict_type == P_TYPE) { | |
867 s->mb_intra = estimate_motion(s, mb_x, mb_y, | |
868 &motion_x, | |
869 &motion_y); | |
870 } else { | |
871 s->mb_intra = 1; | |
872 } | |
873 | |
874 /* get the pixels */ | |
875 wrap = s->linesize; | |
876 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
877 get_pixels(s->block[0], ptr, wrap); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
878 get_pixels(s->block[1], ptr + 8, wrap); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
879 get_pixels(s->block[2], ptr + 8 * wrap, wrap); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
880 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); |
0 | 881 wrap = s->linesize >> 1; |
882 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8; | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
883 get_pixels(s->block[4], ptr, wrap); |
0 | 884 |
885 wrap = s->linesize >> 1; | |
886 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8; | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
887 get_pixels(s->block[5], ptr, wrap); |
0 | 888 |
889 /* subtract previous frame if non intra */ | |
890 if (!s->mb_intra) { | |
891 int dxy, offset, mx, my; | |
892 | |
893 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
894 ptr = s->last_picture[0] + | |
895 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + | |
896 (mb_x * 16 + (motion_x >> 1)); | |
897 | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
898 sub_pixels_2(s->block[0], ptr, s->linesize, dxy); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
899 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
900 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
901 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy); |
0 | 902 |
903 if (s->out_format == FMT_H263) { | |
904 /* special rounding for h263 */ | |
905 dxy = 0; | |
906 if ((motion_x & 3) != 0) | |
907 dxy |= 1; | |
908 if ((motion_y & 3) != 0) | |
909 dxy |= 2; | |
910 mx = motion_x >> 2; | |
911 my = motion_y >> 2; | |
912 } else { | |
913 mx = motion_x / 2; | |
914 my = motion_y / 2; | |
915 dxy = ((my & 1) << 1) | (mx & 1); | |
916 mx >>= 1; | |
917 my >>= 1; | |
918 } | |
919 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx); | |
920 ptr = s->last_picture[1] + offset; | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
921 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy); |
0 | 922 ptr = s->last_picture[2] + offset; |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
923 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy); |
0 | 924 } |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
925 emms_c(); |
0 | 926 |
927 /* DCT & quantize */ | |
928 if (s->h263_msmpeg4) { | |
929 msmpeg4_dc_scale(s); | |
930 } else if (s->h263_pred) { | |
931 h263_dc_scale(s); | |
932 } else { | |
933 /* default quantization values */ | |
934 s->y_dc_scale = 8; | |
935 s->c_dc_scale = 8; | |
936 } | |
937 | |
938 for(i=0;i<6;i++) { | |
939 int last_index; | |
940 if (av_fdct == jpeg_fdct_ifast) | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
941 last_index = dct_quantize(s, s->block[i], i, s->qscale); |
0 | 942 else |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
943 last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale); |
0 | 944 s->block_last_index[i] = last_index; |
945 } | |
946 | |
947 /* huffman encode */ | |
948 switch(s->out_format) { | |
949 case FMT_MPEG1: | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
950 mpeg1_encode_mb(s, s->block, motion_x, motion_y); |
0 | 951 break; |
952 case FMT_H263: | |
953 if (s->h263_msmpeg4) | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
954 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); |
0 | 955 else |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
956 h263_encode_mb(s, s->block, motion_x, motion_y); |
0 | 957 break; |
958 case FMT_MJPEG: | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
959 mjpeg_encode_mb(s, s->block); |
0 | 960 break; |
961 } | |
962 | |
963 /* decompress blocks so that we keep the state of the decoder */ | |
964 s->mv[0][0][0] = motion_x; | |
965 s->mv[0][0][1] = motion_y; | |
966 | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
967 MPV_decode_mb(s, s->block); |
0 | 968 } |
969 } | |
970 } | |
971 | |
972 static int dct_quantize(MpegEncContext *s, | |
973 DCTELEM *block, int n, | |
974 int qscale) | |
975 { | |
976 int i, j, level, last_non_zero, q; | |
977 const int *qmat; | |
978 | |
979 av_fdct (block); | |
980 | |
981 if (s->mb_intra) { | |
982 if (n < 4) | |
983 q = s->y_dc_scale; | |
984 else | |
985 q = s->c_dc_scale; | |
986 q = q << 3; | |
987 | |
988 /* note: block[0] is assumed to be positive */ | |
989 block[0] = (block[0] + (q >> 1)) / q; | |
990 i = 1; | |
991 last_non_zero = 0; | |
992 if (s->out_format == FMT_H263) { | |
993 qmat = s->q_non_intra_matrix; | |
994 } else { | |
995 qmat = s->q_intra_matrix; | |
996 } | |
997 } else { | |
998 i = 0; | |
999 last_non_zero = -1; | |
1000 qmat = s->q_non_intra_matrix; | |
1001 } | |
1002 | |
1003 for(;i<64;i++) { | |
1004 j = zigzag_direct[i]; | |
1005 level = block[j]; | |
1006 level = level * qmat[j]; | |
1007 #ifdef PARANOID | |
1008 { | |
1009 static int count = 0; | |
1010 int level1, level2, qmat1; | |
1011 double val; | |
1012 if (qmat == s->q_non_intra_matrix) { | |
1013 qmat1 = default_non_intra_matrix[j] * s->qscale; | |
1014 } else { | |
1015 qmat1 = default_intra_matrix[j] * s->qscale; | |
1016 } | |
1017 if (av_fdct != jpeg_fdct_ifast) | |
1018 val = ((double)block[j] * 8.0) / (double)qmat1; | |
1019 else | |
1020 val = ((double)block[j] * 8.0 * 2048.0) / | |
1021 ((double)qmat1 * aanscales[j]); | |
1022 level1 = (int)val; | |
1023 level2 = level / (1 << (QMAT_SHIFT - 3)); | |
1024 if (level1 != level2) { | |
1025 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", | |
1026 count, level2, level1, block[j], qmat1, qmat[j], | |
1027 val); | |
1028 count++; | |
1029 } | |
1030 | |
1031 } | |
1032 #endif | |
1033 /* XXX: slight error for the low range. Test should be equivalent to | |
1034 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 << | |
1035 (QMAT_SHIFT - 3))) | |
1036 */ | |
1037 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != | |
1038 level) { | |
1039 level = level / (1 << (QMAT_SHIFT - 3)); | |
1040 /* XXX: currently, this code is not optimal. the range should be: | |
1041 mpeg1: -255..255 | |
1042 mpeg2: -2048..2047 | |
1043 h263: -128..127 | |
1044 mpeg4: -2048..2047 | |
1045 */ | |
1046 if (level > 127) | |
1047 level = 127; | |
1048 else if (level < -128) | |
1049 level = -128; | |
1050 block[j] = level; | |
1051 last_non_zero = i; | |
1052 } else { | |
1053 block[j] = 0; | |
1054 } | |
1055 } | |
1056 return last_non_zero; | |
1057 } | |
1058 | |
1059 static int dct_quantize_mmx(MpegEncContext *s, | |
1060 DCTELEM *block, int n, | |
1061 int qscale) | |
1062 { | |
1063 int i, j, level, last_non_zero, q; | |
1064 const int *qmat; | |
1065 | |
1066 av_fdct (block); | |
1067 | |
1068 if (s->mb_intra) { | |
1069 if (n < 4) | |
1070 q = s->y_dc_scale; | |
1071 else | |
1072 q = s->c_dc_scale; | |
1073 | |
1074 /* note: block[0] is assumed to be positive */ | |
1075 block[0] = (block[0] + (q >> 1)) / q; | |
1076 i = 1; | |
1077 last_non_zero = 0; | |
1078 if (s->out_format == FMT_H263) { | |
1079 qmat = s->q_non_intra_matrix; | |
1080 } else { | |
1081 qmat = s->q_intra_matrix; | |
1082 } | |
1083 } else { | |
1084 i = 0; | |
1085 last_non_zero = -1; | |
1086 qmat = s->q_non_intra_matrix; | |
1087 } | |
1088 | |
1089 for(;i<64;i++) { | |
1090 j = zigzag_direct[i]; | |
1091 level = block[j]; | |
1092 level = level * qmat[j]; | |
1093 /* XXX: slight error for the low range. Test should be equivalent to | |
1094 (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 << | |
1095 (QMAT_SHIFT_MMX - 3))) | |
1096 */ | |
1097 if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) != | |
1098 level) { | |
1099 level = level / (1 << (QMAT_SHIFT_MMX - 3)); | |
1100 /* XXX: currently, this code is not optimal. the range should be: | |
1101 mpeg1: -255..255 | |
1102 mpeg2: -2048..2047 | |
1103 h263: -128..127 | |
1104 mpeg4: -2048..2047 | |
1105 */ | |
1106 if (level > 127) | |
1107 level = 127; | |
1108 else if (level < -128) | |
1109 level = -128; | |
1110 block[j] = level; | |
1111 last_non_zero = i; | |
1112 } else { | |
1113 block[j] = 0; | |
1114 } | |
1115 } | |
1116 return last_non_zero; | |
1117 } | |
1118 | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1119 static void dct_unquantize_mpeg1_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1120 DCTELEM *block, int n, int qscale) |
0 | 1121 { |
1122 int i, level; | |
1123 const UINT16 *quant_matrix; | |
1124 | |
1125 if (s->mb_intra) { | |
1126 if (n < 4) | |
1127 block[0] = block[0] * s->y_dc_scale; | |
1128 else | |
1129 block[0] = block[0] * s->c_dc_scale; | |
1130 /* XXX: only mpeg1 */ | |
1131 quant_matrix = s->intra_matrix; | |
1132 for(i=1;i<64;i++) { | |
1133 level = block[i]; | |
1134 if (level) { | |
1135 if (level < 0) { | |
1136 level = -level; | |
1137 level = (int)(level * qscale * quant_matrix[i]) >> 3; | |
1138 level = (level - 1) | 1; | |
1139 level = -level; | |
1140 } else { | |
1141 level = (int)(level * qscale * quant_matrix[i]) >> 3; | |
1142 level = (level - 1) | 1; | |
1143 } | |
1144 #ifdef PARANOID | |
1145 if (level < -2048 || level > 2047) | |
1146 fprintf(stderr, "unquant error %d %d\n", i, level); | |
1147 #endif | |
1148 block[i] = level; | |
1149 } | |
1150 } | |
1151 } else { | |
1152 i = 0; | |
1153 quant_matrix = s->non_intra_matrix; | |
1154 for(;i<64;i++) { | |
1155 level = block[i]; | |
1156 if (level) { | |
1157 if (level < 0) { | |
1158 level = -level; | |
1159 level = (((level << 1) + 1) * qscale * | |
1160 ((int) (quant_matrix[i]))) >> 4; | |
1161 level = (level - 1) | 1; | |
1162 level = -level; | |
1163 } else { | |
1164 level = (((level << 1) + 1) * qscale * | |
1165 ((int) (quant_matrix[i]))) >> 4; | |
1166 level = (level - 1) | 1; | |
1167 } | |
1168 #ifdef PARANOID | |
1169 if (level < -2048 || level > 2047) | |
1170 fprintf(stderr, "unquant error %d %d\n", i, level); | |
1171 #endif | |
1172 block[i] = level; | |
1173 } | |
1174 } | |
1175 } | |
1176 } | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1177 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1178 static void dct_unquantize_h263_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1179 DCTELEM *block, int n, int qscale) |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1180 { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1181 int i, level, qmul, qadd; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1182 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1183 if (s->mb_intra) { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1184 if (n < 4) |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1185 block[0] = block[0] * s->y_dc_scale; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1186 else |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1187 block[0] = block[0] * s->c_dc_scale; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1188 i = 1; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1189 } else { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1190 i = 0; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1191 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1192 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1193 qmul = s->qscale << 1; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1194 qadd = (s->qscale - 1) | 1; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1195 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1196 for(;i<64;i++) { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1197 level = block[i]; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1198 if (level) { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1199 if (level < 0) { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1200 level = level * qmul - qadd; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1201 } else { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1202 level = level * qmul + qadd; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1203 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1204 #ifdef PARANOID |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1205 if (level < -2048 || level > 2047) |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1206 fprintf(stderr, "unquant error %d %d\n", i, level); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1207 #endif |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1208 block[i] = level; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1209 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1210 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1211 } |
0 | 1212 |
1213 /* rate control */ | |
1214 | |
1215 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */ | |
1216 #define I_FRAME_SIZE_RATIO 3.0 | |
1217 #define QSCALE_K 20 | |
1218 | |
1219 static void rate_control_init(MpegEncContext *s) | |
1220 { | |
1221 s->wanted_bits = 0; | |
1222 | |
1223 if (s->intra_only) { | |
1224 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate; | |
1225 s->P_frame_bits = s->I_frame_bits; | |
1226 } else { | |
1227 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / | |
1228 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1))); | |
1229 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO); | |
1230 } | |
1231 | |
1232 #if defined(DEBUG) | |
1233 printf("I_frame_size=%d P_frame_size=%d\n", | |
1234 s->I_frame_bits, s->P_frame_bits); | |
1235 #endif | |
1236 } | |
1237 | |
1238 | |
1239 /* | |
1240 * This heuristic is rather poor, but at least we do not have to | |
1241 * change the qscale at every macroblock. | |
1242 */ | |
1243 static int rate_estimate_qscale(MpegEncContext *s) | |
1244 { | |
1245 long long total_bits = s->total_bits; | |
1246 float q; | |
1247 int qscale, diff, qmin; | |
1248 | |
1249 if (s->pict_type == I_TYPE) { | |
1250 s->wanted_bits += s->I_frame_bits; | |
1251 } else { | |
1252 s->wanted_bits += s->P_frame_bits; | |
1253 } | |
1254 diff = s->wanted_bits - total_bits; | |
1255 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width); | |
1256 /* adjust for I frame */ | |
1257 if (s->pict_type == I_TYPE && !s->intra_only) { | |
1258 q /= I_FRAME_SIZE_RATIO; | |
1259 } | |
1260 | |
1261 /* using a too small Q scale leeds to problems in mpeg1 and h263 | |
1262 because AC coefficients are clamped to 255 or 127 */ | |
1263 qmin = 3; | |
1264 if (q < qmin) | |
1265 q = qmin; | |
1266 else if (q > 31) | |
1267 q = 31; | |
1268 qscale = (int)(q + 0.5); | |
1269 #if defined(DEBUG) | |
1270 printf("%d: total=%Ld br=%0.1f diff=%d qest=%0.1f\n", | |
1271 s->picture_number, | |
1272 total_bits, | |
1273 (float)s->frame_rate / FRAME_RATE_BASE * | |
1274 total_bits / s->picture_number, | |
1275 diff, q); | |
1276 #endif | |
1277 return qscale; | |
1278 } | |
1279 | |
1280 AVCodec mpeg1video_encoder = { | |
1281 "mpeg1video", | |
1282 CODEC_TYPE_VIDEO, | |
1283 CODEC_ID_MPEG1VIDEO, | |
1284 sizeof(MpegEncContext), | |
1285 MPV_encode_init, | |
1286 MPV_encode_picture, | |
1287 MPV_encode_end, | |
1288 }; | |
1289 | |
1290 AVCodec h263_encoder = { | |
1291 "h263", | |
1292 CODEC_TYPE_VIDEO, | |
1293 CODEC_ID_H263, | |
1294 sizeof(MpegEncContext), | |
1295 MPV_encode_init, | |
1296 MPV_encode_picture, | |
1297 MPV_encode_end, | |
1298 }; | |
1299 | |
1300 AVCodec h263p_encoder = { | |
1301 "h263p", | |
1302 CODEC_TYPE_VIDEO, | |
1303 CODEC_ID_H263P, | |
1304 sizeof(MpegEncContext), | |
1305 MPV_encode_init, | |
1306 MPV_encode_picture, | |
1307 MPV_encode_end, | |
1308 }; | |
1309 | |
1310 AVCodec rv10_encoder = { | |
1311 "rv10", | |
1312 CODEC_TYPE_VIDEO, | |
1313 CODEC_ID_RV10, | |
1314 sizeof(MpegEncContext), | |
1315 MPV_encode_init, | |
1316 MPV_encode_picture, | |
1317 MPV_encode_end, | |
1318 }; | |
1319 | |
1320 AVCodec mjpeg_encoder = { | |
1321 "mjpeg", | |
1322 CODEC_TYPE_VIDEO, | |
1323 CODEC_ID_MJPEG, | |
1324 sizeof(MpegEncContext), | |
1325 MPV_encode_init, | |
1326 MPV_encode_picture, | |
1327 MPV_encode_end, | |
1328 }; | |
1329 | |
1330 AVCodec opendivx_encoder = { | |
1331 "opendivx", | |
1332 CODEC_TYPE_VIDEO, | |
1333 CODEC_ID_OPENDIVX, | |
1334 sizeof(MpegEncContext), | |
1335 MPV_encode_init, | |
1336 MPV_encode_picture, | |
1337 MPV_encode_end, | |
1338 }; | |
1339 | |
1340 AVCodec msmpeg4_encoder = { | |
1341 "msmpeg4", | |
1342 CODEC_TYPE_VIDEO, | |
1343 CODEC_ID_MSMPEG4, | |
1344 sizeof(MpegEncContext), | |
1345 MPV_encode_init, | |
1346 MPV_encode_picture, | |
1347 MPV_encode_end, | |
1348 }; |