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