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