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