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