Mercurial > libavcodec.hg
annotate mpegvideo.c @ 288:f82cce6cb182 libavcodec
10l (motion_val was uninitilized)
20l (first frame of a gop could be a p-frame sometimes)
add hq flag to MpegEncContext
author | michaelni |
---|---|
date | Sun, 24 Mar 2002 04:16:51 +0000 |
parents | 32a3dcce2e9b |
children | 73a9ce3d9715 |
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 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
70 static UINT16 default_mv_penalty[MAX_FCODE][MAX_MV*2+1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
71 static UINT8 default_fcode_tab[MAX_MV*2+1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
72 |
0 | 73 /* default motion estimation */ |
74 int motion_estimation_method = ME_LOG; | |
75 | |
200 | 76 extern UINT8 zigzag_end[64]; |
77 | |
220 | 78 static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale) |
0 | 79 { |
80 int i; | |
81 | |
82 if (av_fdct == jpeg_fdct_ifast) { | |
83 for(i=0;i<64;i++) { | |
84 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
220 | 85 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ |
86 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ | |
87 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ | |
0 | 88 |
220 | 89 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / |
90 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)])); | |
0 | 91 } |
92 } else { | |
93 for(i=0;i<64;i++) { | |
94 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
220 | 95 So 16 <= qscale * quant_matrix[i] <= 7905 |
96 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905 | |
97 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 | |
0 | 98 */ |
220 | 99 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); |
100 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]); | |
0 | 101 } |
102 } | |
103 } | |
104 | |
105 /* init common structure for both encoder and decoder */ | |
106 int MPV_common_init(MpegEncContext *s) | |
107 { | |
108 int c_size, i; | |
109 UINT8 *pict; | |
110 | |
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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 |
174ef88f619a
use block[] in structure to have it 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 #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
|
117 MPV_common_init_mmx(s); |
8 | 118 #endif |
0 | 119 s->mb_width = (s->width + 15) / 16; |
120 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
|
121 s->mb_num = s->mb_width * s->mb_height; |
0 | 122 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH; |
123 | |
124 for(i=0;i<3;i++) { | |
125 int w, h, shift, pict_start; | |
126 | |
127 w = s->linesize; | |
128 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | |
129 shift = (i == 0) ? 0 : 1; | |
130 c_size = (w >> shift) * (h >> shift); | |
131 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | |
132 | |
133 pict = av_mallocz(c_size); | |
134 if (pict == NULL) | |
135 goto fail; | |
136 s->last_picture_base[i] = pict; | |
137 s->last_picture[i] = pict + pict_start; | |
138 | |
139 pict = av_mallocz(c_size); | |
140 if (pict == NULL) | |
141 goto fail; | |
142 s->next_picture_base[i] = pict; | |
143 s->next_picture[i] = pict + pict_start; | |
144 | |
145 if (s->has_b_frames) { | |
146 pict = av_mallocz(c_size); | |
147 if (pict == NULL) | |
148 goto fail; | |
149 s->aux_picture_base[i] = pict; | |
150 s->aux_picture[i] = pict + pict_start; | |
151 } | |
152 } | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
153 |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
154 if (s->encoding) { |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
155 /* Allocate MB type table */ |
239 | 156 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
|
157 if (s->mb_type == NULL) { |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
158 perror("malloc"); |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
159 goto fail; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
160 } |
239 | 161 |
162 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16)); | |
163 if (s->mb_var == NULL) { | |
164 perror("malloc"); | |
165 goto fail; | |
166 } | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
167 /* Allocate MV table */ |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
168 /* By now we just have one MV per MB */ |
239 | 169 s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16)); |
170 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
|
171 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
|
172 perror("malloc"); |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
173 goto fail; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
174 } |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
175 } |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
176 |
288 | 177 if (s->out_format == FMT_H263 || s->encoding) { |
0 | 178 int size; |
179 /* MV prediction */ | |
180 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
181 s->motion_val = malloc(size * 2 * sizeof(INT16)); | |
182 if (s->motion_val == NULL) | |
183 goto fail; | |
184 memset(s->motion_val, 0, size * 2 * sizeof(INT16)); | |
185 } | |
186 | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
187 if (s->h263_pred || s->h263_plus) { |
0 | 188 int y_size, c_size, i, size; |
189 | |
190 /* dc values */ | |
191 | |
192 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
193 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
194 size = y_size + 2 * c_size; | |
195 s->dc_val[0] = malloc(size * sizeof(INT16)); | |
196 if (s->dc_val[0] == NULL) | |
197 goto fail; | |
198 s->dc_val[1] = s->dc_val[0] + y_size; | |
199 s->dc_val[2] = s->dc_val[1] + c_size; | |
200 for(i=0;i<size;i++) | |
201 s->dc_val[0][i] = 1024; | |
202 | |
203 /* ac values */ | |
204 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16); | |
205 if (s->ac_val[0] == NULL) | |
206 goto fail; | |
207 s->ac_val[1] = s->ac_val[0] + y_size; | |
208 s->ac_val[2] = s->ac_val[1] + c_size; | |
209 | |
210 /* cbp values */ | |
211 s->coded_block = av_mallocz(y_size); | |
212 if (!s->coded_block) | |
213 goto fail; | |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
214 |
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
215 /* 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
|
216 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
|
217 if (!s->mbintra_table) |
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
218 goto fail; |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
219 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
|
220 } |
0 | 221 /* default structure is frame */ |
222 s->picture_structure = PICT_FRAME; | |
223 | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
224 /* init macroblock skip table */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
225 if (!s->encoding) { |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
226 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
|
227 if (!s->mbskip_table) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
228 goto fail; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
229 } |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
230 |
0 | 231 s->context_initialized = 1; |
232 return 0; | |
233 fail: | |
244 | 234 MPV_common_end(s); |
235 return -1; | |
236 } | |
237 | |
238 /* init common structure for both encoder and decoder */ | |
239 void MPV_common_end(MpegEncContext *s) | |
240 { | |
241 int i; | |
242 | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
243 if (s->mb_type) |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
244 free(s->mb_type); |
239 | 245 if (s->mb_var) |
246 free(s->mb_var); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
247 if (s->mv_table[0]) |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
248 free(s->mv_table[0]); |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
249 if (s->mv_table[1]) |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
250 free(s->mv_table[1]); |
0 | 251 if (s->motion_val) |
252 free(s->motion_val); | |
253 if (s->dc_val[0]) | |
254 free(s->dc_val[0]); | |
255 if (s->ac_val[0]) | |
256 free(s->ac_val[0]); | |
257 if (s->coded_block) | |
258 free(s->coded_block); | |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
259 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
|
260 free(s->mbintra_table); |
244 | 261 |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
262 if (s->mbskip_table) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
263 free(s->mbskip_table); |
0 | 264 for(i=0;i<3;i++) { |
265 if (s->last_picture_base[i]) | |
244 | 266 free(s->last_picture_base[i]); |
267 if (s->next_picture_base[i]) | |
268 free(s->next_picture_base[i]); | |
0 | 269 if (s->has_b_frames) |
270 free(s->aux_picture_base[i]); | |
271 } | |
272 s->context_initialized = 0; | |
273 } | |
274 | |
275 /* init video encoder */ | |
276 int MPV_encode_init(AVCodecContext *avctx) | |
277 { | |
278 MpegEncContext *s = avctx->priv_data; | |
60 | 279 int i; |
0 | 280 |
281 s->bit_rate = avctx->bit_rate; | |
268 | 282 s->bit_rate_tolerance = avctx->bit_rate_tolerance; |
0 | 283 s->frame_rate = avctx->frame_rate; |
284 s->width = avctx->width; | |
285 s->height = avctx->height; | |
286 s->gop_size = avctx->gop_size; | |
162 | 287 s->rtp_mode = avctx->rtp_mode; |
288 s->rtp_payload_size = avctx->rtp_payload_size; | |
231 | 289 if (avctx->rtp_callback) |
290 s->rtp_callback = avctx->rtp_callback; | |
268 | 291 s->qmin= avctx->qmin; |
292 s->qmax= avctx->qmax; | |
293 s->max_qdiff= avctx->max_qdiff; | |
294 s->qcompress= avctx->qcompress; | |
295 s->qblur= avctx->qblur; | |
194
27d1773552c9
mpeg4 encoder fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
191
diff
changeset
|
296 s->avctx = avctx; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
297 s->aspect_ratio_info= avctx->aspect_ratio_info; |
288 | 298 s->hq= (avctx->flags & CODEC_FLAG_HQ); |
162 | 299 |
0 | 300 if (s->gop_size <= 1) { |
301 s->intra_only = 1; | |
302 s->gop_size = 12; | |
303 } else { | |
304 s->intra_only = 0; | |
305 } | |
306 s->full_search = motion_estimation_method; | |
307 | |
308 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
|
309 |
0 | 310 switch(avctx->codec->id) { |
311 case CODEC_ID_MPEG1VIDEO: | |
312 s->out_format = FMT_MPEG1; | |
313 break; | |
314 case CODEC_ID_MJPEG: | |
315 s->out_format = FMT_MJPEG; | |
316 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
|
317 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
|
318 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
|
319 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
|
320 s->mjpeg_vsample[2] = 1; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
321 s->mjpeg_hsample[0] = 2; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
322 s->mjpeg_hsample[1] = 1; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
323 s->mjpeg_hsample[2] = 1; |
0 | 324 if (mjpeg_init(s) < 0) |
325 return -1; | |
326 break; | |
327 case CODEC_ID_H263: | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
328 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
|
329 printf("Input picture size isn't suitable for h263 codec! try h263+\n"); |
0 | 330 return -1; |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
331 } |
0 | 332 s->out_format = FMT_H263; |
333 break; | |
334 case CODEC_ID_H263P: | |
335 s->out_format = FMT_H263; | |
162 | 336 s->rtp_mode = 1; |
337 s->rtp_payload_size = 1200; | |
0 | 338 s->h263_plus = 1; |
78 | 339 s->unrestricted_mv = 1; |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
340 |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
341 /* These are just to be sure */ |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
342 s->umvplus = 0; |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
343 s->umvplus_dec = 0; |
0 | 344 break; |
345 case CODEC_ID_RV10: | |
346 s->out_format = FMT_H263; | |
347 s->h263_rv10 = 1; | |
348 break; | |
71 | 349 case CODEC_ID_MPEG4: |
0 | 350 s->out_format = FMT_H263; |
351 s->h263_pred = 1; | |
352 s->unrestricted_mv = 1; | |
353 break; | |
354 case CODEC_ID_MSMPEG4: | |
355 s->out_format = FMT_H263; | |
356 s->h263_msmpeg4 = 1; | |
357 s->h263_pred = 1; | |
358 s->unrestricted_mv = 1; | |
359 break; | |
360 default: | |
361 return -1; | |
362 } | |
363 | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
364 { /* set up some save defaults, some codecs might override them later */ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
365 static int done=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
366 if(!done){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
367 int i; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
368 done=1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
369 memset(default_mv_penalty, 0, sizeof(UINT16)*MAX_FCODE*(2*MAX_MV+1)); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
370 memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1)); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
371 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
372 for(i=-16; i<16; i++){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
373 default_fcode_tab[i + MAX_MV]= 1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
374 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
375 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
376 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
377 s->mv_penalty= default_mv_penalty; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
378 s->fcode_tab= default_fcode_tab; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
379 |
0 | 380 if (s->out_format == FMT_H263) |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
381 h263_encode_init(s); |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
382 else if (s->out_format == FMT_MPEG1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
383 mpeg1_encode_init(s); |
0 | 384 |
287 | 385 /* dont use mv_penalty table for crap MV as it would be confused */ |
386 if(s->full_search<4) s->mv_penalty= default_mv_penalty; | |
387 | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
388 s->encoding = 1; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
389 |
0 | 390 /* init */ |
391 if (MPV_common_init(s) < 0) | |
392 return -1; | |
393 | |
60 | 394 /* init default q matrix */ |
395 for(i=0;i<64;i++) { | |
396 s->intra_matrix[i] = default_intra_matrix[i]; | |
397 s->non_intra_matrix[i] = default_non_intra_matrix[i]; | |
398 } | |
399 | |
0 | 400 /* rate control init */ |
401 rate_control_init(s); | |
402 | |
403 s->picture_number = 0; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
404 s->picture_in_gop_number = 0; |
0 | 405 s->fake_picture_number = 0; |
406 /* motion detector init */ | |
407 s->f_code = 1; | |
408 | |
409 return 0; | |
410 } | |
411 | |
412 int MPV_encode_end(AVCodecContext *avctx) | |
413 { | |
414 MpegEncContext *s = avctx->priv_data; | |
415 | |
416 #ifdef STATS | |
417 print_stats(); | |
418 #endif | |
419 MPV_common_end(s); | |
420 if (s->out_format == FMT_MJPEG) | |
421 mjpeg_close(s); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
422 |
0 | 423 return 0; |
424 } | |
425 | |
426 /* draw the edges of width 'w' of an image of size width, height */ | |
206 | 427 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w) |
0 | 428 { |
429 UINT8 *ptr, *last_line; | |
430 int i; | |
431 | |
432 last_line = buf + (height - 1) * wrap; | |
433 for(i=0;i<w;i++) { | |
434 /* top and bottom */ | |
435 memcpy(buf - (i + 1) * wrap, buf, width); | |
436 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
437 } | |
438 /* left and right */ | |
439 ptr = buf; | |
440 for(i=0;i<height;i++) { | |
441 memset(ptr - w, ptr[0], w); | |
442 memset(ptr + width, ptr[width-1], w); | |
443 ptr += wrap; | |
444 } | |
445 /* corners */ | |
446 for(i=0;i<w;i++) { | |
447 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
448 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
449 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
450 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
451 } | |
452 } | |
453 | |
454 /* generic function for encode/decode called before a frame is coded/decoded */ | |
455 void MPV_frame_start(MpegEncContext *s) | |
456 { | |
457 int i; | |
458 UINT8 *tmp; | |
459 | |
46
931417475f5b
fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents:
40
diff
changeset
|
460 s->mb_skiped = 0; |
0 | 461 if (s->pict_type == B_TYPE) { |
462 for(i=0;i<3;i++) { | |
463 s->current_picture[i] = s->aux_picture[i]; | |
464 } | |
465 } else { | |
262 | 466 s->last_non_b_pict_type= s->pict_type; |
0 | 467 for(i=0;i<3;i++) { |
468 /* swap next and last */ | |
469 tmp = s->last_picture[i]; | |
470 s->last_picture[i] = s->next_picture[i]; | |
471 s->next_picture[i] = tmp; | |
472 s->current_picture[i] = tmp; | |
473 } | |
474 } | |
475 } | |
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
|
476 |
0 | 477 /* generic function for encode/decode called after a frame has been coded/decoded */ |
478 void MPV_frame_end(MpegEncContext *s) | |
479 { | |
480 /* draw edge for correct motion prediction if outside */ | |
220 | 481 if (s->pict_type != B_TYPE && !s->intra_only) { |
257 | 482 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){ |
176
9ce215ee9216
unrestricted MC fixed - thanks to Michael Niedermayer for idea
arpi_esp
parents:
162
diff
changeset
|
483 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
|
484 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
|
485 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
|
486 }else{ |
257 | 487 /* mpeg4? / opendivx / xvid */ |
0 | 488 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH); |
489 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
490 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
|
491 } |
0 | 492 } |
207 | 493 emms_c(); |
0 | 494 } |
495 | |
496 int MPV_encode_picture(AVCodecContext *avctx, | |
497 unsigned char *buf, int buf_size, void *data) | |
498 { | |
499 MpegEncContext *s = avctx->priv_data; | |
500 AVPicture *pict = data; | |
501 int i, j; | |
502 | |
503 if (s->fixed_qscale) | |
504 s->qscale = avctx->quality; | |
505 | |
506 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
507 | |
508 if (!s->intra_only) { | |
509 /* first picture of GOP is intra */ | |
288 | 510 if (s->picture_in_gop_number % s->gop_size==0){ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
511 s->picture_in_gop_number=0; |
0 | 512 s->pict_type = I_TYPE; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
513 }else |
0 | 514 s->pict_type = P_TYPE; |
515 } else { | |
516 s->pict_type = I_TYPE; | |
517 } | |
518 | |
519 MPV_frame_start(s); | |
220 | 520 |
0 | 521 for(i=0;i<3;i++) { |
522 UINT8 *src = pict->data[i]; | |
523 UINT8 *dest = s->current_picture[i]; | |
524 int src_wrap = pict->linesize[i]; | |
525 int dest_wrap = s->linesize; | |
526 int w = s->width; | |
527 int h = s->height; | |
528 | |
529 if (i >= 1) { | |
530 dest_wrap >>= 1; | |
531 w >>= 1; | |
532 h >>= 1; | |
533 } | |
534 | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
535 if(dest_wrap==src_wrap){ |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
536 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
|
537 } else { |
220 | 538 for(j=0;j<h;j++) { |
539 memcpy(dest, src, w); | |
540 dest += dest_wrap; | |
541 src += src_wrap; | |
542 } | |
227
ec1bc02a0a47
avoid copying input when encoding non intra stuff too
michaelni
parents:
220
diff
changeset
|
543 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
|
544 } |
0 | 545 } |
546 | |
547 encode_picture(s, s->picture_number); | |
271 | 548 avctx->key_frame = (s->pict_type == I_TYPE); |
286 | 549 avctx->header_bits = s->header_bits; |
550 avctx->mv_bits = s->mv_bits; | |
551 avctx->misc_bits = s->misc_bits; | |
552 avctx->i_tex_bits = s->i_tex_bits; | |
553 avctx->p_tex_bits = s->p_tex_bits; | |
554 avctx->i_count = s->i_count; | |
555 avctx->p_count = s->p_count; | |
556 avctx->skip_count = s->skip_count; | |
557 | |
0 | 558 MPV_frame_end(s); |
559 s->picture_number++; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
560 s->picture_in_gop_number++; |
0 | 561 |
562 if (s->out_format == FMT_MJPEG) | |
563 mjpeg_picture_trailer(s); | |
564 | |
565 flush_put_bits(&s->pb); | |
268 | 566 s->last_frame_bits= s->frame_bits; |
567 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; | |
568 s->total_bits += s->frame_bits; | |
286 | 569 avctx->frame_bits = s->frame_bits; |
570 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", | |
571 //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits); | |
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
|
572 |
0 | 573 avctx->quality = s->qscale; |
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
574 if (avctx->get_psnr) { |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
575 /* At this point pict->data should have the original frame */ |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
576 /* an s->current_picture should have the coded/decoded frame */ |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
577 get_psnr(pict->data, s->current_picture, |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
578 pict->linesize, s->linesize, avctx); |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
579 } |
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
|
580 return pbBufPtr(&s->pb) - s->pb.buf; |
0 | 581 } |
582 | |
583 static inline int clip(int a, int amin, int amax) | |
584 { | |
585 if (a < amin) | |
586 return amin; | |
587 else if (a > amax) | |
588 return amax; | |
589 else | |
590 return a; | |
591 } | |
592 | |
255 | 593 static inline void gmc1_motion(MpegEncContext *s, |
594 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
595 int dest_offset, | |
596 UINT8 **ref_picture, int src_offset, | |
597 int h) | |
598 { | |
599 UINT8 *ptr; | |
600 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
601 int motion_x, motion_y; | |
602 | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
603 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n"); |
255 | 604 motion_x= s->sprite_offset[0][0]; |
605 motion_y= s->sprite_offset[0][1]; | |
606 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
607 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
608 motion_x<<=(3-s->sprite_warping_accuracy); | |
609 motion_y<<=(3-s->sprite_warping_accuracy); | |
610 src_x = clip(src_x, -16, s->width); | |
611 if (src_x == s->width) | |
612 motion_x =0; | |
613 src_y = clip(src_y, -16, s->height); | |
614 if (src_y == s->height) | |
615 motion_y =0; | |
616 | |
617 linesize = s->linesize; | |
618 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
619 | |
620 dest_y+=dest_offset; | |
621 gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
622 gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
623 | |
624 motion_x= s->sprite_offset[1][0]; | |
625 motion_y= s->sprite_offset[1][1]; | |
626 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
627 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
628 motion_x<<=(3-s->sprite_warping_accuracy); | |
629 motion_y<<=(3-s->sprite_warping_accuracy); | |
630 src_x = clip(src_x, -8, s->width>>1); | |
631 if (src_x == s->width>>1) | |
632 motion_x =0; | |
633 src_y = clip(src_y, -8, s->height>>1); | |
634 if (src_y == s->height>>1) | |
635 motion_y =0; | |
636 | |
637 offset = (src_y * linesize>>1) + src_x + (src_offset>>1); | |
638 ptr = ref_picture[1] + offset; | |
639 gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
640 ptr = ref_picture[2] + offset; | |
641 gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
642 | |
643 return; | |
644 } | |
645 | |
0 | 646 /* apply one mpeg motion vector to the three components */ |
647 static inline void mpeg_motion(MpegEncContext *s, | |
648 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
649 int dest_offset, | |
650 UINT8 **ref_picture, int src_offset, | |
651 int field_based, op_pixels_func *pix_op, | |
652 int motion_x, int motion_y, int h) | |
653 { | |
654 UINT8 *ptr; | |
655 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
255 | 656 if(s->quarter_sample) |
657 { | |
658 motion_x>>=1; | |
659 motion_y>>=1; | |
660 } | |
0 | 661 dxy = ((motion_y & 1) << 1) | (motion_x & 1); |
662 src_x = s->mb_x * 16 + (motion_x >> 1); | |
663 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
664 | |
665 /* WARNING: do no forget half pels */ | |
666 height = s->height >> field_based; | |
667 src_x = clip(src_x, -16, s->width); | |
668 if (src_x == s->width) | |
669 dxy &= ~1; | |
670 src_y = clip(src_y, -16, height); | |
671 if (src_y == height) | |
672 dxy &= ~2; | |
673 linesize = s->linesize << field_based; | |
674 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; | |
675 dest_y += dest_offset; | |
676 pix_op[dxy](dest_y, ptr, linesize, h); | |
677 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h); | |
678 | |
679 if (s->out_format == FMT_H263) { | |
680 dxy = 0; | |
681 if ((motion_x & 3) != 0) | |
682 dxy |= 1; | |
683 if ((motion_y & 3) != 0) | |
684 dxy |= 2; | |
685 mx = motion_x >> 2; | |
686 my = motion_y >> 2; | |
687 } else { | |
688 mx = motion_x / 2; | |
689 my = motion_y / 2; | |
690 dxy = ((my & 1) << 1) | (mx & 1); | |
691 mx >>= 1; | |
692 my >>= 1; | |
693 } | |
694 | |
695 src_x = s->mb_x * 8 + mx; | |
696 src_y = s->mb_y * (8 >> field_based) + my; | |
697 src_x = clip(src_x, -8, s->width >> 1); | |
698 if (src_x == (s->width >> 1)) | |
699 dxy &= ~1; | |
700 src_y = clip(src_y, -8, height >> 1); | |
701 if (src_y == (height >> 1)) | |
702 dxy &= ~2; | |
703 | |
704 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
705 ptr = ref_picture[1] + offset; | |
706 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
707 ptr = ref_picture[2] + offset; | |
708 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
709 } | |
710 | |
255 | 711 static inline void qpel_motion(MpegEncContext *s, |
712 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
713 int dest_offset, | |
714 UINT8 **ref_picture, int src_offset, | |
715 int field_based, op_pixels_func *pix_op, | |
716 qpel_mc_func *qpix_op, | |
717 int motion_x, int motion_y, int h) | |
718 { | |
719 UINT8 *ptr; | |
720 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
721 | |
722 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
723 src_x = s->mb_x * 16 + (motion_x >> 2); | |
724 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); | |
725 | |
726 height = s->height >> field_based; | |
727 src_x = clip(src_x, -16, s->width); | |
728 if (src_x == s->width) | |
729 dxy &= ~3; | |
730 src_y = clip(src_y, -16, height); | |
731 if (src_y == height) | |
732 dxy &= ~12; | |
733 linesize = s->linesize << field_based; | |
734 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
735 dest_y += dest_offset; | |
736 //printf("%d %d %d\n", src_x, src_y, dxy); | |
737 qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3); | |
738 qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3); | |
739 qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3); | |
740 qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3); | |
741 | |
742 mx= (motion_x>>1) | (motion_x&1); | |
743 my= (motion_y>>1) | (motion_y&1); | |
744 | |
745 dxy = 0; | |
746 if ((mx & 3) != 0) | |
747 dxy |= 1; | |
748 if ((my & 3) != 0) | |
749 dxy |= 2; | |
750 mx = mx >> 2; | |
751 my = my >> 2; | |
752 | |
753 src_x = s->mb_x * 8 + mx; | |
754 src_y = s->mb_y * (8 >> field_based) + my; | |
755 src_x = clip(src_x, -8, s->width >> 1); | |
756 if (src_x == (s->width >> 1)) | |
757 dxy &= ~1; | |
758 src_y = clip(src_y, -8, height >> 1); | |
759 if (src_y == (height >> 1)) | |
760 dxy &= ~2; | |
761 | |
762 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
763 ptr = ref_picture[1] + offset; | |
764 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
765 ptr = ref_picture[2] + offset; | |
766 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
767 } | |
768 | |
769 | |
0 | 770 static inline void MPV_motion(MpegEncContext *s, |
771 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
772 int dir, UINT8 **ref_picture, | |
255 | 773 op_pixels_func *pix_op, qpel_mc_func *qpix_op) |
0 | 774 { |
775 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
776 int mb_x, mb_y, i; | |
777 UINT8 *ptr, *dest; | |
778 | |
779 mb_x = s->mb_x; | |
780 mb_y = s->mb_y; | |
781 | |
782 switch(s->mv_type) { | |
783 case MV_TYPE_16X16: | |
255 | 784 if(s->mcsel){ |
785 #if 0 | |
786 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
787 ref_picture, 0, | |
788 0, pix_op, | |
789 s->sprite_offset[0][0]>>3, | |
790 s->sprite_offset[0][1]>>3, | |
791 16); | |
792 #else | |
793 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0, | |
794 ref_picture, 0, | |
795 16); | |
796 #endif | |
262 | 797 }else if(s->quarter_sample && dir==0){ //FIXME |
255 | 798 qpel_motion(s, dest_y, dest_cb, dest_cr, 0, |
799 ref_picture, 0, | |
800 0, pix_op, qpix_op, | |
801 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
802 }else{ | |
803 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
804 ref_picture, 0, | |
805 0, pix_op, | |
806 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
807 } | |
0 | 808 break; |
809 case MV_TYPE_8X8: | |
810 for(i=0;i<4;i++) { | |
811 motion_x = s->mv[dir][i][0]; | |
812 motion_y = s->mv[dir][i][1]; | |
813 | |
814 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
815 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
816 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8; | |
817 | |
818 /* WARNING: do no forget half pels */ | |
819 src_x = clip(src_x, -16, s->width); | |
820 if (src_x == s->width) | |
821 dxy &= ~1; | |
822 src_y = clip(src_y, -16, s->height); | |
823 if (src_y == s->height) | |
824 dxy &= ~2; | |
825 | |
826 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
827 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
828 pix_op[dxy](dest, ptr, s->linesize, 8); | |
829 } | |
830 /* In case of 8X8, we construct a single chroma motion vector | |
831 with a special rounding */ | |
832 mx = 0; | |
833 my = 0; | |
834 for(i=0;i<4;i++) { | |
835 mx += s->mv[dir][i][0]; | |
836 my += s->mv[dir][i][1]; | |
837 } | |
838 if (mx >= 0) | |
839 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
840 else { | |
841 mx = -mx; | |
842 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
843 } | |
844 if (my >= 0) | |
845 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
846 else { | |
847 my = -my; | |
848 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
849 } | |
850 dxy = ((my & 1) << 1) | (mx & 1); | |
851 mx >>= 1; | |
852 my >>= 1; | |
853 | |
854 src_x = mb_x * 8 + mx; | |
855 src_y = mb_y * 8 + my; | |
856 src_x = clip(src_x, -8, s->width/2); | |
857 if (src_x == s->width/2) | |
858 dxy &= ~1; | |
859 src_y = clip(src_y, -8, s->height/2); | |
860 if (src_y == s->height/2) | |
861 dxy &= ~2; | |
862 | |
863 offset = (src_y * (s->linesize >> 1)) + src_x; | |
864 ptr = ref_picture[1] + offset; | |
865 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8); | |
866 ptr = ref_picture[2] + offset; | |
867 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8); | |
868 break; | |
869 case MV_TYPE_FIELD: | |
870 if (s->picture_structure == PICT_FRAME) { | |
871 /* top field */ | |
872 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
873 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
874 1, pix_op, | |
875 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
876 /* bottom field */ | |
877 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
878 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
879 1, pix_op, | |
880 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
881 } else { | |
882 | |
883 | |
884 } | |
885 break; | |
886 } | |
887 } | |
888 | |
889 | |
890 /* put block[] to dest[] */ | |
891 static inline void put_dct(MpegEncContext *s, | |
892 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
893 { | |
894 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
|
895 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
|
896 ff_idct (block); |
0 | 897 put_pixels_clamped(block, dest, line_size); |
898 } | |
899 | |
900 /* add block[] to dest[] */ | |
901 static inline void add_dct(MpegEncContext *s, | |
902 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
903 { | |
904 if (s->block_last_index[i] >= 0) { | |
905 if (!s->mpeg2) | |
206 | 906 if(s->encoding || (!s->h263_msmpeg4)) |
200 | 907 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
|
908 ff_idct (block); |
0 | 909 add_pixels_clamped(block, dest, line_size); |
910 } | |
911 } | |
912 | |
913 /* generic function called after a macroblock has been parsed by the | |
914 decoder or after it has been encoded by the encoder. | |
915 | |
916 Important variables used: | |
917 s->mb_intra : true if intra macroblock | |
918 s->mv_dir : motion vector direction | |
919 s->mv_type : motion vector type | |
920 s->mv : motion vector | |
921 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
922 */ | |
923 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
924 { | |
244 | 925 int mb_x, mb_y; |
0 | 926 int dct_linesize, dct_offset; |
927 op_pixels_func *op_pix; | |
255 | 928 qpel_mc_func *op_qpix; |
0 | 929 |
930 mb_x = s->mb_x; | |
931 mb_y = s->mb_y; | |
932 | |
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
933 #ifdef FF_POSTPROCESS |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
934 quant_store[mb_y][mb_x]=s->qscale; |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
935 //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
|
936 #endif |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
937 |
0 | 938 /* update DC predictors for P macroblocks */ |
939 if (!s->mb_intra) { | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
940 if (s->h263_pred || s->h263_aic) { |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
941 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
|
942 { |
244 | 943 int wrap, xy, v; |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
944 s->mbintra_table[mb_x + mb_y*s->mb_width]=0; |
0 | 945 wrap = 2 * s->mb_width + 2; |
244 | 946 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; |
0 | 947 v = 1024; |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
948 |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
949 s->dc_val[0][xy] = v; |
244 | 950 s->dc_val[0][xy + 1] = v; |
951 s->dc_val[0][xy + wrap] = v; | |
952 s->dc_val[0][xy + 1 + wrap] = v; | |
0 | 953 /* ac pred */ |
244 | 954 memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16)); |
955 memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16)); | |
956 memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16)); | |
957 memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16)); | |
0 | 958 if (s->h263_msmpeg4) { |
244 | 959 s->coded_block[xy] = 0; |
960 s->coded_block[xy + 1] = 0; | |
961 s->coded_block[xy + wrap] = 0; | |
962 s->coded_block[xy + 1 + wrap] = 0; | |
0 | 963 } |
964 /* chroma */ | |
965 wrap = s->mb_width + 2; | |
244 | 966 xy = mb_x + 1 + (mb_y + 1) * wrap; |
967 s->dc_val[1][xy] = v; | |
968 s->dc_val[2][xy] = v; | |
0 | 969 /* ac pred */ |
244 | 970 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); |
971 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
|
972 } |
0 | 973 } else { |
974 s->last_dc[0] = 128 << s->intra_dc_precision; | |
975 s->last_dc[1] = 128 << s->intra_dc_precision; | |
976 s->last_dc[2] = 128 << s->intra_dc_precision; | |
977 } | |
978 } | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
979 else if (s->h263_pred || s->h263_aic) |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
980 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
|
981 |
280 | 982 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */ |
983 if (s->out_format == FMT_H263) { | |
262 | 984 if(s->pict_type!=B_TYPE){ |
244 | 985 int xy, wrap, motion_x, motion_y; |
0 | 986 |
987 wrap = 2 * s->mb_width + 2; | |
244 | 988 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; |
0 | 989 if (s->mb_intra) { |
990 motion_x = 0; | |
991 motion_y = 0; | |
992 goto motion_init; | |
993 } else if (s->mv_type == MV_TYPE_16X16) { | |
994 motion_x = s->mv[0][0][0]; | |
995 motion_y = s->mv[0][0][1]; | |
996 motion_init: | |
997 /* no update if 8X8 because it has been done during parsing */ | |
244 | 998 s->motion_val[xy][0] = motion_x; |
999 s->motion_val[xy][1] = motion_y; | |
1000 s->motion_val[xy + 1][0] = motion_x; | |
1001 s->motion_val[xy + 1][1] = motion_y; | |
1002 s->motion_val[xy + wrap][0] = motion_x; | |
1003 s->motion_val[xy + wrap][1] = motion_y; | |
1004 s->motion_val[xy + 1 + wrap][0] = motion_x; | |
1005 s->motion_val[xy + 1 + wrap][1] = motion_y; | |
0 | 1006 } |
262 | 1007 } |
0 | 1008 } |
1009 | |
1010 if (!s->intra_only) { | |
1011 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
|
1012 UINT8 *mbskip_ptr; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1013 |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1014 /* 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
|
1015 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
|
1016 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
|
1017 if (s->mb_skiped) { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1018 s->mb_skiped = 0; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1019 /* 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
|
1020 if (*mbskip_ptr != 0) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1021 goto the_end; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1022 *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
|
1023 } else { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1024 *mbskip_ptr = 0; /* not skipped */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1025 } |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1026 } |
0 | 1027 |
1028 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16; | |
1029 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
1030 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
1031 | |
1032 if (s->interlaced_dct) { | |
1033 dct_linesize = s->linesize * 2; | |
1034 dct_offset = s->linesize; | |
1035 } else { | |
1036 dct_linesize = s->linesize; | |
1037 dct_offset = s->linesize * 8; | |
1038 } | |
1039 | |
1040 if (!s->mb_intra) { | |
1041 /* motion handling */ | |
255 | 1042 if (!s->no_rounding){ |
0 | 1043 op_pix = put_pixels_tab; |
255 | 1044 op_qpix= qpel_mc_rnd_tab; |
1045 }else{ | |
0 | 1046 op_pix = put_no_rnd_pixels_tab; |
255 | 1047 op_qpix= qpel_mc_no_rnd_tab; |
1048 } | |
0 | 1049 |
1050 if (s->mv_dir & MV_DIR_FORWARD) { | |
255 | 1051 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); |
0 | 1052 if (!s->no_rounding) |
1053 op_pix = avg_pixels_tab; | |
1054 else | |
1055 op_pix = avg_no_rnd_pixels_tab; | |
1056 } | |
1057 if (s->mv_dir & MV_DIR_BACKWARD) { | |
255 | 1058 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); |
0 | 1059 } |
1060 | |
1061 /* add dct residue */ | |
1062 add_dct(s, block[0], 0, dest_y, dct_linesize); | |
1063 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
1064 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
1065 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
1066 | |
57 | 1067 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
1068 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
0 | 1069 } else { |
1070 /* dct only in intra block */ | |
1071 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
1072 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
1073 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
1074 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
1075 | |
57 | 1076 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
1077 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
0 | 1078 } |
1079 } | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1080 the_end: |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1081 emms_c(); |
0 | 1082 } |
1083 | |
1084 static void encode_picture(MpegEncContext *s, int picture_number) | |
1085 { | |
231 | 1086 int mb_x, mb_y, wrap, last_gob, pdif = 0; |
0 | 1087 UINT8 *ptr; |
1088 int i, motion_x, motion_y; | |
286 | 1089 int bits; |
0 | 1090 |
1091 s->picture_number = picture_number; | |
268 | 1092 |
1093 s->last_mc_mb_var = s->mc_mb_var; | |
1094 /* Reset the average MB variance */ | |
1095 s->avg_mb_var = 0; | |
1096 s->mc_mb_var = 0; | |
1097 /* Estimate motion for every MB */ | |
1098 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | |
1099 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
1100 int xy= mb_y * s->mb_width + mb_x; | |
288 | 1101 const int mot_stride = s->mb_width*2 + 2; |
1102 int mot_xy = (mb_y*2 + 1)*mot_stride + mb_x*2 + 1; | |
268 | 1103 s->mb_x = mb_x; |
1104 s->mb_y = mb_y; | |
1105 | |
1106 /* compute motion vector and macro block type (intra or non intra) */ | |
1107 motion_x = 0; | |
1108 motion_y = 0; | |
1109 if (s->pict_type == P_TYPE) { | |
1110 s->mb_intra = estimate_motion(s, mb_x, mb_y, | |
1111 &motion_x, | |
1112 &motion_y); | |
1113 } else { | |
1114 s->mb_intra = 1; | |
1115 } | |
1116 /* Store MB type and MV */ | |
1117 s->mb_type[xy] = s->mb_intra; | |
1118 s->mv_table[0][xy] = motion_x; | |
1119 s->mv_table[1][xy] = motion_y; | |
288 | 1120 |
1121 s->motion_val[mot_xy ][0]= motion_x; | |
1122 s->motion_val[mot_xy ][1]= motion_y; | |
1123 s->motion_val[mot_xy+1][0]= motion_x; | |
1124 s->motion_val[mot_xy+1][1]= motion_y; | |
1125 mot_xy += mot_stride; | |
1126 s->motion_val[mot_xy ][0]= motion_x; | |
1127 s->motion_val[mot_xy ][1]= motion_y; | |
1128 s->motion_val[mot_xy+1][0]= motion_x; | |
1129 s->motion_val[mot_xy+1][1]= motion_y; | |
268 | 1130 } |
1131 } | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1132 emms_c(); |
268 | 1133 |
271 | 1134 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE){ //FIXME subtract MV bits |
1135 int i; | |
1136 s->pict_type= I_TYPE; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1137 s->picture_in_gop_number=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1138 for(i=0; i<s->mb_num; i++){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1139 s->mb_type[i] = 1; |
271 | 1140 s->mv_table[0][i] = 0; |
1141 s->mv_table[1][i] = 0; | |
1142 } | |
1143 } | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1144 |
287 | 1145 /* find best f_code for ME which do unlimited searches */ |
1146 if(s->pict_type==P_TYPE && s->full_search>3){ | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1147 int mv_num[8]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1148 int i; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1149 int loose=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1150 UINT8 * fcode_tab= s->fcode_tab; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1151 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1152 for(i=0; i<8; i++) mv_num[i]=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1153 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1154 for(i=0; i<s->mb_num; i++){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1155 if(s->mb_type[i] == 0){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1156 mv_num[ fcode_tab[s->mv_table[0][i] + MAX_MV] ]++; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1157 mv_num[ fcode_tab[s->mv_table[1][i] + MAX_MV] ]++; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1158 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1159 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1160 //else printf("I"); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1161 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1162 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1163 for(i=MAX_FCODE; i>1; i--){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1164 loose+= mv_num[i]; |
286 | 1165 if(loose > 10) break; //FIXME this is pretty ineffective |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1166 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1167 s->f_code= i; |
287 | 1168 /* for(i=0; i<=MAX_FCODE; i++){ |
1169 printf("%d ", mv_num[i]); | |
1170 } | |
1171 printf("\n");*/ | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1172 }else{ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1173 s->f_code= 1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1174 } |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
1175 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1176 //printf("f_code %d ///\n", s->f_code); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1177 /* convert MBs with too long MVs to I-Blocks */ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1178 if(s->pict_type==P_TYPE){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1179 int i; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1180 const int f_code= s->f_code; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1181 UINT8 * fcode_tab= s->fcode_tab; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1182 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1183 for(i=0; i<s->mb_num; i++){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1184 if(s->mb_type[i] == 0){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1185 if( fcode_tab[s->mv_table[0][i] + MAX_MV] > f_code |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1186 || fcode_tab[s->mv_table[0][i] + MAX_MV] == 0 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1187 || fcode_tab[s->mv_table[1][i] + MAX_MV] > f_code |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1188 || fcode_tab[s->mv_table[1][i] + MAX_MV] == 0 ){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1189 s->mb_type[i] = 1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1190 s->mv_table[0][i] = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1191 s->mv_table[1][i] = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1192 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1193 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1194 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1195 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1196 |
271 | 1197 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); |
1198 | |
0 | 1199 if (!s->fixed_qscale) |
1200 s->qscale = rate_estimate_qscale(s); | |
1201 | |
1202 /* precompute matrix */ | |
1203 if (s->out_format == FMT_MJPEG) { | |
1204 /* for mjpeg, we do include qscale in the matrix */ | |
1205 s->intra_matrix[0] = default_intra_matrix[0]; | |
1206 for(i=1;i<64;i++) | |
1207 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3; | |
220 | 1208 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8); |
0 | 1209 } else { |
220 | 1210 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale); |
1211 convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale); | |
0 | 1212 } |
1213 | |
286 | 1214 s->last_bits= get_bit_count(&s->pb); |
0 | 1215 switch(s->out_format) { |
1216 case FMT_MJPEG: | |
1217 mjpeg_picture_header(s); | |
1218 break; | |
1219 case FMT_H263: | |
1220 if (s->h263_msmpeg4) | |
1221 msmpeg4_encode_picture_header(s, picture_number); | |
1222 else if (s->h263_pred) | |
1223 mpeg4_encode_picture_header(s, picture_number); | |
1224 else if (s->h263_rv10) | |
1225 rv10_encode_picture_header(s, picture_number); | |
1226 else | |
1227 h263_encode_picture_header(s, picture_number); | |
1228 break; | |
1229 case FMT_MPEG1: | |
1230 mpeg1_encode_picture_header(s, picture_number); | |
1231 break; | |
1232 } | |
286 | 1233 bits= get_bit_count(&s->pb); |
1234 s->header_bits= bits - s->last_bits; | |
1235 s->last_bits= bits; | |
1236 s->mv_bits=0; | |
1237 s->misc_bits=0; | |
1238 s->i_tex_bits=0; | |
1239 s->p_tex_bits=0; | |
1240 s->i_count=0; | |
1241 s->p_count=0; | |
1242 s->skip_count=0; | |
1243 | |
0 | 1244 /* init last dc values */ |
1245 /* note: quant matrix value (8) is implied here */ | |
1246 s->last_dc[0] = 128; | |
1247 s->last_dc[1] = 128; | |
1248 s->last_dc[2] = 128; | |
1249 s->mb_incr = 1; | |
1250 s->last_mv[0][0][0] = 0; | |
1251 s->last_mv[0][0][1] = 0; | |
1252 s->mv_type = MV_TYPE_16X16; | |
1253 s->mv_dir = MV_DIR_FORWARD; | |
1254 | |
162 | 1255 /* Get the GOB height based on picture height */ |
231 | 1256 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) { |
162 | 1257 if (s->height <= 400) |
1258 s->gob_index = 1; | |
1259 else if (s->height <= 800) | |
1260 s->gob_index = 2; | |
1261 else | |
1262 s->gob_index = 4; | |
1263 } | |
268 | 1264 |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1265 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
|
1266 |
266 | 1267 s->block_wrap[0]= |
1268 s->block_wrap[1]= | |
1269 s->block_wrap[2]= | |
1270 s->block_wrap[3]= s->mb_width*2 + 2; | |
1271 s->block_wrap[4]= | |
1272 s->block_wrap[5]= s->mb_width + 2; | |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1273 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
|
1274 /* 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
|
1275 /* 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
|
1276 if (s->rtp_mode) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1277 if (!mb_y) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1278 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
|
1279 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
|
1280 } 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
|
1281 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
|
1282 if (last_gob) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1283 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
|
1284 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1285 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1286 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1287 |
266 | 1288 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
1289 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
1290 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
1291 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
1292 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
1293 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1294 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
0 | 1295 |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1296 s->mb_x = mb_x; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1297 s->mb_y = mb_y; |
266 | 1298 s->block_index[0]+=2; |
1299 s->block_index[1]+=2; | |
1300 s->block_index[2]+=2; | |
1301 s->block_index[3]+=2; | |
1302 s->block_index[4]++; | |
1303 s->block_index[5]++; | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1304 #if 0 |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1305 /* 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
|
1306 motion_x = 0; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1307 motion_y = 0; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1308 if (s->pict_type == P_TYPE) { |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1309 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
|
1310 &motion_x, |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1311 &motion_y); |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1312 } else { |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1313 s->mb_intra = 1; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1314 } |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1315 #endif |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1316 |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1317 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
|
1318 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
|
1319 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
|
1320 |
0 | 1321 /* get the pixels */ |
1322 wrap = s->linesize; | |
1323 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
|
1324 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
|
1325 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
|
1326 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
|
1327 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); |
0 | 1328 wrap = s->linesize >> 1; |
1329 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
|
1330 get_pixels(s->block[4], ptr, wrap); |
0 | 1331 |
1332 wrap = s->linesize >> 1; | |
1333 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
|
1334 get_pixels(s->block[5], ptr, wrap); |
0 | 1335 |
1336 /* subtract previous frame if non intra */ | |
1337 if (!s->mb_intra) { | |
1338 int dxy, offset, mx, my; | |
1339 | |
1340 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
1341 ptr = s->last_picture[0] + | |
1342 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + | |
1343 (mb_x * 16 + (motion_x >> 1)); | |
1344 | |
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
|
1345 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
|
1346 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
|
1347 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
|
1348 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy); |
0 | 1349 |
1350 if (s->out_format == FMT_H263) { | |
1351 /* special rounding for h263 */ | |
1352 dxy = 0; | |
1353 if ((motion_x & 3) != 0) | |
1354 dxy |= 1; | |
1355 if ((motion_y & 3) != 0) | |
1356 dxy |= 2; | |
1357 mx = motion_x >> 2; | |
1358 my = motion_y >> 2; | |
1359 } else { | |
1360 mx = motion_x / 2; | |
1361 my = motion_y / 2; | |
1362 dxy = ((my & 1) << 1) | (mx & 1); | |
1363 mx >>= 1; | |
1364 my >>= 1; | |
1365 } | |
1366 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx); | |
1367 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
|
1368 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy); |
0 | 1369 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
|
1370 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy); |
0 | 1371 } |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1372 emms_c(); |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1373 |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1374 #if 0 |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1375 { |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1376 float adap_parm; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1377 |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1378 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) / |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1379 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0); |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1380 |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1381 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1382 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1383 s->qscale, adap_parm, s->qscale*adap_parm, |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1384 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1385 } |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1386 #endif |
0 | 1387 /* DCT & quantize */ |
1388 if (s->h263_msmpeg4) { | |
1389 msmpeg4_dc_scale(s); | |
1390 } else if (s->h263_pred) { | |
1391 h263_dc_scale(s); | |
1392 } else { | |
1393 /* default quantization values */ | |
1394 s->y_dc_scale = 8; | |
1395 s->c_dc_scale = 8; | |
1396 } | |
1397 for(i=0;i<6;i++) { | |
220 | 1398 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale); |
0 | 1399 } |
1400 | |
1401 /* huffman encode */ | |
1402 switch(s->out_format) { | |
1403 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
|
1404 mpeg1_encode_mb(s, s->block, motion_x, motion_y); |
0 | 1405 break; |
1406 case FMT_H263: | |
1407 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
|
1408 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
1409 else if(s->h263_pred) |
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
1410 mpeg4_encode_mb(s, s->block, motion_x, motion_y); |
0 | 1411 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
|
1412 h263_encode_mb(s, s->block, motion_x, motion_y); |
0 | 1413 break; |
1414 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
|
1415 mjpeg_encode_mb(s, s->block); |
0 | 1416 break; |
1417 } | |
286 | 1418 |
0 | 1419 /* decompress blocks so that we keep the state of the decoder */ |
1420 s->mv[0][0][0] = motion_x; | |
1421 s->mv[0][0][1] = motion_y; | |
1422 | |
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
|
1423 MPV_decode_mb(s, s->block); |
0 | 1424 } |
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
|
1425 |
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
|
1426 |
231 | 1427 /* Obtain average GOB size for RTP */ |
1428 if (s->rtp_mode) { | |
1429 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
|
1430 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line; |
231 | 1431 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
|
1432 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
|
1433 s->ptr_last_mb_line = pbBufPtr(&s->pb); |
231 | 1434 } |
1435 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, | |
1436 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); | |
1437 s->first_gob_line = 0; | |
1438 } | |
0 | 1439 } |
286 | 1440 |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
249
diff
changeset
|
1441 if (s->h263_msmpeg4 && s->pict_type == I_TYPE) |
208 | 1442 msmpeg4_encode_ext_header(s); |
1443 | |
162 | 1444 //if (s->gob_number) |
1445 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | |
231 | 1446 |
1447 /* Send the last GOB if RTP */ | |
1448 if (s->rtp_mode) { | |
1449 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
|
1450 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
231 | 1451 /* Call the RTP callback to send the last GOB */ |
1452 if (s->rtp_callback) | |
1453 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
|
1454 s->ptr_lastgob = pbBufPtr(&s->pb); |
231 | 1455 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif); |
1456 } | |
1457 | |
0 | 1458 } |
1459 | |
220 | 1460 static int dct_quantize_c(MpegEncContext *s, |
0 | 1461 DCTELEM *block, int n, |
1462 int qscale) | |
1463 { | |
1464 int i, j, level, last_non_zero, q; | |
1465 const int *qmat; | |
216 | 1466 int minLevel, maxLevel; |
1467 | |
1468 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){ | |
1469 /* mpeg4 */ | |
1470 minLevel= -2048; | |
1471 maxLevel= 2047; | |
1472 }else if(s->out_format==FMT_MPEG1){ | |
1473 /* mpeg1 */ | |
1474 minLevel= -255; | |
1475 maxLevel= 255; | |
230 | 1476 }else if(s->out_format==FMT_MJPEG){ |
1477 /* (m)jpeg */ | |
1478 minLevel= -1023; | |
1479 maxLevel= 1023; | |
216 | 1480 }else{ |
1481 /* h263 / msmpeg4 */ | |
1482 minLevel= -128; | |
1483 maxLevel= 127; | |
1484 } | |
0 | 1485 |
1486 av_fdct (block); | |
1487 | |
64 | 1488 /* we need this permutation so that we correct the IDCT |
1489 permutation. will be moved into DCT code */ | |
1490 block_permute(block); | |
1491 | |
0 | 1492 if (s->mb_intra) { |
1493 if (n < 4) | |
1494 q = s->y_dc_scale; | |
1495 else | |
1496 q = s->c_dc_scale; | |
1497 q = q << 3; | |
1498 | |
1499 /* note: block[0] is assumed to be positive */ | |
1500 block[0] = (block[0] + (q >> 1)) / q; | |
1501 i = 1; | |
1502 last_non_zero = 0; | |
1503 if (s->out_format == FMT_H263) { | |
1504 qmat = s->q_non_intra_matrix; | |
1505 } else { | |
1506 qmat = s->q_intra_matrix; | |
1507 } | |
1508 } else { | |
1509 i = 0; | |
1510 last_non_zero = -1; | |
1511 qmat = s->q_non_intra_matrix; | |
1512 } | |
1513 | |
1514 for(;i<64;i++) { | |
1515 j = zigzag_direct[i]; | |
1516 level = block[j]; | |
1517 level = level * qmat[j]; | |
1518 #ifdef PARANOID | |
1519 { | |
1520 static int count = 0; | |
1521 int level1, level2, qmat1; | |
1522 double val; | |
1523 if (qmat == s->q_non_intra_matrix) { | |
1524 qmat1 = default_non_intra_matrix[j] * s->qscale; | |
1525 } else { | |
1526 qmat1 = default_intra_matrix[j] * s->qscale; | |
1527 } | |
1528 if (av_fdct != jpeg_fdct_ifast) | |
1529 val = ((double)block[j] * 8.0) / (double)qmat1; | |
1530 else | |
1531 val = ((double)block[j] * 8.0 * 2048.0) / | |
1532 ((double)qmat1 * aanscales[j]); | |
1533 level1 = (int)val; | |
1534 level2 = level / (1 << (QMAT_SHIFT - 3)); | |
1535 if (level1 != level2) { | |
1536 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", | |
1537 count, level2, level1, block[j], qmat1, qmat[j], | |
1538 val); | |
1539 count++; | |
1540 } | |
1541 | |
1542 } | |
1543 #endif | |
1544 /* XXX: slight error for the low range. Test should be equivalent to | |
1545 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 << | |
1546 (QMAT_SHIFT - 3))) | |
1547 */ | |
1548 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != | |
1549 level) { | |
1550 level = level / (1 << (QMAT_SHIFT - 3)); | |
1551 /* XXX: currently, this code is not optimal. the range should be: | |
1552 mpeg1: -255..255 | |
1553 mpeg2: -2048..2047 | |
1554 h263: -128..127 | |
1555 mpeg4: -2048..2047 | |
1556 */ | |
216 | 1557 if (level > maxLevel) |
1558 level = maxLevel; | |
1559 else if (level < minLevel) | |
1560 level = minLevel; | |
0 | 1561 |
1562 block[j] = level; | |
1563 last_non_zero = i; | |
1564 } else { | |
1565 block[j] = 0; | |
1566 } | |
1567 } | |
1568 return last_non_zero; | |
1569 } | |
1570 | |
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
|
1571 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
|
1572 DCTELEM *block, int n, int qscale) |
0 | 1573 { |
200 | 1574 int i, level, nCoeffs; |
0 | 1575 const UINT16 *quant_matrix; |
1576 | |
200 | 1577 if(s->alternate_scan) nCoeffs= 64; |
1578 else nCoeffs= s->block_last_index[n]+1; | |
1579 | |
0 | 1580 if (s->mb_intra) { |
1581 if (n < 4) | |
1582 block[0] = block[0] * s->y_dc_scale; | |
1583 else | |
1584 block[0] = block[0] * s->c_dc_scale; | |
1585 /* XXX: only mpeg1 */ | |
1586 quant_matrix = s->intra_matrix; | |
200 | 1587 for(i=1;i<nCoeffs;i++) { |
1588 int j= zigzag_direct[i]; | |
1589 level = block[j]; | |
0 | 1590 if (level) { |
1591 if (level < 0) { | |
1592 level = -level; | |
200 | 1593 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 1594 level = (level - 1) | 1; |
1595 level = -level; | |
1596 } else { | |
200 | 1597 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 1598 level = (level - 1) | 1; |
1599 } | |
1600 #ifdef PARANOID | |
1601 if (level < -2048 || level > 2047) | |
1602 fprintf(stderr, "unquant error %d %d\n", i, level); | |
1603 #endif | |
200 | 1604 block[j] = level; |
0 | 1605 } |
1606 } | |
1607 } else { | |
1608 i = 0; | |
1609 quant_matrix = s->non_intra_matrix; | |
217 | 1610 for(;i<nCoeffs;i++) { |
200 | 1611 int j= zigzag_direct[i]; |
1612 level = block[j]; | |
0 | 1613 if (level) { |
1614 if (level < 0) { | |
1615 level = -level; | |
1616 level = (((level << 1) + 1) * qscale * | |
200 | 1617 ((int) (quant_matrix[j]))) >> 4; |
0 | 1618 level = (level - 1) | 1; |
1619 level = -level; | |
1620 } else { | |
1621 level = (((level << 1) + 1) * qscale * | |
200 | 1622 ((int) (quant_matrix[j]))) >> 4; |
0 | 1623 level = (level - 1) | 1; |
1624 } | |
1625 #ifdef PARANOID | |
1626 if (level < -2048 || level > 2047) | |
1627 fprintf(stderr, "unquant error %d %d\n", i, level); | |
1628 #endif | |
200 | 1629 block[j] = level; |
0 | 1630 } |
1631 } | |
1632 } | |
1633 } | |
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
|
1634 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1635 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
|
1636 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
|
1637 { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1638 int i, level, qmul, qadd; |
200 | 1639 int nCoeffs; |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1640 |
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
|
1641 if (s->mb_intra) { |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1642 if (!s->h263_aic) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1643 if (n < 4) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1644 block[0] = block[0] * s->y_dc_scale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1645 else |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1646 block[0] = block[0] * s->c_dc_scale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1647 } |
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
|
1648 i = 1; |
200 | 1649 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
|
1650 } 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
|
1651 i = 0; |
200 | 1652 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
|
1653 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1654 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1655 qmul = s->qscale << 1; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1656 if (s->h263_aic && s->mb_intra) |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1657 qadd = 0; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1658 else |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1659 qadd = (s->qscale - 1) | 1; |
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
|
1660 |
200 | 1661 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
|
1662 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
|
1663 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
|
1664 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
|
1665 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
|
1666 } 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
|
1667 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
|
1668 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1669 #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
|
1670 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
|
1671 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
|
1672 #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
|
1673 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
|
1674 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1675 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1676 } |
0 | 1677 |
1678 /* rate control */ | |
1679 | |
1680 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */ | |
1681 #define I_FRAME_SIZE_RATIO 3.0 | |
1682 #define QSCALE_K 20 | |
1683 | |
1684 static void rate_control_init(MpegEncContext *s) | |
1685 { | |
268 | 1686 #if 1 |
1687 emms_c(); | |
1688 | |
1689 //initial values, they dont really matter as they will be totally different within a few frames | |
1690 s->i_pred.coeff= s->p_pred.coeff= 7.0; | |
1691 s->i_pred.count= s->p_pred.count= 1.0; | |
1692 | |
1693 s->i_pred.decay= s->p_pred.decay= 0.4; | |
1694 | |
1695 // use more bits at the beginning, otherwise high motion at the begin will look like shit | |
1696 s->qsum=100; | |
1697 s->qcount=100; | |
1698 | |
1699 s->short_term_qsum=0.001; | |
1700 s->short_term_qcount=0.001; | |
1701 #else | |
0 | 1702 s->wanted_bits = 0; |
1703 | |
1704 if (s->intra_only) { | |
1705 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate; | |
1706 s->P_frame_bits = s->I_frame_bits; | |
1707 } else { | |
1708 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / | |
1709 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1))); | |
1710 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO); | |
1711 } | |
268 | 1712 |
0 | 1713 #if defined(DEBUG) |
1714 printf("I_frame_size=%d P_frame_size=%d\n", | |
1715 s->I_frame_bits, s->P_frame_bits); | |
1716 #endif | |
268 | 1717 #endif |
1718 } | |
1719 | |
1720 static double predict(Predictor *p, double q, double var) | |
1721 { | |
1722 return p->coeff*var / (q*p->count); | |
1723 } | |
1724 | |
1725 static void update_predictor(Predictor *p, double q, double var, double size) | |
1726 { | |
1727 double new_coeff= size*q / (var + 1); | |
1728 if(var<1000) return; | |
1729 /*{ | |
1730 int pred= predict(p, q, var); | |
1731 int error= abs(pred-size); | |
1732 static double sum=0; | |
1733 static int count=0; | |
1734 if(count>5) sum+=error; | |
1735 count++; | |
1736 if(256*256*256*64%count==0){ | |
1737 printf("%d %f %f\n", count, sum/count, p->coeff); | |
1738 } | |
1739 }*/ | |
1740 p->count*= p->decay; | |
1741 p->coeff*= p->decay; | |
1742 p->count++; | |
1743 p->coeff+= new_coeff; | |
0 | 1744 } |
1745 | |
1746 static int rate_estimate_qscale(MpegEncContext *s) | |
1747 { | |
268 | 1748 #if 1 |
1749 int qmin= s->qmin; | |
1750 int qmax= s->qmax; | |
1751 int rate_q=5; | |
1752 float q; | |
1753 int qscale; | |
1754 float br_compensation; | |
1755 double diff; | |
1756 double short_term_q; | |
1757 double long_term_q; | |
1758 int last_qscale= s->qscale; | |
1759 double fps; | |
1760 INT64 wanted_bits; | |
1761 emms_c(); | |
1762 | |
1763 fps= (double)s->frame_rate / FRAME_RATE_BASE; | |
1764 wanted_bits= s->bit_rate*(double)s->picture_number/fps; | |
1765 | |
1766 | |
1767 if(s->picture_number>2){ | |
1768 /* update predictors */ | |
1769 if(s->last_pict_type == I_TYPE){ | |
1770 //FIXME | |
1771 }else{ //P Frame | |
1772 //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff); | |
1773 update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits); | |
1774 } | |
1775 } | |
1776 | |
1777 if(s->pict_type == I_TYPE){ | |
1778 //FIXME | |
1779 rate_q= s->qsum/s->qcount; | |
1780 }else{ //P Frame | |
1781 int i; | |
1782 int diff, best_diff=1000000000; | |
1783 for(i=1; i<=31; i++){ | |
1784 diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps; | |
1785 if(diff<0) diff= -diff; | |
1786 if(diff<best_diff){ | |
1787 best_diff= diff; | |
1788 rate_q= i; | |
1789 } | |
1790 } | |
1791 } | |
1792 | |
1793 s->short_term_qsum*=s->qblur; | |
1794 s->short_term_qcount*=s->qblur; | |
1795 | |
1796 s->short_term_qsum+= rate_q; | |
1797 s->short_term_qcount++; | |
1798 short_term_q= s->short_term_qsum/s->short_term_qcount; | |
1799 | |
1800 long_term_q= s->qsum/s->qcount*s->total_bits/wanted_bits; | |
1801 | |
1802 // q= (long_term_q - short_term_q)*s->qcompress + short_term_q; | |
1803 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q); | |
1804 | |
1805 diff= s->total_bits - wanted_bits; | |
1806 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance; | |
270 | 1807 if(br_compensation<=0.0) br_compensation=0.001; |
268 | 1808 q/=br_compensation; |
1809 | |
1810 qscale= (int)(q + 0.5); | |
1811 if (qscale<qmin) qscale=qmin; | |
1812 else if(qscale>qmax) qscale=qmax; | |
1813 | |
1814 if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff; | |
1815 else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff; | |
1816 | |
1817 s->qsum+= qscale; | |
1818 s->qcount++; | |
1819 | |
1820 s->last_pict_type= s->pict_type; | |
1821 //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%d fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, | |
1822 // rate_q, (int)short_term_q, s->mc_mb_var, s->frame_bits); | |
1823 //printf("%d %d\n", s->bit_rate, (int)fps); | |
1824 return qscale; | |
1825 #else | |
187
3f3b14d3a23d
qscale estimate fix, diff extended to 64bit wide. patch by Stephen Davies <steve@daviesfam.org>
arpi_esp
parents:
178
diff
changeset
|
1826 INT64 diff, total_bits = s->total_bits; |
0 | 1827 float q; |
268 | 1828 int qscale; |
0 | 1829 if (s->pict_type == I_TYPE) { |
1830 s->wanted_bits += s->I_frame_bits; | |
1831 } else { | |
1832 s->wanted_bits += s->P_frame_bits; | |
1833 } | |
1834 diff = s->wanted_bits - total_bits; | |
1835 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width); | |
1836 /* adjust for I frame */ | |
1837 if (s->pict_type == I_TYPE && !s->intra_only) { | |
1838 q /= I_FRAME_SIZE_RATIO; | |
1839 } | |
1840 | |
1841 /* using a too small Q scale leeds to problems in mpeg1 and h263 | |
1842 because AC coefficients are clamped to 255 or 127 */ | |
1843 qmin = 3; | |
1844 if (q < qmin) | |
1845 q = qmin; | |
1846 else if (q > 31) | |
1847 q = 31; | |
1848 qscale = (int)(q + 0.5); | |
1849 #if defined(DEBUG) | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1850 printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n", |
0 | 1851 s->picture_number, |
64 | 1852 (double)total_bits, |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1853 (double)s->wanted_bits, |
0 | 1854 (float)s->frame_rate / FRAME_RATE_BASE * |
1855 total_bits / s->picture_number, | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1856 (int)diff, q); |
0 | 1857 #endif |
1858 return qscale; | |
268 | 1859 #endif |
0 | 1860 } |
1861 | |
1862 AVCodec mpeg1video_encoder = { | |
1863 "mpeg1video", | |
1864 CODEC_TYPE_VIDEO, | |
1865 CODEC_ID_MPEG1VIDEO, | |
1866 sizeof(MpegEncContext), | |
1867 MPV_encode_init, | |
1868 MPV_encode_picture, | |
1869 MPV_encode_end, | |
1870 }; | |
1871 | |
1872 AVCodec h263_encoder = { | |
1873 "h263", | |
1874 CODEC_TYPE_VIDEO, | |
1875 CODEC_ID_H263, | |
1876 sizeof(MpegEncContext), | |
1877 MPV_encode_init, | |
1878 MPV_encode_picture, | |
1879 MPV_encode_end, | |
1880 }; | |
1881 | |
1882 AVCodec h263p_encoder = { | |
1883 "h263p", | |
1884 CODEC_TYPE_VIDEO, | |
1885 CODEC_ID_H263P, | |
1886 sizeof(MpegEncContext), | |
1887 MPV_encode_init, | |
1888 MPV_encode_picture, | |
1889 MPV_encode_end, | |
1890 }; | |
1891 | |
1892 AVCodec rv10_encoder = { | |
1893 "rv10", | |
1894 CODEC_TYPE_VIDEO, | |
1895 CODEC_ID_RV10, | |
1896 sizeof(MpegEncContext), | |
1897 MPV_encode_init, | |
1898 MPV_encode_picture, | |
1899 MPV_encode_end, | |
1900 }; | |
1901 | |
1902 AVCodec mjpeg_encoder = { | |
1903 "mjpeg", | |
1904 CODEC_TYPE_VIDEO, | |
1905 CODEC_ID_MJPEG, | |
1906 sizeof(MpegEncContext), | |
1907 MPV_encode_init, | |
1908 MPV_encode_picture, | |
1909 MPV_encode_end, | |
1910 }; | |
1911 | |
71 | 1912 AVCodec mpeg4_encoder = { |
1913 "mpeg4", | |
0 | 1914 CODEC_TYPE_VIDEO, |
71 | 1915 CODEC_ID_MPEG4, |
0 | 1916 sizeof(MpegEncContext), |
1917 MPV_encode_init, | |
1918 MPV_encode_picture, | |
1919 MPV_encode_end, | |
1920 }; | |
1921 | |
1922 AVCodec msmpeg4_encoder = { | |
1923 "msmpeg4", | |
1924 CODEC_TYPE_VIDEO, | |
1925 CODEC_ID_MSMPEG4, | |
1926 sizeof(MpegEncContext), | |
1927 MPV_encode_init, | |
1928 MPV_encode_picture, | |
1929 MPV_encode_end, | |
1930 }; |