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