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