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