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