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