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