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