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