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