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