Mercurial > libavcodec.hg
annotate mpegvideo.c @ 280:3dc1ca4ba717 libavcodec
fixing epzs & mpeg1 (hopefully now really ...)
| author | michaelni |
|---|---|
| date | Fri, 22 Mar 2002 16:51:44 +0000 |
| parents | ae5c33165d5c |
| children | 1fc96b02142e |
| 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. | |
| 18 */ | |
| 19 #include <stdlib.h> | |
| 20 #include <stdio.h> | |
| 21 #include <math.h> | |
| 22 #include <string.h> | |
| 23 #include "avcodec.h" | |
| 24 #include "dsputil.h" | |
| 25 #include "mpegvideo.h" | |
| 26 | |
|
17
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
27 #ifdef USE_FASTMEMCPY |
|
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
28 #include "fastmemcpy.h" |
|
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
29 #endif |
|
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
30 |
|
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
|
31 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
|
32 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
|
33 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
|
34 static void dct_unquantize_mpeg1_c(MpegEncContext *s, |
|
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
35 DCTELEM *block, int n, int qscale); |
|
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_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
|
37 DCTELEM *block, int n, int qscale); |
| 206 | 38 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w); |
| 220 | 39 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale); |
| 206 | 40 |
| 220 | 41 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c; |
| 206 | 42 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c; |
| 43 | |
| 0 | 44 #define EDGE_WIDTH 16 |
| 45 | |
| 46 /* enable all paranoid tests for rounding, overflows, etc... */ | |
| 47 //#define PARANOID | |
| 48 | |
| 49 //#define DEBUG | |
| 50 | |
| 51 /* for jpeg fast DCT */ | |
| 52 #define CONST_BITS 14 | |
| 53 | |
| 54 static const unsigned short aanscales[64] = { | |
| 55 /* precomputed values scaled up by 14 bits */ | |
| 56 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
| 57 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
| 58 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
| 59 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
| 60 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
| 61 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
| 62 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
| 63 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
| 64 }; | |
| 65 | |
| 66 static UINT8 h263_chroma_roundtab[16] = { | |
| 67 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | |
| 68 }; | |
| 69 | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
70 static UINT16 default_mv_penalty[MAX_FCODE][MAX_MV*2+1]; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
71 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
|
72 |
| 0 | 73 /* default motion estimation */ |
| 74 int motion_estimation_method = ME_LOG; | |
| 75 | |
| 200 | 76 extern UINT8 zigzag_end[64]; |
| 77 | |
| 220 | 78 static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale) |
| 0 | 79 { |
| 80 int i; | |
| 81 | |
| 82 if (av_fdct == jpeg_fdct_ifast) { | |
| 83 for(i=0;i<64;i++) { | |
| 84 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
| 220 | 85 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ |
| 86 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ | |
| 87 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ | |
| 0 | 88 |
| 220 | 89 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / |
| 90 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)])); | |
| 0 | 91 } |
| 92 } else { | |
| 93 for(i=0;i<64;i++) { | |
| 94 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
| 220 | 95 So 16 <= qscale * quant_matrix[i] <= 7905 |
| 96 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905 | |
| 97 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 | |
| 0 | 98 */ |
| 220 | 99 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); |
| 100 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]); | |
| 0 | 101 } |
| 102 } | |
| 103 } | |
| 104 | |
| 105 /* init common structure for both encoder and decoder */ | |
| 106 int MPV_common_init(MpegEncContext *s) | |
| 107 { | |
| 108 int c_size, i; | |
| 109 UINT8 *pict; | |
| 110 | |
|
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
|
111 if (s->out_format == FMT_H263) |
|
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
|
112 s->dct_unquantize = dct_unquantize_h263_c; |
|
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
|
113 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
|
114 s->dct_unquantize = dct_unquantize_mpeg1_c; |
|
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
|
115 |
|
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
|
116 #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
|
117 MPV_common_init_mmx(s); |
| 8 | 118 #endif |
| 0 | 119 s->mb_width = (s->width + 15) / 16; |
| 120 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
|
121 s->mb_num = s->mb_width * s->mb_height; |
| 0 | 122 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH; |
| 123 | |
| 124 for(i=0;i<3;i++) { | |
| 125 int w, h, shift, pict_start; | |
| 126 | |
| 127 w = s->linesize; | |
| 128 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | |
| 129 shift = (i == 0) ? 0 : 1; | |
| 130 c_size = (w >> shift) * (h >> shift); | |
| 131 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | |
| 132 | |
| 133 pict = av_mallocz(c_size); | |
| 134 if (pict == NULL) | |
| 135 goto fail; | |
| 136 s->last_picture_base[i] = pict; | |
| 137 s->last_picture[i] = pict + pict_start; | |
| 138 | |
| 139 pict = av_mallocz(c_size); | |
| 140 if (pict == NULL) | |
| 141 goto fail; | |
| 142 s->next_picture_base[i] = pict; | |
| 143 s->next_picture[i] = pict + pict_start; | |
| 144 | |
| 145 if (s->has_b_frames) { | |
| 146 pict = av_mallocz(c_size); | |
| 147 if (pict == NULL) | |
| 148 goto fail; | |
| 149 s->aux_picture_base[i] = pict; | |
| 150 s->aux_picture[i] = pict + pict_start; | |
| 151 } | |
| 152 } | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
153 |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
154 if (s->encoding) { |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
155 /* Allocate MB type table */ |
| 239 | 156 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
|
157 if (s->mb_type == NULL) { |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
158 perror("malloc"); |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
159 goto fail; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
160 } |
| 239 | 161 |
| 162 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16)); | |
| 163 if (s->mb_var == NULL) { | |
| 164 perror("malloc"); | |
| 165 goto fail; | |
| 166 } | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
167 /* Allocate MV table */ |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
168 /* By now we just have one MV per MB */ |
| 239 | 169 s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16)); |
| 170 s->mv_table[1] = av_mallocz(s->mb_num * sizeof(INT16)); | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
171 if (s->mv_table[1] == NULL || s->mv_table[0] == NULL) { |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
172 perror("malloc"); |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
173 goto fail; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
174 } |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
175 } |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
176 |
| 280 | 177 if (s->out_format == FMT_H263) { |
| 0 | 178 int size; |
| 179 /* MV prediction */ | |
| 180 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
| 181 s->motion_val = malloc(size * 2 * sizeof(INT16)); | |
| 182 if (s->motion_val == NULL) | |
| 183 goto fail; | |
| 184 memset(s->motion_val, 0, size * 2 * sizeof(INT16)); | |
| 185 } | |
| 186 | |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
187 if (s->h263_pred || s->h263_plus) { |
| 0 | 188 int y_size, c_size, i, size; |
| 189 | |
| 190 /* dc values */ | |
| 191 | |
| 192 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
| 193 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
| 194 size = y_size + 2 * c_size; | |
| 195 s->dc_val[0] = malloc(size * sizeof(INT16)); | |
| 196 if (s->dc_val[0] == NULL) | |
| 197 goto fail; | |
| 198 s->dc_val[1] = s->dc_val[0] + y_size; | |
| 199 s->dc_val[2] = s->dc_val[1] + c_size; | |
| 200 for(i=0;i<size;i++) | |
| 201 s->dc_val[0][i] = 1024; | |
| 202 | |
| 203 /* ac values */ | |
| 204 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16); | |
| 205 if (s->ac_val[0] == NULL) | |
| 206 goto fail; | |
| 207 s->ac_val[1] = s->ac_val[0] + y_size; | |
| 208 s->ac_val[2] = s->ac_val[1] + c_size; | |
| 209 | |
| 210 /* cbp values */ | |
| 211 s->coded_block = av_mallocz(y_size); | |
| 212 if (!s->coded_block) | |
| 213 goto fail; | |
|
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
214 |
|
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
215 /* 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
|
216 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
|
217 if (!s->mbintra_table) |
|
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
218 goto fail; |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
219 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
|
220 } |
| 0 | 221 /* default structure is frame */ |
| 222 s->picture_structure = PICT_FRAME; | |
| 223 | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
224 /* init macroblock skip table */ |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
225 if (!s->encoding) { |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
226 s->mbskip_table = av_mallocz(s->mb_num); |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
227 if (!s->mbskip_table) |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
228 goto fail; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
229 } |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
230 |
| 0 | 231 s->context_initialized = 1; |
| 232 return 0; | |
| 233 fail: | |
| 244 | 234 MPV_common_end(s); |
| 235 return -1; | |
| 236 } | |
| 237 | |
| 238 /* init common structure for both encoder and decoder */ | |
| 239 void MPV_common_end(MpegEncContext *s) | |
| 240 { | |
| 241 int i; | |
| 242 | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
243 if (s->mb_type) |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
244 free(s->mb_type); |
| 239 | 245 if (s->mb_var) |
| 246 free(s->mb_var); | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
247 if (s->mv_table[0]) |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
248 free(s->mv_table[0]); |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
249 if (s->mv_table[1]) |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
250 free(s->mv_table[1]); |
| 0 | 251 if (s->motion_val) |
| 252 free(s->motion_val); | |
| 253 if (s->dc_val[0]) | |
| 254 free(s->dc_val[0]); | |
| 255 if (s->ac_val[0]) | |
| 256 free(s->ac_val[0]); | |
| 257 if (s->coded_block) | |
| 258 free(s->coded_block); | |
|
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
259 if (s->mbintra_table) |
|
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
|
260 free(s->mbintra_table); |
| 244 | 261 |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
262 if (s->mbskip_table) |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
263 free(s->mbskip_table); |
| 0 | 264 for(i=0;i<3;i++) { |
| 265 if (s->last_picture_base[i]) | |
| 244 | 266 free(s->last_picture_base[i]); |
| 267 if (s->next_picture_base[i]) | |
| 268 free(s->next_picture_base[i]); | |
| 0 | 269 if (s->has_b_frames) |
| 270 free(s->aux_picture_base[i]); | |
| 271 } | |
| 272 s->context_initialized = 0; | |
| 273 } | |
| 274 | |
| 275 /* init video encoder */ | |
| 276 int MPV_encode_init(AVCodecContext *avctx) | |
| 277 { | |
| 278 MpegEncContext *s = avctx->priv_data; | |
| 60 | 279 int i; |
| 0 | 280 |
| 281 s->bit_rate = avctx->bit_rate; | |
| 268 | 282 s->bit_rate_tolerance = avctx->bit_rate_tolerance; |
| 0 | 283 s->frame_rate = avctx->frame_rate; |
| 284 s->width = avctx->width; | |
| 285 s->height = avctx->height; | |
| 286 s->gop_size = avctx->gop_size; | |
| 162 | 287 s->rtp_mode = avctx->rtp_mode; |
| 288 s->rtp_payload_size = avctx->rtp_payload_size; | |
| 231 | 289 if (avctx->rtp_callback) |
| 290 s->rtp_callback = avctx->rtp_callback; | |
| 268 | 291 s->qmin= avctx->qmin; |
| 292 s->qmax= avctx->qmax; | |
| 293 s->max_qdiff= avctx->max_qdiff; | |
| 294 s->qcompress= avctx->qcompress; | |
| 295 s->qblur= avctx->qblur; | |
|
194
27d1773552c9
mpeg4 encoder fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
191
diff
changeset
|
296 s->avctx = avctx; |
| 162 | 297 |
| 0 | 298 if (s->gop_size <= 1) { |
| 299 s->intra_only = 1; | |
| 300 s->gop_size = 12; | |
| 301 } else { | |
| 302 s->intra_only = 0; | |
| 303 } | |
| 304 s->full_search = motion_estimation_method; | |
| 305 | |
| 306 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
|
307 |
| 0 | 308 switch(avctx->codec->id) { |
| 309 case CODEC_ID_MPEG1VIDEO: | |
| 310 s->out_format = FMT_MPEG1; | |
| 311 break; | |
| 312 case CODEC_ID_MJPEG: | |
| 313 s->out_format = FMT_MJPEG; | |
| 314 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
|
315 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
|
316 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
|
317 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
|
318 s->mjpeg_vsample[2] = 1; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
319 s->mjpeg_hsample[0] = 2; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
320 s->mjpeg_hsample[1] = 1; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
321 s->mjpeg_hsample[2] = 1; |
| 0 | 322 if (mjpeg_init(s) < 0) |
| 323 return -1; | |
| 324 break; | |
| 325 case CODEC_ID_H263: | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
326 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
|
327 printf("Input picture size isn't suitable for h263 codec! try h263+\n"); |
| 0 | 328 return -1; |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
329 } |
| 0 | 330 s->out_format = FMT_H263; |
| 331 break; | |
| 332 case CODEC_ID_H263P: | |
| 333 s->out_format = FMT_H263; | |
| 162 | 334 s->rtp_mode = 1; |
| 335 s->rtp_payload_size = 1200; | |
| 0 | 336 s->h263_plus = 1; |
| 78 | 337 s->unrestricted_mv = 1; |
|
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
338 |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
339 /* These are just to be sure */ |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
340 s->umvplus = 0; |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
341 s->umvplus_dec = 0; |
| 0 | 342 break; |
| 343 case CODEC_ID_RV10: | |
| 344 s->out_format = FMT_H263; | |
| 345 s->h263_rv10 = 1; | |
| 346 break; | |
| 71 | 347 case CODEC_ID_MPEG4: |
| 0 | 348 s->out_format = FMT_H263; |
| 349 s->h263_pred = 1; | |
| 350 s->unrestricted_mv = 1; | |
| 351 break; | |
| 352 case CODEC_ID_MSMPEG4: | |
| 353 s->out_format = FMT_H263; | |
| 354 s->h263_msmpeg4 = 1; | |
| 355 s->h263_pred = 1; | |
| 356 s->unrestricted_mv = 1; | |
| 357 break; | |
| 358 default: | |
| 359 return -1; | |
| 360 } | |
| 361 | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
362 { /* 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
|
363 static int done=0; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
364 if(!done){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
365 int i; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
366 done=1; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
367 memset(default_mv_penalty, 0, sizeof(UINT16)*MAX_FCODE*(2*MAX_MV+1)); |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
368 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
|
369 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
370 for(i=-16; i<16; i++){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
371 default_fcode_tab[i + MAX_MV]= 1; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
372 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
373 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
374 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
375 s->mv_penalty= default_mv_penalty; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
376 s->fcode_tab= default_fcode_tab; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
377 |
| 0 | 378 if (s->out_format == FMT_H263) |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
379 h263_encode_init(s); |
| 0 | 380 |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
381 s->encoding = 1; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
382 |
| 0 | 383 /* init */ |
| 384 if (MPV_common_init(s) < 0) | |
| 385 return -1; | |
| 386 | |
| 60 | 387 /* init default q matrix */ |
| 388 for(i=0;i<64;i++) { | |
| 389 s->intra_matrix[i] = default_intra_matrix[i]; | |
| 390 s->non_intra_matrix[i] = default_non_intra_matrix[i]; | |
| 391 } | |
| 392 | |
| 0 | 393 /* rate control init */ |
| 394 rate_control_init(s); | |
| 395 | |
| 396 s->picture_number = 0; | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
397 s->picture_in_gop_number = 0; |
| 0 | 398 s->fake_picture_number = 0; |
| 399 /* motion detector init */ | |
| 400 s->f_code = 1; | |
| 401 | |
| 402 return 0; | |
| 403 } | |
| 404 | |
| 405 int MPV_encode_end(AVCodecContext *avctx) | |
| 406 { | |
| 407 MpegEncContext *s = avctx->priv_data; | |
| 408 | |
| 409 #ifdef STATS | |
| 410 print_stats(); | |
| 411 #endif | |
| 412 MPV_common_end(s); | |
| 413 if (s->out_format == FMT_MJPEG) | |
| 414 mjpeg_close(s); | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
415 |
| 0 | 416 return 0; |
| 417 } | |
| 418 | |
| 419 /* draw the edges of width 'w' of an image of size width, height */ | |
| 206 | 420 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w) |
| 0 | 421 { |
| 422 UINT8 *ptr, *last_line; | |
| 423 int i; | |
| 424 | |
| 425 last_line = buf + (height - 1) * wrap; | |
| 426 for(i=0;i<w;i++) { | |
| 427 /* top and bottom */ | |
| 428 memcpy(buf - (i + 1) * wrap, buf, width); | |
| 429 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
| 430 } | |
| 431 /* left and right */ | |
| 432 ptr = buf; | |
| 433 for(i=0;i<height;i++) { | |
| 434 memset(ptr - w, ptr[0], w); | |
| 435 memset(ptr + width, ptr[width-1], w); | |
| 436 ptr += wrap; | |
| 437 } | |
| 438 /* corners */ | |
| 439 for(i=0;i<w;i++) { | |
| 440 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
| 441 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
| 442 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
| 443 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 /* generic function for encode/decode called before a frame is coded/decoded */ | |
| 448 void MPV_frame_start(MpegEncContext *s) | |
| 449 { | |
| 450 int i; | |
| 451 UINT8 *tmp; | |
| 452 | |
|
46
931417475f5b
fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents:
40
diff
changeset
|
453 s->mb_skiped = 0; |
| 0 | 454 if (s->pict_type == B_TYPE) { |
| 455 for(i=0;i<3;i++) { | |
| 456 s->current_picture[i] = s->aux_picture[i]; | |
| 457 } | |
| 458 } else { | |
| 262 | 459 s->last_non_b_pict_type= s->pict_type; |
| 0 | 460 for(i=0;i<3;i++) { |
| 461 /* swap next and last */ | |
| 462 tmp = s->last_picture[i]; | |
| 463 s->last_picture[i] = s->next_picture[i]; | |
| 464 s->next_picture[i] = tmp; | |
| 465 s->current_picture[i] = tmp; | |
| 466 } | |
| 467 } | |
| 468 } | |
|
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
|
469 |
| 0 | 470 /* generic function for encode/decode called after a frame has been coded/decoded */ |
| 471 void MPV_frame_end(MpegEncContext *s) | |
| 472 { | |
| 473 /* draw edge for correct motion prediction if outside */ | |
| 220 | 474 if (s->pict_type != B_TYPE && !s->intra_only) { |
| 257 | 475 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
|
476 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
|
477 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
|
478 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
|
479 }else{ |
| 257 | 480 /* mpeg4? / opendivx / xvid */ |
| 0 | 481 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH); |
| 482 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
| 483 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
|
484 } |
| 0 | 485 } |
| 207 | 486 emms_c(); |
| 0 | 487 } |
| 488 | |
| 489 int MPV_encode_picture(AVCodecContext *avctx, | |
| 490 unsigned char *buf, int buf_size, void *data) | |
| 491 { | |
| 492 MpegEncContext *s = avctx->priv_data; | |
| 493 AVPicture *pict = data; | |
| 494 int i, j; | |
| 495 | |
| 496 if (s->fixed_qscale) | |
| 497 s->qscale = avctx->quality; | |
| 498 | |
| 499 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
| 500 | |
| 501 if (!s->intra_only) { | |
| 502 /* first picture of GOP is intra */ | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
503 if (s->picture_in_gop_number >= s->gop_size){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
504 s->picture_in_gop_number=0; |
| 0 | 505 s->pict_type = I_TYPE; |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
506 }else |
| 0 | 507 s->pict_type = P_TYPE; |
| 508 } else { | |
| 509 s->pict_type = I_TYPE; | |
| 510 } | |
| 511 | |
| 512 MPV_frame_start(s); | |
| 220 | 513 |
| 0 | 514 for(i=0;i<3;i++) { |
| 515 UINT8 *src = pict->data[i]; | |
| 516 UINT8 *dest = s->current_picture[i]; | |
| 517 int src_wrap = pict->linesize[i]; | |
| 518 int dest_wrap = s->linesize; | |
| 519 int w = s->width; | |
| 520 int h = s->height; | |
| 521 | |
| 522 if (i >= 1) { | |
| 523 dest_wrap >>= 1; | |
| 524 w >>= 1; | |
| 525 h >>= 1; | |
| 526 } | |
| 527 | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
528 if(dest_wrap==src_wrap){ |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
529 s->new_picture[i] = pict->data[i]; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
530 } else { |
| 220 | 531 for(j=0;j<h;j++) { |
| 532 memcpy(dest, src, w); | |
| 533 dest += dest_wrap; | |
| 534 src += src_wrap; | |
| 535 } | |
|
227
ec1bc02a0a47
avoid copying input when encoding non intra stuff too
michaelni
parents:
220
diff
changeset
|
536 s->new_picture[i] = s->current_picture[i]; |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
537 } |
| 0 | 538 } |
| 539 | |
| 540 encode_picture(s, s->picture_number); | |
| 271 | 541 avctx->key_frame = (s->pict_type == I_TYPE); |
| 0 | 542 |
| 543 MPV_frame_end(s); | |
| 544 s->picture_number++; | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
545 s->picture_in_gop_number++; |
| 0 | 546 |
| 547 if (s->out_format == FMT_MJPEG) | |
| 548 mjpeg_picture_trailer(s); | |
| 549 | |
| 550 flush_put_bits(&s->pb); | |
| 268 | 551 s->last_frame_bits= s->frame_bits; |
| 552 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; | |
| 553 s->total_bits += s->frame_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
|
554 |
| 0 | 555 avctx->quality = s->qscale; |
|
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
556 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
|
557 /* 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
|
558 /* 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
|
559 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
|
560 pict->linesize, s->linesize, avctx); |
|
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
561 } |
|
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
|
562 return pbBufPtr(&s->pb) - s->pb.buf; |
| 0 | 563 } |
| 564 | |
| 565 static inline int clip(int a, int amin, int amax) | |
| 566 { | |
| 567 if (a < amin) | |
| 568 return amin; | |
| 569 else if (a > amax) | |
| 570 return amax; | |
| 571 else | |
| 572 return a; | |
| 573 } | |
| 574 | |
| 255 | 575 static inline void gmc1_motion(MpegEncContext *s, |
| 576 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 577 int dest_offset, | |
| 578 UINT8 **ref_picture, int src_offset, | |
| 579 int h) | |
| 580 { | |
| 581 UINT8 *ptr; | |
| 582 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
| 583 int motion_x, motion_y; | |
| 584 | |
|
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
585 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n"); |
| 255 | 586 motion_x= s->sprite_offset[0][0]; |
| 587 motion_y= s->sprite_offset[0][1]; | |
| 588 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
| 589 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
| 590 motion_x<<=(3-s->sprite_warping_accuracy); | |
| 591 motion_y<<=(3-s->sprite_warping_accuracy); | |
| 592 src_x = clip(src_x, -16, s->width); | |
| 593 if (src_x == s->width) | |
| 594 motion_x =0; | |
| 595 src_y = clip(src_y, -16, s->height); | |
| 596 if (src_y == s->height) | |
| 597 motion_y =0; | |
| 598 | |
| 599 linesize = s->linesize; | |
| 600 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
| 601 | |
| 602 dest_y+=dest_offset; | |
| 603 gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
| 604 gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
| 605 | |
| 606 motion_x= s->sprite_offset[1][0]; | |
| 607 motion_y= s->sprite_offset[1][1]; | |
| 608 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
| 609 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
| 610 motion_x<<=(3-s->sprite_warping_accuracy); | |
| 611 motion_y<<=(3-s->sprite_warping_accuracy); | |
| 612 src_x = clip(src_x, -8, s->width>>1); | |
| 613 if (src_x == s->width>>1) | |
| 614 motion_x =0; | |
| 615 src_y = clip(src_y, -8, s->height>>1); | |
| 616 if (src_y == s->height>>1) | |
| 617 motion_y =0; | |
| 618 | |
| 619 offset = (src_y * linesize>>1) + src_x + (src_offset>>1); | |
| 620 ptr = ref_picture[1] + offset; | |
| 621 gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
| 622 ptr = ref_picture[2] + offset; | |
| 623 gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
| 624 | |
| 625 return; | |
| 626 } | |
| 627 | |
| 0 | 628 /* apply one mpeg motion vector to the three components */ |
| 629 static inline void mpeg_motion(MpegEncContext *s, | |
| 630 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 631 int dest_offset, | |
| 632 UINT8 **ref_picture, int src_offset, | |
| 633 int field_based, op_pixels_func *pix_op, | |
| 634 int motion_x, int motion_y, int h) | |
| 635 { | |
| 636 UINT8 *ptr; | |
| 637 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
| 255 | 638 if(s->quarter_sample) |
| 639 { | |
| 640 motion_x>>=1; | |
| 641 motion_y>>=1; | |
| 642 } | |
| 0 | 643 dxy = ((motion_y & 1) << 1) | (motion_x & 1); |
| 644 src_x = s->mb_x * 16 + (motion_x >> 1); | |
| 645 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
| 646 | |
| 647 /* WARNING: do no forget half pels */ | |
| 648 height = s->height >> field_based; | |
| 649 src_x = clip(src_x, -16, s->width); | |
| 650 if (src_x == s->width) | |
| 651 dxy &= ~1; | |
| 652 src_y = clip(src_y, -16, height); | |
| 653 if (src_y == height) | |
| 654 dxy &= ~2; | |
| 655 linesize = s->linesize << field_based; | |
| 656 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; | |
| 657 dest_y += dest_offset; | |
| 658 pix_op[dxy](dest_y, ptr, linesize, h); | |
| 659 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h); | |
| 660 | |
| 661 if (s->out_format == FMT_H263) { | |
| 662 dxy = 0; | |
| 663 if ((motion_x & 3) != 0) | |
| 664 dxy |= 1; | |
| 665 if ((motion_y & 3) != 0) | |
| 666 dxy |= 2; | |
| 667 mx = motion_x >> 2; | |
| 668 my = motion_y >> 2; | |
| 669 } else { | |
| 670 mx = motion_x / 2; | |
| 671 my = motion_y / 2; | |
| 672 dxy = ((my & 1) << 1) | (mx & 1); | |
| 673 mx >>= 1; | |
| 674 my >>= 1; | |
| 675 } | |
| 676 | |
| 677 src_x = s->mb_x * 8 + mx; | |
| 678 src_y = s->mb_y * (8 >> field_based) + my; | |
| 679 src_x = clip(src_x, -8, s->width >> 1); | |
| 680 if (src_x == (s->width >> 1)) | |
| 681 dxy &= ~1; | |
| 682 src_y = clip(src_y, -8, height >> 1); | |
| 683 if (src_y == (height >> 1)) | |
| 684 dxy &= ~2; | |
| 685 | |
| 686 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
| 687 ptr = ref_picture[1] + offset; | |
| 688 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
| 689 ptr = ref_picture[2] + offset; | |
| 690 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
| 691 } | |
| 692 | |
| 255 | 693 static inline void qpel_motion(MpegEncContext *s, |
| 694 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 695 int dest_offset, | |
| 696 UINT8 **ref_picture, int src_offset, | |
| 697 int field_based, op_pixels_func *pix_op, | |
| 698 qpel_mc_func *qpix_op, | |
| 699 int motion_x, int motion_y, int h) | |
| 700 { | |
| 701 UINT8 *ptr; | |
| 702 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
| 703 | |
| 704 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
| 705 src_x = s->mb_x * 16 + (motion_x >> 2); | |
| 706 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); | |
| 707 | |
| 708 height = s->height >> field_based; | |
| 709 src_x = clip(src_x, -16, s->width); | |
| 710 if (src_x == s->width) | |
| 711 dxy &= ~3; | |
| 712 src_y = clip(src_y, -16, height); | |
| 713 if (src_y == height) | |
| 714 dxy &= ~12; | |
| 715 linesize = s->linesize << field_based; | |
| 716 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
| 717 dest_y += dest_offset; | |
| 718 //printf("%d %d %d\n", src_x, src_y, dxy); | |
| 719 qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3); | |
| 720 qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3); | |
| 721 qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3); | |
| 722 qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3); | |
| 723 | |
| 724 mx= (motion_x>>1) | (motion_x&1); | |
| 725 my= (motion_y>>1) | (motion_y&1); | |
| 726 | |
| 727 dxy = 0; | |
| 728 if ((mx & 3) != 0) | |
| 729 dxy |= 1; | |
| 730 if ((my & 3) != 0) | |
| 731 dxy |= 2; | |
| 732 mx = mx >> 2; | |
| 733 my = my >> 2; | |
| 734 | |
| 735 src_x = s->mb_x * 8 + mx; | |
| 736 src_y = s->mb_y * (8 >> field_based) + my; | |
| 737 src_x = clip(src_x, -8, s->width >> 1); | |
| 738 if (src_x == (s->width >> 1)) | |
| 739 dxy &= ~1; | |
| 740 src_y = clip(src_y, -8, height >> 1); | |
| 741 if (src_y == (height >> 1)) | |
| 742 dxy &= ~2; | |
| 743 | |
| 744 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
| 745 ptr = ref_picture[1] + offset; | |
| 746 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
| 747 ptr = ref_picture[2] + offset; | |
| 748 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
| 749 } | |
| 750 | |
| 751 | |
| 0 | 752 static inline void MPV_motion(MpegEncContext *s, |
| 753 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 754 int dir, UINT8 **ref_picture, | |
| 255 | 755 op_pixels_func *pix_op, qpel_mc_func *qpix_op) |
| 0 | 756 { |
| 757 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
| 758 int mb_x, mb_y, i; | |
| 759 UINT8 *ptr, *dest; | |
| 760 | |
| 761 mb_x = s->mb_x; | |
| 762 mb_y = s->mb_y; | |
| 763 | |
| 764 switch(s->mv_type) { | |
| 765 case MV_TYPE_16X16: | |
| 255 | 766 if(s->mcsel){ |
| 767 #if 0 | |
| 768 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 769 ref_picture, 0, | |
| 770 0, pix_op, | |
| 771 s->sprite_offset[0][0]>>3, | |
| 772 s->sprite_offset[0][1]>>3, | |
| 773 16); | |
| 774 #else | |
| 775 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 776 ref_picture, 0, | |
| 777 16); | |
| 778 #endif | |
| 262 | 779 }else if(s->quarter_sample && dir==0){ //FIXME |
| 255 | 780 qpel_motion(s, dest_y, dest_cb, dest_cr, 0, |
| 781 ref_picture, 0, | |
| 782 0, pix_op, qpix_op, | |
| 783 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
| 784 }else{ | |
| 785 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 786 ref_picture, 0, | |
| 787 0, pix_op, | |
| 788 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
| 789 } | |
| 0 | 790 break; |
| 791 case MV_TYPE_8X8: | |
| 792 for(i=0;i<4;i++) { | |
| 793 motion_x = s->mv[dir][i][0]; | |
| 794 motion_y = s->mv[dir][i][1]; | |
| 795 | |
| 796 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
| 797 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
| 798 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8; | |
| 799 | |
| 800 /* WARNING: do no forget half pels */ | |
| 801 src_x = clip(src_x, -16, s->width); | |
| 802 if (src_x == s->width) | |
| 803 dxy &= ~1; | |
| 804 src_y = clip(src_y, -16, s->height); | |
| 805 if (src_y == s->height) | |
| 806 dxy &= ~2; | |
| 807 | |
| 808 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
| 809 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
| 810 pix_op[dxy](dest, ptr, s->linesize, 8); | |
| 811 } | |
| 812 /* In case of 8X8, we construct a single chroma motion vector | |
| 813 with a special rounding */ | |
| 814 mx = 0; | |
| 815 my = 0; | |
| 816 for(i=0;i<4;i++) { | |
| 817 mx += s->mv[dir][i][0]; | |
| 818 my += s->mv[dir][i][1]; | |
| 819 } | |
| 820 if (mx >= 0) | |
| 821 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
| 822 else { | |
| 823 mx = -mx; | |
| 824 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
| 825 } | |
| 826 if (my >= 0) | |
| 827 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
| 828 else { | |
| 829 my = -my; | |
| 830 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
| 831 } | |
| 832 dxy = ((my & 1) << 1) | (mx & 1); | |
| 833 mx >>= 1; | |
| 834 my >>= 1; | |
| 835 | |
| 836 src_x = mb_x * 8 + mx; | |
| 837 src_y = mb_y * 8 + my; | |
| 838 src_x = clip(src_x, -8, s->width/2); | |
| 839 if (src_x == s->width/2) | |
| 840 dxy &= ~1; | |
| 841 src_y = clip(src_y, -8, s->height/2); | |
| 842 if (src_y == s->height/2) | |
| 843 dxy &= ~2; | |
| 844 | |
| 845 offset = (src_y * (s->linesize >> 1)) + src_x; | |
| 846 ptr = ref_picture[1] + offset; | |
| 847 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8); | |
| 848 ptr = ref_picture[2] + offset; | |
| 849 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8); | |
| 850 break; | |
| 851 case MV_TYPE_FIELD: | |
| 852 if (s->picture_structure == PICT_FRAME) { | |
| 853 /* top field */ | |
| 854 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 855 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
| 856 1, pix_op, | |
| 857 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
| 858 /* bottom field */ | |
| 859 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
| 860 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
| 861 1, pix_op, | |
| 862 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
| 863 } else { | |
| 864 | |
| 865 | |
| 866 } | |
| 867 break; | |
| 868 } | |
| 869 } | |
| 870 | |
| 871 | |
| 872 /* put block[] to dest[] */ | |
| 873 static inline void put_dct(MpegEncContext *s, | |
| 874 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
| 875 { | |
| 876 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
|
877 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
|
878 ff_idct (block); |
| 0 | 879 put_pixels_clamped(block, dest, line_size); |
| 880 } | |
| 881 | |
| 882 /* add block[] to dest[] */ | |
| 883 static inline void add_dct(MpegEncContext *s, | |
| 884 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
| 885 { | |
| 886 if (s->block_last_index[i] >= 0) { | |
| 887 if (!s->mpeg2) | |
| 206 | 888 if(s->encoding || (!s->h263_msmpeg4)) |
| 200 | 889 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
|
890 ff_idct (block); |
| 0 | 891 add_pixels_clamped(block, dest, line_size); |
| 892 } | |
| 893 } | |
| 894 | |
| 895 /* generic function called after a macroblock has been parsed by the | |
| 896 decoder or after it has been encoded by the encoder. | |
| 897 | |
| 898 Important variables used: | |
| 899 s->mb_intra : true if intra macroblock | |
| 900 s->mv_dir : motion vector direction | |
| 901 s->mv_type : motion vector type | |
| 902 s->mv : motion vector | |
| 903 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
| 904 */ | |
| 905 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
| 906 { | |
| 244 | 907 int mb_x, mb_y; |
| 0 | 908 int dct_linesize, dct_offset; |
| 909 op_pixels_func *op_pix; | |
| 255 | 910 qpel_mc_func *op_qpix; |
| 0 | 911 |
| 912 mb_x = s->mb_x; | |
| 913 mb_y = s->mb_y; | |
| 914 | |
|
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
915 #ifdef FF_POSTPROCESS |
|
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
916 quant_store[mb_y][mb_x]=s->qscale; |
|
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
917 //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
|
918 #endif |
|
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
919 |
| 0 | 920 /* update DC predictors for P macroblocks */ |
| 921 if (!s->mb_intra) { | |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
922 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
|
923 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
|
924 { |
| 244 | 925 int wrap, xy, v; |
|
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
926 s->mbintra_table[mb_x + mb_y*s->mb_width]=0; |
| 0 | 927 wrap = 2 * s->mb_width + 2; |
| 244 | 928 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; |
| 0 | 929 v = 1024; |
|
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
930 |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
931 s->dc_val[0][xy] = v; |
| 244 | 932 s->dc_val[0][xy + 1] = v; |
| 933 s->dc_val[0][xy + wrap] = v; | |
| 934 s->dc_val[0][xy + 1 + wrap] = v; | |
| 0 | 935 /* ac pred */ |
| 244 | 936 memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16)); |
| 937 memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16)); | |
| 938 memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16)); | |
| 939 memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16)); | |
| 0 | 940 if (s->h263_msmpeg4) { |
| 244 | 941 s->coded_block[xy] = 0; |
| 942 s->coded_block[xy + 1] = 0; | |
| 943 s->coded_block[xy + wrap] = 0; | |
| 944 s->coded_block[xy + 1 + wrap] = 0; | |
| 0 | 945 } |
| 946 /* chroma */ | |
| 947 wrap = s->mb_width + 2; | |
| 244 | 948 xy = mb_x + 1 + (mb_y + 1) * wrap; |
| 949 s->dc_val[1][xy] = v; | |
| 950 s->dc_val[2][xy] = v; | |
| 0 | 951 /* ac pred */ |
| 244 | 952 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); |
| 953 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
|
954 } |
| 0 | 955 } else { |
| 956 s->last_dc[0] = 128 << s->intra_dc_precision; | |
| 957 s->last_dc[1] = 128 << s->intra_dc_precision; | |
| 958 s->last_dc[2] = 128 << s->intra_dc_precision; | |
| 959 } | |
| 960 } | |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
961 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
|
962 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
|
963 |
| 280 | 964 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */ |
| 965 if (s->out_format == FMT_H263) { | |
| 262 | 966 if(s->pict_type!=B_TYPE){ |
| 244 | 967 int xy, wrap, motion_x, motion_y; |
| 0 | 968 |
| 969 wrap = 2 * s->mb_width + 2; | |
| 244 | 970 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; |
| 0 | 971 if (s->mb_intra) { |
| 972 motion_x = 0; | |
| 973 motion_y = 0; | |
| 974 goto motion_init; | |
| 975 } else if (s->mv_type == MV_TYPE_16X16) { | |
| 976 motion_x = s->mv[0][0][0]; | |
| 977 motion_y = s->mv[0][0][1]; | |
| 978 motion_init: | |
| 979 /* no update if 8X8 because it has been done during parsing */ | |
| 244 | 980 s->motion_val[xy][0] = motion_x; |
| 981 s->motion_val[xy][1] = motion_y; | |
| 982 s->motion_val[xy + 1][0] = motion_x; | |
| 983 s->motion_val[xy + 1][1] = motion_y; | |
| 984 s->motion_val[xy + wrap][0] = motion_x; | |
| 985 s->motion_val[xy + wrap][1] = motion_y; | |
| 986 s->motion_val[xy + 1 + wrap][0] = motion_x; | |
| 987 s->motion_val[xy + 1 + wrap][1] = motion_y; | |
| 0 | 988 } |
| 262 | 989 } |
| 0 | 990 } |
| 991 | |
| 992 if (!s->intra_only) { | |
| 993 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
|
994 UINT8 *mbskip_ptr; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
995 |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
996 /* avoid copy if macroblock skipped in last frame too */ |
|
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
|
997 if (!s->encoding && s->pict_type != B_TYPE) { |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
998 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
|
999 if (s->mb_skiped) { |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1000 s->mb_skiped = 0; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1001 /* if previous was skipped too, then nothing to do ! */ |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1002 if (*mbskip_ptr != 0) |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1003 goto the_end; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1004 *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
|
1005 } else { |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1006 *mbskip_ptr = 0; /* not skipped */ |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1007 } |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1008 } |
| 0 | 1009 |
| 1010 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16; | |
| 1011 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
| 1012 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
| 1013 | |
| 1014 if (s->interlaced_dct) { | |
| 1015 dct_linesize = s->linesize * 2; | |
| 1016 dct_offset = s->linesize; | |
| 1017 } else { | |
| 1018 dct_linesize = s->linesize; | |
| 1019 dct_offset = s->linesize * 8; | |
| 1020 } | |
| 1021 | |
| 1022 if (!s->mb_intra) { | |
| 1023 /* motion handling */ | |
| 255 | 1024 if (!s->no_rounding){ |
| 0 | 1025 op_pix = put_pixels_tab; |
| 255 | 1026 op_qpix= qpel_mc_rnd_tab; |
| 1027 }else{ | |
| 0 | 1028 op_pix = put_no_rnd_pixels_tab; |
| 255 | 1029 op_qpix= qpel_mc_no_rnd_tab; |
| 1030 } | |
| 0 | 1031 |
| 1032 if (s->mv_dir & MV_DIR_FORWARD) { | |
| 255 | 1033 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); |
| 0 | 1034 if (!s->no_rounding) |
| 1035 op_pix = avg_pixels_tab; | |
| 1036 else | |
| 1037 op_pix = avg_no_rnd_pixels_tab; | |
| 1038 } | |
| 1039 if (s->mv_dir & MV_DIR_BACKWARD) { | |
| 255 | 1040 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); |
| 0 | 1041 } |
| 1042 | |
| 1043 /* add dct residue */ | |
| 1044 add_dct(s, block[0], 0, dest_y, dct_linesize); | |
| 1045 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
| 1046 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
| 1047 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
| 1048 | |
| 57 | 1049 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
| 1050 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
| 0 | 1051 } else { |
| 1052 /* dct only in intra block */ | |
| 1053 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
| 1054 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
| 1055 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
| 1056 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
| 1057 | |
| 57 | 1058 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
| 1059 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
| 0 | 1060 } |
| 1061 } | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1062 the_end: |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1063 emms_c(); |
| 0 | 1064 } |
| 1065 | |
| 1066 static void encode_picture(MpegEncContext *s, int picture_number) | |
| 1067 { | |
| 231 | 1068 int mb_x, mb_y, wrap, last_gob, pdif = 0; |
| 0 | 1069 UINT8 *ptr; |
| 1070 int i, motion_x, motion_y; | |
| 1071 | |
| 1072 s->picture_number = picture_number; | |
| 268 | 1073 |
| 1074 s->last_mc_mb_var = s->mc_mb_var; | |
| 1075 /* Reset the average MB variance */ | |
| 1076 s->avg_mb_var = 0; | |
| 1077 s->mc_mb_var = 0; | |
| 1078 | |
| 1079 /* Estimate motion for every MB */ | |
| 1080 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | |
| 1081 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
| 1082 int xy= mb_y * s->mb_width + mb_x; | |
| 1083 s->mb_x = mb_x; | |
| 1084 s->mb_y = mb_y; | |
| 1085 | |
| 1086 /* compute motion vector and macro block type (intra or non intra) */ | |
| 1087 motion_x = 0; | |
| 1088 motion_y = 0; | |
| 1089 if (s->pict_type == P_TYPE) { | |
| 1090 s->mb_intra = estimate_motion(s, mb_x, mb_y, | |
| 1091 &motion_x, | |
| 1092 &motion_y); | |
| 1093 } else { | |
| 1094 s->mb_intra = 1; | |
| 1095 } | |
| 1096 /* Store MB type and MV */ | |
| 1097 s->mb_type[xy] = s->mb_intra; | |
| 1098 s->mv_table[0][xy] = motion_x; | |
| 1099 s->mv_table[1][xy] = motion_y; | |
| 1100 } | |
| 1101 } | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1102 emms_c(); |
| 268 | 1103 |
| 271 | 1104 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE){ //FIXME subtract MV bits |
| 1105 int i; | |
| 1106 s->pict_type= I_TYPE; | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1107 s->picture_in_gop_number=0; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1108 for(i=0; i<s->mb_num; i++){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1109 s->mb_type[i] = 1; |
| 271 | 1110 s->mv_table[0][i] = 0; |
| 1111 s->mv_table[1][i] = 0; | |
| 1112 } | |
| 1113 } | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1114 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1115 /* find best f_code */ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1116 if(s->pict_type==P_TYPE){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1117 int mv_num[8]; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1118 int i; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1119 int loose=0; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1120 UINT8 * fcode_tab= s->fcode_tab; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1121 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1122 for(i=0; i<8; i++) mv_num[i]=0; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1123 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1124 for(i=0; i<s->mb_num; i++){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1125 if(s->mb_type[i] == 0){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1126 mv_num[ fcode_tab[s->mv_table[0][i] + MAX_MV] ]++; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1127 mv_num[ fcode_tab[s->mv_table[1][i] + MAX_MV] ]++; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1128 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i); |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1129 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1130 //else printf("I"); |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1131 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1132 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1133 for(i=MAX_FCODE; i>1; i--){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1134 loose+= mv_num[i]; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1135 if(loose > 4) break; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1136 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1137 s->f_code= i; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1138 }else{ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1139 s->f_code= 1; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1140 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1141 //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
|
1142 /* convert MBs with too long MVs to I-Blocks */ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1143 if(s->pict_type==P_TYPE){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1144 int i; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1145 const int f_code= s->f_code; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1146 UINT8 * fcode_tab= s->fcode_tab; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1147 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1148 for(i=0; i<s->mb_num; i++){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1149 if(s->mb_type[i] == 0){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1150 if( fcode_tab[s->mv_table[0][i] + MAX_MV] > f_code |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1151 || fcode_tab[s->mv_table[0][i] + MAX_MV] == 0 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1152 || fcode_tab[s->mv_table[1][i] + MAX_MV] > f_code |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1153 || fcode_tab[s->mv_table[1][i] + MAX_MV] == 0 ){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1154 s->mb_type[i] = 1; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1155 s->mv_table[0][i] = 0; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1156 s->mv_table[1][i] = 0; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1157 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1158 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1159 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1160 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1161 |
| 271 | 1162 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); |
| 1163 | |
| 0 | 1164 if (!s->fixed_qscale) |
| 1165 s->qscale = rate_estimate_qscale(s); | |
| 1166 | |
| 1167 /* precompute matrix */ | |
| 1168 if (s->out_format == FMT_MJPEG) { | |
| 1169 /* for mjpeg, we do include qscale in the matrix */ | |
| 1170 s->intra_matrix[0] = default_intra_matrix[0]; | |
| 1171 for(i=1;i<64;i++) | |
| 1172 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3; | |
| 220 | 1173 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8); |
| 0 | 1174 } else { |
| 220 | 1175 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale); |
| 1176 convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale); | |
| 0 | 1177 } |
| 1178 | |
| 1179 switch(s->out_format) { | |
| 1180 case FMT_MJPEG: | |
| 1181 mjpeg_picture_header(s); | |
| 1182 break; | |
| 1183 case FMT_H263: | |
| 1184 if (s->h263_msmpeg4) | |
| 1185 msmpeg4_encode_picture_header(s, picture_number); | |
| 1186 else if (s->h263_pred) | |
| 1187 mpeg4_encode_picture_header(s, picture_number); | |
| 1188 else if (s->h263_rv10) | |
| 1189 rv10_encode_picture_header(s, picture_number); | |
| 1190 else | |
| 1191 h263_encode_picture_header(s, picture_number); | |
| 1192 break; | |
| 1193 case FMT_MPEG1: | |
| 1194 mpeg1_encode_picture_header(s, picture_number); | |
| 1195 break; | |
| 1196 } | |
| 1197 | |
| 1198 /* init last dc values */ | |
| 1199 /* note: quant matrix value (8) is implied here */ | |
| 1200 s->last_dc[0] = 128; | |
| 1201 s->last_dc[1] = 128; | |
| 1202 s->last_dc[2] = 128; | |
| 1203 s->mb_incr = 1; | |
| 1204 s->last_mv[0][0][0] = 0; | |
| 1205 s->last_mv[0][0][1] = 0; | |
| 1206 s->mv_type = MV_TYPE_16X16; | |
| 1207 s->mv_dir = MV_DIR_FORWARD; | |
| 1208 | |
| 162 | 1209 /* Get the GOB height based on picture height */ |
| 231 | 1210 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) { |
| 162 | 1211 if (s->height <= 400) |
| 1212 s->gob_index = 1; | |
| 1213 else if (s->height <= 800) | |
| 1214 s->gob_index = 2; | |
| 1215 else | |
| 1216 s->gob_index = 4; | |
| 1217 } | |
| 268 | 1218 |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1219 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
|
1220 |
| 266 | 1221 s->block_wrap[0]= |
| 1222 s->block_wrap[1]= | |
| 1223 s->block_wrap[2]= | |
| 1224 s->block_wrap[3]= s->mb_width*2 + 2; | |
| 1225 s->block_wrap[4]= | |
| 1226 s->block_wrap[5]= s->mb_width + 2; | |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1227 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
|
1228 /* 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
|
1229 /* 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
|
1230 if (s->rtp_mode) { |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1231 if (!mb_y) { |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1232 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
|
1233 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
|
1234 } 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
|
1235 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
|
1236 if (last_gob) { |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1237 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
|
1238 } |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1239 } |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1240 } |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1241 |
| 266 | 1242 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
| 1243 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
| 1244 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
| 1245 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
| 1246 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
| 1247 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
|
1248 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
| 0 | 1249 |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1250 s->mb_x = mb_x; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1251 s->mb_y = mb_y; |
| 266 | 1252 s->block_index[0]+=2; |
| 1253 s->block_index[1]+=2; | |
| 1254 s->block_index[2]+=2; | |
| 1255 s->block_index[3]+=2; | |
| 1256 s->block_index[4]++; | |
| 1257 s->block_index[5]++; | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1258 #if 0 |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1259 /* compute motion vector and macro block type (intra or non intra) */ |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1260 motion_x = 0; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1261 motion_y = 0; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1262 if (s->pict_type == P_TYPE) { |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1263 s->mb_intra = estimate_motion(s, mb_x, mb_y, |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1264 &motion_x, |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1265 &motion_y); |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1266 } else { |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1267 s->mb_intra = 1; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1268 } |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1269 #endif |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1270 |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1271 s->mb_intra = s->mb_type[mb_y * s->mb_width + mb_x]; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1272 motion_x = s->mv_table[0][mb_y * s->mb_width + mb_x]; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1273 motion_y = s->mv_table[1][mb_y * s->mb_width + mb_x]; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1274 |
| 0 | 1275 /* get the pixels */ |
| 1276 wrap = s->linesize; | |
| 1277 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; | |
|
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
|
1278 get_pixels(s->block[0], ptr, wrap); |
|
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
|
1279 get_pixels(s->block[1], ptr + 8, wrap); |
|
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
|
1280 get_pixels(s->block[2], ptr + 8 * wrap, wrap); |
|
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
|
1281 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); |
| 0 | 1282 wrap = s->linesize >> 1; |
| 1283 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8; | |
|
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
|
1284 get_pixels(s->block[4], ptr, wrap); |
| 0 | 1285 |
| 1286 wrap = s->linesize >> 1; | |
| 1287 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8; | |
|
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
|
1288 get_pixels(s->block[5], ptr, wrap); |
| 0 | 1289 |
| 1290 /* subtract previous frame if non intra */ | |
| 1291 if (!s->mb_intra) { | |
| 1292 int dxy, offset, mx, my; | |
| 1293 | |
| 1294 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
| 1295 ptr = s->last_picture[0] + | |
| 1296 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + | |
| 1297 (mb_x * 16 + (motion_x >> 1)); | |
| 1298 | |
|
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
|
1299 sub_pixels_2(s->block[0], ptr, s->linesize, dxy); |
|
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
|
1300 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy); |
|
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
|
1301 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy); |
|
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
|
1302 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy); |
| 0 | 1303 |
| 1304 if (s->out_format == FMT_H263) { | |
| 1305 /* special rounding for h263 */ | |
| 1306 dxy = 0; | |
| 1307 if ((motion_x & 3) != 0) | |
| 1308 dxy |= 1; | |
| 1309 if ((motion_y & 3) != 0) | |
| 1310 dxy |= 2; | |
| 1311 mx = motion_x >> 2; | |
| 1312 my = motion_y >> 2; | |
| 1313 } else { | |
| 1314 mx = motion_x / 2; | |
| 1315 my = motion_y / 2; | |
| 1316 dxy = ((my & 1) << 1) | (mx & 1); | |
| 1317 mx >>= 1; | |
| 1318 my >>= 1; | |
| 1319 } | |
| 1320 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx); | |
| 1321 ptr = s->last_picture[1] + offset; | |
|
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
|
1322 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy); |
| 0 | 1323 ptr = s->last_picture[2] + offset; |
|
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
|
1324 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy); |
| 0 | 1325 } |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1326 emms_c(); |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1327 |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1328 #if 0 |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1329 { |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1330 float adap_parm; |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1331 |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1332 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) / |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1333 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0); |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1334 |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1335 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1336 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1337 s->qscale, adap_parm, s->qscale*adap_parm, |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1338 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1339 } |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1340 #endif |
| 0 | 1341 /* DCT & quantize */ |
| 1342 if (s->h263_msmpeg4) { | |
| 1343 msmpeg4_dc_scale(s); | |
| 1344 } else if (s->h263_pred) { | |
| 1345 h263_dc_scale(s); | |
| 1346 } else { | |
| 1347 /* default quantization values */ | |
| 1348 s->y_dc_scale = 8; | |
| 1349 s->c_dc_scale = 8; | |
| 1350 } | |
| 1351 for(i=0;i<6;i++) { | |
| 220 | 1352 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale); |
| 0 | 1353 } |
| 1354 | |
| 1355 /* huffman encode */ | |
| 1356 switch(s->out_format) { | |
| 1357 case FMT_MPEG1: | |
|
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
|
1358 mpeg1_encode_mb(s, s->block, motion_x, motion_y); |
| 0 | 1359 break; |
| 1360 case FMT_H263: | |
| 1361 if (s->h263_msmpeg4) | |
|
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
|
1362 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); |
|
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
1363 else if(s->h263_pred) |
|
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
1364 mpeg4_encode_mb(s, s->block, motion_x, motion_y); |
| 0 | 1365 else |
|
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
|
1366 h263_encode_mb(s, s->block, motion_x, motion_y); |
| 0 | 1367 break; |
| 1368 case FMT_MJPEG: | |
|
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
|
1369 mjpeg_encode_mb(s, s->block); |
| 0 | 1370 break; |
| 1371 } | |
| 1372 | |
| 1373 /* decompress blocks so that we keep the state of the decoder */ | |
| 1374 s->mv[0][0][0] = motion_x; | |
| 1375 s->mv[0][0][1] = motion_y; | |
| 1376 | |
|
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
|
1377 MPV_decode_mb(s, s->block); |
| 0 | 1378 } |
|
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
|
1379 |
|
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
|
1380 |
| 231 | 1381 /* Obtain average GOB size for RTP */ |
| 1382 if (s->rtp_mode) { | |
| 1383 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
|
1384 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line; |
| 231 | 1385 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
|
1386 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
|
1387 s->ptr_last_mb_line = pbBufPtr(&s->pb); |
| 231 | 1388 } |
| 1389 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, | |
| 1390 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); | |
| 1391 s->first_gob_line = 0; | |
| 1392 } | |
| 0 | 1393 } |
| 208 | 1394 |
|
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
249
diff
changeset
|
1395 if (s->h263_msmpeg4 && s->pict_type == I_TYPE) |
| 208 | 1396 msmpeg4_encode_ext_header(s); |
| 1397 | |
| 162 | 1398 //if (s->gob_number) |
| 1399 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | |
| 231 | 1400 |
| 1401 /* Send the last GOB if RTP */ | |
| 1402 if (s->rtp_mode) { | |
| 1403 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
|
1404 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
| 231 | 1405 /* Call the RTP callback to send the last GOB */ |
| 1406 if (s->rtp_callback) | |
| 1407 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
|
1408 s->ptr_lastgob = pbBufPtr(&s->pb); |
| 231 | 1409 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif); |
| 1410 } | |
| 1411 | |
| 0 | 1412 } |
| 1413 | |
| 220 | 1414 static int dct_quantize_c(MpegEncContext *s, |
| 0 | 1415 DCTELEM *block, int n, |
| 1416 int qscale) | |
| 1417 { | |
| 1418 int i, j, level, last_non_zero, q; | |
| 1419 const int *qmat; | |
| 216 | 1420 int minLevel, maxLevel; |
| 1421 | |
| 1422 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){ | |
| 1423 /* mpeg4 */ | |
| 1424 minLevel= -2048; | |
| 1425 maxLevel= 2047; | |
| 1426 }else if(s->out_format==FMT_MPEG1){ | |
| 1427 /* mpeg1 */ | |
| 1428 minLevel= -255; | |
| 1429 maxLevel= 255; | |
| 230 | 1430 }else if(s->out_format==FMT_MJPEG){ |
| 1431 /* (m)jpeg */ | |
| 1432 minLevel= -1023; | |
| 1433 maxLevel= 1023; | |
| 216 | 1434 }else{ |
| 1435 /* h263 / msmpeg4 */ | |
| 1436 minLevel= -128; | |
| 1437 maxLevel= 127; | |
| 1438 } | |
| 0 | 1439 |
| 1440 av_fdct (block); | |
| 1441 | |
| 64 | 1442 /* we need this permutation so that we correct the IDCT |
| 1443 permutation. will be moved into DCT code */ | |
| 1444 block_permute(block); | |
| 1445 | |
| 0 | 1446 if (s->mb_intra) { |
| 1447 if (n < 4) | |
| 1448 q = s->y_dc_scale; | |
| 1449 else | |
| 1450 q = s->c_dc_scale; | |
| 1451 q = q << 3; | |
| 1452 | |
| 1453 /* note: block[0] is assumed to be positive */ | |
| 1454 block[0] = (block[0] + (q >> 1)) / q; | |
| 1455 i = 1; | |
| 1456 last_non_zero = 0; | |
| 1457 if (s->out_format == FMT_H263) { | |
| 1458 qmat = s->q_non_intra_matrix; | |
| 1459 } else { | |
| 1460 qmat = s->q_intra_matrix; | |
| 1461 } | |
| 1462 } else { | |
| 1463 i = 0; | |
| 1464 last_non_zero = -1; | |
| 1465 qmat = s->q_non_intra_matrix; | |
| 1466 } | |
| 1467 | |
| 1468 for(;i<64;i++) { | |
| 1469 j = zigzag_direct[i]; | |
| 1470 level = block[j]; | |
| 1471 level = level * qmat[j]; | |
| 1472 #ifdef PARANOID | |
| 1473 { | |
| 1474 static int count = 0; | |
| 1475 int level1, level2, qmat1; | |
| 1476 double val; | |
| 1477 if (qmat == s->q_non_intra_matrix) { | |
| 1478 qmat1 = default_non_intra_matrix[j] * s->qscale; | |
| 1479 } else { | |
| 1480 qmat1 = default_intra_matrix[j] * s->qscale; | |
| 1481 } | |
| 1482 if (av_fdct != jpeg_fdct_ifast) | |
| 1483 val = ((double)block[j] * 8.0) / (double)qmat1; | |
| 1484 else | |
| 1485 val = ((double)block[j] * 8.0 * 2048.0) / | |
| 1486 ((double)qmat1 * aanscales[j]); | |
| 1487 level1 = (int)val; | |
| 1488 level2 = level / (1 << (QMAT_SHIFT - 3)); | |
| 1489 if (level1 != level2) { | |
| 1490 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", | |
| 1491 count, level2, level1, block[j], qmat1, qmat[j], | |
| 1492 val); | |
| 1493 count++; | |
| 1494 } | |
| 1495 | |
| 1496 } | |
| 1497 #endif | |
| 1498 /* XXX: slight error for the low range. Test should be equivalent to | |
| 1499 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 << | |
| 1500 (QMAT_SHIFT - 3))) | |
| 1501 */ | |
| 1502 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != | |
| 1503 level) { | |
| 1504 level = level / (1 << (QMAT_SHIFT - 3)); | |
| 1505 /* XXX: currently, this code is not optimal. the range should be: | |
| 1506 mpeg1: -255..255 | |
| 1507 mpeg2: -2048..2047 | |
| 1508 h263: -128..127 | |
| 1509 mpeg4: -2048..2047 | |
| 1510 */ | |
| 216 | 1511 if (level > maxLevel) |
| 1512 level = maxLevel; | |
| 1513 else if (level < minLevel) | |
| 1514 level = minLevel; | |
| 0 | 1515 |
| 1516 block[j] = level; | |
| 1517 last_non_zero = i; | |
| 1518 } else { | |
| 1519 block[j] = 0; | |
| 1520 } | |
| 1521 } | |
| 1522 return last_non_zero; | |
| 1523 } | |
| 1524 | |
|
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
|
1525 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
|
1526 DCTELEM *block, int n, int qscale) |
| 0 | 1527 { |
| 200 | 1528 int i, level, nCoeffs; |
| 0 | 1529 const UINT16 *quant_matrix; |
| 1530 | |
| 200 | 1531 if(s->alternate_scan) nCoeffs= 64; |
| 1532 else nCoeffs= s->block_last_index[n]+1; | |
| 1533 | |
| 0 | 1534 if (s->mb_intra) { |
| 1535 if (n < 4) | |
| 1536 block[0] = block[0] * s->y_dc_scale; | |
| 1537 else | |
| 1538 block[0] = block[0] * s->c_dc_scale; | |
| 1539 /* XXX: only mpeg1 */ | |
| 1540 quant_matrix = s->intra_matrix; | |
| 200 | 1541 for(i=1;i<nCoeffs;i++) { |
| 1542 int j= zigzag_direct[i]; | |
| 1543 level = block[j]; | |
| 0 | 1544 if (level) { |
| 1545 if (level < 0) { | |
| 1546 level = -level; | |
| 200 | 1547 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
| 0 | 1548 level = (level - 1) | 1; |
| 1549 level = -level; | |
| 1550 } else { | |
| 200 | 1551 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
| 0 | 1552 level = (level - 1) | 1; |
| 1553 } | |
| 1554 #ifdef PARANOID | |
| 1555 if (level < -2048 || level > 2047) | |
| 1556 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 1557 #endif | |
| 200 | 1558 block[j] = level; |
| 0 | 1559 } |
| 1560 } | |
| 1561 } else { | |
| 1562 i = 0; | |
| 1563 quant_matrix = s->non_intra_matrix; | |
| 217 | 1564 for(;i<nCoeffs;i++) { |
| 200 | 1565 int j= zigzag_direct[i]; |
| 1566 level = block[j]; | |
| 0 | 1567 if (level) { |
| 1568 if (level < 0) { | |
| 1569 level = -level; | |
| 1570 level = (((level << 1) + 1) * qscale * | |
| 200 | 1571 ((int) (quant_matrix[j]))) >> 4; |
| 0 | 1572 level = (level - 1) | 1; |
| 1573 level = -level; | |
| 1574 } else { | |
| 1575 level = (((level << 1) + 1) * qscale * | |
| 200 | 1576 ((int) (quant_matrix[j]))) >> 4; |
| 0 | 1577 level = (level - 1) | 1; |
| 1578 } | |
| 1579 #ifdef PARANOID | |
| 1580 if (level < -2048 || level > 2047) | |
| 1581 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 1582 #endif | |
| 200 | 1583 block[j] = level; |
| 0 | 1584 } |
| 1585 } | |
| 1586 } | |
| 1587 } | |
|
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
|
1588 |
|
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
|
1589 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
|
1590 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
|
1591 { |
|
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
|
1592 int i, level, qmul, qadd; |
| 200 | 1593 int nCoeffs; |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1594 |
|
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
|
1595 if (s->mb_intra) { |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1596 if (!s->h263_aic) { |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1597 if (n < 4) |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1598 block[0] = block[0] * s->y_dc_scale; |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1599 else |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1600 block[0] = block[0] * s->c_dc_scale; |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1601 } |
|
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
|
1602 i = 1; |
| 200 | 1603 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
|
1604 } 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
|
1605 i = 0; |
| 200 | 1606 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
|
1607 } |
|
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
|
1608 |
|
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
|
1609 qmul = s->qscale << 1; |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1610 if (s->h263_aic && s->mb_intra) |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1611 qadd = 0; |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1612 else |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1613 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
|
1614 |
| 200 | 1615 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
|
1616 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
|
1617 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
|
1618 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
|
1619 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
|
1620 } 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
|
1621 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
|
1622 } |
|
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
|
1623 #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
|
1624 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
|
1625 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
|
1626 #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
|
1627 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
|
1628 } |
|
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
|
1629 } |
|
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
|
1630 } |
| 0 | 1631 |
| 1632 /* rate control */ | |
| 1633 | |
| 1634 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */ | |
| 1635 #define I_FRAME_SIZE_RATIO 3.0 | |
| 1636 #define QSCALE_K 20 | |
| 1637 | |
| 1638 static void rate_control_init(MpegEncContext *s) | |
| 1639 { | |
| 268 | 1640 #if 1 |
| 1641 emms_c(); | |
| 1642 | |
| 1643 //initial values, they dont really matter as they will be totally different within a few frames | |
| 1644 s->i_pred.coeff= s->p_pred.coeff= 7.0; | |
| 1645 s->i_pred.count= s->p_pred.count= 1.0; | |
| 1646 | |
| 1647 s->i_pred.decay= s->p_pred.decay= 0.4; | |
| 1648 | |
| 1649 // use more bits at the beginning, otherwise high motion at the begin will look like shit | |
| 1650 s->qsum=100; | |
| 1651 s->qcount=100; | |
| 1652 | |
| 1653 s->short_term_qsum=0.001; | |
| 1654 s->short_term_qcount=0.001; | |
| 1655 #else | |
| 0 | 1656 s->wanted_bits = 0; |
| 1657 | |
| 1658 if (s->intra_only) { | |
| 1659 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate; | |
| 1660 s->P_frame_bits = s->I_frame_bits; | |
| 1661 } else { | |
| 1662 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / | |
| 1663 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1))); | |
| 1664 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO); | |
| 1665 } | |
| 268 | 1666 |
| 0 | 1667 #if defined(DEBUG) |
| 1668 printf("I_frame_size=%d P_frame_size=%d\n", | |
| 1669 s->I_frame_bits, s->P_frame_bits); | |
| 1670 #endif | |
| 268 | 1671 #endif |
| 1672 } | |
| 1673 | |
| 1674 static double predict(Predictor *p, double q, double var) | |
| 1675 { | |
| 1676 return p->coeff*var / (q*p->count); | |
| 1677 } | |
| 1678 | |
| 1679 static void update_predictor(Predictor *p, double q, double var, double size) | |
| 1680 { | |
| 1681 double new_coeff= size*q / (var + 1); | |
| 1682 if(var<1000) return; | |
| 1683 /*{ | |
| 1684 int pred= predict(p, q, var); | |
| 1685 int error= abs(pred-size); | |
| 1686 static double sum=0; | |
| 1687 static int count=0; | |
| 1688 if(count>5) sum+=error; | |
| 1689 count++; | |
| 1690 if(256*256*256*64%count==0){ | |
| 1691 printf("%d %f %f\n", count, sum/count, p->coeff); | |
| 1692 } | |
| 1693 }*/ | |
| 1694 p->count*= p->decay; | |
| 1695 p->coeff*= p->decay; | |
| 1696 p->count++; | |
| 1697 p->coeff+= new_coeff; | |
| 0 | 1698 } |
| 1699 | |
| 1700 static int rate_estimate_qscale(MpegEncContext *s) | |
| 1701 { | |
| 268 | 1702 #if 1 |
| 1703 int qmin= s->qmin; | |
| 1704 int qmax= s->qmax; | |
| 1705 int rate_q=5; | |
| 1706 float q; | |
| 1707 int qscale; | |
| 1708 float br_compensation; | |
| 1709 double diff; | |
| 1710 double short_term_q; | |
| 1711 double long_term_q; | |
| 1712 int last_qscale= s->qscale; | |
| 1713 double fps; | |
| 1714 INT64 wanted_bits; | |
| 1715 emms_c(); | |
| 1716 | |
| 1717 fps= (double)s->frame_rate / FRAME_RATE_BASE; | |
| 1718 wanted_bits= s->bit_rate*(double)s->picture_number/fps; | |
| 1719 | |
| 1720 | |
| 1721 if(s->picture_number>2){ | |
| 1722 /* update predictors */ | |
| 1723 if(s->last_pict_type == I_TYPE){ | |
| 1724 //FIXME | |
| 1725 }else{ //P Frame | |
| 1726 //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff); | |
| 1727 update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits); | |
| 1728 } | |
| 1729 } | |
| 1730 | |
| 1731 if(s->pict_type == I_TYPE){ | |
| 1732 //FIXME | |
| 1733 rate_q= s->qsum/s->qcount; | |
| 1734 }else{ //P Frame | |
| 1735 int i; | |
| 1736 int diff, best_diff=1000000000; | |
| 1737 for(i=1; i<=31; i++){ | |
| 1738 diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps; | |
| 1739 if(diff<0) diff= -diff; | |
| 1740 if(diff<best_diff){ | |
| 1741 best_diff= diff; | |
| 1742 rate_q= i; | |
| 1743 } | |
| 1744 } | |
| 1745 } | |
| 1746 | |
| 1747 s->short_term_qsum*=s->qblur; | |
| 1748 s->short_term_qcount*=s->qblur; | |
| 1749 | |
| 1750 s->short_term_qsum+= rate_q; | |
| 1751 s->short_term_qcount++; | |
| 1752 short_term_q= s->short_term_qsum/s->short_term_qcount; | |
| 1753 | |
| 1754 long_term_q= s->qsum/s->qcount*s->total_bits/wanted_bits; | |
| 1755 | |
| 1756 // q= (long_term_q - short_term_q)*s->qcompress + short_term_q; | |
| 1757 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q); | |
| 1758 | |
| 1759 diff= s->total_bits - wanted_bits; | |
| 1760 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance; | |
| 270 | 1761 if(br_compensation<=0.0) br_compensation=0.001; |
| 268 | 1762 q/=br_compensation; |
| 1763 | |
| 1764 qscale= (int)(q + 0.5); | |
| 1765 if (qscale<qmin) qscale=qmin; | |
| 1766 else if(qscale>qmax) qscale=qmax; | |
| 1767 | |
| 1768 if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff; | |
| 1769 else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff; | |
| 1770 | |
| 1771 s->qsum+= qscale; | |
| 1772 s->qcount++; | |
| 1773 | |
| 1774 s->last_pict_type= s->pict_type; | |
| 1775 //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%d fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, | |
| 1776 // rate_q, (int)short_term_q, s->mc_mb_var, s->frame_bits); | |
| 1777 //printf("%d %d\n", s->bit_rate, (int)fps); | |
| 1778 return qscale; | |
| 1779 #else | |
|
187
3f3b14d3a23d
qscale estimate fix, diff extended to 64bit wide. patch by Stephen Davies <steve@daviesfam.org>
arpi_esp
parents:
178
diff
changeset
|
1780 INT64 diff, total_bits = s->total_bits; |
| 0 | 1781 float q; |
| 268 | 1782 int qscale; |
| 0 | 1783 if (s->pict_type == I_TYPE) { |
| 1784 s->wanted_bits += s->I_frame_bits; | |
| 1785 } else { | |
| 1786 s->wanted_bits += s->P_frame_bits; | |
| 1787 } | |
| 1788 diff = s->wanted_bits - total_bits; | |
| 1789 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width); | |
| 1790 /* adjust for I frame */ | |
| 1791 if (s->pict_type == I_TYPE && !s->intra_only) { | |
| 1792 q /= I_FRAME_SIZE_RATIO; | |
| 1793 } | |
| 1794 | |
| 1795 /* using a too small Q scale leeds to problems in mpeg1 and h263 | |
| 1796 because AC coefficients are clamped to 255 or 127 */ | |
| 1797 qmin = 3; | |
| 1798 if (q < qmin) | |
| 1799 q = qmin; | |
| 1800 else if (q > 31) | |
| 1801 q = 31; | |
| 1802 qscale = (int)(q + 0.5); | |
| 1803 #if defined(DEBUG) | |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1804 printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n", |
| 0 | 1805 s->picture_number, |
| 64 | 1806 (double)total_bits, |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1807 (double)s->wanted_bits, |
| 0 | 1808 (float)s->frame_rate / FRAME_RATE_BASE * |
| 1809 total_bits / s->picture_number, | |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1810 (int)diff, q); |
| 0 | 1811 #endif |
| 1812 return qscale; | |
| 268 | 1813 #endif |
| 0 | 1814 } |
| 1815 | |
| 1816 AVCodec mpeg1video_encoder = { | |
| 1817 "mpeg1video", | |
| 1818 CODEC_TYPE_VIDEO, | |
| 1819 CODEC_ID_MPEG1VIDEO, | |
| 1820 sizeof(MpegEncContext), | |
| 1821 MPV_encode_init, | |
| 1822 MPV_encode_picture, | |
| 1823 MPV_encode_end, | |
| 1824 }; | |
| 1825 | |
| 1826 AVCodec h263_encoder = { | |
| 1827 "h263", | |
| 1828 CODEC_TYPE_VIDEO, | |
| 1829 CODEC_ID_H263, | |
| 1830 sizeof(MpegEncContext), | |
| 1831 MPV_encode_init, | |
| 1832 MPV_encode_picture, | |
| 1833 MPV_encode_end, | |
| 1834 }; | |
| 1835 | |
| 1836 AVCodec h263p_encoder = { | |
| 1837 "h263p", | |
| 1838 CODEC_TYPE_VIDEO, | |
| 1839 CODEC_ID_H263P, | |
| 1840 sizeof(MpegEncContext), | |
| 1841 MPV_encode_init, | |
| 1842 MPV_encode_picture, | |
| 1843 MPV_encode_end, | |
| 1844 }; | |
| 1845 | |
| 1846 AVCodec rv10_encoder = { | |
| 1847 "rv10", | |
| 1848 CODEC_TYPE_VIDEO, | |
| 1849 CODEC_ID_RV10, | |
| 1850 sizeof(MpegEncContext), | |
| 1851 MPV_encode_init, | |
| 1852 MPV_encode_picture, | |
| 1853 MPV_encode_end, | |
| 1854 }; | |
| 1855 | |
| 1856 AVCodec mjpeg_encoder = { | |
| 1857 "mjpeg", | |
| 1858 CODEC_TYPE_VIDEO, | |
| 1859 CODEC_ID_MJPEG, | |
| 1860 sizeof(MpegEncContext), | |
| 1861 MPV_encode_init, | |
| 1862 MPV_encode_picture, | |
| 1863 MPV_encode_end, | |
| 1864 }; | |
| 1865 | |
| 71 | 1866 AVCodec mpeg4_encoder = { |
| 1867 "mpeg4", | |
| 0 | 1868 CODEC_TYPE_VIDEO, |
| 71 | 1869 CODEC_ID_MPEG4, |
| 0 | 1870 sizeof(MpegEncContext), |
| 1871 MPV_encode_init, | |
| 1872 MPV_encode_picture, | |
| 1873 MPV_encode_end, | |
| 1874 }; | |
| 1875 | |
| 1876 AVCodec msmpeg4_encoder = { | |
| 1877 "msmpeg4", | |
| 1878 CODEC_TYPE_VIDEO, | |
| 1879 CODEC_ID_MSMPEG4, | |
| 1880 sizeof(MpegEncContext), | |
| 1881 MPV_encode_init, | |
| 1882 MPV_encode_picture, | |
| 1883 MPV_encode_end, | |
| 1884 }; |
