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