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