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