mbuffer.c
资源名称:chapter15.rar [点击查看]
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:136k
源码类别:
Audio
开发平台:
Visual C++
- dpb.max_long_term_pic_idx = max_long_term_frame_idx_plus1 - 1;
- // check for invalid frames
- for (i=0; i<dpb.ltref_frames_in_buffer; i++)
- {
- if (dpb.fs_ltref[i]->long_term_frame_idx > dpb.max_long_term_pic_idx)
- {
- unmark_for_long_term_reference(dpb.fs_ltref[i]);
- }
- }
- }
- /*!
- ************************************************************************
- * brief
- * Mark all long term reference pictures unused for reference
- ************************************************************************
- */
- static void mm_unmark_all_long_term_for_reference (void)
- {
- mm_update_max_long_term_frame_idx(0);
- }
- /*!
- ************************************************************************
- * brief
- * Mark all short term reference pictures unused for reference
- ************************************************************************
- */
- static void mm_unmark_all_short_term_for_reference (void)
- {
- unsigned int i;
- for (i=0; i<dpb.ref_frames_in_buffer; i++)
- {
- unmark_for_reference(dpb.fs_ref[i]);
- }
- update_ref_list();
- }
- /*!
- ************************************************************************
- * brief
- * Mark the current picture used for long term reference
- ************************************************************************
- */
- static void mm_mark_current_picture_long_term(StorablePicture *p, int long_term_frame_idx)
- {
- // remove long term pictures with same long_term_frame_idx
- if (p->structure == FRAME)
- {
- unmark_long_term_frame_for_reference_by_frame_idx(long_term_frame_idx);
- }
- else
- {
- unmark_long_term_field_for_reference_by_frame_idx(p->structure, long_term_frame_idx, 1, p->pic_num, 0);
- }
- p->is_long_term = 1;
- p->long_term_frame_idx = long_term_frame_idx;
- }
- /*!
- ************************************************************************
- * brief
- * Perform Adaptive memory control decoded reference picture marking process
- ************************************************************************
- */
- static void adaptive_memory_management(StorablePicture* p)
- {
- DecRefPicMarking_t *tmp_drpm;
- img->last_has_mmco_5 = 0;
- assert (!img->currentPicture->idr_flag);
- assert (img->adaptive_ref_pic_buffering_flag);
- while (img->dec_ref_pic_marking_buffer)
- {
- tmp_drpm = img->dec_ref_pic_marking_buffer;
- switch (tmp_drpm->memory_management_control_operation)
- {
- case 0:
- if (tmp_drpm->Next != NULL)
- {
- error ("memory_management_control_operation = 0 not last operation in buffer", 500);
- }
- break;
- case 1:
- mm_unmark_short_term_for_reference(p, tmp_drpm->difference_of_pic_nums_minus1);
- update_ref_list();
- break;
- case 2:
- mm_unmark_long_term_for_reference(p, tmp_drpm->long_term_pic_num);
- update_ltref_list();
- break;
- case 3:
- mm_assign_long_term_frame_idx(p, tmp_drpm->difference_of_pic_nums_minus1, tmp_drpm->long_term_frame_idx);
- update_ref_list();
- update_ltref_list();
- break;
- case 4:
- mm_update_max_long_term_frame_idx (tmp_drpm->max_long_term_frame_idx_plus1);
- update_ltref_list();
- break;
- case 5:
- mm_unmark_all_short_term_for_reference();
- mm_unmark_all_long_term_for_reference();
- img->last_has_mmco_5 = 1;
- break;
- case 6:
- mm_mark_current_picture_long_term(p, tmp_drpm->long_term_frame_idx);
- check_num_ref();
- break;
- default:
- error ("invalid memory_management_control_operation in buffer", 500);
- }
- img->dec_ref_pic_marking_buffer = tmp_drpm->Next;
- free (tmp_drpm);
- }
- if ( img->last_has_mmco_5 )
- {
- p->pic_num = p->frame_num = 0;
- switch (p->structure)
- {
- case TOP_FIELD:
- {
- p->poc = p->top_poc = img->toppoc =0;
- break;
- }
- case BOTTOM_FIELD:
- {
- p->poc = p->bottom_poc = img->bottompoc = 0;
- break;
- }
- case FRAME:
- {
- p->top_poc -= p->poc;
- p->bottom_poc -= p->poc;
- img->toppoc = p->top_poc;
- img->bottompoc = p->bottom_poc;
- p->poc = imin (p->top_poc, p->bottom_poc);
- img->framepoc = p->poc;
- break;
- }
- }
- img->ThisPOC = p->poc;
- flush_dpb();
- }
- }
- /*!
- ************************************************************************
- * brief
- * Store a picture in DPB. This includes cheking for space in DPB and
- * flushing frames.
- * If we received a frame, we need to check for a new store, if we
- * got a field, check if it's the second field of an already allocated
- * store.
- *
- * param p
- * Picture to be stored
- *
- ************************************************************************
- */
- void store_picture_in_dpb(StorablePicture* p)
- {
- unsigned i;
- int poc, pos;
- // diagnostics
- //printf ("Storing (%s) non-ref pic with frame_num #%dn", (p->type == FRAME)?"FRAME":(p->type == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num);
- // if frame, check for new store,
- assert (p!=NULL);
- p->used_for_reference = (img->nal_reference_idc != NALU_PRIORITY_DISPOSABLE);
- img->last_has_mmco_5=0;
- img->last_pic_bottom_field = (img->structure == BOTTOM_FIELD);
- if (img->currentPicture->idr_flag)
- idr_memory_management(p);
- else
- {
- // adaptive memory management
- if (p->used_for_reference && (img->adaptive_ref_pic_buffering_flag))
- adaptive_memory_management(p);
- }
- if ((p->structure==TOP_FIELD)||(p->structure==BOTTOM_FIELD))
- {
- // check for frame store with same pic_number
- if (dpb.last_picture)
- {
- if ((int)dpb.last_picture->frame_num == p->pic_num)
- {
- if (((p->structure==TOP_FIELD)&&(dpb.last_picture->is_used==2))||((p->structure==BOTTOM_FIELD)&&(dpb.last_picture->is_used==1)))
- {
- if ((p->used_for_reference && (dpb.last_picture->is_orig_reference!=0))||
- (!p->used_for_reference && (dpb.last_picture->is_orig_reference==0)))
- {
- insert_picture_in_dpb(dpb.last_picture, p);
- update_ref_list();
- update_ltref_list();
- dump_dpb();
- dpb.last_picture = NULL;
- return;
- }
- }
- }
- }
- }
- // this is a frame or a field which has no stored complementary field
- // sliding window, if necessary
- if ((!img->currentPicture->idr_flag)&&(p->used_for_reference && (!img->adaptive_ref_pic_buffering_flag)))
- {
- sliding_window_memory_management(p);
- }
- // first try to remove unused frames
- if (dpb.used_size==dpb.size)
- {
- remove_unused_frame_from_dpb();
- }
- // then output frames until one can be removed
- while (dpb.used_size==dpb.size)
- {
- // non-reference frames may be output directly
- if (!p->used_for_reference)
- {
- get_smallest_poc(&poc, &pos);
- if ((-1==pos) || (p->poc < poc))
- {
- direct_output(p, p_dec);
- return;
- }
- }
- // flush a frame
- output_one_frame_from_dpb();
- }
- // check for duplicate frame number in short term reference buffer
- if ((p->used_for_reference)&&(!p->is_long_term))
- {
- for (i=0; i<dpb.ref_frames_in_buffer; i++)
- {
- if (dpb.fs_ref[i]->frame_num == p->frame_num)
- {
- error("duplicate frame_num im short-term reference picture buffer", 500);
- }
- }
- }
- // store at end of buffer
- // printf ("store frame/field at pos %dn",dpb.used_size);
- insert_picture_in_dpb(dpb.fs[dpb.used_size],p);
- if (p->structure != FRAME)
- {
- dpb.last_picture = dpb.fs[dpb.used_size];
- }
- else
- {
- dpb.last_picture = NULL;
- }
- dpb.used_size++;
- update_ref_list();
- update_ltref_list();
- check_num_ref();
- dump_dpb();
- }
- /*!
- ************************************************************************
- * brief
- * Insert the frame picture into the if the top field has already
- * been stored for the coding decision
- *
- * param p
- * StorablePicture to be inserted
- *
- ************************************************************************
- */
- void replace_top_pic_with_frame(StorablePicture* p)
- {
- FrameStore* fs = NULL;
- unsigned i, found;
- assert (p!=NULL);
- assert (p->structure==FRAME);
- p->used_for_reference = (img->nal_reference_idc != NALU_PRIORITY_DISPOSABLE);
- // upsample a reference picture
- if (p->used_for_reference)
- {
- if( IS_INDEPENDENT(params) )
- {
- UnifiedOneForthPix_JV(0, p);
- UnifiedOneForthPix_JV(1, p);
- UnifiedOneForthPix_JV(2, p);
- }
- else
- {
- UnifiedOneForthPix(p);
- }
- }
- found=0;
- for (i=0;i<dpb.used_size;i++)
- {
- if((dpb.fs[i]->frame_num == img->frame_num)&&(dpb.fs[i]->is_used==1))
- {
- found=1;
- fs = dpb.fs[i];
- break;
- }
- }
- if (!found)
- {
- // this should only happen for non-reference pictures when the dpb is full of reference pics
- direct_output_paff(p, p_dec);
- }
- else
- {
- free_storable_picture(fs->top_field);
- fs->top_field=NULL;
- fs->frame=p;
- fs->is_used = 3;
- if (p->used_for_reference)
- {
- fs->is_reference = 3;
- if (p->is_long_term)
- {
- fs->is_long_term = 3;
- }
- }
- // generate field views
- dpb_split_field(fs);
- update_ref_list();
- update_ltref_list();
- }
- }
- /*!
- ************************************************************************
- * brief
- * Insert the picture into the DPB. A free DPB position is necessary
- * for frames, .
- *
- * param fs
- * FrameStore into which the picture will be inserted
- * param p
- * StorablePicture to be inserted
- *
- ************************************************************************
- */
- static void insert_picture_in_dpb(FrameStore* fs, StorablePicture* p)
- {
- // printf ("insert (%s) pic with frame_num #%d, poc %dn", (p->structure == FRAME)?"FRAME":(p->structure == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num, p->poc);
- assert (p!=NULL);
- assert (fs!=NULL);
- // upsample a reference picture
- if (p->used_for_reference)
- {
- if( IS_INDEPENDENT(params) )
- {
- UnifiedOneForthPix_JV(0, p);
- UnifiedOneForthPix_JV(1, p);
- UnifiedOneForthPix_JV(2, p);
- }
- else
- {
- UnifiedOneForthPix(p);
- }
- }
- switch (p->structure)
- {
- case FRAME:
- fs->frame = p;
- fs->is_used = 3;
- if (p->used_for_reference)
- {
- fs->is_reference = 3;
- fs->is_orig_reference = 3;
- if (p->is_long_term)
- {
- fs->is_long_term = 3;
- fs->long_term_frame_idx = p->long_term_frame_idx;
- }
- }
- // generate field views
- dpb_split_field(fs);
- break;
- case TOP_FIELD:
- fs->top_field = p;
- fs->is_used |= 1;
- if (p->used_for_reference)
- {
- fs->is_reference |= 1;
- fs->is_orig_reference |= 1;
- if (p->is_long_term)
- {
- fs->is_long_term |= 1;
- fs->long_term_frame_idx = p->long_term_frame_idx;
- }
- }
- if (fs->is_used == 3)
- {
- // generate frame view
- dpb_combine_field(fs);
- }
- else
- {
- fs->poc = p->poc;
- gen_field_ref_ids(p);
- }
- break;
- case BOTTOM_FIELD:
- fs->bottom_field = p;
- fs->is_used |= 2;
- if (p->used_for_reference)
- {
- fs->is_reference |= 2;
- fs->is_orig_reference |= 2;
- if (p->is_long_term)
- {
- fs->is_long_term |= 2;
- fs->long_term_frame_idx = p->long_term_frame_idx;
- }
- }
- if (fs->is_used == 3)
- {
- // generate frame view
- dpb_combine_field(fs);
- } else
- {
- fs->poc = p->poc;
- gen_field_ref_ids(p);
- }
- break;
- }
- fs->frame_num = p->pic_num;
- fs->is_output = p->is_output;
- }
- /*!
- ************************************************************************
- * brief
- * Check if one of the frames/fields in frame store is used for reference
- ************************************************************************
- */
- static int is_used_for_reference(FrameStore* fs)
- {
- if (fs->is_reference)
- {
- return 1;
- }
- if (fs->is_used == 3) // frame
- {
- if (fs->frame->used_for_reference)
- {
- return 1;
- }
- }
- if (fs->is_used & 1) // top field
- {
- if (fs->top_field)
- {
- if (fs->top_field->used_for_reference)
- {
- return 1;
- }
- }
- }
- if (fs->is_used & 2) // bottom field
- {
- if (fs->bottom_field)
- {
- if (fs->bottom_field->used_for_reference)
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /*!
- ************************************************************************
- * brief
- * Check if one of the frames/fields in frame store is used for short-term reference
- ************************************************************************
- */
- static int is_short_term_reference(FrameStore* fs)
- {
- if (fs->is_used==3) // frame
- {
- if ((fs->frame->used_for_reference)&&(!fs->frame->is_long_term))
- {
- return 1;
- }
- }
- if (fs->is_used & 1) // top field
- {
- if (fs->top_field)
- {
- if ((fs->top_field->used_for_reference)&&(!fs->top_field->is_long_term))
- {
- return 1;
- }
- }
- }
- if (fs->is_used & 2) // bottom field
- {
- if (fs->bottom_field)
- {
- if ((fs->bottom_field->used_for_reference)&&(!fs->bottom_field->is_long_term))
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /*!
- ************************************************************************
- * brief
- * Check if one of the frames/fields in frame store is used for short-term reference
- ************************************************************************
- */
- static int is_long_term_reference(FrameStore* fs)
- {
- if (fs->is_used==3) // frame
- {
- if ((fs->frame->used_for_reference)&&(fs->frame->is_long_term))
- {
- return 1;
- }
- }
- if (fs->is_used & 1) // top field
- {
- if (fs->top_field)
- {
- if ((fs->top_field->used_for_reference)&&(fs->top_field->is_long_term))
- {
- return 1;
- }
- }
- }
- if (fs->is_used & 2) // bottom field
- {
- if (fs->bottom_field)
- {
- if ((fs->bottom_field->used_for_reference)&&(fs->bottom_field->is_long_term))
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /*!
- ************************************************************************
- * brief
- * remove one frame from DPB
- ************************************************************************
- */
- static void remove_frame_from_dpb(int pos)
- {
- FrameStore* fs = dpb.fs[pos];
- FrameStore* tmp;
- unsigned i;
- // printf ("remove frame with frame_num #%dn", fs->frame_num);
- switch (fs->is_used)
- {
- case 3:
- free_storable_picture(fs->frame);
- free_storable_picture(fs->top_field);
- free_storable_picture(fs->bottom_field);
- fs->frame=NULL;
- fs->top_field=NULL;
- fs->bottom_field=NULL;
- break;
- case 2:
- free_storable_picture(fs->bottom_field);
- fs->bottom_field=NULL;
- break;
- case 1:
- free_storable_picture(fs->top_field);
- fs->top_field=NULL;
- break;
- case 0:
- break;
- default:
- error("invalid frame store type",500);
- }
- fs->is_used = 0;
- fs->is_long_term = 0;
- fs->is_reference = 0;
- fs->is_orig_reference = 0;
- // move empty framestore to end of buffer
- tmp = dpb.fs[pos];
- for (i=pos; i<dpb.used_size-1;i++)
- {
- dpb.fs[i] = dpb.fs[i+1];
- }
- dpb.fs[dpb.used_size-1] = tmp;
- dpb.used_size--;
- }
- /*!
- ************************************************************************
- * brief
- * find smallest POC in the DPB.
- ************************************************************************
- */
- static void get_smallest_poc(int *poc,int * pos)
- {
- unsigned i;
- if (dpb.used_size<1)
- {
- error("Cannot determine smallest POC, DPB empty.",150);
- }
- *pos=-1;
- *poc = INT_MAX;
- for (i=0; i<dpb.used_size; i++)
- {
- if ((*poc>dpb.fs[i]->poc)&&(!dpb.fs[i]->is_output))
- {
- *poc = dpb.fs[i]->poc;
- *pos=i;
- }
- }
- }
- /*!
- ************************************************************************
- * brief
- * Remove a picture from DPB which is no longer needed.
- ************************************************************************
- */
- static int remove_unused_frame_from_dpb(void)
- {
- unsigned i;
- // check for frames that were already output and no longer used for reference
- for (i=0; i<dpb.used_size; i++)
- {
- if (dpb.fs[i]->is_output && (!is_used_for_reference(dpb.fs[i])))
- {
- remove_frame_from_dpb(i);
- return 1;
- }
- }
- return 0;
- }
- /*!
- ************************************************************************
- * brief
- * Output one picture stored in the DPB.
- ************************************************************************
- */
- static void output_one_frame_from_dpb(void)
- {
- int poc, pos;
- //diagnostics
- if (dpb.used_size<1)
- {
- error("Cannot output frame, DPB empty.",150);
- }
- // find smallest POC
- get_smallest_poc(&poc, &pos);
- if(pos==-1)
- {
- error("no frames for output available", 150);
- }
- // call the output function
- // printf ("output frame with frame_num #%d, poc %d (dpb. dpb.size=%d, dpb.used_size=%d)n", dpb.fs[pos]->frame_num, dpb.fs[pos]->frame->poc, dpb.size, dpb.used_size);
- write_stored_frame(dpb.fs[pos], p_dec);
- // if redundant picture in use, output POC may be not in ascending order
- if(params->redundant_pic_flag == 0)
- {
- if (dpb.last_output_poc >= poc)
- {
- error ("output POC must be in ascending order", 150);
- }
- }
- dpb.last_output_poc = poc;
- // free frame store and move empty store to end of buffer
- if (!is_used_for_reference(dpb.fs[pos]))
- {
- remove_frame_from_dpb(pos);
- }
- }
- /*!
- ************************************************************************
- * brief
- * All stored picture are output. Should be called to empty the buffer
- ************************************************************************
- */
- void flush_dpb(void)
- {
- unsigned i;
- //diagnostics
- // printf("Flush remaining frames from dpb. dpb.size=%d, dpb.used_size=%dn",dpb.size,dpb.used_size);
- // mark all frames unused
- for (i=0; i<dpb.used_size; i++)
- {
- unmark_for_reference (dpb.fs[i]);
- }
- while (remove_unused_frame_from_dpb()) ;
- // output frames in POC order
- while (dpb.used_size)
- {
- output_one_frame_from_dpb();
- }
- dpb.last_output_poc = INT_MIN;
- }
- void gen_field_ref_ids(StorablePicture *p)
- {
- int i,j, dummylist0, dummylist1;
- //! Generate Frame parameters from field information.
- for (i=0 ; i<p->size_x/4 ; i++)
- {
- for (j=0 ; j<p->size_y/4 ; j++)
- {
- dummylist0= p->motion.ref_idx[LIST_0][j][i];
- dummylist1= p->motion.ref_idx[LIST_1][j][i];
- //! association with id already known for fields.
- p->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? p->ref_pic_num[LIST_0][dummylist0] : 0;
- p->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? p->ref_pic_num[LIST_1][dummylist1] : 0;
- p->motion.field_frame[j][i]=1;
- }
- }
- }
- /*!
- ************************************************************************
- * brief
- * Extract top field from a frame
- ************************************************************************
- */
- void dpb_split_field(FrameStore *fs)
- {
- int i, j, ii, jj, jj4;
- int idiv,jdiv;
- int currentmb;
- int dummylist0,dummylist1;
- int twosz16 = 2*(fs->frame->size_x>>4);
- fs->poc = fs->frame->poc;
- if (!active_sps->frame_mbs_only_flag)
- {
- fs->top_field = alloc_storable_picture(TOP_FIELD, fs->frame->size_x, fs->frame->size_y/2, fs->frame->size_x_cr, fs->frame->size_y_cr/2);
- fs->bottom_field = alloc_storable_picture(BOTTOM_FIELD, fs->frame->size_x, fs->frame->size_y/2, fs->frame->size_x_cr, fs->frame->size_y_cr/2);
- for (i=0; i<fs->frame->size_y/2; i++)
- {
- memcpy(fs->top_field->imgY[i], fs->frame->imgY[i*2], fs->frame->size_x*sizeof(imgpel));
- }
- for (i=0; i<fs->frame->size_y_cr/2; i++)
- {
- memcpy(fs->top_field->imgUV[0][i], fs->frame->imgUV[0][i*2], fs->frame->size_x_cr*sizeof(imgpel));
- memcpy(fs->top_field->imgUV[1][i], fs->frame->imgUV[1][i*2], fs->frame->size_x_cr*sizeof(imgpel));
- }
- for (i=0; i<fs->frame->size_y/2; i++)
- {
- memcpy(fs->bottom_field->imgY[i], fs->frame->imgY[i*2 + 1], fs->frame->size_x*sizeof(imgpel));
- }
- for (i=0; i<fs->frame->size_y_cr/2; i++)
- {
- memcpy(fs->bottom_field->imgUV[0][i], fs->frame->imgUV[0][i*2 + 1], fs->frame->size_x_cr*sizeof(imgpel));
- memcpy(fs->bottom_field->imgUV[1][i], fs->frame->imgUV[1][i*2 + 1], fs->frame->size_x_cr*sizeof(imgpel));
- }
- if( IS_INDEPENDENT(params) )
- {
- UnifiedOneForthPix_JV(0, fs->top_field);
- UnifiedOneForthPix_JV(1, fs->top_field);
- UnifiedOneForthPix_JV(2, fs->top_field);
- UnifiedOneForthPix_JV(0, fs->bottom_field);
- UnifiedOneForthPix_JV(1, fs->bottom_field);
- UnifiedOneForthPix_JV(2, fs->bottom_field);
- }
- else
- {
- UnifiedOneForthPix(fs->top_field);
- UnifiedOneForthPix(fs->bottom_field);
- }
- fs->top_field->poc = fs->frame->top_poc;
- fs->bottom_field->poc = fs->frame->bottom_poc;
- fs->top_field->frame_poc = fs->frame->frame_poc;
- fs->top_field->bottom_poc =fs->bottom_field->bottom_poc = fs->frame->bottom_poc;
- fs->top_field->top_poc =fs->bottom_field->top_poc = fs->frame->top_poc;
- fs->bottom_field->frame_poc = fs->frame->frame_poc;
- fs->top_field->used_for_reference = fs->bottom_field->used_for_reference
- = fs->frame->used_for_reference;
- fs->top_field->is_long_term = fs->bottom_field->is_long_term
- = fs->frame->is_long_term;
- fs->long_term_frame_idx = fs->top_field->long_term_frame_idx
- = fs->bottom_field->long_term_frame_idx
- = fs->frame->long_term_frame_idx;
- fs->top_field->coded_frame = fs->bottom_field->coded_frame = 1;
- fs->top_field->MbaffFrameFlag = fs->bottom_field->MbaffFrameFlag
- = fs->frame->MbaffFrameFlag;
- fs->frame->top_field = fs->top_field;
- fs->frame->bottom_field = fs->bottom_field;
- fs->top_field->bottom_field = fs->bottom_field;
- fs->top_field->frame = fs->frame;
- fs->bottom_field->top_field = fs->top_field;
- fs->bottom_field->frame = fs->frame;
- fs->top_field->chroma_format_idc = fs->bottom_field->chroma_format_idc = fs->frame->chroma_format_idc;
- //store reference picture index
- memcpy(fs->top_field->ref_pic_num[LIST_1] , fs->frame->ref_pic_num[2 + LIST_1], 2*listXsize[LIST_1] * sizeof(int64));
- memcpy(fs->bottom_field->ref_pic_num[LIST_1], fs->frame->ref_pic_num[4 + LIST_1], 2*listXsize[LIST_1] * sizeof(int64));
- memcpy(fs->top_field->ref_pic_num[LIST_0] , fs->frame->ref_pic_num[2 + LIST_0], 2*listXsize[LIST_0] * sizeof(int64));
- memcpy(fs->bottom_field->ref_pic_num[LIST_0], fs->frame->ref_pic_num[4 + LIST_0], 2*listXsize[LIST_0] * sizeof(int64));
- }
- else
- {
- fs->top_field=NULL;
- fs->bottom_field=NULL;
- fs->frame->top_field=NULL;
- fs->frame->bottom_field=NULL;
- }
- for (j=0 ; j<fs->frame->size_y/4 ; j++)
- {
- jdiv=j/4;
- for (i=0 ; i<fs->frame->size_x/4 ; i++)
- {
- idiv=i/4;
- currentmb = twosz16*(jdiv/2)+ (idiv)*2 + (jdiv%2);
- if (fs->frame->MbaffFrameFlag && fs->frame->motion.mb_field[currentmb])
- {
- int list_offset = currentmb%2? 4: 2;
- dummylist0 = fs->frame->motion.ref_idx[LIST_0][j][i];
- dummylist1 = fs->frame->motion.ref_idx[LIST_1][j][i];
- //! association with id already known for fields.
- fs->frame->motion.ref_id[LIST_0 + list_offset][j][i] = (dummylist0>=0)? fs->frame->ref_pic_num[LIST_0 + list_offset][dummylist0] : 0;
- fs->frame->motion.ref_id[LIST_1 + list_offset][j][i] = (dummylist1>=0)? fs->frame->ref_pic_num[LIST_1 + list_offset][dummylist1] : 0;
- //! need to make association with frames
- fs->frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->frm_ref_pic_num[LIST_0 + list_offset][dummylist0] : 0;
- fs->frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->frm_ref_pic_num[LIST_1 + list_offset][dummylist1] : 0;
- }
- else
- {
- dummylist0 = fs->frame->motion.ref_idx[LIST_0][j][i];
- dummylist1 = fs->frame->motion.ref_idx[LIST_1][j][i];
- fs->frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->ref_pic_num[LIST_0][dummylist0] : -1;
- fs->frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->ref_pic_num[LIST_1][dummylist1] : -1;
- }
- }
- }
- if (!active_sps->frame_mbs_only_flag && fs->frame->MbaffFrameFlag)
- {
- for (j=0 ; j<fs->frame->size_y/8; j++)
- {
- jj = (j/4)*8 + j%4;
- jj4 = jj + 4;
- jdiv=j/2;
- for (i=0 ; i<fs->frame->size_x/4 ; i++)
- {
- idiv=i/4;
- currentmb = twosz16*(jdiv/2)+ (idiv)*2 + (jdiv%2);
- // Assign field mvs attached to MB-Frame buffer to the proper buffer
- if (fs->frame->motion.mb_field[currentmb])
- {
- fs->bottom_field->motion.field_frame[j][i] = fs->top_field->motion.field_frame[j][i]=1;
- fs->frame->motion.field_frame[2*j][i] = fs->frame->motion.field_frame[2*j+1][i]=1;
- fs->bottom_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj4][i][0];
- fs->bottom_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj4][i][1];
- fs->bottom_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj4][i][0];
- fs->bottom_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj4][i][1];
- fs->bottom_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj4][i];
- fs->bottom_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj4][i];
- fs->bottom_field->motion.ref_id[LIST_0][j][i] = fs->frame->motion.ref_id[LIST_0+4][jj4][i];
- fs->bottom_field->motion.ref_id[LIST_1][j][i] = fs->frame->motion.ref_id[LIST_1+4][jj4][i];
- fs->top_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj][i][0];
- fs->top_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj][i][1];
- fs->top_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj][i][0];
- fs->top_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj][i][1];
- fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj][i];
- fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj][i];
- fs->top_field->motion.ref_id[LIST_0][j][i] = fs->frame->motion.ref_id[LIST_0+2][jj][i];
- fs->top_field->motion.ref_id[LIST_1][j][i] = fs->frame->motion.ref_id[LIST_1+2][jj][i];
- }
- }
- }
- }
- //! Generate field MVs from Frame MVs
- if (!active_sps->frame_mbs_only_flag)
- {
- for (j=0 ; j<fs->frame->size_y/8 ; j++)
- {
- jj = 2* RSD(j);
- jdiv = j/2;
- for (i=0 ; i<fs->frame->size_x/4 ; i++)
- {
- ii = RSD(i);
- idiv = i/4;
- currentmb = twosz16*(jdiv/2)+ (idiv)*2 + (jdiv%2);
- if (!fs->frame->MbaffFrameFlag || !fs->frame->motion.mb_field[currentmb])
- {
- fs->frame->motion.field_frame[2*j+1][i] = fs->frame->motion.field_frame[2*j][i]=0;
- fs->top_field->motion.field_frame[j][i] = fs->bottom_field->motion.field_frame[j][i] = 0;
- fs->top_field->motion.mv[LIST_0][j][i][0] = fs->bottom_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj][ii][0];
- fs->top_field->motion.mv[LIST_0][j][i][1] = fs->bottom_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj][ii][1];
- fs->top_field->motion.mv[LIST_1][j][i][0] = fs->bottom_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj][ii][0];
- fs->top_field->motion.mv[LIST_1][j][i][1] = fs->bottom_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj][ii][1];
- // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
- if (fs->frame->motion.ref_idx[LIST_0][jj][ii] == -1)
- fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->bottom_field->motion.ref_idx[LIST_0][j][i] = - 1;
- else
- {
- dummylist0=fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->bottom_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj][ii];
- fs->top_field ->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->top_ref_pic_num[LIST_0][dummylist0] : 0;
- fs->bottom_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->bottom_ref_pic_num[LIST_0][dummylist0] : 0;
- }
- if (fs->frame->motion.ref_idx[LIST_1][jj][ii] == -1)
- fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->bottom_field->motion.ref_idx[LIST_1][j][i] = - 1;
- else
- {
- dummylist1=fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->bottom_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj][ii];
- fs->top_field ->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->top_ref_pic_num[LIST_1][dummylist1] : 0;
- fs->bottom_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->bottom_ref_pic_num[LIST_1][dummylist1] : 0;
- }
- }
- else
- {
- fs->frame->motion.field_frame[2*j+1][i] = fs->frame->motion.field_frame[2*j][i]= fs->frame->motion.mb_field[currentmb];
- }
- }
- }
- }
- else
- {
- memset( &(fs->frame->motion.field_frame[0][0]), 0, fs->frame->size_y * (fs->frame->size_x >>4) * sizeof(byte));
- }
- }
- /*!
- ************************************************************************
- * brief
- * Generate a frame from top and bottom fields,
- * YUV components and display information only
- ************************************************************************
- */
- void dpb_combine_field_yuv(FrameStore *fs)
- {
- int i;
- fs->frame = alloc_storable_picture(FRAME, fs->top_field->size_x, fs->top_field->size_y*2, fs->top_field->size_x_cr, fs->top_field->size_y_cr*2);
- for (i=0; i<fs->top_field->size_y; i++)
- {
- memcpy(fs->frame->imgY[i*2], fs->top_field->imgY[i] , fs->top_field->size_x*sizeof(imgpel)); // top field
- memcpy(fs->frame->imgY[i*2 + 1], fs->bottom_field->imgY[i], fs->bottom_field->size_x*sizeof(imgpel)); // bottom field
- }
- for (i=0; i<fs->top_field->size_y_cr; i++)
- {
- memcpy(fs->frame->imgUV[0][i*2], fs->top_field->imgUV[0][i], fs->top_field->size_x_cr*sizeof(imgpel));
- memcpy(fs->frame->imgUV[0][i*2 + 1], fs->bottom_field->imgUV[0][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
- memcpy(fs->frame->imgUV[1][i*2], fs->top_field->imgUV[1][i], fs->top_field->size_x_cr*sizeof(imgpel));
- memcpy(fs->frame->imgUV[1][i*2 + 1], fs->bottom_field->imgUV[1][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
- }
- fs->poc=fs->frame->poc =fs->frame->frame_poc = imin (fs->top_field->poc, fs->bottom_field->poc);
- fs->bottom_field->frame_poc=fs->top_field->frame_poc=fs->frame->poc;
- fs->bottom_field->top_poc=fs->frame->top_poc=fs->top_field->poc;
- fs->top_field->bottom_poc=fs->frame->bottom_poc=fs->bottom_field->poc;
- fs->frame->used_for_reference = (fs->top_field->used_for_reference && fs->bottom_field->used_for_reference );
- fs->frame->is_long_term = (fs->top_field->is_long_term && fs->bottom_field->is_long_term );
- if (fs->frame->is_long_term)
- fs->frame->long_term_frame_idx = fs->long_term_frame_idx;
- fs->frame->top_field = fs->top_field;
- fs->frame->bottom_field = fs->bottom_field;
- fs->frame->coded_frame = 0;
- fs->frame->chroma_format_idc = fs->top_field->chroma_format_idc;
- fs->frame->frame_cropping_flag = fs->top_field->frame_cropping_flag;
- if (fs->frame->frame_cropping_flag)
- {
- fs->frame->frame_cropping_rect_top_offset = fs->top_field->frame_cropping_rect_top_offset;
- fs->frame->frame_cropping_rect_bottom_offset = fs->top_field->frame_cropping_rect_bottom_offset;
- fs->frame->frame_cropping_rect_left_offset = fs->top_field->frame_cropping_rect_left_offset;
- fs->frame->frame_cropping_rect_right_offset = fs->top_field->frame_cropping_rect_right_offset;
- }
- fs->top_field->frame = fs->bottom_field->frame = fs->frame;
- }
- /*!
- ************************************************************************
- * brief
- * Generate a frame from top and bottom fields
- ************************************************************************
- */
- void dpb_combine_field(FrameStore *fs)
- {
- int i,j, jj, jj4;
- int dummylist0, dummylist1;
- dpb_combine_field_yuv(fs);
- if( IS_INDEPENDENT(params) )
- {
- UnifiedOneForthPix_JV(0, fs->frame);
- UnifiedOneForthPix_JV(1, fs->frame);
- UnifiedOneForthPix_JV(2, fs->frame);
- }
- else
- {
- UnifiedOneForthPix(fs->frame);
- }
- //combine field for frame
- for (i=0;i<(listXsize[LIST_1]+1)/2;i++)
- {
- fs->frame->ref_pic_num[LIST_1][i]= i64min ((fs->top_field->ref_pic_num[LIST_1][2*i]/2)*2, (fs->bottom_field->ref_pic_num[LIST_1][2*i]/2)*2);
- }
- for (i=0;i<(listXsize[LIST_0]+1)/2;i++)
- {
- fs->frame->ref_pic_num[LIST_0][i]= i64min ((fs->top_field->ref_pic_num[LIST_0][2*i]/2)*2, (fs->bottom_field->ref_pic_num[LIST_0][2*i]/2)*2);
- }
- //! Use inference flag to remap mvs/references
- //! Generate Frame parameters from field information.
- for (j=0 ; j<fs->top_field->size_y/4 ; j++)
- {
- jj = 8*(j/4) + (j%4);
- jj4 = jj + 4;
- for (i=0 ; i<fs->top_field->size_x/4 ; i++)
- {
- fs->frame->motion.field_frame[jj][i]= fs->frame->motion.field_frame[jj4][i]=1;
- fs->frame->motion.mv[LIST_0][jj][i][0] = fs->top_field->motion.mv[LIST_0][j][i][0];
- fs->frame->motion.mv[LIST_0][jj][i][1] = fs->top_field->motion.mv[LIST_0][j][i][1] ;
- fs->frame->motion.mv[LIST_1][jj][i][0] = fs->top_field->motion.mv[LIST_1][j][i][0];
- fs->frame->motion.mv[LIST_1][jj][i][1] = fs->top_field->motion.mv[LIST_1][j][i][1] ;
- dummylist0=fs->frame->motion.ref_idx[LIST_0][jj][i] = fs->top_field->motion.ref_idx[LIST_0][j][i];
- dummylist1=fs->frame->motion.ref_idx[LIST_1][jj][i] = fs->top_field->motion.ref_idx[LIST_1][j][i];
- //! association with id already known for fields.
- fs->top_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->top_field->ref_pic_num[LIST_0][dummylist0] : 0;
- fs->top_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->top_field->ref_pic_num[LIST_1][dummylist1] : 0;
- //! need to make association with frames
- fs->frame->motion.ref_id[LIST_0][jj][i] = (dummylist0>=0)? fs->top_field->frm_ref_pic_num[LIST_0][dummylist0] : 0;
- fs->frame->motion.ref_id[LIST_1][jj][i] = (dummylist1>=0)? fs->top_field->frm_ref_pic_num[LIST_1][dummylist1] : 0;
- fs->frame->motion.mv[LIST_0][jj4][i][0] = fs->bottom_field->motion.mv[LIST_0][j][i][0];
- fs->frame->motion.mv[LIST_0][jj4][i][1] = fs->bottom_field->motion.mv[LIST_0][j][i][1] ;
- fs->frame->motion.mv[LIST_1][jj4][i][0] = fs->bottom_field->motion.mv[LIST_1][j][i][0];
- fs->frame->motion.mv[LIST_1][jj4][i][1] = fs->bottom_field->motion.mv[LIST_1][j][i][1] ;
- dummylist0=fs->frame->motion.ref_idx[LIST_0][jj4][i] = fs->bottom_field->motion.ref_idx[LIST_0][j][i];
- dummylist1=fs->frame->motion.ref_idx[LIST_1][jj4][i] = fs->bottom_field->motion.ref_idx[LIST_1][j][i];
- fs->bottom_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->bottom_field->ref_pic_num[LIST_0][dummylist0] : 0;
- fs->bottom_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->bottom_field->ref_pic_num[LIST_1][dummylist1] : 0;
- //! need to make association with frames
- fs->frame->motion.ref_id[LIST_0][jj4][i] = (dummylist0>=0)? fs->bottom_field->frm_ref_pic_num[LIST_0][dummylist0] : -1;
- fs->frame->motion.ref_id[LIST_1][jj4][i] = (dummylist1>=0)? fs->bottom_field->frm_ref_pic_num[LIST_1][dummylist1] : -1;
- fs->top_field->motion.field_frame[j][i]=1;
- fs->bottom_field->motion.field_frame[j][i]=1;
- }
- }
- }
- /*!
- ************************************************************************
- * brief
- * Allocate memory for buffering of reference picture reordering commands
- ************************************************************************
- */
- void alloc_ref_pic_list_reordering_buffer(Slice *currSlice)
- {
- int size = img->num_ref_idx_l0_active+1;
- if (img->type!=I_SLICE && img->type!=SI_SLICE)
- {
- if ((currSlice->reordering_of_pic_nums_idc_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: remapping_of_pic_nums_idc_l0");
- if ((currSlice->abs_diff_pic_num_minus1_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l0");
- if ((currSlice->long_term_pic_idx_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l0");
- }
- else
- {
- currSlice->reordering_of_pic_nums_idc_l0 = NULL;
- currSlice->abs_diff_pic_num_minus1_l0 = NULL;
- currSlice->long_term_pic_idx_l0 = NULL;
- }
- size = img->num_ref_idx_l1_active+1;
- if (img->type==B_SLICE)
- {
- if ((currSlice->reordering_of_pic_nums_idc_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: remapping_of_pic_nums_idc_l1");
- if ((currSlice->abs_diff_pic_num_minus1_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l1");
- if ((currSlice->long_term_pic_idx_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l1");
- }
- else
- {
- currSlice->reordering_of_pic_nums_idc_l1 = NULL;
- currSlice->abs_diff_pic_num_minus1_l1 = NULL;
- currSlice->long_term_pic_idx_l1 = NULL;
- }
- }
- /*!
- ************************************************************************
- * brief
- * Free memory for buffering of reference picture reordering commands
- ************************************************************************
- */
- void free_ref_pic_list_reordering_buffer(Slice *currSlice)
- {
- if (currSlice->reordering_of_pic_nums_idc_l0)
- free(currSlice->reordering_of_pic_nums_idc_l0);
- if (currSlice->abs_diff_pic_num_minus1_l0)
- free(currSlice->abs_diff_pic_num_minus1_l0);
- if (currSlice->long_term_pic_idx_l0)
- free(currSlice->long_term_pic_idx_l0);
- currSlice->reordering_of_pic_nums_idc_l0 = NULL;
- currSlice->abs_diff_pic_num_minus1_l0 = NULL;
- currSlice->long_term_pic_idx_l0 = NULL;
- if (currSlice->reordering_of_pic_nums_idc_l1)
- free(currSlice->reordering_of_pic_nums_idc_l1);
- if (currSlice->abs_diff_pic_num_minus1_l1)
- free(currSlice->abs_diff_pic_num_minus1_l1);
- if (currSlice->long_term_pic_idx_l1)
- free(currSlice->long_term_pic_idx_l1);
- currSlice->reordering_of_pic_nums_idc_l1 = NULL;
- currSlice->abs_diff_pic_num_minus1_l1 = NULL;
- currSlice->long_term_pic_idx_l1 = NULL;
- }
- /*!
- ************************************************************************
- * brief
- * Tian Dong
- * June 13, 2002, Modifed on July 30, 2003
- *
- * If a gap in frame_num is found, try to fill the gap
- * param img
- *
- ************************************************************************
- */
- void fill_frame_num_gap(ImageParameters *img)
- {
- int CurrFrameNum;
- int UnusedShortTermFrameNum;
- StorablePicture *picture = NULL;
- int nal_ref_idc_bak;
- // printf("A gap in frame number is found, try to fill it.n");
- nal_ref_idc_bak = img->nal_reference_idc;
- img->nal_reference_idc = NALU_PRIORITY_LOW;
- UnusedShortTermFrameNum = (img->pre_frame_num + 1) % max_frame_num;
- CurrFrameNum = img->frame_num;
- while (CurrFrameNum != UnusedShortTermFrameNum)
- {
- picture = alloc_storable_picture (FRAME, img->width, img->height, img->width_cr, img->height_cr);
- picture->coded_frame = 1;
- picture->pic_num = UnusedShortTermFrameNum;
- picture->non_existing = 1;
- picture->is_output = 1;
- img->adaptive_ref_pic_buffering_flag = 0;
- store_picture_in_dpb(picture);
- picture=NULL;
- UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % max_frame_num;
- }
- img->nal_reference_idc = nal_ref_idc_bak;
- }
- /*!
- ************************************************************************
- * brief
- * Allocate motion parameter memory for colocated structure
- *
- ************************************************************************
- */
- void alloc_motion_params(MotionParams *ftype, int size_y, int size_x)
- {
- get_mem3Dint64 (&(ftype->ref_pic_id), 2, size_y, size_x);
- get_mem4Dshort (&(ftype->mv) , 2, size_y, size_x, 2);
- get_mem3D ((byte****)(&(ftype->ref_idx)) , 2, size_y, size_x);
- get_mem2D (&(ftype->moving_block) , size_y, size_x);
- }
- /*!
- ************************************************************************
- * brief
- * Allocate co-located memory
- *
- * param size_x
- * horizontal luma size
- * param size_y
- * vertical luma size
- * param mb_adaptive_frame_field_flag
- * flag that indicates macroblock adaptive frame/field coding
- *
- * return
- * the allocated StorablePicture structure
- ************************************************************************
- */
- ColocatedParams* alloc_colocated(int size_x, int size_y, int mb_adaptive_frame_field_flag)
- {
- ColocatedParams *s;
- s = calloc(1, sizeof(ColocatedParams));
- if (NULL == s)
- no_mem_exit("alloc_colocated: s");
- s->size_x = size_x;
- s->size_y = size_y;
- alloc_motion_params(&s->frame, size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
- if (mb_adaptive_frame_field_flag)
- {
- alloc_motion_params(&s->top , size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
- alloc_motion_params(&s->bottom, size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
- }
- s->mb_adaptive_frame_field_flag = mb_adaptive_frame_field_flag;
- return s;
- }
- /*!
- ************************************************************************
- * brief
- * Free motion parameter memory for colocated structure
- *
- ************************************************************************
- */
- void free_motion_params(MotionParams *ftype)
- {
- free_mem3Dint64 (ftype->ref_pic_id);
- free_mem3D ((byte***)ftype->ref_idx);
- free_mem4Dshort (ftype->mv);
- if (ftype->moving_block)
- {
- free_mem2D (ftype->moving_block);
- ftype->moving_block=NULL;
- }
- }
- /*!
- ************************************************************************
- * brief
- * Free co-located memory.
- *
- * param p
- * Picture to be freed
- *
- ************************************************************************
- */
- void free_colocated(ColocatedParams* p)
- {
- if (p)
- {
- free_motion_params(&p->frame);
- if (p->mb_adaptive_frame_field_flag)
- {
- free_motion_params(&p->top );
- free_motion_params(&p->bottom);
- }
- free(p);
- p=NULL;
- }
- }
- /*!
- ************************************************************************
- * brief
- * Compute co-located motion info
- *
- ************************************************************************
- */
- void compute_colocated(ColocatedParams* p, StorablePicture **listX[6])
- {
- StorablePicture *fs, *fs_top, *fs_bottom;
- MotionParams *ftype = NULL;
- int i,j, ii, jj, jdiv;
- fs_top = fs_bottom = fs = listX[LIST_1 ][0];
- if (img->MbaffFrameFlag)
- {
- fs_top= listX[LIST_1 + 2][0];
- fs_bottom= listX[LIST_1 + 4][0];
- }
- else
- {
- if (img->structure!=FRAME)
- {
- if ((img->structure != fs->structure) && (fs->coded_frame))
- {
- if (img->structure==TOP_FIELD)
- {
- fs_top = fs_bottom = fs = listX[LIST_1 ][0]->top_field;
- }
- else
- {
- fs_top = fs_bottom = fs = listX[LIST_1 ][0]->bottom_field;
- }
- }
- }
- }
- if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/4 ; j++)
- {
- jdiv = j>>1;
- jj = j/2 + 4 * (j/8);
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- if (img->MbaffFrameFlag && fs->motion.field_frame[j][i])
- {
- //! Assign frame buffers for field MBs
- //! Check whether we should use top or bottom field mvs.
- //! Depending on the assigned poc values.
- if (iabs(enc_picture->poc - fs_bottom->poc) > iabs(enc_picture->poc - fs_top->poc) )
- {
- ftype->mv[LIST_0][j][i][0] = fs_top->motion.mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs_top->motion.mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs_top->motion.mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs_top->motion.mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs_top->motion.ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs_top->motion.ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj][i];
- p->is_long_term = fs_top->is_long_term;
- }
- else
- {
- ftype->mv[LIST_0][j][i][0] = fs_bottom->motion.mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs_bottom->motion.mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs_bottom->motion.mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs_bottom->motion.mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs_bottom->motion.ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs_bottom->motion.ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj + 4][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj + 4][i];
- p->is_long_term = fs_bottom->is_long_term;
- }
- }
- else
- {
- ftype->mv[LIST_0][j][i][0] = fs->motion.mv[LIST_0][j][i][0];
- ftype->mv[LIST_0][j][i][1] = fs->motion.mv[LIST_0][j][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs->motion.mv[LIST_1][j][i][0];
- ftype->mv[LIST_1][j][i][1] = fs->motion.mv[LIST_1][j][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs->motion.ref_idx[LIST_0][j][i];
- ftype->ref_idx[LIST_1][j][i] = fs->motion.ref_idx[LIST_1][j][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][j][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][j][i];
- p->is_long_term = fs->is_long_term;
- }
- }
- }
- }
- //! Generate field MVs from Frame MVs
- if (img->structure || img->MbaffFrameFlag)
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/8 ; j++)
- {
- jj = RSD(j);
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- ii = RSD(i);
- //! Do nothing if macroblock as field coded in MB-AFF
- if (!img->MbaffFrameFlag )
- {
- ftype->mv[LIST_0][j][i][0] = fs->motion.mv[LIST_0][jj][ii][0];
- ftype->mv[LIST_0][j][i][1] = fs->motion.mv[LIST_0][jj][ii][1];
- ftype->mv[LIST_1][j][i][0] = fs->motion.mv[LIST_1][jj][ii][0];
- ftype->mv[LIST_1][j][i][1] = fs->motion.mv[LIST_1][jj][ii][1];
- // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
- if (fs->motion.ref_idx[LIST_0][jj][ii] == -1)
- {
- ftype->ref_idx [LIST_0][j][i] = -1;
- ftype->ref_pic_id[LIST_0][j][i] = -1;
- }
- else
- {
- ftype->ref_idx [LIST_0][j][i] = fs->motion.ref_idx[LIST_0][jj][ii] ;
- ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id [LIST_0][jj][ii];
- }
- if (fs->motion.ref_idx[LIST_1][jj][ii] == -1)
- {
- ftype->ref_idx [LIST_1][j][i] = -1;
- ftype->ref_pic_id[LIST_1][j][i] = -1;
- }
- else
- {
- ftype->ref_idx [LIST_1][j][i] = fs->motion.ref_idx[LIST_1][jj][ii];
- ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id [LIST_1][jj][ii];
- }
- p->is_long_term = fs->is_long_term;
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- ftype->moving_block[j][i] =
- !((!p->is_long_term
- && ((ftype->ref_idx[LIST_0][j][i] == 0)
- && (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
- || ((ftype->ref_idx[LIST_0][j][i] == -1)
- && (ftype->ref_idx[LIST_1][j][i] == 0)
- && (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
- }
- }
- else
- {
- p->bottom.mv[LIST_0][j][i][0] = fs_bottom->motion.mv[LIST_0][jj][ii][0];
- p->bottom.mv[LIST_0][j][i][1] = fs_bottom->motion.mv[LIST_0][jj][ii][1];
- p->bottom.mv[LIST_1][j][i][0] = fs_bottom->motion.mv[LIST_1][jj][ii][0];
- p->bottom.mv[LIST_1][j][i][1] = fs_bottom->motion.mv[LIST_1][jj][ii][1];
- p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->motion.ref_idx[LIST_0][jj][ii];
- p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->motion.ref_idx[LIST_1][jj][ii];
- p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->motion.ref_id[LIST_0][jj][ii];
- p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->motion.ref_id[LIST_1][jj][ii];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- p->bottom.moving_block[j][i] =
- !((!fs_bottom->is_long_term
- && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
- && (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
- || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
- && (p->bottom.ref_idx[LIST_1][j][i] == 0)
- && (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
- }
- p->top.mv[LIST_0][j][i][0] = fs_top->motion.mv[LIST_0][jj][ii][0];
- p->top.mv[LIST_0][j][i][1] = fs_top->motion.mv[LIST_0][jj][ii][1];
- p->top.mv[LIST_1][j][i][0] = fs_top->motion.mv[LIST_1][jj][ii][0];
- p->top.mv[LIST_1][j][i][1] = fs_top->motion.mv[LIST_1][jj][ii][1];
- p->top.ref_idx[LIST_0][j][i] = fs_top->motion.ref_idx[LIST_0][jj][ii];
- p->top.ref_idx[LIST_1][j][i] = fs_top->motion.ref_idx[LIST_1][jj][ii];
- p->top.ref_pic_id[LIST_0][j][i] = fs_top->motion.ref_id[LIST_0][jj][ii];
- p->top.ref_pic_id[LIST_1][j][i] = fs_top->motion.ref_id[LIST_1][jj][ii];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- p->top.moving_block[j][i] =
- !((!fs_top->is_long_term
- && ((p->top.ref_idx[LIST_0][j][i] == 0)
- && (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
- || ((p->top.ref_idx[LIST_0][j][i] == -1)
- && (p->top.ref_idx[LIST_1][j][i] == 0)
- && (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
- }
- if ((img->direct_spatial_mv_pred_flag == 0 ) && !fs->motion.field_frame[2*j][i])
- {
- p->top.mv[LIST_0][j][i][1] /= 2;
- p->top.mv[LIST_1][j][i][1] /= 2;
- p->bottom.mv[LIST_0][j][i][1] /= 2;
- p->bottom.mv[LIST_1][j][i][1] /= 2;
- }
- }
- }
- }
- }
- if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
- {
- //! Use inference flag to remap mvs/references
- //! Frame with field co-located
- if (!img->structure)
- {
- ftype = &p->frame;
- for (j=0 ; j < (fs->size_y>>2) ; j++)
- {
- jdiv = j>>1;
- jj = (j>>1) + 4*(j>>3);
- for (i=0 ; i < (fs->size_x>>2) ; i++)
- {
- if (fs->motion.field_frame[j][i])
- {
- if (iabs(enc_picture->poc - fs->bottom_field->poc) > iabs(enc_picture->poc - fs->top_field->poc))
- {
- ftype->mv[LIST_0][j][i][0] = fs->top_field->motion.mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs->top_field->motion.mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs->top_field->motion.mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs->top_field->motion.mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs->top_field->motion.ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs->top_field->motion.ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj][i];
- p->is_long_term = fs->top_field->is_long_term;
- }
- else
- {
- ftype->mv[LIST_0][j][i][0] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs->bottom_field->motion.ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs->bottom_field->motion.ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj + 4][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj + 4][i];
- p->is_long_term = fs->bottom_field->is_long_term;
- }
- }
- }
- }
- }
- }
- p->is_long_term = fs->is_long_term;
- if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
- {
- ftype = &p->frame;
- for (j=0 ; j < (fs->size_y>>2) ; j++)
- {
- jj = RSD(j);
- for (i=0 ; i < (fs->size_x>>2) ; i++)
- {
- ii = RSD(i);
- ftype->mv[LIST_0][j][i][0]=ftype->mv[LIST_0][jj][ii][0];
- ftype->mv[LIST_0][j][i][1]=ftype->mv[LIST_0][jj][ii][1];
- ftype->mv[LIST_1][j][i][0]=ftype->mv[LIST_1][jj][ii][0];
- ftype->mv[LIST_1][j][i][1]=ftype->mv[LIST_1][jj][ii][1];
- ftype->ref_idx[LIST_0][j][i]=ftype->ref_idx[LIST_0][jj][ii];
- ftype->ref_idx[LIST_1][j][i]=ftype->ref_idx[LIST_1][jj][ii];
- ftype->ref_pic_id[LIST_0][j][i] = ftype->ref_pic_id[LIST_0][jj][ii];
- ftype->ref_pic_id[LIST_1][j][i] = ftype->ref_pic_id[LIST_1][jj][ii];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- ftype->moving_block[j][i]=
- !((!p->is_long_term
- && ((ftype->ref_idx[LIST_0][j][i] == 0)
- && (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
- || ((ftype->ref_idx[LIST_0][j][i] == -1)
- && (ftype->ref_idx[LIST_1][j][i] == 0)
- && (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
- }
- }
- }
- }
- else
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/4 ; j++)
- {
- jj = RSD(j);
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- ii = RSD(i);
- //! Use inference flag to remap mvs/references
- ftype->mv[LIST_0][j][i][0]=fs->motion.mv[LIST_0][j][i][0];
- ftype->mv[LIST_0][j][i][1]=fs->motion.mv[LIST_0][j][i][1];
- ftype->mv[LIST_1][j][i][0]=fs->motion.mv[LIST_1][j][i][0];
- ftype->mv[LIST_1][j][i][1]=fs->motion.mv[LIST_1][j][i][1];
- ftype->ref_idx[LIST_0][j][i]=fs->motion.ref_idx[LIST_0][j][i];
- ftype->ref_idx[LIST_1][j][i]=fs->motion.ref_idx[LIST_1][j][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][j][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][j][i];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- ftype->moving_block[j][i]=
- !((!p->is_long_term
- && ((ftype->ref_idx[LIST_0][j][i] == 0)
- && (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
- || ((ftype->ref_idx[LIST_0][j][i] == -1)
- && (ftype->ref_idx[LIST_1][j][i] == 0)
- && (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
- }
- }
- }
- }
- if (img->direct_spatial_mv_pred_flag ==0)
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/4 ; j++)
- {
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- if ((!img->MbaffFrameFlag &&!img->structure && fs->motion.field_frame[j][i]) || (img->MbaffFrameFlag && fs->motion.field_frame[j][i]))
- {
- ftype->mv[LIST_0][j][i][1] *= 2;
- ftype->mv[LIST_1][j][i][1] *= 2;
- }
- else if (img->structure && !fs->motion.field_frame[j][i])
- {
- ftype->mv[LIST_0][j][i][1] /= 2;
- ftype->mv[LIST_1][j][i][1] /= 2;
- }
- }
- }
- for (j=0; j<2 + (img->MbaffFrameFlag * 4);j+=2)
- {
- for (i=0; i<listXsize[j];i++)
- {
- int prescale, iTRb, iTRp;
- if (j==0)
- {
- iTRb = iClip3( -128, 127, enc_picture->poc - listX[LIST_0 + j][i]->poc );
- }
- else if (j == 2)
- {
- iTRb = iClip3( -128, 127, enc_picture->top_poc - listX[LIST_0 + j][i]->poc );
- }
- else
- {
- iTRb = iClip3( -128, 127, enc_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
- }
- iTRp = iClip3( -128, 127, listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
- if (iTRp!=0)
- {
- prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
- img->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
- }
- else
- {
- img->mvscale[j][i] = 9999;
- }
- }
- }
- }
- }
- /*!
- ************************************************************************
- * brief
- * Compute co-located motion info
- * for 4:4:4 Independent mode
- *
- ************************************************************************
- */
- void compute_colocated_JV(ColocatedParams* p, StorablePicture **listX[6])
- {
- StorablePicture *fs, *fs_top, *fs_bottom;
- MotionParams *ftype;
- int i,j, ii, jj, jdiv;
- int np = img->colour_plane_id;
- fs_top = fs_bottom = fs = listX[LIST_1 ][0];
- if (img->MbaffFrameFlag)
- {
- fs_top= listX[LIST_1 + 2][0];
- fs_bottom= listX[LIST_1 + 4][0];
- }
- else
- {
- if (img->structure!=FRAME)
- {
- if ((img->structure != fs->structure) && (fs->coded_frame))
- {
- if (img->structure==TOP_FIELD)
- {
- fs_top = fs_bottom = fs = listX[LIST_1 ][0]->top_field;
- }
- else
- {
- fs_top = fs_bottom = fs = listX[LIST_1 ][0]->bottom_field;
- }
- }
- }
- }
- if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/4 ; j++)
- {
- jdiv = j/2;
- jj = j/2 + 4 * (j/8);
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- if (img->MbaffFrameFlag && fs->motion.field_frame[j][i])
- {
- //! Assign frame buffers for field MBs
- //! Check whether we should use top or bottom field mvs.
- //! Depending on the assigned poc values.
- if (iabs(enc_picture->poc - fs_bottom->poc) > iabs(enc_picture->poc - fs_top->poc) )
- {
- ftype->mv[LIST_0][j][i][0] = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs_top->JVmotion[np].ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs_top->JVmotion[np].ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj][i];
- p->is_long_term = fs_top->is_long_term;
- }
- else
- {
- ftype->mv[LIST_0][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
- p->is_long_term = fs_bottom->is_long_term;
- }
- }
- else
- {
- ftype->mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][j][i][0];
- ftype->mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][j][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][j][i][0];
- ftype->mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][j][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][j][i];
- ftype->ref_idx[LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][j][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
- p->is_long_term = fs->is_long_term;
- }
- }
- }
- }
- //! Generate field MVs from Frame MVs
- if (img->structure || img->MbaffFrameFlag)
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/8 ; j++)
- {
- jj = RSD(j);
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- ii = RSD(i);
- //! Do nothing if macroblock as field coded in MB-AFF
- if (!img->MbaffFrameFlag )
- {
- ftype->mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][jj][ii][0];
- ftype->mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][jj][ii][1];
- ftype->mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][jj][ii][0];
- ftype->mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][jj][ii][1];
- // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
- if (fs->motion.ref_idx[LIST_0][jj][ii] == -1)
- {
- ftype->ref_idx [LIST_0][j][i] = -1;
- ftype->ref_pic_id[LIST_0][j][i] = -1;
- }
- else
- {
- ftype->ref_idx [LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][jj][ii] ;
- ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj][ii];
- }
- if (fs->motion.ref_idx[LIST_1][jj][ii] == -1)
- {
- ftype->ref_idx [LIST_1][j][i] = -1;
- ftype->ref_pic_id[LIST_1][j][i] = -1;
- }
- else
- {
- ftype->ref_idx [LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][jj][ii];
- ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj][ii];
- }
- p->is_long_term = fs->is_long_term;
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- ftype->moving_block[j][i] =
- !((!p->is_long_term
- && ((ftype->ref_idx[LIST_0][j][i] == 0)
- && (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
- || ((ftype->ref_idx[LIST_0][j][i] == -1)
- && (ftype->ref_idx[LIST_1][j][i] == 0)
- && (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
- }
- }
- else
- {
- p->bottom.mv[LIST_0][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][0];
- p->bottom.mv[LIST_0][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][1];
- p->bottom.mv[LIST_1][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][0];
- p->bottom.mv[LIST_1][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][1];
- p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_0][jj][ii];
- p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_1][jj][ii];
- p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_0][jj][ii];
- p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_1][jj][ii];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- p->bottom.moving_block[j][i] =
- !((!fs_bottom->is_long_term
- && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
- && (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
- || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
- && (p->bottom.ref_idx[LIST_1][j][i] == 0)
- && (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
- }
- p->top.mv[LIST_0][j][i][0] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][0];
- p->top.mv[LIST_0][j][i][1] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][1];
- p->top.mv[LIST_1][j][i][0] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][0];
- p->top.mv[LIST_1][j][i][1] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][1];
- p->top.ref_idx[LIST_0][j][i] = fs_top->JVmotion[np].ref_idx[LIST_0][jj][ii];
- p->top.ref_idx[LIST_1][j][i] = fs_top->JVmotion[np].ref_idx[LIST_1][jj][ii];
- p->top.ref_pic_id[LIST_0][j][i] = fs_top->JVmotion[np].ref_id[LIST_0][jj][ii];
- p->top.ref_pic_id[LIST_1][j][i] = fs_top->JVmotion[np].ref_id[LIST_1][jj][ii];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- p->top.moving_block[j][i] =
- !((!fs_top->is_long_term
- && ((p->top.ref_idx[LIST_0][j][i] == 0)
- && (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
- || ((p->top.ref_idx[LIST_0][j][i] == -1)
- && (p->top.ref_idx[LIST_1][j][i] == 0)
- && (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
- }
- if ((img->direct_spatial_mv_pred_flag == 0 ) && !fs->motion.field_frame[2*j][i])
- {
- p->top.mv[LIST_0][j][i][1] /= 2;
- p->top.mv[LIST_1][j][i][1] /= 2;
- p->bottom.mv[LIST_0][j][i][1] /= 2;
- p->bottom.mv[LIST_1][j][i][1] /= 2;
- }
- }
- }
- }
- }
- if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
- {
- //! Use inference flag to remap mvs/references
- //! Frame with field co-located
- if (!img->structure)
- {
- ftype = &p->frame;
- for (j=0 ; j < (fs->size_y>>2) ; j++)
- {
- jdiv = j>>1;
- jj = (j>>1) + 4*(j>>3);
- for (i=0 ; i < (fs->size_x>>2) ; i++)
- {
- if (fs->motion.field_frame[j][i])
- {
- if (iabs(enc_picture->poc - fs->bottom_field->poc) > iabs(enc_picture->poc - fs->top_field->poc))
- {
- ftype->mv[LIST_0][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs->top_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs->top_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj][i];
- p->is_long_term = fs->top_field->is_long_term;
- }
- else
- {
- ftype->mv[LIST_0][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
- ftype->mv[LIST_0][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
- ftype->mv[LIST_1][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
- ftype->mv[LIST_1][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
- ftype->ref_idx[LIST_0][j][i] = fs->bottom_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
- ftype->ref_idx[LIST_1][j][i] = fs->bottom_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
- p->is_long_term = fs->bottom_field->is_long_term;
- }
- }
- }
- }
- }
- }
- p->is_long_term = fs->is_long_term;
- if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
- {
- ftype = &p->frame;
- for (j=0 ; j < (fs->size_y>>2) ; j++)
- {
- jj = RSD(j);
- for (i=0 ; i < (fs->size_x>>2) ; i++)
- {
- ii = RSD(i);
- ftype->mv[LIST_0][j][i][0]=ftype->mv[LIST_0][jj][ii][0];
- ftype->mv[LIST_0][j][i][1]=ftype->mv[LIST_0][jj][ii][1];
- ftype->mv[LIST_1][j][i][0]=ftype->mv[LIST_1][jj][ii][0];
- ftype->mv[LIST_1][j][i][1]=ftype->mv[LIST_1][jj][ii][1];
- ftype->ref_idx[LIST_0][j][i]=ftype->ref_idx[LIST_0][jj][ii];
- ftype->ref_idx[LIST_1][j][i]=ftype->ref_idx[LIST_1][jj][ii];
- ftype->ref_pic_id[LIST_0][j][i] = ftype->ref_pic_id[LIST_0][jj][ii];
- ftype->ref_pic_id[LIST_1][j][i] = ftype->ref_pic_id[LIST_1][jj][ii];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- ftype->moving_block[j][i]=
- !((!p->is_long_term
- && ((ftype->ref_idx[LIST_0][j][i] == 0)
- && (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
- || ((ftype->ref_idx[LIST_0][j][i] == -1)
- && (ftype->ref_idx[LIST_1][j][i] == 0)
- && (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
- }
- }
- }
- }
- else
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/4 ; j++)
- {
- jj = RSD(j);
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- ii = RSD(i);
- //! Use inference flag to remap mvs/references
- ftype->mv[LIST_0][j][i][0]=fs->JVmotion[np].mv[LIST_0][j][i][0];
- ftype->mv[LIST_0][j][i][1]=fs->JVmotion[np].mv[LIST_0][j][i][1];
- ftype->mv[LIST_1][j][i][0]=fs->JVmotion[np].mv[LIST_1][j][i][0];
- ftype->mv[LIST_1][j][i][1]=fs->JVmotion[np].mv[LIST_1][j][i][1];
- ftype->ref_idx[LIST_0][j][i]=fs->JVmotion[np].ref_idx[LIST_0][j][i];
- ftype->ref_idx[LIST_1][j][i]=fs->JVmotion[np].ref_idx[LIST_1][j][i];
- ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
- ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
- if (img->direct_spatial_mv_pred_flag == 1)
- {
- ftype->moving_block[j][i]=
- !((!p->is_long_term
- && ((ftype->ref_idx[LIST_0][j][i] == 0)
- && (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
- || ((ftype->ref_idx[LIST_0][j][i] == -1)
- && (ftype->ref_idx[LIST_1][j][i] == 0)
- && (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
- && (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
- }
- }
- }
- }
- if (img->direct_spatial_mv_pred_flag ==0)
- {
- ftype = &p->frame;
- for (j=0 ; j<fs->size_y/4 ; j++)
- {
- for (i=0 ; i<fs->size_x/4 ; i++)
- {
- if ((!img->MbaffFrameFlag &&!img->structure && fs->motion.field_frame[j][i]) || (img->MbaffFrameFlag && fs->motion.field_frame[j][i]))
- {
- ftype->mv[LIST_0][j][i][1] *= 2;
- ftype->mv[LIST_1][j][i][1] *= 2;
- }
- else if (img->structure && !fs->motion.field_frame[j][i])
- {
- ftype->mv[LIST_0][j][i][1] /= 2;
- ftype->mv[LIST_1][j][i][1] /= 2;
- }
- }
- }
- for (j=0; j<2 + (img->MbaffFrameFlag * 4);j+=2)
- {
- for (i=0; i<listXsize[j];i++)
- {
- int prescale, iTRb, iTRp;
- if (j==0)
- {
- iTRb = iClip3( -128, 127, enc_picture->poc - listX[LIST_0 + j][i]->poc );
- }
- else if (j == 2)
- {
- iTRb = iClip3( -128, 127, enc_picture->top_poc - listX[LIST_0 + j][i]->poc );
- }
- else
- {
- iTRb = iClip3( -128, 127, enc_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
- }
- iTRp = iClip3( -128, 127, listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
- if (iTRp!=0)
- {
- prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
- img->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
- }
- else
- {
- img->mvscale[j][i] = 9999;
- }
- }
- }
- }
- }
- /*!
- ************************************************************************
- * brief
- * Copy StorablePicture parameters
- * for 4:4:4 Independent mode
- *
- ************************************************************************
- */
- void copy_storable_param_JV( int nplane, StorablePicture *d, StorablePicture *s )
- {
- // copy ref_idx
- memcpy( d->JVmotion[nplane].ref_idx[0][0], s->motion.ref_idx[0][0], 2 * (img->height/BLOCK_SIZE) * (img->width/BLOCK_SIZE) * sizeof(byte) );
- // copy ref_pic_id
- memcpy( d->JVmotion[nplane].ref_pic_id[0][0], s->motion.ref_pic_id[0][0], 6 * (img->height/BLOCK_SIZE) * (img->width/BLOCK_SIZE) * sizeof(int64) );
- // copy motion.ref_id
- memcpy( d->JVmotion[nplane].ref_id[0][0], s->motion.ref_id[0][0], 6 * (img->width/BLOCK_SIZE) * (img->height/BLOCK_SIZE) * sizeof(int64));
- // copy mv
- memcpy( d->JVmotion[nplane].mv[0][0][0], s->motion.mv[0][0][0], 2 * img->height/BLOCK_SIZE * img->width/BLOCK_SIZE * sizeof(short)*2 );
- }