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