00001
00022 #include <limits.h>
00023
00024 #include "global.h"
00025 #include "erc_api.h"
00026 #include "header.h"
00027 #include "image.h"
00028 #include "mbuffer.h"
00029 #include "memalloc.h"
00030 #include "output.h"
00031
00032
00033
00034 static void insert_picture_in_dpb (ImageParameters *p_Img, FrameStore* fs, StorablePicture* p);
00035 static void output_one_frame_from_dpb(ImageParameters *p_Img);
00036 static void get_smallest_poc (DecodedPictureBuffer *p_Dpb, int *poc,int * pos);
00037 static void gen_field_ref_ids (StorablePicture *p);
00038 static int remove_unused_frame_from_dpb (ImageParameters *p_Img, DecodedPictureBuffer *p_Dpb);
00039 static int is_used_for_reference (FrameStore* fs);
00040 static int is_short_term_reference (FrameStore* fs);
00041 static int is_long_term_reference (FrameStore* fs);
00042
00043 #define MAX_LIST_SIZE 33
00044
00051 void dump_dpb(DecodedPictureBuffer *p_Dpb)
00052 {
00053 #if DUMP_DPB
00054 unsigned i;
00055
00056 for (i=0; i<p_Dpb->used_size;i++)
00057 {
00058 printf("(");
00059 printf("fn=%d ", p_Dpb->fs[i]->frame_num);
00060 if (p_Dpb->fs[i]->is_used & 1)
00061 {
00062 if (p_Dpb->fs[i]->top_field)
00063 printf("T: poc=%d ", p_Dpb->fs[i]->top_field->poc);
00064 else
00065 printf("T: poc=%d ", p_Dpb->fs[i]->frame->top_poc);
00066 }
00067 if (p_Dpb->fs[i]->is_used & 2)
00068 {
00069 if (p_Dpb->fs[i]->bottom_field)
00070 printf("B: poc=%d ", p_Dpb->fs[i]->bottom_field->poc);
00071 else
00072 printf("B: poc=%d ", p_Dpb->fs[i]->frame->bottom_poc);
00073 }
00074 if (p_Dpb->fs[i]->is_used == 3)
00075 printf("F: poc=%d ", p_Dpb->fs[i]->frame->poc);
00076 printf("G: poc=%d) ", p_Dpb->fs[i]->poc);
00077 if (p_Dpb->fs[i]->is_reference) printf ("ref (%d) ", p_Dpb->fs[i]->is_reference);
00078 if (p_Dpb->fs[i]->is_long_term) printf ("lt_ref (%d) ", p_Dpb->fs[i]->is_reference);
00079 if (p_Dpb->fs[i]->is_output) printf ("out ");
00080 if (p_Dpb->fs[i]->is_used == 3)
00081 {
00082 if (p_Dpb->fs[i]->frame->non_existing) printf ("ne ");
00083 }
00084 printf ("\n");
00085 }
00086 #endif
00087 }
00088
00097 int getDpbSize(seq_parameter_set_rbsp_t *active_sps)
00098 {
00099 int pic_size = (active_sps->pic_width_in_mbs_minus1 + 1) * (active_sps->pic_height_in_map_units_minus1 + 1) * (active_sps->frame_mbs_only_flag?1:2) * 384;
00100
00101 int size = 0;
00102
00103 switch (active_sps->level_idc)
00104 {
00105 case 9:
00106 size = 152064;
00107 break;
00108 case 10:
00109 size = 152064;
00110 break;
00111 case 11:
00112 if (!IS_FREXT_PROFILE(active_sps->profile_idc) && (active_sps->constrained_set3_flag == 1))
00113 size = 152064;
00114 else
00115 size = 345600;
00116 break;
00117 case 12:
00118 size = 912384;
00119 break;
00120 case 13:
00121 size = 912384;
00122 break;
00123 case 20:
00124 size = 912384;
00125 break;
00126 case 21:
00127 size = 1824768;
00128 break;
00129 case 22:
00130 size = 3110400;
00131 break;
00132 case 30:
00133 size = 3110400;
00134 break;
00135 case 31:
00136 size = 6912000;
00137 break;
00138 case 32:
00139 size = 7864320;
00140 break;
00141 case 40:
00142 size = 12582912;
00143 break;
00144 case 41:
00145 size = 12582912;
00146 break;
00147 case 42:
00148 size = 13369344;
00149 break;
00150 case 50:
00151 size = 42393600;
00152 break;
00153 case 51:
00154 size = 70778880;
00155 break;
00156 default:
00157 error ("undefined level", 500);
00158 break;
00159 }
00160
00161 size /= pic_size;
00162 size = imin( size, 16);
00163
00164 if (active_sps->vui_parameters_present_flag && active_sps->vui_seq_parameters.bitstream_restriction_flag)
00165 {
00166 if ((int)active_sps->vui_seq_parameters.max_dec_frame_buffering > size)
00167 {
00168 error ("max_dec_frame_buffering larger than MaxDpbSize", 500);
00169 }
00170 size = imax (1, active_sps->vui_seq_parameters.max_dec_frame_buffering);
00171 }
00172
00173 return size;
00174 }
00175
00184 void check_num_ref(DecodedPictureBuffer *p_Dpb)
00185 {
00186 if ((int)(p_Dpb->ltref_frames_in_buffer + p_Dpb->ref_frames_in_buffer ) > (imax(1, p_Dpb->num_ref_frames)))
00187 {
00188 error ("Max. number of reference frames exceeded. Invalid stream.", 500);
00189 }
00190 }
00191
00192
00200 void init_dpb(ImageParameters *p_Img)
00201 {
00202 unsigned i,j;
00203 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
00204 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
00205
00206 if (p_Dpb->init_done)
00207 {
00208 free_dpb(p_Img);
00209 }
00210
00211 p_Dpb->p_Img = p_Img;
00212 p_Dpb->size = getDpbSize(active_sps);
00213
00214 p_Dpb->num_ref_frames = active_sps->num_ref_frames;
00215
00216 if (p_Dpb->size < active_sps->num_ref_frames)
00217 {
00218 error ("DPB size at specified level is smaller than the specified number of reference frames. This is not allowed.\n", 1000);
00219 }
00220
00221 p_Dpb->used_size = 0;
00222 p_Dpb->last_picture = NULL;
00223
00224 p_Dpb->ref_frames_in_buffer = 0;
00225 p_Dpb->ltref_frames_in_buffer = 0;
00226
00227 p_Dpb->fs = calloc(p_Dpb->size, sizeof (FrameStore*));
00228 if (NULL==p_Dpb->fs)
00229 no_mem_exit("init_dpb: dpb->fs");
00230
00231 p_Dpb->fs_ref = calloc(p_Dpb->size, sizeof (FrameStore*));
00232 if (NULL==p_Dpb->fs_ref)
00233 no_mem_exit("init_dpb: dpb->fs_ref");
00234
00235 p_Dpb->fs_ltref = calloc(p_Dpb->size, sizeof (FrameStore*));
00236 if (NULL==p_Dpb->fs_ltref)
00237 no_mem_exit("init_dpb: dpb->fs_ltref");
00238
00239 for (i=0; i<p_Dpb->size; i++)
00240 {
00241 p_Dpb->fs[i] = alloc_frame_store();
00242 p_Dpb->fs_ref[i] = NULL;
00243 p_Dpb->fs_ltref[i] = NULL;
00244 }
00245
00246 for (i=0; i<6; i++)
00247 {
00248 p_Img->listX[i] = calloc(MAX_LIST_SIZE, sizeof (StorablePicture*));
00249 if (NULL==p_Img->listX[i])
00250 no_mem_exit("init_dpb: p_Img->listX[i]");
00251 }
00252
00253
00254 p_Img->no_reference_picture = alloc_storable_picture (p_Img, FRAME, p_Img->width, p_Img->height, p_Img->width_cr, p_Img->height_cr);
00255 p_Img->no_reference_picture->top_field = p_Img->no_reference_picture;
00256 p_Img->no_reference_picture->bottom_field = p_Img->no_reference_picture;
00257 p_Img->no_reference_picture->frame = p_Img->no_reference_picture;
00258
00259
00260 for (j=0;j<6;j++)
00261 {
00262 for (i=0; i<MAX_LIST_SIZE; i++)
00263 {
00264 p_Img->listX[j][i] = NULL;
00265 }
00266 p_Img->listXsize[j]=0;
00267 }
00268
00269 p_Dpb->last_output_poc = INT_MIN;
00270
00271 p_Img->last_has_mmco_5 = 0;
00272
00273 p_Dpb->init_done = 1;
00274
00275
00276 if(p_Img->conceal_mode !=0)
00277 p_Img->last_out_fs = alloc_frame_store();
00278 }
00285 void free_dpb(ImageParameters *p_Img)
00286 {
00287 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
00288 unsigned i;
00289 if (p_Dpb->fs)
00290 {
00291 for (i=0; i<p_Dpb->size; i++)
00292 {
00293 free_frame_store(p_Img, p_Dpb->fs[i]);
00294 }
00295 free (p_Dpb->fs);
00296 p_Dpb->fs=NULL;
00297 }
00298 if (p_Dpb->fs_ref)
00299 {
00300 free (p_Dpb->fs_ref);
00301 }
00302 if (p_Dpb->fs_ltref)
00303 {
00304 free (p_Dpb->fs_ltref);
00305 }
00306 p_Dpb->last_output_poc = INT_MIN;
00307
00308 for (i=0; i<6; i++)
00309 if (p_Img->listX[i])
00310 {
00311 free (p_Img->listX[i]);
00312 p_Img->listX[i] = NULL;
00313 }
00314
00315 p_Dpb->init_done = 0;
00316
00317
00318 if(p_Img->conceal_mode != 0)
00319 free_frame_store(p_Img, p_Img->last_out_fs);
00320
00321 free_storable_picture(p_Img, p_Img->no_reference_picture);
00322 }
00323
00324
00334 FrameStore* alloc_frame_store(void)
00335 {
00336 FrameStore *f;
00337
00338 f = calloc (1, sizeof(FrameStore));
00339 if (NULL==f)
00340 no_mem_exit("alloc_frame_store: f");
00341
00342 f->is_used = 0;
00343 f->is_reference = 0;
00344 f->is_long_term = 0;
00345 f->is_orig_reference = 0;
00346
00347 f->is_output = 0;
00348
00349 f->frame = NULL;;
00350 f->top_field = NULL;
00351 f->bottom_field = NULL;
00352
00353 return f;
00354 }
00355
00356 void alloc_pic_motion(ImageParameters *p_Img, PicMotionParams *motion, int size_y, int size_x)
00357 {
00358 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
00359
00360 if (active_sps->frame_mbs_only_flag)
00361 {
00362 get_mem3Dint64 (&(motion->ref_pic_id), 2, size_y, size_x);
00363 get_mem3Dint64 (&(motion->ref_id), 2, size_y, size_x);
00364 }
00365 else
00366 {
00367 get_mem3Dint64 (&(motion->ref_pic_id), 6, size_y, size_x);
00368 get_mem3Dint64 (&(motion->ref_id), 6, size_y, size_x);
00369 }
00370
00371 get_mem4Dshort (&(motion->mv), 2, size_y, size_x, 2);
00372 get_mem3D ((byte****)(&(motion->ref_idx)), 2, size_y , size_x);
00373
00374 motion->mb_field = calloc (size_y * size_x, sizeof(byte));
00375 if (motion->mb_field == NULL)
00376 no_mem_exit("alloc_storable_picture: motion->mb_field");
00377
00378 get_mem2D (&(motion->field_frame), size_y, size_x);
00379 }
00380
00403 StorablePicture* alloc_storable_picture(ImageParameters *p_Img, PictureStructure structure, int size_x, int size_y, int size_x_cr, int size_y_cr)
00404 {
00405 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
00406
00407 StorablePicture *s;
00408 int nplane;
00409
00410
00411
00412 s = calloc (1, sizeof(StorablePicture));
00413 if (NULL==s)
00414 no_mem_exit("alloc_storable_picture: s");
00415
00416 if (structure!=FRAME)
00417 {
00418 size_y /= 2;
00419 size_y_cr /= 2;
00420 }
00421
00422 s->PicSizeInMbs = (size_x*size_y)/256;
00423 s->imgUV = NULL;
00424
00425 get_mem2Dpel (&(s->imgY), size_y, size_x);
00426
00427 if (active_sps->chroma_format_idc != YUV400)
00428 get_mem3Dpel (&(s->imgUV), 2, size_y_cr, size_x_cr);
00429
00430 get_mem2Dshort (&(s->slice_id), size_y / MB_BLOCK_SIZE, size_x / MB_BLOCK_SIZE);
00431
00432 alloc_pic_motion(p_Img, &s->motion, size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
00433
00434 if( IS_INDEPENDENT(p_Img) )
00435 {
00436 for( nplane=0; nplane<MAX_PLANE; nplane++ )
00437 {
00438 alloc_pic_motion(p_Img, &s->JVmotion[nplane], size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
00439 }
00440 }
00441
00442 s->pic_num=0;
00443 s->frame_num=0;
00444 s->long_term_frame_idx=0;
00445 s->long_term_pic_num=0;
00446 s->used_for_reference=0;
00447 s->is_long_term=0;
00448 s->non_existing=0;
00449 s->is_output = 0;
00450 s->max_slice_id = 0;
00451
00452 s->structure=structure;
00453
00454 s->size_x = size_x;
00455 s->size_y = size_y;
00456 s->size_x_cr = size_x_cr;
00457 s->size_y_cr = size_y_cr;
00458 s->size_x_m1 = size_x - 1;
00459 s->size_y_m1 = size_y - 1;
00460 s->size_x_cr_m1 = size_x_cr - 1;
00461 s->size_y_cr_m1 = size_y_cr - 1;
00462
00463 s->top_field = p_Img->no_reference_picture;
00464 s->bottom_field = p_Img->no_reference_picture;
00465 s->frame = p_Img->no_reference_picture;
00466
00467 s->dec_ref_pic_marking_buffer = NULL;
00468
00469 s->coded_frame = 0;
00470 s->MbaffFrameFlag = 0;
00471
00472 s->top_poc = s->bottom_poc = s->poc = 0;
00473 s->seiHasTone_mapping = 0;
00474
00475 return s;
00476 }
00477
00490 void free_frame_store(ImageParameters *p_Img, FrameStore* f)
00491 {
00492 if (f)
00493 {
00494 if (f->frame)
00495 {
00496 free_storable_picture(p_Img, f->frame);
00497 f->frame=NULL;
00498 }
00499 if (f->top_field)
00500 {
00501 free_storable_picture(p_Img, f->top_field);
00502 f->top_field=NULL;
00503 }
00504 if (f->bottom_field)
00505 {
00506 free_storable_picture(p_Img, f->bottom_field);
00507 f->bottom_field=NULL;
00508 }
00509 free(f);
00510 }
00511 }
00512
00513 void free_pic_motion(PicMotionParams *motion)
00514 {
00515 if (motion->ref_pic_id)
00516 {
00517 free_mem3Dint64 (motion->ref_pic_id);
00518 motion->ref_pic_id = NULL;
00519 }
00520 if (motion->ref_id)
00521 {
00522 free_mem3Dint64 (motion->ref_id);
00523 motion->ref_id = NULL;
00524 }
00525
00526 if (motion->mv)
00527 {
00528 free_mem4Dshort (motion->mv);
00529 motion->mv = NULL;
00530 }
00531
00532 if (motion->ref_idx)
00533 {
00534 free_mem3D ((byte***)motion->ref_idx);
00535 motion->ref_idx = NULL;
00536 }
00537
00538 if (motion->mb_field)
00539 {
00540 free(motion->mb_field);
00541 motion->mb_field = NULL;
00542 }
00543
00544 if (motion->field_frame)
00545 {
00546 free_mem2D (motion->field_frame);
00547 motion->field_frame=NULL;
00548 }
00549 }
00550
00551
00564 void free_storable_picture(ImageParameters *p_Img, StorablePicture* p)
00565 {
00566 int nplane;
00567 if (p)
00568 {
00569 free_pic_motion(&p->motion);
00570
00571 if( IS_INDEPENDENT(p_Img) )
00572 {
00573 for( nplane=0; nplane<MAX_PLANE; nplane++ )
00574 {
00575 free_pic_motion(&p->JVmotion[nplane]);
00576 }
00577 }
00578
00579 if (p->imgY)
00580 {
00581 free_mem2Dpel (p->imgY);
00582 p->imgY=NULL;
00583 }
00584
00585 if (p->imgUV)
00586 {
00587 free_mem3Dpel (p->imgUV);
00588 p->imgUV=NULL;
00589 }
00590
00591 if (p->slice_id)
00592 {
00593 free_mem2Dshort(p->slice_id);
00594 p->slice_id=NULL;
00595 }
00596
00597 if (p->seiHasTone_mapping)
00598 free(p->tone_mapping_lut);
00599
00600 free(p);
00601 p = NULL;
00602 }
00603 }
00604
00612 static void unmark_for_reference(FrameStore* fs)
00613 {
00614
00615 if (fs->is_used & 1)
00616 {
00617 if (fs->top_field)
00618 {
00619 fs->top_field->used_for_reference = 0;
00620 }
00621 }
00622 if (fs->is_used & 2)
00623 {
00624 if (fs->bottom_field)
00625 {
00626 fs->bottom_field->used_for_reference = 0;
00627 }
00628 }
00629 if (fs->is_used == 3)
00630 {
00631 if (fs->top_field && fs->bottom_field)
00632 {
00633 fs->top_field->used_for_reference = 0;
00634 fs->bottom_field->used_for_reference = 0;
00635 }
00636 fs->frame->used_for_reference = 0;
00637 }
00638
00639 fs->is_reference = 0;
00640
00641 if(fs->frame)
00642 {
00643 free_pic_motion(&fs->frame->motion);
00644 }
00645
00646 if (fs->top_field)
00647 {
00648 free_pic_motion(&fs->top_field->motion);
00649 }
00650
00651 if (fs->bottom_field)
00652 {
00653 free_pic_motion(&fs->bottom_field->motion);
00654 }
00655 }
00656
00657
00665 static void unmark_for_long_term_reference(FrameStore* fs)
00666 {
00667
00668 if (fs->is_used & 1)
00669 {
00670 if (fs->top_field)
00671 {
00672 fs->top_field->used_for_reference = 0;
00673 fs->top_field->is_long_term = 0;
00674 }
00675 }
00676 if (fs->is_used & 2)
00677 {
00678 if (fs->bottom_field)
00679 {
00680 fs->bottom_field->used_for_reference = 0;
00681 fs->bottom_field->is_long_term = 0;
00682 }
00683 }
00684 if (fs->is_used == 3)
00685 {
00686 if (fs->top_field && fs->bottom_field)
00687 {
00688 fs->top_field->used_for_reference = 0;
00689 fs->top_field->is_long_term = 0;
00690 fs->bottom_field->used_for_reference = 0;
00691 fs->bottom_field->is_long_term = 0;
00692 }
00693 fs->frame->used_for_reference = 0;
00694 fs->frame->is_long_term = 0;
00695 }
00696
00697 fs->is_reference = 0;
00698 fs->is_long_term = 0;
00699 }
00700
00701
00709 static inline int compare_pic_by_pic_num_desc( const void *arg1, const void *arg2 )
00710 {
00711 int pic_num1 = (*(StorablePicture**)arg1)->pic_num;
00712 int pic_num2 = (*(StorablePicture**)arg2)->pic_num;
00713
00714 if (pic_num1 < pic_num2)
00715 return 1;
00716 if (pic_num1 > pic_num2)
00717 return -1;
00718 else
00719 return 0;
00720 }
00721
00729 static inline int compare_pic_by_lt_pic_num_asc( const void *arg1, const void *arg2 )
00730 {
00731 int long_term_pic_num1 = (*(StorablePicture**)arg1)->long_term_pic_num;
00732 int long_term_pic_num2 = (*(StorablePicture**)arg2)->long_term_pic_num;
00733
00734 if ( long_term_pic_num1 < long_term_pic_num2)
00735 return -1;
00736 if ( long_term_pic_num1 > long_term_pic_num2)
00737 return 1;
00738 else
00739 return 0;
00740 }
00741
00749 static inline int compare_fs_by_frame_num_desc( const void *arg1, const void *arg2 )
00750 {
00751 int frame_num_wrap1 = (*(FrameStore**)arg1)->frame_num_wrap;
00752 int frame_num_wrap2 = (*(FrameStore**)arg2)->frame_num_wrap;
00753 if ( frame_num_wrap1 < frame_num_wrap2)
00754 return 1;
00755 if ( frame_num_wrap1 > frame_num_wrap2)
00756 return -1;
00757 else
00758 return 0;
00759 }
00760
00761
00769 static inline int compare_fs_by_lt_pic_idx_asc( const void *arg1, const void *arg2 )
00770 {
00771 int long_term_frame_idx1 = (*(FrameStore**)arg1)->long_term_frame_idx;
00772 int long_term_frame_idx2 = (*(FrameStore**)arg2)->long_term_frame_idx;
00773
00774 if ( long_term_frame_idx1 < long_term_frame_idx2)
00775 return -1;
00776 if ( long_term_frame_idx1 > long_term_frame_idx2)
00777 return 1;
00778 else
00779 return 0;
00780 }
00781
00782
00790 static inline int compare_pic_by_poc_asc( const void *arg1, const void *arg2 )
00791 {
00792 int poc1 = (*(StorablePicture**)arg1)->poc;
00793 int poc2 = (*(StorablePicture**)arg2)->poc;
00794
00795 if ( poc1 < poc2)
00796 return -1;
00797 if ( poc1 > poc2)
00798 return 1;
00799 else
00800 return 0;
00801 }
00802
00803
00811 static inline int compare_pic_by_poc_desc( const void *arg1, const void *arg2 )
00812 {
00813 int poc1 = (*(StorablePicture**)arg1)->poc;
00814 int poc2 = (*(StorablePicture**)arg2)->poc;
00815
00816 if (poc1 < poc2)
00817 return 1;
00818 if (poc1 > poc2)
00819 return -1;
00820 else
00821 return 0;
00822 }
00823
00824
00832 static inline int compare_fs_by_poc_asc( const void *arg1, const void *arg2 )
00833 {
00834 int poc1 = (*(FrameStore**)arg1)->poc;
00835 int poc2 = (*(FrameStore**)arg2)->poc;
00836
00837 if (poc1 < poc2)
00838 return -1;
00839 if (poc1 > poc2)
00840 return 1;
00841 else
00842 return 0;
00843 }
00844
00845
00853 static inline int compare_fs_by_poc_desc( const void *arg1, const void *arg2 )
00854 {
00855 int poc1 = (*(FrameStore**)arg1)->poc;
00856 int poc2 = (*(FrameStore**)arg2)->poc;
00857
00858 if (poc1 < poc2)
00859 return 1;
00860 if (poc1 > poc2)
00861 return -1;
00862 else
00863 return 0;
00864 }
00865
00866
00874 int is_short_ref(StorablePicture *s)
00875 {
00876 return ((s->used_for_reference) && (!(s->is_long_term)));
00877 }
00878
00879
00887 int is_long_ref(StorablePicture *s)
00888 {
00889 return ((s->used_for_reference) && (s->is_long_term));
00890 }
00891
00892
00900 static void gen_pic_list_from_frame_list(PictureStructure currStructure, FrameStore **fs_list, int list_idx, StorablePicture **list, char *list_size, int long_term)
00901 {
00902 int top_idx = 0;
00903 int bot_idx = 0;
00904
00905 int (*is_ref)(StorablePicture *s);
00906
00907 if (long_term)
00908 is_ref=is_long_ref;
00909 else
00910 is_ref=is_short_ref;
00911
00912 if (currStructure == TOP_FIELD)
00913 {
00914 while ((top_idx<list_idx)||(bot_idx<list_idx))
00915 {
00916 for ( ; top_idx<list_idx; top_idx++)
00917 {
00918 if(fs_list[top_idx]->is_used & 1)
00919 {
00920 if(is_ref(fs_list[top_idx]->top_field))
00921 {
00922
00923 list[(short) *list_size] = fs_list[top_idx]->top_field;
00924 (*list_size)++;
00925 top_idx++;
00926 break;
00927 }
00928 }
00929 }
00930 for ( ; bot_idx<list_idx; bot_idx++)
00931 {
00932 if(fs_list[bot_idx]->is_used & 2)
00933 {
00934 if(is_ref(fs_list[bot_idx]->bottom_field))
00935 {
00936
00937 list[(short) *list_size] = fs_list[bot_idx]->bottom_field;
00938 (*list_size)++;
00939 bot_idx++;
00940 break;
00941 }
00942 }
00943 }
00944 }
00945 }
00946 if (currStructure == BOTTOM_FIELD)
00947 {
00948 while ((top_idx<list_idx)||(bot_idx<list_idx))
00949 {
00950 for ( ; bot_idx<list_idx; bot_idx++)
00951 {
00952 if(fs_list[bot_idx]->is_used & 2)
00953 {
00954 if(is_ref(fs_list[bot_idx]->bottom_field))
00955 {
00956
00957 list[(short) *list_size] = fs_list[bot_idx]->bottom_field;
00958 (*list_size)++;
00959 bot_idx++;
00960 break;
00961 }
00962 }
00963 }
00964 for ( ; top_idx<list_idx; top_idx++)
00965 {
00966 if(fs_list[top_idx]->is_used & 1)
00967 {
00968 if(is_ref(fs_list[top_idx]->top_field))
00969 {
00970
00971 list[(short) *list_size] = fs_list[top_idx]->top_field;
00972 (*list_size)++;
00973 top_idx++;
00974 break;
00975 }
00976 }
00977 }
00978 }
00979 }
00980 }
00981
00982
00990 void init_lists(Slice *currSlice)
00991 {
00992 ImageParameters *p_Img = currSlice->p_Img;
00993 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
00994 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
00995
00996 int add_top = 0, add_bottom = 0;
00997 unsigned i;
00998 int j;
00999 int MaxFrameNum = 1 << (active_sps->log2_max_frame_num_minus4 + 4);
01000 int diff;
01001
01002 int list0idx = 0;
01003 int list0idx_1 = 0;
01004 int listltidx = 0;
01005
01006 FrameStore **fs_list0;
01007 FrameStore **fs_list1;
01008 FrameStore **fs_listlt;
01009
01010 StorablePicture *tmp_s;
01011
01012 if (currSlice->structure == FRAME)
01013 {
01014 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01015 {
01016 if (p_Dpb->fs_ref[i]->is_used==3)
01017 {
01018 if ((p_Dpb->fs_ref[i]->frame->used_for_reference)&&(!p_Dpb->fs_ref[i]->frame->is_long_term))
01019 {
01020 if( p_Dpb->fs_ref[i]->frame_num > p_Img->frame_num )
01021 {
01022 p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num - MaxFrameNum;
01023 }
01024 else
01025 {
01026 p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num;
01027 }
01028 p_Dpb->fs_ref[i]->frame->pic_num = p_Dpb->fs_ref[i]->frame_num_wrap;
01029 }
01030 }
01031 }
01032
01033 for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++)
01034 {
01035 if (p_Dpb->fs_ltref[i]->is_used==3)
01036 {
01037 if (p_Dpb->fs_ltref[i]->frame->is_long_term)
01038 {
01039 p_Dpb->fs_ltref[i]->frame->long_term_pic_num = p_Dpb->fs_ltref[i]->frame->long_term_frame_idx;
01040 }
01041 }
01042 }
01043 }
01044 else
01045 {
01046 if (currSlice->structure == TOP_FIELD)
01047 {
01048 add_top = 1;
01049 add_bottom = 0;
01050 }
01051 else
01052 {
01053 add_top = 0;
01054 add_bottom = 1;
01055 }
01056
01057 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01058 {
01059 if (p_Dpb->fs_ref[i]->is_reference)
01060 {
01061 if( p_Dpb->fs_ref[i]->frame_num > p_Img->frame_num )
01062 {
01063 p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num - MaxFrameNum;
01064 }
01065 else
01066 {
01067 p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num;
01068 }
01069 if (p_Dpb->fs_ref[i]->is_reference & 1)
01070 {
01071 p_Dpb->fs_ref[i]->top_field->pic_num = (2 * p_Dpb->fs_ref[i]->frame_num_wrap) + add_top;
01072 }
01073 if (p_Dpb->fs_ref[i]->is_reference & 2)
01074 {
01075 p_Dpb->fs_ref[i]->bottom_field->pic_num = (2 * p_Dpb->fs_ref[i]->frame_num_wrap) + add_bottom;
01076 }
01077 }
01078 }
01079
01080 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01081 {
01082 if (p_Dpb->fs_ltref[i]->is_long_term & 1)
01083 {
01084 p_Dpb->fs_ltref[i]->top_field->long_term_pic_num = 2 * p_Dpb->fs_ltref[i]->top_field->long_term_frame_idx + add_top;
01085 }
01086 if (p_Dpb->fs_ltref[i]->is_long_term & 2)
01087 {
01088 p_Dpb->fs_ltref[i]->bottom_field->long_term_pic_num = 2 * p_Dpb->fs_ltref[i]->bottom_field->long_term_frame_idx + add_bottom;
01089 }
01090 }
01091 }
01092
01093 if ((currSlice->slice_type == I_SLICE)||(currSlice->slice_type == SI_SLICE))
01094 {
01095 p_Img->listXsize[0] = 0;
01096 p_Img->listXsize[1] = 0;
01097 return;
01098 }
01099
01100 if ((currSlice->slice_type == P_SLICE)||(currSlice->slice_type == SP_SLICE))
01101 {
01102
01103 if (currSlice->structure == FRAME)
01104 {
01105 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01106 {
01107 if (p_Dpb->fs_ref[i]->is_used==3)
01108 {
01109 if ((p_Dpb->fs_ref[i]->frame->used_for_reference)&&(!p_Dpb->fs_ref[i]->frame->is_long_term))
01110 {
01111 p_Img->listX[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
01112 }
01113 }
01114 }
01115
01116 qsort((void *)p_Img->listX[0], list0idx, sizeof(StorablePicture*), compare_pic_by_pic_num_desc);
01117 p_Img->listXsize[0] = list0idx;
01118
01119
01120
01121 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01122 {
01123 if (p_Dpb->fs_ltref[i]->is_used==3)
01124 {
01125 if (p_Dpb->fs_ltref[i]->frame->is_long_term)
01126 {
01127 p_Img->listX[0][list0idx++]=p_Dpb->fs_ltref[i]->frame;
01128 }
01129 }
01130 }
01131 qsort((void *)&p_Img->listX[0][(short) p_Img->listXsize[0]], list0idx - p_Img->listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
01132 p_Img->listXsize[0] = list0idx;
01133 }
01134 else
01135 {
01136 fs_list0 = calloc(p_Dpb->size, sizeof (FrameStore*));
01137 if (NULL==fs_list0)
01138 no_mem_exit("init_lists: fs_list0");
01139 fs_listlt = calloc(p_Dpb->size, sizeof (FrameStore*));
01140 if (NULL==fs_listlt)
01141 no_mem_exit("init_lists: fs_listlt");
01142
01143 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01144 {
01145 if (p_Dpb->fs_ref[i]->is_reference)
01146 {
01147 fs_list0[list0idx++] = p_Dpb->fs_ref[i];
01148 }
01149 }
01150
01151 qsort((void *)fs_list0, list0idx, sizeof(FrameStore*), compare_fs_by_frame_num_desc);
01152
01153
01154
01155 p_Img->listXsize[0] = 0;
01156 gen_pic_list_from_frame_list(currSlice->structure, fs_list0, list0idx, p_Img->listX[0], &p_Img->listXsize[0], 0);
01157
01158
01159
01160
01161 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01162 {
01163 fs_listlt[listltidx++]=p_Dpb->fs_ltref[i];
01164 }
01165
01166 qsort((void *)fs_listlt, listltidx, sizeof(FrameStore*), compare_fs_by_lt_pic_idx_asc);
01167
01168 gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, p_Img->listX[0], &p_Img->listXsize[0], 1);
01169
01170 free(fs_list0);
01171 free(fs_listlt);
01172 }
01173 p_Img->listXsize[1] = 0;
01174 }
01175 else
01176 {
01177
01178 if (currSlice->structure == FRAME)
01179 {
01180 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01181 {
01182 if (p_Dpb->fs_ref[i]->is_used==3)
01183 {
01184 if ((p_Dpb->fs_ref[i]->frame->used_for_reference)&&(!p_Dpb->fs_ref[i]->frame->is_long_term))
01185 {
01186 if (p_Img->framepoc >= p_Dpb->fs_ref[i]->frame->poc)
01187
01188 {
01189 p_Img->listX[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
01190 }
01191 }
01192 }
01193 }
01194 qsort((void *)p_Img->listX[0], list0idx, sizeof(StorablePicture*), compare_pic_by_poc_desc);
01195 list0idx_1 = list0idx;
01196 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01197 {
01198 if (p_Dpb->fs_ref[i]->is_used==3)
01199 {
01200 if ((p_Dpb->fs_ref[i]->frame->used_for_reference)&&(!p_Dpb->fs_ref[i]->frame->is_long_term))
01201 {
01202 if (p_Img->framepoc < p_Dpb->fs_ref[i]->frame->poc)
01203 {
01204 p_Img->listX[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
01205 }
01206 }
01207 }
01208 }
01209 qsort((void *)&p_Img->listX[0][list0idx_1], list0idx-list0idx_1, sizeof(StorablePicture*), compare_pic_by_poc_asc);
01210
01211 for (j=0; j<list0idx_1; j++)
01212 {
01213 p_Img->listX[1][list0idx-list0idx_1+j]=p_Img->listX[0][j];
01214 }
01215 for (j=list0idx_1; j<list0idx; j++)
01216 {
01217 p_Img->listX[1][j-list0idx_1]=p_Img->listX[0][j];
01218 }
01219
01220 p_Img->listXsize[0] = p_Img->listXsize[1] = list0idx;
01221
01222
01223
01224
01225
01226 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01227 {
01228 if (p_Dpb->fs_ltref[i]->is_used==3)
01229 {
01230 if (p_Dpb->fs_ltref[i]->frame->is_long_term)
01231 {
01232 p_Img->listX[0][list0idx] =p_Dpb->fs_ltref[i]->frame;
01233 p_Img->listX[1][list0idx++]=p_Dpb->fs_ltref[i]->frame;
01234 }
01235 }
01236 }
01237 qsort((void *)&p_Img->listX[0][(short) p_Img->listXsize[0]], list0idx-p_Img->listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
01238 qsort((void *)&p_Img->listX[1][(short) p_Img->listXsize[0]], list0idx-p_Img->listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
01239 p_Img->listXsize[0] = p_Img->listXsize[1] = list0idx;
01240 }
01241 else
01242 {
01243 fs_list0 = calloc(p_Dpb->size, sizeof (FrameStore*));
01244 if (NULL==fs_list0)
01245 no_mem_exit("init_lists: fs_list0");
01246 fs_list1 = calloc(p_Dpb->size, sizeof (FrameStore*));
01247 if (NULL==fs_list1)
01248 no_mem_exit("init_lists: fs_list1");
01249 fs_listlt = calloc(p_Dpb->size, sizeof (FrameStore*));
01250 if (NULL==fs_listlt)
01251 no_mem_exit("init_lists: fs_listlt");
01252
01253 p_Img->listXsize[0] = 0;
01254 p_Img->listXsize[1] = 1;
01255
01256 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01257 {
01258 if (p_Dpb->fs_ref[i]->is_used)
01259 {
01260 if (p_Img->ThisPOC >= p_Dpb->fs_ref[i]->poc)
01261 {
01262 fs_list0[list0idx++] = p_Dpb->fs_ref[i];
01263 }
01264 }
01265 }
01266 qsort((void *)fs_list0, list0idx, sizeof(FrameStore*), compare_fs_by_poc_desc);
01267 list0idx_1 = list0idx;
01268 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01269 {
01270 if (p_Dpb->fs_ref[i]->is_used)
01271 {
01272 if (p_Img->ThisPOC < p_Dpb->fs_ref[i]->poc)
01273 {
01274 fs_list0[list0idx++] = p_Dpb->fs_ref[i];
01275 }
01276 }
01277 }
01278 qsort((void *)&fs_list0[list0idx_1], list0idx-list0idx_1, sizeof(FrameStore*), compare_fs_by_poc_asc);
01279
01280 for (j=0; j<list0idx_1; j++)
01281 {
01282 fs_list1[list0idx-list0idx_1+j]=fs_list0[j];
01283 }
01284 for (j=list0idx_1; j<list0idx; j++)
01285 {
01286 fs_list1[j-list0idx_1]=fs_list0[j];
01287 }
01288
01289
01290
01291
01292 p_Img->listXsize[0] = 0;
01293 p_Img->listXsize[1] = 0;
01294 gen_pic_list_from_frame_list(currSlice->structure, fs_list0, list0idx, p_Img->listX[0], &p_Img->listXsize[0], 0);
01295 gen_pic_list_from_frame_list(currSlice->structure, fs_list1, list0idx, p_Img->listX[1], &p_Img->listXsize[1], 0);
01296
01297
01298
01299
01300
01301 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01302 {
01303 fs_listlt[listltidx++]=p_Dpb->fs_ltref[i];
01304 }
01305
01306 qsort((void *)fs_listlt, listltidx, sizeof(FrameStore*), compare_fs_by_lt_pic_idx_asc);
01307
01308 gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, p_Img->listX[0], &p_Img->listXsize[0], 1);
01309 gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, p_Img->listX[1], &p_Img->listXsize[1], 1);
01310
01311 free(fs_list0);
01312 free(fs_list1);
01313 free(fs_listlt);
01314 }
01315 }
01316
01317 if ((p_Img->listXsize[0] == p_Img->listXsize[1]) && (p_Img->listXsize[0] > 1))
01318 {
01319
01320 diff=0;
01321 for (j = 0; j< p_Img->listXsize[0]; j++)
01322 {
01323 if (p_Img->listX[0][j]!=p_Img->listX[1][j])
01324 diff=1;
01325 }
01326 if (!diff)
01327 {
01328 tmp_s = p_Img->listX[1][0];
01329 p_Img->listX[1][0]=p_Img->listX[1][1];
01330 p_Img->listX[1][1]=tmp_s;
01331 }
01332 }
01333
01334 p_Img->listXsize[0] = imin (p_Img->listXsize[0], currSlice->num_ref_idx_l0_active);
01335 p_Img->listXsize[1] = imin (p_Img->listXsize[1], currSlice->num_ref_idx_l1_active);
01336
01337
01338 for (i=p_Img->listXsize[0]; i< (MAX_LIST_SIZE) ; i++)
01339 {
01340 p_Img->listX[0][i] = p_Img->no_reference_picture;
01341
01342 }
01343 for (i=p_Img->listXsize[1]; i< (MAX_LIST_SIZE) ; i++)
01344 {
01345 p_Img->listX[1][i] = p_Img->no_reference_picture;
01346 }
01347 }
01348
01360 void init_mbaff_lists(ImageParameters *p_Img)
01361 {
01362 unsigned j;
01363 int i;
01364
01365 for (i=2;i<6;i++)
01366 {
01367 for (j=0; j<MAX_LIST_SIZE; j++)
01368 {
01369 p_Img->listX[i][j] = p_Img->no_reference_picture;
01370 }
01371 p_Img->listXsize[i]=0;
01372 }
01373
01374 for (i=0; i<p_Img->listXsize[0]; i++)
01375 {
01376 p_Img->listX[2][2*i ] = p_Img->listX[0][i]->top_field;
01377 p_Img->listX[2][2*i+1] = p_Img->listX[0][i]->bottom_field;
01378 p_Img->listX[4][2*i ] = p_Img->listX[0][i]->bottom_field;
01379 p_Img->listX[4][2*i+1] = p_Img->listX[0][i]->top_field;
01380 }
01381 p_Img->listXsize[2]=p_Img->listXsize[4]=p_Img->listXsize[0] * 2;
01382
01383 for (i=0; i<p_Img->listXsize[1]; i++)
01384 {
01385 p_Img->listX[3][2*i ] = p_Img->listX[1][i]->top_field;
01386 p_Img->listX[3][2*i+1] = p_Img->listX[1][i]->bottom_field;
01387 p_Img->listX[5][2*i ] = p_Img->listX[1][i]->bottom_field;
01388 p_Img->listX[5][2*i+1] = p_Img->listX[1][i]->top_field;
01389 }
01390 p_Img->listXsize[3]=p_Img->listXsize[5]=p_Img->listXsize[1] * 2;
01391 }
01392
01400 static StorablePicture* get_short_term_pic(ImageParameters *p_Img, int picNum)
01401 {
01402 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
01403 unsigned i;
01404
01405 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01406 {
01407 if (p_Img->structure==FRAME)
01408 {
01409 if (p_Dpb->fs_ref[i]->is_reference == 3)
01410 if ((!p_Dpb->fs_ref[i]->frame->is_long_term)&&(p_Dpb->fs_ref[i]->frame->pic_num == picNum))
01411 return p_Dpb->fs_ref[i]->frame;
01412 }
01413 else
01414 {
01415 if (p_Dpb->fs_ref[i]->is_reference & 1)
01416 if ((!p_Dpb->fs_ref[i]->top_field->is_long_term)&&(p_Dpb->fs_ref[i]->top_field->pic_num == picNum))
01417 return p_Dpb->fs_ref[i]->top_field;
01418 if (p_Dpb->fs_ref[i]->is_reference & 2)
01419 if ((!p_Dpb->fs_ref[i]->bottom_field->is_long_term)&&(p_Dpb->fs_ref[i]->bottom_field->pic_num == picNum))
01420 return p_Dpb->fs_ref[i]->bottom_field;
01421 }
01422 }
01423
01424 return p_Img->no_reference_picture;
01425 }
01426
01434 static StorablePicture* get_long_term_pic(ImageParameters *p_Img, int LongtermPicNum)
01435 {
01436 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
01437 unsigned i;
01438
01439 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01440 {
01441 if (p_Img->structure==FRAME)
01442 {
01443 if (p_Dpb->fs_ltref[i]->is_reference == 3)
01444 if ((p_Dpb->fs_ltref[i]->frame->is_long_term)&&(p_Dpb->fs_ltref[i]->frame->long_term_pic_num == LongtermPicNum))
01445 return p_Dpb->fs_ltref[i]->frame;
01446 }
01447 else
01448 {
01449 if (p_Dpb->fs_ltref[i]->is_reference & 1)
01450 if ((p_Dpb->fs_ltref[i]->top_field->is_long_term)&&(p_Dpb->fs_ltref[i]->top_field->long_term_pic_num == LongtermPicNum))
01451 return p_Dpb->fs_ltref[i]->top_field;
01452 if (p_Dpb->fs_ltref[i]->is_reference & 2)
01453 if ((p_Dpb->fs_ltref[i]->bottom_field->is_long_term)&&(p_Dpb->fs_ltref[i]->bottom_field->long_term_pic_num == LongtermPicNum))
01454 return p_Dpb->fs_ltref[i]->bottom_field;
01455 }
01456 }
01457 return NULL;
01458 }
01459
01467 static void reorder_short_term(ImageParameters *p_Img, StorablePicture **RefPicListX, int num_ref_idx_lX_active_minus1, int picNumLX, int *refIdxLX)
01468 {
01469 int cIdx, nIdx;
01470
01471 StorablePicture *picLX;
01472
01473 picLX = get_short_term_pic(p_Img, picNumLX);
01474
01475 for( cIdx = num_ref_idx_lX_active_minus1+1; cIdx > *refIdxLX; cIdx-- )
01476 RefPicListX[ cIdx ] = RefPicListX[ cIdx - 1];
01477
01478 RefPicListX[ (*refIdxLX)++ ] = picLX;
01479
01480 nIdx = *refIdxLX;
01481
01482 for( cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1+1; cIdx++ )
01483 if (RefPicListX[ cIdx ])
01484 if( (RefPicListX[ cIdx ]->is_long_term ) || (RefPicListX[ cIdx ]->pic_num != picNumLX ))
01485 RefPicListX[ nIdx++ ] = RefPicListX[ cIdx ];
01486
01487 }
01488
01489
01497 static void reorder_long_term(ImageParameters *p_Img, StorablePicture **RefPicListX, int num_ref_idx_lX_active_minus1, int LongTermPicNum, int *refIdxLX)
01498 {
01499 int cIdx, nIdx;
01500
01501 StorablePicture *picLX;
01502
01503 picLX = get_long_term_pic(p_Img, LongTermPicNum);
01504
01505 for( cIdx = num_ref_idx_lX_active_minus1+1; cIdx > *refIdxLX; cIdx-- )
01506 RefPicListX[ cIdx ] = RefPicListX[ cIdx - 1];
01507
01508 RefPicListX[ (*refIdxLX)++ ] = picLX;
01509
01510 nIdx = *refIdxLX;
01511
01512 for( cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1+1; cIdx++ )
01513 if (RefPicListX[ cIdx ])
01514 if( (!RefPicListX[ cIdx ]->is_long_term ) || (RefPicListX[ cIdx ]->long_term_pic_num != LongTermPicNum ))
01515 RefPicListX[ nIdx++ ] = RefPicListX[ cIdx ];
01516 }
01517
01518
01526 void reorder_ref_pic_list(ImageParameters *p_Img, StorablePicture **list, char *list_size, int num_ref_idx_lX_active_minus1, int *reordering_of_pic_nums_idc, int *abs_diff_pic_num_minus1, int *long_term_pic_idx)
01527 {
01528 int i;
01529
01530 int maxPicNum, currPicNum, picNumLXNoWrap, picNumLXPred, picNumLX;
01531 int refIdxLX = 0;
01532
01533 if (p_Img->structure==FRAME)
01534 {
01535 maxPicNum = p_Img->MaxFrameNum;
01536 currPicNum = p_Img->frame_num;
01537 }
01538 else
01539 {
01540 maxPicNum = 2 * p_Img->MaxFrameNum;
01541 currPicNum = 2 * p_Img->frame_num + 1;
01542 }
01543
01544 picNumLXPred = currPicNum;
01545
01546 for (i=0; reordering_of_pic_nums_idc[i]!=3; i++)
01547 {
01548 if (reordering_of_pic_nums_idc[i]>3)
01549 error ("Invalid remapping_of_pic_nums_idc command", 500);
01550
01551 if (reordering_of_pic_nums_idc[i] < 2)
01552 {
01553 if (reordering_of_pic_nums_idc[i] == 0)
01554 {
01555 if( picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 ) < 0 )
01556 picNumLXNoWrap = picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 ) + maxPicNum;
01557 else
01558 picNumLXNoWrap = picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 );
01559 }
01560 else
01561 {
01562 if( picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 ) >= maxPicNum )
01563 picNumLXNoWrap = picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 ) - maxPicNum;
01564 else
01565 picNumLXNoWrap = picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 );
01566 }
01567 picNumLXPred = picNumLXNoWrap;
01568
01569 if( picNumLXNoWrap > currPicNum )
01570 picNumLX = picNumLXNoWrap - maxPicNum;
01571 else
01572 picNumLX = picNumLXNoWrap;
01573
01574 reorder_short_term(p_Img, list, num_ref_idx_lX_active_minus1, picNumLX, &refIdxLX);
01575 }
01576 else
01577 {
01578 reorder_long_term(p_Img, list, num_ref_idx_lX_active_minus1, long_term_pic_idx[i], &refIdxLX);
01579 }
01580
01581 }
01582
01583 *list_size = num_ref_idx_lX_active_minus1 + 1;
01584 }
01585
01586
01587
01595 void update_ref_list(DecodedPictureBuffer *p_Dpb)
01596 {
01597 unsigned i, j;
01598 for (i=0, j=0; i<p_Dpb->used_size; i++)
01599 {
01600 if (is_short_term_reference(p_Dpb->fs[i]))
01601 {
01602 p_Dpb->fs_ref[j++]=p_Dpb->fs[i];
01603 }
01604 }
01605
01606 p_Dpb->ref_frames_in_buffer = j;
01607
01608 while (j<p_Dpb->size)
01609 {
01610 p_Dpb->fs_ref[j++]=NULL;
01611 }
01612 }
01613
01614
01623 void update_ltref_list(DecodedPictureBuffer *p_Dpb)
01624 {
01625 unsigned i, j;
01626 for (i=0, j=0; i<p_Dpb->used_size; i++)
01627 {
01628 if (is_long_term_reference(p_Dpb->fs[i]))
01629 {
01630 p_Dpb->fs_ltref[j++]=p_Dpb->fs[i];
01631 }
01632 }
01633
01634 p_Dpb->ltref_frames_in_buffer=j;
01635
01636 while (j<p_Dpb->size)
01637 {
01638 p_Dpb->fs_ltref[j++]=NULL;
01639 }
01640 }
01641
01649 static void idr_memory_management(ImageParameters *p_Img, StorablePicture* p)
01650 {
01651 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
01652 unsigned i;
01653
01654 assert (p->idr_flag);
01655
01656 if (p->no_output_of_prior_pics_flag)
01657 {
01658
01659 for (i=0; i<p_Dpb->used_size; i++)
01660 {
01661
01662 free_frame_store(p_Img, p_Dpb->fs[i]);
01663 p_Dpb->fs[i] = alloc_frame_store();
01664 }
01665 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01666 {
01667 p_Dpb->fs_ref[i]=NULL;
01668 }
01669 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01670 {
01671 p_Dpb->fs_ltref[i]=NULL;
01672 }
01673 p_Dpb->used_size=0;
01674 }
01675 else
01676 {
01677 flush_dpb(p_Img);
01678 }
01679 p_Dpb->last_picture = NULL;
01680
01681 update_ref_list(p_Dpb);
01682 update_ltref_list(p_Dpb);
01683 p_Dpb->last_output_poc = INT_MIN;
01684
01685 if (p->long_term_reference_flag)
01686 {
01687 p_Dpb->max_long_term_pic_idx = 0;
01688 p->is_long_term = 1;
01689 p->long_term_frame_idx = 0;
01690 }
01691 else
01692 {
01693 p_Dpb->max_long_term_pic_idx = -1;
01694 p->is_long_term = 0;
01695 }
01696 }
01697
01705 static void sliding_window_memory_management(DecodedPictureBuffer *p_Dpb, StorablePicture* p)
01706 {
01707 unsigned i;
01708
01709 assert (!p->idr_flag);
01710
01711 if (p_Dpb->ref_frames_in_buffer==p_Dpb->num_ref_frames - p_Dpb->ltref_frames_in_buffer)
01712 {
01713 for (i=0; i<p_Dpb->used_size;i++)
01714 {
01715 if (p_Dpb->fs[i]->is_reference && (!(p_Dpb->fs[i]->is_long_term)))
01716 {
01717 unmark_for_reference(p_Dpb->fs[i]);
01718 update_ref_list(p_Dpb);
01719 break;
01720 }
01721 }
01722 }
01723
01724 p->is_long_term = 0;
01725 }
01726
01733 static int get_pic_num_x (StorablePicture *p, int difference_of_pic_nums_minus1)
01734 {
01735 int currPicNum;
01736
01737 if (p->structure == FRAME)
01738 currPicNum = p->frame_num;
01739 else
01740 currPicNum = 2 * p->frame_num + 1;
01741
01742 return currPicNum - (difference_of_pic_nums_minus1 + 1);
01743 }
01744
01745
01752 static void mm_unmark_short_term_for_reference(DecodedPictureBuffer *p_Dpb, StorablePicture *p, int difference_of_pic_nums_minus1)
01753 {
01754 int picNumX;
01755
01756 unsigned i;
01757
01758 picNumX = get_pic_num_x(p, difference_of_pic_nums_minus1);
01759
01760 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01761 {
01762 if (p->structure == FRAME)
01763 {
01764 if ((p_Dpb->fs_ref[i]->is_reference==3) && (p_Dpb->fs_ref[i]->is_long_term==0))
01765 {
01766 if (p_Dpb->fs_ref[i]->frame->pic_num == picNumX)
01767 {
01768 unmark_for_reference(p_Dpb->fs_ref[i]);
01769 return;
01770 }
01771 }
01772 }
01773 else
01774 {
01775 if ((p_Dpb->fs_ref[i]->is_reference & 1) && (!(p_Dpb->fs_ref[i]->is_long_term & 1)))
01776 {
01777 if (p_Dpb->fs_ref[i]->top_field->pic_num == picNumX)
01778 {
01779 p_Dpb->fs_ref[i]->top_field->used_for_reference = 0;
01780 p_Dpb->fs_ref[i]->is_reference &= 2;
01781 if (p_Dpb->fs_ref[i]->is_used == 3)
01782 {
01783 p_Dpb->fs_ref[i]->frame->used_for_reference = 0;
01784 }
01785 return;
01786 }
01787 }
01788 if ((p_Dpb->fs_ref[i]->is_reference & 2) && (!(p_Dpb->fs_ref[i]->is_long_term & 2)))
01789 {
01790 if (p_Dpb->fs_ref[i]->bottom_field->pic_num == picNumX)
01791 {
01792 p_Dpb->fs_ref[i]->bottom_field->used_for_reference = 0;
01793 p_Dpb->fs_ref[i]->is_reference &= 1;
01794 if (p_Dpb->fs_ref[i]->is_used == 3)
01795 {
01796 p_Dpb->fs_ref[i]->frame->used_for_reference = 0;
01797 }
01798 return;
01799 }
01800 }
01801 }
01802 }
01803 }
01804
01805
01812 static void mm_unmark_long_term_for_reference(DecodedPictureBuffer *p_Dpb, StorablePicture *p, int long_term_pic_num)
01813 {
01814 unsigned i;
01815 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01816 {
01817 if (p->structure == FRAME)
01818 {
01819 if ((p_Dpb->fs_ltref[i]->is_reference==3) && (p_Dpb->fs_ltref[i]->is_long_term==3))
01820 {
01821 if (p_Dpb->fs_ltref[i]->frame->long_term_pic_num == long_term_pic_num)
01822 {
01823 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01824 }
01825 }
01826 }
01827 else
01828 {
01829 if ((p_Dpb->fs_ltref[i]->is_reference & 1) && ((p_Dpb->fs_ltref[i]->is_long_term & 1)))
01830 {
01831 if (p_Dpb->fs_ltref[i]->top_field->long_term_pic_num == long_term_pic_num)
01832 {
01833 p_Dpb->fs_ltref[i]->top_field->used_for_reference = 0;
01834 p_Dpb->fs_ltref[i]->top_field->is_long_term = 0;
01835 p_Dpb->fs_ltref[i]->is_reference &= 2;
01836 p_Dpb->fs_ltref[i]->is_long_term &= 2;
01837 if (p_Dpb->fs_ltref[i]->is_used == 3)
01838 {
01839 p_Dpb->fs_ltref[i]->frame->used_for_reference = 0;
01840 p_Dpb->fs_ltref[i]->frame->is_long_term = 0;
01841 }
01842 return;
01843 }
01844 }
01845 if ((p_Dpb->fs_ltref[i]->is_reference & 2) && ((p_Dpb->fs_ltref[i]->is_long_term & 2)))
01846 {
01847 if (p_Dpb->fs_ltref[i]->bottom_field->long_term_pic_num == long_term_pic_num)
01848 {
01849 p_Dpb->fs_ltref[i]->bottom_field->used_for_reference = 0;
01850 p_Dpb->fs_ltref[i]->bottom_field->is_long_term = 0;
01851 p_Dpb->fs_ltref[i]->is_reference &= 1;
01852 p_Dpb->fs_ltref[i]->is_long_term &= 1;
01853 if (p_Dpb->fs_ltref[i]->is_used == 3)
01854 {
01855 p_Dpb->fs_ltref[i]->frame->used_for_reference = 0;
01856 p_Dpb->fs_ltref[i]->frame->is_long_term = 0;
01857 }
01858 return;
01859 }
01860 }
01861 }
01862 }
01863 }
01864
01865
01872 static void unmark_long_term_frame_for_reference_by_frame_idx(DecodedPictureBuffer *p_Dpb, int long_term_frame_idx)
01873 {
01874 unsigned i;
01875 for(i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01876 {
01877 if (p_Dpb->fs_ltref[i]->long_term_frame_idx == long_term_frame_idx)
01878 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01879 }
01880 }
01881
01889 static void unmark_long_term_field_for_reference_by_frame_idx(ImageParameters *p_Img, PictureStructure structure, int long_term_frame_idx, int mark_current, unsigned curr_frame_num, int curr_pic_num)
01890 {
01891 unsigned i;
01892 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
01893
01894 assert(structure!=FRAME);
01895 if (curr_pic_num<0)
01896 curr_pic_num+=(2*p_Img->MaxFrameNum);
01897
01898 for(i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
01899 {
01900 if (p_Dpb->fs_ltref[i]->long_term_frame_idx == long_term_frame_idx)
01901 {
01902 if (structure == TOP_FIELD)
01903 {
01904 if ((p_Dpb->fs_ltref[i]->is_long_term == 3))
01905 {
01906 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01907 }
01908 else
01909 {
01910 if ((p_Dpb->fs_ltref[i]->is_long_term == 1))
01911 {
01912 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01913 }
01914 else
01915 {
01916 if (mark_current)
01917 {
01918 if (p_Dpb->last_picture)
01919 {
01920 if ( ( p_Dpb->last_picture != p_Dpb->fs_ltref[i] )|| p_Dpb->last_picture->frame_num != curr_frame_num)
01921 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01922 }
01923 else
01924 {
01925 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01926 }
01927 }
01928 else
01929 {
01930 if ((p_Dpb->fs_ltref[i]->frame_num) != (unsigned)(curr_pic_num/2))
01931 {
01932 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01933 }
01934 }
01935 }
01936 }
01937 }
01938 if (structure == BOTTOM_FIELD)
01939 {
01940 if ((p_Dpb->fs_ltref[i]->is_long_term == 3))
01941 {
01942 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01943 }
01944 else
01945 {
01946 if ((p_Dpb->fs_ltref[i]->is_long_term == 2))
01947 {
01948 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01949 }
01950 else
01951 {
01952 if (mark_current)
01953 {
01954 if (p_Dpb->last_picture)
01955 {
01956 if ( ( p_Dpb->last_picture != p_Dpb->fs_ltref[i] )|| p_Dpb->last_picture->frame_num != curr_frame_num)
01957 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01958 }
01959 else
01960 {
01961 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01962 }
01963 }
01964 else
01965 {
01966 if ((p_Dpb->fs_ltref[i]->frame_num) != (unsigned)(curr_pic_num/2))
01967 {
01968 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
01969 }
01970 }
01971 }
01972 }
01973 }
01974 }
01975 }
01976 }
01977
01978
01985 static void mark_pic_long_term(DecodedPictureBuffer *p_Dpb, StorablePicture* p, int long_term_frame_idx, int picNumX)
01986 {
01987 unsigned i;
01988 int add_top, add_bottom;
01989
01990 if (p->structure == FRAME)
01991 {
01992 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
01993 {
01994 if (p_Dpb->fs_ref[i]->is_reference == 3)
01995 {
01996 if ((!p_Dpb->fs_ref[i]->frame->is_long_term)&&(p_Dpb->fs_ref[i]->frame->pic_num == picNumX))
01997 {
01998 p_Dpb->fs_ref[i]->long_term_frame_idx = p_Dpb->fs_ref[i]->frame->long_term_frame_idx
01999 = long_term_frame_idx;
02000 p_Dpb->fs_ref[i]->frame->long_term_pic_num = long_term_frame_idx;
02001 p_Dpb->fs_ref[i]->frame->is_long_term = 1;
02002
02003 if (p_Dpb->fs_ref[i]->top_field && p_Dpb->fs_ref[i]->bottom_field)
02004 {
02005 p_Dpb->fs_ref[i]->top_field->long_term_frame_idx = p_Dpb->fs_ref[i]->bottom_field->long_term_frame_idx
02006 = long_term_frame_idx;
02007 p_Dpb->fs_ref[i]->top_field->long_term_pic_num = long_term_frame_idx;
02008 p_Dpb->fs_ref[i]->bottom_field->long_term_pic_num = long_term_frame_idx;
02009
02010 p_Dpb->fs_ref[i]->top_field->is_long_term = p_Dpb->fs_ref[i]->bottom_field->is_long_term
02011 = 1;
02012
02013 }
02014 p_Dpb->fs_ref[i]->is_long_term = 3;
02015 return;
02016 }
02017 }
02018 }
02019 printf ("Warning: reference frame for long term marking not found\n");
02020 }
02021 else
02022 {
02023 if (p->structure == TOP_FIELD)
02024 {
02025 add_top = 1;
02026 add_bottom = 0;
02027 }
02028 else
02029 {
02030 add_top = 0;
02031 add_bottom = 1;
02032 }
02033 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
02034 {
02035 if (p_Dpb->fs_ref[i]->is_reference & 1)
02036 {
02037 if ((!p_Dpb->fs_ref[i]->top_field->is_long_term)&&(p_Dpb->fs_ref[i]->top_field->pic_num == picNumX))
02038 {
02039 if ((p_Dpb->fs_ref[i]->is_long_term) && (p_Dpb->fs_ref[i]->long_term_frame_idx != long_term_frame_idx))
02040 {
02041 printf ("Warning: assigning long_term_frame_idx different from other field\n");
02042 }
02043
02044 p_Dpb->fs_ref[i]->long_term_frame_idx = p_Dpb->fs_ref[i]->top_field->long_term_frame_idx
02045 = long_term_frame_idx;
02046 p_Dpb->fs_ref[i]->top_field->long_term_pic_num = 2 * long_term_frame_idx + add_top;
02047 p_Dpb->fs_ref[i]->top_field->is_long_term = 1;
02048 p_Dpb->fs_ref[i]->is_long_term |= 1;
02049 if (p_Dpb->fs_ref[i]->is_long_term == 3)
02050 {
02051 p_Dpb->fs_ref[i]->frame->is_long_term = 1;
02052 p_Dpb->fs_ref[i]->frame->long_term_frame_idx = p_Dpb->fs_ref[i]->frame->long_term_pic_num = long_term_frame_idx;
02053 }
02054 return;
02055 }
02056 }
02057 if (p_Dpb->fs_ref[i]->is_reference & 2)
02058 {
02059 if ((!p_Dpb->fs_ref[i]->bottom_field->is_long_term)&&(p_Dpb->fs_ref[i]->bottom_field->pic_num == picNumX))
02060 {
02061 if ((p_Dpb->fs_ref[i]->is_long_term) && (p_Dpb->fs_ref[i]->long_term_frame_idx != long_term_frame_idx))
02062 {
02063 printf ("Warning: assigning long_term_frame_idx different from other field\n");
02064 }
02065
02066 p_Dpb->fs_ref[i]->long_term_frame_idx = p_Dpb->fs_ref[i]->bottom_field->long_term_frame_idx
02067 = long_term_frame_idx;
02068 p_Dpb->fs_ref[i]->bottom_field->long_term_pic_num = 2 * long_term_frame_idx + add_bottom;
02069 p_Dpb->fs_ref[i]->bottom_field->is_long_term = 1;
02070 p_Dpb->fs_ref[i]->is_long_term |= 2;
02071 if (p_Dpb->fs_ref[i]->is_long_term == 3)
02072 {
02073 p_Dpb->fs_ref[i]->frame->is_long_term = 1;
02074 p_Dpb->fs_ref[i]->frame->long_term_frame_idx = p_Dpb->fs_ref[i]->frame->long_term_pic_num = long_term_frame_idx;
02075 }
02076 return;
02077 }
02078 }
02079 }
02080 printf ("Warning: reference field for long term marking not found\n");
02081 }
02082 }
02083
02084
02091 static void mm_assign_long_term_frame_idx(ImageParameters *p_Img, StorablePicture* p, int difference_of_pic_nums_minus1, int long_term_frame_idx)
02092 {
02093 int picNumX;
02094 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
02095
02096 picNumX = get_pic_num_x(p, difference_of_pic_nums_minus1);
02097
02098
02099 if (p->structure == FRAME)
02100 {
02101 unmark_long_term_frame_for_reference_by_frame_idx(p_Dpb, long_term_frame_idx);
02102 }
02103 else
02104 {
02105 unsigned i;
02106 PictureStructure structure = FRAME;
02107
02108 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
02109 {
02110 if (p_Dpb->fs_ref[i]->is_reference & 1)
02111 {
02112 if (p_Dpb->fs_ref[i]->top_field->pic_num == picNumX)
02113 {
02114 structure = TOP_FIELD;
02115 break;
02116 }
02117 }
02118 if (p_Dpb->fs_ref[i]->is_reference & 2)
02119 {
02120 if (p_Dpb->fs_ref[i]->bottom_field->pic_num == picNumX)
02121 {
02122 structure = BOTTOM_FIELD;
02123 break;
02124 }
02125 }
02126 }
02127 if (structure==FRAME)
02128 {
02129 error ("field for long term marking not found",200);
02130 }
02131
02132 unmark_long_term_field_for_reference_by_frame_idx(p_Img, structure, long_term_frame_idx, 0, 0, picNumX);
02133 }
02134
02135 mark_pic_long_term(p_Dpb, p, long_term_frame_idx, picNumX);
02136 }
02137
02144 void mm_update_max_long_term_frame_idx(DecodedPictureBuffer *p_Dpb, int max_long_term_frame_idx_plus1)
02145 {
02146 unsigned i;
02147
02148 p_Dpb->max_long_term_pic_idx = max_long_term_frame_idx_plus1 - 1;
02149
02150
02151 for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
02152 {
02153 if (p_Dpb->fs_ltref[i]->long_term_frame_idx > p_Dpb->max_long_term_pic_idx)
02154 {
02155 unmark_for_long_term_reference(p_Dpb->fs_ltref[i]);
02156 }
02157 }
02158 }
02159
02160
02167 static void mm_unmark_all_long_term_for_reference (DecodedPictureBuffer *p_Dpb)
02168 {
02169 mm_update_max_long_term_frame_idx(p_Dpb, 0);
02170 }
02171
02178 static void mm_unmark_all_short_term_for_reference (DecodedPictureBuffer *p_Dpb)
02179 {
02180 unsigned int i;
02181 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
02182 {
02183 unmark_for_reference(p_Dpb->fs_ref[i]);
02184 }
02185 update_ref_list(p_Dpb);
02186 }
02187
02188
02195 static void mm_mark_current_picture_long_term(ImageParameters *p_Img, DecodedPictureBuffer *p_Dpb, StorablePicture *p, int long_term_frame_idx)
02196 {
02197
02198 if (p->structure == FRAME)
02199 {
02200 unmark_long_term_frame_for_reference_by_frame_idx(p_Dpb, long_term_frame_idx);
02201 }
02202 else
02203 {
02204 unmark_long_term_field_for_reference_by_frame_idx(p_Img, p->structure, long_term_frame_idx, 1, p->pic_num, 0);
02205 }
02206
02207 p->is_long_term = 1;
02208 p->long_term_frame_idx = long_term_frame_idx;
02209 }
02210
02211
02218 static void adaptive_memory_management(ImageParameters *p_Img, StorablePicture* p)
02219 {
02220 DecRefPicMarking_t *tmp_drpm;
02221 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
02222
02223 p_Img->last_has_mmco_5 = 0;
02224
02225 assert (!p->idr_flag);
02226 assert (p->adaptive_ref_pic_buffering_flag);
02227
02228 while (p->dec_ref_pic_marking_buffer)
02229 {
02230 tmp_drpm = p->dec_ref_pic_marking_buffer;
02231 switch (tmp_drpm->memory_management_control_operation)
02232 {
02233 case 0:
02234 if (tmp_drpm->Next != NULL)
02235 {
02236 error ("memory_management_control_operation = 0 not last operation in buffer", 500);
02237 }
02238 break;
02239 case 1:
02240 mm_unmark_short_term_for_reference(p_Dpb, p, tmp_drpm->difference_of_pic_nums_minus1);
02241 update_ref_list(p_Dpb);
02242 break;
02243 case 2:
02244 mm_unmark_long_term_for_reference(p_Dpb, p, tmp_drpm->long_term_pic_num);
02245 update_ltref_list(p_Dpb);
02246 break;
02247 case 3:
02248 mm_assign_long_term_frame_idx(p_Img, p, tmp_drpm->difference_of_pic_nums_minus1, tmp_drpm->long_term_frame_idx);
02249 update_ref_list(p_Dpb);
02250 update_ltref_list(p_Dpb);
02251 break;
02252 case 4:
02253 mm_update_max_long_term_frame_idx (p_Dpb, tmp_drpm->max_long_term_frame_idx_plus1);
02254 update_ltref_list(p_Dpb);
02255 break;
02256 case 5:
02257 mm_unmark_all_short_term_for_reference(p_Dpb);
02258 mm_unmark_all_long_term_for_reference(p_Dpb);
02259 p_Img->last_has_mmco_5 = 1;
02260 break;
02261 case 6:
02262 mm_mark_current_picture_long_term(p_Img, p_Dpb, p, tmp_drpm->long_term_frame_idx);
02263 check_num_ref(p_Dpb);
02264 break;
02265 default:
02266 error ("invalid memory_management_control_operation in buffer", 500);
02267 }
02268 p->dec_ref_pic_marking_buffer = tmp_drpm->Next;
02269 free (tmp_drpm);
02270 }
02271 if ( p_Img->last_has_mmco_5 )
02272 {
02273 p->pic_num = p->frame_num = 0;
02274
02275 switch (p->structure)
02276 {
02277 case TOP_FIELD:
02278 {
02279 p->poc = p->top_poc = p_Img->toppoc =0;
02280 break;
02281 }
02282 case BOTTOM_FIELD:
02283 {
02284 p->poc = p->bottom_poc = p_Img->bottompoc = 0;
02285 break;
02286 }
02287 case FRAME:
02288 {
02289 p->top_poc -= p->poc;
02290 p->bottom_poc -= p->poc;
02291
02292 p_Img->toppoc = p->top_poc;
02293 p_Img->bottompoc = p->bottom_poc;
02294
02295 p->poc = imin (p->top_poc, p->bottom_poc);
02296 p_Img->framepoc = p->poc;
02297 break;
02298 }
02299 }
02300 p_Img->ThisPOC = p->poc;
02301 flush_dpb(p_Img);
02302 }
02303 }
02304
02305
02322 void store_picture_in_dpb(ImageParameters *p_Img, StorablePicture* p)
02323 {
02324 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
02325 unsigned i;
02326 int poc, pos;
02327
02328
02329
02330
02331
02332 assert (p!=NULL);
02333
02334 p_Img->last_has_mmco_5=0;
02335 p_Img->last_pic_bottom_field = (p->structure == BOTTOM_FIELD);
02336
02337 if (p->idr_flag)
02338 {
02339 idr_memory_management(p_Img, p);
02340
02341 memset(p_Img->pocs_in_dpb, 0, sizeof(int)*100);
02342 }
02343 else
02344 {
02345
02346 if (p->used_for_reference && (p->adaptive_ref_pic_buffering_flag))
02347 adaptive_memory_management(p_Img, p);
02348 }
02349
02350 if ((p->structure==TOP_FIELD)||(p->structure==BOTTOM_FIELD))
02351 {
02352
02353 if (p_Dpb->last_picture)
02354 {
02355 if ((int)p_Dpb->last_picture->frame_num == p->pic_num)
02356 {
02357 if (((p->structure==TOP_FIELD)&&(p_Dpb->last_picture->is_used==2))||((p->structure==BOTTOM_FIELD)&&(p_Dpb->last_picture->is_used==1)))
02358 {
02359 if ((p->used_for_reference && (p_Dpb->last_picture->is_orig_reference!=0))||
02360 (!p->used_for_reference && (p_Dpb->last_picture->is_orig_reference==0)))
02361 {
02362 insert_picture_in_dpb(p_Img, p_Dpb->last_picture, p);
02363 update_ref_list(p_Dpb);
02364 update_ltref_list(p_Dpb);
02365 dump_dpb(p_Dpb);
02366 p_Dpb->last_picture = NULL;
02367 return;
02368 }
02369 }
02370 }
02371 }
02372 }
02373
02374
02375
02376
02377 if ((!p->idr_flag)&&(p->used_for_reference && (!p->adaptive_ref_pic_buffering_flag)))
02378 {
02379 sliding_window_memory_management(p_Dpb, p);
02380 }
02381
02382
02383 if(p_Img->conceal_mode != 0)
02384 for(i=0;i<p_Dpb->size;i++)
02385 if(p_Dpb->fs[i]->is_reference)
02386 p_Dpb->fs[i]->concealment_reference = 1;
02387
02388
02389 if (p_Dpb->used_size==p_Dpb->size)
02390 {
02391
02392 if (p_Img->conceal_mode != 0)
02393 conceal_non_ref_pics(p_Img, 2);
02394
02395 remove_unused_frame_from_dpb(p_Img, p_Dpb);
02396
02397 if(p_Img->conceal_mode != 0)
02398 sliding_window_poc_management(p_Dpb, p);
02399 }
02400
02401
02402 while (p_Dpb->used_size == p_Dpb->size)
02403 {
02404
02405 if (!p->used_for_reference)
02406 {
02407 get_smallest_poc(p_Dpb, &poc, &pos);
02408 if ((-1==pos) || (p->poc < poc))
02409 {
02410 direct_output(p_Img, p, p_Img->p_out);
02411 return;
02412 }
02413 }
02414
02415 output_one_frame_from_dpb(p_Img);
02416 }
02417
02418
02419 if ((p->used_for_reference)&&(!p->is_long_term))
02420 {
02421 for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
02422 {
02423 if (p_Dpb->fs_ref[i]->frame_num == p->frame_num)
02424 {
02425 error("duplicate frame_num in short-term reference picture buffer", 500);
02426 }
02427 }
02428
02429 }
02430
02431 insert_picture_in_dpb(p_Img, p_Dpb->fs[p_Dpb->used_size],p);
02432
02433
02434 if (p->idr_flag)
02435 {
02436 p_Img->earlier_missing_poc = 0;
02437 }
02438
02439 if (p->structure != FRAME)
02440 {
02441 p_Dpb->last_picture = p_Dpb->fs[p_Dpb->used_size];
02442 }
02443 else
02444 {
02445 p_Dpb->last_picture = NULL;
02446 }
02447
02448 p_Dpb->used_size++;
02449
02450 if(p_Img->conceal_mode != 0)
02451 p_Img->pocs_in_dpb[p_Dpb->used_size-1] = p->poc;
02452
02453 update_ref_list(p_Dpb);
02454 update_ltref_list(p_Dpb);
02455
02456 check_num_ref(p_Dpb);
02457
02458 dump_dpb(p_Dpb);
02459 }
02460
02476 static void insert_picture_in_dpb(ImageParameters *p_Img, FrameStore* fs, StorablePicture* p)
02477 {
02478 InputParameters *p_Inp = p_Img->p_Inp;
02479
02480 assert (p!=NULL);
02481 assert (fs!=NULL);
02482 switch (p->structure)
02483 {
02484 case FRAME:
02485 fs->frame = p;
02486 fs->is_used = 3;
02487 if (p->used_for_reference)
02488 {
02489 fs->is_reference = 3;
02490 fs->is_orig_reference = 3;
02491 if (p->is_long_term)
02492 {
02493 fs->is_long_term = 3;
02494 fs->long_term_frame_idx = p->long_term_frame_idx;
02495 }
02496 }
02497
02498 dpb_split_field(p_Img, fs);
02499 break;
02500 case TOP_FIELD:
02501 fs->top_field = p;
02502 fs->is_used |= 1;
02503 if (p->used_for_reference)
02504 {
02505 fs->is_reference |= 1;
02506 fs->is_orig_reference |= 1;
02507 if (p->is_long_term)
02508 {
02509 fs->is_long_term |= 1;
02510 fs->long_term_frame_idx = p->long_term_frame_idx;
02511 }
02512 }
02513 if (fs->is_used == 3)
02514 {
02515
02516 dpb_combine_field(p_Img, fs);
02517 } else
02518 {
02519 fs->poc = p->poc;
02520 gen_field_ref_ids(p);
02521 }
02522 break;
02523 case BOTTOM_FIELD:
02524 fs->bottom_field = p;
02525 fs->is_used |= 2;
02526 if (p->used_for_reference)
02527 {
02528 fs->is_reference |= 2;
02529 fs->is_orig_reference |= 2;
02530 if (p->is_long_term)
02531 {
02532 fs->is_long_term |= 2;
02533 fs->long_term_frame_idx = p->long_term_frame_idx;
02534 }
02535 }
02536 if (fs->is_used == 3)
02537 {
02538
02539 dpb_combine_field(p_Img, fs);
02540 }
02541 else
02542 {
02543 fs->poc = p->poc;
02544 gen_field_ref_ids(p);
02545 }
02546 break;
02547 }
02548 fs->frame_num = p->pic_num;
02549 fs->recovery_frame = p->recovery_frame;
02550
02551 fs->is_output = p->is_output;
02552
02553 if (fs->is_used==3)
02554 {
02555 calculate_frame_no(p_Img, p);
02556 if (-1 != p_Img->p_ref && !p_Inp->silent)
02557 find_snr(p_Img, fs->frame, &p_Img->p_ref);
02558 }
02559 }
02560
02567 static int is_used_for_reference(FrameStore* fs)
02568 {
02569 if (fs->is_reference)
02570 {
02571 return 1;
02572 }
02573
02574 if (fs->is_used == 3)
02575 {
02576 if (fs->frame->used_for_reference)
02577 {
02578 return 1;
02579 }
02580 }
02581
02582 if (fs->is_used & 1)
02583 {
02584 if (fs->top_field)
02585 {
02586 if (fs->top_field->used_for_reference)
02587 {
02588 return 1;
02589 }
02590 }
02591 }
02592
02593 if (fs->is_used & 2)
02594 {
02595 if (fs->bottom_field)
02596 {
02597 if (fs->bottom_field->used_for_reference)
02598 {
02599 return 1;
02600 }
02601 }
02602 }
02603 return 0;
02604 }
02605
02606
02613 static int is_short_term_reference(FrameStore* fs)
02614 {
02615
02616 if (fs->is_used==3)
02617 {
02618 if ((fs->frame->used_for_reference)&&(!fs->frame->is_long_term))
02619 {
02620 return 1;
02621 }
02622 }
02623
02624 if (fs->is_used & 1)
02625 {
02626 if (fs->top_field)
02627 {
02628 if ((fs->top_field->used_for_reference)&&(!fs->top_field->is_long_term))
02629 {
02630 return 1;
02631 }
02632 }
02633 }
02634
02635 if (fs->is_used & 2)
02636 {
02637 if (fs->bottom_field)
02638 {
02639 if ((fs->bottom_field->used_for_reference)&&(!fs->bottom_field->is_long_term))
02640 {
02641 return 1;
02642 }
02643 }
02644 }
02645 return 0;
02646 }
02647
02648
02655 static int is_long_term_reference(FrameStore* fs)
02656 {
02657
02658 if (fs->is_used==3)
02659 {
02660 if ((fs->frame->used_for_reference)&&(fs->frame->is_long_term))
02661 {
02662 return 1;
02663 }
02664 }
02665
02666 if (fs->is_used & 1)
02667 {
02668 if (fs->top_field)
02669 {
02670 if ((fs->top_field->used_for_reference)&&(fs->top_field->is_long_term))
02671 {
02672 return 1;
02673 }
02674 }
02675 }
02676
02677 if (fs->is_used & 2)
02678 {
02679 if (fs->bottom_field)
02680 {
02681 if ((fs->bottom_field->used_for_reference)&&(fs->bottom_field->is_long_term))
02682 {
02683 return 1;
02684 }
02685 }
02686 }
02687 return 0;
02688 }
02689
02690
02697 static void remove_frame_from_dpb(ImageParameters *p_Img, int pos)
02698 {
02699 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
02700 FrameStore* fs = p_Dpb->fs[pos];
02701 FrameStore* tmp;
02702 unsigned i;
02703
02704
02705 switch (fs->is_used)
02706 {
02707 case 3:
02708 free_storable_picture(p_Img, fs->frame);
02709 free_storable_picture(p_Img, fs->top_field);
02710 free_storable_picture(p_Img, fs->bottom_field);
02711 fs->frame=NULL;
02712 fs->top_field=NULL;
02713 fs->bottom_field=NULL;
02714 break;
02715 case 2:
02716 free_storable_picture(p_Img, fs->bottom_field);
02717 fs->bottom_field=NULL;
02718 break;
02719 case 1:
02720 free_storable_picture(p_Img, fs->top_field);
02721 fs->top_field=NULL;
02722 break;
02723 case 0:
02724 break;
02725 default:
02726 error("invalid frame store type",500);
02727 }
02728 fs->is_used = 0;
02729 fs->is_long_term = 0;
02730 fs->is_reference = 0;
02731 fs->is_orig_reference = 0;
02732
02733
02734 tmp = p_Dpb->fs[pos];
02735
02736 for (i=pos; i<p_Dpb->used_size-1;i++)
02737 {
02738 p_Dpb->fs[i] = p_Dpb->fs[i+1];
02739 }
02740 p_Dpb->fs[p_Dpb->used_size-1] = tmp;
02741 p_Dpb->used_size--;
02742 }
02743
02750 static void get_smallest_poc(DecodedPictureBuffer *p_Dpb, int *poc,int * pos)
02751 {
02752 unsigned i;
02753
02754 if (p_Dpb->used_size<1)
02755 {
02756 error("Cannot determine smallest POC, DPB empty.",150);
02757 }
02758
02759 *pos=-1;
02760 *poc = INT_MAX;
02761 for (i=0; i<p_Dpb->used_size; i++)
02762 {
02763 if ((*poc > p_Dpb->fs[i]->poc)&&(!p_Dpb->fs[i]->is_output))
02764 {
02765 *poc = p_Dpb->fs[i]->poc;
02766 *pos=i;
02767 }
02768 }
02769 }
02770
02777 static int remove_unused_frame_from_dpb(ImageParameters *p_Img, DecodedPictureBuffer *p_Dpb)
02778 {
02779 unsigned i;
02780
02781
02782 for (i = 0; i < p_Dpb->used_size; i++)
02783 {
02784 if (p_Dpb->fs[i]->is_output && (!is_used_for_reference(p_Dpb->fs[i])))
02785 {
02786 remove_frame_from_dpb(p_Img, i);
02787 return 1;
02788 }
02789 }
02790 return 0;
02791 }
02792
02799 static void output_one_frame_from_dpb(ImageParameters *p_Img)
02800 {
02801 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
02802 int poc, pos;
02803
02804 if (p_Dpb->used_size<1)
02805 {
02806 error("Cannot output frame, DPB empty.",150);
02807 }
02808
02809
02810 get_smallest_poc(p_Dpb, &poc, &pos);
02811
02812 if(pos==-1)
02813 {
02814 error("no frames for output available", 150);
02815 }
02816
02817
02818
02819
02820
02821 if(p_Img->conceal_mode != 0)
02822 {
02823 if(p_Dpb->last_output_poc == 0)
02824 {
02825 write_lost_ref_after_idr(p_Img, pos);
02826 }
02827 write_lost_non_ref_pic(p_Img, poc, p_Img->p_out);
02828 }
02829
02830
02831
02832 write_stored_frame(p_Img, p_Dpb->fs[pos], p_Img->p_out);
02833
02834
02835 if(p_Img->conceal_mode == 0)
02836 if (p_Dpb->last_output_poc >= poc)
02837 {
02838 error ("output POC must be in ascending order", 150);
02839 }
02840 p_Dpb->last_output_poc = poc;
02841
02842 if (!is_used_for_reference(p_Dpb->fs[pos]))
02843 {
02844 remove_frame_from_dpb(p_Img, pos);
02845 }
02846 }
02847
02848
02849
02856 void flush_dpb(ImageParameters *p_Img)
02857 {
02858 unsigned i;
02859 DecodedPictureBuffer *p_Dpb = p_Img->p_Dpb;
02860
02861
02862
02863
02864
02865 if (p_Img->conceal_mode != 0)
02866 conceal_non_ref_pics(p_Img, 0);
02867
02868
02869 for (i=0; i<p_Dpb->used_size; i++)
02870 {
02871 unmark_for_reference (p_Dpb->fs[i]);
02872 }
02873
02874 while (remove_unused_frame_from_dpb(p_Img, p_Dpb)) ;
02875
02876
02877 while (p_Dpb->used_size)
02878 {
02879 output_one_frame_from_dpb(p_Img);
02880 }
02881
02882 p_Dpb->last_output_poc = INT_MIN;
02883 }
02884
02885
02886 static void gen_field_ref_ids(StorablePicture *p)
02887 {
02888 int i,j, dummylist0, dummylist1;
02890 for (i=0 ; i<p->size_x/4 ; i++)
02891 {
02892 for (j=0 ; j<p->size_y/4 ; j++)
02893 {
02894 dummylist0= p->motion.ref_idx[LIST_0][j][i];
02895 dummylist1= p->motion.ref_idx[LIST_1][j][i];
02897 p->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? p->ref_pic_num[p->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
02898 p->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? p->ref_pic_num[p->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
02899 p->motion.field_frame[j][i]=1;
02900 }
02901 }
02902 }
02903
02910 void dpb_split_field(ImageParameters *p_Img, FrameStore *fs)
02911 {
02912 int i, j, ii, jj, jj4;
02913 int idiv,jdiv;
02914 int currentmb;
02915 int dummylist0, dummylist1;
02916 int twosz16 = 2 * (fs->frame->size_x >> 4);
02917 StorablePicture *fs_top, *fs_btm;
02918 StorablePicture *frame = fs->frame;
02919
02920
02921 fs->poc = frame->poc;
02922
02923 if (!frame->frame_mbs_only_flag)
02924 {
02925 fs_top = fs->top_field = alloc_storable_picture(p_Img, TOP_FIELD, frame->size_x, frame->size_y, frame->size_x_cr, frame->size_y_cr);
02926 fs_btm = fs->bottom_field = alloc_storable_picture(p_Img, BOTTOM_FIELD, frame->size_x, frame->size_y, frame->size_x_cr, frame->size_y_cr);
02927
02928 for (i = 0; i < (frame->size_y>>1); i++)
02929 {
02930 memcpy(fs_top->imgY[i], frame->imgY[i*2], frame->size_x*sizeof(imgpel));
02931 }
02932
02933 for (i = 0; i< (frame->size_y_cr>>1); i++)
02934 {
02935 memcpy(fs_top->imgUV[0][i], frame->imgUV[0][i*2], frame->size_x_cr*sizeof(imgpel));
02936 memcpy(fs_top->imgUV[1][i], frame->imgUV[1][i*2], frame->size_x_cr*sizeof(imgpel));
02937 }
02938
02939 for (i = 0; i < (frame->size_y>>1); i++)
02940 {
02941 memcpy(fs_btm->imgY[i], frame->imgY[i*2 + 1], frame->size_x*sizeof(imgpel));
02942 }
02943
02944 for (i = 0; i < (frame->size_y_cr>>1); i++)
02945 {
02946 memcpy(fs_btm->imgUV[0][i], frame->imgUV[0][i*2 + 1], frame->size_x_cr*sizeof(imgpel));
02947 memcpy(fs_btm->imgUV[1][i], frame->imgUV[1][i*2 + 1], frame->size_x_cr*sizeof(imgpel));
02948 }
02949
02950 fs_top->poc = frame->top_poc;
02951 fs_btm->poc = frame->bottom_poc;
02952
02953 fs_top->frame_poc = frame->frame_poc;
02954
02955 fs_top->bottom_poc = fs_btm->bottom_poc = frame->bottom_poc;
02956 fs_top->top_poc = fs_btm->top_poc = frame->top_poc;
02957 fs_btm->frame_poc = frame->frame_poc;
02958
02959 fs_top->used_for_reference = fs_btm->used_for_reference
02960 = frame->used_for_reference;
02961 fs_top->is_long_term = fs_btm->is_long_term
02962 = frame->is_long_term;
02963 fs->long_term_frame_idx = fs_top->long_term_frame_idx
02964 = fs_btm->long_term_frame_idx
02965 = frame->long_term_frame_idx;
02966
02967 fs_top->coded_frame = fs_btm->coded_frame = 1;
02968 fs_top->MbaffFrameFlag = fs_btm->MbaffFrameFlag
02969 = frame->MbaffFrameFlag;
02970
02971 frame->top_field = fs_top;
02972 frame->bottom_field = fs_btm;
02973
02974 fs_top->bottom_field = fs_btm;
02975 fs_top->frame = frame;
02976 fs_btm->top_field = fs_top;
02977 fs_btm->frame = frame;
02978
02979 fs_top->chroma_format_idc = fs_btm->chroma_format_idc = frame->chroma_format_idc;
02980
02981
02982 for (j=0; j<=frame->max_slice_id; j++)
02983 {
02984 memcpy(&fs_top->ref_pic_num[j][LIST_0][0], &frame->ref_pic_num[j][2 + LIST_0][0], 66 * sizeof(int64));
02985
02986 memcpy(&fs_btm->ref_pic_num[j][LIST_0][0], &frame->ref_pic_num[j][4 + LIST_0][0], 66 * sizeof(int64));
02987
02988 }
02989 }
02990 else
02991 {
02992 fs_top=NULL;
02993 fs_btm=NULL;
02994 frame->top_field=NULL;
02995 frame->bottom_field=NULL;
02996 }
02997
02998 if (!frame->MbaffFrameFlag)
02999 {
03000 for (j = 0; (j < frame->size_y >> 2) ; j++)
03001 {
03002 jdiv = j >> 2;
03003 for (i = 0 ; i < (frame->size_x >> 2) ; i++)
03004 {
03005 idiv = (i >> 2);
03006
03007 dummylist0 = frame->motion.ref_idx[LIST_0][j][i];
03008 dummylist1 = frame->motion.ref_idx[LIST_1][j][i];
03009 frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? frame->ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_0][dummylist0] : -1;
03010 frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? frame->ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_1][dummylist1] : -1;
03011 }
03012 }
03013 }
03014 else
03015 {
03016 for (j = 0; (j < frame->size_y >> 2) ; j++)
03017 {
03018 jdiv = j >> 2;
03019 for (i = 0 ; i < (frame->size_x >> 2) ; i++)
03020 {
03021 idiv = (i >> 2);
03022 currentmb = twosz16*(jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
03023
03024 if (frame->motion.mb_field[currentmb])
03025 {
03026 int list_offset = currentmb%2? 4: 2;
03027 dummylist0 = frame->motion.ref_idx[LIST_0][j][i];
03028 dummylist1 = frame->motion.ref_idx[LIST_1][j][i];
03030 frame->motion.ref_id[LIST_0 + list_offset][j][i] = (dummylist0>=0)? frame->ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_0 + list_offset][dummylist0] : 0;
03031 frame->motion.ref_id[LIST_1 + list_offset][j][i] = (dummylist1>=0)? frame->ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_1 + list_offset][dummylist1] : 0;
03033 frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? frame->frm_ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_0 + list_offset][dummylist0] : 0;
03034 frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? frame->frm_ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_1 + list_offset][dummylist1] : 0;
03035
03036 }
03037 else
03038 {
03039 dummylist0 = frame->motion.ref_idx[LIST_0][j][i];
03040 dummylist1 = frame->motion.ref_idx[LIST_1][j][i];
03041 frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? frame->ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_0][dummylist0] : -1;
03042 frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? frame->ref_pic_num[frame->slice_id[jdiv][idiv]][LIST_1][dummylist1] : -1;
03043 }
03044 }
03045 }
03046 }
03047
03048 if (!frame->frame_mbs_only_flag && frame->MbaffFrameFlag)
03049 {
03050 PicMotionParams *frm_motion = &frame->motion;
03051 PicMotionParams *top_motion = &fs_top->motion;
03052 PicMotionParams *btm_motion = &fs_btm->motion;
03053 for (j=0 ; j< (frame->size_y >> 3); j++)
03054 {
03055 jj = (j >> 2)*8 + (j & 0x03);
03056 jj4 = jj + 4;
03057 jdiv = (j >> 1);
03058 for (i=0 ; i < (frame->size_x>>2); i++)
03059 {
03060 idiv = (i >> 2);
03061
03062 currentmb = twosz16*(jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
03063
03064 if (frm_motion->mb_field[currentmb])
03065 {
03066 btm_motion->field_frame[j][i] = top_motion->field_frame[j][i]=1;
03067 frm_motion->field_frame[2*j][i] = frm_motion->field_frame[2*j+1][i]=1;
03068
03069 btm_motion->mv[LIST_0][j][i][0] = frm_motion->mv[LIST_0][jj4][i][0];
03070 btm_motion->mv[LIST_0][j][i][1] = frm_motion->mv[LIST_0][jj4][i][1];
03071 btm_motion->mv[LIST_1][j][i][0] = frm_motion->mv[LIST_1][jj4][i][0];
03072 btm_motion->mv[LIST_1][j][i][1] = frm_motion->mv[LIST_1][jj4][i][1];
03073 btm_motion->ref_idx[LIST_0][j][i] = frm_motion->ref_idx[LIST_0][jj4][i];
03074 btm_motion->ref_idx[LIST_1][j][i] = frm_motion->ref_idx[LIST_1][jj4][i];
03075 btm_motion->ref_id[LIST_0][j][i] = frm_motion->ref_id[LIST_0+4][jj4][i];
03076 btm_motion->ref_id[LIST_1][j][i] = frm_motion->ref_id[LIST_1+4][jj4][i];
03077
03078
03079 top_motion->mv[LIST_0][j][i][0] = frm_motion->mv[LIST_0][jj][i][0];
03080 top_motion->mv[LIST_0][j][i][1] = frm_motion->mv[LIST_0][jj][i][1];
03081 top_motion->mv[LIST_1][j][i][0] = frm_motion->mv[LIST_1][jj][i][0];
03082 top_motion->mv[LIST_1][j][i][1] = frm_motion->mv[LIST_1][jj][i][1];
03083 top_motion->ref_idx[LIST_0][j][i] = frm_motion->ref_idx[LIST_0][jj][i];
03084 top_motion->ref_idx[LIST_1][j][i] = frm_motion->ref_idx[LIST_1][jj][i];
03085 top_motion->ref_id[LIST_0][j][i] = frm_motion->ref_id[LIST_0+2][jj][i];
03086 top_motion->ref_id[LIST_1][j][i] = frm_motion->ref_id[LIST_1+2][jj][i];
03087 }
03088 }
03089 }
03090 }
03091
03093 if (!frame->frame_mbs_only_flag)
03094 {
03095 for (j=0 ; j < (frame->size_y >> 3) ; j++)
03096 {
03097 jj = 2* RSD(j);
03098 jdiv = (j >> 1);
03099 for (i=0 ; i < (frame->size_x >> 2) ; i++)
03100 {
03101 ii = RSD(i);
03102 idiv = (i >> 2);
03103
03104 currentmb = twosz16 * (jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
03105
03106 if (!frame->MbaffFrameFlag || !frame->motion.mb_field[currentmb])
03107 {
03108 frame->motion.field_frame[2*j+1][i] = frame->motion.field_frame[2*j][i]=0;
03109
03110 fs_top->motion.field_frame[j][i] = fs_btm->motion.field_frame[j][i] = 0;
03111
03112 fs_top->motion.mv[LIST_0][j][i][0] = fs_btm->motion.mv[LIST_0][j][i][0] = frame->motion.mv[LIST_0][jj][ii][0];
03113 fs_top->motion.mv[LIST_0][j][i][1] = fs_btm->motion.mv[LIST_0][j][i][1] = frame->motion.mv[LIST_0][jj][ii][1];
03114 fs_top->motion.mv[LIST_1][j][i][0] = fs_btm->motion.mv[LIST_1][j][i][0] = frame->motion.mv[LIST_1][jj][ii][0];
03115 fs_top->motion.mv[LIST_1][j][i][1] = fs_btm->motion.mv[LIST_1][j][i][1] = frame->motion.mv[LIST_1][jj][ii][1];
03116
03117
03118 if (frame->motion.ref_idx[LIST_0][jj][ii] == -1)
03119 fs_top->motion.ref_idx[LIST_0][j][i] = fs_btm->motion.ref_idx[LIST_0][j][i] = - 1;
03120 else
03121 {
03122 dummylist0=fs_top->motion.ref_idx[LIST_0][j][i] = fs_btm->motion.ref_idx[LIST_0][j][i] = frame->motion.ref_idx[LIST_0][jj][ii] ;
03123 fs_top->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? frame->top_ref_pic_num[frame->slice_id[jj>>2][ii>>2]][LIST_0][dummylist0] : 0;
03124 fs_btm->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? frame->bottom_ref_pic_num[frame->slice_id[jj>>2][ii>>2]][LIST_0][dummylist0] : 0;
03125 }
03126
03127 if (frame->motion.ref_idx[LIST_1][jj][ii] == -1)
03128 fs_top->motion.ref_idx[LIST_1][j][i] = fs_btm->motion.ref_idx[LIST_1][j][i] = - 1;
03129 else
03130 {
03131 dummylist1=fs_top->motion.ref_idx[LIST_1][j][i] = fs_btm->motion.ref_idx[LIST_1][j][i] = frame->motion.ref_idx[LIST_1][jj][ii];
03132
03133 fs_top->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? frame->top_ref_pic_num[frame->slice_id[jj>>2][ii>>2]][LIST_1][dummylist1] : 0;
03134 fs_btm->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? frame->bottom_ref_pic_num[frame->slice_id[jj>>2][ii>>2]][LIST_1][dummylist1] : 0;
03135 }
03136 }
03137 else
03138 {
03139 frame->motion.field_frame[2*j+1][i] = frame->motion.field_frame[2*j][i]= frame->motion.mb_field[currentmb];
03140 }
03141 }
03142 }
03143 }
03144 else
03145 {
03146 memset( &(frame->motion.field_frame[0][0]), 0, (frame->size_y * frame->size_x >> 4) * sizeof(byte));
03147 }
03148 }
03149
03150
03158 void dpb_combine_field_yuv(ImageParameters *p_Img, FrameStore *fs)
03159 {
03160 int i, j;
03161
03162 fs->frame = alloc_storable_picture(p_Img, 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);
03163
03164 for (i=0; i<fs->top_field->size_y; i++)
03165 {
03166 memcpy(fs->frame->imgY[i*2], fs->top_field->imgY[i] , fs->top_field->size_x * sizeof(imgpel));
03167 memcpy(fs->frame->imgY[i*2 + 1], fs->bottom_field->imgY[i], fs->bottom_field->size_x * sizeof(imgpel));
03168 }
03169
03170 for (j = 0; j < 2; j++)
03171 {
03172 for (i=0; i<fs->top_field->size_y_cr; i++)
03173 {
03174 memcpy(fs->frame->imgUV[j][i*2], fs->top_field->imgUV[j][i], fs->top_field->size_x_cr*sizeof(imgpel));
03175 memcpy(fs->frame->imgUV[j][i*2 + 1], fs->bottom_field->imgUV[j][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
03176 }
03177 }
03178
03179 fs->poc=fs->frame->poc =fs->frame->frame_poc = imin (fs->top_field->poc, fs->bottom_field->poc);
03180
03181 fs->bottom_field->frame_poc=fs->top_field->frame_poc=fs->frame->poc;
03182
03183 fs->bottom_field->top_poc=fs->frame->top_poc=fs->top_field->poc;
03184 fs->top_field->bottom_poc=fs->frame->bottom_poc=fs->bottom_field->poc;
03185
03186 fs->frame->used_for_reference = (fs->top_field->used_for_reference && fs->bottom_field->used_for_reference );
03187 fs->frame->is_long_term = (fs->top_field->is_long_term && fs->bottom_field->is_long_term );
03188
03189 if (fs->frame->is_long_term)
03190 fs->frame->long_term_frame_idx = fs->long_term_frame_idx;
03191
03192 fs->frame->top_field = fs->top_field;
03193 fs->frame->bottom_field = fs->bottom_field;
03194
03195 fs->frame->coded_frame = 0;
03196
03197 fs->frame->chroma_format_idc = fs->top_field->chroma_format_idc;
03198 fs->frame->frame_cropping_flag = fs->top_field->frame_cropping_flag;
03199 if (fs->frame->frame_cropping_flag)
03200 {
03201 fs->frame->frame_cropping_rect_top_offset = fs->top_field->frame_cropping_rect_top_offset;
03202 fs->frame->frame_cropping_rect_bottom_offset = fs->top_field->frame_cropping_rect_bottom_offset;
03203 fs->frame->frame_cropping_rect_left_offset = fs->top_field->frame_cropping_rect_left_offset;
03204 fs->frame->frame_cropping_rect_right_offset = fs->top_field->frame_cropping_rect_right_offset;
03205 }
03206
03207 fs->top_field->frame = fs->bottom_field->frame = fs->frame;
03208 }
03209
03210
03217 void dpb_combine_field(ImageParameters *p_Img, FrameStore *fs)
03218 {
03219 int i,j, k, jj, jj4;
03220 int dummylist0, dummylist1;
03221
03222 dpb_combine_field_yuv(p_Img, fs);
03223
03224
03225
03226 for (j=0; j<=(imax(fs->top_field->max_slice_id, fs->bottom_field->max_slice_id)); j++)
03227 {
03228 for (k = LIST_0; k <= LIST_1; k++)
03229 {
03230 for (i=0;i<16;i++)
03231 {
03232 fs->frame->ref_pic_num[j][k][i]= i64min ((fs->top_field->ref_pic_num[j][k][2*i]/2)*2, (fs->bottom_field->ref_pic_num[j][k][2*i]/2)*2);
03233 }
03234 }
03235 }
03236
03238
03240 for (j=0 ; j < (fs->top_field->size_y >> 2) ; j++)
03241 {
03242 jj = 8*(j >> 2) + (j & 0x03);
03243 jj4 = jj + 4;
03244 for (i=0 ; i< (fs->top_field->size_x >> 2) ; i++)
03245 {
03246 fs->frame->motion.field_frame[jj][i]= fs->frame->motion.field_frame[jj4][i]=1;
03247
03248 fs->frame->motion.mv[LIST_0][jj][i][0] = fs->top_field->motion.mv[LIST_0][j][i][0];
03249 fs->frame->motion.mv[LIST_0][jj][i][1] = fs->top_field->motion.mv[LIST_0][j][i][1];
03250 fs->frame->motion.mv[LIST_1][jj][i][0] = fs->top_field->motion.mv[LIST_1][j][i][0];
03251 fs->frame->motion.mv[LIST_1][jj][i][1] = fs->top_field->motion.mv[LIST_1][j][i][1];
03252
03253 dummylist0=fs->frame->motion.ref_idx[LIST_0][jj][i] = fs->top_field->motion.ref_idx[LIST_0][j][i];
03254 dummylist1=fs->frame->motion.ref_idx[LIST_1][jj][i] = fs->top_field->motion.ref_idx[LIST_1][j][i];
03255
03257 fs->top_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->top_field->ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
03258 fs->top_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->top_field->ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
03259
03261 fs->frame->motion.ref_id[LIST_0][jj][i] = (dummylist0>=0)? fs->top_field->frm_ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
03262 fs->frame->motion.ref_id[LIST_1][jj][i] = (dummylist1>=0)? fs->top_field->frm_ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
03263
03264 fs->frame->motion.mv[LIST_0][jj4][i][0] = fs->bottom_field->motion.mv[LIST_0][j][i][0];
03265 fs->frame->motion.mv[LIST_0][jj4][i][1] = fs->bottom_field->motion.mv[LIST_0][j][i][1] ;
03266 fs->frame->motion.mv[LIST_1][jj4][i][0] = fs->bottom_field->motion.mv[LIST_1][j][i][0];
03267 fs->frame->motion.mv[LIST_1][jj4][i][1] = fs->bottom_field->motion.mv[LIST_1][j][i][1] ;
03268
03269 dummylist0=fs->frame->motion.ref_idx[LIST_0][jj4][i] = fs->bottom_field->motion.ref_idx[LIST_0][j][i];
03270 dummylist1=fs->frame->motion.ref_idx[LIST_1][jj4][i] = fs->bottom_field->motion.ref_idx[LIST_1][j][i];
03271
03272 fs->bottom_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->bottom_field->ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
03273 fs->bottom_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->bottom_field->ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
03274
03276 fs->frame->motion.ref_id[LIST_0][jj4][i] = (dummylist0>=0)? fs->bottom_field->frm_ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : -1;
03277 fs->frame->motion.ref_id[LIST_1][jj4][i] = (dummylist1>=0)? fs->bottom_field->frm_ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : -1;
03278
03279 fs->top_field->motion.field_frame[j][i]=1;
03280 fs->bottom_field->motion.field_frame[j][i]=1;
03281 }
03282 }
03283 }
03284
03285
03292 void alloc_ref_pic_list_reordering_buffer(Slice *currSlice)
03293 {
03294 ImageParameters *p_Img = currSlice->p_Img;
03295 int size = currSlice->num_ref_idx_l0_active + 1;
03296
03297 if (p_Img->type!=I_SLICE && p_Img->type!=SI_SLICE)
03298 {
03299 if ((currSlice->reordering_of_pic_nums_idc_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: reordering_of_pic_nums_idc_l0");
03300 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");
03301 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");
03302 }
03303 else
03304 {
03305 currSlice->reordering_of_pic_nums_idc_l0 = NULL;
03306 currSlice->abs_diff_pic_num_minus1_l0 = NULL;
03307 currSlice->long_term_pic_idx_l0 = NULL;
03308 }
03309
03310 size = currSlice->num_ref_idx_l1_active+1;
03311
03312 if (p_Img->type==B_SLICE)
03313 {
03314 if ((currSlice->reordering_of_pic_nums_idc_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: reordering_of_pic_nums_idc_l1");
03315 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");
03316 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");
03317 }
03318 else
03319 {
03320 currSlice->reordering_of_pic_nums_idc_l1 = NULL;
03321 currSlice->abs_diff_pic_num_minus1_l1 = NULL;
03322 currSlice->long_term_pic_idx_l1 = NULL;
03323 }
03324 }
03325
03326
03333 void free_ref_pic_list_reordering_buffer(Slice *currSlice)
03334 {
03335
03336 if (currSlice->reordering_of_pic_nums_idc_l0)
03337 free(currSlice->reordering_of_pic_nums_idc_l0);
03338 if (currSlice->abs_diff_pic_num_minus1_l0)
03339 free(currSlice->abs_diff_pic_num_minus1_l0);
03340 if (currSlice->long_term_pic_idx_l0)
03341 free(currSlice->long_term_pic_idx_l0);
03342
03343 currSlice->reordering_of_pic_nums_idc_l0 = NULL;
03344 currSlice->abs_diff_pic_num_minus1_l0 = NULL;
03345 currSlice->long_term_pic_idx_l0 = NULL;
03346
03347 if (currSlice->reordering_of_pic_nums_idc_l1)
03348 free(currSlice->reordering_of_pic_nums_idc_l1);
03349 if (currSlice->abs_diff_pic_num_minus1_l1)
03350 free(currSlice->abs_diff_pic_num_minus1_l1);
03351 if (currSlice->long_term_pic_idx_l1)
03352 free(currSlice->long_term_pic_idx_l1);
03353
03354 currSlice->reordering_of_pic_nums_idc_l1 = NULL;
03355 currSlice->abs_diff_pic_num_minus1_l1 = NULL;
03356 currSlice->long_term_pic_idx_l1 = NULL;
03357 }
03358
03370 void fill_frame_num_gap(ImageParameters *p_Img)
03371 {
03372 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
03373
03374 int CurrFrameNum;
03375 int UnusedShortTermFrameNum;
03376 StorablePicture *picture = NULL;
03377 int tmp1 = p_Img->delta_pic_order_cnt[0];
03378 int tmp2 = p_Img->delta_pic_order_cnt[1];
03379 p_Img->delta_pic_order_cnt[0] = p_Img->delta_pic_order_cnt[1] = 0;
03380
03381
03382
03383 UnusedShortTermFrameNum = (p_Img->pre_frame_num + 1) % p_Img->MaxFrameNum;
03384 CurrFrameNum = p_Img->frame_num;
03385
03386 while (CurrFrameNum != UnusedShortTermFrameNum)
03387 {
03388 picture = alloc_storable_picture (p_Img, FRAME, p_Img->width, p_Img->height, p_Img->width_cr, p_Img->height_cr);
03389 picture->coded_frame = 1;
03390 picture->pic_num = UnusedShortTermFrameNum;
03391 picture->frame_num = UnusedShortTermFrameNum;
03392 picture->non_existing = 1;
03393 picture->is_output = 1;
03394 picture->used_for_reference = 1;
03395
03396 picture->adaptive_ref_pic_buffering_flag = 0;
03397
03398 p_Img->frame_num = UnusedShortTermFrameNum;
03399 if (active_sps->pic_order_cnt_type!=0)
03400 {
03401 decode_poc(p_Img);
03402 }
03403 picture->top_poc=p_Img->toppoc;
03404 picture->bottom_poc=p_Img->bottompoc;
03405 picture->frame_poc=p_Img->framepoc;
03406 picture->poc=p_Img->framepoc;
03407
03408 store_picture_in_dpb(p_Img, picture);
03409
03410 picture=NULL;
03411 p_Img->pre_frame_num = UnusedShortTermFrameNum;
03412 UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % p_Img->MaxFrameNum;
03413 }
03414 p_Img->delta_pic_order_cnt[0] = tmp1;
03415 p_Img->delta_pic_order_cnt[1] = tmp2;
03416 p_Img->frame_num = CurrFrameNum;
03417
03418 }
03419
03427 void alloc_motion_params(MotionParams *ftype, int size_y, int size_x)
03428 {
03429 get_mem3Dint64 (&(ftype->ref_pic_id), 2, size_y, size_x);
03430 get_mem4Dshort (&(ftype->mv) , 2, size_y, size_x, 2);
03431 get_mem3D ((byte****)(&(ftype->ref_idx)) , 2, size_y, size_x);
03432 get_mem2D (&(ftype->moving_block) , size_y, size_x);
03433 }
03434
03451 ColocatedParams* alloc_colocated(int size_x, int size_y, int mb_adaptive_frame_field_flag)
03452 {
03453 ColocatedParams *s;
03454
03455 s = calloc(1, sizeof(ColocatedParams));
03456 if (NULL == s)
03457 no_mem_exit("alloc_colocated: s");
03458
03459 s->size_x = size_x;
03460 s->size_y = size_y;
03461
03462 alloc_motion_params(&s->frame, size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
03463
03464 if (mb_adaptive_frame_field_flag)
03465 {
03466 alloc_motion_params(&s->top , size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
03467 alloc_motion_params(&s->bottom, size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
03468 }
03469
03470 s->mb_adaptive_frame_field_flag = mb_adaptive_frame_field_flag;
03471
03472 return s;
03473 }
03474
03485 void free_colocated(ColocatedParams* p)
03486 {
03487 if (p)
03488 {
03489 free_mem3D ((byte***)p->frame.ref_idx);
03490 free_mem3Dint64 (p->frame.ref_pic_id);
03491 free_mem4Dshort (p->frame.mv);
03492
03493 if (p->frame.moving_block)
03494 {
03495 free_mem2D (p->frame.moving_block);
03496 p->frame.moving_block=NULL;
03497 }
03498
03499 if (p->mb_adaptive_frame_field_flag)
03500 {
03501 free_mem3D ((byte***)p->top.ref_idx);
03502 free_mem3Dint64 (p->top.ref_pic_id);
03503 free_mem4Dshort (p->top.mv);
03504
03505 if (p->top.moving_block)
03506 {
03507 free_mem2D (p->top.moving_block);
03508 p->top.moving_block=NULL;
03509 }
03510
03511 free_mem3D ((byte***)p->bottom.ref_idx);
03512 free_mem3Dint64 (p->bottom.ref_pic_id);
03513 free_mem4Dshort (p->bottom.mv);
03514
03515 if (p->bottom.moving_block)
03516 {
03517 free_mem2D (p->bottom.moving_block);
03518 p->bottom.moving_block=NULL;
03519 }
03520 }
03521
03522 free(p);
03523
03524 p = NULL;
03525 }
03526 }
03527
03536 void compute_colocated (Slice *currSlice, ColocatedParams* p, StorablePicture **listX[6])
03537 {
03538 StorablePicture *fs = listX[LIST_1 ][0];
03539 StorablePicture *fs_top = fs, *fs_bottom = fs;
03540 int i,j, ii, jj, jdiv;
03541 int fs_size_x4 = (fs->size_x >> 2);
03542 int fs_size_y4 = (fs->size_y >> 2);
03543 MotionParams *p_motion = &p->frame;
03544 PicMotionParams *p_frm_motion = &fs->motion;
03545 ImageParameters *p_Img = currSlice->p_Img;
03546 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
03547
03548
03549 if (currSlice->MbaffFrameFlag)
03550 {
03551 fs_top = listX[LIST_1 + 2][0];
03552 fs_bottom = listX[LIST_1 + 4][0];
03553 }
03554 else
03555 {
03556 if (p_Img->field_pic_flag)
03557 {
03558 if ((p_Img->structure != fs->structure) && (fs->coded_frame))
03559 {
03560 if (p_Img->structure==TOP_FIELD)
03561 {
03562 fs_top = fs_bottom = fs = listX[LIST_1 ][0]->top_field;
03563 }
03564 else
03565 {
03566 fs_top = fs_bottom = fs = listX[LIST_1 ][0]->bottom_field;
03567 }
03568 }
03569 p_frm_motion = &fs->motion;
03570 }
03571 }
03572
03573 if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
03574 {
03575 if (!currSlice->MbaffFrameFlag)
03576 {
03577 int k;
03578
03579 for (k = LIST_0; k<=LIST_1; k++)
03580 {
03581 for (j = 0; j < (fs->size_y>>2); j++)
03582 {
03583 for (i = 0 ; i < fs_size_x4 ; i++)
03584 {
03585 p_motion->mv[k][j][i][0] = p_frm_motion->mv[k][j][i][0];
03586 p_motion->mv[k][j][i][1] = p_frm_motion->mv[k][j][i][1];
03587 p_motion->ref_idx[k][j][i] = p_frm_motion->ref_idx[k][j][i];
03588 p_motion->ref_pic_id[k][j][i] = p_frm_motion->ref_id[k][j][i];
03589 }
03590 }
03591 }
03592 p->is_long_term = fs->is_long_term;
03593 }
03594 else
03595 {
03596 for (j=0 ; j < (fs->size_y>>2); j++)
03597 {
03598 jdiv = (j>>1);
03599 jj = jdiv + ((j>>3)<<2);
03600 for (i=0 ; i < fs_size_x4 ; i++)
03601 {
03602 if (p_frm_motion->field_frame[j][i])
03603 {
03607
03608 if (iabs(p_Img->dec_picture->poc - fs_bottom->poc)> iabs(p_Img->dec_picture->poc -fs_top->poc) )
03609 {
03610 p_motion->mv[LIST_0][j][i][0] = fs_top->motion.mv[LIST_0][jdiv][i][0];
03611 p_motion->mv[LIST_0][j][i][1] = fs_top->motion.mv[LIST_0][jdiv][i][1] ;
03612 p_motion->mv[LIST_1][j][i][0] = fs_top->motion.mv[LIST_1][jdiv][i][0];
03613 p_motion->mv[LIST_1][j][i][1] = fs_top->motion.mv[LIST_1][jdiv][i][1] ;
03614 p_motion->ref_idx[LIST_0][j][i] = fs_top->motion.ref_idx[LIST_0][jdiv][i];
03615 p_motion->ref_idx[LIST_1][j][i] = fs_top->motion.ref_idx[LIST_1][jdiv][i];
03616 p_motion->ref_pic_id[LIST_0][j][i] = p_frm_motion->ref_id[LIST_0][jj][i];
03617 p_motion->ref_pic_id[LIST_1][j][i] = p_frm_motion->ref_id[LIST_1][jj][i];
03618
03619 p->is_long_term = fs_top->is_long_term;
03620 }
03621 else
03622 {
03623 p_motion->mv[LIST_0][j][i][0] = fs_bottom->motion.mv[LIST_0][jdiv][i][0];
03624 p_motion->mv[LIST_0][j][i][1] = fs_bottom->motion.mv[LIST_0][jdiv][i][1] ;
03625 p_motion->mv[LIST_1][j][i][0] = fs_bottom->motion.mv[LIST_1][jdiv][i][0];
03626 p_motion->mv[LIST_1][j][i][1] = fs_bottom->motion.mv[LIST_1][jdiv][i][1] ;
03627 p_motion->ref_idx[LIST_0][j][i] = fs_bottom->motion.ref_idx[LIST_0][jdiv][i];
03628 p_motion->ref_idx[LIST_1][j][i] = fs_bottom->motion.ref_idx[LIST_1][jdiv][i];
03629 p_motion->ref_pic_id[LIST_0][j][i] = p_frm_motion->ref_id[LIST_0][jj + 4][i];
03630 p_motion->ref_pic_id[LIST_1][j][i] = p_frm_motion->ref_id[LIST_1][jj + 4][i];
03631
03632 p->is_long_term = fs_bottom->is_long_term;
03633 }
03634 }
03635 else
03636 {
03637 p_motion->mv[LIST_0][j][i][0] = p_frm_motion->mv[LIST_0][j][i][0];
03638 p_motion->mv[LIST_0][j][i][1] = p_frm_motion->mv[LIST_0][j][i][1] ;
03639 p_motion->mv[LIST_1][j][i][0] = p_frm_motion->mv[LIST_1][j][i][0];
03640 p_motion->mv[LIST_1][j][i][1] = p_frm_motion->mv[LIST_1][j][i][1] ;
03641 p_motion->ref_idx[LIST_0][j][i] = p_frm_motion->ref_idx[LIST_0][j][i];
03642 p_motion->ref_idx[LIST_1][j][i] = p_frm_motion->ref_idx[LIST_1][j][i];
03643 p_motion->ref_pic_id[LIST_0][j][i] = p_frm_motion->ref_id[LIST_0][j][i];
03644 p_motion->ref_pic_id[LIST_1][j][i] = p_frm_motion->ref_id[LIST_1][j][i];
03645
03646 p->is_long_term = fs->is_long_term;
03647 }
03648 }
03649 }
03650 }
03651 }
03652
03654 if (p_Img->structure || currSlice->MbaffFrameFlag)
03655 {
03656 for (j = 0; j < fs->size_y >> 3; j++)
03657 {
03658 jj = RSD(j);
03659 for (i = 0 ; i < fs->size_x >> 2; i++)
03660 {
03661 ii = RSD(i);
03663 if (!currSlice->MbaffFrameFlag )
03664 {
03665 p_motion->mv[LIST_0][j][i][0] = p_frm_motion->mv[LIST_0][jj][ii][0];
03666 p_motion->mv[LIST_0][j][i][1] = p_frm_motion->mv[LIST_0][jj][ii][1];
03667 p_motion->mv[LIST_1][j][i][0] = p_frm_motion->mv[LIST_1][jj][ii][0];
03668 p_motion->mv[LIST_1][j][i][1] = p_frm_motion->mv[LIST_1][jj][ii][1];
03669
03670
03671
03672 if (p_frm_motion->ref_idx[LIST_0][jj][ii] == -1)
03673 {
03674 p_motion->ref_idx [LIST_0][j][i] = -1;
03675 p_motion->ref_pic_id[LIST_0][j][i] = -1;
03676 }
03677 else
03678 {
03679 p_motion->ref_idx [LIST_0][j][i] = p_frm_motion->ref_idx[LIST_0][jj][ii] ;
03680 p_motion->ref_pic_id[LIST_0][j][i] = p_frm_motion->ref_id [LIST_0][jj][ii];
03681 }
03682
03683 if (p_frm_motion->ref_idx[LIST_1][jj][ii] == -1)
03684 {
03685 p_motion->ref_idx [LIST_1][j][i] = -1;
03686 p_motion->ref_pic_id[LIST_1][j][i] = -1;
03687 }
03688 else
03689 {
03690 p_motion->ref_idx [LIST_1][j][i] = p_frm_motion->ref_idx[LIST_1][jj][ii];
03691 p_motion->ref_pic_id[LIST_1][j][i] = p_frm_motion->ref_id [LIST_1][jj][ii];
03692 }
03693
03694 p->is_long_term = fs->is_long_term;
03695
03696 if (currSlice->direct_spatial_mv_pred_flag == 1)
03697 {
03698 p_motion->moving_block[j][i] =
03699 !((!p->is_long_term
03700 && ((p_motion->ref_idx[LIST_0][j][i] == 0)
03701 && (iabs(p_motion->mv[LIST_0][j][i][0])>>1 == 0)
03702 && (iabs(p_motion->mv[LIST_0][j][i][1])>>1 == 0)))
03703 || ((p_motion->ref_idx[LIST_0][j][i] == -1)
03704 && (p_motion->ref_idx[LIST_1][j][i] == 0)
03705 && (iabs(p_motion->mv[LIST_1][j][i][0])>>1 == 0)
03706 && (iabs(p_motion->mv[LIST_1][j][i][1])>>1 == 0)));
03707 }
03708 }
03709 else
03710 {
03711 p->bottom.mv[LIST_0][j][i][0] = fs_bottom->motion.mv[LIST_0][jj][ii][0];
03712 p->bottom.mv[LIST_0][j][i][1] = fs_bottom->motion.mv[LIST_0][jj][ii][1];
03713 p->bottom.mv[LIST_1][j][i][0] = fs_bottom->motion.mv[LIST_1][jj][ii][0];
03714 p->bottom.mv[LIST_1][j][i][1] = fs_bottom->motion.mv[LIST_1][jj][ii][1];
03715 p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->motion.ref_idx[LIST_0][jj][ii];
03716 p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->motion.ref_idx[LIST_1][jj][ii];
03717 p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->motion.ref_id[LIST_0][jj][ii];
03718 p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->motion.ref_id[LIST_1][jj][ii];
03719
03720 if (currSlice->direct_spatial_mv_pred_flag == 1)
03721 {
03722 p->bottom.moving_block[j][i] =
03723 !((!fs_bottom->is_long_term
03724 && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
03725 && (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
03726 && (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
03727 || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
03728 && (p->bottom.ref_idx[LIST_1][j][i] == 0)
03729 && (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
03730 && (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
03731 }
03732
03733 p->top.mv[LIST_0][j][i][0] = fs_top->motion.mv[LIST_0][jj][ii][0];
03734 p->top.mv[LIST_0][j][i][1] = fs_top->motion.mv[LIST_0][jj][ii][1];
03735 p->top.mv[LIST_1][j][i][0] = fs_top->motion.mv[LIST_1][jj][ii][0];
03736 p->top.mv[LIST_1][j][i][1] = fs_top->motion.mv[LIST_1][jj][ii][1];
03737 p->top.ref_idx[LIST_0][j][i] = fs_top->motion.ref_idx[LIST_0][jj][ii];
03738 p->top.ref_idx[LIST_1][j][i] = fs_top->motion.ref_idx[LIST_1][jj][ii];
03739 p->top.ref_pic_id[LIST_0][j][i] = fs_top->motion.ref_id[LIST_0][jj][ii];
03740 p->top.ref_pic_id[LIST_1][j][i] = fs_top->motion.ref_id[LIST_1][jj][ii];
03741
03742 if (currSlice->direct_spatial_mv_pred_flag == 1)
03743 {
03744 p->top.moving_block[j][i] =
03745 !((!fs_top->is_long_term
03746 && ((p->top.ref_idx[LIST_0][j][i] == 0)
03747 && (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
03748 && (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
03749 || ((p->top.ref_idx[LIST_0][j][i] == -1)
03750 && (p->top.ref_idx[LIST_1][j][i] == 0)
03751 && (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
03752 && (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
03753 }
03754
03755 if ((currSlice->direct_spatial_mv_pred_flag == 0 ) && !p_frm_motion->field_frame[2*j][i])
03756 {
03757 p->top.mv[LIST_0][j][i][1] /= 2;
03758 p->top.mv[LIST_1][j][i][1] /= 2;
03759 p->bottom.mv[LIST_0][j][i][1] /= 2;
03760 p->bottom.mv[LIST_1][j][i][1] /= 2;
03761 }
03762 }
03763 }
03764 }
03765 }
03766
03767
03768 if (!active_sps->frame_mbs_only_flag)
03769 {
03772 if (!p_Img->structure)
03773 {
03774 for (j=0 ; j < fs_size_y4; j++)
03775 {
03776 jdiv = (j >> 1);
03777 jj = jdiv + ((j >> 3) << 2);
03778 for (i = 0 ; i < fs_size_x4; i++)
03779 {
03780 if (p_frm_motion->field_frame[j][i])
03781 {
03782 if (iabs(p_Img->dec_picture->poc - fs->bottom_field->poc) > iabs(p_Img->dec_picture->poc - fs->top_field->poc))
03783 {
03784 p_motion->mv[LIST_0][j][i][0] = fs->top_field->motion.mv[LIST_0][jdiv][i][0];
03785 p_motion->mv[LIST_0][j][i][1] = fs->top_field->motion.mv[LIST_0][jdiv][i][1] ;
03786 p_motion->mv[LIST_1][j][i][0] = fs->top_field->motion.mv[LIST_1][jdiv][i][0];
03787 p_motion->mv[LIST_1][j][i][1] = fs->top_field->motion.mv[LIST_1][jdiv][i][1] ;
03788
03789 p_motion->ref_idx[LIST_0][j][i] = fs->top_field->motion.ref_idx[LIST_0][jdiv][i];
03790 p_motion->ref_idx[LIST_1][j][i] = fs->top_field->motion.ref_idx[LIST_1][jdiv][i];
03791 p_motion->ref_pic_id[LIST_0][j][i] = p_frm_motion->ref_id[LIST_0][jj][i];
03792 p_motion->ref_pic_id[LIST_1][j][i] = p_frm_motion->ref_id[LIST_1][jj][i];
03793 p->is_long_term = fs->top_field->is_long_term;
03794 }
03795 else
03796 {
03797 p_motion->mv[LIST_0][j][i][0] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][0];
03798 p_motion->mv[LIST_0][j][i][1] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][1] ;
03799 p_motion->mv[LIST_1][j][i][0] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][0];
03800 p_motion->mv[LIST_1][j][i][1] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][1] ;
03801
03802 p_motion->ref_idx[LIST_0][j][i] = fs->bottom_field->motion.ref_idx[LIST_0][jdiv][i];
03803 p_motion->ref_idx[LIST_1][j][i] = fs->bottom_field->motion.ref_idx[LIST_1][jdiv][i];
03804 p_motion->ref_pic_id[LIST_0][j][i] = p_frm_motion->ref_id[LIST_0][jj + 4][i];
03805 p_motion->ref_pic_id[LIST_1][j][i] = p_frm_motion->ref_id[LIST_1][jj + 4][i];
03806 p->is_long_term = fs->bottom_field->is_long_term;
03807 }
03808 }
03809 }
03810 }
03811 }
03812 }
03813
03814 p->is_long_term = fs->is_long_term;
03815
03816 if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
03817 {
03818 if (currSlice->direct_spatial_mv_pred_flag == 1)
03819 {
03820 for (j=0 ; j < (fs->size_y>>2); j++)
03821 {
03822 jj = RSD(j);
03823 for (i=0 ; i < (fs->size_x>>2); i++)
03824 {
03825 ii = RSD(i);
03826
03827 p_motion->mv[LIST_0][j][i][0]=p_motion->mv[LIST_0][jj][ii][0];
03828 p_motion->mv[LIST_0][j][i][1]=p_motion->mv[LIST_0][jj][ii][1];
03829 p_motion->mv[LIST_1][j][i][0]=p_motion->mv[LIST_1][jj][ii][0];
03830 p_motion->mv[LIST_1][j][i][1]=p_motion->mv[LIST_1][jj][ii][1];
03831
03832 p_motion->ref_idx[LIST_0][j][i]=p_motion->ref_idx[LIST_0][jj][ii];
03833 p_motion->ref_idx[LIST_1][j][i]=p_motion->ref_idx[LIST_1][jj][ii];
03834 p_motion->ref_pic_id[LIST_0][j][i] = p_motion->ref_pic_id[LIST_0][jj][ii];
03835 p_motion->ref_pic_id[LIST_1][j][i] = p_motion->ref_pic_id[LIST_1][jj][ii];
03836
03837 p_motion->moving_block[j][i]= (byte) (
03838 !((!p->is_long_term
03839 && ((p_motion->ref_idx[LIST_0][j][i] == 0)
03840 && (iabs(p_motion->mv[LIST_0][j][i][0])>>1 == 0)
03841 && (iabs(p_motion->mv[LIST_0][j][i][1])>>1 == 0)))
03842 || ((p_motion->ref_idx[LIST_0][j][i] == -1)
03843 && (p_motion->ref_idx[LIST_1][j][i] == 0)
03844 && (iabs(p_motion->mv[LIST_1][j][i][0])>>1 == 0)
03845 && (iabs(p_motion->mv[LIST_1][j][i][1])>>1 == 0))));
03846 }
03847 }
03848 }
03849 else
03850 {
03851 for (j=0 ; j < (fs->size_y>>2); j++)
03852 {
03853 jj = RSD(j);
03854 for (i=0 ; i < (fs->size_x>>2); i++)
03855 {
03856 ii = RSD(i);
03857
03858 p_motion->mv[LIST_0][j][i][0]=p_motion->mv[LIST_0][jj][ii][0];
03859 p_motion->mv[LIST_0][j][i][1]=p_motion->mv[LIST_0][jj][ii][1];
03860 p_motion->mv[LIST_1][j][i][0]=p_motion->mv[LIST_1][jj][ii][0];
03861 p_motion->mv[LIST_1][j][i][1]=p_motion->mv[LIST_1][jj][ii][1];
03862
03863 p_motion->ref_idx[LIST_0][j][i]=p_motion->ref_idx[LIST_0][jj][ii];
03864 p_motion->ref_idx[LIST_1][j][i]=p_motion->ref_idx[LIST_1][jj][ii];
03865 p_motion->ref_pic_id[LIST_0][j][i] = p_motion->ref_pic_id[LIST_0][jj][ii];
03866 p_motion->ref_pic_id[LIST_1][j][i] = p_motion->ref_pic_id[LIST_1][jj][ii];
03867 }
03868 }
03869 }
03870 }
03871 else
03872 {
03873 memcpy(&p_motion->mv[LIST_0][0][0][0], &p_frm_motion->mv[LIST_0][0][0][0], 4 * fs_size_y4 * fs_size_x4 * sizeof(short));
03874 memcpy(p_motion->ref_idx[LIST_0][0], p_frm_motion->ref_idx[LIST_0][0], 2 * fs_size_y4 * fs_size_x4 * sizeof(char));
03875 memcpy(p_motion->ref_pic_id[LIST_0][0], p_frm_motion->ref_id [LIST_0][0], 2 * fs_size_y4 * fs_size_x4 * sizeof(int64));
03876
03877 if (currSlice->direct_spatial_mv_pred_flag == 1)
03878 {
03879 for (j=0 ; j < fs_size_y4; j++)
03880 {
03881 for (i=0 ; i < fs_size_x4; i++)
03882 {
03883 p_motion->moving_block[j][i]=
03884 !((!p->is_long_term
03885 && ((p_motion->ref_idx[LIST_0][j][i] == 0)
03886 && (iabs(p_motion->mv[LIST_0][j][i][0])>>1 == 0)
03887 && (iabs(p_motion->mv[LIST_0][j][i][1])>>1 == 0)))
03888 || ((p_motion->ref_idx[LIST_0][j][i] == -1)
03889 && (p_motion->ref_idx[LIST_1][j][i] == 0)
03890 && (iabs(p_motion->mv[LIST_1][j][i][0])>>1 == 0)
03891 && (iabs(p_motion->mv[LIST_1][j][i][1])>>1 == 0)));
03892 }
03893 }
03894 }
03895 }
03896
03897 if (currSlice->direct_spatial_mv_pred_flag == 0)
03898 {
03899 if (currSlice->MbaffFrameFlag || !p_Img->structure)
03900 {
03901 for (j=0 ; j < fs_size_y4; j++)
03902 {
03903 for (i=0 ; i < fs_size_x4 ; i++)
03904 {
03905 if (p_frm_motion->field_frame[j][i])
03906 {
03907 p_motion->mv[LIST_0][j][i][1] *= 2;
03908 p_motion->mv[LIST_1][j][i][1] *= 2;
03909 }
03910 }
03911 }
03912 }
03913 else if (p_Img->structure)
03914 {
03915 for (j=0 ; j < fs_size_y4; j++)
03916 {
03917 for (i=0 ; i < fs_size_x4 ; i++)
03918 {
03919 if (!p_frm_motion->field_frame[j][i])
03920 {
03921 p_motion->mv[LIST_0][j][i][1] /= 2;
03922 p_motion->mv[LIST_1][j][i][1] /= 2;
03923 }
03924 }
03925 }
03926 }
03927
03928 for (j=0; j<2 + (currSlice->MbaffFrameFlag * 4);j+=2)
03929 {
03930 for (i=0; i<p_Img->listXsize[j];i++)
03931 {
03932 int prescale, iTRb, iTRp;
03933
03934 if (j==0)
03935 {
03936 iTRb = iClip3( -128, 127, p_Img->dec_picture->poc - listX[LIST_0 + j][i]->poc );
03937 }
03938 else if (j == 2)
03939 {
03940 iTRb = iClip3( -128, 127, p_Img->dec_picture->top_poc - listX[LIST_0 + j][i]->poc );
03941 }
03942 else
03943 {
03944 iTRb = iClip3( -128, 127, p_Img->dec_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
03945 }
03946
03947 iTRp = iClip3( -128, 127, listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
03948
03949 if (iTRp!=0)
03950 {
03951 prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
03952 currSlice->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
03953 }
03954 else
03955 {
03956 currSlice->mvscale[j][i] = 9999;
03957 }
03958 }
03959 }
03960 }
03961 }
03962
03972 void compute_colocated_JV(Slice *currSlice, ColocatedParams* p, StorablePicture **listX[6])
03973 {
03974 ImageParameters *p_Img = currSlice->p_Img;
03975 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
03976
03977 StorablePicture *fs, *fs_top, *fs_bottom;
03978 int i,j, ii, jj, jdiv;
03979 int np = p_Img->colour_plane_id;
03980
03981 fs_top = fs_bottom = fs = listX[LIST_1 ][0];
03982
03983 if (currSlice->MbaffFrameFlag)
03984 {
03985 fs_top= listX[LIST_1 + 2][0];
03986 fs_bottom= listX[LIST_1 + 4][0];
03987 }
03988 else
03989 {
03990 if (p_Img->field_pic_flag)
03991 {
03992 if ((p_Img->structure != fs->structure) && (fs->coded_frame))
03993 {
03994 if (p_Img->structure==TOP_FIELD)
03995 {
03996 fs_top=fs_bottom=fs = listX[LIST_1 ][0]->top_field;
03997 }
03998 else
03999 {
04000 fs_top=fs_bottom=fs = listX[LIST_1 ][0]->bottom_field;
04001 }
04002 }
04003 }
04004 }
04005
04006 if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
04007 {
04008 for (j=0 ; j<fs->size_y/4 ; j++)
04009 {
04010 jdiv = j/2;
04011 jj = j/2 + 4 * (j/8);
04012 for (i=0 ; i<fs->size_x/4 ; i++)
04013 {
04014
04015 if (currSlice->MbaffFrameFlag && fs->motion.field_frame[j][i])
04016 {
04020
04021 if (iabs(p_Img->dec_picture->poc - fs_bottom->poc)> iabs(p_Img->dec_picture->poc -fs_top->poc) )
04022 {
04023 p->frame.mv[LIST_0][j][i][0] = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][0];
04024 p->frame.mv[LIST_0][j][i][1] = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
04025 p->frame.mv[LIST_1][j][i][0] = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][0];
04026 p->frame.mv[LIST_1][j][i][1] = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
04027 p->frame.ref_idx[LIST_0][j][i] = fs_top->JVmotion[np].ref_idx[LIST_0][jdiv][i];
04028 p->frame.ref_idx[LIST_1][j][i] = fs_top->JVmotion[np].ref_idx[LIST_1][jdiv][i];
04029 p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj][i];
04030 p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj][i];
04031
04032 p->is_long_term = fs_top->is_long_term;
04033 }
04034 else
04035 {
04036 p->frame.mv[LIST_0][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][0];
04037 p->frame.mv[LIST_0][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
04038 p->frame.mv[LIST_1][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][0];
04039 p->frame.mv[LIST_1][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
04040 p->frame.ref_idx[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_0][jdiv][i];
04041 p->frame.ref_idx[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_1][jdiv][i];
04042 p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
04043 p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
04044
04045 p->is_long_term = fs_bottom->is_long_term;
04046 }
04047 }
04048 else
04049 {
04050 p->frame.mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][j][i][0];
04051 p->frame.mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][j][i][1] ;
04052 p->frame.mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][j][i][0];
04053 p->frame.mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][j][i][1] ;
04054 p->frame.ref_idx[LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][j][i];
04055 p->frame.ref_idx[LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][j][i];
04056 p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
04057 p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
04058
04059 p->is_long_term = fs->is_long_term;
04060 }
04061 }
04062 }
04063 }
04064
04065
04067 if (p_Img->structure || currSlice->MbaffFrameFlag)
04068 {
04069 for (j=0 ; j<fs->size_y/8 ; j++)
04070 {
04071 jj = RSD(j);
04072 for (i=0 ; i<fs->size_x/4 ; i++)
04073 {
04074 ii = RSD(i);
04076 if (!currSlice->MbaffFrameFlag )
04077 {
04078 p->frame.mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][jj][ii][0];
04079 p->frame.mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][jj][ii][1];
04080 p->frame.mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][jj][ii][0];
04081 p->frame.mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][jj][ii][1];
04082
04083
04084
04085 if (fs->JVmotion[np].ref_idx[LIST_0][jj][ii] == -1)
04086 {
04087 p->frame.ref_idx [LIST_0][j][i] = -1;
04088 p->frame.ref_pic_id[LIST_0][j][i] = -1;
04089 }
04090 else
04091 {
04092 p->frame.ref_idx [LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][jj][ii] ;
04093 p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id [LIST_0][jj][ii];
04094 }
04095
04096 if (fs->JVmotion[np].ref_idx[LIST_1][jj][ii] == -1)
04097 {
04098 p->frame.ref_idx [LIST_1][j][i] = -1;
04099 p->frame.ref_pic_id[LIST_1][j][i] = -1;
04100 }
04101 else
04102 {
04103 p->frame.ref_idx [LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][jj][ii];
04104 p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id [LIST_1][jj][ii];
04105 }
04106
04107 p->is_long_term = fs->is_long_term;
04108
04109 if (currSlice->direct_spatial_mv_pred_flag == 1)
04110 {
04111 p->frame.moving_block[j][i] =
04112 !((!p->is_long_term
04113 && ((p->frame.ref_idx[LIST_0][j][i] == 0)
04114 && (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
04115 && (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
04116 || ((p->frame.ref_idx[LIST_0][j][i] == -1)
04117 && (p->frame.ref_idx[LIST_1][j][i] == 0)
04118 && (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
04119 && (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0)));
04120 }
04121 }
04122 else
04123 {
04124 p->bottom.mv[LIST_0][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][0];
04125 p->bottom.mv[LIST_0][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][1];
04126 p->bottom.mv[LIST_1][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][0];
04127 p->bottom.mv[LIST_1][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][1];
04128 p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_0][jj][ii];
04129 p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_1][jj][ii];
04130 p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_0][jj][ii];
04131 p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_1][jj][ii];
04132
04133 if (currSlice->direct_spatial_mv_pred_flag == 1)
04134 {
04135 p->bottom.moving_block[j][i] =
04136 !((!fs_bottom->is_long_term
04137 && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
04138 && (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
04139 && (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
04140 || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
04141 && (p->bottom.ref_idx[LIST_1][j][i] == 0)
04142 && (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
04143 && (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
04144 }
04145
04146 p->top.mv[LIST_0][j][i][0] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][0];
04147 p->top.mv[LIST_0][j][i][1] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][1];
04148 p->top.mv[LIST_1][j][i][0] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][0];
04149 p->top.mv[LIST_1][j][i][1] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][1];
04150 p->top.ref_idx[LIST_0][j][i] = fs_top->JVmotion[np].ref_idx[LIST_0][jj][ii];
04151 p->top.ref_idx[LIST_1][j][i] = fs_top->JVmotion[np].ref_idx[LIST_1][jj][ii];
04152 p->top.ref_pic_id[LIST_0][j][i] = fs_top->JVmotion[np].ref_id[LIST_0][jj][ii];
04153 p->top.ref_pic_id[LIST_1][j][i] = fs_top->JVmotion[np].ref_id[LIST_1][jj][ii];
04154
04155 if (currSlice->direct_spatial_mv_pred_flag == 1)
04156 {
04157 p->top.moving_block[j][i] =
04158 !((!fs_top->is_long_term
04159 && ((p->top.ref_idx[LIST_0][j][i] == 0)
04160 && (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
04161 && (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
04162 || ((p->top.ref_idx[LIST_0][j][i] == -1)
04163 && (p->top.ref_idx[LIST_1][j][i] == 0)
04164 && (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
04165 && (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
04166 }
04167
04168 if ((currSlice->direct_spatial_mv_pred_flag == 0 ) && !fs->motion.field_frame[2*j][i])
04169 {
04170 p->top.mv[LIST_0][j][i][1] /= 2;
04171 p->top.mv[LIST_1][j][i][1] /= 2;
04172 p->bottom.mv[LIST_0][j][i][1] /= 2;
04173 p->bottom.mv[LIST_1][j][i][1] /= 2;
04174 }
04175
04176 }
04177 }
04178 }
04179 }
04180
04181
04182 if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
04183 {
04186
04187 if (!p_Img->structure)
04188 {
04189 for (j=0 ; j<fs->size_y/4 ; j++)
04190 {
04191 jdiv = j/2;
04192 jj = j/2 + 4*(j/8);
04193 for (i=0 ; i<fs->size_x/4 ; i++)
04194 {
04195
04196 if (fs->motion.field_frame[j][i])
04197 {
04198 if (iabs(p_Img->dec_picture->poc - fs->bottom_field->poc) > iabs(p_Img->dec_picture->poc - fs->top_field->poc))
04199 {
04200 p->frame.mv[LIST_0][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
04201 p->frame.mv[LIST_0][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
04202 p->frame.mv[LIST_1][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
04203 p->frame.mv[LIST_1][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
04204
04205 p->frame.ref_idx[LIST_0][j][i] = fs->top_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
04206 p->frame.ref_idx[LIST_1][j][i] = fs->top_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
04207 p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj][i];
04208 p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj][i];
04209 p->is_long_term = fs->top_field->is_long_term;
04210 }
04211 else
04212 {
04213 p->frame.mv[LIST_0][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
04214 p->frame.mv[LIST_0][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
04215 p->frame.mv[LIST_1][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
04216 p->frame.mv[LIST_1][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
04217
04218 p->frame.ref_idx[LIST_0][j][i] = fs->bottom_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
04219 p->frame.ref_idx[LIST_1][j][i] = fs->bottom_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
04220 p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
04221 p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
04222 p->is_long_term = fs->bottom_field->is_long_term;
04223 }
04224 }
04225 }
04226 }
04227 }
04228 }
04229
04230 p->is_long_term = fs->is_long_term;
04231
04232 if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
04233 {
04234 for (j=0 ; j<fs->size_y/4 ; j++)
04235 {
04236 jj = RSD(j);
04237 for (i=0 ; i<fs->size_x/4 ; i++)
04238 {
04239 ii = RSD(i);
04240
04241 p->frame.mv[LIST_0][j][i][0] = p->frame.mv[LIST_0][jj][ii][0];
04242 p->frame.mv[LIST_0][j][i][1] = p->frame.mv[LIST_0][jj][ii][1];
04243 p->frame.mv[LIST_1][j][i][0] = p->frame.mv[LIST_1][jj][ii][0];
04244 p->frame.mv[LIST_1][j][i][1] = p->frame.mv[LIST_1][jj][ii][1];
04245
04246 p->frame.ref_idx[LIST_0][j][i]=p->frame.ref_idx[LIST_0][jj][ii];
04247 p->frame.ref_idx[LIST_1][j][i]=p->frame.ref_idx[LIST_1][jj][ii];
04248 p->frame.ref_pic_id[LIST_0][j][i] = p->frame.ref_pic_id[LIST_0][jj][ii];
04249 p->frame.ref_pic_id[LIST_1][j][i] = p->frame.ref_pic_id[LIST_1][jj][ii];
04250
04251 if (currSlice->direct_spatial_mv_pred_flag == 1)
04252 {
04253 p->frame.moving_block[j][i]= (byte) (
04254 !((!p->is_long_term
04255 && ((p->frame.ref_idx[LIST_0][j][i] == 0)
04256 && (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
04257 && (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
04258 || ((p->frame.ref_idx[LIST_0][j][i] == -1)
04259 && (p->frame.ref_idx[LIST_1][j][i] == 0)
04260 && (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
04261 && (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0))));
04262 }
04263 }
04264 }
04265 }
04266 else
04267 {
04268 for (j=0 ; j<fs->size_y/4 ; j++)
04269 {
04270 jj = RSD(j);
04271 for (i=0 ; i<fs->size_x/4 ; i++)
04272 {
04273 ii = RSD(i);
04275 p->frame.mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][j][i][0];
04276 p->frame.mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][j][i][1];
04277 p->frame.mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][j][i][0];
04278 p->frame.mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][j][i][1];
04279
04280 p->frame.ref_idx[LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][j][i];
04281 p->frame.ref_idx[LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][j][i];
04282 p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
04283 p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
04284
04285 if (currSlice->direct_spatial_mv_pred_flag == 1)
04286 {
04287 p->frame.moving_block[j][i]= (byte) (
04288 !((!p->is_long_term
04289 && ((p->frame.ref_idx[LIST_0][j][i] == 0)
04290 && (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
04291 && (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
04292 || ((p->frame.ref_idx[LIST_0][j][i] == -1)
04293 && (p->frame.ref_idx[LIST_1][j][i] == 0)
04294 && (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
04295 && (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0))));
04296 }
04297 }
04298 }
04299 }
04300
04301
04302 if (currSlice->direct_spatial_mv_pred_flag == 0)
04303 {
04304 for (j=0 ; j<fs->size_y/4 ; j++)
04305 {
04306 for (i=0 ; i<fs->size_x/4 ; i++)
04307 {
04308 if ((!currSlice->MbaffFrameFlag &&!p_Img->structure && fs->motion.field_frame[j][i]) || (currSlice->MbaffFrameFlag && fs->motion.field_frame[j][i]))
04309 {
04310 p->frame.mv[LIST_0][j][i][1] *= 2;
04311 p->frame.mv[LIST_1][j][i][1] *= 2;
04312 }
04313 else if (p_Img->structure && !fs->motion.field_frame[j][i])
04314 {
04315 p->frame.mv[LIST_0][j][i][1] /= 2;
04316 p->frame.mv[LIST_1][j][i][1] /= 2;
04317 }
04318
04319 }
04320 }
04321
04322 for (j=0; j<2 + (currSlice->MbaffFrameFlag * 4);j+=2)
04323 {
04324 for (i=0; i<p_Img->listXsize[j];i++)
04325 {
04326 int prescale, iTRb, iTRp;
04327
04328 if (j==0)
04329 {
04330 iTRb = iClip3( -128, 127, p_Img->dec_picture->poc - listX[LIST_0 + j][i]->poc );
04331 }
04332 else if (j == 2)
04333 {
04334 iTRb = iClip3( -128, 127, p_Img->dec_picture->top_poc - listX[LIST_0 + j][i]->poc );
04335 }
04336 else
04337 {
04338 iTRb = iClip3( -128, 127, p_Img->dec_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
04339 }
04340
04341 iTRp = iClip3( -128, 127, listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
04342
04343 if (iTRp!=0)
04344 {
04345 prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
04346 currSlice->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
04347 }
04348 else
04349 {
04350 currSlice->mvscale[j][i] = 9999;
04351 }
04352 }
04353 }
04354 }
04355 }
04356
04357 void copy_storable_param_JV( ImageParameters *p_Img, PicMotionParams *JVplane, PicMotionParams *motion )
04358 {
04359 seq_parameter_set_rbsp_t *active_sps = p_Img->active_sps;
04360
04361 int md_size = (p_Img->height / BLOCK_SIZE) * (p_Img->width / BLOCK_SIZE);
04362 int ref_size = active_sps->frame_mbs_only_flag ? 2 * md_size : 6 * md_size;
04363
04364
04365 memcpy( JVplane->ref_idx[0][0], motion->ref_idx[0][0], 2 * md_size * sizeof(byte));
04366
04367
04368
04369 memcpy( JVplane->ref_pic_id[0][0], motion->ref_pic_id[0][0], ref_size * sizeof(int64));
04370
04371
04372 memcpy( JVplane->ref_id[0][0], motion->ref_id[0][0], ref_size * sizeof(int64));
04373
04374
04375 memcpy( JVplane->mv[0][0][0], motion->mv[0][0][0], 2 * md_size * 2 * sizeof(short) );
04376 }