Mercurial > libavcodec.hg
annotate mpegvideo.c @ 320:cda7d0857baf libavcodec
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
- EPZS ME algo used by default.
- HQ flag activated for ffmpeg.
- Cosmetics ...
author | pulento |
---|---|
date | Sun, 14 Apr 2002 18:57:51 +0000 |
parents | 40d8092e2ff0 |
children | 2b00e171b1d4 |
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. | |
295 | 18 * |
19 * 4MV & hq encoding stuff by Michael Niedermayer <michaelni@gmx.at> | |
0 | 20 */ |
21 #include <stdlib.h> | |
22 #include <stdio.h> | |
23 #include <math.h> | |
24 #include <string.h> | |
25 #include "avcodec.h" | |
26 #include "dsputil.h" | |
27 #include "mpegvideo.h" | |
28 | |
17
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
29 #ifdef USE_FASTMEMCPY |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
30 #include "fastmemcpy.h" |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
31 #endif |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
32 |
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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 DCTELEM *block, int n, int qscale); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
38 static 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
|
39 DCTELEM *block, int n, int qscale); |
206 | 40 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w); |
220 | 41 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale); |
206 | 42 |
220 | 43 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c; |
206 | 44 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c; |
45 | |
0 | 46 #define EDGE_WIDTH 16 |
47 | |
48 /* enable all paranoid tests for rounding, overflows, etc... */ | |
49 //#define PARANOID | |
50 | |
51 //#define DEBUG | |
52 | |
53 /* for jpeg fast DCT */ | |
54 #define CONST_BITS 14 | |
55 | |
56 static const unsigned short aanscales[64] = { | |
57 /* precomputed values scaled up by 14 bits */ | |
58 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
59 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
60 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
61 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
62 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
63 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
64 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
65 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
66 }; | |
67 | |
68 static UINT8 h263_chroma_roundtab[16] = { | |
69 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | |
70 }; | |
71 | |
292
73a9ce3d9715
fcode_tables where too small, found by Klaas-Pieter Vlieg <vlieg@eurescom.de>
michaelni
parents:
288
diff
changeset
|
72 static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1]; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
73 static UINT8 default_fcode_tab[MAX_MV*2+1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
74 |
0 | 75 /* default motion estimation */ |
76 int motion_estimation_method = ME_LOG; | |
77 | |
200 | 78 extern UINT8 zigzag_end[64]; |
79 | |
220 | 80 static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale) |
0 | 81 { |
82 int i; | |
83 | |
84 if (av_fdct == jpeg_fdct_ifast) { | |
85 for(i=0;i<64;i++) { | |
86 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
220 | 87 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ |
88 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ | |
89 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ | |
0 | 90 |
220 | 91 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / |
92 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)])); | |
0 | 93 } |
94 } else { | |
95 for(i=0;i<64;i++) { | |
96 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
220 | 97 So 16 <= qscale * quant_matrix[i] <= 7905 |
98 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905 | |
99 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 | |
0 | 100 */ |
220 | 101 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); |
102 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]); | |
0 | 103 } |
104 } | |
105 } | |
106 | |
107 /* init common structure for both encoder and decoder */ | |
108 int MPV_common_init(MpegEncContext *s) | |
109 { | |
110 int c_size, i; | |
111 UINT8 *pict; | |
112 | |
312 | 113 s->dct_unquantize_h263 = dct_unquantize_h263_c; |
114 s->dct_unquantize_mpeg = dct_unquantize_mpeg1_c; | |
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
|
115 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
116 #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
|
117 MPV_common_init_mmx(s); |
8 | 118 #endif |
312 | 119 //setup default unquantizers (mpeg4 might change it later) |
120 if(s->out_format == FMT_H263) | |
121 s->dct_unquantize = s->dct_unquantize_h263; | |
122 else | |
123 s->dct_unquantize = s->dct_unquantize_mpeg; | |
124 | |
0 | 125 s->mb_width = (s->width + 15) / 16; |
126 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
|
127 s->mb_num = s->mb_width * s->mb_height; |
0 | 128 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH; |
129 | |
130 for(i=0;i<3;i++) { | |
131 int w, h, shift, pict_start; | |
132 | |
133 w = s->linesize; | |
134 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | |
135 shift = (i == 0) ? 0 : 1; | |
136 c_size = (w >> shift) * (h >> shift); | |
137 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | |
138 | |
139 pict = av_mallocz(c_size); | |
140 if (pict == NULL) | |
141 goto fail; | |
142 s->last_picture_base[i] = pict; | |
143 s->last_picture[i] = pict + pict_start; | |
144 | |
145 pict = av_mallocz(c_size); | |
146 if (pict == NULL) | |
147 goto fail; | |
148 s->next_picture_base[i] = pict; | |
149 s->next_picture[i] = pict + pict_start; | |
150 | |
151 if (s->has_b_frames) { | |
152 pict = av_mallocz(c_size); | |
153 if (pict == NULL) | |
154 goto fail; | |
155 s->aux_picture_base[i] = pict; | |
156 s->aux_picture[i] = pict + pict_start; | |
157 } | |
158 } | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
159 |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
160 if (s->encoding) { |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
161 /* Allocate MB type table */ |
239 | 162 s->mb_type = av_mallocz(s->mb_num * sizeof(char)); |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
163 if (s->mb_type == 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 } |
239 | 167 |
168 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16)); | |
169 if (s->mb_var == NULL) { | |
170 perror("malloc"); | |
171 goto fail; | |
172 } | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
173 /* Allocate MV table */ |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
174 /* By now we just have one MV per MB */ |
239 | 175 s->mv_table[0] = av_mallocz(s->mb_num * sizeof(INT16)); |
176 s->mv_table[1] = av_mallocz(s->mb_num * sizeof(INT16)); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
177 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
|
178 perror("malloc"); |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
179 goto fail; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
180 } |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
181 } |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
182 |
288 | 183 if (s->out_format == FMT_H263 || s->encoding) { |
0 | 184 int size; |
185 /* MV prediction */ | |
186 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
187 s->motion_val = malloc(size * 2 * sizeof(INT16)); | |
188 if (s->motion_val == NULL) | |
189 goto fail; | |
190 memset(s->motion_val, 0, size * 2 * sizeof(INT16)); | |
191 } | |
192 | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
193 if (s->h263_pred || s->h263_plus) { |
0 | 194 int y_size, c_size, i, size; |
195 | |
196 /* dc values */ | |
197 | |
198 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
199 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
200 size = y_size + 2 * c_size; | |
201 s->dc_val[0] = malloc(size * sizeof(INT16)); | |
202 if (s->dc_val[0] == NULL) | |
203 goto fail; | |
204 s->dc_val[1] = s->dc_val[0] + y_size; | |
205 s->dc_val[2] = s->dc_val[1] + c_size; | |
206 for(i=0;i<size;i++) | |
207 s->dc_val[0][i] = 1024; | |
208 | |
209 /* ac values */ | |
210 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16); | |
211 if (s->ac_val[0] == NULL) | |
212 goto fail; | |
213 s->ac_val[1] = s->ac_val[0] + y_size; | |
214 s->ac_val[2] = s->ac_val[1] + c_size; | |
215 | |
216 /* cbp values */ | |
217 s->coded_block = av_mallocz(y_size); | |
218 if (!s->coded_block) | |
219 goto fail; | |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
220 |
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
221 /* 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
|
222 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
|
223 if (!s->mbintra_table) |
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
224 goto fail; |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
225 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
|
226 } |
0 | 227 /* default structure is frame */ |
228 s->picture_structure = PICT_FRAME; | |
229 | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
230 /* init macroblock skip table */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
231 if (!s->encoding) { |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
232 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
|
233 if (!s->mbskip_table) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
234 goto fail; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
235 } |
294 | 236 |
237 s->block= s->intra_block; | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
238 |
0 | 239 s->context_initialized = 1; |
240 return 0; | |
241 fail: | |
244 | 242 MPV_common_end(s); |
243 return -1; | |
244 } | |
245 | |
246 /* init common structure for both encoder and decoder */ | |
247 void MPV_common_end(MpegEncContext *s) | |
248 { | |
249 int i; | |
250 | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
251 if (s->mb_type) |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
252 free(s->mb_type); |
239 | 253 if (s->mb_var) |
254 free(s->mb_var); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
255 if (s->mv_table[0]) |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
256 free(s->mv_table[0]); |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
257 if (s->mv_table[1]) |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
258 free(s->mv_table[1]); |
0 | 259 if (s->motion_val) |
260 free(s->motion_val); | |
261 if (s->dc_val[0]) | |
262 free(s->dc_val[0]); | |
263 if (s->ac_val[0]) | |
264 free(s->ac_val[0]); | |
265 if (s->coded_block) | |
266 free(s->coded_block); | |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
267 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
|
268 free(s->mbintra_table); |
244 | 269 |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
270 if (s->mbskip_table) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
271 free(s->mbskip_table); |
0 | 272 for(i=0;i<3;i++) { |
273 if (s->last_picture_base[i]) | |
244 | 274 free(s->last_picture_base[i]); |
275 if (s->next_picture_base[i]) | |
276 free(s->next_picture_base[i]); | |
0 | 277 if (s->has_b_frames) |
278 free(s->aux_picture_base[i]); | |
279 } | |
280 s->context_initialized = 0; | |
281 } | |
282 | |
283 /* init video encoder */ | |
284 int MPV_encode_init(AVCodecContext *avctx) | |
285 { | |
286 MpegEncContext *s = avctx->priv_data; | |
60 | 287 int i; |
0 | 288 |
315 | 289 avctx->pix_fmt = PIX_FMT_YUV420P; |
290 | |
0 | 291 s->bit_rate = avctx->bit_rate; |
268 | 292 s->bit_rate_tolerance = avctx->bit_rate_tolerance; |
0 | 293 s->frame_rate = avctx->frame_rate; |
294 s->width = avctx->width; | |
295 s->height = avctx->height; | |
296 s->gop_size = avctx->gop_size; | |
162 | 297 s->rtp_mode = avctx->rtp_mode; |
298 s->rtp_payload_size = avctx->rtp_payload_size; | |
231 | 299 if (avctx->rtp_callback) |
300 s->rtp_callback = avctx->rtp_callback; | |
268 | 301 s->qmin= avctx->qmin; |
302 s->qmax= avctx->qmax; | |
303 s->max_qdiff= avctx->max_qdiff; | |
304 s->qcompress= avctx->qcompress; | |
305 s->qblur= avctx->qblur; | |
194
27d1773552c9
mpeg4 encoder fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
191
diff
changeset
|
306 s->avctx = avctx; |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
307 s->aspect_ratio_info= avctx->aspect_ratio_info; |
294 | 308 s->flags= avctx->flags; |
162 | 309 |
0 | 310 if (s->gop_size <= 1) { |
311 s->intra_only = 1; | |
312 s->gop_size = 12; | |
313 } else { | |
314 s->intra_only = 0; | |
315 } | |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
316 |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
317 /* ME algorithm */ |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
318 s->me_method = avctx->me_method; |
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
319 /* Fixed QSCALE */ |
0 | 320 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
|
321 |
0 | 322 switch(avctx->codec->id) { |
323 case CODEC_ID_MPEG1VIDEO: | |
324 s->out_format = FMT_MPEG1; | |
325 break; | |
326 case CODEC_ID_MJPEG: | |
327 s->out_format = FMT_MJPEG; | |
328 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
|
329 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
|
330 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
|
331 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
|
332 s->mjpeg_vsample[2] = 1; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
333 s->mjpeg_hsample[0] = 2; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
334 s->mjpeg_hsample[1] = 1; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
335 s->mjpeg_hsample[2] = 1; |
0 | 336 if (mjpeg_init(s) < 0) |
337 return -1; | |
338 break; | |
339 case CODEC_ID_H263: | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
340 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
|
341 printf("Input picture size isn't suitable for h263 codec! try h263+\n"); |
0 | 342 return -1; |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
343 } |
0 | 344 s->out_format = FMT_H263; |
345 break; | |
346 case CODEC_ID_H263P: | |
347 s->out_format = FMT_H263; | |
162 | 348 s->rtp_mode = 1; |
349 s->rtp_payload_size = 1200; | |
0 | 350 s->h263_plus = 1; |
78 | 351 s->unrestricted_mv = 1; |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
352 |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
353 /* These are just to be sure */ |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
354 s->umvplus = 0; |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
355 s->umvplus_dec = 0; |
0 | 356 break; |
357 case CODEC_ID_RV10: | |
358 s->out_format = FMT_H263; | |
359 s->h263_rv10 = 1; | |
360 break; | |
71 | 361 case CODEC_ID_MPEG4: |
0 | 362 s->out_format = FMT_H263; |
363 s->h263_pred = 1; | |
364 s->unrestricted_mv = 1; | |
365 break; | |
307 | 366 case CODEC_ID_MSMPEG4V1: |
0 | 367 s->out_format = FMT_H263; |
368 s->h263_msmpeg4 = 1; | |
369 s->h263_pred = 1; | |
370 s->unrestricted_mv = 1; | |
307 | 371 s->msmpeg4_version= 1; |
372 break; | |
373 case CODEC_ID_MSMPEG4V2: | |
374 s->out_format = FMT_H263; | |
375 s->h263_msmpeg4 = 1; | |
376 s->h263_pred = 1; | |
377 s->unrestricted_mv = 1; | |
378 s->msmpeg4_version= 2; | |
379 break; | |
380 case CODEC_ID_MSMPEG4V3: | |
381 s->out_format = FMT_H263; | |
382 s->h263_msmpeg4 = 1; | |
383 s->h263_pred = 1; | |
384 s->unrestricted_mv = 1; | |
385 s->msmpeg4_version= 3; | |
0 | 386 break; |
387 default: | |
388 return -1; | |
389 } | |
295 | 390 |
391 if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){ | |
392 printf("4MV is currently only supported in HQ mode\n"); | |
393 return -1; | |
394 } | |
0 | 395 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
396 { /* set up some save defaults, some codecs might override them later */ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
397 static int done=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
398 if(!done){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
399 int i; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
400 done=1; |
292
73a9ce3d9715
fcode_tables where too small, found by Klaas-Pieter Vlieg <vlieg@eurescom.de>
michaelni
parents:
288
diff
changeset
|
401 memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1)); |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
402 memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1)); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
403 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
404 for(i=-16; i<16; i++){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
405 default_fcode_tab[i + MAX_MV]= 1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
406 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
407 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
408 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
409 s->mv_penalty= default_mv_penalty; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
410 s->fcode_tab= default_fcode_tab; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
411 |
0 | 412 if (s->out_format == FMT_H263) |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
413 h263_encode_init(s); |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
414 else if (s->out_format == FMT_MPEG1) |
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
415 mpeg1_encode_init(s); |
0 | 416 |
287 | 417 /* dont use mv_penalty table for crap MV as it would be confused */ |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
418 if (s->me_method < 0) s->mv_penalty = default_mv_penalty; |
287 | 419 |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
420 s->encoding = 1; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
421 |
0 | 422 /* init */ |
423 if (MPV_common_init(s) < 0) | |
424 return -1; | |
425 | |
60 | 426 /* init default q matrix */ |
427 for(i=0;i<64;i++) { | |
428 s->intra_matrix[i] = default_intra_matrix[i]; | |
429 s->non_intra_matrix[i] = default_non_intra_matrix[i]; | |
430 } | |
431 | |
0 | 432 /* rate control init */ |
433 rate_control_init(s); | |
434 | |
435 s->picture_number = 0; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
436 s->picture_in_gop_number = 0; |
0 | 437 s->fake_picture_number = 0; |
438 /* motion detector init */ | |
439 s->f_code = 1; | |
440 | |
441 return 0; | |
442 } | |
443 | |
444 int MPV_encode_end(AVCodecContext *avctx) | |
445 { | |
446 MpegEncContext *s = avctx->priv_data; | |
447 | |
448 #ifdef STATS | |
449 print_stats(); | |
450 #endif | |
451 MPV_common_end(s); | |
452 if (s->out_format == FMT_MJPEG) | |
453 mjpeg_close(s); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
454 |
0 | 455 return 0; |
456 } | |
457 | |
458 /* draw the edges of width 'w' of an image of size width, height */ | |
206 | 459 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w) |
0 | 460 { |
461 UINT8 *ptr, *last_line; | |
462 int i; | |
463 | |
464 last_line = buf + (height - 1) * wrap; | |
465 for(i=0;i<w;i++) { | |
466 /* top and bottom */ | |
467 memcpy(buf - (i + 1) * wrap, buf, width); | |
468 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
469 } | |
470 /* left and right */ | |
471 ptr = buf; | |
472 for(i=0;i<height;i++) { | |
473 memset(ptr - w, ptr[0], w); | |
474 memset(ptr + width, ptr[width-1], w); | |
475 ptr += wrap; | |
476 } | |
477 /* corners */ | |
478 for(i=0;i<w;i++) { | |
479 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
480 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
481 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
482 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
483 } | |
484 } | |
485 | |
486 /* generic function for encode/decode called before a frame is coded/decoded */ | |
487 void MPV_frame_start(MpegEncContext *s) | |
488 { | |
489 int i; | |
490 UINT8 *tmp; | |
491 | |
46
931417475f5b
fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents:
40
diff
changeset
|
492 s->mb_skiped = 0; |
0 | 493 if (s->pict_type == B_TYPE) { |
494 for(i=0;i<3;i++) { | |
495 s->current_picture[i] = s->aux_picture[i]; | |
496 } | |
497 } else { | |
262 | 498 s->last_non_b_pict_type= s->pict_type; |
0 | 499 for(i=0;i<3;i++) { |
500 /* swap next and last */ | |
501 tmp = s->last_picture[i]; | |
502 s->last_picture[i] = s->next_picture[i]; | |
503 s->next_picture[i] = tmp; | |
504 s->current_picture[i] = tmp; | |
505 } | |
506 } | |
507 } | |
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
|
508 |
0 | 509 /* generic function for encode/decode called after a frame has been coded/decoded */ |
510 void MPV_frame_end(MpegEncContext *s) | |
511 { | |
512 /* draw edge for correct motion prediction if outside */ | |
220 | 513 if (s->pict_type != B_TYPE && !s->intra_only) { |
257 | 514 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){ |
176
9ce215ee9216
unrestricted MC fixed - thanks to Michael Niedermayer for idea
arpi_esp
parents:
162
diff
changeset
|
515 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
|
516 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
|
517 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
|
518 }else{ |
257 | 519 /* mpeg4? / opendivx / xvid */ |
0 | 520 draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH); |
521 draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2); | |
522 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
|
523 } |
0 | 524 } |
207 | 525 emms_c(); |
0 | 526 } |
527 | |
528 int MPV_encode_picture(AVCodecContext *avctx, | |
529 unsigned char *buf, int buf_size, void *data) | |
530 { | |
531 MpegEncContext *s = avctx->priv_data; | |
532 AVPicture *pict = data; | |
533 int i, j; | |
534 | |
535 if (s->fixed_qscale) | |
536 s->qscale = avctx->quality; | |
537 | |
538 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
539 | |
298
e20de99b6295
using Juanjo's way to pass forced type - especially as I_TYPE/P_TYPE defines aren't exported in the public interface, and the flagged method matches the way as qscale forcing work
arpi_esp
parents:
297
diff
changeset
|
540 s->force_type= (avctx->flags&CODEC_FLAG_TYPE) ? |
e20de99b6295
using Juanjo's way to pass forced type - especially as I_TYPE/P_TYPE defines aren't exported in the public interface, and the flagged method matches the way as qscale forcing work
arpi_esp
parents:
297
diff
changeset
|
541 (avctx->key_frame ? I_TYPE : P_TYPE) : 0; |
0 | 542 if (!s->intra_only) { |
543 /* first picture of GOP is intra */ | |
297 | 544 if (s->picture_in_gop_number % s->gop_size==0 || s->force_type==I_TYPE){ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
545 s->picture_in_gop_number=0; |
0 | 546 s->pict_type = I_TYPE; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
547 }else |
0 | 548 s->pict_type = P_TYPE; |
549 } else { | |
550 s->pict_type = I_TYPE; | |
551 } | |
552 | |
553 MPV_frame_start(s); | |
220 | 554 |
0 | 555 for(i=0;i<3;i++) { |
556 UINT8 *src = pict->data[i]; | |
557 UINT8 *dest = s->current_picture[i]; | |
558 int src_wrap = pict->linesize[i]; | |
559 int dest_wrap = s->linesize; | |
560 int w = s->width; | |
561 int h = s->height; | |
562 | |
563 if (i >= 1) { | |
564 dest_wrap >>= 1; | |
565 w >>= 1; | |
566 h >>= 1; | |
567 } | |
568 | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
569 if(dest_wrap==src_wrap){ |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
570 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
|
571 } else { |
220 | 572 for(j=0;j<h;j++) { |
573 memcpy(dest, src, w); | |
574 dest += dest_wrap; | |
575 src += src_wrap; | |
576 } | |
227
ec1bc02a0a47
avoid copying input when encoding non intra stuff too
michaelni
parents:
220
diff
changeset
|
577 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
|
578 } |
0 | 579 } |
580 | |
581 encode_picture(s, s->picture_number); | |
271 | 582 avctx->key_frame = (s->pict_type == I_TYPE); |
286 | 583 avctx->header_bits = s->header_bits; |
584 avctx->mv_bits = s->mv_bits; | |
585 avctx->misc_bits = s->misc_bits; | |
586 avctx->i_tex_bits = s->i_tex_bits; | |
587 avctx->p_tex_bits = s->p_tex_bits; | |
588 avctx->i_count = s->i_count; | |
589 avctx->p_count = s->p_count; | |
590 avctx->skip_count = s->skip_count; | |
591 | |
0 | 592 MPV_frame_end(s); |
593 s->picture_number++; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
594 s->picture_in_gop_number++; |
0 | 595 |
596 if (s->out_format == FMT_MJPEG) | |
597 mjpeg_picture_trailer(s); | |
598 | |
599 flush_put_bits(&s->pb); | |
268 | 600 s->last_frame_bits= s->frame_bits; |
601 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; | |
602 s->total_bits += s->frame_bits; | |
286 | 603 avctx->frame_bits = s->frame_bits; |
604 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", | |
605 //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits); | |
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
|
606 |
0 | 607 avctx->quality = s->qscale; |
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
608 if (avctx->get_psnr) { |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
609 /* At this point pict->data should have the original frame */ |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
610 /* an s->current_picture should have the coded/decoded frame */ |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
611 get_psnr(pict->data, s->current_picture, |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
612 pict->linesize, s->linesize, avctx); |
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
613 } |
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
|
614 return pbBufPtr(&s->pb) - s->pb.buf; |
0 | 615 } |
616 | |
617 static inline int clip(int a, int amin, int amax) | |
618 { | |
619 if (a < amin) | |
620 return amin; | |
621 else if (a > amax) | |
622 return amax; | |
623 else | |
624 return a; | |
625 } | |
626 | |
255 | 627 static inline void gmc1_motion(MpegEncContext *s, |
628 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
629 int dest_offset, | |
630 UINT8 **ref_picture, int src_offset, | |
631 int h) | |
632 { | |
633 UINT8 *ptr; | |
634 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
635 int motion_x, motion_y; | |
636 | |
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
637 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n"); |
255 | 638 motion_x= s->sprite_offset[0][0]; |
639 motion_y= s->sprite_offset[0][1]; | |
640 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
641 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
642 motion_x<<=(3-s->sprite_warping_accuracy); | |
643 motion_y<<=(3-s->sprite_warping_accuracy); | |
644 src_x = clip(src_x, -16, s->width); | |
645 if (src_x == s->width) | |
646 motion_x =0; | |
647 src_y = clip(src_y, -16, s->height); | |
648 if (src_y == s->height) | |
649 motion_y =0; | |
650 | |
651 linesize = s->linesize; | |
652 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
653 | |
654 dest_y+=dest_offset; | |
655 gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
656 gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
657 | |
658 motion_x= s->sprite_offset[1][0]; | |
659 motion_y= s->sprite_offset[1][1]; | |
660 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
661 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
662 motion_x<<=(3-s->sprite_warping_accuracy); | |
663 motion_y<<=(3-s->sprite_warping_accuracy); | |
664 src_x = clip(src_x, -8, s->width>>1); | |
665 if (src_x == s->width>>1) | |
666 motion_x =0; | |
667 src_y = clip(src_y, -8, s->height>>1); | |
668 if (src_y == s->height>>1) | |
669 motion_y =0; | |
670 | |
671 offset = (src_y * linesize>>1) + src_x + (src_offset>>1); | |
672 ptr = ref_picture[1] + offset; | |
673 gmc1(dest_cb + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
674 ptr = ref_picture[2] + offset; | |
675 gmc1(dest_cr + (dest_offset>>1), ptr, linesize>>1, h>>1, motion_x&15, motion_y&15, s->no_rounding); | |
676 | |
677 return; | |
678 } | |
679 | |
0 | 680 /* apply one mpeg motion vector to the three components */ |
681 static inline void mpeg_motion(MpegEncContext *s, | |
682 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
683 int dest_offset, | |
684 UINT8 **ref_picture, int src_offset, | |
685 int field_based, op_pixels_func *pix_op, | |
686 int motion_x, int motion_y, int h) | |
687 { | |
688 UINT8 *ptr; | |
689 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
255 | 690 if(s->quarter_sample) |
691 { | |
692 motion_x>>=1; | |
693 motion_y>>=1; | |
694 } | |
0 | 695 dxy = ((motion_y & 1) << 1) | (motion_x & 1); |
696 src_x = s->mb_x * 16 + (motion_x >> 1); | |
697 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
698 | |
699 /* WARNING: do no forget half pels */ | |
700 height = s->height >> field_based; | |
701 src_x = clip(src_x, -16, s->width); | |
702 if (src_x == s->width) | |
703 dxy &= ~1; | |
704 src_y = clip(src_y, -16, height); | |
705 if (src_y == height) | |
706 dxy &= ~2; | |
707 linesize = s->linesize << field_based; | |
708 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; | |
709 dest_y += dest_offset; | |
710 pix_op[dxy](dest_y, ptr, linesize, h); | |
711 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h); | |
712 | |
713 if (s->out_format == FMT_H263) { | |
714 dxy = 0; | |
715 if ((motion_x & 3) != 0) | |
716 dxy |= 1; | |
717 if ((motion_y & 3) != 0) | |
718 dxy |= 2; | |
719 mx = motion_x >> 2; | |
720 my = motion_y >> 2; | |
721 } else { | |
722 mx = motion_x / 2; | |
723 my = motion_y / 2; | |
724 dxy = ((my & 1) << 1) | (mx & 1); | |
725 mx >>= 1; | |
726 my >>= 1; | |
727 } | |
728 | |
729 src_x = s->mb_x * 8 + mx; | |
730 src_y = s->mb_y * (8 >> field_based) + my; | |
731 src_x = clip(src_x, -8, s->width >> 1); | |
732 if (src_x == (s->width >> 1)) | |
733 dxy &= ~1; | |
734 src_y = clip(src_y, -8, height >> 1); | |
735 if (src_y == (height >> 1)) | |
736 dxy &= ~2; | |
737 | |
738 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
739 ptr = ref_picture[1] + offset; | |
740 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
741 ptr = ref_picture[2] + offset; | |
742 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
743 } | |
744 | |
255 | 745 static inline void qpel_motion(MpegEncContext *s, |
746 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
747 int dest_offset, | |
748 UINT8 **ref_picture, int src_offset, | |
749 int field_based, op_pixels_func *pix_op, | |
750 qpel_mc_func *qpix_op, | |
751 int motion_x, int motion_y, int h) | |
752 { | |
753 UINT8 *ptr; | |
754 int dxy, offset, mx, my, src_x, src_y, height, linesize; | |
755 | |
756 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
757 src_x = s->mb_x * 16 + (motion_x >> 2); | |
758 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); | |
759 | |
760 height = s->height >> field_based; | |
761 src_x = clip(src_x, -16, s->width); | |
762 if (src_x == s->width) | |
763 dxy &= ~3; | |
764 src_y = clip(src_y, -16, height); | |
765 if (src_y == height) | |
766 dxy &= ~12; | |
767 linesize = s->linesize << field_based; | |
768 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
769 dest_y += dest_offset; | |
770 //printf("%d %d %d\n", src_x, src_y, dxy); | |
771 qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3); | |
772 qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3); | |
773 qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3); | |
774 qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3); | |
775 | |
776 mx= (motion_x>>1) | (motion_x&1); | |
777 my= (motion_y>>1) | (motion_y&1); | |
778 | |
779 dxy = 0; | |
780 if ((mx & 3) != 0) | |
781 dxy |= 1; | |
782 if ((my & 3) != 0) | |
783 dxy |= 2; | |
784 mx = mx >> 2; | |
785 my = my >> 2; | |
786 | |
787 src_x = s->mb_x * 8 + mx; | |
788 src_y = s->mb_y * (8 >> field_based) + my; | |
789 src_x = clip(src_x, -8, s->width >> 1); | |
790 if (src_x == (s->width >> 1)) | |
791 dxy &= ~1; | |
792 src_y = clip(src_y, -8, height >> 1); | |
793 if (src_y == (height >> 1)) | |
794 dxy &= ~2; | |
795 | |
796 offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1); | |
797 ptr = ref_picture[1] + offset; | |
798 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
799 ptr = ref_picture[2] + offset; | |
800 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1); | |
801 } | |
802 | |
803 | |
0 | 804 static inline void MPV_motion(MpegEncContext *s, |
805 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
806 int dir, UINT8 **ref_picture, | |
255 | 807 op_pixels_func *pix_op, qpel_mc_func *qpix_op) |
0 | 808 { |
809 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
810 int mb_x, mb_y, i; | |
811 UINT8 *ptr, *dest; | |
812 | |
813 mb_x = s->mb_x; | |
814 mb_y = s->mb_y; | |
815 | |
816 switch(s->mv_type) { | |
817 case MV_TYPE_16X16: | |
255 | 818 if(s->mcsel){ |
819 #if 0 | |
820 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
821 ref_picture, 0, | |
822 0, pix_op, | |
823 s->sprite_offset[0][0]>>3, | |
824 s->sprite_offset[0][1]>>3, | |
825 16); | |
826 #else | |
827 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0, | |
828 ref_picture, 0, | |
829 16); | |
830 #endif | |
262 | 831 }else if(s->quarter_sample && dir==0){ //FIXME |
255 | 832 qpel_motion(s, dest_y, dest_cb, dest_cr, 0, |
833 ref_picture, 0, | |
834 0, pix_op, qpix_op, | |
835 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
836 }else{ | |
837 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
838 ref_picture, 0, | |
839 0, pix_op, | |
840 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
841 } | |
0 | 842 break; |
843 case MV_TYPE_8X8: | |
844 for(i=0;i<4;i++) { | |
845 motion_x = s->mv[dir][i][0]; | |
846 motion_y = s->mv[dir][i][1]; | |
847 | |
848 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
849 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
295 | 850 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8; |
0 | 851 |
852 /* WARNING: do no forget half pels */ | |
853 src_x = clip(src_x, -16, s->width); | |
854 if (src_x == s->width) | |
855 dxy &= ~1; | |
856 src_y = clip(src_y, -16, s->height); | |
857 if (src_y == s->height) | |
858 dxy &= ~2; | |
859 | |
860 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
861 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
862 pix_op[dxy](dest, ptr, s->linesize, 8); | |
863 } | |
864 /* In case of 8X8, we construct a single chroma motion vector | |
865 with a special rounding */ | |
866 mx = 0; | |
867 my = 0; | |
868 for(i=0;i<4;i++) { | |
869 mx += s->mv[dir][i][0]; | |
870 my += s->mv[dir][i][1]; | |
871 } | |
872 if (mx >= 0) | |
873 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
874 else { | |
875 mx = -mx; | |
876 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
877 } | |
878 if (my >= 0) | |
879 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
880 else { | |
881 my = -my; | |
882 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
883 } | |
884 dxy = ((my & 1) << 1) | (mx & 1); | |
885 mx >>= 1; | |
886 my >>= 1; | |
887 | |
888 src_x = mb_x * 8 + mx; | |
889 src_y = mb_y * 8 + my; | |
890 src_x = clip(src_x, -8, s->width/2); | |
891 if (src_x == s->width/2) | |
892 dxy &= ~1; | |
893 src_y = clip(src_y, -8, s->height/2); | |
894 if (src_y == s->height/2) | |
895 dxy &= ~2; | |
896 | |
897 offset = (src_y * (s->linesize >> 1)) + src_x; | |
898 ptr = ref_picture[1] + offset; | |
899 pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8); | |
900 ptr = ref_picture[2] + offset; | |
901 pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8); | |
902 break; | |
903 case MV_TYPE_FIELD: | |
904 if (s->picture_structure == PICT_FRAME) { | |
905 /* top field */ | |
906 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
907 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
908 1, pix_op, | |
909 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
910 /* bottom field */ | |
911 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
912 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
913 1, pix_op, | |
914 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
915 } else { | |
916 | |
917 | |
918 } | |
919 break; | |
920 } | |
921 } | |
922 | |
923 | |
924 /* put block[] to dest[] */ | |
925 static inline void put_dct(MpegEncContext *s, | |
926 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
927 { | |
928 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
|
929 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
|
930 ff_idct (block); |
0 | 931 put_pixels_clamped(block, dest, line_size); |
932 } | |
933 | |
934 /* add block[] to dest[] */ | |
935 static inline void add_dct(MpegEncContext *s, | |
936 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
937 { | |
938 if (s->block_last_index[i] >= 0) { | |
939 if (!s->mpeg2) | |
206 | 940 if(s->encoding || (!s->h263_msmpeg4)) |
200 | 941 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
|
942 ff_idct (block); |
0 | 943 add_pixels_clamped(block, dest, line_size); |
944 } | |
945 } | |
946 | |
947 /* generic function called after a macroblock has been parsed by the | |
948 decoder or after it has been encoded by the encoder. | |
949 | |
950 Important variables used: | |
951 s->mb_intra : true if intra macroblock | |
952 s->mv_dir : motion vector direction | |
953 s->mv_type : motion vector type | |
954 s->mv : motion vector | |
955 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
956 */ | |
957 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
958 { | |
244 | 959 int mb_x, mb_y; |
0 | 960 int dct_linesize, dct_offset; |
961 op_pixels_func *op_pix; | |
255 | 962 qpel_mc_func *op_qpix; |
0 | 963 |
964 mb_x = s->mb_x; | |
965 mb_y = s->mb_y; | |
966 | |
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
967 #ifdef FF_POSTPROCESS |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
968 quant_store[mb_y][mb_x]=s->qscale; |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
969 //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
|
970 #endif |
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
971 |
0 | 972 /* update DC predictors for P macroblocks */ |
973 if (!s->mb_intra) { | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
974 if (s->h263_pred || s->h263_aic) { |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
975 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
|
976 { |
244 | 977 int wrap, xy, v; |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
978 s->mbintra_table[mb_x + mb_y*s->mb_width]=0; |
0 | 979 wrap = 2 * s->mb_width + 2; |
244 | 980 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; |
0 | 981 v = 1024; |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
982 |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
983 s->dc_val[0][xy] = v; |
244 | 984 s->dc_val[0][xy + 1] = v; |
985 s->dc_val[0][xy + wrap] = v; | |
986 s->dc_val[0][xy + 1 + wrap] = v; | |
0 | 987 /* ac pred */ |
244 | 988 memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16)); |
989 memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16)); | |
990 memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16)); | |
991 memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16)); | |
0 | 992 if (s->h263_msmpeg4) { |
244 | 993 s->coded_block[xy] = 0; |
994 s->coded_block[xy + 1] = 0; | |
995 s->coded_block[xy + wrap] = 0; | |
996 s->coded_block[xy + 1 + wrap] = 0; | |
0 | 997 } |
998 /* chroma */ | |
999 wrap = s->mb_width + 2; | |
244 | 1000 xy = mb_x + 1 + (mb_y + 1) * wrap; |
1001 s->dc_val[1][xy] = v; | |
1002 s->dc_val[2][xy] = v; | |
0 | 1003 /* ac pred */ |
244 | 1004 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); |
1005 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16)); | |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
1006 } |
0 | 1007 } else { |
1008 s->last_dc[0] = 128 << s->intra_dc_precision; | |
1009 s->last_dc[1] = 128 << s->intra_dc_precision; | |
1010 s->last_dc[2] = 128 << s->intra_dc_precision; | |
1011 } | |
1012 } | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1013 else if (s->h263_pred || s->h263_aic) |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
1014 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
|
1015 |
280 | 1016 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */ |
1017 if (s->out_format == FMT_H263) { | |
262 | 1018 if(s->pict_type!=B_TYPE){ |
244 | 1019 int xy, wrap, motion_x, motion_y; |
0 | 1020 |
1021 wrap = 2 * s->mb_width + 2; | |
244 | 1022 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; |
0 | 1023 if (s->mb_intra) { |
1024 motion_x = 0; | |
1025 motion_y = 0; | |
1026 goto motion_init; | |
1027 } else if (s->mv_type == MV_TYPE_16X16) { | |
1028 motion_x = s->mv[0][0][0]; | |
1029 motion_y = s->mv[0][0][1]; | |
1030 motion_init: | |
1031 /* no update if 8X8 because it has been done during parsing */ | |
244 | 1032 s->motion_val[xy][0] = motion_x; |
1033 s->motion_val[xy][1] = motion_y; | |
1034 s->motion_val[xy + 1][0] = motion_x; | |
1035 s->motion_val[xy + 1][1] = motion_y; | |
1036 s->motion_val[xy + wrap][0] = motion_x; | |
1037 s->motion_val[xy + wrap][1] = motion_y; | |
1038 s->motion_val[xy + 1 + wrap][0] = motion_x; | |
1039 s->motion_val[xy + 1 + wrap][1] = motion_y; | |
0 | 1040 } |
262 | 1041 } |
0 | 1042 } |
1043 | |
1044 if (!s->intra_only) { | |
1045 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
|
1046 UINT8 *mbskip_ptr; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1047 |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1048 /* 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
|
1049 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
|
1050 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
|
1051 if (s->mb_skiped) { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1052 s->mb_skiped = 0; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1053 /* 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
|
1054 if (*mbskip_ptr != 0) |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1055 goto the_end; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1056 *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
|
1057 } else { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1058 *mbskip_ptr = 0; /* not skipped */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1059 } |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1060 } |
0 | 1061 |
1062 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16; | |
1063 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
1064 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
1065 | |
1066 if (s->interlaced_dct) { | |
1067 dct_linesize = s->linesize * 2; | |
1068 dct_offset = s->linesize; | |
1069 } else { | |
1070 dct_linesize = s->linesize; | |
1071 dct_offset = s->linesize * 8; | |
1072 } | |
1073 | |
1074 if (!s->mb_intra) { | |
1075 /* motion handling */ | |
255 | 1076 if (!s->no_rounding){ |
0 | 1077 op_pix = put_pixels_tab; |
255 | 1078 op_qpix= qpel_mc_rnd_tab; |
1079 }else{ | |
0 | 1080 op_pix = put_no_rnd_pixels_tab; |
255 | 1081 op_qpix= qpel_mc_no_rnd_tab; |
1082 } | |
0 | 1083 |
1084 if (s->mv_dir & MV_DIR_FORWARD) { | |
255 | 1085 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); |
0 | 1086 if (!s->no_rounding) |
1087 op_pix = avg_pixels_tab; | |
1088 else | |
1089 op_pix = avg_no_rnd_pixels_tab; | |
1090 } | |
1091 if (s->mv_dir & MV_DIR_BACKWARD) { | |
255 | 1092 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); |
0 | 1093 } |
1094 | |
1095 /* add dct residue */ | |
1096 add_dct(s, block[0], 0, dest_y, dct_linesize); | |
1097 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
1098 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
1099 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
1100 | |
57 | 1101 add_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
1102 add_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
0 | 1103 } else { |
1104 /* dct only in intra block */ | |
1105 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
1106 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
1107 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
1108 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
1109 | |
57 | 1110 put_dct(s, block[4], 4, dest_cb, s->linesize >> 1); |
1111 put_dct(s, block[5], 5, dest_cr, s->linesize >> 1); | |
0 | 1112 } |
1113 } | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1114 the_end: |
294 | 1115 emms_c(); //FIXME remove |
1116 } | |
1117 | |
1118 static void encode_mb(MpegEncContext *s) | |
1119 { | |
1120 int wrap; | |
1121 const int mb_x= s->mb_x; | |
1122 const int mb_y= s->mb_y; | |
1123 UINT8 *ptr; | |
1124 const int motion_x= s->mv[0][0][0]; | |
1125 const int motion_y= s->mv[0][0][1]; | |
1126 int i; | |
1127 | |
1128 /* get the pixels */ | |
1129 wrap = s->linesize; | |
1130 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; | |
1131 get_pixels(s->block[0], ptr, wrap); | |
1132 get_pixels(s->block[1], ptr + 8, wrap); | |
1133 get_pixels(s->block[2], ptr + 8 * wrap, wrap); | |
1134 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); | |
1135 wrap = s->linesize >> 1; | |
1136 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8; | |
1137 get_pixels(s->block[4], ptr, wrap); | |
1138 | |
1139 wrap = s->linesize >> 1; | |
1140 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8; | |
1141 get_pixels(s->block[5], ptr, wrap); | |
1142 | |
1143 /* subtract previous frame if non intra */ | |
1144 if (!s->mb_intra) { | |
1145 int dxy, offset, mx, my; | |
295 | 1146 |
1147 if(s->mv_type==MV_TYPE_16X16){ | |
1148 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
1149 ptr = s->last_picture[0] + | |
1150 ((mb_y * 16 + (motion_y >> 1)) * s->linesize) + | |
1151 (mb_x * 16 + (motion_x >> 1)); | |
294 | 1152 |
295 | 1153 sub_pixels_2(s->block[0], ptr, s->linesize, dxy); |
1154 sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy); | |
1155 sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy); | |
1156 sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy); | |
294 | 1157 |
295 | 1158 if (s->out_format == FMT_H263) { |
1159 /* special rounding for h263 */ | |
1160 dxy = 0; | |
1161 if ((motion_x & 3) != 0) | |
1162 dxy |= 1; | |
1163 if ((motion_y & 3) != 0) | |
1164 dxy |= 2; | |
1165 mx = motion_x >> 2; | |
1166 my = motion_y >> 2; | |
1167 } else { | |
1168 mx = motion_x / 2; | |
1169 my = motion_y / 2; | |
1170 dxy = ((my & 1) << 1) | (mx & 1); | |
1171 mx >>= 1; | |
1172 my >>= 1; | |
1173 } | |
1174 offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx); | |
1175 ptr = s->last_picture[1] + offset; | |
1176 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy); | |
1177 ptr = s->last_picture[2] + offset; | |
1178 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy); | |
1179 }else{ | |
1180 int src_x, src_y; | |
1181 | |
1182 for(i=0;i<4;i++) { | |
1183 int motion_x = s->mv[0][i][0]; | |
1184 int motion_y = s->mv[0][i][1]; | |
1185 | |
1186 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
1187 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
1188 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8; | |
1189 | |
1190 ptr = s->last_picture[0] + (src_y * s->linesize) + (src_x); | |
1191 sub_pixels_2(s->block[i], ptr, s->linesize, dxy); | |
1192 } | |
1193 /* In case of 8X8, we construct a single chroma motion vector | |
1194 with a special rounding */ | |
1195 mx = 0; | |
1196 my = 0; | |
1197 for(i=0;i<4;i++) { | |
1198 mx += s->mv[0][i][0]; | |
1199 my += s->mv[0][i][1]; | |
1200 } | |
1201 if (mx >= 0) | |
1202 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
1203 else { | |
1204 mx = -mx; | |
1205 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
1206 } | |
1207 if (my >= 0) | |
1208 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
1209 else { | |
1210 my = -my; | |
1211 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
1212 } | |
294 | 1213 dxy = ((my & 1) << 1) | (mx & 1); |
1214 mx >>= 1; | |
1215 my >>= 1; | |
295 | 1216 |
1217 src_x = mb_x * 8 + mx; | |
1218 src_y = mb_y * 8 + my; | |
1219 src_x = clip(src_x, -8, s->width/2); | |
1220 if (src_x == s->width/2) | |
1221 dxy &= ~1; | |
1222 src_y = clip(src_y, -8, s->height/2); | |
1223 if (src_y == s->height/2) | |
1224 dxy &= ~2; | |
1225 | |
1226 offset = (src_y * (s->linesize >> 1)) + src_x; | |
1227 ptr = s->last_picture[1] + offset; | |
1228 sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy); | |
1229 ptr = s->last_picture[2] + offset; | |
1230 sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy); | |
294 | 1231 } |
1232 } | |
1233 | |
1234 #if 0 | |
1235 { | |
1236 float adap_parm; | |
1237 | |
1238 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) / | |
1239 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0); | |
1240 | |
1241 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", | |
1242 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', | |
1243 s->qscale, adap_parm, s->qscale*adap_parm, | |
1244 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); | |
1245 } | |
1246 #endif | |
1247 /* DCT & quantize */ | |
1248 if (s->h263_msmpeg4) { | |
1249 msmpeg4_dc_scale(s); | |
1250 } else if (s->h263_pred) { | |
1251 h263_dc_scale(s); | |
1252 } else { | |
1253 /* default quantization values */ | |
1254 s->y_dc_scale = 8; | |
1255 s->c_dc_scale = 8; | |
1256 } | |
1257 for(i=0;i<6;i++) { | |
1258 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale); | |
1259 } | |
1260 | |
1261 /* huffman encode */ | |
1262 switch(s->out_format) { | |
1263 case FMT_MPEG1: | |
1264 mpeg1_encode_mb(s, s->block, motion_x, motion_y); | |
1265 break; | |
1266 case FMT_H263: | |
1267 if (s->h263_msmpeg4) | |
1268 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); | |
1269 else if(s->h263_pred) | |
1270 mpeg4_encode_mb(s, s->block, motion_x, motion_y); | |
1271 else | |
1272 h263_encode_mb(s, s->block, motion_x, motion_y); | |
1273 break; | |
1274 case FMT_MJPEG: | |
1275 mjpeg_encode_mb(s, s->block); | |
1276 break; | |
1277 } | |
1278 } | |
1279 | |
1280 static void copy_bits(PutBitContext *pb, UINT8 *src, int length) | |
1281 { | |
1282 int bytes= length>>3; | |
1283 int bits= length&7; | |
1284 int i; | |
1285 | |
1286 for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]); | |
1287 put_bits(pb, bits, src[i]>>(8-bits)); | |
0 | 1288 } |
1289 | |
1290 static void encode_picture(MpegEncContext *s, int picture_number) | |
1291 { | |
294 | 1292 int mb_x, mb_y, last_gob, pdif = 0; |
1293 int i; | |
286 | 1294 int bits; |
294 | 1295 MpegEncContext best_s; |
1296 UINT8 bit_buf[4][3000]; //FIXME check that this is ALLWAYS large enogh for a MB | |
0 | 1297 |
1298 s->picture_number = picture_number; | |
268 | 1299 |
294 | 1300 s->block_wrap[0]= |
1301 s->block_wrap[1]= | |
1302 s->block_wrap[2]= | |
1303 s->block_wrap[3]= s->mb_width*2 + 2; | |
1304 s->block_wrap[4]= | |
1305 s->block_wrap[5]= s->mb_width + 2; | |
1306 | |
268 | 1307 s->last_mc_mb_var = s->mc_mb_var; |
1308 /* Reset the average MB variance */ | |
1309 s->avg_mb_var = 0; | |
1310 s->mc_mb_var = 0; | |
1311 /* Estimate motion for every MB */ | |
294 | 1312 if(s->pict_type == P_TYPE){ |
1313 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | |
1314 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; | |
1315 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
1316 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
1317 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
1318 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
1319 s->mb_x = mb_x; | |
1320 s->mb_y = mb_y; | |
1321 s->block_index[0]+=2; | |
1322 s->block_index[1]+=2; | |
1323 s->block_index[2]+=2; | |
1324 s->block_index[3]+=2; | |
268 | 1325 |
294 | 1326 /* compute motion vector & mb_type and store in context */ |
1327 estimate_motion(s, mb_x, mb_y); | |
1328 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER; | |
268 | 1329 } |
1330 } | |
294 | 1331 emms_c(); |
1332 }else{ | |
1333 /* I-Frame */ | |
1334 //FIXME do we need to zero them? | |
1335 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); | |
1336 memset(s->mv_table[0] , 0, sizeof(INT16)*s->mb_width*s->mb_height); | |
1337 memset(s->mv_table[1] , 0, sizeof(INT16)*s->mb_width*s->mb_height); | |
1338 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); | |
268 | 1339 } |
1340 | |
297 | 1341 if(s->avg_mb_var < s->mc_mb_var && s->pict_type != B_TYPE && (!s->force_type)){ //FIXME subtract MV bits |
271 | 1342 s->pict_type= I_TYPE; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1343 s->picture_in_gop_number=0; |
294 | 1344 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
1345 //printf("Scene change detected, encoding as I Frame\n"); | |
271 | 1346 } |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1347 |
287 | 1348 /* find best f_code for ME which do unlimited searches */ |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
1349 if(s->pict_type == P_TYPE && s->me_method >= 0){ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1350 int mv_num[8]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1351 int i; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1352 int loose=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1353 UINT8 * fcode_tab= s->fcode_tab; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1354 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1355 for(i=0; i<8; i++) mv_num[i]=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1356 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1357 for(i=0; i<s->mb_num; i++){ |
295 | 1358 if(s->mb_type[i] & MB_TYPE_INTER){ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1359 mv_num[ fcode_tab[s->mv_table[0][i] + MAX_MV] ]++; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1360 mv_num[ fcode_tab[s->mv_table[1][i] + MAX_MV] ]++; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1361 //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1362 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1363 //else printf("I"); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1364 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1365 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1366 for(i=MAX_FCODE; i>1; i--){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1367 loose+= mv_num[i]; |
286 | 1368 if(loose > 10) break; //FIXME this is pretty ineffective |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1369 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1370 s->f_code= i; |
287 | 1371 /* for(i=0; i<=MAX_FCODE; i++){ |
1372 printf("%d ", mv_num[i]); | |
1373 } | |
1374 printf("\n");*/ | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1375 }else{ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1376 s->f_code= 1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1377 } |
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
1378 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1379 //printf("f_code %d ///\n", s->f_code); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1380 /* convert MBs with too long MVs to I-Blocks */ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1381 if(s->pict_type==P_TYPE){ |
295 | 1382 int i, x, y; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1383 const int f_code= s->f_code; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1384 UINT8 * fcode_tab= s->fcode_tab; |
295 | 1385 //FIXME try to clip instead of intra izing ;) |
1386 /* clip / convert to intra 16x16 type MVs */ | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1387 for(i=0; i<s->mb_num; i++){ |
294 | 1388 if(s->mb_type[i]&MB_TYPE_INTER){ |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1389 if( fcode_tab[s->mv_table[0][i] + MAX_MV] > f_code |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1390 || fcode_tab[s->mv_table[0][i] + MAX_MV] == 0 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1391 || fcode_tab[s->mv_table[1][i] + MAX_MV] > f_code |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1392 || fcode_tab[s->mv_table[1][i] + MAX_MV] == 0 ){ |
294 | 1393 s->mb_type[i] &= ~MB_TYPE_INTER; |
1394 s->mb_type[i] |= MB_TYPE_INTRA; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1395 s->mv_table[0][i] = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1396 s->mv_table[1][i] = 0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1397 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1398 } |
295 | 1399 } |
1400 | |
1401 if(s->flags&CODEC_FLAG_4MV){ | |
1402 int wrap= 2+ s->mb_width*2; | |
1403 | |
1404 /* clip / convert to intra 8x8 type MVs */ | |
1405 for(y=0; y<s->mb_height; y++){ | |
1406 int xy= (y*2 + 1)*wrap + 1; | |
1407 i= y*s->mb_width; | |
1408 | |
1409 for(x=0; x<s->mb_width; x++){ | |
1410 if(s->mb_type[i]&MB_TYPE_INTER4V){ | |
1411 int block; | |
1412 for(block=0; block<4; block++){ | |
1413 int off= (block& 1) + (block>>1)*wrap; | |
1414 int mx= s->motion_val[ xy + off ][0]; | |
1415 int my= s->motion_val[ xy + off ][1]; | |
1416 | |
1417 if( fcode_tab[mx + MAX_MV] > f_code | |
1418 || fcode_tab[mx + MAX_MV] == 0 | |
1419 || fcode_tab[my + MAX_MV] > f_code | |
1420 || fcode_tab[my + MAX_MV] == 0 ){ | |
1421 s->mb_type[i] &= ~MB_TYPE_INTER4V; | |
1422 s->mb_type[i] |= MB_TYPE_INTRA; | |
1423 } | |
1424 } | |
1425 xy+=2; | |
1426 i++; | |
1427 } | |
1428 } | |
294 | 1429 } |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1430 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1431 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
1432 |
271 | 1433 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); |
1434 | |
0 | 1435 if (!s->fixed_qscale) |
1436 s->qscale = rate_estimate_qscale(s); | |
1437 | |
1438 /* precompute matrix */ | |
1439 if (s->out_format == FMT_MJPEG) { | |
1440 /* for mjpeg, we do include qscale in the matrix */ | |
1441 s->intra_matrix[0] = default_intra_matrix[0]; | |
1442 for(i=1;i<64;i++) | |
1443 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3; | |
220 | 1444 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8); |
0 | 1445 } else { |
220 | 1446 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale); |
1447 convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale); | |
0 | 1448 } |
1449 | |
286 | 1450 s->last_bits= get_bit_count(&s->pb); |
0 | 1451 switch(s->out_format) { |
1452 case FMT_MJPEG: | |
1453 mjpeg_picture_header(s); | |
1454 break; | |
1455 case FMT_H263: | |
1456 if (s->h263_msmpeg4) | |
1457 msmpeg4_encode_picture_header(s, picture_number); | |
1458 else if (s->h263_pred) | |
1459 mpeg4_encode_picture_header(s, picture_number); | |
1460 else if (s->h263_rv10) | |
1461 rv10_encode_picture_header(s, picture_number); | |
1462 else | |
1463 h263_encode_picture_header(s, picture_number); | |
1464 break; | |
1465 case FMT_MPEG1: | |
1466 mpeg1_encode_picture_header(s, picture_number); | |
1467 break; | |
1468 } | |
286 | 1469 bits= get_bit_count(&s->pb); |
1470 s->header_bits= bits - s->last_bits; | |
1471 s->last_bits= bits; | |
1472 s->mv_bits=0; | |
1473 s->misc_bits=0; | |
1474 s->i_tex_bits=0; | |
1475 s->p_tex_bits=0; | |
1476 s->i_count=0; | |
1477 s->p_count=0; | |
1478 s->skip_count=0; | |
1479 | |
0 | 1480 /* init last dc values */ |
1481 /* note: quant matrix value (8) is implied here */ | |
1482 s->last_dc[0] = 128; | |
1483 s->last_dc[1] = 128; | |
1484 s->last_dc[2] = 128; | |
1485 s->mb_incr = 1; | |
1486 s->last_mv[0][0][0] = 0; | |
1487 s->last_mv[0][0][1] = 0; | |
1488 | |
162 | 1489 /* Get the GOB height based on picture height */ |
231 | 1490 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) { |
162 | 1491 if (s->height <= 400) |
1492 s->gob_index = 1; | |
1493 else if (s->height <= 800) | |
1494 s->gob_index = 2; | |
1495 else | |
1496 s->gob_index = 4; | |
1497 } | |
268 | 1498 |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1499 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
|
1500 |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1501 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
|
1502 /* 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
|
1503 /* 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
|
1504 if (s->rtp_mode) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1505 if (!mb_y) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1506 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
|
1507 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
|
1508 } 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
|
1509 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
|
1510 if (last_gob) { |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1511 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
|
1512 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1513 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1514 } |
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
1515 |
266 | 1516 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
1517 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
1518 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
1519 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
1520 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
1521 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1522 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
294 | 1523 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; |
1524 PutBitContext pb; | |
1525 int d; | |
1526 int dmin=10000000; | |
1527 int best=0; | |
0 | 1528 |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1529 s->mb_x = mb_x; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1530 s->mb_y = mb_y; |
266 | 1531 s->block_index[0]+=2; |
1532 s->block_index[1]+=2; | |
1533 s->block_index[2]+=2; | |
1534 s->block_index[3]+=2; | |
1535 s->block_index[4]++; | |
1536 s->block_index[5]++; | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
1537 |
294 | 1538 s->mv_dir = MV_DIR_FORWARD; |
1539 if(mb_type & (mb_type-1)){ // more than 1 MB type possible | |
1540 pb= s->pb; | |
1541 if(mb_type&MB_TYPE_INTER){ | |
295 | 1542 s->mv_type = MV_TYPE_16X16; |
294 | 1543 s->mb_intra= 0; |
1544 s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x]; | |
1545 s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x]; | |
1546 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL); | |
1547 s->block= s->inter_block; | |
0 | 1548 |
294 | 1549 encode_mb(s); |
1550 d= get_bit_count(&s->pb); | |
1551 if(d<dmin){ | |
1552 flush_put_bits(&s->pb); | |
1553 dmin=d; | |
1554 best_s.mv[0][0][0]= s->mv[0][0][0]; | |
1555 best_s.mv[0][0][1]= s->mv[0][0][1]; | |
1556 best_s.mb_intra= 0; | |
295 | 1557 best_s.mv_type = MV_TYPE_16X16; |
294 | 1558 best_s.pb=s->pb; |
1559 best_s.block= s->block; | |
1560 best=1; | |
1561 for(i=0; i<6; i++) | |
1562 best_s.block_last_index[i]= s->block_last_index[i]; | |
1563 } | |
0 | 1564 } |
295 | 1565 if(mb_type&MB_TYPE_INTER4V){ |
1566 s->mv_type = MV_TYPE_8X8; | |
1567 s->mb_intra= 0; | |
1568 for(i=0; i<4; i++){ | |
1569 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
1570 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
1571 } | |
1572 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL); | |
1573 s->block= s->inter4v_block; | |
1574 | |
1575 encode_mb(s); | |
1576 d= get_bit_count(&s->pb); | |
1577 if(d<dmin){ | |
1578 flush_put_bits(&s->pb); | |
1579 dmin=d; | |
1580 for(i=0; i<4; i++){ | |
1581 best_s.mv[0][i][0] = s->mv[0][i][0]; | |
1582 best_s.mv[0][i][1] = s->mv[0][i][1]; | |
1583 } | |
1584 best_s.mb_intra= 0; | |
1585 best_s.mv_type = MV_TYPE_8X8; | |
1586 best_s.pb=s->pb; | |
1587 best_s.block= s->block; | |
1588 best=2; | |
1589 for(i=0; i<6; i++) | |
1590 best_s.block_last_index[i]= s->block_last_index[i]; | |
1591 } | |
1592 } | |
294 | 1593 if(mb_type&MB_TYPE_INTRA){ |
295 | 1594 s->mv_type = MV_TYPE_16X16; |
294 | 1595 s->mb_intra= 1; |
1596 s->mv[0][0][0] = 0; | |
1597 s->mv[0][0][1] = 0; | |
1598 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL); | |
1599 s->block= s->intra_block; | |
1600 | |
1601 encode_mb(s); | |
1602 d= get_bit_count(&s->pb); | |
1603 if(d<dmin){ | |
1604 flush_put_bits(&s->pb); | |
1605 dmin=d; | |
1606 best_s.mv[0][0][0]= 0; | |
1607 best_s.mv[0][0][1]= 0; | |
1608 best_s.mb_intra= 1; | |
295 | 1609 best_s.mv_type = MV_TYPE_16X16; |
294 | 1610 best_s.pb=s->pb; |
1611 best_s.block= s->block; | |
1612 for(i=0; i<6; i++) | |
1613 best_s.block_last_index[i]= s->block_last_index[i]; | |
1614 best=0; | |
1615 } | |
1616 /* force cleaning of ac/dc if needed ... */ | |
1617 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; | |
1618 } | |
295 | 1619 for(i=0; i<4; i++){ |
1620 s->mv[0][i][0] = best_s.mv[0][i][0]; | |
1621 s->mv[0][i][1] = best_s.mv[0][i][1]; | |
1622 } | |
294 | 1623 s->mb_intra= best_s.mb_intra; |
295 | 1624 s->mv_type= best_s.mv_type; |
294 | 1625 for(i=0; i<6; i++) |
1626 s->block_last_index[i]= best_s.block_last_index[i]; | |
1627 copy_bits(&pb, bit_buf[best], dmin); | |
1628 s->block= best_s.block; | |
1629 s->pb= pb; | |
1630 } else { | |
1631 // only one MB-Type possible | |
1632 if(mb_type&MB_TYPE_INTRA){ | |
1633 s->mb_intra= 1; | |
1634 s->mv[0][0][0] = 0; | |
1635 s->mv[0][0][1] = 0; | |
1636 }else{ | |
1637 s->mb_intra= 0; | |
1638 s->mv[0][0][0] = s->mv_table[0][mb_y * s->mb_width + mb_x]; | |
1639 s->mv[0][0][1] = s->mv_table[1][mb_y * s->mb_width + mb_x]; | |
1640 } | |
1641 encode_mb(s); | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1642 } |
0 | 1643 |
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
|
1644 MPV_decode_mb(s, s->block); |
0 | 1645 } |
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
|
1646 |
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
|
1647 |
231 | 1648 /* Obtain average GOB size for RTP */ |
1649 if (s->rtp_mode) { | |
1650 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
|
1651 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line; |
231 | 1652 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
|
1653 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
|
1654 s->ptr_last_mb_line = pbBufPtr(&s->pb); |
231 | 1655 } |
1656 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, | |
1657 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); | |
1658 s->first_gob_line = 0; | |
1659 } | |
0 | 1660 } |
294 | 1661 emms_c(); |
286 | 1662 |
311 | 1663 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE) |
208 | 1664 msmpeg4_encode_ext_header(s); |
1665 | |
162 | 1666 //if (s->gob_number) |
1667 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | |
231 | 1668 |
1669 /* Send the last GOB if RTP */ | |
1670 if (s->rtp_mode) { | |
1671 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
|
1672 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
231 | 1673 /* Call the RTP callback to send the last GOB */ |
1674 if (s->rtp_callback) | |
1675 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
|
1676 s->ptr_lastgob = pbBufPtr(&s->pb); |
231 | 1677 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif); |
1678 } | |
0 | 1679 } |
1680 | |
220 | 1681 static int dct_quantize_c(MpegEncContext *s, |
0 | 1682 DCTELEM *block, int n, |
1683 int qscale) | |
1684 { | |
1685 int i, j, level, last_non_zero, q; | |
1686 const int *qmat; | |
216 | 1687 int minLevel, maxLevel; |
1688 | |
1689 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){ | |
1690 /* mpeg4 */ | |
1691 minLevel= -2048; | |
1692 maxLevel= 2047; | |
1693 }else if(s->out_format==FMT_MPEG1){ | |
1694 /* mpeg1 */ | |
1695 minLevel= -255; | |
1696 maxLevel= 255; | |
230 | 1697 }else if(s->out_format==FMT_MJPEG){ |
1698 /* (m)jpeg */ | |
1699 minLevel= -1023; | |
1700 maxLevel= 1023; | |
216 | 1701 }else{ |
1702 /* h263 / msmpeg4 */ | |
1703 minLevel= -128; | |
1704 maxLevel= 127; | |
1705 } | |
0 | 1706 |
1707 av_fdct (block); | |
1708 | |
64 | 1709 /* we need this permutation so that we correct the IDCT |
1710 permutation. will be moved into DCT code */ | |
1711 block_permute(block); | |
1712 | |
0 | 1713 if (s->mb_intra) { |
1714 if (n < 4) | |
1715 q = s->y_dc_scale; | |
1716 else | |
1717 q = s->c_dc_scale; | |
1718 q = q << 3; | |
1719 | |
1720 /* note: block[0] is assumed to be positive */ | |
1721 block[0] = (block[0] + (q >> 1)) / q; | |
1722 i = 1; | |
1723 last_non_zero = 0; | |
1724 if (s->out_format == FMT_H263) { | |
1725 qmat = s->q_non_intra_matrix; | |
1726 } else { | |
1727 qmat = s->q_intra_matrix; | |
1728 } | |
1729 } else { | |
1730 i = 0; | |
1731 last_non_zero = -1; | |
1732 qmat = s->q_non_intra_matrix; | |
1733 } | |
1734 | |
1735 for(;i<64;i++) { | |
1736 j = zigzag_direct[i]; | |
1737 level = block[j]; | |
1738 level = level * qmat[j]; | |
1739 #ifdef PARANOID | |
1740 { | |
1741 static int count = 0; | |
1742 int level1, level2, qmat1; | |
1743 double val; | |
1744 if (qmat == s->q_non_intra_matrix) { | |
1745 qmat1 = default_non_intra_matrix[j] * s->qscale; | |
1746 } else { | |
1747 qmat1 = default_intra_matrix[j] * s->qscale; | |
1748 } | |
1749 if (av_fdct != jpeg_fdct_ifast) | |
1750 val = ((double)block[j] * 8.0) / (double)qmat1; | |
1751 else | |
1752 val = ((double)block[j] * 8.0 * 2048.0) / | |
1753 ((double)qmat1 * aanscales[j]); | |
1754 level1 = (int)val; | |
1755 level2 = level / (1 << (QMAT_SHIFT - 3)); | |
1756 if (level1 != level2) { | |
1757 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n", | |
1758 count, level2, level1, block[j], qmat1, qmat[j], | |
1759 val); | |
1760 count++; | |
1761 } | |
1762 | |
1763 } | |
1764 #endif | |
1765 /* XXX: slight error for the low range. Test should be equivalent to | |
1766 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 << | |
1767 (QMAT_SHIFT - 3))) | |
1768 */ | |
1769 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) != | |
1770 level) { | |
1771 level = level / (1 << (QMAT_SHIFT - 3)); | |
1772 /* XXX: currently, this code is not optimal. the range should be: | |
1773 mpeg1: -255..255 | |
1774 mpeg2: -2048..2047 | |
1775 h263: -128..127 | |
1776 mpeg4: -2048..2047 | |
1777 */ | |
216 | 1778 if (level > maxLevel) |
1779 level = maxLevel; | |
1780 else if (level < minLevel) | |
1781 level = minLevel; | |
0 | 1782 |
1783 block[j] = level; | |
1784 last_non_zero = i; | |
1785 } else { | |
1786 block[j] = 0; | |
1787 } | |
1788 } | |
1789 return last_non_zero; | |
1790 } | |
1791 | |
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
|
1792 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
|
1793 DCTELEM *block, int n, int qscale) |
0 | 1794 { |
200 | 1795 int i, level, nCoeffs; |
0 | 1796 const UINT16 *quant_matrix; |
1797 | |
200 | 1798 if(s->alternate_scan) nCoeffs= 64; |
1799 else nCoeffs= s->block_last_index[n]+1; | |
1800 | |
0 | 1801 if (s->mb_intra) { |
1802 if (n < 4) | |
1803 block[0] = block[0] * s->y_dc_scale; | |
1804 else | |
1805 block[0] = block[0] * s->c_dc_scale; | |
1806 /* XXX: only mpeg1 */ | |
1807 quant_matrix = s->intra_matrix; | |
200 | 1808 for(i=1;i<nCoeffs;i++) { |
1809 int j= zigzag_direct[i]; | |
1810 level = block[j]; | |
0 | 1811 if (level) { |
1812 if (level < 0) { | |
1813 level = -level; | |
200 | 1814 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 1815 level = (level - 1) | 1; |
1816 level = -level; | |
1817 } else { | |
200 | 1818 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 1819 level = (level - 1) | 1; |
1820 } | |
1821 #ifdef PARANOID | |
1822 if (level < -2048 || level > 2047) | |
1823 fprintf(stderr, "unquant error %d %d\n", i, level); | |
1824 #endif | |
200 | 1825 block[j] = level; |
0 | 1826 } |
1827 } | |
1828 } else { | |
1829 i = 0; | |
1830 quant_matrix = s->non_intra_matrix; | |
217 | 1831 for(;i<nCoeffs;i++) { |
200 | 1832 int j= zigzag_direct[i]; |
1833 level = block[j]; | |
0 | 1834 if (level) { |
1835 if (level < 0) { | |
1836 level = -level; | |
1837 level = (((level << 1) + 1) * qscale * | |
200 | 1838 ((int) (quant_matrix[j]))) >> 4; |
0 | 1839 level = (level - 1) | 1; |
1840 level = -level; | |
1841 } else { | |
1842 level = (((level << 1) + 1) * qscale * | |
200 | 1843 ((int) (quant_matrix[j]))) >> 4; |
0 | 1844 level = (level - 1) | 1; |
1845 } | |
1846 #ifdef PARANOID | |
1847 if (level < -2048 || level > 2047) | |
1848 fprintf(stderr, "unquant error %d %d\n", i, level); | |
1849 #endif | |
200 | 1850 block[j] = level; |
0 | 1851 } |
1852 } | |
1853 } | |
1854 } | |
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
|
1855 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1856 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
|
1857 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
|
1858 { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1859 int i, level, qmul, qadd; |
200 | 1860 int nCoeffs; |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1861 |
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
|
1862 if (s->mb_intra) { |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1863 if (!s->h263_aic) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1864 if (n < 4) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1865 block[0] = block[0] * s->y_dc_scale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1866 else |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1867 block[0] = block[0] * s->c_dc_scale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1868 } |
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
|
1869 i = 1; |
200 | 1870 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
|
1871 } 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
|
1872 i = 0; |
200 | 1873 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
|
1874 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1875 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1876 qmul = s->qscale << 1; |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1877 if (s->h263_aic && s->mb_intra) |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1878 qadd = 0; |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1879 else |
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
1880 qadd = (s->qscale - 1) | 1; |
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
|
1881 |
200 | 1882 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
|
1883 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
|
1884 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
|
1885 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
|
1886 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
|
1887 } 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
|
1888 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
|
1889 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1890 #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
|
1891 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
|
1892 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
|
1893 #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
|
1894 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
|
1895 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1896 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
1897 } |
0 | 1898 |
1899 /* rate control */ | |
1900 | |
1901 /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */ | |
1902 #define I_FRAME_SIZE_RATIO 3.0 | |
1903 #define QSCALE_K 20 | |
1904 | |
1905 static void rate_control_init(MpegEncContext *s) | |
1906 { | |
268 | 1907 #if 1 |
1908 emms_c(); | |
1909 | |
1910 //initial values, they dont really matter as they will be totally different within a few frames | |
1911 s->i_pred.coeff= s->p_pred.coeff= 7.0; | |
1912 s->i_pred.count= s->p_pred.count= 1.0; | |
1913 | |
1914 s->i_pred.decay= s->p_pred.decay= 0.4; | |
1915 | |
1916 // use more bits at the beginning, otherwise high motion at the begin will look like shit | |
1917 s->qsum=100; | |
1918 s->qcount=100; | |
1919 | |
1920 s->short_term_qsum=0.001; | |
1921 s->short_term_qcount=0.001; | |
1922 #else | |
0 | 1923 s->wanted_bits = 0; |
1924 | |
1925 if (s->intra_only) { | |
1926 s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate; | |
1927 s->P_frame_bits = s->I_frame_bits; | |
1928 } else { | |
1929 s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) / | |
1930 (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1))); | |
1931 s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO); | |
1932 } | |
268 | 1933 |
0 | 1934 #if defined(DEBUG) |
1935 printf("I_frame_size=%d P_frame_size=%d\n", | |
1936 s->I_frame_bits, s->P_frame_bits); | |
1937 #endif | |
268 | 1938 #endif |
1939 } | |
1940 | |
1941 static double predict(Predictor *p, double q, double var) | |
1942 { | |
1943 return p->coeff*var / (q*p->count); | |
1944 } | |
1945 | |
1946 static void update_predictor(Predictor *p, double q, double var, double size) | |
1947 { | |
1948 double new_coeff= size*q / (var + 1); | |
1949 if(var<1000) return; | |
1950 /*{ | |
1951 int pred= predict(p, q, var); | |
1952 int error= abs(pred-size); | |
1953 static double sum=0; | |
1954 static int count=0; | |
1955 if(count>5) sum+=error; | |
1956 count++; | |
1957 if(256*256*256*64%count==0){ | |
1958 printf("%d %f %f\n", count, sum/count, p->coeff); | |
1959 } | |
1960 }*/ | |
1961 p->count*= p->decay; | |
1962 p->coeff*= p->decay; | |
1963 p->count++; | |
1964 p->coeff+= new_coeff; | |
0 | 1965 } |
1966 | |
1967 static int rate_estimate_qscale(MpegEncContext *s) | |
1968 { | |
268 | 1969 #if 1 |
1970 int qmin= s->qmin; | |
1971 int qmax= s->qmax; | |
1972 int rate_q=5; | |
1973 float q; | |
1974 int qscale; | |
1975 float br_compensation; | |
1976 double diff; | |
1977 double short_term_q; | |
1978 double long_term_q; | |
1979 int last_qscale= s->qscale; | |
1980 double fps; | |
1981 INT64 wanted_bits; | |
1982 emms_c(); | |
297 | 1983 |
268 | 1984 fps= (double)s->frame_rate / FRAME_RATE_BASE; |
1985 wanted_bits= s->bit_rate*(double)s->picture_number/fps; | |
1986 | |
1987 | |
1988 if(s->picture_number>2){ | |
1989 /* update predictors */ | |
1990 if(s->last_pict_type == I_TYPE){ | |
1991 //FIXME | |
1992 }else{ //P Frame | |
1993 //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff); | |
1994 update_predictor(&s->p_pred, s->qscale, s->last_mc_mb_var, s->frame_bits); | |
1995 } | |
1996 } | |
1997 | |
1998 if(s->pict_type == I_TYPE){ | |
1999 //FIXME | |
2000 rate_q= s->qsum/s->qcount; | |
2001 }else{ //P Frame | |
2002 int i; | |
2003 int diff, best_diff=1000000000; | |
2004 for(i=1; i<=31; i++){ | |
2005 diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps; | |
2006 if(diff<0) diff= -diff; | |
2007 if(diff<best_diff){ | |
2008 best_diff= diff; | |
2009 rate_q= i; | |
2010 } | |
2011 } | |
2012 } | |
2013 | |
2014 s->short_term_qsum*=s->qblur; | |
2015 s->short_term_qcount*=s->qblur; | |
2016 | |
2017 s->short_term_qsum+= rate_q; | |
2018 s->short_term_qcount++; | |
2019 short_term_q= s->short_term_qsum/s->short_term_qcount; | |
2020 | |
302 | 2021 long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0 |
268 | 2022 |
2023 // q= (long_term_q - short_term_q)*s->qcompress + short_term_q; | |
2024 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q); | |
2025 | |
2026 diff= s->total_bits - wanted_bits; | |
2027 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance; | |
270 | 2028 if(br_compensation<=0.0) br_compensation=0.001; |
268 | 2029 q/=br_compensation; |
2030 | |
2031 qscale= (int)(q + 0.5); | |
2032 if (qscale<qmin) qscale=qmin; | |
2033 else if(qscale>qmax) qscale=qmax; | |
2034 | |
2035 if (qscale<last_qscale-s->max_qdiff) qscale=last_qscale-s->max_qdiff; | |
2036 else if(qscale>last_qscale+s->max_qdiff) qscale=last_qscale+s->max_qdiff; | |
2037 | |
2038 s->qsum+= qscale; | |
2039 s->qcount++; | |
2040 | |
2041 s->last_pict_type= s->pict_type; | |
302 | 2042 //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation, |
2043 // rate_q, short_term_q, s->mc_mb_var, s->frame_bits); | |
268 | 2044 //printf("%d %d\n", s->bit_rate, (int)fps); |
2045 return qscale; | |
2046 #else | |
187
3f3b14d3a23d
qscale estimate fix, diff extended to 64bit wide. patch by Stephen Davies <steve@daviesfam.org>
arpi_esp
parents:
178
diff
changeset
|
2047 INT64 diff, total_bits = s->total_bits; |
0 | 2048 float q; |
268 | 2049 int qscale; |
0 | 2050 if (s->pict_type == I_TYPE) { |
2051 s->wanted_bits += s->I_frame_bits; | |
2052 } else { | |
2053 s->wanted_bits += s->P_frame_bits; | |
2054 } | |
2055 diff = s->wanted_bits - total_bits; | |
2056 q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width); | |
2057 /* adjust for I frame */ | |
2058 if (s->pict_type == I_TYPE && !s->intra_only) { | |
2059 q /= I_FRAME_SIZE_RATIO; | |
2060 } | |
2061 | |
2062 /* using a too small Q scale leeds to problems in mpeg1 and h263 | |
2063 because AC coefficients are clamped to 255 or 127 */ | |
2064 qmin = 3; | |
2065 if (q < qmin) | |
2066 q = qmin; | |
2067 else if (q > 31) | |
2068 q = 31; | |
2069 qscale = (int)(q + 0.5); | |
2070 #if defined(DEBUG) | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2071 printf("\n%d: total=%0.0f wanted=%0.0f br=%0.1f diff=%d qest=%2.1f\n", |
0 | 2072 s->picture_number, |
64 | 2073 (double)total_bits, |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2074 (double)s->wanted_bits, |
0 | 2075 (float)s->frame_rate / FRAME_RATE_BASE * |
2076 total_bits / s->picture_number, | |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2077 (int)diff, q); |
0 | 2078 #endif |
2079 return qscale; | |
268 | 2080 #endif |
0 | 2081 } |
2082 | |
2083 AVCodec mpeg1video_encoder = { | |
2084 "mpeg1video", | |
2085 CODEC_TYPE_VIDEO, | |
2086 CODEC_ID_MPEG1VIDEO, | |
2087 sizeof(MpegEncContext), | |
2088 MPV_encode_init, | |
2089 MPV_encode_picture, | |
2090 MPV_encode_end, | |
2091 }; | |
2092 | |
2093 AVCodec h263_encoder = { | |
2094 "h263", | |
2095 CODEC_TYPE_VIDEO, | |
2096 CODEC_ID_H263, | |
2097 sizeof(MpegEncContext), | |
2098 MPV_encode_init, | |
2099 MPV_encode_picture, | |
2100 MPV_encode_end, | |
2101 }; | |
2102 | |
2103 AVCodec h263p_encoder = { | |
2104 "h263p", | |
2105 CODEC_TYPE_VIDEO, | |
2106 CODEC_ID_H263P, | |
2107 sizeof(MpegEncContext), | |
2108 MPV_encode_init, | |
2109 MPV_encode_picture, | |
2110 MPV_encode_end, | |
2111 }; | |
2112 | |
2113 AVCodec rv10_encoder = { | |
2114 "rv10", | |
2115 CODEC_TYPE_VIDEO, | |
2116 CODEC_ID_RV10, | |
2117 sizeof(MpegEncContext), | |
2118 MPV_encode_init, | |
2119 MPV_encode_picture, | |
2120 MPV_encode_end, | |
2121 }; | |
2122 | |
2123 AVCodec mjpeg_encoder = { | |
2124 "mjpeg", | |
2125 CODEC_TYPE_VIDEO, | |
2126 CODEC_ID_MJPEG, | |
2127 sizeof(MpegEncContext), | |
2128 MPV_encode_init, | |
2129 MPV_encode_picture, | |
2130 MPV_encode_end, | |
2131 }; | |
2132 | |
71 | 2133 AVCodec mpeg4_encoder = { |
2134 "mpeg4", | |
0 | 2135 CODEC_TYPE_VIDEO, |
71 | 2136 CODEC_ID_MPEG4, |
0 | 2137 sizeof(MpegEncContext), |
2138 MPV_encode_init, | |
2139 MPV_encode_picture, | |
2140 MPV_encode_end, | |
2141 }; | |
2142 | |
307 | 2143 AVCodec msmpeg4v1_encoder = { |
2144 "msmpeg4v1", | |
0 | 2145 CODEC_TYPE_VIDEO, |
307 | 2146 CODEC_ID_MSMPEG4V1, |
0 | 2147 sizeof(MpegEncContext), |
2148 MPV_encode_init, | |
2149 MPV_encode_picture, | |
2150 MPV_encode_end, | |
2151 }; | |
307 | 2152 |
2153 AVCodec msmpeg4v2_encoder = { | |
2154 "msmpeg4v2", | |
2155 CODEC_TYPE_VIDEO, | |
2156 CODEC_ID_MSMPEG4V2, | |
2157 sizeof(MpegEncContext), | |
2158 MPV_encode_init, | |
2159 MPV_encode_picture, | |
2160 MPV_encode_end, | |
2161 }; | |
2162 | |
2163 AVCodec msmpeg4v3_encoder = { | |
2164 "msmpeg4", | |
2165 CODEC_TYPE_VIDEO, | |
2166 CODEC_ID_MSMPEG4V3, | |
2167 sizeof(MpegEncContext), | |
2168 MPV_encode_init, | |
2169 MPV_encode_picture, | |
2170 MPV_encode_end, | |
2171 }; |