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