comparison mpegvideo.c @ 1539:14d01ccc0081 libavcodec

picture buffer cleanup, this fixes a assertion failure (if assertion checking was manualy enabled for utils.c) and a memleak at least
author michael
date Thu, 16 Oct 2003 00:21:54 +0000
parents 3b31998fe22f
children 576861d6343a
comparison
equal deleted inserted replaced
1538:2312ceb69d17 1539:14d01ccc0081
235 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan); 235 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
236 236
237 s->picture_structure= PICT_FRAME; 237 s->picture_structure= PICT_FRAME;
238 238
239 return 0; 239 return 0;
240 }
241
242 static void copy_picture(Picture *dst, Picture *src){
243 *dst = *src;
244 dst->type= FF_BUFFER_TYPE_COPY;
240 } 245 }
241 246
242 /** 247 /**
243 * allocates a Picture 248 * allocates a Picture
244 * The pixels are allocated/set by calling get_buffer() if shared=0 249 * The pixels are allocated/set by calling get_buffer() if shared=0
1045 s->current_picture_ptr->pict_type= s->pict_type; 1050 s->current_picture_ptr->pict_type= s->pict_type;
1046 // if(s->flags && CODEC_FLAG_QSCALE) 1051 // if(s->flags && CODEC_FLAG_QSCALE)
1047 // s->current_picture_ptr->quality= s->new_picture_ptr->quality; 1052 // s->current_picture_ptr->quality= s->new_picture_ptr->quality;
1048 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE; 1053 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE;
1049 1054
1050 s->current_picture= *s->current_picture_ptr; 1055 copy_picture(&s->current_picture, s->current_picture_ptr);
1051 1056
1052 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){ 1057 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
1053 if (s->pict_type != B_TYPE) { 1058 if (s->pict_type != B_TYPE) {
1054 s->last_picture_ptr= s->next_picture_ptr; 1059 s->last_picture_ptr= s->next_picture_ptr;
1055 s->next_picture_ptr= s->current_picture_ptr; 1060 s->next_picture_ptr= s->current_picture_ptr;
1056 } 1061 }
1057 1062
1058 if(s->last_picture_ptr) s->last_picture= *s->last_picture_ptr; 1063 if(s->last_picture_ptr) copy_picture(&s->last_picture, s->last_picture_ptr);
1059 if(s->next_picture_ptr) s->next_picture= *s->next_picture_ptr; 1064 if(s->next_picture_ptr) copy_picture(&s->next_picture, s->next_picture_ptr);
1060 if(s->new_picture_ptr ) s->new_picture = *s->new_picture_ptr; 1065
1061 1066 if(s->pict_type != I_TYPE && (s->last_picture_ptr==NULL || s->last_picture_ptr->data[0]==NULL)){
1062 if(s->pict_type != I_TYPE && s->last_picture_ptr==NULL){
1063 fprintf(stderr, "warning: first frame is no keyframe\n"); 1067 fprintf(stderr, "warning: first frame is no keyframe\n");
1064 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference 1068 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference
1065 goto alloc; 1069 goto alloc;
1066 } 1070 }
1067 1071
1397 pic->data[i]= pic_arg->data[i]; 1401 pic->data[i]= pic_arg->data[i];
1398 pic->linesize[i]= pic_arg->linesize[i]; 1402 pic->linesize[i]= pic_arg->linesize[i];
1399 } 1403 }
1400 alloc_picture(s, (Picture*)pic, 1); 1404 alloc_picture(s, (Picture*)pic, 1);
1401 }else{ 1405 }else{
1406 int offset= 16;
1402 i= find_unused_picture(s, 0); 1407 i= find_unused_picture(s, 0);
1403 1408
1404 pic= (AVFrame*)&s->picture[i]; 1409 pic= (AVFrame*)&s->picture[i];
1405 pic->reference= 3; 1410 pic->reference= 3;
1406 1411
1407 alloc_picture(s, (Picture*)pic, 0); 1412 alloc_picture(s, (Picture*)pic, 0);
1408 for(i=0; i<4; i++){ 1413
1409 /* the input will be 16 pixels to the right relative to the actual buffer start 1414 if( pic->data[0] + offset == pic_arg->data[0]
1410 * and the current_pic, so the buffer can be reused, yes its not beatifull 1415 && pic->data[1] + offset == pic_arg->data[1]
1411 */ 1416 && pic->data[2] + offset == pic_arg->data[2]){
1412 pic->data[i]+= 16;
1413 }
1414
1415 if( pic->data[0] == pic_arg->data[0]
1416 && pic->data[1] == pic_arg->data[1]
1417 && pic->data[2] == pic_arg->data[2]){
1418 // empty 1417 // empty
1419 }else{ 1418 }else{
1420 int h_chroma_shift, v_chroma_shift; 1419 int h_chroma_shift, v_chroma_shift;
1421 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); 1420 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
1422 1421
1426 int h_shift= i ? h_chroma_shift : 0; 1425 int h_shift= i ? h_chroma_shift : 0;
1427 int v_shift= i ? v_chroma_shift : 0; 1426 int v_shift= i ? v_chroma_shift : 0;
1428 int w= s->width >>h_shift; 1427 int w= s->width >>h_shift;
1429 int h= s->height>>v_shift; 1428 int h= s->height>>v_shift;
1430 uint8_t *src= pic_arg->data[i]; 1429 uint8_t *src= pic_arg->data[i];
1431 uint8_t *dst= pic->data[i]; 1430 uint8_t *dst= pic->data[i] + offset;
1432 1431
1433 if(src_stride==dst_stride) 1432 if(src_stride==dst_stride)
1434 memcpy(dst, src, src_stride*h); 1433 memcpy(dst, src, src_stride*h);
1435 else{ 1434 else{
1436 while(h--){ 1435 while(h--){
1548 } 1547 }
1549 1548
1550 if(s->reordered_input_picture[0]){ 1549 if(s->reordered_input_picture[0]){
1551 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0; 1550 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE ? 3 : 0;
1552 1551
1553 s->new_picture= *s->reordered_input_picture[0]; 1552 copy_picture(&s->new_picture, s->reordered_input_picture[0]);
1554 1553
1555 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){ 1554 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){
1556 // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable 1555 // input is a shared pix, so we cant modifiy it -> alloc a new one & ensure that the shared one is reuseable
1557 1556
1558 int i= find_unused_picture(s, 0); 1557 int i= find_unused_picture(s, 0);
1579 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER 1578 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER
1580 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL); 1579 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL);
1581 1580
1582 s->current_picture_ptr= s->reordered_input_picture[0]; 1581 s->current_picture_ptr= s->reordered_input_picture[0];
1583 for(i=0; i<4; i++){ 1582 for(i=0; i<4; i++){
1584 //reverse the +16 we did before storing the input 1583 s->new_picture.data[i]+=16;
1585 s->current_picture_ptr->data[i]-=16; 1584 }
1586 } 1585 }
1587 } 1586 copy_picture(&s->current_picture, s->current_picture_ptr);
1588 s->current_picture= *s->current_picture_ptr;
1589 1587
1590 s->picture_number= s->new_picture.display_picture_number; 1588 s->picture_number= s->new_picture.display_picture_number;
1591 //printf("dpn:%d\n", s->picture_number); 1589 //printf("dpn:%d\n", s->picture_number);
1592 }else{ 1590 }else{
1593 memset(&s->new_picture, 0, sizeof(Picture)); 1591 memset(&s->new_picture, 0, sizeof(Picture));