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