Mercurial > libavcodec.hg
annotate mpegvideo.c @ 170:dfdac3e25dcd libavcodec
runtime cpu detection
| author | michael |
|---|---|
| date | Mon, 26 Nov 2001 21:17:23 +0000 |
| parents | de80712db90b |
| children | 9ce215ee9216 |
| 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); |
|
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
38 static int dct_quantize(MpegEncContext *s, 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
|
39 static int dct_quantize_mmx(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
|
40 DCTELEM *block, int n, |
|
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
41 int qscale); |
| 0 | 42 #define EDGE_WIDTH 16 |
| 43 | |
| 44 /* enable all paranoid tests for rounding, overflows, etc... */ | |
| 45 //#define PARANOID | |
| 46 | |
| 47 //#define DEBUG | |
| 48 | |
| 49 /* for jpeg fast DCT */ | |
| 50 #define CONST_BITS 14 | |
| 51 | |
| 52 static const unsigned short aanscales[64] = { | |
| 53 /* precomputed values scaled up by 14 bits */ | |
| 54 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
| 55 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
| 56 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
| 57 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
| 58 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
| 59 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
| 60 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
| 61 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
| 62 }; | |
| 63 | |
| 64 static UINT8 h263_chroma_roundtab[16] = { | |
| 65 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | |
| 66 }; | |
| 67 | |
| 68 /* default motion estimation */ | |
| 69 int motion_estimation_method = ME_LOG; | |
| 70 | |
| 71 /* XXX: should use variable shift ? */ | |
| 72 #define QMAT_SHIFT_MMX 19 | |
| 73 #define QMAT_SHIFT 25 | |
| 74 | |
| 75 static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale) | |
| 76 { | |
| 77 int i; | |
| 78 | |
| 79 if (av_fdct == jpeg_fdct_ifast) { | |
| 80 for(i=0;i<64;i++) { | |
| 81 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
| 82 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ | |
| 83 | |
| 64 | 84 qmat[i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / |
| 85 (aanscales[i] * qscale * quant_matrix[i])); | |
| 0 | 86 } |
| 87 } else { | |
| 88 for(i=0;i<64;i++) { | |
| 89 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
| 90 So 16 <= qscale * quant_matrix[i] <= 7905 | |
| 91 so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905 | |
| 92 */ | |
| 93 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 /* init common structure for both encoder and decoder */ | |
| 99 int MPV_common_init(MpegEncContext *s) | |
| 100 { | |
| 101 int c_size, i; | |
| 102 UINT8 *pict; | |
| 103 | |
|
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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 |
|
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
|
109 #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
|
110 MPV_common_init_mmx(s); |
| 8 | 111 #endif |
| 0 | 112 s->mb_width = (s->width + 15) / 16; |
| 113 s->mb_height = (s->height + 15) / 16; | |
| 114 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH; | |
| 115 | |
| 116 for(i=0;i<3;i++) { | |
| 117 int w, h, shift, pict_start; | |
| 118 | |
| 119 w = s->linesize; | |
| 120 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | |
| 121 shift = (i == 0) ? 0 : 1; | |
| 122 c_size = (w >> shift) * (h >> shift); | |
| 123 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | |
| 124 | |
| 125 pict = av_mallocz(c_size); | |
| 126 if (pict == NULL) | |
| 127 goto fail; | |
| 128 s->last_picture_base[i] = pict; | |
| 129 s->last_picture[i] = pict + pict_start; | |
| 130 | |
| 131 pict = av_mallocz(c_size); | |
| 132 if (pict == NULL) | |
| 133 goto fail; | |
| 134 s->next_picture_base[i] = pict; | |
| 135 s->next_picture[i] = pict + pict_start; | |
| 136 | |
| 137 if (s->has_b_frames) { | |
| 138 pict = av_mallocz(c_size); | |
| 139 if (pict == NULL) | |
| 140 goto fail; | |
| 141 s->aux_picture_base[i] = pict; | |
| 142 s->aux_picture[i] = pict + pict_start; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 if (s->out_format == FMT_H263) { | |
| 147 int size; | |
| 148 /* MV prediction */ | |
| 149 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
| 150 s->motion_val = malloc(size * 2 * sizeof(INT16)); | |
| 151 if (s->motion_val == NULL) | |
| 152 goto fail; | |
| 153 memset(s->motion_val, 0, size * 2 * sizeof(INT16)); | |
| 154 } | |
| 155 | |
| 156 if (s->h263_pred) { | |
| 157 int y_size, c_size, i, size; | |
| 158 | |
| 159 /* dc values */ | |
| 160 | |
| 161 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
| 162 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
| 163 size = y_size + 2 * c_size; | |
| 164 s->dc_val[0] = malloc(size * sizeof(INT16)); | |
| 165 if (s->dc_val[0] == NULL) | |
| 166 goto fail; | |
| 167 s->dc_val[1] = s->dc_val[0] + y_size; | |
| 168 s->dc_val[2] = s->dc_val[1] + c_size; | |
| 169 for(i=0;i<size;i++) | |
| 170 s->dc_val[0][i] = 1024; | |
| 171 | |
| 172 /* ac values */ | |
| 173 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16); | |
| 174 if (s->ac_val[0] == NULL) | |
| 175 goto fail; | |
| 176 s->ac_val[1] = s->ac_val[0] + y_size; | |
| 177 s->ac_val[2] = s->ac_val[1] + c_size; | |
| 178 | |
| 179 /* cbp values */ | |
| 180 s->coded_block = av_mallocz(y_size); | |
| 181 if (!s->coded_block) | |
| 182 goto fail; | |
| 183 } | |
| 184 /* default structure is frame */ | |
| 185 s->picture_structure = PICT_FRAME; | |
| 186 | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
187 /* init macroblock skip table */ |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
188 if (!s->encoding) { |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
189 s->mbskip_table = av_mallocz(s->mb_width * s->mb_height); |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
190 if (!s->mbskip_table) |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
191 goto fail; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
192 } |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
193 |
| 0 | 194 s->context_initialized = 1; |
| 195 return 0; | |
| 196 fail: | |
| 197 if (s->motion_val) | |
| 198 free(s->motion_val); | |
| 199 if (s->dc_val[0]) | |
| 200 free(s->dc_val[0]); | |
| 201 if (s->ac_val[0]) | |
| 202 free(s->ac_val[0]); | |
| 203 if (s->coded_block) | |
| 204 free(s->coded_block); | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
205 if (s->mbskip_table) |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
206 free(s->mbskip_table); |
| 0 | 207 for(i=0;i<3;i++) { |
| 208 if (s->last_picture_base[i]) | |
| 209 free(s->last_picture_base[i]); | |
| 210 if (s->next_picture_base[i]) | |
| 211 free(s->next_picture_base[i]); | |
| 212 if (s->aux_picture_base[i]) | |
| 213 free(s->aux_picture_base[i]); | |
| 214 } | |
| 215 return -1; | |
| 216 } | |
| 217 | |
| 218 /* init common structure for both encoder and decoder */ | |
| 219 void MPV_common_end(MpegEncContext *s) | |
| 220 { | |
| 221 int i; | |
| 222 | |
| 223 if (s->motion_val) | |
| 224 free(s->motion_val); | |
| 225 if (s->h263_pred) { | |
| 226 free(s->dc_val[0]); | |
| 227 free(s->ac_val[0]); | |
| 228 free(s->coded_block); | |
| 229 } | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
230 if (s->mbskip_table) |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
231 free(s->mbskip_table); |
| 0 | 232 for(i=0;i<3;i++) { |
| 233 free(s->last_picture_base[i]); | |
| 234 free(s->next_picture_base[i]); | |
| 235 if (s->has_b_frames) | |
| 236 free(s->aux_picture_base[i]); | |
| 237 } | |
| 238 s->context_initialized = 0; | |
| 239 } | |
| 240 | |
| 241 /* init video encoder */ | |
| 242 int MPV_encode_init(AVCodecContext *avctx) | |
| 243 { | |
| 244 MpegEncContext *s = avctx->priv_data; | |
| 60 | 245 int i; |
| 0 | 246 |
| 247 s->bit_rate = avctx->bit_rate; | |
| 248 s->frame_rate = avctx->frame_rate; | |
| 249 s->width = avctx->width; | |
| 250 s->height = avctx->height; | |
| 251 s->gop_size = avctx->gop_size; | |
| 162 | 252 s->rtp_mode = avctx->rtp_mode; |
| 253 s->rtp_payload_size = avctx->rtp_payload_size; | |
| 254 | |
| 0 | 255 if (s->gop_size <= 1) { |
| 256 s->intra_only = 1; | |
| 257 s->gop_size = 12; | |
| 258 } else { | |
| 259 s->intra_only = 0; | |
| 260 } | |
| 261 s->full_search = motion_estimation_method; | |
| 262 | |
| 263 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE); | |
| 264 | |
| 265 switch(avctx->codec->id) { | |
| 266 case CODEC_ID_MPEG1VIDEO: | |
| 267 s->out_format = FMT_MPEG1; | |
| 268 break; | |
| 269 case CODEC_ID_MJPEG: | |
| 270 s->out_format = FMT_MJPEG; | |
| 271 s->intra_only = 1; /* force intra only for jpeg */ | |
| 272 if (mjpeg_init(s) < 0) | |
| 273 return -1; | |
| 274 break; | |
| 275 case CODEC_ID_H263: | |
| 276 if (h263_get_picture_format(s->width, s->height) == 7) | |
| 277 return -1; | |
| 278 s->out_format = FMT_H263; | |
| 279 break; | |
| 280 case CODEC_ID_H263P: | |
| 281 s->out_format = FMT_H263; | |
| 162 | 282 s->rtp_mode = 1; |
| 283 s->rtp_payload_size = 1200; | |
| 0 | 284 s->h263_plus = 1; |
| 78 | 285 s->unrestricted_mv = 1; |
|
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
286 |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
287 /* These are just to be sure */ |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
288 s->umvplus = 0; |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
289 s->umvplus_dec = 0; |
| 0 | 290 break; |
| 291 case CODEC_ID_RV10: | |
| 292 s->out_format = FMT_H263; | |
| 293 s->h263_rv10 = 1; | |
| 294 break; | |
| 71 | 295 case CODEC_ID_MPEG4: |
| 0 | 296 s->out_format = FMT_H263; |
| 297 s->h263_pred = 1; | |
| 298 s->unrestricted_mv = 1; | |
| 299 break; | |
| 300 case CODEC_ID_MSMPEG4: | |
| 301 s->out_format = FMT_H263; | |
| 302 s->h263_msmpeg4 = 1; | |
| 303 s->h263_pred = 1; | |
| 304 s->unrestricted_mv = 1; | |
| 305 break; | |
| 306 default: | |
| 307 return -1; | |
| 308 } | |
| 309 | |
| 310 if (s->out_format == FMT_H263) | |
| 311 h263_encode_init_vlc(s); | |
| 312 | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
313 s->encoding = 1; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
314 |
| 0 | 315 /* init */ |
| 316 if (MPV_common_init(s) < 0) | |
| 317 return -1; | |
| 318 | |
| 60 | 319 /* init default q matrix */ |
| 320 for(i=0;i<64;i++) { | |
| 321 s->intra_matrix[i] = default_intra_matrix[i]; | |
| 322 s->non_intra_matrix[i] = default_non_intra_matrix[i]; | |
| 323 } | |
| 324 | |
| 0 | 325 /* rate control init */ |
| 326 rate_control_init(s); | |
| 327 | |
| 328 s->picture_number = 0; | |
| 329 s->fake_picture_number = 0; | |
| 330 /* motion detector init */ | |
| 331 s->f_code = 1; | |
| 332 | |
| 333 return 0; | |
| 334 } | |
| 335 | |
| 336 int MPV_encode_end(AVCodecContext *avctx) | |
| 337 { | |
| 338 MpegEncContext *s = avctx->priv_data; | |
| 339 | |
| 340 #ifdef STATS | |
| 341 print_stats(); | |
| 342 #endif | |
| 343 MPV_common_end(s); | |
| 344 if (s->out_format == FMT_MJPEG) | |
| 345 mjpeg_close(s); | |
| 346 return 0; | |
| 347 } | |
| 348 | |
| 349 /* draw the edges of width 'w' of an image of size width, height */ | |
| 350 static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w) | |
| 351 { | |
| 352 UINT8 *ptr, *last_line; | |
| 353 int i; | |
| 354 | |
| 355 last_line = buf + (height - 1) * wrap; | |
| 356 for(i=0;i<w;i++) { | |
| 357 /* top and bottom */ | |
| 358 memcpy(buf - (i + 1) * wrap, buf, width); | |
| 359 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
| 360 } | |
| 361 /* left and right */ | |
| 362 ptr = buf; | |
| 363 for(i=0;i<height;i++) { | |
| 364 memset(ptr - w, ptr[0], w); | |
| 365 memset(ptr + width, ptr[width-1], w); | |
| 366 ptr += wrap; | |
| 367 } | |
| 368 /* corners */ | |
| 369 for(i=0;i<w;i++) { | |
| 370 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
| 371 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
| 372 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
| 373 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 /* generic function for encode/decode called before a frame is coded/decoded */ | |
| 378 void MPV_frame_start(MpegEncContext *s) | |
| 379 { | |
| 380 int i; | |
| 381 UINT8 *tmp; | |
| 382 | |
|
46
931417475f5b
fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents:
40
diff
changeset
|
383 s->mb_skiped = 0; |
| 0 | 384 if (s->pict_type == B_TYPE) { |
| 385 for(i=0;i<3;i++) { | |
| 386 s->current_picture[i] = s->aux_picture[i]; | |
| 387 } | |
| 388 } else { | |
| 389 for(i=0;i<3;i++) { | |
| 390 /* swap next and last */ | |
| 391 tmp = s->last_picture[i]; | |
| 392 s->last_picture[i] = s->next_picture[i]; | |
| 393 s->next_picture[i] = tmp; | |
| 394 s->current_picture[i] = tmp; | |
| 395 } | |
| 396 } | |
| 397 } | |
|
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
|
398 |
| 0 | 399 /* generic function for encode/decode called after a frame has been coded/decoded */ |
| 400 void MPV_frame_end(MpegEncContext *s) | |
| 401 { | |
| 402 /* draw edge for correct motion prediction if outside */ | |
| 403 if (s->pict_type != B_TYPE) { | |
| 404 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH); | |
| 405 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
| 406 draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 int MPV_encode_picture(AVCodecContext *avctx, | |
| 411 unsigned char *buf, int buf_size, void *data) | |
| 412 { | |
| 413 MpegEncContext *s = avctx->priv_data; | |
| 414 AVPicture *pict = data; | |
| 415 int i, j; | |
| 416 | |
| 417 if (s->fixed_qscale) | |
| 418 s->qscale = avctx->quality; | |
| 419 | |
| 420 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
| 421 | |
| 422 if (!s->intra_only) { | |
| 423 /* first picture of GOP is intra */ | |
| 424 if ((s->picture_number % s->gop_size) == 0) | |
| 425 s->pict_type = I_TYPE; | |
| 426 else | |
| 427 s->pict_type = P_TYPE; | |
| 428 } else { | |
| 429 s->pict_type = I_TYPE; | |
| 430 } | |
| 431 avctx->key_frame = (s->pict_type == I_TYPE); | |
| 432 | |
| 433 MPV_frame_start(s); | |
| 434 | |
| 435 for(i=0;i<3;i++) { | |
| 436 UINT8 *src = pict->data[i]; | |
| 437 UINT8 *dest = s->current_picture[i]; | |
| 438 int src_wrap = pict->linesize[i]; | |
| 439 int dest_wrap = s->linesize; | |
| 440 int w = s->width; | |
| 441 int h = s->height; | |
| 442 | |
| 443 if (i >= 1) { | |
| 444 dest_wrap >>= 1; | |
| 445 w >>= 1; | |
| 446 h >>= 1; | |
| 447 } | |
| 448 | |
| 449 for(j=0;j<h;j++) { | |
| 450 memcpy(dest, src, w); | |
| 451 dest += dest_wrap; | |
| 452 src += src_wrap; | |
| 453 } | |
| 454 s->new_picture[i] = s->current_picture[i]; | |
| 455 } | |
| 456 | |
| 457 encode_picture(s, s->picture_number); | |
| 458 | |
| 459 MPV_frame_end(s); | |
| 460 s->picture_number++; | |
| 461 | |
| 462 if (s->out_format == FMT_MJPEG) | |
| 463 mjpeg_picture_trailer(s); | |
| 464 | |
| 465 flush_put_bits(&s->pb); | |
| 466 s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8; | |
| 467 avctx->quality = s->qscale; | |
| 468 return s->pb.buf_ptr - s->pb.buf; | |
| 469 } | |
| 470 | |
| 471 static inline int clip(int a, int amin, int amax) | |
| 472 { | |
| 473 if (a < amin) | |
| 474 return amin; | |
| 475 else if (a > amax) | |
| 476 return amax; | |
| 477 else | |
| 478 return a; | |
| 479 } | |
| 480 | |
| 481 /* apply one mpeg motion vector to the three components */ | |
| 482 static inline void mpeg_motion(MpegEncContext *s, | |
| 483 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 484 int dest_offset, | |
| 485 UINT8 **ref_picture, int src_offset, | |
| 486 int field_based, op_pixels_func *pix_op, | |
| 487 int motion_x, int motion_y, int h) | |
| 488 { | |
| 489 UINT8 *ptr; | |
| 490 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
| 491 | |
| 492 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
| 493 src_x = s->mb_x * 16 + (motion_x >> 1); | |
| 494 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
| 495 | |
| 496 /* WARNING: do no forget half pels */ | |
| 497 height = s->height >> field_based; | |
| 498 src_x = clip(src_x, -16, s->width); | |
| 499 if (src_x == s->width) | |
| 500 dxy &= ~1; | |
| 501 src_y = clip(src_y, -16, height); | |
| 502 if (src_y == height) | |
| 503 dxy &= ~2; | |
| 504 linesize = s->linesize << field_based; | |
| 505 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; | |
| 506 dest_y += dest_offset; | |
| 507 pix_op[dxy](dest_y, ptr, linesize, h); | |
| 508 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h); | |
| 509 | |
| 510 if (s->out_format == FMT_H263) { | |
| 511 dxy = 0; | |
| 512 if ((motion_x & 3) != 0) | |
| 513 dxy |= 1; | |
| 514 if ((motion_y & 3) != 0) | |
| 515 dxy |= 2; | |
| 516 mx = motion_x >> 2; | |
| 517 my = motion_y >> 2; | |
| 518 } else { | |
| 519 mx = motion_x / 2; | |
| 520 my = motion_y / 2; | |
| 521 dxy = ((my & 1) << 1) | (mx & 1); | |
| 522 mx >>= 1; | |
| 523 my >>= 1; | |
| 524 } | |
| 525 | |
| 526 src_x = s->mb_x * 8 + mx; | |
| 527 src_y = s->mb_y * (8 >> field_based) + my; | |
| 528 src_x = clip(src_x, -8, s->width >> 1); | |
| 529 if (src_x == (s->width >> 1)) | |
| 530 dxy &= ~1; | |
| 531 src_y = clip(src_y, -8, height >> 1); | |
| 532 if (src_y == (height >> 1)) | |
| 533 dxy &= ~2; | |
| 534 | |
| 535 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
| 536 ptr = ref_picture[1] + offset; | |
| 537 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
| 538 ptr = ref_picture[2] + offset; | |
| 539 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
| 540 } | |
| 541 | |
| 542 static inline void MPV_motion(MpegEncContext *s, | |
| 543 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 544 int dir, UINT8 **ref_picture, | |
| 545 op_pixels_func *pix_op) | |
| 546 { | |
| 547 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
| 548 int mb_x, mb_y, i; | |
| 549 UINT8 *ptr, *dest; | |
| 550 | |
| 551 mb_x = s->mb_x; | |
| 552 mb_y = s->mb_y; | |
| 553 | |
| 554 switch(s->mv_type) { | |
| 555 case MV_TYPE_16X16: | |
| 556 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 557 ref_picture, 0, | |
| 558 0, pix_op, | |
| 559 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
| 560 break; | |
| 561 case MV_TYPE_8X8: | |
| 562 for(i=0;i<4;i++) { | |
| 563 motion_x = s->mv[dir][i][0]; | |
| 564 motion_y = s->mv[dir][i][1]; | |
| 565 | |
| 566 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
| 567 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
| 568 src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8; | |
| 569 | |
| 570 /* WARNING: do no forget half pels */ | |
| 571 src_x = clip(src_x, -16, s->width); | |
| 572 if (src_x == s->width) | |
| 573 dxy &= ~1; | |
| 574 src_y = clip(src_y, -16, s->height); | |
| 575 if (src_y == s->height) | |
| 576 dxy &= ~2; | |
| 577 | |
| 578 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
| 579 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
| 580 pix_op[dxy](dest, ptr, s->linesize, 8); | |
| 581 } | |
| 582 /* In case of 8X8, we construct a single chroma motion vector | |
| 583 with a special rounding */ | |
| 584 mx = 0; | |
| 585 my = 0; | |
| 586 for(i=0;i<4;i++) { | |
| 587 mx += s->mv[dir][i][0]; | |
| 588 my += s->mv[dir][i][1]; | |
| 589 } | |
| 590 if (mx >= 0) | |
| 591 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
| 592 else { | |
| 593 mx = -mx; | |
| 594 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
| 595 } | |
| 596 if (my >= 0) | |
| 597 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
| 598 else { | |
| 599 my = -my; | |
| 600 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
| 601 } | |
| 602 dxy = ((my & 1) << 1) | (mx & 1); | |
| 603 mx >>= 1; | |
| 604 my >>= 1; | |
| 605 | |
| 606 src_x = mb_x * 8 + mx; | |
| 607 src_y = mb_y * 8 + my; | |
| 608 src_x = clip(src_x, -8, s->width/2); | |
| 609 if (src_x == s->width/2) | |
| 610 dxy &= ~1; | |
| 611 src_y = clip(src_y, -8, s->height/2); | |
| 612 if (src_y == s->height/2) | |
| 613 dxy &= ~2; | |
| 614 | |
| 615 offset = (src_y * (s->linesize >> 1)) + src_x; | |
| 616 ptr = ref_picture[1] + offset; | |
| 617 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8); | |
| 618 ptr = ref_picture[2] + offset; | |
| 619 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8); | |
| 620 break; | |
| 621 case MV_TYPE_FIELD: | |
| 622 if (s->picture_structure == PICT_FRAME) { | |
| 623 /* top field */ | |
| 624 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 625 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
| 626 1, pix_op, | |
| 627 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
| 628 /* bottom field */ | |
| 629 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
| 630 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
| 631 1, pix_op, | |
| 632 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
| 633 } else { | |
| 634 | |
| 635 | |
| 636 } | |
| 637 break; | |
| 638 } | |
| 639 } | |
| 640 | |
| 641 | |
| 642 /* put block[] to dest[] */ | |
| 643 static inline void put_dct(MpegEncContext *s, | |
| 644 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
| 645 { | |
| 646 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
|
647 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
|
648 ff_idct (block); |
| 0 | 649 put_pixels_clamped(block, dest, line_size); |
| 650 } | |
| 651 | |
| 652 /* add block[] to dest[] */ | |
| 653 static inline void add_dct(MpegEncContext *s, | |
| 654 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
| 655 { | |
| 656 if (s->block_last_index[i] >= 0) { | |
| 657 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
|
658 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
|
659 ff_idct (block); |
| 0 | 660 add_pixels_clamped(block, dest, line_size); |
| 661 } | |
| 662 } | |
| 663 | |
| 664 /* generic function called after a macroblock has been parsed by the | |
| 665 decoder or after it has been encoded by the encoder. | |
| 666 | |
| 667 Important variables used: | |
| 668 s->mb_intra : true if intra macroblock | |
| 669 s->mv_dir : motion vector direction | |
| 670 s->mv_type : motion vector type | |
| 671 s->mv : motion vector | |
| 672 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
| 673 */ | |
| 674 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
| 675 { | |
| 676 int mb_x, mb_y, motion_x, motion_y; | |
| 677 int dct_linesize, dct_offset; | |
| 678 op_pixels_func *op_pix; | |
| 679 | |
| 680 mb_x = s->mb_x; | |
| 681 mb_y = s->mb_y; | |
| 682 | |
|
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
683 #ifdef FF_POSTPROCESS |
|
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
684 quant_store[mb_y][mb_x]=s->qscale; |
|
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
685 //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
|
686 #endif |
|
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
687 |
| 0 | 688 /* update DC predictors for P macroblocks */ |
| 689 if (!s->mb_intra) { | |
| 690 if (s->h263_pred) { | |
| 691 int wrap, x, y, v; | |
| 692 wrap = 2 * s->mb_width + 2; | |
| 693 v = 1024; | |
| 694 x = 2 * mb_x + 1; | |
| 695 y = 2 * mb_y + 1; | |
| 696 s->dc_val[0][(x) + (y) * wrap] = v; | |
| 697 s->dc_val[0][(x + 1) + (y) * wrap] = v; | |
| 698 s->dc_val[0][(x) + (y + 1) * wrap] = v; | |
| 699 s->dc_val[0][(x + 1) + (y + 1) * wrap] = v; | |
| 700 /* ac pred */ | |
| 701 memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
| 702 memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
| 703 memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16)); | |
| 704 memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16)); | |
| 705 if (s->h263_msmpeg4) { | |
| 706 s->coded_block[(x) + (y) * wrap] = 0; | |
| 707 s->coded_block[(x + 1) + (y) * wrap] = 0; | |
| 708 s->coded_block[(x) + (y + 1) * wrap] = 0; | |
| 709 s->coded_block[(x + 1) + (y + 1) * wrap] = 0; | |
| 710 } | |
| 711 /* chroma */ | |
| 712 wrap = s->mb_width + 2; | |
| 713 x = mb_x + 1; | |
| 714 y = mb_y + 1; | |
| 715 s->dc_val[1][(x) + (y) * wrap] = v; | |
| 716 s->dc_val[2][(x) + (y) * wrap] = v; | |
| 717 /* ac pred */ | |
| 718 memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
| 719 memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16)); | |
| 720 } else { | |
| 721 s->last_dc[0] = 128 << s->intra_dc_precision; | |
| 722 s->last_dc[1] = 128 << s->intra_dc_precision; | |
| 723 s->last_dc[2] = 128 << s->intra_dc_precision; | |
| 724 } | |
| 725 } | |
| 726 | |
| 727 /* update motion predictor */ | |
| 728 if (s->out_format == FMT_H263) { | |
| 729 int x, y, wrap; | |
| 730 | |
| 731 x = 2 * mb_x + 1; | |
| 732 y = 2 * mb_y + 1; | |
| 733 wrap = 2 * s->mb_width + 2; | |
| 734 if (s->mb_intra) { | |
| 735 motion_x = 0; | |
| 736 motion_y = 0; | |
| 737 goto motion_init; | |
| 738 } else if (s->mv_type == MV_TYPE_16X16) { | |
| 739 motion_x = s->mv[0][0][0]; | |
| 740 motion_y = s->mv[0][0][1]; | |
| 741 motion_init: | |
| 742 /* no update if 8X8 because it has been done during parsing */ | |
| 743 s->motion_val[(x) + (y) * wrap][0] = motion_x; | |
| 744 s->motion_val[(x) + (y) * wrap][1] = motion_y; | |
| 745 s->motion_val[(x + 1) + (y) * wrap][0] = motion_x; | |
| 746 s->motion_val[(x + 1) + (y) * wrap][1] = motion_y; | |
| 747 s->motion_val[(x) + (y + 1) * wrap][0] = motion_x; | |
| 748 s->motion_val[(x) + (y + 1) * wrap][1] = motion_y; | |
| 749 s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x; | |
| 750 s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y; | |
| 751 } | |
| 752 } | |
| 753 | |
| 754 if (!s->intra_only) { | |
| 755 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
|
756 UINT8 *mbskip_ptr; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
757 |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
758 /* 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
|
759 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
|
760 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
|
761 if (s->mb_skiped) { |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
762 s->mb_skiped = 0; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
763 /* 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
|
764 if (*mbskip_ptr != 0) |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
765 goto the_end; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
766 *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
|
767 } else { |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
768 *mbskip_ptr = 0; /* not skipped */ |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
769 } |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
770 } |
| 0 | 771 |
| 772 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16; | |
| 773 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
| 774 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
| 775 | |
| 776 if (s->interlaced_dct) { | |
| 777 dct_linesize = s->linesize * 2; | |
| 778 dct_offset = s->linesize; | |
| 779 } else { | |
| 780 dct_linesize = s->linesize; | |
| 781 dct_offset = s->linesize * 8; | |
| 782 } | |
| 783 | |
| 784 if (!s->mb_intra) { | |
| 785 /* motion handling */ | |
| 786 if (!s->no_rounding) | |
| 787 op_pix = put_pixels_tab; | |
| 788 else | |
| 789 op_pix = put_no_rnd_pixels_tab; | |
| 790 | |
| 791 if (s->mv_dir & MV_DIR_FORWARD) { | |
| 792 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix); | |
| 793 if (!s->no_rounding) | |
| 794 op_pix = avg_pixels_tab; | |
| 795 else | |
| 796 op_pix = avg_no_rnd_pixels_tab; | |
| 797 } | |
| 798 if (s->mv_dir & MV_DIR_BACKWARD) { | |
| 799 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix); | |
| 800 } | |
| 801 | |
| 802 /* add dct residue */ | |
| 803 add_dct(s, block[0], 0, dest_y, dct_linesize); | |
| 804 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
| 805 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
| 806 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
| 807 | |
| 57 | 808 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
| 809 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
| 0 | 810 } else { |
| 811 /* dct only in intra block */ | |
| 812 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
| 813 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
| 814 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
| 815 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
| 816 | |
| 57 | 817 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
| 818 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
| 0 | 819 } |
| 820 } | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
821 the_end: |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
822 emms_c(); |
| 0 | 823 } |
| 824 | |
| 825 static void encode_picture(MpegEncContext *s, int picture_number) | |
| 826 { | |
| 162 | 827 int mb_x, mb_y, wrap, last_gob; |
| 0 | 828 UINT8 *ptr; |
| 829 int i, motion_x, motion_y; | |
| 830 | |
| 831 s->picture_number = picture_number; | |
| 832 if (!s->fixed_qscale) | |
| 833 s->qscale = rate_estimate_qscale(s); | |
| 834 | |
| 835 /* precompute matrix */ | |
| 836 if (s->out_format == FMT_MJPEG) { | |
| 837 /* for mjpeg, we do include qscale in the matrix */ | |
| 838 s->intra_matrix[0] = default_intra_matrix[0]; | |
| 839 for(i=1;i<64;i++) | |
| 840 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3; | |
| 841 convert_matrix(s->q_intra_matrix, s->intra_matrix, 8); | |
| 842 } else { | |
| 843 convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale); | |
| 844 convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale); | |
| 845 } | |
| 846 | |
| 847 switch(s->out_format) { | |
| 848 case FMT_MJPEG: | |
| 849 mjpeg_picture_header(s); | |
| 850 break; | |
| 851 case FMT_H263: | |
| 852 if (s->h263_msmpeg4) | |
| 853 msmpeg4_encode_picture_header(s, picture_number); | |
| 854 else if (s->h263_pred) | |
| 855 mpeg4_encode_picture_header(s, picture_number); | |
| 856 else if (s->h263_rv10) | |
| 857 rv10_encode_picture_header(s, picture_number); | |
| 858 else | |
| 859 h263_encode_picture_header(s, picture_number); | |
| 860 break; | |
| 861 case FMT_MPEG1: | |
| 862 mpeg1_encode_picture_header(s, picture_number); | |
| 863 break; | |
| 864 } | |
| 865 | |
| 866 /* init last dc values */ | |
| 867 /* note: quant matrix value (8) is implied here */ | |
| 868 s->last_dc[0] = 128; | |
| 869 s->last_dc[1] = 128; | |
| 870 s->last_dc[2] = 128; | |
| 871 s->mb_incr = 1; | |
| 872 s->last_mv[0][0][0] = 0; | |
| 873 s->last_mv[0][0][1] = 0; | |
| 874 s->mv_type = MV_TYPE_16X16; | |
| 875 s->mv_dir = MV_DIR_FORWARD; | |
| 876 | |
| 162 | 877 /* Get the GOB height based on picture height */ |
| 878 if (s->out_format == FMT_H263 && s->h263_plus) { | |
| 879 if (s->height <= 400) | |
| 880 s->gob_index = 1; | |
| 881 else if (s->height <= 800) | |
| 882 s->gob_index = 2; | |
| 883 else | |
| 884 s->gob_index = 4; | |
| 885 } | |
| 886 | |
| 0 | 887 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
| 162 | 888 /* Put GOB header based on RTP MTU */ |
| 889 if (!mb_y) { | |
| 890 s->ptr_lastgob = s->pb.buf_ptr; | |
| 891 s->ptr_last_mb_line = s->pb.buf_ptr; | |
| 892 } else if (s->out_format == FMT_H263 && s->h263_plus) { | |
| 893 last_gob = h263_encode_gob_header(s, mb_y); | |
| 894 if (last_gob) { | |
| 895 //fprintf(stderr,"\nLast GOB size: %d", last_gob); | |
| 896 s->first_gob_line = 1; | |
| 897 } else | |
| 898 s->first_gob_line = 0; | |
| 899 } | |
| 0 | 900 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
| 901 | |
| 902 s->mb_x = mb_x; | |
| 903 s->mb_y = mb_y; | |
| 904 | |
| 905 /* compute motion vector and macro block type (intra or non intra) */ | |
| 906 motion_x = 0; | |
| 907 motion_y = 0; | |
| 908 if (s->pict_type == P_TYPE) { | |
| 909 s->mb_intra = estimate_motion(s, mb_x, mb_y, | |
| 910 &motion_x, | |
| 911 &motion_y); | |
| 912 } else { | |
| 913 s->mb_intra = 1; | |
| 914 } | |
| 915 | |
| 916 /* get the pixels */ | |
| 917 wrap = s->linesize; | |
| 918 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
|
919 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
|
920 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
|
921 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
|
922 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); |
| 0 | 923 wrap = s->linesize >> 1; |
| 924 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
|
925 get_pixels(s->block[4], ptr, wrap); |
| 0 | 926 |
| 927 wrap = s->linesize >> 1; | |
| 928 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
|
929 get_pixels(s->block[5], ptr, wrap); |
| 0 | 930 |
| 931 /* subtract previous frame if non intra */ | |
| 932 if (!s->mb_intra) { | |
| 933 int dxy, offset, mx, my; | |
| 934 | |
| 935 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
| 936 ptr = s->last_picture[0] + | |
| 937 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + | |
| 938 (mb_x * 16 + (motion_x >> 1)); | |
| 939 | |
|
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
|
940 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
|
941 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
|
942 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
|
943 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy); |
| 0 | 944 |
| 945 if (s->out_format == FMT_H263) { | |
| 946 /* special rounding for h263 */ | |
| 947 dxy = 0; | |
| 948 if ((motion_x & 3) != 0) | |
| 949 dxy |= 1; | |
| 950 if ((motion_y & 3) != 0) | |
| 951 dxy |= 2; | |
| 952 mx = motion_x >> 2; | |
| 953 my = motion_y >> 2; | |
| 954 } else { | |
| 955 mx = motion_x / 2; | |
| 956 my = motion_y / 2; | |
| 957 dxy = ((my & 1) << 1) | (mx & 1); | |
| 958 mx >>= 1; | |
| 959 my >>= 1; | |
| 960 } | |
| 961 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx); | |
| 962 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
|
963 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy); |
| 0 | 964 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
|
965 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy); |
| 0 | 966 } |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
967 emms_c(); |
| 0 | 968 |
| 969 /* DCT & quantize */ | |
| 970 if (s->h263_msmpeg4) { | |
| 971 msmpeg4_dc_scale(s); | |
| 972 } else if (s->h263_pred) { | |
| 973 h263_dc_scale(s); | |
| 974 } else { | |
| 975 /* default quantization values */ | |
| 976 s->y_dc_scale = 8; | |
| 977 s->c_dc_scale = 8; | |
| 978 } | |
| 979 | |
| 980 for(i=0;i<6;i++) { | |
| 981 int last_index; | |
| 982 if (av_fdct == jpeg_fdct_ifast) | |
|
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
|
983 last_index = dct_quantize(s, s->block[i], i, s->qscale); |
| 0 | 984 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
|
985 last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale); |
| 0 | 986 s->block_last_index[i] = last_index; |
| 987 } | |
| 988 | |
| 989 /* huffman encode */ | |
| 990 switch(s->out_format) { | |
| 991 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
|
992 mpeg1_encode_mb(s, s->block, motion_x, motion_y); |
| 0 | 993 break; |
| 994 case FMT_H263: | |
| 995 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
|
996 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); |
| 0 | 997 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
|
998 h263_encode_mb(s, s->block, motion_x, motion_y); |
| 0 | 999 break; |
| 1000 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
|
1001 mjpeg_encode_mb(s, s->block); |
| 0 | 1002 break; |
| 1003 } | |
| 1004 | |
| 1005 /* decompress blocks so that we keep the state of the decoder */ | |
| 1006 s->mv[0][0][0] = motion_x; | |
| 1007 s->mv[0][0][1] = motion_y; | |
| 1008 | |
|
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
|
1009 MPV_decode_mb(s, s->block); |
| 0 | 1010 } |
| 162 | 1011 /* Obtain average MB line size for RTP */ |
| 1012 if (!mb_y) | |
| 1013 s->mb_line_avgsize = s->pb.buf_ptr - s->ptr_last_mb_line; | |
| 1014 else | |
| 1015 s->mb_line_avgsize = (s->mb_line_avgsize + s->pb.buf_ptr - s->ptr_last_mb_line) >> 1; | |
| 1016 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, | |
| 1017 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); | |
| 1018 s->ptr_last_mb_line = s->pb.buf_ptr; | |
| 0 | 1019 } |
| 162 | 1020 //if (s->gob_number) |
| 1021 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | |
| 0 | 1022 } |
| 1023 | |
| 1024 static int dct_quantize(MpegEncContext *s, | |
| 1025 DCTELEM *block, int n, | |
| 1026 int qscale) | |
| 1027 { | |
| 1028 int i, j, level, last_non_zero, q; | |
| 1029 const int *qmat; | |
| 1030 | |
| 1031 av_fdct (block); | |
| 1032 | |
| 64 | 1033 /* we need this permutation so that we correct the IDCT |
| 1034 permutation. will be moved into DCT code */ | |
| 1035 block_permute(block); | |
| 1036 | |
| 0 | 1037 if (s->mb_intra) { |
| 1038 if (n < 4) | |
| 1039 q = s->y_dc_scale; | |
| 1040 else | |
| 1041 q = s->c_dc_scale; | |
| 1042 q = q << 3; | |
| 1043 | |
| 1044 /* note: block[0] is assumed to be positive */ | |
| 1045 block[0] = (block[0] + (q >> 1)) / q; | |
| 1046 i = 1; | |
| 1047 last_non_zero = 0; | |
| 1048 if (s->out_format == FMT_H263) { | |
| 1049 qmat = s->q_non_intra_matrix; | |
| 1050 } else { | |
| 1051 qmat = s->q_intra_matrix; | |
| 1052 } | |
| 1053 } else { | |
| 1054 i = 0; | |
| 1055 last_non_zero = -1; | |
| 1056 qmat = s->q_non_intra_matrix; | |
| 1057 } | |
| 1058 | |
| 1059 for(;i<64;i++) { | |
| 1060 j = zigzag_direct[i]; | |
| 1061 level = block[j]; | |
| 1062 level = level * qmat[j]; | |
| 1063 #ifdef PARANOID | |
| 1064 { | |
| 1065 static int count = 0; | |
| 1066 int level1, level2, qmat1; | |
| 1067 double val; | |
| 1068 if (qmat == s->q_non_intra_matrix) { | |
| 1069 qmat1 = default_non_intra_matrix[j] * s->qscale; | |
| 1070 } else { | |
| 1071 qmat1 = default_intra_matrix[j] * s->qscale; | |
| 1072 } | |
| 1073 if (av_fdct != jpeg_fdct_ifast) | |
| 1074 val = ((double)block[j] * 8.0) / (double)qmat1; | |
| 1075 else | |
| 1076 val = ((double)block[j] * 8.0 * 2048.0) / | |
| 1077 ((double)qmat1 * aanscales[j]); | |
| 1078 level1 = (int)val; | |
| 1079 level2 = level / (1 << (QMAT_SHIFT - 3)); | |
| 1080 if (level1 != level2) { | |
| 1081 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", | |
| 1082 count, level2, level1, block[j], qmat1, qmat[j], | |
| 1083 val); | |
| 1084 count++; | |
| 1085 } | |
| 1086 | |
| 1087 } | |
| 1088 #endif | |
| 1089 /* XXX: slight error for the low range. Test should be equivalent to | |
| 1090 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 << | |
| 1091 (QMAT_SHIFT - 3))) | |
| 1092 */ | |
| 1093 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != | |
| 1094 level) { | |
| 1095 level = level / (1 << (QMAT_SHIFT - 3)); | |
| 1096 /* XXX: currently, this code is not optimal. the range should be: | |
| 1097 mpeg1: -255..255 | |
| 1098 mpeg2: -2048..2047 | |
| 1099 h263: -128..127 | |
| 1100 mpeg4: -2048..2047 | |
| 1101 */ | |
| 1102 if (level > 127) | |
| 1103 level = 127; | |
| 1104 else if (level < -128) | |
| 1105 level = -128; | |
| 1106 block[j] = level; | |
| 1107 last_non_zero = i; | |
| 1108 } else { | |
| 1109 block[j] = 0; | |
| 1110 } | |
| 1111 } | |
| 1112 return last_non_zero; | |
| 1113 } | |
| 1114 | |
| 1115 static int dct_quantize_mmx(MpegEncContext *s, | |
| 1116 DCTELEM *block, int n, | |
| 1117 int qscale) | |
| 1118 { | |
| 1119 int i, j, level, last_non_zero, q; | |
| 1120 const int *qmat; | |
| 1121 | |
| 1122 av_fdct (block); | |
|
40
f324d9047ca3
fixed block permutation in encoder (not optimal - should move it in forward DCT code)
glantau
parents:
19
diff
changeset
|
1123 |
|
f324d9047ca3
fixed block permutation in encoder (not optimal - should move it in forward DCT code)
glantau
parents:
19
diff
changeset
|
1124 /* we need this permutation so that we correct the IDCT |
|
f324d9047ca3
fixed block permutation in encoder (not optimal - should move it in forward DCT code)
glantau
parents:
19
diff
changeset
|
1125 permutation. will be moved into DCT code */ |
|
f324d9047ca3
fixed block permutation in encoder (not optimal - should move it in forward DCT code)
glantau
parents:
19
diff
changeset
|
1126 block_permute(block); |
| 0 | 1127 |
| 1128 if (s->mb_intra) { | |
| 1129 if (n < 4) | |
| 1130 q = s->y_dc_scale; | |
| 1131 else | |
| 1132 q = s->c_dc_scale; | |
| 1133 | |
| 1134 /* note: block[0] is assumed to be positive */ | |
| 1135 block[0] = (block[0] + (q >> 1)) / q; | |
| 1136 i = 1; | |
| 1137 last_non_zero = 0; | |
| 1138 if (s->out_format == FMT_H263) { | |
| 1139 qmat = s->q_non_intra_matrix; | |
| 1140 } else { | |
| 1141 qmat = s->q_intra_matrix; | |
| 1142 } | |
| 1143 } else { | |
| 1144 i = 0; | |
| 1145 last_non_zero = -1; | |
| 1146 qmat = s->q_non_intra_matrix; | |
| 1147 } | |
| 1148 | |
| 1149 for(;i<64;i++) { | |
| 1150 j = zigzag_direct[i]; | |
| 1151 level = block[j]; | |
| 1152 level = level * qmat[j]; | |
| 1153 /* XXX: slight error for the low range. Test should be equivalent to | |
| 1154 (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 << | |
| 1155 (QMAT_SHIFT_MMX - 3))) | |
| 1156 */ | |
| 1157 if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) != | |
| 1158 level) { | |
| 1159 level = level / (1 << (QMAT_SHIFT_MMX - 3)); | |
| 1160 /* XXX: currently, this code is not optimal. the range should be: | |
| 1161 mpeg1: -255..255 | |
| 1162 mpeg2: -2048..2047 | |
| 1163 h263: -128..127 | |
| 1164 mpeg4: -2048..2047 | |
| 1165 */ | |
| 1166 if (level > 127) | |
| 1167 level = 127; | |
| 1168 else if (level < -128) | |
| 1169 level = -128; | |
| 1170 block[j] = level; | |
| 1171 last_non_zero = i; | |
| 1172 } else { | |
| 1173 block[j] = 0; | |
| 1174 } | |
| 1175 } | |
| 1176 return last_non_zero; | |
| 1177 } | |
| 1178 | |
|
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
|
1179 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
|
1180 DCTELEM *block, int n, int qscale) |
| 0 | 1181 { |
| 1182 int i, level; | |
| 1183 const UINT16 *quant_matrix; | |
| 1184 | |
| 1185 if (s->mb_intra) { | |
| 1186 if (n < 4) | |
| 1187 block[0] = block[0] * s->y_dc_scale; | |
| 1188 else | |
| 1189 block[0] = block[0] * s->c_dc_scale; | |
| 1190 /* XXX: only mpeg1 */ | |
| 1191 quant_matrix = s->intra_matrix; | |
| 1192 for(i=1;i<64;i++) { | |
| 1193 level = block[i]; | |
| 1194 if (level) { | |
| 1195 if (level < 0) { | |
| 1196 level = -level; | |
| 1197 level = (int)(level * qscale * quant_matrix[i]) >> 3; | |
| 1198 level = (level - 1) | 1; | |
| 1199 level = -level; | |
| 1200 } else { | |
| 1201 level = (int)(level * qscale * quant_matrix[i]) >> 3; | |
| 1202 level = (level - 1) | 1; | |
| 1203 } | |
| 1204 #ifdef PARANOID | |
| 1205 if (level < -2048 || level > 2047) | |
| 1206 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 1207 #endif | |
| 1208 block[i] = level; | |
| 1209 } | |
| 1210 } | |
| 1211 } else { | |
| 1212 i = 0; | |
| 1213 quant_matrix = s->non_intra_matrix; | |
| 1214 for(;i<64;i++) { | |
| 1215 level = block[i]; | |
| 1216 if (level) { | |
| 1217 if (level < 0) { | |
| 1218 level = -level; | |
| 1219 level = (((level << 1) + 1) * qscale * | |
| 1220 ((int) (quant_matrix[i]))) >> 4; | |
| 1221 level = (level - 1) | 1; | |
| 1222 level = -level; | |
| 1223 } else { | |
| 1224 level = (((level << 1) + 1) * qscale * | |
| 1225 ((int) (quant_matrix[i]))) >> 4; | |
| 1226 level = (level - 1) | 1; | |
| 1227 } | |
| 1228 #ifdef PARANOID | |
| 1229 if (level < -2048 || level > 2047) | |
| 1230 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 1231 #endif | |
| 1232 block[i] = level; | |
| 1233 } | |
| 1234 } | |
| 1235 } | |
| 1236 } | |
|
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
|
1237 |
|
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
|
1238 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
|
1239 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
|
1240 { |
|
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
|
1241 int i, 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
|
1242 |
|
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
|
1243 if (s->mb_intra) { |
|
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
|
1244 if (n < 4) |
|
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
|
1245 block[0] = block[0] * s->y_dc_scale; |
|
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
|
1246 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
|
1247 block[0] = block[0] * s->c_dc_scale; |
|
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1248 i = 1; |
|
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
|
1249 } 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
|
1250 i = 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
|
1251 } |
|
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
|
1252 |
|
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
|
1253 qmul = s->qscale << 1; |
|
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
|
1254 qadd = (s->qscale - 1) | 1; |
|
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
|
1255 |
|
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
|
1256 for(;i<64;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
|
1257 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
|
1258 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
|
1259 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
|
1260 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
|
1261 } 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
|
1262 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
|
1263 } |
|
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
|
1264 #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
|
1265 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
|
1266 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
|
1267 #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
|
1268 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
|
1269 } |
|
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
|
1270 } |
|
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
|
1271 } |
| 0 | 1272 |
| 1273 /* rate control */ | |
| 1274 | |
| 1275 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */ | |
| 1276 #define I_FRAME_SIZE_RATIO 3.0 | |
| 1277 #define QSCALE_K 20 | |
| 1278 | |
| 1279 static void rate_control_init(MpegEncContext *s) | |
| 1280 { | |
| 1281 s->wanted_bits = 0; | |
| 1282 | |
| 1283 if (s->intra_only) { | |
| 1284 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate; | |
| 1285 s->P_frame_bits = s->I_frame_bits; | |
| 1286 } else { | |
| 1287 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / | |
| 1288 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1))); | |
| 1289 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO); | |
| 1290 } | |
| 1291 | |
| 1292 #if defined(DEBUG) | |
| 1293 printf("I_frame_size=%d P_frame_size=%d\n", | |
| 1294 s->I_frame_bits, s->P_frame_bits); | |
| 1295 #endif | |
| 1296 } | |
| 1297 | |
| 1298 | |
| 1299 /* | |
| 1300 * This heuristic is rather poor, but at least we do not have to | |
| 1301 * change the qscale at every macroblock. | |
| 1302 */ | |
| 1303 static int rate_estimate_qscale(MpegEncContext *s) | |
| 1304 { | |
| 64 | 1305 INT64 total_bits = s->total_bits; |
| 0 | 1306 float q; |
| 1307 int qscale, diff, qmin; | |
| 1308 | |
| 1309 if (s->pict_type == I_TYPE) { | |
| 1310 s->wanted_bits += s->I_frame_bits; | |
| 1311 } else { | |
| 1312 s->wanted_bits += s->P_frame_bits; | |
| 1313 } | |
| 1314 diff = s->wanted_bits - total_bits; | |
| 1315 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width); | |
| 1316 /* adjust for I frame */ | |
| 1317 if (s->pict_type == I_TYPE && !s->intra_only) { | |
| 1318 q /= I_FRAME_SIZE_RATIO; | |
| 1319 } | |
| 1320 | |
| 1321 /* using a too small Q scale leeds to problems in mpeg1 and h263 | |
| 1322 because AC coefficients are clamped to 255 or 127 */ | |
| 1323 qmin = 3; | |
| 1324 if (q < qmin) | |
| 1325 q = qmin; | |
| 1326 else if (q > 31) | |
| 1327 q = 31; | |
| 1328 qscale = (int)(q + 0.5); | |
| 1329 #if defined(DEBUG) | |
| 64 | 1330 printf("%d: total=%0.0f br=%0.1f diff=%d qest=%0.1f\n", |
| 0 | 1331 s->picture_number, |
| 64 | 1332 (double)total_bits, |
| 0 | 1333 (float)s->frame_rate / FRAME_RATE_BASE * |
| 1334 total_bits / s->picture_number, | |
| 1335 diff, q); | |
| 1336 #endif | |
| 1337 return qscale; | |
| 1338 } | |
| 1339 | |
| 1340 AVCodec mpeg1video_encoder = { | |
| 1341 "mpeg1video", | |
| 1342 CODEC_TYPE_VIDEO, | |
| 1343 CODEC_ID_MPEG1VIDEO, | |
| 1344 sizeof(MpegEncContext), | |
| 1345 MPV_encode_init, | |
| 1346 MPV_encode_picture, | |
| 1347 MPV_encode_end, | |
| 1348 }; | |
| 1349 | |
| 1350 AVCodec h263_encoder = { | |
| 1351 "h263", | |
| 1352 CODEC_TYPE_VIDEO, | |
| 1353 CODEC_ID_H263, | |
| 1354 sizeof(MpegEncContext), | |
| 1355 MPV_encode_init, | |
| 1356 MPV_encode_picture, | |
| 1357 MPV_encode_end, | |
| 1358 }; | |
| 1359 | |
| 1360 AVCodec h263p_encoder = { | |
| 1361 "h263p", | |
| 1362 CODEC_TYPE_VIDEO, | |
| 1363 CODEC_ID_H263P, | |
| 1364 sizeof(MpegEncContext), | |
| 1365 MPV_encode_init, | |
| 1366 MPV_encode_picture, | |
| 1367 MPV_encode_end, | |
| 1368 }; | |
| 1369 | |
| 1370 AVCodec rv10_encoder = { | |
| 1371 "rv10", | |
| 1372 CODEC_TYPE_VIDEO, | |
| 1373 CODEC_ID_RV10, | |
| 1374 sizeof(MpegEncContext), | |
| 1375 MPV_encode_init, | |
| 1376 MPV_encode_picture, | |
| 1377 MPV_encode_end, | |
| 1378 }; | |
| 1379 | |
| 1380 AVCodec mjpeg_encoder = { | |
| 1381 "mjpeg", | |
| 1382 CODEC_TYPE_VIDEO, | |
| 1383 CODEC_ID_MJPEG, | |
| 1384 sizeof(MpegEncContext), | |
| 1385 MPV_encode_init, | |
| 1386 MPV_encode_picture, | |
| 1387 MPV_encode_end, | |
| 1388 }; | |
| 1389 | |
| 71 | 1390 AVCodec mpeg4_encoder = { |
| 1391 "mpeg4", | |
| 0 | 1392 CODEC_TYPE_VIDEO, |
| 71 | 1393 CODEC_ID_MPEG4, |
| 0 | 1394 sizeof(MpegEncContext), |
| 1395 MPV_encode_init, | |
| 1396 MPV_encode_picture, | |
| 1397 MPV_encode_end, | |
| 1398 }; | |
| 1399 | |
| 1400 AVCodec msmpeg4_encoder = { | |
| 1401 "msmpeg4", | |
| 1402 CODEC_TYPE_VIDEO, | |
| 1403 CODEC_ID_MSMPEG4, | |
| 1404 sizeof(MpegEncContext), | |
| 1405 MPV_encode_init, | |
| 1406 MPV_encode_picture, | |
| 1407 MPV_encode_end, | |
| 1408 }; |
