Mercurial > libavcodec.hg
comparison mpegvideo.c @ 3939:16ace038f452 libavcodec
fix buffer underflows by reencoding the current frame with a higher QP
author | michael |
---|---|
date | Thu, 05 Oct 2006 19:27:18 +0000 |
parents | a31f865dd68d |
children | c8c591fe26f8 |
comparison
equal
deleted
inserted
replaced
3938:92d8a536268f | 3939:16ace038f452 |
---|---|
2137 int h_shift= i ? h_chroma_shift : 0; | 2137 int h_shift= i ? h_chroma_shift : 0; |
2138 int v_shift= i ? v_chroma_shift : 0; | 2138 int v_shift= i ? v_chroma_shift : 0; |
2139 int w= s->width >>h_shift; | 2139 int w= s->width >>h_shift; |
2140 int h= s->height>>v_shift; | 2140 int h= s->height>>v_shift; |
2141 uint8_t *src= pic_arg->data[i]; | 2141 uint8_t *src= pic_arg->data[i]; |
2142 uint8_t *dst= pic->data[i] + INPLACE_OFFSET; | 2142 uint8_t *dst= pic->data[i]; |
2143 | |
2144 if(!s->avctx->rc_buffer_size) | |
2145 dst +=INPLACE_OFFSET; | |
2143 | 2146 |
2144 if(src_stride==dst_stride) | 2147 if(src_stride==dst_stride) |
2145 memcpy(dst, src, src_stride*h); | 2148 memcpy(dst, src, src_stride*h); |
2146 else{ | 2149 else{ |
2147 while(h--){ | 2150 while(h--){ |
2436 if(s->reordered_input_picture[0]){ | 2439 if(s->reordered_input_picture[0]){ |
2437 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0; | 2440 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0; |
2438 | 2441 |
2439 copy_picture(&s->new_picture, s->reordered_input_picture[0]); | 2442 copy_picture(&s->new_picture, s->reordered_input_picture[0]); |
2440 | 2443 |
2441 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){ | 2444 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED || s->avctx->rc_buffer_size){ |
2442 // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable | 2445 // input is a shared pix, so we can't modifiy it -> alloc a new one & ensure that the shared one is reuseable |
2443 | 2446 |
2444 int i= ff_find_unused_picture(s, 0); | 2447 int i= ff_find_unused_picture(s, 0); |
2445 Picture *pic= &s->picture[i]; | 2448 Picture *pic= &s->picture[i]; |
2446 | 2449 |
2450 pic->reference = s->reordered_input_picture[0]->reference; | |
2451 alloc_picture(s, pic, 0); | |
2452 | |
2447 /* mark us unused / free shared pic */ | 2453 /* mark us unused / free shared pic */ |
2454 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_INTERNAL) | |
2455 s->avctx->release_buffer(s->avctx, (AVFrame*)s->reordered_input_picture[0]); | |
2448 for(i=0; i<4; i++) | 2456 for(i=0; i<4; i++) |
2449 s->reordered_input_picture[0]->data[i]= NULL; | 2457 s->reordered_input_picture[0]->data[i]= NULL; |
2450 s->reordered_input_picture[0]->type= 0; | 2458 s->reordered_input_picture[0]->type= 0; |
2451 | |
2452 pic->reference = s->reordered_input_picture[0]->reference; | |
2453 | |
2454 alloc_picture(s, pic, 0); | |
2455 | 2459 |
2456 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]); | 2460 copy_picture_attributes(s, (AVFrame*)pic, (AVFrame*)s->reordered_input_picture[0]); |
2457 | 2461 |
2458 s->current_picture_ptr= pic; | 2462 s->current_picture_ptr= pic; |
2459 }else{ | 2463 }else{ |
2504 if(s->new_picture.data[0]){ | 2508 if(s->new_picture.data[0]){ |
2505 s->pict_type= s->new_picture.pict_type; | 2509 s->pict_type= s->new_picture.pict_type; |
2506 //emms_c(); | 2510 //emms_c(); |
2507 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale); | 2511 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale); |
2508 MPV_frame_start(s, avctx); | 2512 MPV_frame_start(s, avctx); |
2509 | 2513 vbv_retry: |
2510 if (encode_picture(s, s->picture_number) < 0) | 2514 if (encode_picture(s, s->picture_number) < 0) |
2511 return -1; | 2515 return -1; |
2512 | 2516 |
2513 avctx->real_pict_num = s->picture_number; | 2517 avctx->real_pict_num = s->picture_number; |
2514 avctx->header_bits = s->header_bits; | 2518 avctx->header_bits = s->header_bits; |
2522 | 2526 |
2523 MPV_frame_end(s); | 2527 MPV_frame_end(s); |
2524 | 2528 |
2525 if (s->out_format == FMT_MJPEG) | 2529 if (s->out_format == FMT_MJPEG) |
2526 mjpeg_picture_trailer(s); | 2530 mjpeg_picture_trailer(s); |
2531 | |
2532 if(avctx->rc_buffer_size){ | |
2533 RateControlContext *rcc= &s->rc_context; | |
2534 int max_size= rcc->buffer_index/3; | |
2535 | |
2536 if(put_bits_count(&s->pb) > max_size && s->qscale < s->avctx->qmax){ | |
2537 s->next_lambda= s->lambda*(s->qscale+1) / s->qscale; | |
2538 s->mb_skipped = 0; //done in MPV_frame_start() | |
2539 if(s->pict_type==P_TYPE){ //done in encode_picture() so we must undo it | |
2540 if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4) | |
2541 s->no_rounding ^= 1; | |
2542 } | |
2543 // av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda); | |
2544 for(i=0; i<avctx->thread_count; i++){ | |
2545 PutBitContext *pb= &s->thread_context[i]->pb; | |
2546 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf); | |
2547 } | |
2548 goto vbv_retry; | |
2549 } | |
2550 | |
2551 assert(s->avctx->rc_max_rate); | |
2552 } | |
2527 | 2553 |
2528 if(s->flags&CODEC_FLAG_PASS1) | 2554 if(s->flags&CODEC_FLAG_PASS1) |
2529 ff_write_pass1_stats(s); | 2555 ff_write_pass1_stats(s); |
2530 | 2556 |
2531 for(i=0; i<4; i++){ | 2557 for(i=0; i<4; i++){ |
5467 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb)); | 5493 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb)); |
5468 flush_put_bits(&dst->pb); | 5494 flush_put_bits(&dst->pb); |
5469 } | 5495 } |
5470 | 5496 |
5471 static int estimate_qp(MpegEncContext *s, int dry_run){ | 5497 static int estimate_qp(MpegEncContext *s, int dry_run){ |
5472 if (!s->fixed_qscale) { | 5498 if (s->next_lambda){ |
5499 s->current_picture_ptr->quality= | |
5500 s->current_picture.quality = s->next_lambda; | |
5501 if(!dry_run) s->next_lambda= 0; | |
5502 } else if (!s->fixed_qscale) { | |
5473 s->current_picture_ptr->quality= | 5503 s->current_picture_ptr->quality= |
5474 s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run); | 5504 s->current_picture.quality = ff_rate_estimate_qscale(s, dry_run); |
5475 if (s->current_picture.quality < 0) | 5505 if (s->current_picture.quality < 0) |
5476 return -1; | 5506 return -1; |
5477 } | 5507 } |