Mercurial > libavcodec.hg
comparison mpegvideo.c @ 456:c006ee838856 libavcodec
single coefficient elimination
prequantization
more readable malloc & check if NULL
error concealment / error resilience
b_quant_offset (unfinished, should be 0 for now)
data partitioning
author | michaelni |
---|---|
date | Sun, 02 Jun 2002 12:20:39 +0000 |
parents | 718a22dc121f |
children | 3a7486d90b96 |
comparison
equal
deleted
inserted
replaced
455:7f2d2be2aee9 | 456:c006ee838856 |
---|---|
107 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]); | 107 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]); |
108 } | 108 } |
109 } | 109 } |
110 } | 110 } |
111 } | 111 } |
112 // move into common.c perhaps | |
113 #define CHECKED_ALLOCZ(p, size)\ | |
114 {\ | |
115 p= av_mallocz(size);\ | |
116 if(p==NULL){\ | |
117 perror("malloc");\ | |
118 goto fail;\ | |
119 }\ | |
120 } | |
112 | 121 |
113 /* init common structure for both encoder and decoder */ | 122 /* init common structure for both encoder and decoder */ |
114 int MPV_common_init(MpegEncContext *s) | 123 int MPV_common_init(MpegEncContext *s) |
115 { | 124 { |
116 int c_size, i; | 125 int c_size, i; |
141 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | 150 h = s->mb_height * 16 + 2 * EDGE_WIDTH; |
142 shift = (i == 0) ? 0 : 1; | 151 shift = (i == 0) ? 0 : 1; |
143 c_size = (w >> shift) * (h >> shift); | 152 c_size = (w >> shift) * (h >> shift); |
144 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | 153 pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); |
145 | 154 |
146 pict = av_mallocz(c_size); | 155 CHECKED_ALLOCZ(pict, c_size) |
147 if (pict == NULL) | |
148 goto fail; | |
149 s->last_picture_base[i] = pict; | 156 s->last_picture_base[i] = pict; |
150 s->last_picture[i] = pict + pict_start; | 157 s->last_picture[i] = pict + pict_start; |
151 | 158 |
152 pict = av_mallocz(c_size); | 159 CHECKED_ALLOCZ(pict, c_size) |
153 if (pict == NULL) | |
154 goto fail; | |
155 s->next_picture_base[i] = pict; | 160 s->next_picture_base[i] = pict; |
156 s->next_picture[i] = pict + pict_start; | 161 s->next_picture[i] = pict + pict_start; |
157 | 162 |
158 if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) { | 163 if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) { |
159 /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but | 164 /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but |
160 do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */ | 165 do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */ |
161 pict = av_mallocz(c_size); | 166 CHECKED_ALLOCZ(pict, c_size) |
162 if (pict == NULL) | |
163 goto fail; | |
164 s->aux_picture_base[i] = pict; | 167 s->aux_picture_base[i] = pict; |
165 s->aux_picture[i] = pict + pict_start; | 168 s->aux_picture[i] = pict + pict_start; |
166 } | 169 } |
167 } | 170 } |
168 | 171 |
169 if (s->encoding) { | 172 if (s->encoding) { |
170 int j; | 173 int j; |
171 int mv_table_size= (s->mb_width+2)*(s->mb_height+2); | 174 int mv_table_size= (s->mb_width+2)*(s->mb_height+2); |
172 | |
173 /* Allocate MB type table */ | |
174 s->mb_type = av_mallocz(s->mb_num * sizeof(char)); | |
175 if (s->mb_type == NULL) { | |
176 perror("malloc"); | |
177 goto fail; | |
178 } | |
179 | 175 |
180 s->mb_var = av_mallocz(s->mb_num * sizeof(INT16)); | 176 CHECKED_ALLOCZ(s->mb_var , s->mb_num * sizeof(INT16)) |
181 if (s->mb_var == NULL) { | 177 CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16)) |
182 perror("malloc"); | |
183 goto fail; | |
184 } | |
185 | 178 |
186 /* Allocate MV tables */ | 179 /* Allocate MV tables */ |
187 s->p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | 180 CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16)) |
188 if (s->p_mv_table == NULL) { | 181 CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) |
189 perror("malloc"); | 182 CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16)) |
190 goto fail; | 183 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) |
191 } | 184 CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16)) |
192 s->last_p_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | 185 CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16)) |
193 if (s->last_p_mv_table == NULL) { | 186 CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16)) |
194 perror("malloc"); | 187 CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16)) |
195 goto fail; | 188 |
196 } | 189 CHECKED_ALLOCZ(s->me_scratchpad, s->linesize*16*3*sizeof(uint8_t)) |
197 s->b_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | 190 |
198 if (s->b_forw_mv_table == NULL) { | 191 CHECKED_ALLOCZ(s->me_map , ME_MAP_SIZE*sizeof(uint32_t)) |
199 perror("malloc"); | 192 CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t)) |
200 goto fail; | |
201 } | |
202 s->b_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | |
203 if (s->b_back_mv_table == NULL) { | |
204 perror("malloc"); | |
205 goto fail; | |
206 } | |
207 s->b_bidir_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | |
208 if (s->b_bidir_forw_mv_table == NULL) { | |
209 perror("malloc"); | |
210 goto fail; | |
211 } | |
212 s->b_bidir_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | |
213 if (s->b_bidir_back_mv_table == NULL) { | |
214 perror("malloc"); | |
215 goto fail; | |
216 } | |
217 s->b_direct_forw_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | |
218 if (s->b_direct_forw_mv_table == NULL) { | |
219 perror("malloc"); | |
220 goto fail; | |
221 } | |
222 s->b_direct_back_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | |
223 if (s->b_direct_back_mv_table == NULL) { | |
224 perror("malloc"); | |
225 goto fail; | |
226 } | |
227 s->b_direct_mv_table = av_mallocz(mv_table_size * 2 * sizeof(INT16)); | |
228 if (s->b_direct_mv_table == NULL) { | |
229 perror("malloc"); | |
230 goto fail; | |
231 } | |
232 | |
233 s->me_scratchpad = av_mallocz( s->linesize*16*3*sizeof(uint8_t)); | |
234 if (s->me_scratchpad == NULL) { | |
235 perror("malloc"); | |
236 goto fail; | |
237 } | |
238 | 193 |
239 if(s->max_b_frames){ | 194 if(s->max_b_frames){ |
240 for(j=0; j<REORDER_BUFFER_SIZE; j++){ | 195 for(j=0; j<REORDER_BUFFER_SIZE; j++){ |
241 int i; | 196 int i; |
242 for(i=0;i<3;i++) { | 197 for(i=0;i<3;i++) { |
245 w = s->linesize; | 200 w = s->linesize; |
246 h = s->mb_height * 16; | 201 h = s->mb_height * 16; |
247 shift = (i == 0) ? 0 : 1; | 202 shift = (i == 0) ? 0 : 1; |
248 c_size = (w >> shift) * (h >> shift); | 203 c_size = (w >> shift) * (h >> shift); |
249 | 204 |
250 pict = av_mallocz(c_size); | 205 CHECKED_ALLOCZ(pict, c_size); |
251 if (pict == NULL) | |
252 goto fail; | |
253 s->picture_buffer[j][i] = pict; | 206 s->picture_buffer[j][i] = pict; |
254 } | 207 } |
255 } | 208 } |
256 } | 209 } |
210 | |
211 if(s->codec_id==CODEC_ID_MPEG4){ | |
212 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE); | |
213 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE); | |
214 } | |
257 } | 215 } |
258 | 216 |
259 if (s->out_format == FMT_H263 || s->encoding) { | 217 if (s->out_format == FMT_H263 || s->encoding) { |
260 int size; | 218 int size; |
219 /* Allocate MB type table */ | |
220 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8)) | |
221 | |
261 /* MV prediction */ | 222 /* MV prediction */ |
262 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | 223 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); |
263 s->motion_val = av_malloc(size * 2 * sizeof(INT16)); | 224 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16)); |
264 if (s->motion_val == NULL) | |
265 goto fail; | |
266 memset(s->motion_val, 0, size * 2 * sizeof(INT16)); | |
267 } | 225 } |
268 | 226 |
269 if (s->h263_pred || s->h263_plus) { | 227 if (s->h263_pred || s->h263_plus) { |
270 int y_size, c_size, i, size; | 228 int y_size, c_size, i, size; |
271 | 229 |
272 /* dc values */ | 230 /* dc values */ |
273 | 231 |
274 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | 232 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); |
275 c_size = (s->mb_width + 2) * (s->mb_height + 2); | 233 c_size = (s->mb_width + 2) * (s->mb_height + 2); |
276 size = y_size + 2 * c_size; | 234 size = y_size + 2 * c_size; |
277 s->dc_val[0] = av_malloc(size * sizeof(INT16)); | 235 CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16)); |
278 if (s->dc_val[0] == NULL) | |
279 goto fail; | |
280 s->dc_val[1] = s->dc_val[0] + y_size; | 236 s->dc_val[1] = s->dc_val[0] + y_size; |
281 s->dc_val[2] = s->dc_val[1] + c_size; | 237 s->dc_val[2] = s->dc_val[1] + c_size; |
282 for(i=0;i<size;i++) | 238 for(i=0;i<size;i++) |
283 s->dc_val[0][i] = 1024; | 239 s->dc_val[0][i] = 1024; |
284 | 240 |
285 /* ac values */ | 241 /* ac values */ |
286 s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16); | 242 CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16); |
287 if (s->ac_val[0] == NULL) | |
288 goto fail; | |
289 s->ac_val[1] = s->ac_val[0] + y_size; | 243 s->ac_val[1] = s->ac_val[0] + y_size; |
290 s->ac_val[2] = s->ac_val[1] + c_size; | 244 s->ac_val[2] = s->ac_val[1] + c_size; |
291 | 245 |
292 /* cbp values */ | 246 /* cbp values */ |
293 s->coded_block = av_mallocz(y_size); | 247 CHECKED_ALLOCZ(s->coded_block, y_size); |
294 if (!s->coded_block) | |
295 goto fail; | |
296 | 248 |
297 /* which mb is a intra block */ | 249 /* which mb is a intra block */ |
298 s->mbintra_table = av_mallocz(s->mb_num); | 250 CHECKED_ALLOCZ(s->mbintra_table, s->mb_num); |
299 if (!s->mbintra_table) | |
300 goto fail; | |
301 memset(s->mbintra_table, 1, s->mb_num); | 251 memset(s->mbintra_table, 1, s->mb_num); |
302 | 252 |
303 /* divx501 bitstream reorder buffer */ | 253 /* divx501 bitstream reorder buffer */ |
304 s->bitstream_buffer= av_mallocz(BITSTREAM_BUFFER_SIZE); | 254 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE); |
305 if (!s->bitstream_buffer) | 255 |
306 goto fail; | 256 /* cbp, ac_pred, pred_dir */ |
257 CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8)) | |
258 CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8)) | |
259 | |
260 CHECKED_ALLOCZ(s->qscale_table , s->mb_num * sizeof(UINT8)) | |
307 } | 261 } |
308 /* default structure is frame */ | 262 /* default structure is frame */ |
309 s->picture_structure = PICT_FRAME; | 263 s->picture_structure = PICT_FRAME; |
310 | 264 |
311 /* init macroblock skip table */ | 265 /* init macroblock skip table */ |
312 s->mbskip_table = av_mallocz(s->mb_num); | 266 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num); |
313 if (!s->mbskip_table) | |
314 goto fail; | |
315 | 267 |
316 s->block= s->blocks[0]; | 268 s->block= s->blocks[0]; |
317 | 269 |
318 s->context_initialized = 1; | 270 s->context_initialized = 1; |
319 return 0; | 271 return 0; |
320 fail: | 272 fail: |
321 MPV_common_end(s); | 273 MPV_common_end(s); |
322 return -1; | 274 return -1; |
323 } | 275 } |
324 | 276 |
277 | |
278 //extern int sads; | |
279 | |
325 /* init common structure for both encoder and decoder */ | 280 /* init common structure for both encoder and decoder */ |
326 void MPV_common_end(MpegEncContext *s) | 281 void MPV_common_end(MpegEncContext *s) |
327 { | 282 { |
328 int i; | 283 int i; |
329 | 284 |
330 av_freep(&s->mb_type); | 285 av_freep(&s->mb_type); |
331 av_freep(&s->mb_var); | 286 av_freep(&s->mb_var); |
287 av_freep(&s->mc_mb_var); | |
332 av_freep(&s->p_mv_table); | 288 av_freep(&s->p_mv_table); |
333 av_freep(&s->last_p_mv_table); | |
334 av_freep(&s->b_forw_mv_table); | 289 av_freep(&s->b_forw_mv_table); |
335 av_freep(&s->b_back_mv_table); | 290 av_freep(&s->b_back_mv_table); |
336 av_freep(&s->b_bidir_forw_mv_table); | 291 av_freep(&s->b_bidir_forw_mv_table); |
337 av_freep(&s->b_bidir_back_mv_table); | 292 av_freep(&s->b_bidir_back_mv_table); |
338 av_freep(&s->b_direct_forw_mv_table); | 293 av_freep(&s->b_direct_forw_mv_table); |
341 av_freep(&s->motion_val); | 296 av_freep(&s->motion_val); |
342 av_freep(&s->dc_val[0]); | 297 av_freep(&s->dc_val[0]); |
343 av_freep(&s->ac_val[0]); | 298 av_freep(&s->ac_val[0]); |
344 av_freep(&s->coded_block); | 299 av_freep(&s->coded_block); |
345 av_freep(&s->mbintra_table); | 300 av_freep(&s->mbintra_table); |
301 av_freep(&s->cbp_table); | |
302 av_freep(&s->pred_dir_table); | |
303 av_freep(&s->qscale_table); | |
346 av_freep(&s->me_scratchpad); | 304 av_freep(&s->me_scratchpad); |
347 | 305 av_freep(&s->me_map); |
306 av_freep(&s->me_score_map); | |
307 | |
348 av_freep(&s->mbskip_table); | 308 av_freep(&s->mbskip_table); |
349 av_freep(&s->bitstream_buffer); | 309 av_freep(&s->bitstream_buffer); |
310 av_freep(&s->tex_pb_buffer); | |
311 av_freep(&s->pb2_buffer); | |
350 for(i=0;i<3;i++) { | 312 for(i=0;i<3;i++) { |
351 int j; | 313 int j; |
352 av_freep(&s->last_picture_base[i]); | 314 av_freep(&s->last_picture_base[i]); |
353 av_freep(&s->next_picture_base[i]); | 315 av_freep(&s->next_picture_base[i]); |
354 av_freep(&s->aux_picture_base[i]); | 316 av_freep(&s->aux_picture_base[i]); |
370 s->bit_rate = avctx->bit_rate; | 332 s->bit_rate = avctx->bit_rate; |
371 s->bit_rate_tolerance = avctx->bit_rate_tolerance; | 333 s->bit_rate_tolerance = avctx->bit_rate_tolerance; |
372 s->frame_rate = avctx->frame_rate; | 334 s->frame_rate = avctx->frame_rate; |
373 s->width = avctx->width; | 335 s->width = avctx->width; |
374 s->height = avctx->height; | 336 s->height = avctx->height; |
337 if(avctx->gop_size > 600){ | |
338 fprintf(stderr, "Warning keyframe intervall too large! reducing it ...\n"); | |
339 avctx->gop_size=600; | |
340 } | |
375 s->gop_size = avctx->gop_size; | 341 s->gop_size = avctx->gop_size; |
376 s->rtp_mode = avctx->rtp_mode; | 342 s->rtp_mode = avctx->rtp_mode; |
377 s->rtp_payload_size = avctx->rtp_payload_size; | 343 s->rtp_payload_size = avctx->rtp_payload_size; |
378 if (avctx->rtp_callback) | 344 if (avctx->rtp_callback) |
379 s->rtp_callback = avctx->rtp_callback; | 345 s->rtp_callback = avctx->rtp_callback; |
381 s->qmax= avctx->qmax; | 347 s->qmax= avctx->qmax; |
382 s->max_qdiff= avctx->max_qdiff; | 348 s->max_qdiff= avctx->max_qdiff; |
383 s->qcompress= avctx->qcompress; | 349 s->qcompress= avctx->qcompress; |
384 s->qblur= avctx->qblur; | 350 s->qblur= avctx->qblur; |
385 s->b_quant_factor= avctx->b_quant_factor; | 351 s->b_quant_factor= avctx->b_quant_factor; |
352 s->b_quant_offset= avctx->b_quant_offset; | |
386 s->avctx = avctx; | 353 s->avctx = avctx; |
387 s->aspect_ratio_info= avctx->aspect_ratio_info; | 354 s->aspect_ratio_info= avctx->aspect_ratio_info; |
388 s->flags= avctx->flags; | 355 s->flags= avctx->flags; |
389 s->max_b_frames= avctx->max_b_frames; | 356 s->max_b_frames= avctx->max_b_frames; |
390 s->rc_strategy= avctx->rc_strategy; | 357 s->rc_strategy= avctx->rc_strategy; |
391 s->b_frame_strategy= avctx->b_frame_strategy; | 358 s->b_frame_strategy= avctx->b_frame_strategy; |
392 s->codec_id= avctx->codec->id; | 359 s->codec_id= avctx->codec->id; |
360 s->luma_elim_threshold = avctx->luma_elim_threshold; | |
361 s->chroma_elim_threshold= avctx->chroma_elim_threshold; | |
362 s->strict_std_compliance= avctx->strict_std_compliance; | |
363 s->data_partitioning= avctx->flags & CODEC_FLAG_PART; | |
393 | 364 |
394 if (s->gop_size <= 1) { | 365 if (s->gop_size <= 1) { |
395 s->intra_only = 1; | 366 s->intra_only = 1; |
396 s->gop_size = 12; | 367 s->gop_size = 12; |
397 } else { | 368 } else { |
488 break; | 459 break; |
489 default: | 460 default: |
490 return -1; | 461 return -1; |
491 } | 462 } |
492 | 463 |
493 if((s->flags&CODEC_FLAG_4MV) && !(s->flags&CODEC_FLAG_HQ)){ | |
494 printf("4MV is currently only supported in HQ mode\n"); | |
495 return -1; | |
496 } | |
497 | |
498 { /* set up some save defaults, some codecs might override them later */ | 464 { /* set up some save defaults, some codecs might override them later */ |
499 static int done=0; | 465 static int done=0; |
500 if(!done){ | 466 if(!done){ |
501 int i; | 467 int i; |
502 done=1; | 468 done=1; |
607 { | 573 { |
608 int i; | 574 int i; |
609 UINT8 *tmp; | 575 UINT8 *tmp; |
610 | 576 |
611 s->mb_skiped = 0; | 577 s->mb_skiped = 0; |
578 s->decoding_error=0; | |
579 | |
612 if (s->pict_type == B_TYPE) { | 580 if (s->pict_type == B_TYPE) { |
613 for(i=0;i<3;i++) { | 581 for(i=0;i<3;i++) { |
614 s->current_picture[i] = s->aux_picture[i]; | 582 s->current_picture[i] = s->aux_picture[i]; |
615 } | 583 } |
616 } else { | 584 } else { |
625 } | 593 } |
626 | 594 |
627 /* generic function for encode/decode called after a frame has been coded/decoded */ | 595 /* generic function for encode/decode called after a frame has been coded/decoded */ |
628 void MPV_frame_end(MpegEncContext *s) | 596 void MPV_frame_end(MpegEncContext *s) |
629 { | 597 { |
598 // if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads); | |
599 | |
630 /* draw edge for correct motion prediction if outside */ | 600 /* draw edge for correct motion prediction if outside */ |
631 if (s->pict_type != B_TYPE && !s->intra_only) { | 601 if (s->pict_type != B_TYPE && !s->intra_only) { |
632 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){ | 602 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4 || s->divx_version==500){ |
633 draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH); | 603 draw_edges(s->current_picture[0], s->linesize, s->mb_width*16, s->mb_height*16, EDGE_WIDTH); |
634 draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2); | 604 draw_edges(s->current_picture[1], s->linesize/2, s->mb_width*8, s->mb_height*8, EDGE_WIDTH/2); |
643 emms_c(); | 613 emms_c(); |
644 | 614 |
645 if(s->pict_type!=B_TYPE){ | 615 if(s->pict_type!=B_TYPE){ |
646 s->last_non_b_pict_type= s->pict_type; | 616 s->last_non_b_pict_type= s->pict_type; |
647 s->last_non_b_qscale= s->qscale; | 617 s->last_non_b_qscale= s->qscale; |
648 s->last_non_b_mc_mb_var= s->mc_mb_var; | 618 s->last_non_b_mc_mb_var= s->mc_mb_var_sum; |
649 s->num_available_buffers++; | 619 s->num_available_buffers++; |
650 if(s->num_available_buffers>2) s->num_available_buffers= 2; | 620 if(s->num_available_buffers>2) s->num_available_buffers= 2; |
651 } | 621 } |
652 } | 622 } |
653 | 623 |
802 | 772 |
803 avctx->quality = s->qscale; | 773 avctx->quality = s->qscale; |
804 | 774 |
805 if(s->flags&CODEC_FLAG_PASS1) | 775 if(s->flags&CODEC_FLAG_PASS1) |
806 ff_write_pass1_stats(s); | 776 ff_write_pass1_stats(s); |
777 | |
807 } | 778 } |
808 | 779 |
809 s->input_picture_number++; | 780 s->input_picture_number++; |
810 s->input_picture_in_gop_number++; | 781 s->input_picture_in_gop_number++; |
811 | 782 |
895 if(s->quarter_sample) | 866 if(s->quarter_sample) |
896 { | 867 { |
897 motion_x>>=1; | 868 motion_x>>=1; |
898 motion_y>>=1; | 869 motion_y>>=1; |
899 } | 870 } |
900 | |
901 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | 871 dxy = ((motion_y & 1) << 1) | (motion_x & 1); |
902 src_x = s->mb_x * 16 + (motion_x >> 1); | 872 src_x = s->mb_x * 16 + (motion_x >> 1); |
903 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | 873 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); |
904 | 874 |
905 /* WARNING: do no forget half pels */ | 875 /* WARNING: do no forget half pels */ |
1152 ff_idct (block); | 1122 ff_idct (block); |
1153 add_pixels_clamped(block, dest, line_size); | 1123 add_pixels_clamped(block, dest, line_size); |
1154 } | 1124 } |
1155 } | 1125 } |
1156 | 1126 |
1127 /** | |
1128 * cleans dc, ac, coded_block for the current non intra MB | |
1129 */ | |
1130 void ff_clean_intra_table_entries(MpegEncContext *s) | |
1131 { | |
1132 int wrap = s->block_wrap[0]; | |
1133 int xy = s->block_index[0]; | |
1134 | |
1135 s->dc_val[0][xy ] = | |
1136 s->dc_val[0][xy + 1 ] = | |
1137 s->dc_val[0][xy + wrap] = | |
1138 s->dc_val[0][xy + 1 + wrap] = 1024; | |
1139 /* ac pred */ | |
1140 memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16)); | |
1141 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16)); | |
1142 if (s->msmpeg4_version>=3) { | |
1143 s->coded_block[xy ] = | |
1144 s->coded_block[xy + 1 ] = | |
1145 s->coded_block[xy + wrap] = | |
1146 s->coded_block[xy + 1 + wrap] = 0; | |
1147 } | |
1148 /* chroma */ | |
1149 wrap = s->block_wrap[4]; | |
1150 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap; | |
1151 s->dc_val[1][xy] = | |
1152 s->dc_val[2][xy] = 1024; | |
1153 /* ac pred */ | |
1154 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); | |
1155 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16)); | |
1156 | |
1157 s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0; | |
1158 } | |
1159 | |
1157 /* generic function called after a macroblock has been parsed by the | 1160 /* generic function called after a macroblock has been parsed by the |
1158 decoder or after it has been encoded by the encoder. | 1161 decoder or after it has been encoded by the encoder. |
1159 | 1162 |
1160 Important variables used: | 1163 Important variables used: |
1161 s->mb_intra : true if intra macroblock | 1164 s->mb_intra : true if intra macroblock |
1180 #endif | 1183 #endif |
1181 | 1184 |
1182 /* update DC predictors for P macroblocks */ | 1185 /* update DC predictors for P macroblocks */ |
1183 if (!s->mb_intra) { | 1186 if (!s->mb_intra) { |
1184 if (s->h263_pred || s->h263_aic) { | 1187 if (s->h263_pred || s->h263_aic) { |
1185 if(s->mbintra_table[mb_x + mb_y*s->mb_width]) | 1188 if(s->mbintra_table[mb_x + mb_y*s->mb_width]) |
1186 { | 1189 ff_clean_intra_table_entries(s); |
1187 int wrap, xy, v; | |
1188 s->mbintra_table[mb_x + mb_y*s->mb_width]=0; | |
1189 wrap = 2 * s->mb_width + 2; | |
1190 xy = 2 * mb_x + 1 + (2 * mb_y + 1) * wrap; | |
1191 v = 1024; | |
1192 | |
1193 s->dc_val[0][xy] = v; | |
1194 s->dc_val[0][xy + 1] = v; | |
1195 s->dc_val[0][xy + wrap] = v; | |
1196 s->dc_val[0][xy + 1 + wrap] = v; | |
1197 /* ac pred */ | |
1198 memset(s->ac_val[0][xy], 0, 16 * sizeof(INT16)); | |
1199 memset(s->ac_val[0][xy + 1], 0, 16 * sizeof(INT16)); | |
1200 memset(s->ac_val[0][xy + wrap], 0, 16 * sizeof(INT16)); | |
1201 memset(s->ac_val[0][xy + 1 + wrap], 0, 16 * sizeof(INT16)); | |
1202 if (s->h263_msmpeg4) { | |
1203 s->coded_block[xy] = 0; | |
1204 s->coded_block[xy + 1] = 0; | |
1205 s->coded_block[xy + wrap] = 0; | |
1206 s->coded_block[xy + 1 + wrap] = 0; | |
1207 } | |
1208 /* chroma */ | |
1209 wrap = s->mb_width + 2; | |
1210 xy = mb_x + 1 + (mb_y + 1) * wrap; | |
1211 s->dc_val[1][xy] = v; | |
1212 s->dc_val[2][xy] = v; | |
1213 /* ac pred */ | |
1214 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); | |
1215 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16)); | |
1216 } | |
1217 } else { | 1190 } else { |
1218 s->last_dc[0] = 128 << s->intra_dc_precision; | 1191 s->last_dc[0] = |
1219 s->last_dc[1] = 128 << s->intra_dc_precision; | 1192 s->last_dc[1] = |
1220 s->last_dc[2] = 128 << s->intra_dc_precision; | 1193 s->last_dc[2] = 128 << s->intra_dc_precision; |
1221 } | 1194 } |
1222 } | 1195 } |
1223 else if (s->h263_pred || s->h263_aic) | 1196 else if (s->h263_pred || s->h263_aic) |
1224 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; | 1197 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; |
1282 dct_linesize = s->linesize; | 1255 dct_linesize = s->linesize; |
1283 dct_offset = s->linesize * 8; | 1256 dct_offset = s->linesize * 8; |
1284 } | 1257 } |
1285 | 1258 |
1286 if (!s->mb_intra) { | 1259 if (!s->mb_intra) { |
1260 const int xy= s->mb_y * s->mb_width + s->mb_x; | |
1287 /* motion handling */ | 1261 /* motion handling */ |
1288 if((s->flags&CODEC_FLAG_HQ) || (!s->encoding)){ | 1262 /* decoding or more than one mb_type (MC was allready done otherwise) */ |
1263 if((!s->encoding) || (s->mb_type[xy]&(s->mb_type[xy]-1))){ | |
1289 if ((!s->no_rounding) || s->pict_type==B_TYPE){ | 1264 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
1290 op_pix = put_pixels_tab; | 1265 op_pix = put_pixels_tab; |
1291 op_qpix= qpel_mc_rnd_tab; | 1266 op_qpix= qpel_mc_rnd_tab; |
1292 }else{ | 1267 }else{ |
1293 op_pix = put_no_rnd_pixels_tab; | 1268 op_pix = put_no_rnd_pixels_tab; |
1327 } | 1302 } |
1328 the_end: | 1303 the_end: |
1329 emms_c(); //FIXME remove | 1304 emms_c(); //FIXME remove |
1330 } | 1305 } |
1331 | 1306 |
1307 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold, int skip_dc) | |
1308 { | |
1309 static const char tab[64]= | |
1310 {3,2,2,1,1,1,1,1, | |
1311 1,1,1,1,1,1,1,1, | |
1312 1,1,1,1,1,1,1,1, | |
1313 0,0,0,0,0,0,0,0, | |
1314 0,0,0,0,0,0,0,0, | |
1315 0,0,0,0,0,0,0,0, | |
1316 0,0,0,0,0,0,0,0, | |
1317 0,0,0,0,0,0,0,0}; | |
1318 int score=0; | |
1319 int run=0; | |
1320 int i; | |
1321 DCTELEM *block= s->block[n]; | |
1322 const int last_index= s->block_last_index[n]; | |
1323 | |
1324 if(skip_dc) skip_dc=1; | |
1325 | |
1326 /* are all which we could set to zero are allready zero? */ | |
1327 if(last_index<=skip_dc - 1) return; | |
1328 | |
1329 for(i=0; i<=last_index; i++){ | |
1330 const int j = zigzag_direct[i]; | |
1331 const int level = ABS(block[j]); | |
1332 if(level==1){ | |
1333 if(skip_dc && i==0) continue; | |
1334 score+= tab[run]; | |
1335 run=0; | |
1336 }else if(level>1){ | |
1337 return; | |
1338 }else{ | |
1339 run++; | |
1340 } | |
1341 } | |
1342 if(score >= threshold) return; | |
1343 for(i=skip_dc; i<=last_index; i++){ | |
1344 const int j = zigzag_direct[i]; | |
1345 block[j]=0; | |
1346 } | |
1347 if(block[0]) s->block_last_index[n]= 0; | |
1348 else s->block_last_index[n]= -1; | |
1349 } | |
1350 | |
1332 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index) | 1351 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index) |
1333 { | 1352 { |
1334 int i; | 1353 int i; |
1335 const int maxlevel= s->max_qcoeff; | 1354 const int maxlevel= s->max_qcoeff; |
1336 const int minlevel= s->min_qcoeff; | 1355 const int minlevel= s->min_qcoeff; |
1348 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) | 1367 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) |
1349 { | 1368 { |
1350 const int mb_x= s->mb_x; | 1369 const int mb_x= s->mb_x; |
1351 const int mb_y= s->mb_y; | 1370 const int mb_y= s->mb_y; |
1352 int i; | 1371 int i; |
1372 int skip_dct[6]; | |
1353 #if 0 | 1373 #if 0 |
1354 if (s->interlaced_dct) { | 1374 if (s->interlaced_dct) { |
1355 dct_linesize = s->linesize * 2; | 1375 dct_linesize = s->linesize * 2; |
1356 dct_offset = s->linesize; | 1376 dct_offset = s->linesize; |
1357 } else { | 1377 } else { |
1358 dct_linesize = s->linesize; | 1378 dct_linesize = s->linesize; |
1359 dct_offset = s->linesize * 8; | 1379 dct_offset = s->linesize * 8; |
1360 } | 1380 } |
1361 #endif | 1381 #endif |
1382 for(i=0; i<6; i++) skip_dct[i]=0; | |
1362 | 1383 |
1363 if (s->mb_intra) { | 1384 if (s->mb_intra) { |
1364 UINT8 *ptr; | 1385 UINT8 *ptr; |
1365 int wrap; | 1386 int wrap; |
1366 | 1387 |
1379 get_pixels(s->block[5], ptr, wrap); | 1400 get_pixels(s->block[5], ptr, wrap); |
1380 }else{ | 1401 }else{ |
1381 op_pixels_func *op_pix; | 1402 op_pixels_func *op_pix; |
1382 qpel_mc_func *op_qpix; | 1403 qpel_mc_func *op_qpix; |
1383 UINT8 *dest_y, *dest_cb, *dest_cr; | 1404 UINT8 *dest_y, *dest_cb, *dest_cr; |
1384 UINT8 *ptr; | 1405 UINT8 *ptr_y, *ptr_cb, *ptr_cr; |
1385 int wrap; | 1406 int wrap_y, wrap_c; |
1386 | 1407 |
1387 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16; | 1408 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16; |
1388 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | 1409 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; |
1389 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | 1410 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; |
1411 wrap_y = s->linesize; | |
1412 wrap_c = wrap_y>>1; | |
1413 ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16; | |
1414 ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
1415 ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
1390 | 1416 |
1391 if ((!s->no_rounding) || s->pict_type==B_TYPE){ | 1417 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
1392 op_pix = put_pixels_tab; | 1418 op_pix = put_pixels_tab; |
1393 op_qpix= qpel_mc_rnd_tab; | 1419 op_qpix= qpel_mc_rnd_tab; |
1394 }else{ | 1420 }else{ |
1404 op_pix = avg_no_rnd_pixels_tab; | 1430 op_pix = avg_no_rnd_pixels_tab; |
1405 } | 1431 } |
1406 if (s->mv_dir & MV_DIR_BACKWARD) { | 1432 if (s->mv_dir & MV_DIR_BACKWARD) { |
1407 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); | 1433 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); |
1408 } | 1434 } |
1409 wrap = s->linesize; | 1435 |
1410 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; | 1436 diff_pixels(s->block[0], ptr_y , dest_y , wrap_y); |
1411 diff_pixels(s->block[0], ptr , dest_y , wrap); | 1437 diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); |
1412 diff_pixels(s->block[1], ptr + 8, dest_y + 8, wrap); | 1438 diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y); |
1413 diff_pixels(s->block[2], ptr + 8 * wrap , dest_y + 8 * wrap , wrap); | 1439 diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y); |
1414 diff_pixels(s->block[3], ptr + 8 * wrap + 8, dest_y + 8 * wrap + 8, wrap); | 1440 diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c); |
1415 | 1441 diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); |
1416 wrap >>=1; | 1442 |
1417 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8; | 1443 /* pre quantization */ |
1418 diff_pixels(s->block[4], ptr, dest_cb, wrap); | 1444 if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){ |
1419 | 1445 if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1; |
1420 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8; | 1446 if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1; |
1421 diff_pixels(s->block[5], ptr, dest_cr, wrap); | 1447 if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1; |
1448 if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1; | |
1449 if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1; | |
1450 if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1; | |
1451 #if 0 | |
1452 { | |
1453 static int stat[7]; | |
1454 int num=0; | |
1455 for(i=0; i<6; i++) | |
1456 if(skip_dct[i]) num++; | |
1457 stat[num]++; | |
1458 | |
1459 if(s->mb_x==0 && s->mb_y==0){ | |
1460 for(i=0; i<7; i++){ | |
1461 printf("%6d %1d\n", stat[i], i); | |
1462 } | |
1463 } | |
1464 } | |
1465 #endif | |
1466 } | |
1467 | |
1422 } | 1468 } |
1423 | 1469 |
1424 #if 0 | 1470 #if 0 |
1425 { | 1471 { |
1426 float adap_parm; | 1472 float adap_parm; |
1433 s->qscale, adap_parm, s->qscale*adap_parm, | 1479 s->qscale, adap_parm, s->qscale*adap_parm, |
1434 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); | 1480 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); |
1435 } | 1481 } |
1436 #endif | 1482 #endif |
1437 /* DCT & quantize */ | 1483 /* DCT & quantize */ |
1438 if (s->h263_pred && s->msmpeg4_version!=2) { | 1484 if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) { |
1439 h263_dc_scale(s); | 1485 h263_dc_scale(s); |
1440 } else if (s->h263_aic) { | 1486 } else if (s->h263_aic) { |
1441 s->y_dc_scale = 2*s->qscale; | 1487 s->y_dc_scale = 2*s->qscale; |
1442 s->c_dc_scale = 2*s->qscale; | 1488 s->c_dc_scale = 2*s->qscale; |
1443 } else { | 1489 } else { |
1451 s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow); | 1497 s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow); |
1452 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); | 1498 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
1453 } | 1499 } |
1454 }else{ | 1500 }else{ |
1455 for(i=0;i<6;i++) { | 1501 for(i=0;i<6;i++) { |
1456 int overflow; | 1502 if(!skip_dct[i]){ |
1457 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow); | 1503 int overflow; |
1504 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow); | |
1458 // FIXME we could decide to change to quantizer instead of clipping | 1505 // FIXME we could decide to change to quantizer instead of clipping |
1459 // JS: I don't think that would be a good idea it could lower quality instead | 1506 // JS: I don't think that would be a good idea it could lower quality instead |
1460 // of improve it. Just INTRADC clipping deserves changes in quantizer | 1507 // of improve it. Just INTRADC clipping deserves changes in quantizer |
1461 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); | 1508 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
1462 } | 1509 }else |
1510 s->block_last_index[i]= -1; | |
1511 } | |
1512 if(s->luma_elim_threshold && !s->mb_intra) | |
1513 for(i=0; i<4; i++) | |
1514 dct_single_coeff_elimination(s, i, s->luma_elim_threshold, 0); | |
1515 if(s->chroma_elim_threshold && !s->mb_intra) | |
1516 for(i=4; i<6; i++) | |
1517 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold, 1); | |
1463 } | 1518 } |
1464 | 1519 |
1465 /* huffman encode */ | 1520 /* huffman encode */ |
1466 switch(s->out_format) { | 1521 switch(s->out_format) { |
1467 case FMT_MPEG1: | 1522 case FMT_MPEG1: |
1479 mjpeg_encode_mb(s, s->block); | 1534 mjpeg_encode_mb(s, s->block); |
1480 break; | 1535 break; |
1481 } | 1536 } |
1482 } | 1537 } |
1483 | 1538 |
1484 static void copy_bits(PutBitContext *pb, UINT8 *src, int length) | 1539 void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length) |
1485 { | 1540 { |
1486 #if 1 | 1541 #if 1 |
1487 int bytes= length>>4; | 1542 int bytes= length>>4; |
1488 int bits= length&15; | 1543 int bits= length&15; |
1489 int i; | 1544 int i; |
1545 | |
1546 if(length==0) return; | |
1490 | 1547 |
1491 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i])); | 1548 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i])); |
1492 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits)); | 1549 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits)); |
1493 #else | 1550 #else |
1494 int bytes= length>>3; | 1551 int bytes= length>>3; |
1498 for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]); | 1555 for(i=0; i<bytes; i++) put_bits(pb, 8, src[i]); |
1499 put_bits(pb, bits, src[i]>>(8-bits)); | 1556 put_bits(pb, bits, src[i]>>(8-bits)); |
1500 #endif | 1557 #endif |
1501 } | 1558 } |
1502 | 1559 |
1503 static void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){ | 1560 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
1504 int i; | 1561 int i; |
1505 | 1562 |
1506 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | 1563 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? |
1507 | 1564 |
1508 /* mpeg1 */ | 1565 /* mpeg1 */ |
1521 d->last_bits= 0; | 1578 d->last_bits= 0; |
1522 | 1579 |
1523 d->mb_skiped= s->mb_skiped; | 1580 d->mb_skiped= s->mb_skiped; |
1524 } | 1581 } |
1525 | 1582 |
1526 static void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ | 1583 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
1527 int i; | 1584 int i; |
1528 | 1585 |
1529 memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); | 1586 memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); |
1530 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | 1587 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? |
1531 | 1588 |
1546 d->mb_intra= s->mb_intra; | 1603 d->mb_intra= s->mb_intra; |
1547 d->mb_skiped= s->mb_skiped; | 1604 d->mb_skiped= s->mb_skiped; |
1548 d->mv_type= s->mv_type; | 1605 d->mv_type= s->mv_type; |
1549 d->mv_dir= s->mv_dir; | 1606 d->mv_dir= s->mv_dir; |
1550 d->pb= s->pb; | 1607 d->pb= s->pb; |
1608 if(s->data_partitioning){ | |
1609 d->pb2= s->pb2; | |
1610 d->tex_pb= s->tex_pb; | |
1611 } | |
1551 d->block= s->block; | 1612 d->block= s->block; |
1552 for(i=0; i<6; i++) | 1613 for(i=0; i<6; i++) |
1553 d->block_last_index[i]= s->block_last_index[i]; | 1614 d->block_last_index[i]= s->block_last_index[i]; |
1554 } | 1615 } |
1555 | 1616 |
1617 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, | |
1618 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], | |
1619 int *dmin, int *next_block, int motion_x, int motion_y) | |
1620 { | |
1621 int bits_count; | |
1622 | |
1623 copy_context_before_encode(s, backup, type); | |
1624 | |
1625 s->block= s->blocks[*next_block]; | |
1626 s->pb= pb[*next_block]; | |
1627 if(s->data_partitioning){ | |
1628 s->pb2 = pb2 [*next_block]; | |
1629 s->tex_pb= tex_pb[*next_block]; | |
1630 } | |
1631 | |
1632 encode_mb(s, motion_x, motion_y); | |
1633 | |
1634 bits_count= get_bit_count(&s->pb); | |
1635 if(s->data_partitioning){ | |
1636 bits_count+= get_bit_count(&s->pb2); | |
1637 bits_count+= get_bit_count(&s->tex_pb); | |
1638 } | |
1639 | |
1640 if(bits_count<*dmin){ | |
1641 *dmin= bits_count; | |
1642 *next_block^=1; | |
1643 | |
1644 copy_context_after_encode(best, s, type); | |
1645 } | |
1646 } | |
1556 | 1647 |
1557 static void encode_picture(MpegEncContext *s, int picture_number) | 1648 static void encode_picture(MpegEncContext *s, int picture_number) |
1558 { | 1649 { |
1559 int mb_x, mb_y, last_gob, pdif = 0; | 1650 int mb_x, mb_y, last_gob, pdif = 0; |
1560 int i; | 1651 int i; |
1561 int bits; | 1652 int bits; |
1562 MpegEncContext best_s, backup_s; | 1653 MpegEncContext best_s, backup_s; |
1563 UINT8 bit_buf[7][3000]; //FIXME check that this is ALLWAYS large enogh for a MB | 1654 UINT8 bit_buf[2][3000]; |
1655 UINT8 bit_buf2[2][3000]; | |
1656 UINT8 bit_buf_tex[2][3000]; | |
1657 PutBitContext pb[2], pb2[2], tex_pb[2]; | |
1658 | |
1659 for(i=0; i<2; i++){ | |
1660 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL); | |
1661 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL); | |
1662 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL); | |
1663 } | |
1564 | 1664 |
1565 s->picture_number = picture_number; | 1665 s->picture_number = picture_number; |
1566 | 1666 |
1567 s->block_wrap[0]= | 1667 s->block_wrap[0]= |
1568 s->block_wrap[1]= | 1668 s->block_wrap[1]= |
1570 s->block_wrap[3]= s->mb_width*2 + 2; | 1670 s->block_wrap[3]= s->mb_width*2 + 2; |
1571 s->block_wrap[4]= | 1671 s->block_wrap[4]= |
1572 s->block_wrap[5]= s->mb_width + 2; | 1672 s->block_wrap[5]= s->mb_width + 2; |
1573 | 1673 |
1574 /* Reset the average MB variance */ | 1674 /* Reset the average MB variance */ |
1575 s->avg_mb_var = 0; | 1675 s->mb_var_sum = 0; |
1576 s->mc_mb_var = 0; | 1676 s->mc_mb_var_sum = 0; |
1577 | 1677 |
1578 /* we need to initialize some time vars before we can encode b-frames */ | 1678 /* we need to initialize some time vars before we can encode b-frames */ |
1579 if (s->h263_pred && !s->h263_msmpeg4) | 1679 if (s->h263_pred && !s->h263_msmpeg4) |
1580 ff_set_mpeg4_time(s, s->picture_number); | 1680 ff_set_mpeg4_time(s, s->picture_number); |
1581 | 1681 |
1582 /* Estimate motion for every MB */ | 1682 /* Estimate motion for every MB */ |
1583 if(s->pict_type != I_TYPE){ | 1683 if(s->pict_type != I_TYPE){ |
1584 // int16_t (*tmp)[2]= s->p_mv_table; | |
1585 // s->p_mv_table= s->last_mv_table; | |
1586 // s->last_mv_table= s->mv_table; | |
1587 | |
1588 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | 1684 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
1589 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; | 1685 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
1590 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | 1686 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); |
1591 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | 1687 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; |
1592 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | 1688 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); |
1605 ff_estimate_p_frame_motion(s, mb_x, mb_y); | 1701 ff_estimate_p_frame_motion(s, mb_x, mb_y); |
1606 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER; | 1702 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER; |
1607 } | 1703 } |
1608 } | 1704 } |
1609 emms_c(); | 1705 emms_c(); |
1610 }else if(s->pict_type == I_TYPE){ | 1706 }else /* if(s->pict_type == I_TYPE) */{ |
1611 /* I-Frame */ | 1707 /* I-Frame */ |
1612 //FIXME do we need to zero them? | 1708 //FIXME do we need to zero them? |
1613 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); | 1709 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); |
1614 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2); | 1710 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2); |
1615 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); | 1711 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
1616 } | 1712 } |
1617 | 1713 |
1618 if(s->avg_mb_var < s->mc_mb_var && s->pict_type == P_TYPE){ //FIXME subtract MV bits | 1714 if(s->mb_var_sum < s->mc_mb_var_sum && s->pict_type == P_TYPE){ //FIXME subtract MV bits |
1619 s->pict_type= I_TYPE; | 1715 s->pict_type= I_TYPE; |
1620 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); | 1716 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
1621 if(s->max_b_frames==0){ | 1717 if(s->max_b_frames==0){ |
1622 s->input_pict_type= I_TYPE; | 1718 s->input_pict_type= I_TYPE; |
1623 s->input_picture_in_gop_number=0; | 1719 s->input_picture_in_gop_number=0; |
1701 s->gob_index = 1; | 1797 s->gob_index = 1; |
1702 else if (s->height <= 800) | 1798 else if (s->height <= 800) |
1703 s->gob_index = 2; | 1799 s->gob_index = 2; |
1704 else | 1800 else |
1705 s->gob_index = 4; | 1801 s->gob_index = 4; |
1706 } | 1802 }else if(s->codec_id==CODEC_ID_MPEG4){ |
1707 | 1803 s->gob_index = 1; |
1708 s->avg_mb_var = s->avg_mb_var / s->mb_num; | 1804 } |
1709 | 1805 |
1806 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE) | |
1807 ff_mpeg4_init_partitions(s); | |
1808 | |
1809 s->resync_mb_x=0; | |
1810 s->resync_mb_y=0; | |
1710 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | 1811 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
1711 /* Put GOB header based on RTP MTU */ | 1812 /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/ |
1712 /* TODO: Put all this stuff in a separate generic function */ | 1813 /* TODO: Put all this stuff in a separate generic function */ |
1713 if (s->rtp_mode) { | 1814 if (s->rtp_mode) { |
1714 if (!mb_y) { | 1815 if (!mb_y) { |
1715 s->ptr_lastgob = s->pb.buf; | 1816 s->ptr_lastgob = s->pb.buf; |
1716 s->ptr_last_mb_line = s->pb.buf; | 1817 s->ptr_last_mb_line = s->pb.buf; |
1717 } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) { | 1818 } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) { |
1819 // MN: we could move the space check from h263 -> here, as its not h263 specific | |
1718 last_gob = h263_encode_gob_header(s, mb_y); | 1820 last_gob = h263_encode_gob_header(s, mb_y); |
1719 if (last_gob) { | 1821 if (last_gob) { |
1720 s->first_gob_line = 1; | 1822 s->first_slice_line = 1; |
1823 }else{ | |
1824 /*MN: we reset it here instead at the end of each line cuz mpeg4 can have | |
1825 slice lines starting & ending in the middle*/ | |
1826 s->first_slice_line = 0; | |
1721 } | 1827 } |
1722 } | 1828 } |
1723 } | 1829 } |
1724 | 1830 |
1725 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; | 1831 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
1729 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | 1835 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); |
1730 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); | 1836 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); |
1731 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | 1837 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
1732 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; | 1838 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; |
1733 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; | 1839 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; |
1734 PutBitContext pb; | 1840 // int d; |
1735 int d; | |
1736 int dmin=10000000; | 1841 int dmin=10000000; |
1737 int best=0; | |
1738 | 1842 |
1739 s->mb_x = mb_x; | 1843 s->mb_x = mb_x; |
1740 s->mb_y = mb_y; | 1844 s->mb_y = mb_y; |
1741 s->block_index[0]+=2; | 1845 s->block_index[0]+=2; |
1742 s->block_index[1]+=2; | 1846 s->block_index[1]+=2; |
1743 s->block_index[2]+=2; | 1847 s->block_index[2]+=2; |
1744 s->block_index[3]+=2; | 1848 s->block_index[3]+=2; |
1745 s->block_index[4]++; | 1849 s->block_index[4]++; |
1746 s->block_index[5]++; | 1850 s->block_index[5]++; |
1851 | |
1852 /* write gob / video packet header for formats which support it at any MB (MPEG4) */ | |
1853 if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){ | |
1854 int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob; | |
1855 | |
1856 //the *2 is there so we stay below the requested size | |
1857 if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){ | |
1858 if(s->codec_id==CODEC_ID_MPEG4){ | |
1859 if(s->data_partitioning && s->pict_type!=B_TYPE){ | |
1860 ff_mpeg4_merge_partitions(s); | |
1861 ff_mpeg4_init_partitions(s); | |
1862 } | |
1863 ff_mpeg4_encode_video_packet_header(s); | |
1864 | |
1865 if(s->flags&CODEC_FLAG_PASS1){ | |
1866 int bits= get_bit_count(&s->pb); | |
1867 s->misc_bits+= bits - s->last_bits; | |
1868 s->last_bits= bits; | |
1869 } | |
1870 ff_mpeg4_clean_buffers(s); | |
1871 } | |
1872 s->ptr_lastgob = pbBufPtr(&s->pb); | |
1873 s->first_slice_line=1; | |
1874 s->resync_mb_x=mb_x; | |
1875 s->resync_mb_y=mb_y; | |
1876 } | |
1877 | |
1878 if( (s->resync_mb_x == s->mb_x) | |
1879 && s->resync_mb_y+1 == s->mb_y){ | |
1880 s->first_slice_line=0; | |
1881 } | |
1882 } | |
1883 | |
1747 if(mb_type & (mb_type-1)){ // more than 1 MB type possible | 1884 if(mb_type & (mb_type-1)){ // more than 1 MB type possible |
1748 int next_block=0; | 1885 int next_block=0; |
1749 pb= s->pb; | 1886 int pb_bits_count, pb2_bits_count, tex_pb_bits_count; |
1750 | 1887 |
1751 copy_context_before_encode(&backup_s, s, -1); | 1888 copy_context_before_encode(&backup_s, s, -1); |
1889 backup_s.pb= s->pb; | |
1890 best_s.data_partitioning= s->data_partitioning; | |
1891 if(s->data_partitioning){ | |
1892 backup_s.pb2= s->pb2; | |
1893 backup_s.tex_pb= s->tex_pb; | |
1894 } | |
1752 | 1895 |
1753 if(mb_type&MB_TYPE_INTER){ | 1896 if(mb_type&MB_TYPE_INTER){ |
1754 s->mv_dir = MV_DIR_FORWARD; | 1897 s->mv_dir = MV_DIR_FORWARD; |
1755 s->mv_type = MV_TYPE_16X16; | 1898 s->mv_type = MV_TYPE_16X16; |
1756 s->mb_intra= 0; | 1899 s->mb_intra= 0; |
1757 s->mv[0][0][0] = s->p_mv_table[xy][0]; | 1900 s->mv[0][0][0] = s->p_mv_table[xy][0]; |
1758 s->mv[0][0][1] = s->p_mv_table[xy][1]; | 1901 s->mv[0][0][1] = s->p_mv_table[xy][1]; |
1759 init_put_bits(&s->pb, bit_buf[1], 3000, NULL, NULL); | 1902 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, |
1760 s->block= s->blocks[next_block]; | 1903 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); |
1761 s->last_bits= 0; //done in copy_context_before_encode but we skip that here | |
1762 | |
1763 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]); | |
1764 d= get_bit_count(&s->pb); | |
1765 if(d<dmin){ | |
1766 flush_put_bits(&s->pb); | |
1767 dmin=d; | |
1768 copy_context_after_encode(&best_s, s, MB_TYPE_INTER); | |
1769 best=1; | |
1770 next_block^=1; | |
1771 } | |
1772 } | 1904 } |
1773 if(mb_type&MB_TYPE_INTER4V){ | 1905 if(mb_type&MB_TYPE_INTER4V){ |
1774 copy_context_before_encode(s, &backup_s, MB_TYPE_INTER4V); | |
1775 s->mv_dir = MV_DIR_FORWARD; | 1906 s->mv_dir = MV_DIR_FORWARD; |
1776 s->mv_type = MV_TYPE_8X8; | 1907 s->mv_type = MV_TYPE_8X8; |
1777 s->mb_intra= 0; | 1908 s->mb_intra= 0; |
1778 for(i=0; i<4; i++){ | 1909 for(i=0; i<4; i++){ |
1779 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | 1910 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; |
1780 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | 1911 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; |
1781 } | 1912 } |
1782 init_put_bits(&s->pb, bit_buf[2], 3000, NULL, NULL); | 1913 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, |
1783 s->block= s->blocks[next_block]; | 1914 &dmin, &next_block, 0, 0); |
1784 | |
1785 encode_mb(s, 0, 0); | |
1786 d= get_bit_count(&s->pb); | |
1787 if(d<dmin){ | |
1788 flush_put_bits(&s->pb); | |
1789 dmin=d; | |
1790 copy_context_after_encode(&best_s, s, MB_TYPE_INTER4V); | |
1791 best=2; | |
1792 next_block^=1; | |
1793 } | |
1794 } | 1915 } |
1795 if(mb_type&MB_TYPE_FORWARD){ | 1916 if(mb_type&MB_TYPE_FORWARD){ |
1796 copy_context_before_encode(s, &backup_s, MB_TYPE_FORWARD); | |
1797 s->mv_dir = MV_DIR_FORWARD; | 1917 s->mv_dir = MV_DIR_FORWARD; |
1798 s->mv_type = MV_TYPE_16X16; | 1918 s->mv_type = MV_TYPE_16X16; |
1799 s->mb_intra= 0; | 1919 s->mb_intra= 0; |
1800 s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | 1920 s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; |
1801 s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | 1921 s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; |
1802 init_put_bits(&s->pb, bit_buf[3], 3000, NULL, NULL); | 1922 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, |
1803 s->block= s->blocks[next_block]; | 1923 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); |
1804 | |
1805 encode_mb(s, s->mv[0][0][0], s->mv[0][0][1]); | |
1806 d= get_bit_count(&s->pb); | |
1807 if(d<dmin){ | |
1808 flush_put_bits(&s->pb); | |
1809 dmin=d; | |
1810 copy_context_after_encode(&best_s, s, MB_TYPE_FORWARD); | |
1811 best=3; | |
1812 next_block^=1; | |
1813 } | |
1814 } | 1924 } |
1815 if(mb_type&MB_TYPE_BACKWARD){ | 1925 if(mb_type&MB_TYPE_BACKWARD){ |
1816 copy_context_before_encode(s, &backup_s, MB_TYPE_BACKWARD); | |
1817 s->mv_dir = MV_DIR_BACKWARD; | 1926 s->mv_dir = MV_DIR_BACKWARD; |
1818 s->mv_type = MV_TYPE_16X16; | 1927 s->mv_type = MV_TYPE_16X16; |
1819 s->mb_intra= 0; | 1928 s->mb_intra= 0; |
1820 s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | 1929 s->mv[1][0][0] = s->b_back_mv_table[xy][0]; |
1821 s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | 1930 s->mv[1][0][1] = s->b_back_mv_table[xy][1]; |
1822 init_put_bits(&s->pb, bit_buf[4], 3000, NULL, NULL); | 1931 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, |
1823 s->block= s->blocks[next_block]; | 1932 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]); |
1824 | |
1825 encode_mb(s, s->mv[1][0][0], s->mv[1][0][1]); | |
1826 d= get_bit_count(&s->pb); | |
1827 if(d<dmin){ | |
1828 flush_put_bits(&s->pb); | |
1829 dmin=d; | |
1830 copy_context_after_encode(&best_s, s, MB_TYPE_BACKWARD); | |
1831 best=4; | |
1832 next_block^=1; | |
1833 } | |
1834 } | 1933 } |
1835 if(mb_type&MB_TYPE_BIDIR){ | 1934 if(mb_type&MB_TYPE_BIDIR){ |
1836 copy_context_before_encode(s, &backup_s, MB_TYPE_BIDIR); | |
1837 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | 1935 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; |
1838 s->mv_type = MV_TYPE_16X16; | 1936 s->mv_type = MV_TYPE_16X16; |
1839 s->mb_intra= 0; | 1937 s->mb_intra= 0; |
1840 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | 1938 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; |
1841 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | 1939 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; |
1842 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | 1940 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; |
1843 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | 1941 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; |
1844 init_put_bits(&s->pb, bit_buf[5], 3000, NULL, NULL); | 1942 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, |
1845 s->block= s->blocks[next_block]; | 1943 &dmin, &next_block, 0, 0); |
1846 | |
1847 encode_mb(s, 0, 0); | |
1848 d= get_bit_count(&s->pb); | |
1849 if(d<dmin){ | |
1850 flush_put_bits(&s->pb); | |
1851 dmin=d; | |
1852 copy_context_after_encode(&best_s, s, MB_TYPE_BIDIR); | |
1853 best=5; | |
1854 next_block^=1; | |
1855 } | |
1856 } | 1944 } |
1857 if(mb_type&MB_TYPE_DIRECT){ | 1945 if(mb_type&MB_TYPE_DIRECT){ |
1858 copy_context_before_encode(s, &backup_s, MB_TYPE_DIRECT); | |
1859 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | 1946 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
1860 s->mv_type = MV_TYPE_16X16; //FIXME | 1947 s->mv_type = MV_TYPE_16X16; //FIXME |
1861 s->mb_intra= 0; | 1948 s->mb_intra= 0; |
1862 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0]; | 1949 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0]; |
1863 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1]; | 1950 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1]; |
1864 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0]; | 1951 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0]; |
1865 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1]; | 1952 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1]; |
1866 init_put_bits(&s->pb, bit_buf[6], 3000, NULL, NULL); | 1953 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, |
1867 s->block= s->blocks[next_block]; | 1954 &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]); |
1868 | |
1869 encode_mb(s, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]); | |
1870 d= get_bit_count(&s->pb); | |
1871 if(d<dmin){ | |
1872 flush_put_bits(&s->pb); | |
1873 dmin=d; | |
1874 copy_context_after_encode(&best_s, s, MB_TYPE_DIRECT); | |
1875 best=6; | |
1876 next_block^=1; | |
1877 } | |
1878 } | 1955 } |
1879 if(mb_type&MB_TYPE_INTRA){ | 1956 if(mb_type&MB_TYPE_INTRA){ |
1880 copy_context_before_encode(s, &backup_s, MB_TYPE_INTRA); | |
1881 s->mv_dir = MV_DIR_FORWARD; | 1957 s->mv_dir = MV_DIR_FORWARD; |
1882 s->mv_type = MV_TYPE_16X16; | 1958 s->mv_type = MV_TYPE_16X16; |
1883 s->mb_intra= 1; | 1959 s->mb_intra= 1; |
1884 s->mv[0][0][0] = 0; | 1960 s->mv[0][0][0] = 0; |
1885 s->mv[0][0][1] = 0; | 1961 s->mv[0][0][1] = 0; |
1886 init_put_bits(&s->pb, bit_buf[0], 3000, NULL, NULL); | 1962 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, |
1887 s->block= s->blocks[next_block]; | 1963 &dmin, &next_block, 0, 0); |
1888 | |
1889 encode_mb(s, 0, 0); | |
1890 d= get_bit_count(&s->pb); | |
1891 if(d<dmin){ | |
1892 flush_put_bits(&s->pb); | |
1893 dmin=d; | |
1894 copy_context_after_encode(&best_s, s, MB_TYPE_INTRA); | |
1895 best=0; | |
1896 next_block^=1; | |
1897 } | |
1898 /* force cleaning of ac/dc pred stuff if needed ... */ | 1964 /* force cleaning of ac/dc pred stuff if needed ... */ |
1899 if(s->h263_pred || s->h263_aic) | 1965 if(s->h263_pred || s->h263_aic) |
1900 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; | 1966 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; |
1901 } | 1967 } |
1902 copy_context_after_encode(s, &best_s, -1); | 1968 copy_context_after_encode(s, &best_s, -1); |
1903 copy_bits(&pb, bit_buf[best], dmin); | 1969 |
1904 s->pb= pb; | 1970 pb_bits_count= get_bit_count(&s->pb); |
1971 flush_put_bits(&s->pb); | |
1972 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count); | |
1973 s->pb= backup_s.pb; | |
1974 | |
1975 if(s->data_partitioning){ | |
1976 pb2_bits_count= get_bit_count(&s->pb2); | |
1977 flush_put_bits(&s->pb2); | |
1978 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count); | |
1979 s->pb2= backup_s.pb2; | |
1980 | |
1981 tex_pb_bits_count= get_bit_count(&s->tex_pb); | |
1982 flush_put_bits(&s->tex_pb); | |
1983 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count); | |
1984 s->tex_pb= backup_s.tex_pb; | |
1985 } | |
1905 s->last_bits= get_bit_count(&s->pb); | 1986 s->last_bits= get_bit_count(&s->pb); |
1906 } else { | 1987 } else { |
1907 int motion_x, motion_y; | 1988 int motion_x, motion_y; |
1908 s->mv_type=MV_TYPE_16X16; | 1989 s->mv_type=MV_TYPE_16X16; |
1909 // only one MB-Type possible | 1990 // only one MB-Type possible |
1917 case MB_TYPE_INTER: | 1998 case MB_TYPE_INTER: |
1918 s->mv_dir = MV_DIR_FORWARD; | 1999 s->mv_dir = MV_DIR_FORWARD; |
1919 s->mb_intra= 0; | 2000 s->mb_intra= 0; |
1920 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; | 2001 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; |
1921 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; | 2002 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; |
2003 break; | |
2004 case MB_TYPE_INTER4V: | |
2005 s->mv_dir = MV_DIR_FORWARD; | |
2006 s->mv_type = MV_TYPE_8X8; | |
2007 s->mb_intra= 0; | |
2008 for(i=0; i<4; i++){ | |
2009 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
2010 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
2011 } | |
2012 motion_x= motion_y= 0; | |
1922 break; | 2013 break; |
1923 case MB_TYPE_DIRECT: | 2014 case MB_TYPE_DIRECT: |
1924 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | 2015 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
1925 s->mb_intra= 0; | 2016 s->mb_intra= 0; |
1926 motion_x=s->b_direct_mv_table[xy][0]; | 2017 motion_x=s->b_direct_mv_table[xy][0]; |
1964 s->p_mv_table[xy][0]=0; | 2055 s->p_mv_table[xy][0]=0; |
1965 s->p_mv_table[xy][1]=0; | 2056 s->p_mv_table[xy][1]=0; |
1966 } | 2057 } |
1967 | 2058 |
1968 MPV_decode_mb(s, s->block); | 2059 MPV_decode_mb(s, s->block); |
2060 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb)); | |
1969 } | 2061 } |
1970 | 2062 |
1971 | 2063 |
1972 /* Obtain average GOB size for RTP */ | 2064 /* Obtain average GOB size for RTP */ |
1973 if (s->rtp_mode) { | 2065 if (s->rtp_mode) { |
1977 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1; | 2069 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1; |
1978 s->ptr_last_mb_line = pbBufPtr(&s->pb); | 2070 s->ptr_last_mb_line = pbBufPtr(&s->pb); |
1979 } | 2071 } |
1980 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, | 2072 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, |
1981 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); | 2073 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); |
1982 s->first_gob_line = 0; | 2074 if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean |
1983 } | 2075 } |
1984 } | 2076 } |
1985 emms_c(); | 2077 emms_c(); |
1986 | 2078 |
1987 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type == I_TYPE) | 2079 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE) |
2080 ff_mpeg4_merge_partitions(s); | |
2081 | |
2082 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE) | |
1988 msmpeg4_encode_ext_header(s); | 2083 msmpeg4_encode_ext_header(s); |
2084 | |
2085 if(s->codec_id==CODEC_ID_MPEG4) | |
2086 ff_mpeg4_stuffing(&s->pb); | |
1989 | 2087 |
1990 //if (s->gob_number) | 2088 //if (s->gob_number) |
1991 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | 2089 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); |
1992 | 2090 |
1993 /* Send the last GOB if RTP */ | 2091 /* Send the last GOB if RTP */ |
2009 int i, j, level, last_non_zero, q; | 2107 int i, j, level, last_non_zero, q; |
2010 const int *qmat; | 2108 const int *qmat; |
2011 int bias; | 2109 int bias; |
2012 int max=0; | 2110 int max=0; |
2013 unsigned int threshold1, threshold2; | 2111 unsigned int threshold1, threshold2; |
2014 | 2112 |
2015 av_fdct (block); | 2113 av_fdct (block); |
2016 | 2114 |
2017 /* we need this permutation so that we correct the IDCT | 2115 /* we need this permutation so that we correct the IDCT |
2018 permutation. will be moved into DCT code */ | 2116 permutation. will be moved into DCT code */ |
2019 block_permute(block); | 2117 block_permute(block); |
2238 block[i] = level; | 2336 block[i] = level; |
2239 } | 2337 } |
2240 } | 2338 } |
2241 } | 2339 } |
2242 | 2340 |
2341 static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y) | |
2342 { | |
2343 int dc, dcb, dcr, y, i; | |
2344 for(i=0; i<4; i++){ | |
2345 dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)]; | |
2346 for(y=0; y<8; y++){ | |
2347 int x; | |
2348 for(x=0; x<8; x++){ | |
2349 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8; | |
2350 } | |
2351 } | |
2352 } | |
2353 dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)]; | |
2354 dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)]; | |
2355 for(y=0; y<8; y++){ | |
2356 int x; | |
2357 for(x=0; x<8; x++){ | |
2358 dest_cb[x + y*(s->linesize>>1)]= dcb/8; | |
2359 dest_cr[x + y*(s->linesize>>1)]= dcr/8; | |
2360 } | |
2361 } | |
2362 } | |
2363 | |
2364 /** | |
2365 * will conceal past errors, and allso drop b frames if needed | |
2366 * | |
2367 */ | |
2368 void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos) | |
2369 { | |
2370 int mb_x= s->mb_x; | |
2371 int mb_y= s->mb_y; | |
2372 int mb_dist=0; | |
2373 int i, intra_count=0, inter_count=0; | |
2374 int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune | |
2375 int inter_conceal= s->msmpeg4_version ? 50 : 50; | |
2376 | |
2377 // for last block | |
2378 if(mb_x>=s->mb_width) mb_x= s->mb_width -1; | |
2379 if(mb_y>=s->mb_height) mb_y= s->mb_height-1; | |
2380 | |
2381 if(s->decoding_error==0 && unknown_pos){ | |
2382 if(s->data_partitioning && s->pict_type!=B_TYPE) | |
2383 s->decoding_error= DECODING_AC_LOST; | |
2384 else | |
2385 s->decoding_error= DECODING_DESYNC; | |
2386 } | |
2387 | |
2388 if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1; | |
2389 | |
2390 for(i=mb_x + mb_y*s->mb_width; i>=0; i--){ | |
2391 if(s->mbintra_table[i]) intra_count++; | |
2392 else inter_count++; | |
2393 } | |
2394 | |
2395 if(s->decoding_error==DECODING_AC_LOST){ | |
2396 intra_conceal*=2; | |
2397 inter_conceal*=2; | |
2398 }else if(s->decoding_error==DECODING_ACDC_LOST){ | |
2399 intra_conceal*=2; | |
2400 inter_conceal*=2; | |
2401 } | |
2402 | |
2403 if(unknown_pos && (intra_count<inter_count)){ | |
2404 intra_conceal= inter_conceal= s->mb_num; | |
2405 // printf("%d %d\n",intra_count, inter_count); | |
2406 } | |
2407 | |
2408 fprintf(stderr, "concealing errors\n"); | |
2409 | |
2410 /* for all MBs from the current one back until the last resync marker */ | |
2411 for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){ | |
2412 for(; mb_x>=0; mb_x--){ | |
2413 uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16; | |
2414 uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
2415 uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8; | |
2416 int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion | |
2417 int mb_y_backup= s->mb_y; | |
2418 s->mb_x=mb_x; | |
2419 s->mb_y=mb_y; | |
2420 if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){ | |
2421 if(s->decoding_error==DECODING_AC_LOST){ | |
2422 remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y); | |
2423 // printf("remove ac to %d %d\n", mb_x, mb_y); | |
2424 }else{ | |
2425 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
2426 s->last_picture, 0, 0, put_pixels_tab, | |
2427 0/*mx*/, 0/*my*/, 16); | |
2428 } | |
2429 } | |
2430 else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){ | |
2431 int mx=0; | |
2432 int my=0; | |
2433 | |
2434 if(s->decoding_error!=DECODING_DESYNC){ | |
2435 int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2); | |
2436 mx= s->motion_val[ xy ][0]; | |
2437 my= s->motion_val[ xy ][1]; | |
2438 } | |
2439 | |
2440 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
2441 s->last_picture, 0, 0, put_pixels_tab, | |
2442 mx, my, 16); | |
2443 } | |
2444 s->mb_x= mb_x_backup; | |
2445 s->mb_y= mb_y_backup; | |
2446 | |
2447 if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return; | |
2448 if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++; | |
2449 } | |
2450 mb_x=s->mb_width-1; | |
2451 } | |
2452 } | |
2453 | |
2243 AVCodec mpeg1video_encoder = { | 2454 AVCodec mpeg1video_encoder = { |
2244 "mpeg1video", | 2455 "mpeg1video", |
2245 CODEC_TYPE_VIDEO, | 2456 CODEC_TYPE_VIDEO, |
2246 CODEC_ID_MPEG1VIDEO, | 2457 CODEC_ID_MPEG1VIDEO, |
2247 sizeof(MpegEncContext), | 2458 sizeof(MpegEncContext), |