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