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