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