mbuffer.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:136k
源码类别:

Audio

开发平台:

Visual C++

  1.   dpb.max_long_term_pic_idx = max_long_term_frame_idx_plus1 - 1;
  2.   // check for invalid frames
  3.   for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  4.   {
  5.     if (dpb.fs_ltref[i]->long_term_frame_idx > dpb.max_long_term_pic_idx)
  6.     {
  7.       unmark_for_long_term_reference(dpb.fs_ltref[i]);
  8.     }
  9.   }
  10. }
  11. /*!
  12.  ************************************************************************
  13.  * brief
  14.  *    Mark all long term reference pictures unused for reference
  15.  ************************************************************************
  16.  */
  17. static void mm_unmark_all_long_term_for_reference (void)
  18. {
  19.   mm_update_max_long_term_frame_idx(0);
  20. }
  21. /*!
  22.  ************************************************************************
  23.  * brief
  24.  *    Mark all short term reference pictures unused for reference
  25.  ************************************************************************
  26.  */
  27. static void mm_unmark_all_short_term_for_reference (void)
  28. {
  29.   unsigned int i;
  30.   for (i=0; i<dpb.ref_frames_in_buffer; i++)
  31.   {
  32.     unmark_for_reference(dpb.fs_ref[i]);
  33.   }
  34.   update_ref_list();
  35. }
  36. /*!
  37.  ************************************************************************
  38.  * brief
  39.  *    Mark the current picture used for long term reference
  40.  ************************************************************************
  41.  */
  42. static void mm_mark_current_picture_long_term(StorablePicture *p, int long_term_frame_idx)
  43. {
  44.   // remove long term pictures with same long_term_frame_idx
  45.   if (p->structure == FRAME)
  46.   {
  47.     unmark_long_term_frame_for_reference_by_frame_idx(long_term_frame_idx);
  48.   }
  49.   else
  50.   {
  51.     unmark_long_term_field_for_reference_by_frame_idx(p->structure, long_term_frame_idx, 1, p->pic_num, 0);
  52.   }
  53.   p->is_long_term = 1;
  54.   p->long_term_frame_idx = long_term_frame_idx;
  55. }
  56. /*!
  57.  ************************************************************************
  58.  * brief
  59.  *    Perform Adaptive memory control decoded reference picture marking process
  60.  ************************************************************************
  61.  */
  62. static void adaptive_memory_management(StorablePicture* p)
  63. {
  64.   DecRefPicMarking_t *tmp_drpm;
  65.   img->last_has_mmco_5 = 0;
  66.   assert (!img->currentPicture->idr_flag);
  67.   assert (img->adaptive_ref_pic_buffering_flag);
  68.   while (img->dec_ref_pic_marking_buffer)
  69.   {
  70.     tmp_drpm = img->dec_ref_pic_marking_buffer;
  71.     switch (tmp_drpm->memory_management_control_operation)
  72.     {
  73.       case 0:
  74.         if (tmp_drpm->Next != NULL)
  75.         {
  76.           error ("memory_management_control_operation = 0 not last operation in buffer", 500);
  77.         }
  78.         break;
  79.       case 1:
  80.         mm_unmark_short_term_for_reference(p, tmp_drpm->difference_of_pic_nums_minus1);
  81.         update_ref_list();
  82.         break;
  83.       case 2:
  84.         mm_unmark_long_term_for_reference(p, tmp_drpm->long_term_pic_num);
  85.         update_ltref_list();
  86.         break;
  87.       case 3:
  88.         mm_assign_long_term_frame_idx(p, tmp_drpm->difference_of_pic_nums_minus1, tmp_drpm->long_term_frame_idx);
  89.         update_ref_list();
  90.         update_ltref_list();
  91.         break;
  92.       case 4:
  93.         mm_update_max_long_term_frame_idx (tmp_drpm->max_long_term_frame_idx_plus1);
  94.         update_ltref_list();
  95.         break;
  96.       case 5:
  97.         mm_unmark_all_short_term_for_reference();
  98.         mm_unmark_all_long_term_for_reference();
  99.        img->last_has_mmco_5 = 1;
  100.         break;
  101.       case 6:
  102.         mm_mark_current_picture_long_term(p, tmp_drpm->long_term_frame_idx);
  103.         check_num_ref();
  104.         break;
  105.       default:
  106.         error ("invalid memory_management_control_operation in buffer", 500);
  107.     }
  108.     img->dec_ref_pic_marking_buffer = tmp_drpm->Next;
  109.     free (tmp_drpm);
  110.   }
  111.   if ( img->last_has_mmco_5 )
  112.   {
  113.     p->pic_num = p->frame_num = 0;
  114.     switch (p->structure)
  115.     {
  116.     case TOP_FIELD:
  117.       {
  118.         p->poc = p->top_poc = img->toppoc =0;
  119.         break;
  120.       }
  121.     case BOTTOM_FIELD:
  122.       {
  123.         p->poc = p->bottom_poc = img->bottompoc = 0;
  124.         break;
  125.       }
  126.     case FRAME:
  127.       {
  128.         p->top_poc    -= p->poc;
  129.         p->bottom_poc -= p->poc;
  130.         img->toppoc = p->top_poc;
  131.         img->bottompoc = p->bottom_poc;
  132.         p->poc = imin (p->top_poc, p->bottom_poc);
  133.         img->framepoc = p->poc;
  134.         break;
  135.       }
  136.     }
  137.     img->ThisPOC = p->poc;
  138.     flush_dpb();
  139.   }
  140. }
  141. /*!
  142.  ************************************************************************
  143.  * brief
  144.  *    Store a picture in DPB. This includes cheking for space in DPB and
  145.  *    flushing frames.
  146.  *    If we received a frame, we need to check for a new store, if we
  147.  *    got a field, check if it's the second field of an already allocated
  148.  *    store.
  149.  *
  150.  * param p
  151.  *    Picture to be stored
  152.  *
  153.  ************************************************************************
  154.  */
  155. void store_picture_in_dpb(StorablePicture* p)
  156. {
  157.   unsigned i;
  158.   int poc, pos;
  159.   // diagnostics
  160.   //printf ("Storing (%s) non-ref pic with frame_num #%dn", (p->type == FRAME)?"FRAME":(p->type == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num);
  161.   // if frame, check for new store,
  162.   assert (p!=NULL);
  163.   p->used_for_reference = (img->nal_reference_idc != NALU_PRIORITY_DISPOSABLE);
  164.   img->last_has_mmco_5=0;
  165.   img->last_pic_bottom_field = (img->structure == BOTTOM_FIELD);
  166.   if (img->currentPicture->idr_flag)
  167.     idr_memory_management(p);
  168.   else
  169.   {
  170.     // adaptive memory management
  171.     if (p->used_for_reference && (img->adaptive_ref_pic_buffering_flag))
  172.       adaptive_memory_management(p);
  173.   }
  174.   if ((p->structure==TOP_FIELD)||(p->structure==BOTTOM_FIELD))
  175.   {
  176.     // check for frame store with same pic_number
  177.     if (dpb.last_picture)
  178.     {
  179.       if ((int)dpb.last_picture->frame_num == p->pic_num)
  180.       {
  181.         if (((p->structure==TOP_FIELD)&&(dpb.last_picture->is_used==2))||((p->structure==BOTTOM_FIELD)&&(dpb.last_picture->is_used==1)))
  182.         {
  183.           if ((p->used_for_reference && (dpb.last_picture->is_orig_reference!=0))||
  184.               (!p->used_for_reference && (dpb.last_picture->is_orig_reference==0)))
  185.           {
  186.             insert_picture_in_dpb(dpb.last_picture, p);
  187.             update_ref_list();
  188.             update_ltref_list();
  189.             dump_dpb();
  190.             dpb.last_picture = NULL;
  191.             return;
  192.           }
  193.         }
  194.       }
  195.     }
  196.   }
  197.   // this is a frame or a field which has no stored complementary field
  198.   // sliding window, if necessary
  199.   if ((!img->currentPicture->idr_flag)&&(p->used_for_reference && (!img->adaptive_ref_pic_buffering_flag)))
  200.   {
  201.     sliding_window_memory_management(p);
  202.   }
  203.   // first try to remove unused frames
  204.   if (dpb.used_size==dpb.size)
  205.   {
  206.     remove_unused_frame_from_dpb();
  207.   }
  208.   // then output frames until one can be removed
  209.   while (dpb.used_size==dpb.size)
  210.   {
  211.     // non-reference frames may be output directly
  212.     if (!p->used_for_reference)
  213.     {
  214.       get_smallest_poc(&poc, &pos);
  215.       if ((-1==pos) || (p->poc < poc))
  216.       {
  217.         direct_output(p, p_dec);
  218.         return;
  219.       }
  220.     }
  221.     // flush a frame
  222.     output_one_frame_from_dpb();
  223.   }
  224.   // check for duplicate frame number in short term reference buffer
  225.   if ((p->used_for_reference)&&(!p->is_long_term))
  226.   {
  227.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  228.     {
  229.       if (dpb.fs_ref[i]->frame_num == p->frame_num)
  230.       {
  231.         error("duplicate frame_num im short-term reference picture buffer", 500);
  232.       }
  233.     }
  234.   }
  235.   // store at end of buffer
  236. //  printf ("store frame/field at pos %dn",dpb.used_size);
  237.   insert_picture_in_dpb(dpb.fs[dpb.used_size],p);
  238.   if (p->structure != FRAME)
  239.   {
  240.     dpb.last_picture = dpb.fs[dpb.used_size];
  241.   }
  242.   else
  243.   {
  244.     dpb.last_picture = NULL;
  245.   }
  246.   dpb.used_size++;
  247.   update_ref_list();
  248.   update_ltref_list();
  249.   check_num_ref();
  250.   dump_dpb();
  251. }
  252. /*!
  253.  ************************************************************************
  254.  * brief
  255.  *    Insert the frame picture into the if the top field has already
  256.  *    been stored for the coding decision
  257.  *
  258.  * param p
  259.  *    StorablePicture to be inserted
  260.  *
  261.  ************************************************************************
  262.  */
  263. void replace_top_pic_with_frame(StorablePicture* p)
  264. {
  265.   FrameStore* fs = NULL;
  266.   unsigned i, found;
  267.   assert (p!=NULL);
  268.   assert (p->structure==FRAME);
  269.   p->used_for_reference = (img->nal_reference_idc != NALU_PRIORITY_DISPOSABLE);
  270.   // upsample a reference picture
  271.   if (p->used_for_reference)
  272.   {
  273.     if( IS_INDEPENDENT(params) )
  274.     {
  275.       UnifiedOneForthPix_JV(0, p);
  276.       UnifiedOneForthPix_JV(1, p);
  277.       UnifiedOneForthPix_JV(2, p);
  278.     }
  279.     else
  280.     {
  281.       UnifiedOneForthPix(p);
  282.     }
  283.   }
  284.   found=0;
  285.   for (i=0;i<dpb.used_size;i++)
  286.   {
  287.     if((dpb.fs[i]->frame_num == img->frame_num)&&(dpb.fs[i]->is_used==1))
  288.     {
  289.       found=1;
  290.       fs = dpb.fs[i];
  291.       break;
  292.     }
  293.   }
  294.   if (!found)
  295.   {
  296.     // this should only happen for non-reference pictures when the dpb is full of reference pics
  297.     direct_output_paff(p, p_dec);
  298.   }
  299.   else
  300.   {
  301.     free_storable_picture(fs->top_field);
  302.     fs->top_field=NULL;
  303.     fs->frame=p;
  304.     fs->is_used = 3;
  305.     if (p->used_for_reference)
  306.     {
  307.       fs->is_reference = 3;
  308.       if (p->is_long_term)
  309.       {
  310.         fs->is_long_term = 3;
  311.       }
  312.     }
  313.     // generate field views
  314.     dpb_split_field(fs);
  315.     update_ref_list();
  316.     update_ltref_list();
  317.   }
  318. }
  319. /*!
  320.  ************************************************************************
  321.  * brief
  322.  *    Insert the picture into the DPB. A free DPB position is necessary
  323.  *    for frames, .
  324.  *
  325.  * param fs
  326.  *    FrameStore into which the picture will be inserted
  327.  * param p
  328.  *    StorablePicture to be inserted
  329.  *
  330.  ************************************************************************
  331.  */
  332. static void insert_picture_in_dpb(FrameStore* fs, StorablePicture* p)
  333. {
  334.   //  printf ("insert (%s) pic with frame_num #%d, poc %dn", (p->structure == FRAME)?"FRAME":(p->structure == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num, p->poc);
  335.   assert (p!=NULL);
  336.   assert (fs!=NULL);
  337.   // upsample a reference picture
  338.   if (p->used_for_reference)
  339.   {
  340.     if( IS_INDEPENDENT(params) )
  341.     {
  342.       UnifiedOneForthPix_JV(0, p);
  343.       UnifiedOneForthPix_JV(1, p);
  344.       UnifiedOneForthPix_JV(2, p);
  345.     }
  346.     else
  347.     {
  348.       UnifiedOneForthPix(p);
  349.     }
  350.   }
  351.   switch (p->structure)
  352.   {
  353.   case FRAME:
  354.     fs->frame = p;
  355.     fs->is_used = 3;
  356.     if (p->used_for_reference)
  357.     {
  358.       fs->is_reference = 3;
  359.       fs->is_orig_reference = 3;
  360.       if (p->is_long_term)
  361.       {
  362.         fs->is_long_term = 3;
  363.         fs->long_term_frame_idx = p->long_term_frame_idx;
  364.       }
  365.     }
  366.     // generate field views
  367.     dpb_split_field(fs);
  368.     break;
  369.   case TOP_FIELD:
  370.     fs->top_field = p;
  371.     fs->is_used |= 1;
  372.     if (p->used_for_reference)
  373.     {
  374.       fs->is_reference |= 1;
  375.       fs->is_orig_reference |= 1;
  376.       if (p->is_long_term)
  377.       {
  378.         fs->is_long_term |= 1;
  379.         fs->long_term_frame_idx = p->long_term_frame_idx;
  380.       }
  381.     }
  382.     if (fs->is_used == 3)
  383.     {
  384.       // generate frame view
  385.       dpb_combine_field(fs);
  386.     }
  387.     else
  388.     {
  389.       fs->poc = p->poc;
  390.       gen_field_ref_ids(p);
  391.     }
  392.     break;
  393.   case BOTTOM_FIELD:
  394.     fs->bottom_field = p;
  395.     fs->is_used |= 2;
  396.     if (p->used_for_reference)
  397.     {
  398.       fs->is_reference |= 2;
  399.       fs->is_orig_reference |= 2;
  400.       if (p->is_long_term)
  401.       {
  402.         fs->is_long_term |= 2;
  403.         fs->long_term_frame_idx = p->long_term_frame_idx;
  404.       }
  405.     }
  406.     if (fs->is_used == 3)
  407.     {
  408.       // generate frame view
  409.       dpb_combine_field(fs);
  410.     } else
  411.     {
  412.       fs->poc = p->poc;
  413.       gen_field_ref_ids(p);
  414.     }
  415.     break;
  416.   }
  417.   fs->frame_num = p->pic_num;
  418.   fs->is_output = p->is_output;
  419. }
  420. /*!
  421.  ************************************************************************
  422.  * brief
  423.  *    Check if one of the frames/fields in frame store is used for reference
  424.  ************************************************************************
  425.  */
  426. static int is_used_for_reference(FrameStore* fs)
  427. {
  428.   if (fs->is_reference)
  429.   {
  430.     return 1;
  431.   }
  432.   if (fs->is_used == 3) // frame
  433.   {
  434.     if (fs->frame->used_for_reference)
  435.     {
  436.       return 1;
  437.     }
  438.   }
  439.   if (fs->is_used & 1) // top field
  440.   {
  441.     if (fs->top_field)
  442.     {
  443.       if (fs->top_field->used_for_reference)
  444.       {
  445.         return 1;
  446.       }
  447.     }
  448.   }
  449.   if (fs->is_used & 2) // bottom field
  450.   {
  451.     if (fs->bottom_field)
  452.     {
  453.       if (fs->bottom_field->used_for_reference)
  454.       {
  455.         return 1;
  456.       }
  457.     }
  458.   }
  459.   return 0;
  460. }
  461. /*!
  462.  ************************************************************************
  463.  * brief
  464.  *    Check if one of the frames/fields in frame store is used for short-term reference
  465.  ************************************************************************
  466.  */
  467. static int is_short_term_reference(FrameStore* fs)
  468. {
  469.   if (fs->is_used==3) // frame
  470.   {
  471.     if ((fs->frame->used_for_reference)&&(!fs->frame->is_long_term))
  472.     {
  473.       return 1;
  474.     }
  475.   }
  476.   if (fs->is_used & 1) // top field
  477.   {
  478.     if (fs->top_field)
  479.     {
  480.       if ((fs->top_field->used_for_reference)&&(!fs->top_field->is_long_term))
  481.       {
  482.         return 1;
  483.       }
  484.     }
  485.   }
  486.   if (fs->is_used & 2) // bottom field
  487.   {
  488.     if (fs->bottom_field)
  489.     {
  490.       if ((fs->bottom_field->used_for_reference)&&(!fs->bottom_field->is_long_term))
  491.       {
  492.         return 1;
  493.       }
  494.     }
  495.   }
  496.   return 0;
  497. }
  498. /*!
  499.  ************************************************************************
  500.  * brief
  501.  *    Check if one of the frames/fields in frame store is used for short-term reference
  502.  ************************************************************************
  503.  */
  504. static int is_long_term_reference(FrameStore* fs)
  505. {
  506.   if (fs->is_used==3) // frame
  507.   {
  508.     if ((fs->frame->used_for_reference)&&(fs->frame->is_long_term))
  509.     {
  510.       return 1;
  511.     }
  512.   }
  513.   if (fs->is_used & 1) // top field
  514.   {
  515.     if (fs->top_field)
  516.     {
  517.       if ((fs->top_field->used_for_reference)&&(fs->top_field->is_long_term))
  518.       {
  519.         return 1;
  520.       }
  521.     }
  522.   }
  523.   if (fs->is_used & 2) // bottom field
  524.   {
  525.     if (fs->bottom_field)
  526.     {
  527.       if ((fs->bottom_field->used_for_reference)&&(fs->bottom_field->is_long_term))
  528.       {
  529.         return 1;
  530.       }
  531.     }
  532.   }
  533.   return 0;
  534. }
  535. /*!
  536.  ************************************************************************
  537.  * brief
  538.  *    remove one frame from DPB
  539.  ************************************************************************
  540.  */
  541. static void remove_frame_from_dpb(int pos)
  542. {
  543.   FrameStore* fs = dpb.fs[pos];
  544.   FrameStore* tmp;
  545.   unsigned i;
  546. //  printf ("remove frame with frame_num #%dn", fs->frame_num);
  547.   switch (fs->is_used)
  548.   {
  549.   case 3:
  550.     free_storable_picture(fs->frame);
  551.     free_storable_picture(fs->top_field);
  552.     free_storable_picture(fs->bottom_field);
  553.     fs->frame=NULL;
  554.     fs->top_field=NULL;
  555.     fs->bottom_field=NULL;
  556.     break;
  557.   case 2:
  558.     free_storable_picture(fs->bottom_field);
  559.     fs->bottom_field=NULL;
  560.     break;
  561.   case 1:
  562.     free_storable_picture(fs->top_field);
  563.     fs->top_field=NULL;
  564.     break;
  565.   case 0:
  566.     break;
  567.   default:
  568.     error("invalid frame store type",500);
  569.   }
  570.   fs->is_used = 0;
  571.   fs->is_long_term = 0;
  572.   fs->is_reference = 0;
  573.   fs->is_orig_reference = 0;
  574.   // move empty framestore to end of buffer
  575.   tmp = dpb.fs[pos];
  576.   for (i=pos; i<dpb.used_size-1;i++)
  577.   {
  578.     dpb.fs[i] = dpb.fs[i+1];
  579.   }
  580.   dpb.fs[dpb.used_size-1] = tmp;
  581.   dpb.used_size--;
  582. }
  583. /*!
  584.  ************************************************************************
  585.  * brief
  586.  *    find smallest POC in the DPB.
  587.  ************************************************************************
  588.  */
  589. static void get_smallest_poc(int *poc,int * pos)
  590. {
  591.   unsigned i;
  592.   if (dpb.used_size<1)
  593.   {
  594.     error("Cannot determine smallest POC, DPB empty.",150);
  595.   }
  596.   *pos=-1;
  597.   *poc = INT_MAX;
  598.   for (i=0; i<dpb.used_size; i++)
  599.   {
  600.     if ((*poc>dpb.fs[i]->poc)&&(!dpb.fs[i]->is_output))
  601.     {
  602.       *poc = dpb.fs[i]->poc;
  603.       *pos=i;
  604.     }
  605.   }
  606. }
  607. /*!
  608.  ************************************************************************
  609.  * brief
  610.  *    Remove a picture from DPB which is no longer needed.
  611.  ************************************************************************
  612.  */
  613. static int remove_unused_frame_from_dpb(void)
  614. {
  615.   unsigned i;
  616.   // check for frames that were already output and no longer used for reference
  617.   for (i=0; i<dpb.used_size; i++)
  618.   {
  619.     if (dpb.fs[i]->is_output && (!is_used_for_reference(dpb.fs[i])))
  620.     {
  621.       remove_frame_from_dpb(i);
  622.       return 1;
  623.     }
  624.   }
  625.   return 0;
  626. }
  627. /*!
  628.  ************************************************************************
  629.  * brief
  630.  *    Output one picture stored in the DPB.
  631.  ************************************************************************
  632.  */
  633. static void output_one_frame_from_dpb(void)
  634. {
  635.   int poc, pos;
  636.   //diagnostics
  637.   if (dpb.used_size<1)
  638.   {
  639.     error("Cannot output frame, DPB empty.",150);
  640.   }
  641.   // find smallest POC
  642.   get_smallest_poc(&poc, &pos);
  643.   if(pos==-1)
  644.   {
  645.     error("no frames for output available", 150);
  646.   }
  647.   // call the output function
  648. //  printf ("output frame with frame_num #%d, poc %d (dpb. dpb.size=%d, dpb.used_size=%d)n", dpb.fs[pos]->frame_num, dpb.fs[pos]->frame->poc, dpb.size, dpb.used_size);
  649.   write_stored_frame(dpb.fs[pos], p_dec);
  650.   // if redundant picture in use, output POC may be not in ascending order
  651.   if(params->redundant_pic_flag == 0)
  652.   {
  653.     if (dpb.last_output_poc >= poc)
  654.     {
  655.       error ("output POC must be in ascending order", 150);
  656.     }
  657.   }
  658.   dpb.last_output_poc = poc;
  659.   // free frame store and move empty store to end of buffer
  660.   if (!is_used_for_reference(dpb.fs[pos]))
  661.   {
  662.     remove_frame_from_dpb(pos);
  663.   }
  664. }
  665. /*!
  666.  ************************************************************************
  667.  * brief
  668.  *    All stored picture are output. Should be called to empty the buffer
  669.  ************************************************************************
  670.  */
  671. void flush_dpb(void)
  672. {
  673.   unsigned i;
  674.   //diagnostics
  675. //  printf("Flush remaining frames from dpb. dpb.size=%d, dpb.used_size=%dn",dpb.size,dpb.used_size);
  676.   // mark all frames unused
  677.   for (i=0; i<dpb.used_size; i++)
  678.   {
  679.     unmark_for_reference (dpb.fs[i]);
  680.   }
  681.   while (remove_unused_frame_from_dpb()) ;
  682.   // output frames in POC order
  683.   while (dpb.used_size)
  684.   {
  685.     output_one_frame_from_dpb();
  686.   }
  687.   dpb.last_output_poc = INT_MIN;
  688. }
  689. void gen_field_ref_ids(StorablePicture *p)
  690. {
  691.   int i,j, dummylist0, dummylist1;
  692.    //! Generate Frame parameters from field information.
  693.   for (i=0 ; i<p->size_x/4 ; i++)
  694.   {
  695.     for (j=0 ; j<p->size_y/4 ; j++)
  696.     {
  697.         dummylist0= p->motion.ref_idx[LIST_0][j][i];
  698.         dummylist1= p->motion.ref_idx[LIST_1][j][i];
  699.         //! association with id already known for fields.
  700.         p->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? p->ref_pic_num[LIST_0][dummylist0] : 0;
  701.         p->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? p->ref_pic_num[LIST_1][dummylist1] : 0;
  702.         p->motion.field_frame[j][i]=1;
  703.     }
  704.   }
  705. }
  706. /*!
  707.  ************************************************************************
  708.  * brief
  709.  *    Extract top field from a frame
  710.  ************************************************************************
  711.  */
  712. void dpb_split_field(FrameStore *fs)
  713. {
  714.   int i, j, ii, jj, jj4;
  715.   int idiv,jdiv;
  716.   int currentmb;
  717.   int dummylist0,dummylist1;
  718.   int twosz16 = 2*(fs->frame->size_x>>4);
  719.   fs->poc = fs->frame->poc;
  720.   if (!active_sps->frame_mbs_only_flag)
  721.   {
  722.     fs->top_field    = alloc_storable_picture(TOP_FIELD,    fs->frame->size_x, fs->frame->size_y/2, fs->frame->size_x_cr, fs->frame->size_y_cr/2);
  723.     fs->bottom_field = alloc_storable_picture(BOTTOM_FIELD, fs->frame->size_x, fs->frame->size_y/2, fs->frame->size_x_cr, fs->frame->size_y_cr/2);
  724.     for (i=0; i<fs->frame->size_y/2; i++)
  725.     {
  726.       memcpy(fs->top_field->imgY[i], fs->frame->imgY[i*2], fs->frame->size_x*sizeof(imgpel));
  727.     }
  728.     for (i=0; i<fs->frame->size_y_cr/2; i++)
  729.     {
  730.       memcpy(fs->top_field->imgUV[0][i], fs->frame->imgUV[0][i*2], fs->frame->size_x_cr*sizeof(imgpel));
  731.       memcpy(fs->top_field->imgUV[1][i], fs->frame->imgUV[1][i*2], fs->frame->size_x_cr*sizeof(imgpel));
  732.     }
  733.     for (i=0; i<fs->frame->size_y/2; i++)
  734.     {
  735.       memcpy(fs->bottom_field->imgY[i], fs->frame->imgY[i*2 + 1], fs->frame->size_x*sizeof(imgpel));
  736.     }
  737.     for (i=0; i<fs->frame->size_y_cr/2; i++)
  738.     {
  739.       memcpy(fs->bottom_field->imgUV[0][i], fs->frame->imgUV[0][i*2 + 1], fs->frame->size_x_cr*sizeof(imgpel));
  740.       memcpy(fs->bottom_field->imgUV[1][i], fs->frame->imgUV[1][i*2 + 1], fs->frame->size_x_cr*sizeof(imgpel));
  741.     }
  742.     if( IS_INDEPENDENT(params) )
  743.     {
  744.       UnifiedOneForthPix_JV(0, fs->top_field);
  745.       UnifiedOneForthPix_JV(1, fs->top_field);
  746.       UnifiedOneForthPix_JV(2, fs->top_field);
  747.       UnifiedOneForthPix_JV(0, fs->bottom_field);
  748.       UnifiedOneForthPix_JV(1, fs->bottom_field);
  749.       UnifiedOneForthPix_JV(2, fs->bottom_field);
  750.     }
  751.     else
  752.     {
  753.       UnifiedOneForthPix(fs->top_field);
  754.       UnifiedOneForthPix(fs->bottom_field);
  755.     }
  756.     fs->top_field->poc = fs->frame->top_poc;
  757.     fs->bottom_field->poc =  fs->frame->bottom_poc;
  758.     fs->top_field->frame_poc =  fs->frame->frame_poc;
  759.     fs->top_field->bottom_poc =fs->bottom_field->bottom_poc =  fs->frame->bottom_poc;
  760.     fs->top_field->top_poc =fs->bottom_field->top_poc =  fs->frame->top_poc;
  761.     fs->bottom_field->frame_poc =  fs->frame->frame_poc;
  762.     fs->top_field->used_for_reference = fs->bottom_field->used_for_reference
  763.                                       = fs->frame->used_for_reference;
  764.     fs->top_field->is_long_term = fs->bottom_field->is_long_term
  765.                                 = fs->frame->is_long_term;
  766.     fs->long_term_frame_idx = fs->top_field->long_term_frame_idx
  767.                             = fs->bottom_field->long_term_frame_idx
  768.                             = fs->frame->long_term_frame_idx;
  769.     fs->top_field->coded_frame = fs->bottom_field->coded_frame = 1;
  770.     fs->top_field->MbaffFrameFlag = fs->bottom_field->MbaffFrameFlag
  771.                                   = fs->frame->MbaffFrameFlag;
  772.     fs->frame->top_field    = fs->top_field;
  773.     fs->frame->bottom_field = fs->bottom_field;
  774.     fs->top_field->bottom_field = fs->bottom_field;
  775.     fs->top_field->frame        = fs->frame;
  776.     fs->bottom_field->top_field = fs->top_field;
  777.     fs->bottom_field->frame     = fs->frame;
  778.     fs->top_field->chroma_format_idc = fs->bottom_field->chroma_format_idc = fs->frame->chroma_format_idc;
  779.     //store reference picture index
  780.     memcpy(fs->top_field->ref_pic_num[LIST_1]   , fs->frame->ref_pic_num[2 + LIST_1], 2*listXsize[LIST_1] * sizeof(int64));
  781.     memcpy(fs->bottom_field->ref_pic_num[LIST_1], fs->frame->ref_pic_num[4 + LIST_1], 2*listXsize[LIST_1] * sizeof(int64));
  782.     memcpy(fs->top_field->ref_pic_num[LIST_0]   , fs->frame->ref_pic_num[2 + LIST_0], 2*listXsize[LIST_0] * sizeof(int64));
  783.     memcpy(fs->bottom_field->ref_pic_num[LIST_0], fs->frame->ref_pic_num[4 + LIST_0], 2*listXsize[LIST_0] * sizeof(int64));
  784.   }
  785.   else
  786.   {
  787.     fs->top_field=NULL;
  788.     fs->bottom_field=NULL;
  789.     fs->frame->top_field=NULL;
  790.     fs->frame->bottom_field=NULL;
  791.   }
  792.   for (j=0 ; j<fs->frame->size_y/4 ; j++)
  793.   {
  794.     jdiv=j/4;
  795.     for (i=0 ; i<fs->frame->size_x/4 ; i++)
  796.     {
  797.       idiv=i/4;
  798.       currentmb = twosz16*(jdiv/2)+ (idiv)*2 + (jdiv%2);
  799.       if (fs->frame->MbaffFrameFlag  && fs->frame->motion.mb_field[currentmb])
  800.       {
  801.         int list_offset = currentmb%2? 4: 2;
  802.         dummylist0 = fs->frame->motion.ref_idx[LIST_0][j][i];
  803.         dummylist1 = fs->frame->motion.ref_idx[LIST_1][j][i];
  804.         //! association with id already known for fields.
  805.         fs->frame->motion.ref_id[LIST_0 + list_offset][j][i] = (dummylist0>=0)? fs->frame->ref_pic_num[LIST_0 + list_offset][dummylist0] : 0;
  806.         fs->frame->motion.ref_id[LIST_1 + list_offset][j][i] = (dummylist1>=0)? fs->frame->ref_pic_num[LIST_1 + list_offset][dummylist1] : 0;
  807.         //! need to make association with frames
  808.         fs->frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->frm_ref_pic_num[LIST_0 + list_offset][dummylist0] : 0;
  809.         fs->frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->frm_ref_pic_num[LIST_1 + list_offset][dummylist1] : 0;
  810.       }
  811.       else
  812.       {
  813.         dummylist0 = fs->frame->motion.ref_idx[LIST_0][j][i];
  814.         dummylist1 = fs->frame->motion.ref_idx[LIST_1][j][i];
  815.         fs->frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->ref_pic_num[LIST_0][dummylist0] : -1;
  816.         fs->frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->ref_pic_num[LIST_1][dummylist1] : -1;
  817.       }
  818.     }
  819.   }
  820.   if (!active_sps->frame_mbs_only_flag && fs->frame->MbaffFrameFlag)
  821.   {
  822.     for (j=0 ; j<fs->frame->size_y/8; j++)
  823.     {
  824.       jj = (j/4)*8 + j%4;
  825.       jj4 = jj + 4;
  826.       jdiv=j/2;
  827.       for (i=0 ; i<fs->frame->size_x/4 ; i++)
  828.       {
  829.         idiv=i/4;
  830.         currentmb = twosz16*(jdiv/2)+ (idiv)*2 + (jdiv%2);
  831.         // Assign field mvs attached to MB-Frame buffer to the proper buffer
  832.         if (fs->frame->motion.mb_field[currentmb])
  833.         {
  834.           fs->bottom_field->motion.field_frame[j][i] = fs->top_field->motion.field_frame[j][i]=1;
  835.           fs->frame->motion.field_frame[2*j][i] = fs->frame->motion.field_frame[2*j+1][i]=1;
  836.           fs->bottom_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj4][i][0];
  837.           fs->bottom_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj4][i][1];
  838.           fs->bottom_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj4][i][0];
  839.           fs->bottom_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj4][i][1];
  840.           fs->bottom_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj4][i];
  841.           fs->bottom_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj4][i];
  842.           fs->bottom_field->motion.ref_id[LIST_0][j][i] = fs->frame->motion.ref_id[LIST_0+4][jj4][i];
  843.           fs->bottom_field->motion.ref_id[LIST_1][j][i] = fs->frame->motion.ref_id[LIST_1+4][jj4][i];
  844.           fs->top_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj][i][0];
  845.           fs->top_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj][i][1];
  846.           fs->top_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj][i][0];
  847.           fs->top_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj][i][1];
  848.           fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj][i];
  849.           fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj][i];
  850.           fs->top_field->motion.ref_id[LIST_0][j][i] = fs->frame->motion.ref_id[LIST_0+2][jj][i];
  851.           fs->top_field->motion.ref_id[LIST_1][j][i] = fs->frame->motion.ref_id[LIST_1+2][jj][i];
  852.         }
  853.       }
  854.     }
  855.   }
  856.   //! Generate field MVs from Frame MVs
  857.   if (!active_sps->frame_mbs_only_flag)
  858.   {
  859.     for (j=0 ; j<fs->frame->size_y/8 ; j++)
  860.     {
  861.       jj = 2* RSD(j);
  862.       jdiv = j/2;
  863.       for (i=0 ; i<fs->frame->size_x/4 ; i++)
  864.       {
  865.         ii = RSD(i);
  866.         idiv = i/4;
  867.         currentmb = twosz16*(jdiv/2)+ (idiv)*2 + (jdiv%2);
  868.         if (!fs->frame->MbaffFrameFlag  || !fs->frame->motion.mb_field[currentmb])
  869.         {
  870.           fs->frame->motion.field_frame[2*j+1][i] = fs->frame->motion.field_frame[2*j][i]=0;
  871.           fs->top_field->motion.field_frame[j][i] = fs->bottom_field->motion.field_frame[j][i] = 0;
  872.           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];
  873.           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];
  874.           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];
  875.           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];
  876.           // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
  877.           if (fs->frame->motion.ref_idx[LIST_0][jj][ii] == -1)
  878.             fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->bottom_field->motion.ref_idx[LIST_0][j][i] = - 1;
  879.           else
  880.           {
  881.             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];
  882.             fs->top_field   ->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->top_ref_pic_num[LIST_0][dummylist0] : 0;
  883.             fs->bottom_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->bottom_ref_pic_num[LIST_0][dummylist0] : 0;
  884.           }
  885.           if (fs->frame->motion.ref_idx[LIST_1][jj][ii] == -1)
  886.             fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->bottom_field->motion.ref_idx[LIST_1][j][i] = - 1;
  887.           else
  888.           {
  889.             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];
  890.             fs->top_field   ->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->top_ref_pic_num[LIST_1][dummylist1] : 0;
  891.             fs->bottom_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->bottom_ref_pic_num[LIST_1][dummylist1] : 0;
  892.           }
  893.         }
  894.         else
  895.         {
  896.           fs->frame->motion.field_frame[2*j+1][i] = fs->frame->motion.field_frame[2*j][i]= fs->frame->motion.mb_field[currentmb];
  897.         }
  898.       }
  899.     }
  900.   }
  901.   else
  902.   {
  903.     memset( &(fs->frame->motion.field_frame[0][0]), 0, fs->frame->size_y * (fs->frame->size_x >>4) * sizeof(byte));
  904.   }
  905. }
  906. /*!
  907.  ************************************************************************
  908.  * brief
  909.  *    Generate a frame from top and bottom fields,
  910.  *    YUV components and display information only
  911.  ************************************************************************
  912.  */
  913. void dpb_combine_field_yuv(FrameStore *fs)
  914. {
  915.   int i;
  916.   fs->frame = alloc_storable_picture(FRAME, fs->top_field->size_x, fs->top_field->size_y*2, fs->top_field->size_x_cr, fs->top_field->size_y_cr*2);
  917.   for (i=0; i<fs->top_field->size_y; i++)
  918.   {
  919.     memcpy(fs->frame->imgY[i*2],     fs->top_field->imgY[i]   , fs->top_field->size_x*sizeof(imgpel));     // top field
  920.     memcpy(fs->frame->imgY[i*2 + 1], fs->bottom_field->imgY[i], fs->bottom_field->size_x*sizeof(imgpel)); // bottom field
  921.   }
  922.   for (i=0; i<fs->top_field->size_y_cr; i++)
  923.   {
  924.     memcpy(fs->frame->imgUV[0][i*2],     fs->top_field->imgUV[0][i],    fs->top_field->size_x_cr*sizeof(imgpel));
  925.     memcpy(fs->frame->imgUV[0][i*2 + 1], fs->bottom_field->imgUV[0][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
  926.     memcpy(fs->frame->imgUV[1][i*2],     fs->top_field->imgUV[1][i],    fs->top_field->size_x_cr*sizeof(imgpel));
  927.     memcpy(fs->frame->imgUV[1][i*2 + 1], fs->bottom_field->imgUV[1][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
  928.   }
  929.   fs->poc=fs->frame->poc =fs->frame->frame_poc = imin (fs->top_field->poc, fs->bottom_field->poc);
  930.   fs->bottom_field->frame_poc=fs->top_field->frame_poc=fs->frame->poc;
  931.   fs->bottom_field->top_poc=fs->frame->top_poc=fs->top_field->poc;
  932.   fs->top_field->bottom_poc=fs->frame->bottom_poc=fs->bottom_field->poc;
  933.   fs->frame->used_for_reference = (fs->top_field->used_for_reference && fs->bottom_field->used_for_reference );
  934.   fs->frame->is_long_term = (fs->top_field->is_long_term && fs->bottom_field->is_long_term );
  935.   if (fs->frame->is_long_term)
  936.     fs->frame->long_term_frame_idx = fs->long_term_frame_idx;
  937.   fs->frame->top_field    = fs->top_field;
  938.   fs->frame->bottom_field = fs->bottom_field;
  939.   fs->frame->coded_frame = 0;
  940.   fs->frame->chroma_format_idc = fs->top_field->chroma_format_idc;
  941.   fs->frame->frame_cropping_flag = fs->top_field->frame_cropping_flag;
  942.   if (fs->frame->frame_cropping_flag)
  943.   {
  944.     fs->frame->frame_cropping_rect_top_offset = fs->top_field->frame_cropping_rect_top_offset;
  945.     fs->frame->frame_cropping_rect_bottom_offset = fs->top_field->frame_cropping_rect_bottom_offset;
  946.     fs->frame->frame_cropping_rect_left_offset = fs->top_field->frame_cropping_rect_left_offset;
  947.     fs->frame->frame_cropping_rect_right_offset = fs->top_field->frame_cropping_rect_right_offset;
  948.   }
  949.   fs->top_field->frame = fs->bottom_field->frame = fs->frame;
  950. }
  951. /*!
  952.  ************************************************************************
  953.  * brief
  954.  *    Generate a frame from top and bottom fields
  955.  ************************************************************************
  956.  */
  957. void dpb_combine_field(FrameStore *fs)
  958. {
  959.   int i,j, jj, jj4;
  960.   int dummylist0, dummylist1;
  961.   dpb_combine_field_yuv(fs);
  962.   if( IS_INDEPENDENT(params) )
  963.   {
  964.     UnifiedOneForthPix_JV(0, fs->frame);
  965.     UnifiedOneForthPix_JV(1, fs->frame);
  966.     UnifiedOneForthPix_JV(2, fs->frame);
  967.   }
  968.   else
  969.   {
  970.     UnifiedOneForthPix(fs->frame);
  971.   }
  972.   //combine field for frame
  973.   for (i=0;i<(listXsize[LIST_1]+1)/2;i++)
  974.   {
  975.     fs->frame->ref_pic_num[LIST_1][i]= i64min ((fs->top_field->ref_pic_num[LIST_1][2*i]/2)*2, (fs->bottom_field->ref_pic_num[LIST_1][2*i]/2)*2);
  976.   }
  977.   for (i=0;i<(listXsize[LIST_0]+1)/2;i++)
  978.   {
  979.     fs->frame->ref_pic_num[LIST_0][i]= i64min ((fs->top_field->ref_pic_num[LIST_0][2*i]/2)*2, (fs->bottom_field->ref_pic_num[LIST_0][2*i]/2)*2);
  980.   }
  981.    //! Use inference flag to remap mvs/references
  982.   //! Generate Frame parameters from field information.
  983.   for (j=0 ; j<fs->top_field->size_y/4 ; j++)
  984.   {
  985.     jj = 8*(j/4) + (j%4);
  986.     jj4 = jj + 4;
  987.     for (i=0 ; i<fs->top_field->size_x/4 ; i++)
  988.     {
  989.       fs->frame->motion.field_frame[jj][i]= fs->frame->motion.field_frame[jj4][i]=1;
  990.       fs->frame->motion.mv[LIST_0][jj][i][0] = fs->top_field->motion.mv[LIST_0][j][i][0];
  991.       fs->frame->motion.mv[LIST_0][jj][i][1] = fs->top_field->motion.mv[LIST_0][j][i][1] ;
  992.       fs->frame->motion.mv[LIST_1][jj][i][0] = fs->top_field->motion.mv[LIST_1][j][i][0];
  993.       fs->frame->motion.mv[LIST_1][jj][i][1] = fs->top_field->motion.mv[LIST_1][j][i][1] ;
  994.       dummylist0=fs->frame->motion.ref_idx[LIST_0][jj][i]  = fs->top_field->motion.ref_idx[LIST_0][j][i];
  995.       dummylist1=fs->frame->motion.ref_idx[LIST_1][jj][i]  = fs->top_field->motion.ref_idx[LIST_1][j][i];
  996.       //! association with id already known for fields.
  997.       fs->top_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->top_field->ref_pic_num[LIST_0][dummylist0] : 0;
  998.       fs->top_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->top_field->ref_pic_num[LIST_1][dummylist1] : 0;
  999.       //! need to make association with frames
  1000.       fs->frame->motion.ref_id[LIST_0][jj][i] = (dummylist0>=0)? fs->top_field->frm_ref_pic_num[LIST_0][dummylist0] : 0;
  1001.       fs->frame->motion.ref_id[LIST_1][jj][i] = (dummylist1>=0)? fs->top_field->frm_ref_pic_num[LIST_1][dummylist1] : 0;
  1002.       fs->frame->motion.mv[LIST_0][jj4][i][0] = fs->bottom_field->motion.mv[LIST_0][j][i][0];
  1003.       fs->frame->motion.mv[LIST_0][jj4][i][1] = fs->bottom_field->motion.mv[LIST_0][j][i][1] ;
  1004.       fs->frame->motion.mv[LIST_1][jj4][i][0] = fs->bottom_field->motion.mv[LIST_1][j][i][0];
  1005.       fs->frame->motion.mv[LIST_1][jj4][i][1] = fs->bottom_field->motion.mv[LIST_1][j][i][1] ;
  1006.       dummylist0=fs->frame->motion.ref_idx[LIST_0][jj4][i]  = fs->bottom_field->motion.ref_idx[LIST_0][j][i];
  1007.       dummylist1=fs->frame->motion.ref_idx[LIST_1][jj4][i]  = fs->bottom_field->motion.ref_idx[LIST_1][j][i];
  1008.       fs->bottom_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->bottom_field->ref_pic_num[LIST_0][dummylist0] : 0;
  1009.       fs->bottom_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->bottom_field->ref_pic_num[LIST_1][dummylist1] : 0;
  1010.       //! need to make association with frames
  1011.       fs->frame->motion.ref_id[LIST_0][jj4][i] = (dummylist0>=0)? fs->bottom_field->frm_ref_pic_num[LIST_0][dummylist0] : -1;
  1012.       fs->frame->motion.ref_id[LIST_1][jj4][i] = (dummylist1>=0)? fs->bottom_field->frm_ref_pic_num[LIST_1][dummylist1] : -1;
  1013.       fs->top_field->motion.field_frame[j][i]=1;
  1014.       fs->bottom_field->motion.field_frame[j][i]=1;
  1015.     }
  1016.   }
  1017. }
  1018. /*!
  1019.  ************************************************************************
  1020.  * brief
  1021.  *    Allocate memory for buffering of reference picture reordering commands
  1022.  ************************************************************************
  1023.  */
  1024. void alloc_ref_pic_list_reordering_buffer(Slice *currSlice)
  1025. {
  1026.   int size = img->num_ref_idx_l0_active+1;
  1027.   if (img->type!=I_SLICE && img->type!=SI_SLICE)
  1028.   {
  1029.     if ((currSlice->reordering_of_pic_nums_idc_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: remapping_of_pic_nums_idc_l0");
  1030.     if ((currSlice->abs_diff_pic_num_minus1_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l0");
  1031.     if ((currSlice->long_term_pic_idx_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l0");
  1032.   }
  1033.   else
  1034.   {
  1035.     currSlice->reordering_of_pic_nums_idc_l0 = NULL;
  1036.     currSlice->abs_diff_pic_num_minus1_l0 = NULL;
  1037.     currSlice->long_term_pic_idx_l0 = NULL;
  1038.   }
  1039.   size = img->num_ref_idx_l1_active+1;
  1040.   if (img->type==B_SLICE)
  1041.   {
  1042.     if ((currSlice->reordering_of_pic_nums_idc_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: remapping_of_pic_nums_idc_l1");
  1043.     if ((currSlice->abs_diff_pic_num_minus1_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l1");
  1044.     if ((currSlice->long_term_pic_idx_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l1");
  1045.   }
  1046.   else
  1047.   {
  1048.     currSlice->reordering_of_pic_nums_idc_l1 = NULL;
  1049.     currSlice->abs_diff_pic_num_minus1_l1 = NULL;
  1050.     currSlice->long_term_pic_idx_l1 = NULL;
  1051.   }
  1052. }
  1053. /*!
  1054.  ************************************************************************
  1055.  * brief
  1056.  *    Free memory for buffering of reference picture reordering commands
  1057.  ************************************************************************
  1058.  */
  1059. void free_ref_pic_list_reordering_buffer(Slice *currSlice)
  1060. {
  1061.   if (currSlice->reordering_of_pic_nums_idc_l0)
  1062.     free(currSlice->reordering_of_pic_nums_idc_l0);
  1063.   if (currSlice->abs_diff_pic_num_minus1_l0)
  1064.     free(currSlice->abs_diff_pic_num_minus1_l0);
  1065.   if (currSlice->long_term_pic_idx_l0)
  1066.     free(currSlice->long_term_pic_idx_l0);
  1067.   currSlice->reordering_of_pic_nums_idc_l0 = NULL;
  1068.   currSlice->abs_diff_pic_num_minus1_l0 = NULL;
  1069.   currSlice->long_term_pic_idx_l0 = NULL;
  1070.   if (currSlice->reordering_of_pic_nums_idc_l1)
  1071.     free(currSlice->reordering_of_pic_nums_idc_l1);
  1072.   if (currSlice->abs_diff_pic_num_minus1_l1)
  1073.     free(currSlice->abs_diff_pic_num_minus1_l1);
  1074.   if (currSlice->long_term_pic_idx_l1)
  1075.     free(currSlice->long_term_pic_idx_l1);
  1076.   currSlice->reordering_of_pic_nums_idc_l1 = NULL;
  1077.   currSlice->abs_diff_pic_num_minus1_l1 = NULL;
  1078.   currSlice->long_term_pic_idx_l1 = NULL;
  1079. }
  1080. /*!
  1081.  ************************************************************************
  1082.  * brief
  1083.  *      Tian Dong
  1084.  *          June 13, 2002, Modifed on July 30, 2003
  1085.  *
  1086.  *      If a gap in frame_num is found, try to fill the gap
  1087.  * param img
  1088.  *
  1089.  ************************************************************************
  1090.  */
  1091. void fill_frame_num_gap(ImageParameters *img)
  1092. {
  1093.   int CurrFrameNum;
  1094.   int UnusedShortTermFrameNum;
  1095.   StorablePicture *picture = NULL;
  1096.   int nal_ref_idc_bak;
  1097. //  printf("A gap in frame number is found, try to fill it.n");
  1098.   nal_ref_idc_bak = img->nal_reference_idc;
  1099.   img->nal_reference_idc = NALU_PRIORITY_LOW;
  1100.   UnusedShortTermFrameNum = (img->pre_frame_num + 1) % max_frame_num;
  1101.   CurrFrameNum = img->frame_num;
  1102.   while (CurrFrameNum != UnusedShortTermFrameNum)
  1103.   {
  1104.     picture = alloc_storable_picture (FRAME, img->width, img->height, img->width_cr, img->height_cr);
  1105.     picture->coded_frame = 1;
  1106.     picture->pic_num = UnusedShortTermFrameNum;
  1107.     picture->non_existing = 1;
  1108.     picture->is_output = 1;
  1109.     img->adaptive_ref_pic_buffering_flag = 0;
  1110.     store_picture_in_dpb(picture);
  1111.     picture=NULL;
  1112.     UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % max_frame_num;
  1113.   }
  1114.   img->nal_reference_idc = nal_ref_idc_bak;
  1115. }
  1116. /*!
  1117.  ************************************************************************
  1118.  * brief
  1119.  *    Allocate motion parameter memory for colocated structure
  1120.  *
  1121.  ************************************************************************
  1122.  */
  1123. void alloc_motion_params(MotionParams *ftype, int size_y, int size_x)
  1124. {
  1125.   get_mem3Dint64 (&(ftype->ref_pic_id), 2, size_y, size_x);
  1126.   get_mem4Dshort (&(ftype->mv)        , 2, size_y, size_x, 2);
  1127.   get_mem3D      ((byte****)(&(ftype->ref_idx)) , 2, size_y, size_x);
  1128.   get_mem2D      (&(ftype->moving_block) , size_y, size_x);
  1129. }
  1130. /*!
  1131.  ************************************************************************
  1132.  * brief
  1133.  *    Allocate co-located memory
  1134.  *
  1135.  * param size_x
  1136.  *    horizontal luma size
  1137.  * param size_y
  1138.  *    vertical luma size
  1139.  * param mb_adaptive_frame_field_flag
  1140.  *    flag that indicates macroblock adaptive frame/field coding
  1141.  *
  1142.  * return
  1143.  *    the allocated StorablePicture structure
  1144.  ************************************************************************
  1145.  */
  1146. ColocatedParams* alloc_colocated(int size_x, int size_y, int mb_adaptive_frame_field_flag)
  1147. {
  1148.   ColocatedParams *s;
  1149.   s = calloc(1, sizeof(ColocatedParams));
  1150.   if (NULL == s)
  1151.     no_mem_exit("alloc_colocated: s");
  1152.   s->size_x = size_x;
  1153.   s->size_y = size_y;
  1154.   alloc_motion_params(&s->frame, size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
  1155.   if (mb_adaptive_frame_field_flag)
  1156.   {
  1157.     alloc_motion_params(&s->top   , size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
  1158.     alloc_motion_params(&s->bottom, size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
  1159.   }
  1160.   s->mb_adaptive_frame_field_flag  = mb_adaptive_frame_field_flag;
  1161.   return s;
  1162. }
  1163. /*!
  1164.  ************************************************************************
  1165.  * brief
  1166.  *    Free motion parameter memory for colocated structure
  1167.  *
  1168.  ************************************************************************
  1169.  */
  1170. void free_motion_params(MotionParams *ftype)
  1171. {
  1172.   free_mem3Dint64 (ftype->ref_pic_id);
  1173.   free_mem3D      ((byte***)ftype->ref_idx);
  1174.   free_mem4Dshort (ftype->mv);
  1175.   if (ftype->moving_block)
  1176.   {
  1177.     free_mem2D (ftype->moving_block);
  1178.     ftype->moving_block=NULL;
  1179.   }
  1180. }
  1181. /*!
  1182.  ************************************************************************
  1183.  * brief
  1184.  *    Free co-located memory.
  1185.  *
  1186.  * param p
  1187.  *    Picture to be freed
  1188.  *
  1189.  ************************************************************************
  1190.  */
  1191. void free_colocated(ColocatedParams* p)
  1192. {
  1193.   if (p)
  1194.   {
  1195.     free_motion_params(&p->frame);
  1196.     if (p->mb_adaptive_frame_field_flag)
  1197.     {
  1198.       free_motion_params(&p->top   );
  1199.       free_motion_params(&p->bottom);
  1200.     }
  1201.     free(p);
  1202.     p=NULL;
  1203.   }
  1204. }
  1205. /*!
  1206.  ************************************************************************
  1207.  * brief
  1208.  *    Compute co-located motion info
  1209.  *
  1210.  ************************************************************************
  1211.  */
  1212. void compute_colocated(ColocatedParams* p, StorablePicture **listX[6])
  1213. {
  1214.   StorablePicture *fs, *fs_top, *fs_bottom;
  1215.   MotionParams *ftype = NULL;
  1216.   int i,j, ii, jj, jdiv;
  1217.   fs_top = fs_bottom = fs = listX[LIST_1 ][0];
  1218.   if (img->MbaffFrameFlag)
  1219.   {
  1220.     fs_top= listX[LIST_1 + 2][0];
  1221.     fs_bottom= listX[LIST_1 + 4][0];
  1222.   }
  1223.   else
  1224.   {
  1225.     if (img->structure!=FRAME)
  1226.     {
  1227.       if ((img->structure != fs->structure) && (fs->coded_frame))
  1228.       {
  1229.         if (img->structure==TOP_FIELD)
  1230.         {
  1231.           fs_top = fs_bottom = fs = listX[LIST_1 ][0]->top_field;
  1232.         }
  1233.         else
  1234.         {
  1235.           fs_top = fs_bottom = fs = listX[LIST_1 ][0]->bottom_field;
  1236.         }
  1237.       }
  1238.     }
  1239.   }
  1240.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1241.   {    
  1242.     ftype = &p->frame;
  1243.     for (j=0 ; j<fs->size_y/4 ; j++)
  1244.     {
  1245.       jdiv = j>>1;
  1246.       jj = j/2 + 4 * (j/8);
  1247.       for (i=0 ; i<fs->size_x/4 ; i++)
  1248.       {
  1249.         if (img->MbaffFrameFlag && fs->motion.field_frame[j][i])
  1250.         {
  1251.           //! Assign frame buffers for field MBs
  1252.           //! Check whether we should use top or bottom field mvs.
  1253.           //! Depending on the assigned poc values.
  1254.           if (iabs(enc_picture->poc - fs_bottom->poc) > iabs(enc_picture->poc - fs_top->poc) )
  1255.           {
  1256.             ftype->mv[LIST_0][j][i][0]    = fs_top->motion.mv[LIST_0][jdiv][i][0];
  1257.             ftype->mv[LIST_0][j][i][1]    = fs_top->motion.mv[LIST_0][jdiv][i][1] ;
  1258.             ftype->mv[LIST_1][j][i][0]    = fs_top->motion.mv[LIST_1][jdiv][i][0];
  1259.             ftype->mv[LIST_1][j][i][1]    = fs_top->motion.mv[LIST_1][jdiv][i][1] ;
  1260.             ftype->ref_idx[LIST_0][j][i]  = fs_top->motion.ref_idx[LIST_0][jdiv][i];
  1261.             ftype->ref_idx[LIST_1][j][i]  = fs_top->motion.ref_idx[LIST_1][jdiv][i];
  1262.             ftype->ref_pic_id[LIST_0][j][i]   = fs->motion.ref_id[LIST_0][jj][i];
  1263.             ftype->ref_pic_id[LIST_1][j][i]   = fs->motion.ref_id[LIST_1][jj][i];
  1264.             p->is_long_term             = fs_top->is_long_term;
  1265.           }
  1266.           else
  1267.           {
  1268.             ftype->mv[LIST_0][j][i][0]      = fs_bottom->motion.mv[LIST_0][jdiv][i][0];
  1269.             ftype->mv[LIST_0][j][i][1]      = fs_bottom->motion.mv[LIST_0][jdiv][i][1] ;
  1270.             ftype->mv[LIST_1][j][i][0]      = fs_bottom->motion.mv[LIST_1][jdiv][i][0];
  1271.             ftype->mv[LIST_1][j][i][1]      = fs_bottom->motion.mv[LIST_1][jdiv][i][1] ;
  1272.             ftype->ref_idx[LIST_0][j][i]    = fs_bottom->motion.ref_idx[LIST_0][jdiv][i];
  1273.             ftype->ref_idx[LIST_1][j][i]    = fs_bottom->motion.ref_idx[LIST_1][jdiv][i];
  1274.             ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj + 4][i];
  1275.             ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj + 4][i];
  1276.             p->is_long_term             = fs_bottom->is_long_term;
  1277.           }
  1278.         }
  1279.         else
  1280.         {
  1281.           ftype->mv[LIST_0][j][i][0]      = fs->motion.mv[LIST_0][j][i][0];
  1282.           ftype->mv[LIST_0][j][i][1]      = fs->motion.mv[LIST_0][j][i][1] ;
  1283.           ftype->mv[LIST_1][j][i][0]      = fs->motion.mv[LIST_1][j][i][0];
  1284.           ftype->mv[LIST_1][j][i][1]      = fs->motion.mv[LIST_1][j][i][1] ;
  1285.           ftype->ref_idx[LIST_0][j][i]    = fs->motion.ref_idx[LIST_0][j][i];
  1286.           ftype->ref_idx[LIST_1][j][i]    = fs->motion.ref_idx[LIST_1][j][i];
  1287.           ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][j][i];
  1288.           ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][j][i];
  1289.           p->is_long_term             = fs->is_long_term;
  1290.         }
  1291.       }
  1292.     }
  1293.   }
  1294.   //! Generate field MVs from Frame MVs
  1295.   if (img->structure || img->MbaffFrameFlag)
  1296.   {
  1297.     ftype = &p->frame;
  1298.     for (j=0 ; j<fs->size_y/8 ; j++)
  1299.     {
  1300.       jj = RSD(j);
  1301.       for (i=0 ; i<fs->size_x/4 ; i++)
  1302.       {
  1303.         ii = RSD(i);
  1304.         //! Do nothing if macroblock as field coded in MB-AFF
  1305.         if (!img->MbaffFrameFlag )
  1306.         {
  1307.           ftype->mv[LIST_0][j][i][0] = fs->motion.mv[LIST_0][jj][ii][0];
  1308.           ftype->mv[LIST_0][j][i][1] = fs->motion.mv[LIST_0][jj][ii][1];
  1309.           ftype->mv[LIST_1][j][i][0] = fs->motion.mv[LIST_1][jj][ii][0];
  1310.           ftype->mv[LIST_1][j][i][1] = fs->motion.mv[LIST_1][jj][ii][1];
  1311.           // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
  1312.           if (fs->motion.ref_idx[LIST_0][jj][ii] == -1)
  1313.           {
  1314.             ftype->ref_idx   [LIST_0][j][i] = -1;
  1315.             ftype->ref_pic_id[LIST_0][j][i] = -1;
  1316.           }
  1317.           else
  1318.           {
  1319.             ftype->ref_idx   [LIST_0][j][i] = fs->motion.ref_idx[LIST_0][jj][ii] ;
  1320.             ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id [LIST_0][jj][ii];
  1321.           }
  1322.           if (fs->motion.ref_idx[LIST_1][jj][ii] == -1)
  1323.           {
  1324.             ftype->ref_idx   [LIST_1][j][i] = -1;
  1325.             ftype->ref_pic_id[LIST_1][j][i] = -1;
  1326.           }
  1327.           else
  1328.           {
  1329.             ftype->ref_idx   [LIST_1][j][i] = fs->motion.ref_idx[LIST_1][jj][ii];
  1330.             ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id [LIST_1][jj][ii];
  1331.           }
  1332.           p->is_long_term = fs->is_long_term;
  1333.           if (img->direct_spatial_mv_pred_flag == 1)
  1334.           {
  1335.             ftype->moving_block[j][i] =
  1336.               !((!p->is_long_term
  1337.               && ((ftype->ref_idx[LIST_0][j][i] == 0)
  1338.               &&  (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
  1339.               &&  (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
  1340.               || ((ftype->ref_idx[LIST_0][j][i] == -1)
  1341.               &&  (ftype->ref_idx[LIST_1][j][i] == 0)
  1342.               &&  (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
  1343.               &&  (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
  1344.           }
  1345.         }
  1346.         else
  1347.         {
  1348.           p->bottom.mv[LIST_0][j][i][0] = fs_bottom->motion.mv[LIST_0][jj][ii][0];
  1349.           p->bottom.mv[LIST_0][j][i][1] = fs_bottom->motion.mv[LIST_0][jj][ii][1];
  1350.           p->bottom.mv[LIST_1][j][i][0] = fs_bottom->motion.mv[LIST_1][jj][ii][0];
  1351.           p->bottom.mv[LIST_1][j][i][1] = fs_bottom->motion.mv[LIST_1][jj][ii][1];
  1352.           p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->motion.ref_idx[LIST_0][jj][ii];
  1353.           p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->motion.ref_idx[LIST_1][jj][ii];
  1354.           p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->motion.ref_id[LIST_0][jj][ii];
  1355.           p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->motion.ref_id[LIST_1][jj][ii];
  1356.           if (img->direct_spatial_mv_pred_flag == 1)
  1357.           {
  1358.             p->bottom.moving_block[j][i] =
  1359.               !((!fs_bottom->is_long_term
  1360.               && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
  1361.               &&  (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
  1362.               &&  (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
  1363.               || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
  1364.               &&  (p->bottom.ref_idx[LIST_1][j][i] == 0)
  1365.               &&  (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
  1366.               &&  (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
  1367.           }
  1368.           p->top.mv[LIST_0][j][i][0] = fs_top->motion.mv[LIST_0][jj][ii][0];
  1369.           p->top.mv[LIST_0][j][i][1] = fs_top->motion.mv[LIST_0][jj][ii][1];
  1370.           p->top.mv[LIST_1][j][i][0] = fs_top->motion.mv[LIST_1][jj][ii][0];
  1371.           p->top.mv[LIST_1][j][i][1] = fs_top->motion.mv[LIST_1][jj][ii][1];
  1372.           p->top.ref_idx[LIST_0][j][i] = fs_top->motion.ref_idx[LIST_0][jj][ii];
  1373.           p->top.ref_idx[LIST_1][j][i] = fs_top->motion.ref_idx[LIST_1][jj][ii];
  1374.           p->top.ref_pic_id[LIST_0][j][i] = fs_top->motion.ref_id[LIST_0][jj][ii];
  1375.           p->top.ref_pic_id[LIST_1][j][i] = fs_top->motion.ref_id[LIST_1][jj][ii];
  1376.           if (img->direct_spatial_mv_pred_flag == 1)
  1377.           {
  1378.             p->top.moving_block[j][i] =
  1379.               !((!fs_top->is_long_term
  1380.               && ((p->top.ref_idx[LIST_0][j][i] == 0)
  1381.               &&  (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
  1382.               &&  (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
  1383.               || ((p->top.ref_idx[LIST_0][j][i] == -1)
  1384.               &&  (p->top.ref_idx[LIST_1][j][i] == 0)
  1385.               &&  (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
  1386.               &&  (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
  1387.           }
  1388.           if ((img->direct_spatial_mv_pred_flag == 0 ) && !fs->motion.field_frame[2*j][i])
  1389.           {
  1390.             p->top.mv[LIST_0][j][i][1] /= 2;
  1391.             p->top.mv[LIST_1][j][i][1] /= 2;
  1392.             p->bottom.mv[LIST_0][j][i][1] /= 2;
  1393.             p->bottom.mv[LIST_1][j][i][1] /= 2;
  1394.           }
  1395.         }
  1396.       }
  1397.     }
  1398.   }
  1399.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1400.   {
  1401.     //! Use inference flag to remap mvs/references
  1402.     //! Frame with field co-located
  1403.     if (!img->structure)
  1404.     {
  1405.       ftype = &p->frame;
  1406.       for (j=0 ; j < (fs->size_y>>2) ; j++)
  1407.       {
  1408.         jdiv = j>>1;
  1409.         jj = (j>>1) + 4*(j>>3);
  1410.         for (i=0 ; i < (fs->size_x>>2) ; i++)
  1411.         {
  1412.           if (fs->motion.field_frame[j][i])
  1413.           {
  1414.             if (iabs(enc_picture->poc - fs->bottom_field->poc) > iabs(enc_picture->poc - fs->top_field->poc))
  1415.             {
  1416.               ftype->mv[LIST_0][j][i][0] = fs->top_field->motion.mv[LIST_0][jdiv][i][0];
  1417.               ftype->mv[LIST_0][j][i][1] = fs->top_field->motion.mv[LIST_0][jdiv][i][1] ;
  1418.               ftype->mv[LIST_1][j][i][0] = fs->top_field->motion.mv[LIST_1][jdiv][i][0];
  1419.               ftype->mv[LIST_1][j][i][1] = fs->top_field->motion.mv[LIST_1][jdiv][i][1] ;
  1420.               ftype->ref_idx[LIST_0][j][i]  = fs->top_field->motion.ref_idx[LIST_0][jdiv][i];
  1421.               ftype->ref_idx[LIST_1][j][i]  = fs->top_field->motion.ref_idx[LIST_1][jdiv][i];
  1422.               ftype->ref_pic_id[LIST_0][j][i]   = fs->motion.ref_id[LIST_0][jj][i];
  1423.               ftype->ref_pic_id[LIST_1][j][i]   = fs->motion.ref_id[LIST_1][jj][i];
  1424.               p->is_long_term               = fs->top_field->is_long_term;
  1425.             }
  1426.             else
  1427.             {
  1428.               ftype->mv[LIST_0][j][i][0] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][0];
  1429.               ftype->mv[LIST_0][j][i][1] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][1] ;
  1430.               ftype->mv[LIST_1][j][i][0] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][0];
  1431.               ftype->mv[LIST_1][j][i][1] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][1] ;
  1432.               ftype->ref_idx[LIST_0][j][i]  = fs->bottom_field->motion.ref_idx[LIST_0][jdiv][i];
  1433.               ftype->ref_idx[LIST_1][j][i]  = fs->bottom_field->motion.ref_idx[LIST_1][jdiv][i];
  1434.               ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj + 4][i];
  1435.               ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj + 4][i];
  1436.               p->is_long_term             = fs->bottom_field->is_long_term;
  1437.             }
  1438.           }
  1439.         }
  1440.       }
  1441.     }
  1442.   }
  1443.   p->is_long_term = fs->is_long_term;
  1444.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1445.   {
  1446.     ftype = &p->frame;
  1447.     for (j=0 ; j < (fs->size_y>>2) ; j++)
  1448.     {
  1449.       jj = RSD(j);
  1450.       for (i=0 ; i < (fs->size_x>>2) ; i++)
  1451.       {
  1452.         ii = RSD(i);
  1453.         ftype->mv[LIST_0][j][i][0]=ftype->mv[LIST_0][jj][ii][0];
  1454.         ftype->mv[LIST_0][j][i][1]=ftype->mv[LIST_0][jj][ii][1];
  1455.         ftype->mv[LIST_1][j][i][0]=ftype->mv[LIST_1][jj][ii][0];
  1456.         ftype->mv[LIST_1][j][i][1]=ftype->mv[LIST_1][jj][ii][1];
  1457.         ftype->ref_idx[LIST_0][j][i]=ftype->ref_idx[LIST_0][jj][ii];
  1458.         ftype->ref_idx[LIST_1][j][i]=ftype->ref_idx[LIST_1][jj][ii];
  1459.         ftype->ref_pic_id[LIST_0][j][i] = ftype->ref_pic_id[LIST_0][jj][ii];
  1460.         ftype->ref_pic_id[LIST_1][j][i] = ftype->ref_pic_id[LIST_1][jj][ii];
  1461.         if (img->direct_spatial_mv_pred_flag == 1)
  1462.         {
  1463.           ftype->moving_block[j][i]=
  1464.             !((!p->is_long_term
  1465.             && ((ftype->ref_idx[LIST_0][j][i] == 0)
  1466.             &&  (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
  1467.             &&  (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
  1468.             || ((ftype->ref_idx[LIST_0][j][i] == -1)
  1469.             &&  (ftype->ref_idx[LIST_1][j][i] == 0)
  1470.             &&  (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
  1471.             &&  (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
  1472.         }
  1473.       }
  1474.     }
  1475.   }
  1476.   else
  1477.   {
  1478.     ftype = &p->frame;
  1479.     for (j=0 ; j<fs->size_y/4 ; j++)
  1480.     {
  1481.       jj = RSD(j);
  1482.       for (i=0 ; i<fs->size_x/4 ; i++)
  1483.       {
  1484.         ii = RSD(i);
  1485.         //! Use inference flag to remap mvs/references
  1486.         ftype->mv[LIST_0][j][i][0]=fs->motion.mv[LIST_0][j][i][0];
  1487.         ftype->mv[LIST_0][j][i][1]=fs->motion.mv[LIST_0][j][i][1];
  1488.         ftype->mv[LIST_1][j][i][0]=fs->motion.mv[LIST_1][j][i][0];
  1489.         ftype->mv[LIST_1][j][i][1]=fs->motion.mv[LIST_1][j][i][1];
  1490.         ftype->ref_idx[LIST_0][j][i]=fs->motion.ref_idx[LIST_0][j][i];
  1491.         ftype->ref_idx[LIST_1][j][i]=fs->motion.ref_idx[LIST_1][j][i];
  1492.         ftype->ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][j][i];
  1493.         ftype->ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][j][i];
  1494.         if (img->direct_spatial_mv_pred_flag == 1)
  1495.         {
  1496.           ftype->moving_block[j][i]=
  1497.             !((!p->is_long_term
  1498.             && ((ftype->ref_idx[LIST_0][j][i] == 0)
  1499.             &&  (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
  1500.             &&  (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
  1501.             || ((ftype->ref_idx[LIST_0][j][i] == -1)
  1502.             &&  (ftype->ref_idx[LIST_1][j][i] == 0)
  1503.             &&  (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
  1504.             &&  (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
  1505.         }
  1506.       }
  1507.     }
  1508.   }
  1509.   if (img->direct_spatial_mv_pred_flag ==0)
  1510.   {
  1511.     ftype = &p->frame;
  1512.     for (j=0 ; j<fs->size_y/4 ; j++)
  1513.     {
  1514.       for (i=0 ; i<fs->size_x/4 ; i++)
  1515.       {
  1516.         if ((!img->MbaffFrameFlag &&!img->structure && fs->motion.field_frame[j][i]) || (img->MbaffFrameFlag && fs->motion.field_frame[j][i]))
  1517.         {
  1518.           ftype->mv[LIST_0][j][i][1] *= 2;
  1519.           ftype->mv[LIST_1][j][i][1] *= 2;
  1520.         }
  1521.         else  if (img->structure && !fs->motion.field_frame[j][i])
  1522.         {
  1523.           ftype->mv[LIST_0][j][i][1] /= 2;
  1524.           ftype->mv[LIST_1][j][i][1] /= 2;
  1525.         }
  1526.       }
  1527.     }
  1528.     for (j=0; j<2 + (img->MbaffFrameFlag * 4);j+=2)
  1529.     {
  1530.       for (i=0; i<listXsize[j];i++)
  1531.       {
  1532.         int prescale, iTRb, iTRp;
  1533.         if (j==0)
  1534.         {
  1535.           iTRb = iClip3( -128, 127, enc_picture->poc - listX[LIST_0 + j][i]->poc );
  1536.         }
  1537.         else if (j == 2)
  1538.         {
  1539.           iTRb = iClip3( -128, 127, enc_picture->top_poc - listX[LIST_0 + j][i]->poc );
  1540.         }
  1541.         else
  1542.         {
  1543.           iTRb = iClip3( -128, 127, enc_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
  1544.         }
  1545.         iTRp = iClip3( -128, 127,  listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
  1546.         if (iTRp!=0)
  1547.         {
  1548.           prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
  1549.           img->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
  1550.         }
  1551.         else
  1552.         {
  1553.           img->mvscale[j][i] = 9999;
  1554.         }
  1555.       }
  1556.     }
  1557.   }
  1558. }
  1559. /*!
  1560.  ************************************************************************
  1561.  * brief
  1562.  *    Compute co-located motion info
  1563.  *    for 4:4:4 Independent mode
  1564.  *
  1565.  ************************************************************************
  1566.  */
  1567. void compute_colocated_JV(ColocatedParams* p, StorablePicture **listX[6])
  1568. {
  1569.   StorablePicture *fs, *fs_top, *fs_bottom;
  1570.   MotionParams *ftype;
  1571.   int i,j, ii, jj, jdiv;
  1572.   int np = img->colour_plane_id;
  1573.   fs_top = fs_bottom = fs = listX[LIST_1 ][0];
  1574.   if (img->MbaffFrameFlag)
  1575.   {
  1576.     fs_top= listX[LIST_1 + 2][0];
  1577.     fs_bottom= listX[LIST_1 + 4][0];
  1578.   }
  1579.   else
  1580.   {
  1581.     if (img->structure!=FRAME)
  1582.     {
  1583.       if ((img->structure != fs->structure) && (fs->coded_frame))
  1584.       {
  1585.         if (img->structure==TOP_FIELD)
  1586.         {
  1587.           fs_top = fs_bottom = fs = listX[LIST_1 ][0]->top_field;
  1588.         }
  1589.         else
  1590.         {
  1591.           fs_top = fs_bottom = fs = listX[LIST_1 ][0]->bottom_field;
  1592.         }
  1593.       }
  1594.     }
  1595.   }
  1596.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1597.   {
  1598.     ftype = &p->frame;
  1599.     for (j=0 ; j<fs->size_y/4 ; j++)
  1600.     {
  1601.       jdiv = j/2;
  1602.       jj = j/2 + 4 * (j/8);
  1603.       for (i=0 ; i<fs->size_x/4 ; i++)
  1604.       {
  1605.         if (img->MbaffFrameFlag && fs->motion.field_frame[j][i])
  1606.         {
  1607.           //! Assign frame buffers for field MBs
  1608.           //! Check whether we should use top or bottom field mvs.
  1609.           //! Depending on the assigned poc values.
  1610.           if (iabs(enc_picture->poc - fs_bottom->poc) > iabs(enc_picture->poc - fs_top->poc) )
  1611.           {
  1612.             ftype->mv[LIST_0][j][i][0]    = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1613.             ftype->mv[LIST_0][j][i][1]    = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1614.             ftype->mv[LIST_1][j][i][0]    = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1615.             ftype->mv[LIST_1][j][i][1]    = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1616.             ftype->ref_idx[LIST_0][j][i]  = fs_top->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1617.             ftype->ref_idx[LIST_1][j][i]  = fs_top->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1618.             ftype->ref_pic_id[LIST_0][j][i]   = fs->JVmotion[np].ref_id[LIST_0][jj][i];
  1619.             ftype->ref_pic_id[LIST_1][j][i]   = fs->JVmotion[np].ref_id[LIST_1][jj][i];
  1620.             p->is_long_term             = fs_top->is_long_term;
  1621.           }
  1622.           else
  1623.           {
  1624.             ftype->mv[LIST_0][j][i][0]      = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1625.             ftype->mv[LIST_0][j][i][1]      = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1626.             ftype->mv[LIST_1][j][i][0]      = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1627.             ftype->mv[LIST_1][j][i][1]      = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1628.             ftype->ref_idx[LIST_0][j][i]    = fs_bottom->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1629.             ftype->ref_idx[LIST_1][j][i]    = fs_bottom->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1630.             ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
  1631.             ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
  1632.             p->is_long_term             = fs_bottom->is_long_term;
  1633.           }
  1634.         }
  1635.         else
  1636.         {
  1637.           ftype->mv[LIST_0][j][i][0]      = fs->JVmotion[np].mv[LIST_0][j][i][0];
  1638.           ftype->mv[LIST_0][j][i][1]      = fs->JVmotion[np].mv[LIST_0][j][i][1] ;
  1639.           ftype->mv[LIST_1][j][i][0]      = fs->JVmotion[np].mv[LIST_1][j][i][0];
  1640.           ftype->mv[LIST_1][j][i][1]      = fs->JVmotion[np].mv[LIST_1][j][i][1] ;
  1641.           ftype->ref_idx[LIST_0][j][i]    = fs->JVmotion[np].ref_idx[LIST_0][j][i];
  1642.           ftype->ref_idx[LIST_1][j][i]    = fs->JVmotion[np].ref_idx[LIST_1][j][i];
  1643.           ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
  1644.           ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
  1645.           p->is_long_term             = fs->is_long_term;
  1646.         }
  1647.       }
  1648.     }
  1649.   }
  1650.   //! Generate field MVs from Frame MVs
  1651.   if (img->structure || img->MbaffFrameFlag)
  1652.   {
  1653.     ftype = &p->frame;
  1654.     for (j=0 ; j<fs->size_y/8 ; j++)
  1655.     {
  1656.       jj = RSD(j);
  1657.       for (i=0 ; i<fs->size_x/4 ; i++)
  1658.       {
  1659.         ii = RSD(i);
  1660.         //! Do nothing if macroblock as field coded in MB-AFF
  1661.         if (!img->MbaffFrameFlag )
  1662.         {
  1663.           ftype->mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][jj][ii][0];
  1664.           ftype->mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][jj][ii][1];
  1665.           ftype->mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][jj][ii][0];
  1666.           ftype->mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][jj][ii][1];
  1667.           // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
  1668.           if (fs->motion.ref_idx[LIST_0][jj][ii] == -1)
  1669.           {
  1670.             ftype->ref_idx   [LIST_0][j][i] = -1;
  1671.             ftype->ref_pic_id[LIST_0][j][i] = -1;
  1672.           }
  1673.           else
  1674.           {
  1675.             ftype->ref_idx   [LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][jj][ii] ;
  1676.             ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj][ii];
  1677.           }
  1678.           if (fs->motion.ref_idx[LIST_1][jj][ii] == -1)
  1679.           {
  1680.             ftype->ref_idx   [LIST_1][j][i] = -1;
  1681.             ftype->ref_pic_id[LIST_1][j][i] = -1;
  1682.           }
  1683.           else
  1684.           {
  1685.             ftype->ref_idx   [LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][jj][ii];
  1686.             ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj][ii];
  1687.           }
  1688.           p->is_long_term = fs->is_long_term;
  1689.           if (img->direct_spatial_mv_pred_flag == 1)
  1690.           {
  1691.             ftype->moving_block[j][i] =
  1692.               !((!p->is_long_term
  1693.               && ((ftype->ref_idx[LIST_0][j][i] == 0)
  1694.               &&  (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
  1695.               &&  (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
  1696.               || ((ftype->ref_idx[LIST_0][j][i] == -1)
  1697.               &&  (ftype->ref_idx[LIST_1][j][i] == 0)
  1698.               &&  (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
  1699.               &&  (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
  1700.           }
  1701.         }
  1702.         else
  1703.         {
  1704.           p->bottom.mv[LIST_0][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][0];
  1705.           p->bottom.mv[LIST_0][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][1];
  1706.           p->bottom.mv[LIST_1][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][0];
  1707.           p->bottom.mv[LIST_1][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][1];
  1708.           p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_0][jj][ii];
  1709.           p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_1][jj][ii];
  1710.           p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_0][jj][ii];
  1711.           p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_1][jj][ii];
  1712.           if (img->direct_spatial_mv_pred_flag == 1)
  1713.           {
  1714.             p->bottom.moving_block[j][i] =
  1715.               !((!fs_bottom->is_long_term
  1716.               && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
  1717.               &&  (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
  1718.               &&  (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
  1719.               || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
  1720.               &&  (p->bottom.ref_idx[LIST_1][j][i] == 0)
  1721.               &&  (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
  1722.               &&  (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
  1723.           }
  1724.           p->top.mv[LIST_0][j][i][0] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][0];
  1725.           p->top.mv[LIST_0][j][i][1] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][1];
  1726.           p->top.mv[LIST_1][j][i][0] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][0];
  1727.           p->top.mv[LIST_1][j][i][1] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][1];
  1728.           p->top.ref_idx[LIST_0][j][i] = fs_top->JVmotion[np].ref_idx[LIST_0][jj][ii];
  1729.           p->top.ref_idx[LIST_1][j][i] = fs_top->JVmotion[np].ref_idx[LIST_1][jj][ii];
  1730.           p->top.ref_pic_id[LIST_0][j][i] = fs_top->JVmotion[np].ref_id[LIST_0][jj][ii];
  1731.           p->top.ref_pic_id[LIST_1][j][i] = fs_top->JVmotion[np].ref_id[LIST_1][jj][ii];
  1732.           if (img->direct_spatial_mv_pred_flag == 1)
  1733.           {
  1734.             p->top.moving_block[j][i] =
  1735.               !((!fs_top->is_long_term
  1736.               && ((p->top.ref_idx[LIST_0][j][i] == 0)
  1737.               &&  (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
  1738.               &&  (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
  1739.               || ((p->top.ref_idx[LIST_0][j][i] == -1)
  1740.               &&  (p->top.ref_idx[LIST_1][j][i] == 0)
  1741.               &&  (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
  1742.               &&  (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
  1743.           }
  1744.           if ((img->direct_spatial_mv_pred_flag == 0 ) && !fs->motion.field_frame[2*j][i])
  1745.           {
  1746.             p->top.mv[LIST_0][j][i][1] /= 2;
  1747.             p->top.mv[LIST_1][j][i][1] /= 2;
  1748.             p->bottom.mv[LIST_0][j][i][1] /= 2;
  1749.             p->bottom.mv[LIST_1][j][i][1] /= 2;
  1750.           }
  1751.         }
  1752.       }
  1753.     }
  1754.   }
  1755.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1756.   {
  1757.     //! Use inference flag to remap mvs/references
  1758.     //! Frame with field co-located
  1759.     if (!img->structure)
  1760.     {
  1761.       ftype = &p->frame;
  1762.       for (j=0 ; j < (fs->size_y>>2) ; j++)
  1763.       {
  1764.         jdiv = j>>1;
  1765.         jj = (j>>1) + 4*(j>>3);
  1766.         for (i=0 ; i < (fs->size_x>>2) ; i++)
  1767.         {
  1768.           if (fs->motion.field_frame[j][i])
  1769.           {
  1770.             if (iabs(enc_picture->poc - fs->bottom_field->poc) > iabs(enc_picture->poc - fs->top_field->poc))
  1771.             {
  1772.               ftype->mv[LIST_0][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1773.               ftype->mv[LIST_0][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1774.               ftype->mv[LIST_1][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1775.               ftype->mv[LIST_1][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1776.               ftype->ref_idx[LIST_0][j][i]  = fs->top_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1777.               ftype->ref_idx[LIST_1][j][i]  = fs->top_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1778.               ftype->ref_pic_id[LIST_0][j][i]   = fs->JVmotion[np].ref_id[LIST_0][jj][i];
  1779.               ftype->ref_pic_id[LIST_1][j][i]   = fs->JVmotion[np].ref_id[LIST_1][jj][i];
  1780.               p->is_long_term               = fs->top_field->is_long_term;
  1781.             }
  1782.             else
  1783.             {
  1784.               ftype->mv[LIST_0][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1785.               ftype->mv[LIST_0][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1786.               ftype->mv[LIST_1][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1787.               ftype->mv[LIST_1][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1788.               ftype->ref_idx[LIST_0][j][i]  = fs->bottom_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1789.               ftype->ref_idx[LIST_1][j][i]  = fs->bottom_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1790.               ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
  1791.               ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
  1792.               p->is_long_term             = fs->bottom_field->is_long_term;
  1793.             }
  1794.           }
  1795.         }
  1796.       }
  1797.     }
  1798.   }
  1799.   p->is_long_term = fs->is_long_term;
  1800.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1801.   {
  1802.     ftype = &p->frame;
  1803.     for (j=0 ; j < (fs->size_y>>2) ; j++)
  1804.     {
  1805.       jj = RSD(j);
  1806.       for (i=0 ; i < (fs->size_x>>2) ; i++)
  1807.       {
  1808.         ii = RSD(i);
  1809.         ftype->mv[LIST_0][j][i][0]=ftype->mv[LIST_0][jj][ii][0];
  1810.         ftype->mv[LIST_0][j][i][1]=ftype->mv[LIST_0][jj][ii][1];
  1811.         ftype->mv[LIST_1][j][i][0]=ftype->mv[LIST_1][jj][ii][0];
  1812.         ftype->mv[LIST_1][j][i][1]=ftype->mv[LIST_1][jj][ii][1];
  1813.         ftype->ref_idx[LIST_0][j][i]=ftype->ref_idx[LIST_0][jj][ii];
  1814.         ftype->ref_idx[LIST_1][j][i]=ftype->ref_idx[LIST_1][jj][ii];
  1815.         ftype->ref_pic_id[LIST_0][j][i] = ftype->ref_pic_id[LIST_0][jj][ii];
  1816.         ftype->ref_pic_id[LIST_1][j][i] = ftype->ref_pic_id[LIST_1][jj][ii];
  1817.         if (img->direct_spatial_mv_pred_flag == 1)
  1818.         {
  1819.           ftype->moving_block[j][i]=
  1820.             !((!p->is_long_term
  1821.             && ((ftype->ref_idx[LIST_0][j][i] == 0)
  1822.             &&  (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
  1823.             &&  (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
  1824.             || ((ftype->ref_idx[LIST_0][j][i] == -1)
  1825.             &&  (ftype->ref_idx[LIST_1][j][i] == 0)
  1826.             &&  (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
  1827.             &&  (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
  1828.         }
  1829.       }
  1830.     }
  1831.   }
  1832.   else
  1833.   {
  1834.     ftype = &p->frame;
  1835.     for (j=0 ; j<fs->size_y/4 ; j++)
  1836.     {
  1837.       jj = RSD(j);
  1838.       for (i=0 ; i<fs->size_x/4 ; i++)
  1839.       {
  1840.         ii = RSD(i);
  1841.         //! Use inference flag to remap mvs/references
  1842.         ftype->mv[LIST_0][j][i][0]=fs->JVmotion[np].mv[LIST_0][j][i][0];
  1843.         ftype->mv[LIST_0][j][i][1]=fs->JVmotion[np].mv[LIST_0][j][i][1];
  1844.         ftype->mv[LIST_1][j][i][0]=fs->JVmotion[np].mv[LIST_1][j][i][0];
  1845.         ftype->mv[LIST_1][j][i][1]=fs->JVmotion[np].mv[LIST_1][j][i][1];
  1846.         ftype->ref_idx[LIST_0][j][i]=fs->JVmotion[np].ref_idx[LIST_0][j][i];
  1847.         ftype->ref_idx[LIST_1][j][i]=fs->JVmotion[np].ref_idx[LIST_1][j][i];
  1848.         ftype->ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
  1849.         ftype->ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
  1850.         if (img->direct_spatial_mv_pred_flag == 1)
  1851.         {
  1852.           ftype->moving_block[j][i]=
  1853.             !((!p->is_long_term
  1854.             && ((ftype->ref_idx[LIST_0][j][i] == 0)
  1855.             &&  (iabs(ftype->mv[LIST_0][j][i][0])>>1 == 0)
  1856.             &&  (iabs(ftype->mv[LIST_0][j][i][1])>>1 == 0)))
  1857.             || ((ftype->ref_idx[LIST_0][j][i] == -1)
  1858.             &&  (ftype->ref_idx[LIST_1][j][i] == 0)
  1859.             &&  (iabs(ftype->mv[LIST_1][j][i][0])>>1 == 0)
  1860.             &&  (iabs(ftype->mv[LIST_1][j][i][1])>>1 == 0)));
  1861.         }
  1862.       }
  1863.     }
  1864.   }
  1865.   if (img->direct_spatial_mv_pred_flag ==0)
  1866.   {
  1867.     ftype = &p->frame;
  1868.     for (j=0 ; j<fs->size_y/4 ; j++)
  1869.     {
  1870.       for (i=0 ; i<fs->size_x/4 ; i++)
  1871.       {
  1872.         if ((!img->MbaffFrameFlag &&!img->structure && fs->motion.field_frame[j][i]) || (img->MbaffFrameFlag && fs->motion.field_frame[j][i]))
  1873.         {
  1874.           ftype->mv[LIST_0][j][i][1] *= 2;
  1875.           ftype->mv[LIST_1][j][i][1] *= 2;
  1876.         }
  1877.         else  if (img->structure && !fs->motion.field_frame[j][i])
  1878.         {
  1879.           ftype->mv[LIST_0][j][i][1] /= 2;
  1880.           ftype->mv[LIST_1][j][i][1] /= 2;
  1881.         }
  1882.       }
  1883.     }
  1884.     for (j=0; j<2 + (img->MbaffFrameFlag * 4);j+=2)
  1885.     {
  1886.       for (i=0; i<listXsize[j];i++)
  1887.       {
  1888.         int prescale, iTRb, iTRp;
  1889.         if (j==0)
  1890.         {
  1891.           iTRb = iClip3( -128, 127, enc_picture->poc - listX[LIST_0 + j][i]->poc );
  1892.         }
  1893.         else if (j == 2)
  1894.         {
  1895.           iTRb = iClip3( -128, 127, enc_picture->top_poc - listX[LIST_0 + j][i]->poc );
  1896.         }
  1897.         else
  1898.         {
  1899.           iTRb = iClip3( -128, 127, enc_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
  1900.         }
  1901.         iTRp = iClip3( -128, 127,  listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
  1902.         if (iTRp!=0)
  1903.         {
  1904.           prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
  1905.           img->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
  1906.         }
  1907.         else
  1908.         {
  1909.           img->mvscale[j][i] = 9999;
  1910.         }
  1911.       }
  1912.     }
  1913.   }
  1914. }
  1915. /*!
  1916.  ************************************************************************
  1917.  * brief
  1918.  *    Copy StorablePicture parameters
  1919.  *    for 4:4:4 Independent mode
  1920.  *
  1921.  ************************************************************************
  1922.  */
  1923. void copy_storable_param_JV( int nplane, StorablePicture *d, StorablePicture *s )
  1924. {
  1925.   // copy ref_idx
  1926.   memcpy( d->JVmotion[nplane].ref_idx[0][0], s->motion.ref_idx[0][0], 2 * (img->height/BLOCK_SIZE) * (img->width/BLOCK_SIZE) * sizeof(byte) );
  1927.   // copy ref_pic_id
  1928.   memcpy( d->JVmotion[nplane].ref_pic_id[0][0], s->motion.ref_pic_id[0][0], 6 * (img->height/BLOCK_SIZE) * (img->width/BLOCK_SIZE) * sizeof(int64) );
  1929.   // copy motion.ref_id
  1930.   memcpy( d->JVmotion[nplane].ref_id[0][0], s->motion.ref_id[0][0], 6 * (img->width/BLOCK_SIZE) * (img->height/BLOCK_SIZE) * sizeof(int64));
  1931.   // copy mv
  1932.   memcpy( d->JVmotion[nplane].mv[0][0][0], s->motion.mv[0][0][0], 2 * img->height/BLOCK_SIZE * img->width/BLOCK_SIZE * sizeof(short)*2 );
  1933. }