Mercurial > libavcodec.hg
annotate mpegvideo.c @ 498:0b4450c15067 libavcodec
dc scale simplification/optimization
author | michaelni |
---|---|
date | Tue, 18 Jun 2002 00:46:02 +0000 |
parents | cee02f2f4a5a |
children | 8b7a54d58549 |
rev | line source |
---|---|
0 | 1 /* |
2 * The simplest mpeg encoder (well, it was the simplest!) | |
429 | 3 * Copyright (c) 2000,2001 Fabrice Bellard. |
0 | 4 * |
429 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
295 | 18 * |
325 | 19 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at> |
0 | 20 */ |
21 #include "avcodec.h" | |
22 #include "dsputil.h" | |
23 #include "mpegvideo.h" | |
24 | |
17
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
25 #ifdef USE_FASTMEMCPY |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
26 #include "fastmemcpy.h" |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
27 #endif |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
28 |
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
|
29 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
|
30 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
|
31 DCTELEM *block, int n, int qscale); |
325 | 32 static void dct_unquantize_mpeg2_c(MpegEncContext *s, |
33 DCTELEM *block, int n, int qscale); | |
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
|
34 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
|
35 DCTELEM *block, int n, int qscale); |
206 | 36 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w); |
344 | 37 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); |
206 | 38 |
344 | 39 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow)= dct_quantize_c; |
206 | 40 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c; |
41 | |
0 | 42 #define EDGE_WIDTH 16 |
43 | |
44 /* enable all paranoid tests for rounding, overflows, etc... */ | |
45 //#define PARANOID | |
46 | |
47 //#define DEBUG | |
48 | |
321 | 49 |
0 | 50 /* for jpeg fast DCT */ |
51 #define CONST_BITS 14 | |
52 | |
53 static const unsigned short aanscales[64] = { | |
54 /* precomputed values scaled up by 14 bits */ | |
55 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
56 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
57 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
58 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
59 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
60 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
61 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
62 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
63 }; | |
64 | |
65 static UINT8 h263_chroma_roundtab[16] = { | |
66 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | |
67 }; | |
68 | |
292
73a9ce3d9715
fcode_tables where too small, found by Klaas-Pieter Vlieg <vlieg@eurescom.de>
michaelni
parents:
288
diff
changeset
|
69 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
|
70 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
|
71 |
321 | 72 extern UINT8 zigzag_end[64]; |
73 | |
0 | 74 /* default motion estimation */ |
321 | 75 int motion_estimation_method = ME_EPZS; |
200 | 76 |
344 | 77 static void convert_matrix(int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64], |
78 const UINT16 *quant_matrix, int bias) | |
0 | 79 { |
344 | 80 int qscale; |
0 | 81 |
344 | 82 for(qscale=1; qscale<32; qscale++){ |
83 int i; | |
474
11dbd00682fc
avoid name clash with libjpeg - added missing externs
bellard
parents:
467
diff
changeset
|
84 if (av_fdct == fdct_ifast) { |
344 | 85 for(i=0;i<64;i++) { |
86 const int j= block_permute_op(i); | |
87 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
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 */ | |
91 | |
92 qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / | |
93 (aanscales[i] * qscale * quant_matrix[j])); | |
94 } | |
95 } else { | |
96 for(i=0;i<64;i++) { | |
97 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
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 | |
101 */ | |
102 qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); | |
103 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]); | |
104 | |
105 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1; | |
106 | |
107 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]); | |
108 } | |
0 | 109 } |
110 } | |
111 } | |
456 | 112 // move into common.c perhaps |
113 #define CHECKED_ALLOCZ(p, size)\ | |
114 {\ | |
115 p= av_mallocz(size);\ | |
116 if(p==NULL){\ | |
117 perror("malloc");\ | |
118 goto fail;\ | |
119 }\ | |
120 } | |
0 | 121 |
122 /* init common structure for both encoder and decoder */ | |
123 int MPV_common_init(MpegEncContext *s) | |
124 { | |
125 int c_size, i; | |
126 UINT8 *pict; | |
127 | |
312 | 128 s->dct_unquantize_h263 = dct_unquantize_h263_c; |
325 | 129 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c; |
130 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_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
|
131 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
132 #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
|
133 MPV_common_init_mmx(s); |
8 | 134 #endif |
312 | 135 //setup default unquantizers (mpeg4 might change it later) |
136 if(s->out_format == FMT_H263) | |
137 s->dct_unquantize = s->dct_unquantize_h263; | |
138 else | |
325 | 139 s->dct_unquantize = s->dct_unquantize_mpeg1; |
312 | 140 |
0 | 141 s->mb_width = (s->width + 15) / 16; |
142 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
|
143 s->mb_num = s->mb_width * s->mb_height; |
0 | 144 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH; |
145 | |
146 for(i=0;i<3;i++) { | |
147 int w, h, shift, pict_start; | |
148 | |
149 w = s->linesize; | |
150 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | |
151 shift = (i == 0) ? 0 : 1; | |
152 c_size = (w >> shift) * (h >> shift); | |
153 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | |
154 | |
456 | 155 CHECKED_ALLOCZ(pict, c_size) |
0 | 156 s->last_picture_base[i] = pict; |
157 s->last_picture[i] = pict + pict_start; | |
485 | 158 if(i>0) memset(s->last_picture_base[i], 128, c_size); |
0 | 159 |
456 | 160 CHECKED_ALLOCZ(pict, c_size) |
0 | 161 s->next_picture_base[i] = pict; |
162 s->next_picture[i] = pict + pict_start; | |
485 | 163 if(i>0) memset(s->next_picture_base[i], 128, c_size); |
324 | 164 |
365
fdeec2834c79
there are divx5? encoded files without a userdata section but with b-frames :(
michaelni
parents:
350
diff
changeset
|
165 if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) { |
fdeec2834c79
there are divx5? encoded files without a userdata section but with b-frames :(
michaelni
parents:
350
diff
changeset
|
166 /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but |
fdeec2834c79
there are divx5? encoded files without a userdata section but with b-frames :(
michaelni
parents:
350
diff
changeset
|
167 do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */ |
456 | 168 CHECKED_ALLOCZ(pict, c_size) |
0 | 169 s->aux_picture_base[i] = pict; |
170 s->aux_picture[i] = pict + pict_start; | |
485 | 171 if(i>0) memset(s->aux_picture_base[i], 128, c_size); |
0 | 172 } |
173 } | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
174 |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
175 if (s->encoding) { |
324 | 176 int j; |
177 int mv_table_size= (s->mb_width+2)*(s->mb_height+2); | |
239 | 178 |
456 | 179 CHECKED_ALLOCZ(s->mb_var , s->mb_num * sizeof(INT16)) |
180 CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16)) | |
324 | 181 |
182 /* Allocate MV tables */ | |
456 | 183 CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16)) |
184 CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
185 CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
186 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
187 CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
188 CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16)) | |
189 CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16)) | |
190 CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
324 | 191 |
456 | 192 CHECKED_ALLOCZ(s->me_scratchpad, s->linesize*16*3*sizeof(uint8_t)) |
193 | |
194 CHECKED_ALLOCZ(s->me_map , ME_MAP_SIZE*sizeof(uint32_t)) | |
195 CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t)) | |
327 | 196 |
324 | 197 if(s->max_b_frames){ |
198 for(j=0; j<REORDER_BUFFER_SIZE; j++){ | |
199 int i; | |
200 for(i=0;i<3;i++) { | |
201 int w, h, shift; | |
202 | |
203 w = s->linesize; | |
204 h = s->mb_height * 16; | |
205 shift = (i == 0) ? 0 : 1; | |
206 c_size = (w >> shift) * (h >> shift); | |
207 | |
456 | 208 CHECKED_ALLOCZ(pict, c_size); |
324 | 209 s->picture_buffer[j][i] = pict; |
210 } | |
211 } | |
212 } | |
456 | 213 |
214 if(s->codec_id==CODEC_ID_MPEG4){ | |
215 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE); | |
216 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE); | |
217 } | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
218 } |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
219 |
288 | 220 if (s->out_format == FMT_H263 || s->encoding) { |
0 | 221 int size; |
456 | 222 /* Allocate MB type table */ |
223 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8)) | |
224 | |
0 | 225 /* MV prediction */ |
226 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
456 | 227 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16)); |
0 | 228 } |
229 | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
230 if (s->h263_pred || s->h263_plus) { |
0 | 231 int y_size, c_size, i, size; |
232 | |
233 /* dc values */ | |
234 | |
235 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
236 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
237 size = y_size + 2 * c_size; | |
456 | 238 CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16)); |
0 | 239 s->dc_val[1] = s->dc_val[0] + y_size; |
240 s->dc_val[2] = s->dc_val[1] + c_size; | |
241 for(i=0;i<size;i++) | |
242 s->dc_val[0][i] = 1024; | |
243 | |
244 /* ac values */ | |
456 | 245 CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16); |
0 | 246 s->ac_val[1] = s->ac_val[0] + y_size; |
247 s->ac_val[2] = s->ac_val[1] + c_size; | |
248 | |
249 /* cbp values */ | |
456 | 250 CHECKED_ALLOCZ(s->coded_block, y_size); |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
251 |
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
252 /* which mb is a intra block */ |
456 | 253 CHECKED_ALLOCZ(s->mbintra_table, s->mb_num); |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
254 memset(s->mbintra_table, 1, s->mb_num); |
333 | 255 |
256 /* divx501 bitstream reorder buffer */ | |
456 | 257 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE); |
258 | |
259 /* cbp, ac_pred, pred_dir */ | |
260 CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8)) | |
261 CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8)) | |
262 | |
263 CHECKED_ALLOCZ(s->qscale_table , s->mb_num * sizeof(UINT8)) | |
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
|
264 } |
0 | 265 /* default structure is frame */ |
266 s->picture_structure = PICT_FRAME; | |
267 | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
268 /* init macroblock skip table */ |
456 | 269 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num); |
294 | 270 |
327 | 271 s->block= s->blocks[0]; |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
272 |
0 | 273 s->context_initialized = 1; |
274 return 0; | |
275 fail: | |
244 | 276 MPV_common_end(s); |
277 return -1; | |
278 } | |
279 | |
456 | 280 |
281 //extern int sads; | |
282 | |
244 | 283 /* init common structure for both encoder and decoder */ |
284 void MPV_common_end(MpegEncContext *s) | |
285 { | |
286 int i; | |
287 | |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
288 av_freep(&s->mb_type); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
289 av_freep(&s->mb_var); |
456 | 290 av_freep(&s->mc_mb_var); |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
291 av_freep(&s->p_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
292 av_freep(&s->b_forw_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
293 av_freep(&s->b_back_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
294 av_freep(&s->b_bidir_forw_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
295 av_freep(&s->b_bidir_back_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
296 av_freep(&s->b_direct_forw_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
297 av_freep(&s->b_direct_back_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
298 av_freep(&s->b_direct_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
299 av_freep(&s->motion_val); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
300 av_freep(&s->dc_val[0]); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
301 av_freep(&s->ac_val[0]); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
302 av_freep(&s->coded_block); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
303 av_freep(&s->mbintra_table); |
456 | 304 av_freep(&s->cbp_table); |
305 av_freep(&s->pred_dir_table); | |
306 av_freep(&s->qscale_table); | |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
307 av_freep(&s->me_scratchpad); |
456 | 308 av_freep(&s->me_map); |
309 av_freep(&s->me_score_map); | |
310 | |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
311 av_freep(&s->mbskip_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
312 av_freep(&s->bitstream_buffer); |
456 | 313 av_freep(&s->tex_pb_buffer); |
314 av_freep(&s->pb2_buffer); | |
0 | 315 for(i=0;i<3;i++) { |
324 | 316 int j; |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
317 av_freep(&s->last_picture_base[i]); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
318 av_freep(&s->next_picture_base[i]); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
319 av_freep(&s->aux_picture_base[i]); |
324 | 320 for(j=0; j<REORDER_BUFFER_SIZE; j++){ |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
321 av_freep(&s->picture_buffer[j][i]); |
324 | 322 } |
0 | 323 } |
324 s->context_initialized = 0; | |
325 } | |
326 | |
327 /* init video encoder */ | |
328 int MPV_encode_init(AVCodecContext *avctx) | |
329 { | |
330 MpegEncContext *s = avctx->priv_data; | |
60 | 331 int i; |
0 | 332 |
315 | 333 avctx->pix_fmt = PIX_FMT_YUV420P; |
334 | |
0 | 335 s->bit_rate = avctx->bit_rate; |
268 | 336 s->bit_rate_tolerance = avctx->bit_rate_tolerance; |
0 | 337 s->frame_rate = avctx->frame_rate; |
338 s->width = avctx->width; | |
339 s->height = avctx->height; | |
456 | 340 if(avctx->gop_size > 600){ |
462
3fdd0627f6bd
typo (found by Bohdan Horst <nexus at hoth.amu.edu.pl>)
michaelni
parents:
461
diff
changeset
|
341 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n"); |
456 | 342 avctx->gop_size=600; |
343 } | |
0 | 344 s->gop_size = avctx->gop_size; |
162 | 345 s->rtp_mode = avctx->rtp_mode; |
346 s->rtp_payload_size = avctx->rtp_payload_size; | |
231 | 347 if (avctx->rtp_callback) |
348 s->rtp_callback = avctx->rtp_callback; | |
268 | 349 s->qmin= avctx->qmin; |
350 s->qmax= avctx->qmax; | |
351 s->max_qdiff= avctx->max_qdiff; | |
352 s->qcompress= avctx->qcompress; | |
353 s->qblur= avctx->qblur; | |
329 | 354 s->b_quant_factor= avctx->b_quant_factor; |
456 | 355 s->b_quant_offset= avctx->b_quant_offset; |
194
27d1773552c9
mpeg4 encoder fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
191
diff
changeset
|
356 s->avctx = avctx; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
357 s->aspect_ratio_info= avctx->aspect_ratio_info; |
294 | 358 s->flags= avctx->flags; |
324 | 359 s->max_b_frames= avctx->max_b_frames; |
329 | 360 s->rc_strategy= avctx->rc_strategy; |
361 s->b_frame_strategy= avctx->b_frame_strategy; | |
344 | 362 s->codec_id= avctx->codec->id; |
456 | 363 s->luma_elim_threshold = avctx->luma_elim_threshold; |
364 s->chroma_elim_threshold= avctx->chroma_elim_threshold; | |
365 s->strict_std_compliance= avctx->strict_std_compliance; | |
366 s->data_partitioning= avctx->flags & CODEC_FLAG_PART; | |
344 | 367 |
0 | 368 if (s->gop_size <= 1) { |
369 s->intra_only = 1; | |
370 s->gop_size = 12; | |
371 } else { | |
372 s->intra_only = 0; | |
373 } | |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
374 |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
375 /* ME algorithm */ |
321 | 376 if (avctx->me_method == 0) |
377 /* For compatibility */ | |
378 s->me_method = motion_estimation_method; | |
379 else | |
380 s->me_method = avctx->me_method; | |
381 | |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
382 /* Fixed QSCALE */ |
0 | 383 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
|
384 |
0 | 385 switch(avctx->codec->id) { |
386 case CODEC_ID_MPEG1VIDEO: | |
387 s->out_format = FMT_MPEG1; | |
336 | 388 avctx->delay=0; //FIXME not sure, should check the spec |
0 | 389 break; |
390 case CODEC_ID_MJPEG: | |
391 s->out_format = FMT_MJPEG; | |
392 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
|
393 s->mjpeg_write_tables = 1; /* write all tables */ |
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
365
diff
changeset
|
394 s->mjpeg_data_only_frames = 0; /* write all the needed headers */ |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
395 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
|
396 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
|
397 s->mjpeg_vsample[2] = 1; |
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
365
diff
changeset
|
398 s->mjpeg_hsample[0] = 2; |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
399 s->mjpeg_hsample[1] = 1; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
400 s->mjpeg_hsample[2] = 1; |
0 | 401 if (mjpeg_init(s) < 0) |
402 return -1; | |
336 | 403 avctx->delay=0; |
0 | 404 break; |
405 case CODEC_ID_H263: | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
406 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
|
407 printf("Input picture size isn't suitable for h263 codec! try h263+\n"); |
0 | 408 return -1; |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
409 } |
0 | 410 s->out_format = FMT_H263; |
336 | 411 avctx->delay=0; |
0 | 412 break; |
413 case CODEC_ID_H263P: | |
414 s->out_format = FMT_H263; | |
162 | 415 s->rtp_mode = 1; |
416 s->rtp_payload_size = 1200; | |
0 | 417 s->h263_plus = 1; |
78 | 418 s->unrestricted_mv = 1; |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
419 s->h263_aic = 1; |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
420 |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
421 /* These are just to be sure */ |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
422 s->umvplus = 0; |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
423 s->umvplus_dec = 0; |
336 | 424 avctx->delay=0; |
0 | 425 break; |
426 case CODEC_ID_RV10: | |
427 s->out_format = FMT_H263; | |
428 s->h263_rv10 = 1; | |
336 | 429 avctx->delay=0; |
0 | 430 break; |
71 | 431 case CODEC_ID_MPEG4: |
0 | 432 s->out_format = FMT_H263; |
433 s->h263_pred = 1; | |
434 s->unrestricted_mv = 1; | |
324 | 435 s->has_b_frames= s->max_b_frames ? 1 : 0; |
336 | 436 s->low_delay=0; |
437 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); | |
0 | 438 break; |
307 | 439 case CODEC_ID_MSMPEG4V1: |
0 | 440 s->out_format = FMT_H263; |
441 s->h263_msmpeg4 = 1; | |
442 s->h263_pred = 1; | |
443 s->unrestricted_mv = 1; | |
307 | 444 s->msmpeg4_version= 1; |
336 | 445 avctx->delay=0; |
307 | 446 break; |
447 case CODEC_ID_MSMPEG4V2: | |
448 s->out_format = FMT_H263; | |
449 s->h263_msmpeg4 = 1; | |
450 s->h263_pred = 1; | |
451 s->unrestricted_mv = 1; | |
452 s->msmpeg4_version= 2; | |
336 | 453 avctx->delay=0; |
307 | 454 break; |
455 case CODEC_ID_MSMPEG4V3: | |
456 s->out_format = FMT_H263; | |
457 s->h263_msmpeg4 = 1; | |
458 s->h263_pred = 1; | |
459 s->unrestricted_mv = 1; | |
460 s->msmpeg4_version= 3; | |
336 | 461 avctx->delay=0; |
0 | 462 break; |
463 default: | |
464 return -1; | |
465 } | |
295 | 466 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
467 { /* 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
|
468 static int done=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
469 if(!done){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
470 int i; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
471 done=1; |
292
73a9ce3d9715
fcode_tables where too small, found by Klaas-Pieter Vlieg <vlieg@eurescom.de>
michaelni
parents:
288
diff
changeset
|
472 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
|
473 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
|
474 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
475 for(i=-16; i<16; i++){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
476 default_fcode_tab[i + MAX_MV]= 1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
477 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
478 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
479 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
480 s->mv_penalty= default_mv_penalty; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
481 s->fcode_tab= default_fcode_tab; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
482 |
0 | 483 if (s->out_format == FMT_H263) |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
484 h263_encode_init(s); |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
485 else if (s->out_format == FMT_MPEG1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
486 mpeg1_encode_init(s); |
0 | 487 |
287 | 488 /* dont use mv_penalty table for crap MV as it would be confused */ |
324 | 489 if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty; |
287 | 490 |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
491 s->encoding = 1; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
492 |
0 | 493 /* init */ |
494 if (MPV_common_init(s) < 0) | |
495 return -1; | |
496 | |
60 | 497 /* init default q matrix */ |
498 for(i=0;i<64;i++) { | |
344 | 499 if(s->out_format == FMT_H263) |
500 s->intra_matrix[i] = default_non_intra_matrix[i]; | |
501 else | |
502 s->intra_matrix[i] = default_intra_matrix[i]; | |
503 | |
504 s->inter_matrix[i] = default_non_intra_matrix[i]; | |
505 } | |
506 | |
507 /* precompute matrix */ | |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
508 /* for mjpeg, we do include qscale in the matrix */ |
344 | 509 if (s->out_format != FMT_MJPEG) { |
510 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, | |
511 s->intra_matrix, s->intra_quant_bias); | |
512 convert_matrix(s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, | |
513 s->inter_matrix, s->inter_quant_bias); | |
60 | 514 } |
515 | |
329 | 516 if(ff_rate_control_init(s) < 0) |
517 return -1; | |
0 | 518 |
519 s->picture_number = 0; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
520 s->picture_in_gop_number = 0; |
0 | 521 s->fake_picture_number = 0; |
522 /* motion detector init */ | |
523 s->f_code = 1; | |
324 | 524 s->b_code = 1; |
0 | 525 |
526 return 0; | |
527 } | |
528 | |
529 int MPV_encode_end(AVCodecContext *avctx) | |
530 { | |
531 MpegEncContext *s = avctx->priv_data; | |
532 | |
533 #ifdef STATS | |
534 print_stats(); | |
535 #endif | |
329 | 536 |
537 ff_rate_control_uninit(s); | |
538 | |
0 | 539 MPV_common_end(s); |
540 if (s->out_format == FMT_MJPEG) | |
541 mjpeg_close(s); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
542 |
0 | 543 return 0; |
544 } | |
545 | |
546 /* draw the edges of width 'w' of an image of size width, height */ | |
206 | 547 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w) |
0 | 548 { |
549 UINT8 *ptr, *last_line; | |
550 int i; | |
551 | |
552 last_line = buf + (height - 1) * wrap; | |
553 for(i=0;i<w;i++) { | |
554 /* top and bottom */ | |
555 memcpy(buf - (i + 1) * wrap, buf, width); | |
556 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
557 } | |
558 /* left and right */ | |
559 ptr = buf; | |
560 for(i=0;i<height;i++) { | |
561 memset(ptr - w, ptr[0], w); | |
562 memset(ptr + width, ptr[width-1], w); | |
563 ptr += wrap; | |
564 } | |
565 /* corners */ | |
566 for(i=0;i<w;i++) { | |
567 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
568 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
569 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
570 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
571 } | |
572 } | |
573 | |
574 /* generic function for encode/decode called before a frame is coded/decoded */ | |
575 void MPV_frame_start(MpegEncContext *s) | |
576 { | |
577 int i; | |
578 UINT8 *tmp; | |
579 | |
46
931417475f5b
fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents:
40
diff
changeset
|
580 s->mb_skiped = 0; |
456 | 581 s->decoding_error=0; |
582 | |
0 | 583 if (s->pict_type == B_TYPE) { |
584 for(i=0;i<3;i++) { | |
585 s->current_picture[i] = s->aux_picture[i]; | |
586 } | |
587 } else { | |
588 for(i=0;i<3;i++) { | |
589 /* swap next and last */ | |
590 tmp = s->last_picture[i]; | |
591 s->last_picture[i] = s->next_picture[i]; | |
592 s->next_picture[i] = tmp; | |
593 s->current_picture[i] = tmp; | |
594 } | |
595 } | |
596 } | |
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
|
597 |
0 | 598 /* generic function for encode/decode called after a frame has been coded/decoded */ |
599 void MPV_frame_end(MpegEncContext *s) | |
600 { | |
456 | 601 // if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads); |
602 | |
0 | 603 /* draw edge for correct motion prediction if outside */ |
220 | 604 if (s->pict_type != B_TYPE && !s->intra_only) { |
461 | 605 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
|
606 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
|
607 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
|
608 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
|
609 }else{ |
257 | 610 /* mpeg4? / opendivx / xvid */ |
0 | 611 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH); |
612 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
613 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
|
614 } |
0 | 615 } |
207 | 616 emms_c(); |
329 | 617 |
618 if(s->pict_type!=B_TYPE){ | |
619 s->last_non_b_pict_type= s->pict_type; | |
620 s->last_non_b_qscale= s->qscale; | |
456 | 621 s->last_non_b_mc_mb_var= s->mc_mb_var_sum; |
341 | 622 s->num_available_buffers++; |
623 if(s->num_available_buffers>2) s->num_available_buffers= 2; | |
329 | 624 } |
0 | 625 } |
626 | |
324 | 627 /* reorder input for encoding */ |
628 void reorder_input(MpegEncContext *s, AVPicture *pict) | |
629 { | |
630 int i, j, index; | |
631 | |
632 if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES; | |
633 | |
634 // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff) | |
635 | |
636 for(j=0; j<REORDER_BUFFER_SIZE-1; j++){ | |
637 s->coded_order[j]= s->coded_order[j+1]; | |
638 } | |
639 s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers | |
339 | 640 s->coded_order[j].pict_type=0; |
324 | 641 |
642 switch(s->input_pict_type){ | |
643 default: | |
644 case I_TYPE: | |
645 case S_TYPE: | |
646 case P_TYPE: | |
647 index= s->max_b_frames - s->b_frames_since_non_b; | |
648 s->b_frames_since_non_b=0; | |
649 break; | |
650 case B_TYPE: | |
651 index= s->max_b_frames + 1; | |
652 s->b_frames_since_non_b++; | |
653 break; | |
654 } | |
655 //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize); | |
656 if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED)) | |
657 && pict->linesize[0] == s->linesize | |
658 && pict->linesize[1] == s->linesize>>1 | |
659 && pict->linesize[2] == s->linesize>>1){ | |
660 //printf("ptr\n"); | |
661 for(i=0; i<3; i++){ | |
662 s->coded_order[index].picture[i]= pict->data[i]; | |
663 } | |
664 }else{ | |
665 //printf("copy\n"); | |
666 for(i=0; i<3; i++){ | |
667 uint8_t *src = pict->data[i]; | |
668 uint8_t *dest; | |
669 int src_wrap = pict->linesize[i]; | |
670 int dest_wrap = s->linesize; | |
671 int w = s->width; | |
672 int h = s->height; | |
673 | |
674 if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering | |
675 else dest= s->picture_buffer[s->picture_buffer_index][i]; | |
676 | |
677 if (i >= 1) { | |
678 dest_wrap >>= 1; | |
679 w >>= 1; | |
680 h >>= 1; | |
681 } | |
682 | |
683 s->coded_order[index].picture[i]= dest; | |
684 for(j=0;j<h;j++) { | |
685 memcpy(dest, src, w); | |
686 dest += dest_wrap; | |
687 src += src_wrap; | |
688 } | |
689 } | |
690 if(index!=0){ | |
691 s->picture_buffer_index++; | |
692 if(s->picture_buffer_index >= REORDER_BUFFER_SIZE-1) s->picture_buffer_index=0; | |
693 } | |
694 } | |
695 s->coded_order[index].pict_type = s->input_pict_type; | |
696 s->coded_order[index].qscale = s->input_qscale; | |
697 s->coded_order[index].force_type= s->force_input_type; | |
698 s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number; | |
699 s->coded_order[index].picture_number= s->input_picture_number; | |
700 | |
701 for(i=0; i<3; i++){ | |
702 s->new_picture[i]= s->coded_order[0].picture[i]; | |
703 } | |
704 } | |
705 | |
0 | 706 int MPV_encode_picture(AVCodecContext *avctx, |
707 unsigned char *buf, int buf_size, void *data) | |
708 { | |
709 MpegEncContext *s = avctx->priv_data; | |
710 AVPicture *pict = data; | |
711 | |
324 | 712 s->input_qscale = avctx->quality; |
0 | 713 |
714 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
715 | |
329 | 716 if(avctx->flags&CODEC_FLAG_TYPE){ |
717 s->input_pict_type= | |
718 s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE; | |
719 }else if(s->flags&CODEC_FLAG_PASS2){ | |
720 s->input_pict_type= | |
721 s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type; | |
722 }else{ | |
723 s->force_input_type=0; | |
724 if (!s->intra_only) { | |
725 /* first picture of GOP is intra */ | |
726 if (s->input_picture_in_gop_number % s->gop_size==0){ | |
727 s->input_pict_type = I_TYPE; | |
728 }else if(s->max_b_frames==0){ | |
729 s->input_pict_type = P_TYPE; | |
730 }else{ | |
731 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ | |
732 s->input_pict_type = B_TYPE; | |
733 else | |
734 s->input_pict_type = P_TYPE; | |
735 } | |
736 } else { | |
324 | 737 s->input_pict_type = I_TYPE; |
738 } | |
0 | 739 } |
740 | |
329 | 741 if(s->input_pict_type==I_TYPE) |
742 s->input_picture_in_gop_number=0; | |
743 | |
324 | 744 reorder_input(s, pict); |
745 | |
746 /* output? */ | |
747 if(s->coded_order[0].picture[0]){ | |
748 | |
749 s->pict_type= s->coded_order[0].pict_type; | |
750 if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */ | |
751 s->qscale= s->coded_order[0].qscale; | |
752 s->force_type= s->coded_order[0].force_type; | |
753 s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number; | |
754 s->picture_number= s->coded_order[0].picture_number; | |
755 | |
756 MPV_frame_start(s); | |
286 | 757 |
324 | 758 encode_picture(s, s->picture_number); |
376 | 759 avctx->key_frame = (s->pict_type == I_TYPE); |
760 avctx->pict_type = s->pict_type; | |
761 avctx->real_pict_num = s->picture_number; | |
324 | 762 avctx->header_bits = s->header_bits; |
763 avctx->mv_bits = s->mv_bits; | |
764 avctx->misc_bits = s->misc_bits; | |
765 avctx->i_tex_bits = s->i_tex_bits; | |
766 avctx->p_tex_bits = s->p_tex_bits; | |
767 avctx->i_count = s->i_count; | |
768 avctx->p_count = s->p_count; | |
769 avctx->skip_count = s->skip_count; | |
0 | 770 |
324 | 771 MPV_frame_end(s); |
772 | |
773 if (s->out_format == FMT_MJPEG) | |
774 mjpeg_picture_trailer(s); | |
775 | |
776 avctx->quality = s->qscale; | |
329 | 777 |
778 if(s->flags&CODEC_FLAG_PASS1) | |
779 ff_write_pass1_stats(s); | |
456 | 780 |
324 | 781 } |
782 | |
783 s->input_picture_number++; | |
784 s->input_picture_in_gop_number++; | |
0 | 785 |
786 flush_put_bits(&s->pb); | |
268 | 787 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; |
329 | 788 if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits; |
789 else s->pb_frame_bits= s->frame_bits; | |
790 | |
268 | 791 s->total_bits += s->frame_bits; |
286 | 792 avctx->frame_bits = s->frame_bits; |
793 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", | |
794 //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
|
795 |
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
796 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
|
797 /* 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
|
798 /* 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
|
799 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
|
800 pict->linesize, s->linesize, avctx); |
324 | 801 // printf("%f\n", avctx->psnr_y); |
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
802 } |
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
|
803 return pbBufPtr(&s->pb) - s->pb.buf; |
0 | 804 } |
805 | |
255 | 806 static inline void gmc1_motion(MpegEncContext *s, |
807 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
808 int dest_offset, | |
809 UINT8 **ref_picture, int src_offset, | |
810 int h) | |
811 { | |
812 UINT8 *ptr; | |
324 | 813 int offset, src_x, src_y, linesize; |
255 | 814 int motion_x, motion_y; |
815 | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
816 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n"); |
255 | 817 motion_x= s->sprite_offset[0][0]; |
818 motion_y= s->sprite_offset[0][1]; | |
819 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
820 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
821 motion_x<<=(3-s->sprite_warping_accuracy); | |
822 motion_y<<=(3-s->sprite_warping_accuracy); | |
823 src_x = clip(src_x, -16, s->width); | |
824 if (src_x == s->width) | |
825 motion_x =0; | |
826 src_y = clip(src_y, -16, s->height); | |
827 if (src_y == s->height) | |
828 motion_y =0; | |
829 | |
830 linesize = s->linesize; | |
831 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
832 | |
833 dest_y+=dest_offset; | |
834 gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
835 gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
836 | |
837 motion_x= s->sprite_offset[1][0]; | |
838 motion_y= s->sprite_offset[1][1]; | |
839 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
840 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
841 motion_x<<=(3-s->sprite_warping_accuracy); | |
842 motion_y<<=(3-s->sprite_warping_accuracy); | |
843 src_x = clip(src_x, -8, s->width>>1); | |
844 if (src_x == s->width>>1) | |
845 motion_x =0; | |
846 src_y = clip(src_y, -8, s->height>>1); | |
847 if (src_y == s->height>>1) | |
848 motion_y =0; | |
849 | |
850 offset = (src_y * linesize>>1) + src_x + (src_offset>>1); | |
851 ptr = ref_picture[1] + offset; | |
852 gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
853 ptr = ref_picture[2] + offset; | |
854 gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
855 | |
856 return; | |
857 } | |
858 | |
0 | 859 /* apply one mpeg motion vector to the three components */ |
860 static inline void mpeg_motion(MpegEncContext *s, | |
861 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
862 int dest_offset, | |
863 UINT8 **ref_picture, int src_offset, | |
864 int field_based, op_pixels_func *pix_op, | |
865 int motion_x, int motion_y, int h) | |
866 { | |
867 UINT8 *ptr; | |
868 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
255 | 869 if(s->quarter_sample) |
870 { | |
871 motion_x>>=1; | |
872 motion_y>>=1; | |
873 } | |
0 | 874 dxy = ((motion_y & 1) << 1) | (motion_x & 1); |
875 src_x = s->mb_x * 16 + (motion_x >> 1); | |
876 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
877 | |
878 /* WARNING: do no forget half pels */ | |
879 height = s->height >> field_based; | |
880 src_x = clip(src_x, -16, s->width); | |
881 if (src_x == s->width) | |
882 dxy &= ~1; | |
883 src_y = clip(src_y, -16, height); | |
884 if (src_y == height) | |
885 dxy &= ~2; | |
886 linesize = s->linesize << field_based; | |
887 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; | |
888 dest_y += dest_offset; | |
889 pix_op[dxy](dest_y, ptr, linesize, h); | |
890 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h); | |
891 | |
485 | 892 if(s->flags&CODEC_FLAG_GRAY) return; |
893 | |
0 | 894 if (s->out_format == FMT_H263) { |
895 dxy = 0; | |
896 if ((motion_x & 3) != 0) | |
897 dxy |= 1; | |
898 if ((motion_y & 3) != 0) | |
899 dxy |= 2; | |
900 mx = motion_x >> 2; | |
901 my = motion_y >> 2; | |
902 } else { | |
903 mx = motion_x / 2; | |
904 my = motion_y / 2; | |
905 dxy = ((my & 1) << 1) | (mx & 1); | |
906 mx >>= 1; | |
907 my >>= 1; | |
908 } | |
909 | |
910 src_x = s->mb_x * 8 + mx; | |
911 src_y = s->mb_y * (8 >> field_based) + my; | |
912 src_x = clip(src_x, -8, s->width >> 1); | |
913 if (src_x == (s->width >> 1)) | |
914 dxy &= ~1; | |
915 src_y = clip(src_y, -8, height >> 1); | |
916 if (src_y == (height >> 1)) | |
917 dxy &= ~2; | |
918 | |
919 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
920 ptr = ref_picture[1] + offset; | |
921 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
922 ptr = ref_picture[2] + offset; | |
923 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
924 } | |
925 | |
255 | 926 static inline void qpel_motion(MpegEncContext *s, |
927 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
928 int dest_offset, | |
929 UINT8 **ref_picture, int src_offset, | |
930 int field_based, op_pixels_func *pix_op, | |
931 qpel_mc_func *qpix_op, | |
932 int motion_x, int motion_y, int h) | |
933 { | |
934 UINT8 *ptr; | |
935 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
936 | |
937 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
938 src_x = s->mb_x * 16 + (motion_x >> 2); | |
939 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); | |
940 | |
941 height = s->height >> field_based; | |
942 src_x = clip(src_x, -16, s->width); | |
943 if (src_x == s->width) | |
944 dxy &= ~3; | |
945 src_y = clip(src_y, -16, height); | |
946 if (src_y == height) | |
947 dxy &= ~12; | |
948 linesize = s->linesize << field_based; | |
949 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
950 dest_y += dest_offset; | |
951 //printf("%d %d %d\n", src_x, src_y, dxy); | |
952 qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3); | |
953 qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3); | |
954 qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3); | |
955 qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3); | |
956 | |
485 | 957 if(s->flags&CODEC_FLAG_GRAY) return; |
958 | |
255 | 959 mx= (motion_x>>1) | (motion_x&1); |
960 my= (motion_y>>1) | (motion_y&1); | |
961 | |
962 dxy = 0; | |
963 if ((mx & 3) != 0) | |
964 dxy |= 1; | |
965 if ((my & 3) != 0) | |
966 dxy |= 2; | |
967 mx = mx >> 2; | |
968 my = my >> 2; | |
969 | |
970 src_x = s->mb_x * 8 + mx; | |
971 src_y = s->mb_y * (8 >> field_based) + my; | |
972 src_x = clip(src_x, -8, s->width >> 1); | |
973 if (src_x == (s->width >> 1)) | |
974 dxy &= ~1; | |
975 src_y = clip(src_y, -8, height >> 1); | |
976 if (src_y == (height >> 1)) | |
977 dxy &= ~2; | |
978 | |
979 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
980 ptr = ref_picture[1] + offset; | |
981 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
982 ptr = ref_picture[2] + offset; | |
983 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
984 } | |
985 | |
986 | |
0 | 987 static inline void MPV_motion(MpegEncContext *s, |
988 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
989 int dir, UINT8 **ref_picture, | |
255 | 990 op_pixels_func *pix_op, qpel_mc_func *qpix_op) |
0 | 991 { |
992 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
993 int mb_x, mb_y, i; | |
994 UINT8 *ptr, *dest; | |
995 | |
996 mb_x = s->mb_x; | |
997 mb_y = s->mb_y; | |
998 | |
999 switch(s->mv_type) { | |
1000 case MV_TYPE_16X16: | |
255 | 1001 if(s->mcsel){ |
1002 #if 0 | |
1003 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1004 ref_picture, 0, | |
1005 0, pix_op, | |
1006 s->sprite_offset[0][0]>>3, | |
1007 s->sprite_offset[0][1]>>3, | |
1008 16); | |
1009 #else | |
1010 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1011 ref_picture, 0, | |
1012 16); | |
1013 #endif | |
262 | 1014 }else if(s->quarter_sample && dir==0){ //FIXME |
255 | 1015 qpel_motion(s, dest_y, dest_cb, dest_cr, 0, |
1016 ref_picture, 0, | |
1017 0, pix_op, qpix_op, | |
1018 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
1019 }else{ | |
1020 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1021 ref_picture, 0, | |
1022 0, pix_op, | |
1023 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
1024 } | |
0 | 1025 break; |
1026 case MV_TYPE_8X8: | |
1027 for(i=0;i<4;i++) { | |
1028 motion_x = s->mv[dir][i][0]; | |
1029 motion_y = s->mv[dir][i][1]; | |
1030 | |
1031 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
1032 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
295 | 1033 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8; |
0 | 1034 |
1035 /* WARNING: do no forget half pels */ | |
1036 src_x = clip(src_x, -16, s->width); | |
1037 if (src_x == s->width) | |
1038 dxy &= ~1; | |
1039 src_y = clip(src_y, -16, s->height); | |
1040 if (src_y == s->height) | |
1041 dxy &= ~2; | |
1042 | |
1043 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
1044 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
1045 pix_op[dxy](dest, ptr, s->linesize, 8); | |
1046 } | |
485 | 1047 |
1048 if(s->flags&CODEC_FLAG_GRAY) break; | |
0 | 1049 /* In case of 8X8, we construct a single chroma motion vector |
1050 with a special rounding */ | |
1051 mx = 0; | |
1052 my = 0; | |
1053 for(i=0;i<4;i++) { | |
1054 mx += s->mv[dir][i][0]; | |
1055 my += s->mv[dir][i][1]; | |
1056 } | |
1057 if (mx >= 0) | |
1058 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
1059 else { | |
1060 mx = -mx; | |
1061 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
1062 } | |
1063 if (my >= 0) | |
1064 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
1065 else { | |
1066 my = -my; | |
1067 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
1068 } | |
1069 dxy = ((my & 1) << 1) | (mx & 1); | |
1070 mx >>= 1; | |
1071 my >>= 1; | |
1072 | |
1073 src_x = mb_x * 8 + mx; | |
1074 src_y = mb_y * 8 + my; | |
1075 src_x = clip(src_x, -8, s->width/2); | |
1076 if (src_x == s->width/2) | |
1077 dxy &= ~1; | |
1078 src_y = clip(src_y, -8, s->height/2); | |
1079 if (src_y == s->height/2) | |
1080 dxy &= ~2; | |
1081 | |
1082 offset = (src_y * (s->linesize >> 1)) + src_x; | |
1083 ptr = ref_picture[1] + offset; | |
1084 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8); | |
1085 ptr = ref_picture[2] + offset; | |
1086 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8); | |
1087 break; | |
1088 case MV_TYPE_FIELD: | |
1089 if (s->picture_structure == PICT_FRAME) { | |
1090 /* top field */ | |
1091 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1092 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
1093 1, pix_op, | |
1094 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
1095 /* bottom field */ | |
1096 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
1097 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
1098 1, pix_op, | |
1099 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
1100 } else { | |
1101 | |
1102 | |
1103 } | |
1104 break; | |
1105 } | |
1106 } | |
1107 | |
1108 | |
1109 /* put block[] to dest[] */ | |
1110 static inline void put_dct(MpegEncContext *s, | |
1111 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
1112 { | |
1113 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
|
1114 s->dct_unquantize(s, block, i, s->qscale); |
478 | 1115 ff_idct_put (dest, line_size, block); |
0 | 1116 } |
1117 | |
1118 /* add block[] to dest[] */ | |
1119 static inline void add_dct(MpegEncContext *s, | |
1120 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
1121 { | |
1122 if (s->block_last_index[i] >= 0) { | |
481 | 1123 ff_idct_add (dest, line_size, block); |
1124 } | |
1125 } | |
1126 | |
1127 static inline void add_dequant_dct(MpegEncContext *s, | |
1128 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
1129 { | |
1130 if (s->block_last_index[i] >= 0) { | |
1131 s->dct_unquantize(s, block, i, s->qscale); | |
324 | 1132 |
478 | 1133 ff_idct_add (dest, line_size, block); |
0 | 1134 } |
1135 } | |
1136 | |
456 | 1137 /** |
1138 * cleans dc, ac, coded_block for the current non intra MB | |
1139 */ | |
1140 void ff_clean_intra_table_entries(MpegEncContext *s) | |
1141 { | |
1142 int wrap = s->block_wrap[0]; | |
1143 int xy = s->block_index[0]; | |
1144 | |
1145 s->dc_val[0][xy ] = | |
1146 s->dc_val[0][xy + 1 ] = | |
1147 s->dc_val[0][xy + wrap] = | |
1148 s->dc_val[0][xy + 1 + wrap] = 1024; | |
1149 /* ac pred */ | |
1150 memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16)); | |
1151 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16)); | |
1152 if (s->msmpeg4_version>=3) { | |
1153 s->coded_block[xy ] = | |
1154 s->coded_block[xy + 1 ] = | |
1155 s->coded_block[xy + wrap] = | |
1156 s->coded_block[xy + 1 + wrap] = 0; | |
1157 } | |
1158 /* chroma */ | |
1159 wrap = s->block_wrap[4]; | |
1160 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap; | |
1161 s->dc_val[1][xy] = | |
1162 s->dc_val[2][xy] = 1024; | |
1163 /* ac pred */ | |
1164 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); | |
1165 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16)); | |
1166 | |
1167 s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0; | |
1168 } | |
1169 | |
0 | 1170 /* generic function called after a macroblock has been parsed by the |
1171 decoder or after it has been encoded by the encoder. | |
1172 | |
1173 Important variables used: | |
1174 s->mb_intra : true if intra macroblock | |
1175 s->mv_dir : motion vector direction | |
1176 s->mv_type : motion vector type | |
1177 s->mv : motion vector | |
1178 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
1179 */ | |
1180 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
1181 { | |
244 | 1182 int mb_x, mb_y; |
481 | 1183 const int mb_xy = s->mb_y * s->mb_width + s->mb_x; |
0 | 1184 |
1185 mb_x = s->mb_x; | |
1186 mb_y = s->mb_y; | |
1187 | |
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1188 #ifdef FF_POSTPROCESS |
466
805714c0c10f
new field for communicating with external postprocessing
nickols_k
parents:
462
diff
changeset
|
1189 /* Obsolete. Exists for compatibility with mplayer only. */ |
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1190 quant_store[mb_y][mb_x]=s->qscale; |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1191 //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale); |
466
805714c0c10f
new field for communicating with external postprocessing
nickols_k
parents:
462
diff
changeset
|
1192 #else |
467 | 1193 if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale; |
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1194 #endif |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1195 |
0 | 1196 /* update DC predictors for P macroblocks */ |
1197 if (!s->mb_intra) { | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1198 if (s->h263_pred || s->h263_aic) { |
481 | 1199 if(s->mbintra_table[mb_xy]) |
456 | 1200 ff_clean_intra_table_entries(s); |
0 | 1201 } else { |
456 | 1202 s->last_dc[0] = |
1203 s->last_dc[1] = | |
0 | 1204 s->last_dc[2] = 128 << s->intra_dc_precision; |
1205 } | |
1206 } | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1207 else if (s->h263_pred || s->h263_aic) |
481 | 1208 s->mbintra_table[mb_xy]=1; |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
1209 |
280 | 1210 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */ |
481 | 1211 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here |
1212 int motion_x, motion_y; | |
0 | 1213 |
481 | 1214 const int wrap = s->block_wrap[0]; |
1215 const int xy = s->block_index[0]; | |
0 | 1216 if (s->mb_intra) { |
1217 motion_x = 0; | |
1218 motion_y = 0; | |
1219 goto motion_init; | |
1220 } else if (s->mv_type == MV_TYPE_16X16) { | |
1221 motion_x = s->mv[0][0][0]; | |
1222 motion_y = s->mv[0][0][1]; | |
1223 motion_init: | |
1224 /* no update if 8X8 because it has been done during parsing */ | |
244 | 1225 s->motion_val[xy][0] = motion_x; |
1226 s->motion_val[xy][1] = motion_y; | |
1227 s->motion_val[xy + 1][0] = motion_x; | |
1228 s->motion_val[xy + 1][1] = motion_y; | |
1229 s->motion_val[xy + wrap][0] = motion_x; | |
1230 s->motion_val[xy + wrap][1] = motion_y; | |
1231 s->motion_val[xy + 1 + wrap][0] = motion_x; | |
1232 s->motion_val[xy + 1 + wrap][1] = motion_y; | |
0 | 1233 } |
1234 } | |
1235 | |
324 | 1236 if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { |
0 | 1237 UINT8 *dest_y, *dest_cb, *dest_cr; |
481 | 1238 int dct_linesize, dct_offset; |
1239 op_pixels_func *op_pix; | |
1240 qpel_mc_func *op_qpix; | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1241 |
324 | 1242 /* avoid copy if macroblock skipped in last frame too |
1243 dont touch it for B-frames as they need the skip info from the next p-frame */ | |
1244 if (s->pict_type != B_TYPE) { | |
481 | 1245 UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy]; |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1246 if (s->mb_skiped) { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1247 s->mb_skiped = 0; |
324 | 1248 /* if previous was skipped too, then nothing to do ! |
1249 skip only during decoding as we might trash the buffers during encoding a bit */ | |
1250 if (*mbskip_ptr != 0 && !s->encoding) | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1251 goto the_end; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1252 *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
|
1253 } else { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1254 *mbskip_ptr = 0; /* not skipped */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1255 } |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1256 } |
0 | 1257 |
1258 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16; | |
1259 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
1260 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
1261 | |
1262 if (s->interlaced_dct) { | |
1263 dct_linesize = s->linesize * 2; | |
1264 dct_offset = s->linesize; | |
1265 } else { | |
1266 dct_linesize = s->linesize; | |
1267 dct_offset = s->linesize * 8; | |
1268 } | |
1269 | |
1270 if (!s->mb_intra) { | |
1271 /* motion handling */ | |
456 | 1272 /* decoding or more than one mb_type (MC was allready done otherwise) */ |
481 | 1273 if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){ |
327 | 1274 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
324 | 1275 op_pix = put_pixels_tab; |
1276 op_qpix= qpel_mc_rnd_tab; | |
1277 }else{ | |
1278 op_pix = put_no_rnd_pixels_tab; | |
1279 op_qpix= qpel_mc_no_rnd_tab; | |
1280 } | |
0 | 1281 |
324 | 1282 if (s->mv_dir & MV_DIR_FORWARD) { |
1283 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); | |
327 | 1284 if ((!s->no_rounding) || s->pict_type==B_TYPE) |
324 | 1285 op_pix = avg_pixels_tab; |
1286 else | |
1287 op_pix = avg_no_rnd_pixels_tab; | |
1288 } | |
1289 if (s->mv_dir & MV_DIR_BACKWARD) { | |
1290 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); | |
1291 } | |
0 | 1292 } |
1293 | |
481 | 1294 /* skip dequant / idct if we are really late ;) */ |
1295 if(s->hurry_up>1) goto the_end; | |
1296 | |
0 | 1297 /* add dct residue */ |
481 | 1298 if(!s->mpeg2 && (s->encoding || (!s->h263_msmpeg4))){ |
1299 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize); | |
1300 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
1301 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
1302 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
0 | 1303 |
485 | 1304 if(!(s->flags&CODEC_FLAG_GRAY)){ |
1305 add_dequant_dct(s, block[4], 4, dest_cb, s->linesize >> 1); | |
1306 add_dequant_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
1307 } | |
481 | 1308 } else { |
1309 add_dct(s, block[0], 0, dest_y, dct_linesize); | |
1310 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
1311 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
1312 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
1313 | |
485 | 1314 if(!(s->flags&CODEC_FLAG_GRAY)){ |
1315 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1); | |
1316 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
1317 } | |
481 | 1318 } |
0 | 1319 } else { |
1320 /* dct only in intra block */ | |
1321 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
1322 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
1323 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
1324 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
1325 | |
485 | 1326 if(!(s->flags&CODEC_FLAG_GRAY)){ |
1327 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1); | |
1328 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
1329 } | |
0 | 1330 } |
1331 } | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1332 the_end: |
294 | 1333 emms_c(); //FIXME remove |
1334 } | |
1335 | |
456 | 1336 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc) |
1337 { | |
1338 static const char tab[64]= | |
1339 {3,2,2,1,1,1,1,1, | |
1340 1,1,1,1,1,1,1,1, | |
1341 1,1,1,1,1,1,1,1, | |
1342 0,0,0,0,0,0,0,0, | |
1343 0,0,0,0,0,0,0,0, | |
1344 0,0,0,0,0,0,0,0, | |
1345 0,0,0,0,0,0,0,0, | |
1346 0,0,0,0,0,0,0,0}; | |
1347 int score=0; | |
1348 int run=0; | |
1349 int i; | |
1350 DCTELEM *block= s->block[n]; | |
1351 const int last_index= s->block_last_index[n]; | |
1352 | |
1353 if(skip_dc) skip_dc=1; | |
1354 | |
1355 /* are all which we could set to zero are allready zero? */ | |
1356 if(last_index<=skip_dc - 1) return; | |
1357 | |
1358 for(i=0; i<=last_index; i++){ | |
1359 const int j = zigzag_direct[i]; | |
1360 const int level = ABS(block[j]); | |
1361 if(level==1){ | |
1362 if(skip_dc && i==0) continue; | |
1363 score+= tab[run]; | |
1364 run=0; | |
1365 }else if(level>1){ | |
1366 return; | |
1367 }else{ | |
1368 run++; | |
1369 } | |
1370 } | |
1371 if(score >= threshold) return; | |
1372 for(i=skip_dc; i<=last_index; i++){ | |
1373 const int j = zigzag_direct[i]; | |
1374 block[j]=0; | |
1375 } | |
1376 if(block[0]) s->block_last_index[n]= 0; | |
1377 else s->block_last_index[n]= -1; | |
1378 } | |
1379 | |
344 | 1380 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index) |
1381 { | |
1382 int i; | |
1383 const int maxlevel= s->max_qcoeff; | |
1384 const int minlevel= s->min_qcoeff; | |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1385 |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1386 for(i=0;i<=last_index; i++){ |
344 | 1387 const int j = zigzag_direct[i]; |
1388 int level = block[j]; | |
1389 | |
1390 if (level>maxlevel) level=maxlevel; | |
1391 else if(level<minlevel) level=minlevel; | |
1392 block[j]= level; | |
1393 } | |
1394 } | |
324 | 1395 |
1396 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) | |
294 | 1397 { |
1398 const int mb_x= s->mb_x; | |
1399 const int mb_y= s->mb_y; | |
1400 int i; | |
456 | 1401 int skip_dct[6]; |
324 | 1402 #if 0 |
1403 if (s->interlaced_dct) { | |
1404 dct_linesize = s->linesize * 2; | |
1405 dct_offset = s->linesize; | |
1406 } else { | |
1407 dct_linesize = s->linesize; | |
1408 dct_offset = s->linesize * 8; | |
1409 } | |
1410 #endif | |
456 | 1411 for(i=0; i<6; i++) skip_dct[i]=0; |
294 | 1412 |
324 | 1413 if (s->mb_intra) { |
1414 UINT8 *ptr; | |
1415 int wrap; | |
294 | 1416 |
324 | 1417 wrap = s->linesize; |
1418 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; | |
1419 get_pixels(s->block[0], ptr , wrap); | |
1420 get_pixels(s->block[1], ptr + 8, wrap); | |
1421 get_pixels(s->block[2], ptr + 8 * wrap , wrap); | |
1422 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); | |
294 | 1423 |
487 | 1424 if(s->flags&CODEC_FLAG_GRAY){ |
1425 skip_dct[4]= 1; | |
1426 skip_dct[5]= 1; | |
1427 }else{ | |
1428 wrap >>=1; | |
1429 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8; | |
1430 get_pixels(s->block[4], ptr, wrap); | |
294 | 1431 |
487 | 1432 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8; |
1433 get_pixels(s->block[5], ptr, wrap); | |
1434 } | |
324 | 1435 }else{ |
1436 op_pixels_func *op_pix; | |
1437 qpel_mc_func *op_qpix; | |
1438 UINT8 *dest_y, *dest_cb, *dest_cr; | |
456 | 1439 UINT8 *ptr_y, *ptr_cb, *ptr_cr; |
1440 int wrap_y, wrap_c; | |
294 | 1441 |
324 | 1442 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16; |
1443 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
1444 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
456 | 1445 wrap_y = s->linesize; |
1446 wrap_c = wrap_y>>1; | |
1447 ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16; | |
1448 ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
1449 ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
324 | 1450 |
327 | 1451 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
324 | 1452 op_pix = put_pixels_tab; |
1453 op_qpix= qpel_mc_rnd_tab; | |
295 | 1454 }else{ |
324 | 1455 op_pix = put_no_rnd_pixels_tab; |
1456 op_qpix= qpel_mc_no_rnd_tab; | |
1457 } | |
295 | 1458 |
324 | 1459 if (s->mv_dir & MV_DIR_FORWARD) { |
1460 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); | |
327 | 1461 if ((!s->no_rounding) || s->pict_type==B_TYPE) |
324 | 1462 op_pix = avg_pixels_tab; |
1463 else | |
1464 op_pix = avg_no_rnd_pixels_tab; | |
1465 } | |
1466 if (s->mv_dir & MV_DIR_BACKWARD) { | |
1467 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); | |
1468 } | |
295 | 1469 |
456 | 1470 diff_pixels(s->block[0], ptr_y , dest_y , wrap_y); |
1471 diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); | |
1472 diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y); | |
1473 diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y); | |
487 | 1474 |
1475 if(s->flags&CODEC_FLAG_GRAY){ | |
1476 skip_dct[4]= 1; | |
1477 skip_dct[5]= 1; | |
1478 }else{ | |
1479 diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c); | |
1480 diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); | |
1481 } | |
1482 | |
456 | 1483 /* pre quantization */ |
1484 if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){ | |
1485 if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1; | |
1486 if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1; | |
1487 if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1; | |
1488 if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1; | |
1489 if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1; | |
1490 if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1; | |
1491 #if 0 | |
1492 { | |
1493 static int stat[7]; | |
1494 int num=0; | |
1495 for(i=0; i<6; i++) | |
1496 if(skip_dct[i]) num++; | |
1497 stat[num]++; | |
1498 | |
1499 if(s->mb_x==0 && s->mb_y==0){ | |
1500 for(i=0; i<7; i++){ | |
1501 printf("%6d %1d\n", stat[i], i); | |
1502 } | |
1503 } | |
1504 } | |
1505 #endif | |
1506 } | |
324 | 1507 |
294 | 1508 } |
1509 | |
1510 #if 0 | |
1511 { | |
1512 float adap_parm; | |
1513 | |
1514 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) / | |
1515 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0); | |
1516 | |
1517 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", | |
1518 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', | |
1519 s->qscale, adap_parm, s->qscale*adap_parm, | |
1520 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); | |
1521 } | |
1522 #endif | |
1523 /* DCT & quantize */ | |
456 | 1524 if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) { |
294 | 1525 h263_dc_scale(s); |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1526 } else if (s->h263_aic) { |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1527 s->y_dc_scale = 2*s->qscale; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1528 s->c_dc_scale = 2*s->qscale; |
294 | 1529 } else { |
1530 /* default quantization values */ | |
1531 s->y_dc_scale = 8; | |
1532 s->c_dc_scale = 8; | |
1533 } | |
344 | 1534 if(s->out_format==FMT_MJPEG){ |
1535 for(i=0;i<6;i++) { | |
1536 int overflow; | |
1537 s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow); | |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1538 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
344 | 1539 } |
1540 }else{ | |
1541 for(i=0;i<6;i++) { | |
456 | 1542 if(!skip_dct[i]){ |
1543 int overflow; | |
1544 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow); | |
344 | 1545 // FIXME we could decide to change to quantizer instead of clipping |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1546 // JS: I don't think that would be a good idea it could lower quality instead |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1547 // of improve it. Just INTRADC clipping deserves changes in quantizer |
456 | 1548 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
1549 }else | |
1550 s->block_last_index[i]= -1; | |
344 | 1551 } |
456 | 1552 if(s->luma_elim_threshold && !s->mb_intra) |
1553 for(i=0; i<4; i++) | |
1554 dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0); | |
1555 if(s->chroma_elim_threshold && !s->mb_intra) | |
1556 for(i=4; i<6; i++) | |
1557 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1); | |
294 | 1558 } |
1559 | |
487 | 1560 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){ |
1561 s->block_last_index[4]= | |
1562 s->block_last_index[5]= 0; | |
1563 s->block[4][0]= | |
1564 s->block[5][0]= 128; | |
1565 } | |
1566 | |
294 | 1567 /* huffman encode */ |
1568 switch(s->out_format) { | |
1569 case FMT_MPEG1: | |
1570 mpeg1_encode_mb(s, s->block, motion_x, motion_y); | |
1571 break; | |
1572 case FMT_H263: | |
1573 if (s->h263_msmpeg4) | |
1574 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); | |
1575 else if(s->h263_pred) | |
1576 mpeg4_encode_mb(s, s->block, motion_x, motion_y); | |
1577 else | |
1578 h263_encode_mb(s, s->block, motion_x, motion_y); | |
1579 break; | |
1580 case FMT_MJPEG: | |
1581 mjpeg_encode_mb(s, s->block); | |
1582 break; | |
1583 } | |
1584 } | |
1585 | |
456 | 1586 void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length) |
294 | 1587 { |
326 | 1588 #if 1 |
1589 int bytes= length>>4; | |
1590 int bits= length&15; | |
1591 int i; | |
1592 | |
456 | 1593 if(length==0) return; |
1594 | |
326 | 1595 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i])); |
1596 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits)); | |
1597 #else | |
294 | 1598 int bytes= length>>3; |
1599 int bits= length&7; | |
1600 int i; | |
1601 | |
1602 for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]); | |
1603 put_bits(pb, bits, src[i]>>(8-bits)); | |
326 | 1604 #endif |
0 | 1605 } |
1606 | |
456 | 1607 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
326 | 1608 int i; |
1609 | |
1610 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
1611 | |
1612 /* mpeg1 */ | |
1613 d->mb_incr= s->mb_incr; | |
1614 for(i=0; i<3; i++) | |
1615 d->last_dc[i]= s->last_dc[i]; | |
1616 | |
1617 /* statistics */ | |
1618 d->mv_bits= s->mv_bits; | |
1619 d->i_tex_bits= s->i_tex_bits; | |
1620 d->p_tex_bits= s->p_tex_bits; | |
1621 d->i_count= s->i_count; | |
1622 d->p_count= s->p_count; | |
1623 d->skip_count= s->skip_count; | |
1624 d->misc_bits= s->misc_bits; | |
329 | 1625 d->last_bits= 0; |
327 | 1626 |
1627 d->mb_skiped= s->mb_skiped; | |
326 | 1628 } |
1629 | |
456 | 1630 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
326 | 1631 int i; |
1632 | |
1633 memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); | |
1634 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
1635 | |
1636 /* mpeg1 */ | |
1637 d->mb_incr= s->mb_incr; | |
1638 for(i=0; i<3; i++) | |
1639 d->last_dc[i]= s->last_dc[i]; | |
1640 | |
1641 /* statistics */ | |
1642 d->mv_bits= s->mv_bits; | |
1643 d->i_tex_bits= s->i_tex_bits; | |
1644 d->p_tex_bits= s->p_tex_bits; | |
1645 d->i_count= s->i_count; | |
1646 d->p_count= s->p_count; | |
1647 d->skip_count= s->skip_count; | |
1648 d->misc_bits= s->misc_bits; | |
1649 | |
1650 d->mb_intra= s->mb_intra; | |
327 | 1651 d->mb_skiped= s->mb_skiped; |
326 | 1652 d->mv_type= s->mv_type; |
1653 d->mv_dir= s->mv_dir; | |
1654 d->pb= s->pb; | |
456 | 1655 if(s->data_partitioning){ |
1656 d->pb2= s->pb2; | |
1657 d->tex_pb= s->tex_pb; | |
1658 } | |
326 | 1659 d->block= s->block; |
1660 for(i=0; i<6; i++) | |
1661 d->block_last_index[i]= s->block_last_index[i]; | |
1662 } | |
1663 | |
456 | 1664 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, |
1665 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], | |
1666 int *dmin, int *next_block, int motion_x, int motion_y) | |
1667 { | |
1668 int bits_count; | |
1669 | |
1670 copy_context_before_encode(s, backup, type); | |
1671 | |
1672 s->block= s->blocks[*next_block]; | |
1673 s->pb= pb[*next_block]; | |
1674 if(s->data_partitioning){ | |
1675 s->pb2 = pb2 [*next_block]; | |
1676 s->tex_pb= tex_pb[*next_block]; | |
1677 } | |
1678 | |
1679 encode_mb(s, motion_x, motion_y); | |
1680 | |
1681 bits_count= get_bit_count(&s->pb); | |
1682 if(s->data_partitioning){ | |
1683 bits_count+= get_bit_count(&s->pb2); | |
1684 bits_count+= get_bit_count(&s->tex_pb); | |
1685 } | |
1686 | |
1687 if(bits_count<*dmin){ | |
1688 *dmin= bits_count; | |
1689 *next_block^=1; | |
1690 | |
1691 copy_context_after_encode(best, s, type); | |
1692 } | |
1693 } | |
326 | 1694 |
0 | 1695 static void encode_picture(MpegEncContext *s, int picture_number) |
1696 { | |
294 | 1697 int mb_x, mb_y, last_gob, pdif = 0; |
1698 int i; | |
286 | 1699 int bits; |
326 | 1700 MpegEncContext best_s, backup_s; |
456 | 1701 UINT8 bit_buf[2][3000]; |
1702 UINT8 bit_buf2[2][3000]; | |
1703 UINT8 bit_buf_tex[2][3000]; | |
1704 PutBitContext pb[2], pb2[2], tex_pb[2]; | |
1705 | |
1706 for(i=0; i<2; i++){ | |
1707 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL); | |
1708 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL); | |
1709 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL); | |
1710 } | |
0 | 1711 |
1712 s->picture_number = picture_number; | |
268 | 1713 |
294 | 1714 s->block_wrap[0]= |
1715 s->block_wrap[1]= | |
1716 s->block_wrap[2]= | |
1717 s->block_wrap[3]= s->mb_width*2 + 2; | |
1718 s->block_wrap[4]= | |
1719 s->block_wrap[5]= s->mb_width + 2; | |
1720 | |
268 | 1721 /* Reset the average MB variance */ |
456 | 1722 s->mb_var_sum = 0; |
1723 s->mc_mb_var_sum = 0; | |
327 | 1724 |
1725 /* we need to initialize some time vars before we can encode b-frames */ | |
1726 if (s->h263_pred && !s->h263_msmpeg4) | |
1727 ff_set_mpeg4_time(s, s->picture_number); | |
1728 | |
268 | 1729 /* Estimate motion for every MB */ |
324 | 1730 if(s->pict_type != I_TYPE){ |
294 | 1731 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
1732 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; | |
1733 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
1734 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
1735 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
1736 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
1737 s->mb_x = mb_x; | |
1738 s->mb_y = mb_y; | |
1739 s->block_index[0]+=2; | |
1740 s->block_index[1]+=2; | |
1741 s->block_index[2]+=2; | |
1742 s->block_index[3]+=2; | |
268 | 1743 |
294 | 1744 /* compute motion vector & mb_type and store in context */ |
324 | 1745 if(s->pict_type==B_TYPE) |
1746 ff_estimate_b_frame_motion(s, mb_x, mb_y); | |
1747 else | |
1748 ff_estimate_p_frame_motion(s, mb_x, mb_y); | |
294 | 1749 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER; |
268 | 1750 } |
1751 } | |
294 | 1752 emms_c(); |
456 | 1753 }else /* if(s->pict_type == I_TYPE) */{ |
294 | 1754 /* I-Frame */ |
1755 //FIXME do we need to zero them? | |
1756 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); | |
324 | 1757 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2); |
294 | 1758 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
268 | 1759 } |
1760 | |
456 | 1761 if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits |
271 | 1762 s->pict_type= I_TYPE; |
294 | 1763 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
329 | 1764 if(s->max_b_frames==0){ |
1765 s->input_pict_type= I_TYPE; | |
1766 s->input_picture_in_gop_number=0; | |
1767 } | |
294 | 1768 //printf("Scene change detected, encoding as I Frame\n"); |
271 | 1769 } |
324 | 1770 |
1771 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) | |
1772 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); | |
1773 ff_fix_long_p_mvs(s); | |
1774 if(s->pict_type==B_TYPE){ | |
1775 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD); | |
1776 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD); | |
327 | 1777 |
1778 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD); | |
1779 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD); | |
1780 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR); | |
1781 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR); | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1782 } |
324 | 1783 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1784 //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
|
1785 |
271 | 1786 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); |
1787 | |
329 | 1788 if(s->flags&CODEC_FLAG_PASS2) |
1789 s->qscale = ff_rate_estimate_qscale_pass2(s); | |
1790 else if (!s->fixed_qscale) | |
1791 s->qscale = ff_rate_estimate_qscale(s); | |
1792 | |
0 | 1793 if (s->out_format == FMT_MJPEG) { |
1794 /* for mjpeg, we do include qscale in the matrix */ | |
1795 s->intra_matrix[0] = default_intra_matrix[0]; | |
1796 for(i=1;i<64;i++) | |
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
365
diff
changeset
|
1797 s->intra_matrix[i] = CLAMP_TO_8BIT((default_intra_matrix[i] * s->qscale) >> 3); |
344 | 1798 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, |
1799 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias); | |
0 | 1800 } |
1801 | |
286 | 1802 s->last_bits= get_bit_count(&s->pb); |
0 | 1803 switch(s->out_format) { |
1804 case FMT_MJPEG: | |
1805 mjpeg_picture_header(s); | |
1806 break; | |
1807 case FMT_H263: | |
1808 if (s->h263_msmpeg4) | |
1809 msmpeg4_encode_picture_header(s, picture_number); | |
1810 else if (s->h263_pred) | |
1811 mpeg4_encode_picture_header(s, picture_number); | |
1812 else if (s->h263_rv10) | |
1813 rv10_encode_picture_header(s, picture_number); | |
1814 else | |
1815 h263_encode_picture_header(s, picture_number); | |
1816 break; | |
1817 case FMT_MPEG1: | |
1818 mpeg1_encode_picture_header(s, picture_number); | |
1819 break; | |
1820 } | |
286 | 1821 bits= get_bit_count(&s->pb); |
1822 s->header_bits= bits - s->last_bits; | |
1823 s->last_bits= bits; | |
1824 s->mv_bits=0; | |
1825 s->misc_bits=0; | |
1826 s->i_tex_bits=0; | |
1827 s->p_tex_bits=0; | |
1828 s->i_count=0; | |
1829 s->p_count=0; | |
1830 s->skip_count=0; | |
1831 | |
0 | 1832 /* init last dc values */ |
1833 /* note: quant matrix value (8) is implied here */ | |
1834 s->last_dc[0] = 128; | |
1835 s->last_dc[1] = 128; | |
1836 s->last_dc[2] = 128; | |
1837 s->mb_incr = 1; | |
1838 s->last_mv[0][0][0] = 0; | |
1839 s->last_mv[0][0][1] = 0; | |
1840 | |
162 | 1841 /* Get the GOB height based on picture height */ |
231 | 1842 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) { |
162 | 1843 if (s->height <= 400) |
1844 s->gob_index = 1; | |
1845 else if (s->height <= 800) | |
1846 s->gob_index = 2; | |
1847 else | |
1848 s->gob_index = 4; | |
456 | 1849 }else if(s->codec_id==CODEC_ID_MPEG4){ |
1850 s->gob_index = 1; | |
162 | 1851 } |
456 | 1852 |
1853 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE) | |
1854 ff_mpeg4_init_partitions(s); | |
1855 | |
1856 s->resync_mb_x=0; | |
1857 s->resync_mb_y=0; | |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1858 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
456 | 1859 /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/ |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1860 /* 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
|
1861 if (s->rtp_mode) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1862 if (!mb_y) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1863 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
|
1864 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
|
1865 } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) { |
456 | 1866 // MN: we could move the space check from h263 -> here, as its not h263 specific |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1867 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
|
1868 if (last_gob) { |
456 | 1869 s->first_slice_line = 1; |
1870 }else{ | |
1871 /*MN: we reset it here instead at the end of each line cuz mpeg4 can have | |
1872 slice lines starting & ending in the middle*/ | |
1873 s->first_slice_line = 0; | |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1874 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1875 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1876 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1877 |
266 | 1878 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
1879 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
1880 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
1881 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
1882 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
1883 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
|
1884 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
327 | 1885 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; |
1886 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; | |
456 | 1887 // int d; |
294 | 1888 int dmin=10000000; |
0 | 1889 |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1890 s->mb_x = mb_x; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1891 s->mb_y = mb_y; |
266 | 1892 s->block_index[0]+=2; |
1893 s->block_index[1]+=2; | |
1894 s->block_index[2]+=2; | |
1895 s->block_index[3]+=2; | |
1896 s->block_index[4]++; | |
1897 s->block_index[5]++; | |
456 | 1898 |
1899 /* write gob / video packet header for formats which support it at any MB (MPEG4) */ | |
1900 if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){ | |
1901 int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob; | |
1902 | |
1903 //the *2 is there so we stay below the requested size | |
1904 if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){ | |
1905 if(s->codec_id==CODEC_ID_MPEG4){ | |
1906 if(s->data_partitioning && s->pict_type!=B_TYPE){ | |
1907 ff_mpeg4_merge_partitions(s); | |
1908 ff_mpeg4_init_partitions(s); | |
1909 } | |
1910 ff_mpeg4_encode_video_packet_header(s); | |
1911 | |
1912 if(s->flags&CODEC_FLAG_PASS1){ | |
1913 int bits= get_bit_count(&s->pb); | |
1914 s->misc_bits+= bits - s->last_bits; | |
1915 s->last_bits= bits; | |
1916 } | |
1917 ff_mpeg4_clean_buffers(s); | |
1918 } | |
1919 s->ptr_lastgob = pbBufPtr(&s->pb); | |
1920 s->first_slice_line=1; | |
1921 s->resync_mb_x=mb_x; | |
1922 s->resync_mb_y=mb_y; | |
1923 } | |
1924 | |
1925 if( (s->resync_mb_x == s->mb_x) | |
1926 && s->resync_mb_y+1 == s->mb_y){ | |
1927 s->first_slice_line=0; | |
1928 } | |
1929 } | |
1930 | |
294 | 1931 if(mb_type & (mb_type-1)){ // more than 1 MB type possible |
327 | 1932 int next_block=0; |
456 | 1933 int pb_bits_count, pb2_bits_count, tex_pb_bits_count; |
326 | 1934 |
1935 copy_context_before_encode(&backup_s, s, -1); | |
456 | 1936 backup_s.pb= s->pb; |
1937 best_s.data_partitioning= s->data_partitioning; | |
1938 if(s->data_partitioning){ | |
1939 backup_s.pb2= s->pb2; | |
1940 backup_s.tex_pb= s->tex_pb; | |
1941 } | |
326 | 1942 |
294 | 1943 if(mb_type&MB_TYPE_INTER){ |
327 | 1944 s->mv_dir = MV_DIR_FORWARD; |
295 | 1945 s->mv_type = MV_TYPE_16X16; |
294 | 1946 s->mb_intra= 0; |
324 | 1947 s->mv[0][0][0] = s->p_mv_table[xy][0]; |
1948 s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
456 | 1949 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, |
1950 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
0 | 1951 } |
326 | 1952 if(mb_type&MB_TYPE_INTER4V){ |
327 | 1953 s->mv_dir = MV_DIR_FORWARD; |
295 | 1954 s->mv_type = MV_TYPE_8X8; |
1955 s->mb_intra= 0; | |
1956 for(i=0; i<4; i++){ | |
1957 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
1958 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
1959 } | |
456 | 1960 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, |
1961 &dmin, &next_block, 0, 0); | |
327 | 1962 } |
1963 if(mb_type&MB_TYPE_FORWARD){ | |
1964 s->mv_dir = MV_DIR_FORWARD; | |
1965 s->mv_type = MV_TYPE_16X16; | |
1966 s->mb_intra= 0; | |
1967 s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
1968 s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
456 | 1969 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, |
1970 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
327 | 1971 } |
1972 if(mb_type&MB_TYPE_BACKWARD){ | |
1973 s->mv_dir = MV_DIR_BACKWARD; | |
1974 s->mv_type = MV_TYPE_16X16; | |
1975 s->mb_intra= 0; | |
1976 s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
1977 s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
456 | 1978 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, |
1979 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]); | |
327 | 1980 } |
1981 if(mb_type&MB_TYPE_BIDIR){ | |
1982 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
1983 s->mv_type = MV_TYPE_16X16; | |
1984 s->mb_intra= 0; | |
1985 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
1986 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
1987 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
1988 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
456 | 1989 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, |
1990 &dmin, &next_block, 0, 0); | |
327 | 1991 } |
1992 if(mb_type&MB_TYPE_DIRECT){ | |
1993 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
1994 s->mv_type = MV_TYPE_16X16; //FIXME | |
1995 s->mb_intra= 0; | |
1996 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0]; | |
1997 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1]; | |
1998 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0]; | |
1999 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1]; | |
456 | 2000 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, |
2001 &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]); | |
295 | 2002 } |
294 | 2003 if(mb_type&MB_TYPE_INTRA){ |
327 | 2004 s->mv_dir = MV_DIR_FORWARD; |
295 | 2005 s->mv_type = MV_TYPE_16X16; |
294 | 2006 s->mb_intra= 1; |
2007 s->mv[0][0][0] = 0; | |
2008 s->mv[0][0][1] = 0; | |
456 | 2009 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, |
2010 &dmin, &next_block, 0, 0); | |
326 | 2011 /* force cleaning of ac/dc pred stuff if needed ... */ |
2012 if(s->h263_pred || s->h263_aic) | |
2013 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; | |
295 | 2014 } |
326 | 2015 copy_context_after_encode(s, &best_s, -1); |
456 | 2016 |
2017 pb_bits_count= get_bit_count(&s->pb); | |
2018 flush_put_bits(&s->pb); | |
2019 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count); | |
2020 s->pb= backup_s.pb; | |
2021 | |
2022 if(s->data_partitioning){ | |
2023 pb2_bits_count= get_bit_count(&s->pb2); | |
2024 flush_put_bits(&s->pb2); | |
2025 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count); | |
2026 s->pb2= backup_s.pb2; | |
2027 | |
2028 tex_pb_bits_count= get_bit_count(&s->tex_pb); | |
2029 flush_put_bits(&s->tex_pb); | |
2030 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count); | |
2031 s->tex_pb= backup_s.tex_pb; | |
2032 } | |
329 | 2033 s->last_bits= get_bit_count(&s->pb); |
294 | 2034 } else { |
324 | 2035 int motion_x, motion_y; |
2036 s->mv_type=MV_TYPE_16X16; | |
294 | 2037 // only one MB-Type possible |
327 | 2038 switch(mb_type){ |
2039 case MB_TYPE_INTRA: | |
324 | 2040 s->mv_dir = MV_DIR_FORWARD; |
294 | 2041 s->mb_intra= 1; |
324 | 2042 motion_x= s->mv[0][0][0] = 0; |
2043 motion_y= s->mv[0][0][1] = 0; | |
327 | 2044 break; |
2045 case MB_TYPE_INTER: | |
324 | 2046 s->mv_dir = MV_DIR_FORWARD; |
2047 s->mb_intra= 0; | |
2048 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; | |
2049 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
327 | 2050 break; |
456 | 2051 case MB_TYPE_INTER4V: |
2052 s->mv_dir = MV_DIR_FORWARD; | |
2053 s->mv_type = MV_TYPE_8X8; | |
2054 s->mb_intra= 0; | |
2055 for(i=0; i<4; i++){ | |
2056 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
2057 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
2058 } | |
2059 motion_x= motion_y= 0; | |
2060 break; | |
327 | 2061 case MB_TYPE_DIRECT: |
324 | 2062 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
2063 s->mb_intra= 0; | |
327 | 2064 motion_x=s->b_direct_mv_table[xy][0]; |
2065 motion_y=s->b_direct_mv_table[xy][1]; | |
2066 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0]; | |
2067 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1]; | |
2068 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0]; | |
2069 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1]; | |
2070 break; | |
2071 case MB_TYPE_BIDIR: | |
324 | 2072 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; |
294 | 2073 s->mb_intra= 0; |
324 | 2074 motion_x=0; |
2075 motion_y=0; | |
2076 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
2077 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
2078 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
2079 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
327 | 2080 break; |
2081 case MB_TYPE_BACKWARD: | |
324 | 2082 s->mv_dir = MV_DIR_BACKWARD; |
2083 s->mb_intra= 0; | |
2084 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
2085 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
327 | 2086 break; |
2087 case MB_TYPE_FORWARD: | |
324 | 2088 s->mv_dir = MV_DIR_FORWARD; |
2089 s->mb_intra= 0; | |
2090 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
2091 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
2092 // printf(" %d %d ", motion_x, motion_y); | |
327 | 2093 break; |
2094 default: | |
324 | 2095 motion_x=motion_y=0; //gcc warning fix |
2096 printf("illegal MB type\n"); | |
294 | 2097 } |
324 | 2098 encode_mb(s, motion_x, motion_y); |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2099 } |
327 | 2100 /* clean the MV table in IPS frames for direct mode in B frames */ |
2101 if(s->mb_intra /* && I,P,S_TYPE */){ | |
2102 s->p_mv_table[xy][0]=0; | |
2103 s->p_mv_table[xy][1]=0; | |
2104 } | |
0 | 2105 |
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
|
2106 MPV_decode_mb(s, s->block); |
456 | 2107 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb)); |
0 | 2108 } |
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
|
2109 |
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
|
2110 |
231 | 2111 /* Obtain average GOB size for RTP */ |
2112 if (s->rtp_mode) { | |
2113 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
|
2114 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line; |
231 | 2115 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
|
2116 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
|
2117 s->ptr_last_mb_line = pbBufPtr(&s->pb); |
231 | 2118 } |
2119 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, | |
2120 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); | |
456 | 2121 if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean |
231 | 2122 } |
0 | 2123 } |
294 | 2124 emms_c(); |
286 | 2125 |
456 | 2126 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE) |
2127 ff_mpeg4_merge_partitions(s); | |
2128 | |
2129 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE) | |
208 | 2130 msmpeg4_encode_ext_header(s); |
2131 | |
456 | 2132 if(s->codec_id==CODEC_ID_MPEG4) |
2133 ff_mpeg4_stuffing(&s->pb); | |
2134 | |
162 | 2135 //if (s->gob_number) |
2136 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | |
231 | 2137 |
2138 /* Send the last GOB if RTP */ | |
2139 if (s->rtp_mode) { | |
2140 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
|
2141 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
231 | 2142 /* Call the RTP callback to send the last GOB */ |
2143 if (s->rtp_callback) | |
2144 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
|
2145 s->ptr_lastgob = pbBufPtr(&s->pb); |
231 | 2146 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif); |
2147 } | |
0 | 2148 } |
2149 | |
220 | 2150 static int dct_quantize_c(MpegEncContext *s, |
0 | 2151 DCTELEM *block, int n, |
344 | 2152 int qscale, int *overflow) |
0 | 2153 { |
2154 int i, j, level, last_non_zero, q; | |
2155 const int *qmat; | |
344 | 2156 int bias; |
2157 int max=0; | |
2158 unsigned int threshold1, threshold2; | |
456 | 2159 |
0 | 2160 av_fdct (block); |
2161 | |
64 | 2162 /* we need this permutation so that we correct the IDCT |
2163 permutation. will be moved into DCT code */ | |
2164 block_permute(block); | |
2165 | |
0 | 2166 if (s->mb_intra) { |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2167 if (!s->h263_aic) { |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2168 if (n < 4) |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2169 q = s->y_dc_scale; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2170 else |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2171 q = s->c_dc_scale; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2172 q = q << 3; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2173 } else |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2174 /* For AIC we skip quant/dequant of INTRADC */ |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2175 q = 1 << 3; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2176 |
0 | 2177 /* note: block[0] is assumed to be positive */ |
2178 block[0] = (block[0] + (q >> 1)) / q; | |
2179 i = 1; | |
2180 last_non_zero = 0; | |
344 | 2181 qmat = s->q_intra_matrix[qscale]; |
2182 bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT); | |
0 | 2183 } else { |
2184 i = 0; | |
2185 last_non_zero = -1; | |
344 | 2186 qmat = s->q_inter_matrix[qscale]; |
2187 bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT); | |
0 | 2188 } |
344 | 2189 threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1; |
2190 threshold2= threshold1<<1; | |
0 | 2191 |
2192 for(;i<64;i++) { | |
2193 j = zigzag_direct[i]; | |
2194 level = block[j]; | |
2195 level = level * qmat[j]; | |
344 | 2196 |
2197 // if( bias+level >= (1<<(QMAT_SHIFT - 3)) | |
2198 // || bias-level >= (1<<(QMAT_SHIFT - 3))){ | |
2199 if(((unsigned)(level+threshold1))>threshold2){ | |
2200 if(level>0){ | |
2201 level= (bias + level)>>(QMAT_SHIFT - 3); | |
2202 block[j]= level; | |
2203 }else{ | |
2204 level= (bias - level)>>(QMAT_SHIFT - 3); | |
2205 block[j]= -level; | |
0 | 2206 } |
344 | 2207 max |=level; |
0 | 2208 last_non_zero = i; |
344 | 2209 }else{ |
2210 block[j]=0; | |
0 | 2211 } |
2212 } | |
344 | 2213 *overflow= s->max_qcoeff < max; //overflow might have happend |
2214 | |
0 | 2215 return last_non_zero; |
2216 } | |
2217 | |
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
|
2218 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
|
2219 DCTELEM *block, int n, int qscale) |
0 | 2220 { |
200 | 2221 int i, level, nCoeffs; |
0 | 2222 const UINT16 *quant_matrix; |
2223 | |
200 | 2224 if(s->alternate_scan) nCoeffs= 64; |
2225 else nCoeffs= s->block_last_index[n]+1; | |
2226 | |
0 | 2227 if (s->mb_intra) { |
2228 if (n < 4) | |
2229 block[0] = block[0] * s->y_dc_scale; | |
2230 else | |
2231 block[0] = block[0] * s->c_dc_scale; | |
2232 /* XXX: only mpeg1 */ | |
2233 quant_matrix = s->intra_matrix; | |
200 | 2234 for(i=1;i<nCoeffs;i++) { |
2235 int j= zigzag_direct[i]; | |
2236 level = block[j]; | |
0 | 2237 if (level) { |
2238 if (level < 0) { | |
2239 level = -level; | |
200 | 2240 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 2241 level = (level - 1) | 1; |
2242 level = -level; | |
2243 } else { | |
200 | 2244 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 2245 level = (level - 1) | 1; |
2246 } | |
2247 #ifdef PARANOID | |
2248 if (level < -2048 || level > 2047) | |
2249 fprintf(stderr, "unquant error %d %d\n", i, level); | |
2250 #endif | |
200 | 2251 block[j] = level; |
0 | 2252 } |
2253 } | |
2254 } else { | |
2255 i = 0; | |
344 | 2256 quant_matrix = s->inter_matrix; |
217 | 2257 for(;i<nCoeffs;i++) { |
200 | 2258 int j= zigzag_direct[i]; |
2259 level = block[j]; | |
0 | 2260 if (level) { |
2261 if (level < 0) { | |
2262 level = -level; | |
2263 level = (((level << 1) + 1) * qscale * | |
200 | 2264 ((int) (quant_matrix[j]))) >> 4; |
0 | 2265 level = (level - 1) | 1; |
2266 level = -level; | |
2267 } else { | |
2268 level = (((level << 1) + 1) * qscale * | |
200 | 2269 ((int) (quant_matrix[j]))) >> 4; |
0 | 2270 level = (level - 1) | 1; |
2271 } | |
2272 #ifdef PARANOID | |
2273 if (level < -2048 || level > 2047) | |
2274 fprintf(stderr, "unquant error %d %d\n", i, level); | |
2275 #endif | |
200 | 2276 block[j] = level; |
0 | 2277 } |
2278 } | |
2279 } | |
2280 } | |
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
|
2281 |
325 | 2282 static void dct_unquantize_mpeg2_c(MpegEncContext *s, |
2283 DCTELEM *block, int n, int qscale) | |
2284 { | |
2285 int i, level, nCoeffs; | |
2286 const UINT16 *quant_matrix; | |
2287 | |
2288 if(s->alternate_scan) nCoeffs= 64; | |
2289 else nCoeffs= s->block_last_index[n]+1; | |
2290 | |
2291 if (s->mb_intra) { | |
2292 if (n < 4) | |
2293 block[0] = block[0] * s->y_dc_scale; | |
2294 else | |
2295 block[0] = block[0] * s->c_dc_scale; | |
2296 quant_matrix = s->intra_matrix; | |
2297 for(i=1;i<nCoeffs;i++) { | |
2298 int j= zigzag_direct[i]; | |
2299 level = block[j]; | |
2300 if (level) { | |
2301 if (level < 0) { | |
2302 level = -level; | |
2303 level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2304 level = -level; | |
2305 } else { | |
2306 level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
2307 } | |
2308 #ifdef PARANOID | |
2309 if (level < -2048 || level > 2047) | |
2310 fprintf(stderr, "unquant error %d %d\n", i, level); | |
2311 #endif | |
2312 block[j] = level; | |
2313 } | |
2314 } | |
2315 } else { | |
2316 int sum=-1; | |
2317 i = 0; | |
344 | 2318 quant_matrix = s->inter_matrix; |
325 | 2319 for(;i<nCoeffs;i++) { |
2320 int j= zigzag_direct[i]; | |
2321 level = block[j]; | |
2322 if (level) { | |
2323 if (level < 0) { | |
2324 level = -level; | |
2325 level = (((level << 1) + 1) * qscale * | |
2326 ((int) (quant_matrix[j]))) >> 4; | |
2327 level = -level; | |
2328 } else { | |
2329 level = (((level << 1) + 1) * qscale * | |
2330 ((int) (quant_matrix[j]))) >> 4; | |
2331 } | |
2332 #ifdef PARANOID | |
2333 if (level < -2048 || level > 2047) | |
2334 fprintf(stderr, "unquant error %d %d\n", i, level); | |
2335 #endif | |
2336 block[j] = level; | |
2337 sum+=level; | |
2338 } | |
2339 } | |
2340 block[63]^=sum&1; | |
2341 } | |
2342 } | |
2343 | |
2344 | |
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
|
2345 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
|
2346 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
|
2347 { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
2348 int i, level, qmul, qadd; |
200 | 2349 int nCoeffs; |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2350 |
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
|
2351 if (s->mb_intra) { |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2352 if (!s->h263_aic) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2353 if (n < 4) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2354 block[0] = block[0] * s->y_dc_scale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2355 else |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2356 block[0] = block[0] * s->c_dc_scale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2357 } |
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
|
2358 i = 1; |
200 | 2359 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
|
2360 } 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
|
2361 i = 0; |
200 | 2362 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
|
2363 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
2364 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
2365 qmul = s->qscale << 1; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2366 if (s->h263_aic && s->mb_intra) |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2367 qadd = 0; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2368 else |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2369 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
|
2370 |
200 | 2371 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
|
2372 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
|
2373 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
|
2374 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
|
2375 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
|
2376 } 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
|
2377 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
|
2378 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
2379 #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
|
2380 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
|
2381 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
|
2382 #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
|
2383 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
|
2384 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
2385 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
2386 } |
0 | 2387 |
456 | 2388 static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y) |
2389 { | |
2390 int dc, dcb, dcr, y, i; | |
2391 for(i=0; i<4; i++){ | |
2392 dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)]; | |
2393 for(y=0; y<8; y++){ | |
2394 int x; | |
2395 for(x=0; x<8; x++){ | |
2396 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8; | |
2397 } | |
2398 } | |
2399 } | |
2400 dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)]; | |
2401 dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)]; | |
2402 for(y=0; y<8; y++){ | |
2403 int x; | |
2404 for(x=0; x<8; x++){ | |
2405 dest_cb[x + y*(s->linesize>>1)]= dcb/8; | |
2406 dest_cr[x + y*(s->linesize>>1)]= dcr/8; | |
2407 } | |
2408 } | |
2409 } | |
2410 | |
2411 /** | |
2412 * will conceal past errors, and allso drop b frames if needed | |
2413 * | |
2414 */ | |
2415 void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos) | |
2416 { | |
2417 int mb_x= s->mb_x; | |
2418 int mb_y= s->mb_y; | |
2419 int mb_dist=0; | |
2420 int i, intra_count=0, inter_count=0; | |
2421 int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune | |
2422 int inter_conceal= s->msmpeg4_version ? 50 : 50; | |
2423 | |
2424 // for last block | |
2425 if(mb_x>=s->mb_width) mb_x= s->mb_width -1; | |
2426 if(mb_y>=s->mb_height) mb_y= s->mb_height-1; | |
2427 | |
2428 if(s->decoding_error==0 && unknown_pos){ | |
2429 if(s->data_partitioning && s->pict_type!=B_TYPE) | |
2430 s->decoding_error= DECODING_AC_LOST; | |
2431 else | |
2432 s->decoding_error= DECODING_DESYNC; | |
2433 } | |
2434 | |
2435 if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1; | |
2436 | |
2437 for(i=mb_x + mb_y*s->mb_width; i>=0; i--){ | |
2438 if(s->mbintra_table[i]) intra_count++; | |
2439 else inter_count++; | |
2440 } | |
2441 | |
2442 if(s->decoding_error==DECODING_AC_LOST){ | |
2443 intra_conceal*=2; | |
2444 inter_conceal*=2; | |
2445 }else if(s->decoding_error==DECODING_ACDC_LOST){ | |
2446 intra_conceal*=2; | |
2447 inter_conceal*=2; | |
2448 } | |
2449 | |
2450 if(unknown_pos && (intra_count<inter_count)){ | |
2451 intra_conceal= inter_conceal= s->mb_num; | |
2452 // printf("%d %d\n",intra_count, inter_count); | |
2453 } | |
2454 | |
2455 fprintf(stderr, "concealing errors\n"); | |
2456 | |
2457 /* for all MBs from the current one back until the last resync marker */ | |
2458 for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){ | |
2459 for(; mb_x>=0; mb_x--){ | |
2460 uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16; | |
2461 uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
2462 uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
2463 int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion | |
2464 int mb_y_backup= s->mb_y; | |
2465 s->mb_x=mb_x; | |
2466 s->mb_y=mb_y; | |
2467 if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){ | |
2468 if(s->decoding_error==DECODING_AC_LOST){ | |
2469 remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y); | |
2470 // printf("remove ac to %d %d\n", mb_x, mb_y); | |
2471 }else{ | |
2472 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
2473 s->last_picture, 0, 0, put_pixels_tab, | |
2474 0/*mx*/, 0/*my*/, 16); | |
2475 } | |
2476 } | |
2477 else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){ | |
2478 int mx=0; | |
2479 int my=0; | |
2480 | |
2481 if(s->decoding_error!=DECODING_DESYNC){ | |
2482 int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2); | |
2483 mx= s->motion_val[ xy ][0]; | |
2484 my= s->motion_val[ xy ][1]; | |
2485 } | |
2486 | |
2487 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
2488 s->last_picture, 0, 0, put_pixels_tab, | |
2489 mx, my, 16); | |
2490 } | |
2491 s->mb_x= mb_x_backup; | |
2492 s->mb_y= mb_y_backup; | |
2493 | |
2494 if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return; | |
2495 if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++; | |
2496 } | |
2497 mb_x=s->mb_width-1; | |
2498 } | |
2499 } | |
2500 | |
0 | 2501 AVCodec mpeg1video_encoder = { |
2502 "mpeg1video", | |
2503 CODEC_TYPE_VIDEO, | |
2504 CODEC_ID_MPEG1VIDEO, | |
2505 sizeof(MpegEncContext), | |
2506 MPV_encode_init, | |
2507 MPV_encode_picture, | |
2508 MPV_encode_end, | |
2509 }; | |
2510 | |
2511 AVCodec h263_encoder = { | |
2512 "h263", | |
2513 CODEC_TYPE_VIDEO, | |
2514 CODEC_ID_H263, | |
2515 sizeof(MpegEncContext), | |
2516 MPV_encode_init, | |
2517 MPV_encode_picture, | |
2518 MPV_encode_end, | |
2519 }; | |
2520 | |
2521 AVCodec h263p_encoder = { | |
2522 "h263p", | |
2523 CODEC_TYPE_VIDEO, | |
2524 CODEC_ID_H263P, | |
2525 sizeof(MpegEncContext), | |
2526 MPV_encode_init, | |
2527 MPV_encode_picture, | |
2528 MPV_encode_end, | |
2529 }; | |
2530 | |
2531 AVCodec rv10_encoder = { | |
2532 "rv10", | |
2533 CODEC_TYPE_VIDEO, | |
2534 CODEC_ID_RV10, | |
2535 sizeof(MpegEncContext), | |
2536 MPV_encode_init, | |
2537 MPV_encode_picture, | |
2538 MPV_encode_end, | |
2539 }; | |
2540 | |
2541 AVCodec mjpeg_encoder = { | |
2542 "mjpeg", | |
2543 CODEC_TYPE_VIDEO, | |
2544 CODEC_ID_MJPEG, | |
2545 sizeof(MpegEncContext), | |
2546 MPV_encode_init, | |
2547 MPV_encode_picture, | |
2548 MPV_encode_end, | |
2549 }; | |
2550 | |
71 | 2551 AVCodec mpeg4_encoder = { |
2552 "mpeg4", | |
0 | 2553 CODEC_TYPE_VIDEO, |
71 | 2554 CODEC_ID_MPEG4, |
0 | 2555 sizeof(MpegEncContext), |
2556 MPV_encode_init, | |
2557 MPV_encode_picture, | |
2558 MPV_encode_end, | |
2559 }; | |
2560 | |
307 | 2561 AVCodec msmpeg4v1_encoder = { |
2562 "msmpeg4v1", | |
0 | 2563 CODEC_TYPE_VIDEO, |
307 | 2564 CODEC_ID_MSMPEG4V1, |
0 | 2565 sizeof(MpegEncContext), |
2566 MPV_encode_init, | |
2567 MPV_encode_picture, | |
2568 MPV_encode_end, | |
2569 }; | |
307 | 2570 |
2571 AVCodec msmpeg4v2_encoder = { | |
2572 "msmpeg4v2", | |
2573 CODEC_TYPE_VIDEO, | |
2574 CODEC_ID_MSMPEG4V2, | |
2575 sizeof(MpegEncContext), | |
2576 MPV_encode_init, | |
2577 MPV_encode_picture, | |
2578 MPV_encode_end, | |
2579 }; | |
2580 | |
2581 AVCodec msmpeg4v3_encoder = { | |
2582 "msmpeg4", | |
2583 CODEC_TYPE_VIDEO, | |
2584 CODEC_ID_MSMPEG4V3, | |
2585 sizeof(MpegEncContext), | |
2586 MPV_encode_init, | |
2587 MPV_encode_picture, | |
2588 MPV_encode_end, | |
2589 }; |