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